WIP FPC-III support
[linux/fpc-iii.git] / drivers / block / rnbd / rnbd-srv-sysfs.c
blob05ffe488ddc64e75c4dee8c6369aac84df2ae3c3
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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.
8 */
9 #undef pr_fmt
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>
21 #include "rnbd-srv.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);
33 kfree(dev);
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,
43 const char *dev_name)
45 struct kobject *bdev_kobj;
46 int ret;
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;
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;
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;
67 return 0;
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;
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,
87 char *page)
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 =
98 __ATTR_RO(read_only);
100 static ssize_t access_mode_show(struct kobject *kobj,
101 struct kobj_attribute *attr,
102 char *page)
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",
132 attr->attr.name);
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);
146 return -EINVAL;
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);
155 return count;
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,
168 NULL,
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)
199 int ret;
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;
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);
216 return ret;
219 int rnbd_srv_create_sysfs_files(void)
221 int err;
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);
231 goto cls_destroy;
233 rnbd_devs_kobj = kobject_create_and_add("devices", &rnbd_dev->kobj);
234 if (!rnbd_devs_kobj) {
235 err = -ENOMEM;
236 goto dev_destroy;
239 return 0;
241 dev_destroy:
242 device_destroy(rnbd_dev_class, MKDEV(0, 0));
243 cls_destroy:
244 class_destroy(rnbd_dev_class);
246 return err;
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);