blob: 20f75708594f6e40d5388acae5071c87853531bc [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 Kornievskaiad408ebe2021-06-08 15:59:18 -040085static void rpc_sysfs_xprt_release(struct kobject *kobj)
86{
87 struct rpc_sysfs_xprt *xprt;
88
89 xprt = container_of(kobj, struct rpc_sysfs_xprt, kobject);
90 kfree(xprt);
91}
92
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -040093static const void *rpc_sysfs_client_namespace(struct kobject *kobj)
94{
95 return container_of(kobj, struct rpc_sysfs_client, kobject)->net;
96}
97
Olga Kornievskaiabaea9942021-06-08 15:59:16 -040098static const void *rpc_sysfs_xprt_switch_namespace(struct kobject *kobj)
99{
100 return container_of(kobj, struct rpc_sysfs_xprt_switch, kobject)->net;
101}
102
Olga Kornievskaiad408ebe2021-06-08 15:59:18 -0400103static const void *rpc_sysfs_xprt_namespace(struct kobject *kobj)
104{
105 return container_of(kobj, struct rpc_sysfs_xprt,
106 kobject)->xprt->xprt_net;
107}
108
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400109static struct kobj_type rpc_sysfs_client_type = {
110 .release = rpc_sysfs_client_release,
111 .sysfs_ops = &kobj_sysfs_ops,
112 .namespace = rpc_sysfs_client_namespace,
113};
114
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400115static struct kobj_type rpc_sysfs_xprt_switch_type = {
116 .release = rpc_sysfs_xprt_switch_release,
117 .sysfs_ops = &kobj_sysfs_ops,
118 .namespace = rpc_sysfs_xprt_switch_namespace,
119};
120
Olga Kornievskaiad408ebe2021-06-08 15:59:18 -0400121static struct kobj_type rpc_sysfs_xprt_type = {
122 .release = rpc_sysfs_xprt_release,
123 .sysfs_ops = &kobj_sysfs_ops,
124 .namespace = rpc_sysfs_xprt_namespace,
125};
126
Olga Kornievskaia74678742021-06-08 15:59:10 -0400127void rpc_sysfs_exit(void)
128{
Olga Kornievskaiac441f122021-06-08 15:59:11 -0400129 kobject_put(rpc_sunrpc_client_kobj);
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400130 kobject_put(rpc_sunrpc_xprt_switch_kobj);
Olga Kornievskaia74678742021-06-08 15:59:10 -0400131 kset_unregister(rpc_sunrpc_kset);
132}
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400133
134static struct rpc_sysfs_client *rpc_sysfs_client_alloc(struct kobject *parent,
135 struct net *net,
136 int clid)
137{
138 struct rpc_sysfs_client *p;
139
140 p = kzalloc(sizeof(*p), GFP_KERNEL);
141 if (p) {
142 p->net = net;
143 p->kobject.kset = rpc_sunrpc_kset;
144 if (kobject_init_and_add(&p->kobject, &rpc_sysfs_client_type,
145 parent, "clnt-%d", clid) == 0)
146 return p;
147 kobject_put(&p->kobject);
148 }
149 return NULL;
150}
151
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400152static struct rpc_sysfs_xprt_switch *
153rpc_sysfs_xprt_switch_alloc(struct kobject *parent,
154 struct rpc_xprt_switch *xprt_switch,
155 struct net *net,
156 gfp_t gfp_flags)
157{
158 struct rpc_sysfs_xprt_switch *p;
159
160 p = kzalloc(sizeof(*p), gfp_flags);
161 if (p) {
162 p->net = net;
163 p->kobject.kset = rpc_sunrpc_kset;
164 if (kobject_init_and_add(&p->kobject,
165 &rpc_sysfs_xprt_switch_type,
166 parent, "switch-%d",
167 xprt_switch->xps_id) == 0)
168 return p;
169 kobject_put(&p->kobject);
170 }
171 return NULL;
172}
173
Olga Kornievskaiad408ebe2021-06-08 15:59:18 -0400174static struct rpc_sysfs_xprt *rpc_sysfs_xprt_alloc(struct kobject *parent,
175 struct rpc_xprt *xprt,
176 gfp_t gfp_flags)
177{
178 struct rpc_sysfs_xprt *p;
179
180 p = kzalloc(sizeof(*p), gfp_flags);
181 if (!p)
182 goto out;
183 p->kobject.kset = rpc_sunrpc_kset;
184 if (kobject_init_and_add(&p->kobject, &rpc_sysfs_xprt_type,
185 parent, "xprt-%d-%s", xprt->id,
186 xprt->address_strings[RPC_DISPLAY_PROTO]) == 0)
187 return p;
188 kobject_put(&p->kobject);
189out:
190 return NULL;
191}
192
Olga Kornievskaia2a338a52021-06-08 15:59:17 -0400193void rpc_sysfs_client_setup(struct rpc_clnt *clnt,
194 struct rpc_xprt_switch *xprt_switch,
195 struct net *net)
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400196{
197 struct rpc_sysfs_client *rpc_client;
198
Olga Kornievskaia2a338a52021-06-08 15:59:17 -0400199 rpc_client = rpc_sysfs_client_alloc(rpc_sunrpc_client_kobj,
200 net, clnt->cl_clid);
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400201 if (rpc_client) {
Olga Kornievskaia2a338a52021-06-08 15:59:17 -0400202 char name[] = "switch";
203 struct rpc_sysfs_xprt_switch *xswitch =
204 (struct rpc_sysfs_xprt_switch *)xprt_switch->xps_sysfs;
205 int ret;
206
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400207 clnt->cl_sysfs = rpc_client;
Olga Kornievskaia2a338a52021-06-08 15:59:17 -0400208 rpc_client->clnt = clnt;
209 rpc_client->xprt_switch = xprt_switch;
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400210 kobject_uevent(&rpc_client->kobject, KOBJ_ADD);
Olga Kornievskaia2a338a52021-06-08 15:59:17 -0400211 ret = sysfs_create_link_nowarn(&rpc_client->kobject,
212 &xswitch->kobject, name);
213 if (ret)
214 pr_warn("can't create link to %s in sysfs (%d)\n",
215 name, ret);
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400216 }
217}
218
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400219void rpc_sysfs_xprt_switch_setup(struct rpc_xprt_switch *xprt_switch,
220 struct rpc_xprt *xprt,
221 gfp_t gfp_flags)
222{
223 struct rpc_sysfs_xprt_switch *rpc_xprt_switch;
224 struct net *net;
225
226 if (xprt_switch->xps_net)
227 net = xprt_switch->xps_net;
228 else
229 net = xprt->xprt_net;
230 rpc_xprt_switch =
231 rpc_sysfs_xprt_switch_alloc(rpc_sunrpc_xprt_switch_kobj,
232 xprt_switch, net, gfp_flags);
233 if (rpc_xprt_switch) {
234 xprt_switch->xps_sysfs = rpc_xprt_switch;
235 rpc_xprt_switch->xprt_switch = xprt_switch;
236 rpc_xprt_switch->xprt = xprt;
237 kobject_uevent(&rpc_xprt_switch->kobject, KOBJ_ADD);
238 }
239}
240
Olga Kornievskaiad408ebe2021-06-08 15:59:18 -0400241void rpc_sysfs_xprt_setup(struct rpc_xprt_switch *xprt_switch,
242 struct rpc_xprt *xprt,
243 gfp_t gfp_flags)
244{
245 struct rpc_sysfs_xprt *rpc_xprt;
246 struct rpc_sysfs_xprt_switch *switch_obj =
247 (struct rpc_sysfs_xprt_switch *)xprt_switch->xps_sysfs;
248
249 rpc_xprt = rpc_sysfs_xprt_alloc(&switch_obj->kobject, xprt, gfp_flags);
250 if (rpc_xprt) {
251 xprt->xprt_sysfs = rpc_xprt;
252 rpc_xprt->xprt = xprt;
253 kobject_uevent(&rpc_xprt->kobject, KOBJ_ADD);
254 }
255}
256
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400257void rpc_sysfs_client_destroy(struct rpc_clnt *clnt)
258{
259 struct rpc_sysfs_client *rpc_client = clnt->cl_sysfs;
260
261 if (rpc_client) {
Olga Kornievskaia2a338a52021-06-08 15:59:17 -0400262 char name[] = "switch";
263
264 sysfs_remove_link(&rpc_client->kobject, name);
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400265 kobject_uevent(&rpc_client->kobject, KOBJ_REMOVE);
266 kobject_del(&rpc_client->kobject);
267 kobject_put(&rpc_client->kobject);
268 clnt->cl_sysfs = NULL;
269 }
270}
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400271
272void rpc_sysfs_xprt_switch_destroy(struct rpc_xprt_switch *xprt_switch)
273{
274 struct rpc_sysfs_xprt_switch *rpc_xprt_switch = xprt_switch->xps_sysfs;
275
276 if (rpc_xprt_switch) {
277 kobject_uevent(&rpc_xprt_switch->kobject, KOBJ_REMOVE);
278 kobject_del(&rpc_xprt_switch->kobject);
279 kobject_put(&rpc_xprt_switch->kobject);
280 xprt_switch->xps_sysfs = NULL;
281 }
282}
Olga Kornievskaiad408ebe2021-06-08 15:59:18 -0400283
284void rpc_sysfs_xprt_destroy(struct rpc_xprt *xprt)
285{
286 struct rpc_sysfs_xprt *rpc_xprt = xprt->xprt_sysfs;
287
288 if (rpc_xprt) {
289 kobject_uevent(&rpc_xprt->kobject, KOBJ_REMOVE);
290 kobject_del(&rpc_xprt->kobject);
291 kobject_put(&rpc_xprt->kobject);
292 xprt->xprt_sysfs = NULL;
293 }
294}