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