1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2020 Anna Schumaker <Anna.Schumaker@Netapp.com>
5 #include <linux/sunrpc/clnt.h>
6 #include <linux/kobject.h>
9 static struct kset *rpc_sunrpc_kset;
10 static struct kobject *rpc_sunrpc_client_kobj;
12 static void rpc_sysfs_object_release(struct kobject *kobj)
17 static const struct kobj_ns_type_operations *
18 rpc_sysfs_object_child_ns_type(struct kobject *kobj)
20 return &net_ns_type_operations;
23 static struct kobj_type rpc_sysfs_object_type = {
24 .release = rpc_sysfs_object_release,
25 .sysfs_ops = &kobj_sysfs_ops,
26 .child_ns_type = rpc_sysfs_object_child_ns_type,
29 static struct kobject *rpc_sysfs_object_alloc(const char *name,
31 struct kobject *parent)
35 kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
38 if (kobject_init_and_add(kobj, &rpc_sysfs_object_type,
39 parent, "%s", name) == 0)
46 int rpc_sysfs_init(void)
48 rpc_sunrpc_kset = kset_create_and_add("sunrpc", NULL, kernel_kobj);
51 rpc_sunrpc_client_kobj = rpc_sysfs_object_alloc("client", rpc_sunrpc_kset, NULL);
52 if (!rpc_sunrpc_client_kobj) {
53 kset_unregister(rpc_sunrpc_kset);
54 rpc_sunrpc_client_kobj = NULL;
60 static void rpc_sysfs_client_release(struct kobject *kobj)
62 struct rpc_sysfs_client *c;
64 c = container_of(kobj, struct rpc_sysfs_client, kobject);
68 static const void *rpc_sysfs_client_namespace(struct kobject *kobj)
70 return container_of(kobj, struct rpc_sysfs_client, kobject)->net;
73 static struct kobj_type rpc_sysfs_client_type = {
74 .release = rpc_sysfs_client_release,
75 .sysfs_ops = &kobj_sysfs_ops,
76 .namespace = rpc_sysfs_client_namespace,
79 void rpc_sysfs_exit(void)
81 kobject_put(rpc_sunrpc_client_kobj);
82 kset_unregister(rpc_sunrpc_kset);
85 static struct rpc_sysfs_client *rpc_sysfs_client_alloc(struct kobject *parent,
89 struct rpc_sysfs_client *p;
91 p = kzalloc(sizeof(*p), GFP_KERNEL);
94 p->kobject.kset = rpc_sunrpc_kset;
95 if (kobject_init_and_add(&p->kobject, &rpc_sysfs_client_type,
96 parent, "clnt-%d", clid) == 0)
98 kobject_put(&p->kobject);
103 void rpc_sysfs_client_setup(struct rpc_clnt *clnt, struct net *net)
105 struct rpc_sysfs_client *rpc_client;
107 rpc_client = rpc_sysfs_client_alloc(rpc_sunrpc_client_kobj, net, clnt->cl_clid);
109 clnt->cl_sysfs = rpc_client;
110 kobject_uevent(&rpc_client->kobject, KOBJ_ADD);
114 void rpc_sysfs_client_destroy(struct rpc_clnt *clnt)
116 struct rpc_sysfs_client *rpc_client = clnt->cl_sysfs;
119 kobject_uevent(&rpc_client->kobject, KOBJ_REMOVE);
120 kobject_del(&rpc_client->kobject);
121 kobject_put(&rpc_client->kobject);
122 clnt->cl_sysfs = NULL;