4962826e96396b97260ed4b0ec5ea9f7d00154c4
[platform/kernel/linux-starfive.git] / drivers / block / rnbd / rnbd-srv-sysfs.c
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 <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>
18
19 #include "rnbd-srv.h"
20
21 static struct device *rnbd_dev;
22 static struct class *rnbd_dev_class;
23 static struct kobject *rnbd_devs_kobj;
24
25 static void rnbd_srv_dev_release(struct kobject *kobj)
26 {
27         struct rnbd_srv_dev *dev;
28
29         dev = container_of(kobj, struct rnbd_srv_dev, dev_kobj);
30
31         kfree(dev);
32 }
33
34 static struct kobj_type dev_ktype = {
35         .sysfs_ops = &kobj_sysfs_ops,
36         .release = rnbd_srv_dev_release
37 };
38
39 int rnbd_srv_create_dev_sysfs(struct rnbd_srv_dev *dev,
40                                struct block_device *bdev)
41 {
42         struct kobject *bdev_kobj;
43         int ret;
44
45         ret = kobject_init_and_add(&dev->dev_kobj, &dev_ktype,
46                                    rnbd_devs_kobj, "%pg", bdev);
47         if (ret) {
48                 kobject_put(&dev->dev_kobj);
49                 return ret;
50         }
51
52         dev->dev_sessions_kobj = kobject_create_and_add("sessions",
53                                                         &dev->dev_kobj);
54         if (!dev->dev_sessions_kobj) {
55                 ret = -ENOMEM;
56                 goto free_dev_kobj;
57         }
58
59         bdev_kobj = &disk_to_dev(bdev->bd_disk)->kobj;
60         ret = sysfs_create_link(&dev->dev_kobj, bdev_kobj, "block_dev");
61         if (ret)
62                 goto put_sess_kobj;
63
64         return 0;
65
66 put_sess_kobj:
67         kobject_put(dev->dev_sessions_kobj);
68 free_dev_kobj:
69         kobject_del(&dev->dev_kobj);
70         kobject_put(&dev->dev_kobj);
71         return ret;
72 }
73
74 void rnbd_srv_destroy_dev_sysfs(struct rnbd_srv_dev *dev)
75 {
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);
81 }
82
83 static ssize_t read_only_show(struct kobject *kobj, struct kobj_attribute *attr,
84                               char *page)
85 {
86         struct rnbd_srv_sess_dev *sess_dev;
87
88         sess_dev = container_of(kobj, struct rnbd_srv_sess_dev, kobj);
89
90         return sysfs_emit(page, "%d\n",
91                           !(sess_dev->open_flags & FMODE_WRITE));
92 }
93
94 static struct kobj_attribute rnbd_srv_dev_session_ro_attr =
95         __ATTR_RO(read_only);
96
97 static ssize_t access_mode_show(struct kobject *kobj,
98                                 struct kobj_attribute *attr,
99                                 char *page)
100 {
101         struct rnbd_srv_sess_dev *sess_dev;
102
103         sess_dev = container_of(kobj, struct rnbd_srv_sess_dev, kobj);
104
105         return sysfs_emit(page, "%s\n",
106                           rnbd_access_modes[sess_dev->access_mode].str);
107 }
108
109 static struct kobj_attribute rnbd_srv_dev_session_access_mode_attr =
110         __ATTR_RO(access_mode);
111
112 static ssize_t mapping_path_show(struct kobject *kobj,
113                                  struct kobj_attribute *attr, char *page)
114 {
115         struct rnbd_srv_sess_dev *sess_dev;
116
117         sess_dev = container_of(kobj, struct rnbd_srv_sess_dev, kobj);
118
119         return sysfs_emit(page, "%s\n", sess_dev->pathname);
120 }
121
122 static struct kobj_attribute rnbd_srv_dev_session_mapping_path_attr =
123         __ATTR_RO(mapping_path);
124
125 static ssize_t rnbd_srv_dev_session_force_close_show(struct kobject *kobj,
126                                         struct kobj_attribute *attr, char *page)
127 {
128         return sysfs_emit(page, "Usage: echo 1 > %s\n",
129                           attr->attr.name);
130 }
131
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)
135 {
136         struct rnbd_srv_sess_dev *sess_dev;
137
138         sess_dev = container_of(kobj, struct rnbd_srv_sess_dev, kobj);
139
140         if (!sysfs_streq(buf, "1")) {
141                 rnbd_srv_err(sess_dev, "%s: invalid value: '%s'\n",
142                               attr->attr.name, buf);
143                 return -EINVAL;
144         }
145
146         rnbd_srv_info(sess_dev, "force close requested\n");
147         rnbd_srv_sess_dev_force_close(sess_dev, attr);
148
149         return count;
150 }
151
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);
156
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,
162         NULL,
163 };
164
165 static struct attribute_group rnbd_srv_default_dev_session_attr_group = {
166         .attrs = rnbd_srv_default_dev_sessions_attrs,
167 };
168
169 void rnbd_srv_destroy_dev_session_sysfs(struct rnbd_srv_sess_dev *sess_dev)
170 {
171         sysfs_remove_group(&sess_dev->kobj,
172                            &rnbd_srv_default_dev_session_attr_group);
173
174         kobject_del(&sess_dev->kobj);
175         kobject_put(&sess_dev->kobj);
176 }
177
178 static void rnbd_srv_sess_dev_release(struct kobject *kobj)
179 {
180         struct rnbd_srv_sess_dev *sess_dev;
181
182         sess_dev = container_of(kobj, struct rnbd_srv_sess_dev, kobj);
183         rnbd_destroy_sess_dev(sess_dev, sess_dev->keep_id);
184 }
185
186 static struct kobj_type rnbd_srv_sess_dev_ktype = {
187         .sysfs_ops      = &kobj_sysfs_ops,
188         .release        = rnbd_srv_sess_dev_release,
189 };
190
191 int rnbd_srv_create_dev_session_sysfs(struct rnbd_srv_sess_dev *sess_dev)
192 {
193         int ret;
194
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);
198         if (ret) {
199                 kobject_put(&sess_dev->kobj);
200                 return ret;
201         }
202
203         ret = sysfs_create_group(&sess_dev->kobj,
204                                  &rnbd_srv_default_dev_session_attr_group);
205         if (ret) {
206                 kobject_del(&sess_dev->kobj);
207                 kobject_put(&sess_dev->kobj);
208         }
209
210         return ret;
211 }
212
213 int rnbd_srv_create_sysfs_files(void)
214 {
215         int err;
216
217         rnbd_dev_class = class_create("rnbd-server");
218         if (IS_ERR(rnbd_dev_class))
219                 return PTR_ERR(rnbd_dev_class);
220
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);
225                 goto cls_destroy;
226         }
227         rnbd_devs_kobj = kobject_create_and_add("devices", &rnbd_dev->kobj);
228         if (!rnbd_devs_kobj) {
229                 err = -ENOMEM;
230                 goto dev_destroy;
231         }
232
233         return 0;
234
235 dev_destroy:
236         device_destroy(rnbd_dev_class, MKDEV(0, 0));
237 cls_destroy:
238         class_destroy(rnbd_dev_class);
239
240         return err;
241 }
242
243 void rnbd_srv_destroy_sysfs_files(void)
244 {
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);
249 }