1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/blkdev.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 holder = bd_find_holder_disk(bdev, disk);
88 holder = kzalloc(sizeof(*holder), GFP_KERNEL);
94 INIT_LIST_HEAD(&holder->list);
97 if (disk->slave_dir) {
98 ret = __link_disk_holder(bdev, disk);
105 list_add(&holder->list, &disk->slave_bdevs);
107 * del_gendisk drops the initial reference to bd_holder_dir, so we need
108 * to keep our own here to allow for cleanup past that point.
110 kobject_get(bdev->bd_holder_dir);
113 mutex_unlock(&disk->open_mutex);
116 EXPORT_SYMBOL_GPL(bd_link_disk_holder);
118 static void __unlink_disk_holder(struct block_device *bdev,
119 struct gendisk *disk)
121 del_symlink(disk->slave_dir, bdev_kobj(bdev));
122 del_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj);
126 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
127 * @bdev: the calimed slave bdev
128 * @disk: the holding disk
130 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
135 void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
137 struct bd_holder_disk *holder;
139 mutex_lock(&disk->open_mutex);
140 holder = bd_find_holder_disk(bdev, disk);
141 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
143 __unlink_disk_holder(bdev, disk);
144 kobject_put(bdev->bd_holder_dir);
145 list_del_init(&holder->list);
148 mutex_unlock(&disk->open_mutex);
150 EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
152 int bd_register_pending_holders(struct gendisk *disk)
154 struct bd_holder_disk *holder;
157 mutex_lock(&disk->open_mutex);
158 list_for_each_entry(holder, &disk->slave_bdevs, list) {
159 ret = __link_disk_holder(holder->bdev, disk);
163 mutex_unlock(&disk->open_mutex);
167 list_for_each_entry_continue_reverse(holder, &disk->slave_bdevs, list)
168 __unlink_disk_holder(holder->bdev, disk);
169 mutex_unlock(&disk->open_mutex);