The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
vfs.h File Reference
#include "common/error.h"
#include "kernel/fs/fs.h"

Go to the source code of this file.

Data Structures

struct  vfs_file_t
 virtual file system file handle structure representing an open file or directory. More...

Macros

#define VFS_PATH_MAX   256
#define VFS_SEEK_SET   0
#define VFS_SEEK_CUR   1
#define VFS_SEEK_END   2

Typedefs

typedef fs_stat_t vfs_stat_t

Functions

kerr_t vfs_mount_root (fs_mount_t *mount)
 mount a filesystem as the root filesystem.
kerr_t vfs_mount (const char *prefix, fs_mount_t *mount)
 mount a filesystem at a path prefix.
bool vfs_mount_at (uint32_t index, char *prefix_out, uint32_t prefix_size, fs_mount_t **mount_out)
 a mounted filesystem, by its place in the mount table (for /proc/mounts).
kerr_t vfs_umount (const char *prefix)
 unmount the filesystem at an exact path prefix.
fs_mount_t * vfs_resolve_mount_path (const char *path, const char **rel_out)
 find the mounted filesystem responsible for an absolute path, and the sub-path relative to that mount's prefix.
kerr_t vfs_follow (char *path, uint32_t size, bool follow_last, const char *root)
 replace the symbolic links along a canonical absolute path by their targets (vfs_link.c).
const char * vfs_inside_root (const char *path, const char *root)
 a real path as a process with this root directory sees it.
bool vfs_join_root (const char *root, const char *virtual_path, char *out, uint32_t size)
 the real path of a path as a process with this root sees it.
kerr_t vfs_readlink (const char *path, char *target, uint32_t size)
 read the target of the symbolic link at path, with a NUL.
kerr_t vfs_symlink (const char *path, const char *target)
 make a symbolic link at path to target.
kerr_t vfs_link (const char *old_path, const char *new_path)
 give a file one more name (both paths on one mount, else KERR_INVAL).
kerr_t vfs_mknod (const char *path, uint32_t mode, uint32_t rdev)
 make a named pipe or a device node (fs_ops_t mknod).
kerr_t vfs_open (const char *path, vfs_file_t *file)
 open a file or directory by absolute path in the virtual file system.
kerr_t vfs_create (const char *path)
 create a regular file by absolute path.
kerr_t vfs_mkdir (const char *path)
 create a directory by absolute path.
kerr_t vfs_remove (const char *path)
 remove a file by absolute path.
kerr_t vfs_rmdir (const char *path)
 remove a directory by absolute path.
kerr_t vfs_rename (const char *old_path, const char *new_path)
 rename a file or directory by absolute paths.
kerr_t vfs_stat (const char *path, vfs_stat_t *stat_out)
 stat a file or directory by absolute path.
bool vfs_resolve_path (const char *cwd, const char *input, char *output, size_t size)
 resolve an input path against the current working directory.
bool vfs_join_path (const char *base, const char *name, char *out, size_t size)
 join two path components into an output buffer.
uint32_t vfs_read (vfs_file_t *file, uint32_t size, void *buffer)
 read data from an open file in the virtual file system.
uint32_t vfs_write (vfs_file_t *file, uint32_t size, const void *buffer)
 write data to an open file in the virtual file system.
kerr_t vfs_truncate (vfs_file_t *file, uint64_t length)
 cut or grow an open file to length bytes (growth reads as zeros).
kerr_t vfs_read_entry (vfs_file_t *file, fs_dirent_t *entryOut)
 read a directory entry from an open directory in the virtual file system.
void vfs_close (vfs_file_t *file)
 close an open file in the virtual file system.
uint8_t vfs_file_type (vfs_file_t *file)
 get the type of an open file in the virtual file system.
int32_t vfs_ioctl (vfs_file_t *file, uint32_t request, void *arg)
 issue a device-control (ioctl) request on an open file.
int32_t vfs_mmap (vfs_file_t *file, addr_space_t as, uintptr_t virt, size_t length, int prot, uint64_t offset)
 map an open file's memory into a process address space.
