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