]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame_incremental - block/genhd.c
block: fix diskstats access
[mirror_ubuntu-bionic-kernel.git] / block / genhd.c
... / ...
CommitLineData
1/*
2 * gendisk handling
3 */
4
5#include <linux/module.h>
6#include <linux/fs.h>
7#include <linux/genhd.h>
8#include <linux/kdev_t.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#include <linux/buffer_head.h>
18#include <linux/mutex.h>
19
20#include "blk.h"
21
22static DEFINE_MUTEX(block_class_lock);
23#ifndef CONFIG_SYSFS_DEPRECATED
24struct kobject *block_depr;
25#endif
26
27static struct device_type disk_type;
28
29/**
30 * disk_get_part - get partition
31 * @disk: disk to look partition from
32 * @partno: partition number
33 *
34 * Look for partition @partno from @disk. If found, increment
35 * reference count and return it.
36 *
37 * CONTEXT:
38 * Don't care.
39 *
40 * RETURNS:
41 * Pointer to the found partition on success, NULL if not found.
42 */
43struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
44{
45 struct hd_struct *part;
46
47 if (unlikely(partno < 1 || partno > disk_max_parts(disk)))
48 return NULL;
49 rcu_read_lock();
50 part = rcu_dereference(disk->__part[partno - 1]);
51 if (part)
52 get_device(&part->dev);
53 rcu_read_unlock();
54
55 return part;
56}
57EXPORT_SYMBOL_GPL(disk_get_part);
58
59/**
60 * disk_part_iter_init - initialize partition iterator
61 * @piter: iterator to initialize
62 * @disk: disk to iterate over
63 * @flags: DISK_PITER_* flags
64 *
65 * Initialize @piter so that it iterates over partitions of @disk.
66 *
67 * CONTEXT:
68 * Don't care.
69 */
70void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
71 unsigned int flags)
72{
73 piter->disk = disk;
74 piter->part = NULL;
75
76 if (flags & DISK_PITER_REVERSE)
77 piter->idx = disk_max_parts(piter->disk) - 1;
78 else
79 piter->idx = 0;
80
81 piter->flags = flags;
82}
83EXPORT_SYMBOL_GPL(disk_part_iter_init);
84
85/**
86 * disk_part_iter_next - proceed iterator to the next partition and return it
87 * @piter: iterator of interest
88 *
89 * Proceed @piter to the next partition and return it.
90 *
91 * CONTEXT:
92 * Don't care.
93 */
94struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
95{
96 int inc, end;
97
98 /* put the last partition */
99 disk_put_part(piter->part);
100 piter->part = NULL;
101
102 rcu_read_lock();
103
104 /* determine iteration parameters */
105 if (piter->flags & DISK_PITER_REVERSE) {
106 inc = -1;
107 end = -1;
108 } else {
109 inc = 1;
110 end = disk_max_parts(piter->disk);
111 }
112
113 /* iterate to the next partition */
114 for (; piter->idx != end; piter->idx += inc) {
115 struct hd_struct *part;
116
117 part = rcu_dereference(piter->disk->__part[piter->idx]);
118 if (!part)
119 continue;
120 if (!(piter->flags & DISK_PITER_INCL_EMPTY) && !part->nr_sects)
121 continue;
122
123 get_device(&part->dev);
124 piter->part = part;
125 piter->idx += inc;
126 break;
127 }
128
129 rcu_read_unlock();
130
131 return piter->part;
132}
133EXPORT_SYMBOL_GPL(disk_part_iter_next);
134
135/**
136 * disk_part_iter_exit - finish up partition iteration
137 * @piter: iter of interest
138 *
139 * Called when iteration is over. Cleans up @piter.
140 *
141 * CONTEXT:
142 * Don't care.
143 */
144void disk_part_iter_exit(struct disk_part_iter *piter)
145{
146 disk_put_part(piter->part);
147 piter->part = NULL;
148}
149EXPORT_SYMBOL_GPL(disk_part_iter_exit);
150
151/**
152 * disk_map_sector_rcu - map sector to partition
153 * @disk: gendisk of interest
154 * @sector: sector to map
155 *
156 * Find out which partition @sector maps to on @disk. This is
157 * primarily used for stats accounting.
158 *
159 * CONTEXT:
160 * RCU read locked. The returned partition pointer is valid only
161 * while preemption is disabled.
162 *
163 * RETURNS:
164 * Found partition on success, NULL if there's no matching partition.
165 */
166struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
167{
168 int i;
169
170 for (i = 0; i < disk_max_parts(disk); i++) {
171 struct hd_struct *part = rcu_dereference(disk->__part[i]);
172
173 if (part && part->start_sect <= sector &&
174 sector < part->start_sect + part->nr_sects)
175 return part;
176 }
177 return NULL;
178}
179EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
180
181/*
182 * Can be deleted altogether. Later.
183 *
184 */
185static struct blk_major_name {
186 struct blk_major_name *next;
187 int major;
188 char name[16];
189} *major_names[BLKDEV_MAJOR_HASH_SIZE];
190
191/* index in the above - for now: assume no multimajor ranges */
192static inline int major_to_index(int major)
193{
194 return major % BLKDEV_MAJOR_HASH_SIZE;
195}
196
197#ifdef CONFIG_PROC_FS
198void blkdev_show(struct seq_file *seqf, off_t offset)
199{
200 struct blk_major_name *dp;
201
202 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
203 mutex_lock(&block_class_lock);
204 for (dp = major_names[offset]; dp; dp = dp->next)
205 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
206 mutex_unlock(&block_class_lock);
207 }
208}
209#endif /* CONFIG_PROC_FS */
210
211int register_blkdev(unsigned int major, const char *name)
212{
213 struct blk_major_name **n, *p;
214 int index, ret = 0;
215
216 mutex_lock(&block_class_lock);
217
218 /* temporary */
219 if (major == 0) {
220 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
221 if (major_names[index] == NULL)
222 break;
223 }
224
225 if (index == 0) {
226 printk("register_blkdev: failed to get major for %s\n",
227 name);
228 ret = -EBUSY;
229 goto out;
230 }
231 major = index;
232 ret = major;
233 }
234
235 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
236 if (p == NULL) {
237 ret = -ENOMEM;
238 goto out;
239 }
240
241 p->major = major;
242 strlcpy(p->name, name, sizeof(p->name));
243 p->next = NULL;
244 index = major_to_index(major);
245
246 for (n = &major_names[index]; *n; n = &(*n)->next) {
247 if ((*n)->major == major)
248 break;
249 }
250 if (!*n)
251 *n = p;
252 else
253 ret = -EBUSY;
254
255 if (ret < 0) {
256 printk("register_blkdev: cannot get major %d for %s\n",
257 major, name);
258 kfree(p);
259 }
260out:
261 mutex_unlock(&block_class_lock);
262 return ret;
263}
264
265EXPORT_SYMBOL(register_blkdev);
266
267void unregister_blkdev(unsigned int major, const char *name)
268{
269 struct blk_major_name **n;
270 struct blk_major_name *p = NULL;
271 int index = major_to_index(major);
272
273 mutex_lock(&block_class_lock);
274 for (n = &major_names[index]; *n; n = &(*n)->next)
275 if ((*n)->major == major)
276 break;
277 if (!*n || strcmp((*n)->name, name)) {
278 WARN_ON(1);
279 } else {
280 p = *n;
281 *n = p->next;
282 }
283 mutex_unlock(&block_class_lock);
284 kfree(p);
285}
286
287EXPORT_SYMBOL(unregister_blkdev);
288
289static struct kobj_map *bdev_map;
290
291/*
292 * Register device numbers dev..(dev+range-1)
293 * range must be nonzero
294 * The hash chain is sorted on range, so that subranges can override.
295 */
296void blk_register_region(dev_t devt, unsigned long range, struct module *module,
297 struct kobject *(*probe)(dev_t, int *, void *),
298 int (*lock)(dev_t, void *), void *data)
299{
300 kobj_map(bdev_map, devt, range, module, probe, lock, data);
301}
302
303EXPORT_SYMBOL(blk_register_region);
304
305void blk_unregister_region(dev_t devt, unsigned long range)
306{
307 kobj_unmap(bdev_map, devt, range);
308}
309
310EXPORT_SYMBOL(blk_unregister_region);
311
312static struct kobject *exact_match(dev_t devt, int *partno, void *data)
313{
314 struct gendisk *p = data;
315
316 return &p->dev.kobj;
317}
318
319static int exact_lock(dev_t devt, void *data)
320{
321 struct gendisk *p = data;
322
323 if (!get_disk(p))
324 return -1;
325 return 0;
326}
327
328/**
329 * add_disk - add partitioning information to kernel list
330 * @disk: per-device partitioning information
331 *
332 * This function registers the partitioning information in @disk
333 * with the kernel.
334 */
335void add_disk(struct gendisk *disk)
336{
337 struct backing_dev_info *bdi;
338 int retval;
339
340 disk->flags |= GENHD_FL_UP;
341 disk->dev.devt = MKDEV(disk->major, disk->first_minor);
342 blk_register_region(disk_devt(disk), disk->minors, NULL,
343 exact_match, exact_lock, disk);
344 register_disk(disk);
345 blk_register_queue(disk);
346
347 bdi = &disk->queue->backing_dev_info;
348 bdi_register_dev(bdi, disk_devt(disk));
349 retval = sysfs_create_link(&disk->dev.kobj, &bdi->dev->kobj, "bdi");
350 WARN_ON(retval);
351}
352
353EXPORT_SYMBOL(add_disk);
354EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
355
356void unlink_gendisk(struct gendisk *disk)
357{
358 sysfs_remove_link(&disk->dev.kobj, "bdi");
359 bdi_unregister(&disk->queue->backing_dev_info);
360 blk_unregister_queue(disk);
361 blk_unregister_region(disk_devt(disk), disk->minors);
362}
363
364/**
365 * get_gendisk - get partitioning information for a given device
366 * @devt: device to get partitioning information for
367 * @part: returned partition index
368 *
369 * This function gets the structure containing partitioning
370 * information for the given device @devt.
371 */
372struct gendisk *get_gendisk(dev_t devt, int *partno)
373{
374 struct kobject *kobj = kobj_lookup(bdev_map, devt, partno);
375 struct device *dev = kobj_to_dev(kobj);
376
377 return kobj ? dev_to_disk(dev) : NULL;
378}
379
380/**
381 * bdget_disk - do bdget() by gendisk and partition number
382 * @disk: gendisk of interest
383 * @partno: partition number
384 *
385 * Find partition @partno from @disk, do bdget() on it.
386 *
387 * CONTEXT:
388 * Don't care.
389 *
390 * RETURNS:
391 * Resulting block_device on success, NULL on failure.
392 */
393extern struct block_device *bdget_disk(struct gendisk *disk, int partno)
394{
395 dev_t devt = MKDEV(0, 0);
396
397 if (partno == 0)
398 devt = disk_devt(disk);
399 else {
400 struct hd_struct *part;
401
402 part = disk_get_part(disk, partno);
403 if (part && part->nr_sects)
404 devt = part_devt(part);
405 disk_put_part(part);
406 }
407
408 if (likely(devt != MKDEV(0, 0)))
409 return bdget(devt);
410 return NULL;
411}
412EXPORT_SYMBOL(bdget_disk);
413
414/*
415 * print a full list of all partitions - intended for places where the root
416 * filesystem can't be mounted and thus to give the victim some idea of what
417 * went wrong
418 */
419void __init printk_all_partitions(void)
420{
421 struct class_dev_iter iter;
422 struct device *dev;
423
424 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
425 while ((dev = class_dev_iter_next(&iter))) {
426 struct gendisk *disk = dev_to_disk(dev);
427 struct disk_part_iter piter;
428 struct hd_struct *part;
429 char buf[BDEVNAME_SIZE];
430
431 /*
432 * Don't show empty devices or things that have been
433 * surpressed
434 */
435 if (get_capacity(disk) == 0 ||
436 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
437 continue;
438
439 /*
440 * Note, unlike /proc/partitions, I am showing the
441 * numbers in hex - the same format as the root=
442 * option takes.
443 */
444 printk("%02x%02x %10llu %s",
445 MAJOR(disk_devt(disk)), MINOR(disk_devt(disk)),
446 (unsigned long long)get_capacity(disk) >> 1,
447 disk_name(disk, 0, buf));
448 if (disk->driverfs_dev != NULL &&
449 disk->driverfs_dev->driver != NULL)
450 printk(" driver: %s\n",
451 disk->driverfs_dev->driver->name);
452 else
453 printk(" (driver?)\n");
454
455 /* now show the partitions */
456 disk_part_iter_init(&piter, disk, 0);
457 while ((part = disk_part_iter_next(&piter)))
458 printk(" %02x%02x %10llu %s\n",
459 MAJOR(part_devt(part)), MINOR(part_devt(part)),
460 (unsigned long long)part->nr_sects >> 1,
461 disk_name(disk, part->partno, buf));
462 disk_part_iter_exit(&piter);
463 }
464 class_dev_iter_exit(&iter);
465}
466
467#ifdef CONFIG_PROC_FS
468/* iterator */
469static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
470{
471 loff_t skip = *pos;
472 struct class_dev_iter *iter;
473 struct device *dev;
474
475 iter = kmalloc(GFP_KERNEL, sizeof(*iter));
476 if (!iter)
477 return ERR_PTR(-ENOMEM);
478
479 seqf->private = iter;
480 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
481 do {
482 dev = class_dev_iter_next(iter);
483 if (!dev)
484 return NULL;
485 } while (skip--);
486
487 return dev_to_disk(dev);
488}
489
490static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
491{
492 struct device *dev;
493
494 (*pos)++;
495 dev = class_dev_iter_next(seqf->private);
496 if (dev)
497 return dev_to_disk(dev);
498
499 return NULL;
500}
501
502static void disk_seqf_stop(struct seq_file *seqf, void *v)
503{
504 struct class_dev_iter *iter = seqf->private;
505
506 /* stop is called even after start failed :-( */
507 if (iter) {
508 class_dev_iter_exit(iter);
509 kfree(iter);
510 }
511}
512
513static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
514{
515 static void *p;
516
517 p = disk_seqf_start(seqf, pos);
518 if (!IS_ERR(p) && p)
519 seq_puts(seqf, "major minor #blocks name\n\n");
520 return p;
521}
522
523static int show_partition(struct seq_file *seqf, void *v)
524{
525 struct gendisk *sgp = v;
526 struct disk_part_iter piter;
527 struct hd_struct *part;
528 char buf[BDEVNAME_SIZE];
529
530 /* Don't show non-partitionable removeable devices or empty devices */
531 if (!get_capacity(sgp) || (!disk_max_parts(sgp) &&
532 (sgp->flags & GENHD_FL_REMOVABLE)))
533 return 0;
534 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
535 return 0;
536
537 /* show the full disk and all non-0 size partitions of it */
538 seq_printf(seqf, "%4d %4d %10llu %s\n",
539 MAJOR(disk_devt(sgp)), MINOR(disk_devt(sgp)),
540 (unsigned long long)get_capacity(sgp) >> 1,
541 disk_name(sgp, 0, buf));
542
543 disk_part_iter_init(&piter, sgp, 0);
544 while ((part = disk_part_iter_next(&piter)))
545 seq_printf(seqf, "%4d %4d %10llu %s\n",
546 MAJOR(part_devt(part)), MINOR(part_devt(part)),
547 (unsigned long long)part->nr_sects >> 1,
548 disk_name(sgp, part->partno, buf));
549 disk_part_iter_exit(&piter);
550
551 return 0;
552}
553
554const struct seq_operations partitions_op = {
555 .start = show_partition_start,
556 .next = disk_seqf_next,
557 .stop = disk_seqf_stop,
558 .show = show_partition
559};
560#endif
561
562
563static struct kobject *base_probe(dev_t devt, int *partno, void *data)
564{
565 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
566 /* Make old-style 2.4 aliases work */
567 request_module("block-major-%d", MAJOR(devt));
568 return NULL;
569}
570
571static int __init genhd_device_init(void)
572{
573 int error;
574
575 block_class.dev_kobj = sysfs_dev_block_kobj;
576 error = class_register(&block_class);
577 if (unlikely(error))
578 return error;
579 bdev_map = kobj_map_init(base_probe, &block_class_lock);
580 blk_dev_init();
581
582#ifndef CONFIG_SYSFS_DEPRECATED
583 /* create top-level block dir */
584 block_depr = kobject_create_and_add("block", NULL);
585#endif
586 return 0;
587}
588
589subsys_initcall(genhd_device_init);
590
591static ssize_t disk_range_show(struct device *dev,
592 struct device_attribute *attr, char *buf)
593{
594 struct gendisk *disk = dev_to_disk(dev);
595
596 return sprintf(buf, "%d\n", disk->minors);
597}
598
599static ssize_t disk_removable_show(struct device *dev,
600 struct device_attribute *attr, char *buf)
601{
602 struct gendisk *disk = dev_to_disk(dev);
603
604 return sprintf(buf, "%d\n",
605 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
606}
607
608static ssize_t disk_ro_show(struct device *dev,
609 struct device_attribute *attr, char *buf)
610{
611 struct gendisk *disk = dev_to_disk(dev);
612
613 return sprintf(buf, "%d\n", disk->policy ? 1 : 0);
614}
615
616static ssize_t disk_size_show(struct device *dev,
617 struct device_attribute *attr, char *buf)
618{
619 struct gendisk *disk = dev_to_disk(dev);
620
621 return sprintf(buf, "%llu\n", (unsigned long long)get_capacity(disk));
622}
623
624static ssize_t disk_capability_show(struct device *dev,
625 struct device_attribute *attr, char *buf)
626{
627 struct gendisk *disk = dev_to_disk(dev);
628
629 return sprintf(buf, "%x\n", disk->flags);
630}
631
632static ssize_t disk_stat_show(struct device *dev,
633 struct device_attribute *attr, char *buf)
634{
635 struct gendisk *disk = dev_to_disk(dev);
636 int cpu;
637
638 cpu = disk_stat_lock();
639 disk_round_stats(cpu, disk);
640 disk_stat_unlock();
641 return sprintf(buf,
642 "%8lu %8lu %8llu %8u "
643 "%8lu %8lu %8llu %8u "
644 "%8u %8u %8u"
645 "\n",
646 disk_stat_read(disk, ios[READ]),
647 disk_stat_read(disk, merges[READ]),
648 (unsigned long long)disk_stat_read(disk, sectors[READ]),
649 jiffies_to_msecs(disk_stat_read(disk, ticks[READ])),
650 disk_stat_read(disk, ios[WRITE]),
651 disk_stat_read(disk, merges[WRITE]),
652 (unsigned long long)disk_stat_read(disk, sectors[WRITE]),
653 jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])),
654 disk->in_flight,
655 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
656 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
657}
658
659#ifdef CONFIG_FAIL_MAKE_REQUEST
660static ssize_t disk_fail_show(struct device *dev,
661 struct device_attribute *attr, char *buf)
662{
663 struct gendisk *disk = dev_to_disk(dev);
664
665 return sprintf(buf, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0);
666}
667
668static ssize_t disk_fail_store(struct device *dev,
669 struct device_attribute *attr,
670 const char *buf, size_t count)
671{
672 struct gendisk *disk = dev_to_disk(dev);
673 int i;
674
675 if (count > 0 && sscanf(buf, "%d", &i) > 0) {
676 if (i == 0)
677 disk->flags &= ~GENHD_FL_FAIL;
678 else
679 disk->flags |= GENHD_FL_FAIL;
680 }
681
682 return count;
683}
684
685#endif
686
687static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
688static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
689static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
690static DEVICE_ATTR(size, S_IRUGO, disk_size_show, NULL);
691static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
692static DEVICE_ATTR(stat, S_IRUGO, disk_stat_show, NULL);
693#ifdef CONFIG_FAIL_MAKE_REQUEST
694static struct device_attribute dev_attr_fail =
695 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, disk_fail_show, disk_fail_store);
696#endif
697
698static struct attribute *disk_attrs[] = {
699 &dev_attr_range.attr,
700 &dev_attr_removable.attr,
701 &dev_attr_ro.attr,
702 &dev_attr_size.attr,
703 &dev_attr_capability.attr,
704 &dev_attr_stat.attr,
705#ifdef CONFIG_FAIL_MAKE_REQUEST
706 &dev_attr_fail.attr,
707#endif
708 NULL
709};
710
711static struct attribute_group disk_attr_group = {
712 .attrs = disk_attrs,
713};
714
715static struct attribute_group *disk_attr_groups[] = {
716 &disk_attr_group,
717 NULL
718};
719
720static void disk_release(struct device *dev)
721{
722 struct gendisk *disk = dev_to_disk(dev);
723
724 kfree(disk->random);
725 kfree(disk->__part);
726 free_disk_stats(disk);
727 kfree(disk);
728}
729struct class block_class = {
730 .name = "block",
731};
732
733static struct device_type disk_type = {
734 .name = "disk",
735 .groups = disk_attr_groups,
736 .release = disk_release,
737};
738
739#ifdef CONFIG_PROC_FS
740/*
741 * aggregate disk stat collector. Uses the same stats that the sysfs
742 * entries do, above, but makes them available through one seq_file.
743 *
744 * The output looks suspiciously like /proc/partitions with a bunch of
745 * extra fields.
746 */
747static int diskstats_show(struct seq_file *seqf, void *v)
748{
749 struct gendisk *gp = v;
750 struct disk_part_iter piter;
751 struct hd_struct *hd;
752 char buf[BDEVNAME_SIZE];
753 int cpu;
754
755 /*
756 if (&gp->dev.kobj.entry == block_class.devices.next)
757 seq_puts(seqf, "major minor name"
758 " rio rmerge rsect ruse wio wmerge "
759 "wsect wuse running use aveq"
760 "\n\n");
761 */
762
763 cpu = disk_stat_lock();
764 disk_round_stats(cpu, gp);
765 disk_stat_unlock();
766 seq_printf(seqf, "%4d %4d %s %lu %lu %llu %u %lu %lu %llu %u %u %u %u\n",
767 MAJOR(disk_devt(gp)), MINOR(disk_devt(gp)),
768 disk_name(gp, 0, buf),
769 disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]),
770 (unsigned long long)disk_stat_read(gp, sectors[0]),
771 jiffies_to_msecs(disk_stat_read(gp, ticks[0])),
772 disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]),
773 (unsigned long long)disk_stat_read(gp, sectors[1]),
774 jiffies_to_msecs(disk_stat_read(gp, ticks[1])),
775 gp->in_flight,
776 jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
777 jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
778
779 /* now show all non-0 size partitions of it */
780 disk_part_iter_init(&piter, gp, 0);
781 while ((hd = disk_part_iter_next(&piter))) {
782 cpu = disk_stat_lock();
783 part_round_stats(cpu, hd);
784 disk_stat_unlock();
785 seq_printf(seqf, "%4d %4d %s %lu %lu %llu "
786 "%u %lu %lu %llu %u %u %u %u\n",
787 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
788 disk_name(gp, hd->partno, buf),
789 part_stat_read(hd, ios[0]),
790 part_stat_read(hd, merges[0]),
791 (unsigned long long)part_stat_read(hd, sectors[0]),
792 jiffies_to_msecs(part_stat_read(hd, ticks[0])),
793 part_stat_read(hd, ios[1]),
794 part_stat_read(hd, merges[1]),
795 (unsigned long long)part_stat_read(hd, sectors[1]),
796 jiffies_to_msecs(part_stat_read(hd, ticks[1])),
797 hd->in_flight,
798 jiffies_to_msecs(part_stat_read(hd, io_ticks)),
799 jiffies_to_msecs(part_stat_read(hd, time_in_queue))
800 );
801 }
802 disk_part_iter_exit(&piter);
803
804 return 0;
805}
806
807const struct seq_operations diskstats_op = {
808 .start = disk_seqf_start,
809 .next = disk_seqf_next,
810 .stop = disk_seqf_stop,
811 .show = diskstats_show
812};
813#endif /* CONFIG_PROC_FS */
814
815static void media_change_notify_thread(struct work_struct *work)
816{
817 struct gendisk *gd = container_of(work, struct gendisk, async_notify);
818 char event[] = "MEDIA_CHANGE=1";
819 char *envp[] = { event, NULL };
820
821 /*
822 * set enviroment vars to indicate which event this is for
823 * so that user space will know to go check the media status.
824 */
825 kobject_uevent_env(&gd->dev.kobj, KOBJ_CHANGE, envp);
826 put_device(gd->driverfs_dev);
827}
828
829#if 0
830void genhd_media_change_notify(struct gendisk *disk)
831{
832 get_device(disk->driverfs_dev);
833 schedule_work(&disk->async_notify);
834}
835EXPORT_SYMBOL_GPL(genhd_media_change_notify);
836#endif /* 0 */
837
838dev_t blk_lookup_devt(const char *name, int partno)
839{
840 dev_t devt = MKDEV(0, 0);
841 struct class_dev_iter iter;
842 struct device *dev;
843
844 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
845 while ((dev = class_dev_iter_next(&iter))) {
846 struct gendisk *disk = dev_to_disk(dev);
847
848 if (strcmp(dev->bus_id, name))
849 continue;
850 if (partno < 0 || partno > disk_max_parts(disk))
851 continue;
852
853 if (partno == 0)
854 devt = disk_devt(disk);
855 else {
856 struct hd_struct *part;
857
858 part = disk_get_part(disk, partno);
859 if (!part || !part->nr_sects) {
860 disk_put_part(part);
861 continue;
862 }
863
864 devt = part_devt(part);
865 disk_put_part(part);
866 }
867 break;
868 }
869 class_dev_iter_exit(&iter);
870 return devt;
871}
872EXPORT_SYMBOL(blk_lookup_devt);
873
874struct gendisk *alloc_disk(int minors)
875{
876 return alloc_disk_node(minors, -1);
877}
878
879struct gendisk *alloc_disk_node(int minors, int node_id)
880{
881 struct gendisk *disk;
882
883 disk = kmalloc_node(sizeof(struct gendisk),
884 GFP_KERNEL | __GFP_ZERO, node_id);
885 if (disk) {
886 if (!init_disk_stats(disk)) {
887 kfree(disk);
888 return NULL;
889 }
890 if (minors > 1) {
891 int size = (minors - 1) * sizeof(struct hd_struct *);
892 disk->__part = kmalloc_node(size,
893 GFP_KERNEL | __GFP_ZERO, node_id);
894 if (!disk->__part) {
895 free_disk_stats(disk);
896 kfree(disk);
897 return NULL;
898 }
899 }
900 disk->minors = minors;
901 rand_initialize_disk(disk);
902 disk->dev.class = &block_class;
903 disk->dev.type = &disk_type;
904 device_initialize(&disk->dev);
905 INIT_WORK(&disk->async_notify,
906 media_change_notify_thread);
907 }
908 return disk;
909}
910
911EXPORT_SYMBOL(alloc_disk);
912EXPORT_SYMBOL(alloc_disk_node);
913
914struct kobject *get_disk(struct gendisk *disk)
915{
916 struct module *owner;
917 struct kobject *kobj;
918
919 if (!disk->fops)
920 return NULL;
921 owner = disk->fops->owner;
922 if (owner && !try_module_get(owner))
923 return NULL;
924 kobj = kobject_get(&disk->dev.kobj);
925 if (kobj == NULL) {
926 module_put(owner);
927 return NULL;
928 }
929 return kobj;
930
931}
932
933EXPORT_SYMBOL(get_disk);
934
935void put_disk(struct gendisk *disk)
936{
937 if (disk)
938 kobject_put(&disk->dev.kobj);
939}
940
941EXPORT_SYMBOL(put_disk);
942
943void set_device_ro(struct block_device *bdev, int flag)
944{
945 if (bdev->bd_contains != bdev)
946 bdev->bd_part->policy = flag;
947 else
948 bdev->bd_disk->policy = flag;
949}
950
951EXPORT_SYMBOL(set_device_ro);
952
953void set_disk_ro(struct gendisk *disk, int flag)
954{
955 struct disk_part_iter piter;
956 struct hd_struct *part;
957
958 disk->policy = flag;
959 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
960 while ((part = disk_part_iter_next(&piter)))
961 part->policy = flag;
962 disk_part_iter_exit(&piter);
963}
964
965EXPORT_SYMBOL(set_disk_ro);
966
967int bdev_read_only(struct block_device *bdev)
968{
969 if (!bdev)
970 return 0;
971 else if (bdev->bd_contains != bdev)
972 return bdev->bd_part->policy;
973 else
974 return bdev->bd_disk->policy;
975}
976
977EXPORT_SYMBOL(bdev_read_only);
978
979int invalidate_partition(struct gendisk *disk, int partno)
980{
981 int res = 0;
982 struct block_device *bdev = bdget_disk(disk, partno);
983 if (bdev) {
984 fsync_bdev(bdev);
985 res = __invalidate_device(bdev);
986 bdput(bdev);
987 }
988 return res;
989}
990
991EXPORT_SYMBOL(invalidate_partition);