blob: 69aa7389d4840d65a5f0023303e75383f05918b6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * gendisk handling
3 */
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/module.h>
6#include <linux/fs.h>
7#include <linux/genhd.h>
Andrew Mortonb446b602007-02-20 13:57:48 -08008#include <linux/kdev_t.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#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 Hellwig2ef41632005-05-05 16:15:59 -070017#include <linux/buffer_head.h>
Jes Sorensen58383af2006-02-06 14:12:43 -080018#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -080020struct kset *block_kset;
21static struct kset_uevent_ops block_uevent_ops;
Jes Sorensen58383af2006-02-06 14:12:43 -080022static DEFINE_MUTEX(block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/*
25 * Can be deleted altogether. Later.
26 *
27 */
28static struct blk_major_name {
29 struct blk_major_name *next;
30 int major;
31 char name[16];
Joe Korty68eef3b2006-03-31 02:30:32 -080032} *major_names[BLKDEV_MAJOR_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/* index in the above - for now: assume no multimajor ranges */
35static inline int major_to_index(int major)
36{
Joe Korty68eef3b2006-03-31 02:30:32 -080037 return major % BLKDEV_MAJOR_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038}
39
Joe Korty68eef3b2006-03-31 02:30:32 -080040#ifdef CONFIG_PROC_FS
Neil Horman7170be52006-01-14 13:20:38 -080041
Joe Korty68eef3b2006-03-31 02:30:32 -080042void blkdev_show(struct seq_file *f, off_t offset)
Neil Horman7170be52006-01-14 13:20:38 -080043{
Joe Korty68eef3b2006-03-31 02:30:32 -080044 struct blk_major_name *dp;
Neil Horman7170be52006-01-14 13:20:38 -080045
Joe Korty68eef3b2006-03-31 02:30:32 -080046 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
47 mutex_lock(&block_subsys_lock);
48 for (dp = major_names[offset]; dp; dp = dp->next)
49 seq_printf(f, "%3d %s\n", dp->major, dp->name);
50 mutex_unlock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
Neil Horman7170be52006-01-14 13:20:38 -080053
Joe Korty68eef3b2006-03-31 02:30:32 -080054#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56int register_blkdev(unsigned int major, const char *name)
57{
58 struct blk_major_name **n, *p;
59 int index, ret = 0;
60
Jes Sorensen58383af2006-02-06 14:12:43 -080061 mutex_lock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 /* temporary */
64 if (major == 0) {
65 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
66 if (major_names[index] == NULL)
67 break;
68 }
69
70 if (index == 0) {
71 printk("register_blkdev: failed to get major for %s\n",
72 name);
73 ret = -EBUSY;
74 goto out;
75 }
76 major = index;
77 ret = major;
78 }
79
80 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
81 if (p == NULL) {
82 ret = -ENOMEM;
83 goto out;
84 }
85
86 p->major = major;
87 strlcpy(p->name, name, sizeof(p->name));
88 p->next = NULL;
89 index = major_to_index(major);
90
91 for (n = &major_names[index]; *n; n = &(*n)->next) {
92 if ((*n)->major == major)
93 break;
94 }
95 if (!*n)
96 *n = p;
97 else
98 ret = -EBUSY;
99
100 if (ret < 0) {
101 printk("register_blkdev: cannot get major %d for %s\n",
102 major, name);
103 kfree(p);
104 }
105out:
Jes Sorensen58383af2006-02-06 14:12:43 -0800106 mutex_unlock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return ret;
108}
109
110EXPORT_SYMBOL(register_blkdev);
111
Akinobu Mitaf4480242007-07-17 04:03:47 -0700112void unregister_blkdev(unsigned int major, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 struct blk_major_name **n;
115 struct blk_major_name *p = NULL;
116 int index = major_to_index(major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Jes Sorensen58383af2006-02-06 14:12:43 -0800118 mutex_lock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 for (n = &major_names[index]; *n; n = &(*n)->next)
120 if ((*n)->major == major)
121 break;
Akinobu Mita294462a2007-07-17 04:03:45 -0700122 if (!*n || strcmp((*n)->name, name)) {
123 WARN_ON(1);
Akinobu Mita294462a2007-07-17 04:03:45 -0700124 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 p = *n;
126 *n = p->next;
127 }
Jes Sorensen58383af2006-02-06 14:12:43 -0800128 mutex_unlock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132EXPORT_SYMBOL(unregister_blkdev);
133
134static struct kobj_map *bdev_map;
135
136/*
137 * Register device numbers dev..(dev+range-1)
138 * range must be nonzero
139 * The hash chain is sorted on range, so that subranges can override.
140 */
141void blk_register_region(dev_t dev, unsigned long range, struct module *module,
142 struct kobject *(*probe)(dev_t, int *, void *),
143 int (*lock)(dev_t, void *), void *data)
144{
145 kobj_map(bdev_map, dev, range, module, probe, lock, data);
146}
147
148EXPORT_SYMBOL(blk_register_region);
149
150void blk_unregister_region(dev_t dev, unsigned long range)
151{
152 kobj_unmap(bdev_map, dev, range);
153}
154
155EXPORT_SYMBOL(blk_unregister_region);
156
157static struct kobject *exact_match(dev_t dev, int *part, void *data)
158{
159 struct gendisk *p = data;
160 return &p->kobj;
161}
162
163static int exact_lock(dev_t dev, void *data)
164{
165 struct gendisk *p = data;
166
167 if (!get_disk(p))
168 return -1;
169 return 0;
170}
171
172/**
173 * add_disk - add partitioning information to kernel list
174 * @disk: per-device partitioning information
175 *
176 * This function registers the partitioning information in @disk
177 * with the kernel.
178 */
179void add_disk(struct gendisk *disk)
180{
181 disk->flags |= GENHD_FL_UP;
182 blk_register_region(MKDEV(disk->major, disk->first_minor),
183 disk->minors, NULL, exact_match, exact_lock, disk);
184 register_disk(disk);
185 blk_register_queue(disk);
186}
187
188EXPORT_SYMBOL(add_disk);
189EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
190
191void unlink_gendisk(struct gendisk *disk)
192{
193 blk_unregister_queue(disk);
194 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
195 disk->minors);
196}
197
198#define to_disk(obj) container_of(obj,struct gendisk,kobj)
199
200/**
201 * get_gendisk - get partitioning information for a given device
202 * @dev: device to get partitioning information for
203 *
204 * This function gets the structure containing partitioning
205 * information for the given device @dev.
206 */
207struct gendisk *get_gendisk(dev_t dev, int *part)
208{
209 struct kobject *kobj = kobj_lookup(bdev_map, dev, part);
210 return kobj ? to_disk(kobj) : NULL;
211}
212
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700213/*
214 * print a full list of all partitions - intended for places where the root
215 * filesystem can't be mounted and thus to give the victim some idea of what
216 * went wrong
217 */
218void __init printk_all_partitions(void)
219{
220 int n;
221 struct gendisk *sgp;
222
223 mutex_lock(&block_subsys_lock);
224 /* For each block device... */
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -0800225 list_for_each_entry(sgp, &block_kset->list, kobj.entry) {
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700226 char buf[BDEVNAME_SIZE];
227 /*
228 * Don't show empty devices or things that have been surpressed
229 */
230 if (get_capacity(sgp) == 0 ||
231 (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
232 continue;
233
234 /*
235 * Note, unlike /proc/partitions, I am showing the numbers in
236 * hex - the same format as the root= option takes.
237 */
238 printk("%02x%02x %10llu %s",
239 sgp->major, sgp->first_minor,
240 (unsigned long long)get_capacity(sgp) >> 1,
241 disk_name(sgp, 0, buf));
242 if (sgp->driverfs_dev != NULL &&
243 sgp->driverfs_dev->driver != NULL)
244 printk(" driver: %s\n",
245 sgp->driverfs_dev->driver->name);
246 else
247 printk(" (driver?)\n");
248
249 /* now show the partitions */
250 for (n = 0; n < sgp->minors - 1; ++n) {
251 if (sgp->part[n] == NULL)
252 continue;
253 if (sgp->part[n]->nr_sects == 0)
254 continue;
255 printk(" %02x%02x %10llu %s\n",
256 sgp->major, n + 1 + sgp->first_minor,
257 (unsigned long long)sgp->part[n]->nr_sects >> 1,
258 disk_name(sgp, n + 1, buf));
259 } /* partition subloop */
260 } /* Block device loop */
261
262 mutex_unlock(&block_subsys_lock);
263 return;
264}
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266#ifdef CONFIG_PROC_FS
267/* iterator */
268static void *part_start(struct seq_file *part, loff_t *pos)
269{
270 struct list_head *p;
271 loff_t l = *pos;
272
Jes Sorensen58383af2006-02-06 14:12:43 -0800273 mutex_lock(&block_subsys_lock);
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -0800274 list_for_each(p, &block_kset->list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (!l--)
276 return list_entry(p, struct gendisk, kobj.entry);
277 return NULL;
278}
279
280static void *part_next(struct seq_file *part, void *v, loff_t *pos)
281{
282 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
283 ++*pos;
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -0800284 return p==&block_kset->list ? NULL :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 list_entry(p, struct gendisk, kobj.entry);
286}
287
288static void part_stop(struct seq_file *part, void *v)
289{
Jes Sorensen58383af2006-02-06 14:12:43 -0800290 mutex_unlock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293static int show_partition(struct seq_file *part, void *v)
294{
295 struct gendisk *sgp = v;
296 int n;
297 char buf[BDEVNAME_SIZE];
298
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -0800299 if (&sgp->kobj.entry == block_kset->list.next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 seq_puts(part, "major minor #blocks name\n\n");
301
302 /* Don't show non-partitionable removeable devices or empty devices */
303 if (!get_capacity(sgp) ||
304 (sgp->minors == 1 && (sgp->flags & GENHD_FL_REMOVABLE)))
305 return 0;
306 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
307 return 0;
308
309 /* show the full disk and all non-0 size partitions of it */
310 seq_printf(part, "%4d %4d %10llu %s\n",
311 sgp->major, sgp->first_minor,
312 (unsigned long long)get_capacity(sgp) >> 1,
313 disk_name(sgp, 0, buf));
314 for (n = 0; n < sgp->minors - 1; n++) {
315 if (!sgp->part[n])
316 continue;
317 if (sgp->part[n]->nr_sects == 0)
318 continue;
319 seq_printf(part, "%4d %4d %10llu %s\n",
320 sgp->major, n + 1 + sgp->first_minor,
321 (unsigned long long)sgp->part[n]->nr_sects >> 1 ,
322 disk_name(sgp, n + 1, buf));
323 }
324
325 return 0;
326}
327
328struct seq_operations partitions_op = {
329 .start =part_start,
330 .next = part_next,
331 .stop = part_stop,
332 .show = show_partition
333};
334#endif
335
336
337extern int blk_dev_init(void);
338
339static struct kobject *base_probe(dev_t dev, int *part, void *data)
340{
341 if (request_module("block-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
342 /* Make old-style 2.4 aliases work */
343 request_module("block-major-%d", MAJOR(dev));
344 return NULL;
345}
346
347static int __init genhd_device_init(void)
348{
Jes Sorensen58383af2006-02-06 14:12:43 -0800349 bdev_map = kobj_map_init(base_probe, &block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 blk_dev_init();
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -0800351 block_kset = kset_create_and_add("block", &block_uevent_ops, NULL);
352 if (!block_kset) {
353 printk(KERN_WARNING "%s: kset_create error\n", __FUNCTION__);
354 return -ENOMEM;
355 }
356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
359subsys_initcall(genhd_device_init);
360
361
362
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{
369 struct gendisk *disk = to_disk(kobj);
370 struct disk_attribute *disk_attr =
371 container_of(attr,struct disk_attribute,attr);
Dmitry Torokhov6c1852a2005-04-29 01:26:06 -0500372 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 if (disk_attr->show)
375 ret = disk_attr->show(disk,page);
376 return ret;
377}
378
Kay Sieversa7fd6702005-10-01 14:49:43 +0200379static ssize_t disk_attr_store(struct kobject * kobj, struct attribute * attr,
380 const char *page, size_t count)
381{
382 struct gendisk *disk = to_disk(kobj);
383 struct disk_attribute *disk_attr =
384 container_of(attr,struct disk_attribute,attr);
385 ssize_t ret = 0;
386
387 if (disk_attr->store)
388 ret = disk_attr->store(disk, page, count);
389 return ret;
390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392static struct sysfs_ops disk_sysfs_ops = {
393 .show = &disk_attr_show,
Kay Sieversa7fd6702005-10-01 14:49:43 +0200394 .store = &disk_attr_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395};
396
Kay Sieversa7fd6702005-10-01 14:49:43 +0200397static ssize_t disk_uevent_store(struct gendisk * disk,
398 const char *buf, size_t count)
399{
Kay Sievers312c0042005-11-16 09:00:00 +0100400 kobject_uevent(&disk->kobj, KOBJ_ADD);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200401 return count;
402}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403static 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
417}
418static ssize_t disk_size_read(struct gendisk * disk, char *page)
419{
420 return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk));
421}
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700422static ssize_t disk_capability_read(struct gendisk *disk, char *page)
423{
424 return sprintf(page, "%x\n", disk->flags);
425}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426static ssize_t disk_stats_read(struct gendisk * disk, char *page)
427{
428 preempt_disable();
429 disk_round_stats(disk);
430 preempt_enable();
431 return sprintf(page,
Ben Woodard837c7872006-03-22 08:09:31 +0100432 "%8lu %8lu %8llu %8u "
433 "%8lu %8lu %8llu %8u "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 "%8u %8u %8u"
435 "\n",
Jens Axboe47a00412005-11-09 13:38:47 +0100436 disk_stat_read(disk, ios[READ]),
437 disk_stat_read(disk, merges[READ]),
438 (unsigned long long)disk_stat_read(disk, sectors[READ]),
439 jiffies_to_msecs(disk_stat_read(disk, ticks[READ])),
440 disk_stat_read(disk, ios[WRITE]),
441 disk_stat_read(disk, merges[WRITE]),
442 (unsigned long long)disk_stat_read(disk, sectors[WRITE]),
443 jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 disk->in_flight,
445 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
446 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
447}
Kay Sieversa7fd6702005-10-01 14:49:43 +0200448static struct disk_attribute disk_attr_uevent = {
449 .attr = {.name = "uevent", .mode = S_IWUSR },
450 .store = disk_uevent_store
451};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452static 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};
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700468static struct disk_attribute disk_attr_capability = {
469 .attr = {.name = "capability", .mode = S_IRUGO },
470 .show = disk_capability_read
471};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472static struct disk_attribute disk_attr_stat = {
473 .attr = {.name = "stat", .mode = S_IRUGO },
474 .show = disk_stats_read
475};
476
Akinobu Mitac17bb492006-12-08 02:39:46 -0800477#ifdef CONFIG_FAIL_MAKE_REQUEST
478
479static ssize_t disk_fail_store(struct gendisk * disk,
480 const char *buf, size_t count)
481{
482 int i;
483
484 if (count > 0 && sscanf(buf, "%d", &i) > 0) {
485 if (i == 0)
486 disk->flags &= ~GENHD_FL_FAIL;
487 else
488 disk->flags |= GENHD_FL_FAIL;
489 }
490
491 return count;
492}
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
503#endif
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505static struct attribute * default_attrs[] = {
Kay Sieversa7fd6702005-10-01 14:49:43 +0200506 &disk_attr_uevent.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 &disk_attr_dev.attr,
508 &disk_attr_range.attr,
509 &disk_attr_removable.attr,
510 &disk_attr_size.attr,
511 &disk_attr_stat.attr,
Kristen Carlson Accardi86ce18d2007-05-23 13:57:38 -0700512 &disk_attr_capability.attr,
Akinobu Mitac17bb492006-12-08 02:39:46 -0800513#ifdef CONFIG_FAIL_MAKE_REQUEST
514 &disk_attr_fail.attr,
515#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 NULL,
517};
518
519static void disk_release(struct kobject * kobj)
520{
521 struct gendisk *disk = to_disk(kobj);
522 kfree(disk->random);
523 kfree(disk->part);
524 free_disk_stats(disk);
525 kfree(disk);
526}
527
528static struct kobj_type ktype_block = {
529 .release = disk_release,
530 .sysfs_ops = &disk_sysfs_ops,
531 .default_attrs = default_attrs,
532};
533
534extern struct kobj_type ktype_part;
535
Kay Sievers312c0042005-11-16 09:00:00 +0100536static int block_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 struct kobj_type *ktype = get_ktype(kobj);
539
540 return ((ktype == &ktype_block) || (ktype == &ktype_part));
541}
542
Kay Sievers7eff2e72007-08-14 15:15:12 +0200543static int block_uevent(struct kset *kset, struct kobject *kobj,
544 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 struct kobj_type *ktype = get_ktype(kobj);
547 struct device *physdev;
548 struct gendisk *disk;
549 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 if (ktype == &ktype_block) {
552 disk = container_of(kobj, struct gendisk, kobj);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200553 add_uevent_var(env, "MINOR=%u", disk->first_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 } else if (ktype == &ktype_part) {
555 disk = container_of(kobj->parent, struct gendisk, kobj);
556 part = container_of(kobj, struct hd_struct, kobj);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200557 add_uevent_var(env, "MINOR=%u",
Kay Sievers312c0042005-11-16 09:00:00 +0100558 disk->first_minor + part->partno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 } else
560 return 0;
561
Kay Sievers7eff2e72007-08-14 15:15:12 +0200562 add_uevent_var(env, "MAJOR=%u", disk->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
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
Kay Sievers7eff2e72007-08-14 15:15:12 +0200569 add_uevent_var(env, "PHYSDEVPATH=%s", path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 kfree(path);
571
572 if (physdev->bus)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200573 add_uevent_var(env, "PHYSDEVBUS=%s", physdev->bus->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 if (physdev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200576 add_uevent_var(env, physdev->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return 0;
580}
581
Kay Sievers312c0042005-11-16 09:00:00 +0100582static struct kset_uevent_ops block_uevent_ops = {
583 .filter = block_uevent_filter,
584 .uevent = block_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585};
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587/*
588 * aggregate disk stat collector. Uses the same stats that the sysfs
589 * 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 *
593 * The output looks suspiciously like /proc/partitions with a bunch of
594 * extra fields.
595 */
596
597/* iterator */
598static void *diskstats_start(struct seq_file *part, loff_t *pos)
599{
600 loff_t k = *pos;
601 struct list_head *p;
602
Jes Sorensen58383af2006-02-06 14:12:43 -0800603 mutex_lock(&block_subsys_lock);
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -0800604 list_for_each(p, &block_kset->list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (!k--)
606 return list_entry(p, struct gendisk, kobj.entry);
607 return NULL;
608}
609
610static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos)
611{
612 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
613 ++*pos;
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -0800614 return p==&block_kset->list ? NULL :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 list_entry(p, struct gendisk, kobj.entry);
616}
617
618static void diskstats_stop(struct seq_file *part, void *v)
619{
Jes Sorensen58383af2006-02-06 14:12:43 -0800620 mutex_unlock(&block_subsys_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
623static int diskstats_show(struct seq_file *s, void *v)
624{
625 struct gendisk *gp = v;
626 char buf[BDEVNAME_SIZE];
627 int n = 0;
628
629 /*
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -0800630 if (&sgp->kobj.entry == block_kset->list.next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 seq_puts(s, "major minor name"
632 " rio rmerge rsect ruse wio wmerge "
633 "wsect wuse running use aveq"
634 "\n\n");
635 */
636
637 preempt_disable();
638 disk_round_stats(gp);
639 preempt_enable();
Ben Woodard837c7872006-03-22 08:09:31 +0100640 seq_printf(s, "%4d %4d %s %lu %lu %llu %u %lu %lu %llu %u %u %u %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 gp->major, n + gp->first_minor, disk_name(gp, n, buf),
Jens Axboea3623572005-11-01 09:26:16 +0100642 disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]),
643 (unsigned long long)disk_stat_read(gp, sectors[0]),
644 jiffies_to_msecs(disk_stat_read(gp, ticks[0])),
645 disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]),
646 (unsigned long long)disk_stat_read(gp, sectors[1]),
647 jiffies_to_msecs(disk_stat_read(gp, ticks[1])),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 gp->in_flight,
649 jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
650 jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
651
652 /* now show all non-0 size partitions of it */
653 for (n = 0; n < gp->minors - 1; n++) {
654 struct hd_struct *hd = gp->part[n];
655
656 if (hd && hd->nr_sects)
657 seq_printf(s, "%4d %4d %s %u %u %u %u\n",
658 gp->major, n + gp->first_minor + 1,
659 disk_name(gp, n + 1, buf),
Jens Axboea3623572005-11-01 09:26:16 +0100660 hd->ios[0], hd->sectors[0],
661 hd->ios[1], hd->sectors[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663
664 return 0;
665}
666
667struct seq_operations diskstats_op = {
668 .start = diskstats_start,
669 .next = diskstats_next,
670 .stop = diskstats_stop,
671 .show = diskstats_show
672};
673
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -0700674static void media_change_notify_thread(struct work_struct *work)
675{
676 struct gendisk *gd = container_of(work, struct gendisk, async_notify);
677 char event[] = "MEDIA_CHANGE=1";
678 char *envp[] = { event, NULL };
679
680 /*
681 * set enviroment vars to indicate which event this is for
682 * so that user space will know to go check the media status.
683 */
684 kobject_uevent_env(&gd->kobj, KOBJ_CHANGE, envp);
685 put_device(gd->driverfs_dev);
686}
687
688void genhd_media_change_notify(struct gendisk *disk)
689{
690 get_device(disk->driverfs_dev);
691 schedule_work(&disk->async_notify);
692}
693EXPORT_SYMBOL_GPL(genhd_media_change_notify);
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695struct gendisk *alloc_disk(int minors)
696{
Christoph Lameter19460892005-06-23 00:08:19 -0700697 return alloc_disk_node(minors, -1);
698}
699
700struct gendisk *alloc_disk_node(int minors, int node_id)
701{
702 struct gendisk *disk;
703
Christoph Lameter94f60302007-07-17 04:03:29 -0700704 disk = kmalloc_node(sizeof(struct gendisk),
705 GFP_KERNEL | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (disk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (!init_disk_stats(disk)) {
708 kfree(disk);
709 return NULL;
710 }
711 if (minors > 1) {
712 int size = (minors - 1) * sizeof(struct hd_struct *);
Christoph Lameter94f60302007-07-17 04:03:29 -0700713 disk->part = kmalloc_node(size,
714 GFP_KERNEL | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (!disk->part) {
Jerome Marchandc7674032007-11-23 09:17:53 +0100716 free_disk_stats(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 kfree(disk);
718 return NULL;
719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721 disk->minors = minors;
Greg Kroah-Hartman830d3cf2007-11-06 10:36:58 -0800722 disk->kobj.kset = block_kset;
Greg Kroah-Hartman3514fac2007-10-16 10:11:44 -0600723 disk->kobj.ktype = &ktype_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 kobject_init(&disk->kobj);
725 rand_initialize_disk(disk);
Kristen Carlson Accardi8ce7ad72007-05-23 13:57:38 -0700726 INIT_WORK(&disk->async_notify,
727 media_change_notify_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729 return disk;
730}
731
732EXPORT_SYMBOL(alloc_disk);
Christoph Lameter19460892005-06-23 00:08:19 -0700733EXPORT_SYMBOL(alloc_disk_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735struct kobject *get_disk(struct gendisk *disk)
736{
737 struct module *owner;
738 struct kobject *kobj;
739
740 if (!disk->fops)
741 return NULL;
742 owner = disk->fops->owner;
743 if (owner && !try_module_get(owner))
744 return NULL;
745 kobj = kobject_get(&disk->kobj);
746 if (kobj == NULL) {
747 module_put(owner);
748 return NULL;
749 }
750 return kobj;
751
752}
753
754EXPORT_SYMBOL(get_disk);
755
756void put_disk(struct gendisk *disk)
757{
758 if (disk)
759 kobject_put(&disk->kobj);
760}
761
762EXPORT_SYMBOL(put_disk);
763
764void set_device_ro(struct block_device *bdev, int flag)
765{
766 if (bdev->bd_contains != bdev)
767 bdev->bd_part->policy = flag;
768 else
769 bdev->bd_disk->policy = flag;
770}
771
772EXPORT_SYMBOL(set_device_ro);
773
774void set_disk_ro(struct gendisk *disk, int flag)
775{
776 int i;
777 disk->policy = flag;
778 for (i = 0; i < disk->minors - 1; i++)
779 if (disk->part[i]) disk->part[i]->policy = flag;
780}
781
782EXPORT_SYMBOL(set_disk_ro);
783
784int bdev_read_only(struct block_device *bdev)
785{
786 if (!bdev)
787 return 0;
788 else if (bdev->bd_contains != bdev)
789 return bdev->bd_part->policy;
790 else
791 return bdev->bd_disk->policy;
792}
793
794EXPORT_SYMBOL(bdev_read_only);
795
796int invalidate_partition(struct gendisk *disk, int index)
797{
798 int res = 0;
799 struct block_device *bdev = bdget_disk(disk, index);
800 if (bdev) {
Christoph Hellwig2ef41632005-05-05 16:15:59 -0700801 fsync_bdev(bdev);
802 res = __invalidate_device(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 bdput(bdev);
804 }
805 return res;
806}
807
808EXPORT_SYMBOL(invalidate_partition);