blob: ed9f7131543f9156c5f16318ba8cc133f07ebea4 [file] [log] [blame]
Olga Kornievskaia74678742021-06-08 15:59:10 -04001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2020 Anna Schumaker <Anna.Schumaker@Netapp.com>
4 */
Olga Kornievskaiac441f122021-06-08 15:59:11 -04005#include <linux/sunrpc/clnt.h>
Olga Kornievskaia74678742021-06-08 15:59:10 -04006#include <linux/kobject.h>
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -04007#include "sysfs.h"
Olga Kornievskaia74678742021-06-08 15:59:10 -04008
9static struct kset *rpc_sunrpc_kset;
Olga Kornievskaiabaea9942021-06-08 15:59:16 -040010static struct kobject *rpc_sunrpc_client_kobj, *rpc_sunrpc_xprt_switch_kobj;
Olga Kornievskaiac441f122021-06-08 15:59:11 -040011
12static void rpc_sysfs_object_release(struct kobject *kobj)
13{
14 kfree(kobj);
15}
16
17static const struct kobj_ns_type_operations *
18rpc_sysfs_object_child_ns_type(struct kobject *kobj)
19{
20 return &net_ns_type_operations;
21}
22
23static 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,
27};
28
29static struct kobject *rpc_sysfs_object_alloc(const char *name,
30 struct kset *kset,
31 struct kobject *parent)
32{
33 struct kobject *kobj;
34
35 kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
36 if (kobj) {
37 kobj->kset = kset;
38 if (kobject_init_and_add(kobj, &rpc_sysfs_object_type,
39 parent, "%s", name) == 0)
40 return kobj;
41 kobject_put(kobj);
42 }
43 return NULL;
44}
Olga Kornievskaia74678742021-06-08 15:59:10 -040045
46int rpc_sysfs_init(void)
47{
48 rpc_sunrpc_kset = kset_create_and_add("sunrpc", NULL, kernel_kobj);
49 if (!rpc_sunrpc_kset)
50 return -ENOMEM;
Olga Kornievskaiabaea9942021-06-08 15:59:16 -040051 rpc_sunrpc_client_kobj =
52 rpc_sysfs_object_alloc("rpc-clients", rpc_sunrpc_kset, NULL);
53 if (!rpc_sunrpc_client_kobj)
54 goto err_client;
55 rpc_sunrpc_xprt_switch_kobj =
56 rpc_sysfs_object_alloc("xprt-switches", rpc_sunrpc_kset, NULL);
57 if (!rpc_sunrpc_xprt_switch_kobj)
58 goto err_switch;
Olga Kornievskaia74678742021-06-08 15:59:10 -040059 return 0;
Olga Kornievskaiabaea9942021-06-08 15:59:16 -040060err_switch:
61 kobject_put(rpc_sunrpc_client_kobj);
62 rpc_sunrpc_client_kobj = NULL;
63err_client:
64 kset_unregister(rpc_sunrpc_kset);
65 rpc_sunrpc_kset = NULL;
66 return -ENOMEM;
Olga Kornievskaia74678742021-06-08 15:59:10 -040067}
68
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -040069static void rpc_sysfs_client_release(struct kobject *kobj)
70{
71 struct rpc_sysfs_client *c;
72
73 c = container_of(kobj, struct rpc_sysfs_client, kobject);
74 kfree(c);
75}
76
Olga Kornievskaiabaea9942021-06-08 15:59:16 -040077static void rpc_sysfs_xprt_switch_release(struct kobject *kobj)
78{
79 struct rpc_sysfs_xprt_switch *xprt_switch;
80
81 xprt_switch = container_of(kobj, struct rpc_sysfs_xprt_switch, kobject);
82 kfree(xprt_switch);
83}
84
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -040085static const void *rpc_sysfs_client_namespace(struct kobject *kobj)
86{
87 return container_of(kobj, struct rpc_sysfs_client, kobject)->net;
88}
89
Olga Kornievskaiabaea9942021-06-08 15:59:16 -040090static const void *rpc_sysfs_xprt_switch_namespace(struct kobject *kobj)
91{
92 return container_of(kobj, struct rpc_sysfs_xprt_switch, kobject)->net;
93}
94
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -040095static struct kobj_type rpc_sysfs_client_type = {
96 .release = rpc_sysfs_client_release,
97 .sysfs_ops = &kobj_sysfs_ops,
98 .namespace = rpc_sysfs_client_namespace,
99};
100
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400101static struct kobj_type rpc_sysfs_xprt_switch_type = {
102 .release = rpc_sysfs_xprt_switch_release,
103 .sysfs_ops = &kobj_sysfs_ops,
104 .namespace = rpc_sysfs_xprt_switch_namespace,
105};
106
Olga Kornievskaia74678742021-06-08 15:59:10 -0400107void rpc_sysfs_exit(void)
108{
Olga Kornievskaiac441f122021-06-08 15:59:11 -0400109 kobject_put(rpc_sunrpc_client_kobj);
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400110 kobject_put(rpc_sunrpc_xprt_switch_kobj);
Olga Kornievskaia74678742021-06-08 15:59:10 -0400111 kset_unregister(rpc_sunrpc_kset);
112}
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400113
114static struct rpc_sysfs_client *rpc_sysfs_client_alloc(struct kobject *parent,
115 struct net *net,
116 int clid)
117{
118 struct rpc_sysfs_client *p;
119
120 p = kzalloc(sizeof(*p), GFP_KERNEL);
121 if (p) {
122 p->net = net;
123 p->kobject.kset = rpc_sunrpc_kset;
124 if (kobject_init_and_add(&p->kobject, &rpc_sysfs_client_type,
125 parent, "clnt-%d", clid) == 0)
126 return p;
127 kobject_put(&p->kobject);
128 }
129 return NULL;
130}
131
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400132static struct rpc_sysfs_xprt_switch *
133rpc_sysfs_xprt_switch_alloc(struct kobject *parent,
134 struct rpc_xprt_switch *xprt_switch,
135 struct net *net,
136 gfp_t gfp_flags)
137{
138 struct rpc_sysfs_xprt_switch *p;
139
140 p = kzalloc(sizeof(*p), gfp_flags);
141 if (p) {
142 p->net = net;
143 p->kobject.kset = rpc_sunrpc_kset;
144 if (kobject_init_and_add(&p->kobject,
145 &rpc_sysfs_xprt_switch_type,
146 parent, "switch-%d",
147 xprt_switch->xps_id) == 0)
148 return p;
149 kobject_put(&p->kobject);
150 }
151 return NULL;
152}
153
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400154void rpc_sysfs_client_setup(struct rpc_clnt *clnt, struct net *net)
155{
156 struct rpc_sysfs_client *rpc_client;
157
158 rpc_client = rpc_sysfs_client_alloc(rpc_sunrpc_client_kobj, net, clnt->cl_clid);
159 if (rpc_client) {
160 clnt->cl_sysfs = rpc_client;
161 kobject_uevent(&rpc_client->kobject, KOBJ_ADD);
162 }
163}
164
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400165void rpc_sysfs_xprt_switch_setup(struct rpc_xprt_switch *xprt_switch,
166 struct rpc_xprt *xprt,
167 gfp_t gfp_flags)
168{
169 struct rpc_sysfs_xprt_switch *rpc_xprt_switch;
170 struct net *net;
171
172 if (xprt_switch->xps_net)
173 net = xprt_switch->xps_net;
174 else
175 net = xprt->xprt_net;
176 rpc_xprt_switch =
177 rpc_sysfs_xprt_switch_alloc(rpc_sunrpc_xprt_switch_kobj,
178 xprt_switch, net, gfp_flags);
179 if (rpc_xprt_switch) {
180 xprt_switch->xps_sysfs = rpc_xprt_switch;
181 rpc_xprt_switch->xprt_switch = xprt_switch;
182 rpc_xprt_switch->xprt = xprt;
183 kobject_uevent(&rpc_xprt_switch->kobject, KOBJ_ADD);
184 }
185}
186
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400187void rpc_sysfs_client_destroy(struct rpc_clnt *clnt)
188{
189 struct rpc_sysfs_client *rpc_client = clnt->cl_sysfs;
190
191 if (rpc_client) {
192 kobject_uevent(&rpc_client->kobject, KOBJ_REMOVE);
193 kobject_del(&rpc_client->kobject);
194 kobject_put(&rpc_client->kobject);
195 clnt->cl_sysfs = NULL;
196 }
197}
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400198
199void rpc_sysfs_xprt_switch_destroy(struct rpc_xprt_switch *xprt_switch)
200{
201 struct rpc_sysfs_xprt_switch *rpc_xprt_switch = xprt_switch->xps_sysfs;
202
203 if (rpc_xprt_switch) {
204 kobject_uevent(&rpc_xprt_switch->kobject, KOBJ_REMOVE);
205 kobject_del(&rpc_xprt_switch->kobject);
206 kobject_put(&rpc_xprt_switch->kobject);
207 xprt_switch->xps_sysfs = NULL;
208 }
209}