xref: /linux/include/linux/nstree.h (revision 415d34b92c1f921a9ff3c38f56319cbc5536f642)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2025 Christian Brauner <brauner@kernel.org> */
3 #ifndef _LINUX_NSTREE_H
4 #define _LINUX_NSTREE_H
5 
6 #include <linux/ns/nstree_types.h>
7 #include <linux/nsproxy.h>
8 #include <linux/rbtree.h>
9 #include <linux/seqlock.h>
10 #include <linux/rculist.h>
11 #include <linux/cookie.h>
12 #include <uapi/linux/nsfs.h>
13 
14 struct ns_common;
15 
16 extern struct ns_tree_root cgroup_ns_tree;
17 extern struct ns_tree_root ipc_ns_tree;
18 extern struct ns_tree_root mnt_ns_tree;
19 extern struct ns_tree_root net_ns_tree;
20 extern struct ns_tree_root pid_ns_tree;
21 extern struct ns_tree_root time_ns_tree;
22 extern struct ns_tree_root user_ns_tree;
23 extern struct ns_tree_root uts_ns_tree;
24 
25 void ns_tree_node_init(struct ns_tree_node *node);
26 void ns_tree_root_init(struct ns_tree_root *root);
27 bool ns_tree_node_empty(const struct ns_tree_node *node);
28 struct rb_node *ns_tree_node_add(struct ns_tree_node *node,
29 				  struct ns_tree_root *root,
30 				  int (*cmp)(struct rb_node *, const struct rb_node *));
31 void ns_tree_node_del(struct ns_tree_node *node, struct ns_tree_root *root);
32 
33 #define to_ns_tree(__ns)					\
34 	_Generic((__ns),					\
35 		struct cgroup_namespace *: &(cgroup_ns_tree),	\
36 		struct ipc_namespace *:    &(ipc_ns_tree),	\
37 		struct net *:              &(net_ns_tree),	\
38 		struct pid_namespace *:    &(pid_ns_tree),	\
39 		struct mnt_namespace *:    &(mnt_ns_tree),	\
40 		struct time_namespace *:   &(time_ns_tree),	\
41 		struct user_namespace *:   &(user_ns_tree),	\
42 		struct uts_namespace *:    &(uts_ns_tree))
43 
44 #define ns_tree_gen_id(__ns)                 \
45 	__ns_tree_gen_id(to_ns_common(__ns), \
46 			 (((__ns) == ns_init_ns(__ns)) ? ns_init_id(__ns) : 0))
47 
48 u64 __ns_tree_gen_id(struct ns_common *ns, u64 id);
49 void __ns_tree_add_raw(struct ns_common *ns, struct ns_tree_root *ns_tree);
50 void __ns_tree_remove(struct ns_common *ns, struct ns_tree_root *ns_tree);
51 struct ns_common *ns_tree_lookup_rcu(u64 ns_id, int ns_type);
52 struct ns_common *__ns_tree_adjoined_rcu(struct ns_common *ns,
53 					 struct ns_tree_root *ns_tree,
54 					 bool previous);
55 
__ns_tree_add(struct ns_common * ns,struct ns_tree_root * ns_tree,u64 id)56 static inline void __ns_tree_add(struct ns_common *ns, struct ns_tree_root *ns_tree, u64 id)
57 {
58 	__ns_tree_gen_id(ns, id);
59 	__ns_tree_add_raw(ns, ns_tree);
60 }
61 
62 /**
63  * ns_tree_add_raw - Add a namespace to a namespace
64  * @ns: Namespace to add
65  *
66  * This function adds a namespace to the appropriate namespace tree
67  * without assigning a id.
68  */
69 #define ns_tree_add_raw(__ns) __ns_tree_add_raw(to_ns_common(__ns), to_ns_tree(__ns))
70 
71 /**
72  * ns_tree_add - Add a namespace to a namespace tree
73  * @ns: Namespace to add
74  *
75  * This function assigns a new id to the namespace and adds it to the
76  * appropriate namespace tree and list.
77  */
78 #define ns_tree_add(__ns)                                   \
79 	__ns_tree_add(to_ns_common(__ns), to_ns_tree(__ns), \
80 		      (((__ns) == ns_init_ns(__ns)) ? ns_init_id(__ns) : 0))
81 
82 /**
83  * ns_tree_remove - Remove a namespace from a namespace tree
84  * @ns: Namespace to remove
85  *
86  * This function removes a namespace from the appropriate namespace
87  * tree and list.
88  */
89 #define ns_tree_remove(__ns)  __ns_tree_remove(to_ns_common(__ns), to_ns_tree(__ns))
90 
91 #define ns_tree_adjoined_rcu(__ns, __previous) \
92 	__ns_tree_adjoined_rcu(to_ns_common(__ns), to_ns_tree(__ns), __previous)
93 
94 #define ns_tree_active(__ns) (!RB_EMPTY_NODE(&to_ns_common(__ns)->ns_tree_node.ns_node))
95 
96 #endif /* _LINUX_NSTREE_H */
97