1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/genhd.h>
3 #include <linux/slab.h>
5 struct bd_holder_disk {
7 struct block_device *bdev;
11 static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
14 struct bd_holder_disk *holder;
16 list_for_each_entry(holder, &disk->slave_bdevs, list)
17 if (holder->bdev == bdev)
22 static int add_symlink(struct kobject *from, struct kobject *to)
24 return sysfs_create_link(from, to, kobject_name(to));
27 static void del_symlink(struct kobject *from, struct kobject *to)
29 sysfs_remove_link(from, kobject_name(to));
32 static int __link_disk_holder(struct block_device *bdev, struct gendisk *disk)
36 ret = add_symlink(disk->slave_dir, bdev_kobj(bdev));
39 ret = add_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj);
41 del_symlink(disk->slave_dir, bdev_kobj(bdev));
46 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
47 * @bdev: the claimed slave bdev
48 * @disk: the holding disk
50 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
52 * This functions creates the following sysfs symlinks.
54 * - from "slaves" directory of the holder @disk to the claimed @bdev
55 * - from "holders" directory of the @bdev to the holder @disk
57 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
58 * passed to bd_link_disk_holder(), then:
60 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
61 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
63 * The caller must have claimed @bdev before calling this function and
64 * ensure that both @bdev and @disk are valid during the creation and
65 * lifetime of these symlinks.
71 * 0 on success, -errno on failure.
73 int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
75 struct bd_holder_disk *holder;
78 mutex_lock(&disk->open_mutex);
80 WARN_ON_ONCE(!bdev->bd_holder);
82 /* FIXME: remove the following once add_disk() handles errors */
83 if (WARN_ON(!bdev->bd_holder_dir))
86 holder = bd_find_holder_disk(bdev, disk);
92 holder = kzalloc(sizeof(*holder), GFP_KERNEL);
98 INIT_LIST_HEAD(&holder->list);
101 if (disk->slave_dir) {
102 ret = __link_disk_holder(bdev, disk);
109 list_add(&holder->list, &disk->slave_bdevs);
111 * del_gendisk drops the initial reference to bd_holder_dir, so we need
112 * to keep our own here to allow for cleanup past that point.
114 kobject_get(bdev->bd_holder_dir);
117 mutex_unlock(&disk->open_mutex);
120 EXPORT_SYMBOL_GPL(bd_link_disk_holder);
122 static void __unlink_disk_holder(struct block_device *bdev,
123 struct gendisk *disk)
125 del_symlink(disk->slave_dir, bdev_kobj(bdev));
126 del_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj);
130 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
131 * @bdev: the calimed slave bdev
132 * @disk: the holding disk
134 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
139 void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
141 struct bd_holder_disk *holder;
143 mutex_lock(&disk->open_mutex);
144 holder = bd_find_holder_disk(bdev, disk);
145 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
147 __unlink_disk_holder(bdev, disk);
148 kobject_put(bdev->bd_holder_dir);
149 list_del_init(&holder->list);
152 mutex_unlock(&disk->open_mutex);
154 EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
156 int bd_register_pending_holders(struct gendisk *disk)
158 struct bd_holder_disk *holder;
161 mutex_lock(&disk->open_mutex);
162 list_for_each_entry(holder, &disk->slave_bdevs, list) {
163 ret = __link_disk_holder(holder->bdev, disk);
167 mutex_unlock(&disk->open_mutex);
171 list_for_each_entry_continue_reverse(holder, &disk->slave_bdevs, list)
172 __unlink_disk_holder(holder->bdev, disk);
173 mutex_unlock(&disk->open_mutex);