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
nfs_kset_release(struct kobject * kobj)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
nfs_netns_object_child_ns_type(const struct kobject * kobj)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
nfs_sysfs_init(void)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
nfs_sysfs_exit(void)69 void nfs_sysfs_exit(void)
70 {
71 kset_unregister(nfs_kset);
72 }
73
nfs_netns_identifier_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)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' */
nfs_string_strip(const char * c,size_t len)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
nfs_netns_identifier_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)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
nfs_netns_client_release(struct kobject * kobj)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
nfs_netns_client_namespace(const struct kobject * kobj)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
nfs_netns_object_release(struct kobject * kobj)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
nfs_netns_namespace(const struct kobject * kobj)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
nfs_netns_client_alloc(struct kobject * parent,struct net * net)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
nfs_netns_sysfs_setup(struct nfs_net * netns,struct net * net)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
nfs_netns_sysfs_destroy(struct nfs_net * netns)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
shutdown_match_client(const struct rpc_task * task,const void * data)221 static bool shutdown_match_client(const struct rpc_task *task, const void *data)
222 {
223 return true;
224 }
225
shutdown_client(struct rpc_clnt * clnt)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 */
shutdown_nfs_client(struct nfs_client * clp)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
shutdown_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)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
shutdown_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)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 #if IS_ENABLED(CONFIG_NFS_V4_1)
296 static ssize_t
implid_domain_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)297 implid_domain_show(struct kobject *kobj, struct kobj_attribute *attr,
298 char *buf)
299 {
300 struct nfs_server *server = container_of(kobj, struct nfs_server, kobj);
301 struct nfs41_impl_id *impl_id = server->nfs_client->cl_implid;
302
303 if (!impl_id || strlen(impl_id->domain) == 0)
304 return 0; //sysfs_emit(buf, "");
305 return sysfs_emit(buf, "%s\n", impl_id->domain);
306 }
307
308 static struct kobj_attribute nfs_sysfs_attr_implid_domain = __ATTR_RO(implid_domain);
309
310
311 static ssize_t
implid_name_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)312 implid_name_show(struct kobject *kobj, struct kobj_attribute *attr,
313 char *buf)
314 {
315 struct nfs_server *server = container_of(kobj, struct nfs_server, kobj);
316 struct nfs41_impl_id *impl_id = server->nfs_client->cl_implid;
317
318 if (!impl_id || strlen(impl_id->name) == 0)
319 return 0; //sysfs_emit(buf, "");
320 return sysfs_emit(buf, "%s\n", impl_id->name);
321 }
322
323 static struct kobj_attribute nfs_sysfs_attr_implid_name = __ATTR_RO(implid_name);
324
325 #endif /* IS_ENABLED(CONFIG_NFS_V4_1) */
326
327 #define RPC_CLIENT_NAME_SIZE 64
328
nfs_sysfs_link_rpc_client(struct nfs_server * server,struct rpc_clnt * clnt,const char * uniq)329 void nfs_sysfs_link_rpc_client(struct nfs_server *server,
330 struct rpc_clnt *clnt, const char *uniq)
331 {
332 char name[RPC_CLIENT_NAME_SIZE];
333 int ret;
334
335 strscpy(name, clnt->cl_program->name, sizeof(name));
336 strncat(name, uniq ? uniq : "", sizeof(name) - strlen(name) - 1);
337 strncat(name, "_client", sizeof(name) - strlen(name) - 1);
338
339 ret = sysfs_create_link_nowarn(&server->kobj,
340 &clnt->cl_sysfs->kobject, name);
341 if (ret < 0)
342 pr_warn("NFS: can't create link to %s in sysfs (%d)\n",
343 name, ret);
344 }
345 EXPORT_SYMBOL_GPL(nfs_sysfs_link_rpc_client);
346
nfs_sysfs_sb_release(struct kobject * kobj)347 static void nfs_sysfs_sb_release(struct kobject *kobj)
348 {
349 /* no-op: why? see lib/kobject.c kobject_cleanup() */
350 }
351
nfs_netns_server_namespace(const struct kobject * kobj)352 static const void *nfs_netns_server_namespace(const struct kobject *kobj)
353 {
354 return container_of(kobj, struct nfs_server, kobj)->nfs_client->cl_net;
355 }
356
357 static struct kobj_type nfs_sb_ktype = {
358 .release = nfs_sysfs_sb_release,
359 .sysfs_ops = &kobj_sysfs_ops,
360 .namespace = nfs_netns_server_namespace,
361 .child_ns_type = nfs_netns_object_child_ns_type,
362 };
363
364 #if IS_ENABLED(CONFIG_NFS_V4_1)
nfs_sysfs_add_nfsv41_server(struct nfs_server * server)365 static void nfs_sysfs_add_nfsv41_server(struct nfs_server *server)
366 {
367 int ret;
368
369 if (!server->nfs_client->cl_implid)
370 return;
371
372 ret = sysfs_create_file_ns(&server->kobj, &nfs_sysfs_attr_implid_domain.attr,
373 nfs_netns_server_namespace(&server->kobj));
374 if (ret < 0)
375 pr_warn("NFS: sysfs_create_file_ns for server-%d failed (%d)\n",
376 server->s_sysfs_id, ret);
377
378 ret = sysfs_create_file_ns(&server->kobj, &nfs_sysfs_attr_implid_name.attr,
379 nfs_netns_server_namespace(&server->kobj));
380 if (ret < 0)
381 pr_warn("NFS: sysfs_create_file_ns for server-%d failed (%d)\n",
382 server->s_sysfs_id, ret);
383 }
384 #else /* CONFIG_NFS_V4_1 */
nfs_sysfs_add_nfsv41_server(struct nfs_server * server)385 static inline void nfs_sysfs_add_nfsv41_server(struct nfs_server *server)
386 {
387 }
388 #endif /* CONFIG_NFS_V4_1 */
389
nfs_sysfs_add_server(struct nfs_server * server)390 void nfs_sysfs_add_server(struct nfs_server *server)
391 {
392 int ret;
393
394 ret = kobject_init_and_add(&server->kobj, &nfs_sb_ktype,
395 &nfs_kset->kobj, "server-%d", server->s_sysfs_id);
396 if (ret < 0) {
397 pr_warn("NFS: nfs sysfs add server-%d failed (%d)\n",
398 server->s_sysfs_id, ret);
399 return;
400 }
401 ret = sysfs_create_file_ns(&server->kobj, &nfs_sysfs_attr_shutdown.attr,
402 nfs_netns_server_namespace(&server->kobj));
403 if (ret < 0)
404 pr_warn("NFS: sysfs_create_file_ns for server-%d failed (%d)\n",
405 server->s_sysfs_id, ret);
406
407 nfs_sysfs_add_nfsv41_server(server);
408 }
409 EXPORT_SYMBOL_GPL(nfs_sysfs_add_server);
410
nfs_sysfs_move_server_to_sb(struct super_block * s)411 void nfs_sysfs_move_server_to_sb(struct super_block *s)
412 {
413 struct nfs_server *server = s->s_fs_info;
414 int ret;
415
416 ret = kobject_rename(&server->kobj, s->s_id);
417 if (ret < 0)
418 pr_warn("NFS: rename sysfs %s failed (%d)\n",
419 server->kobj.name, ret);
420 }
421
nfs_sysfs_move_sb_to_server(struct nfs_server * server)422 void nfs_sysfs_move_sb_to_server(struct nfs_server *server)
423 {
424 const char *s;
425 int ret = -ENOMEM;
426
427 s = kasprintf(GFP_KERNEL, "server-%d", server->s_sysfs_id);
428 if (s) {
429 ret = kobject_rename(&server->kobj, s);
430 kfree(s);
431 }
432 if (ret < 0)
433 pr_warn("NFS: rename sysfs %s failed (%d)\n",
434 server->kobj.name, ret);
435 }
436
437 /* unlink, not dec-ref */
nfs_sysfs_remove_server(struct nfs_server * server)438 void nfs_sysfs_remove_server(struct nfs_server *server)
439 {
440 kobject_del(&server->kobj);
441 }
442