1// SPDX-License-Identifier: GPL-2.0-only
2
3#include <linux/ns_common.h>
4#include <linux/proc_ns.h>
5#include <linux/vfsdebug.h>
6
7#ifdef CONFIG_DEBUG_VFS
8static void ns_debug(struct ns_common *ns, const struct proc_ns_operations *ops)
9{
10 switch (ns->ns_type) {
11#ifdef CONFIG_CGROUPS
12 case CLONE_NEWCGROUP:
13 VFS_WARN_ON_ONCE(ops != &cgroupns_operations);
14 break;
15#endif
16#ifdef CONFIG_IPC_NS
17 case CLONE_NEWIPC:
18 VFS_WARN_ON_ONCE(ops != &ipcns_operations);
19 break;
20#endif
21 case CLONE_NEWNS:
22 VFS_WARN_ON_ONCE(ops != &mntns_operations);
23 break;
24#ifdef CONFIG_NET_NS
25 case CLONE_NEWNET:
26 VFS_WARN_ON_ONCE(ops != &netns_operations);
27 break;
28#endif
29#ifdef CONFIG_PID_NS
30 case CLONE_NEWPID:
31 VFS_WARN_ON_ONCE(ops != &pidns_operations);
32 break;
33#endif
34#ifdef CONFIG_TIME_NS
35 case CLONE_NEWTIME:
36 VFS_WARN_ON_ONCE(ops != &timens_operations);
37 break;
38#endif
39#ifdef CONFIG_USER_NS
40 case CLONE_NEWUSER:
41 VFS_WARN_ON_ONCE(ops != &userns_operations);
42 break;
43#endif
44#ifdef CONFIG_UTS_NS
45 case CLONE_NEWUTS:
46 VFS_WARN_ON_ONCE(ops != &utsns_operations);
47 break;
48#endif
49 }
50}
51#endif
52
53int __ns_common_init(struct ns_common *ns, u32 ns_type, const struct proc_ns_operations *ops, int inum)
54{
55 refcount_set(r: &ns->__ns_ref, n: 1);
56 ns->stashed = NULL;
57 ns->ops = ops;
58 ns->ns_id = 0;
59 ns->ns_type = ns_type;
60 RB_CLEAR_NODE(&ns->ns_tree_node);
61 INIT_LIST_HEAD(list: &ns->ns_list_node);
62
63#ifdef CONFIG_DEBUG_VFS
64 ns_debug(ns, ops);
65#endif
66
67 if (inum) {
68 ns->inum = inum;
69 return 0;
70 }
71 return proc_alloc_inum(pino: &ns->inum);
72}
73
74void __ns_common_free(struct ns_common *ns)
75{
76 proc_free_inum(inum: ns->inum);
77}
78