Skip to content

Commit a57b955

Browse files
Cleaned up major amounts of code trying to fix why vfs doesn't work in some places.
1 parent 4267916 commit a57b955

14 files changed

Lines changed: 569 additions & 226 deletions

File tree

disk.img

0 Bytes
Binary file not shown.

source/includes/sys/dirent.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @file dirent.h
3+
* @author Pradosh (pradoshgame@gmail.com)
4+
* @brief Directory entry structure (64-bit variant).
5+
* @version 0.1
6+
* @date 2026-04-03
7+
*
8+
* @copyright Copyright (c) Pradosh 2026
9+
*
10+
*/
11+
#ifndef SYS_DIRENT_H
12+
#define SYS_DIRENT_H
13+
14+
#include <stdint.h>
15+
16+
/**
17+
* @brief Directory entry structure (64-bit variant).
18+
*
19+
* Represents a single entry returned when reading directories.
20+
* Contains inode number, offset, record length, type, and name.
21+
*/
22+
typedef struct {
23+
uint64_t d_ino;
24+
int64_t d_off;
25+
uint16_t d_reclen;
26+
uint8_t d_type;
27+
char d_name[];
28+
} linux_dirent64_t;
29+
30+
#endif

source/includes/sys/helper.h

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/**
2+
* @file helper.h
3+
* @author Pradosh (pradoshgame@gmail.com)
4+
* @brief Contains helper code for syscalls.
5+
* @version 0.1
6+
* @date 2026-04-03
7+
*
8+
* @copyright Copyright (c) Pradosh 2026
9+
*
10+
*/
11+
#ifndef SYS_HELPER_H
12+
#define SYS_HELPER_H
13+
14+
#include <heap.h>
15+
#include <syscalls.h>
16+
#include <memory.h>
17+
18+
/**
19+
* @brief Frees a dynamically allocated array of strings.
20+
*
21+
* Releases memory for each string in the array and then frees
22+
* the array itself. Assumes each element and the array were
23+
* allocated using the kernel allocator (kfree-compatible).
24+
*
25+
* @param arr Pointer to the array of string pointers.
26+
* @param count Number of elements in the array.
27+
*
28+
* @note If @p arr is NULL, the function does nothing.
29+
* @warning Each element in @p arr must be individually allocated.
30+
*/
31+
static void free_copied_string_array(char** arr, int count) {
32+
if (!arr)
33+
return;
34+
35+
for (int i = 0; i < count; ++i) {
36+
if (arr[i]) // safety check
37+
kfree(arr[i]);
38+
}
39+
40+
kfree(arr);
41+
}
42+
43+
/**
44+
* @brief Copies a NULL-terminated array of user strings into kernel space.
45+
*
46+
* Allocates a new array of strings and copies each string from the user-provided
47+
* array into kernel-managed memory. The resulting array is always NULL-terminated.
48+
*
49+
* A maximum of 32 strings are copied to prevent unbounded memory usage.
50+
*
51+
* @param user_arr Pointer to a NULL-terminated array of user-space strings.
52+
* @param out_arr Output pointer that receives the newly allocated array.
53+
*
54+
* @return Number of strings successfully copied on success.
55+
* @retval 0 If @p user_arr is NULL (no strings to copy).
56+
* @retval -LINUX_ENOMEM If memory allocation fails at any point.
57+
*
58+
* @note The caller is responsible for freeing the returned array using
59+
* free_copied_string_array().
60+
*
61+
* @warning Each string in @p user_arr must be valid and accessible.
62+
* No validation of user-space pointers is performed.
63+
*
64+
* @warning The function copies at most 32 strings; additional entries
65+
* in @p user_arr are ignored.
66+
*/
67+
static int copy_user_string_array(char* const* user_arr, char*** out_arr) {
68+
if (!user_arr) {
69+
*out_arr = NULL;
70+
return 0;
71+
}
72+
73+
char** copied = kmalloc(sizeof(char*) * 33);
74+
if (!copied)
75+
return -LINUX_ENOMEM;
76+
77+
int count = 0;
78+
for (; count < 32; ++count) {
79+
const char* src = user_arr[count];
80+
if (!src) {
81+
copied[count] = NULL;
82+
*out_arr = copied;
83+
return count;
84+
}
85+
86+
size_t len = strlen(src);
87+
copied[count] = kmalloc(len + 1);
88+
if (!copied[count]) {
89+
free_copied_string_array(copied, count);
90+
return -LINUX_ENOMEM;
91+
}
92+
93+
memcpy(copied[count], src, len + 1);
94+
}
95+
96+
copied[32] = NULL;
97+
*out_arr = copied;
98+
return 32;
99+
}
100+
101+
/**
102+
* @brief Computes a hash value for a filesystem path.
103+
*
104+
* Uses a 32-bit FNV-1a hash algorithm to generate a deterministic
105+
* hash from a null-terminated path string. This can be used for
106+
* fast lookup or mapping paths to inode identifiers.
107+
*
108+
* @param path Null-terminated string representing the file path.
109+
*
110+
* @return 32-bit hash value for the given path.
111+
*
112+
* @note Returns a non-zero hash value. If the computed hash is zero,
113+
* it is replaced with 1 to avoid special-case handling.
114+
*
115+
* @warning The input @p path must be a valid null-terminated string.
116+
* Passing NULL results in undefined behavior.
117+
*/
118+
static uint32_t path_inode_hash(const char* path) {
119+
if (!path)
120+
return 1;
121+
122+
uint32_t hash = 2166136261u;
123+
for (const unsigned char* p = (const unsigned char*)path; p && *p; ++p) {
124+
hash ^= *p;
125+
hash *= 16777619u;
126+
}
127+
return hash ? hash : 1;
128+
}
129+
130+
#endif

