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/genhd.h>
17 #include <linux/list.h>
18 #include <linux/moduleparam.h>
19 #include <linux/device.h>
20 
21 #include "rnbd-srv.h"
22 
23 static struct device *rnbd_dev;
24 static struct class *rnbd_dev_class;
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 			       const char *dev_name)
44 {
45 	struct kobject *bdev_kobj;
46 	int ret;
47 
48 	ret = kobject_init_and_add(&dev->dev_kobj, &dev_ktype,
49 				   rnbd_devs_kobj, dev_name);
50 	if (ret) {
51 		kobject_put(&dev->dev_kobj);
52 		return ret;
53 	}
54 
55 	dev->dev_sessions_kobj = kobject_create_and_add("sessions",
56 							&dev->dev_kobj);
57 	if (!dev->dev_sessions_kobj) {
58 		ret = -ENOMEM;
59 		goto free_dev_kobj;
60 	}
61 
62 	bdev_kobj = &disk_to_dev(bdev->bd_disk)->kobj;
63 	ret = sysfs_create_link(&dev->dev_kobj, bdev_kobj, "block_dev");
64 	if (ret)
65 		goto put_sess_kobj;
66 
67 	return 0;
68 
69 put_sess_kobj:
70 	kobject_put(dev->dev_sessions_kobj);
71 free_dev_kobj:
72 	kobject_del(&dev->dev_kobj);
73 	kobject_put(&dev->dev_kobj);
74 	return ret;
75 }
76 
77 void rnbd_srv_destroy_dev_sysfs(struct rnbd_srv_dev *dev)
78 {
79 	sysfs_remove_link(&dev->dev_kobj, "block_dev");
80 	kobject_del(dev->dev_sessions_kobj);
81 	kobject_put(dev->dev_sessions_kobj);
82 	kobject_del(&dev->dev_kobj);
83 	kobject_put(&dev->dev_kobj);
84 }
85 
86 static ssize_t read_only_show(struct kobject *kobj, struct kobj_attribute *attr,
87 			      char *page)
88 {
89 	struct rnbd_srv_sess_dev *sess_dev;
90 
91 	sess_dev = container_of(kobj, struct rnbd_srv_sess_dev, kobj);
92 
93 	return scnprintf(page, PAGE_SIZE, "%d\n",
94 			 !(sess_dev->open_flags & FMODE_WRITE));
95 }
96 
97 static struct kobj_attribute rnbd_srv_dev_session_ro_attr =
98 	__ATTR_RO(read_only);
99 
100 static ssize_t access_mode_show(struct kobject *kobj,
101 				struct kobj_attribute *attr,
102 				char *page)
103 {
104 	struct rnbd_srv_sess_dev *sess_dev;
105 
106 	sess_dev = container_of(kobj, struct rnbd_srv_sess_dev, kobj);
107 
108 	return scnprintf(page, PAGE_SIZE, "%s\n",
109 			 rnbd_access_mode_str(sess_dev->access_mode));
110 }
111 
112 static struct kobj_attribute rnbd_srv_dev_session_access_mode_attr =
113 	__ATTR_RO(access_mode);
114 
115 static ssize_t mapping_path_show(struct kobject *kobj,
116 				 struct kobj_attribute *attr, char *page)
117 {
118 	struct rnbd_srv_sess_dev *sess_dev;
119 
120 	sess_dev = container_of(kobj, struct rnbd_srv_sess_dev, kobj);
121 
122 	return scnprintf(page, PAGE_SIZE, "%s\n", sess_dev->pathname);
123 }
124 
125 static struct kobj_attribute rnbd_srv_dev_session_mapping_path_attr =
126 	__ATTR_RO(mapping_path);
127 
128 static ssize_t rnbd_srv_dev_session_force_close_show(struct kobject *kobj,
129 					struct kobj_attribute *attr, char *page)
130 {
131 	return scnprintf(page, PAGE_SIZE, "Usage: echo 1 > %s\n",
132 			 attr->attr.name);
133 }
134 
135 static ssize_t rnbd_srv_dev_session_force_close_store(struct kobject *kobj,
136 					struct kobj_attribute *attr,
137 					const char *buf, size_t count)
138 {
139 	struct rnbd_srv_sess_dev *sess_dev;
140 
141 	sess_dev = container_of(kobj, struct rnbd_srv_sess_dev, kobj);
142 
143 	if (!sysfs_streq(buf, "1")) {
144 		rnbd_srv_err(sess_dev, "%s: invalid value: '%s'\n",
145 			      attr->attr.name, buf);
146 		return -EINVAL;
147 	}
148 
149 	rnbd_srv_info(sess_dev, "force close requested\n");
150 
151 	/* first remove sysfs itself to avoid deadlock */
152 	sysfs_remove_file_self(&sess_dev->kobj, &attr->attr);
153 	rnbd_srv_sess_dev_force_close(sess_dev);
154 
155 	return count;
156 }
157 
158 static struct kobj_attribute rnbd_srv_dev_session_force_close_attr =
159 	__ATTR(force_close, 0644,
160 	       rnbd_srv_dev_session_force_close_show,
161 	       rnbd_srv_dev_session_force_close_store);
162 
163 static struct attribute *rnbd_srv_default_dev_sessions_attrs[] = {
164 	&rnbd_srv_dev_session_access_mode_attr.attr,
165 	&rnbd_srv_dev_session_ro_attr.attr,
166 	&rnbd_srv_dev_session_mapping_path_attr.attr,
167 	&rnbd_srv_dev_session_force_close_attr.attr,
168 	NULL,
169 };
170 
171 static struct attribute_group rnbd_srv_default_dev_session_attr_group = {
172 	.attrs = rnbd_srv_default_dev_sessions_attrs,
173 };
174 
175 void rnbd_srv_destroy_dev_session_sysfs(struct rnbd_srv_sess_dev *sess_dev)
176 {
177 	sysfs_remove_group(&sess_dev->kobj,
178 			   &rnbd_srv_default_dev_session_attr_group);
179 
180 	kobject_del(&sess_dev->kobj);
181 	kobject_put(&sess_dev->kobj);
182 }
183 
184 static void rnbd_srv_sess_dev_release(struct kobject *kobj)
185 {
186 	struct rnbd_srv_sess_dev *sess_dev;
187 
188 	sess_dev = container_of(kobj, struct rnbd_srv_sess_dev, kobj);
189 	rnbd_destroy_sess_dev(sess_dev, sess_dev->keep_id);
190 }
191 
192 static struct kobj_type rnbd_srv_sess_dev_ktype = {
193 	.sysfs_ops	= &kobj_sysfs_ops,
194 	.release	= rnbd_srv_sess_dev_release,
195 };
196 
197 int rnbd_srv_create_dev_session_sysfs(struct rnbd_srv_sess_dev *sess_dev)
198 {
199 	int ret;
200 
201 	ret = kobject_init_and_add(&sess_dev->kobj, &rnbd_srv_sess_dev_ktype,
202 				   sess_dev->dev->dev_sessions_kobj, "%s",
203 				   sess_dev->sess->sessname);
204 	if (ret) {
205 		kobject_put(&sess_dev->kobj);
206 		return ret;
207 	}
208 
209 	ret = sysfs_create_group(&sess_dev->kobj,
210 				 &rnbd_srv_default_dev_session_attr_group);
211 	if (ret) {
212 		kobject_del(&sess_dev->kobj);
213 		kobject_put(&sess_dev->kobj);
214 	}
215 
216 	return ret;
217 }
218 
219 int rnbd_srv_create_sysfs_files(void)
220 {
221 	int err;
222 
223 	rnbd_dev_class = class_create(THIS_MODULE, "rnbd-server");
224 	if (IS_ERR(rnbd_dev_class))
225 		return PTR_ERR(rnbd_dev_class);
226 
227 	rnbd_dev = device_create(rnbd_dev_class, NULL,
228 				  MKDEV(0, 0), NULL, "ctl");
229 	if (IS_ERR(rnbd_dev)) {
230 		err = PTR_ERR(rnbd_dev);
231 		goto cls_destroy;
232 	}
233 	rnbd_devs_kobj = kobject_create_and_add("devices", &rnbd_dev->kobj);
234 	if (!rnbd_devs_kobj) {
235 		err = -ENOMEM;
236 		goto dev_destroy;
237 	}
238 
239 	return 0;
240 
241 dev_destroy:
242 	device_destroy(rnbd_dev_class, MKDEV(0, 0));
243 cls_destroy:
244 	class_destroy(rnbd_dev_class);
245 
246 	return err;
247 }
248 
249 void rnbd_srv_destroy_sysfs_files(void)
250 {
251 	kobject_del(rnbd_devs_kobj);
252 	kobject_put(rnbd_devs_kobj);
253 	device_destroy(rnbd_dev_class, MKDEV(0, 0));
254 	class_destroy(rnbd_dev_class);
255 }
256