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 <uapi/linux/limits.h>
13 #include <linux/kobject.h>
14 #include <linux/sysfs.h>
15 #include <linux/stat.h>
16 #include <linux/list.h>
17 #include <linux/moduleparam.h>
18 #include <linux/device.h>
19 
20 #include "rnbd-srv.h"
21 
22 static struct device *rnbd_dev;
23 static struct class *rnbd_dev_class;
24 static struct kobject *rnbd_devs_kobj;
25 
26 static void rnbd_srv_dev_release(struct kobject *kobj)
27 {
28 	struct rnbd_srv_dev *dev;
29 
30 	dev = container_of(kobj, struct rnbd_srv_dev, dev_kobj);
31 
32 	kfree(dev);
33 }
34 
35 static struct kobj_type dev_ktype = {
36 	.sysfs_ops = &kobj_sysfs_ops,
37 	.release = rnbd_srv_dev_release
38 };
39 
40 int rnbd_srv_create_dev_sysfs(struct rnbd_srv_dev *dev,
41 			       struct block_device *bdev)
42 {
43 	struct kobject *bdev_kobj;
44 	int ret;
45 
46 	ret = kobject_init_and_add(&dev->dev_kobj, &dev_ktype,
47 				   rnbd_devs_kobj, "%pg", bdev);
48 	if (ret) {
49 		kobject_put(&dev->dev_kobj);
50 		return ret;
51 	}
52 
53 	dev->dev_sessions_kobj = kobject_create_and_add("sessions",
54 							&dev->dev_kobj);
55 	if (!dev->dev_sessions_kobj) {
56 		ret = -ENOMEM;
57 		goto free_dev_kobj;
58 	}
59 
60 	bdev_kobj = &disk_to_dev(bdev->bd_disk)->kobj;
61 	ret = sysfs_create_link(&dev->dev_kobj, bdev_kobj, "block_dev");
62 	if (ret)
63 		goto put_sess_kobj;
64 
65 	return 0;
66 
67 put_sess_kobj:
68 	kobject_put(dev->dev_sessions_kobj);
69 free_dev_kobj:
70 	kobject_del(&dev->dev_kobj);
71 	kobject_put(&dev->dev_kobj);
72 	return ret;
73 }
74 
75 void rnbd_srv_destroy_dev_sysfs(struct rnbd_srv_dev *dev)
76 {
77 	sysfs_remove_link(&dev->dev_kobj, "block_dev");
78 	kobject_del(dev->dev_sessions_kobj);
79 	kobject_put(dev->dev_sessions_kobj);
80 	kobject_del(&dev->dev_kobj);
81 	kobject_put(&dev->dev_kobj);
82 }
83 
84 static ssize_t read_only_show(struct kobject *kobj, struct kobj_attribute *attr,
85 			      char *page)
86 {
87 	struct rnbd_srv_sess_dev *sess_dev;
88 
89 	sess_dev = container_of(kobj, struct rnbd_srv_sess_dev, kobj);
90 
91 	return sysfs_emit(page, "%d\n",
92 			  !(sess_dev->open_flags & FMODE_WRITE));
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_mode_str(sess_dev->access_mode));
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 	rnbd_dev_class = class_create("rnbd-server");
219 	if (IS_ERR(rnbd_dev_class))
220 		return PTR_ERR(rnbd_dev_class);
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_destroy(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_destroy(rnbd_dev_class);
250 }
251