aboutsummaryrefslogtreecommitdiffstats
path: root/block/genhd.c
diff options
context:
space:
mode:
authorKay Sievers <kay.sievers@vrfy.org>2007-05-21 16:08:01 -0400
committerGreg Kroah-Hartman <gregkh@suse.de>2008-01-24 23:40:36 -0500
commitedfaa7c36574f1bf09c65ad602412db9da5f96bf (patch)
treed591b80ff9229e4845e41d68e2f4c5aadb017027 /block/genhd.c
parent09f82ea92822a7bbb7e816508abbda47ed54a77f (diff)
Driver core: convert block from raw kobjects to core devices
This moves the block devices to /sys/class/block. It will create a flat list of all block devices, with the disks and partitions in one directory. For compatibility /sys/block is created and contains symlinks to the disks. /sys/class/block |-- sda -> ../../devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda |-- sda1 -> ../../devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda1 |-- sda10 -> ../../devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda10 |-- sda5 -> ../../devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda5 |-- sda6 -> ../../devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda6 |-- sda7 -> ../../devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda7 |-- sda8 -> ../../devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda8 |-- sda9 -> ../../devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda/sda9 `-- sr0 -> ../../devices/pci0000:00/0000:00:1f.2/host1/target1:0:0/1:0:0:0/block/sr0 /sys/block/ |-- sda -> ../devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda `-- sr0 -> ../devices/pci0000:00/0000:00:1f.2/host1/target1:0:0/1:0:0:0/block/sr0 Signed-off-by: Kay Sievers <kay.sievers@vrfy.org> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'block/genhd.c')
-rw-r--r--block/genhd.c416
1 files changed, 183 insertions, 233 deletions
diff --git a/block/genhd.c b/block/genhd.c
index 69aa7389d484..5e4ab4b37d9f 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -17,9 +17,10 @@
17#include <linux/buffer_head.h> 17#include <linux/buffer_head.h>
18#include <linux/mutex.h> 18#include <linux/mutex.h>
19 19
20struct kset *block_kset; 20static DEFINE_MUTEX(block_class_lock);
21static struct kset_uevent_ops block_uevent_ops; 21#ifndef CONFIG_SYSFS_DEPRECATED
22static DEFINE_MUTEX(block_subsys_lock); 22struct kobject *block_depr;
23#endif
23 24
24/* 25/*
25 * Can be deleted altogether. Later. 26 * Can be deleted altogether. Later.
@@ -38,19 +39,17 @@ static inline int major_to_index(int major)
38} 39}
39 40
40#ifdef CONFIG_PROC_FS 41#ifdef CONFIG_PROC_FS
41
42void blkdev_show(struct seq_file *f, off_t offset) 42void blkdev_show(struct seq_file *f, off_t offset)
43{ 43{
44 struct blk_major_name *dp; 44 struct blk_major_name *dp;
45 45
46 if (offset < BLKDEV_MAJOR_HASH_SIZE) { 46 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
47 mutex_lock(&block_subsys_lock); 47 mutex_lock(&block_class_lock);
48 for (dp = major_names[offset]; dp; dp = dp->next) 48 for (dp = major_names[offset]; dp; dp = dp->next)
49 seq_printf(f, "%3d %s\n", dp->major, dp->name); 49 seq_printf(f, "%3d %s\n", dp->major, dp->name);
50 mutex_unlock(&block_subsys_lock); 50 mutex_unlock(&block_class_lock);
51 } 51 }
52} 52}
53
54#endif /* CONFIG_PROC_FS */ 53#endif /* CONFIG_PROC_FS */
55 54
56int register_blkdev(unsigned int major, const char *name) 55int register_blkdev(unsigned int major, const char *name)
@@ -58,7 +57,7 @@ int register_blkdev(unsigned int major, const char *name)
58 struct blk_major_name **n, *p; 57 struct blk_major_name **n, *p;
59 int index, ret = 0; 58 int index, ret = 0;
60 59
61 mutex_lock(&block_subsys_lock); 60 mutex_lock(&block_class_lock);
62 61
63 /* temporary */ 62 /* temporary */
64 if (major == 0) { 63 if (major == 0) {
@@ -103,7 +102,7 @@ int register_blkdev(unsigned int major, const char *name)
103 kfree(p); 102 kfree(p);
104 } 103 }
105out: 104out:
106 mutex_unlock(&block_subsys_lock); 105 mutex_unlock(&block_class_lock);
107 return ret; 106 return ret;
108} 107}
109 108
@@ -115,7 +114,7 @@ void unregister_blkdev(unsigned int major, const char *name)
115 struct blk_major_name *p = NULL; 114 struct blk_major_name *p = NULL;
116 int index = major_to_index(major); 115 int index = major_to_index(major);
117 116
118 mutex_lock(&block_subsys_lock); 117 mutex_lock(&block_class_lock);
119 for (n = &major_names[index]; *n; n = &(*n)->next) 118 for (n = &major_names[index]; *n; n = &(*n)->next)
120 if ((*n)->major == major) 119 if ((*n)->major == major)
121 break; 120 break;
@@ -125,7 +124,7 @@ void unregister_blkdev(unsigned int major, const char *name)
125 p = *n; 124 p = *n;
126 *n = p->next; 125 *n = p->next;
127 } 126 }
128 mutex_unlock(&block_subsys_lock); 127 mutex_unlock(&block_class_lock);
129 kfree(p); 128 kfree(p);
130} 129}
131 130
@@ -138,29 +137,30 @@ static struct kobj_map *bdev_map;
138 * range must be nonzero 137 * range must be nonzero
139 * The hash chain is sorted on range, so that subranges can override. 138 * The hash chain is sorted on range, so that subranges can override.
140 */ 139 */
141void blk_register_region(dev_t dev, unsigned long range, struct module *module, 140void blk_register_region(dev_t devt, unsigned long range, struct module *module,
142 struct kobject *(*probe)(dev_t, int *, void *), 141 struct kobject *(*probe)(dev_t, int *, void *),
143 int (*lock)(dev_t, void *), void *data) 142 int (*lock)(dev_t, void *), void *data)
144{ 143{
145 kobj_map(bdev_map, dev, range, module, probe, lock, data); 144 kobj_map(bdev_map, devt, range, module, probe, lock, data);
146} 145}
147 146
148EXPORT_SYMBOL(blk_register_region); 147EXPORT_SYMBOL(blk_register_region);
149 148
150void blk_unregister_region(dev_t dev, unsigned long range) 149void blk_unregister_region(dev_t devt, unsigned long range)
151{ 150{
152 kobj_unmap(bdev_map, dev, range); 151 kobj_unmap(bdev_map, devt, range);
153} 152}
154 153
155EXPORT_SYMBOL(blk_unregister_region); 154EXPORT_SYMBOL(blk_unregister_region);
156 155
157static struct kobject *exact_match(dev_t dev, int *part, void *data) 156static struct kobject *exact_match(dev_t devt, int *part, void *data)
158{ 157{
159 struct gendisk *p = data; 158 struct gendisk *p = data;
160 return &p->kobj; 159
160 return &p->dev.kobj;
161} 161}
162 162
163static int exact_lock(dev_t dev, void *data) 163static int exact_lock(dev_t devt, void *data)
164{ 164{
165 struct gendisk *p = data; 165 struct gendisk *p = data;
166 166
@@ -195,8 +195,6 @@ void unlink_gendisk(struct gendisk *disk)
195 disk->minors); 195 disk->minors);
196} 196}
197 197
198#define to_disk(obj) container_of(obj,struct gendisk,kobj)
199
200/** 198/**
201 * get_gendisk - get partitioning information for a given device 199 * get_gendisk - get partitioning information for a given device
202 * @dev: device to get partitioning information for 200 * @dev: device to get partitioning information for
@@ -204,10 +202,12 @@ void unlink_gendisk(struct gendisk *disk)
204 * This function gets the structure containing partitioning 202 * This function gets the structure containing partitioning
205 * information for the given device @dev. 203 * information for the given device @dev.
206 */ 204 */
207struct gendisk *get_gendisk(dev_t dev, int *part) 205struct gendisk *get_gendisk(dev_t devt, int *part)
208{ 206{
209 struct kobject *kobj = kobj_lookup(bdev_map, dev, part); 207 struct kobject *kobj = kobj_lookup(bdev_map, devt, part);
210 return kobj ? to_disk(kobj) : NULL; 208 struct device *dev = kobj_to_dev(kobj);
209
210 return kobj ? dev_to_disk(dev) : NULL;
211} 211}
212 212
213/* 213/*
@@ -217,13 +217,17 @@ struct gendisk *get_gendisk(dev_t dev, int *part)
217 */ 217 */
218void __init printk_all_partitions(void) 218void __init printk_all_partitions(void)
219{ 219{
220 int n; 220 struct device *dev;
221 struct gendisk *sgp; 221 struct gendisk *sgp;
222 char buf[BDEVNAME_SIZE];
223 int n;
222 224
223 mutex_lock(&block_subsys_lock); 225 mutex_lock(&block_class_lock);
224 /* For each block device... */ 226 /* For each block device... */
225 list_for_each_entry(sgp, &block_kset->list, kobj.entry) { 227 list_for_each_entry(dev, &block_class.devices, node) {
226 char buf[BDEVNAME_SIZE]; 228 if (dev->type != &disk_type)
229 continue;
230 sgp = dev_to_disk(dev);
227 /* 231 /*
228 * Don't show empty devices or things that have been surpressed 232 * Don't show empty devices or things that have been surpressed
229 */ 233 */
@@ -256,38 +260,46 @@ void __init printk_all_partitions(void)
256 sgp->major, n + 1 + sgp->first_minor, 260 sgp->major, n + 1 + sgp->first_minor,
257 (unsigned long long)sgp->part[n]->nr_sects >> 1, 261 (unsigned long long)sgp->part[n]->nr_sects >> 1,
258 disk_name(sgp, n + 1, buf)); 262 disk_name(sgp, n + 1, buf));
259 } /* partition subloop */ 263 }
260 } /* Block device loop */ 264 }
261 265
262 mutex_unlock(&block_subsys_lock); 266 mutex_unlock(&block_class_lock);
263 return;
264} 267}
265 268
266#ifdef CONFIG_PROC_FS 269#ifdef CONFIG_PROC_FS
267/* iterator */ 270/* iterator */
268static void *part_start(struct seq_file *part, loff_t *pos) 271static void *part_start(struct seq_file *part, loff_t *pos)
269{ 272{
270 struct list_head *p; 273 loff_t k = *pos;
271 loff_t l = *pos; 274 struct device *dev;
272 275
273 mutex_lock(&block_subsys_lock); 276 mutex_lock(&block_class_lock);
274 list_for_each(p, &block_kset->list) 277 list_for_each_entry(dev, &block_class.devices, node) {
275 if (!l--) 278 if (dev->type != &disk_type)
276 return list_entry(p, struct gendisk, kobj.entry); 279 continue;
280 if (!k--)
281 return dev_to_disk(dev);
282 }
277 return NULL; 283 return NULL;
278} 284}
279 285
280static void *part_next(struct seq_file *part, void *v, loff_t *pos) 286static void *part_next(struct seq_file *part, void *v, loff_t *pos)
281{ 287{
282 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next; 288 struct gendisk *gp = v;
289 struct device *dev;
283 ++*pos; 290 ++*pos;
284 return p==&block_kset->list ? NULL : 291 list_for_each_entry(dev, &gp->dev.node, node) {
285 list_entry(p, struct gendisk, kobj.entry); 292 if (&dev->node == &block_class.devices)
293 return NULL;
294 if (dev->type == &disk_type)
295 return dev_to_disk(dev);
296 }
297 return NULL;
286} 298}
287 299
288static void part_stop(struct seq_file *part, void *v) 300static void part_stop(struct seq_file *part, void *v)
289{ 301{
290 mutex_unlock(&block_subsys_lock); 302 mutex_unlock(&block_class_lock);
291} 303}
292 304
293static int show_partition(struct seq_file *part, void *v) 305static int show_partition(struct seq_file *part, void *v)
@@ -296,7 +308,7 @@ static int show_partition(struct seq_file *part, void *v)
296 int n; 308 int n;
297 char buf[BDEVNAME_SIZE]; 309 char buf[BDEVNAME_SIZE];
298 310
299 if (&sgp->kobj.entry == block_kset->list.next) 311 if (&sgp->dev.node == block_class.devices.next)
300 seq_puts(part, "major minor #blocks name\n\n"); 312 seq_puts(part, "major minor #blocks name\n\n");
301 313
302 /* Don't show non-partitionable removeable devices or empty devices */ 314 /* Don't show non-partitionable removeable devices or empty devices */
@@ -326,109 +338,81 @@ static int show_partition(struct seq_file *part, void *v)
326} 338}
327 339
328struct seq_operations partitions_op = { 340struct seq_operations partitions_op = {
329 .start =part_start, 341 .start = part_start,
330 .next = part_next, 342 .next = part_next,
331 .stop = part_stop, 343 .stop = part_stop,
332 .show = show_partition 344 .show = show_partition
333}; 345};
334#endif 346#endif
335 347
336 348
337extern int blk_dev_init(void); 349extern int blk_dev_init(void);
338 350
339static struct kobject *base_probe(dev_t dev, int *part, void *data) 351static struct kobject *base_probe(dev_t devt, int *part, void *data)
340{ 352{
341 if (request_module("block-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0) 353 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
342 /* Make old-style 2.4 aliases work */ 354 /* Make old-style 2.4 aliases work */
343 request_module("block-major-%d", MAJOR(dev)); 355 request_module("block-major-%d", MAJOR(devt));
344 return NULL; 356 return NULL;
345} 357}
346 358
347static int __init genhd_device_init(void) 359static int __init genhd_device_init(void)
348{ 360{
349 bdev_map = kobj_map_init(base_probe, &block_subsys_lock); 361 class_register(&block_class);
362 bdev_map = kobj_map_init(base_probe, &block_class_lock);
350 blk_dev_init(); 363 blk_dev_init();
351 block_kset = kset_create_and_add("block", &block_uevent_ops, NULL); 364
352 if (!block_kset) { 365#ifndef CONFIG_SYSFS_DEPRECATED
353 printk(KERN_WARNING "%s: kset_create error\n", __FUNCTION__); 366 /* create top-level block dir */
354 return -ENOMEM; 367 block_depr = kobject_create_and_add("block", NULL);
355 } 368#endif
356 return 0; 369 return 0;
357} 370}
358 371
359subsys_initcall(genhd_device_init); 372subsys_initcall(genhd_device_init);
360 373
361 374static ssize_t disk_range_show(struct device *dev,
362 375 struct device_attribute *attr, char *buf)
363/*
364 * kobject & sysfs bindings for block devices
365 */
366static ssize_t disk_attr_show(struct kobject *kobj, struct attribute *attr,
367 char *page)
368{ 376{
369 struct gendisk *disk = to_disk(kobj); 377 struct gendisk *disk = dev_to_disk(dev);
370 struct disk_attribute *disk_attr =
371 container_of(attr,struct disk_attribute,attr);
372 ssize_t ret = -EIO;
373 378
374 if (disk_attr->show) 379 return sprintf(buf, "%d\n", disk->minors);
375 ret = disk_attr->show(disk,page);
376 return ret;
377} 380}
378 381
379static ssize_t disk_attr_store(struct kobject * kobj, struct attribute * attr, 382static ssize_t disk_removable_show(struct device *dev,
380 const char *page, size_t count) 383 struct device_attribute *attr, char *buf)
381{ 384{
382 struct gendisk *disk = to_disk(kobj); 385 struct gendisk *disk = dev_to_disk(dev);
383 struct disk_attribute *disk_attr =
384 container_of(attr,struct disk_attribute,attr);
385 ssize_t ret = 0;
386 386
387 if (disk_attr->store) 387 return sprintf(buf, "%d\n",
388 ret = disk_attr->store(disk, page, count); 388 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
389 return ret;
390} 389}
391 390
392static struct sysfs_ops disk_sysfs_ops = { 391static ssize_t disk_size_show(struct device *dev,
393 .show = &disk_attr_show, 392 struct device_attribute *attr, char *buf)
394 .store = &disk_attr_store,
395};
396
397static ssize_t disk_uevent_store(struct gendisk * disk,
398 const char *buf, size_t count)
399{ 393{
400 kobject_uevent(&disk->kobj, KOBJ_ADD); 394 struct gendisk *disk = dev_to_disk(dev);
401 return count;
402}
403static ssize_t disk_dev_read(struct gendisk * disk, char *page)
404{
405 dev_t base = MKDEV(disk->major, disk->first_minor);
406 return print_dev_t(page, base);
407}
408static ssize_t disk_range_read(struct gendisk * disk, char *page)
409{
410 return sprintf(page, "%d\n", disk->minors);
411}
412static ssize_t disk_removable_read(struct gendisk * disk, char *page)
413{
414 return sprintf(page, "%d\n",
415 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
416 395
396 return sprintf(buf, "%llu\n", (unsigned long long)get_capacity(disk));
417} 397}
418static ssize_t disk_size_read(struct gendisk * disk, char *page) 398
419{ 399static ssize_t disk_capability_show(struct device *dev,
420 return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk)); 400 struct device_attribute *attr, char *buf)
421}
422static ssize_t disk_capability_read(struct gendisk *disk, char *page)
423{ 401{
424 return sprintf(page, "%x\n", disk->flags); 402 struct gendisk *disk = dev_to_disk(dev);
403
404 return sprintf(buf, "%x\n", disk->flags);
425} 405}
426static ssize_t disk_stats_read(struct gendisk * disk, char *page) 406
407static ssize_t disk_stat_show(struct device *dev,
408 struct device_attribute *attr, char *buf)
427{ 409{
410 struct gendisk *disk = dev_to_disk(dev);
411
428 preempt_disable(); 412 preempt_disable();
429 disk_round_stats(disk); 413 disk_round_stats(disk);
430 preempt_enable(); 414 preempt_enable();
431 return sprintf(page, 415 return sprintf(buf,
432 "%8lu %8lu %8llu %8u " 416 "%8lu %8lu %8llu %8u "
433 "%8lu %8lu %8llu %8u " 417 "%8lu %8lu %8llu %8u "
434 "%8u %8u %8u" 418 "%8u %8u %8u"
@@ -445,40 +429,21 @@ static ssize_t disk_stats_read(struct gendisk * disk, char *page)
445 jiffies_to_msecs(disk_stat_read(disk, io_ticks)), 429 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
446 jiffies_to_msecs(disk_stat_read(disk, time_in_queue))); 430 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
447} 431}
448static struct disk_attribute disk_attr_uevent = {
449 .attr = {.name = "uevent", .mode = S_IWUSR },
450 .store = disk_uevent_store
451};
452static struct disk_attribute disk_attr_dev = {
453 .attr = {.name = "dev", .mode = S_IRUGO },
454 .show = disk_dev_read
455};
456static struct disk_attribute disk_attr_range = {
457 .attr = {.name = "range", .mode = S_IRUGO },
458 .show = disk_range_read
459};
460static struct disk_attribute disk_attr_removable = {
461 .attr = {.name = "removable", .mode = S_IRUGO },
462 .show = disk_removable_read
463};
464static struct disk_attribute disk_attr_size = {
465 .attr = {.name = "size", .mode = S_IRUGO },
466 .show = disk_size_read
467};
468static struct disk_attribute disk_attr_capability = {
469 .attr = {.name = "capability", .mode = S_IRUGO },
470 .show = disk_capability_read
471};
472static struct disk_attribute disk_attr_stat = {
473 .attr = {.name = "stat", .mode = S_IRUGO },
474 .show = disk_stats_read
475};
476 432
477#ifdef CONFIG_FAIL_MAKE_REQUEST 433#ifdef CONFIG_FAIL_MAKE_REQUEST
434static ssize_t disk_fail_show(struct device *dev,
435 struct device_attribute *attr, char *buf)
436{
437 struct gendisk *disk = dev_to_disk(dev);
438
439 return sprintf(buf, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0);
440}
478 441
479static ssize_t disk_fail_store(struct gendisk * disk, 442static ssize_t disk_fail_store(struct device *dev,
443 struct device_attribute *attr,
480 const char *buf, size_t count) 444 const char *buf, size_t count)
481{ 445{
446 struct gendisk *disk = dev_to_disk(dev);
482 int i; 447 int i;
483 448
484 if (count > 0 && sscanf(buf, "%d", &i) > 0) { 449 if (count > 0 && sscanf(buf, "%d", &i) > 0) {
@@ -490,134 +455,100 @@ static ssize_t disk_fail_store(struct gendisk * disk,
490 455
491 return count; 456 return count;
492} 457}
493static ssize_t disk_fail_read(struct gendisk * disk, char *page)
494{
495 return sprintf(page, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0);
496}
497static struct disk_attribute disk_attr_fail = {
498 .attr = {.name = "make-it-fail", .mode = S_IRUGO | S_IWUSR },
499 .store = disk_fail_store,
500 .show = disk_fail_read
501};
502 458
503#endif 459#endif
504 460
505static struct attribute * default_attrs[] = { 461static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
506 &disk_attr_uevent.attr, 462static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
507 &disk_attr_dev.attr, 463static DEVICE_ATTR(size, S_IRUGO, disk_size_show, NULL);
508 &disk_attr_range.attr, 464static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
509 &disk_attr_removable.attr, 465static DEVICE_ATTR(stat, S_IRUGO, disk_stat_show, NULL);
510 &disk_attr_size.attr,
511 &disk_attr_stat.attr,
512 &disk_attr_capability.attr,
513#ifdef CONFIG_FAIL_MAKE_REQUEST 466#ifdef CONFIG_FAIL_MAKE_REQUEST
514 &disk_attr_fail.attr, 467static struct device_attribute dev_attr_fail =
468 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, disk_fail_show, disk_fail_store);
515#endif 469#endif
516 NULL, 470
471static struct attribute *disk_attrs[] = {
472 &dev_attr_range.attr,
473 &dev_attr_removable.attr,
474 &dev_attr_size.attr,
475 &dev_attr_capability.attr,
476 &dev_attr_stat.attr,
477#ifdef CONFIG_FAIL_MAKE_REQUEST
478 &dev_attr_fail.attr,
479#endif
480 NULL
481};
482
483static struct attribute_group disk_attr_group = {
484 .attrs = disk_attrs,
485};
486
487static struct attribute_group *disk_attr_groups[] = {
488 &disk_attr_group,
489 NULL
517}; 490};
518 491
519static void disk_release(struct kobject * kobj) 492static void disk_release(struct device *dev)
520{ 493{
521 struct gendisk *disk = to_disk(kobj); 494 struct gendisk *disk = dev_to_disk(dev);
495
522 kfree(disk->random); 496 kfree(disk->random);
523 kfree(disk->part); 497 kfree(disk->part);
524 free_disk_stats(disk); 498 free_disk_stats(disk);
525 kfree(disk); 499 kfree(disk);
526} 500}
527 501struct class block_class = {
528static struct kobj_type ktype_block = { 502 .name = "block",
529 .release = disk_release,
530 .sysfs_ops = &disk_sysfs_ops,
531 .default_attrs = default_attrs,
532}; 503};
533 504
534extern struct kobj_type ktype_part; 505struct device_type disk_type = {
535 506 .name = "disk",
536static int block_uevent_filter(struct kset *kset, struct kobject *kobj) 507 .groups = disk_attr_groups,
537{ 508 .release = disk_release,
538 struct kobj_type *ktype = get_ktype(kobj);
539
540 return ((ktype == &ktype_block) || (ktype == &ktype_part));
541}
542
543static int block_uevent(struct kset *kset, struct kobject *kobj,
544 struct kobj_uevent_env *env)
545{
546 struct kobj_type *ktype = get_ktype(kobj);
547 struct device *physdev;
548 struct gendisk *disk;
549 struct hd_struct *part;
550
551 if (ktype == &ktype_block) {
552 disk = container_of(kobj, struct gendisk, kobj);
553 add_uevent_var(env, "MINOR=%u", disk->first_minor);
554 } else if (ktype == &ktype_part) {
555 disk = container_of(kobj->parent, struct gendisk, kobj);
556 part = container_of(kobj, struct hd_struct, kobj);
557 add_uevent_var(env, "MINOR=%u",
558 disk->first_minor + part->partno);
559 } else
560 return 0;
561
562 add_uevent_var(env, "MAJOR=%u", disk->major);
563
564 /* add physical device, backing this device */
565 physdev = disk->driverfs_dev;
566 if (physdev) {
567 char *path = kobject_get_path(&physdev->kobj, GFP_KERNEL);
568
569 add_uevent_var(env, "PHYSDEVPATH=%s", path);
570 kfree(path);
571
572 if (physdev->bus)
573 add_uevent_var(env, "PHYSDEVBUS=%s", physdev->bus->name);
574
575 if (physdev->driver)
576 add_uevent_var(env, physdev->driver->name);
577 }
578
579 return 0;
580}
581
582static struct kset_uevent_ops block_uevent_ops = {
583 .filter = block_uevent_filter,
584 .uevent = block_uevent,
585}; 509};
586 510
587/* 511/*
588 * aggregate disk stat collector. Uses the same stats that the sysfs 512 * aggregate disk stat collector. Uses the same stats that the sysfs
589 * entries do, above, but makes them available through one seq_file. 513 * entries do, above, but makes them available through one seq_file.
590 * Watching a few disks may be efficient through sysfs, but watching
591 * all of them will be more efficient through this interface.
592 * 514 *
593 * The output looks suspiciously like /proc/partitions with a bunch of 515 * The output looks suspiciously like /proc/partitions with a bunch of
594 * extra fields. 516 * extra fields.
595 */ 517 */
596 518
597/* iterator */
598static void *diskstats_start(struct seq_file *part, loff_t *pos) 519static void *diskstats_start(struct seq_file *part, loff_t *pos)
599{ 520{
600 loff_t k = *pos; 521 loff_t k = *pos;
601 struct list_head *p; 522 struct device *dev;
602 523
603 mutex_lock(&block_subsys_lock); 524 mutex_lock(&block_class_lock);
604 list_for_each(p, &block_kset->list) 525 list_for_each_entry(dev, &block_class.devices, node) {
526 if (dev->type != &disk_type)
527 continue;
605 if (!k--) 528 if (!k--)
606 return list_entry(p, struct gendisk, kobj.entry); 529 return dev_to_disk(dev);
530 }
607 return NULL; 531 return NULL;
608} 532}
609 533
610static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos) 534static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos)
611{ 535{
612 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next; 536 struct gendisk *gp = v;
537 struct device *dev;
538
613 ++*pos; 539 ++*pos;
614 return p==&block_kset->list ? NULL : 540 list_for_each_entry(dev, &gp->dev.node, node) {
615 list_entry(p, struct gendisk, kobj.entry); 541 if (&dev->node == &block_class.devices)
542 return NULL;
543 if (dev->type == &disk_type)
544 return dev_to_disk(dev);
545 }
546 return NULL;
616} 547}
617 548
618static void diskstats_stop(struct seq_file *part, void *v) 549static void diskstats_stop(struct seq_file *part, void *v)
619{ 550{
620 mutex_unlock(&block_subsys_lock); 551 mutex_unlock(&block_class_lock);
621} 552}
622 553
623static int diskstats_show(struct seq_file *s, void *v) 554static int diskstats_show(struct seq_file *s, void *v)
@@ -627,7 +558,7 @@ static int diskstats_show(struct seq_file *s, void *v)
627 int n = 0; 558 int n = 0;
628 559
629 /* 560 /*
630 if (&sgp->kobj.entry == block_kset->list.next) 561 if (&gp->dev.kobj.entry == block_class.devices.next)
631 seq_puts(s, "major minor name" 562 seq_puts(s, "major minor name"
632 " rio rmerge rsect ruse wio wmerge " 563 " rio rmerge rsect ruse wio wmerge "
633 "wsect wuse running use aveq" 564 "wsect wuse running use aveq"
@@ -681,7 +612,7 @@ static void media_change_notify_thread(struct work_struct *work)
681 * set enviroment vars to indicate which event this is for 612 * set enviroment vars to indicate which event this is for
682 * so that user space will know to go check the media status. 613 * so that user space will know to go check the media status.
683 */ 614 */
684 kobject_uevent_env(&gd->kobj, KOBJ_CHANGE, envp); 615 kobject_uevent_env(&gd->dev.kobj, KOBJ_CHANGE, envp);
685 put_device(gd->driverfs_dev); 616 put_device(gd->driverfs_dev);
686} 617}
687 618
@@ -692,6 +623,25 @@ void genhd_media_change_notify(struct gendisk *disk)
692} 623}
693EXPORT_SYMBOL_GPL(genhd_media_change_notify); 624EXPORT_SYMBOL_GPL(genhd_media_change_notify);
694 625
626dev_t blk_lookup_devt(const char *name)
627{
628 struct device *dev;
629 dev_t devt = MKDEV(0, 0);
630
631 mutex_lock(&block_class_lock);
632 list_for_each_entry(dev, &block_class.devices, node) {
633 if (strcmp(dev->bus_id, name) == 0) {
634 devt = dev->devt;
635 break;
636 }
637 }
638 mutex_unlock(&block_class_lock);
639
640 return devt;
641}
642
643EXPORT_SYMBOL(blk_lookup_devt);
644
695struct gendisk *alloc_disk(int minors) 645struct gendisk *alloc_disk(int minors)
696{ 646{
697 return alloc_disk_node(minors, -1); 647 return alloc_disk_node(minors, -1);
@@ -719,10 +669,10 @@ struct gendisk *alloc_disk_node(int minors, int node_id)
719 } 669 }
720 } 670 }
721 disk->minors = minors; 671 disk->minors = minors;
722 disk->kobj.kset = block_kset;
723 disk->kobj.ktype = &ktype_block;
724 kobject_init(&disk->kobj);
725 rand_initialize_disk(disk); 672 rand_initialize_disk(disk);
673 disk->dev.class = &block_class;
674 disk->dev.type = &disk_type;
675 device_initialize(&disk->dev);
726 INIT_WORK(&disk->async_notify, 676 INIT_WORK(&disk->async_notify,
727 media_change_notify_thread); 677 media_change_notify_thread);
728 } 678 }
@@ -742,7 +692,7 @@ struct kobject *get_disk(struct gendisk *disk)
742 owner = disk->fops->owner; 692 owner = disk->fops->owner;
743 if (owner && !try_module_get(owner)) 693 if (owner && !try_module_get(owner))
744 return NULL; 694 return NULL;
745 kobj = kobject_get(&disk->kobj); 695 kobj = kobject_get(&disk->dev.kobj);
746 if (kobj == NULL) { 696 if (kobj == NULL) {
747 module_put(owner); 697 module_put(owner);
748 return NULL; 698 return NULL;
@@ -756,7 +706,7 @@ EXPORT_SYMBOL(get_disk);
756void put_disk(struct gendisk *disk) 706void put_disk(struct gendisk *disk)
757{ 707{
758 if (disk) 708 if (disk)
759 kobject_put(&disk->kobj); 709 kobject_put(&disk->dev.kobj);
760} 710}
761 711
762EXPORT_SYMBOL(put_disk); 712EXPORT_SYMBOL(put_disk);