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