1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/genhd.h>
4 struct bd_holder_disk {
6 struct block_device *bdev;
10 static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
13 struct bd_holder_disk *holder;
15 list_for_each_entry(holder, &disk->slave_bdevs, list)
16 if (holder->bdev == bdev)
21 static int add_symlink(struct kobject *from, struct kobject *to)
23 return sysfs_create_link(from, to, kobject_name(to));
26 static void del_symlink(struct kobject *from, struct kobject *to)
28 sysfs_remove_link(from, kobject_name(to));
31 static int __link_disk_holder(struct block_device *bdev, struct gendisk *disk)
35 ret = add_symlink(disk->slave_dir, bdev_kobj(bdev));
38 ret = add_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj);
40 del_symlink(disk->slave_dir, bdev_kobj(bdev));
45 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
46 * @bdev: the claimed slave bdev
47 * @disk: the holding disk
49 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
51 * This functions creates the following sysfs symlinks.
53 * - from "slaves" directory of the holder @disk to the claimed @bdev
54 * - from "holders" directory of the @bdev to the holder @disk
56 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
57 * passed to bd_link_disk_holder(), then:
59 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
60 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
62 * The caller must have claimed @bdev before calling this function and
63 * ensure that both @bdev and @disk are valid during the creation and
64 * lifetime of these symlinks.
70 * 0 on success, -errno on failure.
72 int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
74 struct bd_holder_disk *holder;
77 mutex_lock(&disk->open_mutex);
79 WARN_ON_ONCE(!bdev->bd_holder);
81 /* FIXME: remove the following once add_disk() handles errors */
82 if (WARN_ON(!bdev->bd_holder_dir))
85 holder = bd_find_holder_disk(bdev, disk);
91 holder = kzalloc(sizeof(*holder), GFP_KERNEL);
97 INIT_LIST_HEAD(&holder->list);
100 if (disk->slave_dir) {
101 ret = __link_disk_holder(bdev, disk);
108 list_add(&holder->list, &disk->slave_bdevs);
110 * del_gendisk drops the initial reference to bd_holder_dir, so we need
111 * to keep our own here to allow for cleanup past that point.
113 kobject_get(bdev->bd_holder_dir);
116 mutex_unlock(&disk->open_mutex);
119 EXPORT_SYMBOL_GPL(bd_link_disk_holder);
121 static void __unlink_disk_holder(struct block_device *bdev,
122 struct gendisk *disk)
124 del_symlink(disk->slave_dir, bdev_kobj(bdev));
125 del_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj);
129 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
130 * @bdev: the calimed slave bdev
131 * @disk: the holding disk
133 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
138 void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
140 struct bd_holder_disk *holder;
142 mutex_lock(&disk->open_mutex);
143 holder = bd_find_holder_disk(bdev, disk);
144 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
146 __unlink_disk_holder(bdev, disk);
147 kobject_put(bdev->bd_holder_dir);
148 list_del_init(&holder->list);
151 mutex_unlock(&disk->open_mutex);
153 EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
155 int bd_register_pending_holders(struct gendisk *disk)
157 struct bd_holder_disk *holder;
160 mutex_lock(&disk->open_mutex);
161 list_for_each_entry(holder, &disk->slave_bdevs, list) {
162 ret = __link_disk_holder(holder->bdev, disk);
166 mutex_unlock(&disk->open_mutex);
170 list_for_each_entry_continue_reverse(holder, &disk->slave_bdevs, list)
171 __unlink_disk_holder(holder->bdev, disk);
172 mutex_unlock(&disk->open_mutex);