PatchworkOS
Loading...
Searching...
No Matches
sysfs.c File Reference
#include <kernel/fs/sysfs.h>
#include <kernel/fs/dentry.h>
#include <kernel/fs/namespace.h>
#include <kernel/fs/vfs.h>
#include <kernel/log/log.h>
#include <kernel/log/panic.h>
#include <kernel/sync/lock.h>
#include <assert.h>
#include <errno.h>
#include <stdatomic.h>
#include <sys/list.h>

Go to the source code of this file.

Data Structures

struct  sysfs_mount_ctx_t
 

Functions

static _Atomic (inode_number_t)
 
static dentry_tsysfs_mount (filesystem_t *fs, const char *devName, void *private)
 
void sysfs_init (void)
 Initializes the SysFS.
 
dentry_tsysfs_get_dev (void)
 Gets the default SysFS directory.
 
mount_tsysfs_mount_new (const path_t *parent, const char *name, namespace_t *ns, const superblock_ops_t *superblockOps)
 Mount a new instance of SysFS.
 
dentry_tsysfs_dir_new (dentry_t *parent, const char *name, const inode_ops_t *inodeOps, void *private)
 Create a new directory inside a mounted SysFS instance.
 
dentry_tsysfs_file_new (dentry_t *parent, const char *name, const inode_ops_t *inodeOps, const file_ops_t *fileOps, void *private)
 Create a new file inside a mounted SysFS instance.
 

Variables

static dentry_ops_t dentryOps
 
static filesystem_t sysfs
 

Function Documentation

◆ _Atomic()

static _Atomic ( inode_number_t  )
staticnew

Definition at line 15 of file sysfs.c.

◆ sysfs_dir_new()

dentry_t * sysfs_dir_new ( dentry_t parent,
const char *  name,
const inode_ops_t inodeOps,
void *  private 
)

Create a new directory inside a mounted SysFS instance.

Used to, for example, create a new directory for a device or resource inside /dev or /proc.

Parameters
parentThe parent directory, if NULL then sysfs_get_dev() is used.
nameThe name of the new directory.
inodeOpsThe inode operations for the new directory, can be NULL.
privatePrivate data associated with the new directory, can be NULL, will be stored in the inode.
Returns
On success, the new SysFS directory. On failure, NULL and errno is set.

Definition at line 174 of file sysfs.c.

References atomic_fetch_add, dentry_make_positive(), dentry_new(), DEREF_DEFER, EINVAL, ERR, errno, EXDEV, superblock_t::fs, INODE_DIR, inode_new(), inodeOps, NULL, inode_t::private, REF, mount_t::root, dentry_t::superblock, and sysfs.

Referenced by acpi_tables_expose(), fb_new(), kbd_new(), local_listen_dir_init(), mouse_new(), pipe_init(), process_dir_init(), process_procfs_init(), shmem_init(), socket_family_register(), and statistics_init().

◆ sysfs_file_new()

dentry_t * sysfs_file_new ( dentry_t parent,
const char *  name,
const inode_ops_t inodeOps,
const file_ops_t fileOps,
void *  private 
)

Create a new file inside a mounted SysFS instance.

Used to, for example, create a new file for a device or resource inside /dev or /proc.

Parameters
parentThe parent directory, if NULL then sysfs_get_dev() is used.
nameThe name of the new file.
inodeOpsThe inode operations for the new file, can be NULL.
fileOpsThe file operations for the new file, can be NULL.
privatePrivate data associated with the new file, can be NULL.
Returns
On success, the new SysFS file. On failure, NULL and errno is set.

Definition at line 216 of file sysfs.c.

References atomic_fetch_add, dentry_make_positive(), dentry_new(), DEREF_DEFER, EINVAL, ERR, errno, EXDEV, file, fileOps, superblock_t::fs, INODE_FILE, inode_new(), inodeOps, NULL, inode_t::private, REF, mount_t::root, dentry_t::superblock, and sysfs.

Referenced by acpi_tables_expose(), const_init(), fb_new(), kbd_new(), local_listen_new(), log_file_expose(), mouse_new(), pipe_init(), process_dir_init(), shmem_init(), socket_family_register(), socket_new(), and statistics_init().

◆ sysfs_get_dev()

dentry_t * sysfs_get_dev ( void  )

Gets the default SysFS directory.

The default SysFS directory is the root of the /dev mount. The /dev directory is for devices or "other" resources which may not warrant an entire dedicated filesystem.

Returns
A reference to the /dev SysFS directory.

Definition at line 95 of file sysfs.c.

References REF, and mount_t::root.

◆ sysfs_init()

void sysfs_init ( void  )

Initializes the SysFS.

Definition at line 79 of file sysfs.c.

References ERR, LOG_INFO, NULL, panic(), sysfs, sysfs_mount_new(), and vfs_register_fs().

Referenced by init_finalize().

◆ sysfs_mount()

◆ sysfs_mount_new()

mount_t * sysfs_mount_new ( const path_t parent,
const char *  name,
namespace_t ns,
const superblock_ops_t superblockOps 
)

Mount a new instance of SysFS.

Used to, for example, create /dev, /proc and directories whose contents should only be visible within a specific namespace.

If parent is NULL, then the sysfs instance will be mounted to a already existing directory in the root of the namespace. If it is not NULL, then it must point to a sysfs directory, a new directory of the name name will be created inside it and the SysFS instance will be mounted there.

Parameters
parentThe parent directory to mount the SysFS in. If NULL, the root of the namespace is used.
nameThe name of the directory to mount the SysFS in.
nsThe namespace to mount the SysFS in. If NULL, the kernel process's namespace is used.
superblockOpsThe superblock operations for the new SysFS instance, can be NULL.
Returns
On success, the mounted SysFS instance. On failure, NULL and errno is set.

Definition at line 100 of file sysfs.c.

References assert, atomic_fetch_add, path_t::dentry, dentry_make_positive(), dentry_new(), DEREF_DEFER, EINVAL, ERR, errno, EXDEV, superblock_t::fs, INODE_DIR, inode_new(), kernelProcess, path_t::mount, mount, namespace_mount(), NULL, PATH_CREATE, PATH_DEFER, PATH_EMPTY, path_put(), path_set(), process_get_kernel(), mount_t::root, namespace_t::rootMount, dentry_t::superblock, sysfs_mount_ctx_t::superblockOps, superblockOps, sysfs, SYSFS_NAME, VFS_DEVICE_NAME_NONE, and vfs_get_or_lookup_dentry().

Referenced by acpi_get_sysfs_root(), net_init(), process_procfs_init(), socket_new(), and sysfs_init().

Variable Documentation

◆ dentryOps

dentry_ops_t dentryOps
static
Initial value:
= {
}
uint64_t dentry_generic_getdents(dentry_t *dentry, dirent_t *buffer, uint64_t count, uint64_t *offset, path_flags_t flags)
Helper function for a basic getdents.
Definition dentry.c:195

Definition at line 23 of file sysfs.c.

Referenced by sysfs_mount().

◆ sysfs

filesystem_t sysfs
static
Initial value:
= {
.name = SYSFS_NAME,
.mount = sysfs_mount,
}
static dentry_t * sysfs_mount(filesystem_t *fs, const char *devName, void *private)
Definition sysfs.c:32
#define SYSFS_NAME
Definition sysfs.h:22

Definition at line 74 of file sysfs.c.

Referenced by sysfs_dir_new(), sysfs_file_new(), sysfs_init(), and sysfs_mount_new().