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