PatchworkOS  19e446b
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/vnode.h>
5#include <sys/fs.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 System Filesystem.
15 * @ingroup kernel_fs
16 * @defgroup kernel_fs_sysfs System Filesystem
17 *
18 * The sysfs is a virtual filesystem that provides information about devices and kernel modules.
19 *
20 * @{
21 */
22
23/**
24 * @brief The name of the system filesystem.
25 */
26#define SYSFS_NAME "sysfs"
27
28/**
29 * @brief Initializes the sysfs and mount an instance at `/sys`.
30 *
31 * The System Filesystem is one of the few filesystem that will be mounted automatically by the kernel, this is
32 * necessary as otherwise user space would be unable to access the `fs` sysfs directory and thus unable to mount any
33 * filesystem.
34 *
35 */
36void sysfs_init(void);
37
38/**
39 * @brief Create a new directory inside a mounted sysfs instance.
40 *
41 * @param parent The parent directory, if `NULL` then the root is used.
42 * @param name The name of the new directory.
43 * @param vnodeOps The vnode operations for the new directory, can be `NULL`.
44 * @param private Private data to store in the vnode of the new directory, can be `NULL`.
45 * @return On success, the new sysfs directory. On failure, `NULL` and `errno` is set.
46 */
47dentry_t* sysfs_dir_new(dentry_t* parent, const char* name, const vnode_ops_t* vnodeOps, void* data);
48
49/**
50 * @brief Create a new file inside a mounted sysfs instance.
51 *
52 * @param parent The parent directory, if `NULL` then the root is used.
53 * @param name The name of the new file.
54 * @param vnodeOps The vnode operations for the new file, can be `NULL`.
55 * @param fileOps The file operations for the new file, can be `NULL`.
56 * @param private Private data to store in the vnode of the new file, can be `NULL`.
57 * @return On success, the new sysfs file. On failure, `NULL` and `errno` is set.
58 */
59dentry_t* sysfs_file_new(dentry_t* parent, const char* name, const vnode_ops_t* vnodeOps, const file_ops_t* fileOps,
60 void* data);
61
62/**
63 * @brief Create a new symbolic link inside a mounted sysfs instance.
64 *
65 * @param parent The parent directory, if `NULL` then the root is used.
66 * @param name The name of the new symbolic link.
67 * @param vnodeOps The vnode operations for the new symbolic link.
68 * @param private Private data to store in the vnode of the new symbolic link, can be `NULL`.
69 * @return On success, the new sysfs symbolic link. On failure, `NULL` and `errno` is set.
70 */
71dentry_t* sysfs_symlink_new(dentry_t* parent, const char* name, const vnode_ops_t* vnodeOps, void* data);
72
73/**
74 * @brief Descriptor for batch file creation.
75 * @struct sysfs_file_desc_t
76 */
77typedef struct sysfs_file_desc
78{
79 const char* name; ///< Name of the file, `NULL` marks end of array.
80 const vnode_ops_t* vnodeOps; ///< Vnode operations, can be `NULL`.
81 const file_ops_t* fileOps; ///< File operations, can be `NULL`.
82 void* data; ///< Private data to store in the vnode of the file.
84
85/**
86 * @brief Create multiple files in a sysfs directory.
87 *
88 * @param out Output list to store created dentries, can be `NULL`. The dentries use the `otherEntry` list entry.
89 * @param parent The parent directory, if `NULL` then the root is used.
90 * @param descs Array of file descriptors, terminated by an entry with `name == NULL`.
91 * @return On success, the number of files created. On failure, `ERR` and `errno` is set.
92 */
93uint64_t sysfs_files_new(list_t* out, dentry_t* parent, const sysfs_file_desc_t* descs);
94
95/**
96 * @brief Free all files in a list created by `sysfs_files_new()`.
97 *
98 * @param files The list of files to free.
99 */
101
102/** @} */
static fd_t data
Definition dwm.c:21
dentry_t * sysfs_symlink_new(dentry_t *parent, const char *name, const vnode_ops_t *vnodeOps, void *data)
Create a new symbolic link inside a mounted sysfs instance.
Definition sysfs.c:193
uint64_t sysfs_files_new(list_t *out, dentry_t *parent, const sysfs_file_desc_t *descs)
Create multiple files in a sysfs directory.
Definition sysfs.c:227
void sysfs_files_free(list_t *files)
Free all files in a list created by sysfs_files_new().
Definition sysfs.c:282
void sysfs_init(void)
Initializes the sysfs and mount an instance at /sys.
Definition sysfs.c:50
dentry_t * sysfs_file_new(dentry_t *parent, const char *name, const vnode_ops_t *vnodeOps, const file_ops_t *fileOps, void *data)
Create a new file inside a mounted sysfs instance.
Definition sysfs.c:153
dentry_t * sysfs_dir_new(dentry_t *parent, const char *name, const vnode_ops_t *vnodeOps, void *data)
Create a new directory inside a mounted sysfs instance.
Definition sysfs.c:114
static list_t files
Definition file.c:9
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
Directory entry structure.
Definition dentry.h:155
File operations structure.
Definition file.h:54
File structure.
Definition file.h:39
A doubly linked list.
Definition list.h:46
Superblock operations structure.
Definition superblock.h:59
Superblock structure.
Definition superblock.h:33
Descriptor for batch file creation.
Definition sysfs.h:78
void * data
Private data to store in the vnode of the file.
Definition sysfs.h:82
const vnode_ops_t * vnodeOps
Vnode operations, can be NULL.
Definition sysfs.h:80
const char * name
Name of the file, NULL marks end of array.
Definition sysfs.h:79
const file_ops_t * fileOps
File operations, can be NULL.
Definition sysfs.h:81
vnode operations structure.
Definition vnode.h:69
static vnode_ops_t vnodeOps
Definition tmpfs.c:195
static file_ops_t fileOps
Definition tmpfs.c:86