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
);
184 static inline int sector_in_part(struct hd_struct
*part
, sector_t sector
)
186 return part
->start_sect
<= sector
&&
187 sector
< part
->start_sect
+ part
->nr_sects
;
191 * disk_map_sector_rcu - map sector to partition
192 * @disk: gendisk of interest
193 * @sector: sector to map
195 * Find out which partition @sector maps to on @disk. This is
196 * primarily used for stats accounting.
199 * RCU read locked. The returned partition pointer is valid only
200 * while preemption is disabled.
203 * Found partition on success, part0 is returned if no partition matches
205 struct hd_struct
*disk_map_sector_rcu(struct gendisk
*disk
, sector_t sector
)
207 struct disk_part_tbl
*ptbl
;
208 struct hd_struct
*part
;
211 ptbl
= rcu_dereference(disk
->part_tbl
);
213 part
= rcu_dereference(ptbl
->last_lookup
);
214 if (part
&& sector_in_part(part
, sector
))
217 for (i
= 1; i
< ptbl
->len
; i
++) {
218 part
= rcu_dereference(ptbl
->part
[i
]);
220 if (part
&& sector_in_part(part
, sector
)) {
221 rcu_assign_pointer(ptbl
->last_lookup
, part
);
227 EXPORT_SYMBOL_GPL(disk_map_sector_rcu
);
230 * Can be deleted altogether. Later.
233 static struct blk_major_name
{
234 struct blk_major_name
*next
;
237 } *major_names
[BLKDEV_MAJOR_HASH_SIZE
];
239 /* index in the above - for now: assume no multimajor ranges */
240 static inline int major_to_index(int major
)
242 return major
% BLKDEV_MAJOR_HASH_SIZE
;
245 #ifdef CONFIG_PROC_FS
246 void blkdev_show(struct seq_file
*seqf
, off_t offset
)
248 struct blk_major_name
*dp
;
250 if (offset
< BLKDEV_MAJOR_HASH_SIZE
) {
251 mutex_lock(&block_class_lock
);
252 for (dp
= major_names
[offset
]; dp
; dp
= dp
->next
)
253 seq_printf(seqf
, "%3d %s\n", dp
->major
, dp
->name
);
254 mutex_unlock(&block_class_lock
);
257 #endif /* CONFIG_PROC_FS */
260 * register_blkdev - register a new block device
262 * @major: the requested major device number [1..255]. If @major=0, try to
263 * allocate any unused major number.
264 * @name: the name of the new block device as a zero terminated string
266 * The @name must be unique within the system.
268 * The return value depends on the @major input parameter.
269 * - if a major device number was requested in range [1..255] then the
270 * function returns zero on success, or a negative error code
271 * - if any unused major number was requested with @major=0 parameter
272 * then the return value is the allocated major number in range
273 * [1..255] or a negative error code otherwise
275 int register_blkdev(unsigned int major
, const char *name
)
277 struct blk_major_name
**n
, *p
;
280 mutex_lock(&block_class_lock
);
284 for (index
= ARRAY_SIZE(major_names
)-1; index
> 0; index
--) {
285 if (major_names
[index
] == NULL
)
290 printk("register_blkdev: failed to get major for %s\n",
299 p
= kmalloc(sizeof(struct blk_major_name
), GFP_KERNEL
);
306 strlcpy(p
->name
, name
, sizeof(p
->name
));
308 index
= major_to_index(major
);
310 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
) {
311 if ((*n
)->major
== major
)
320 printk("register_blkdev: cannot get major %d for %s\n",
325 mutex_unlock(&block_class_lock
);
329 EXPORT_SYMBOL(register_blkdev
);
331 void unregister_blkdev(unsigned int major
, const char *name
)
333 struct blk_major_name
**n
;
334 struct blk_major_name
*p
= NULL
;
335 int index
= major_to_index(major
);
337 mutex_lock(&block_class_lock
);
338 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
)
339 if ((*n
)->major
== major
)
341 if (!*n
|| strcmp((*n
)->name
, name
)) {
347 mutex_unlock(&block_class_lock
);
351 EXPORT_SYMBOL(unregister_blkdev
);
353 static struct kobj_map
*bdev_map
;
356 * blk_mangle_minor - scatter minor numbers apart
357 * @minor: minor number to mangle
359 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
360 * is enabled. Mangling twice gives the original value.
368 static int blk_mangle_minor(int minor
)
370 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
373 for (i
= 0; i
< MINORBITS
/ 2; i
++) {
374 int low
= minor
& (1 << i
);
375 int high
= minor
& (1 << (MINORBITS
- 1 - i
));
376 int distance
= MINORBITS
- 1 - 2 * i
;
378 minor
^= low
| high
; /* clear both bits */
379 low
<<= distance
; /* swap the positions */
381 minor
|= low
| high
; /* and set */
388 * blk_alloc_devt - allocate a dev_t for a partition
389 * @part: partition to allocate dev_t for
390 * @devt: out parameter for resulting dev_t
392 * Allocate a dev_t for block device.
395 * 0 on success, allocated dev_t is returned in *@devt. -errno on
401 int blk_alloc_devt(struct hd_struct
*part
, dev_t
*devt
)
403 struct gendisk
*disk
= part_to_disk(part
);
406 /* in consecutive minor range? */
407 if (part
->partno
< disk
->minors
) {
408 *devt
= MKDEV(disk
->major
, disk
->first_minor
+ part
->partno
);
412 /* allocate ext devt */
414 if (!idr_pre_get(&ext_devt_idr
, GFP_KERNEL
))
416 rc
= idr_get_new(&ext_devt_idr
, part
, &idx
);
417 } while (rc
== -EAGAIN
);
422 if (idx
> MAX_EXT_DEVT
) {
423 idr_remove(&ext_devt_idr
, idx
);
427 *devt
= MKDEV(BLOCK_EXT_MAJOR
, blk_mangle_minor(idx
));
432 * blk_free_devt - free a dev_t
433 * @devt: dev_t to free
435 * Free @devt which was allocated using blk_alloc_devt().
440 void blk_free_devt(dev_t devt
)
444 if (devt
== MKDEV(0, 0))
447 if (MAJOR(devt
) == BLOCK_EXT_MAJOR
) {
448 mutex_lock(&ext_devt_mutex
);
449 idr_remove(&ext_devt_idr
, blk_mangle_minor(MINOR(devt
)));
450 mutex_unlock(&ext_devt_mutex
);
454 static char *bdevt_str(dev_t devt
, char *buf
)
456 if (MAJOR(devt
) <= 0xff && MINOR(devt
) <= 0xff) {
457 char tbuf
[BDEVT_SIZE
];
458 snprintf(tbuf
, BDEVT_SIZE
, "%02x%02x", MAJOR(devt
), MINOR(devt
));
459 snprintf(buf
, BDEVT_SIZE
, "%-9s", tbuf
);
461 snprintf(buf
, BDEVT_SIZE
, "%03x:%05x", MAJOR(devt
), MINOR(devt
));
467 * Register device numbers dev..(dev+range-1)
468 * range must be nonzero
469 * The hash chain is sorted on range, so that subranges can override.
471 void blk_register_region(dev_t devt
, unsigned long range
, struct module
*module
,
472 struct kobject
*(*probe
)(dev_t
, int *, void *),
473 int (*lock
)(dev_t
, void *), void *data
)
475 kobj_map(bdev_map
, devt
, range
, module
, probe
, lock
, data
);
478 EXPORT_SYMBOL(blk_register_region
);
480 void blk_unregister_region(dev_t devt
, unsigned long range
)
482 kobj_unmap(bdev_map
, devt
, range
);
485 EXPORT_SYMBOL(blk_unregister_region
);
487 static struct kobject
*exact_match(dev_t devt
, int *partno
, void *data
)
489 struct gendisk
*p
= data
;
491 return &disk_to_dev(p
)->kobj
;
494 static int exact_lock(dev_t devt
, void *data
)
496 struct gendisk
*p
= data
;
504 * add_disk - add partitioning information to kernel list
505 * @disk: per-device partitioning information
507 * This function registers the partitioning information in @disk
510 * FIXME: error handling
512 void add_disk(struct gendisk
*disk
)
514 struct backing_dev_info
*bdi
;
518 /* minors == 0 indicates to use ext devt from part0 and should
519 * be accompanied with EXT_DEVT flag. Make sure all
520 * parameters make sense.
522 WARN_ON(disk
->minors
&& !(disk
->major
|| disk
->first_minor
));
523 WARN_ON(!disk
->minors
&& !(disk
->flags
& GENHD_FL_EXT_DEVT
));
525 disk
->flags
|= GENHD_FL_UP
;
527 retval
= blk_alloc_devt(&disk
->part0
, &devt
);
532 disk_to_dev(disk
)->devt
= devt
;
534 /* ->major and ->first_minor aren't supposed to be
535 * dereferenced from here on, but set them just in case.
537 disk
->major
= MAJOR(devt
);
538 disk
->first_minor
= MINOR(devt
);
540 blk_register_region(disk_devt(disk
), disk
->minors
, NULL
,
541 exact_match
, exact_lock
, disk
);
543 blk_register_queue(disk
);
545 bdi
= &disk
->queue
->backing_dev_info
;
546 bdi_register_dev(bdi
, disk_devt(disk
));
547 retval
= sysfs_create_link(&disk_to_dev(disk
)->kobj
, &bdi
->dev
->kobj
,
552 EXPORT_SYMBOL(add_disk
);
553 EXPORT_SYMBOL(del_gendisk
); /* in partitions/check.c */
555 void unlink_gendisk(struct gendisk
*disk
)
557 sysfs_remove_link(&disk_to_dev(disk
)->kobj
, "bdi");
558 bdi_unregister(&disk
->queue
->backing_dev_info
);
559 blk_unregister_queue(disk
);
560 blk_unregister_region(disk_devt(disk
), disk
->minors
);
564 * get_gendisk - get partitioning information for a given device
565 * @devt: device to get partitioning information for
566 * @partno: returned partition index
568 * This function gets the structure containing partitioning
569 * information for the given device @devt.
571 struct gendisk
*get_gendisk(dev_t devt
, int *partno
)
573 struct gendisk
*disk
= NULL
;
575 if (MAJOR(devt
) != BLOCK_EXT_MAJOR
) {
576 struct kobject
*kobj
;
578 kobj
= kobj_lookup(bdev_map
, devt
, partno
);
580 disk
= dev_to_disk(kobj_to_dev(kobj
));
582 struct hd_struct
*part
;
584 mutex_lock(&ext_devt_mutex
);
585 part
= idr_find(&ext_devt_idr
, blk_mangle_minor(MINOR(devt
)));
586 if (part
&& get_disk(part_to_disk(part
))) {
587 *partno
= part
->partno
;
588 disk
= part_to_disk(part
);
590 mutex_unlock(&ext_devt_mutex
);
597 * bdget_disk - do bdget() by gendisk and partition number
598 * @disk: gendisk of interest
599 * @partno: partition number
601 * Find partition @partno from @disk, do bdget() on it.
607 * Resulting block_device on success, NULL on failure.
609 struct block_device
*bdget_disk(struct gendisk
*disk
, int partno
)
611 struct hd_struct
*part
;
612 struct block_device
*bdev
= NULL
;
614 part
= disk_get_part(disk
, partno
);
616 bdev
= bdget(part_devt(part
));
621 EXPORT_SYMBOL(bdget_disk
);
624 * print a full list of all partitions - intended for places where the root
625 * filesystem can't be mounted and thus to give the victim some idea of what
628 void __init
printk_all_partitions(void)
630 struct class_dev_iter iter
;
633 class_dev_iter_init(&iter
, &block_class
, NULL
, &disk_type
);
634 while ((dev
= class_dev_iter_next(&iter
))) {
635 struct gendisk
*disk
= dev_to_disk(dev
);
636 struct disk_part_iter piter
;
637 struct hd_struct
*part
;
638 char name_buf
[BDEVNAME_SIZE
];
639 char devt_buf
[BDEVT_SIZE
];
642 * Don't show empty devices or things that have been
645 if (get_capacity(disk
) == 0 ||
646 (disk
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
))
650 * Note, unlike /proc/partitions, I am showing the
651 * numbers in hex - the same format as the root=
654 disk_part_iter_init(&piter
, disk
, DISK_PITER_INCL_PART0
);
655 while ((part
= disk_part_iter_next(&piter
))) {
656 bool is_part0
= part
== &disk
->part0
;
658 printk("%s%s %10llu %s", is_part0
? "" : " ",
659 bdevt_str(part_devt(part
), devt_buf
),
660 (unsigned long long)part
->nr_sects
>> 1,
661 disk_name(disk
, part
->partno
, name_buf
));
663 if (disk
->driverfs_dev
!= NULL
&&
664 disk
->driverfs_dev
->driver
!= NULL
)
665 printk(" driver: %s\n",
666 disk
->driverfs_dev
->driver
->name
);
668 printk(" (driver?)\n");
672 disk_part_iter_exit(&piter
);
674 class_dev_iter_exit(&iter
);
677 #ifdef CONFIG_PROC_FS
679 static void *disk_seqf_start(struct seq_file
*seqf
, loff_t
*pos
)
682 struct class_dev_iter
*iter
;
685 iter
= kmalloc(sizeof(*iter
), GFP_KERNEL
);
687 return ERR_PTR(-ENOMEM
);
689 seqf
->private = iter
;
690 class_dev_iter_init(iter
, &block_class
, NULL
, &disk_type
);
692 dev
= class_dev_iter_next(iter
);
697 return dev_to_disk(dev
);
700 static void *disk_seqf_next(struct seq_file
*seqf
, void *v
, loff_t
*pos
)
705 dev
= class_dev_iter_next(seqf
->private);
707 return dev_to_disk(dev
);
712 static void disk_seqf_stop(struct seq_file
*seqf
, void *v
)
714 struct class_dev_iter
*iter
= seqf
->private;
716 /* stop is called even after start failed :-( */
718 class_dev_iter_exit(iter
);
723 static void *show_partition_start(struct seq_file
*seqf
, loff_t
*pos
)
727 p
= disk_seqf_start(seqf
, pos
);
728 if (!IS_ERR(p
) && p
&& !*pos
)
729 seq_puts(seqf
, "major minor #blocks name\n\n");
733 static int show_partition(struct seq_file
*seqf
, void *v
)
735 struct gendisk
*sgp
= v
;
736 struct disk_part_iter piter
;
737 struct hd_struct
*part
;
738 char buf
[BDEVNAME_SIZE
];
740 /* Don't show non-partitionable removeable devices or empty devices */
741 if (!get_capacity(sgp
) || (!disk_partitionable(sgp
) &&
742 (sgp
->flags
& GENHD_FL_REMOVABLE
)))
744 if (sgp
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
)
747 /* show the full disk and all non-0 size partitions of it */
748 disk_part_iter_init(&piter
, sgp
, DISK_PITER_INCL_PART0
);
749 while ((part
= disk_part_iter_next(&piter
)))
750 seq_printf(seqf
, "%4d %7d %10llu %s\n",
751 MAJOR(part_devt(part
)), MINOR(part_devt(part
)),
752 (unsigned long long)part
->nr_sects
>> 1,
753 disk_name(sgp
, part
->partno
, buf
));
754 disk_part_iter_exit(&piter
);
759 static const struct seq_operations partitions_op
= {
760 .start
= show_partition_start
,
761 .next
= disk_seqf_next
,
762 .stop
= disk_seqf_stop
,
763 .show
= show_partition
766 static int partitions_open(struct inode
*inode
, struct file
*file
)
768 return seq_open(file
, &partitions_op
);
771 static const struct file_operations proc_partitions_operations
= {
772 .open
= partitions_open
,
775 .release
= seq_release
,
780 static struct kobject
*base_probe(dev_t devt
, int *partno
, void *data
)
782 if (request_module("block-major-%d-%d", MAJOR(devt
), MINOR(devt
)) > 0)
783 /* Make old-style 2.4 aliases work */
784 request_module("block-major-%d", MAJOR(devt
));
788 static int __init
genhd_device_init(void)
792 block_class
.dev_kobj
= sysfs_dev_block_kobj
;
793 error
= class_register(&block_class
);
796 bdev_map
= kobj_map_init(base_probe
, &block_class_lock
);
799 register_blkdev(BLOCK_EXT_MAJOR
, "blkext");
801 #ifndef CONFIG_SYSFS_DEPRECATED
802 /* create top-level block dir */
803 block_depr
= kobject_create_and_add("block", NULL
);
808 subsys_initcall(genhd_device_init
);
810 static ssize_t
disk_range_show(struct device
*dev
,
811 struct device_attribute
*attr
, char *buf
)
813 struct gendisk
*disk
= dev_to_disk(dev
);
815 return sprintf(buf
, "%d\n", disk
->minors
);
818 static ssize_t
disk_ext_range_show(struct device
*dev
,
819 struct device_attribute
*attr
, char *buf
)
821 struct gendisk
*disk
= dev_to_disk(dev
);
823 return sprintf(buf
, "%d\n", disk_max_parts(disk
));
826 static ssize_t
disk_removable_show(struct device
*dev
,
827 struct device_attribute
*attr
, char *buf
)
829 struct gendisk
*disk
= dev_to_disk(dev
);
831 return sprintf(buf
, "%d\n",
832 (disk
->flags
& GENHD_FL_REMOVABLE
? 1 : 0));
835 static ssize_t
disk_ro_show(struct device
*dev
,
836 struct device_attribute
*attr
, char *buf
)
838 struct gendisk
*disk
= dev_to_disk(dev
);
840 return sprintf(buf
, "%d\n", get_disk_ro(disk
) ? 1 : 0);
843 static ssize_t
disk_capability_show(struct device
*dev
,
844 struct device_attribute
*attr
, char *buf
)
846 struct gendisk
*disk
= dev_to_disk(dev
);
848 return sprintf(buf
, "%x\n", disk
->flags
);
851 static DEVICE_ATTR(range
, S_IRUGO
, disk_range_show
, NULL
);
852 static DEVICE_ATTR(ext_range
, S_IRUGO
, disk_ext_range_show
, NULL
);
853 static DEVICE_ATTR(removable
, S_IRUGO
, disk_removable_show
, NULL
);
854 static DEVICE_ATTR(ro
, S_IRUGO
, disk_ro_show
, NULL
);
855 static DEVICE_ATTR(size
, S_IRUGO
, part_size_show
, NULL
);
856 static DEVICE_ATTR(capability
, S_IRUGO
, disk_capability_show
, NULL
);
857 static DEVICE_ATTR(stat
, S_IRUGO
, part_stat_show
, NULL
);
858 #ifdef CONFIG_FAIL_MAKE_REQUEST
859 static struct device_attribute dev_attr_fail
=
860 __ATTR(make
-it
-fail
, S_IRUGO
|S_IWUSR
, part_fail_show
, part_fail_store
);
862 #ifdef CONFIG_FAIL_IO_TIMEOUT
863 static struct device_attribute dev_attr_fail_timeout
=
864 __ATTR(io
-timeout
-fail
, S_IRUGO
|S_IWUSR
, part_timeout_show
,
868 static struct attribute
*disk_attrs
[] = {
869 &dev_attr_range
.attr
,
870 &dev_attr_ext_range
.attr
,
871 &dev_attr_removable
.attr
,
874 &dev_attr_capability
.attr
,
876 #ifdef CONFIG_FAIL_MAKE_REQUEST
879 #ifdef CONFIG_FAIL_IO_TIMEOUT
880 &dev_attr_fail_timeout
.attr
,
885 static struct attribute_group disk_attr_group
= {
889 static struct attribute_group
*disk_attr_groups
[] = {
894 static void disk_free_ptbl_rcu_cb(struct rcu_head
*head
)
896 struct disk_part_tbl
*ptbl
=
897 container_of(head
, struct disk_part_tbl
, rcu_head
);
903 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
904 * @disk: disk to replace part_tbl for
905 * @new_ptbl: new part_tbl to install
907 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
908 * original ptbl is freed using RCU callback.
911 * Matching bd_mutx locked.
913 static void disk_replace_part_tbl(struct gendisk
*disk
,
914 struct disk_part_tbl
*new_ptbl
)
916 struct disk_part_tbl
*old_ptbl
= disk
->part_tbl
;
918 rcu_assign_pointer(disk
->part_tbl
, new_ptbl
);
921 rcu_assign_pointer(old_ptbl
->last_lookup
, NULL
);
922 call_rcu(&old_ptbl
->rcu_head
, disk_free_ptbl_rcu_cb
);
927 * disk_expand_part_tbl - expand disk->part_tbl
928 * @disk: disk to expand part_tbl for
929 * @partno: expand such that this partno can fit in
931 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
932 * uses RCU to allow unlocked dereferencing for stats and other stuff.
935 * Matching bd_mutex locked, might sleep.
938 * 0 on success, -errno on failure.
940 int disk_expand_part_tbl(struct gendisk
*disk
, int partno
)
942 struct disk_part_tbl
*old_ptbl
= disk
->part_tbl
;
943 struct disk_part_tbl
*new_ptbl
;
944 int len
= old_ptbl
? old_ptbl
->len
: 0;
945 int target
= partno
+ 1;
949 /* disk_max_parts() is zero during initialization, ignore if so */
950 if (disk_max_parts(disk
) && target
> disk_max_parts(disk
))
956 size
= sizeof(*new_ptbl
) + target
* sizeof(new_ptbl
->part
[0]);
957 new_ptbl
= kzalloc_node(size
, GFP_KERNEL
, disk
->node_id
);
961 INIT_RCU_HEAD(&new_ptbl
->rcu_head
);
962 new_ptbl
->len
= target
;
964 for (i
= 0; i
< len
; i
++)
965 rcu_assign_pointer(new_ptbl
->part
[i
], old_ptbl
->part
[i
]);
967 disk_replace_part_tbl(disk
, new_ptbl
);
971 static void disk_release(struct device
*dev
)
973 struct gendisk
*disk
= dev_to_disk(dev
);
976 disk_replace_part_tbl(disk
, NULL
);
977 free_part_stats(&disk
->part0
);
980 struct class block_class
= {
984 static struct device_type disk_type
= {
986 .groups
= disk_attr_groups
,
987 .release
= disk_release
,
990 #ifdef CONFIG_PROC_FS
992 * aggregate disk stat collector. Uses the same stats that the sysfs
993 * entries do, above, but makes them available through one seq_file.
995 * The output looks suspiciously like /proc/partitions with a bunch of
998 static int diskstats_show(struct seq_file
*seqf
, void *v
)
1000 struct gendisk
*gp
= v
;
1001 struct disk_part_iter piter
;
1002 struct hd_struct
*hd
;
1003 char buf
[BDEVNAME_SIZE
];
1007 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
1008 seq_puts(seqf, "major minor name"
1009 " rio rmerge rsect ruse wio wmerge "
1010 "wsect wuse running use aveq"
1014 disk_part_iter_init(&piter
, gp
, DISK_PITER_INCL_PART0
);
1015 while ((hd
= disk_part_iter_next(&piter
))) {
1016 cpu
= part_stat_lock();
1017 part_round_stats(cpu
, hd
);
1019 seq_printf(seqf
, "%4d %7d %s %lu %lu %llu "
1020 "%u %lu %lu %llu %u %u %u %u\n",
1021 MAJOR(part_devt(hd
)), MINOR(part_devt(hd
)),
1022 disk_name(gp
, hd
->partno
, buf
),
1023 part_stat_read(hd
, ios
[0]),
1024 part_stat_read(hd
, merges
[0]),
1025 (unsigned long long)part_stat_read(hd
, sectors
[0]),
1026 jiffies_to_msecs(part_stat_read(hd
, ticks
[0])),
1027 part_stat_read(hd
, ios
[1]),
1028 part_stat_read(hd
, merges
[1]),
1029 (unsigned long long)part_stat_read(hd
, sectors
[1]),
1030 jiffies_to_msecs(part_stat_read(hd
, ticks
[1])),
1032 jiffies_to_msecs(part_stat_read(hd
, io_ticks
)),
1033 jiffies_to_msecs(part_stat_read(hd
, time_in_queue
))
1036 disk_part_iter_exit(&piter
);
1041 static const struct seq_operations diskstats_op
= {
1042 .start
= disk_seqf_start
,
1043 .next
= disk_seqf_next
,
1044 .stop
= disk_seqf_stop
,
1045 .show
= diskstats_show
1048 static int diskstats_open(struct inode
*inode
, struct file
*file
)
1050 return seq_open(file
, &diskstats_op
);
1053 static const struct file_operations proc_diskstats_operations
= {
1054 .open
= diskstats_open
,
1056 .llseek
= seq_lseek
,
1057 .release
= seq_release
,
1060 static int __init
proc_genhd_init(void)
1062 proc_create("diskstats", 0, NULL
, &proc_diskstats_operations
);
1063 proc_create("partitions", 0, NULL
, &proc_partitions_operations
);
1066 module_init(proc_genhd_init
);
1067 #endif /* CONFIG_PROC_FS */
1069 static void media_change_notify_thread(struct work_struct
*work
)
1071 struct gendisk
*gd
= container_of(work
, struct gendisk
, async_notify
);
1072 char event
[] = "MEDIA_CHANGE=1";
1073 char *envp
[] = { event
, NULL
};
1076 * set enviroment vars to indicate which event this is for
1077 * so that user space will know to go check the media status.
1079 kobject_uevent_env(&disk_to_dev(gd
)->kobj
, KOBJ_CHANGE
, envp
);
1080 put_device(gd
->driverfs_dev
);
1084 void genhd_media_change_notify(struct gendisk
*disk
)
1086 get_device(disk
->driverfs_dev
);
1087 schedule_work(&disk
->async_notify
);
1089 EXPORT_SYMBOL_GPL(genhd_media_change_notify
);
1092 dev_t
blk_lookup_devt(const char *name
, int partno
)
1094 dev_t devt
= MKDEV(0, 0);
1095 struct class_dev_iter iter
;
1098 class_dev_iter_init(&iter
, &block_class
, NULL
, &disk_type
);
1099 while ((dev
= class_dev_iter_next(&iter
))) {
1100 struct gendisk
*disk
= dev_to_disk(dev
);
1101 struct hd_struct
*part
;
1103 if (strcmp(dev_name(dev
), name
))
1106 if (partno
< disk
->minors
) {
1107 /* We need to return the right devno, even
1108 * if the partition doesn't exist yet.
1110 devt
= MKDEV(MAJOR(dev
->devt
),
1111 MINOR(dev
->devt
) + partno
);
1114 part
= disk_get_part(disk
, partno
);
1116 devt
= part_devt(part
);
1117 disk_put_part(part
);
1120 disk_put_part(part
);
1122 class_dev_iter_exit(&iter
);
1125 EXPORT_SYMBOL(blk_lookup_devt
);
1127 struct gendisk
*alloc_disk(int minors
)
1129 return alloc_disk_node(minors
, -1);
1131 EXPORT_SYMBOL(alloc_disk
);
1133 struct gendisk
*alloc_disk_node(int minors
, int node_id
)
1135 struct gendisk
*disk
;
1137 disk
= kmalloc_node(sizeof(struct gendisk
),
1138 GFP_KERNEL
| __GFP_ZERO
, node_id
);
1140 if (!init_part_stats(&disk
->part0
)) {
1144 disk
->node_id
= node_id
;
1145 if (disk_expand_part_tbl(disk
, 0)) {
1146 free_part_stats(&disk
->part0
);
1150 disk
->part_tbl
->part
[0] = &disk
->part0
;
1152 disk
->minors
= minors
;
1153 rand_initialize_disk(disk
);
1154 disk_to_dev(disk
)->class = &block_class
;
1155 disk_to_dev(disk
)->type
= &disk_type
;
1156 device_initialize(disk_to_dev(disk
));
1157 INIT_WORK(&disk
->async_notify
,
1158 media_change_notify_thread
);
1162 EXPORT_SYMBOL(alloc_disk_node
);
1164 struct kobject
*get_disk(struct gendisk
*disk
)
1166 struct module
*owner
;
1167 struct kobject
*kobj
;
1171 owner
= disk
->fops
->owner
;
1172 if (owner
&& !try_module_get(owner
))
1174 kobj
= kobject_get(&disk_to_dev(disk
)->kobj
);
1183 EXPORT_SYMBOL(get_disk
);
1185 void put_disk(struct gendisk
*disk
)
1188 kobject_put(&disk_to_dev(disk
)->kobj
);
1191 EXPORT_SYMBOL(put_disk
);
1193 void set_device_ro(struct block_device
*bdev
, int flag
)
1195 bdev
->bd_part
->policy
= flag
;
1198 EXPORT_SYMBOL(set_device_ro
);
1200 void set_disk_ro(struct gendisk
*disk
, int flag
)
1202 struct disk_part_iter piter
;
1203 struct hd_struct
*part
;
1205 disk_part_iter_init(&piter
, disk
,
1206 DISK_PITER_INCL_EMPTY
| DISK_PITER_INCL_PART0
);
1207 while ((part
= disk_part_iter_next(&piter
)))
1208 part
->policy
= flag
;
1209 disk_part_iter_exit(&piter
);
1212 EXPORT_SYMBOL(set_disk_ro
);
1214 int bdev_read_only(struct block_device
*bdev
)
1218 return bdev
->bd_part
->policy
;
1221 EXPORT_SYMBOL(bdev_read_only
);
1223 int invalidate_partition(struct gendisk
*disk
, int partno
)
1226 struct block_device
*bdev
= bdget_disk(disk
, partno
);
1229 res
= __invalidate_device(bdev
);
1235 EXPORT_SYMBOL(invalidate_partition
);