xref: /linux/drivers/block/rnbd/rnbd-srv-sysfs.c (revision 08b7174fb8d126e607e385e34b9e1da4f3be274f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * RDMA Network Block Driver
4  *
5  * Copyright (c) 2014 - 2018 ProfitBricks GmbH. All rights reserved.
6  * Copyright (c) 2018 - 2019 1&1 IONOS Cloud GmbH. All rights reserved.
7  * Copyright (c) 2019 - 2020 1&1 IONOS SE. All rights reserved.
8  */
9 #undef pr_fmt
10 #define pr_fmt(fmt) KBUILD_MODNAME " L" __stringify(__LINE__) ": " fmt
11 
12 #include <linux/kobject.h>
13 #include <linux/sysfs.h>
14 #include <linux/stat.h>
15 #include <linux/list.h>
16 #include <linux/moduleparam.h>
17 #include <linux/device.h>
18 
19 #include "rnbd-srv.h"
20 
21 static struct device *rnbd_dev;
22 static const struct class rnbd_dev_class = {
23 	.name = "rnbd-server",
24 };
25 static struct kobject *rnbd_devs_kobj;
26 
27 static void rnbd_srv_dev_release(struct kobject *kobj)
28 {
29 	struct rnbd_srv_dev *dev;
30 
31 	dev = container_of(kobj, struct rnbd_srv_dev, dev_kobj);
32 
33 	kfree(dev);
34 }
35 
36 static struct kobj_type dev_ktype = {
37 	.sysfs_ops = &kobj_sysfs_ops,
38 	.release = rnbd_srv_dev_release
39 };
40 
41 int rnbd_srv_create_dev_sysfs(struct rnbd_srv_dev *dev,
42 			       struct block_device *bdev)
43 {
44 	struct kobject *bdev_kobj;
45 	int ret;
46 
47 	ret = kobject_init_and_add(&dev->dev_kobj, &dev_ktype,
48 				   rnbd_devs_kobj, "%pg", bdev);
49 	if (ret) {
50 		kobject_put(&dev->dev_kobj);
51 		return ret;
52 	}
53 
54 	dev->dev_sessions_kobj = kobject_create_and_add("sessions",
55 							&dev->dev_kobj);
56 	if (!dev->dev_sessions_kobj) {
57 		ret = -ENOMEM;
58 		goto free_dev_kobj;
59 	}
60 
61 	bdev_kobj = &disk_to_dev(bdev->bd_disk)->kobj;
62 	ret = sysfs_create_link(&dev->dev_kobj, bdev_kobj, "block_dev");
63 	if (ret)
64 		goto put_sess_kobj;
65 
66 	return 0;
67 
68 put_sess_kobj:
69 	kobject_put(dev->dev_sessions_kobj);
70 free_dev_kobj:
71 	kobject_del(&dev->dev_kobj);
72 	kobject_put(&dev->dev_kobj);
73 	return ret;
74 }
75 
76 void rnbd_srv_destroy_dev_sysfs(struct rnbd_srv_dev *dev)
77 {
78 	sysfs_remove_link(&dev->dev_kobj, "block_dev");
79 	kobject_del(dev->dev_sessions_kobj);
80 	kobject_put(dev->dev_sessions_kobj);
81 	kobject_del(&dev->dev_kobj);
82 	kobject_put(&dev->dev_kobj);
83 }
84 
85 static ssize_t read_only_show(struct kobject *kobj, struct kobj_attribute *attr,
86 			      char *page)
87 {
88 	struct rnbd_srv_sess_dev *sess_dev;
89 
90 	sess_dev = container_of(kobj, struct rnbd_srv_sess_dev, kobj);
91 
92 	return sysfs_emit(page, "%d\n", sess_dev->readonly);
93 }
94 
95 static struct kobj_attribute rnbd_srv_dev_session_ro_attr =
96 	__ATTR_RO(read_only);
97 
98 static ssize_t access_mode_show(struct kobject *kobj,
99 				struct kobj_attribute *attr,
100 				char *page)
101 {
102 	struct rnbd_srv_sess_dev *sess_dev;
103 
104 	sess_dev = container_of(kobj, struct rnbd_srv_sess_dev, kobj);
105 
106 	return sysfs_emit(page, "%s\n",
107 			  rnbd_access_modes[sess_dev->access_mode].str);
108 }
109 
110 static struct kobj_attribute rnbd_srv_dev_session_access_mode_attr =
111 	__ATTR_RO(access_mode);
112 
113 static ssize_t mapping_path_show(struct kobject *kobj,
114 				 struct kobj_attribute *attr, char *page)
115 {
116 	struct rnbd_srv_sess_dev *sess_dev;
117 
118 	sess_dev = container_of(kobj, struct rnbd_srv_sess_dev, kobj);
119 
120 	return sysfs_emit(page, "%s\n", sess_dev->pathname);
121 }
122 
123 static struct kobj_attribute rnbd_srv_dev_session_mapping_path_attr =
124 	__ATTR_RO(mapping_path);
125 
126 static ssize_t rnbd_srv_dev_session_force_close_show(struct kobject *kobj,
127 					struct kobj_attribute *attr, char *page)
128 {
129 	return sysfs_emit(page, "Usage: echo 1 > %s\n",
130 			  attr->attr.name);
131 }
132 
133 static ssize_t rnbd_srv_dev_session_force_close_store(struct kobject *kobj,
134 					struct kobj_attribute *attr,
135 					const char *buf, size_t count)
136 {
137 	struct rnbd_srv_sess_dev *sess_dev;
138 
139 	sess_dev = container_of(kobj, struct rnbd_srv_sess_dev, kobj);
140 
141 	if (!sysfs_streq(buf, "1")) {
142 		rnbd_srv_err(sess_dev, "%s: invalid value: '%s'\n",
143 			      attr->attr.name, buf);
144 		return -EINVAL;
145 	}
146 
147 	rnbd_srv_info(sess_dev, "force close requested\n");
148 	rnbd_srv_sess_dev_force_close(sess_dev, attr);
149 
150 	return count;
151 }
152 
153 static struct kobj_attribute rnbd_srv_dev_session_force_close_attr =
154 	__ATTR(force_close, 0644,
155 	       rnbd_srv_dev_session_force_close_show,
156 	       rnbd_srv_dev_session_force_close_store);
157 
158 static struct attribute *rnbd_srv_default_dev_sessions_attrs[] = {
159 	&rnbd_srv_dev_session_access_mode_attr.attr,
160 	&rnbd_srv_dev_session_ro_attr.attr,
161 	&rnbd_srv_dev_session_mapping_path_attr.attr,
162 	&rnbd_srv_dev_session_force_close_attr.attr,
163 	NULL,
164 };
165 
166 static struct attribute_group rnbd_srv_default_dev_session_attr_group = {
167 	.attrs = rnbd_srv_default_dev_sessions_attrs,
168 };
169 
170 void rnbd_srv_destroy_dev_session_sysfs(struct rnbd_srv_sess_dev *sess_dev)
171 {
172 	sysfs_remove_group(&sess_dev->kobj,
173 			   &rnbd_srv_default_dev_session_attr_group);
174 
175 	kobject_del(&sess_dev->kobj);
176 	kobject_put(&sess_dev->kobj);
177 }
178 
179 static void rnbd_srv_sess_dev_release(struct kobject *kobj)
180 {
181 	struct rnbd_srv_sess_dev *sess_dev;
182 
183 	sess_dev = container_of(kobj, struct rnbd_srv_sess_dev, kobj);
184 	rnbd_destroy_sess_dev(sess_dev, sess_dev->keep_id);
185 }
186 
187 static struct kobj_type rnbd_srv_sess_dev_ktype = {
188 	.sysfs_ops	= &kobj_sysfs_ops,
189 	.release	= rnbd_srv_sess_dev_release,
190 };
191 
192 int rnbd_srv_create_dev_session_sysfs(struct rnbd_srv_sess_dev *sess_dev)
193 {
194 	int ret;
195 
196 	ret = kobject_init_and_add(&sess_dev->kobj, &rnbd_srv_sess_dev_ktype,
197 				   sess_dev->dev->dev_sessions_kobj, "%s",
198 				   sess_dev->sess->sessname);
199 	if (ret) {
200 		kobject_put(&sess_dev->kobj);
201 		return ret;
202 	}
203 
204 	ret = sysfs_create_group(&sess_dev->kobj,
205 				 &rnbd_srv_default_dev_session_attr_group);
206 	if (ret) {
207 		kobject_del(&sess_dev->kobj);
208 		kobject_put(&sess_dev->kobj);
209 	}
210 
211 	return ret;
212 }
213 
214 int rnbd_srv_create_sysfs_files(void)
215 {
216 	int err;
217 
218 	err = class_register(&rnbd_dev_class);
219 	if (err)
220 		return err;
221 
222 	rnbd_dev = device_create(&rnbd_dev_class, NULL,
223 				 MKDEV(0, 0), NULL, "ctl");
224 	if (IS_ERR(rnbd_dev)) {
225 		err = PTR_ERR(rnbd_dev);
226 		goto cls_destroy;
227 	}
228 	rnbd_devs_kobj = kobject_create_and_add("devices", &rnbd_dev->kobj);
229 	if (!rnbd_devs_kobj) {
230 		err = -ENOMEM;
231 		goto dev_destroy;
232 	}
233 
234 	return 0;
235 
236 dev_destroy:
237 	device_destroy(&rnbd_dev_class, MKDEV(0, 0));
238 cls_destroy:
239 	class_unregister(&rnbd_dev_class);
240 
241 	return err;
242 }
243 
244 void rnbd_srv_destroy_sysfs_files(void)
245 {
246 	kobject_del(rnbd_devs_kobj);
247 	kobject_put(rnbd_devs_kobj);
248 	device_destroy(&rnbd_dev_class, MKDEV(0, 0));
249 	class_unregister(&rnbd_dev_class);
250 }
251