]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - block/genhd.c
unregister_blkdev(): delete redundant message
[mirror_ubuntu-bionic-kernel.git] / block / genhd.c
CommitLineData
1da177e4
LT
1/*
2 * gendisk handling
3 */
4
1da177e4
LT
5#include <linux/module.h>
6#include <linux/fs.h>
7#include <linux/genhd.h>
b446b60e 8#include <linux/kdev_t.h>
1da177e4
LT
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>
2ef41634 17#include <linux/buffer_head.h>
58383af6 18#include <linux/mutex.h>
1da177e4 19
823bccfc 20struct kset block_subsys;
58383af6 21static DEFINE_MUTEX(block_subsys_lock);
1da177e4
LT
22
23/*
24 * Can be deleted altogether. Later.
25 *
26 */
27static struct blk_major_name {
28 struct blk_major_name *next;
29 int major;
30 char name[16];
68eef3b4 31} *major_names[BLKDEV_MAJOR_HASH_SIZE];
1da177e4
LT
32
33/* index in the above - for now: assume no multimajor ranges */
34static inline int major_to_index(int major)
35{
68eef3b4 36 return major % BLKDEV_MAJOR_HASH_SIZE;
7170be5f
NH
37}
38
68eef3b4 39#ifdef CONFIG_PROC_FS
7170be5f 40
68eef3b4 41void blkdev_show(struct seq_file *f, off_t offset)
7170be5f 42{
68eef3b4 43 struct blk_major_name *dp;
7170be5f 44
68eef3b4
JK
45 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
46 mutex_lock(&block_subsys_lock);
47 for (dp = major_names[offset]; dp; dp = dp->next)
48 seq_printf(f, "%3d %s\n", dp->major, dp->name);
49 mutex_unlock(&block_subsys_lock);
1da177e4 50 }
1da177e4 51}
7170be5f 52
68eef3b4 53#endif /* CONFIG_PROC_FS */
1da177e4
LT
54
55int register_blkdev(unsigned int major, const char *name)
56{
57 struct blk_major_name **n, *p;
58 int index, ret = 0;
59
58383af6 60 mutex_lock(&block_subsys_lock);
1da177e4
LT
61
62 /* temporary */
63 if (major == 0) {
64 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
65 if (major_names[index] == NULL)
66 break;
67 }
68
69 if (index == 0) {
70 printk("register_blkdev: failed to get major for %s\n",
71 name);
72 ret = -EBUSY;
73 goto out;
74 }
75 major = index;
76 ret = major;
77 }
78
79 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
80 if (p == NULL) {
81 ret = -ENOMEM;
82 goto out;
83 }
84
85 p->major = major;
86 strlcpy(p->name, name, sizeof(p->name));
87 p->next = NULL;
88 index = major_to_index(major);
89
90 for (n = &major_names[index]; *n; n = &(*n)->next) {
91 if ((*n)->major == major)
92 break;
93 }
94 if (!*n)
95 *n = p;
96 else
97 ret = -EBUSY;
98
99 if (ret < 0) {
100 printk("register_blkdev: cannot get major %d for %s\n",
101 major, name);
102 kfree(p);
103 }
104out:
58383af6 105 mutex_unlock(&block_subsys_lock);
1da177e4
LT
106 return ret;
107}
108
109EXPORT_SYMBOL(register_blkdev);
110
111/* todo: make void - error printk here */
112int unregister_blkdev(unsigned int major, const char *name)
113{
114 struct blk_major_name **n;
115 struct blk_major_name *p = NULL;
116 int index = major_to_index(major);
117 int ret = 0;
118
58383af6 119 mutex_lock(&block_subsys_lock);
1da177e4
LT
120 for (n = &major_names[index]; *n; n = &(*n)->next)
121 if ((*n)->major == major)
122 break;
294462a5
AM
123 if (!*n || strcmp((*n)->name, name)) {
124 WARN_ON(1);
1da177e4 125 ret = -EINVAL;
294462a5 126 } else {
1da177e4
LT
127 p = *n;
128 *n = p->next;
129 }
58383af6 130 mutex_unlock(&block_subsys_lock);
1da177e4
LT
131 kfree(p);
132
133 return ret;
134}
135
136EXPORT_SYMBOL(unregister_blkdev);
137
138static struct kobj_map *bdev_map;
139
140/*
141 * Register device numbers dev..(dev+range-1)
142 * range must be nonzero
143 * The hash chain is sorted on range, so that subranges can override.
144 */
145void blk_register_region(dev_t dev, unsigned long range, struct module *module,
146 struct kobject *(*probe)(dev_t, int *, void *),
147 int (*lock)(dev_t, void *), void *data)
148{
149 kobj_map(bdev_map, dev, range, module, probe, lock, data);
150}
151
152EXPORT_SYMBOL(blk_register_region);
153
154void blk_unregister_region(dev_t dev, unsigned long range)
155{
156 kobj_unmap(bdev_map, dev, range);
157}
158
159EXPORT_SYMBOL(blk_unregister_region);
160
161static struct kobject *exact_match(dev_t dev, int *part, void *data)
162{
163 struct gendisk *p = data;
164 return &p->kobj;
165}
166
167static int exact_lock(dev_t dev, void *data)
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 */
183void add_disk(struct gendisk *disk)
184{
185 disk->flags |= GENHD_FL_UP;
186 blk_register_region(MKDEV(disk->major, disk->first_minor),
187 disk->minors, NULL, exact_match, exact_lock, disk);
188 register_disk(disk);
189 blk_register_queue(disk);
190}
191
192EXPORT_SYMBOL(add_disk);
193EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
194
195void unlink_gendisk(struct gendisk *disk)
196{
197 blk_unregister_queue(disk);
198 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
199 disk->minors);
200}
201
202#define to_disk(obj) container_of(obj,struct gendisk,kobj)
203
204/**
205 * get_gendisk - get partitioning information for a given device
206 * @dev: device to get partitioning information for
207 *
208 * This function gets the structure containing partitioning
209 * information for the given device @dev.
210 */
211struct gendisk *get_gendisk(dev_t dev, int *part)
212{
213 struct kobject *kobj = kobj_lookup(bdev_map, dev, part);
214 return kobj ? to_disk(kobj) : NULL;
215}
216
dd2a345f
DG
217/*
218 * print a full list of all partitions - intended for places where the root
219 * filesystem can't be mounted and thus to give the victim some idea of what
220 * went wrong
221 */
222void __init printk_all_partitions(void)
223{
224 int n;
225 struct gendisk *sgp;
226
227 mutex_lock(&block_subsys_lock);
228 /* For each block device... */
229 list_for_each_entry(sgp, &block_subsys.list, kobj.entry) {
230 char buf[BDEVNAME_SIZE];
231 /*
232 * Don't show empty devices or things that have been surpressed
233 */
234 if (get_capacity(sgp) == 0 ||
235 (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
236 continue;
237
238 /*
239 * Note, unlike /proc/partitions, I am showing the numbers in
240 * hex - the same format as the root= option takes.
241 */
242 printk("%02x%02x %10llu %s",
243 sgp->major, sgp->first_minor,
244 (unsigned long long)get_capacity(sgp) >> 1,
245 disk_name(sgp, 0, buf));
246 if (sgp->driverfs_dev != NULL &&
247 sgp->driverfs_dev->driver != NULL)
248 printk(" driver: %s\n",
249 sgp->driverfs_dev->driver->name);
250 else
251 printk(" (driver?)\n");
252
253 /* now show the partitions */
254 for (n = 0; n < sgp->minors - 1; ++n) {
255 if (sgp->part[n] == NULL)
256 continue;
257 if (sgp->part[n]->nr_sects == 0)
258 continue;
259 printk(" %02x%02x %10llu %s\n",
260 sgp->major, n + 1 + sgp->first_minor,
261 (unsigned long long)sgp->part[n]->nr_sects >> 1,
262 disk_name(sgp, n + 1, buf));
263 } /* partition subloop */
264 } /* Block device loop */
265
266 mutex_unlock(&block_subsys_lock);
267 return;
268}
269
1da177e4
LT
270#ifdef CONFIG_PROC_FS
271/* iterator */
272static void *part_start(struct seq_file *part, loff_t *pos)
273{
274 struct list_head *p;
275 loff_t l = *pos;
276
58383af6 277 mutex_lock(&block_subsys_lock);
823bccfc 278 list_for_each(p, &block_subsys.list)
1da177e4
LT
279 if (!l--)
280 return list_entry(p, struct gendisk, kobj.entry);
281 return NULL;
282}
283
284static void *part_next(struct seq_file *part, void *v, loff_t *pos)
285{
286 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
287 ++*pos;
823bccfc 288 return p==&block_subsys.list ? NULL :
1da177e4
LT
289 list_entry(p, struct gendisk, kobj.entry);
290}
291
292static void part_stop(struct seq_file *part, void *v)
293{
58383af6 294 mutex_unlock(&block_subsys_lock);
1da177e4
LT
295}
296
297static int show_partition(struct seq_file *part, void *v)
298{
299 struct gendisk *sgp = v;
300 int n;
301 char buf[BDEVNAME_SIZE];
302
823bccfc 303 if (&sgp->kobj.entry == block_subsys.list.next)
1da177e4
LT
304 seq_puts(part, "major minor #blocks name\n\n");
305
306 /* Don't show non-partitionable removeable devices or empty devices */
307 if (!get_capacity(sgp) ||
308 (sgp->minors == 1 && (sgp->flags & GENHD_FL_REMOVABLE)))
309 return 0;
310 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
311 return 0;
312
313 /* show the full disk and all non-0 size partitions of it */
314 seq_printf(part, "%4d %4d %10llu %s\n",
315 sgp->major, sgp->first_minor,
316 (unsigned long long)get_capacity(sgp) >> 1,
317 disk_name(sgp, 0, buf));
318 for (n = 0; n < sgp->minors - 1; n++) {
319 if (!sgp->part[n])
320 continue;
321 if (sgp->part[n]->nr_sects == 0)
322 continue;
323 seq_printf(part, "%4d %4d %10llu %s\n",
324 sgp->major, n + 1 + sgp->first_minor,
325 (unsigned long long)sgp->part[n]->nr_sects >> 1 ,
326 disk_name(sgp, n + 1, buf));
327 }
328
329 return 0;
330}
331
332struct seq_operations partitions_op = {
333 .start =part_start,
334 .next = part_next,
335 .stop = part_stop,
336 .show = show_partition
337};
338#endif
339
340
341extern int blk_dev_init(void);
342
343static struct kobject *base_probe(dev_t dev, int *part, void *data)
344{
345 if (request_module("block-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
346 /* Make old-style 2.4 aliases work */
347 request_module("block-major-%d", MAJOR(dev));
348 return NULL;
349}
350
351static int __init genhd_device_init(void)
352{
87a57261
RD
353 int err;
354
58383af6 355 bdev_map = kobj_map_init(base_probe, &block_subsys_lock);
1da177e4 356 blk_dev_init();
87a57261
RD
357 err = subsystem_register(&block_subsys);
358 if (err < 0)
359 printk(KERN_WARNING "%s: subsystem_register error: %d\n",
360 __FUNCTION__, err);
361 return err;
1da177e4
LT
362}
363
364subsys_initcall(genhd_device_init);
365
366
367
368/*
369 * kobject & sysfs bindings for block devices
370 */
371static ssize_t disk_attr_show(struct kobject *kobj, struct attribute *attr,
372 char *page)
373{
374 struct gendisk *disk = to_disk(kobj);
375 struct disk_attribute *disk_attr =
376 container_of(attr,struct disk_attribute,attr);
6c1852a0 377 ssize_t ret = -EIO;
1da177e4
LT
378
379 if (disk_attr->show)
380 ret = disk_attr->show(disk,page);
381 return ret;
382}
383
a7fd6706
KS
384static ssize_t disk_attr_store(struct kobject * kobj, struct attribute * attr,
385 const char *page, size_t count)
386{
387 struct gendisk *disk = to_disk(kobj);
388 struct disk_attribute *disk_attr =
389 container_of(attr,struct disk_attribute,attr);
390 ssize_t ret = 0;
391
392 if (disk_attr->store)
393 ret = disk_attr->store(disk, page, count);
394 return ret;
395}
396
1da177e4
LT
397static struct sysfs_ops disk_sysfs_ops = {
398 .show = &disk_attr_show,
a7fd6706 399 .store = &disk_attr_store,
1da177e4
LT
400};
401
a7fd6706
KS
402static ssize_t disk_uevent_store(struct gendisk * disk,
403 const char *buf, size_t count)
404{
312c004d 405 kobject_uevent(&disk->kobj, KOBJ_ADD);
a7fd6706
KS
406 return count;
407}
1da177e4
LT
408static ssize_t disk_dev_read(struct gendisk * disk, char *page)
409{
410 dev_t base = MKDEV(disk->major, disk->first_minor);
411 return print_dev_t(page, base);
412}
413static ssize_t disk_range_read(struct gendisk * disk, char *page)
414{
415 return sprintf(page, "%d\n", disk->minors);
416}
417static ssize_t disk_removable_read(struct gendisk * disk, char *page)
418{
419 return sprintf(page, "%d\n",
420 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
421
422}
423static ssize_t disk_size_read(struct gendisk * disk, char *page)
424{
425 return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk));
426}
86ce18d7
KCA
427static ssize_t disk_capability_read(struct gendisk *disk, char *page)
428{
429 return sprintf(page, "%x\n", disk->flags);
430}
1da177e4
LT
431static ssize_t disk_stats_read(struct gendisk * disk, char *page)
432{
433 preempt_disable();
434 disk_round_stats(disk);
435 preempt_enable();
436 return sprintf(page,
837c7878
BW
437 "%8lu %8lu %8llu %8u "
438 "%8lu %8lu %8llu %8u "
1da177e4
LT
439 "%8u %8u %8u"
440 "\n",
47a00410
JA
441 disk_stat_read(disk, ios[READ]),
442 disk_stat_read(disk, merges[READ]),
443 (unsigned long long)disk_stat_read(disk, sectors[READ]),
444 jiffies_to_msecs(disk_stat_read(disk, ticks[READ])),
445 disk_stat_read(disk, ios[WRITE]),
446 disk_stat_read(disk, merges[WRITE]),
447 (unsigned long long)disk_stat_read(disk, sectors[WRITE]),
448 jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])),
1da177e4
LT
449 disk->in_flight,
450 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
451 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
452}
a7fd6706
KS
453static struct disk_attribute disk_attr_uevent = {
454 .attr = {.name = "uevent", .mode = S_IWUSR },
455 .store = disk_uevent_store
456};
1da177e4
LT
457static struct disk_attribute disk_attr_dev = {
458 .attr = {.name = "dev", .mode = S_IRUGO },
459 .show = disk_dev_read
460};
461static struct disk_attribute disk_attr_range = {
462 .attr = {.name = "range", .mode = S_IRUGO },
463 .show = disk_range_read
464};
465static struct disk_attribute disk_attr_removable = {
466 .attr = {.name = "removable", .mode = S_IRUGO },
467 .show = disk_removable_read
468};
469static struct disk_attribute disk_attr_size = {
470 .attr = {.name = "size", .mode = S_IRUGO },
471 .show = disk_size_read
472};
86ce18d7
KCA
473static struct disk_attribute disk_attr_capability = {
474 .attr = {.name = "capability", .mode = S_IRUGO },
475 .show = disk_capability_read
476};
1da177e4
LT
477static struct disk_attribute disk_attr_stat = {
478 .attr = {.name = "stat", .mode = S_IRUGO },
479 .show = disk_stats_read
480};
481
c17bb495
AM
482#ifdef CONFIG_FAIL_MAKE_REQUEST
483
484static ssize_t disk_fail_store(struct gendisk * disk,
485 const char *buf, size_t count)
486{
487 int i;
488
489 if (count > 0 && sscanf(buf, "%d", &i) > 0) {
490 if (i == 0)
491 disk->flags &= ~GENHD_FL_FAIL;
492 else
493 disk->flags |= GENHD_FL_FAIL;
494 }
495
496 return count;
497}
498static ssize_t disk_fail_read(struct gendisk * disk, char *page)
499{
500 return sprintf(page, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0);
501}
502static struct disk_attribute disk_attr_fail = {
503 .attr = {.name = "make-it-fail", .mode = S_IRUGO | S_IWUSR },
504 .store = disk_fail_store,
505 .show = disk_fail_read
506};
507
508#endif
509
1da177e4 510static struct attribute * default_attrs[] = {
a7fd6706 511 &disk_attr_uevent.attr,
1da177e4
LT
512 &disk_attr_dev.attr,
513 &disk_attr_range.attr,
514 &disk_attr_removable.attr,
515 &disk_attr_size.attr,
516 &disk_attr_stat.attr,
86ce18d7 517 &disk_attr_capability.attr,
c17bb495
AM
518#ifdef CONFIG_FAIL_MAKE_REQUEST
519 &disk_attr_fail.attr,
520#endif
1da177e4
LT
521 NULL,
522};
523
524static void disk_release(struct kobject * kobj)
525{
526 struct gendisk *disk = to_disk(kobj);
527 kfree(disk->random);
528 kfree(disk->part);
529 free_disk_stats(disk);
530 kfree(disk);
531}
532
533static struct kobj_type ktype_block = {
534 .release = disk_release,
535 .sysfs_ops = &disk_sysfs_ops,
536 .default_attrs = default_attrs,
537};
538
539extern struct kobj_type ktype_part;
540
312c004d 541static int block_uevent_filter(struct kset *kset, struct kobject *kobj)
1da177e4
LT
542{
543 struct kobj_type *ktype = get_ktype(kobj);
544
545 return ((ktype == &ktype_block) || (ktype == &ktype_part));
546}
547
312c004d 548static int block_uevent(struct kset *kset, struct kobject *kobj, char **envp,
1da177e4
LT
549 int num_envp, char *buffer, int buffer_size)
550{
551 struct kobj_type *ktype = get_ktype(kobj);
552 struct device *physdev;
553 struct gendisk *disk;
554 struct hd_struct *part;
555 int length = 0;
556 int i = 0;
557
558 if (ktype == &ktype_block) {
559 disk = container_of(kobj, struct gendisk, kobj);
312c004d
KS
560 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
561 &length, "MINOR=%u", disk->first_minor);
1da177e4
LT
562 } else if (ktype == &ktype_part) {
563 disk = container_of(kobj->parent, struct gendisk, kobj);
564 part = container_of(kobj, struct hd_struct, kobj);
312c004d
KS
565 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
566 &length, "MINOR=%u",
567 disk->first_minor + part->partno);
1da177e4
LT
568 } else
569 return 0;
570
312c004d
KS
571 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
572 "MAJOR=%u", disk->major);
1da177e4
LT
573
574 /* add physical device, backing this device */
575 physdev = disk->driverfs_dev;
576 if (physdev) {
577 char *path = kobject_get_path(&physdev->kobj, GFP_KERNEL);
578
312c004d
KS
579 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
580 &length, "PHYSDEVPATH=%s", path);
1da177e4
LT
581 kfree(path);
582
583 if (physdev->bus)
312c004d
KS
584 add_uevent_var(envp, num_envp, &i,
585 buffer, buffer_size, &length,
586 "PHYSDEVBUS=%s",
587 physdev->bus->name);
1da177e4
LT
588
589 if (physdev->driver)
312c004d
KS
590 add_uevent_var(envp, num_envp, &i,
591 buffer, buffer_size, &length,
592 "PHYSDEVDRIVER=%s",
593 physdev->driver->name);
1da177e4
LT
594 }
595
596 /* terminate, set to next free slot, shrink available space */
597 envp[i] = NULL;
598 envp = &envp[i];
599 num_envp -= i;
600 buffer = &buffer[length];
601 buffer_size -= length;
602
603 return 0;
604}
605
312c004d
KS
606static struct kset_uevent_ops block_uevent_ops = {
607 .filter = block_uevent_filter,
608 .uevent = block_uevent,
1da177e4
LT
609};
610
b9d9c82b 611decl_subsys(block, &ktype_block, &block_uevent_ops);
1da177e4
LT
612
613/*
614 * aggregate disk stat collector. Uses the same stats that the sysfs
615 * entries do, above, but makes them available through one seq_file.
616 * Watching a few disks may be efficient through sysfs, but watching
617 * all of them will be more efficient through this interface.
618 *
619 * The output looks suspiciously like /proc/partitions with a bunch of
620 * extra fields.
621 */
622
623/* iterator */
624static void *diskstats_start(struct seq_file *part, loff_t *pos)
625{
626 loff_t k = *pos;
627 struct list_head *p;
628
58383af6 629 mutex_lock(&block_subsys_lock);
823bccfc 630 list_for_each(p, &block_subsys.list)
1da177e4
LT
631 if (!k--)
632 return list_entry(p, struct gendisk, kobj.entry);
633 return NULL;
634}
635
636static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos)
637{
638 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
639 ++*pos;
823bccfc 640 return p==&block_subsys.list ? NULL :
1da177e4
LT
641 list_entry(p, struct gendisk, kobj.entry);
642}
643
644static void diskstats_stop(struct seq_file *part, void *v)
645{
58383af6 646 mutex_unlock(&block_subsys_lock);
1da177e4
LT
647}
648
649static int diskstats_show(struct seq_file *s, void *v)
650{
651 struct gendisk *gp = v;
652 char buf[BDEVNAME_SIZE];
653 int n = 0;
654
655 /*
656 if (&sgp->kobj.entry == block_subsys.kset.list.next)
657 seq_puts(s, "major minor name"
658 " rio rmerge rsect ruse wio wmerge "
659 "wsect wuse running use aveq"
660 "\n\n");
661 */
662
663 preempt_disable();
664 disk_round_stats(gp);
665 preempt_enable();
837c7878 666 seq_printf(s, "%4d %4d %s %lu %lu %llu %u %lu %lu %llu %u %u %u %u\n",
1da177e4 667 gp->major, n + gp->first_minor, disk_name(gp, n, buf),
a362357b
JA
668 disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]),
669 (unsigned long long)disk_stat_read(gp, sectors[0]),
670 jiffies_to_msecs(disk_stat_read(gp, ticks[0])),
671 disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]),
672 (unsigned long long)disk_stat_read(gp, sectors[1]),
673 jiffies_to_msecs(disk_stat_read(gp, ticks[1])),
1da177e4
LT
674 gp->in_flight,
675 jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
676 jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
677
678 /* now show all non-0 size partitions of it */
679 for (n = 0; n < gp->minors - 1; n++) {
680 struct hd_struct *hd = gp->part[n];
681
682 if (hd && hd->nr_sects)
683 seq_printf(s, "%4d %4d %s %u %u %u %u\n",
684 gp->major, n + gp->first_minor + 1,
685 disk_name(gp, n + 1, buf),
a362357b
JA
686 hd->ios[0], hd->sectors[0],
687 hd->ios[1], hd->sectors[1]);
1da177e4
LT
688 }
689
690 return 0;
691}
692
693struct seq_operations diskstats_op = {
694 .start = diskstats_start,
695 .next = diskstats_next,
696 .stop = diskstats_stop,
697 .show = diskstats_show
698};
699
8ce7ad7b
KCA
700static void media_change_notify_thread(struct work_struct *work)
701{
702 struct gendisk *gd = container_of(work, struct gendisk, async_notify);
703 char event[] = "MEDIA_CHANGE=1";
704 char *envp[] = { event, NULL };
705
706 /*
707 * set enviroment vars to indicate which event this is for
708 * so that user space will know to go check the media status.
709 */
710 kobject_uevent_env(&gd->kobj, KOBJ_CHANGE, envp);
711 put_device(gd->driverfs_dev);
712}
713
714void genhd_media_change_notify(struct gendisk *disk)
715{
716 get_device(disk->driverfs_dev);
717 schedule_work(&disk->async_notify);
718}
719EXPORT_SYMBOL_GPL(genhd_media_change_notify);
720
1da177e4
LT
721struct gendisk *alloc_disk(int minors)
722{
1946089a
CL
723 return alloc_disk_node(minors, -1);
724}
725
726struct gendisk *alloc_disk_node(int minors, int node_id)
727{
728 struct gendisk *disk;
729
94f6030c
CL
730 disk = kmalloc_node(sizeof(struct gendisk),
731 GFP_KERNEL | __GFP_ZERO, node_id);
1da177e4 732 if (disk) {
1da177e4
LT
733 if (!init_disk_stats(disk)) {
734 kfree(disk);
735 return NULL;
736 }
737 if (minors > 1) {
738 int size = (minors - 1) * sizeof(struct hd_struct *);
94f6030c
CL
739 disk->part = kmalloc_node(size,
740 GFP_KERNEL | __GFP_ZERO, node_id);
1da177e4
LT
741 if (!disk->part) {
742 kfree(disk);
743 return NULL;
744 }
1da177e4
LT
745 }
746 disk->minors = minors;
747 kobj_set_kset_s(disk,block_subsys);
748 kobject_init(&disk->kobj);
749 rand_initialize_disk(disk);
8ce7ad7b
KCA
750 INIT_WORK(&disk->async_notify,
751 media_change_notify_thread);
1da177e4
LT
752 }
753 return disk;
754}
755
756EXPORT_SYMBOL(alloc_disk);
1946089a 757EXPORT_SYMBOL(alloc_disk_node);
1da177e4
LT
758
759struct kobject *get_disk(struct gendisk *disk)
760{
761 struct module *owner;
762 struct kobject *kobj;
763
764 if (!disk->fops)
765 return NULL;
766 owner = disk->fops->owner;
767 if (owner && !try_module_get(owner))
768 return NULL;
769 kobj = kobject_get(&disk->kobj);
770 if (kobj == NULL) {
771 module_put(owner);
772 return NULL;
773 }
774 return kobj;
775
776}
777
778EXPORT_SYMBOL(get_disk);
779
780void put_disk(struct gendisk *disk)
781{
782 if (disk)
783 kobject_put(&disk->kobj);
784}
785
786EXPORT_SYMBOL(put_disk);
787
788void set_device_ro(struct block_device *bdev, int flag)
789{
790 if (bdev->bd_contains != bdev)
791 bdev->bd_part->policy = flag;
792 else
793 bdev->bd_disk->policy = flag;
794}
795
796EXPORT_SYMBOL(set_device_ro);
797
798void set_disk_ro(struct gendisk *disk, int flag)
799{
800 int i;
801 disk->policy = flag;
802 for (i = 0; i < disk->minors - 1; i++)
803 if (disk->part[i]) disk->part[i]->policy = flag;
804}
805
806EXPORT_SYMBOL(set_disk_ro);
807
808int bdev_read_only(struct block_device *bdev)
809{
810 if (!bdev)
811 return 0;
812 else if (bdev->bd_contains != bdev)
813 return bdev->bd_part->policy;
814 else
815 return bdev->bd_disk->policy;
816}
817
818EXPORT_SYMBOL(bdev_read_only);
819
820int invalidate_partition(struct gendisk *disk, int index)
821{
822 int res = 0;
823 struct block_device *bdev = bdget_disk(disk, index);
824 if (bdev) {
2ef41634
CH
825 fsync_bdev(bdev);
826 res = __invalidate_device(bdev);
1da177e4
LT
827 bdput(bdev);
828 }
829 return res;
830}
831
832EXPORT_SYMBOL(invalidate_partition);