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 kobject
*holder_dir
;
11 static DEFINE_MUTEX(blk_holder_mutex
);
13 static struct bd_holder_disk
*bd_find_holder_disk(struct block_device
*bdev
,
16 struct bd_holder_disk
*holder
;
18 list_for_each_entry(holder
, &disk
->slave_bdevs
, list
)
19 if (holder
->holder_dir
== bdev
->bd_holder_dir
)
24 static int add_symlink(struct kobject
*from
, struct kobject
*to
)
26 return sysfs_create_link(from
, to
, kobject_name(to
));
29 static void del_symlink(struct kobject
*from
, struct kobject
*to
)
31 sysfs_remove_link(from
, kobject_name(to
));
35 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
36 * @bdev: the claimed slave bdev
37 * @disk: the holding disk
39 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
41 * This functions creates the following sysfs symlinks.
43 * - from "slaves" directory of the holder @disk to the claimed @bdev
44 * - from "holders" directory of the @bdev to the holder @disk
46 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
47 * passed to bd_link_disk_holder(), then:
49 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
50 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
52 * The caller must have claimed @bdev before calling this function and
53 * ensure that both @bdev and @disk are valid during the creation and
54 * lifetime of these symlinks.
60 * 0 on success, -errno on failure.
62 int bd_link_disk_holder(struct block_device
*bdev
, struct gendisk
*disk
)
64 struct bd_holder_disk
*holder
;
67 if (WARN_ON_ONCE(!disk
->slave_dir
))
70 if (bdev
->bd_disk
== disk
)
74 * del_gendisk drops the initial reference to bd_holder_dir, so we
75 * need to keep our own here to allow for cleanup past that point.
77 mutex_lock(&bdev
->bd_disk
->open_mutex
);
78 if (!disk_live(bdev
->bd_disk
)) {
79 mutex_unlock(&bdev
->bd_disk
->open_mutex
);
82 kobject_get(bdev
->bd_holder_dir
);
83 mutex_unlock(&bdev
->bd_disk
->open_mutex
);
85 mutex_lock(&blk_holder_mutex
);
86 WARN_ON_ONCE(!bdev
->bd_holder
);
88 holder
= bd_find_holder_disk(bdev
, disk
);
90 kobject_put(bdev
->bd_holder_dir
);
95 holder
= kzalloc(sizeof(*holder
), GFP_KERNEL
);
101 INIT_LIST_HEAD(&holder
->list
);
103 holder
->holder_dir
= bdev
->bd_holder_dir
;
105 ret
= add_symlink(disk
->slave_dir
, bdev_kobj(bdev
));
107 goto out_free_holder
;
108 ret
= add_symlink(bdev
->bd_holder_dir
, &disk_to_dev(disk
)->kobj
);
110 goto out_del_symlink
;
111 list_add(&holder
->list
, &disk
->slave_bdevs
);
113 mutex_unlock(&blk_holder_mutex
);
117 del_symlink(disk
->slave_dir
, bdev_kobj(bdev
));
121 mutex_unlock(&blk_holder_mutex
);
123 kobject_put(bdev
->bd_holder_dir
);
126 EXPORT_SYMBOL_GPL(bd_link_disk_holder
);
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 if (WARN_ON_ONCE(!disk
->slave_dir
))
145 mutex_lock(&blk_holder_mutex
);
146 holder
= bd_find_holder_disk(bdev
, disk
);
147 if (!WARN_ON_ONCE(holder
== NULL
) && !--holder
->refcnt
) {
148 del_symlink(disk
->slave_dir
, bdev_kobj(bdev
));
149 del_symlink(holder
->holder_dir
, &disk_to_dev(disk
)->kobj
);
150 kobject_put(holder
->holder_dir
);
151 list_del_init(&holder
->list
);
154 mutex_unlock(&blk_holder_mutex
);
156 EXPORT_SYMBOL_GPL(bd_unlink_disk_holder
);