PatchworkOS  19e446b
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/fs.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 vnode vnode_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 vnode.
36 *
37 */
38typedef struct file
39{
41 size_t pos;
46 void* data;
47} file_t;
48
49/**
50 * @brief File operations structure.
51 * @struct file_ops_t
52 */
53typedef struct file_ops
54{
55 uint64_t (*open)(file_t* file);
57 void (*close)(file_t* file);
58 size_t (*read)(file_t* file, void* buffer, size_t count, size_t* offset);
59 size_t (*write)(file_t* file, const void* buffer, size_t count, size_t* offset);
61 uint64_t (*ioctl)(file_t* file, uint64_t request, void* argp, size_t size);
62 wait_queue_t* (*poll)(file_t* file, poll_events_t* revents);
63 void* (*mmap)(file_t* file, void* address, size_t length, size_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 * - `ENOMEM`: Out of memory.
92 */
93file_t* file_new(const path_t* path, mode_t mode);
94
95/**
96 * @brief Helper function for basic seeking.
97 *
98 * This can be used by filesystems that do not have any special requirements for seeking.
99 *
100 * Used by setting the file ops seek to this function.
101 */
103
104/** @} */
EFI_PHYSICAL_ADDRESS buffer
Definition main.c:237
static uintptr_t address
Mapped virtual address of the HPET registers.
Definition hpet.c:96
size_t file_generic_seek(file_t *file, ssize_t offset, seek_origin_t origin)
Helper function for basic seeking.
Definition file.c:76
file_t * file_new(const path_t *path, mode_t mode)
Create a new file structure.
Definition file.c:40
mode_t
Path flags and permissions.
Definition path.h:79
fd_t open(const char *path)
System call for opening files.
Definition open.c:8
uint64_t close(fd_t fd)
System call for closing files.
Definition close.c:8
size_t write(fd_t fd, const void *buffer, size_t count)
System call for writing to files.
Definition write.c:8
size_t seek(fd_t fd, ssize_t offset, seek_origin_t origin)
System call for changing the file offset.
Definition seek.c:8
size_t read(fd_t fd, void *buffer, size_t count)
System call for reading from files.
Definition read.c:8
uint8_t seek_origin_t
Type for the seek() origin argument.
Definition fs.h:260
poll_events_t
Poll events type.
Definition fs.h:286
uint64_t open2(const char *path, fd_t fd[2])
System call for opening 2 file descriptors from one file.
Definition open2.c:8
uint64_t ioctl(fd_t fd, uint64_t request, void *argp, size_t size)
System call for extended driver behaviour.
Definition ioctl.c:8
__INT64_TYPE__ ssize_t
Signed size type.
Definition ssize_t.h:11
static uint64_t offset
Definition screen.c:19
static list_t files
Definition file.c:9
static const path_flag_t flags[]
Definition path.c:47
static atomic_long count
Definition main.c:11
__SIZE_TYPE__ size_t
Definition size_t.h:4
__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
void * data
Definition file.h:46
const file_ops_t * ops
Definition file.h:45
ref_t ref
Definition file.h:40
mode_t mode
Definition file.h:42
vnode_t * vnode
Definition file.h:43
size_t pos
Definition file.h:41
path_t path
Definition file.h:44
Path structure.
Definition path.h:127
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:52
vnode structure.
Definition vnode.h:48
The primitive that threads block on.
Definition wait.h:185