xref: /linux/kernel/nscommon.c (revision 5890f504ef543190beae2a4e244bbfa7c3e0b57c)
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
8 static void ns_debug(struct ns_common *ns, const struct proc_ns_operations *ops)
9 {
10 	switch (ns->ops->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 	default:
50 		VFS_WARN_ON_ONCE(true);
51 	}
52 }
53 #endif
54 
55 int __ns_common_init(struct ns_common *ns, const struct proc_ns_operations *ops, int inum)
56 {
57 	refcount_set(&ns->__ns_ref, 1);
58 	ns->stashed = NULL;
59 	ns->ops = ops;
60 	ns->ns_id = 0;
61 	RB_CLEAR_NODE(&ns->ns_tree_node);
62 	INIT_LIST_HEAD(&ns->ns_list_node);
63 
64 #ifdef CONFIG_DEBUG_VFS
65 	ns_debug(ns, ops);
66 #endif
67 
68 	if (inum) {
69 		ns->inum = inum;
70 		return 0;
71 	}
72 	return proc_alloc_inum(&ns->inum);
73 }
74 
75 void __ns_common_free(struct ns_common *ns)
76 {
77 	proc_free_inum(ns->inum);
78 }
79