PatchworkOS
Loading...
Searching...
No Matches
VFS Context

Virtual File System context. More...

Data Structures

struct  vfs_ctx_t
 VFS context structure. More...
 

Functions

void vfs_ctx_init (vfs_ctx_t *ctx, const path_t *cwd)
 Initialize a VFS context.
 
void vfs_ctx_deinit (vfs_ctx_t *ctx)
 Deinitialize a VFS context.
 
file_tvfs_ctx_get_file (vfs_ctx_t *ctx, fd_t fd)
 Get a file from a VFS context.
 
uint64_t vfs_ctx_get_cwd (vfs_ctx_t *ctx, path_t *outCwd)
 
uint64_t vfs_ctx_set_cwd (vfs_ctx_t *ctx, const path_t *cwd)
 Set the current working directory of a VFS context.
 
fd_t vfs_ctx_alloc_fd (vfs_ctx_t *ctx, file_t *file)
 Allocate a new file descriptor in a VFS context.
 
fd_t vfs_ctx_set_fd (vfs_ctx_t *ctx, fd_t fd, file_t *file)
 Allocate a specific file descriptor in a VFS context.
 
uint64_t vfs_ctx_free_fd (vfs_ctx_t *ctx, fd_t fd)
 Free a file descriptor in a VFS context.
 
fd_t vfs_ctx_dup (vfs_ctx_t *ctx, fd_t oldFd)
 Duplicate a file descriptor in a VFS context.
 
fd_t vfs_ctx_dup2 (vfs_ctx_t *ctx, fd_t oldFd, fd_t newFd)
 Duplicate a file descriptor in a VFS context to a specific file descriptor.
 

Detailed Description

Virtual File System context.

A VFS context represents the state of the VFS for a single process. It contains the current working directory and the open file descriptors.

Function Documentation

◆ vfs_ctx_alloc_fd()

fd_t vfs_ctx_alloc_fd ( vfs_ctx_t ctx,
file_t file 
)

Allocate a new file descriptor in a VFS context.

Will create a new reference to the file.

Parameters
ctxThe context to open the file descriptor in.
fileThe file to open.
Returns
On success, the new file descriptor. On failure, ERR and errno is set.

Definition at line 142 of file vfs_ctx.c.

References vfs_ctx_t::allocBitmap, bitmap_find_first_clear(), bitmap_set(), CONFIG_MAX_FD, EBUSY, EMFILE, ERR, errno, file, vfs_ctx_t::files, vfs_ctx_t::initalized, vfs_ctx_t::lock, LOCK_SCOPE, and REF.

Referenced by SYSCALL_DEFINE(), SYSCALL_DEFINE(), and SYSCALL_DEFINE().

◆ vfs_ctx_deinit()

void vfs_ctx_deinit ( vfs_ctx_t ctx)

Deinitialize a VFS context.

This will close all open file descriptors and release the current working directory.

Parameters
ctxThe context to deinitialize.

Definition at line 30 of file vfs_ctx.c.

References CONFIG_MAX_FD, vfs_ctx_t::cwd, DEREF, vfs_ctx_t::files, vfs_ctx_t::initalized, vfs_ctx_t::lock, LOCK_SCOPE, NULL, and path_put().

Referenced by process_kill().

◆ vfs_ctx_dup()

fd_t vfs_ctx_dup ( vfs_ctx_t ctx,
fd_t  oldFd 
)

Duplicate a file descriptor in a VFS context.

Will create a new reference to the file.

Parameters
ctxThe context to duplicate the file descriptor in.
oldFdThe file descriptor to duplicate.
Returns
On success, the new file descriptor. On failure, ERR and errno is set.

Definition at line 218 of file vfs_ctx.c.

References vfs_ctx_t::allocBitmap, bitmap_find_first_clear(), bitmap_set(), CONFIG_MAX_FD, EBADF, EBUSY, EMFILE, ERR, errno, vfs_ctx_t::files, vfs_ctx_t::initalized, vfs_ctx_t::lock, LOCK_SCOPE, NULL, and REF.

Referenced by SYSCALL_DEFINE().

◆ vfs_ctx_dup2()

fd_t vfs_ctx_dup2 ( vfs_ctx_t ctx,
fd_t  oldFd,
fd_t  newFd 
)

Duplicate a file descriptor in a VFS context to a specific file descriptor.

Will create a new reference to the file and if the newFd is already in use, it will be closed first.

Parameters
ctxThe context to duplicate the file descriptor in.
oldFdThe file descriptor to duplicate.
newFdThe file descriptor to duplicate to.
Returns
On success, the new file descriptor. On failure, ERR and errno is set.

Definition at line 251 of file vfs_ctx.c.

References vfs_ctx_t::allocBitmap, bitmap_set(), CONFIG_MAX_FD, DEREF, EBADF, EBUSY, ERR, errno, vfs_ctx_t::files, vfs_ctx_t::initalized, vfs_ctx_t::lock, LOCK_SCOPE, NULL, and REF.

