PatchworkOS  c9fea19
A non-POSIX operating system.
Loading...
Searching...
No Matches
namespace.h
Go to the documentation of this file.
1#pragma once
2
3#include <kernel/fs/path.h>
6#include <kernel/utils/map.h>
7#include <sys/io.h>
8#include <sys/list.h>
9
10typedef struct namespace namespace_t;
11typedef struct mount mount_t;
12typedef struct process process_t;
13
14/**
15 * @brief Per-process Namespaces.
16 * @defgroup kernel_fs_namespace Namespaces
17 * @ingroup kernel_fs
18 *
19 * The per-process namespace system allows each process to have its own view of the filesystem hierarchy, by having each
20 * process store its own set of mountpoints instead of having a global set of mountpoints.
21 *
22 * ## Propagation
23 *
24 * When a new mount or bind is created in a namespace, it is only added to that specific namespace. However, its
25 * possible to propagate mounts to children and/or parent namespaces using mount flags (`mount_flags_t`), this allows
26 * those namespaces to also see the new mount or bind.
27 *
28 * @{
29 */
30
31/**
32 * @brief A mount in a namespace.
33 * @struct namespace_mount
34 *
35 * Used to allow a single mount to exist in multiple namespaces.
36 */
43
44/**
45 * @brief Namespace structure.
46 * @struct namespace
47 *
48 * Stored in each process structure.
49 */
50typedef struct namespace
51{
52 list_entry_t entry; ///< The entry for the parent's children list.
53 list_t children; ///< List of child namespaces.
54 namespace_t* parent; ///< The parent namespace, can be `NULL`.
55 list_t mounts; ///< List of mounts in this namespace.
56 map_t mountMap; ///< Map used to go from source dentries to namespace mounts.
57 mount_t* root; ///< The root mount of the namespace.
59 // clang-format off
61// clang-format on
62
63/**
64 * @brief Initializes a namespace.
65 *
66 * @param ns The namespace to initialize.
67 * @param parent The parent namespace to inherit all mounts from, can be `NULL` to create an empty namespace.
68 */
70
71/**
72 * @brief Clear and deinitialize a namespace.
73 *
74 * @param ns The namespace to deinitialize.
75 */
77
78/**
79 * @brief Sets the parent of a namespace and inherits all mounts from the parent.
80 *
81 * @param ns The namespace to set the parent of.
82 * @param parent The new parent namespace.
83 * @return On success, `0`. On failure, `ERR` and `errno` is set to:
84 * - `EINVAL`: Invalid parameters.
85 * - `EBUSY`: The namespace already has a parent.
86 * - `ENOMEM`: Out of memory.
87 */
89
90/**
91 * @brief If the given path is a mountpoint in the namespace, traverse to the mounted filesystem, else no-op.
92 *
93 * @param ns The namespace to traverse in.
94 * @param path The mountpoint path to traverse, will be updated to the new path if traversed.
95 * @return On success, `0`. On failure, `ERR` and `errno` is set to:
96 * - `EINVAL`: Invalid parameters.
97 */
99
100/**
101 * @brief Mount a filesystem in a namespace.
102 *
103 * @param ns The namespace to mount in.
104 * @param deviceName The device name, or `VFS_DEVICE_NAME_NONE` for no device.
105 * @param target The target path to mount to.
106 * @param fsName The filesystem name.
107 * @param flags Mount flags.
108 * @param mode The maximum allowed permissions for files/directories opened under this mount.
109 * @param private Private data for the filesystem's mount function.
110 * @return On success, the new mount. On failure, returns `NULL` and `errno` is set to:
111 * - `EINVAL`: Invalid parameters.
112 * - `EXDEV`: The target path is not visible in the namespace.
113 * - `ENODEV`: The specified filesystem does not exist.
114 * - `EBUSY`: Attempt to mount to already existing root.
115 * - `ENOMEM`: Out of memory.
116 * - `ENOENT`: The root does not exist or the target is negative.
117 * - Other errors as returned by the filesystem's `mount()` function or `mount_new()`.
118 */
119mount_t* namespace_mount(namespace_t* ns, path_t* target, const char* deviceName, const char* fsName,
120 mount_flags_t flags, mode_t mode, void* private);
121
122/**
123 * @brief Bind a source dentry to a target path in a namespace.
124 *
125 * @param ns The namespace to mount in.
126 * @param source The source dentry to bind from, could be either a file or directory and from any filesystem.
127 * @param target The target path to bind to.
128 * @param flags Mount flags.
129 * @param mode The maximum allowed permissions for files/directories opened under this mount.
130 * @return On success, the new mount. On failure, returns `NULL` and `errno` is set to:
131 * - `EINVAL`: Invalid parameters.
132 * - `ENOMEM`: Out of memory.
133 * - Other errors as returned by `mount_new()`.
134 */
136
137/**
138 * @brief Get the root path of a namespace.
139 *
140 * @param ns The namespace, can be `NULL` to get the kernel process's namespace root.
141 * @param out The output root path.
142 * @return On success, `0`. On failure, `ERR` and `errno` is set to:
143 * - `EINVAL`: Invalid parameters.
144 * - `ENOENT`: The namespace has no root mount.
145 */
147
148/**
149 * @brief Clears all mounts from a namespace.
150 *
151 * @note The parent of the namespace will inherit all child namespaces of the deinitialized namespace.
152 *
153 * @param ns The namespace to clear.
154 */
156
157/** @} */
void namespace_init(namespace_t *ns)
Initializes a namespace.
Definition namespace.c:100
uint64_t namespace_get_root_path(namespace_t *ns, path_t *out)
Get the root path of a namespace.
Definition namespace.c:286
uint64_t namespace_traverse(namespace_t *ns, path_t *path)
If the given path is a mountpoint in the namespace, traverse to the mounted filesystem,...
Definition namespace.c:163
uint64_t namespace_set_parent(namespace_t *ns, namespace_t *parent)
Sets the parent of a namespace and inherits all mounts from the parent.
Definition namespace.c:123
mount_t * namespace_bind(namespace_t *ns, dentry_t *source, path_t *target, mount_flags_t flags, mode_t mode)
Bind a source dentry to a target path in a namespace.
Definition namespace.c:259
void namespace_deinit(namespace_t *ns)
Clear and deinitialize a namespace.
Definition namespace.c:111
void namespace_clear(namespace_t *ns)
Clears all mounts from a namespace.
Definition namespace.c:306
mode_t
Path flags and permissions.
Definition path.h:74
mount_flags_t
Mount flags type.
Definition io.h:488
static clock_source_t source
Structure to describe the HPET to the sys time subsystem.
Definition hpet.c:192
static const path_flag_t flags[]
Definition path.c:42
static mount_t * mount
Definition ramfs.c:28
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
Directory entry structure.
Definition dentry.h:84
A entry in a doubly linked list.
Definition list.h:36
A doubly linked list.
Definition list.h:49
Map entry structure.
Definition map.h:68
Hash map structure.
Definition map.h:89
Mount structure.
Definition mount.h:44
list_entry_t entry
Definition namespace.h:39
map_entry_t mapEntry
Definition namespace.h:40
mount_t * mount
Definition namespace.h:41
A mount in a namespace.
list_entry_t entry
The entry for the parent's children list.
Definition namespace.h:52
list_t mounts
List of mounts in this namespace.
Definition namespace.h:55
mount_t * root
The root mount of the namespace.
Definition namespace.h:57
rwlock_t lock
Definition namespace.h:58
namespace_t * parent
The parent namespace, can be NULL.
Definition namespace.h:54
map_t mountMap
Map used to go from source dentries to namespace mounts.
Definition namespace.h:56
list_t children
List of child namespaces.
Definition namespace.h:53
Namespace structure.
Path structure.
Definition path.h:125
Process structure.
Definition process.h:205
Read-Write Ticket Lock structure.
Definition rwlock.h:61