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> |
| 8 | |
Olga Kornievskaia | c5a382e | 2021-06-08 15:59:12 -0400 | [diff] [blame] | 9 | #include "sysfs.h" |
Olga Kornievskaia | 7467874 | 2021-06-08 15:59:10 -0400 | [diff] [blame] | 10 | |
Olga Kornievskaia | 587bc72 | 2021-06-08 15:59:19 -0400 | [diff] [blame] | 11 | struct xprt_addr { |
| 12 | const char *addr; |
| 13 | struct rcu_head rcu; |
| 14 | }; |
| 15 | |
| 16 | static void free_xprt_addr(struct rcu_head *head) |
| 17 | { |
| 18 | struct xprt_addr *addr = container_of(head, struct xprt_addr, rcu); |
| 19 | |
| 20 | kfree(addr->addr); |
| 21 | kfree(addr); |
| 22 | } |
| 23 | |
Olga Kornievskaia | 7467874 | 2021-06-08 15:59:10 -0400 | [diff] [blame] | 24 | static struct kset *rpc_sunrpc_kset; |
Olga Kornievskaia | baea994 | 2021-06-08 15:59:16 -0400 | [diff] [blame] | 25 | static struct kobject *rpc_sunrpc_client_kobj, *rpc_sunrpc_xprt_switch_kobj; |
Olga Kornievskaia | c441f12 | 2021-06-08 15:59:11 -0400 | [diff] [blame] | 26 | |
| 27 | static void rpc_sysfs_object_release(struct kobject *kobj) |
| 28 | { |
| 29 | kfree(kobj); |
| 30 | } |
| 31 | |
| 32 | static const struct kobj_ns_type_operations * |
| 33 | rpc_sysfs_object_child_ns_type(struct kobject *kobj) |
| 34 | { |
| 35 | return &net_ns_type_operations; |
| 36 | } |
| 37 | |
| 38 | static struct kobj_type rpc_sysfs_object_type = { |
| 39 | .release = rpc_sysfs_object_release, |
| 40 | .sysfs_ops = &kobj_sysfs_ops, |
| 41 | .child_ns_type = rpc_sysfs_object_child_ns_type, |
| 42 | }; |
| 43 | |
| 44 | static struct kobject *rpc_sysfs_object_alloc(const char *name, |
| 45 | struct kset *kset, |
| 46 | struct kobject *parent) |
| 47 | { |
| 48 | struct kobject *kobj; |
| 49 | |
| 50 | kobj = kzalloc(sizeof(*kobj), GFP_KERNEL); |
| 51 | if (kobj) { |
| 52 | kobj->kset = kset; |
| 53 | if (kobject_init_and_add(kobj, &rpc_sysfs_object_type, |
| 54 | parent, "%s", name) == 0) |
| 55 | return kobj; |
| 56 | kobject_put(kobj); |
| 57 | } |
| 58 | return NULL; |
| 59 | } |
Olga Kornievskaia | 7467874 | 2021-06-08 15:59:10 -0400 | [diff] [blame] | 60 | |
Olga Kornievskaia | 587bc72 | 2021-06-08 15:59:19 -0400 | [diff] [blame] | 61 | static inline struct rpc_xprt * |
| 62 | rpc_sysfs_xprt_kobj_get_xprt(struct kobject *kobj) |
| 63 | { |
| 64 | struct rpc_sysfs_xprt *x = container_of(kobj, |
| 65 | struct rpc_sysfs_xprt, kobject); |
| 66 | |
| 67 | return xprt_get(x->xprt); |
| 68 | } |
| 69 | |
Olga Kornievskaia | 0e55903 | 2021-06-08 15:59:21 -0400 | [diff] [blame] | 70 | static inline struct rpc_xprt_switch * |
| 71 | rpc_sysfs_xprt_switch_kobj_get_xprt(struct kobject *kobj) |
| 72 | { |
| 73 | struct rpc_sysfs_xprt_switch *x = container_of(kobj, |
| 74 | struct rpc_sysfs_xprt_switch, kobject); |
| 75 | |
| 76 | return xprt_switch_get(x->xprt_switch); |
| 77 | } |
| 78 | |
Olga Kornievskaia | 587bc72 | 2021-06-08 15:59:19 -0400 | [diff] [blame] | 79 | static ssize_t rpc_sysfs_xprt_dstaddr_show(struct kobject *kobj, |
| 80 | struct kobj_attribute *attr, |
| 81 | char *buf) |
| 82 | { |
| 83 | struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj); |
| 84 | ssize_t ret; |
| 85 | |
| 86 | if (!xprt) |
| 87 | return 0; |
| 88 | ret = sprintf(buf, "%s\n", xprt->address_strings[RPC_DISPLAY_ADDR]); |
| 89 | xprt_put(xprt); |
| 90 | return ret + 1; |
| 91 | } |
| 92 | |
Olga Kornievskaia | 4a09651 | 2021-06-08 15:59:20 -0400 | [diff] [blame] | 93 | static ssize_t rpc_sysfs_xprt_info_show(struct kobject *kobj, |
| 94 | struct kobj_attribute *attr, |
| 95 | char *buf) |
| 96 | { |
| 97 | struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj); |
| 98 | ssize_t ret; |
| 99 | |
| 100 | if (!xprt) |
| 101 | return 0; |
| 102 | |
| 103 | ret = sprintf(buf, "last_used=%lu\ncur_cong=%lu\ncong_win=%lu\n" |
| 104 | "max_num_slots=%u\nmin_num_slots=%u\nnum_reqs=%u\n" |
| 105 | "binding_q_len=%u\nsending_q_len=%u\npending_q_len=%u\n" |
| 106 | "backlog_q_len=%u\n", xprt->last_used, xprt->cong, |
| 107 | xprt->cwnd, xprt->max_reqs, xprt->min_reqs, |
| 108 | xprt->num_reqs, xprt->binding.qlen, xprt->sending.qlen, |
| 109 | xprt->pending.qlen, xprt->backlog.qlen); |
| 110 | xprt_put(xprt); |
| 111 | return ret + 1; |
| 112 | } |
| 113 | |
Olga Kornievskaia | 681d569 | 2021-06-08 15:59:22 -0400 | [diff] [blame^] | 114 | static ssize_t rpc_sysfs_xprt_state_show(struct kobject *kobj, |
| 115 | struct kobj_attribute *attr, |
| 116 | char *buf) |
| 117 | { |
| 118 | struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj); |
| 119 | ssize_t ret; |
| 120 | int locked, connected, connecting, close_wait, bound, binding, |
| 121 | closing, congested, cwnd_wait, write_space; |
| 122 | |
| 123 | if (!xprt) |
| 124 | return 0; |
| 125 | |
| 126 | if (!xprt->state) { |
| 127 | ret = sprintf(buf, "state=CLOSED\n"); |
| 128 | } else { |
| 129 | locked = test_bit(XPRT_LOCKED, &xprt->state); |
| 130 | connected = test_bit(XPRT_CONNECTED, &xprt->state); |
| 131 | connecting = test_bit(XPRT_CONNECTING, &xprt->state); |
| 132 | close_wait = test_bit(XPRT_CLOSE_WAIT, &xprt->state); |
| 133 | bound = test_bit(XPRT_BOUND, &xprt->state); |
| 134 | binding = test_bit(XPRT_BINDING, &xprt->state); |
| 135 | closing = test_bit(XPRT_CLOSING, &xprt->state); |
| 136 | congested = test_bit(XPRT_CONGESTED, &xprt->state); |
| 137 | cwnd_wait = test_bit(XPRT_CWND_WAIT, &xprt->state); |
| 138 | write_space = test_bit(XPRT_WRITE_SPACE, &xprt->state); |
| 139 | |
| 140 | ret = sprintf(buf, "state=%s %s %s %s %s %s %s %s %s %s\n", |
| 141 | locked ? "LOCKED" : "", |
| 142 | connected ? "CONNECTED" : "", |
| 143 | connecting ? "CONNECTING" : "", |
| 144 | close_wait ? "CLOSE_WAIT" : "", |
| 145 | bound ? "BOUND" : "", |
| 146 | binding ? "BOUNDING" : "", |
| 147 | closing ? "CLOSING" : "", |
| 148 | congested ? "CONGESTED" : "", |
| 149 | cwnd_wait ? "CWND_WAIT" : "", |
| 150 | write_space ? "WRITE_SPACE" : ""); |
| 151 | } |
| 152 | |
| 153 | xprt_put(xprt); |
| 154 | return ret + 1; |
| 155 | } |
| 156 | |
Olga Kornievskaia | 0e55903 | 2021-06-08 15:59:21 -0400 | [diff] [blame] | 157 | static ssize_t rpc_sysfs_xprt_switch_info_show(struct kobject *kobj, |
| 158 | struct kobj_attribute *attr, |
| 159 | char *buf) |
| 160 | { |
| 161 | struct rpc_xprt_switch *xprt_switch = |
| 162 | rpc_sysfs_xprt_switch_kobj_get_xprt(kobj); |
| 163 | ssize_t ret; |
| 164 | |
| 165 | if (!xprt_switch) |
| 166 | return 0; |
| 167 | ret = sprintf(buf, "num_xprts=%u\nnum_active=%u\nqueue_len=%ld\n", |
| 168 | xprt_switch->xps_nxprts, xprt_switch->xps_nactive, |
| 169 | atomic_long_read(&xprt_switch->xps_queuelen)); |
| 170 | xprt_switch_put(xprt_switch); |
| 171 | return ret + 1; |
| 172 | } |
| 173 | |
Olga Kornievskaia | 587bc72 | 2021-06-08 15:59:19 -0400 | [diff] [blame] | 174 | static ssize_t rpc_sysfs_xprt_dstaddr_store(struct kobject *kobj, |
| 175 | struct kobj_attribute *attr, |
| 176 | const char *buf, size_t count) |
| 177 | { |
| 178 | struct rpc_xprt *xprt = rpc_sysfs_xprt_kobj_get_xprt(kobj); |
| 179 | struct sockaddr *saddr; |
| 180 | char *dst_addr; |
| 181 | int port; |
| 182 | struct xprt_addr *saved_addr; |
| 183 | size_t buf_len; |
| 184 | |
| 185 | if (!xprt) |
| 186 | return 0; |
| 187 | if (!(xprt->xprt_class->ident == XPRT_TRANSPORT_TCP || |
| 188 | xprt->xprt_class->ident == XPRT_TRANSPORT_RDMA)) { |
| 189 | xprt_put(xprt); |
| 190 | return -EOPNOTSUPP; |
| 191 | } |
| 192 | |
| 193 | if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) { |
| 194 | count = -EINTR; |
| 195 | goto out_put; |
| 196 | } |
| 197 | saddr = (struct sockaddr *)&xprt->addr; |
| 198 | port = rpc_get_port(saddr); |
| 199 | |
| 200 | /* buf_len is the len until the first occurence of either |
| 201 | * '\n' or '\0' |
| 202 | */ |
| 203 | buf_len = strcspn(buf, "\n"); |
| 204 | |
| 205 | dst_addr = kstrndup(buf, buf_len, GFP_KERNEL); |
| 206 | if (!dst_addr) |
| 207 | goto out_err; |
| 208 | saved_addr = kzalloc(sizeof(*saved_addr), GFP_KERNEL); |
| 209 | if (!saved_addr) |
| 210 | goto out_err_free; |
| 211 | saved_addr->addr = |
| 212 | rcu_dereference_raw(xprt->address_strings[RPC_DISPLAY_ADDR]); |
| 213 | rcu_assign_pointer(xprt->address_strings[RPC_DISPLAY_ADDR], dst_addr); |
| 214 | call_rcu(&saved_addr->rcu, free_xprt_addr); |
| 215 | xprt->addrlen = rpc_pton(xprt->xprt_net, buf, buf_len, saddr, |
| 216 | sizeof(*saddr)); |
| 217 | rpc_set_port(saddr, port); |
| 218 | |
| 219 | xprt_force_disconnect(xprt); |
| 220 | out: |
| 221 | xprt_release_write(xprt, NULL); |
| 222 | out_put: |
| 223 | xprt_put(xprt); |
| 224 | return count; |
| 225 | out_err_free: |
| 226 | kfree(dst_addr); |
| 227 | out_err: |
| 228 | count = -ENOMEM; |
| 229 | goto out; |
| 230 | } |
| 231 | |
Olga Kornievskaia | 7467874 | 2021-06-08 15:59:10 -0400 | [diff] [blame] | 232 | int rpc_sysfs_init(void) |
| 233 | { |
| 234 | rpc_sunrpc_kset = kset_create_and_add("sunrpc", NULL, kernel_kobj); |
| 235 | if (!rpc_sunrpc_kset) |
| 236 | return -ENOMEM; |
Olga Kornievskaia | baea994 | 2021-06-08 15:59:16 -0400 | [diff] [blame] | 237 | rpc_sunrpc_client_kobj = |
| 238 | rpc_sysfs_object_alloc("rpc-clients", rpc_sunrpc_kset, NULL); |
| 239 | if (!rpc_sunrpc_client_kobj) |
| 240 | goto err_client; |
| 241 | rpc_sunrpc_xprt_switch_kobj = |
| 242 | rpc_sysfs_object_alloc("xprt-switches", rpc_sunrpc_kset, NULL); |
| 243 | if (!rpc_sunrpc_xprt_switch_kobj) |
| 244 | goto err_switch; |
Olga Kornievskaia | 7467874 | 2021-06-08 15:59:10 -0400 | [diff] [blame] | 245 | return 0; |
Olga Kornievskaia | baea994 | 2021-06-08 15:59:16 -0400 | [diff] [blame] | 246 | err_switch: |
| 247 | kobject_put(rpc_sunrpc_client_kobj); |
| 248 | rpc_sunrpc_client_kobj = NULL; |
| 249 | err_client: |
| 250 | kset_unregister(rpc_sunrpc_kset); |
| 251 | rpc_sunrpc_kset = NULL; |
| 252 | return -ENOMEM; |
Olga Kornievskaia | 7467874 | 2021-06-08 15:59:10 -0400 | [diff] [blame] | 253 | } |
| 254 | |
Olga Kornievskaia | c5a382e | 2021-06-08 15:59:12 -0400 | [diff] [blame] | 255 | static void rpc_sysfs_client_release(struct kobject *kobj) |
| 256 | { |
| 257 | struct rpc_sysfs_client *c; |
| 258 | |
| 259 | c = container_of(kobj, struct rpc_sysfs_client, kobject); |
| 260 | kfree(c); |
| 261 | } |
| 262 | |
Olga Kornievskaia | baea994 | 2021-06-08 15:59:16 -0400 | [diff] [blame] | 263 | static void rpc_sysfs_xprt_switch_release(struct kobject *kobj) |
| 264 | { |
| 265 | struct rpc_sysfs_xprt_switch *xprt_switch; |
| 266 | |
| 267 | xprt_switch = container_of(kobj, struct rpc_sysfs_xprt_switch, kobject); |
| 268 | kfree(xprt_switch); |
| 269 | } |
| 270 | |
Olga Kornievskaia | d408ebe | 2021-06-08 15:59:18 -0400 | [diff] [blame] | 271 | static void rpc_sysfs_xprt_release(struct kobject *kobj) |
| 272 | { |
| 273 | struct rpc_sysfs_xprt *xprt; |
| 274 | |
| 275 | xprt = container_of(kobj, struct rpc_sysfs_xprt, kobject); |
| 276 | kfree(xprt); |
| 277 | } |
| 278 | |
Olga Kornievskaia | c5a382e | 2021-06-08 15:59:12 -0400 | [diff] [blame] | 279 | static const void *rpc_sysfs_client_namespace(struct kobject *kobj) |
| 280 | { |
| 281 | return container_of(kobj, struct rpc_sysfs_client, kobject)->net; |
| 282 | } |
| 283 | |
Olga Kornievskaia | baea994 | 2021-06-08 15:59:16 -0400 | [diff] [blame] | 284 | static const void *rpc_sysfs_xprt_switch_namespace(struct kobject *kobj) |
| 285 | { |
| 286 | return container_of(kobj, struct rpc_sysfs_xprt_switch, kobject)->net; |
| 287 | } |
| 288 | |
Olga Kornievskaia | d408ebe | 2021-06-08 15:59:18 -0400 | [diff] [blame] | 289 | static const void *rpc_sysfs_xprt_namespace(struct kobject *kobj) |
| 290 | { |
| 291 | return container_of(kobj, struct rpc_sysfs_xprt, |
| 292 | kobject)->xprt->xprt_net; |
| 293 | } |
| 294 | |
Olga Kornievskaia | 587bc72 | 2021-06-08 15:59:19 -0400 | [diff] [blame] | 295 | static struct kobj_attribute rpc_sysfs_xprt_dstaddr = __ATTR(dstaddr, |
| 296 | 0644, rpc_sysfs_xprt_dstaddr_show, rpc_sysfs_xprt_dstaddr_store); |
| 297 | |
Olga Kornievskaia | 4a09651 | 2021-06-08 15:59:20 -0400 | [diff] [blame] | 298 | static struct kobj_attribute rpc_sysfs_xprt_info = __ATTR(xprt_info, |
| 299 | 0444, rpc_sysfs_xprt_info_show, NULL); |
| 300 | |
Olga Kornievskaia | 681d569 | 2021-06-08 15:59:22 -0400 | [diff] [blame^] | 301 | static struct kobj_attribute rpc_sysfs_xprt_change_state = __ATTR(xprt_state, |
| 302 | 0644, rpc_sysfs_xprt_state_show, NULL); |
| 303 | |
Olga Kornievskaia | 587bc72 | 2021-06-08 15:59:19 -0400 | [diff] [blame] | 304 | static struct attribute *rpc_sysfs_xprt_attrs[] = { |
| 305 | &rpc_sysfs_xprt_dstaddr.attr, |
Olga Kornievskaia | 4a09651 | 2021-06-08 15:59:20 -0400 | [diff] [blame] | 306 | &rpc_sysfs_xprt_info.attr, |
Olga Kornievskaia | 681d569 | 2021-06-08 15:59:22 -0400 | [diff] [blame^] | 307 | &rpc_sysfs_xprt_change_state.attr, |
Olga Kornievskaia | 587bc72 | 2021-06-08 15:59:19 -0400 | [diff] [blame] | 308 | NULL, |
| 309 | }; |
| 310 | |
Olga Kornievskaia | 0e55903 | 2021-06-08 15:59:21 -0400 | [diff] [blame] | 311 | static struct kobj_attribute rpc_sysfs_xprt_switch_info = |
| 312 | __ATTR(xprt_switch_info, 0444, rpc_sysfs_xprt_switch_info_show, NULL); |
| 313 | |
| 314 | static struct attribute *rpc_sysfs_xprt_switch_attrs[] = { |
| 315 | &rpc_sysfs_xprt_switch_info.attr, |
| 316 | NULL, |
| 317 | }; |
| 318 | |
Olga Kornievskaia | c5a382e | 2021-06-08 15:59:12 -0400 | [diff] [blame] | 319 | static struct kobj_type rpc_sysfs_client_type = { |
| 320 | .release = rpc_sysfs_client_release, |
| 321 | .sysfs_ops = &kobj_sysfs_ops, |
| 322 | .namespace = rpc_sysfs_client_namespace, |
| 323 | }; |
| 324 | |
Olga Kornievskaia | baea994 | 2021-06-08 15:59:16 -0400 | [diff] [blame] | 325 | static struct kobj_type rpc_sysfs_xprt_switch_type = { |
| 326 | .release = rpc_sysfs_xprt_switch_release, |
Olga Kornievskaia | 0e55903 | 2021-06-08 15:59:21 -0400 | [diff] [blame] | 327 | .default_attrs = rpc_sysfs_xprt_switch_attrs, |
Olga Kornievskaia | baea994 | 2021-06-08 15:59:16 -0400 | [diff] [blame] | 328 | .sysfs_ops = &kobj_sysfs_ops, |
| 329 | .namespace = rpc_sysfs_xprt_switch_namespace, |
| 330 | }; |
| 331 | |
Olga Kornievskaia | d408ebe | 2021-06-08 15:59:18 -0400 | [diff] [blame] | 332 | static struct kobj_type rpc_sysfs_xprt_type = { |
| 333 | .release = rpc_sysfs_xprt_release, |
Olga Kornievskaia | 587bc72 | 2021-06-08 15:59:19 -0400 | [diff] [blame] | 334 | .default_attrs = rpc_sysfs_xprt_attrs, |
Olga Kornievskaia | d408ebe | 2021-06-08 15:59:18 -0400 | [diff] [blame] | 335 | .sysfs_ops = &kobj_sysfs_ops, |
| 336 | .namespace = rpc_sysfs_xprt_namespace, |
| 337 | }; |
| 338 | |
Olga Kornievskaia | 7467874 | 2021-06-08 15:59:10 -0400 | [diff] [blame] | 339 | void rpc_sysfs_exit(void) |
| 340 | { |
Olga Kornievskaia | c441f12 | 2021-06-08 15:59:11 -0400 | [diff] [blame] | 341 | kobject_put(rpc_sunrpc_client_kobj); |
Olga Kornievskaia | baea994 | 2021-06-08 15:59:16 -0400 | [diff] [blame] | 342 | kobject_put(rpc_sunrpc_xprt_switch_kobj); |
Olga Kornievskaia | 7467874 | 2021-06-08 15:59:10 -0400 | [diff] [blame] | 343 | kset_unregister(rpc_sunrpc_kset); |
| 344 | } |
Olga Kornievskaia | c5a382e | 2021-06-08 15:59:12 -0400 | [diff] [blame] | 345 | |
| 346 | static struct rpc_sysfs_client *rpc_sysfs_client_alloc(struct kobject *parent, |
| 347 | struct net *net, |
| 348 | int clid) |
| 349 | { |
| 350 | struct rpc_sysfs_client *p; |
| 351 | |
| 352 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
| 353 | if (p) { |
| 354 | p->net = net; |
| 355 | p->kobject.kset = rpc_sunrpc_kset; |
| 356 | if (kobject_init_and_add(&p->kobject, &rpc_sysfs_client_type, |
| 357 | parent, "clnt-%d", clid) == 0) |
| 358 | return p; |
| 359 | kobject_put(&p->kobject); |
| 360 | } |
| 361 | return NULL; |
| 362 | } |
| 363 | |
Olga Kornievskaia | baea994 | 2021-06-08 15:59:16 -0400 | [diff] [blame] | 364 | static struct rpc_sysfs_xprt_switch * |
| 365 | rpc_sysfs_xprt_switch_alloc(struct kobject *parent, |
| 366 | struct rpc_xprt_switch *xprt_switch, |
| 367 | struct net *net, |
| 368 | gfp_t gfp_flags) |
| 369 | { |
| 370 | struct rpc_sysfs_xprt_switch *p; |
| 371 | |
| 372 | p = kzalloc(sizeof(*p), gfp_flags); |
| 373 | if (p) { |
| 374 | p->net = net; |
| 375 | p->kobject.kset = rpc_sunrpc_kset; |
| 376 | if (kobject_init_and_add(&p->kobject, |
| 377 | &rpc_sysfs_xprt_switch_type, |
| 378 | parent, "switch-%d", |
| 379 | xprt_switch->xps_id) == 0) |
| 380 | return p; |
| 381 | kobject_put(&p->kobject); |
| 382 | } |
| 383 | return NULL; |
| 384 | } |
| 385 | |
Olga Kornievskaia | d408ebe | 2021-06-08 15:59:18 -0400 | [diff] [blame] | 386 | static struct rpc_sysfs_xprt *rpc_sysfs_xprt_alloc(struct kobject *parent, |
| 387 | struct rpc_xprt *xprt, |
| 388 | gfp_t gfp_flags) |
| 389 | { |
| 390 | struct rpc_sysfs_xprt *p; |
| 391 | |
| 392 | p = kzalloc(sizeof(*p), gfp_flags); |
| 393 | if (!p) |
| 394 | goto out; |
| 395 | p->kobject.kset = rpc_sunrpc_kset; |
| 396 | if (kobject_init_and_add(&p->kobject, &rpc_sysfs_xprt_type, |
| 397 | parent, "xprt-%d-%s", xprt->id, |
| 398 | xprt->address_strings[RPC_DISPLAY_PROTO]) == 0) |
| 399 | return p; |
| 400 | kobject_put(&p->kobject); |
| 401 | out: |
| 402 | return NULL; |
| 403 | } |
| 404 | |
Olga Kornievskaia | 2a338a5 | 2021-06-08 15:59:17 -0400 | [diff] [blame] | 405 | void rpc_sysfs_client_setup(struct rpc_clnt *clnt, |
| 406 | struct rpc_xprt_switch *xprt_switch, |
| 407 | struct net *net) |
Olga Kornievskaia | c5a382e | 2021-06-08 15:59:12 -0400 | [diff] [blame] | 408 | { |
| 409 | struct rpc_sysfs_client *rpc_client; |
| 410 | |
Olga Kornievskaia | 2a338a5 | 2021-06-08 15:59:17 -0400 | [diff] [blame] | 411 | rpc_client = rpc_sysfs_client_alloc(rpc_sunrpc_client_kobj, |
| 412 | net, clnt->cl_clid); |
Olga Kornievskaia | c5a382e | 2021-06-08 15:59:12 -0400 | [diff] [blame] | 413 | if (rpc_client) { |
Olga Kornievskaia | 2a338a5 | 2021-06-08 15:59:17 -0400 | [diff] [blame] | 414 | char name[] = "switch"; |
| 415 | struct rpc_sysfs_xprt_switch *xswitch = |
| 416 | (struct rpc_sysfs_xprt_switch *)xprt_switch->xps_sysfs; |
| 417 | int ret; |
| 418 | |
Olga Kornievskaia | c5a382e | 2021-06-08 15:59:12 -0400 | [diff] [blame] | 419 | clnt->cl_sysfs = rpc_client; |
Olga Kornievskaia | 2a338a5 | 2021-06-08 15:59:17 -0400 | [diff] [blame] | 420 | rpc_client->clnt = clnt; |
| 421 | rpc_client->xprt_switch = xprt_switch; |
Olga Kornievskaia | c5a382e | 2021-06-08 15:59:12 -0400 | [diff] [blame] | 422 | kobject_uevent(&rpc_client->kobject, KOBJ_ADD); |
Olga Kornievskaia | 2a338a5 | 2021-06-08 15:59:17 -0400 | [diff] [blame] | 423 | ret = sysfs_create_link_nowarn(&rpc_client->kobject, |
| 424 | &xswitch->kobject, name); |
| 425 | if (ret) |
| 426 | pr_warn("can't create link to %s in sysfs (%d)\n", |
| 427 | name, ret); |
Olga Kornievskaia | c5a382e | 2021-06-08 15:59:12 -0400 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
Olga Kornievskaia | baea994 | 2021-06-08 15:59:16 -0400 | [diff] [blame] | 431 | void rpc_sysfs_xprt_switch_setup(struct rpc_xprt_switch *xprt_switch, |
| 432 | struct rpc_xprt *xprt, |
| 433 | gfp_t gfp_flags) |
| 434 | { |
| 435 | struct rpc_sysfs_xprt_switch *rpc_xprt_switch; |
| 436 | struct net *net; |
| 437 | |
| 438 | if (xprt_switch->xps_net) |
| 439 | net = xprt_switch->xps_net; |
| 440 | else |
| 441 | net = xprt->xprt_net; |
| 442 | rpc_xprt_switch = |
| 443 | rpc_sysfs_xprt_switch_alloc(rpc_sunrpc_xprt_switch_kobj, |
| 444 | xprt_switch, net, gfp_flags); |
| 445 | if (rpc_xprt_switch) { |
| 446 | xprt_switch->xps_sysfs = rpc_xprt_switch; |
| 447 | rpc_xprt_switch->xprt_switch = xprt_switch; |
| 448 | rpc_xprt_switch->xprt = xprt; |
| 449 | kobject_uevent(&rpc_xprt_switch->kobject, KOBJ_ADD); |
| 450 | } |
| 451 | } |
| 452 | |
Olga Kornievskaia | d408ebe | 2021-06-08 15:59:18 -0400 | [diff] [blame] | 453 | void rpc_sysfs_xprt_setup(struct rpc_xprt_switch *xprt_switch, |
| 454 | struct rpc_xprt *xprt, |
| 455 | gfp_t gfp_flags) |
| 456 | { |
| 457 | struct rpc_sysfs_xprt *rpc_xprt; |
| 458 | struct rpc_sysfs_xprt_switch *switch_obj = |
| 459 | (struct rpc_sysfs_xprt_switch *)xprt_switch->xps_sysfs; |
| 460 | |
| 461 | rpc_xprt = rpc_sysfs_xprt_alloc(&switch_obj->kobject, xprt, gfp_flags); |
| 462 | if (rpc_xprt) { |
| 463 | xprt->xprt_sysfs = rpc_xprt; |
| 464 | rpc_xprt->xprt = xprt; |
| 465 | kobject_uevent(&rpc_xprt->kobject, KOBJ_ADD); |
| 466 | } |
| 467 | } |
| 468 | |
Olga Kornievskaia | c5a382e | 2021-06-08 15:59:12 -0400 | [diff] [blame] | 469 | void rpc_sysfs_client_destroy(struct rpc_clnt *clnt) |
| 470 | { |
| 471 | struct rpc_sysfs_client *rpc_client = clnt->cl_sysfs; |
| 472 | |
| 473 | if (rpc_client) { |
Olga Kornievskaia | 2a338a5 | 2021-06-08 15:59:17 -0400 | [diff] [blame] | 474 | char name[] = "switch"; |
| 475 | |
| 476 | sysfs_remove_link(&rpc_client->kobject, name); |
Olga Kornievskaia | c5a382e | 2021-06-08 15:59:12 -0400 | [diff] [blame] | 477 | kobject_uevent(&rpc_client->kobject, KOBJ_REMOVE); |
| 478 | kobject_del(&rpc_client->kobject); |
| 479 | kobject_put(&rpc_client->kobject); |
| 480 | clnt->cl_sysfs = NULL; |
| 481 | } |
| 482 | } |
Olga Kornievskaia | baea994 | 2021-06-08 15:59:16 -0400 | [diff] [blame] | 483 | |
| 484 | void rpc_sysfs_xprt_switch_destroy(struct rpc_xprt_switch *xprt_switch) |
| 485 | { |
| 486 | struct rpc_sysfs_xprt_switch *rpc_xprt_switch = xprt_switch->xps_sysfs; |
| 487 | |
| 488 | if (rpc_xprt_switch) { |
| 489 | kobject_uevent(&rpc_xprt_switch->kobject, KOBJ_REMOVE); |
| 490 | kobject_del(&rpc_xprt_switch->kobject); |
| 491 | kobject_put(&rpc_xprt_switch->kobject); |
| 492 | xprt_switch->xps_sysfs = NULL; |
| 493 | } |
| 494 | } |
Olga Kornievskaia | d408ebe | 2021-06-08 15:59:18 -0400 | [diff] [blame] | 495 | |
| 496 | void rpc_sysfs_xprt_destroy(struct rpc_xprt *xprt) |
| 497 | { |
| 498 | struct rpc_sysfs_xprt *rpc_xprt = xprt->xprt_sysfs; |
| 499 | |
| 500 | if (rpc_xprt) { |
| 501 | kobject_uevent(&rpc_xprt->kobject, KOBJ_REMOVE); |
| 502 | kobject_del(&rpc_xprt->kobject); |
| 503 | kobject_put(&rpc_xprt->kobject); |
| 504 | xprt->xprt_sysfs = NULL; |
| 505 | } |
| 506 | } |