|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <kernel/fs/dentry.h> |
| 4 | +#include <kernel/fs/inode.h> |
| 5 | +#include <sys/io.h> |
| 6 | + |
| 7 | +typedef struct file file_t; |
| 8 | +typedef struct file_ops file_ops_t; |
| 9 | + |
| 10 | +typedef struct superblock superblock_t; |
| 11 | +typedef 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 | + */ |
| 36 | +void 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 inodeOps The inode operations for the new directory, can be `NULL`. |
| 44 | + * @param private Private data to store in the inode of the new directory, can be `NULL`. |
| 45 | + * @return On success, the new sysfs directory. On failure, `NULL` and `errno` is set. |
| 46 | + */ |
| 47 | +dentry_t* sysfs_dir_new(dentry_t* parent, const char* name, const inode_ops_t* inodeOps, void* private); |
| 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 inodeOps The inode 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 inode of the new file, can be `NULL`. |
| 57 | + * @return On success, the new sysfs file. On failure, `NULL` and `errno` is set. |
| 58 | + */ |
| 59 | +dentry_t* sysfs_file_new(dentry_t* parent, const char* name, const inode_ops_t* inodeOps, const file_ops_t* fileOps, |
| 60 | + void* private); |
| 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 inodeOps The inode operations for the new symbolic link. |
| 68 | + * @param private Private data to store in the inode 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 | + */ |
| 71 | +dentry_t* sysfs_symlink_new(dentry_t* parent, const char* name, const inode_ops_t* inodeOps, void* private); |
| 72 | + |
| 73 | +/** |
| 74 | + * @brief Descriptor for batch file creation. |
| 75 | + * @struct sysfs_file_desc_t |
| 76 | + */ |
| 77 | +typedef struct sysfs_file_desc |
| 78 | +{ |
| 79 | + const char* name; ///< Name of the file, `NULL` marks end of array. |
| 80 | + const inode_ops_t* inodeOps; ///< Inode operations, can be `NULL`. |
| 81 | + const file_ops_t* fileOps; ///< File operations, can be `NULL`. |
| 82 | + void* private; ///< Private data to store in the inode of the file. |
| 83 | +} sysfs_file_desc_t; |
| 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 | + */ |
| 93 | +uint64_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 | + */ |
| 100 | +void sysfs_files_free(list_t* files); |
| 101 | + |
| 102 | +/** @} */ |
0 commit comments