The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
file.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * open-file descriptions.
5 *
6 * open(), pipe() and the console each make a file_t. a file descriptor is
7 * a pointer to one, so dup, dup2, fork and spawn share the description: the
8 * same file offset, the same status flags. the description is released
9 * (the file closed, the pipe end shut) when its last descriptor closes.
10 *
11 * each kind of file (a filesystem file, a pipe end, the console) supplies a
12 * file_ops_t, so the syscalls never switch on the kind.
13 *
14 * locking: the reference count is guarded by one global spinlock. the
15 * operations do their own locking (pipes their spinlock, ext2 its mutex).
16 */
17
18#include "common/stdint.h"
19#include "common/error.h"
20#include "arch/arch.h"
21#include "kernel/vfs/vfs.h"
22#include "kernel/vfs/stat.h"
23#include "kernel/syscall/pipe.h"
24
25// access modes and status flags (the low bits are ABI, as in libc fcntl.h)
26#define O_RDONLY 0x0000
27#define O_WRONLY 0x0001
28#define O_RDWR 0x0002
29#define O_ACCMODE 0x0003
30#define O_CREAT 0x0040
31#define O_EXCL 0x0080
32#define O_TRUNC 0x0200
33#define O_APPEND 0x0400
34#define O_NONBLOCK 0x0800
35#define O_DIRECTORY 0x10000
36#define O_CLOEXEC 0x80000
37
38typedef struct file file_t;
39
40/**
41 * what a kind of file can do. a NULL entry means the operation is not
42 * supported, and the file_* wrappers return the usual errno for that.
43 * every function returns a count or 0 on success, or a negative errno.
44 */
45typedef struct
46{
47 int32_t (*read)(file_t *file, void *buf, uint32_t count);
48 int32_t (*write)(file_t *file, const void *buf, uint32_t count);
49 int64_t (*seek)(file_t *file, int64_t offset, int whence);
50 int32_t (*ioctl)(file_t *file, uint32_t request, void *arg);
51 int32_t (*mmap)(file_t *file, addr_space_t as, uintptr_t virt, size_t length, int prot,
52 uint64_t offset);
55 int32_t (*stat)(file_t *file, struct kstat *st);
56 uint32_t (*poll)(file_t *file); // POLL* events that would not block now
57 void (*release)(file_t *file); // the last reference is gone
59
60/**
61 * an open-file description.
62 */
63struct file
64{
66 uint32_t refcount; // descriptors (and kernel users) referring to it
67 uint32_t flags; // O_ access mode and status flags
68 fs_dirent_t *lookahead; // a directory entry read but not yet returned
69 char *path; // a filesystem file: its absolute path (for the *at calls)
70 union
71 {
72 vfs_file_t vfs; // a filesystem file or directory (its offset inside)
73 struct
74 {
77 } pipe; // a pipe end
78 void *device_data; // a device stream (device_ops_t open)
79 };
80};
81
82/**
83 * open a file or directory through the VFS.
84 * @param path absolute path.
85 * @param flags O_ access mode and status flags to keep in the description.
86 * @param out receives the new description (one reference).
87 * @return KERR_OK, KERR_NOMEM, or the VFS's error.
88 */
89kerr_t file_open(const char *path, uint32_t flags, file_t **out);
90
91/**
92 * a new end of a named pipe, not yet joined to one (kernel/vfs/fifo.c
93 * joins it). its release leaves the named pipe.
94 * @param flags the open flags.
95 * @return the file, or NULL (no memory).
96 */
98
99/**
100 * make the two ends of a new pipe.
101 * @param read_end receives the read end (one reference).
102 * @param write_end receives the write end (one reference).
103 * @return KERR_OK or KERR_NOMEM.
104 */
105kerr_t file_open_pipe(file_t **read_end, file_t **write_end);
106
107/**
108 * the console (keyboard in, screen out). there is one console description;
109 * every process starts with it on fds 0, 1 and 2.
110 * @return the console description, with a new reference.
111 */
112file_t *file_console(void);
113
114/**
115 * a new description of another kind (a device stream), with one
116 * reference. its ops release it, and free it with kfree.
117 * @return the description, or NULL if the heap is full.
118 */
120
121/**
122 * add a reference to a description.
123 * @param file the description.
124 */
125void file_ref(file_t *file);
126
127/**
128 * drop a reference; the last one releases the description. may sleep
129 * (closing a filesystem file takes its mutex).
130 * @param file the description (NULL is ignored).
131 */
132void file_unref(file_t *file);
133
134/**
135 * true if the description is a filesystem file (not a pipe or the console).
136 */
137bool file_is_vfs(const file_t *file);
138
139/**
140 * true if the description was opened for reading (or writing).
141 */
142bool file_readable(const file_t *file);
143bool file_writable(const file_t *file);
144
145/**
146 * the operations, with an errno for the unsupported ones: read and write
147 * EBADF (wrong access mode, or no op), seek ESPIPE, ioctl ENOTTY, mmap
148 * ENODEV, read_entry ENOTDIR, truncate EINVAL. the buffers are kernel
149 * pointers or user pointers the caller has checked.
150 */
151int32_t file_read(file_t *file, void *buf, uint32_t count);
152int32_t file_write(file_t *file, const void *buf, uint32_t count);
153int64_t file_seek(file_t *file, int64_t offset, int whence);
154int32_t file_ioctl(file_t *file, uint32_t request, void *arg);
155int32_t file_mmap(file_t *file, addr_space_t as, uintptr_t virt, size_t length, int prot,
156 uint64_t offset);
158
159/**
160 * give back the entry file_read_entry returned last, so the next call
161 * returns it again (getdents, when it does not fit the caller's buffer).
162 * @return 0, or -ENOMEM.
163 */
166int32_t file_stat(file_t *file, struct kstat *st);
167
168/**
169 * the readiness of a file (kernel/vfs/poll.h). a file without a poll op
170 * (a regular file) is always ready to read and write.
171 * @return POLL* events.
172 */
174
175/**
176 * read or write at an offset without moving the description's offset
177 * (pread, pwrite). only for files that can seek (ESPIPE otherwise). not
178 * atomic against another process using the same description.
179 */
180int32_t file_pread(file_t *file, void *buf, uint32_t count, int64_t offset);
181int32_t file_pwrite(file_t *file, const void *buf, uint32_t count, int64_t offset);
uintptr_t addr_space_t
an address space handle.
Definition arch.h:105
file_t * file_console(void)
the console (keyboard in, screen out).
Definition console_file.c:527
kerr_t
unified kernel error codes.
Definition error.h:14
int32_t file_pwrite(file_t *file, const void *buf, uint32_t count, int64_t offset)
Definition file.c:203
int32_t file_mmap(file_t *file, addr_space_t as, uintptr_t virt, size_t length, int prot, uint64_t offset)
Definition file.c:106
void file_ref(file_t *file)
add a reference to a description.
Definition file.c:40
bool file_is_vfs(const file_t *file)
true if the description is a filesystem file (not a pipe or the console).
Definition file.c:292
int32_t file_stat(file_t *file, struct kstat *st)
Definition file.c:163
int32_t file_ioctl(file_t *file, uint32_t request, void *arg)
Definition file.c:101
uint32_t file_poll(file_t *file)
the readiness of a file (kernel/vfs/poll.h).
Definition file.c:145
bool file_readable(const file_t *file)
true if the description was opened for reading (or writing).
Definition file.c:68
int64_t file_seek(file_t *file, int64_t offset, int whence)
Definition file.c:96
kerr_t file_open(const char *path, uint32_t flags, file_t **out)
open a file or directory through the VFS.
Definition file.c:297
int32_t file_unread_entry(file_t *file, const fs_dirent_t *entry)
give back the entry file_read_entry returned last, so the next call returns it again (getdents,...
Definition file.c:124
file_t * file_new_fifo_end(uint32_t flags)
a new end of a named pipe, not yet joined to one (kernel/vfs/fifo.c joins it).
Definition file.c:392
int32_t file_read(file_t *file, void *buf, uint32_t count)
the operations, with an errno for the unsupported ones: read and write EBADF (wrong access mode,...
Definition file.c:78
int32_t file_write(file_t *file, const void *buf, uint32_t count)
Definition file.c:87
file_t * file_new(const file_ops_t *ops, uint32_t flags)
a new description of another kind (a device stream), with one reference.
Definition file.c:35
bool file_writable(const file_t *file)
Definition file.c:73
int32_t file_read_entry(file_t *file, fs_dirent_t *entry)
Definition file.c:112
int32_t file_truncate(file_t *file, uint64_t length)
Definition file.c:136
kerr_t file_open_pipe(file_t **read_end, file_t **write_end)
make the two ends of a new pipe.
Definition file.c:397
int32_t file_pread(file_t *file, void *buf, uint32_t count, int64_t offset)
read or write at an offset without moving the description's offset (pread, pwrite).
Definition file.c:198
void file_unref(file_t *file)
drop a reference; the last one releases the description.
Definition file.c:48
struct fs_dirent fs_dirent_t
Definition fs.h:36
uint8_t flags
Definition idt.c:3
struct file file_t
Definition file.h:38
unsigned int uint32_t
Definition fb.h:17
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
int int32_t
Definition stdint.h:14
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:19
__INT64_TYPE__ int64_t
Definition stdint.h:16
struct pipe_buf pipe_buf_t
a pipe.
pipe_dir_t
pipe endpoint direction.
Definition pipe.h:34
what a kind of file can do.
Definition file.h:46
int32_t(*) write(file_t *file, const void *buf, uint32_t count)
Definition file.h:48
int32_t(*) ioctl(file_t *file, uint32_t request, void *arg)
Definition file.h:50
int64_t(*) seek(file_t *file, int64_t offset, int whence)
Definition file.h:49
int32_t(*) mmap(file_t *file, addr_space_t as, uintptr_t virt, size_t length, int prot, uint64_t offset)
Definition file.h:51
uint32_t(*) poll(file_t *file)
Definition file.h:56
int32_t(*) truncate(file_t *file, uint64_t length)
Definition file.h:54
int32_t(*) stat(file_t *file, struct kstat *st)
Definition file.h:55
int32_t(*) read_entry(file_t *file, fs_dirent_t *entry)
Definition file.h:53
int32_t(*) read(file_t *file, void *buf, uint32_t count)
Definition file.h:47
void(*) release(file_t *file)
Definition file.h:57
an open-file description.
Definition file.h:64
uint32_t flags
Definition file.h:67
char * path
Definition file.h:69
fs_dirent_t * lookahead
Definition file.h:68
const file_ops_t * ops
Definition file.h:65
uint32_t refcount
Definition file.h:66
Definition stat.h:23
virtual file system file handle structure representing an open file or directory.
Definition vfs.h:21
struct file::@121010160247101261310353350172266371276115374177::@067332303217203003101323037017207353071354347272 pipe