int64_t vfs_lseek (vfs_file_t *file, int64_t offset, int whence)
 move an open file's offset, as lseek does: through the filesystem's own seek op if it has one (devfs), else the ext2 file cursor.
void vfs_seek_end (vfs_file_t *file)
 move an open file's offset to its end (O_APPEND writes).
kerr_t vfs_fstat (vfs_file_t *file, vfs_stat_t *stat_out)
 stat an open file or directory.
kerr_t vfs_setattr (const char *path, const fs_attr_t *attr)
 change the permission bits and/or owner of a file by path, or of an open file.
kerr_t vfs_fsetattr (vfs_file_t *file, const fs_attr_t *attr)

Macro Definition Documentation

◆ VFS_PATH_MAX

#define VFS_PATH_MAX   256

◆ VFS_SEEK_SET

#define VFS_SEEK_SET   0

◆ VFS_SEEK_CUR

#define VFS_SEEK_CUR   1

◆ VFS_SEEK_END

#define VFS_SEEK_END   2

Typedef Documentation

◆ vfs_stat_t

Function Documentation

◆ vfs_mount_root()

kerr_t vfs_mount_root ( fs_mount_t * mount)

mount a filesystem as the root filesystem.

convenience wrapper for vfs_mount("/", mount).

Parameters
mountpointer to the filesystem mount structure to be used as the root filesystem.
Returns
KERR_OK on success, or an error code on failure.

◆ vfs_mount()

kerr_t vfs_mount ( const char * prefix,
fs_mount_t * mount )

mount a filesystem at a path prefix.

a later, more specific prefix takes precedence over a shorter one during path resolution (e.g. "/dev" wins over "/"). remounting an existing prefix replaces its backing filesystem.

Parameters
prefixabsolute mount point, e.g. "/" or "/dev".
mountthe filesystem to mount there (must already be mounted).
Returns
KERR_OK on success, KERR_INVAL on bad input, KERR_NOSPC if full.

◆ vfs_mount_at()

bool vfs_mount_at ( uint32_t index,
char * prefix_out,
uint32_t prefix_size,
fs_mount_t ** mount_out )

a mounted filesystem, by its place in the mount table (for /proc/mounts).

Parameters
index0, 1, ... the entries in the order they were mounted.
prefix_outgets the mount point.
mount_outgets the filesystem.
Returns
false when index is past the last entry.

◆ vfs_umount()

kerr_t vfs_umount ( const char * prefix)

unmount the filesystem at an exact path prefix.

Parameters
prefixthe mount point to remove.
Returns
KERR_OK on success, KERR_NOENT if nothing is mounted there.

◆ vfs_resolve_mount_path()

fs_mount_t * vfs_resolve_mount_path ( const char * path,
const char ** rel_out )

find the mounted filesystem responsible for an absolute path, and the sub-path relative to that mount's prefix.

the backing filesystem is handed a path relative to its own mount point, not the full absolute path: the root mount "/" sees the path unchanged (still leading '/'), while a sub-mount at "/dev" sees "/fb0" for "/dev/fb0" and "/" for "/dev" itself. the relative path always keeps a leading '/'.

Parameters
pathabsolute path to resolve.
rel_outif non-NULL, receives a pointer into path (or a static "/") for the mount-relative sub-path. only valid while path is.
Returns
the backing mount, or NULL if no filesystem covers the path.

◆ vfs_follow()

kerr_t vfs_follow ( char * path,
uint32_t size,
bool follow_last,
const char * root )

replace the symbolic links along a canonical absolute path by their targets (vfs_link.c).

Parameters
paththe real path, changed in place.
sizethe size of the path buffer.
follow_lastalso the last name, when it is a link.
rootthe process's root directory ("/" for the real one): an absolute target starts there, and ".." stops there.
Returns
KERR_OK (a missing name is left for the operation to report), KERR_LOOP (too many links), KERR_RANGE (the path got too long).

◆ vfs_inside_root()

const char * vfs_inside_root ( const char * path,
const char * root )

a real path as a process with this root directory sees it.

