1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * RDMA Network Block Driver
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.
10 #define pr_fmt(fmt) KBUILD_MODNAME " L" __stringify(__LINE__) ": " fmt
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>
21 static struct device
*rnbd_dev
;
22 static const struct class rnbd_dev_class
= {
23 .name
= "rnbd-server",
25 static struct kobject
*rnbd_devs_kobj
;
27 static void rnbd_srv_dev_release(struct kobject
*kobj
)
29 struct rnbd_srv_dev
*dev
;
31 dev
= container_of(kobj
, struct rnbd_srv_dev
, dev_kobj
);
36 static const struct kobj_type dev_ktype
= {
37 .sysfs_ops
= &kobj_sysfs_ops
,
38 .release
= rnbd_srv_dev_release
41 int rnbd_srv_create_dev_sysfs(struct rnbd_srv_dev
*dev
,
42 struct block_device
*bdev
)
44 struct kobject
*bdev_kobj
;
47 ret
= kobject_init_and_add(&dev
->dev_kobj
, &dev_ktype
,
48 rnbd_devs_kobj
, "%pg", bdev
);
50 kobject_put(&dev
->dev_kobj
);
54 dev
->dev_sessions_kobj
= kobject_create_and_add("sessions",
56 if (!dev
->dev_sessions_kobj
) {
61 bdev_kobj
= &disk_to_dev(bdev
->bd_disk
)->kobj
;
62 ret
= sysfs_create_link(&dev
->dev_kobj
, bdev_kobj
, "block_dev");
69 kobject_put(dev
->dev_sessions_kobj
);
71 kobject_del(&dev
->dev_kobj
);
72 kobject_put(&dev
->dev_kobj
);
76 void rnbd_srv_destroy_dev_sysfs(struct rnbd_srv_dev
*dev
)
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
);
85 static ssize_t
read_only_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
88 struct rnbd_srv_sess_dev
*sess_dev
;
90 sess_dev
= container_of(kobj
, struct rnbd_srv_sess_dev
, kobj
);
92 return sysfs_emit(page
, "%d\n", sess_dev
->readonly
);
95 static struct kobj_attribute rnbd_srv_dev_session_ro_attr
=
98 static ssize_t
access_mode_show(struct kobject
*kobj
,
99 struct kobj_attribute
*attr
,
102 struct rnbd_srv_sess_dev
*sess_dev
;
104 sess_dev
= container_of(kobj
, struct rnbd_srv_sess_dev
, kobj
);
106 return sysfs_emit(page
, "%s\n",
107 rnbd_access_modes
[sess_dev
->access_mode
].str
);
110 static struct kobj_attribute rnbd_srv_dev_session_access_mode_attr
=
111 __ATTR_RO(access_mode
);
113 static ssize_t
mapping_path_show(struct kobject
*kobj
,
114 struct kobj_attribute
*attr
, char *page
)
116 struct rnbd_srv_sess_dev
*sess_dev
;
118 sess_dev
= container_of(kobj
, struct rnbd_srv_sess_dev
, kobj
);
120 return sysfs_emit(page
, "%s\n", sess_dev
->pathname
);
123 static struct kobj_attribute rnbd_srv_dev_session_mapping_path_attr
=
124 __ATTR_RO(mapping_path
);
126 static ssize_t
rnbd_srv_dev_session_force_close_show(struct kobject
*kobj
,
127 struct kobj_attribute
*attr
, char *page
)
129 return sysfs_emit(page
, "Usage: echo 1 > %s\n",
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
)
137 struct rnbd_srv_sess_dev
*sess_dev
;
139 sess_dev
= container_of(kobj
, struct rnbd_srv_sess_dev
, kobj
);
141 if (!sysfs_streq(buf
, "1")) {
142 rnbd_srv_err(sess_dev
, "%s: invalid value: '%s'\n",
143 attr
->attr
.name
, buf
);
147 rnbd_srv_info(sess_dev
, "force close requested\n");
148 rnbd_srv_sess_dev_force_close(sess_dev
, attr
);
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
);
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
,
166 static struct attribute_group rnbd_srv_default_dev_session_attr_group
= {
167 .attrs
= rnbd_srv_default_dev_sessions_attrs
,
170 void rnbd_srv_destroy_dev_session_sysfs(struct rnbd_srv_sess_dev
*sess_dev
)
172 sysfs_remove_group(&sess_dev
->kobj
,
173 &rnbd_srv_default_dev_session_attr_group
);
175 kobject_del(&sess_dev
->kobj
);
176 kobject_put(&sess_dev
->kobj
);
179 static void rnbd_srv_sess_dev_release(struct kobject
*kobj
)
181 struct rnbd_srv_sess_dev
*sess_dev
;
183 sess_dev
= container_of(kobj
, struct rnbd_srv_sess_dev
, kobj
);
184 rnbd_destroy_sess_dev(sess_dev
, sess_dev
->keep_id
);
187 static const struct kobj_type rnbd_srv_sess_dev_ktype
= {
188 .sysfs_ops
= &kobj_sysfs_ops
,
189 .release
= rnbd_srv_sess_dev_release
,
192 int rnbd_srv_create_dev_session_sysfs(struct rnbd_srv_sess_dev
*sess_dev
)
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
);
200 kobject_put(&sess_dev
->kobj
);
204 ret
= sysfs_create_group(&sess_dev
->kobj
,
205 &rnbd_srv_default_dev_session_attr_group
);
207 kobject_del(&sess_dev
->kobj
);
208 kobject_put(&sess_dev
->kobj
);
214 int rnbd_srv_create_sysfs_files(void)
218 err
= class_register(&rnbd_dev_class
);
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
);
228 rnbd_devs_kobj
= kobject_create_and_add("devices", &rnbd_dev
->kobj
);
229 if (!rnbd_devs_kobj
) {
237 device_destroy(&rnbd_dev_class
, MKDEV(0, 0));
239 class_unregister(&rnbd_dev_class
);
244 void rnbd_srv_destroy_sysfs_files(void)
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
);