aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/block/genhd.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/block/genhd.c')
-rw-r--r--drivers/block/genhd.c685
1 files changed, 685 insertions, 0 deletions
diff --git a/drivers/block/genhd.c b/drivers/block/genhd.c
new file mode 100644
index 000000000000..ab4db71375e0
--- /dev/null
+++ b/drivers/block/genhd.c
@@ -0,0 +1,685 @@
1/*
2 * gendisk handling
3 */
4
5#include <linux/config.h>
6#include <linux/module.h>
7#include <linux/fs.h>
8#include <linux/genhd.h>
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>
17
18#define MAX_PROBE_HASH 255 /* random */
19
20static struct subsystem block_subsys;
21
22static DECLARE_MUTEX(block_subsys_sem);
23
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];
32} *major_names[MAX_PROBE_HASH];
33
34/* index in the above - for now: assume no multimajor ranges */
35static inline int major_to_index(int major)
36{
37 return major % MAX_PROBE_HASH;
38}
39
40#ifdef CONFIG_PROC_FS
41/* get block device names in somewhat random order */
42int get_blkdev_list(char *p)
43{
44 struct blk_major_name *n;
45 int i, len;
46
47 len = sprintf(p, "\nBlock devices:\n");
48
49 down(&block_subsys_sem);
50 for (i = 0; i < ARRAY_SIZE(major_names); i++) {
51 for (n = major_names[i]; n; n = n->next)
52 len += sprintf(p+len, "%3d %s\n",
53 n->major, n->name);
54 }
55 up(&block_subsys_sem);
56
57 return len;
58}
59#endif
60
61int register_blkdev(unsigned int major, const char *name)
62{
63 struct blk_major_name **n, *p;
64 int index, ret = 0;
65
66 down(&block_subsys_sem);
67
68 /* temporary */
69 if (major == 0) {
70 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
71 if (major_names[index] == NULL)
72 break;
73 }
74
75 if (index == 0) {
76 printk("register_blkdev: failed to get major for %s\n",
77 name);
78 ret = -EBUSY;
79 goto out;
80 }
81 major = index;
82 ret = major;
83 }
84
85 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
86 if (p == NULL) {
87 ret = -ENOMEM;
88 goto out;
89 }
90
91 p->major = major;
92 strlcpy(p->name, name, sizeof(p->name));
93 p->next = NULL;
94 index = major_to_index(major);
95
96 for (n = &major_names[index]; *n; n = &(*n)->next) {
97 if ((*n)->major == major)
98 break;
99 }
100 if (!*n)
101 *n = p;
102 else
103 ret = -EBUSY;
104
105 if (ret < 0) {
106 printk("register_blkdev: cannot get major %d for %s\n",
107 major, name);
108 kfree(p);
109 }
110out:
111 up(&block_subsys_sem);
112 return ret;
113}
114
115EXPORT_SYMBOL(register_blkdev);
116
117/* todo: make void - error printk here */
118int unregister_blkdev(unsigned int major, const char *name)
119{
120 struct blk_major_name **n;
121 struct blk_major_name *p = NULL;
122 int index = major_to_index(major);
123 int ret = 0;
124
125 down(&block_subsys_sem);
126 for (n = &major_names[index]; *n; n = &(*n)->next)
127 if ((*n)->major == major)
128 break;
129 if (!*n || strcmp((*n)->name, name))
130 ret = -EINVAL;
131 else {
132 p = *n;
133 *n = p->next;
134 }
135 up(&block_subsys_sem);
136 kfree(p);
137
138 return ret;
139}
140
141EXPORT_SYMBOL(unregister_blkdev);
142
143static struct kobj_map *bdev_map;
144
145/*
146 * Register device numbers dev..(dev+range-1)
147 * range must be nonzero
148 * The hash chain is sorted on range, so that subranges can override.
149 */
150void blk_register_region(dev_t dev, unsigned long range, struct module *module,
151 struct kobject *(*probe)(dev_t, int *, void *),
152 int (*lock)(dev_t, void *), void *data)
153{
154 kobj_map(bdev_map, dev, range, module, probe, lock, data);
155}
156
157EXPORT_SYMBOL(blk_register_region);
158
159void blk_unregister_region(dev_t dev, unsigned long range)
160{
161 kobj_unmap(bdev_map, dev, range);
162}
163
164EXPORT_SYMBOL(blk_unregister_region);
165
166static struct kobject *exact_match(dev_t dev, int *part, void *data)
167{
168 struct gendisk *p = data;
169 return &p->kobj;
170}
171
172static int exact_lock(dev_t dev, void *data)
173{
174 struct gendisk *p = data;
175
176 if (!get_disk(p))
177 return -1;
178 return 0;
179}
180
181/**
182 * add_disk - add partitioning information to kernel list
183 * @disk: per-device partitioning information
184 *
185 * This function registers the partitioning information in @disk
186 * with the kernel.
187 */
188void add_disk(struct gendisk *disk)
189{
190 disk->flags |= GENHD_FL_UP;
191 blk_register_region(MKDEV(disk->major, disk->first_minor),
192 disk->minors, NULL, exact_match, exact_lock, disk);
193 register_disk(disk);
194 blk_register_queue(disk);
195}
196
197EXPORT_SYMBOL(add_disk);
198EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
199
200void unlink_gendisk(struct gendisk *disk)
201{
202 blk_unregister_queue(disk);
203 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
204 disk->minors);
205}
206
207#define to_disk(obj) container_of(obj,struct gendisk,kobj)
208
209/**
210 * get_gendisk - get partitioning information for a given device
211 * @dev: device to get partitioning information for
212 *
213 * This function gets the structure containing partitioning
214 * information for the given device @dev.
215 */
216struct gendisk *get_gendisk(dev_t dev, int *part)
217{
218 struct kobject *kobj = kobj_lookup(bdev_map, dev, part);
219 return kobj ? to_disk(kobj) : NULL;
220}
221
222#ifdef CONFIG_PROC_FS
223/* iterator */
224static void *part_start(struct seq_file *part, loff_t *pos)
225{
226 struct list_head *p;
227 loff_t l = *pos;
228
229 down(&block_subsys_sem);
230 list_for_each(p, &block_subsys.kset.list)
231 if (!l--)
232 return list_entry(p, struct gendisk, kobj.entry);
233 return NULL;
234}
235
236static void *part_next(struct seq_file *part, void *v, loff_t *pos)
237{
238 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
239 ++*pos;
240 return p==&block_subsys.kset.list ? NULL :
241 list_entry(p, struct gendisk, kobj.entry);
242}
243
244static void part_stop(struct seq_file *part, void *v)
245{
246 up(&block_subsys_sem);
247}
248
249static int show_partition(struct seq_file *part, void *v)
250{
251 struct gendisk *sgp = v;
252 int n;
253 char buf[BDEVNAME_SIZE];
254
255 if (&sgp->kobj.entry == block_subsys.kset.list.next)
256 seq_puts(part, "major minor #blocks name\n\n");
257
258 /* Don't show non-partitionable removeable devices or empty devices */
259 if (!get_capacity(sgp) ||
260 (sgp->minors == 1 && (sgp->flags & GENHD_FL_REMOVABLE)))
261 return 0;
262 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
263 return 0;
264
265 /* show the full disk and all non-0 size partitions of it */
266 seq_printf(part, "%4d %4d %10llu %s\n",
267 sgp->major, sgp->first_minor,
268 (unsigned long long)get_capacity(sgp) >> 1,
269 disk_name(sgp, 0, buf));
270 for (n = 0; n < sgp->minors - 1; n++) {
271 if (!sgp->part[n])
272 continue;
273 if (sgp->part[n]->nr_sects == 0)
274 continue;
275 seq_printf(part, "%4d %4d %10llu %s\n",
276 sgp->major, n + 1 + sgp->first_minor,
277 (unsigned long long)sgp->part[n]->nr_sects >> 1 ,
278 disk_name(sgp, n + 1, buf));
279 }
280
281 return 0;
282}
283
284struct seq_operations partitions_op = {
285 .start =part_start,
286 .next = part_next,
287 .stop = part_stop,
288 .show = show_partition
289};
290#endif
291
292
293extern int blk_dev_init(void);
294
295static struct kobject *base_probe(dev_t dev, int *part, void *data)
296{
297 if (request_module("block-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
298 /* Make old-style 2.4 aliases work */
299 request_module("block-major-%d", MAJOR(dev));
300 return NULL;
301}
302
303static int __init genhd_device_init(void)
304{
305 bdev_map = kobj_map_init(base_probe, &block_subsys_sem);
306 blk_dev_init();
307 subsystem_register(&block_subsys);
308 return 0;
309}
310
311subsys_initcall(genhd_device_init);
312
313
314
315/*
316 * kobject & sysfs bindings for block devices
317 */
318static ssize_t disk_attr_show(struct kobject *kobj, struct attribute *attr,
319 char *page)
320{
321 struct gendisk *disk = to_disk(kobj);
322 struct disk_attribute *disk_attr =
323 container_of(attr,struct disk_attribute,attr);
324 ssize_t ret = 0;
325
326 if (disk_attr->show)
327 ret = disk_attr->show(disk,page);
328 return ret;
329}
330
331static struct sysfs_ops disk_sysfs_ops = {
332 .show = &disk_attr_show,
333};
334
335static ssize_t disk_dev_read(struct gendisk * disk, char *page)
336{
337 dev_t base = MKDEV(disk->major, disk->first_minor);
338 return print_dev_t(page, base);
339}
340static ssize_t disk_range_read(struct gendisk * disk, char *page)
341{
342 return sprintf(page, "%d\n", disk->minors);
343}
344static ssize_t disk_removable_read(struct gendisk * disk, char *page)
345{
346 return sprintf(page, "%d\n",
347 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
348
349}
350static ssize_t disk_size_read(struct gendisk * disk, char *page)
351{
352 return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk));
353}
354
355static ssize_t disk_stats_read(struct gendisk * disk, char *page)
356{
357 preempt_disable();
358 disk_round_stats(disk);
359 preempt_enable();
360 return sprintf(page,
361 "%8u %8u %8llu %8u "
362 "%8u %8u %8llu %8u "
363 "%8u %8u %8u"
364 "\n",
365 disk_stat_read(disk, reads), disk_stat_read(disk, read_merges),
366 (unsigned long long)disk_stat_read(disk, read_sectors),
367 jiffies_to_msecs(disk_stat_read(disk, read_ticks)),
368 disk_stat_read(disk, writes),
369 disk_stat_read(disk, write_merges),
370 (unsigned long long)disk_stat_read(disk, write_sectors),
371 jiffies_to_msecs(disk_stat_read(disk, write_ticks)),
372 disk->in_flight,
373 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
374 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
375}
376static struct disk_attribute disk_attr_dev = {
377 .attr = {.name = "dev", .mode = S_IRUGO },
378 .show = disk_dev_read
379};
380static struct disk_attribute disk_attr_range = {
381 .attr = {.name = "range", .mode = S_IRUGO },
382 .show = disk_range_read
383};
384static struct disk_attribute disk_attr_removable = {
385 .attr = {.name = "removable", .mode = S_IRUGO },
386 .show = disk_removable_read
387};
388static struct disk_attribute disk_attr_size = {
389 .attr = {.name = "size", .mode = S_IRUGO },
390 .show = disk_size_read
391};
392static struct disk_attribute disk_attr_stat = {
393 .attr = {.name = "stat", .mode = S_IRUGO },
394 .show = disk_stats_read
395};
396
397static struct attribute * default_attrs[] = {
398 &disk_attr_dev.attr,
399 &disk_attr_range.attr,
400 &disk_attr_removable.attr,
401 &disk_attr_size.attr,
402 &disk_attr_stat.attr,
403 NULL,
404};
405
406static void disk_release(struct kobject * kobj)
407{
408 struct gendisk *disk = to_disk(kobj);
409 kfree(disk->random);
410 kfree(disk->part);
411 free_disk_stats(disk);
412 kfree(disk);
413}
414
415static struct kobj_type ktype_block = {
416 .release = disk_release,
417 .sysfs_ops = &disk_sysfs_ops,
418 .default_attrs = default_attrs,
419};
420
421extern struct kobj_type ktype_part;
422
423static int block_hotplug_filter(struct kset *kset, struct kobject *kobj)
424{
425 struct kobj_type *ktype = get_ktype(kobj);
426
427 return ((ktype == &ktype_block) || (ktype == &ktype_part));
428}
429
430static int block_hotplug(struct kset *kset, struct kobject *kobj, char **envp,
431 int num_envp, char *buffer, int buffer_size)
432{
433 struct kobj_type *ktype = get_ktype(kobj);
434 struct device *physdev;
435 struct gendisk *disk;
436 struct hd_struct *part;
437 int length = 0;
438 int i = 0;
439
440 if (ktype == &ktype_block) {
441 disk = container_of(kobj, struct gendisk, kobj);
442 add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size,
443 &length, "MINOR=%u", disk->first_minor);
444 } else if (ktype == &ktype_part) {
445 disk = container_of(kobj->parent, struct gendisk, kobj);
446 part = container_of(kobj, struct hd_struct, kobj);
447 add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size,
448 &length, "MINOR=%u",
449 disk->first_minor + part->partno);
450 } else
451 return 0;
452
453 add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, &length,
454 "MAJOR=%u", disk->major);
455
456 /* add physical device, backing this device */
457 physdev = disk->driverfs_dev;
458 if (physdev) {
459 char *path = kobject_get_path(&physdev->kobj, GFP_KERNEL);
460
461 add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size,
462 &length, "PHYSDEVPATH=%s", path);
463 kfree(path);
464
465 if (physdev->bus)
466 add_hotplug_env_var(envp, num_envp, &i,
467 buffer, buffer_size, &length,
468 "PHYSDEVBUS=%s",
469 physdev->bus->name);
470
471 if (physdev->driver)
472 add_hotplug_env_var(envp, num_envp, &i,
473 buffer, buffer_size, &length,
474 "PHYSDEVDRIVER=%s",
475 physdev->driver->name);
476 }
477
478 /* terminate, set to next free slot, shrink available space */
479 envp[i] = NULL;
480 envp = &envp[i];
481 num_envp -= i;
482 buffer = &buffer[length];
483 buffer_size -= length;
484
485 return 0;
486}
487
488static struct kset_hotplug_ops block_hotplug_ops = {
489 .filter = block_hotplug_filter,
490 .hotplug = block_hotplug,
491};
492
493/* declare block_subsys. */
494static decl_subsys(block, &ktype_block, &block_hotplug_ops);
495
496
497/*
498 * aggregate disk stat collector. Uses the same stats that the sysfs
499 * entries do, above, but makes them available through one seq_file.
500 * Watching a few disks may be efficient through sysfs, but watching
501 * all of them will be more efficient through this interface.
502 *
503 * The output looks suspiciously like /proc/partitions with a bunch of
504 * extra fields.
505 */
506
507/* iterator */
508static void *diskstats_start(struct seq_file *part, loff_t *pos)
509{
510 loff_t k = *pos;
511 struct list_head *p;
512
513 down(&block_subsys_sem);
514 list_for_each(p, &block_subsys.kset.list)
515 if (!k--)
516 return list_entry(p, struct gendisk, kobj.entry);
517 return NULL;
518}
519
520static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos)
521{
522 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
523 ++*pos;
524 return p==&block_subsys.kset.list ? NULL :
525 list_entry(p, struct gendisk, kobj.entry);
526}
527
528static void diskstats_stop(struct seq_file *part, void *v)
529{
530 up(&block_subsys_sem);
531}
532
533static int diskstats_show(struct seq_file *s, void *v)
534{
535 struct gendisk *gp = v;
536 char buf[BDEVNAME_SIZE];
537 int n = 0;
538
539 /*
540 if (&sgp->kobj.entry == block_subsys.kset.list.next)
541 seq_puts(s, "major minor name"
542 " rio rmerge rsect ruse wio wmerge "
543 "wsect wuse running use aveq"
544 "\n\n");
545 */
546
547 preempt_disable();
548 disk_round_stats(gp);
549 preempt_enable();
550 seq_printf(s, "%4d %4d %s %u %u %llu %u %u %u %llu %u %u %u %u\n",
551 gp->major, n + gp->first_minor, disk_name(gp, n, buf),
552 disk_stat_read(gp, reads), disk_stat_read(gp, read_merges),
553 (unsigned long long)disk_stat_read(gp, read_sectors),
554 jiffies_to_msecs(disk_stat_read(gp, read_ticks)),
555 disk_stat_read(gp, writes), disk_stat_read(gp, write_merges),
556 (unsigned long long)disk_stat_read(gp, write_sectors),
557 jiffies_to_msecs(disk_stat_read(gp, write_ticks)),
558 gp->in_flight,
559 jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
560 jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
561
562 /* now show all non-0 size partitions of it */
563 for (n = 0; n < gp->minors - 1; n++) {
564 struct hd_struct *hd = gp->part[n];
565
566 if (hd && hd->nr_sects)
567 seq_printf(s, "%4d %4d %s %u %u %u %u\n",
568 gp->major, n + gp->first_minor + 1,
569 disk_name(gp, n + 1, buf),
570 hd->reads, hd->read_sectors,
571 hd->writes, hd->write_sectors);
572 }
573
574 return 0;
575}
576
577struct seq_operations diskstats_op = {
578 .start = diskstats_start,
579 .next = diskstats_next,
580 .stop = diskstats_stop,
581 .show = diskstats_show
582};
583
584
585struct gendisk *alloc_disk(int minors)
586{
587 struct gendisk *disk = kmalloc(sizeof(struct gendisk), GFP_KERNEL);
588 if (disk) {
589 memset(disk, 0, sizeof(struct gendisk));
590 if (!init_disk_stats(disk)) {
591 kfree(disk);
592 return NULL;
593 }
594 if (minors > 1) {
595 int size = (minors - 1) * sizeof(struct hd_struct *);
596 disk->part = kmalloc(size, GFP_KERNEL);
597 if (!disk->part) {
598 kfree(disk);
599 return NULL;
600 }
601 memset(disk->part, 0, size);
602 }
603 disk->minors = minors;
604 kobj_set_kset_s(disk,block_subsys);
605 kobject_init(&disk->kobj);
606 rand_initialize_disk(disk);
607 }
608 return disk;
609}
610
611EXPORT_SYMBOL(alloc_disk);
612
613struct kobject *get_disk(struct gendisk *disk)
614{
615 struct module *owner;
616 struct kobject *kobj;
617
618 if (!disk->fops)
619 return NULL;
620 owner = disk->fops->owner;
621 if (owner && !try_module_get(owner))
622 return NULL;
623 kobj = kobject_get(&disk->kobj);
624 if (kobj == NULL) {
625 module_put(owner);
626 return NULL;
627 }
628 return kobj;
629
630}
631
632EXPORT_SYMBOL(get_disk);
633
634void put_disk(struct gendisk *disk)
635{
636 if (disk)
637 kobject_put(&disk->kobj);
638}
639
640EXPORT_SYMBOL(put_disk);
641
642void set_device_ro(struct block_device *bdev, int flag)
643{
644 if (bdev->bd_contains != bdev)
645 bdev->bd_part->policy = flag;
646 else
647 bdev->bd_disk->policy = flag;
648}
649
650EXPORT_SYMBOL(set_device_ro);
651
652void set_disk_ro(struct gendisk *disk, int flag)
653{
654 int i;
655 disk->policy = flag;
656 for (i = 0; i < disk->minors - 1; i++)
657 if (disk->part[i]) disk->part[i]->policy = flag;
658}
659
660EXPORT_SYMBOL(set_disk_ro);
661
662int bdev_read_only(struct block_device *bdev)
663{
664 if (!bdev)
665 return 0;
666 else if (bdev->bd_contains != bdev)
667 return bdev->bd_part->policy;
668 else
669 return bdev->bd_disk->policy;
670}
671
672EXPORT_SYMBOL(bdev_read_only);
673
674int invalidate_partition(struct gendisk *disk, int index)
675{
676 int res = 0;
677 struct block_device *bdev = bdget_disk(disk, index);
678 if (bdev) {
679 res = __invalidate_device(bdev, 1);
680 bdput(bdev);
681 }
682 return res;
683}
684
685EXPORT_SYMBOL(invalidate_partition);