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/proc_fs.h>
14 #include <linux/seq_file.h>
15 #include <linux/slab.h>
16 #include <linux/kmod.h>
17 #include <linux/kobj_map.h>
18 #include <linux/buffer_head.h>
19 #include <linux/mutex.h>
20 #include <linux/idr.h>
24 static DEFINE_MUTEX(block_class_lock
);
25 #ifndef CONFIG_SYSFS_DEPRECATED
26 struct kobject
*block_depr
;
29 /* for extended dynamic devt allocation, currently only one major is used */
30 #define MAX_EXT_DEVT (1 << MINORBITS)
32 /* For extended devt allocation. ext_devt_mutex prevents look up
33 * results from going away underneath its user.
35 static DEFINE_MUTEX(ext_devt_mutex
);
36 static DEFINE_IDR(ext_devt_idr
);
38 static struct device_type disk_type
;
41 * disk_get_part - get partition
42 * @disk: disk to look partition from
43 * @partno: partition number
45 * Look for partition @partno from @disk. If found, increment
46 * reference count and return it.
52 * Pointer to the found partition on success, NULL if not found.
54 struct hd_struct
*disk_get_part(struct gendisk
*disk
, int partno
)
56 struct hd_struct
*part
= NULL
;
57 struct disk_part_tbl
*ptbl
;
59 if (unlikely(partno
< 0))
64 ptbl
= rcu_dereference(disk
->part_tbl
);
65 if (likely(partno
< ptbl
->len
)) {
66 part
= rcu_dereference(ptbl
->part
[partno
]);
68 get_device(part_to_dev(part
));
75 EXPORT_SYMBOL_GPL(disk_get_part
);
78 * disk_part_iter_init - initialize partition iterator
79 * @piter: iterator to initialize
80 * @disk: disk to iterate over
81 * @flags: DISK_PITER_* flags
83 * Initialize @piter so that it iterates over partitions of @disk.
88 void disk_part_iter_init(struct disk_part_iter
*piter
, struct gendisk
*disk
,
91 struct disk_part_tbl
*ptbl
;
94 ptbl
= rcu_dereference(disk
->part_tbl
);
99 if (flags
& DISK_PITER_REVERSE
)
100 piter
->idx
= ptbl
->len
- 1;
101 else if (flags
& DISK_PITER_INCL_PART0
)
106 piter
->flags
= flags
;
110 EXPORT_SYMBOL_GPL(disk_part_iter_init
);
113 * disk_part_iter_next - proceed iterator to the next partition and return it
114 * @piter: iterator of interest
116 * Proceed @piter to the next partition and return it.
121 struct hd_struct
*disk_part_iter_next(struct disk_part_iter
*piter
)
123 struct disk_part_tbl
*ptbl
;
126 /* put the last partition */
127 disk_put_part(piter
->part
);
132 ptbl
= rcu_dereference(piter
->disk
->part_tbl
);
134 /* determine iteration parameters */
135 if (piter
->flags
& DISK_PITER_REVERSE
) {
137 if (piter
->flags
& DISK_PITER_INCL_PART0
)
146 /* iterate to the next partition */
147 for (; piter
->idx
!= end
; piter
->idx
+= inc
) {
148 struct hd_struct
*part
;
150 part
= rcu_dereference(ptbl
->part
[piter
->idx
]);
153 if (!(piter
->flags
& DISK_PITER_INCL_EMPTY
) && !part
->nr_sects
)
156 get_device(part_to_dev(part
));
166 EXPORT_SYMBOL_GPL(disk_part_iter_next
);
169 * disk_part_iter_exit - finish up partition iteration
170 * @piter: iter of interest
172 * Called when iteration is over. Cleans up @piter.
177 void disk_part_iter_exit(struct disk_part_iter
*piter
)
179 disk_put_part(piter
->part
);
182 EXPORT_SYMBOL_GPL(disk_part_iter_exit
);
185 * disk_map_sector_rcu - map sector to partition
186 * @disk: gendisk of interest
187 * @sector: sector to map
189 * Find out which partition @sector maps to on @disk. This is
190 * primarily used for stats accounting.
193 * RCU read locked. The returned partition pointer is valid only
194 * while preemption is disabled.
197 * Found partition on success, part0 is returned if no partition matches
199 struct hd_struct
*disk_map_sector_rcu(struct gendisk
*disk
, sector_t sector
)
201 struct disk_part_tbl
*ptbl
;
204 ptbl
= rcu_dereference(disk
->part_tbl
);
206 for (i
= 1; i
< ptbl
->len
; i
++) {
207 struct hd_struct
*part
= rcu_dereference(ptbl
->part
[i
]);
209 if (part
&& part
->start_sect
<= sector
&&
210 sector
< part
->start_sect
+ part
->nr_sects
)
215 EXPORT_SYMBOL_GPL(disk_map_sector_rcu
);
218 * Can be deleted altogether. Later.
221 static struct blk_major_name
{
222 struct blk_major_name
*next
;
225 } *major_names
[BLKDEV_MAJOR_HASH_SIZE
];
227 /* index in the above - for now: assume no multimajor ranges */
228 static inline int major_to_index(int major
)
230 return major
% BLKDEV_MAJOR_HASH_SIZE
;
233 #ifdef CONFIG_PROC_FS
234 void blkdev_show(struct seq_file
*seqf
, off_t offset
)
236 struct blk_major_name
*dp
;
238 if (offset
< BLKDEV_MAJOR_HASH_SIZE
) {
239 mutex_lock(&block_class_lock
);
240 for (dp
= major_names
[offset
]; dp
; dp
= dp
->next
)
241 seq_printf(seqf
, "%3d %s\n", dp
->major
, dp
->name
);
242 mutex_unlock(&block_class_lock
);
245 #endif /* CONFIG_PROC_FS */
247 int register_blkdev(unsigned int major
, const char *name
)
249 struct blk_major_name
**n
, *p
;
252 mutex_lock(&block_class_lock
);
256 for (index
= ARRAY_SIZE(major_names
)-1; index
> 0; index
--) {
257 if (major_names
[index
] == NULL
)
262 printk("register_blkdev: failed to get major for %s\n",
271 p
= kmalloc(sizeof(struct blk_major_name
), GFP_KERNEL
);
278 strlcpy(p
->name
, name
, sizeof(p
->name
));
280 index
= major_to_index(major
);
282 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
) {
283 if ((*n
)->major
== major
)
292 printk("register_blkdev: cannot get major %d for %s\n",
297 mutex_unlock(&block_class_lock
);
301 EXPORT_SYMBOL(register_blkdev
);
303 void unregister_blkdev(unsigned int major
, const char *name
)
305 struct blk_major_name
**n
;
306 struct blk_major_name
*p
= NULL
;
307 int index
= major_to_index(major
);
309 mutex_lock(&block_class_lock
);
310 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
)
311 if ((*n
)->major
== major
)
313 if (!*n
|| strcmp((*n
)->name
, name
)) {
319 mutex_unlock(&block_class_lock
);
323 EXPORT_SYMBOL(unregister_blkdev
);
325 static struct kobj_map
*bdev_map
;
328 * blk_mangle_minor - scatter minor numbers apart
329 * @minor: minor number to mangle
331 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
332 * is enabled. Mangling twice gives the original value.
340 static int blk_mangle_minor(int minor
)
342 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
345 for (i
= 0; i
< MINORBITS
/ 2; i
++) {
346 int low
= minor
& (1 << i
);
347 int high
= minor
& (1 << (MINORBITS
- 1 - i
));
348 int distance
= MINORBITS
- 1 - 2 * i
;
350 minor
^= low
| high
; /* clear both bits */
351 low
<<= distance
; /* swap the positions */
353 minor
|= low
| high
; /* and set */
360 * blk_alloc_devt - allocate a dev_t for a partition
361 * @part: partition to allocate dev_t for
362 * @devt: out parameter for resulting dev_t
364 * Allocate a dev_t for block device.
367 * 0 on success, allocated dev_t is returned in *@devt. -errno on
373 int blk_alloc_devt(struct hd_struct
*part
, dev_t
*devt
)
375 struct gendisk
*disk
= part_to_disk(part
);
378 /* in consecutive minor range? */
379 if (part
->partno
< disk
->minors
) {
380 *devt
= MKDEV(disk
->major
, disk
->first_minor
+ part
->partno
);
384 /* allocate ext devt */
386 if (!idr_pre_get(&ext_devt_idr
, GFP_KERNEL
))
388 rc
= idr_get_new(&ext_devt_idr
, part
, &idx
);
389 } while (rc
== -EAGAIN
);
394 if (idx
> MAX_EXT_DEVT
) {
395 idr_remove(&ext_devt_idr
, idx
);
399 *devt
= MKDEV(BLOCK_EXT_MAJOR
, blk_mangle_minor(idx
));
404 * blk_free_devt - free a dev_t
405 * @devt: dev_t to free
407 * Free @devt which was allocated using blk_alloc_devt().
412 void blk_free_devt(dev_t devt
)
416 if (devt
== MKDEV(0, 0))
419 if (MAJOR(devt
) == BLOCK_EXT_MAJOR
) {
420 mutex_lock(&ext_devt_mutex
);
421 idr_remove(&ext_devt_idr
, blk_mangle_minor(MINOR(devt
)));
422 mutex_unlock(&ext_devt_mutex
);
426 static char *bdevt_str(dev_t devt
, char *buf
)
428 if (MAJOR(devt
) <= 0xff && MINOR(devt
) <= 0xff) {
429 char tbuf
[BDEVT_SIZE
];
430 snprintf(tbuf
, BDEVT_SIZE
, "%02x%02x", MAJOR(devt
), MINOR(devt
));
431 snprintf(buf
, BDEVT_SIZE
, "%-9s", tbuf
);
433 snprintf(buf
, BDEVT_SIZE
, "%03x:%05x", MAJOR(devt
), MINOR(devt
));
439 * Register device numbers dev..(dev+range-1)
440 * range must be nonzero
441 * The hash chain is sorted on range, so that subranges can override.
443 void blk_register_region(dev_t devt
, unsigned long range
, struct module
*module
,
444 struct kobject
*(*probe
)(dev_t
, int *, void *),
445 int (*lock
)(dev_t
, void *), void *data
)
447 kobj_map(bdev_map
, devt
, range
, module
, probe
, lock
, data
);
450 EXPORT_SYMBOL(blk_register_region
);
452 void blk_unregister_region(dev_t devt
, unsigned long range
)
454 kobj_unmap(bdev_map
, devt
, range
);
457 EXPORT_SYMBOL(blk_unregister_region
);
459 static struct kobject
*exact_match(dev_t devt
, int *partno
, void *data
)
461 struct gendisk
*p
= data
;
463 return &disk_to_dev(p
)->kobj
;
466 static int exact_lock(dev_t devt
, void *data
)
468 struct gendisk
*p
= data
;
476 * add_disk - add partitioning information to kernel list
477 * @disk: per-device partitioning information
479 * This function registers the partitioning information in @disk
482 * FIXME: error handling
484 void add_disk(struct gendisk
*disk
)
486 struct backing_dev_info
*bdi
;
490 /* minors == 0 indicates to use ext devt from part0 and should
491 * be accompanied with EXT_DEVT flag. Make sure all
492 * parameters make sense.
494 WARN_ON(disk
->minors
&& !(disk
->major
|| disk
->first_minor
));
495 WARN_ON(!disk
->minors
&& !(disk
->flags
& GENHD_FL_EXT_DEVT
));
497 disk
->flags
|= GENHD_FL_UP
;
499 retval
= blk_alloc_devt(&disk
->part0
, &devt
);
504 disk_to_dev(disk
)->devt
= devt
;
506 /* ->major and ->first_minor aren't supposed to be
507 * dereferenced from here on, but set them just in case.
509 disk
->major
= MAJOR(devt
);
510 disk
->first_minor
= MINOR(devt
);
512 blk_register_region(disk_devt(disk
), disk
->minors
, NULL
,
513 exact_match
, exact_lock
, disk
);
515 blk_register_queue(disk
);
517 bdi
= &disk
->queue
->backing_dev_info
;
518 bdi_register_dev(bdi
, disk_devt(disk
));
519 retval
= sysfs_create_link(&disk_to_dev(disk
)->kobj
, &bdi
->dev
->kobj
,
524 EXPORT_SYMBOL(add_disk
);
525 EXPORT_SYMBOL(del_gendisk
); /* in partitions/check.c */
527 void unlink_gendisk(struct gendisk
*disk
)
529 sysfs_remove_link(&disk_to_dev(disk
)->kobj
, "bdi");
530 bdi_unregister(&disk
->queue
->backing_dev_info
);
531 blk_unregister_queue(disk
);
532 blk_unregister_region(disk_devt(disk
), disk
->minors
);
536 * get_gendisk - get partitioning information for a given device
537 * @devt: device to get partitioning information for
538 * @partno: returned partition index
540 * This function gets the structure containing partitioning
541 * information for the given device @devt.
543 struct gendisk
*get_gendisk(dev_t devt
, int *partno
)
545 struct gendisk
*disk
= NULL
;
547 if (MAJOR(devt
) != BLOCK_EXT_MAJOR
) {
548 struct kobject
*kobj
;
550 kobj
= kobj_lookup(bdev_map
, devt
, partno
);
552 disk
= dev_to_disk(kobj_to_dev(kobj
));
554 struct hd_struct
*part
;
556 mutex_lock(&ext_devt_mutex
);
557 part
= idr_find(&ext_devt_idr
, blk_mangle_minor(MINOR(devt
)));
558 if (part
&& get_disk(part_to_disk(part
))) {
559 *partno
= part
->partno
;
560 disk
= part_to_disk(part
);
562 mutex_unlock(&ext_devt_mutex
);
569 * bdget_disk - do bdget() by gendisk and partition number
570 * @disk: gendisk of interest
571 * @partno: partition number
573 * Find partition @partno from @disk, do bdget() on it.
579 * Resulting block_device on success, NULL on failure.
581 struct block_device
*bdget_disk(struct gendisk
*disk
, int partno
)
583 struct hd_struct
*part
;
584 struct block_device
*bdev
= NULL
;
586 part
= disk_get_part(disk
, partno
);
588 bdev
= bdget(part_devt(part
));
593 EXPORT_SYMBOL(bdget_disk
);
596 * print a full list of all partitions - intended for places where the root
597 * filesystem can't be mounted and thus to give the victim some idea of what
600 void __init
printk_all_partitions(void)
602 struct class_dev_iter iter
;
605 class_dev_iter_init(&iter
, &block_class
, NULL
, &disk_type
);
606 while ((dev
= class_dev_iter_next(&iter
))) {
607 struct gendisk
*disk
= dev_to_disk(dev
);
608 struct disk_part_iter piter
;
609 struct hd_struct
*part
;
610 char name_buf
[BDEVNAME_SIZE
];
611 char devt_buf
[BDEVT_SIZE
];
614 * Don't show empty devices or things that have been
617 if (get_capacity(disk
) == 0 ||
618 (disk
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
))
622 * Note, unlike /proc/partitions, I am showing the
623 * numbers in hex - the same format as the root=
626 disk_part_iter_init(&piter
, disk
, DISK_PITER_INCL_PART0
);
627 while ((part
= disk_part_iter_next(&piter
))) {
628 bool is_part0
= part
== &disk
->part0
;
630 printk("%s%s %10llu %s", is_part0
? "" : " ",
631 bdevt_str(part_devt(part
), devt_buf
),
632 (unsigned long long)part
->nr_sects
>> 1,
633 disk_name(disk
, part
->partno
, name_buf
));
635 if (disk
->driverfs_dev
!= NULL
&&
636 disk
->driverfs_dev
->driver
!= NULL
)
637 printk(" driver: %s\n",
638 disk
->driverfs_dev
->driver
->name
);
640 printk(" (driver?)\n");
644 disk_part_iter_exit(&piter
);
646 class_dev_iter_exit(&iter
);
649 #ifdef CONFIG_PROC_FS
651 static void *disk_seqf_start(struct seq_file
*seqf
, loff_t
*pos
)
654 struct class_dev_iter
*iter
;
657 iter
= kmalloc(sizeof(*iter
), GFP_KERNEL
);
659 return ERR_PTR(-ENOMEM
);
661 seqf
->private = iter
;
662 class_dev_iter_init(iter
, &block_class
, NULL
, &disk_type
);
664 dev
= class_dev_iter_next(iter
);
669 return dev_to_disk(dev
);
672 static void *disk_seqf_next(struct seq_file
*seqf
, void *v
, loff_t
*pos
)
677 dev
= class_dev_iter_next(seqf
->private);
679 return dev_to_disk(dev
);
684 static void disk_seqf_stop(struct seq_file
*seqf
, void *v
)
686 struct class_dev_iter
*iter
= seqf
->private;
688 /* stop is called even after start failed :-( */
690 class_dev_iter_exit(iter
);
695 static void *show_partition_start(struct seq_file
*seqf
, loff_t
*pos
)
699 p
= disk_seqf_start(seqf
, pos
);
700 if (!IS_ERR(p
) && p
&& !*pos
)
701 seq_puts(seqf
, "major minor #blocks name\n\n");
705 static int show_partition(struct seq_file
*seqf
, void *v
)
707 struct gendisk
*sgp
= v
;
708 struct disk_part_iter piter
;
709 struct hd_struct
*part
;
710 char buf
[BDEVNAME_SIZE
];
712 /* Don't show non-partitionable removeable devices or empty devices */
713 if (!get_capacity(sgp
) || (!disk_partitionable(sgp
) &&
714 (sgp
->flags
& GENHD_FL_REMOVABLE
)))
716 if (sgp
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
)
719 /* show the full disk and all non-0 size partitions of it */
720 disk_part_iter_init(&piter
, sgp
, DISK_PITER_INCL_PART0
);
721 while ((part
= disk_part_iter_next(&piter
)))
722 seq_printf(seqf
, "%4d %7d %10llu %s\n",
723 MAJOR(part_devt(part
)), MINOR(part_devt(part
)),
724 (unsigned long long)part
->nr_sects
>> 1,
725 disk_name(sgp
, part
->partno
, buf
));
726 disk_part_iter_exit(&piter
);
731 static const struct seq_operations partitions_op
= {
732 .start
= show_partition_start
,
733 .next
= disk_seqf_next
,
734 .stop
= disk_seqf_stop
,
735 .show
= show_partition
738 static int partitions_open(struct inode
*inode
, struct file
*file
)
740 return seq_open(file
, &partitions_op
);
743 static const struct file_operations proc_partitions_operations
= {
744 .open
= partitions_open
,
747 .release
= seq_release
,
752 static struct kobject
*base_probe(dev_t devt
, int *partno
, void *data
)
754 if (request_module("block-major-%d-%d", MAJOR(devt
), MINOR(devt
)) > 0)
755 /* Make old-style 2.4 aliases work */
756 request_module("block-major-%d", MAJOR(devt
));
760 static int __init
genhd_device_init(void)
764 block_class
.dev_kobj
= sysfs_dev_block_kobj
;
765 error
= class_register(&block_class
);
768 bdev_map
= kobj_map_init(base_probe
, &block_class_lock
);
771 register_blkdev(BLOCK_EXT_MAJOR
, "blkext");
773 #ifndef CONFIG_SYSFS_DEPRECATED
774 /* create top-level block dir */
775 block_depr
= kobject_create_and_add("block", NULL
);
780 subsys_initcall(genhd_device_init
);
782 static ssize_t
disk_range_show(struct device
*dev
,
783 struct device_attribute
*attr
, char *buf
)
785 struct gendisk
*disk
= dev_to_disk(dev
);
787 return sprintf(buf
, "%d\n", disk
->minors
);
790 static ssize_t
disk_ext_range_show(struct device
*dev
,
791 struct device_attribute
*attr
, char *buf
)
793 struct gendisk
*disk
= dev_to_disk(dev
);
795 return sprintf(buf
, "%d\n", disk_max_parts(disk
));
798 static ssize_t
disk_removable_show(struct device
*dev
,
799 struct device_attribute
*attr
, char *buf
)
801 struct gendisk
*disk
= dev_to_disk(dev
);
803 return sprintf(buf
, "%d\n",
804 (disk
->flags
& GENHD_FL_REMOVABLE
? 1 : 0));
807 static ssize_t
disk_ro_show(struct device
*dev
,
808 struct device_attribute
*attr
, char *buf
)
810 struct gendisk
*disk
= dev_to_disk(dev
);
812 return sprintf(buf
, "%d\n", get_disk_ro(disk
) ? 1 : 0);
815 static ssize_t
disk_capability_show(struct device
*dev
,
816 struct device_attribute
*attr
, char *buf
)
818 struct gendisk
*disk
= dev_to_disk(dev
);
820 return sprintf(buf
, "%x\n", disk
->flags
);
823 static DEVICE_ATTR(range
, S_IRUGO
, disk_range_show
, NULL
);
824 static DEVICE_ATTR(ext_range
, S_IRUGO
, disk_ext_range_show
, NULL
);
825 static DEVICE_ATTR(removable
, S_IRUGO
, disk_removable_show
, NULL
);
826 static DEVICE_ATTR(ro
, S_IRUGO
, disk_ro_show
, NULL
);
827 static DEVICE_ATTR(size
, S_IRUGO
, part_size_show
, NULL
);
828 static DEVICE_ATTR(capability
, S_IRUGO
, disk_capability_show
, NULL
);
829 static DEVICE_ATTR(stat
, S_IRUGO
, part_stat_show
, NULL
);
830 #ifdef CONFIG_FAIL_MAKE_REQUEST
831 static struct device_attribute dev_attr_fail
=
832 __ATTR(make
-it
-fail
, S_IRUGO
|S_IWUSR
, part_fail_show
, part_fail_store
);
834 #ifdef CONFIG_FAIL_IO_TIMEOUT
835 static struct device_attribute dev_attr_fail_timeout
=
836 __ATTR(io
-timeout
-fail
, S_IRUGO
|S_IWUSR
, part_timeout_show
,
840 static struct attribute
*disk_attrs
[] = {
841 &dev_attr_range
.attr
,
842 &dev_attr_ext_range
.attr
,
843 &dev_attr_removable
.attr
,
846 &dev_attr_capability
.attr
,
848 #ifdef CONFIG_FAIL_MAKE_REQUEST
851 #ifdef CONFIG_FAIL_IO_TIMEOUT
852 &dev_attr_fail_timeout
.attr
,
857 static struct attribute_group disk_attr_group
= {
861 static struct attribute_group
*disk_attr_groups
[] = {
866 static void disk_free_ptbl_rcu_cb(struct rcu_head
*head
)
868 struct disk_part_tbl
*ptbl
=
869 container_of(head
, struct disk_part_tbl
, rcu_head
);
875 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
876 * @disk: disk to replace part_tbl for
877 * @new_ptbl: new part_tbl to install
879 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
880 * original ptbl is freed using RCU callback.
883 * Matching bd_mutx locked.
885 static void disk_replace_part_tbl(struct gendisk
*disk
,
886 struct disk_part_tbl
*new_ptbl
)
888 struct disk_part_tbl
*old_ptbl
= disk
->part_tbl
;
890 rcu_assign_pointer(disk
->part_tbl
, new_ptbl
);
892 call_rcu(&old_ptbl
->rcu_head
, disk_free_ptbl_rcu_cb
);
896 * disk_expand_part_tbl - expand disk->part_tbl
897 * @disk: disk to expand part_tbl for
898 * @partno: expand such that this partno can fit in
900 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
901 * uses RCU to allow unlocked dereferencing for stats and other stuff.
904 * Matching bd_mutex locked, might sleep.
907 * 0 on success, -errno on failure.
909 int disk_expand_part_tbl(struct gendisk
*disk
, int partno
)
911 struct disk_part_tbl
*old_ptbl
= disk
->part_tbl
;
912 struct disk_part_tbl
*new_ptbl
;
913 int len
= old_ptbl
? old_ptbl
->len
: 0;
914 int target
= partno
+ 1;
918 /* disk_max_parts() is zero during initialization, ignore if so */
919 if (disk_max_parts(disk
) && target
> disk_max_parts(disk
))
925 size
= sizeof(*new_ptbl
) + target
* sizeof(new_ptbl
->part
[0]);
926 new_ptbl
= kzalloc_node(size
, GFP_KERNEL
, disk
->node_id
);
930 INIT_RCU_HEAD(&new_ptbl
->rcu_head
);
931 new_ptbl
->len
= target
;
933 for (i
= 0; i
< len
; i
++)
934 rcu_assign_pointer(new_ptbl
->part
[i
], old_ptbl
->part
[i
]);
936 disk_replace_part_tbl(disk
, new_ptbl
);
940 static void disk_release(struct device
*dev
)
942 struct gendisk
*disk
= dev_to_disk(dev
);
945 disk_replace_part_tbl(disk
, NULL
);
946 free_part_stats(&disk
->part0
);
949 struct class block_class
= {
953 static struct device_type disk_type
= {
955 .groups
= disk_attr_groups
,
956 .release
= disk_release
,
959 #ifdef CONFIG_PROC_FS
961 * aggregate disk stat collector. Uses the same stats that the sysfs
962 * entries do, above, but makes them available through one seq_file.
964 * The output looks suspiciously like /proc/partitions with a bunch of
967 static int diskstats_show(struct seq_file
*seqf
, void *v
)
969 struct gendisk
*gp
= v
;
970 struct disk_part_iter piter
;
971 struct hd_struct
*hd
;
972 char buf
[BDEVNAME_SIZE
];
976 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
977 seq_puts(seqf, "major minor name"
978 " rio rmerge rsect ruse wio wmerge "
979 "wsect wuse running use aveq"
983 disk_part_iter_init(&piter
, gp
, DISK_PITER_INCL_PART0
);
984 while ((hd
= disk_part_iter_next(&piter
))) {
985 cpu
= part_stat_lock();
986 part_round_stats(cpu
, hd
);
988 seq_printf(seqf
, "%4d %7d %s %lu %lu %llu "
989 "%u %lu %lu %llu %u %u %u %u\n",
990 MAJOR(part_devt(hd
)), MINOR(part_devt(hd
)),
991 disk_name(gp
, hd
->partno
, buf
),
992 part_stat_read(hd
, ios
[0]),
993 part_stat_read(hd
, merges
[0]),
994 (unsigned long long)part_stat_read(hd
, sectors
[0]),
995 jiffies_to_msecs(part_stat_read(hd
, ticks
[0])),
996 part_stat_read(hd
, ios
[1]),
997 part_stat_read(hd
, merges
[1]),
998 (unsigned long long)part_stat_read(hd
, sectors
[1]),
999 jiffies_to_msecs(part_stat_read(hd
, ticks
[1])),
1001 jiffies_to_msecs(part_stat_read(hd
, io_ticks
)),
1002 jiffies_to_msecs(part_stat_read(hd
, time_in_queue
))
1005 disk_part_iter_exit(&piter
);
1010 static const struct seq_operations diskstats_op
= {
1011 .start
= disk_seqf_start
,
1012 .next
= disk_seqf_next
,
1013 .stop
= disk_seqf_stop
,
1014 .show
= diskstats_show
1017 static int diskstats_open(struct inode
*inode
, struct file
*file
)
1019 return seq_open(file
, &diskstats_op
);
1022 static const struct file_operations proc_diskstats_operations
= {
1023 .open
= diskstats_open
,
1025 .llseek
= seq_lseek
,
1026 .release
= seq_release
,
1029 static int __init
proc_genhd_init(void)
1031 proc_create("diskstats", 0, NULL
, &proc_diskstats_operations
);
1032 proc_create("partitions", 0, NULL
, &proc_partitions_operations
);
1035 module_init(proc_genhd_init
);
1036 #endif /* CONFIG_PROC_FS */
1038 static void media_change_notify_thread(struct work_struct
*work
)
1040 struct gendisk
*gd
= container_of(work
, struct gendisk
, async_notify
);
1041 char event
[] = "MEDIA_CHANGE=1";
1042 char *envp
[] = { event
, NULL
};
1045 * set enviroment vars to indicate which event this is for
1046 * so that user space will know to go check the media status.
1048 kobject_uevent_env(&disk_to_dev(gd
)->kobj
, KOBJ_CHANGE
, envp
);
1049 put_device(gd
->driverfs_dev
);
1053 void genhd_media_change_notify(struct gendisk
*disk
)
1055 get_device(disk
->driverfs_dev
);
1056 schedule_work(&disk
->async_notify
);
1058 EXPORT_SYMBOL_GPL(genhd_media_change_notify
);
1061 dev_t
blk_lookup_devt(const char *name
, int partno
)
1063 dev_t devt
= MKDEV(0, 0);
1064 struct class_dev_iter iter
;
1067 class_dev_iter_init(&iter
, &block_class
, NULL
, &disk_type
);
1068 while ((dev
= class_dev_iter_next(&iter
))) {
1069 struct gendisk
*disk
= dev_to_disk(dev
);
1070 struct hd_struct
*part
;
1072 if (strcmp(dev
->bus_id
, name
))
1075 part
= disk_get_part(disk
, partno
);
1077 devt
= part_devt(part
);
1078 disk_put_part(part
);
1081 disk_put_part(part
);
1083 class_dev_iter_exit(&iter
);
1086 EXPORT_SYMBOL(blk_lookup_devt
);
1088 struct gendisk
*alloc_disk(int minors
)
1090 return alloc_disk_node(minors
, -1);
1092 EXPORT_SYMBOL(alloc_disk
);
1094 struct gendisk
*alloc_disk_node(int minors
, int node_id
)
1096 struct gendisk
*disk
;
1098 disk
= kmalloc_node(sizeof(struct gendisk
),
1099 GFP_KERNEL
| __GFP_ZERO
, node_id
);
1101 if (!init_part_stats(&disk
->part0
)) {
1105 if (disk_expand_part_tbl(disk
, 0)) {
1106 free_part_stats(&disk
->part0
);
1110 disk
->part_tbl
->part
[0] = &disk
->part0
;
1112 disk
->minors
= minors
;
1113 rand_initialize_disk(disk
);
1114 disk_to_dev(disk
)->class = &block_class
;
1115 disk_to_dev(disk
)->type
= &disk_type
;
1116 device_initialize(disk_to_dev(disk
));
1117 INIT_WORK(&disk
->async_notify
,
1118 media_change_notify_thread
);
1119 disk
->node_id
= node_id
;
1123 EXPORT_SYMBOL(alloc_disk_node
);
1125 struct kobject
*get_disk(struct gendisk
*disk
)
1127 struct module
*owner
;
1128 struct kobject
*kobj
;
1132 owner
= disk
->fops
->owner
;
1133 if (owner
&& !try_module_get(owner
))
1135 kobj
= kobject_get(&disk_to_dev(disk
)->kobj
);
1144 EXPORT_SYMBOL(get_disk
);
1146 void put_disk(struct gendisk
*disk
)
1149 kobject_put(&disk_to_dev(disk
)->kobj
);
1152 EXPORT_SYMBOL(put_disk
);
1154 void set_device_ro(struct block_device
*bdev
, int flag
)
1156 bdev
->bd_part
->policy
= flag
;
1159 EXPORT_SYMBOL(set_device_ro
);
1161 void set_disk_ro(struct gendisk
*disk
, int flag
)
1163 struct disk_part_iter piter
;
1164 struct hd_struct
*part
;
1166 disk_part_iter_init(&piter
, disk
,
1167 DISK_PITER_INCL_EMPTY
| DISK_PITER_INCL_PART0
);
1168 while ((part
= disk_part_iter_next(&piter
)))
1169 part
->policy
= flag
;
1170 disk_part_iter_exit(&piter
);
1173 EXPORT_SYMBOL(set_disk_ro
);
1175 int bdev_read_only(struct block_device
*bdev
)
1179 return bdev
->bd_part
->policy
;
1182 EXPORT_SYMBOL(bdev_read_only
);
1184 int invalidate_partition(struct gendisk
*disk
, int partno
)
1187 struct block_device
*bdev
= bdget_disk(disk
, partno
);
1190 res
= __invalidate_device(bdev
);
1196 EXPORT_SYMBOL(invalidate_partition
);