drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / drivers / block / rnbd / rnbd-srv-sysfs.c
blob64780094442ce72390b3ed3e8f9d38dd3272011b
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 <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>
19 #include "rnbd-srv.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);
33 kfree(dev);
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;
45 int ret;
47 ret = kobject_init_and_add(&dev->dev_kobj, &dev_ktype,
48 rnbd_devs_kobj, "%pg", bdev);
49 if (ret) {
50 kobject_put(&dev->dev_kobj);
51 return ret;
54 dev->dev_sessions_kobj = kobject_create_and_add("sessions",
55 &dev->dev_kobj);
56 if (!dev->dev_sessions_kobj) {
57 ret = -ENOMEM;
58 goto free_dev_kobj;
61 bdev_kobj = &disk_to_dev(bdev->bd_disk)->kobj;
62 ret = sysfs_create_link(&dev->dev_kobj, bdev_kobj, "block_dev");
63 if (ret)
64 goto put_sess_kobj;
66 return 0;
68 put_sess_kobj:
69 kobject_put(dev->dev_sessions_kobj);
70 free_dev_kobj:
71 kobject_del(&dev->dev_kobj);
72 kobject_put(&dev->dev_kobj);
73 return ret;
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,
86 char *page)
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 =
96 __ATTR_RO(read_only);
98 static ssize_t access_mode_show(struct kobject *kobj,
99 struct kobj_attribute *attr,
100 char *page)
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",
130 attr->attr.name);
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);
144 return -EINVAL;
147 rnbd_srv_info(sess_dev, "force close requested\n");
148 rnbd_srv_sess_dev_force_close(sess_dev, attr);
150 return count;
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,
163 NULL,
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)
194 int ret;
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);
199 if (ret) {
200 kobject_put(&sess_dev->kobj);
201 return ret;
204 ret = sysfs_create_group(&sess_dev->kobj,
205 &rnbd_srv_default_dev_session_attr_group);
206 if (ret) {
207 kobject_del(&sess_dev->kobj);
208 kobject_put(&sess_dev->kobj);
211 return ret;
214 int rnbd_srv_create_sysfs_files(void)
216 int err;
218 err = class_register(&rnbd_dev_class);
219 if (err)
220 return err;
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);
226 goto cls_destroy;
228 rnbd_devs_kobj = kobject_create_and_add("devices", &rnbd_dev->kobj);
229 if (!rnbd_devs_kobj) {
230 err = -ENOMEM;
231 goto dev_destroy;
234 return 0;
236 dev_destroy:
237 device_destroy(&rnbd_dev_class, MKDEV(0, 0));
238 cls_destroy:
239 class_unregister(&rnbd_dev_class);
241 return err;
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);