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> |
| 13 | #include <linux/seq_file.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/kmod.h> |
| 16 | #include <linux/kobj_map.h> |
Christoph Hellwig | 2ef4163 | 2005-05-05 16:15:59 -0700 | [diff] [blame] | 17 | #include <linux/buffer_head.h> |
Jes Sorensen | 58383af | 2006-02-06 14:12:43 -0800 | [diff] [blame] | 18 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Adrian Bunk | ff88972c | 2008-03-04 11:23:45 +0100 | [diff] [blame] | 20 | #include "blk.h" |
| 21 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 22 | static DEFINE_MUTEX(block_class_lock); |
| 23 | #ifndef CONFIG_SYSFS_DEPRECATED |
| 24 | struct kobject *block_depr; |
| 25 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Adrian Bunk | 1826ead | 2008-03-04 11:23:46 +0100 | [diff] [blame] | 27 | static struct device_type disk_type; |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | /* |
| 30 | * Can be deleted altogether. Later. |
| 31 | * |
| 32 | */ |
| 33 | static struct blk_major_name { |
| 34 | struct blk_major_name *next; |
| 35 | int major; |
| 36 | char name[16]; |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 37 | } *major_names[BLKDEV_MAJOR_HASH_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | /* index in the above - for now: assume no multimajor ranges */ |
| 40 | static inline int major_to_index(int major) |
| 41 | { |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 42 | return major % BLKDEV_MAJOR_HASH_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 45 | #ifdef CONFIG_PROC_FS |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 46 | void blkdev_show(struct seq_file *seqf, off_t offset) |
Neil Horman | 7170be5 | 2006-01-14 13:20:38 -0800 | [diff] [blame] | 47 | { |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 48 | struct blk_major_name *dp; |
Neil Horman | 7170be5 | 2006-01-14 13:20:38 -0800 | [diff] [blame] | 49 | |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 50 | if (offset < BLKDEV_MAJOR_HASH_SIZE) { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 51 | mutex_lock(&block_class_lock); |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 52 | for (dp = major_names[offset]; dp; dp = dp->next) |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 53 | seq_printf(seqf, "%3d %s\n", dp->major, dp->name); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 54 | mutex_unlock(&block_class_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 57 | #endif /* CONFIG_PROC_FS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | int register_blkdev(unsigned int major, const char *name) |
| 60 | { |
| 61 | struct blk_major_name **n, *p; |
| 62 | int index, ret = 0; |
| 63 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 64 | mutex_lock(&block_class_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | /* temporary */ |
| 67 | if (major == 0) { |
| 68 | for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) { |
| 69 | if (major_names[index] == NULL) |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | if (index == 0) { |
| 74 | printk("register_blkdev: failed to get major for %s\n", |
| 75 | name); |
| 76 | ret = -EBUSY; |
| 77 | goto out; |
| 78 | } |
| 79 | major = index; |
| 80 | ret = major; |
| 81 | } |
| 82 | |
| 83 | p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL); |
| 84 | if (p == NULL) { |
| 85 | ret = -ENOMEM; |
| 86 | goto out; |
| 87 | } |
| 88 | |
| 89 | p->major = major; |
| 90 | strlcpy(p->name, name, sizeof(p->name)); |
| 91 | p->next = NULL; |
| 92 | index = major_to_index(major); |
| 93 | |
| 94 | for (n = &major_names[index]; *n; n = &(*n)->next) { |
| 95 | if ((*n)->major == major) |
| 96 | break; |
| 97 | } |
| 98 | if (!*n) |
| 99 | *n = p; |
| 100 | else |
| 101 | ret = -EBUSY; |
| 102 | |
| 103 | if (ret < 0) { |
| 104 | printk("register_blkdev: cannot get major %d for %s\n", |
| 105 | major, name); |
| 106 | kfree(p); |
| 107 | } |
| 108 | out: |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 109 | mutex_unlock(&block_class_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | return ret; |
| 111 | } |
| 112 | |
| 113 | EXPORT_SYMBOL(register_blkdev); |
| 114 | |
Akinobu Mita | f448024 | 2007-07-17 04:03:47 -0700 | [diff] [blame] | 115 | void unregister_blkdev(unsigned int major, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
| 117 | struct blk_major_name **n; |
| 118 | struct blk_major_name *p = NULL; |
| 119 | int index = major_to_index(major); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 121 | mutex_lock(&block_class_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | for (n = &major_names[index]; *n; n = &(*n)->next) |
| 123 | if ((*n)->major == major) |
| 124 | break; |
Akinobu Mita | 294462a | 2007-07-17 04:03:45 -0700 | [diff] [blame] | 125 | if (!*n || strcmp((*n)->name, name)) { |
| 126 | WARN_ON(1); |
Akinobu Mita | 294462a | 2007-07-17 04:03:45 -0700 | [diff] [blame] | 127 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | p = *n; |
| 129 | *n = p->next; |
| 130 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 131 | mutex_unlock(&block_class_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | kfree(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | EXPORT_SYMBOL(unregister_blkdev); |
| 136 | |
| 137 | static struct kobj_map *bdev_map; |
| 138 | |
| 139 | /* |
| 140 | * Register device numbers dev..(dev+range-1) |
| 141 | * range must be nonzero |
| 142 | * The hash chain is sorted on range, so that subranges can override. |
| 143 | */ |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 144 | 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] | 145 | struct kobject *(*probe)(dev_t, int *, void *), |
| 146 | int (*lock)(dev_t, void *), void *data) |
| 147 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 148 | kobj_map(bdev_map, devt, range, module, probe, lock, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | EXPORT_SYMBOL(blk_register_region); |
| 152 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 153 | void blk_unregister_region(dev_t devt, unsigned long range) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 155 | kobj_unmap(bdev_map, devt, range); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | EXPORT_SYMBOL(blk_unregister_region); |
| 159 | |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 160 | static struct kobject *exact_match(dev_t devt, int *partno, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
| 162 | struct gendisk *p = data; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 163 | |
| 164 | return &p->dev.kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 167 | static int exact_lock(dev_t devt, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
| 169 | struct gendisk *p = data; |
| 170 | |
| 171 | if (!get_disk(p)) |
| 172 | return -1; |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * add_disk - add partitioning information to kernel list |
| 178 | * @disk: per-device partitioning information |
| 179 | * |
| 180 | * This function registers the partitioning information in @disk |
| 181 | * with the kernel. |
| 182 | */ |
| 183 | void add_disk(struct gendisk *disk) |
| 184 | { |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 185 | struct backing_dev_info *bdi; |
Greg Kroah-Hartman | 6ffeea7 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 186 | int retval; |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | disk->flags |= GENHD_FL_UP; |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame^] | 189 | disk->dev.devt = MKDEV(disk->major, disk->first_minor); |
| 190 | blk_register_region(disk_devt(disk), disk->minors, NULL, |
| 191 | exact_match, exact_lock, disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | register_disk(disk); |
| 193 | blk_register_queue(disk); |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 194 | |
| 195 | bdi = &disk->queue->backing_dev_info; |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame^] | 196 | bdi_register_dev(bdi, disk_devt(disk)); |
Greg Kroah-Hartman | 6ffeea7 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 197 | retval = sysfs_create_link(&disk->dev.kobj, &bdi->dev->kobj, "bdi"); |
| 198 | WARN_ON(retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | EXPORT_SYMBOL(add_disk); |
| 202 | EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */ |
| 203 | |
| 204 | void unlink_gendisk(struct gendisk *disk) |
| 205 | { |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 206 | sysfs_remove_link(&disk->dev.kobj, "bdi"); |
| 207 | bdi_unregister(&disk->queue->backing_dev_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | blk_unregister_queue(disk); |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame^] | 209 | blk_unregister_region(disk_devt(disk), disk->minors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | /** |
| 213 | * get_gendisk - get partitioning information for a given device |
Randy Dunlap | 710027a | 2008-08-19 20:13:11 +0200 | [diff] [blame] | 214 | * @devt: device to get partitioning information for |
| 215 | * @part: returned partition index |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | * |
| 217 | * This function gets the structure containing partitioning |
Randy Dunlap | 710027a | 2008-08-19 20:13:11 +0200 | [diff] [blame] | 218 | * information for the given device @devt. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | */ |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 220 | struct gendisk *get_gendisk(dev_t devt, int *partno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 222 | struct kobject *kobj = kobj_lookup(bdev_map, devt, partno); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 223 | struct device *dev = kobj_to_dev(kobj); |
| 224 | |
| 225 | return kobj ? dev_to_disk(dev) : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame^] | 228 | /** |
| 229 | * bdget_disk - do bdget() by gendisk and partition number |
| 230 | * @disk: gendisk of interest |
| 231 | * @partno: partition number |
| 232 | * |
| 233 | * Find partition @partno from @disk, do bdget() on it. |
| 234 | * |
| 235 | * CONTEXT: |
| 236 | * Don't care. |
| 237 | * |
| 238 | * RETURNS: |
| 239 | * Resulting block_device on success, NULL on failure. |
| 240 | */ |
| 241 | extern struct block_device *bdget_disk(struct gendisk *disk, int partno) |
| 242 | { |
| 243 | dev_t devt = MKDEV(0, 0); |
| 244 | |
| 245 | if (partno == 0) |
| 246 | devt = disk_devt(disk); |
| 247 | else { |
| 248 | struct hd_struct *part = disk->part[partno - 1]; |
| 249 | |
| 250 | if (part && part->nr_sects) |
| 251 | devt = part_devt(part); |
| 252 | } |
| 253 | |
| 254 | if (likely(devt != MKDEV(0, 0))) |
| 255 | return bdget(devt); |
| 256 | return NULL; |
| 257 | } |
| 258 | EXPORT_SYMBOL(bdget_disk); |
| 259 | |
Dave Gilbert | dd2a345 | 2007-05-09 02:33:24 -0700 | [diff] [blame] | 260 | /* |
| 261 | * print a full list of all partitions - intended for places where the root |
| 262 | * filesystem can't be mounted and thus to give the victim some idea of what |
| 263 | * went wrong |
| 264 | */ |
| 265 | void __init printk_all_partitions(void) |
| 266 | { |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 267 | struct class_dev_iter iter; |
| 268 | struct device *dev; |
| 269 | |
| 270 | class_dev_iter_init(&iter, &block_class, NULL, &disk_type); |
| 271 | while ((dev = class_dev_iter_next(&iter))) { |
| 272 | struct gendisk *disk = dev_to_disk(dev); |
| 273 | char buf[BDEVNAME_SIZE]; |
| 274 | int n; |
| 275 | |
| 276 | /* |
| 277 | * Don't show empty devices or things that have been |
| 278 | * surpressed |
| 279 | */ |
| 280 | if (get_capacity(disk) == 0 || |
| 281 | (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)) |
| 282 | continue; |
| 283 | |
| 284 | /* |
| 285 | * Note, unlike /proc/partitions, I am showing the |
| 286 | * numbers in hex - the same format as the root= |
| 287 | * option takes. |
| 288 | */ |
| 289 | printk("%02x%02x %10llu %s", |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame^] | 290 | MAJOR(disk_devt(disk)), MINOR(disk_devt(disk)), |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 291 | (unsigned long long)get_capacity(disk) >> 1, |
| 292 | disk_name(disk, 0, buf)); |
| 293 | if (disk->driverfs_dev != NULL && |
| 294 | disk->driverfs_dev->driver != NULL) |
| 295 | printk(" driver: %s\n", |
| 296 | disk->driverfs_dev->driver->name); |
| 297 | else |
| 298 | printk(" (driver?)\n"); |
| 299 | |
| 300 | /* now show the partitions */ |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame^] | 301 | for (n = 0; n < disk_max_parts(disk); ++n) { |
| 302 | struct hd_struct *part = disk->part[n]; |
| 303 | |
| 304 | if (!part || !part->nr_sects) |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 305 | continue; |
| 306 | printk(" %02x%02x %10llu %s\n", |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame^] | 307 | MAJOR(part_devt(part)), MINOR(part_devt(part)), |
| 308 | (unsigned long long)part->nr_sects >> 1, |
| 309 | disk_name(disk, part->partno, buf)); |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | class_dev_iter_exit(&iter); |
Dave Gilbert | dd2a345 | 2007-05-09 02:33:24 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | #ifdef CONFIG_PROC_FS |
| 316 | /* iterator */ |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 317 | 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] | 318 | { |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 319 | loff_t skip = *pos; |
| 320 | struct class_dev_iter *iter; |
| 321 | struct device *dev; |
Greg Kroah-Hartman | 68c4d4a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 322 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 323 | iter = kmalloc(GFP_KERNEL, sizeof(*iter)); |
| 324 | if (!iter) |
| 325 | return ERR_PTR(-ENOMEM); |
| 326 | |
| 327 | seqf->private = iter; |
| 328 | class_dev_iter_init(iter, &block_class, NULL, &disk_type); |
| 329 | do { |
| 330 | dev = class_dev_iter_next(iter); |
| 331 | if (!dev) |
| 332 | return NULL; |
| 333 | } while (skip--); |
| 334 | |
| 335 | return dev_to_disk(dev); |
Greg Kroah-Hartman | 68c4d4a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 336 | } |
| 337 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 338 | 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] | 339 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 340 | struct device *dev; |
Greg Kroah-Hartman | 66c64af | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 341 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 342 | (*pos)++; |
| 343 | dev = class_dev_iter_next(seqf->private); |
Tejun Heo | 2ac3cee | 2008-09-03 08:53:37 +0200 | [diff] [blame] | 344 | if (dev) |
Greg Kroah-Hartman | 68c4d4a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 345 | return dev_to_disk(dev); |
Tejun Heo | 2ac3cee | 2008-09-03 08:53:37 +0200 | [diff] [blame] | 346 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | return NULL; |
| 348 | } |
| 349 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 350 | static void disk_seqf_stop(struct seq_file *seqf, void *v) |
Greg Kroah-Hartman | 27f3025 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 351 | { |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 352 | struct class_dev_iter *iter = seqf->private; |
Greg Kroah-Hartman | 27f3025 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 353 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 354 | /* stop is called even after start failed :-( */ |
| 355 | if (iter) { |
| 356 | class_dev_iter_exit(iter); |
| 357 | kfree(iter); |
Kay Sievers | 5c0ef6d | 2008-08-16 14:30:30 +0200 | [diff] [blame] | 358 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 361 | static void *show_partition_start(struct seq_file *seqf, loff_t *pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | { |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 363 | static void *p; |
| 364 | |
| 365 | p = disk_seqf_start(seqf, pos); |
| 366 | if (!IS_ERR(p) && p) |
| 367 | seq_puts(seqf, "major minor #blocks name\n\n"); |
| 368 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 371 | static int show_partition(struct seq_file *seqf, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | { |
| 373 | struct gendisk *sgp = v; |
| 374 | int n; |
| 375 | char buf[BDEVNAME_SIZE]; |
| 376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | /* Don't show non-partitionable removeable devices or empty devices */ |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame^] | 378 | if (!get_capacity(sgp) || (!disk_max_parts(sgp) && |
| 379 | (sgp->flags & GENHD_FL_REMOVABLE))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | return 0; |
| 381 | if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO) |
| 382 | return 0; |
| 383 | |
| 384 | /* show the full disk and all non-0 size partitions of it */ |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 385 | seq_printf(seqf, "%4d %4d %10llu %s\n", |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame^] | 386 | MAJOR(disk_devt(sgp)), MINOR(disk_devt(sgp)), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | (unsigned long long)get_capacity(sgp) >> 1, |
| 388 | disk_name(sgp, 0, buf)); |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame^] | 389 | for (n = 0; n < disk_max_parts(sgp); n++) { |
| 390 | struct hd_struct *part = sgp->part[n]; |
| 391 | if (!part) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | continue; |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame^] | 393 | if (part->nr_sects == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | continue; |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 395 | seq_printf(seqf, "%4d %4d %10llu %s\n", |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame^] | 396 | MAJOR(part_devt(part)), MINOR(part_devt(part)), |
| 397 | (unsigned long long)part->nr_sects >> 1, |
| 398 | disk_name(sgp, part->partno, buf)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
Jan Engelhardt | 12f32bb | 2008-01-29 20:57:51 +0100 | [diff] [blame] | 404 | const struct seq_operations partitions_op = { |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 405 | .start = show_partition_start, |
| 406 | .next = disk_seqf_next, |
| 407 | .stop = disk_seqf_stop, |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 408 | .show = show_partition |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | }; |
| 410 | #endif |
| 411 | |
| 412 | |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 413 | static struct kobject *base_probe(dev_t devt, int *partno, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 415 | if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | /* Make old-style 2.4 aliases work */ |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 417 | request_module("block-major-%d", MAJOR(devt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | return NULL; |
| 419 | } |
| 420 | |
| 421 | static int __init genhd_device_init(void) |
| 422 | { |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 423 | int error; |
| 424 | |
| 425 | block_class.dev_kobj = sysfs_dev_block_kobj; |
| 426 | error = class_register(&block_class); |
Roland McGrath | ee27a55 | 2008-03-11 17:13:15 -0700 | [diff] [blame] | 427 | if (unlikely(error)) |
| 428 | return error; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 429 | bdev_map = kobj_map_init(base_probe, &block_class_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | blk_dev_init(); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 431 | |
| 432 | #ifndef CONFIG_SYSFS_DEPRECATED |
| 433 | /* create top-level block dir */ |
| 434 | block_depr = kobject_create_and_add("block", NULL); |
| 435 | #endif |
Greg Kroah-Hartman | 830d3cf | 2007-11-06 10:36:58 -0800 | [diff] [blame] | 436 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | subsys_initcall(genhd_device_init); |
| 440 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 441 | static ssize_t disk_range_show(struct device *dev, |
| 442 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 444 | struct gendisk *disk = dev_to_disk(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 446 | return sprintf(buf, "%d\n", disk->minors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 449 | static ssize_t disk_removable_show(struct device *dev, |
| 450 | struct device_attribute *attr, char *buf) |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 451 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 452 | struct gendisk *disk = dev_to_disk(dev); |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 453 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 454 | return sprintf(buf, "%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0)); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 456 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
Kay Sievers | 1c9ce52 | 2008-06-13 09:41:00 +0200 | [diff] [blame] | 458 | static ssize_t disk_ro_show(struct device *dev, |
| 459 | struct device_attribute *attr, char *buf) |
| 460 | { |
| 461 | struct gendisk *disk = dev_to_disk(dev); |
| 462 | |
| 463 | return sprintf(buf, "%d\n", disk->policy ? 1 : 0); |
| 464 | } |
| 465 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 466 | static ssize_t disk_size_show(struct device *dev, |
| 467 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 469 | struct gendisk *disk = dev_to_disk(dev); |
| 470 | |
| 471 | return sprintf(buf, "%llu\n", (unsigned long long)get_capacity(disk)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 473 | |
| 474 | static ssize_t disk_capability_show(struct device *dev, |
| 475 | struct device_attribute *attr, char *buf) |
Kristen Carlson Accardi | 86ce18d | 2007-05-23 13:57:38 -0700 | [diff] [blame] | 476 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 477 | struct gendisk *disk = dev_to_disk(dev); |
| 478 | |
| 479 | return sprintf(buf, "%x\n", disk->flags); |
Kristen Carlson Accardi | 86ce18d | 2007-05-23 13:57:38 -0700 | [diff] [blame] | 480 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 481 | |
| 482 | static ssize_t disk_stat_show(struct device *dev, |
| 483 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 485 | struct gendisk *disk = dev_to_disk(dev); |
| 486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | preempt_disable(); |
| 488 | disk_round_stats(disk); |
| 489 | preempt_enable(); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 490 | return sprintf(buf, |
Ben Woodard | 837c787 | 2006-03-22 08:09:31 +0100 | [diff] [blame] | 491 | "%8lu %8lu %8llu %8u " |
| 492 | "%8lu %8lu %8llu %8u " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | "%8u %8u %8u" |
| 494 | "\n", |
Jens Axboe | 47a0041 | 2005-11-09 13:38:47 +0100 | [diff] [blame] | 495 | disk_stat_read(disk, ios[READ]), |
| 496 | disk_stat_read(disk, merges[READ]), |
| 497 | (unsigned long long)disk_stat_read(disk, sectors[READ]), |
| 498 | jiffies_to_msecs(disk_stat_read(disk, ticks[READ])), |
| 499 | disk_stat_read(disk, ios[WRITE]), |
| 500 | disk_stat_read(disk, merges[WRITE]), |
| 501 | (unsigned long long)disk_stat_read(disk, sectors[WRITE]), |
| 502 | jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | disk->in_flight, |
| 504 | jiffies_to_msecs(disk_stat_read(disk, io_ticks)), |
| 505 | jiffies_to_msecs(disk_stat_read(disk, time_in_queue))); |
| 506 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 508 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 509 | static ssize_t disk_fail_show(struct device *dev, |
| 510 | struct device_attribute *attr, char *buf) |
| 511 | { |
| 512 | struct gendisk *disk = dev_to_disk(dev); |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 513 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 514 | return sprintf(buf, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0); |
| 515 | } |
| 516 | |
| 517 | static ssize_t disk_fail_store(struct device *dev, |
| 518 | struct device_attribute *attr, |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 519 | const char *buf, size_t count) |
| 520 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 521 | struct gendisk *disk = dev_to_disk(dev); |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 522 | int i; |
| 523 | |
| 524 | if (count > 0 && sscanf(buf, "%d", &i) > 0) { |
| 525 | if (i == 0) |
| 526 | disk->flags &= ~GENHD_FL_FAIL; |
| 527 | else |
| 528 | disk->flags |= GENHD_FL_FAIL; |
| 529 | } |
| 530 | |
| 531 | return count; |
| 532 | } |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 533 | |
| 534 | #endif |
| 535 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 536 | static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL); |
| 537 | static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL); |
Kay Sievers | 1c9ce52 | 2008-06-13 09:41:00 +0200 | [diff] [blame] | 538 | static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 539 | static DEVICE_ATTR(size, S_IRUGO, disk_size_show, NULL); |
| 540 | static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL); |
| 541 | static DEVICE_ATTR(stat, S_IRUGO, disk_stat_show, NULL); |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 542 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 543 | static struct device_attribute dev_attr_fail = |
| 544 | __ATTR(make-it-fail, S_IRUGO|S_IWUSR, disk_fail_show, disk_fail_store); |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 545 | #endif |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 546 | |
| 547 | static struct attribute *disk_attrs[] = { |
| 548 | &dev_attr_range.attr, |
| 549 | &dev_attr_removable.attr, |
Kay Sievers | 1c9ce52 | 2008-06-13 09:41:00 +0200 | [diff] [blame] | 550 | &dev_attr_ro.attr, |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 551 | &dev_attr_size.attr, |
| 552 | &dev_attr_capability.attr, |
| 553 | &dev_attr_stat.attr, |
| 554 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
| 555 | &dev_attr_fail.attr, |
| 556 | #endif |
| 557 | NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | }; |
| 559 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 560 | static struct attribute_group disk_attr_group = { |
| 561 | .attrs = disk_attrs, |
| 562 | }; |
| 563 | |
| 564 | static struct attribute_group *disk_attr_groups[] = { |
| 565 | &disk_attr_group, |
| 566 | NULL |
| 567 | }; |
| 568 | |
| 569 | static void disk_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 571 | struct gendisk *disk = dev_to_disk(dev); |
| 572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | kfree(disk->random); |
| 574 | kfree(disk->part); |
| 575 | free_disk_stats(disk); |
| 576 | kfree(disk); |
| 577 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 578 | struct class block_class = { |
| 579 | .name = "block", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | }; |
| 581 | |
Adrian Bunk | 1826ead | 2008-03-04 11:23:46 +0100 | [diff] [blame] | 582 | static struct device_type disk_type = { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 583 | .name = "disk", |
| 584 | .groups = disk_attr_groups, |
| 585 | .release = disk_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | }; |
| 587 | |
Randy Dunlap | a6e2ba8 | 2008-05-23 09:44:11 -0700 | [diff] [blame] | 588 | #ifdef CONFIG_PROC_FS |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 589 | /* |
| 590 | * aggregate disk stat collector. Uses the same stats that the sysfs |
| 591 | * entries do, above, but makes them available through one seq_file. |
| 592 | * |
| 593 | * The output looks suspiciously like /proc/partitions with a bunch of |
| 594 | * extra fields. |
| 595 | */ |
| 596 | static int diskstats_show(struct seq_file *seqf, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | { |
| 598 | struct gendisk *gp = v; |
| 599 | char buf[BDEVNAME_SIZE]; |
Tejun Heo | 310a2c1 | 2008-08-25 19:47:17 +0900 | [diff] [blame] | 600 | int n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | |
| 602 | /* |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 603 | if (&gp->dev.kobj.entry == block_class.devices.next) |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 604 | seq_puts(seqf, "major minor name" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | " rio rmerge rsect ruse wio wmerge " |
| 606 | "wsect wuse running use aveq" |
| 607 | "\n\n"); |
| 608 | */ |
| 609 | |
| 610 | preempt_disable(); |
| 611 | disk_round_stats(gp); |
| 612 | preempt_enable(); |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 613 | seq_printf(seqf, "%4d %4d %s %lu %lu %llu %u %lu %lu %llu %u %u %u %u\n", |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame^] | 614 | MAJOR(disk_devt(gp)), MINOR(disk_devt(gp)), |
| 615 | disk_name(gp, 0, buf), |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 616 | disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]), |
| 617 | (unsigned long long)disk_stat_read(gp, sectors[0]), |
| 618 | jiffies_to_msecs(disk_stat_read(gp, ticks[0])), |
| 619 | disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]), |
| 620 | (unsigned long long)disk_stat_read(gp, sectors[1]), |
| 621 | jiffies_to_msecs(disk_stat_read(gp, ticks[1])), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | gp->in_flight, |
| 623 | jiffies_to_msecs(disk_stat_read(gp, io_ticks)), |
| 624 | jiffies_to_msecs(disk_stat_read(gp, time_in_queue))); |
| 625 | |
| 626 | /* now show all non-0 size partitions of it */ |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame^] | 627 | for (n = 0; n < disk_max_parts(gp); n++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | struct hd_struct *hd = gp->part[n]; |
| 629 | |
Jerome Marchand | 28f39d5 | 2008-02-08 11:04:56 +0100 | [diff] [blame] | 630 | if (!hd || !hd->nr_sects) |
| 631 | continue; |
| 632 | |
| 633 | preempt_disable(); |
| 634 | part_round_stats(hd); |
| 635 | preempt_enable(); |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 636 | seq_printf(seqf, "%4d %4d %s %lu %lu %llu " |
Jerome Marchand | 28f39d5 | 2008-02-08 11:04:56 +0100 | [diff] [blame] | 637 | "%u %lu %lu %llu %u %u %u %u\n", |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame^] | 638 | MAJOR(part_devt(hd)), MINOR(part_devt(hd)), |
| 639 | disk_name(gp, hd->partno, buf), |
Jerome Marchand | 28f39d5 | 2008-02-08 11:04:56 +0100 | [diff] [blame] | 640 | part_stat_read(hd, ios[0]), |
| 641 | part_stat_read(hd, merges[0]), |
| 642 | (unsigned long long)part_stat_read(hd, sectors[0]), |
| 643 | jiffies_to_msecs(part_stat_read(hd, ticks[0])), |
| 644 | part_stat_read(hd, ios[1]), |
| 645 | part_stat_read(hd, merges[1]), |
| 646 | (unsigned long long)part_stat_read(hd, sectors[1]), |
| 647 | jiffies_to_msecs(part_stat_read(hd, ticks[1])), |
| 648 | hd->in_flight, |
| 649 | jiffies_to_msecs(part_stat_read(hd, io_ticks)), |
| 650 | jiffies_to_msecs(part_stat_read(hd, time_in_queue)) |
| 651 | ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | return 0; |
| 655 | } |
| 656 | |
Jan Engelhardt | 12f32bb | 2008-01-29 20:57:51 +0100 | [diff] [blame] | 657 | const struct seq_operations diskstats_op = { |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 658 | .start = disk_seqf_start, |
| 659 | .next = disk_seqf_next, |
| 660 | .stop = disk_seqf_stop, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | .show = diskstats_show |
| 662 | }; |
Randy Dunlap | a6e2ba8 | 2008-05-23 09:44:11 -0700 | [diff] [blame] | 663 | #endif /* CONFIG_PROC_FS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
Kristen Carlson Accardi | 8ce7ad7 | 2007-05-23 13:57:38 -0700 | [diff] [blame] | 665 | static void media_change_notify_thread(struct work_struct *work) |
| 666 | { |
| 667 | struct gendisk *gd = container_of(work, struct gendisk, async_notify); |
| 668 | char event[] = "MEDIA_CHANGE=1"; |
| 669 | char *envp[] = { event, NULL }; |
| 670 | |
| 671 | /* |
| 672 | * set enviroment vars to indicate which event this is for |
| 673 | * so that user space will know to go check the media status. |
| 674 | */ |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 675 | kobject_uevent_env(&gd->dev.kobj, KOBJ_CHANGE, envp); |
Kristen Carlson Accardi | 8ce7ad7 | 2007-05-23 13:57:38 -0700 | [diff] [blame] | 676 | put_device(gd->driverfs_dev); |
| 677 | } |
| 678 | |
Adrian Bunk | 1826ead | 2008-03-04 11:23:46 +0100 | [diff] [blame] | 679 | #if 0 |
Kristen Carlson Accardi | 8ce7ad7 | 2007-05-23 13:57:38 -0700 | [diff] [blame] | 680 | void genhd_media_change_notify(struct gendisk *disk) |
| 681 | { |
| 682 | get_device(disk->driverfs_dev); |
| 683 | schedule_work(&disk->async_notify); |
| 684 | } |
| 685 | EXPORT_SYMBOL_GPL(genhd_media_change_notify); |
Adrian Bunk | 1826ead | 2008-03-04 11:23:46 +0100 | [diff] [blame] | 686 | #endif /* 0 */ |
Kristen Carlson Accardi | 8ce7ad7 | 2007-05-23 13:57:38 -0700 | [diff] [blame] | 687 | |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 688 | dev_t blk_lookup_devt(const char *name, int partno) |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 689 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 690 | dev_t devt = MKDEV(0, 0); |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 691 | struct class_dev_iter iter; |
| 692 | struct device *dev; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 693 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 694 | class_dev_iter_init(&iter, &block_class, NULL, &disk_type); |
| 695 | while ((dev = class_dev_iter_next(&iter))) { |
| 696 | struct gendisk *disk = dev_to_disk(dev); |
| 697 | |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame^] | 698 | if (strcmp(dev->bus_id, name)) |
| 699 | continue; |
| 700 | if (partno < 0 || partno > disk_max_parts(disk)) |
| 701 | continue; |
| 702 | |
| 703 | if (partno == 0) |
| 704 | devt = disk_devt(disk); |
| 705 | else { |
| 706 | struct hd_struct *part = disk->part[partno - 1]; |
| 707 | |
| 708 | if (!part || !part->nr_sects) |
| 709 | continue; |
| 710 | |
| 711 | devt = part_devt(part); |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 712 | } |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame^] | 713 | break; |
Kay Sievers | 5c0ef6d | 2008-08-16 14:30:30 +0200 | [diff] [blame] | 714 | } |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 715 | class_dev_iter_exit(&iter); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 716 | return devt; |
| 717 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 718 | EXPORT_SYMBOL(blk_lookup_devt); |
| 719 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | struct gendisk *alloc_disk(int minors) |
| 721 | { |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 722 | return alloc_disk_node(minors, -1); |
| 723 | } |
| 724 | |
| 725 | struct gendisk *alloc_disk_node(int minors, int node_id) |
| 726 | { |
| 727 | struct gendisk *disk; |
| 728 | |
Christoph Lameter | 94f6030 | 2007-07-17 04:03:29 -0700 | [diff] [blame] | 729 | disk = kmalloc_node(sizeof(struct gendisk), |
| 730 | GFP_KERNEL | __GFP_ZERO, node_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | if (disk) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | if (!init_disk_stats(disk)) { |
| 733 | kfree(disk); |
| 734 | return NULL; |
| 735 | } |
| 736 | if (minors > 1) { |
| 737 | int size = (minors - 1) * sizeof(struct hd_struct *); |
Christoph Lameter | 94f6030 | 2007-07-17 04:03:29 -0700 | [diff] [blame] | 738 | disk->part = kmalloc_node(size, |
| 739 | GFP_KERNEL | __GFP_ZERO, node_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | if (!disk->part) { |
Jerome Marchand | c767403 | 2007-11-23 09:17:53 +0100 | [diff] [blame] | 741 | free_disk_stats(disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | kfree(disk); |
| 743 | return NULL; |
| 744 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | } |
| 746 | disk->minors = minors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | rand_initialize_disk(disk); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 748 | disk->dev.class = &block_class; |
| 749 | disk->dev.type = &disk_type; |
| 750 | device_initialize(&disk->dev); |
Kristen Carlson Accardi | 8ce7ad7 | 2007-05-23 13:57:38 -0700 | [diff] [blame] | 751 | INIT_WORK(&disk->async_notify, |
| 752 | media_change_notify_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | } |
| 754 | return disk; |
| 755 | } |
| 756 | |
| 757 | EXPORT_SYMBOL(alloc_disk); |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 758 | EXPORT_SYMBOL(alloc_disk_node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
| 760 | struct kobject *get_disk(struct gendisk *disk) |
| 761 | { |
| 762 | struct module *owner; |
| 763 | struct kobject *kobj; |
| 764 | |
| 765 | if (!disk->fops) |
| 766 | return NULL; |
| 767 | owner = disk->fops->owner; |
| 768 | if (owner && !try_module_get(owner)) |
| 769 | return NULL; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 770 | kobj = kobject_get(&disk->dev.kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | if (kobj == NULL) { |
| 772 | module_put(owner); |
| 773 | return NULL; |
| 774 | } |
| 775 | return kobj; |
| 776 | |
| 777 | } |
| 778 | |
| 779 | EXPORT_SYMBOL(get_disk); |
| 780 | |
| 781 | void put_disk(struct gendisk *disk) |
| 782 | { |
| 783 | if (disk) |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 784 | kobject_put(&disk->dev.kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | EXPORT_SYMBOL(put_disk); |
| 788 | |
| 789 | void set_device_ro(struct block_device *bdev, int flag) |
| 790 | { |
| 791 | if (bdev->bd_contains != bdev) |
| 792 | bdev->bd_part->policy = flag; |
| 793 | else |
| 794 | bdev->bd_disk->policy = flag; |
| 795 | } |
| 796 | |
| 797 | EXPORT_SYMBOL(set_device_ro); |
| 798 | |
| 799 | void set_disk_ro(struct gendisk *disk, int flag) |
| 800 | { |
| 801 | int i; |
| 802 | disk->policy = flag; |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame^] | 803 | for (i = 0; i < disk_max_parts(disk); i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | if (disk->part[i]) disk->part[i]->policy = flag; |
| 805 | } |
| 806 | |
| 807 | EXPORT_SYMBOL(set_disk_ro); |
| 808 | |
| 809 | int bdev_read_only(struct block_device *bdev) |
| 810 | { |
| 811 | if (!bdev) |
| 812 | return 0; |
| 813 | else if (bdev->bd_contains != bdev) |
| 814 | return bdev->bd_part->policy; |
| 815 | else |
| 816 | return bdev->bd_disk->policy; |
| 817 | } |
| 818 | |
| 819 | EXPORT_SYMBOL(bdev_read_only); |
| 820 | |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 821 | int invalidate_partition(struct gendisk *disk, int partno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | { |
| 823 | int res = 0; |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 824 | struct block_device *bdev = bdget_disk(disk, partno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | if (bdev) { |
Christoph Hellwig | 2ef4163 | 2005-05-05 16:15:59 -0700 | [diff] [blame] | 826 | fsync_bdev(bdev); |
| 827 | res = __invalidate_device(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | bdput(bdev); |
| 829 | } |
| 830 | return res; |
| 831 | } |
| 832 | |
| 833 | EXPORT_SYMBOL(invalidate_partition); |