blob: 9dc084182337f87dbe41b49b2652c06ee851abde [file] [log] [blame]
Christoph Hellwigc66fd012021-08-04 11:41:40 +02001// SPDX-License-Identifier: GPL-2.0-only
2#include <linux/genhd.h>
3
4struct bd_holder_disk {
5 struct list_head list;
Christoph Hellwig0dbcfe22021-08-04 11:41:42 +02006 struct block_device *bdev;
Christoph Hellwigc66fd012021-08-04 11:41:40 +02007 int refcnt;
8};
9
10static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
11 struct gendisk *disk)
12{
13 struct bd_holder_disk *holder;
14
Christoph Hellwig0dbcfe22021-08-04 11:41:42 +020015 list_for_each_entry(holder, &disk->slave_bdevs, list)
16 if (holder->bdev == bdev)
Christoph Hellwigc66fd012021-08-04 11:41:40 +020017 return holder;
18 return NULL;
19}
20
21static int add_symlink(struct kobject *from, struct kobject *to)
22{
23 return sysfs_create_link(from, to, kobject_name(to));
24}
25
26static void del_symlink(struct kobject *from, struct kobject *to)
27{
28 sysfs_remove_link(from, kobject_name(to));
29}
30
Christoph Hellwigd6263382021-08-04 11:41:43 +020031static int __link_disk_holder(struct block_device *bdev, struct gendisk *disk)
32{
33 int ret;
34
35 ret = add_symlink(disk->slave_dir, bdev_kobj(bdev));
36 if (ret)
37 return ret;
38 ret = add_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj);
39 if (ret)
40 del_symlink(disk->slave_dir, bdev_kobj(bdev));
41 return ret;
42}
43
Christoph Hellwigc66fd012021-08-04 11:41:40 +020044/**
45 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
46 * @bdev: the claimed slave bdev
47 * @disk: the holding disk
48 *
49 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
50 *
51 * This functions creates the following sysfs symlinks.
52 *
53 * - from "slaves" directory of the holder @disk to the claimed @bdev
54 * - from "holders" directory of the @bdev to the holder @disk
55 *
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:
58 *
59 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
60 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
61 *
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.
65 *
66 * CONTEXT:
67 * Might sleep.
68 *
69 * RETURNS:
70 * 0 on success, -errno on failure.
71 */
72int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
73{
74 struct bd_holder_disk *holder;
75 int ret = 0;
76
Christoph Hellwig0dbcfe22021-08-04 11:41:42 +020077 mutex_lock(&disk->open_mutex);
Christoph Hellwigc66fd012021-08-04 11:41:40 +020078
79 WARN_ON_ONCE(!bdev->bd_holder);
80
81 /* FIXME: remove the following once add_disk() handles errors */
Christoph Hellwigd6263382021-08-04 11:41:43 +020082 if (WARN_ON(!bdev->bd_holder_dir))
Christoph Hellwigc66fd012021-08-04 11:41:40 +020083 goto out_unlock;
84
85 holder = bd_find_holder_disk(bdev, disk);
86 if (holder) {
87 holder->refcnt++;
88 goto out_unlock;
89 }
90
91 holder = kzalloc(sizeof(*holder), GFP_KERNEL);
92 if (!holder) {
93 ret = -ENOMEM;
94 goto out_unlock;
95 }
96
97 INIT_LIST_HEAD(&holder->list);
Christoph Hellwig0dbcfe22021-08-04 11:41:42 +020098 holder->bdev = bdev;
Christoph Hellwigc66fd012021-08-04 11:41:40 +020099 holder->refcnt = 1;
Christoph Hellwigd6263382021-08-04 11:41:43 +0200100 if (disk->slave_dir) {
101 ret = __link_disk_holder(bdev, disk);
102 if (ret) {
103 kfree(holder);
104 goto out_unlock;
105 }
106 }
Christoph Hellwigc66fd012021-08-04 11:41:40 +0200107
Christoph Hellwig0dbcfe22021-08-04 11:41:42 +0200108 list_add(&holder->list, &disk->slave_bdevs);
Christoph Hellwig759e0fd2021-08-20 11:49:29 +0200109 /*
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.
112 */
113 kobject_get(bdev->bd_holder_dir);
114
Christoph Hellwigc66fd012021-08-04 11:41:40 +0200115out_unlock:
Christoph Hellwig0dbcfe22021-08-04 11:41:42 +0200116 mutex_unlock(&disk->open_mutex);
Christoph Hellwigc66fd012021-08-04 11:41:40 +0200117 return ret;
118}
119EXPORT_SYMBOL_GPL(bd_link_disk_holder);
120
Christoph Hellwigd6263382021-08-04 11:41:43 +0200121static void __unlink_disk_holder(struct block_device *bdev,
122 struct gendisk *disk)
123{
124 del_symlink(disk->slave_dir, bdev_kobj(bdev));
125 del_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj);
126}
127
Christoph Hellwigc66fd012021-08-04 11:41:40 +0200128/**
129 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
130 * @bdev: the calimed slave bdev
131 * @disk: the holding disk
132 *
133 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
134 *
135 * CONTEXT:
136 * Might sleep.
137 */
138void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
139{
140 struct bd_holder_disk *holder;
141
Christoph Hellwig0dbcfe22021-08-04 11:41:42 +0200142 mutex_lock(&disk->open_mutex);
Christoph Hellwigc66fd012021-08-04 11:41:40 +0200143 holder = bd_find_holder_disk(bdev, disk);
144 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
Christoph Hellwigd6263382021-08-04 11:41:43 +0200145 if (disk->slave_dir)
146 __unlink_disk_holder(bdev, disk);
Christoph Hellwig759e0fd2021-08-20 11:49:29 +0200147 kobject_put(bdev->bd_holder_dir);
Christoph Hellwigc66fd012021-08-04 11:41:40 +0200148 list_del_init(&holder->list);
149 kfree(holder);
150 }
Christoph Hellwig0dbcfe22021-08-04 11:41:42 +0200151 mutex_unlock(&disk->open_mutex);
Christoph Hellwigc66fd012021-08-04 11:41:40 +0200152}
153EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
Christoph Hellwigd6263382021-08-04 11:41:43 +0200154
155int bd_register_pending_holders(struct gendisk *disk)
156{
157 struct bd_holder_disk *holder;
158 int ret;
159
160 mutex_lock(&disk->open_mutex);
161 list_for_each_entry(holder, &disk->slave_bdevs, list) {
162 ret = __link_disk_holder(holder->bdev, disk);
163 if (ret)
164 goto out_undo;
165 }
166 mutex_unlock(&disk->open_mutex);
167 return 0;
168
169out_undo:
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);
173 return ret;
174}