5 #include <linux/module.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 #include <linux/idr.h>
23 static DEFINE_MUTEX(block_class_lock
);
24 #ifndef CONFIG_SYSFS_DEPRECATED
25 struct kobject
*block_depr
;
28 /* for extended dynamic devt allocation, currently only one major is used */
29 #define MAX_EXT_DEVT (1 << MINORBITS)
31 /* For extended devt allocation. ext_devt_mutex prevents look up
32 * results from going away underneath its user.
34 static DEFINE_MUTEX(ext_devt_mutex
);
35 static DEFINE_IDR(ext_devt_idr
);
37 static struct device_type disk_type
;
40 * disk_get_part - get partition
41 * @disk: disk to look partition from
42 * @partno: partition number
44 * Look for partition @partno from @disk. If found, increment
45 * reference count and return it.
51 * Pointer to the found partition on success, NULL if not found.
53 struct hd_struct
*disk_get_part(struct gendisk
*disk
, int partno
)
55 struct hd_struct
*part
;
57 if (unlikely(partno
< 0 || partno
>= disk_max_parts(disk
)))
60 part
= rcu_dereference(disk
->__part
[partno
]);
62 get_device(part_to_dev(part
));
67 EXPORT_SYMBOL_GPL(disk_get_part
);
70 * disk_part_iter_init - initialize partition iterator
71 * @piter: iterator to initialize
72 * @disk: disk to iterate over
73 * @flags: DISK_PITER_* flags
75 * Initialize @piter so that it iterates over partitions of @disk.
80 void disk_part_iter_init(struct disk_part_iter
*piter
, struct gendisk
*disk
,
86 if (flags
& DISK_PITER_REVERSE
)
87 piter
->idx
= disk_max_parts(piter
->disk
) - 1;
88 else if (flags
& DISK_PITER_INCL_PART0
)
95 EXPORT_SYMBOL_GPL(disk_part_iter_init
);
98 * disk_part_iter_next - proceed iterator to the next partition and return it
99 * @piter: iterator of interest
101 * Proceed @piter to the next partition and return it.
106 struct hd_struct
*disk_part_iter_next(struct disk_part_iter
*piter
)
110 /* put the last partition */
111 disk_put_part(piter
->part
);
116 /* determine iteration parameters */
117 if (piter
->flags
& DISK_PITER_REVERSE
) {
119 if (piter
->flags
& DISK_PITER_INCL_PART0
)
125 end
= disk_max_parts(piter
->disk
);
128 /* iterate to the next partition */
129 for (; piter
->idx
!= end
; piter
->idx
+= inc
) {
130 struct hd_struct
*part
;
132 part
= rcu_dereference(piter
->disk
->__part
[piter
->idx
]);
135 if (!(piter
->flags
& DISK_PITER_INCL_EMPTY
) && !part
->nr_sects
)
138 get_device(part_to_dev(part
));
148 EXPORT_SYMBOL_GPL(disk_part_iter_next
);
151 * disk_part_iter_exit - finish up partition iteration
152 * @piter: iter of interest
154 * Called when iteration is over. Cleans up @piter.
159 void disk_part_iter_exit(struct disk_part_iter
*piter
)
161 disk_put_part(piter
->part
);
164 EXPORT_SYMBOL_GPL(disk_part_iter_exit
);
167 * disk_map_sector_rcu - map sector to partition
168 * @disk: gendisk of interest
169 * @sector: sector to map
171 * Find out which partition @sector maps to on @disk. This is
172 * primarily used for stats accounting.
175 * RCU read locked. The returned partition pointer is valid only
176 * while preemption is disabled.
179 * Found partition on success, NULL if there's no matching partition.
181 struct hd_struct
*disk_map_sector_rcu(struct gendisk
*disk
, sector_t sector
)
185 for (i
= 1; i
< disk_max_parts(disk
); i
++) {
186 struct hd_struct
*part
= rcu_dereference(disk
->__part
[i
]);
188 if (part
&& part
->start_sect
<= sector
&&
189 sector
< part
->start_sect
+ part
->nr_sects
)
194 EXPORT_SYMBOL_GPL(disk_map_sector_rcu
);
197 * Can be deleted altogether. Later.
200 static struct blk_major_name
{
201 struct blk_major_name
*next
;
204 } *major_names
[BLKDEV_MAJOR_HASH_SIZE
];
206 /* index in the above - for now: assume no multimajor ranges */
207 static inline int major_to_index(int major
)
209 return major
% BLKDEV_MAJOR_HASH_SIZE
;
212 #ifdef CONFIG_PROC_FS
213 void blkdev_show(struct seq_file
*seqf
, off_t offset
)
215 struct blk_major_name
*dp
;
217 if (offset
< BLKDEV_MAJOR_HASH_SIZE
) {
218 mutex_lock(&block_class_lock
);
219 for (dp
= major_names
[offset
]; dp
; dp
= dp
->next
)
220 seq_printf(seqf
, "%3d %s\n", dp
->major
, dp
->name
);
221 mutex_unlock(&block_class_lock
);
224 #endif /* CONFIG_PROC_FS */
226 int register_blkdev(unsigned int major
, const char *name
)
228 struct blk_major_name
**n
, *p
;
231 mutex_lock(&block_class_lock
);
235 for (index
= ARRAY_SIZE(major_names
)-1; index
> 0; index
--) {
236 if (major_names
[index
] == NULL
)
241 printk("register_blkdev: failed to get major for %s\n",
250 p
= kmalloc(sizeof(struct blk_major_name
), GFP_KERNEL
);
257 strlcpy(p
->name
, name
, sizeof(p
->name
));
259 index
= major_to_index(major
);
261 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
) {
262 if ((*n
)->major
== major
)
271 printk("register_blkdev: cannot get major %d for %s\n",
276 mutex_unlock(&block_class_lock
);
280 EXPORT_SYMBOL(register_blkdev
);
282 void unregister_blkdev(unsigned int major
, const char *name
)
284 struct blk_major_name
**n
;
285 struct blk_major_name
*p
= NULL
;
286 int index
= major_to_index(major
);
288 mutex_lock(&block_class_lock
);
289 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
)
290 if ((*n
)->major
== major
)
292 if (!*n
|| strcmp((*n
)->name
, name
)) {
298 mutex_unlock(&block_class_lock
);
302 EXPORT_SYMBOL(unregister_blkdev
);
304 static struct kobj_map
*bdev_map
;
307 * blk_mangle_minor - scatter minor numbers apart
308 * @minor: minor number to mangle
310 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
311 * is enabled. Mangling twice gives the original value.
319 static int blk_mangle_minor(int minor
)
321 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
324 for (i
= 0; i
< MINORBITS
/ 2; i
++) {
325 int low
= minor
& (1 << i
);
326 int high
= minor
& (1 << (MINORBITS
- 1 - i
));
327 int distance
= MINORBITS
- 1 - 2 * i
;
329 minor
^= low
| high
; /* clear both bits */
330 low
<<= distance
; /* swap the positions */
332 minor
|= low
| high
; /* and set */
339 * blk_alloc_devt - allocate a dev_t for a partition
340 * @part: partition to allocate dev_t for
341 * @gfp_mask: memory allocation flag
342 * @devt: out parameter for resulting dev_t
344 * Allocate a dev_t for block device.
347 * 0 on success, allocated dev_t is returned in *@devt. -errno on
353 int blk_alloc_devt(struct hd_struct
*part
, dev_t
*devt
)
355 struct gendisk
*disk
= part_to_disk(part
);
358 /* in consecutive minor range? */
359 if (part
->partno
< disk
->minors
) {
360 *devt
= MKDEV(disk
->major
, disk
->first_minor
+ part
->partno
);
364 /* allocate ext devt */
366 if (!idr_pre_get(&ext_devt_idr
, GFP_KERNEL
))
368 rc
= idr_get_new(&ext_devt_idr
, part
, &idx
);
369 } while (rc
== -EAGAIN
);
374 if (idx
> MAX_EXT_DEVT
) {
375 idr_remove(&ext_devt_idr
, idx
);
379 *devt
= MKDEV(BLOCK_EXT_MAJOR
, blk_mangle_minor(idx
));
384 * blk_free_devt - free a dev_t
385 * @devt: dev_t to free
387 * Free @devt which was allocated using blk_alloc_devt().
392 void blk_free_devt(dev_t devt
)
396 if (devt
== MKDEV(0, 0))
399 if (MAJOR(devt
) == BLOCK_EXT_MAJOR
) {
400 mutex_lock(&ext_devt_mutex
);
401 idr_remove(&ext_devt_idr
, blk_mangle_minor(MINOR(devt
)));
402 mutex_unlock(&ext_devt_mutex
);
406 static char *bdevt_str(dev_t devt
, char *buf
)
408 if (MAJOR(devt
) <= 0xff && MINOR(devt
) <= 0xff) {
409 char tbuf
[BDEVT_SIZE
];
410 snprintf(tbuf
, BDEVT_SIZE
, "%02x%02x", MAJOR(devt
), MINOR(devt
));
411 snprintf(buf
, BDEVT_SIZE
, "%-9s", tbuf
);
413 snprintf(buf
, BDEVT_SIZE
, "%03x:%05x", MAJOR(devt
), MINOR(devt
));
419 * Register device numbers dev..(dev+range-1)
420 * range must be nonzero
421 * The hash chain is sorted on range, so that subranges can override.
423 void blk_register_region(dev_t devt
, unsigned long range
, struct module
*module
,
424 struct kobject
*(*probe
)(dev_t
, int *, void *),
425 int (*lock
)(dev_t
, void *), void *data
)
427 kobj_map(bdev_map
, devt
, range
, module
, probe
, lock
, data
);
430 EXPORT_SYMBOL(blk_register_region
);
432 void blk_unregister_region(dev_t devt
, unsigned long range
)
434 kobj_unmap(bdev_map
, devt
, range
);
437 EXPORT_SYMBOL(blk_unregister_region
);
439 static struct kobject
*exact_match(dev_t devt
, int *partno
, void *data
)
441 struct gendisk
*p
= data
;
443 return &disk_to_dev(p
)->kobj
;
446 static int exact_lock(dev_t devt
, void *data
)
448 struct gendisk
*p
= data
;
456 * add_disk - add partitioning information to kernel list
457 * @disk: per-device partitioning information
459 * This function registers the partitioning information in @disk
462 void add_disk(struct gendisk
*disk
)
464 struct backing_dev_info
*bdi
;
467 disk
->flags
|= GENHD_FL_UP
;
468 disk_to_dev(disk
)->devt
= MKDEV(disk
->major
, disk
->first_minor
);
469 blk_register_region(disk_devt(disk
), disk
->minors
, NULL
,
470 exact_match
, exact_lock
, disk
);
472 blk_register_queue(disk
);
474 bdi
= &disk
->queue
->backing_dev_info
;
475 bdi_register_dev(bdi
, disk_devt(disk
));
476 retval
= sysfs_create_link(&disk_to_dev(disk
)->kobj
, &bdi
->dev
->kobj
,
481 EXPORT_SYMBOL(add_disk
);
482 EXPORT_SYMBOL(del_gendisk
); /* in partitions/check.c */
484 void unlink_gendisk(struct gendisk
*disk
)
486 sysfs_remove_link(&disk_to_dev(disk
)->kobj
, "bdi");
487 bdi_unregister(&disk
->queue
->backing_dev_info
);
488 blk_unregister_queue(disk
);
489 blk_unregister_region(disk_devt(disk
), disk
->minors
);
493 * get_gendisk - get partitioning information for a given device
494 * @devt: device to get partitioning information for
495 * @part: returned partition index
497 * This function gets the structure containing partitioning
498 * information for the given device @devt.
500 struct gendisk
*get_gendisk(dev_t devt
, int *partno
)
502 struct gendisk
*disk
= NULL
;
504 if (MAJOR(devt
) != BLOCK_EXT_MAJOR
) {
505 struct kobject
*kobj
;
507 kobj
= kobj_lookup(bdev_map
, devt
, partno
);
509 disk
= dev_to_disk(kobj_to_dev(kobj
));
511 struct hd_struct
*part
;
513 mutex_lock(&ext_devt_mutex
);
514 part
= idr_find(&ext_devt_idr
, blk_mangle_minor(MINOR(devt
)));
515 if (part
&& get_disk(part_to_disk(part
))) {
516 *partno
= part
->partno
;
517 disk
= part_to_disk(part
);
519 mutex_unlock(&ext_devt_mutex
);
526 * bdget_disk - do bdget() by gendisk and partition number
527 * @disk: gendisk of interest
528 * @partno: partition number
530 * Find partition @partno from @disk, do bdget() on it.
536 * Resulting block_device on success, NULL on failure.
538 extern struct block_device
*bdget_disk(struct gendisk
*disk
, int partno
)
540 dev_t devt
= MKDEV(0, 0);
543 devt
= disk_devt(disk
);
545 struct hd_struct
*part
;
547 part
= disk_get_part(disk
, partno
);
548 if (part
&& part
->nr_sects
)
549 devt
= part_devt(part
);
553 if (likely(devt
!= MKDEV(0, 0)))
557 EXPORT_SYMBOL(bdget_disk
);
560 * print a full list of all partitions - intended for places where the root
561 * filesystem can't be mounted and thus to give the victim some idea of what
564 void __init
printk_all_partitions(void)
566 struct class_dev_iter iter
;
569 class_dev_iter_init(&iter
, &block_class
, NULL
, &disk_type
);
570 while ((dev
= class_dev_iter_next(&iter
))) {
571 struct gendisk
*disk
= dev_to_disk(dev
);
572 struct disk_part_iter piter
;
573 struct hd_struct
*part
;
574 char name_buf
[BDEVNAME_SIZE
];
575 char devt_buf
[BDEVT_SIZE
];
578 * Don't show empty devices or things that have been
581 if (get_capacity(disk
) == 0 ||
582 (disk
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
))
586 * Note, unlike /proc/partitions, I am showing the
587 * numbers in hex - the same format as the root=
590 printk("%s %10llu %s",
591 bdevt_str(disk_devt(disk
), devt_buf
),
592 (unsigned long long)get_capacity(disk
) >> 1,
593 disk_name(disk
, 0, name_buf
));
594 if (disk
->driverfs_dev
!= NULL
&&
595 disk
->driverfs_dev
->driver
!= NULL
)
596 printk(" driver: %s\n",
597 disk
->driverfs_dev
->driver
->name
);
599 printk(" (driver?)\n");
601 /* now show the partitions */
602 disk_part_iter_init(&piter
, disk
, 0);
603 while ((part
= disk_part_iter_next(&piter
)))
604 printk(" %s %10llu %s\n",
605 bdevt_str(part_devt(part
), devt_buf
),
606 (unsigned long long)part
->nr_sects
>> 1,
607 disk_name(disk
, part
->partno
, name_buf
));
608 disk_part_iter_exit(&piter
);
610 class_dev_iter_exit(&iter
);
613 #ifdef CONFIG_PROC_FS
615 static void *disk_seqf_start(struct seq_file
*seqf
, loff_t
*pos
)
618 struct class_dev_iter
*iter
;
621 iter
= kmalloc(GFP_KERNEL
, sizeof(*iter
));
623 return ERR_PTR(-ENOMEM
);
625 seqf
->private = iter
;
626 class_dev_iter_init(iter
, &block_class
, NULL
, &disk_type
);
628 dev
= class_dev_iter_next(iter
);
633 return dev_to_disk(dev
);
636 static void *disk_seqf_next(struct seq_file
*seqf
, void *v
, loff_t
*pos
)
641 dev
= class_dev_iter_next(seqf
->private);
643 return dev_to_disk(dev
);
648 static void disk_seqf_stop(struct seq_file
*seqf
, void *v
)
650 struct class_dev_iter
*iter
= seqf
->private;
652 /* stop is called even after start failed :-( */
654 class_dev_iter_exit(iter
);
659 static void *show_partition_start(struct seq_file
*seqf
, loff_t
*pos
)
663 p
= disk_seqf_start(seqf
, pos
);
665 seq_puts(seqf
, "major minor #blocks name\n\n");
669 static int show_partition(struct seq_file
*seqf
, void *v
)
671 struct gendisk
*sgp
= v
;
672 struct disk_part_iter piter
;
673 struct hd_struct
*part
;
674 char buf
[BDEVNAME_SIZE
];
676 /* Don't show non-partitionable removeable devices or empty devices */
677 if (!get_capacity(sgp
) || (!disk_partitionable(sgp
) &&
678 (sgp
->flags
& GENHD_FL_REMOVABLE
)))
680 if (sgp
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
)
683 /* show the full disk and all non-0 size partitions of it */
684 seq_printf(seqf
, "%4d %7d %10llu %s\n",
685 MAJOR(disk_devt(sgp
)), MINOR(disk_devt(sgp
)),
686 (unsigned long long)get_capacity(sgp
) >> 1,
687 disk_name(sgp
, 0, buf
));
689 disk_part_iter_init(&piter
, sgp
, 0);
690 while ((part
= disk_part_iter_next(&piter
)))
691 seq_printf(seqf
, "%4d %7d %10llu %s\n",
692 MAJOR(part_devt(part
)), MINOR(part_devt(part
)),
693 (unsigned long long)part
->nr_sects
>> 1,
694 disk_name(sgp
, part
->partno
, buf
));
695 disk_part_iter_exit(&piter
);
700 const struct seq_operations partitions_op
= {
701 .start
= show_partition_start
,
702 .next
= disk_seqf_next
,
703 .stop
= disk_seqf_stop
,
704 .show
= show_partition
709 static struct kobject
*base_probe(dev_t devt
, int *partno
, void *data
)
711 if (request_module("block-major-%d-%d", MAJOR(devt
), MINOR(devt
)) > 0)
712 /* Make old-style 2.4 aliases work */
713 request_module("block-major-%d", MAJOR(devt
));
717 static int __init
genhd_device_init(void)
721 block_class
.dev_kobj
= sysfs_dev_block_kobj
;
722 error
= class_register(&block_class
);
725 bdev_map
= kobj_map_init(base_probe
, &block_class_lock
);
728 #ifndef CONFIG_SYSFS_DEPRECATED
729 /* create top-level block dir */
730 block_depr
= kobject_create_and_add("block", NULL
);
735 subsys_initcall(genhd_device_init
);
737 static ssize_t
disk_range_show(struct device
*dev
,
738 struct device_attribute
*attr
, char *buf
)
740 struct gendisk
*disk
= dev_to_disk(dev
);
742 return sprintf(buf
, "%d\n", disk
->minors
);
745 static ssize_t
disk_ext_range_show(struct device
*dev
,
746 struct device_attribute
*attr
, char *buf
)
748 struct gendisk
*disk
= dev_to_disk(dev
);
750 return sprintf(buf
, "%d\n", disk_max_parts(disk
));
753 static ssize_t
disk_removable_show(struct device
*dev
,
754 struct device_attribute
*attr
, char *buf
)
756 struct gendisk
*disk
= dev_to_disk(dev
);
758 return sprintf(buf
, "%d\n",
759 (disk
->flags
& GENHD_FL_REMOVABLE
? 1 : 0));
762 static ssize_t
disk_ro_show(struct device
*dev
,
763 struct device_attribute
*attr
, char *buf
)
765 struct gendisk
*disk
= dev_to_disk(dev
);
767 return sprintf(buf
, "%d\n", disk
->policy
? 1 : 0);
770 static ssize_t
disk_size_show(struct device
*dev
,
771 struct device_attribute
*attr
, char *buf
)
773 struct gendisk
*disk
= dev_to_disk(dev
);
775 return sprintf(buf
, "%llu\n", (unsigned long long)get_capacity(disk
));
778 static ssize_t
disk_capability_show(struct device
*dev
,
779 struct device_attribute
*attr
, char *buf
)
781 struct gendisk
*disk
= dev_to_disk(dev
);
783 return sprintf(buf
, "%x\n", disk
->flags
);
786 static ssize_t
disk_stat_show(struct device
*dev
,
787 struct device_attribute
*attr
, char *buf
)
789 struct gendisk
*disk
= dev_to_disk(dev
);
792 cpu
= disk_stat_lock();
793 disk_round_stats(cpu
, disk
);
796 "%8lu %8lu %8llu %8u "
797 "%8lu %8lu %8llu %8u "
800 disk_stat_read(disk
, ios
[READ
]),
801 disk_stat_read(disk
, merges
[READ
]),
802 (unsigned long long)disk_stat_read(disk
, sectors
[READ
]),
803 jiffies_to_msecs(disk_stat_read(disk
, ticks
[READ
])),
804 disk_stat_read(disk
, ios
[WRITE
]),
805 disk_stat_read(disk
, merges
[WRITE
]),
806 (unsigned long long)disk_stat_read(disk
, sectors
[WRITE
]),
807 jiffies_to_msecs(disk_stat_read(disk
, ticks
[WRITE
])),
809 jiffies_to_msecs(disk_stat_read(disk
, io_ticks
)),
810 jiffies_to_msecs(disk_stat_read(disk
, time_in_queue
)));
813 #ifdef CONFIG_FAIL_MAKE_REQUEST
814 static ssize_t
disk_fail_show(struct device
*dev
,
815 struct device_attribute
*attr
, char *buf
)
817 struct gendisk
*disk
= dev_to_disk(dev
);
819 return sprintf(buf
, "%d\n", disk
->flags
& GENHD_FL_FAIL
? 1 : 0);
822 static ssize_t
disk_fail_store(struct device
*dev
,
823 struct device_attribute
*attr
,
824 const char *buf
, size_t count
)
826 struct gendisk
*disk
= dev_to_disk(dev
);
829 if (count
> 0 && sscanf(buf
, "%d", &i
) > 0) {
831 disk
->flags
&= ~GENHD_FL_FAIL
;
833 disk
->flags
|= GENHD_FL_FAIL
;
841 static DEVICE_ATTR(range
, S_IRUGO
, disk_range_show
, NULL
);
842 static DEVICE_ATTR(ext_range
, S_IRUGO
, disk_ext_range_show
, NULL
);
843 static DEVICE_ATTR(removable
, S_IRUGO
, disk_removable_show
, NULL
);
844 static DEVICE_ATTR(ro
, S_IRUGO
, disk_ro_show
, NULL
);
845 static DEVICE_ATTR(size
, S_IRUGO
, disk_size_show
, NULL
);
846 static DEVICE_ATTR(capability
, S_IRUGO
, disk_capability_show
, NULL
);
847 static DEVICE_ATTR(stat
, S_IRUGO
, disk_stat_show
, NULL
);
848 #ifdef CONFIG_FAIL_MAKE_REQUEST
849 static struct device_attribute dev_attr_fail
=
850 __ATTR(make
-it
-fail
, S_IRUGO
|S_IWUSR
, disk_fail_show
, disk_fail_store
);
853 static struct attribute
*disk_attrs
[] = {
854 &dev_attr_range
.attr
,
855 &dev_attr_ext_range
.attr
,
856 &dev_attr_removable
.attr
,
859 &dev_attr_capability
.attr
,
861 #ifdef CONFIG_FAIL_MAKE_REQUEST
867 static struct attribute_group disk_attr_group
= {
871 static struct attribute_group
*disk_attr_groups
[] = {
876 static void disk_release(struct device
*dev
)
878 struct gendisk
*disk
= dev_to_disk(dev
);
882 free_disk_stats(disk
);
885 struct class block_class
= {
889 static struct device_type disk_type
= {
891 .groups
= disk_attr_groups
,
892 .release
= disk_release
,
895 #ifdef CONFIG_PROC_FS
897 * aggregate disk stat collector. Uses the same stats that the sysfs
898 * entries do, above, but makes them available through one seq_file.
900 * The output looks suspiciously like /proc/partitions with a bunch of
903 static int diskstats_show(struct seq_file
*seqf
, void *v
)
905 struct gendisk
*gp
= v
;
906 struct disk_part_iter piter
;
907 struct hd_struct
*hd
;
908 char buf
[BDEVNAME_SIZE
];
912 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
913 seq_puts(seqf, "major minor name"
914 " rio rmerge rsect ruse wio wmerge "
915 "wsect wuse running use aveq"
919 cpu
= disk_stat_lock();
920 disk_round_stats(cpu
, gp
);
922 seq_printf(seqf
, "%4d %7d %s %lu %lu %llu %u %lu %lu %llu %u %u %u %u\n",
923 MAJOR(disk_devt(gp
)), MINOR(disk_devt(gp
)),
924 disk_name(gp
, 0, buf
),
925 disk_stat_read(gp
, ios
[0]), disk_stat_read(gp
, merges
[0]),
926 (unsigned long long)disk_stat_read(gp
, sectors
[0]),
927 jiffies_to_msecs(disk_stat_read(gp
, ticks
[0])),
928 disk_stat_read(gp
, ios
[1]), disk_stat_read(gp
, merges
[1]),
929 (unsigned long long)disk_stat_read(gp
, sectors
[1]),
930 jiffies_to_msecs(disk_stat_read(gp
, ticks
[1])),
932 jiffies_to_msecs(disk_stat_read(gp
, io_ticks
)),
933 jiffies_to_msecs(disk_stat_read(gp
, time_in_queue
)));
935 /* now show all non-0 size partitions of it */
936 disk_part_iter_init(&piter
, gp
, 0);
937 while ((hd
= disk_part_iter_next(&piter
))) {
938 cpu
= disk_stat_lock();
939 part_round_stats(cpu
, hd
);
941 seq_printf(seqf
, "%4d %7d %s %lu %lu %llu "
942 "%u %lu %lu %llu %u %u %u %u\n",
943 MAJOR(part_devt(hd
)), MINOR(part_devt(hd
)),
944 disk_name(gp
, hd
->partno
, buf
),
945 part_stat_read(hd
, ios
[0]),
946 part_stat_read(hd
, merges
[0]),
947 (unsigned long long)part_stat_read(hd
, sectors
[0]),
948 jiffies_to_msecs(part_stat_read(hd
, ticks
[0])),
949 part_stat_read(hd
, ios
[1]),
950 part_stat_read(hd
, merges
[1]),
951 (unsigned long long)part_stat_read(hd
, sectors
[1]),
952 jiffies_to_msecs(part_stat_read(hd
, ticks
[1])),
954 jiffies_to_msecs(part_stat_read(hd
, io_ticks
)),
955 jiffies_to_msecs(part_stat_read(hd
, time_in_queue
))
958 disk_part_iter_exit(&piter
);
963 const struct seq_operations diskstats_op
= {
964 .start
= disk_seqf_start
,
965 .next
= disk_seqf_next
,
966 .stop
= disk_seqf_stop
,
967 .show
= diskstats_show
969 #endif /* CONFIG_PROC_FS */
971 static void media_change_notify_thread(struct work_struct
*work
)
973 struct gendisk
*gd
= container_of(work
, struct gendisk
, async_notify
);
974 char event
[] = "MEDIA_CHANGE=1";
975 char *envp
[] = { event
, NULL
};
978 * set enviroment vars to indicate which event this is for
979 * so that user space will know to go check the media status.
981 kobject_uevent_env(&disk_to_dev(gd
)->kobj
, KOBJ_CHANGE
, envp
);
982 put_device(gd
->driverfs_dev
);
986 void genhd_media_change_notify(struct gendisk
*disk
)
988 get_device(disk
->driverfs_dev
);
989 schedule_work(&disk
->async_notify
);
991 EXPORT_SYMBOL_GPL(genhd_media_change_notify
);
994 dev_t
blk_lookup_devt(const char *name
, int partno
)
996 dev_t devt
= MKDEV(0, 0);
997 struct class_dev_iter iter
;
1000 class_dev_iter_init(&iter
, &block_class
, NULL
, &disk_type
);
1001 while ((dev
= class_dev_iter_next(&iter
))) {
1002 struct gendisk
*disk
= dev_to_disk(dev
);
1004 if (strcmp(dev
->bus_id
, name
))
1006 if (partno
< 0 || partno
>= disk_max_parts(disk
))
1010 devt
= disk_devt(disk
);
1012 struct hd_struct
*part
;
1014 part
= disk_get_part(disk
, partno
);
1015 if (!part
|| !part
->nr_sects
) {
1016 disk_put_part(part
);
1020 devt
= part_devt(part
);
1021 disk_put_part(part
);
1025 class_dev_iter_exit(&iter
);
1028 EXPORT_SYMBOL(blk_lookup_devt
);
1030 struct gendisk
*alloc_disk(int minors
)
1032 return alloc_disk_node(minors
, -1);
1035 struct gendisk
*alloc_disk_node(int minors
, int node_id
)
1037 return alloc_disk_ext_node(minors
, 0, node_id
);
1040 struct gendisk
*alloc_disk_ext(int minors
, int ext_minors
)
1042 return alloc_disk_ext_node(minors
, ext_minors
, -1);
1045 struct gendisk
*alloc_disk_ext_node(int minors
, int ext_minors
, int node_id
)
1047 struct gendisk
*disk
;
1049 disk
= kmalloc_node(sizeof(struct gendisk
),
1050 GFP_KERNEL
| __GFP_ZERO
, node_id
);
1052 int tot_minors
= minors
+ ext_minors
;
1053 int size
= tot_minors
* sizeof(struct hd_struct
*);
1055 if (!init_disk_stats(disk
)) {
1060 disk
->__part
= kmalloc_node(size
, GFP_KERNEL
| __GFP_ZERO
,
1062 if (!disk
->__part
) {
1063 free_disk_stats(disk
);
1067 disk
->__part
[0] = &disk
->part0
;
1069 disk
->minors
= minors
;
1070 disk
->ext_minors
= ext_minors
;
1071 rand_initialize_disk(disk
);
1072 disk_to_dev(disk
)->class = &block_class
;
1073 disk_to_dev(disk
)->type
= &disk_type
;
1074 device_initialize(disk_to_dev(disk
));
1075 INIT_WORK(&disk
->async_notify
,
1076 media_change_notify_thread
);
1081 EXPORT_SYMBOL(alloc_disk
);
1082 EXPORT_SYMBOL(alloc_disk_node
);
1083 EXPORT_SYMBOL(alloc_disk_ext
);
1084 EXPORT_SYMBOL(alloc_disk_ext_node
);
1086 struct kobject
*get_disk(struct gendisk
*disk
)
1088 struct module
*owner
;
1089 struct kobject
*kobj
;
1093 owner
= disk
->fops
->owner
;
1094 if (owner
&& !try_module_get(owner
))
1096 kobj
= kobject_get(&disk_to_dev(disk
)->kobj
);
1105 EXPORT_SYMBOL(get_disk
);
1107 void put_disk(struct gendisk
*disk
)
1110 kobject_put(&disk_to_dev(disk
)->kobj
);
1113 EXPORT_SYMBOL(put_disk
);
1115 void set_device_ro(struct block_device
*bdev
, int flag
)
1117 if (bdev
->bd_contains
!= bdev
)
1118 bdev
->bd_part
->policy
= flag
;
1120 bdev
->bd_disk
->policy
= flag
;
1123 EXPORT_SYMBOL(set_device_ro
);
1125 void set_disk_ro(struct gendisk
*disk
, int flag
)
1127 struct disk_part_iter piter
;
1128 struct hd_struct
*part
;
1130 disk
->policy
= flag
;
1131 disk_part_iter_init(&piter
, disk
, DISK_PITER_INCL_EMPTY
);
1132 while ((part
= disk_part_iter_next(&piter
)))
1133 part
->policy
= flag
;
1134 disk_part_iter_exit(&piter
);
1137 EXPORT_SYMBOL(set_disk_ro
);
1139 int bdev_read_only(struct block_device
*bdev
)
1143 else if (bdev
->bd_contains
!= bdev
)
1144 return bdev
->bd_part
->policy
;
1146 return bdev
->bd_disk
->policy
;
1149 EXPORT_SYMBOL(bdev_read_only
);
1151 int invalidate_partition(struct gendisk
*disk
, int partno
)
1154 struct block_device
*bdev
= bdget_disk(disk
, partno
);
1157 res
= __invalidate_device(bdev
);
1163 EXPORT_SYMBOL(invalidate_partition
);