source/includes/sys/resource.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @file resource.h
3+
* @author Pradosh (pradoshgame@gmail.com)
4+
* @brief
5+
* @version 0.1
6+
* @date 2026-04-03
7+
*
8+
* @copyright Copyright (c) Pradosh 2026
9+
*
10+
*/
11+
#ifndef SYS_RESOURCE_H
12+
#define SYS_RESOURCE_H
13+
14+
#include <stdint.h>
15+
16+
typedef struct {
17+
uint64_t rlim_cur;
18+
uint64_t rlim_max;
19+
} linux_rlimit64_t;
20+
21+
#endif

source/includes/sys/stat.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* @file stat.h
3+
* @author Pradosh (pradoshgame@gmail.com)
4+
* @brief
5+
* @version 0.1
6+
* @date 2026-04-03
7+
*
8+
* @copyright Copyright (c) Pradosh 2026
9+
*
10+
*/
11+
#ifndef SYS_STAT_H
12+
#define SYS_STAT_H
13+
14+
#include <stdint.h>
15+
#include <sys/time.h>
16+
17+
/**
18+
* @brief File status information (legacy stat structure).
19+
*
20+
* Contains metadata about a file such as size, permissions,
21+
* ownership, timestamps, and device identifiers.
22+
* Returned by stat-family syscalls.
23+
*/
24+
typedef struct {
25+
uint64_t st_dev;
26+
uint64_t st_ino;
27+
uint64_t st_nlink;
28+
uint32_t st_mode;
29+
uint32_t st_uid;
30+
uint32_t st_gid;
31+
uint32_t __pad0;
32+
uint64_t st_rdev;
33+
int64_t st_size;
34+
int64_t st_blksize;
35+
int64_t st_blocks;
36+
linux_timespec_t st_atim;
37+
linux_timespec_t st_mtim;
38+
linux_timespec_t st_ctim;
39+
int64_t __unused[3];
40+
} linux_stat_t;
41+
42+
/**
43+
* @brief Extended file status information.
44+
*
45+
* Modern replacement for stat, providing more detailed and flexible
46+
* file metadata, including birth time and attribute masks.
47+
* Used with the statx syscall.
48+
*/
49+
typedef struct {
50+
uint32_t stx_mask;
51+
uint32_t stx_blksize;
52+
uint64_t stx_attributes;
53+
uint32_t stx_nlink;
54+
uint32_t stx_uid;
55+
uint32_t stx_gid;
56+
uint16_t stx_mode;
57+
uint16_t __spare0[1];
58+
uint64_t stx_ino;
59+
uint64_t stx_size;
60+
uint64_t stx_blocks;
61+
uint64_t stx_attributes_mask;
62+
linux_timespec_t stx_atime;
63+
linux_timespec_t stx_btime;
64+
linux_timespec_t stx_ctime;
65+
linux_timespec_t stx_mtime;
66+
uint32_t stx_rdev_major;
67+
uint32_t stx_rdev_minor;
68+
uint32_t stx_dev_major;
69+
uint32_t stx_dev_minor;
70+
uint64_t __spare2[14];
71+
} linux_statx_t;
72+
73+
#endif

