blob: a30ad18aa7dc8ffa5b6989bafce501f3a136970e [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 Kornievskaia587bc722021-06-08 15:59:19 -04007#include <linux/sunrpc/addr.h>
Olga Kornievskaiac1830a62021-06-23 23:28:49 -04008#include <linux/sunrpc/xprtsock.h>
Olga Kornievskaia587bc722021-06-08 15:59:19 -04009
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -040010#include "sysfs.h"
Olga Kornievskaia74678742021-06-08 15:59:10 -040011
Olga Kornievskaia587bc722021-06-08 15:59:19 -040012struct xprt_addr {
13 const char *addr;
14 struct rcu_head rcu;
15};
16
17static void free_xprt_addr(struct rcu_head *head)
18{
19 struct xprt_addr *addr = container_of(head, struct xprt_addr, rcu);
20
21 kfree(addr->addr);
22 kfree(addr);
23}
24
Olga Kornievskaia74678742021-06-08 15:59:10 -040025static struct kset *rpc_sunrpc_kset;
Olga Kornievskaiabaea9942021-06-08 15:59:16 -040026static struct kobject *rpc_sunrpc_client_kobj, *rpc_sunrpc_xprt_switch_kobj;
Olga Kornievskaiac441f122021-06-08 15:59:11 -040027
28static void rpc_sysfs_object_release(struct kobject *kobj)
29{
30 kfree(kobj);
31}
32
33static const struct kobj_ns_type_operations *
34rpc_sysfs_object_child_ns_type(struct kobject *kobj)
35{
36 return &net_ns_type_operations;
37}
38
39static struct kobj_type rpc_sysfs_object_type = {
40 .release = rpc_sysfs_object_release,
41 .sysfs_ops = &kobj_sysfs_ops,
42 .child_ns_type = rpc_sysfs_object_child_ns_type,
43};
44
45static struct kobject *rpc_sysfs_object_alloc(const char *name,
46 struct kset *kset,
47 struct kobject *parent)
48{
49 struct kobject *kobj;
50
51 kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
52 if (kobj) {
53 kobj->kset = kset;
54 if (kobject_init_and_add(kobj, &rpc_sysfs_object_type,
55 parent, "%s", name) == 0)
56 return kobj;
57 kobject_put(kobj);
58 }
59 return NULL;
60}
Olga Kornievskaia74678742021-06-08 15:59:10 -040061
Olga Kornievskaia587bc722021-06-08 15:59:19 -040062static inline struct rpc_xprt *
63rpc_sysfs_xprt_kobj_get_xprt(struct kobject *kobj)
64{
65 struct rpc_sysfs_xprt *x = container_of(kobj,
66 struct rpc_sysfs_xprt, kobject);
67
68 return xprt_get(x->xprt);
69}
70
Olga Kornievskaia0e559032021-06-08 15:59:21 -040071static inline struct rpc_xprt_switch *
Olga Kornievskaia5b7eb782021-06-23 23:28:50 -040072rpc_sysfs_xprt_kobj_get_xprt_switch(struct kobject *kobj)
73{
74 struct rpc_sysfs_xprt *x = container_of(kobj,
75 struct rpc_sysfs_xprt, kobject);
76
77 return xprt_switch_get(x->xprt_switch);
78}
79
80static inline struct rpc_xprt_switch *
Olga Kornievskaia0e559032021-06-08 15:59:21 -040081rpc_sysfs_xprt_switch_kobj_get_xprt(struct kobject *kobj)
82{
83 struct rpc_sysfs_xprt_switch *x = container_of(kobj,
84 struct rpc_sysfs_xprt_switch, kobject);
85
86 return xprt_switch_get(x->xprt_switch);
87}
88
Olga Kornievskaia587bc722021-06-08 15:59:19 -040089static ssize_t rpc_sysfs_xprt_dstaddr_show(struct kobject *kobj,
90 struct kobj_attribute *attr,
91 char *buf)
92{
93 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
94 ssize_t ret;
95
96 if (!xprt)
97 return 0;
98 ret = sprintf(buf, "%s\n", xprt->address_strings[RPC_DISPLAY_ADDR]);
99 xprt_put(xprt);
100 return ret + 1;
101}
102
Olga Kornievskaia4a096512021-06-08 15:59:20 -0400103static ssize_t rpc_sysfs_xprt_info_show(struct kobject *kobj,
104 struct kobj_attribute *attr,
105 char *buf)
106{
107 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
108 ssize_t ret;
109
110 if (!xprt)
111 return 0;
112
113 ret = sprintf(buf, "last_used=%lu\ncur_cong=%lu\ncong_win=%lu\n"
114 "max_num_slots=%u\nmin_num_slots=%u\nnum_reqs=%u\n"
115 "binding_q_len=%u\nsending_q_len=%u\npending_q_len=%u\n"
Olga Kornievskaiac1830a62021-06-23 23:28:49 -0400116 "backlog_q_len=%u\nmain_xprt=%d\nsrc_port=%u\n",
117 xprt->last_used, xprt->cong, xprt->cwnd, xprt->max_reqs,
118 xprt->min_reqs, xprt->num_reqs, xprt->binding.qlen,
119 xprt->sending.qlen, xprt->pending.qlen,
120 xprt->backlog.qlen, xprt->main,
121 (xprt->xprt_class->ident == XPRT_TRANSPORT_TCP) ?
122 get_srcport(xprt) : 0);
Olga Kornievskaia4a096512021-06-08 15:59:20 -0400123 xprt_put(xprt);
124 return ret + 1;
125}
126
Olga Kornievskaia681d5692021-06-08 15:59:22 -0400127static ssize_t rpc_sysfs_xprt_state_show(struct kobject *kobj,
128 struct kobj_attribute *attr,
129 char *buf)
130{
131 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
132 ssize_t ret;
133 int locked, connected, connecting, close_wait, bound, binding,
Olga Kornievskaia5b7eb782021-06-23 23:28:50 -0400134 closing, congested, cwnd_wait, write_space, offline;
Olga Kornievskaia681d5692021-06-08 15:59:22 -0400135
136 if (!xprt)
137 return 0;
138
139 if (!xprt->state) {
140 ret = sprintf(buf, "state=CLOSED\n");
141 } else {
142 locked = test_bit(XPRT_LOCKED, &xprt->state);
143 connected = test_bit(XPRT_CONNECTED, &xprt->state);
144 connecting = test_bit(XPRT_CONNECTING, &xprt->state);
145 close_wait = test_bit(XPRT_CLOSE_WAIT, &xprt->state);
146 bound = test_bit(XPRT_BOUND, &xprt->state);
147 binding = test_bit(XPRT_BINDING, &xprt->state);
148 closing = test_bit(XPRT_CLOSING, &xprt->state);
149 congested = test_bit(XPRT_CONGESTED, &xprt->state);
150 cwnd_wait = test_bit(XPRT_CWND_WAIT, &xprt->state);
151 write_space = test_bit(XPRT_WRITE_SPACE, &xprt->state);
Olga Kornievskaia5b7eb782021-06-23 23:28:50 -0400152 offline = test_bit(XPRT_OFFLINE, &xprt->state);
Olga Kornievskaia681d5692021-06-08 15:59:22 -0400153
Olga Kornievskaia5b7eb782021-06-23 23:28:50 -0400154 ret = sprintf(buf, "state=%s %s %s %s %s %s %s %s %s %s %s\n",
Olga Kornievskaia681d5692021-06-08 15:59:22 -0400155 locked ? "LOCKED" : "",
156 connected ? "CONNECTED" : "",
157 connecting ? "CONNECTING" : "",
158 close_wait ? "CLOSE_WAIT" : "",
159 bound ? "BOUND" : "",
160 binding ? "BOUNDING" : "",
161 closing ? "CLOSING" : "",
162 congested ? "CONGESTED" : "",
163 cwnd_wait ? "CWND_WAIT" : "",
Olga Kornievskaia5b7eb782021-06-23 23:28:50 -0400164 write_space ? "WRITE_SPACE" : "",
165 offline ? "OFFLINE" : "");
Olga Kornievskaia681d5692021-06-08 15:59:22 -0400166 }
167
168 xprt_put(xprt);
169 return ret + 1;
170}
171
Olga Kornievskaia0e559032021-06-08 15:59:21 -0400172static ssize_t rpc_sysfs_xprt_switch_info_show(struct kobject *kobj,
173 struct kobj_attribute *attr,
174 char *buf)
175{
176 struct rpc_xprt_switch *xprt_switch =
177 rpc_sysfs_xprt_switch_kobj_get_xprt(kobj);
178 ssize_t ret;
179
180 if (!xprt_switch)
181 return 0;
182 ret = sprintf(buf, "num_xprts=%u\nnum_active=%u\nqueue_len=%ld\n",
183 xprt_switch->xps_nxprts, xprt_switch->xps_nactive,
184 atomic_long_read(&xprt_switch->xps_queuelen));
185 xprt_switch_put(xprt_switch);
186 return ret + 1;
187}
188
Olga Kornievskaia587bc722021-06-08 15:59:19 -0400189static ssize_t rpc_sysfs_xprt_dstaddr_store(struct kobject *kobj,
190 struct kobj_attribute *attr,
191 const char *buf, size_t count)
192{
193 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
194 struct sockaddr *saddr;
195 char *dst_addr;
196 int port;
197 struct xprt_addr *saved_addr;
198 size_t buf_len;
199
200 if (!xprt)
201 return 0;
202 if (!(xprt->xprt_class->ident == XPRT_TRANSPORT_TCP ||
203 xprt->xprt_class->ident == XPRT_TRANSPORT_RDMA)) {
204 xprt_put(xprt);
205 return -EOPNOTSUPP;
206 }
207
208 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) {
209 count = -EINTR;
210 goto out_put;
211 }
212 saddr = (struct sockaddr *)&xprt->addr;
213 port = rpc_get_port(saddr);
214
215 /* buf_len is the len until the first occurence of either
216 * '\n' or '\0'
217 */
218 buf_len = strcspn(buf, "\n");
219
220 dst_addr = kstrndup(buf, buf_len, GFP_KERNEL);
221 if (!dst_addr)
222 goto out_err;
223 saved_addr = kzalloc(sizeof(*saved_addr), GFP_KERNEL);
224 if (!saved_addr)
225 goto out_err_free;
226 saved_addr->addr =
227 rcu_dereference_raw(xprt->address_strings[RPC_DISPLAY_ADDR]);
228 rcu_assign_pointer(xprt->address_strings[RPC_DISPLAY_ADDR], dst_addr);
229 call_rcu(&saved_addr->rcu, free_xprt_addr);
230 xprt->addrlen = rpc_pton(xprt->xprt_net, buf, buf_len, saddr,
231 sizeof(*saddr));
232 rpc_set_port(saddr, port);
233
234 xprt_force_disconnect(xprt);
235out:
236 xprt_release_write(xprt, NULL);
237out_put:
238 xprt_put(xprt);
239 return count;
240out_err_free:
241 kfree(dst_addr);
242out_err:
243 count = -ENOMEM;
244 goto out;
245}
246
Olga Kornievskaia5b7eb782021-06-23 23:28:50 -0400247static ssize_t rpc_sysfs_xprt_state_change(struct kobject *kobj,
248 struct kobj_attribute *attr,
249 const char *buf, size_t count)
250{
251 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
252 int offline = 0, online = 0;
253 struct rpc_xprt_switch *xps = rpc_sysfs_xprt_kobj_get_xprt_switch(kobj);
254
255 if (!xprt)
256 return 0;
257
258 if (!strncmp(buf, "offline", 7))
259 offline = 1;
260 else if (!strncmp(buf, "online", 6))
261 online = 1;
262 else
263 return -EINVAL;
264
265 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) {
266 count = -EINTR;
267 goto out_put;
268 }
269 if (xprt->main) {
270 count = -EINVAL;
271 goto release_tasks;
272 }
273 if (offline) {
274 set_bit(XPRT_OFFLINE, &xprt->state);
275 spin_lock(&xps->xps_lock);
276 xps->xps_nactive--;
277 spin_unlock(&xps->xps_lock);
278 } else if (online) {
279 clear_bit(XPRT_OFFLINE, &xprt->state);
280 spin_lock(&xps->xps_lock);
281 xps->xps_nactive++;
282 spin_unlock(&xps->xps_lock);
283 }
284
285release_tasks:
286 xprt_release_write(xprt, NULL);
287out_put:
288 xprt_put(xprt);
289 xprt_switch_put(xps);
290 return count;
291}
292
Olga Kornievskaia74678742021-06-08 15:59:10 -0400293int rpc_sysfs_init(void)
294{
295 rpc_sunrpc_kset = kset_create_and_add("sunrpc", NULL, kernel_kobj);
296 if (!rpc_sunrpc_kset)
297 return -ENOMEM;
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400298 rpc_sunrpc_client_kobj =
299 rpc_sysfs_object_alloc("rpc-clients", rpc_sunrpc_kset, NULL);
300 if (!rpc_sunrpc_client_kobj)
301 goto err_client;
302 rpc_sunrpc_xprt_switch_kobj =
303 rpc_sysfs_object_alloc("xprt-switches", rpc_sunrpc_kset, NULL);
304 if (!rpc_sunrpc_xprt_switch_kobj)
305 goto err_switch;
Olga Kornievskaia74678742021-06-08 15:59:10 -0400306 return 0;
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400307err_switch:
308 kobject_put(rpc_sunrpc_client_kobj);
309 rpc_sunrpc_client_kobj = NULL;
310err_client:
311 kset_unregister(rpc_sunrpc_kset);
312 rpc_sunrpc_kset = NULL;
313 return -ENOMEM;
Olga Kornievskaia74678742021-06-08 15:59:10 -0400314}
315
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400316static void rpc_sysfs_client_release(struct kobject *kobj)
317{
318 struct rpc_sysfs_client *c;
319
320 c = container_of(kobj, struct rpc_sysfs_client, kobject);
321 kfree(c);
322}
323
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400324static void rpc_sysfs_xprt_switch_release(struct kobject *kobj)
325{
326 struct rpc_sysfs_xprt_switch *xprt_switch;
327
328 xprt_switch = container_of(kobj, struct rpc_sysfs_xprt_switch, kobject);
329 kfree(xprt_switch);
330}
331
Olga Kornievskaiad408ebe2021-06-08 15:59:18 -0400332static void rpc_sysfs_xprt_release(struct kobject *kobj)
333{
334 struct rpc_sysfs_xprt *xprt;
335
336 xprt = container_of(kobj, struct rpc_sysfs_xprt, kobject);
337 kfree(xprt);
338}
339
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400340static const void *rpc_sysfs_client_namespace(struct kobject *kobj)
341{
342 return container_of(kobj, struct rpc_sysfs_client, kobject)->net;
343}
344
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400345static const void *rpc_sysfs_xprt_switch_namespace(struct kobject *kobj)
346{
347 return container_of(kobj, struct rpc_sysfs_xprt_switch, kobject)->net;
348}
349
Olga Kornievskaiad408ebe2021-06-08 15:59:18 -0400350static const void *rpc_sysfs_xprt_namespace(struct kobject *kobj)
351{
352 return container_of(kobj, struct rpc_sysfs_xprt,
353 kobject)->xprt->xprt_net;
354}
355
Olga Kornievskaia587bc722021-06-08 15:59:19 -0400356static struct kobj_attribute rpc_sysfs_xprt_dstaddr = __ATTR(dstaddr,
357 0644, rpc_sysfs_xprt_dstaddr_show, rpc_sysfs_xprt_dstaddr_store);
358
Olga Kornievskaia4a096512021-06-08 15:59:20 -0400359static struct kobj_attribute rpc_sysfs_xprt_info = __ATTR(xprt_info,
360 0444, rpc_sysfs_xprt_info_show, NULL);
361
Olga Kornievskaia681d5692021-06-08 15:59:22 -0400362static struct kobj_attribute rpc_sysfs_xprt_change_state = __ATTR(xprt_state,
Olga Kornievskaia5b7eb782021-06-23 23:28:50 -0400363 0644, rpc_sysfs_xprt_state_show, rpc_sysfs_xprt_state_change);
Olga Kornievskaia681d5692021-06-08 15:59:22 -0400364
Olga Kornievskaia587bc722021-06-08 15:59:19 -0400365static struct attribute *rpc_sysfs_xprt_attrs[] = {
366 &rpc_sysfs_xprt_dstaddr.attr,
Olga Kornievskaia4a096512021-06-08 15:59:20 -0400367 &rpc_sysfs_xprt_info.attr,
Olga Kornievskaia681d5692021-06-08 15:59:22 -0400368 &rpc_sysfs_xprt_change_state.attr,
Olga Kornievskaia587bc722021-06-08 15:59:19 -0400369 NULL,
370};
371
Olga Kornievskaia0e559032021-06-08 15:59:21 -0400372static struct kobj_attribute rpc_sysfs_xprt_switch_info =
373 __ATTR(xprt_switch_info, 0444, rpc_sysfs_xprt_switch_info_show, NULL);
374
375static struct attribute *rpc_sysfs_xprt_switch_attrs[] = {
376 &rpc_sysfs_xprt_switch_info.attr,
377 NULL,
378};
379
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400380static struct kobj_type rpc_sysfs_client_type = {
381 .release = rpc_sysfs_client_release,
382 .sysfs_ops = &kobj_sysfs_ops,
383 .namespace = rpc_sysfs_client_namespace,
384};
385
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400386static struct kobj_type rpc_sysfs_xprt_switch_type = {
387 .release = rpc_sysfs_xprt_switch_release,
Olga Kornievskaia0e559032021-06-08 15:59:21 -0400388 .default_attrs = rpc_sysfs_xprt_switch_attrs,
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400389 .sysfs_ops = &kobj_sysfs_ops,
390 .namespace = rpc_sysfs_xprt_switch_namespace,
391};
392
Olga Kornievskaiad408ebe2021-06-08 15:59:18 -0400393static struct kobj_type rpc_sysfs_xprt_type = {
394 .release = rpc_sysfs_xprt_release,
Olga Kornievskaia587bc722021-06-08 15:59:19 -0400395 .default_attrs = rpc_sysfs_xprt_attrs,
Olga Kornievskaiad408ebe2021-06-08 15:59:18 -0400396 .sysfs_ops = &kobj_sysfs_ops,
397 .namespace = rpc_sysfs_xprt_namespace,
398};
399
Olga Kornievskaia74678742021-06-08 15:59:10 -0400400void rpc_sysfs_exit(void)
401{
Olga Kornievskaiac441f122021-06-08 15:59:11 -0400402 kobject_put(rpc_sunrpc_client_kobj);
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400403 kobject_put(rpc_sunrpc_xprt_switch_kobj);
Olga Kornievskaia74678742021-06-08 15:59:10 -0400404 kset_unregister(rpc_sunrpc_kset);
405}
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400406
407static struct rpc_sysfs_client *rpc_sysfs_client_alloc(struct kobject *parent,
408 struct net *net,
409 int clid)
410{
411 struct rpc_sysfs_client *p;
412
413 p = kzalloc(sizeof(*p), GFP_KERNEL);
414 if (p) {
415 p->net = net;
416 p->kobject.kset = rpc_sunrpc_kset;
417 if (kobject_init_and_add(&p->kobject, &rpc_sysfs_client_type,
418 parent, "clnt-%d", clid) == 0)
419 return p;
420 kobject_put(&p->kobject);
421 }
422 return NULL;
423}
424
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400425static struct rpc_sysfs_xprt_switch *
426rpc_sysfs_xprt_switch_alloc(struct kobject *parent,
427 struct rpc_xprt_switch *xprt_switch,
428 struct net *net,
429 gfp_t gfp_flags)
430{
431 struct rpc_sysfs_xprt_switch *p;
432
433 p = kzalloc(sizeof(*p), gfp_flags);
434 if (p) {
435 p->net = net;
436 p->kobject.kset = rpc_sunrpc_kset;
437 if (kobject_init_and_add(&p->kobject,
438 &rpc_sysfs_xprt_switch_type,
439 parent, "switch-%d",
440 xprt_switch->xps_id) == 0)
441 return p;
442 kobject_put(&p->kobject);
443 }
444 return NULL;
445}
446
Olga Kornievskaiad408ebe2021-06-08 15:59:18 -0400447static struct rpc_sysfs_xprt *rpc_sysfs_xprt_alloc(struct kobject *parent,
448 struct rpc_xprt *xprt,
449 gfp_t gfp_flags)
450{
451 struct rpc_sysfs_xprt *p;
452
453 p = kzalloc(sizeof(*p), gfp_flags);
454 if (!p)
455 goto out;
456 p->kobject.kset = rpc_sunrpc_kset;
457 if (kobject_init_and_add(&p->kobject, &rpc_sysfs_xprt_type,
458 parent, "xprt-%d-%s", xprt->id,
459 xprt->address_strings[RPC_DISPLAY_PROTO]) == 0)
460 return p;
461 kobject_put(&p->kobject);
462out:
463 return NULL;
464}
465
Olga Kornievskaia2a338a52021-06-08 15:59:17 -0400466void rpc_sysfs_client_setup(struct rpc_clnt *clnt,
467 struct rpc_xprt_switch *xprt_switch,
468 struct net *net)
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400469{
470 struct rpc_sysfs_client *rpc_client;
471
Olga Kornievskaia2a338a52021-06-08 15:59:17 -0400472 rpc_client = rpc_sysfs_client_alloc(rpc_sunrpc_client_kobj,
473 net, clnt->cl_clid);
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400474 if (rpc_client) {
Olga Kornievskaia2a338a52021-06-08 15:59:17 -0400475 char name[] = "switch";
476 struct rpc_sysfs_xprt_switch *xswitch =
477 (struct rpc_sysfs_xprt_switch *)xprt_switch->xps_sysfs;
478 int ret;
479
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400480 clnt->cl_sysfs = rpc_client;
Olga Kornievskaia2a338a52021-06-08 15:59:17 -0400481 rpc_client->clnt = clnt;
482 rpc_client->xprt_switch = xprt_switch;
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400483 kobject_uevent(&rpc_client->kobject, KOBJ_ADD);
Olga Kornievskaia2a338a52021-06-08 15:59:17 -0400484 ret = sysfs_create_link_nowarn(&rpc_client->kobject,
485 &xswitch->kobject, name);
486 if (ret)
487 pr_warn("can't create link to %s in sysfs (%d)\n",
488 name, ret);
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400489 }
490}
491
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400492void rpc_sysfs_xprt_switch_setup(struct rpc_xprt_switch *xprt_switch,
493 struct rpc_xprt *xprt,
494 gfp_t gfp_flags)
495{
496 struct rpc_sysfs_xprt_switch *rpc_xprt_switch;
497 struct net *net;
498
499 if (xprt_switch->xps_net)
500 net = xprt_switch->xps_net;
501 else
502 net = xprt->xprt_net;
503 rpc_xprt_switch =
504 rpc_sysfs_xprt_switch_alloc(rpc_sunrpc_xprt_switch_kobj,
505 xprt_switch, net, gfp_flags);
506 if (rpc_xprt_switch) {
507 xprt_switch->xps_sysfs = rpc_xprt_switch;
508 rpc_xprt_switch->xprt_switch = xprt_switch;
509 rpc_xprt_switch->xprt = xprt;
510 kobject_uevent(&rpc_xprt_switch->kobject, KOBJ_ADD);
511 }
512}
513
Olga Kornievskaiad408ebe2021-06-08 15:59:18 -0400514void rpc_sysfs_xprt_setup(struct rpc_xprt_switch *xprt_switch,
515 struct rpc_xprt *xprt,
516 gfp_t gfp_flags)
517{
518 struct rpc_sysfs_xprt *rpc_xprt;
519 struct rpc_sysfs_xprt_switch *switch_obj =
520 (struct rpc_sysfs_xprt_switch *)xprt_switch->xps_sysfs;
521
522 rpc_xprt = rpc_sysfs_xprt_alloc(&switch_obj->kobject, xprt, gfp_flags);
523 if (rpc_xprt) {
524 xprt->xprt_sysfs = rpc_xprt;
525 rpc_xprt->xprt = xprt;
Olga Kornievskaia5b7eb782021-06-23 23:28:50 -0400526 rpc_xprt->xprt_switch = xprt_switch;
Olga Kornievskaiad408ebe2021-06-08 15:59:18 -0400527 kobject_uevent(&rpc_xprt->kobject, KOBJ_ADD);
528 }
529}
530
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400531void rpc_sysfs_client_destroy(struct rpc_clnt *clnt)
532{
533 struct rpc_sysfs_client *rpc_client = clnt->cl_sysfs;
534
535 if (rpc_client) {
Olga Kornievskaia2a338a52021-06-08 15:59:17 -0400536 char name[] = "switch";
537
538 sysfs_remove_link(&rpc_client->kobject, name);
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400539 kobject_uevent(&rpc_client->kobject, KOBJ_REMOVE);
540 kobject_del(&rpc_client->kobject);
541 kobject_put(&rpc_client->kobject);
542 clnt->cl_sysfs = NULL;
543 }
544}
Olga Kornievskaiabaea9942021-06-08 15:59:16 -0400545
546void rpc_sysfs_xprt_switch_destroy(struct rpc_xprt_switch *xprt_switch)
547{
548 struct rpc_sysfs_xprt_switch *rpc_xprt_switch = xprt_switch->xps_sysfs;
549
550 if (rpc_xprt_switch) {
551 kobject_uevent(&rpc_xprt_switch->kobject, KOBJ_REMOVE);
552 kobject_del(&rpc_xprt_switch->kobject);
553 kobject_put(&rpc_xprt_switch->kobject);
554 xprt_switch->xps_sysfs = NULL;
555 }
556}
Olga Kornievskaiad408ebe2021-06-08 15:59:18 -0400557
558void rpc_sysfs_xprt_destroy(struct rpc_xprt *xprt)
559{
560 struct rpc_sysfs_xprt *rpc_xprt = xprt->xprt_sysfs;
561
562 if (rpc_xprt) {
563 kobject_uevent(&rpc_xprt->kobject, KOBJ_REMOVE);
564 kobject_del(&rpc_xprt->kobject);
565 kobject_put(&rpc_xprt->kobject);
566 xprt->xprt_sysfs = NULL;
567 }
568}