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 <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>
23 static struct device
*rnbd_dev
;
24 static struct class *rnbd_dev_class
;
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 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
,
45 struct kobject
*bdev_kobj
;
48 ret
= kobject_init_and_add(&dev
->dev_kobj
, &dev_ktype
,
49 rnbd_devs_kobj
, dev_name
);
51 kobject_put(&dev
->dev_kobj
);
55 dev
->dev_sessions_kobj
= kobject_create_and_add("sessions",
57 if (!dev
->dev_sessions_kobj
) {
62 bdev_kobj
= &disk_to_dev(bdev
->bd_disk
)->kobj
;
63 ret
= sysfs_create_link(&dev
->dev_kobj
, bdev_kobj
, "block_dev");
70 kobject_put(dev
->dev_sessions_kobj
);
72 kobject_del(&dev
->dev_kobj
);
73 kobject_put(&dev
->dev_kobj
);
77 void rnbd_srv_destroy_dev_sysfs(struct rnbd_srv_dev
*dev
)
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
);
86 static ssize_t
read_only_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
89 struct rnbd_srv_sess_dev
*sess_dev
;
91 sess_dev
= container_of(kobj
, struct rnbd_srv_sess_dev
, kobj
);
93 return scnprintf(page
, PAGE_SIZE
, "%d\n",
94 !(sess_dev
->open_flags
& FMODE_WRITE
));
97 static struct kobj_attribute rnbd_srv_dev_session_ro_attr
=
100 static ssize_t
access_mode_show(struct kobject
*kobj
,
101 struct kobj_attribute
*attr
,
104 struct rnbd_srv_sess_dev
*sess_dev
;
106 sess_dev
= container_of(kobj
, struct rnbd_srv_sess_dev
, kobj
);
108 return scnprintf(page
, PAGE_SIZE
, "%s\n",
109 rnbd_access_mode_str(sess_dev
->access_mode
));
112 static struct kobj_attribute rnbd_srv_dev_session_access_mode_attr
=
113 __ATTR_RO(access_mode
);
115 static ssize_t
mapping_path_show(struct kobject
*kobj
,
116 struct kobj_attribute
*attr
, char *page
)
118 struct rnbd_srv_sess_dev
*sess_dev
;
120 sess_dev
= container_of(kobj
, struct rnbd_srv_sess_dev
, kobj
);
122 return scnprintf(page
, PAGE_SIZE
, "%s\n", sess_dev
->pathname
);
125 static struct kobj_attribute rnbd_srv_dev_session_mapping_path_attr
=
126 __ATTR_RO(mapping_path
);
128 static ssize_t
rnbd_srv_dev_session_force_close_show(struct kobject
*kobj
,
129 struct kobj_attribute
*attr
, char *page
)
131 return scnprintf(page
, PAGE_SIZE
, "Usage: echo 1 > %s\n",
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
)
139 struct rnbd_srv_sess_dev
*sess_dev
;
141 sess_dev
= container_of(kobj
, struct rnbd_srv_sess_dev
, kobj
);
143 if (!sysfs_streq(buf
, "1")) {
144 rnbd_srv_err(sess_dev
, "%s: invalid value: '%s'\n",
145 attr
->attr
.name
, buf
);
149 rnbd_srv_info(sess_dev
, "force close requested\n");
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
);
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
);
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
,
171 static struct attribute_group rnbd_srv_default_dev_session_attr_group
= {
172 .attrs
= rnbd_srv_default_dev_sessions_attrs
,
175 void rnbd_srv_destroy_dev_session_sysfs(struct rnbd_srv_sess_dev
*sess_dev
)
177 sysfs_remove_group(&sess_dev
->kobj
,
178 &rnbd_srv_default_dev_session_attr_group
);
180 kobject_del(&sess_dev
->kobj
);
181 kobject_put(&sess_dev
->kobj
);
184 static void rnbd_srv_sess_dev_release(struct kobject
*kobj
)
186 struct rnbd_srv_sess_dev
*sess_dev
;
188 sess_dev
= container_of(kobj
, struct rnbd_srv_sess_dev
, kobj
);
189 rnbd_destroy_sess_dev(sess_dev
, sess_dev
->keep_id
);
192 static struct kobj_type rnbd_srv_sess_dev_ktype
= {
193 .sysfs_ops
= &kobj_sysfs_ops
,
194 .release
= rnbd_srv_sess_dev_release
,
197 int rnbd_srv_create_dev_session_sysfs(struct rnbd_srv_sess_dev
*sess_dev
)
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
);
205 kobject_put(&sess_dev
->kobj
);
209 ret
= sysfs_create_group(&sess_dev
->kobj
,
210 &rnbd_srv_default_dev_session_attr_group
);
212 kobject_del(&sess_dev
->kobj
);
213 kobject_put(&sess_dev
->kobj
);
219 int rnbd_srv_create_sysfs_files(void)
223 rnbd_dev_class
= class_create(THIS_MODULE
, "rnbd-server");
224 if (IS_ERR(rnbd_dev_class
))
225 return PTR_ERR(rnbd_dev_class
);
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
);
233 rnbd_devs_kobj
= kobject_create_and_add("devices", &rnbd_dev
->kobj
);
234 if (!rnbd_devs_kobj
) {
242 device_destroy(rnbd_dev_class
, MKDEV(0, 0));
244 class_destroy(rnbd_dev_class
);
249 void rnbd_srv_destroy_sysfs_files(void)
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
);