source/includes/sys/termios.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @file termios.h
3+
* @author Pradosh (pradoshgame@gmail.com)
4+
* @brief
5+
* @version 0.1
6+
* @date 2026-04-03
7+
*
8+
* @copyright Copyright (c) Pradosh 2026
9+
*
10+
*/
11+
#ifndef SYS_TERMIOS_H
12+
#define SYS_TERMIOS_H
13+
14+
#include <stdint.h>
15+
16+
/**
17+
* @brief Terminal window size information.
18+
*
19+
* Describes the dimensions of a terminal in characters and pixels.
20+
* Commonly used with ioctl calls
21+
*/
22+
typedef struct {
23+
uint16_t ws_row;
24+
uint16_t ws_col;
25+
uint16_t ws_xpixel;
26+
uint16_t ws_ypixel;
27+
} linux_winsize_t;
28+
29+
/**
30+
* @brief Terminal I/O configuration structure.
31+
*
32+
* Controls behavior of terminal devices such as input processing,
33+
* output processing, line discipline, and local modes.
34+
* Used with terminal-related syscalls (e.g., tcgetattr, tcsetattr).
35+
*/
36+
typedef struct {
37+
uint32_t c_iflag;
38+
uint32_t c_oflag;
39+
uint32_t c_cflag;
40+
uint32_t c_lflag;
41+
uint8_t c_line;
42+
uint8_t c_cc[19];
43+
} linux_termios_t;
44+
45+
#endif

source/includes/sys/time.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @file time.h
3+
* @author Pradosh (pradoshgame@gmail.com)
4+
* @brief
5+
* @version 0.1
6+
* @date 2026-04-03
7+
*
8+
* @copyright Copyright (c) Pradosh 2026
9+
*
10+
*/
11+
#ifndef SYS_TIME_H
12+
#define SYS_TIME_H
13+
14+
/**
15+
* @brief Represents time with nanosecond precision.
16+
*
17+
* Used by various syscalls to express absolute or relative time.
18+
* Stores seconds and nanoseconds since the Unix epoch or for intervals.
19+
*/
20+
typedef struct {
21+
long tv_sec;
22+
long tv_nsec;
23+
} linux_timespec_t;
24+
25+
#endif

source/includes/sys/uio.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @file uio.h
3+
* @author Pradosh (pradoshgame@gmail.com)
4+
* @brief
5+
* @version 0.1
6+
* @date 2026-04-03
7+
*
8+
* @copyright Copyright (c) Pradosh 2026
9+
*
10+
*/
11+
#ifndef SYS_UIO_H
12+
#define SYS_UIO_H
13+
14+
#include <stdint.h>
15+
16+
/**
17+
* @brief Scatter/gather I/O vector.
18+
*
19+
* Describes a memory buffer for vectored I/O operations.
20+
* Used by readv/writev syscalls to perform I/O on multiple buffers.
21+
*/
22+
typedef struct {
23+
uint64_t iov_base;
24+
uint64_t iov_len;
25+
} linux_iovec_t;
26+
27+
#endif

0 commit comments

Comments
 (0)