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 #include <linux/sunrpc/addr.h>
8 #include <linux/sunrpc/xprtsock.h>
9
10 #include "sysfs.h"
11
12 struct xprt_addr {
13 const char *addr;
14 struct rcu_head rcu;
15 };
16
free_xprt_addr(struct rcu_head * head)17 static 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
25 static struct kset *rpc_sunrpc_kset;
26 static struct kobject *rpc_sunrpc_client_kobj, *rpc_sunrpc_xprt_switch_kobj;
27
rpc_sysfs_object_release(struct kobject * kobj)28 static void rpc_sysfs_object_release(struct kobject *kobj)
29 {
30 kfree(kobj);
31 }
32
33 static const struct kobj_ns_type_operations *
rpc_sysfs_object_child_ns_type(struct kobject * kobj)34 rpc_sysfs_object_child_ns_type(struct kobject *kobj)
35 {
36 return &net_ns_type_operations;
37 }
38
39 static 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
rpc_sysfs_object_alloc(const char * name,struct kset * kset,struct kobject * parent)45 static 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 }
61
62 static inline struct rpc_xprt *
rpc_sysfs_xprt_kobj_get_xprt(struct kobject * kobj)63 rpc_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
71 static inline struct rpc_xprt_switch *
rpc_sysfs_xprt_kobj_get_xprt_switch(struct kobject * kobj)72 rpc_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
80 static inline struct rpc_xprt_switch *
rpc_sysfs_xprt_switch_kobj_get_xprt(struct kobject * kobj)81 rpc_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
rpc_sysfs_xprt_dstaddr_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)89 static 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
rpc_sysfs_xprt_srcaddr_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)103 static ssize_t rpc_sysfs_xprt_srcaddr_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 struct sockaddr_storage saddr;
109 struct sock_xprt *sock;
110 ssize_t ret = -1;
111
112 if (!xprt)
113 return 0;
114
115 sock = container_of(xprt, struct sock_xprt, xprt);
116 if (kernel_getsockname(sock->sock, (struct sockaddr *)&saddr) < 0)
117 goto out;
118
119 ret = sprintf(buf, "%pISc\n", &saddr);
120 out:
121 xprt_put(xprt);
122 return ret + 1;
123 }
124
rpc_sysfs_xprt_info_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)125 static ssize_t rpc_sysfs_xprt_info_show(struct kobject *kobj,
126 struct kobj_attribute *attr,
127 char *buf)
128 {
129 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
130 ssize_t ret;
131
132 if (!xprt)
133 return 0;
134
135 ret = sprintf(buf, "last_used=%lu\ncur_cong=%lu\ncong_win=%lu\n"
136 "max_num_slots=%u\nmin_num_slots=%u\nnum_reqs=%u\n"
137 "binding_q_len=%u\nsending_q_len=%u\npending_q_len=%u\n"
138 "backlog_q_len=%u\nmain_xprt=%d\nsrc_port=%u\n"
139 "tasks_queuelen=%ld\ndst_port=%s\n",
140 xprt->last_used, xprt->cong, xprt->cwnd, xprt->max_reqs,
141 xprt->min_reqs, xprt->num_reqs, xprt->binding.qlen,
142 xprt->sending.qlen, xprt->pending.qlen,
143 xprt->backlog.qlen, xprt->main,
144 (xprt->xprt_class->ident == XPRT_TRANSPORT_TCP) ?
145 get_srcport(xprt) : 0,
146 atomic_long_read(&xprt->queuelen),
147 (xprt->xprt_class->ident == XPRT_TRANSPORT_TCP) ?
148 xprt->address_strings[RPC_DISPLAY_PORT] : "0");
149 xprt_put(xprt);
150 return ret + 1;
151 }
152
rpc_sysfs_xprt_state_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)153 static ssize_t rpc_sysfs_xprt_state_show(struct kobject *kobj,
154 struct kobj_attribute *attr,
155 char *buf)
156 {
157 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
158 ssize_t ret;
159 int locked, connected, connecting, close_wait, bound, binding,
160 closing, congested, cwnd_wait, write_space, offline, remove;
161
162 if (!xprt)
163 return 0;
164
165 if (!xprt->state) {
166 ret = sprintf(buf, "state=CLOSED\n");
167 } else {
168 locked = test_bit(XPRT_LOCKED, &xprt->state);
169 connected = test_bit(XPRT_CONNECTED, &xprt->state);
170 connecting = test_bit(XPRT_CONNECTING, &xprt->state);
171 close_wait = test_bit(XPRT_CLOSE_WAIT, &xprt->state);
172 bound = test_bit(XPRT_BOUND, &xprt->state);
173 binding = test_bit(XPRT_BINDING, &xprt->state);
174 closing = test_bit(XPRT_CLOSING, &xprt->state);
175 congested = test_bit(XPRT_CONGESTED, &xprt->state);
176 cwnd_wait = test_bit(XPRT_CWND_WAIT, &xprt->state);
177 write_space = test_bit(XPRT_WRITE_SPACE, &xprt->state);
178 offline = test_bit(XPRT_OFFLINE, &xprt->state);
179 remove = test_bit(XPRT_REMOVE, &xprt->state);
180
181 ret = sprintf(buf, "state=%s %s %s %s %s %s %s %s %s %s %s %s\n",
182 locked ? "LOCKED" : "",
183 connected ? "CONNECTED" : "",
184 connecting ? "CONNECTING" : "",
185 close_wait ? "CLOSE_WAIT" : "",
186 bound ? "BOUND" : "",
187 binding ? "BOUNDING" : "",
188 closing ? "CLOSING" : "",
189 congested ? "CONGESTED" : "",
190 cwnd_wait ? "CWND_WAIT" : "",
191 write_space ? "WRITE_SPACE" : "",
192 offline ? "OFFLINE" : "",
193 remove ? "REMOVE" : "");
194 }
195
196 xprt_put(xprt);
197 return ret + 1;
198 }
199
rpc_sysfs_xprt_switch_info_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)200 static ssize_t rpc_sysfs_xprt_switch_info_show(struct kobject *kobj,
201 struct kobj_attribute *attr,
202 char *buf)
203 {
204 struct rpc_xprt_switch *xprt_switch =
205 rpc_sysfs_xprt_switch_kobj_get_xprt(kobj);
206 ssize_t ret;
207
208 if (!xprt_switch)
209 return 0;
210 ret = sprintf(buf, "num_xprts=%u\nnum_active=%u\n"
211 "num_unique_destaddr=%u\nqueue_len=%ld\n",
212 xprt_switch->xps_nxprts, xprt_switch->xps_nactive,
213 xprt_switch->xps_nunique_destaddr_xprts,
214 atomic_long_read(&xprt_switch->xps_queuelen));
215 xprt_switch_put(xprt_switch);
216 return ret + 1;
217 }
218
rpc_sysfs_xprt_dstaddr_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)219 static ssize_t rpc_sysfs_xprt_dstaddr_store(struct kobject *kobj,
220 struct kobj_attribute *attr,
221 const char *buf, size_t count)
222 {
223 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
224 struct sockaddr *saddr;
225 char *dst_addr;
226 int port;
227 struct xprt_addr *saved_addr;
228 size_t buf_len;
229
230 if (!xprt)
231 return 0;
232 if (!(xprt->xprt_class->ident == XPRT_TRANSPORT_TCP ||
233 xprt->xprt_class->ident == XPRT_TRANSPORT_RDMA)) {
234 xprt_put(xprt);
235 return -EOPNOTSUPP;
236 }
237
238 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) {
239 count = -EINTR;
240 goto out_put;
241 }
242 saddr = (struct sockaddr *)&xprt->addr;
243 port = rpc_get_port(saddr);
244
245 /* buf_len is the len until the first occurence of either
246 * '\n' or '\0'
247 */
248 buf_len = strcspn(buf, "\n");
249
250 dst_addr = kstrndup(buf, buf_len, GFP_KERNEL);
251 if (!dst_addr)
252 goto out_err;
253 saved_addr = kzalloc(sizeof(*saved_addr), GFP_KERNEL);
254 if (!saved_addr)
255 goto out_err_free;
256 saved_addr->addr =
257 rcu_dereference_raw(xprt->address_strings[RPC_DISPLAY_ADDR]);
258 rcu_assign_pointer(xprt->address_strings[RPC_DISPLAY_ADDR], dst_addr);
259 call_rcu(&saved_addr->rcu, free_xprt_addr);
260 xprt->addrlen = rpc_pton(xprt->xprt_net, buf, buf_len, saddr,
261 sizeof(*saddr));
262 rpc_set_port(saddr, port);
263
264 xprt_force_disconnect(xprt);
265 out:
266 xprt_release_write(xprt, NULL);
267 out_put:
268 xprt_put(xprt);
269 return count;
270 out_err_free:
271 kfree(dst_addr);
272 out_err:
273 count = -ENOMEM;
274 goto out;
275 }
276
rpc_sysfs_xprt_state_change(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)277 static ssize_t rpc_sysfs_xprt_state_change(struct kobject *kobj,
278 struct kobj_attribute *attr,
279 const char *buf, size_t count)
280 {
281 struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
282 int offline = 0, online = 0, remove = 0;
283 struct rpc_xprt_switch *xps = rpc_sysfs_xprt_kobj_get_xprt_switch(kobj);
284
285 if (!xprt || !xps) {
286 count = 0;
287 goto out_put;
288 }
289
290 if (!strncmp(buf, "offline", 7))
291 offline = 1;
292 else if (!strncmp(buf, "online", 6))
293 online = 1;
294 else if (!strncmp(buf, "remove", 6))
295 remove = 1;
296 else {
297 count = -EINVAL;
298 goto out_put;
299 }
300
301 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) {
302 count = -EINTR;
303 goto out_put;
304 }
305 if (xprt->main) {
306 count = -EINVAL;
307 goto release_tasks;
308 }
309 if (offline) {
310 if (!test_and_set_bit(XPRT_OFFLINE, &xprt->state)) {
311 spin_lock(&xps->xps_lock);
312 xps->xps_nactive--;
313 spin_unlock(&xps->xps_lock);
314 }
315 } else if (online) {
316 if (test_and_clear_bit(XPRT_OFFLINE, &xprt->state)) {
317 spin_lock(&xps->xps_lock);
318 xps->xps_nactive++;
319 spin_unlock(&xps->xps_lock);
320 }
321 } else if (remove) {
322 if (test_bit(XPRT_OFFLINE, &xprt->state)) {
323 if (!test_and_set_bit(XPRT_REMOVE, &xprt->state)) {
324 xprt_force_disconnect(xprt);
325 if (test_bit(XPRT_CONNECTED, &xprt->state)) {
326 if (!xprt->sending.qlen &&
327 !xprt->pending.qlen &&
328 !xprt->backlog.qlen &&
329 !atomic_long_read(&xprt->queuelen))
330 rpc_xprt_switch_remove_xprt(xps, xprt);
331 }
332 }
333 } else {
334 count = -EINVAL;
335 }
336 }
337
338 release_tasks:
339 xprt_release_write(xprt, NULL);
340 out_put:
341 xprt_put(xprt);
342 xprt_switch_put(xps);
343 return count;
344 }
345
rpc_sysfs_init(void)346 int rpc_sysfs_init(void)
347 {
348 rpc_sunrpc_kset = kset_create_and_add("sunrpc", NULL, kernel_kobj);
349 if (!rpc_sunrpc_kset)
350 return -ENOMEM;
351 rpc_sunrpc_client_kobj =
352 rpc_sysfs_object_alloc("rpc-clients", rpc_sunrpc_kset, NULL);
353 if (!rpc_sunrpc_client_kobj)
354 goto err_client;
355 rpc_sunrpc_xprt_switch_kobj =
356 rpc_sysfs_object_alloc("xprt-switches", rpc_sunrpc_kset, NULL);
357 if (!rpc_sunrpc_xprt_switch_kobj)
358 goto err_switch;
359 return 0;
360 err_switch:
361 kobject_put(rpc_sunrpc_client_kobj);
362 rpc_sunrpc_client_kobj = NULL;
363 err_client:
364 kset_unregister(rpc_sunrpc_kset);
365 rpc_sunrpc_kset = NULL;
366 return -ENOMEM;
367 }
368
rpc_sysfs_client_release(struct kobject * kobj)369 static void rpc_sysfs_client_release(struct kobject *kobj)
370 {
371 struct rpc_sysfs_client *c;
372
373 c = container_of(kobj, struct rpc_sysfs_client, kobject);
374 kfree(c);
375 }
376
rpc_sysfs_xprt_switch_release(struct kobject * kobj)377 static void rpc_sysfs_xprt_switch_release(struct kobject *kobj)
378 {
379 struct rpc_sysfs_xprt_switch *xprt_switch;
380
381 xprt_switch = container_of(kobj, struct rpc_sysfs_xprt_switch, kobject);
382 kfree(xprt_switch);
383 }
384
rpc_sysfs_xprt_release(struct kobject * kobj)385 static void rpc_sysfs_xprt_release(struct kobject *kobj)
386 {
387 struct rpc_sysfs_xprt *xprt;
388
389 xprt = container_of(kobj, struct rpc_sysfs_xprt, kobject);
390 kfree(xprt);
391 }
392
rpc_sysfs_client_namespace(struct kobject * kobj)393 static const void *rpc_sysfs_client_namespace(struct kobject *kobj)
394 {
395 return container_of(kobj, struct rpc_sysfs_client, kobject)->net;
396 }
397
rpc_sysfs_xprt_switch_namespace(struct kobject * kobj)398 static const void *rpc_sysfs_xprt_switch_namespace(struct kobject *kobj)
399 {
400 return container_of(kobj, struct rpc_sysfs_xprt_switch, kobject)->net;
401 }
402
rpc_sysfs_xprt_namespace(struct kobject * kobj)403 static const void *rpc_sysfs_xprt_namespace(struct kobject *kobj)
404 {
405 return container_of(kobj, struct rpc_sysfs_xprt,
406 kobject)->xprt->xprt_net;
407 }
408
409 static struct kobj_attribute rpc_sysfs_xprt_dstaddr = __ATTR(dstaddr,
410 0644, rpc_sysfs_xprt_dstaddr_show, rpc_sysfs_xprt_dstaddr_store);
411
412 static struct kobj_attribute rpc_sysfs_xprt_srcaddr = __ATTR(srcaddr,
413 0644, rpc_sysfs_xprt_srcaddr_show, NULL);
414
415 static struct kobj_attribute rpc_sysfs_xprt_info = __ATTR(xprt_info,
416 0444, rpc_sysfs_xprt_info_show, NULL);
417
418 static struct kobj_attribute rpc_sysfs_xprt_change_state = __ATTR(xprt_state,
419 0644, rpc_sysfs_xprt_state_show, rpc_sysfs_xprt_state_change);
420
421 static struct attribute *rpc_sysfs_xprt_attrs[] = {
422 &rpc_sysfs_xprt_dstaddr.attr,
423 &rpc_sysfs_xprt_srcaddr.attr,
424 &rpc_sysfs_xprt_info.attr,
425 &rpc_sysfs_xprt_change_state.attr,
426 NULL,
427 };
428
429 static struct kobj_attribute rpc_sysfs_xprt_switch_info =
430 __ATTR(xprt_switch_info, 0444, rpc_sysfs_xprt_switch_info_show, NULL);
431
432 static struct attribute *rpc_sysfs_xprt_switch_attrs[] = {
433 &rpc_sysfs_xprt_switch_info.attr,
434 NULL,
435 };
436
437 static struct kobj_type rpc_sysfs_client_type = {
438 .release = rpc_sysfs_client_release,
439 .sysfs_ops = &kobj_sysfs_ops,
440 .namespace = rpc_sysfs_client_namespace,
441 };
442
443 static struct kobj_type rpc_sysfs_xprt_switch_type = {
444 .release = rpc_sysfs_xprt_switch_release,
445 .default_attrs = rpc_sysfs_xprt_switch_attrs,
446 .sysfs_ops = &kobj_sysfs_ops,
447 .namespace = rpc_sysfs_xprt_switch_namespace,
448 };
449
450 static struct kobj_type rpc_sysfs_xprt_type = {
451 .release = rpc_sysfs_xprt_release,
452 .default_attrs = rpc_sysfs_xprt_attrs,
453 .sysfs_ops = &kobj_sysfs_ops,
454 .namespace = rpc_sysfs_xprt_namespace,
455 };
456
rpc_sysfs_exit(void)457 void rpc_sysfs_exit(void)
458 {
459 kobject_put(rpc_sunrpc_client_kobj);
460 kobject_put(rpc_sunrpc_xprt_switch_kobj);
461 kset_unregister(rpc_sunrpc_kset);
462 }
463
rpc_sysfs_client_alloc(struct kobject * parent,struct net * net,int clid)464 static struct rpc_sysfs_client *rpc_sysfs_client_alloc(struct kobject *parent,
465 struct net *net,
466 int clid)
467 {
468 struct rpc_sysfs_client *p;
469
470 p = kzalloc(sizeof(*p), GFP_KERNEL);
471 if (p) {
472 p->net = net;
473 p->kobject.kset = rpc_sunrpc_kset;
474 if (kobject_init_and_add(&p->kobject, &rpc_sysfs_client_type,
475 parent, "clnt-%d", clid) == 0)
476 return p;
477 kobject_put(&p->kobject);
478 }
479 return NULL;
480 }
481
482 static struct rpc_sysfs_xprt_switch *
rpc_sysfs_xprt_switch_alloc(struct kobject * parent,struct rpc_xprt_switch * xprt_switch,struct net * net,gfp_t gfp_flags)483 rpc_sysfs_xprt_switch_alloc(struct kobject *parent,
484 struct rpc_xprt_switch *xprt_switch,
485 struct net *net,
486 gfp_t gfp_flags)
487 {
488 struct rpc_sysfs_xprt_switch *p;
489
490 p = kzalloc(sizeof(*p), gfp_flags);
491 if (p) {
492 p->net = net;
493 p->kobject.kset = rpc_sunrpc_kset;
494 if (kobject_init_and_add(&p->kobject,
495 &rpc_sysfs_xprt_switch_type,
496 parent, "switch-%d",
497 xprt_switch->xps_id) == 0)
498 return p;
499 kobject_put(&p->kobject);
500 }
501 return NULL;
502 }
503
rpc_sysfs_xprt_alloc(struct kobject * parent,struct rpc_xprt * xprt,gfp_t gfp_flags)504 static struct rpc_sysfs_xprt *rpc_sysfs_xprt_alloc(struct kobject *parent,
505 struct rpc_xprt *xprt,
506 gfp_t gfp_flags)
507 {
508 struct rpc_sysfs_xprt *p;
509
510 p = kzalloc(sizeof(*p), gfp_flags);
511 if (!p)
512 goto out;
513 p->kobject.kset = rpc_sunrpc_kset;
514 if (kobject_init_and_add(&p->kobject, &rpc_sysfs_xprt_type,
515 parent, "xprt-%d-%s", xprt->id,
516 xprt->address_strings[RPC_DISPLAY_PROTO]) == 0)
517 return p;
518 kobject_put(&p->kobject);
519 out:
520 return NULL;
521 }
522
rpc_sysfs_client_setup(struct rpc_clnt * clnt,struct rpc_xprt_switch * xprt_switch,struct net * net)523 void rpc_sysfs_client_setup(struct rpc_clnt *clnt,
524 struct rpc_xprt_switch *xprt_switch,
525 struct net *net)
526 {
527 struct rpc_sysfs_client *rpc_client;
528 struct rpc_sysfs_xprt_switch *xswitch =
529 (struct rpc_sysfs_xprt_switch *)xprt_switch->xps_sysfs;
530
531 if (!xswitch)
532 return;
533
534 rpc_client = rpc_sysfs_client_alloc(rpc_sunrpc_client_kobj,
535 net, clnt->cl_clid);
536 if (rpc_client) {
537 char name[] = "switch";
538 int ret;
539
540 clnt->cl_sysfs = rpc_client;
541 rpc_client->clnt = clnt;
542 rpc_client->xprt_switch = xprt_switch;
543 kobject_uevent(&rpc_client->kobject, KOBJ_ADD);
544 ret = sysfs_create_link_nowarn(&rpc_client->kobject,
545 &xswitch->kobject, name);
546 if (ret)
547 pr_warn("can't create link to %s in sysfs (%d)\n",
548 name, ret);
549 }
550 }
551
rpc_sysfs_xprt_switch_setup(struct rpc_xprt_switch * xprt_switch,struct rpc_xprt * xprt,gfp_t gfp_flags)552 void rpc_sysfs_xprt_switch_setup(struct rpc_xprt_switch *xprt_switch,
553 struct rpc_xprt *xprt,
554 gfp_t gfp_flags)
555 {
556 struct rpc_sysfs_xprt_switch *rpc_xprt_switch;
557 struct net *net;
558
559 if (xprt_switch->xps_net)
560 net = xprt_switch->xps_net;
561 else
562 net = xprt->xprt_net;
563 rpc_xprt_switch =
564 rpc_sysfs_xprt_switch_alloc(rpc_sunrpc_xprt_switch_kobj,
565 xprt_switch, net, gfp_flags);
566 if (rpc_xprt_switch) {
567 xprt_switch->xps_sysfs = rpc_xprt_switch;
568 rpc_xprt_switch->xprt_switch = xprt_switch;
569 rpc_xprt_switch->xprt = xprt;
570 kobject_uevent(&rpc_xprt_switch->kobject, KOBJ_ADD);
571 } else {
572 xprt_switch->xps_sysfs = NULL;
573 }
574 }
575
rpc_sysfs_xprt_setup(struct rpc_xprt_switch * xprt_switch,struct rpc_xprt * xprt,gfp_t gfp_flags)576 void rpc_sysfs_xprt_setup(struct rpc_xprt_switch *xprt_switch,
577 struct rpc_xprt *xprt,
578 gfp_t gfp_flags)
579 {
580 struct rpc_sysfs_xprt *rpc_xprt;
581 struct rpc_sysfs_xprt_switch *switch_obj =
582 (struct rpc_sysfs_xprt_switch *)xprt_switch->xps_sysfs;
583
584 if (!switch_obj)
585 return;
586
587 rpc_xprt = rpc_sysfs_xprt_alloc(&switch_obj->kobject, xprt, gfp_flags);
588 if (rpc_xprt) {
589 xprt->xprt_sysfs = rpc_xprt;
590 rpc_xprt->xprt = xprt;
591 rpc_xprt->xprt_switch = xprt_switch;
592 kobject_uevent(&rpc_xprt->kobject, KOBJ_ADD);
593 }
594 }
595
rpc_sysfs_client_destroy(struct rpc_clnt * clnt)596 void rpc_sysfs_client_destroy(struct rpc_clnt *clnt)
597 {
598 struct rpc_sysfs_client *rpc_client = clnt->cl_sysfs;
599
600 if (rpc_client) {
601 char name[] = "switch";
602
603 sysfs_remove_link(&rpc_client->kobject, name);
604 kobject_uevent(&rpc_client->kobject, KOBJ_REMOVE);
605 kobject_del(&rpc_client->kobject);
606 kobject_put(&rpc_client->kobject);
607 clnt->cl_sysfs = NULL;
608 }
609 }
610
rpc_sysfs_xprt_switch_destroy(struct rpc_xprt_switch * xprt_switch)611 void rpc_sysfs_xprt_switch_destroy(struct rpc_xprt_switch *xprt_switch)
612 {
613 struct rpc_sysfs_xprt_switch *rpc_xprt_switch = xprt_switch->xps_sysfs;
614
615 if (rpc_xprt_switch) {
616 kobject_uevent(&rpc_xprt_switch->kobject, KOBJ_REMOVE);
617 kobject_del(&rpc_xprt_switch->kobject);
618 kobject_put(&rpc_xprt_switch->kobject);
619 xprt_switch->xps_sysfs = NULL;
620 }
621 }
622
rpc_sysfs_xprt_destroy(struct rpc_xprt * xprt)623 void rpc_sysfs_xprt_destroy(struct rpc_xprt *xprt)
624 {
625 struct rpc_sysfs_xprt *rpc_xprt = xprt->xprt_sysfs;
626
627 if (rpc_xprt) {
628 kobject_uevent(&rpc_xprt->kobject, KOBJ_REMOVE);
629 kobject_del(&rpc_xprt->kobject);
630 kobject_put(&rpc_xprt->kobject);
631 xprt->xprt_sysfs = NULL;
632 }
633 }
634