PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
sysfs.h
Go to the documentation of this file.
1#pragma once
2
3#include <kernel/fs/dentry.h>
4#include <kernel/fs/inode.h>
5#include <sys/io.h>
6
7typedef struct file file_t;
8typedef struct file_ops file_ops_t;
9
10typedef struct superblock superblock_t;
11typedef struct superblock_ops superblock_ops_t;
12
13/**
14 * @brief Filesystem for exposing kernel resources.
15 * @ingroup kernel_fs
16 * @defgroup kernel_fs_sysfs SysFS
17 *
18 * The SysFS filesystem is a convenient helper used by various subsystems to expose kernel resources to user space in
19 * the filesystem. For example, the process subsystem uses SysFS to expose process information under `/proc`.
20 *
21 */
22
23#define SYSFS_NAME "sysfs"
24
25/**
26 * @brief Initializes the SysFS.
27 */
28void sysfs_init(void);
29
30/**
31 * @brief Gets the default SysFS directory.
32 *
33 * The default SysFS directory is the root of the `/dev` mount. The `/dev` directory is for devices or "other"
34 * resources which may not warrant an entire dedicated filesystem.
35 *
36 * @return A reference to the `/dev` SysFS directory.
37 */
39
40/**
41 * @brief Mount a new instance of SysFS.
42 *
43 * Used to, for example, create `/dev`, `/proc` and directories whose contents should only be visible within a
44 * specific namespace.
45 *
46 * If `parent` is `NULL`, then the sysfs instance will be mounted to a already existing directory in the root of the
47 * namespace. If it is not `NULL`, then it must point to a sysfs directory, a new directory of the name `name` will
48 * be created inside it and the SysFS instance will be mounted there.
49 *
50 * @param parent The parent directory to mount the SysFS in. If `NULL`, the root of the namespace is used.
51 * @param name The name of the directory to mount the SysFS in.
52 * @param ns The namespace to mount the SysFS in, or `NULL` to use the current process's namespace.
53 * @param flags Mount flags.
54 * @param mode The maximum allowed permissions for files/directories opened under this mount.
55 * @param superblockOps The superblock operations for the new SysFS instance, can be `NULL`.
56 * @return On success, the mounted SysFS instance. On failure, `NULL` and `errno` is set.
57 */
58mount_t* sysfs_mount_new(const path_t* parent, const char* name, namespace_t* ns, mount_flags_t flags, mode_t mode,
60
61/**
62 * @brief Create a new directory inside a mounted SysFS instance.
63 *
64 * Used to, for example, create a new directory for a device or resource inside `/dev` or `/proc`.
65 *
66 * @param parent The parent directory, if `NULL` then `sysfs_get_dev()` is used.
67 * @param name The name of the new directory.
68 * @param inodeOps The inode operations for the new directory, can be `NULL`.
69 * @param private Private data associated with the new directory, can be `NULL`, will be stored in the inode.
70 * @return On success, the new SysFS directory. On failure, `NULL` and `errno` is set.
71 */
72dentry_t* sysfs_dir_new(dentry_t* parent, const char* name, const inode_ops_t* inodeOps, void* private);
73
74/**
75 * @brief Create a new file inside a mounted SysFS instance.
76 *
77 * Used to, for example, create a new file for a device or resource inside `/dev` or `/proc`.
78 *
79 * @param parent The parent directory, if `NULL` then `sysfs_get_dev()` is used.
80 * @param name The name of the new file.
81 * @param inodeOps The inode operations for the new file, can be `NULL`.
82 * @param fileOps The file operations for the new file, can be `NULL`.
83 * @param private Private data associated with the new file, can be `NULL`.
84 * @return On success, the new SysFS file. On failure, `NULL` and `errno` is set.
85 */
86dentry_t* sysfs_file_new(dentry_t* parent, const char* name, const inode_ops_t* inodeOps, const file_ops_t* fileOps,
87 void* private);
88
89/** @} */
mode_t
Path flags and permissions.
Definition path.h:74
mount_flags_t
Mount flags type.
Definition io.h:488
static dentry_t * file
Definition log_file.c:22
static const path_flag_t flags[]
Definition path.c:42
static inode_ops_t inodeOps
Definition ramfs.c:173
static file_ops_t fileOps
Definition ramfs.c:84
static superblock_ops_t superblockOps
Definition socket.c:353
Directory entry structure.
Definition dentry.h:84
File operations structure.
Definition file.h:54
File structure.
Definition file.h:39
Inode operations structure.
Definition inode.h:82
Mount structure.
Definition mount.h:44
Path structure.
Definition path.h:125
Superblock operations structure.
Definition superblock.h:70
Superblock structure.
Definition superblock.h:44
dentry_t * sysfs_get_dev(void)
Gets the default SysFS directory.
Definition sysfs.c:99
mount_t * sysfs_mount_new(const path_t *parent, const char *name, namespace_t *ns, mount_flags_t flags, mode_t mode, const superblock_ops_t *superblockOps)
Mount a new instance of SysFS.
Definition sysfs.c:104
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.
Definition sysfs.c:177
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.
Definition sysfs.c:216
void sysfs_init(void)
Initializes the SysFS.
Definition sysfs.c:82