Returns
a pointer into path, or "/"; path itself when it is not under the root.

◆ vfs_join_root()

bool vfs_join_root ( const char * root,
const char * virtual_path,
char * out,
uint32_t size )

the real path of a path as a process with this root sees it.

Returns
false when it does not fit in size.

◆ vfs_readlink()

kerr_t vfs_readlink ( const char * path,
char * target,
uint32_t size )

read the target of the symbolic link at path, with a NUL.

Returns
KERR_OK, KERR_INVAL (not a link), KERR_RANGE, KERR_NOTFOUND.

◆ vfs_symlink()

kerr_t vfs_symlink ( const char * path,
const char * target )

make a symbolic link at path to target.

◆ vfs_link()

kerr_t vfs_link ( const char * old_path,
const char * new_path )

give a file one more name (both paths on one mount, else KERR_INVAL).

Returns
KERR_OK, KERR_ISDIR, KERR_EXIST, KERR_RANGE, KERR_NOTFOUND.

◆ vfs_mknod()

kerr_t vfs_mknod ( const char * path,
uint32_t mode,
uint32_t rdev )

make a named pipe or a device node (fs_ops_t mknod).

◆ vfs_open()

kerr_t vfs_open ( const char * path,
vfs_file_t * file )

open a file or directory by absolute path in the virtual file system.

Parameters
pathabsolute path to the file or directory.
filepointer to a vfs_file_t structure that will be initialized with the opened file
Returns
KERR_OK on success, or an error code on failure.

◆ vfs_create()

kerr_t vfs_create ( const char * path)

create a regular file by absolute path.

Parameters
pathabsolute path to the new file.
Returns
KERR_OK on success, or an error code on failure.

◆ vfs_mkdir()

kerr_t vfs_mkdir ( const char * path)

create a directory by absolute path.

Parameters
pathabsolute path to the new directory.
Returns
KERR_OK on success, or an error code on failure.

◆ vfs_remove()

kerr_t vfs_remove ( const char * path)

remove a file by absolute path.

Parameters
pathabsolute path to the file to remove.
Returns
KERR_OK on success, or an error code on failure.

◆ vfs_rmdir()

kerr_t vfs_rmdir ( const char * path)

remove a directory by absolute path.

Parameters
pathabsolute path to the directory to remove.
Returns
KERR_OK on success, or an error code on failure.

◆ vfs_rename()

kerr_t vfs_rename ( const char * old_path,
const char * new_path )

rename a file or directory by absolute paths.

Parameters
old_pathabsolute path to the existing file or directory.
new_pathabsolute path to the new name for the file or directory.
Returns
KERR_OK on success, or an error code on failure.

◆ vfs_stat()

kerr_t vfs_stat ( const char * path,
vfs_stat_t * stat_out )

stat a file or directory by absolute path.

Parameters
pathabsolute path to the file or directory.
stat_outpointer to a vfs_stat_t structure that will be filled with the file's metadata.
Returns
KERR_OK on success, or an error code on failure.

◆ vfs_resolve_path()

bool vfs_resolve_path ( const char * cwd,
const char * input,
char * output,
size_t size )

resolve an input path against the current working directory.

absolute paths are returned as-is, relative paths are prefixed with cwd.

Parameters
cwdcurrent working directory.
inputuser-provided path.
outputdestination buffer for resolved absolute path.
sizesize of destination buffer in bytes.
Returns
true on success, false if inputs are invalid or output would overflow.

◆ vfs_join_path()

bool vfs_join_path ( const char * base,
const char * name,
char * out,
size_t size )

join two path components into an output buffer.

avoids duplicate separators when base already ends with '/'.

Parameters
basebase path component.
namechild path component.
outdestination buffer.
sizedestination buffer size in bytes.
Returns
true on success, false on invalid input or insufficient output size.

◆ vfs_read()

uint32_t vfs_read ( vfs_file_t * file,
uint32_t size,
void * buffer )

read data from an open file in the virtual file system.

