Fennix  1.0.0
Full Documentation
Loading...
Searching...
No Matches
fs.h
Go to the documentation of this file.
1/*
2 This file is part of Fennix Kernel.
4 Fennix Kernel is free software: you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation, either version 3 of
7 the License, or (at your option) any later version.
9 Fennix Kernel is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
16*/
17
18#ifndef __FENNIX_API_FILESYSTEM_H__
19#define __FENNIX_API_FILESYSTEM_H__
20
21#include <types.h>
22
23#define SEEK_SET 0
24#define SEEK_CUR 1
25#define SEEK_END 2
26
32#define S_IFMT32 037777600000
33
48#define S_IFMT 0170000
49
50/* Whiteout */
51#define S_IFWHT 0160000
52/* Socket */
53#define S_IFSOCK 0140000
54/* Symbolic link */
55#define S_IFLNK 0120000
56/* Regular file */
57#define S_IFREG 0100000
58/* Block device */
59#define S_IFBLK 0060000
60/* Directory */
61#define S_IFDIR 0040000
62/* Character device */
63#define S_IFCHR 0020000
64/* FIFO */
65#define S_IFIFO 0010000
66
67#define S_ISUID 04000
68#define S_ISGID 02000
69#define S_ISVTX 01000
70
72#define S_IRWXU 0700
74#define S_IRUSR 0400
76#define S_IWUSR 0200
78#define S_IXUSR 0100
79
81#define S_IRWXG 0070
83#define S_IRGRP 0040
85#define S_IWGRP 0020
87#define S_IXGRP 0010
88
90#define S_IRWXO 0007
92#define S_IROTH 0004
94#define S_IWOTH 0002
96#define S_IXOTH 0001
97
98#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
99#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
100#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
101#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
102#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
103#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
104#define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
105
106#define DT_UNKNOWN 0x0
107#define DT_FIFO 0x1
108#define DT_CHR 0x2
109#define DT_DIR 0x4
110#define DT_BLK 0x6
111#define DT_REG 0x8
112#define DT_LNK 0xA
113#define DT_SOCK 0xC
114#define DT_WHT 0xE
115
116#define IFTODT(x) ((x) >> 12 & 0xF)
117#define DTTOIF(x) ((x) << 12)
118
119#define SYMLOOP_MAX 40
120
121#ifndef __cplusplus
122#define static_assert _Static_assert
123#endif
124
125#ifdef __LP64__
126static_assert(sizeof(dev_t) == 8, "dev_t must be 64 bits");
127static_assert(sizeof(ino_t) == 8, "ino_t must be 64 bits");
128static_assert(sizeof(mode_t) == 4, "mode_t must be 32 bits");
129static_assert(sizeof(nlink_t) == 4, "nlink_t must be 32 bits");
130static_assert(sizeof(uid_t) == 4, "uid_t must be 32 bits");
131static_assert(sizeof(gid_t) == 4, "gid_t must be 32 bits");
132static_assert(sizeof(off_t) == 8, "off_t must be 64 bits");
133static_assert(sizeof(time_t) == 8, "time_t must be 64 bits");
134static_assert(sizeof(blksize_t) == 8, "blksize_t must be 64 bits");
135static_assert(sizeof(blkcnt_t) == 8, "blkcnt_t must be 64 bits");
136#else
137static_assert(sizeof(dev_t) == 4, "dev_t must be 32 bits");
138static_assert(sizeof(ino_t) == 4, "ino_t must be 32 bits");
139static_assert(sizeof(mode_t) == 4, "mode_t must be 32 bits");
140static_assert(sizeof(nlink_t) == 4, "nlink_t must be 32 bits");
141static_assert(sizeof(uid_t) == 4, "uid_t must be 32 bits");
142static_assert(sizeof(gid_t) == 4, "gid_t must be 32 bits");
143static_assert(sizeof(off_t) == 4, "off_t must be 32 bits");
144static_assert(sizeof(time_t) == 4, "time_t must be 32 bits");
145static_assert(sizeof(blksize_t) == 4, "blksize_t must be 32 bits");
146static_assert(sizeof(blkcnt_t) == 4, "blkcnt_t must be 32 bits");
147#endif
148
149#undef static_assert
150
151struct kstat
152{
154 dev_t Device;
155
157 ino_t Index;
158
160 mode_t Mode;
161
163 nlink_t HardLinks;
164
166 uid_t UserID;
167
169 gid_t GroupID;
170
173
175 off_t Size;
176
179
182
185
187 blksize_t BlockSize;
188
190 blkcnt_t Blocks;
191
193 mode_t Attribute;
194
195#ifdef __cplusplus
196
197 dev_t MakeDevice(int Major, int Minor)
198 {
199 return ((Major & 0xFFF) << 8) |
200 (Minor & 0xFF);
201 }
202
203 int GetMajor()
204 {
205 return ((unsigned int)(Device) >> 8) & 0xFFF;
206 }
207
208 int GetMinor()
209 {
210 return Device & 0xFF;
211 }
212
213 void SetFileType(mode_t Type)
214 {
215 Mode = (Mode & ~S_IFMT) |
216 (Type & S_IFMT);
217 }
218
219 mode_t GetFileType() { return Mode & S_IFMT; }
220 void ClearFileType() { Mode = Mode & ~S_IFMT; }
221 bool IsType(mode_t Type) { return (Mode & S_IFMT) == Type; }
222
223 void SetPermissions(mode_t Permissions)
224 {
225 Mode = (Mode & S_IFMT) |
226 (Permissions & ~S_IFMT);
227 }
228
229 mode_t GetPermissions() { return Mode & ~S_IFMT; }
230 void ClearPermissions() { Mode = Mode & S_IFMT; }
231
232#endif // __cplusplus
233};
234
236{
237 ino_t d_ino;
238 off_t d_off;
239 unsigned short d_reclen;
240 unsigned char d_type;
241 char d_name[];
242};
243
244struct Inode
245{
247 ino_t Index;
248 mode_t Mode;
249 uint32_t Flags;
250 off_t Offset;
251
252 uintptr_t KernelData;
254
255#ifdef __cplusplus
256
257 /* ... */
258
259 void SetDevice(int Major, int Minor)
260 {
261 this->RawDevice = ((Major & 0xFFF) << 8) |
262 (Minor & 0xFF);
263 }
264
265 int GetMajor()
266 {
267 return ((unsigned int)(this->RawDevice) >> 8) & 0xFFF;
268 }
269
270 int GetMinor()
271 {
272 return this->RawDevice & 0xFF;
273 }
274
275 Inode()
276 {
277 Device = 0;
278 RawDevice = 0;
279 Index = 0;
280 Mode = 0;
281 Flags = 0;
282 Offset = 0;
283 KernelData = 0x0;
284 PrivateData = nullptr;
285 }
286
287 ~Inode() = default;
288
289#else // __cplusplus
290
291#define INODE_MAKEDEV(major, minor) \
292 ((dev_t)(((major & 0xFFF) << 8) | \
293 (minor & 0xFF)))
294
295#define INODE_MAJOR(rdev) \
296 ((int)(((rdev) >> 8) & 0xFFF))
297
298#define INODE_MINOR(rdev) \
299 ((int)((rdev) & 0xFF))
300
301#endif // __cplusplus
302};
303
305{
306 int (*Lookup)(struct Inode *Parent, const char *Name, struct Inode **Result);
307 int (*Create)(struct Inode *Parent, const char *Name, mode_t Mode, struct Inode **Result);
308 int (*Remove)(struct Inode *Parent, const char *Name);
309 int (*Rename)(struct Inode *Parent, const char *OldName, const char *NewName);
310 ssize_t (*Read)(struct Inode *Node, void *Buffer, size_t Size, off_t Offset);
311 ssize_t (*Write)(struct Inode *Node, const void *Buffer, size_t Size, off_t Offset);
312 int (*Truncate)(struct Inode *Node, off_t Size);
313 int (*Open)(struct Inode *Node, int Flags, mode_t Mode);
314 int (*Close)(struct Inode *Node);
315 int (*Ioctl)(struct Inode *Node, unsigned long Request, void *Argp);
316 ssize_t (*ReadDir)(struct Inode *Node, struct kdirent *Buffer, size_t Size, off_t Offset, off_t Entries);
317 int (*MkDir)(struct Inode *Parent, const char *Name, mode_t Mode, struct Inode **Result);
318 int (*RmDir)(struct Inode *Parent, const char *Name);
319 int (*SymLink)(struct Inode *Parent, const char *Name, const char *Target, struct Inode **Result);
320 ssize_t (*ReadLink)(struct Inode *Node, char *Buffer, size_t Size);
321 off_t (*Seek)(struct Inode *Node, off_t Offset);
322 int (*Stat)(struct Inode *Node, struct kstat *Stat);
323} __attribute__((packed));
324
325struct FileSystemInfo;
326
328{
329 struct
330 {
336 struct Inode *node;
337 struct InodeOperations *ops;
339
347
349{
350 int (*AllocateInode)(struct FileSystemInfo *Info, struct Inode **Result);
351 int (*DeleteInode)(struct FileSystemInfo *Info, struct Inode *Node);
352
363 int (*Synchronize)(struct FileSystemInfo *Info, struct Inode *Node);
364
374 int (*Destroy)(struct FileSystemInfo *Info);
375
385 int (*Probe)(struct FileSystemDevice *Device);
386
398 int (*Mount)(struct FileSystemInfo *FS, struct Inode **Root, struct FileSystemDevice *Device);
399
409 int (*Unmount)(struct FileSystemInfo *FS);
410} __attribute__((packed));
411
413{
414 const char *Name;
415
416 int Flags;
418
421
423} __attribute__((packed));
424
425#ifndef __kernel__
427int UnregisterMountPoint(dev_t Device);
428
429dev_t RegisterFileSystem(struct FileSystemInfo *Info, struct Inode *Root);
430int UnregisterFileSystem(dev_t Device);
431#endif // !__kernel__
432
433#endif // !__FENNIX_API_FILESYSTEM_H__
dev_t RawDevice
Definition fs.h:172
dev_t RegisterMountPoint(FileSystemInfo *fsi, Inode *Root)
dev_t RawDevice
Definition fs.h:246
struct FileSystemDevice __attribute__
int Capabilities
Definition fs.h:417
struct SuperBlockOperations SuperOps
Definition fs.h:419
struct InodeOperations Ops
Definition fs.h:420
int UnregisterMountPoint(dev_t Device)
int Flags
Definition fs.h:416
time_t ChangeTime
Definition fs.h:184
int UnregisterFileSystem(dev_t Device)
const char * Name
Definition fs.h:414
blkcnt_t Blocks
Definition fs.h:190
dev_t Device
Definition fs.h:154
ino_t Index
Definition fs.h:247
dev_t Device
Definition fs.h:246
char d_name[]
Definition fs.h:241
off_t Size
Definition fs.h:175
uid_t UserID
Definition fs.h:166
mode_t Mode
Definition fs.h:248
gid_t GroupID
Definition fs.h:169
mode_t Attribute
Definition fs.h:193
dev_t RegisterFileSystem(struct FileSystemInfo *Info, struct Inode *Root)
const char * Name
Definition fs.h:0
uintptr_t KernelData
Definition fs.h:252
off_t d_off
Definition fs.h:238
nlink_t HardLinks
Definition fs.h:163
time_t ModifyTime
Definition fs.h:181
blksize_t BlockSize
Definition fs.h:187
ino_t d_ino
Definition fs.h:237
unsigned short d_reclen
Definition fs.h:239
ino_t Index
Definition fs.h:157
#define S_IFMT
Definition fs.h:48
uint32_t Flags
Definition fs.h:249
int Flags
Definition fs.h:2
off_t Offset
Definition fs.h:250
void * PrivateData
Definition fs.h:253
struct BlockDevice * Block
Block Device.
Definition fs.h:345
unsigned char d_type
Definition fs.h:240
struct FileSystemDevice::@16 inode
time_t AccessTime
Definition fs.h:178
mode_t Mode
Definition fs.h:160
void * PrivateData
Definition fs.h:422
Definition fs.h:152
Definition fs.h:245
Definition fs.h:236
ssize_t(* ReadDir)(struct Inode *Node, struct kdirent *Buffer, size_t Size, off_t Offset, off_t Entries)
Definition fs.h:316
int(* Ioctl)(struct Inode *Node, unsigned long Request, void *Argp)
Definition fs.h:315
int(* Truncate)(struct Inode *Node, off_t Size)
Definition fs.h:312
ssize_t(* Read)(struct Inode *Node, void *Buffer, size_t Size, off_t Offset)
Definition fs.h:310
int(* Remove)(struct Inode *Parent, const char *Name)
Definition fs.h:308
int(* MkDir)(struct Inode *Parent, const char *Name, mode_t Mode, struct Inode **Result)
Definition fs.h:317
int(* SymLink)(struct Inode *Parent, const char *Name, const char *Target, struct Inode **Result)
Definition fs.h:319
int(* Rename)(struct Inode *Parent, const char *OldName, const char *NewName)
Definition fs.h:309
int(* Lookup)(struct Inode *Parent, const char *Name, struct Inode **Result)
Definition fs.h:306
int(* Open)(struct Inode *Node, int Flags, mode_t Mode)
Definition fs.h:313
int(* Close)(struct Inode *Node)
Definition fs.h:314
int(* Create)(struct Inode *Parent, const char *Name, mode_t Mode, struct Inode **Result)
Definition fs.h:307
ssize_t(* ReadLink)(struct Inode *Node, char *Buffer, size_t Size)
Definition fs.h:320
int(* Stat)(struct Inode *Node, struct kstat *Stat)
Definition fs.h:322
ssize_t(* Write)(struct Inode *Node, const void *Buffer, size_t Size, off_t Offset)
Definition fs.h:311
off_t(* Seek)(struct Inode *Node, off_t Offset)
Definition fs.h:321
int(* RmDir)(struct Inode *Parent, const char *Name)
Definition fs.h:318
int(* Unmount)(struct FileSystemInfo *FS)
Definition fs.h:409
int(* Mount)(struct FileSystemInfo *FS, struct Inode **Root, struct FileSystemDevice *Device)
Definition fs.h:398
int(* Probe)(struct FileSystemDevice *Device)
Definition fs.h:385
int(* DeleteInode)(struct FileSystemInfo *Info, struct Inode *Node)
Definition fs.h:351
int(* AllocateInode)(struct FileSystemInfo *Info, struct Inode **Result)
Definition fs.h:350
int(* Synchronize)(struct FileSystemInfo *Info, struct Inode *Node)
Definition fs.h:363
int(* Destroy)(struct FileSystemInfo *Info)
Definition fs.h:374