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 struct class *rnbd_dev_class;
23 static struct kobject *rnbd_devs_kobj;
25 static void rnbd_srv_dev_release(struct kobject *kobj)
27 struct rnbd_srv_dev *dev;
29 dev = container_of(kobj, struct rnbd_srv_dev, dev_kobj);
34 static struct kobj_type dev_ktype = {
35 .sysfs_ops = &kobj_sysfs_ops,
36 .release = rnbd_srv_dev_release
39 int rnbd_srv_create_dev_sysfs(struct rnbd_srv_dev *dev,
40 struct block_device *bdev)
42 struct kobject *bdev_kobj;
45 ret = kobject_init_and_add(&dev->dev_kobj, &dev_ktype,
46 rnbd_devs_kobj, "%pg", bdev);
48 kobject_put(&dev->dev_kobj);
52 dev->dev_sessions_kobj = kobject_create_and_add("sessions",
54 if (!dev->dev_sessions_kobj) {
59 bdev_kobj = &disk_to_dev(bdev->bd_disk)->kobj;
60 ret = sysfs_create_link(&dev->dev_kobj, bdev_kobj, "block_dev");
67 kobject_put(dev->dev_sessions_kobj);
69 kobject_del(&dev->dev_kobj);
70 kobject_put(&dev->dev_kobj);
74 void rnbd_srv_destroy_dev_sysfs(struct rnbd_srv_dev *dev)
76 sysfs_remove_link(&dev->dev_kobj, "block_dev");
77 kobject_del(dev->dev_sessions_kobj);
78 kobject_put(dev->dev_sessions_kobj);
79 kobject_del(&dev->dev_kobj);
80 kobject_put(&dev->dev_kobj);
83 static ssize_t read_only_show(struct kobject *kobj, struct kobj_attribute *attr,
86 struct rnbd_srv_sess_dev *sess_dev;
88 sess_dev = container_of(kobj, struct rnbd_srv_sess_dev, kobj);
90 return sysfs_emit(page, "%d\n",
91 !(sess_dev->open_flags & FMODE_WRITE));
94 static struct kobj_attribute rnbd_srv_dev_session_ro_attr =
97 static ssize_t access_mode_show(struct kobject *kobj,
98 struct kobj_attribute *attr,
101 struct rnbd_srv_sess_dev *sess_dev;
103 sess_dev = container_of(kobj, struct rnbd_srv_sess_dev, kobj);
105 return sysfs_emit(page, "%s\n",
106 rnbd_access_modes[sess_dev->access_mode].str);
109 static struct kobj_attribute rnbd_srv_dev_session_access_mode_attr =
110 __ATTR_RO(access_mode);
112 static ssize_t mapping_path_show(struct kobject *kobj,
113 struct kobj_attribute *attr, char *page)
115 struct rnbd_srv_sess_dev *sess_dev;
117 sess_dev = container_of(kobj, struct rnbd_srv_sess_dev, kobj);
119 return sysfs_emit(page, "%s\n", sess_dev->pathname);
122 static struct kobj_attribute rnbd_srv_dev_session_mapping_path_attr =
123 __ATTR_RO(mapping_path);
125 static ssize_t rnbd_srv_dev_session_force_close_show(struct kobject *kobj,
126 struct kobj_attribute *attr, char *page)
128 return sysfs_emit(page, "Usage: echo 1 > %s\n",
132 static ssize_t rnbd_srv_dev_session_force_close_store(struct kobject *kobj,
133 struct kobj_attribute *attr,
134 const char *buf, size_t count)
136 struct rnbd_srv_sess_dev *sess_dev;
138 sess_dev = container_of(kobj, struct rnbd_srv_sess_dev, kobj);
140 if (!sysfs_streq(buf, "1")) {
141 rnbd_srv_err(sess_dev, "%s: invalid value: '%s'\n",
142 attr->attr.name, buf);
146 rnbd_srv_info(sess_dev, "force close requested\n");
147 rnbd_srv_sess_dev_force_close(sess_dev, attr);
152 static struct kobj_attribute rnbd_srv_dev_session_force_close_attr =
153 __ATTR(force_close, 0644,
154 rnbd_srv_dev_session_force_close_show,
155 rnbd_srv_dev_session_force_close_store);
157 static struct attribute *rnbd_srv_default_dev_sessions_attrs[] = {
158 &rnbd_srv_dev_session_access_mode_attr.attr,
159 &rnbd_srv_dev_session_ro_attr.attr,
160 &rnbd_srv_dev_session_mapping_path_attr.attr,
161 &rnbd_srv_dev_session_force_close_attr.attr,
165 static struct attribute_group rnbd_srv_default_dev_session_attr_group = {
166 .attrs = rnbd_srv_default_dev_sessions_attrs,
169 void rnbd_srv_destroy_dev_session_sysfs(struct rnbd_srv_sess_dev *sess_dev)
171 sysfs_remove_group(&sess_dev->kobj,
172 &rnbd_srv_default_dev_session_attr_group);
174 kobject_del(&sess_dev->kobj);
175 kobject_put(&sess_dev->kobj);
178 static void rnbd_srv_sess_dev_release(struct kobject *kobj)
180 struct rnbd_srv_sess_dev *sess_dev;
182 sess_dev = container_of(kobj, struct rnbd_srv_sess_dev, kobj);
183 rnbd_destroy_sess_dev(sess_dev, sess_dev->keep_id);
186 static struct kobj_type rnbd_srv_sess_dev_ktype = {
187 .sysfs_ops = &kobj_sysfs_ops,
188 .release = rnbd_srv_sess_dev_release,
191 int rnbd_srv_create_dev_session_sysfs(struct rnbd_srv_sess_dev *sess_dev)
195 ret = kobject_init_and_add(&sess_dev->kobj, &rnbd_srv_sess_dev_ktype,
196 sess_dev->dev->dev_sessions_kobj, "%s",
197 sess_dev->sess->sessname);
199 kobject_put(&sess_dev->kobj);
203 ret = sysfs_create_group(&sess_dev->kobj,
204 &rnbd_srv_default_dev_session_attr_group);
206 kobject_del(&sess_dev->kobj);
207 kobject_put(&sess_dev->kobj);
213 int rnbd_srv_create_sysfs_files(void)
217 rnbd_dev_class = class_create("rnbd-server");
218 if (IS_ERR(rnbd_dev_class))
219 return PTR_ERR(rnbd_dev_class);
221 rnbd_dev = device_create(rnbd_dev_class, NULL,
222 MKDEV(0, 0), NULL, "ctl");
223 if (IS_ERR(rnbd_dev)) {
224 err = PTR_ERR(rnbd_dev);
227 rnbd_devs_kobj = kobject_create_and_add("devices", &rnbd_dev->kobj);
228 if (!rnbd_devs_kobj) {
236 device_destroy(rnbd_dev_class, MKDEV(0, 0));
238 class_destroy(rnbd_dev_class);
243 void rnbd_srv_destroy_sysfs_files(void)
245 kobject_del(rnbd_devs_kobj);
246 kobject_put(rnbd_devs_kobj);
247 device_destroy(rnbd_dev_class, MKDEV(0, 0));
248 class_destroy(rnbd_dev_class);