fa03e2ef836af180a5a9f248e69f0b2ff0d8af4e
[linux-block.git] / net / sunrpc / sysfs.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2020 Anna Schumaker <Anna.Schumaker@Netapp.com>
4  */
5 #include <linux/sunrpc/clnt.h>
6 #include <linux/kobject.h>
7
8 static struct kset *rpc_sunrpc_kset;
9 static struct kobject *rpc_sunrpc_client_kobj;
10
11 static void rpc_sysfs_object_release(struct kobject *kobj)
12 {
13         kfree(kobj);
14 }
15
16 static const struct kobj_ns_type_operations *
17 rpc_sysfs_object_child_ns_type(struct kobject *kobj)
18 {
19         return &net_ns_type_operations;
20 }
21
22 static struct kobj_type rpc_sysfs_object_type = {
23         .release = rpc_sysfs_object_release,
24         .sysfs_ops = &kobj_sysfs_ops,
25         .child_ns_type = rpc_sysfs_object_child_ns_type,
26 };
27
28 static struct kobject *rpc_sysfs_object_alloc(const char *name,
29                                               struct kset *kset,
30                                               struct kobject *parent)
31 {
32         struct kobject *kobj;
33
34         kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
35         if (kobj) {
36                 kobj->kset = kset;
37                 if (kobject_init_and_add(kobj, &rpc_sysfs_object_type,
38                                          parent, "%s", name) == 0)
39                         return kobj;
40                 kobject_put(kobj);
41         }
42         return NULL;
43 }
44
45 int rpc_sysfs_init(void)
46 {
47         rpc_sunrpc_kset = kset_create_and_add("sunrpc", NULL, kernel_kobj);
48         if (!rpc_sunrpc_kset)
49                 return -ENOMEM;
50         rpc_sunrpc_client_kobj = rpc_sysfs_object_alloc("client", rpc_sunrpc_kset, NULL);
51         if (!rpc_sunrpc_client_kobj) {
52                 kset_unregister(rpc_sunrpc_kset);
53                 rpc_sunrpc_client_kobj = NULL;
54                 return -ENOMEM;
55         }
56         return 0;
57 }
58
59 void rpc_sysfs_exit(void)
60 {
61         kobject_put(rpc_sunrpc_client_kobj);
62         kset_unregister(rpc_sunrpc_kset);
63 }