PatchworkOS
Loading...
Searching...
No Matches
Namespaces

Per-process Namespaces. More...

Data Structures

struct  namespace_t
 
struct  namespace_t
 Namespace structure. More...
 

Functions

uint64_t namespace_init (namespace_t *ns, namespace_t *parent, process_t *owner)
 Initializes a namespace.
 
void namespace_deinit (namespace_t *ns)
 Deinitializes a namespace.
 
uint64_t namespace_traverse_mount (namespace_t *ns, const path_t *mountpoint, path_t *outRoot)
 Traverse a mountpoint path to the root of the mounted filesystem.
 
mount_tnamespace_mount (namespace_t *ns, path_t *mountpoint, const char *deviceName, const char *fsName, void *private)
 Mount a filesystem in a namespace.
 
mount_tnamespace_bind (namespace_t *ns, dentry_t *source, path_t *mountpoint)
 Bind a directory to a mountpoint in a namespace.
 
uint64_t namespace_get_root_path (namespace_t *ns, path_t *outPath)
 Get the root path of a namespace.
 

Detailed Description

Per-process Namespaces.

The Per-process namespace system allows each process to have its own view of the filesystem hierarchy, where the children of a parent can see all the mount points that its parents can but can also have additional mount points that are only visible to itself and its children.

For example, say that in the kernel process we define the /usr directory, and then a child process mounts a filesystem at /usr/local. The kernel process and its other children will not see the /usr/local mount point, but the child process that created it and its own children will see it and be able to access it.

This also has the interesting side effect that its possible to hide directories from child processes by mounting a filesystem on to the directory, causing the children to se the mounted filesystem and not the original directory.

Function Documentation

◆ namespace_bind()

mount_t * namespace_bind ( namespace_t ns,
dentry_t source,
path_t mountpoint 
)

Bind a directory to a mountpoint in a namespace.

Parameters
nsThe namespace to mount in, can be NULL.
sourceThe source directory to bind.
mountpointThe mountpoint path.
Returns
On success, the new mount. On failure, returns NULL and errno is set.

Definition at line 195 of file namespace.c.

References assert, atomic_load, path_t::dentry, DENTRY_NEGATIVE, DEREF, EINVAL, ENOENT, ERR, errno, dentry_t::id, mount_t::id, kernelProcess, namespace_t::lock, map_insert(), mount_t::mapEntry, path_t::mount, mount, mount_cache_key(), mount_new(), namespace_t::mountPoints, NULL, process_get_kernel(), REF, rwlock_write_acquire(), rwlock_write_release(), and dentry_t::superblock.

Referenced by process_dir_init(), and SYSCALL_DEFINE().

◆ namespace_deinit()

void namespace_deinit ( namespace_t ns)

Deinitializes a namespace.

Parameters
nsThe namespace to deinitialize.

Definition at line 48 of file namespace.c.

References map_t::capacity, CONTAINER_OF, DEREF, map_t::entries, namespace_t::lock, map_deinit(), MAP_ENTRY_PTR_IS_VALID, mount, namespace_t::mountPoints, NULL, namespace_t::rootMount, rwlock_write_acquire(), and rwlock_write_release().

Referenced by process_init(), and process_kill().

◆ namespace_get_root_path()

uint64_t namespace_get_root_path ( namespace_t ns,
path_t outPath 
)

Get the root path of a namespace.

Parameters
nsThe namespace, can be NULL to get the kernel process's namespace root.
outPathThe output root path.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 274 of file namespace.c.

References assert, EINVAL, ENOENT, ERR, errno, kernelProcess, namespace_t::lock, NULL, path_set(), process_get_kernel(), mount_t::root, namespace_t::rootMount, rwlock_read_acquire(), rwlock_read_release(), and mount_t::superblock.

Referenced by path_walk(), and vfs_ctx_get_cwd().

◆ namespace_init()

uint64_t namespace_init ( namespace_t ns,
namespace_t parent,
process_t owner 
)

Initializes a namespace.

Parameters
nsThe namespace to initialize.
parentThe parent namespace, can be NULL.
ownerThe process that owns this namespace.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 28 of file namespace.c.

References EINVAL, ERR, errno, namespace_t::lock, map_init(), namespace_t::mountPoints, NULL, namespace_t::owner, namespace_t::parent, REF, namespace_t::rootMount, and rwlock_init().

Referenced by process_init().

◆ namespace_mount()

mount_t * namespace_mount ( namespace_t ns,
path_t mountpoint,
const char *  deviceName,
const char *  fsName,
void *  private 
)

Mount a filesystem in a namespace.

If ns is NULL, the filesystem will be mounted in the kernel process's namespace which will make it visible to all processes.

Parameters
nsThe namespace to mount in, can be NULL.
deviceNameThe device name, or VFS_DEVICE_NAME_NONE for no device.
mountpointThe mountpoint path.
fsNameThe filesystem name.
flagsSuperblock flags.
privatePrivate data for the filesystem's mount function.
Returns
On success, the new mount. On failure, returns NULL and errno is set.

Definition at line 107 of file namespace.c.

References assert, atomic_load, path_t::dentry, DENTRY_NEGATIVE, DEREF, DEREF_DEFER, EBUSY, EINVAL, EIO, ENODEV, ENOENT, ERR, errno, dentry_t::id, mount_t::id, kernelProcess, namespace_t::lock, LOG_DEBUG, LOG_INFO, map_insert(), mount_t::mapEntry, path_t::mount, filesystem_t::mount, mount, mount_cache_key(), mount_new(), namespace_t::mountPoints, NULL, process_get_kernel(), REF, namespace_t::rootMount, rwlock_write_acquire(), rwlock_write_release(), RWLOCK_WRITE_SCOPE, dentry_t::superblock, and vfs_get_fs().

Referenced by ramfs_init(), and sysfs_mount_new().

◆ namespace_traverse_mount()

uint64_t namespace_traverse_mount ( namespace_t ns,
const path_t mountpoint,
path_t outRoot 
)

Traverse a mountpoint path to the root of the mounted filesystem.

If the mount point is not found in the namespace or its parents, it will simply return the same path as the mountpoint.

Parameters
outRootThe output root path.
nsThe namespace to use.
mountpointThe mountpoint path to traverse.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 73 of file namespace.c.

References CONTAINER_OF_SAFE, path_t::dentry, EINVAL, ERR, errno, ESTALE, dentry_t::id, mount_t::id, namespace_t::lock, map_get(), path_t::mount, mount, mount_cache_key(), namespace_t::mountPoints, NULL, namespace_t::parent, path_copy(), path_set(), mount_t::root, RWLOCK_READ_SCOPE, and mount_t::superblock.

Referenced by path_walk(), and path_walk_single_step().