PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
file.h
Go to the documentation of this file.
1#pragma once
2
3#include <kernel/fs/path.h>
5#include <kernel/utils/ref.h>
6
7#include <stdatomic.h>
8#include <stdint.h>
9#include <sys/io.h>
10#include <sys/proc.h>
11
12typedef struct wait_queue wait_queue_t;
13
14typedef struct file file_t;
15typedef struct file_ops file_ops_t;
16typedef struct dentry dentry_t;
17typedef struct inode inode_t;
18typedef struct poll_file poll_file_t;
19
20/**
21 * @brief Underlying type of a file descriptor.
22 * @defgroup kernel_fs_file File
23 * @ingroup kernel_fs
24 *
25 * A file is the underlying type of a file descriptor. Note that internally the kernel does not use file descriptors,
26 * they are simply a per-process handle to a file. The kernel uses files directly.
27 *
28 * @{
29 */
30
31/**
32 * @brief File structure.
33 * @struct file_t
34 *
35 * A file structure is protected by the mutex of its inode.
36 *
37 */
38typedef struct file
39{
46 void* private;
47} file_t;
48
49/**
50 * @brief File operations structure.
51 * @struct file_ops_t
52 */
53typedef struct file_ops
54{
57 void (*close)(file_t* file);
59 uint64_t (*write)(file_t* file, const void* buffer, uint64_t count, uint64_t* offset);
61 uint64_t (*ioctl)(file_t* file, uint64_t request, void* argp, uint64_t size);
62 wait_queue_t* (*poll)(file_t* file, poll_events_t* revents);
63 void* (*mmap)(file_t* file, void* address, uint64_t length, uint64_t* offset, pml_flags_t flags);
65
66/**
67 * @brief Structure for polling multiple files.
68 * @struct poll_file_t
69 */
76
77/**
78 * @brief Create a new file structure.
79 *
80 * This does not open the file, instead its used internally by the VFS when opening files.
81 *
82 * There is no `file_free()` instead use `UNREF()`.
83 *
84 * @param path The path of the file.
85 * @param mode The mode with which the file was opened, if no permissions are specified the maximum allowed permissions
86 * from the mount are used.
87 * @return On success, the new file. On failure, returns `NULL` and `errno` is set to:
88 * - `EINVAL`: Invalid parameters.
89 * - `EACCES`: The requested mode exceeds the maximum allowed permissions.
90 * - `ENOENT`: The dentry of the path is negative.
91 * - `EISDIR`: The dentry of the path is a directory but mode does not specify `MODE_DIRECTORY`.
92 * - `ENOTDIR`: The dentry of the path is a file but mode specifies `MODE_DIRECTORY`.
93 * - `ENOMEM`: Out of memory.
94 */
95file_t* file_new(const path_t* path, mode_t mode);
96
97/**
98 * @brief Helper function for basic seeking.
99 *
100 * This can be used by filesystems that do not have any special requirements for seeking.
101 *
102 * Used by setting the file ops seek to this function.
103 */
105
106/** @} */
uint64_t file_generic_seek(file_t *file, int64_t offset, seek_origin_t origin)
Helper function for basic seeking.
Definition file.c:92
file_t * file_new(const path_t *path, mode_t mode)
Create a new file structure.
Definition file.c:32
mode_t
Path flags and permissions.
Definition path.h:74
fd_t open(const char *path)
System call for opening files.
Definition open.c:9
uint64_t close(fd_t fd)
System call for closing files.
Definition close.c:9
uint64_t seek(fd_t fd, int64_t offset, seek_origin_t origin)
System call for changing the file offset.
Definition seek.c:9
uint64_t ioctl(fd_t fd, uint64_t request, void *argp, uint64_t size)
System call for extended driver behaviour.
Definition ioctl.c:9
uint8_t seek_origin_t
Type for the seek() origin argument.
Definition io.h:231
uint64_t read(fd_t fd, void *buffer, uint64_t count)
System call for reading from files.
Definition read.c:9
poll_events_t
Poll events type.
Definition io.h:257
uint64_t open2(const char *path, fd_t fd[2])
System call for opening 2 file descriptors from one file.
Definition open2.c:9
uint64_t write(fd_t fd, const void *buffer, uint64_t count)
System call for writing to files.
Definition write.c:9
static uintptr_t address
Mapped virtual address of the HPET registers.
Definition hpet.c:95
static list_t files
Definition file.c:9
static dentry_t * file
Definition log_file.c:22
EFI_PHYSICAL_ADDRESS buffer
Definition mem.c:15
static const path_flag_t flags[]
Definition path.c:42
static atomic_long count
Definition main.c:10
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__INT64_TYPE__ int64_t
Definition stdint.h:16
Directory entry structure.
Definition dentry.h:84
File operations structure.
Definition file.h:54
File structure.
Definition file.h:39
const file_ops_t * ops
Definition file.h:45
ref_t ref
Definition file.h:40
mode_t mode
Definition file.h:42
inode_t * inode
Definition file.h:43
path_t path
Definition file.h:44
uint64_t pos
Definition file.h:41
Inode structure.
Definition inode.h:56
Path structure.
Definition path.h:125
A entry in a page table without a specified address or callback ID.
Structure for polling multiple files.
Definition file.h:71
file_t * file
Definition file.h:72
poll_events_t events
Definition file.h:73
poll_events_t revents
Definition file.h:74
Reference counting structure.
Definition ref.h:30
The primitive that threads block on.
Definition wait.h:182