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;
40 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
42 module_param_named(mangle_devt, blk_mangle_devt, bool, 0444);
43 EXPORT_SYMBOL_GPL(blk_mangle_devt);
47 * disk_get_part - get partition
48 * @disk: disk to look partition from
49 * @partno: partition number
51 * Look for partition @partno from @disk. If found, increment
52 * reference count and return it.
58 * Pointer to the found partition on success, NULL if not found.
60 struct hd_struct *disk_get_part(struct gendisk *disk, int partno)
62 struct hd_struct *part = NULL;
63 struct disk_part_tbl *ptbl;
65 if (unlikely(partno < 0))
70 ptbl = rcu_dereference(disk->part_tbl);
71 if (likely(partno < ptbl->len)) {
72 part = rcu_dereference(ptbl->part[partno]);
74 get_device(part_to_dev(part));
81 EXPORT_SYMBOL_GPL(disk_get_part);
84 * disk_part_iter_init - initialize partition iterator
85 * @piter: iterator to initialize
86 * @disk: disk to iterate over
87 * @flags: DISK_PITER_* flags
89 * Initialize @piter so that it iterates over partitions of @disk.
94 void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
97 struct disk_part_tbl *ptbl;
100 ptbl = rcu_dereference(disk->part_tbl);
105 if (flags & DISK_PITER_REVERSE)
106 piter->idx = ptbl->len - 1;
107 else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0))
112 piter->flags = flags;
116 EXPORT_SYMBOL_GPL(disk_part_iter_init);
119 * disk_part_iter_next - proceed iterator to the next partition and return it
120 * @piter: iterator of interest
122 * Proceed @piter to the next partition and return it.
127 struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
129 struct disk_part_tbl *ptbl;
132 /* put the last partition */
133 disk_put_part(piter->part);
138 ptbl = rcu_dereference(piter->disk->part_tbl);
140 /* determine iteration parameters */
141 if (piter->flags & DISK_PITER_REVERSE) {
143 if (piter->flags & (DISK_PITER_INCL_PART0 |
144 DISK_PITER_INCL_EMPTY_PART0))
153 /* iterate to the next partition */
154 for (; piter->idx != end; piter->idx += inc) {
155 struct hd_struct *part;
157 part = rcu_dereference(ptbl->part[piter->idx]);
160 if (!part->nr_sects &&
161 !(piter->flags & DISK_PITER_INCL_EMPTY) &&
162 !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
166 get_device(part_to_dev(part));
176 EXPORT_SYMBOL_GPL(disk_part_iter_next);
179 * disk_part_iter_exit - finish up partition iteration
180 * @piter: iter of interest
182 * Called when iteration is over. Cleans up @piter.
187 void disk_part_iter_exit(struct disk_part_iter *piter)
189 disk_put_part(piter->part);
192 EXPORT_SYMBOL_GPL(disk_part_iter_exit);
194 static inline int sector_in_part(struct hd_struct *part, sector_t sector)
196 return part->start_sect <= sector &&
197 sector < part->start_sect + part->nr_sects;
201 * disk_map_sector_rcu - map sector to partition
202 * @disk: gendisk of interest
203 * @sector: sector to map
205 * Find out which partition @sector maps to on @disk. This is
206 * primarily used for stats accounting.
209 * RCU read locked. The returned partition pointer is valid only
210 * while preemption is disabled.
213 * Found partition on success, part0 is returned if no partition matches
215 struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
217 struct disk_part_tbl *ptbl;
218 struct hd_struct *part;
221 ptbl = rcu_dereference(disk->part_tbl);
223 part = rcu_dereference(ptbl->last_lookup);
224 if (part && sector_in_part(part, sector))
227 for (i = 1; i < ptbl->len; i++) {
228 part = rcu_dereference(ptbl->part[i]);
230 if (part && sector_in_part(part, sector)) {
231 rcu_assign_pointer(ptbl->last_lookup, part);
237 EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
240 * Can be deleted altogether. Later.
243 static struct blk_major_name {
244 struct blk_major_name *next;
247 } *major_names[BLKDEV_MAJOR_HASH_SIZE];
249 /* index in the above - for now: assume no multimajor ranges */
250 static inline int major_to_index(int major)
252 return major % BLKDEV_MAJOR_HASH_SIZE;
255 #ifdef CONFIG_PROC_FS
256 void blkdev_show(struct seq_file *seqf, off_t offset)
258 struct blk_major_name *dp;
260 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
261 mutex_lock(&block_class_lock);
262 for (dp = major_names[offset]; dp; dp = dp->next)
263 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
264 mutex_unlock(&block_class_lock);
267 #endif /* CONFIG_PROC_FS */
270 * register_blkdev - register a new block device
272 * @major: the requested major device number [1..255]. If @major=0, try to
273 * allocate any unused major number.
274 * @name: the name of the new block device as a zero terminated string
276 * The @name must be unique within the system.
278 * The return value depends on the @major input parameter.
279 * - if a major device number was requested in range [1..255] then the
280 * function returns zero on success, or a negative error code
281 * - if any unused major number was requested with @major=0 parameter
282 * then the return value is the allocated major number in range
283 * [1..255] or a negative error code otherwise
285 int register_blkdev(unsigned int major, const char *name)
287 struct blk_major_name **n, *p;
290 mutex_lock(&block_class_lock);
294 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
295 if (major_names[index] == NULL)
300 printk("register_blkdev: failed to get major for %s\n",
309 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
316 strlcpy(p->name, name, sizeof(p->name));
318 index = major_to_index(major);
320 for (n = &major_names[index]; *n; n = &(*n)->next) {
321 if ((*n)->major == major)
330 printk("register_blkdev: cannot get major %d for %s\n",
335 mutex_unlock(&block_class_lock);
339 EXPORT_SYMBOL(register_blkdev);
341 void unregister_blkdev(unsigned int major, const char *name)
343 struct blk_major_name **n;
344 struct blk_major_name *p = NULL;
345 int index = major_to_index(major);
347 mutex_lock(&block_class_lock);
348 for (n = &major_names[index]; *n; n = &(*n)->next)
349 if ((*n)->major == major)
351 if (!*n || strcmp((*n)->name, name)) {
357 mutex_unlock(&block_class_lock);
361 EXPORT_SYMBOL(unregister_blkdev);
363 static struct kobj_map *bdev_map;
366 * blk_mangle_minor - scatter minor numbers apart
367 * @minor: minor number to mangle
369 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
370 * is enabled. Mangling twice gives the original value.
378 static int blk_mangle_minor(int minor)
380 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
383 if (!blk_mangle_devt)
386 for (i = 0; i < MINORBITS / 2; i++) {
387 int low = minor & (1 << i);
388 int high = minor & (1 << (MINORBITS - 1 - i));
389 int distance = MINORBITS - 1 - 2 * i;
391 minor ^= low | high; /* clear both bits */
392 low <<= distance; /* swap the positions */
394 minor |= low | high; /* and set */
401 * blk_alloc_devt - allocate a dev_t for a partition
402 * @part: partition to allocate dev_t for
403 * @devt: out parameter for resulting dev_t
405 * Allocate a dev_t for block device.
408 * 0 on success, allocated dev_t is returned in *@devt. -errno on
414 int blk_alloc_devt(struct hd_struct *part, dev_t *devt)
416 struct gendisk *disk = part_to_disk(part);
419 /* in consecutive minor range? */
420 if (part->partno < disk->minors) {
421 *devt = MKDEV(disk->major, disk->first_minor + part->partno);
425 /* allocate ext devt */
427 if (!idr_pre_get(&ext_devt_idr, GFP_KERNEL))
429 rc = idr_get_new(&ext_devt_idr, part, &idx);
430 } while (rc == -EAGAIN);
435 if (idx > MAX_EXT_DEVT) {
436 idr_remove(&ext_devt_idr, idx);
440 *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx));
445 * blk_free_devt - free a dev_t
446 * @devt: dev_t to free
448 * Free @devt which was allocated using blk_alloc_devt().
453 void blk_free_devt(dev_t devt)
457 if (devt == MKDEV(0, 0))
460 if (MAJOR(devt) == BLOCK_EXT_MAJOR) {
461 mutex_lock(&ext_devt_mutex);
462 idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
463 mutex_unlock(&ext_devt_mutex);
467 static char *bdevt_str(dev_t devt, char *buf)
469 if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) {
470 char tbuf[BDEVT_SIZE];
471 snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt));
472 snprintf(buf, BDEVT_SIZE, "%-9s", tbuf);
474 snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt));
480 * Register device numbers dev..(dev+range-1)
481 * range must be nonzero
482 * The hash chain is sorted on range, so that subranges can override.
484 void blk_register_region(dev_t devt, unsigned long range, struct module *module,
485 struct kobject *(*probe)(dev_t, int *, void *),
486 int (*lock)(dev_t, void *), void *data)
488 kobj_map(bdev_map, devt, range, module, probe, lock, data);
491 EXPORT_SYMBOL(blk_register_region);
493 void blk_unregister_region(dev_t devt, unsigned long range)
495 kobj_unmap(bdev_map, devt, range);
498 EXPORT_SYMBOL(blk_unregister_region);
500 static struct kobject *exact_match(dev_t devt, int *partno, void *data)
502 struct gendisk *p = data;
504 return &disk_to_dev(p)->kobj;
507 static int exact_lock(dev_t devt, void *data)
509 struct gendisk *p = data;
516 static int __read_mostly no_partition_scan;
518 static int __init no_partition_scan_setup(char *str)
520 no_partition_scan = 1;
521 printk(KERN_INFO "genhd: omit partition scan.\n");
526 __setup("no_partition_scan", no_partition_scan_setup);
529 * add_disk - add partitioning information to kernel list
530 * @disk: per-device partitioning information
532 * This function registers the partitioning information in @disk
535 * FIXME: error handling
537 void add_disk(struct gendisk *disk)
539 struct backing_dev_info *bdi;
543 /* minors == 0 indicates to use ext devt from part0 and should
544 * be accompanied with EXT_DEVT flag. Make sure all
545 * parameters make sense.
547 WARN_ON(disk->minors && !(disk->major || disk->first_minor));
548 WARN_ON(!disk->minors && !(disk->flags & GENHD_FL_EXT_DEVT));
550 disk->flags |= GENHD_FL_UP;
552 if (no_partition_scan)
553 disk->flags |= GENHD_FL_NO_PARTITION_SCAN;
555 retval = blk_alloc_devt(&disk->part0, &devt);
560 disk_to_dev(disk)->devt = devt;
562 /* ->major and ->first_minor aren't supposed to be
563 * dereferenced from here on, but set them just in case.
565 disk->major = MAJOR(devt);
566 disk->first_minor = MINOR(devt);
568 blk_register_region(disk_devt(disk), disk->minors, NULL,
569 exact_match, exact_lock, disk);
571 blk_register_queue(disk);
573 bdi = &disk->queue->backing_dev_info;
574 bdi_register_dev(bdi, disk_devt(disk));
575 retval = sysfs_create_link(&disk_to_dev(disk)->kobj, &bdi->dev->kobj,
580 EXPORT_SYMBOL(add_disk);
581 EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
583 void unlink_gendisk(struct gendisk *disk)
585 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
586 bdi_unregister(&disk->queue->backing_dev_info);
587 blk_unregister_queue(disk);
588 blk_unregister_region(disk_devt(disk), disk->minors);
592 * get_gendisk - get partitioning information for a given device
593 * @devt: device to get partitioning information for
594 * @partno: returned partition index
596 * This function gets the structure containing partitioning
597 * information for the given device @devt.
599 struct gendisk *get_gendisk(dev_t devt, int *partno)
601 struct gendisk *disk = NULL;
603 if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
604 struct kobject *kobj;
606 kobj = kobj_lookup(bdev_map, devt, partno);
608 disk = dev_to_disk(kobj_to_dev(kobj));
610 struct hd_struct *part;
612 mutex_lock(&ext_devt_mutex);
613 part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
614 if (part && get_disk(part_to_disk(part))) {
615 *partno = part->partno;
616 disk = part_to_disk(part);
618 mutex_unlock(&ext_devt_mutex);
625 * bdget_disk - do bdget() by gendisk and partition number
626 * @disk: gendisk of interest
627 * @partno: partition number
629 * Find partition @partno from @disk, do bdget() on it.
635 * Resulting block_device on success, NULL on failure.
637 struct block_device *bdget_disk(struct gendisk *disk, int partno)
639 struct hd_struct *part;
640 struct block_device *bdev = NULL;
642 part = disk_get_part(disk, partno);
644 bdev = bdget(part_devt(part));
649 EXPORT_SYMBOL(bdget_disk);
652 * print a full list of all partitions - intended for places where the root
653 * filesystem can't be mounted and thus to give the victim some idea of what
656 void __init printk_all_partitions(void)
658 struct class_dev_iter iter;
661 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
662 while ((dev = class_dev_iter_next(&iter))) {
663 struct gendisk *disk = dev_to_disk(dev);
664 struct disk_part_iter piter;
665 struct hd_struct *part;
666 char name_buf[BDEVNAME_SIZE];
667 char devt_buf[BDEVT_SIZE];
670 * Don't show empty devices or things that have been
673 if (get_capacity(disk) == 0 ||
674 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
678 * Note, unlike /proc/partitions, I am showing the
679 * numbers in hex - the same format as the root=
682 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
683 while ((part = disk_part_iter_next(&piter))) {
684 bool is_part0 = part == &disk->part0;
686 printk("%s%s %10llu %s", is_part0 ? "" : " ",
687 bdevt_str(part_devt(part), devt_buf),
688 (unsigned long long)part->nr_sects >> 1,
689 disk_name(disk, part->partno, name_buf));
691 if (disk->driverfs_dev != NULL &&
692 disk->driverfs_dev->driver != NULL)
693 printk(" driver: %s\n",
694 disk->driverfs_dev->driver->name);
696 printk(" (driver?)\n");
700 disk_part_iter_exit(&piter);
702 class_dev_iter_exit(&iter);
705 #ifdef CONFIG_PROC_FS
707 static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
710 struct class_dev_iter *iter;
713 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
715 return ERR_PTR(-ENOMEM);
717 seqf->private = iter;
718 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
720 dev = class_dev_iter_next(iter);
725 return dev_to_disk(dev);
728 static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
733 dev = class_dev_iter_next(seqf->private);
735 return dev_to_disk(dev);
740 static void disk_seqf_stop(struct seq_file *seqf, void *v)
742 struct class_dev_iter *iter = seqf->private;
744 /* stop is called even after start failed :-( */
746 class_dev_iter_exit(iter);
751 static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
755 p = disk_seqf_start(seqf, pos);
756 if (!IS_ERR(p) && p && !*pos)
757 seq_puts(seqf, "major minor #blocks name\n\n");
761 static int show_partition(struct seq_file *seqf, void *v)
763 struct gendisk *sgp = v;
764 struct disk_part_iter piter;
765 struct hd_struct *part;
766 char buf[BDEVNAME_SIZE];
768 /* Don't show non-partitionable removeable devices or empty devices */
769 if (!get_capacity(sgp) || (!disk_partitionable(sgp) &&
770 (sgp->flags & GENHD_FL_REMOVABLE)))
772 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
775 /* show the full disk and all non-0 size partitions of it */
776 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
777 while ((part = disk_part_iter_next(&piter)))
778 seq_printf(seqf, "%4d %7d %10llu %s\n",
779 MAJOR(part_devt(part)), MINOR(part_devt(part)),
780 (unsigned long long)part->nr_sects >> 1,
781 disk_name(sgp, part->partno, buf));
782 disk_part_iter_exit(&piter);
787 static const struct seq_operations partitions_op = {
788 .start = show_partition_start,
789 .next = disk_seqf_next,
790 .stop = disk_seqf_stop,
791 .show = show_partition
794 static int partitions_open(struct inode *inode, struct file *file)
796 return seq_open(file, &partitions_op);
799 static const struct file_operations proc_partitions_operations = {
800 .open = partitions_open,
803 .release = seq_release,
808 static struct kobject *base_probe(dev_t devt, int *partno, void *data)
810 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
811 /* Make old-style 2.4 aliases work */
812 request_module("block-major-%d", MAJOR(devt));
816 static int __init genhd_device_init(void)
820 block_class.dev_kobj = sysfs_dev_block_kobj;
821 error = class_register(&block_class);
824 bdev_map = kobj_map_init(base_probe, &block_class_lock);
827 register_blkdev(BLOCK_EXT_MAJOR, "blkext");
829 #ifndef CONFIG_SYSFS_DEPRECATED
830 /* create top-level block dir */
831 block_depr = kobject_create_and_add("block", NULL);
836 subsys_initcall(genhd_device_init);
838 static ssize_t disk_range_show(struct device *dev,
839 struct device_attribute *attr, char *buf)
841 struct gendisk *disk = dev_to_disk(dev);
843 return sprintf(buf, "%d\n",
844 (disk->flags & GENHD_FL_NO_PARTITION_SCAN ? 0 : disk->minors));
847 static ssize_t disk_range_store(struct device *dev,
848 struct device_attribute *attr,
849 const char *buf, size_t count)
851 struct gendisk *disk = dev_to_disk(dev);
854 if (count > 0 && sscanf(buf, "%d", &i) > 0) {
856 disk->flags |= GENHD_FL_NO_PARTITION_SCAN;
857 else if (i <= disk->minors)
858 disk->flags &= ~GENHD_FL_NO_PARTITION_SCAN;
866 static ssize_t disk_ext_range_show(struct device *dev,
867 struct device_attribute *attr, char *buf)
869 struct gendisk *disk = dev_to_disk(dev);
871 return sprintf(buf, "%d\n", disk_max_parts(disk));
874 static ssize_t disk_removable_show(struct device *dev,
875 struct device_attribute *attr, char *buf)
877 struct gendisk *disk = dev_to_disk(dev);
879 return sprintf(buf, "%d\n",
880 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
883 static ssize_t disk_ro_show(struct device *dev,
884 struct device_attribute *attr, char *buf)
886 struct gendisk *disk = dev_to_disk(dev);
888 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
891 static ssize_t disk_capability_show(struct device *dev,
892 struct device_attribute *attr, char *buf)
894 struct gendisk *disk = dev_to_disk(dev);
896 return sprintf(buf, "%x\n", disk->flags);
899 static DEVICE_ATTR(range, S_IRUGO|S_IWUSR, disk_range_show, disk_range_store);
900 static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
901 static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
902 static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
903 static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
904 static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
905 static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
906 #ifdef CONFIG_FAIL_MAKE_REQUEST
907 static struct device_attribute dev_attr_fail =
908 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
910 #ifdef CONFIG_FAIL_IO_TIMEOUT
911 static struct device_attribute dev_attr_fail_timeout =
912 __ATTR(io-timeout-fail, S_IRUGO|S_IWUSR, part_timeout_show,
916 static struct attribute *disk_attrs[] = {
917 &dev_attr_range.attr,
918 &dev_attr_ext_range.attr,
919 &dev_attr_removable.attr,
922 &dev_attr_capability.attr,
924 #ifdef CONFIG_FAIL_MAKE_REQUEST
927 #ifdef CONFIG_FAIL_IO_TIMEOUT
928 &dev_attr_fail_timeout.attr,
933 static struct attribute_group disk_attr_group = {
937 static struct attribute_group *disk_attr_groups[] = {
942 static void disk_free_ptbl_rcu_cb(struct rcu_head *head)
944 struct disk_part_tbl *ptbl =
945 container_of(head, struct disk_part_tbl, rcu_head);
951 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
952 * @disk: disk to replace part_tbl for
953 * @new_ptbl: new part_tbl to install
955 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
956 * original ptbl is freed using RCU callback.
959 * Matching bd_mutx locked.
961 static void disk_replace_part_tbl(struct gendisk *disk,
962 struct disk_part_tbl *new_ptbl)
964 struct disk_part_tbl *old_ptbl = disk->part_tbl;
966 rcu_assign_pointer(disk->part_tbl, new_ptbl);
969 rcu_assign_pointer(old_ptbl->last_lookup, NULL);
970 call_rcu(&old_ptbl->rcu_head, disk_free_ptbl_rcu_cb);
975 * disk_expand_part_tbl - expand disk->part_tbl
976 * @disk: disk to expand part_tbl for
977 * @partno: expand such that this partno can fit in
979 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
980 * uses RCU to allow unlocked dereferencing for stats and other stuff.
983 * Matching bd_mutex locked, might sleep.
986 * 0 on success, -errno on failure.
988 int disk_expand_part_tbl(struct gendisk *disk, int partno)
990 struct disk_part_tbl *old_ptbl = disk->part_tbl;
991 struct disk_part_tbl *new_ptbl;
992 int len = old_ptbl ? old_ptbl->len : 0;
993 int target = partno + 1;
997 /* disk_max_parts() is zero during initialization, ignore if so */
998 if (disk_max_parts(disk) && target > disk_max_parts(disk))
1004 size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]);
1005 new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id);
1009 INIT_RCU_HEAD(&new_ptbl->rcu_head);
1010 new_ptbl->len = target;
1012 for (i = 0; i < len; i++)
1013 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
1015 disk_replace_part_tbl(disk, new_ptbl);
1019 static void disk_release(struct device *dev)
1021 struct gendisk *disk = dev_to_disk(dev);
1023 kfree(disk->random);
1024 disk_replace_part_tbl(disk, NULL);
1025 free_part_stats(&disk->part0);
1028 struct class block_class = {
1032 static char *block_nodename(struct device *dev)
1034 struct gendisk *disk = dev_to_disk(dev);
1037 return disk->nodename(disk);
1041 static struct device_type disk_type = {
1043 .groups = disk_attr_groups,
1044 .release = disk_release,
1045 .nodename = block_nodename,
1048 #ifdef CONFIG_PROC_FS
1050 * aggregate disk stat collector. Uses the same stats that the sysfs
1051 * entries do, above, but makes them available through one seq_file.
1053 * The output looks suspiciously like /proc/partitions with a bunch of
1056 static int diskstats_show(struct seq_file *seqf, void *v)
1058 struct gendisk *gp = v;
1059 struct disk_part_iter piter;
1060 struct hd_struct *hd;
1061 char buf[BDEVNAME_SIZE];
1065 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
1066 seq_puts(seqf, "major minor name"
1067 " rio rmerge rsect ruse wio wmerge "
1068 "wsect wuse running use aveq"
1072 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0);
1073 while ((hd = disk_part_iter_next(&piter))) {
1074 cpu = part_stat_lock();
1075 part_round_stats(cpu, hd);
1077 seq_printf(seqf, "%4d %7d %s %lu %lu %llu "
1078 "%u %lu %lu %llu %u %u %u %u\n",
1079 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
1080 disk_name(gp, hd->partno, buf),
1081 part_stat_read(hd, ios[0]),
1082 part_stat_read(hd, merges[0]),
1083 (unsigned long long)part_stat_read(hd, sectors[0]),
1084 jiffies_to_msecs(part_stat_read(hd, ticks[0])),
1085 part_stat_read(hd, ios[1]),
1086 part_stat_read(hd, merges[1]),
1087 (unsigned long long)part_stat_read(hd, sectors[1]),
1088 jiffies_to_msecs(part_stat_read(hd, ticks[1])),
1090 jiffies_to_msecs(part_stat_read(hd, io_ticks)),
1091 jiffies_to_msecs(part_stat_read(hd, time_in_queue))
1094 disk_part_iter_exit(&piter);
1099 static const struct seq_operations diskstats_op = {
1100 .start = disk_seqf_start,
1101 .next = disk_seqf_next,
1102 .stop = disk_seqf_stop,
1103 .show = diskstats_show
1106 static int diskstats_open(struct inode *inode, struct file *file)
1108 return seq_open(file, &diskstats_op);
1111 static const struct file_operations proc_diskstats_operations = {
1112 .open = diskstats_open,
1114 .llseek = seq_lseek,
1115 .release = seq_release,
1118 static int __init proc_genhd_init(void)
1120 proc_create("diskstats", 0, NULL, &proc_diskstats_operations);
1121 proc_create("partitions", 0, NULL, &proc_partitions_operations);
1124 module_init(proc_genhd_init);
1125 #endif /* CONFIG_PROC_FS */
1127 static void media_change_notify_thread(struct work_struct *work)
1129 struct gendisk *gd = container_of(work, struct gendisk, async_notify);
1130 char event[] = "MEDIA_CHANGE=1";
1131 char *envp[] = { event, NULL };
1134 * set enviroment vars to indicate which event this is for
1135 * so that user space will know to go check the media status.
1137 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
1138 put_device(gd->driverfs_dev);
1142 void genhd_media_change_notify(struct gendisk *disk)
1144 get_device(disk->driverfs_dev);
1145 schedule_work(&disk->async_notify);
1147 EXPORT_SYMBOL_GPL(genhd_media_change_notify);
1150 dev_t blk_lookup_devt(const char *name, int partno)
1152 dev_t devt = MKDEV(0, 0);
1153 struct class_dev_iter iter;
1156 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1157 while ((dev = class_dev_iter_next(&iter))) {
1158 struct gendisk *disk = dev_to_disk(dev);
1159 struct hd_struct *part;
1161 if (strcmp(dev_name(dev), name))
1164 if (partno < disk->minors) {
1165 /* We need to return the right devno, even
1166 * if the partition doesn't exist yet.
1168 devt = MKDEV(MAJOR(dev->devt),
1169 MINOR(dev->devt) + partno);
1172 part = disk_get_part(disk, partno);
1174 devt = part_devt(part);
1175 disk_put_part(part);
1178 disk_put_part(part);
1180 class_dev_iter_exit(&iter);
1183 EXPORT_SYMBOL(blk_lookup_devt);
1185 struct gendisk *alloc_disk(int minors)
1187 return alloc_disk_node(minors, -1);
1189 EXPORT_SYMBOL(alloc_disk);
1191 struct gendisk *alloc_disk_node(int minors, int node_id)
1193 struct gendisk *disk;
1195 disk = kmalloc_node(sizeof(struct gendisk),
1196 GFP_KERNEL | __GFP_ZERO, node_id);
1198 if (!init_part_stats(&disk->part0)) {
1202 disk->node_id = node_id;
1203 if (disk_expand_part_tbl(disk, 0)) {
1204 free_part_stats(&disk->part0);
1208 disk->part_tbl->part[0] = &disk->part0;
1210 disk->minors = minors;
1211 rand_initialize_disk(disk);
1212 disk_to_dev(disk)->class = &block_class;
1213 disk_to_dev(disk)->type = &disk_type;
1214 device_initialize(disk_to_dev(disk));
1215 INIT_WORK(&disk->async_notify,
1216 media_change_notify_thread);
1220 EXPORT_SYMBOL(alloc_disk_node);
1222 struct kobject *get_disk(struct gendisk *disk)
1224 struct module *owner;
1225 struct kobject *kobj;
1229 owner = disk->fops->owner;
1230 if (owner && !try_module_get(owner))
1232 kobj = kobject_get(&disk_to_dev(disk)->kobj);
1241 EXPORT_SYMBOL(get_disk);
1243 void put_disk(struct gendisk *disk)
1246 kobject_put(&disk_to_dev(disk)->kobj);
1249 EXPORT_SYMBOL(put_disk);
1251 void set_device_ro(struct block_device *bdev, int flag)
1253 bdev->bd_part->policy = flag;
1256 EXPORT_SYMBOL(set_device_ro);
1258 void set_disk_ro(struct gendisk *disk, int flag)
1260 struct disk_part_iter piter;
1261 struct hd_struct *part;
1263 disk_part_iter_init(&piter, disk,
1264 DISK_PITER_INCL_EMPTY | DISK_PITER_INCL_PART0);
1265 while ((part = disk_part_iter_next(&piter)))
1266 part->policy = flag;
1267 disk_part_iter_exit(&piter);
1270 EXPORT_SYMBOL(set_disk_ro);
1272 int bdev_read_only(struct block_device *bdev)
1276 return bdev->bd_part->policy;
1279 EXPORT_SYMBOL(bdev_read_only);
1281 int invalidate_partition(struct gendisk *disk, int partno)
1284 struct block_device *bdev = bdget_disk(disk, partno);
1287 res = __invalidate_device(bdev);
1293 EXPORT_SYMBOL(invalidate_partition);