The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
fs.h
Go to the documentation of this file.
1#pragma once
2
3#include "common/stdint.h"
4#include "arch/arch.h"
5#include "common/error.h"
7#include "fs/ext2/ext2.h"
8
9/**
10 * file system types
11 */
12enum
13{
14 FS_TYPE_UNKNOWN = 0, // unknown or unsupported file type
15 FS_TYPE_FILE = 1, // regular file
16 FS_TYPE_DIR = 2, // directory
17 FS_TYPE_SYMLINK = 3, // symbolic link
18 FS_TYPE_FIFO = 4, // named pipe
19 FS_TYPE_CHAR = 5, // character device node (outside devfs)
20 FS_TYPE_BLOCK = 6, // block device node (outside devfs)
21};
22
23/**
24 * filesystem operations vtable.
25 *
26 * each filesystem type (ext2 today; procfs/tmpfs/FAT32 later) provides one of
27 * these so the generic fs_* layer never names a concrete filesystem. a mount
28 * points at its filesystem's ops; every open file carries the same pointer so
29 * file-level calls dispatch without consulting the mount again.
30 *
31 * mount is the only entry point that is not in the vtable: fs_mount() selects
32 * the filesystem (currently always ext2) and installs its ops.
33 */
34typedef struct fs_mount fs_mount_t;
35typedef struct fs_file fs_file_t;
36typedef struct fs_dirent fs_dirent_t;
37typedef struct fs_stat fs_stat_t;
38
39// which fields of an fs_attr_t to set
40#define FS_ATTR_MODE 0x1 // the permission bits (07777)
41#define FS_ATTR_OWNER 0x2 // uid and gid
42#define FS_ATTR_ATIME 0x4 // the time of the last access
43#define FS_ATTR_MTIME 0x8 // the time of the last change of the data
44
45/**
46 * a change of a file's permissions or owner (setattr).
47 */
48typedef struct
49{
50 uint32_t what; // FS_ATTR_* bits
51 uint32_t mode; // permission bits; the file type is kept
54 uint32_t atime; // seconds since 1970
56} fs_attr_t;
57
58typedef struct
59{
60 kerr_t (*open)(fs_mount_t *mount, const char *path, fs_file_t *file);
61 kerr_t (*create)(fs_mount_t *mount, const char *path);
62 kerr_t (*mkdir)(fs_mount_t *mount, const char *path);
63 kerr_t (*remove)(fs_mount_t *mount, const char *path);
64 kerr_t (*rmdir)(fs_mount_t *mount, const char *path);
65 kerr_t (*rename)(fs_mount_t *mount, const char *old_path, const char *new_path);
66 kerr_t (*stat)(fs_mount_t *mount, const char *path, fs_stat_t *stat_out);
67
68 uint32_t (*read)(fs_file_t *file, uint32_t byte_count, void *data_out);
69 uint32_t (*write)(fs_file_t *file, uint32_t byte_count, const void *data_in);
70 kerr_t (*truncate)(fs_file_t *file, uint64_t length); // cut or grow to length
73 /**
74 * change the permission bits and/or owner of an open file (optional;
75 * NULL means the filesystem keeps no owners: KERR_PERM).
76 */
78 void (*close)(fs_file_t *file);
79
80 /**
81 * device-control operation for a special file (optional; NULL means the
82 * filesystem supports no ioctls, i.e. -ENOTTY). devfs forwards this to the
83 * backing device; disk filesystems like ext2 leave it NULL. returns 0 on
84 * success or a negative errno.
85 */
86 int32_t (*ioctl)(fs_file_t *file, uint32_t request, void *arg);
87
88 /**
89 * map an open special file's memory into a process (optional; NULL means
90 * the file is not mappable, i.e. -ENODEV). devfs forwards this to the
91 * backing device; disk filesystems leave it NULL. returns 0 or -errno.
92 */
94 size_t length, int prot, uint64_t offset);
95
96 /**
97 * reposition an open file's cursor (optional). devfs implements this over
98 * its generic cursor; ext2 leaves it NULL and the syscall layer uses the
99 * ext2 file cursor directly. whence is SEEK_SET(0)/CUR(1)/END(2).
100 * @return the new absolute position, or a negative errno.
101 */
102 int64_t (*seek)(fs_file_t *file, int64_t offset, int whence);
103
104 /**
105 * links (all optional; NULL means the filesystem has none). find_link
106 * reports the first symbolic link along a path (see ext2_find_link);
107 * readlink reads one's target; symlink makes one; link gives a file
108 * one more name.
109 */
110 kerr_t (*find_link)(fs_mount_t *mount, const char *path, bool follow_last, char *target,
111 uint32_t size, uint32_t *end_out);
112 kerr_t (*readlink)(fs_mount_t *mount, const char *path, char *target, uint32_t size);
113 kerr_t (*symlink)(fs_mount_t *mount, const char *path, const char *target);
114 kerr_t (*link)(fs_mount_t *mount, const char *old_path, const char *new_path);
115
116 /**
117 * make a named pipe or a device node (optional: KERR_PERM).
118 * @param mode S_IFIFO, S_IFCHR or S_IFBLK with the permission bits.
119 * @param rdev a device node's number (DEVICE_NUMBER packing).
120 */
121 kerr_t (*mknod)(fs_mount_t *mount, const char *path, uint32_t mode, uint32_t rdev);
122} fs_ops_t;
123
124/**
125 * directory entry structure for reading directory contents.
126 */
128{
129 uint32_t inode; // inode number of the file or directory
130 uint8_t file_type; // type of the file
131 uint64_t size; // size of the file in bytes
132 char name[EXT2_NAME_MAX]; // null-terminated name of the file or directory (max 255 characters)
133};
134
135/**
136 * filesystem mount structure representing a mounted filesystem.
137 */
139{
140 const fs_ops_t *ops; // filesystem operations vtable (set by fs_mount)
141 ext2_volume_t ext2; // embedded ext2_volume_t representing the mounted filesystem (not a pointer)
142 void *fs_data; // generic per-mount state for non-ext2 filesystems (devfs, procfs, ...)
143 const char *type; // the filesystem type for /proc/mounts: "ext2", "devfs", ...
144 const char *source; // the device for /proc/mounts ("hda1", "ram0"), or the type
145 uint8_t is_mounted; // flag indicating whether the filesystem is successfully mounted (1 for mounted, 0 for not mounted)
146};
147
148/**
149 * filesystem metadata structure returned by stat.
150 */
166
167/**
168 * file handle structure representing an open file or directory.
169 */
171{
172 const fs_ops_t *ops; // vtable of the filesystem this file belongs to
173 ext2_file_t ext2_file; // embedded ext2_file_t representing the open file or directory (not a pointer)
174 void *fs_data; // generic per-file state for non-ext2 filesystems (e.g. the target device_t)
175 uint64_t fs_pos; // generic cursor for non-ext2 filesystems (e.g. devfs dir index)
176 uint8_t file_type; // type of the file (FS_TYPE_FILE, FS_TYPE_DIR, or FS_TYPE_UNKNOWN)
177 uint8_t is_open; // flag indicating whether the file is open (1 for open, 0 for closed)
178};
179
180/**
181 * mount a filesystem on a block device.
182 * @param mount filesystem mount object to initialize.
183 * @param device initialized block device backend.
184 * @return true on success. false on failure.
185 */
187
188/**
189 * open a file or directory by absolute path.
190 * @param mount initialized filesystem mount.
191 * @param path absolute path to the file or directory.
192 * @param file output file handle.
193 * @return KERR_OK on success, or an error code on failure.
194 */
195kerr_t fs_open(fs_mount_t *mount, const char *path, fs_file_t *file);
196
197/**
198 * create a regular file at an absolute path.
199 * @param mount initialized filesystem mount.
200 * @param path absolute path to the new file.
201 * @return KERR_OK on success, or an error code on failure.
202 */
203kerr_t fs_create(fs_mount_t *mount, const char *path);
204
205/**
206 * create a directory at an absolute path.
207 * @param mount initialized filesystem mount.
208 * @param path absolute path to the new directory.
209 * @return KERR_OK on success, or an error code on failure.
210 */
211kerr_t fs_mkdir(fs_mount_t *mount, const char *path);
212
213/**
214 * remove a file at an absolute path.
215 * @param mount initialized filesystem mount.
216 * @param path absolute path to the file to remove.
217 * @return KERR_OK on success, or an error code on failure.
218 */
219kerr_t fs_remove(fs_mount_t *mount, const char *path);
220
221/**
222 * remove a directory at an absolute path.
223 * @param mount initialized filesystem mount.
224 * @param path absolute path to the directory to remove.
225 * @return KERR_OK on success, or an error code on failure.
226 */
227kerr_t fs_rmdir(fs_mount_t *mount, const char *path);
228
229/**
230 * rename a file or directory.
231 * @param mount initialized filesystem mount.
232 * @param old_path absolute path to the existing file or directory.
233 * @param new_path absolute path to the new name for the file or directory.
234 * @return KERR_OK on success, or an error code on failure.
235 */
236kerr_t fs_rename(fs_mount_t *mount, const char *old_path, const char *new_path);
237
238/**
239 * stat a file or directory by absolute path.
240 * @param mount initialized filesystem mount.
241 * @param path absolute path to the file or directory.
242 * @param stat_out output stat structure.
243 * @return KERR_OK on success, or an error code on failure.
244 */
245kerr_t fs_stat(fs_mount_t *mount, const char *path, fs_stat_t *stat_out);
246
247/**
248 * the link operations (see fs_ops_t). with no such operation: find_link
249 * finds none, readlink is KERR_INVAL (not a link), symlink and link are
250 * KERR_PERM.
251 */
252kerr_t fs_find_link(fs_mount_t *mount, const char *path, bool follow_last, char *target,
253 uint32_t size, uint32_t *end_out);
254kerr_t fs_readlink(fs_mount_t *mount, const char *path, char *target, uint32_t size);
255kerr_t fs_symlink(fs_mount_t *mount, const char *path, const char *target);
256kerr_t fs_link(fs_mount_t *mount, const char *old_path, const char *new_path);
257kerr_t fs_mknod(fs_mount_t *mount, const char *path, uint32_t mode, uint32_t rdev);
258
259/**
260 * read bytes from an open file into a buffer.
261 * @param file open file handle.
262 * @param byteCount number of bytes to read.
263 * @param dataOut destination buffer.
264 * @return number of bytes actually read.
265 */
266uint32_t fs_read(fs_file_t *file, uint32_t byteCount, void *dataOut);
267
268/**
269 * write bytes to an open file.
270 * @param file open file handle.
271 * @param byteCount number of bytes to write.
272 * @param dataIn source buffer.
273 * @return number of bytes actually written, or 0 on failure.
274 */
275uint32_t fs_write(fs_file_t *file, uint32_t byteCount, const void *dataIn);
276
277/**
278 * cut or grow an open file to length bytes (growth reads as zeros).
279 * @return KERR_OK on success, or an error code on failure.
280 */
282
283/**
284 * stat an open file or directory.
285 * @param file open file handle.
286 * @param stat_out output stat structure.
287 * @return KERR_OK, or KERR_INVAL if the filesystem cannot stat open files.
288 */
290
291/**
292 * change an open file's permission bits and/or owner.
293 * @param file open file handle.
294 * @param attr what to change.
295 * @return KERR_OK, or KERR_PERM if the filesystem keeps no owners.
296 */
298
299/**
300 * read a directory entry from an open directory file.
301 * @param file open directory file handle.
302 * @param entryOut output directory entry.
303 * @return KERR_OK on success, or an error code on failure.
304 */
306
307/**
308 * close an open file or directory.
309 * @param file open file handle to close.
310 * @return void
311 */
312void fs_close(fs_file_t *file);
313
314/**
315 * get the type of an open file or directory.
316 * @param file open file handle.
317 * @return file type (FS_TYPE_FILE, FS_TYPE_DIR, or FS_TYPE_UNKNOWN).
318 */
320
321/**
322 * issue a device-control (ioctl) request on an open special file.
323 * dispatches through the filesystem's ioctl op; filesystems without one
324 * (ext2, etc.) return -ENOTTY.
325 * @param file open file handle.
326 * @param request driver-defined request code.
327 * @param arg request-specific argument (typically a bounds-checked pointer).
328 * @return 0 on success, or a negative errno on failure.
329 */
330int32_t fs_ioctl(fs_file_t *file, uint32_t request, void *arg);
331
332/**
333 * map an open special file's memory into a process address space.
334 * dispatches through the filesystem's mmap op; filesystems without one return
335 * -ENODEV. the frames are mapped eagerly at @p virt in @p page_directory.
336 * @param file open file handle.
337 * @param page_directory physical address of the target process page directory.
338 * @param virt page-aligned destination virtual address.
339 * @param length bytes to map (page-multiple).
340 * @param prot PROT_* bits.
341 * @param offset page-aligned byte offset into the mapped object.
342 * @return 0 on success, or a negative errno on failure.
343 */
345 size_t length, int prot, uint64_t offset);
346
347/**
348 * reposition an open file's cursor via the filesystem's seek op.
349 * @return the new absolute position, or -ENOSYS if the filesystem has no seek
350 * op (the caller then handles seeking itself, e.g. via the ext2
351 * cursor), or another negative errno on failure.
352 */
353int64_t fs_seek(fs_file_t *file, int64_t offset, int whence);
354
355/**
356 * the position a seek gives, as lseek computes it.
357 * @param current the current position.
358 * @param size the size of the file (for SEEK_END).
359 * @param offset the offset relative to whence.
360 * @param whence 0 (SEEK_SET), 1 (SEEK_CUR) or 2 (SEEK_END).
361 * @return the new position, or -EINVAL for a bad whence or a negative
362 * result, or -EOVERFLOW if the result does not fit in 63 bits.
363 */
364int64_t fs_seek_position(uint64_t current, uint64_t size, int64_t offset, int whence);
uintptr_t addr_space_t
an address space handle.
Definition arch.h:105
struct block_device block_device_t
Definition block_device.h:5
kerr_t
unified kernel error codes.
Definition error.h:14
#define EXT2_NAME_MAX
Definition ext2.h:43
struct fs_mount fs_mount_t
filesystem operations vtable.
Definition ext2_ops.h:7
kerr_t fs_rmdir(fs_mount_t *mount, const char *path)
remove a directory at an absolute path.
Definition fs.c:85
kerr_t fs_mkdir(fs_mount_t *mount, const char *path)
create a directory at an absolute path.
Definition fs.c:65
kerr_t fs_symlink(fs_mount_t *mount, const char *path, const char *target)
Definition fs.c:129
int32_t fs_ioctl(fs_file_t *file, uint32_t request, void *arg)
issue a device-control (ioctl) request on an open special file.
Definition fs.c:240
kerr_t fs_remove(fs_mount_t *mount, const char *path)
remove a file at an absolute path.
Definition fs.c:75
kerr_t fs_fstat(fs_file_t *file, fs_stat_t *stat_out)
stat an open file or directory.
Definition fs.c:320
int64_t fs_seek_position(uint64_t current, uint64_t size, int64_t offset, int whence)
the position a seek gives, as lseek computes it.
Definition fs.c:290
kerr_t fs_open(fs_mount_t *mount, const char *path, fs_file_t *file)
open a file or directory by absolute path.
Definition fs.c:33
kerr_t fs_truncate(fs_file_t *file, uint64_t length)
cut or grow an open file to length bytes (growth reads as zeros).
Definition fs.c:198
uint32_t fs_write(fs_file_t *file, uint32_t byteCount, const void *dataIn)
write bytes to an open file.
Definition fs.c:188
uint8_t fs_file_type(const fs_file_t *file)
get the type of an open file or directory.
Definition fs.c:230
kerr_t fs_find_link(fs_mount_t *mount, const char *path, bool follow_last, char *target, uint32_t size, uint32_t *end_out)
the link operations (see fs_ops_t).
Definition fs.c:105
int32_t fs_mmap(fs_file_t *file, addr_space_t as, uintptr_t virt, size_t length, int prot, uint64_t offset)
map an open special file's memory into a process address space.
Definition fs.c:257
uint32_t fs_read(fs_file_t *file, uint32_t byteCount, void *dataOut)
read bytes from an open file into a buffer.
Definition fs.c:178
kerr_t fs_rename(fs_mount_t *mount, const char *old_path, const char *new_path)
rename a file or directory.
Definition fs.c:95
kerr_t fs_setattr(fs_file_t *file, const fs_attr_t *attr)
change an open file's permission bits and/or owner.
Definition fs.c:329
void fs_close(fs_file_t *file)
close an open file or directory.
Definition fs.c:218
kerr_t fs_create(fs_mount_t *mount, const char *path)
create a regular file at an absolute path.
Definition fs.c:55
int64_t fs_seek(fs_file_t *file, int64_t offset, int whence)
reposition an open file's cursor via the filesystem's seek op.
Definition fs.c:274
kerr_t fs_link(fs_mount_t *mount, const char *old_path, const char *new_path)
Definition fs.c:142
kerr_t fs_readlink(fs_mount_t *mount, const char *path, char *target, uint32_t size)
Definition fs.c:120
kerr_t fs_read_entry(fs_file_t *file, fs_dirent_t *entryOut)
read a directory entry from an open directory file.
Definition fs.c:208
kerr_t fs_mknod(fs_mount_t *mount, const char *path, uint32_t mode, uint32_t rdev)
Definition fs.c:155
@ FS_TYPE_FILE
Definition fs.h:15
@ FS_TYPE_FIFO
Definition fs.h:18
@ FS_TYPE_SYMLINK
Definition fs.h:17
@ FS_TYPE_DIR
Definition fs.h:16
@ FS_TYPE_BLOCK
Definition fs.h:20
@ FS_TYPE_UNKNOWN
Definition fs.h:14
@ FS_TYPE_CHAR
Definition fs.h:19
struct fs_stat fs_stat_t
Definition fs.h:37
struct fs_dirent fs_dirent_t
Definition fs.h:36
struct fs_file fs_file_t
Definition fs.h:35
unsigned int uint32_t
Definition fb.h:17
unsigned char uint8_t
Definition fb.h:18
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
unsigned short uint16_t
Definition stdint.h:13
int int32_t
Definition stdint.h:14
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:19
__INT64_TYPE__ int64_t
Definition stdint.h:16
a registered device.
Definition device.h:114
Definition ext2.h:150
Definition ext2.h:123
an open-file description.
Definition file.h:64
a change of a file's permissions or owner (setattr).
Definition fs.h:49
uint32_t what
Definition fs.h:50
uint32_t mtime
Definition fs.h:55
uint32_t atime
Definition fs.h:54
uint32_t uid
Definition fs.h:52
uint32_t mode
Definition fs.h:51
uint32_t gid
Definition fs.h:53
directory entry structure for reading directory contents.
Definition fs.h:128
uint8_t file_type
Definition fs.h:130
uint32_t inode
Definition fs.h:129
char name[EXT2_NAME_MAX]
Definition fs.h:132
uint64_t size
Definition fs.h:131
file handle structure representing an open file or directory.
Definition fs.h:171
uint8_t file_type
Definition fs.h:176
void * fs_data
Definition fs.h:174
uint8_t is_open
Definition fs.h:177
uint64_t fs_pos
Definition fs.h:175
const fs_ops_t * ops
Definition fs.h:172
ext2_file_t ext2_file
Definition fs.h:173
filesystem mount structure representing a mounted filesystem.
Definition fs.h:139
const char * type
Definition fs.h:143
ext2_volume_t ext2
Definition fs.h:141
uint8_t is_mounted
Definition fs.h:145
const fs_ops_t * ops
Definition fs.h:140
const char * source
Definition fs.h:144
void * fs_data
Definition fs.h:142
Definition fs.h:59
kerr_t(*) mknod(fs_mount_t *mount, const char *path, uint32_t mode, uint32_t rdev)
make a named pipe or a device node (optional: KERR_PERM).
Definition fs.h:121
kerr_t(*) find_link(fs_mount_t *mount, const char *path, bool follow_last, char *target, uint32_t size, uint32_t *end_out)
links (all optional; NULL means the filesystem has none).
Definition fs.h:110
int32_t(*) ioctl(fs_file_t *file, uint32_t request, void *arg)
device-control operation for a special file (optional; NULL means the filesystem supports no ioctls,...
Definition fs.h:86
kerr_t(*) read_entry(fs_file_t *file, fs_dirent_t *entry_out)
Definition fs.h:71
uint32_t(*) read(fs_file_t *file, uint32_t byte_count, void *data_out)
Definition fs.h:68
kerr_t(*) setattr(fs_file_t *file, const fs_attr_t *attr)
change the permission bits and/or owner of an open file (optional; NULL means the filesystem keeps no...
Definition fs.h:77
int64_t(*) seek(fs_file_t *file, int64_t offset, int whence)
reposition an open file's cursor (optional).
Definition fs.h:102
int32_t(*) mmap(fs_file_t *file, addr_space_t as, uintptr_t virt, size_t length, int prot, uint64_t offset)
map an open special file's memory into a process (optional; NULL means the file is not mappable,...
Definition fs.h:93
kerr_t(*) rmdir(fs_mount_t *mount, const char *path)
Definition fs.h:64
kerr_t(*) fstat(fs_file_t *file, fs_stat_t *stat_out)
Definition fs.h:72
kerr_t(*) stat(fs_mount_t *mount, const char *path, fs_stat_t *stat_out)
Definition fs.h:66
uint32_t(*) write(fs_file_t *file, uint32_t byte_count, const void *data_in)
Definition fs.h:69
kerr_t(*) mkdir(fs_mount_t *mount, const char *path)
Definition fs.h:62
kerr_t(*) rename(fs_mount_t *mount, const char *old_path, const char *new_path)
Definition fs.h:65
kerr_t(*) open(fs_mount_t *mount, const char *path, fs_file_t *file)
Definition fs.h:60
kerr_t(*) create(fs_mount_t *mount, const char *path)
Definition fs.h:61
kerr_t(*) symlink(fs_mount_t *mount, const char *path, const char *target)
Definition fs.h:113
kerr_t(*) link(fs_mount_t *mount, const char *old_path, const char *new_path)
Definition fs.h:114
kerr_t(*) remove(fs_mount_t *mount, const char *path)
Definition fs.h:63
void(*) close(fs_file_t *file)
Definition fs.h:78
kerr_t(*) readlink(fs_mount_t *mount, const char *path, char *target, uint32_t size)
Definition fs.h:112
kerr_t(*) truncate(fs_file_t *file, uint64_t length)
Definition fs.h:70
filesystem metadata structure returned by stat.
Definition fs.h:152
uint16_t mode
Definition fs.h:155
uint32_t inode
Definition fs.h:153
uint32_t rdev
Definition fs.h:164
uint64_t size
Definition fs.h:157
uint32_t ctime
Definition fs.h:161
uint32_t mtime
Definition fs.h:160
uint32_t atime
Definition fs.h:159
uint32_t blocks
Definition fs.h:158
uint16_t uid
Definition fs.h:162
uint16_t links_count
Definition fs.h:156
uint8_t file_type
Definition fs.h:154
uint16_t gid
Definition fs.h:163