xref: /openbmc/linux/net/sunrpc/sysfs.c (revision 69f2cd6d)
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 
103e44773daSAnna Schumaker static ssize_t rpc_sysfs_xprt_srcaddr_show(struct kobject *kobj,
104e44773daSAnna Schumaker 					   struct kobj_attribute *attr,
105e44773daSAnna Schumaker 					   char *buf)
106e44773daSAnna Schumaker {
107e44773daSAnna Schumaker 	struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
108e44773daSAnna Schumaker 	struct sockaddr_storage saddr;
109e44773daSAnna Schumaker 	struct sock_xprt *sock;
110e44773daSAnna Schumaker 	ssize_t ret = -1;
111e44773daSAnna Schumaker 
112e44773daSAnna Schumaker 	if (!xprt)
113e44773daSAnna Schumaker 		return 0;
114e44773daSAnna Schumaker 
115e44773daSAnna Schumaker 	sock = container_of(xprt, struct sock_xprt, xprt);
116e44773daSAnna Schumaker 	if (kernel_getsockname(sock->sock, (struct sockaddr *)&saddr) < 0)
117e44773daSAnna Schumaker 		goto out;
118e44773daSAnna Schumaker 
119e44773daSAnna Schumaker 	ret = sprintf(buf, "%pISc\n", &saddr);
120e44773daSAnna Schumaker out:
121e44773daSAnna Schumaker 	xprt_put(xprt);
122e44773daSAnna Schumaker 	return ret + 1;
123e44773daSAnna Schumaker }
124e44773daSAnna 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"
139*69f2cd6dSAnna Schumaker 		       "tasks_queuelen=%ld\ndst_port=%s\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,
146*69f2cd6dSAnna Schumaker 		       atomic_long_read(&xprt->queuelen),
147*69f2cd6dSAnna Schumaker 		       (xprt->xprt_class->ident == XPRT_TRANSPORT_TCP) ?
148*69f2cd6dSAnna Schumaker 				xprt->address_strings[RPC_DISPLAY_PORT] : "0");
1494a09651aSOlga Kornievskaia 	xprt_put(xprt);
1504a09651aSOlga Kornievskaia 	return ret + 1;
1514a09651aSOlga Kornievskaia }
1524a09651aSOlga Kornievskaia 
153681d5699SOlga Kornievskaia static ssize_t rpc_sysfs_xprt_state_show(struct kobject *kobj,
154681d5699SOlga Kornievskaia 					 struct kobj_attribute *attr,
155681d5699SOlga Kornievskaia 					 char *buf)
156681d5699SOlga Kornievskaia {
157681d5699SOlga Kornievskaia 	struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
158681d5699SOlga Kornievskaia 	ssize_t ret;
159681d5699SOlga Kornievskaia 	int locked, connected, connecting, close_wait, bound, binding,
1606f081693SOlga Kornievskaia 	    closing, congested, cwnd_wait, write_space, offline, remove;
161681d5699SOlga Kornievskaia 
162681d5699SOlga Kornievskaia 	if (!xprt)
163681d5699SOlga Kornievskaia 		return 0;
164681d5699SOlga Kornievskaia 
165681d5699SOlga Kornievskaia 	if (!xprt->state) {
166681d5699SOlga Kornievskaia 		ret = sprintf(buf, "state=CLOSED\n");
167681d5699SOlga Kornievskaia 	} else {
168681d5699SOlga Kornievskaia 		locked = test_bit(XPRT_LOCKED, &xprt->state);
169681d5699SOlga Kornievskaia 		connected = test_bit(XPRT_CONNECTED, &xprt->state);
170681d5699SOlga Kornievskaia 		connecting = test_bit(XPRT_CONNECTING, &xprt->state);
171681d5699SOlga Kornievskaia 		close_wait = test_bit(XPRT_CLOSE_WAIT, &xprt->state);
172681d5699SOlga Kornievskaia 		bound = test_bit(XPRT_BOUND, &xprt->state);
173681d5699SOlga Kornievskaia 		binding = test_bit(XPRT_BINDING, &xprt->state);
174681d5699SOlga Kornievskaia 		closing = test_bit(XPRT_CLOSING, &xprt->state);
175681d5699SOlga Kornievskaia 		congested = test_bit(XPRT_CONGESTED, &xprt->state);
176681d5699SOlga Kornievskaia 		cwnd_wait = test_bit(XPRT_CWND_WAIT, &xprt->state);
177681d5699SOlga Kornievskaia 		write_space = test_bit(XPRT_WRITE_SPACE, &xprt->state);
1785b7eb784SOlga Kornievskaia 		offline = test_bit(XPRT_OFFLINE, &xprt->state);
1796f081693SOlga Kornievskaia 		remove = test_bit(XPRT_REMOVE, &xprt->state);
180681d5699SOlga Kornievskaia 
1816f081693SOlga Kornievskaia 		ret = sprintf(buf, "state=%s %s %s %s %s %s %s %s %s %s %s %s\n",
182681d5699SOlga Kornievskaia 			      locked ? "LOCKED" : "",
183681d5699SOlga Kornievskaia 			      connected ? "CONNECTED" : "",
184681d5699SOlga Kornievskaia 			      connecting ? "CONNECTING" : "",
185681d5699SOlga Kornievskaia 			      close_wait ? "CLOSE_WAIT" : "",
186681d5699SOlga Kornievskaia 			      bound ? "BOUND" : "",
187681d5699SOlga Kornievskaia 			      binding ? "BOUNDING" : "",
188681d5699SOlga Kornievskaia 			      closing ? "CLOSING" : "",
189681d5699SOlga Kornievskaia 			      congested ? "CONGESTED" : "",
190681d5699SOlga Kornievskaia 			      cwnd_wait ? "CWND_WAIT" : "",
1915b7eb784SOlga Kornievskaia 			      write_space ? "WRITE_SPACE" : "",
1926f081693SOlga Kornievskaia 			      offline ? "OFFLINE" : "",
1936f081693SOlga Kornievskaia 			      remove ? "REMOVE" : "");
194681d5699SOlga Kornievskaia 	}
195681d5699SOlga Kornievskaia 
196681d5699SOlga Kornievskaia 	xprt_put(xprt);
197681d5699SOlga Kornievskaia 	return ret + 1;
198681d5699SOlga Kornievskaia }
199681d5699SOlga Kornievskaia 
2000e559035SOlga Kornievskaia static ssize_t rpc_sysfs_xprt_switch_info_show(struct kobject *kobj,
2010e559035SOlga Kornievskaia 					       struct kobj_attribute *attr,
2020e559035SOlga Kornievskaia 					       char *buf)
2030e559035SOlga Kornievskaia {
2040e559035SOlga Kornievskaia 	struct rpc_xprt_switch *xprt_switch =
2050e559035SOlga Kornievskaia 		rpc_sysfs_xprt_switch_kobj_get_xprt(kobj);
2060e559035SOlga Kornievskaia 	ssize_t ret;
2070e559035SOlga Kornievskaia 
2080e559035SOlga Kornievskaia 	if (!xprt_switch)
2090e559035SOlga Kornievskaia 		return 0;
2100e559035SOlga Kornievskaia 	ret = sprintf(buf, "num_xprts=%u\nnum_active=%u\nqueue_len=%ld\n",
2110e559035SOlga Kornievskaia 		      xprt_switch->xps_nxprts, xprt_switch->xps_nactive,
2120e559035SOlga Kornievskaia 		      atomic_long_read(&xprt_switch->xps_queuelen));
2130e559035SOlga Kornievskaia 	xprt_switch_put(xprt_switch);
2140e559035SOlga Kornievskaia 	return ret + 1;
2150e559035SOlga Kornievskaia }
2160e559035SOlga Kornievskaia 
217587bc725SOlga Kornievskaia static ssize_t rpc_sysfs_xprt_dstaddr_store(struct kobject *kobj,
218587bc725SOlga Kornievskaia 					    struct kobj_attribute *attr,
219587bc725SOlga Kornievskaia 					    const char *buf, size_t count)
220587bc725SOlga Kornievskaia {
221587bc725SOlga Kornievskaia 	struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
222587bc725SOlga Kornievskaia 	struct sockaddr *saddr;
223587bc725SOlga Kornievskaia 	char *dst_addr;
224587bc725SOlga Kornievskaia 	int port;
225587bc725SOlga Kornievskaia 	struct xprt_addr *saved_addr;
226587bc725SOlga Kornievskaia 	size_t buf_len;
227587bc725SOlga Kornievskaia 
228587bc725SOlga Kornievskaia 	if (!xprt)
229587bc725SOlga Kornievskaia 		return 0;
230587bc725SOlga Kornievskaia 	if (!(xprt->xprt_class->ident == XPRT_TRANSPORT_TCP ||
231587bc725SOlga Kornievskaia 	      xprt->xprt_class->ident == XPRT_TRANSPORT_RDMA)) {
232587bc725SOlga Kornievskaia 		xprt_put(xprt);
233587bc725SOlga Kornievskaia 		return -EOPNOTSUPP;
234587bc725SOlga Kornievskaia 	}
235587bc725SOlga Kornievskaia 
236587bc725SOlga Kornievskaia 	if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) {
237587bc725SOlga Kornievskaia 		count = -EINTR;
238587bc725SOlga Kornievskaia 		goto out_put;
239587bc725SOlga Kornievskaia 	}
240587bc725SOlga Kornievskaia 	saddr = (struct sockaddr *)&xprt->addr;
241587bc725SOlga Kornievskaia 	port = rpc_get_port(saddr);
242587bc725SOlga Kornievskaia 
243587bc725SOlga Kornievskaia 	/* buf_len is the len until the first occurence of either
244587bc725SOlga Kornievskaia 	 * '\n' or '\0'
245587bc725SOlga Kornievskaia 	 */
246587bc725SOlga Kornievskaia 	buf_len = strcspn(buf, "\n");
247587bc725SOlga Kornievskaia 
248587bc725SOlga Kornievskaia 	dst_addr = kstrndup(buf, buf_len, GFP_KERNEL);
249587bc725SOlga Kornievskaia 	if (!dst_addr)
250587bc725SOlga Kornievskaia 		goto out_err;
251587bc725SOlga Kornievskaia 	saved_addr = kzalloc(sizeof(*saved_addr), GFP_KERNEL);
252587bc725SOlga Kornievskaia 	if (!saved_addr)
253587bc725SOlga Kornievskaia 		goto out_err_free;
254587bc725SOlga Kornievskaia 	saved_addr->addr =
255587bc725SOlga Kornievskaia 		rcu_dereference_raw(xprt->address_strings[RPC_DISPLAY_ADDR]);
256587bc725SOlga Kornievskaia 	rcu_assign_pointer(xprt->address_strings[RPC_DISPLAY_ADDR], dst_addr);
257587bc725SOlga Kornievskaia 	call_rcu(&saved_addr->rcu, free_xprt_addr);
258587bc725SOlga Kornievskaia 	xprt->addrlen = rpc_pton(xprt->xprt_net, buf, buf_len, saddr,
259587bc725SOlga Kornievskaia 				 sizeof(*saddr));
260587bc725SOlga Kornievskaia 	rpc_set_port(saddr, port);
261587bc725SOlga Kornievskaia 
262587bc725SOlga Kornievskaia 	xprt_force_disconnect(xprt);
263587bc725SOlga Kornievskaia out:
264587bc725SOlga Kornievskaia 	xprt_release_write(xprt, NULL);
265587bc725SOlga Kornievskaia out_put:
266587bc725SOlga Kornievskaia 	xprt_put(xprt);
267587bc725SOlga Kornievskaia 	return count;
268587bc725SOlga Kornievskaia out_err_free:
269587bc725SOlga Kornievskaia 	kfree(dst_addr);
270587bc725SOlga Kornievskaia out_err:
271587bc725SOlga Kornievskaia 	count = -ENOMEM;
272587bc725SOlga Kornievskaia 	goto out;
273587bc725SOlga Kornievskaia }
274587bc725SOlga Kornievskaia 
2755b7eb784SOlga Kornievskaia static ssize_t rpc_sysfs_xprt_state_change(struct kobject *kobj,
2765b7eb784SOlga Kornievskaia 					   struct kobj_attribute *attr,
2775b7eb784SOlga Kornievskaia 					   const char *buf, size_t count)
2785b7eb784SOlga Kornievskaia {
2795b7eb784SOlga Kornievskaia 	struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj);
2806f081693SOlga Kornievskaia 	int offline = 0, online = 0, remove = 0;
2815b7eb784SOlga Kornievskaia 	struct rpc_xprt_switch *xps = rpc_sysfs_xprt_kobj_get_xprt_switch(kobj);
2825b7eb784SOlga Kornievskaia 
2835b7eb784SOlga Kornievskaia 	if (!xprt)
2845b7eb784SOlga Kornievskaia 		return 0;
2855b7eb784SOlga Kornievskaia 
2865b7eb784SOlga Kornievskaia 	if (!strncmp(buf, "offline", 7))
2875b7eb784SOlga Kornievskaia 		offline = 1;
2885b7eb784SOlga Kornievskaia 	else if (!strncmp(buf, "online", 6))
2895b7eb784SOlga Kornievskaia 		online = 1;
2906f081693SOlga Kornievskaia 	else if (!strncmp(buf, "remove", 6))
2916f081693SOlga Kornievskaia 		remove = 1;
2925b7eb784SOlga Kornievskaia 	else
2935b7eb784SOlga Kornievskaia 		return -EINVAL;
2945b7eb784SOlga Kornievskaia 
2955b7eb784SOlga Kornievskaia 	if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) {
2965b7eb784SOlga Kornievskaia 		count = -EINTR;
2975b7eb784SOlga Kornievskaia 		goto out_put;
2985b7eb784SOlga Kornievskaia 	}
2995b7eb784SOlga Kornievskaia 	if (xprt->main) {
3005b7eb784SOlga Kornievskaia 		count = -EINVAL;
3015b7eb784SOlga Kornievskaia 		goto release_tasks;
3025b7eb784SOlga Kornievskaia 	}
3035b7eb784SOlga Kornievskaia 	if (offline) {
3045b7eb784SOlga Kornievskaia 		set_bit(XPRT_OFFLINE, &xprt->state);
3055b7eb784SOlga Kornievskaia 		spin_lock(&xps->xps_lock);
3065b7eb784SOlga Kornievskaia 		xps->xps_nactive--;
3075b7eb784SOlga Kornievskaia 		spin_unlock(&xps->xps_lock);
3085b7eb784SOlga Kornievskaia 	} else if (online) {
3095b7eb784SOlga Kornievskaia 		clear_bit(XPRT_OFFLINE, &xprt->state);
3105b7eb784SOlga Kornievskaia 		spin_lock(&xps->xps_lock);
3115b7eb784SOlga Kornievskaia 		xps->xps_nactive++;
3125b7eb784SOlga Kornievskaia 		spin_unlock(&xps->xps_lock);
3136f081693SOlga Kornievskaia 	} else if (remove) {
3146f081693SOlga Kornievskaia 		if (test_bit(XPRT_OFFLINE, &xprt->state)) {
3156f081693SOlga Kornievskaia 			set_bit(XPRT_REMOVE, &xprt->state);
3166f081693SOlga Kornievskaia 			xprt_force_disconnect(xprt);
3176f081693SOlga Kornievskaia 			if (test_bit(XPRT_CONNECTED, &xprt->state)) {
3186f081693SOlga Kornievskaia 				if (!xprt->sending.qlen &&
3196f081693SOlga Kornievskaia 				    !xprt->pending.qlen &&
3206f081693SOlga Kornievskaia 				    !xprt->backlog.qlen &&
3216f081693SOlga Kornievskaia 				    !atomic_long_read(&xprt->queuelen))
3226f081693SOlga Kornievskaia 					rpc_xprt_switch_remove_xprt(xps, xprt);
3236f081693SOlga Kornievskaia 			}
3246f081693SOlga Kornievskaia 		} else {
3256f081693SOlga Kornievskaia 			count = -EINVAL;
3266f081693SOlga Kornievskaia 		}
3275b7eb784SOlga Kornievskaia 	}
3285b7eb784SOlga Kornievskaia 
3295b7eb784SOlga Kornievskaia release_tasks:
3305b7eb784SOlga Kornievskaia 	xprt_release_write(xprt, NULL);
3315b7eb784SOlga Kornievskaia out_put:
3325b7eb784SOlga Kornievskaia 	xprt_put(xprt);
3335b7eb784SOlga Kornievskaia 	xprt_switch_put(xps);
3345b7eb784SOlga Kornievskaia 	return count;
3355b7eb784SOlga Kornievskaia }
3365b7eb784SOlga Kornievskaia 
33774678748SOlga Kornievskaia int rpc_sysfs_init(void)
33874678748SOlga Kornievskaia {
33974678748SOlga Kornievskaia 	rpc_sunrpc_kset = kset_create_and_add("sunrpc", NULL, kernel_kobj);
34074678748SOlga Kornievskaia 	if (!rpc_sunrpc_kset)
34174678748SOlga Kornievskaia 		return -ENOMEM;
342baea9944SOlga Kornievskaia 	rpc_sunrpc_client_kobj =
343baea9944SOlga Kornievskaia 		rpc_sysfs_object_alloc("rpc-clients", rpc_sunrpc_kset, NULL);
344baea9944SOlga Kornievskaia 	if (!rpc_sunrpc_client_kobj)
345baea9944SOlga Kornievskaia 		goto err_client;
346baea9944SOlga Kornievskaia 	rpc_sunrpc_xprt_switch_kobj =
347baea9944SOlga Kornievskaia 		rpc_sysfs_object_alloc("xprt-switches", rpc_sunrpc_kset, NULL);
348baea9944SOlga Kornievskaia 	if (!rpc_sunrpc_xprt_switch_kobj)
349baea9944SOlga Kornievskaia 		goto err_switch;
35074678748SOlga Kornievskaia 	return 0;
351baea9944SOlga Kornievskaia err_switch:
352baea9944SOlga Kornievskaia 	kobject_put(rpc_sunrpc_client_kobj);
353baea9944SOlga Kornievskaia 	rpc_sunrpc_client_kobj = NULL;
354baea9944SOlga Kornievskaia err_client:
355baea9944SOlga Kornievskaia 	kset_unregister(rpc_sunrpc_kset);
356baea9944SOlga Kornievskaia 	rpc_sunrpc_kset = NULL;
357baea9944SOlga Kornievskaia 	return -ENOMEM;
35874678748SOlga Kornievskaia }
35974678748SOlga Kornievskaia 
360c5a382ebSOlga Kornievskaia static void rpc_sysfs_client_release(struct kobject *kobj)
361c5a382ebSOlga Kornievskaia {
362c5a382ebSOlga Kornievskaia 	struct rpc_sysfs_client *c;
363c5a382ebSOlga Kornievskaia 
364c5a382ebSOlga Kornievskaia 	c = container_of(kobj, struct rpc_sysfs_client, kobject);
365c5a382ebSOlga Kornievskaia 	kfree(c);
366c5a382ebSOlga Kornievskaia }
367c5a382ebSOlga Kornievskaia 
368baea9944SOlga Kornievskaia static void rpc_sysfs_xprt_switch_release(struct kobject *kobj)
369baea9944SOlga Kornievskaia {
370baea9944SOlga Kornievskaia 	struct rpc_sysfs_xprt_switch *xprt_switch;
371baea9944SOlga Kornievskaia 
372baea9944SOlga Kornievskaia 	xprt_switch = container_of(kobj, struct rpc_sysfs_xprt_switch, kobject);
373baea9944SOlga Kornievskaia 	kfree(xprt_switch);
374baea9944SOlga Kornievskaia }
375baea9944SOlga Kornievskaia 
376d408ebe0SOlga Kornievskaia static void rpc_sysfs_xprt_release(struct kobject *kobj)
377d408ebe0SOlga Kornievskaia {
378d408ebe0SOlga Kornievskaia 	struct rpc_sysfs_xprt *xprt;
379d408ebe0SOlga Kornievskaia 
380d408ebe0SOlga Kornievskaia 	xprt = container_of(kobj, struct rpc_sysfs_xprt, kobject);
381d408ebe0SOlga Kornievskaia 	kfree(xprt);
382d408ebe0SOlga Kornievskaia }
383d408ebe0SOlga Kornievskaia 
384c5a382ebSOlga Kornievskaia static const void *rpc_sysfs_client_namespace(struct kobject *kobj)
385c5a382ebSOlga Kornievskaia {
386c5a382ebSOlga Kornievskaia 	return container_of(kobj, struct rpc_sysfs_client, kobject)->net;
387c5a382ebSOlga Kornievskaia }
388c5a382ebSOlga Kornievskaia 
389baea9944SOlga Kornievskaia static const void *rpc_sysfs_xprt_switch_namespace(struct kobject *kobj)
390baea9944SOlga Kornievskaia {
391baea9944SOlga Kornievskaia 	return container_of(kobj, struct rpc_sysfs_xprt_switch, kobject)->net;
392baea9944SOlga Kornievskaia }
393baea9944SOlga Kornievskaia 
394d408ebe0SOlga Kornievskaia static const void *rpc_sysfs_xprt_namespace(struct kobject *kobj)
395d408ebe0SOlga Kornievskaia {
396d408ebe0SOlga Kornievskaia 	return container_of(kobj, struct rpc_sysfs_xprt,
397d408ebe0SOlga Kornievskaia 			    kobject)->xprt->xprt_net;
398d408ebe0SOlga Kornievskaia }
399d408ebe0SOlga Kornievskaia 
400587bc725SOlga Kornievskaia static struct kobj_attribute rpc_sysfs_xprt_dstaddr = __ATTR(dstaddr,
401587bc725SOlga Kornievskaia 	0644, rpc_sysfs_xprt_dstaddr_show, rpc_sysfs_xprt_dstaddr_store);
402587bc725SOlga Kornievskaia 
403e44773daSAnna Schumaker static struct kobj_attribute rpc_sysfs_xprt_srcaddr = __ATTR(srcaddr,
404e44773daSAnna Schumaker 	0644, rpc_sysfs_xprt_srcaddr_show, NULL);
405e44773daSAnna Schumaker 
4064a09651aSOlga Kornievskaia static struct kobj_attribute rpc_sysfs_xprt_info = __ATTR(xprt_info,
4074a09651aSOlga Kornievskaia 	0444, rpc_sysfs_xprt_info_show, NULL);
4084a09651aSOlga Kornievskaia 
409681d5699SOlga Kornievskaia static struct kobj_attribute rpc_sysfs_xprt_change_state = __ATTR(xprt_state,
4105b7eb784SOlga Kornievskaia 	0644, rpc_sysfs_xprt_state_show, rpc_sysfs_xprt_state_change);
411681d5699SOlga Kornievskaia 
412587bc725SOlga Kornievskaia static struct attribute *rpc_sysfs_xprt_attrs[] = {
413587bc725SOlga Kornievskaia 	&rpc_sysfs_xprt_dstaddr.attr,
414e44773daSAnna Schumaker 	&rpc_sysfs_xprt_srcaddr.attr,
4154a09651aSOlga Kornievskaia 	&rpc_sysfs_xprt_info.attr,
416681d5699SOlga Kornievskaia 	&rpc_sysfs_xprt_change_state.attr,
417587bc725SOlga Kornievskaia 	NULL,
418587bc725SOlga Kornievskaia };
419587bc725SOlga Kornievskaia 
4200e559035SOlga Kornievskaia static struct kobj_attribute rpc_sysfs_xprt_switch_info =
4210e559035SOlga Kornievskaia 	__ATTR(xprt_switch_info, 0444, rpc_sysfs_xprt_switch_info_show, NULL);
4220e559035SOlga Kornievskaia 
4230e559035SOlga Kornievskaia static struct attribute *rpc_sysfs_xprt_switch_attrs[] = {
4240e559035SOlga Kornievskaia 	&rpc_sysfs_xprt_switch_info.attr,
4250e559035SOlga Kornievskaia 	NULL,
4260e559035SOlga Kornievskaia };
4270e559035SOlga Kornievskaia 
428c5a382ebSOlga Kornievskaia static struct kobj_type rpc_sysfs_client_type = {
429c5a382ebSOlga Kornievskaia 	.release = rpc_sysfs_client_release,
430c5a382ebSOlga Kornievskaia 	.sysfs_ops = &kobj_sysfs_ops,
431c5a382ebSOlga Kornievskaia 	.namespace = rpc_sysfs_client_namespace,
432c5a382ebSOlga Kornievskaia };
433c5a382ebSOlga Kornievskaia 
434baea9944SOlga Kornievskaia static struct kobj_type rpc_sysfs_xprt_switch_type = {
435baea9944SOlga Kornievskaia 	.release = rpc_sysfs_xprt_switch_release,
4360e559035SOlga Kornievskaia 	.default_attrs = rpc_sysfs_xprt_switch_attrs,
437baea9944SOlga Kornievskaia 	.sysfs_ops = &kobj_sysfs_ops,
438baea9944SOlga Kornievskaia 	.namespace = rpc_sysfs_xprt_switch_namespace,
439baea9944SOlga Kornievskaia };
440baea9944SOlga Kornievskaia 
441d408ebe0SOlga Kornievskaia static struct kobj_type rpc_sysfs_xprt_type = {
442d408ebe0SOlga Kornievskaia 	.release = rpc_sysfs_xprt_release,
443587bc725SOlga Kornievskaia 	.default_attrs = rpc_sysfs_xprt_attrs,
444d408ebe0SOlga Kornievskaia 	.sysfs_ops = &kobj_sysfs_ops,
445d408ebe0SOlga Kornievskaia 	.namespace = rpc_sysfs_xprt_namespace,
446d408ebe0SOlga Kornievskaia };
447d408ebe0SOlga Kornievskaia 
44874678748SOlga Kornievskaia void rpc_sysfs_exit(void)
44974678748SOlga Kornievskaia {
450c441f125SOlga Kornievskaia 	kobject_put(rpc_sunrpc_client_kobj);
451baea9944SOlga Kornievskaia 	kobject_put(rpc_sunrpc_xprt_switch_kobj);
45274678748SOlga Kornievskaia 	kset_unregister(rpc_sunrpc_kset);
45374678748SOlga Kornievskaia }
454c5a382ebSOlga Kornievskaia 
455c5a382ebSOlga Kornievskaia static struct rpc_sysfs_client *rpc_sysfs_client_alloc(struct kobject *parent,
456c5a382ebSOlga Kornievskaia 						       struct net *net,
457c5a382ebSOlga Kornievskaia 						       int clid)
458c5a382ebSOlga Kornievskaia {
459c5a382ebSOlga Kornievskaia 	struct rpc_sysfs_client *p;
460c5a382ebSOlga Kornievskaia 
461c5a382ebSOlga Kornievskaia 	p = kzalloc(sizeof(*p), GFP_KERNEL);
462c5a382ebSOlga Kornievskaia 	if (p) {
463c5a382ebSOlga Kornievskaia 		p->net = net;
464c5a382ebSOlga Kornievskaia 		p->kobject.kset = rpc_sunrpc_kset;
465c5a382ebSOlga Kornievskaia 		if (kobject_init_and_add(&p->kobject, &rpc_sysfs_client_type,
466c5a382ebSOlga Kornievskaia 					 parent, "clnt-%d", clid) == 0)
467c5a382ebSOlga Kornievskaia 			return p;
468c5a382ebSOlga Kornievskaia 		kobject_put(&p->kobject);
469c5a382ebSOlga Kornievskaia 	}
470c5a382ebSOlga Kornievskaia 	return NULL;
471c5a382ebSOlga Kornievskaia }
472c5a382ebSOlga Kornievskaia 
473baea9944SOlga Kornievskaia static struct rpc_sysfs_xprt_switch *
474baea9944SOlga Kornievskaia rpc_sysfs_xprt_switch_alloc(struct kobject *parent,
475baea9944SOlga Kornievskaia 			    struct rpc_xprt_switch *xprt_switch,
476baea9944SOlga Kornievskaia 			    struct net *net,
477baea9944SOlga Kornievskaia 			    gfp_t gfp_flags)
478baea9944SOlga Kornievskaia {
479baea9944SOlga Kornievskaia 	struct rpc_sysfs_xprt_switch *p;
480baea9944SOlga Kornievskaia 
481baea9944SOlga Kornievskaia 	p = kzalloc(sizeof(*p), gfp_flags);
482baea9944SOlga Kornievskaia 	if (p) {
483baea9944SOlga Kornievskaia 		p->net = net;
484baea9944SOlga Kornievskaia 		p->kobject.kset = rpc_sunrpc_kset;
485baea9944SOlga Kornievskaia 		if (kobject_init_and_add(&p->kobject,
486baea9944SOlga Kornievskaia 					 &rpc_sysfs_xprt_switch_type,
487baea9944SOlga Kornievskaia 					 parent, "switch-%d",
488baea9944SOlga Kornievskaia 					 xprt_switch->xps_id) == 0)
489baea9944SOlga Kornievskaia 			return p;
490baea9944SOlga Kornievskaia 		kobject_put(&p->kobject);
491baea9944SOlga Kornievskaia 	}
492baea9944SOlga Kornievskaia 	return NULL;
493baea9944SOlga Kornievskaia }
494baea9944SOlga Kornievskaia 
495d408ebe0SOlga Kornievskaia static struct rpc_sysfs_xprt *rpc_sysfs_xprt_alloc(struct kobject *parent,
496d408ebe0SOlga Kornievskaia 						   struct rpc_xprt *xprt,
497d408ebe0SOlga Kornievskaia 						   gfp_t gfp_flags)
498d408ebe0SOlga Kornievskaia {
499d408ebe0SOlga Kornievskaia 	struct rpc_sysfs_xprt *p;
500d408ebe0SOlga Kornievskaia 
501d408ebe0SOlga Kornievskaia 	p = kzalloc(sizeof(*p), gfp_flags);
502d408ebe0SOlga Kornievskaia 	if (!p)
503d408ebe0SOlga Kornievskaia 		goto out;
504d408ebe0SOlga Kornievskaia 	p->kobject.kset = rpc_sunrpc_kset;
505d408ebe0SOlga Kornievskaia 	if (kobject_init_and_add(&p->kobject, &rpc_sysfs_xprt_type,
506d408ebe0SOlga Kornievskaia 				 parent, "xprt-%d-%s", xprt->id,
507d408ebe0SOlga Kornievskaia 				 xprt->address_strings[RPC_DISPLAY_PROTO]) == 0)
508d408ebe0SOlga Kornievskaia 		return p;
509d408ebe0SOlga Kornievskaia 	kobject_put(&p->kobject);
510d408ebe0SOlga Kornievskaia out:
511d408ebe0SOlga Kornievskaia 	return NULL;
512d408ebe0SOlga Kornievskaia }
513d408ebe0SOlga Kornievskaia 
5142a338a54SOlga Kornievskaia void rpc_sysfs_client_setup(struct rpc_clnt *clnt,
5152a338a54SOlga Kornievskaia 			    struct rpc_xprt_switch *xprt_switch,
5162a338a54SOlga Kornievskaia 			    struct net *net)
517c5a382ebSOlga Kornievskaia {
518c5a382ebSOlga Kornievskaia 	struct rpc_sysfs_client *rpc_client;
519c5a382ebSOlga Kornievskaia 
5202a338a54SOlga Kornievskaia 	rpc_client = rpc_sysfs_client_alloc(rpc_sunrpc_client_kobj,
5212a338a54SOlga Kornievskaia 					    net, clnt->cl_clid);
522c5a382ebSOlga Kornievskaia 	if (rpc_client) {
5232a338a54SOlga Kornievskaia 		char name[] = "switch";
5242a338a54SOlga Kornievskaia 		struct rpc_sysfs_xprt_switch *xswitch =
5252a338a54SOlga Kornievskaia 			(struct rpc_sysfs_xprt_switch *)xprt_switch->xps_sysfs;
5262a338a54SOlga Kornievskaia 		int ret;
5272a338a54SOlga Kornievskaia 
528c5a382ebSOlga Kornievskaia 		clnt->cl_sysfs = rpc_client;
5292a338a54SOlga Kornievskaia 		rpc_client->clnt = clnt;
5302a338a54SOlga Kornievskaia 		rpc_client->xprt_switch = xprt_switch;
531c5a382ebSOlga Kornievskaia 		kobject_uevent(&rpc_client->kobject, KOBJ_ADD);
5322a338a54SOlga Kornievskaia 		ret = sysfs_create_link_nowarn(&rpc_client->kobject,
5332a338a54SOlga Kornievskaia 					       &xswitch->kobject, name);
5342a338a54SOlga Kornievskaia 		if (ret)
5352a338a54SOlga Kornievskaia 			pr_warn("can't create link to %s in sysfs (%d)\n",
5362a338a54SOlga Kornievskaia 				name, ret);
537c5a382ebSOlga Kornievskaia 	}
538c5a382ebSOlga Kornievskaia }
539c5a382ebSOlga Kornievskaia 
540baea9944SOlga Kornievskaia void rpc_sysfs_xprt_switch_setup(struct rpc_xprt_switch *xprt_switch,
541baea9944SOlga Kornievskaia 				 struct rpc_xprt *xprt,
542baea9944SOlga Kornievskaia 				 gfp_t gfp_flags)
543baea9944SOlga Kornievskaia {
544baea9944SOlga Kornievskaia 	struct rpc_sysfs_xprt_switch *rpc_xprt_switch;
545baea9944SOlga Kornievskaia 	struct net *net;
546baea9944SOlga Kornievskaia 
547baea9944SOlga Kornievskaia 	if (xprt_switch->xps_net)
548baea9944SOlga Kornievskaia 		net = xprt_switch->xps_net;
549baea9944SOlga Kornievskaia 	else
550baea9944SOlga Kornievskaia 		net = xprt->xprt_net;
551baea9944SOlga Kornievskaia 	rpc_xprt_switch =
552baea9944SOlga Kornievskaia 		rpc_sysfs_xprt_switch_alloc(rpc_sunrpc_xprt_switch_kobj,
553baea9944SOlga Kornievskaia 					    xprt_switch, net, gfp_flags);
554baea9944SOlga Kornievskaia 	if (rpc_xprt_switch) {
555baea9944SOlga Kornievskaia 		xprt_switch->xps_sysfs = rpc_xprt_switch;
556baea9944SOlga Kornievskaia 		rpc_xprt_switch->xprt_switch = xprt_switch;
557baea9944SOlga Kornievskaia 		rpc_xprt_switch->xprt = xprt;
558baea9944SOlga Kornievskaia 		kobject_uevent(&rpc_xprt_switch->kobject, KOBJ_ADD);
559baea9944SOlga Kornievskaia 	}
560baea9944SOlga Kornievskaia }
561baea9944SOlga Kornievskaia 
562d408ebe0SOlga Kornievskaia void rpc_sysfs_xprt_setup(struct rpc_xprt_switch *xprt_switch,
563d408ebe0SOlga Kornievskaia 			  struct rpc_xprt *xprt,
564d408ebe0SOlga Kornievskaia 			  gfp_t gfp_flags)
565d408ebe0SOlga Kornievskaia {
566d408ebe0SOlga Kornievskaia 	struct rpc_sysfs_xprt *rpc_xprt;
567d408ebe0SOlga Kornievskaia 	struct rpc_sysfs_xprt_switch *switch_obj =
568d408ebe0SOlga Kornievskaia 		(struct rpc_sysfs_xprt_switch *)xprt_switch->xps_sysfs;
569d408ebe0SOlga Kornievskaia 
570d408ebe0SOlga Kornievskaia 	rpc_xprt = rpc_sysfs_xprt_alloc(&switch_obj->kobject, xprt, gfp_flags);
571d408ebe0SOlga Kornievskaia 	if (rpc_xprt) {
572d408ebe0SOlga Kornievskaia 		xprt->xprt_sysfs = rpc_xprt;
573d408ebe0SOlga Kornievskaia 		rpc_xprt->xprt = xprt;
5745b7eb784SOlga Kornievskaia 		rpc_xprt->xprt_switch = xprt_switch;
575d408ebe0SOlga Kornievskaia 		kobject_uevent(&rpc_xprt->kobject, KOBJ_ADD);
576d408ebe0SOlga Kornievskaia 	}
577d408ebe0SOlga Kornievskaia }
578d408ebe0SOlga Kornievskaia 
579c5a382ebSOlga Kornievskaia void rpc_sysfs_client_destroy(struct rpc_clnt *clnt)
580c5a382ebSOlga Kornievskaia {
581c5a382ebSOlga Kornievskaia 	struct rpc_sysfs_client *rpc_client = clnt->cl_sysfs;
582c5a382ebSOlga Kornievskaia 
583c5a382ebSOlga Kornievskaia 	if (rpc_client) {
5842a338a54SOlga Kornievskaia 		char name[] = "switch";
5852a338a54SOlga Kornievskaia 
5862a338a54SOlga Kornievskaia 		sysfs_remove_link(&rpc_client->kobject, name);
587c5a382ebSOlga Kornievskaia 		kobject_uevent(&rpc_client->kobject, KOBJ_REMOVE);
588c5a382ebSOlga Kornievskaia 		kobject_del(&rpc_client->kobject);
589c5a382ebSOlga Kornievskaia 		kobject_put(&rpc_client->kobject);
590c5a382ebSOlga Kornievskaia 		clnt->cl_sysfs = NULL;
591c5a382ebSOlga Kornievskaia 	}
592c5a382ebSOlga Kornievskaia }
593baea9944SOlga Kornievskaia 
594baea9944SOlga Kornievskaia void rpc_sysfs_xprt_switch_destroy(struct rpc_xprt_switch *xprt_switch)
595baea9944SOlga Kornievskaia {
596baea9944SOlga Kornievskaia 	struct rpc_sysfs_xprt_switch *rpc_xprt_switch = xprt_switch->xps_sysfs;
597baea9944SOlga Kornievskaia 
598baea9944SOlga Kornievskaia 	if (rpc_xprt_switch) {
599baea9944SOlga Kornievskaia 		kobject_uevent(&rpc_xprt_switch->kobject, KOBJ_REMOVE);
600baea9944SOlga Kornievskaia 		kobject_del(&rpc_xprt_switch->kobject);
601baea9944SOlga Kornievskaia 		kobject_put(&rpc_xprt_switch->kobject);
602baea9944SOlga Kornievskaia 		xprt_switch->xps_sysfs = NULL;
603baea9944SOlga Kornievskaia 	}
604baea9944SOlga Kornievskaia }
605d408ebe0SOlga Kornievskaia 
606d408ebe0SOlga Kornievskaia void rpc_sysfs_xprt_destroy(struct rpc_xprt *xprt)
607d408ebe0SOlga Kornievskaia {
608d408ebe0SOlga Kornievskaia 	struct rpc_sysfs_xprt *rpc_xprt = xprt->xprt_sysfs;
609d408ebe0SOlga Kornievskaia 
610d408ebe0SOlga Kornievskaia 	if (rpc_xprt) {
611d408ebe0SOlga Kornievskaia 		kobject_uevent(&rpc_xprt->kobject, KOBJ_REMOVE);
612d408ebe0SOlga Kornievskaia 		kobject_del(&rpc_xprt->kobject);
613d408ebe0SOlga Kornievskaia 		kobject_put(&rpc_xprt->kobject);
614d408ebe0SOlga Kornievskaia 		xprt->xprt_sysfs = NULL;
615d408ebe0SOlga Kornievskaia 	}
616d408ebe0SOlga Kornievskaia }
617