xref: /openbmc/linux/net/sunrpc/sysfs.c (revision e44773da)
174678748SOlga Kornievskaia // SPDX-License-Identifier: GPL-2.0
274678748SOlga Kornievskaia /*
374678748SOlga Kornievskaia  * Copyright (c) 2020 Anna Schumaker <Anna.Schumaker@Netapp.com>
474678748SOlga Kornievskaia  */
5c441f125SOlga Kornievskaia #include <linux/sunrpc/clnt.h>
674678748SOlga Kornievskaia #include <linux/kobject.h>
7587bc725SOlga Kornievskaia #include <linux/sunrpc/addr.h>
8c1830a63SOlga Kornievskaia #include <linux/sunrpc/xprtsock.h>
9587bc725SOlga Kornievskaia 
10c5a382ebSOlga Kornievskaia #include "sysfs.h"
1174678748SOlga Kornievskaia 
12587bc725SOlga Kornievskaia struct xprt_addr {
13587bc725SOlga Kornievskaia 	const char *addr;
14587bc725SOlga Kornievskaia 	struct rcu_head rcu;
15587bc725SOlga Kornievskaia };
16587bc725SOlga Kornievskaia 
17587bc725SOlga Kornievskaia static void free_xprt_addr(struct rcu_head *head)
18587bc725SOlga Kornievskaia {
19587bc725SOlga Kornievskaia 	struct xprt_addr *addr = container_of(head, struct xprt_addr, rcu);
20587bc725SOlga Kornievskaia 
21587bc725SOlga Kornievskaia 	kfree(addr->addr);
22587bc725SOlga Kornievskaia 	kfree(addr);
23587bc725SOlga Kornievskaia }
24587bc725SOlga Kornievskaia 
2574678748SOlga Kornievskaia static struct kset *rpc_sunrpc_kset;
26baea9944SOlga Kornievskaia static struct kobject *rpc_sunrpc_client_kobj, *rpc_sunrpc_xprt_switch_kobj;
27c441f125SOlga Kornievskaia 
28c441f125SOlga Kornievskaia static void rpc_sysfs_object_release(struct kobject *kobj)
29c441f125SOlga Kornievskaia {
30c441f125SOlga Kornievskaia 	kfree(kobj);
31c441f125SOlga Kornievskaia }
32c441f125SOlga Kornievskaia 
33c441f125SOlga Kornievskaia static const struct kobj_ns_type_operations *
34c441f125SOlga Kornievskaia rpc_sysfs_object_child_ns_type(struct kobject *kobj)
35c441f125SOlga Kornievskaia {
36c441f125SOlga Kornievskaia 	return &net_ns_type_operations;
37c441f125SOlga Kornievskaia }
38c441f125SOlga Kornievskaia 
39c441f125SOlga Kornievskaia static struct kobj_type rpc_sysfs_object_type = {
40c441f125SOlga Kornievskaia 	.release = rpc_sysfs_object_release,
41c441f125SOlga Kornievskaia 	.sysfs_ops = &kobj_sysfs_ops,
42c441f125SOlga Kornievskaia 	.child_ns_type = rpc_sysfs_object_child_ns_type,
43c441f125SOlga Kornievskaia };
44c441f125SOlga Kornievskaia 
45c441f125SOlga Kornievskaia static struct kobject *rpc_sysfs_object_alloc(const char *name,
46c441f125SOlga Kornievskaia 					      struct kset *kset,
47c441f125SOlga Kornievskaia 					      struct kobject *parent)
48c441f125SOlga Kornievskaia {
49c441f125SOlga Kornievskaia 	struct kobject *kobj;
50c441f125SOlga Kornievskaia 
51c441f125SOlga Kornievskaia 	kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
52c441f125SOlga Kornievskaia 	if (kobj) {
53c441f125SOlga Kornievskaia 		kobj->kset = kset;
54c441f125SOlga Kornievskaia 		if (kobject_init_and_add(kobj, &rpc_sysfs_object_type,
55c441f125SOlga Kornievskaia 					 parent, "%s", name) == 0)
56c441f125SOlga Kornievskaia 			return kobj;
57c441f125SOlga Kornievskaia 		kobject_put(kobj);
58c441f125SOlga Kornievskaia 	}
59c441f125SOlga Kornievskaia 	return NULL;
60c441f125SOlga Kornievskaia }
6174678748SOlga Kornievskaia 
62587bc725SOlga Kornievskaia static inline struct rpc_xprt *
63587bc725SOlga Kornievskaia rpc_sysfs_xprt_kobj_get_xprt(struct kobject *kobj)
64587bc725SOlga Kornievskaia {
65587bc725SOlga Kornievskaia 	struct rpc_sysfs_xprt *x = container_of(kobj,
66587bc725SOlga Kornievskaia 		struct rpc_sysfs_xprt, kobject);
67587bc725SOlga Kornievskaia 
68587bc725SOlga Kornievskaia 	return xprt_get(x->xprt);
69587bc725SOlga Kornievskaia }
70587bc725SOlga Kornievskaia 
710e559035SOlga Kornievskaia static inline struct rpc_xprt_switch *
725b7eb784SOlga Kornievskaia rpc_sysfs_xprt_kobj_get_xprt_switch(struct kobject *kobj)
735b7eb784SOlga Kornievskaia {
745b7eb784SOlga Kornievskaia 	struct rpc_sysfs_xprt *x = container_of(kobj,
755b7eb784SOlga Kornievskaia 		struct rpc_sysfs_xprt, kobject);
765b7eb784SOlga Kornievskaia 
775b7eb784SOlga Kornievskaia 	return xprt_switch_get(x->xprt_switch);
785b7eb784SOlga Kornievskaia }
795b7eb784SOlga Kornievskaia 
805b7eb784SOlga Kornievskaia static inline struct rpc_xprt_switch *
810e559035SOlga Kornievskaia rpc_sysfs_xprt_switch_kobj_get_xprt(struct kobject *kobj)
820e559035SOlga Kornievskaia {
830e559035SOlga Kornievskaia 	struct rpc_sysfs_xprt_switch *x = container_of(kobj,
840e559035SOlga Kornievskaia 		struct rpc_sysfs_xprt_switch, kobject);
850e559035SOlga Kornievskaia 
860e559035SOlga Kornievskaia 	return xprt_switch_get(x->xprt_switch);
870e559035SOlga Kornievskaia }
880e559035SOlga Kornievskaia 
89587bc725SOlga Kornievskaia static ssize_t rpc_sysfs_xprt_dstaddr_show(struct kobject *kobj,
90587bc725SOlga Kornievskaia 					   struct kobj_attribute *attr,
91587bc725SOlga Kornievskaia 					   char *buf)
92587bc725SOlga Kornievskaia {
93587bc725SOlga Kornievskaia 	struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
94587bc725SOlga Kornievskaia 	ssize_t ret;
95587bc725SOlga Kornievskaia 
96587bc725SOlga Kornievskaia 	if (!xprt)
97587bc725SOlga Kornievskaia 		return 0;
98587bc725SOlga Kornievskaia 	ret = sprintf(buf, "%s\n", xprt->address_strings[RPC_DISPLAY_ADDR]);
99587bc725SOlga Kornievskaia 	xprt_put(xprt);
100587bc725SOlga Kornievskaia 	return ret + 1;
101587bc725SOlga Kornievskaia }
102587bc725SOlga Kornievskaia 
103*e44773daSAnna Schumaker static ssize_t rpc_sysfs_xprt_srcaddr_show(struct kobject *kobj,
104*e44773daSAnna Schumaker 					   struct kobj_attribute *attr,
105*e44773daSAnna Schumaker 					   char *buf)
106*e44773daSAnna Schumaker {
107*e44773daSAnna Schumaker 	struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
108*e44773daSAnna Schumaker 	struct sockaddr_storage saddr;
109*e44773daSAnna Schumaker 	struct sock_xprt *sock;
110*e44773daSAnna Schumaker 	ssize_t ret = -1;
111*e44773daSAnna Schumaker 
112*e44773daSAnna Schumaker 	if (!xprt)
113*e44773daSAnna Schumaker 		return 0;
114*e44773daSAnna Schumaker 
115*e44773daSAnna Schumaker 	sock = container_of(xprt, struct sock_xprt, xprt);
116*e44773daSAnna Schumaker 	if (kernel_getsockname(sock->sock, (struct sockaddr *)&saddr) < 0)
117*e44773daSAnna Schumaker 		goto out;
118*e44773daSAnna Schumaker 
119*e44773daSAnna Schumaker 	ret = sprintf(buf, "%pISc\n", &saddr);
120*e44773daSAnna Schumaker out:
121*e44773daSAnna Schumaker 	xprt_put(xprt);
122*e44773daSAnna Schumaker 	return ret + 1;
123*e44773daSAnna Schumaker }
124*e44773daSAnna Schumaker 
1254a09651aSOlga Kornievskaia static ssize_t rpc_sysfs_xprt_info_show(struct kobject *kobj,
1264a09651aSOlga Kornievskaia 					struct kobj_attribute *attr,
1274a09651aSOlga Kornievskaia 					char *buf)
1284a09651aSOlga Kornievskaia {
1294a09651aSOlga Kornievskaia 	struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
1304a09651aSOlga Kornievskaia 	ssize_t ret;
1314a09651aSOlga Kornievskaia 
1324a09651aSOlga Kornievskaia 	if (!xprt)
1334a09651aSOlga Kornievskaia 		return 0;
1344a09651aSOlga Kornievskaia 
1354a09651aSOlga Kornievskaia 	ret = sprintf(buf, "last_used=%lu\ncur_cong=%lu\ncong_win=%lu\n"
1364a09651aSOlga Kornievskaia 		       "max_num_slots=%u\nmin_num_slots=%u\nnum_reqs=%u\n"
1374a09651aSOlga Kornievskaia 		       "binding_q_len=%u\nsending_q_len=%u\npending_q_len=%u\n"
1386a284059SOlga Kornievskaia 		       "backlog_q_len=%u\nmain_xprt=%d\nsrc_port=%u\n"
1396a284059SOlga Kornievskaia 		       "tasks_queuelen=%ld\n",
140c1830a63SOlga Kornievskaia 		       xprt->last_used, xprt->cong, xprt->cwnd, xprt->max_reqs,
141c1830a63SOlga Kornievskaia 		       xprt->min_reqs, xprt->num_reqs, xprt->binding.qlen,
142c1830a63SOlga Kornievskaia 		       xprt->sending.qlen, xprt->pending.qlen,
143c1830a63SOlga Kornievskaia 		       xprt->backlog.qlen, xprt->main,
144c1830a63SOlga Kornievskaia 		       (xprt->xprt_class->ident == XPRT_TRANSPORT_TCP) ?
1456a284059SOlga Kornievskaia 		       get_srcport(xprt) : 0,
1466a284059SOlga Kornievskaia 		       atomic_long_read(&xprt->queuelen));
1474a09651aSOlga Kornievskaia 	xprt_put(xprt);
1484a09651aSOlga Kornievskaia 	return ret + 1;
1494a09651aSOlga Kornievskaia }
1504a09651aSOlga Kornievskaia 
151681d5699SOlga Kornievskaia static ssize_t rpc_sysfs_xprt_state_show(struct kobject *kobj,
152681d5699SOlga Kornievskaia 					 struct kobj_attribute *attr,
153681d5699SOlga Kornievskaia 					 char *buf)
154681d5699SOlga Kornievskaia {
155681d5699SOlga Kornievskaia 	struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
156681d5699SOlga Kornievskaia 	ssize_t ret;
157681d5699SOlga Kornievskaia 	int locked, connected, connecting, close_wait, bound, binding,
1586f081693SOlga Kornievskaia 	    closing, congested, cwnd_wait, write_space, offline, remove;
159681d5699SOlga Kornievskaia 
160681d5699SOlga Kornievskaia 	if (!xprt)
161681d5699SOlga Kornievskaia 		return 0;
162681d5699SOlga Kornievskaia 
163681d5699SOlga Kornievskaia 	if (!xprt->state) {
164681d5699SOlga Kornievskaia 		ret = sprintf(buf, "state=CLOSED\n");
165681d5699SOlga Kornievskaia 	} else {
166681d5699SOlga Kornievskaia 		locked = test_bit(XPRT_LOCKED, &xprt->state);
167681d5699SOlga Kornievskaia 		connected = test_bit(XPRT_CONNECTED, &xprt->state);
168681d5699SOlga Kornievskaia 		connecting = test_bit(XPRT_CONNECTING, &xprt->state);
169681d5699SOlga Kornievskaia 		close_wait = test_bit(XPRT_CLOSE_WAIT, &xprt->state);
170681d5699SOlga Kornievskaia 		bound = test_bit(XPRT_BOUND, &xprt->state);
171681d5699SOlga Kornievskaia 		binding = test_bit(XPRT_BINDING, &xprt->state);
172681d5699SOlga Kornievskaia 		closing = test_bit(XPRT_CLOSING, &xprt->state);
173681d5699SOlga Kornievskaia 		congested = test_bit(XPRT_CONGESTED, &xprt->state);
174681d5699SOlga Kornievskaia 		cwnd_wait = test_bit(XPRT_CWND_WAIT, &xprt->state);
175681d5699SOlga Kornievskaia 		write_space = test_bit(XPRT_WRITE_SPACE, &xprt->state);
1765b7eb784SOlga Kornievskaia 		offline = test_bit(XPRT_OFFLINE, &xprt->state);
1776f081693SOlga Kornievskaia 		remove = test_bit(XPRT_REMOVE, &xprt->state);
178681d5699SOlga Kornievskaia 
1796f081693SOlga Kornievskaia 		ret = sprintf(buf, "state=%s %s %s %s %s %s %s %s %s %s %s %s\n",
180681d5699SOlga Kornievskaia 			      locked ? "LOCKED" : "",
181681d5699SOlga Kornievskaia 			      connected ? "CONNECTED" : "",
182681d5699SOlga Kornievskaia 			      connecting ? "CONNECTING" : "",
183681d5699SOlga Kornievskaia 			      close_wait ? "CLOSE_WAIT" : "",
184681d5699SOlga Kornievskaia 			      bound ? "BOUND" : "",
185681d5699SOlga Kornievskaia 			      binding ? "BOUNDING" : "",
186681d5699SOlga Kornievskaia 			      closing ? "CLOSING" : "",
187681d5699SOlga Kornievskaia 			      congested ? "CONGESTED" : "",
188681d5699SOlga Kornievskaia 			      cwnd_wait ? "CWND_WAIT" : "",
1895b7eb784SOlga Kornievskaia 			      write_space ? "WRITE_SPACE" : "",
1906f081693SOlga Kornievskaia 			      offline ? "OFFLINE" : "",
1916f081693SOlga Kornievskaia 			      remove ? "REMOVE" : "");
192681d5699SOlga Kornievskaia 	}
193681d5699SOlga Kornievskaia 
194681d5699SOlga Kornievskaia 	xprt_put(xprt);
195681d5699SOlga Kornievskaia 	return ret + 1;
196681d5699SOlga Kornievskaia }
197681d5699SOlga Kornievskaia 
1980e559035SOlga Kornievskaia static ssize_t rpc_sysfs_xprt_switch_info_show(struct kobject *kobj,
1990e559035SOlga Kornievskaia 					       struct kobj_attribute *attr,
2000e559035SOlga Kornievskaia 					       char *buf)
2010e559035SOlga Kornievskaia {
2020e559035SOlga Kornievskaia 	struct rpc_xprt_switch *xprt_switch =
2030e559035SOlga Kornievskaia 		rpc_sysfs_xprt_switch_kobj_get_xprt(kobj);
2040e559035SOlga Kornievskaia 	ssize_t ret;
2050e559035SOlga Kornievskaia 
2060e559035SOlga Kornievskaia 	if (!xprt_switch)
2070e559035SOlga Kornievskaia 		return 0;
2080e559035SOlga Kornievskaia 	ret = sprintf(buf, "num_xprts=%u\nnum_active=%u\nqueue_len=%ld\n",
2090e559035SOlga Kornievskaia 		      xprt_switch->xps_nxprts, xprt_switch->xps_nactive,
2100e559035SOlga Kornievskaia 		      atomic_long_read(&xprt_switch->xps_queuelen));
2110e559035SOlga Kornievskaia 	xprt_switch_put(xprt_switch);
2120e559035SOlga Kornievskaia 	return ret + 1;
2130e559035SOlga Kornievskaia }
2140e559035SOlga Kornievskaia 
215587bc725SOlga Kornievskaia static ssize_t rpc_sysfs_xprt_dstaddr_store(struct kobject *kobj,
216587bc725SOlga Kornievskaia 					    struct kobj_attribute *attr,
217587bc725SOlga Kornievskaia 					    const char *buf, size_t count)
218587bc725SOlga Kornievskaia {
219587bc725SOlga Kornievskaia 	struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
220587bc725SOlga Kornievskaia 	struct sockaddr *saddr;
221587bc725SOlga Kornievskaia 	char *dst_addr;
222587bc725SOlga Kornievskaia 	int port;
223587bc725SOlga Kornievskaia 	struct xprt_addr *saved_addr;
224587bc725SOlga Kornievskaia 	size_t buf_len;
225587bc725SOlga Kornievskaia 
226587bc725SOlga Kornievskaia 	if (!xprt)
227587bc725SOlga Kornievskaia 		return 0;
228587bc725SOlga Kornievskaia 	if (!(xprt->xprt_class->ident == XPRT_TRANSPORT_TCP ||
229587bc725SOlga Kornievskaia 	      xprt->xprt_class->ident == XPRT_TRANSPORT_RDMA)) {
230587bc725SOlga Kornievskaia 		xprt_put(xprt);
231587bc725SOlga Kornievskaia 		return -EOPNOTSUPP;
232587bc725SOlga Kornievskaia 	}
233587bc725SOlga Kornievskaia 
234587bc725SOlga Kornievskaia 	if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) {
235587bc725SOlga Kornievskaia 		count = -EINTR;
236587bc725SOlga Kornievskaia 		goto out_put;
237587bc725SOlga Kornievskaia 	}
238587bc725SOlga Kornievskaia 	saddr = (struct sockaddr *)&xprt->addr;
239587bc725SOlga Kornievskaia 	port = rpc_get_port(saddr);
240587bc725SOlga Kornievskaia 
241587bc725SOlga Kornievskaia 	/* buf_len is the len until the first occurence of either
242587bc725SOlga Kornievskaia 	 * '\n' or '\0'
243587bc725SOlga Kornievskaia 	 */
244587bc725SOlga Kornievskaia 	buf_len = strcspn(buf, "\n");
245587bc725SOlga Kornievskaia 
246587bc725SOlga Kornievskaia 	dst_addr = kstrndup(buf, buf_len, GFP_KERNEL);
247587bc725SOlga Kornievskaia 	if (!dst_addr)
248587bc725SOlga Kornievskaia 		goto out_err;
249587bc725SOlga Kornievskaia 	saved_addr = kzalloc(sizeof(*saved_addr), GFP_KERNEL);
250587bc725SOlga Kornievskaia 	if (!saved_addr)
251587bc725SOlga Kornievskaia 		goto out_err_free;
252587bc725SOlga Kornievskaia 	saved_addr->addr =
253587bc725SOlga Kornievskaia 		rcu_dereference_raw(xprt->address_strings[RPC_DISPLAY_ADDR]);
254587bc725SOlga Kornievskaia 	rcu_assign_pointer(xprt->address_strings[RPC_DISPLAY_ADDR], dst_addr);
255587bc725SOlga Kornievskaia 	call_rcu(&saved_addr->rcu, free_xprt_addr);
256587bc725SOlga Kornievskaia 	xprt->addrlen = rpc_pton(xprt->xprt_net, buf, buf_len, saddr,
257587bc725SOlga Kornievskaia 				 sizeof(*saddr));
258587bc725SOlga Kornievskaia 	rpc_set_port(saddr, port);
259587bc725SOlga Kornievskaia 
260587bc725SOlga Kornievskaia 	xprt_force_disconnect(xprt);
261587bc725SOlga Kornievskaia out:
262587bc725SOlga Kornievskaia 	xprt_release_write(xprt, NULL);
263587bc725SOlga Kornievskaia out_put:
264587bc725SOlga Kornievskaia 	xprt_put(xprt);
265587bc725SOlga Kornievskaia 	return count;
266587bc725SOlga Kornievskaia out_err_free:
267587bc725SOlga Kornievskaia 	kfree(dst_addr);
268587bc725SOlga Kornievskaia out_err:
269587bc725SOlga Kornievskaia 	count = -ENOMEM;
270587bc725SOlga Kornievskaia 	goto out;
271587bc725SOlga Kornievskaia }
272587bc725SOlga Kornievskaia 
2735b7eb784SOlga Kornievskaia static ssize_t rpc_sysfs_xprt_state_change(struct kobject *kobj,
2745b7eb784SOlga Kornievskaia 					   struct kobj_attribute *attr,
2755b7eb784SOlga Kornievskaia 					   const char *buf, size_t count)
2765b7eb784SOlga Kornievskaia {
2775b7eb784SOlga Kornievskaia 	struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
2786f081693SOlga Kornievskaia 	int offline = 0, online = 0, remove = 0;
2795b7eb784SOlga Kornievskaia 	struct rpc_xprt_switch *xps = rpc_sysfs_xprt_kobj_get_xprt_switch(kobj);
2805b7eb784SOlga Kornievskaia 
2815b7eb784SOlga Kornievskaia 	if (!xprt)
2825b7eb784SOlga Kornievskaia 		return 0;
2835b7eb784SOlga Kornievskaia 
2845b7eb784SOlga Kornievskaia 	if (!strncmp(buf, "offline", 7))
2855b7eb784SOlga Kornievskaia 		offline = 1;
2865b7eb784SOlga Kornievskaia 	else if (!strncmp(buf, "online", 6))
2875b7eb784SOlga Kornievskaia 		online = 1;
2886f081693SOlga Kornievskaia 	else if (!strncmp(buf, "remove", 6))
2896f081693SOlga Kornievskaia 		remove = 1;
2905b7eb784SOlga Kornievskaia 	else
2915b7eb784SOlga Kornievskaia 		return -EINVAL;
2925b7eb784SOlga Kornievskaia 
2935b7eb784SOlga Kornievskaia 	if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) {
2945b7eb784SOlga Kornievskaia 		count = -EINTR;
2955b7eb784SOlga Kornievskaia 		goto out_put;
2965b7eb784SOlga Kornievskaia 	}
2975b7eb784SOlga Kornievskaia 	if (xprt->main) {
2985b7eb784SOlga Kornievskaia 		count = -EINVAL;
2995b7eb784SOlga Kornievskaia 		goto release_tasks;
3005b7eb784SOlga Kornievskaia 	}
3015b7eb784SOlga Kornievskaia 	if (offline) {
3025b7eb784SOlga Kornievskaia 		set_bit(XPRT_OFFLINE, &xprt->state);
3035b7eb784SOlga Kornievskaia 		spin_lock(&xps->xps_lock);
3045b7eb784SOlga Kornievskaia 		xps->xps_nactive--;
3055b7eb784SOlga Kornievskaia 		spin_unlock(&xps->xps_lock);
3065b7eb784SOlga Kornievskaia 	} else if (online) {
3075b7eb784SOlga Kornievskaia 		clear_bit(XPRT_OFFLINE, &xprt->state);
3085b7eb784SOlga Kornievskaia 		spin_lock(&xps->xps_lock);
3095b7eb784SOlga Kornievskaia 		xps->xps_nactive++;
3105b7eb784SOlga Kornievskaia 		spin_unlock(&xps->xps_lock);
3116f081693SOlga Kornievskaia 	} else if (remove) {
3126f081693SOlga Kornievskaia 		if (test_bit(XPRT_OFFLINE, &xprt->state)) {
3136f081693SOlga Kornievskaia 			set_bit(XPRT_REMOVE, &xprt->state);
3146f081693SOlga Kornievskaia 			xprt_force_disconnect(xprt);
3156f081693SOlga Kornievskaia 			if (test_bit(XPRT_CONNECTED, &xprt->state)) {
3166f081693SOlga Kornievskaia 				if (!xprt->sending.qlen &&
3176f081693SOlga Kornievskaia 				    !xprt->pending.qlen &&
3186f081693SOlga Kornievskaia 				    !xprt->backlog.qlen &&
3196f081693SOlga Kornievskaia 				    !atomic_long_read(&xprt->queuelen))
3206f081693SOlga Kornievskaia 					rpc_xprt_switch_remove_xprt(xps, xprt);
3216f081693SOlga Kornievskaia 			}
3226f081693SOlga Kornievskaia 		} else {
3236f081693SOlga Kornievskaia 			count = -EINVAL;
3246f081693SOlga Kornievskaia 		}
3255b7eb784SOlga Kornievskaia 	}
3265b7eb784SOlga Kornievskaia 
3275b7eb784SOlga Kornievskaia release_tasks:
3285b7eb784SOlga Kornievskaia 	xprt_release_write(xprt, NULL);
3295b7eb784SOlga Kornievskaia out_put:
3305b7eb784SOlga Kornievskaia 	xprt_put(xprt);
3315b7eb784SOlga Kornievskaia 	xprt_switch_put(xps);
3325b7eb784SOlga Kornievskaia 	return count;
3335b7eb784SOlga Kornievskaia }
3345b7eb784SOlga Kornievskaia 
33574678748SOlga Kornievskaia int rpc_sysfs_init(void)
33674678748SOlga Kornievskaia {
33774678748SOlga Kornievskaia 	rpc_sunrpc_kset = kset_create_and_add("sunrpc", NULL, kernel_kobj);
33874678748SOlga Kornievskaia 	if (!rpc_sunrpc_kset)
33974678748SOlga Kornievskaia 		return -ENOMEM;
340baea9944SOlga Kornievskaia 	rpc_sunrpc_client_kobj =
341baea9944SOlga Kornievskaia 		rpc_sysfs_object_alloc("rpc-clients", rpc_sunrpc_kset, NULL);
342baea9944SOlga Kornievskaia 	if (!rpc_sunrpc_client_kobj)
343baea9944SOlga Kornievskaia 		goto err_client;
344baea9944SOlga Kornievskaia 	rpc_sunrpc_xprt_switch_kobj =
345baea9944SOlga Kornievskaia 		rpc_sysfs_object_alloc("xprt-switches", rpc_sunrpc_kset, NULL);
346baea9944SOlga Kornievskaia 	if (!rpc_sunrpc_xprt_switch_kobj)
347baea9944SOlga Kornievskaia 		goto err_switch;
34874678748SOlga Kornievskaia 	return 0;
349baea9944SOlga Kornievskaia err_switch:
350baea9944SOlga Kornievskaia 	kobject_put(rpc_sunrpc_client_kobj);
351baea9944SOlga Kornievskaia 	rpc_sunrpc_client_kobj = NULL;
352baea9944SOlga Kornievskaia err_client:
353baea9944SOlga Kornievskaia 	kset_unregister(rpc_sunrpc_kset);
354baea9944SOlga Kornievskaia 	rpc_sunrpc_kset = NULL;
355baea9944SOlga Kornievskaia 	return -ENOMEM;
35674678748SOlga Kornievskaia }
35774678748SOlga Kornievskaia 
358c5a382ebSOlga Kornievskaia static void rpc_sysfs_client_release(struct kobject *kobj)
359c5a382ebSOlga Kornievskaia {
360c5a382ebSOlga Kornievskaia 	struct rpc_sysfs_client *c;
361c5a382ebSOlga Kornievskaia 
362c5a382ebSOlga Kornievskaia 	c = container_of(kobj, struct rpc_sysfs_client, kobject);
363c5a382ebSOlga Kornievskaia 	kfree(c);
364c5a382ebSOlga Kornievskaia }
365c5a382ebSOlga Kornievskaia 
366baea9944SOlga Kornievskaia static void rpc_sysfs_xprt_switch_release(struct kobject *kobj)
367baea9944SOlga Kornievskaia {
368baea9944SOlga Kornievskaia 	struct rpc_sysfs_xprt_switch *xprt_switch;
369baea9944SOlga Kornievskaia 
370baea9944SOlga Kornievskaia 	xprt_switch = container_of(kobj, struct rpc_sysfs_xprt_switch, kobject);
371baea9944SOlga Kornievskaia 	kfree(xprt_switch);
372baea9944SOlga Kornievskaia }
373baea9944SOlga Kornievskaia 
374d408ebe0SOlga Kornievskaia static void rpc_sysfs_xprt_release(struct kobject *kobj)
375d408ebe0SOlga Kornievskaia {
376d408ebe0SOlga Kornievskaia 	struct rpc_sysfs_xprt *xprt;
377d408ebe0SOlga Kornievskaia 
378d408ebe0SOlga Kornievskaia 	xprt = container_of(kobj, struct rpc_sysfs_xprt, kobject);
379d408ebe0SOlga Kornievskaia 	kfree(xprt);
380d408ebe0SOlga Kornievskaia }
381d408ebe0SOlga Kornievskaia 
382c5a382ebSOlga Kornievskaia static const void *rpc_sysfs_client_namespace(struct kobject *kobj)
383c5a382ebSOlga Kornievskaia {
384c5a382ebSOlga Kornievskaia 	return container_of(kobj, struct rpc_sysfs_client, kobject)->net;
385c5a382ebSOlga Kornievskaia }
386c5a382ebSOlga Kornievskaia 
387baea9944SOlga Kornievskaia static const void *rpc_sysfs_xprt_switch_namespace(struct kobject *kobj)
388baea9944SOlga Kornievskaia {
389baea9944SOlga Kornievskaia 	return container_of(kobj, struct rpc_sysfs_xprt_switch, kobject)->net;
390baea9944SOlga Kornievskaia }
391baea9944SOlga Kornievskaia 
392d408ebe0SOlga Kornievskaia static const void *rpc_sysfs_xprt_namespace(struct kobject *kobj)
393d408ebe0SOlga Kornievskaia {
394d408ebe0SOlga Kornievskaia 	return container_of(kobj, struct rpc_sysfs_xprt,
395d408ebe0SOlga Kornievskaia 			    kobject)->xprt->xprt_net;
396d408ebe0SOlga Kornievskaia }
397d408ebe0SOlga Kornievskaia 
398587bc725SOlga Kornievskaia static struct kobj_attribute rpc_sysfs_xprt_dstaddr = __ATTR(dstaddr,
399587bc725SOlga Kornievskaia 	0644, rpc_sysfs_xprt_dstaddr_show, rpc_sysfs_xprt_dstaddr_store);
400587bc725SOlga Kornievskaia 
401*e44773daSAnna Schumaker static struct kobj_attribute rpc_sysfs_xprt_srcaddr = __ATTR(srcaddr,
402*e44773daSAnna Schumaker 	0644, rpc_sysfs_xprt_srcaddr_show, NULL);
403*e44773daSAnna Schumaker 
4044a09651aSOlga Kornievskaia static struct kobj_attribute rpc_sysfs_xprt_info = __ATTR(xprt_info,
4054a09651aSOlga Kornievskaia 	0444, rpc_sysfs_xprt_info_show, NULL);
4064a09651aSOlga Kornievskaia 
407681d5699SOlga Kornievskaia static struct kobj_attribute rpc_sysfs_xprt_change_state = __ATTR(xprt_state,
4085b7eb784SOlga Kornievskaia 	0644, rpc_sysfs_xprt_state_show, rpc_sysfs_xprt_state_change);
409681d5699SOlga Kornievskaia 
410587bc725SOlga Kornievskaia static struct attribute *rpc_sysfs_xprt_attrs[] = {
411587bc725SOlga Kornievskaia 	&rpc_sysfs_xprt_dstaddr.attr,
412*e44773daSAnna Schumaker 	&rpc_sysfs_xprt_srcaddr.attr,
4134a09651aSOlga Kornievskaia 	&rpc_sysfs_xprt_info.attr,
414681d5699SOlga Kornievskaia 	&rpc_sysfs_xprt_change_state.attr,
415587bc725SOlga Kornievskaia 	NULL,
416587bc725SOlga Kornievskaia };
417587bc725SOlga Kornievskaia 
4180e559035SOlga Kornievskaia static struct kobj_attribute rpc_sysfs_xprt_switch_info =
4190e559035SOlga Kornievskaia 	__ATTR(xprt_switch_info, 0444, rpc_sysfs_xprt_switch_info_show, NULL);
4200e559035SOlga Kornievskaia 
4210e559035SOlga Kornievskaia static struct attribute *rpc_sysfs_xprt_switch_attrs[] = {
4220e559035SOlga Kornievskaia 	&rpc_sysfs_xprt_switch_info.attr,
4230e559035SOlga Kornievskaia 	NULL,
4240e559035SOlga Kornievskaia };
4250e559035SOlga Kornievskaia 
426c5a382ebSOlga Kornievskaia static struct kobj_type rpc_sysfs_client_type = {
427c5a382ebSOlga Kornievskaia 	.release = rpc_sysfs_client_release,
428c5a382ebSOlga Kornievskaia 	.sysfs_ops = &kobj_sysfs_ops,
429c5a382ebSOlga Kornievskaia 	.namespace = rpc_sysfs_client_namespace,
430c5a382ebSOlga Kornievskaia };
431c5a382ebSOlga Kornievskaia 
432baea9944SOlga Kornievskaia static struct kobj_type rpc_sysfs_xprt_switch_type = {
433baea9944SOlga Kornievskaia 	.release = rpc_sysfs_xprt_switch_release,
4340e559035SOlga Kornievskaia 	.default_attrs = rpc_sysfs_xprt_switch_attrs,
435baea9944SOlga Kornievskaia 	.sysfs_ops = &kobj_sysfs_ops,
436baea9944SOlga Kornievskaia 	.namespace = rpc_sysfs_xprt_switch_namespace,
437baea9944SOlga Kornievskaia };
438baea9944SOlga Kornievskaia 
439d408ebe0SOlga Kornievskaia static struct kobj_type rpc_sysfs_xprt_type = {
440d408ebe0SOlga Kornievskaia 	.release = rpc_sysfs_xprt_release,
441587bc725SOlga Kornievskaia 	.default_attrs = rpc_sysfs_xprt_attrs,
442d408ebe0SOlga Kornievskaia 	.sysfs_ops = &kobj_sysfs_ops,
443d408ebe0SOlga Kornievskaia 	.namespace = rpc_sysfs_xprt_namespace,
444d408ebe0SOlga Kornievskaia };
445d408ebe0SOlga Kornievskaia 
44674678748SOlga Kornievskaia void rpc_sysfs_exit(void)
44774678748SOlga Kornievskaia {
448c441f125SOlga Kornievskaia 	kobject_put(rpc_sunrpc_client_kobj);
449baea9944SOlga Kornievskaia 	kobject_put(rpc_sunrpc_xprt_switch_kobj);
45074678748SOlga Kornievskaia 	kset_unregister(rpc_sunrpc_kset);
45174678748SOlga Kornievskaia }
452c5a382ebSOlga Kornievskaia 
453c5a382ebSOlga Kornievskaia static struct rpc_sysfs_client *rpc_sysfs_client_alloc(struct kobject *parent,
454c5a382ebSOlga Kornievskaia 						       struct net *net,
455c5a382ebSOlga Kornievskaia 						       int clid)
456c5a382ebSOlga Kornievskaia {
457c5a382ebSOlga Kornievskaia 	struct rpc_sysfs_client *p;
458c5a382ebSOlga Kornievskaia 
459c5a382ebSOlga Kornievskaia 	p = kzalloc(sizeof(*p), GFP_KERNEL);
460c5a382ebSOlga Kornievskaia 	if (p) {
461c5a382ebSOlga Kornievskaia 		p->net = net;
462c5a382ebSOlga Kornievskaia 		p->kobject.kset = rpc_sunrpc_kset;
463c5a382ebSOlga Kornievskaia 		if (kobject_init_and_add(&p->kobject, &rpc_sysfs_client_type,
464c5a382ebSOlga Kornievskaia 					 parent, "clnt-%d", clid) == 0)
465c5a382ebSOlga Kornievskaia 			return p;
466c5a382ebSOlga Kornievskaia 		kobject_put(&p->kobject);
467c5a382ebSOlga Kornievskaia 	}
468c5a382ebSOlga Kornievskaia 	return NULL;
469c5a382ebSOlga Kornievskaia }
470c5a382ebSOlga Kornievskaia 
471baea9944SOlga Kornievskaia static struct rpc_sysfs_xprt_switch *
472baea9944SOlga Kornievskaia rpc_sysfs_xprt_switch_alloc(struct kobject *parent,
473baea9944SOlga Kornievskaia 			    struct rpc_xprt_switch *xprt_switch,
474baea9944SOlga Kornievskaia 			    struct net *net,
475baea9944SOlga Kornievskaia 			    gfp_t gfp_flags)
476baea9944SOlga Kornievskaia {
477baea9944SOlga Kornievskaia 	struct rpc_sysfs_xprt_switch *p;
478baea9944SOlga Kornievskaia 
479baea9944SOlga Kornievskaia 	p = kzalloc(sizeof(*p), gfp_flags);
480baea9944SOlga Kornievskaia 	if (p) {
481baea9944SOlga Kornievskaia 		p->net = net;
482baea9944SOlga Kornievskaia 		p->kobject.kset = rpc_sunrpc_kset;
483baea9944SOlga Kornievskaia 		if (kobject_init_and_add(&p->kobject,
484baea9944SOlga Kornievskaia 					 &rpc_sysfs_xprt_switch_type,
485baea9944SOlga Kornievskaia 					 parent, "switch-%d",
486baea9944SOlga Kornievskaia 					 xprt_switch->xps_id) == 0)
487baea9944SOlga Kornievskaia 			return p;
488baea9944SOlga Kornievskaia 		kobject_put(&p->kobject);
489baea9944SOlga Kornievskaia 	}
490baea9944SOlga Kornievskaia 	return NULL;
491baea9944SOlga Kornievskaia }
492baea9944SOlga Kornievskaia 
493d408ebe0SOlga Kornievskaia static struct rpc_sysfs_xprt *rpc_sysfs_xprt_alloc(struct kobject *parent,
494d408ebe0SOlga Kornievskaia 						   struct rpc_xprt *xprt,
495d408ebe0SOlga Kornievskaia 						   gfp_t gfp_flags)
496d408ebe0SOlga Kornievskaia {
497d408ebe0SOlga Kornievskaia 	struct rpc_sysfs_xprt *p;
498d408ebe0SOlga Kornievskaia 
499d408ebe0SOlga Kornievskaia 	p = kzalloc(sizeof(*p), gfp_flags);
500d408ebe0SOlga Kornievskaia 	if (!p)
501d408ebe0SOlga Kornievskaia 		goto out;
502d408ebe0SOlga Kornievskaia 	p->kobject.kset = rpc_sunrpc_kset;
503d408ebe0SOlga Kornievskaia 	if (kobject_init_and_add(&p->kobject, &rpc_sysfs_xprt_type,
504d408ebe0SOlga Kornievskaia 				 parent, "xprt-%d-%s", xprt->id,
505d408ebe0SOlga Kornievskaia 				 xprt->address_strings[RPC_DISPLAY_PROTO]) == 0)
506d408ebe0SOlga Kornievskaia 		return p;
507d408ebe0SOlga Kornievskaia 	kobject_put(&p->kobject);
508d408ebe0SOlga Kornievskaia out:
509d408ebe0SOlga Kornievskaia 	return NULL;
510d408ebe0SOlga Kornievskaia }
511d408ebe0SOlga Kornievskaia 
5122a338a54SOlga Kornievskaia void rpc_sysfs_client_setup(struct rpc_clnt *clnt,
5132a338a54SOlga Kornievskaia 			    struct rpc_xprt_switch *xprt_switch,
5142a338a54SOlga Kornievskaia 			    struct net *net)
515c5a382ebSOlga Kornievskaia {
516c5a382ebSOlga Kornievskaia 	struct rpc_sysfs_client *rpc_client;
517c5a382ebSOlga Kornievskaia 
5182a338a54SOlga Kornievskaia 	rpc_client = rpc_sysfs_client_alloc(rpc_sunrpc_client_kobj,
5192a338a54SOlga Kornievskaia 					    net, clnt->cl_clid);
520c5a382ebSOlga Kornievskaia 	if (rpc_client) {
5212a338a54SOlga Kornievskaia 		char name[] = "switch";
5222a338a54SOlga Kornievskaia 		struct rpc_sysfs_xprt_switch *xswitch =
5232a338a54SOlga Kornievskaia 			(struct rpc_sysfs_xprt_switch *)xprt_switch->xps_sysfs;
5242a338a54SOlga Kornievskaia 		int ret;
5252a338a54SOlga Kornievskaia 
526c5a382ebSOlga Kornievskaia 		clnt->cl_sysfs = rpc_client;
5272a338a54SOlga Kornievskaia 		rpc_client->clnt = clnt;
5282a338a54SOlga Kornievskaia 		rpc_client->xprt_switch = xprt_switch;
529c5a382ebSOlga Kornievskaia 		kobject_uevent(&rpc_client->kobject, KOBJ_ADD);
5302a338a54SOlga Kornievskaia 		ret = sysfs_create_link_nowarn(&rpc_client->kobject,
5312a338a54SOlga Kornievskaia 					       &xswitch->kobject, name);
5322a338a54SOlga Kornievskaia 		if (ret)
5332a338a54SOlga Kornievskaia 			pr_warn("can't create link to %s in sysfs (%d)\n",
5342a338a54SOlga Kornievskaia 				name, ret);
535c5a382ebSOlga Kornievskaia 	}
536c5a382ebSOlga Kornievskaia }
537c5a382ebSOlga Kornievskaia 
538baea9944SOlga Kornievskaia void rpc_sysfs_xprt_switch_setup(struct rpc_xprt_switch *xprt_switch,
539baea9944SOlga Kornievskaia 				 struct rpc_xprt *xprt,
540baea9944SOlga Kornievskaia 				 gfp_t gfp_flags)
541baea9944SOlga Kornievskaia {
542baea9944SOlga Kornievskaia 	struct rpc_sysfs_xprt_switch *rpc_xprt_switch;
543baea9944SOlga Kornievskaia 	struct net *net;
544baea9944SOlga Kornievskaia 
545baea9944SOlga Kornievskaia 	if (xprt_switch->xps_net)
546baea9944SOlga Kornievskaia 		net = xprt_switch->xps_net;
547baea9944SOlga Kornievskaia 	else
548baea9944SOlga Kornievskaia 		net = xprt->xprt_net;
549baea9944SOlga Kornievskaia 	rpc_xprt_switch =
550baea9944SOlga Kornievskaia 		rpc_sysfs_xprt_switch_alloc(rpc_sunrpc_xprt_switch_kobj,
551baea9944SOlga Kornievskaia 					    xprt_switch, net, gfp_flags);
552baea9944SOlga Kornievskaia 	if (rpc_xprt_switch) {
553baea9944SOlga Kornievskaia 		xprt_switch->xps_sysfs = rpc_xprt_switch;
554baea9944SOlga Kornievskaia 		rpc_xprt_switch->xprt_switch = xprt_switch;
555baea9944SOlga Kornievskaia 		rpc_xprt_switch->xprt = xprt;
556baea9944SOlga Kornievskaia 		kobject_uevent(&rpc_xprt_switch->kobject, KOBJ_ADD);
557baea9944SOlga Kornievskaia 	}
558baea9944SOlga Kornievskaia }
559baea9944SOlga Kornievskaia 
560d408ebe0SOlga Kornievskaia void rpc_sysfs_xprt_setup(struct rpc_xprt_switch *xprt_switch,
561d408ebe0SOlga Kornievskaia 			  struct rpc_xprt *xprt,
562d408ebe0SOlga Kornievskaia 			  gfp_t gfp_flags)
563d408ebe0SOlga Kornievskaia {
564d408ebe0SOlga Kornievskaia 	struct rpc_sysfs_xprt *rpc_xprt;
565d408ebe0SOlga Kornievskaia 	struct rpc_sysfs_xprt_switch *switch_obj =
566d408ebe0SOlga Kornievskaia 		(struct rpc_sysfs_xprt_switch *)xprt_switch->xps_sysfs;
567d408ebe0SOlga Kornievskaia 
568d408ebe0SOlga Kornievskaia 	rpc_xprt = rpc_sysfs_xprt_alloc(&switch_obj->kobject, xprt, gfp_flags);
569d408ebe0SOlga Kornievskaia 	if (rpc_xprt) {
570d408ebe0SOlga Kornievskaia 		xprt->xprt_sysfs = rpc_xprt;
571d408ebe0SOlga Kornievskaia 		rpc_xprt->xprt = xprt;
5725b7eb784SOlga Kornievskaia 		rpc_xprt->xprt_switch = xprt_switch;
573d408ebe0SOlga Kornievskaia 		kobject_uevent(&rpc_xprt->kobject, KOBJ_ADD);
574d408ebe0SOlga Kornievskaia 	}
575d408ebe0SOlga Kornievskaia }
576d408ebe0SOlga Kornievskaia 
577c5a382ebSOlga Kornievskaia void rpc_sysfs_client_destroy(struct rpc_clnt *clnt)
578c5a382ebSOlga Kornievskaia {
579c5a382ebSOlga Kornievskaia 	struct rpc_sysfs_client *rpc_client = clnt->cl_sysfs;
580c5a382ebSOlga Kornievskaia 
581c5a382ebSOlga Kornievskaia 	if (rpc_client) {
5822a338a54SOlga Kornievskaia 		char name[] = "switch";
5832a338a54SOlga Kornievskaia 
5842a338a54SOlga Kornievskaia 		sysfs_remove_link(&rpc_client->kobject, name);
585c5a382ebSOlga Kornievskaia 		kobject_uevent(&rpc_client->kobject, KOBJ_REMOVE);
586c5a382ebSOlga Kornievskaia 		kobject_del(&rpc_client->kobject);
587c5a382ebSOlga Kornievskaia 		kobject_put(&rpc_client->kobject);
588c5a382ebSOlga Kornievskaia 		clnt->cl_sysfs = NULL;
589c5a382ebSOlga Kornievskaia 	}
590c5a382ebSOlga Kornievskaia }
591baea9944SOlga Kornievskaia 
592baea9944SOlga Kornievskaia void rpc_sysfs_xprt_switch_destroy(struct rpc_xprt_switch *xprt_switch)
593baea9944SOlga Kornievskaia {
594baea9944SOlga Kornievskaia 	struct rpc_sysfs_xprt_switch *rpc_xprt_switch = xprt_switch->xps_sysfs;
595baea9944SOlga Kornievskaia 
596baea9944SOlga Kornievskaia 	if (rpc_xprt_switch) {
597baea9944SOlga Kornievskaia 		kobject_uevent(&rpc_xprt_switch->kobject, KOBJ_REMOVE);
598baea9944SOlga Kornievskaia 		kobject_del(&rpc_xprt_switch->kobject);
599baea9944SOlga Kornievskaia 		kobject_put(&rpc_xprt_switch->kobject);
600baea9944SOlga Kornievskaia 		xprt_switch->xps_sysfs = NULL;
601baea9944SOlga Kornievskaia 	}
602baea9944SOlga Kornievskaia }
603d408ebe0SOlga Kornievskaia 
604d408ebe0SOlga Kornievskaia void rpc_sysfs_xprt_destroy(struct rpc_xprt *xprt)
605d408ebe0SOlga Kornievskaia {
606d408ebe0SOlga Kornievskaia 	struct rpc_sysfs_xprt *rpc_xprt = xprt->xprt_sysfs;
607d408ebe0SOlga Kornievskaia 
608d408ebe0SOlga Kornievskaia 	if (rpc_xprt) {
609d408ebe0SOlga Kornievskaia 		kobject_uevent(&rpc_xprt->kobject, KOBJ_REMOVE);
610d408ebe0SOlga Kornievskaia 		kobject_del(&rpc_xprt->kobject);
611d408ebe0SOlga Kornievskaia 		kobject_put(&rpc_xprt->kobject);
612d408ebe0SOlga Kornievskaia 		xprt->xprt_sysfs = NULL;
613d408ebe0SOlga Kornievskaia 	}
614d408ebe0SOlga Kornievskaia }
615