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 = NULL;
56 struct disk_part_tbl *ptbl;
58 if (unlikely(partno < 0))
63 ptbl = rcu_dereference(disk->part_tbl);
64 if (likely(partno < ptbl->len)) {
65 part = rcu_dereference(ptbl->part[partno]);
67 get_device(part_to_dev(part));
74 EXPORT_SYMBOL_GPL(disk_get_part);
77 * disk_part_iter_init - initialize partition iterator
78 * @piter: iterator to initialize
79 * @disk: disk to iterate over
80 * @flags: DISK_PITER_* flags
82 * Initialize @piter so that it iterates over partitions of @disk.
87 void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk,
90 struct disk_part_tbl *ptbl;
93 ptbl = rcu_dereference(disk->part_tbl);
98 if (flags & DISK_PITER_REVERSE)
99 piter->idx = ptbl->len - 1;
100 else if (flags & DISK_PITER_INCL_PART0)
105 piter->flags = flags;
109 EXPORT_SYMBOL_GPL(disk_part_iter_init);
112 * disk_part_iter_next - proceed iterator to the next partition and return it
113 * @piter: iterator of interest
115 * Proceed @piter to the next partition and return it.
120 struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
122 struct disk_part_tbl *ptbl;
125 /* put the last partition */
126 disk_put_part(piter->part);
131 ptbl = rcu_dereference(piter->disk->part_tbl);
133 /* determine iteration parameters */
134 if (piter->flags & DISK_PITER_REVERSE) {
136 if (piter->flags & DISK_PITER_INCL_PART0)
145 /* iterate to the next partition */
146 for (; piter->idx != end; piter->idx += inc) {
147 struct hd_struct *part;
149 part = rcu_dereference(ptbl->part[piter->idx]);
152 if (!(piter->flags & DISK_PITER_INCL_EMPTY) && !part->nr_sects)
155 get_device(part_to_dev(part));
165 EXPORT_SYMBOL_GPL(disk_part_iter_next);
168 * disk_part_iter_exit - finish up partition iteration
169 * @piter: iter of interest
171 * Called when iteration is over. Cleans up @piter.
176 void disk_part_iter_exit(struct disk_part_iter *piter)
178 disk_put_part(piter->part);
181 EXPORT_SYMBOL_GPL(disk_part_iter_exit);
184 * disk_map_sector_rcu - map sector to partition
185 * @disk: gendisk of interest
186 * @sector: sector to map
188 * Find out which partition @sector maps to on @disk. This is
189 * primarily used for stats accounting.
192 * RCU read locked. The returned partition pointer is valid only
193 * while preemption is disabled.
196 * Found partition on success, part0 is returned if no partition matches
198 struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector)
200 struct disk_part_tbl *ptbl;
203 ptbl = rcu_dereference(disk->part_tbl);
205 for (i = 1; i < ptbl->len; i++) {
206 struct hd_struct *part = rcu_dereference(ptbl->part[i]);
208 if (part && part->start_sect <= sector &&
209 sector < part->start_sect + part->nr_sects)
214 EXPORT_SYMBOL_GPL(disk_map_sector_rcu);
217 * Can be deleted altogether. Later.
220 static struct blk_major_name {
221 struct blk_major_name *next;
224 } *major_names[BLKDEV_MAJOR_HASH_SIZE];
226 /* index in the above - for now: assume no multimajor ranges */
227 static inline int major_to_index(int major)
229 return major % BLKDEV_MAJOR_HASH_SIZE;
232 #ifdef CONFIG_PROC_FS
233 void blkdev_show(struct seq_file *seqf, off_t offset)
235 struct blk_major_name *dp;
237 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
238 mutex_lock(&block_class_lock);
239 for (dp = major_names[offset]; dp; dp = dp->next)
240 seq_printf(seqf, "%3d %s\n", dp->major, dp->name);
241 mutex_unlock(&block_class_lock);
244 #endif /* CONFIG_PROC_FS */
246 int register_blkdev(unsigned int major, const char *name)
248 struct blk_major_name **n, *p;
251 mutex_lock(&block_class_lock);
255 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
256 if (major_names[index] == NULL)
261 printk("register_blkdev: failed to get major for %s\n",
270 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
277 strlcpy(p->name, name, sizeof(p->name));
279 index = major_to_index(major);
281 for (n = &major_names[index]; *n; n = &(*n)->next) {
282 if ((*n)->major == major)
291 printk("register_blkdev: cannot get major %d for %s\n",
296 mutex_unlock(&block_class_lock);
300 EXPORT_SYMBOL(register_blkdev);
302 void unregister_blkdev(unsigned int major, const char *name)
304 struct blk_major_name **n;
305 struct blk_major_name *p = NULL;
306 int index = major_to_index(major);
308 mutex_lock(&block_class_lock);
309 for (n = &major_names[index]; *n; n = &(*n)->next)
310 if ((*n)->major == major)
312 if (!*n || strcmp((*n)->name, name)) {
318 mutex_unlock(&block_class_lock);
322 EXPORT_SYMBOL(unregister_blkdev);
324 static struct kobj_map *bdev_map;
327 * blk_mangle_minor - scatter minor numbers apart
328 * @minor: minor number to mangle
330 * Scatter consecutively allocated @minor number apart if MANGLE_DEVT
331 * is enabled. Mangling twice gives the original value.
339 static int blk_mangle_minor(int minor)
341 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
344 for (i = 0; i < MINORBITS / 2; i++) {
345 int low = minor & (1 << i);
346 int high = minor & (1 << (MINORBITS - 1 - i));
347 int distance = MINORBITS - 1 - 2 * i;
349 minor ^= low | high; /* clear both bits */
350 low <<= distance; /* swap the positions */
352 minor |= low | high; /* and set */
359 * blk_alloc_devt - allocate a dev_t for a partition
360 * @part: partition to allocate dev_t for
361 * @gfp_mask: memory allocation flag
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 void add_disk(struct gendisk *disk)
484 struct backing_dev_info *bdi;
487 disk->flags |= GENHD_FL_UP;
488 disk_to_dev(disk)->devt = MKDEV(disk->major, disk->first_minor);
489 blk_register_region(disk_devt(disk), disk->minors, NULL,
490 exact_match, exact_lock, disk);
492 blk_register_queue(disk);
494 bdi = &disk->queue->backing_dev_info;
495 bdi_register_dev(bdi, disk_devt(disk));
496 retval = sysfs_create_link(&disk_to_dev(disk)->kobj, &bdi->dev->kobj,
501 EXPORT_SYMBOL(add_disk);
502 EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
504 void unlink_gendisk(struct gendisk *disk)
506 sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
507 bdi_unregister(&disk->queue->backing_dev_info);
508 blk_unregister_queue(disk);
509 blk_unregister_region(disk_devt(disk), disk->minors);
513 * get_gendisk - get partitioning information for a given device
514 * @devt: device to get partitioning information for
515 * @part: returned partition index
517 * This function gets the structure containing partitioning
518 * information for the given device @devt.
520 struct gendisk *get_gendisk(dev_t devt, int *partno)
522 struct gendisk *disk = NULL;
524 if (MAJOR(devt) != BLOCK_EXT_MAJOR) {
525 struct kobject *kobj;
527 kobj = kobj_lookup(bdev_map, devt, partno);
529 disk = dev_to_disk(kobj_to_dev(kobj));
531 struct hd_struct *part;
533 mutex_lock(&ext_devt_mutex);
534 part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt)));
535 if (part && get_disk(part_to_disk(part))) {
536 *partno = part->partno;
537 disk = part_to_disk(part);
539 mutex_unlock(&ext_devt_mutex);
546 * bdget_disk - do bdget() by gendisk and partition number
547 * @disk: gendisk of interest
548 * @partno: partition number
550 * Find partition @partno from @disk, do bdget() on it.
556 * Resulting block_device on success, NULL on failure.
558 extern struct block_device *bdget_disk(struct gendisk *disk, int partno)
560 struct hd_struct *part;
561 struct block_device *bdev = NULL;
563 part = disk_get_part(disk, partno);
564 if (part && (part->nr_sects || partno == 0))
565 bdev = bdget(part_devt(part));
570 EXPORT_SYMBOL(bdget_disk);
573 * print a full list of all partitions - intended for places where the root
574 * filesystem can't be mounted and thus to give the victim some idea of what
577 void __init printk_all_partitions(void)
579 struct class_dev_iter iter;
582 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
583 while ((dev = class_dev_iter_next(&iter))) {
584 struct gendisk *disk = dev_to_disk(dev);
585 struct disk_part_iter piter;
586 struct hd_struct *part;
587 char name_buf[BDEVNAME_SIZE];
588 char devt_buf[BDEVT_SIZE];
591 * Don't show empty devices or things that have been
594 if (get_capacity(disk) == 0 ||
595 (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
599 * Note, unlike /proc/partitions, I am showing the
600 * numbers in hex - the same format as the root=
603 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
604 while ((part = disk_part_iter_next(&piter))) {
605 bool is_part0 = part == &disk->part0;
607 printk("%s%s %10llu %s", is_part0 ? "" : " ",
608 bdevt_str(part_devt(part), devt_buf),
609 (unsigned long long)part->nr_sects >> 1,
610 disk_name(disk, part->partno, name_buf));
612 if (disk->driverfs_dev != NULL &&
613 disk->driverfs_dev->driver != NULL)
614 printk(" driver: %s\n",
615 disk->driverfs_dev->driver->name);
617 printk(" (driver?)\n");
621 disk_part_iter_exit(&piter);
623 class_dev_iter_exit(&iter);
626 #ifdef CONFIG_PROC_FS
628 static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos)
631 struct class_dev_iter *iter;
634 iter = kmalloc(GFP_KERNEL, sizeof(*iter));
636 return ERR_PTR(-ENOMEM);
638 seqf->private = iter;
639 class_dev_iter_init(iter, &block_class, NULL, &disk_type);
641 dev = class_dev_iter_next(iter);
646 return dev_to_disk(dev);
649 static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos)
654 dev = class_dev_iter_next(seqf->private);
656 return dev_to_disk(dev);
661 static void disk_seqf_stop(struct seq_file *seqf, void *v)
663 struct class_dev_iter *iter = seqf->private;
665 /* stop is called even after start failed :-( */
667 class_dev_iter_exit(iter);
672 static void *show_partition_start(struct seq_file *seqf, loff_t *pos)
676 p = disk_seqf_start(seqf, pos);
678 seq_puts(seqf, "major minor #blocks name\n\n");
682 static int show_partition(struct seq_file *seqf, void *v)
684 struct gendisk *sgp = v;
685 struct disk_part_iter piter;
686 struct hd_struct *part;
687 char buf[BDEVNAME_SIZE];
689 /* Don't show non-partitionable removeable devices or empty devices */
690 if (!get_capacity(sgp) || (!disk_partitionable(sgp) &&
691 (sgp->flags & GENHD_FL_REMOVABLE)))
693 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
696 /* show the full disk and all non-0 size partitions of it */
697 disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0);
698 while ((part = disk_part_iter_next(&piter)))
699 seq_printf(seqf, "%4d %7d %10llu %s\n",
700 MAJOR(part_devt(part)), MINOR(part_devt(part)),
701 (unsigned long long)part->nr_sects >> 1,
702 disk_name(sgp, part->partno, buf));
703 disk_part_iter_exit(&piter);
708 const struct seq_operations partitions_op = {
709 .start = show_partition_start,
710 .next = disk_seqf_next,
711 .stop = disk_seqf_stop,
712 .show = show_partition
717 static struct kobject *base_probe(dev_t devt, int *partno, void *data)
719 if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0)
720 /* Make old-style 2.4 aliases work */
721 request_module("block-major-%d", MAJOR(devt));
725 static int __init genhd_device_init(void)
729 block_class.dev_kobj = sysfs_dev_block_kobj;
730 error = class_register(&block_class);
733 bdev_map = kobj_map_init(base_probe, &block_class_lock);
736 #ifndef CONFIG_SYSFS_DEPRECATED
737 /* create top-level block dir */
738 block_depr = kobject_create_and_add("block", NULL);
743 subsys_initcall(genhd_device_init);
745 static ssize_t disk_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->minors);
753 static ssize_t disk_ext_range_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", disk_max_parts(disk));
761 static ssize_t disk_removable_show(struct device *dev,
762 struct device_attribute *attr, char *buf)
764 struct gendisk *disk = dev_to_disk(dev);
766 return sprintf(buf, "%d\n",
767 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
770 static ssize_t disk_ro_show(struct device *dev,
771 struct device_attribute *attr, char *buf)
773 struct gendisk *disk = dev_to_disk(dev);
775 return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0);
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 DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL);
787 static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL);
788 static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL);
789 static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL);
790 static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
791 static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL);
792 static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
793 #ifdef CONFIG_FAIL_MAKE_REQUEST
794 static struct device_attribute dev_attr_fail =
795 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
798 static struct attribute *disk_attrs[] = {
799 &dev_attr_range.attr,
800 &dev_attr_ext_range.attr,
801 &dev_attr_removable.attr,
804 &dev_attr_capability.attr,
806 #ifdef CONFIG_FAIL_MAKE_REQUEST
812 static struct attribute_group disk_attr_group = {
816 static struct attribute_group *disk_attr_groups[] = {
821 static void disk_free_ptbl_rcu_cb(struct rcu_head *head)
823 struct disk_part_tbl *ptbl =
824 container_of(head, struct disk_part_tbl, rcu_head);
830 * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way
831 * @disk: disk to replace part_tbl for
832 * @new_ptbl: new part_tbl to install
834 * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The
835 * original ptbl is freed using RCU callback.
838 * Matching bd_mutx locked.
840 static void disk_replace_part_tbl(struct gendisk *disk,
841 struct disk_part_tbl *new_ptbl)
843 struct disk_part_tbl *old_ptbl = disk->part_tbl;
845 rcu_assign_pointer(disk->part_tbl, new_ptbl);
847 call_rcu(&old_ptbl->rcu_head, disk_free_ptbl_rcu_cb);
851 * disk_expand_part_tbl - expand disk->part_tbl
852 * @disk: disk to expand part_tbl for
853 * @partno: expand such that this partno can fit in
855 * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl
856 * uses RCU to allow unlocked dereferencing for stats and other stuff.
859 * Matching bd_mutex locked, might sleep.
862 * 0 on success, -errno on failure.
864 int disk_expand_part_tbl(struct gendisk *disk, int partno)
866 struct disk_part_tbl *old_ptbl = disk->part_tbl;
867 struct disk_part_tbl *new_ptbl;
868 int len = old_ptbl ? old_ptbl->len : 0;
869 int target = partno + 1;
873 /* disk_max_parts() is zero during initialization, ignore if so */
874 if (disk_max_parts(disk) && target > disk_max_parts(disk))
880 size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]);
881 new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id);
885 INIT_RCU_HEAD(&new_ptbl->rcu_head);
886 new_ptbl->len = target;
888 for (i = 0; i < len; i++)
889 rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]);
891 disk_replace_part_tbl(disk, new_ptbl);
895 static void disk_release(struct device *dev)
897 struct gendisk *disk = dev_to_disk(dev);
900 disk_replace_part_tbl(disk, NULL);
901 free_part_stats(&disk->part0);
904 struct class block_class = {
908 static struct device_type disk_type = {
910 .groups = disk_attr_groups,
911 .release = disk_release,
914 #ifdef CONFIG_PROC_FS
916 * aggregate disk stat collector. Uses the same stats that the sysfs
917 * entries do, above, but makes them available through one seq_file.
919 * The output looks suspiciously like /proc/partitions with a bunch of
922 static int diskstats_show(struct seq_file *seqf, void *v)
924 struct gendisk *gp = v;
925 struct disk_part_iter piter;
926 struct hd_struct *hd;
927 char buf[BDEVNAME_SIZE];
931 if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
932 seq_puts(seqf, "major minor name"
933 " rio rmerge rsect ruse wio wmerge "
934 "wsect wuse running use aveq"
938 disk_part_iter_init(&piter, gp, DISK_PITER_INCL_PART0);
939 while ((hd = disk_part_iter_next(&piter))) {
940 cpu = part_stat_lock();
941 part_round_stats(cpu, hd);
943 seq_printf(seqf, "%4d %7d %s %lu %lu %llu "
944 "%u %lu %lu %llu %u %u %u %u\n",
945 MAJOR(part_devt(hd)), MINOR(part_devt(hd)),
946 disk_name(gp, hd->partno, buf),
947 part_stat_read(hd, ios[0]),
948 part_stat_read(hd, merges[0]),
949 (unsigned long long)part_stat_read(hd, sectors[0]),
950 jiffies_to_msecs(part_stat_read(hd, ticks[0])),
951 part_stat_read(hd, ios[1]),
952 part_stat_read(hd, merges[1]),
953 (unsigned long long)part_stat_read(hd, sectors[1]),
954 jiffies_to_msecs(part_stat_read(hd, ticks[1])),
956 jiffies_to_msecs(part_stat_read(hd, io_ticks)),
957 jiffies_to_msecs(part_stat_read(hd, time_in_queue))
960 disk_part_iter_exit(&piter);
965 const struct seq_operations diskstats_op = {
966 .start = disk_seqf_start,
967 .next = disk_seqf_next,
968 .stop = disk_seqf_stop,
969 .show = diskstats_show
971 #endif /* CONFIG_PROC_FS */
973 static void media_change_notify_thread(struct work_struct *work)
975 struct gendisk *gd = container_of(work, struct gendisk, async_notify);
976 char event[] = "MEDIA_CHANGE=1";
977 char *envp[] = { event, NULL };
980 * set enviroment vars to indicate which event this is for
981 * so that user space will know to go check the media status.
983 kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp);
984 put_device(gd->driverfs_dev);
988 void genhd_media_change_notify(struct gendisk *disk)
990 get_device(disk->driverfs_dev);
991 schedule_work(&disk->async_notify);
993 EXPORT_SYMBOL_GPL(genhd_media_change_notify);
996 dev_t blk_lookup_devt(const char *name, int partno)
998 dev_t devt = MKDEV(0, 0);
999 struct class_dev_iter iter;
1002 class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1003 while ((dev = class_dev_iter_next(&iter))) {
1004 struct gendisk *disk = dev_to_disk(dev);
1005 struct hd_struct *part;
1007 if (strcmp(dev->bus_id, name))
1010 part = disk_get_part(disk, partno);
1011 if (part && (part->nr_sects || partno == 0)) {
1012 devt = part_devt(part);
1013 disk_put_part(part);
1016 disk_put_part(part);
1018 class_dev_iter_exit(&iter);
1021 EXPORT_SYMBOL(blk_lookup_devt);
1023 struct gendisk *alloc_disk(int minors)
1025 return alloc_disk_node(minors, -1);
1027 EXPORT_SYMBOL(alloc_disk);
1029 struct gendisk *alloc_disk_node(int minors, int node_id)
1031 struct gendisk *disk;
1033 disk = kmalloc_node(sizeof(struct gendisk),
1034 GFP_KERNEL | __GFP_ZERO, node_id);
1036 if (!init_part_stats(&disk->part0)) {
1040 if (disk_expand_part_tbl(disk, 0)) {
1041 free_part_stats(&disk->part0);
1045 disk->part_tbl->part[0] = &disk->part0;
1047 disk->minors = minors;
1048 rand_initialize_disk(disk);
1049 disk_to_dev(disk)->class = &block_class;
1050 disk_to_dev(disk)->type = &disk_type;
1051 device_initialize(disk_to_dev(disk));
1052 INIT_WORK(&disk->async_notify,
1053 media_change_notify_thread);
1054 disk->node_id = node_id;
1058 EXPORT_SYMBOL(alloc_disk_node);
1060 struct kobject *get_disk(struct gendisk *disk)
1062 struct module *owner;
1063 struct kobject *kobj;
1067 owner = disk->fops->owner;
1068 if (owner && !try_module_get(owner))
1070 kobj = kobject_get(&disk_to_dev(disk)->kobj);
1079 EXPORT_SYMBOL(get_disk);
1081 void put_disk(struct gendisk *disk)
1084 kobject_put(&disk_to_dev(disk)->kobj);
1087 EXPORT_SYMBOL(put_disk);
1089 void set_device_ro(struct block_device *bdev, int flag)
1091 bdev->bd_part->policy = flag;
1094 EXPORT_SYMBOL(set_device_ro);
1096 void set_disk_ro(struct gendisk *disk, int flag)
1098 struct disk_part_iter piter;
1099 struct hd_struct *part;
1101 disk_part_iter_init(&piter, disk,
1102 DISK_PITER_INCL_EMPTY | DISK_PITER_INCL_PART0);
1103 while ((part = disk_part_iter_next(&piter)))
1104 part->policy = flag;
1105 disk_part_iter_exit(&piter);
1108 EXPORT_SYMBOL(set_disk_ro);
1110 int bdev_read_only(struct block_device *bdev)
1114 return bdev->bd_part->policy;
1117 EXPORT_SYMBOL(bdev_read_only);
1119 int invalidate_partition(struct gendisk *disk, int partno)
1122 struct block_device *bdev = bdget_disk(disk, partno);
1125 res = __invalidate_device(bdev);
1131 EXPORT_SYMBOL(invalidate_partition);