Parameters
filepointer to the vfs_file_t structure representing the open file.
sizenumber of bytes to read.
bufferpointer to the buffer where the read data will be stored.
Returns
number of bytes actually read, or 0 on failure.

◆ vfs_write()

uint32_t vfs_write ( vfs_file_t * file,
uint32_t size,
const void * buffer )

write data to an open file in the virtual file system.

Parameters
filepointer to the vfs_file_t structure representing the open file.
sizenumber of bytes to write.
bufferpointer to the source data.
Returns
number of bytes actually written, or 0 on failure.

◆ vfs_truncate()

kerr_t vfs_truncate ( vfs_file_t * file,
uint64_t length )

cut or grow an open file to length bytes (growth reads as zeros).

Returns
KERR_OK on success, or an error code on failure.

◆ vfs_read_entry()

kerr_t vfs_read_entry ( vfs_file_t * file,
fs_dirent_t * entryOut )

read a directory entry from an open directory in the virtual file system.

Parameters
filepointer to the vfs_file_t structure representing the open directory.
entryOutpointer to output directory entry.
Returns
KERR_OK on success, or an error code on failure.

◆ vfs_close()

void vfs_close ( vfs_file_t * file)

close an open file in the virtual file system.

Parameters
filepointer to the vfs_file_t structure representing the open file.
Returns
void

◆ vfs_file_type()

uint8_t vfs_file_type ( vfs_file_t * file)

get the type of an open file in the virtual file system.

Parameters
filepointer to the vfs_file_t structure representing the open file.
Returns
file type (FS_TYPE_FILE, FS_TYPE_DIR, or FS_TYPE_UNKNOWN).

◆ vfs_ioctl()

int32_t vfs_ioctl ( vfs_file_t * file,
uint32_t request,
void * arg )

issue a device-control (ioctl) request on an open file.

dispatches through the backing filesystem; only special filesystems (devfs) implement it, forwarding to the device. others return -ENOTTY.

Parameters
filepointer to the vfs_file_t representing the open file.
requestdriver-defined request code.
argrequest-specific argument (typically a bounds-checked pointer).
Returns
0 on success, or a negative errno on failure.

◆ vfs_mmap()

int32_t vfs_mmap ( vfs_file_t * file,
addr_space_t as,
uintptr_t virt,
size_t length,
int prot,
uint64_t offset )

map an open file's memory into a process address space.

dispatches through the backing filesystem; only device nodes (devfs) that forward to a mappable device succeed. others return -ENODEV.

Parameters
filepointer to the vfs_file_t representing the open file.
page_directoryphysical address of the target process page directory.
virtpage-aligned destination virtual address.
lengthbytes to map (page-multiple).
protPROT_* bits.
offsetpage-aligned byte offset into the mapped object.
Returns
0 on success, or a negative errno on failure.

◆ vfs_lseek()

int64_t vfs_lseek ( vfs_file_t * file,
int64_t offset,
int whence )

move an open file's offset, as lseek does: through the filesystem's own seek op if it has one (devfs), else the ext2 file cursor.

Parameters
filethe open file.
offsetbytes relative to whence.
whenceVFS_SEEK_SET, VFS_SEEK_CUR or VFS_SEEK_END.
Returns
the new offset, or a negative errno (EINVAL for a bad whence or a negative result).

◆ vfs_seek_end()

void vfs_seek_end ( vfs_file_t * file)

move an open file's offset to its end (O_APPEND writes).

Parameters
filethe open file.

◆ vfs_fstat()

kerr_t vfs_fstat ( vfs_file_t * file,
vfs_stat_t * stat_out )

stat an open file or directory.

Parameters
filethe open file.
stat_outreceives its metadata.
Returns
KERR_OK, or an error code.

◆ vfs_setattr()

kerr_t vfs_setattr ( const char * path,
const fs_attr_t * attr )

change the permission bits and/or owner of a file by path, or of an open file.

Returns
KERR_OK, KERR_PERM if the filesystem keeps no owners, or the error of opening the path.

◆ vfs_fsetattr()

kerr_t vfs_fsetattr ( vfs_file_t * file,
const fs_attr_t * attr )