Referenced by SYSCALL_DEFINE().

◆ vfs_ctx_free_fd()

uint64_t vfs_ctx_free_fd ( vfs_ctx_t ctx,
fd_t  fd 
)

Free a file descriptor in a VFS context.

Will release the reference to the file.

Parameters
ctxThe context to close the file descriptor in.
fdThe file descriptor to close.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 191 of file vfs_ctx.c.

References vfs_ctx_t::allocBitmap, bitmap_clear(), CONFIG_MAX_FD, DEREF, EBADF, EBUSY, ERR, errno, vfs_ctx_t::files, vfs_ctx_t::initalized, vfs_ctx_t::lock, LOCK_SCOPE, and NULL.

Referenced by SYSCALL_DEFINE(), and SYSCALL_DEFINE().

◆ vfs_ctx_get_cwd()

uint64_t vfs_ctx_get_cwd ( vfs_ctx_t ctx,
path_t outCwd 
)

@Get the current working directory of a VFS context.

If the current directory is NULL, then returns a reference to the root of the kernel process's namespace.

Will create a new reference to the cwd.

Parameters
ctxThe context to get the current working directory of.
outCwdThe output path to store the current working directory in.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 67 of file vfs_ctx.c.

References assert, vfs_ctx_t::cwd, path_t::dentry, EBUSY, ERR, errno, vfs_ctx_t::initalized, vfs_ctx_t::lock, LOCK_SCOPE, path_t::mount, namespace_get_root_path(), NULL, path_copy(), and process_get_kernel().

Referenced by process_cwd_read(), process_init(), vfs_walk(), and vfs_walk_parent().

◆ vfs_ctx_get_file()

file_t * vfs_ctx_get_file ( vfs_ctx_t ctx,
fd_t  fd 
)

Get a file from a VFS context.

Parameters
ctxThe context to get the file from.
fdThe file descriptor to get.
Returns
On success, a new reference to the file. On failure, returns NULL and errno is set.

Definition at line 48 of file vfs_ctx.c.

References CONFIG_MAX_FD, EBADF, EBUSY, errno, vfs_ctx_t::files, vfs_ctx_t::initalized, vfs_ctx_t::lock, LOCK_SCOPE, NULL, and REF.

Referenced by SYSCALL_DEFINE(), SYSCALL_DEFINE(), SYSCALL_DEFINE(), SYSCALL_DEFINE(), SYSCALL_DEFINE(), SYSCALL_DEFINE(), SYSCALL_DEFINE(), SYSCALL_DEFINE(), SYSCALL_DEFINE(), and SYSCALL_DEFINE().

◆ vfs_ctx_init()

void vfs_ctx_init ( vfs_ctx_t ctx,
const path_t cwd 
)

Initialize a VFS context.

Parameters
ctxThe context to initialize.
cwdThe initial current working directory, can be NULL.

Definition at line 11 of file vfs_ctx.c.

References vfs_ctx_t::allocBitmap, vfs_ctx_t::allocBitmapBuffer, bitmap_init(), CONFIG_MAX_FD, vfs_ctx_t::cwd, vfs_ctx_t::files, vfs_ctx_t::initalized, vfs_ctx_t::lock, lock_init(), memset(), NULL, path_copy(), and PATH_EMPTY.

Referenced by process_init().

◆ vfs_ctx_set_cwd()

uint64_t vfs_ctx_set_cwd ( vfs_ctx_t ctx,
const path_t cwd 
)

Set the current working directory of a VFS context.

Will create a new reference to the cwd and release the old one.

Parameters
ctxThe context to set the current working directory of.
cwdThe new current working directory.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 93 of file vfs_ctx.c.

References vfs_ctx_t::cwd, path_t::dentry, EBUSY, EINVAL, ENOTDIR, ERR, errno, vfs_ctx_t::initalized, dentry_t::inode, INODE_DIR, vfs_ctx_t::lock, LOCK_SCOPE, NULL, path_copy(), path_put(), and inode_t::type.

Referenced by SYSCALL_DEFINE().

◆ vfs_ctx_set_fd()

fd_t vfs_ctx_set_fd ( vfs_ctx_t ctx,
fd_t  fd,
file_t file 
)

Allocate a specific file descriptor in a VFS context.

Will create a new reference to the file and release the old one if it exists.

Parameters
ctxThe context to open the file descriptor in.
fdThe file descriptor to set.
fileThe file to open.
Returns
On success, the file descriptor. On failure, ERR and errno is set.

Definition at line 164 of file vfs_ctx.c.

References vfs_ctx_t::allocBitmap, bitmap_set(), CONFIG_MAX_FD, DEREF, EBUSY, EINVAL, ERR, errno, file, vfs_ctx_t::files, vfs_ctx_t::initalized, vfs_ctx_t::lock, LOCK_SCOPE, NULL, and REF.

Referenced by init_process_spawn(), and SYSCALL_DEFINE().