1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2019 Hammerspace Inc 4 */ 5 6 #include <linux/module.h> 7 #include <linux/kobject.h> 8 #include <linux/sysfs.h> 9 #include <linux/fs.h> 10 #include <linux/slab.h> 11 #include <linux/netdevice.h> 12 #include <linux/string.h> 13 #include <linux/nfs_fs.h> 14 #include <linux/rcupdate.h> 15 #include <linux/lockd/lockd.h> 16 17 #include "internal.h" 18 #include "nfs4_fs.h" 19 #include "netns.h" 20 #include "sysfs.h" 21 22 static struct kset *nfs_kset; 23 24 static void nfs_kset_release(struct kobject *kobj) 25 { 26 struct kset *kset = container_of(kobj, struct kset, kobj); 27 kfree(kset); 28 } 29 30 static const struct kobj_ns_type_operations *nfs_netns_object_child_ns_type( 31 const struct kobject *kobj) 32 { 33 return &net_ns_type_operations; 34 } 35 36 static struct kobj_type nfs_kset_type = { 37 .release = nfs_kset_release, 38 .sysfs_ops = &kobj_sysfs_ops, 39 .child_ns_type = nfs_netns_object_child_ns_type, 40 }; 41 42 int nfs_sysfs_init(void) 43 { 44 int ret; 45 46 nfs_kset = kzalloc(sizeof(*nfs_kset), GFP_KERNEL); 47 if (!nfs_kset) 48 return -ENOMEM; 49 50 ret = kobject_set_name(&nfs_kset->kobj, "nfs"); 51 if (ret) { 52 kfree(nfs_kset); 53 return ret; 54 } 55 56 nfs_kset->kobj.parent = fs_kobj; 57 nfs_kset->kobj.ktype = &nfs_kset_type; 58 nfs_kset->kobj.kset = NULL; 59 60 ret = kset_register(nfs_kset); 61 if (ret) { 62 kfree(nfs_kset); 63 return ret; 64 } 65 66 return 0; 67 } 68 69 void nfs_sysfs_exit(void) 70 { 71 kset_unregister(nfs_kset); 72 } 73 74 static ssize_t nfs_netns_identifier_show(struct kobject *kobj, 75 struct kobj_attribute *attr, char *buf) 76 { 77 struct nfs_netns_client *c = container_of(kobj, 78 struct nfs_netns_client, 79 kobject); 80 ssize_t ret; 81 82 rcu_read_lock(); 83 ret = sysfs_emit(buf, "%s\n", rcu_dereference(c->identifier)); 84 rcu_read_unlock(); 85 return ret; 86 } 87 88 /* Strip trailing '\n' */ 89 static size_t nfs_string_strip(const char *c, size_t len) 90 { 91 while (len > 0 && c[len-1] == '\n') 92 --len; 93 return len; 94 } 95 96 static ssize_t nfs_netns_identifier_store(struct kobject *kobj, 97 struct kobj_attribute *attr, 98 const char *buf, size_t count) 99 { 100 struct nfs_netns_client *c = container_of(kobj, 101 struct nfs_netns_client, 102 kobject); 103 const char *old; 104 char *p; 105 size_t len; 106 107 len = nfs_string_strip(buf, min_t(size_t, count, CONTAINER_ID_MAXLEN)); 108 if (!len) 109 return 0; 110 p = kmemdup_nul(buf, len, GFP_KERNEL); 111 if (!p) 112 return -ENOMEM; 113 old = rcu_dereference_protected(xchg(&c->identifier, (char __rcu *)p), 1); 114 if (old) { 115 synchronize_rcu(); 116 kfree(old); 117 } 118 return count; 119 } 120 121 static void nfs_netns_client_release(struct kobject *kobj) 122 { 123 struct nfs_netns_client *c = container_of(kobj, 124 struct nfs_netns_client, 125 kobject); 126 127 kfree(rcu_dereference_raw(c->identifier)); 128 } 129 130 static const void *nfs_netns_client_namespace(const struct kobject *kobj) 131 { 132 return container_of(kobj, struct nfs_netns_client, kobject)->net; 133 } 134 135 static struct kobj_attribute nfs_netns_client_id = __ATTR(identifier, 136 0644, nfs_netns_identifier_show, nfs_netns_identifier_store); 137 138 static struct attribute *nfs_netns_client_attrs[] = { 139 &nfs_netns_client_id.attr, 140 NULL, 141 }; 142 ATTRIBUTE_GROUPS(nfs_netns_client); 143 144 static struct kobj_type nfs_netns_client_type = { 145 .release = nfs_netns_client_release, 146 .default_groups = nfs_netns_client_groups, 147 .sysfs_ops = &kobj_sysfs_ops, 148 .namespace = nfs_netns_client_namespace, 149 }; 150 151 static void nfs_netns_object_release(struct kobject *kobj) 152 { 153 struct nfs_netns_client *c = container_of(kobj, 154 struct nfs_netns_client, 155 nfs_net_kobj); 156 kfree(c); 157 } 158 159 static const void *nfs_netns_namespace(const struct kobject *kobj) 160 { 161 return container_of(kobj, struct nfs_netns_client, nfs_net_kobj)->net; 162 } 163 164 static struct kobj_type nfs_netns_object_type = { 165 .release = nfs_netns_object_release, 166 .sysfs_ops = &kobj_sysfs_ops, 167 .namespace = nfs_netns_namespace, 168 }; 169 170 static struct nfs_netns_client *nfs_netns_client_alloc(struct kobject *parent, 171 struct net *net) 172 { 173 struct nfs_netns_client *p; 174 175 p = kzalloc(sizeof(*p), GFP_KERNEL); 176 if (p) { 177 p->net = net; 178 p->kobject.kset = nfs_kset; 179 p->nfs_net_kobj.kset = nfs_kset; 180 181 if (kobject_init_and_add(&p->nfs_net_kobj, &nfs_netns_object_type, 182 parent, "net") != 0) { 183 kobject_put(&p->nfs_net_kobj); 184 return NULL; 185 } 186 187 if (kobject_init_and_add(&p->kobject, &nfs_netns_client_type, 188 &p->nfs_net_kobj, "nfs_client") == 0) 189 return p; 190 191 kobject_put(&p->kobject); 192 } 193 return NULL; 194 } 195 196 void nfs_netns_sysfs_setup(struct nfs_net *netns, struct net *net) 197 { 198 struct nfs_netns_client *clp; 199 200 clp = nfs_netns_client_alloc(&nfs_kset->kobj, net); 201 if (clp) { 202 netns->nfs_client = clp; 203 kobject_uevent(&clp->kobject, KOBJ_ADD); 204 } 205 } 206 207 void nfs_netns_sysfs_destroy(struct nfs_net *netns) 208 { 209 struct nfs_netns_client *clp = netns->nfs_client; 210 211 if (clp) { 212 kobject_uevent(&clp->kobject, KOBJ_REMOVE); 213 kobject_del(&clp->kobject); 214 kobject_put(&clp->kobject); 215 kobject_del(&clp->nfs_net_kobj); 216 kobject_put(&clp->nfs_net_kobj); 217 netns->nfs_client = NULL; 218 } 219 } 220 221 static bool shutdown_match_client(const struct rpc_task *task, const void *data) 222 { 223 return true; 224 } 225 226 static void shutdown_client(struct rpc_clnt *clnt) 227 { 228 clnt->cl_shutdown = 1; 229 rpc_cancel_tasks(clnt, -EIO, shutdown_match_client, NULL); 230 } 231 232 /* 233 * Shut down the nfs_client only once all the superblocks 234 * have been shut down. 235 */ 236 static void shutdown_nfs_client(struct nfs_client *clp) 237 { 238 struct nfs_server *server; 239 rcu_read_lock(); 240 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { 241 if (!(server->flags & NFS_MOUNT_SHUTDOWN)) { 242 rcu_read_unlock(); 243 return; 244 } 245 } 246 rcu_read_unlock(); 247 nfs_mark_client_ready(clp, -EIO); 248 shutdown_client(clp->cl_rpcclient); 249 } 250 251 static ssize_t 252 shutdown_show(struct kobject *kobj, struct kobj_attribute *attr, 253 char *buf) 254 { 255 struct nfs_server *server = container_of(kobj, struct nfs_server, kobj); 256 bool shutdown = server->flags & NFS_MOUNT_SHUTDOWN; 257 return sysfs_emit(buf, "%d\n", shutdown); 258 } 259 260 static ssize_t 261 shutdown_store(struct kobject *kobj, struct kobj_attribute *attr, 262 const char *buf, size_t count) 263 { 264 struct nfs_server *server; 265 int ret, val; 266 267 server = container_of(kobj, struct nfs_server, kobj); 268 269 ret = kstrtoint(buf, 0, &val); 270 if (ret) 271 return ret; 272 273 if (val != 1) 274 return -EINVAL; 275 276 /* already shut down? */ 277 if (server->flags & NFS_MOUNT_SHUTDOWN) 278 goto out; 279 280 server->flags |= NFS_MOUNT_SHUTDOWN; 281 shutdown_client(server->client); 282 283 if (!IS_ERR(server->client_acl)) 284 shutdown_client(server->client_acl); 285 286 if (server->nlm_host) 287 shutdown_client(server->nlm_host->h_rpcclnt); 288 out: 289 shutdown_nfs_client(server->nfs_client); 290 return count; 291 } 292 293 static struct kobj_attribute nfs_sysfs_attr_shutdown = __ATTR_RW(shutdown); 294 295 #define RPC_CLIENT_NAME_SIZE 64 296 297 void nfs_sysfs_link_rpc_client(struct nfs_server *server, 298 struct rpc_clnt *clnt, const char *uniq) 299 { 300 char name[RPC_CLIENT_NAME_SIZE]; 301 int ret; 302 303 strscpy(name, clnt->cl_program->name, sizeof(name)); 304 strncat(name, uniq ? uniq : "", sizeof(name) - strlen(name) - 1); 305 strncat(name, "_client", sizeof(name) - strlen(name) - 1); 306 307 ret = sysfs_create_link_nowarn(&server->kobj, 308 &clnt->cl_sysfs->kobject, name); 309 if (ret < 0) 310 pr_warn("NFS: can't create link to %s in sysfs (%d)\n", 311 name, ret); 312 } 313 EXPORT_SYMBOL_GPL(nfs_sysfs_link_rpc_client); 314 315 static void nfs_sysfs_sb_release(struct kobject *kobj) 316 { 317 /* no-op: why? see lib/kobject.c kobject_cleanup() */ 318 } 319 320 static const void *nfs_netns_server_namespace(const struct kobject *kobj) 321 { 322 return container_of(kobj, struct nfs_server, kobj)->nfs_client->cl_net; 323 } 324 325 static struct kobj_type nfs_sb_ktype = { 326 .release = nfs_sysfs_sb_release, 327 .sysfs_ops = &kobj_sysfs_ops, 328 .namespace = nfs_netns_server_namespace, 329 .child_ns_type = nfs_netns_object_child_ns_type, 330 }; 331 332 void nfs_sysfs_add_server(struct nfs_server *server) 333 { 334 int ret; 335 336 ret = kobject_init_and_add(&server->kobj, &nfs_sb_ktype, 337 &nfs_kset->kobj, "server-%d", server->s_sysfs_id); 338 if (ret < 0) { 339 pr_warn("NFS: nfs sysfs add server-%d failed (%d)\n", 340 server->s_sysfs_id, ret); 341 return; 342 } 343 ret = sysfs_create_file_ns(&server->kobj, &nfs_sysfs_attr_shutdown.attr, 344 nfs_netns_server_namespace(&server->kobj)); 345 if (ret < 0) 346 pr_warn("NFS: sysfs_create_file_ns for server-%d failed (%d)\n", 347 server->s_sysfs_id, ret); 348 } 349 EXPORT_SYMBOL_GPL(nfs_sysfs_add_server); 350 351 void nfs_sysfs_move_server_to_sb(struct super_block *s) 352 { 353 struct nfs_server *server = s->s_fs_info; 354 int ret; 355 356 ret = kobject_rename(&server->kobj, s->s_id); 357 if (ret < 0) 358 pr_warn("NFS: rename sysfs %s failed (%d)\n", 359 server->kobj.name, ret); 360 } 361 362 void nfs_sysfs_move_sb_to_server(struct nfs_server *server) 363 { 364 const char *s; 365 int ret = -ENOMEM; 366 367 s = kasprintf(GFP_KERNEL, "server-%d", server->s_sysfs_id); 368 if (s) { 369 ret = kobject_rename(&server->kobj, s); 370 kfree(s); 371 } 372 if (ret < 0) 373 pr_warn("NFS: rename sysfs %s failed (%d)\n", 374 server->kobj.name, ret); 375 } 376 377 /* unlink, not dec-ref */ 378 void nfs_sysfs_remove_server(struct nfs_server *server) 379 { 380 kobject_del(&server->kobj); 381 } 382