Fennix  1.0.0
Full Documentation
fs.h
Go to the documentation of this file.
1 /*
2  This file is part of Fennix Kernel.
3 
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.
8 
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.
13 
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 O_RDONLY 00
99 #define O_WRONLY 01
100 #define O_RDWR 02
101 #define O_CREAT 0100
102 #define O_EXCL 0200
103 #define O_TRUNC 01000
104 #define O_APPEND 02000
105 #define O_NOFOLLOW 0400000
106 #define O_CLOEXEC 02000000
107 
108 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
109 #define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
110 #define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
111 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
112 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
113 #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
114 #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
115 
116 #define DT_UNKNOWN 0x0
117 #define DT_FIFO 0x1
118 #define DT_CHR 0x2
119 #define DT_DIR 0x4
120 #define DT_BLK 0x6
121 #define DT_REG 0x8
122 #define DT_LNK 0xA
123 #define DT_SOCK 0xC
124 #define DT_WHT 0xE
125 
126 #define IFTODT(x) ((x) >> 12 & 0xF)
127 #define DTTOIF(x) ((x) << 12)
128 
129 #define SYMLOOP_MAX 40
130 
131 #ifndef __cplusplus
132 #define static_assert _Static_assert
133 #endif
134 
135 #ifdef __LP64__
136 static_assert(sizeof(dev_t) == 8, "dev_t must be 64 bits");
137 static_assert(sizeof(ino_t) == 8, "ino_t must be 64 bits");
138 static_assert(sizeof(mode_t) == 4, "mode_t must be 32 bits");
139 static_assert(sizeof(nlink_t) == 4, "nlink_t must be 32 bits");
140 static_assert(sizeof(uid_t) == 4, "uid_t must be 32 bits");
141 static_assert(sizeof(gid_t) == 4, "gid_t must be 32 bits");
142 static_assert(sizeof(off_t) == 8, "off_t must be 64 bits");
143 static_assert(sizeof(time_t) == 8, "time_t must be 64 bits");
144 static_assert(sizeof(blksize_t) == 8, "blksize_t must be 64 bits");
145 static_assert(sizeof(blkcnt_t) == 8, "blkcnt_t must be 64 bits");
146 #else
147 static_assert(sizeof(dev_t) == 4, "dev_t must be 32 bits");
148 static_assert(sizeof(ino_t) == 4, "ino_t must be 32 bits");
149 static_assert(sizeof(mode_t) == 2, "mode_t must be 16 bits");
150 static_assert(sizeof(nlink_t) == 2, "nlink_t must be 16 bits");
151 static_assert(sizeof(uid_t) == 2, "uid_t must be 16 bits");
152 static_assert(sizeof(gid_t) == 2, "gid_t must be 16 bits");
153 static_assert(sizeof(off_t) == 4, "off_t must be 32 bits");
154 static_assert(sizeof(time_t) == 4, "time_t must be 32 bits");
155 static_assert(sizeof(blksize_t) == 4, "blksize_t must be 32 bits");
156 static_assert(sizeof(blkcnt_t) == 4, "blkcnt_t must be 32 bits");
157 #endif
158 
159 #undef static_assert
160 
161 struct kstat
162 {
164  dev_t Device;
165 
167  ino_t Index;
168 
170  mode_t Mode;
171 
173  nlink_t HardLinks;
174 
176  uid_t UserID;
177 
179  gid_t GroupID;
180 
182  dev_t RawDevice;
183 
185  off_t Size;
186 
188  time_t AccessTime;
189 
191  time_t ModifyTime;
192 
194  time_t ChangeTime;
195 
197  blksize_t BlockSize;
198 
200  blkcnt_t Blocks;
201 
203  mode_t Attribute;
204 
205 #ifdef __cplusplus
206 
207  dev_t MakeDevice(int Major, int Minor)
208  {
209  return ((Major & 0xFFF) << 8) |
210  (Minor & 0xFF);
211  }
212 
213  int GetMajor()
214  {
215  return ((unsigned int)(Device) >> 8) & 0xFFF;
216  }
217 
218  int GetMinor()
219  {
220  return Device & 0xFF;
221  }
222 
223  void SetFileType(mode_t Type)
224  {
225  Mode = (Mode & ~S_IFMT) |
226  (Type & S_IFMT);
227  }
228 
229  mode_t GetFileType() { return Mode & S_IFMT; }
230  void ClearFileType() { Mode = Mode & ~S_IFMT; }
231  bool IsType(mode_t Type) { return (Mode & S_IFMT) == Type; }
232 
233  void SetPermissions(mode_t Permissions)
234  {
235  Mode = (Mode & S_IFMT) |
236  (Permissions & ~S_IFMT);
237  }
238 
239  mode_t GetPermissions() { return Mode & ~S_IFMT; }
240  void ClearPermissions() { Mode = Mode & S_IFMT; }
241 
242 #endif // __cplusplus
243 };
244 
245 struct kdirent
246 {
247  ino_t d_ino;
248  off_t d_off;
249  unsigned short d_reclen;
250  unsigned char d_type;
251  char d_name[];
252 };
253 
254 struct Inode
255 {
257  ino_t Index;
258  mode_t Mode;
259  uint32_t Flags;
260  off_t Offset;
261 
262  uintptr_t KernelData;
263  void *PrivateData;
264 
265 #ifdef __cplusplus
266 
267  /* ... */
268 
269  void SetDevice(int Major, int Minor)
270  {
271  this->RawDevice = ((Major & 0xFFF) << 8) |
272  (Minor & 0xFF);
273  }
274 
275  int GetMajor()
276  {
277  return ((unsigned int)(this->RawDevice) >> 8) & 0xFFF;
278  }
279 
280  int GetMinor()
281  {
282  return this->RawDevice & 0xFF;
283  }
284 
285  Inode()
286  {
287  Device = 0;
288  RawDevice = 0;
289  Index = 0;
290  Mode = 0;
291  Flags = 0;
292  Offset = 0;
293  KernelData = 0x0;
294  PrivateData = nullptr;
295  }
296 
297  ~Inode() = default;
298 
299 #else // __cplusplus
300 
301 #define INODE_MAKEDEV(major, minor) \
302  ((dev_t)(((major & 0xFFF) << 8) | \
303  (minor & 0xFF)))
304 
305 #define INODE_MAJOR(rdev) \
306  ((int)(((rdev) >> 8) & 0xFFF))
307 
308 #define INODE_MINOR(rdev) \
309  ((int)((rdev) & 0xFF))
310 
311 #endif // __cplusplus
312 };
313 
315 {
316  int (*Lookup)(struct Inode *Parent, const char *Name, struct Inode **Result);
317  int (*Create)(struct Inode *Parent, const char *Name, mode_t Mode, struct Inode **Result);
318  int (*Remove)(struct Inode *Parent, const char *Name);
319  int (*Rename)(struct Inode *Parent, const char *OldName, const char *NewName);
320  ssize_t (*Read)(struct Inode *Node, void *Buffer, size_t Size, off_t Offset);
321  ssize_t (*Write)(struct Inode *Node, const void *Buffer, size_t Size, off_t Offset);
322  int (*Truncate)(struct Inode *Node, off_t Size);
323  int (*Open)(struct Inode *Node, int Flags, mode_t Mode);
324  int (*Close)(struct Inode *Node);
325  int (*Ioctl)(struct Inode *Node, unsigned long Request, void *Argp);
326  ssize_t (*ReadDir)(struct Inode *Node, struct kdirent *Buffer, size_t Size, off_t Offset, off_t Entries);
327  int (*MkDir)(struct Inode *Parent, const char *Name, mode_t Mode, struct Inode **Result);
328  int (*RmDir)(struct Inode *Parent, const char *Name);
329  int (*SymLink)(struct Inode *Parent, const char *Name, const char *Target, struct Inode **Result);
330  ssize_t (*ReadLink)(struct Inode *Node, char *Buffer, size_t Size);
331  off_t (*Seek)(struct Inode *Node, off_t Offset);
332  int (*Stat)(struct Inode *Node, struct kstat *Stat);
333 } __attribute__((packed));
334 
335 #define I_FLAG_ROOT 0x1
336 #define I_FLAG_MOUNTPOINT 0x2
337 #define I_FLAG_CACHE_KEEP 0x4
338 
339 struct FileSystemInfo;
341 {
342  int (*AllocateInode)(struct FileSystemInfo *Info, struct Inode **Result);
343  int (*DeleteInode)(struct FileSystemInfo *Info, struct Inode *Node);
344 
355  int (*Synchronize)(struct FileSystemInfo *Info, struct Inode *Node);
356 
366  int (*Destroy)(struct FileSystemInfo *Info);
367 } __attribute__((packed));
368 
370 {
371  const char *Name;
372  const char *RootName;
373  int Flags;
375  struct InodeOperations Ops;
376 
377  void *PrivateData;
378 } __attribute__((packed));
379 
380 dev_t RegisterFileSystem(struct FileSystemInfo *Info, struct Inode *Root);
381 int UnregisterFileSystem(dev_t Device);
382 
383 #endif // !__FENNIX_API_FILESYSTEM_H__
dev_t RawDevice
Definition: fs.h:182
dev_t RawDevice
Definition: fs.h:256
struct SuperBlockOperations SuperOps
Definition: fs.h:374
struct InodeOperations Ops
Definition: fs.h:375
int Flags
Definition: fs.h:373
time_t ChangeTime
Definition: fs.h:194
int UnregisterFileSystem(dev_t Device)
const char * Name
Definition: fs.h:371
blkcnt_t Blocks
Definition: fs.h:200
dev_t Device
Definition: fs.h:164
ino_t Index
Definition: fs.h:257
dev_t Device
Definition: fs.h:256
char d_name[]
Definition: fs.h:251
off_t Size
Definition: fs.h:185
uid_t UserID
Definition: fs.h:176
mode_t Mode
Definition: fs.h:258
gid_t GroupID
Definition: fs.h:179
mode_t Attribute
Definition: fs.h:203
dev_t RegisterFileSystem(struct FileSystemInfo *Info, struct Inode *Root)
const char * Name
Definition: fs.h:0
uintptr_t KernelData
Definition: fs.h:262
off_t d_off
Definition: fs.h:248
#define static_assert
Definition: fs.h:132
nlink_t HardLinks
Definition: fs.h:173
time_t ModifyTime
Definition: fs.h:191
blksize_t BlockSize
Definition: fs.h:197
ino_t d_ino
Definition: fs.h:247
unsigned short d_reclen
Definition: fs.h:249
ino_t Index
Definition: fs.h:167
#define S_IFMT
Definition: fs.h:48
uint32_t Flags
Definition: fs.h:259
const char * RootName
Definition: fs.h:372
int Flags
Definition: fs.h:2
off_t Offset
Definition: fs.h:260
void * PrivateData
Definition: fs.h:263
unsigned char d_type
Definition: fs.h:250
struct InodeOperations __attribute__((packed))
time_t AccessTime
Definition: fs.h:188
mode_t Mode
Definition: fs.h:170
void * PrivateData
Definition: fs.h:377
Definition: fs.h:162
Definition: fs.h:255
Definition: fs.h:246
ssize_t(* ReadDir)(struct Inode *Node, struct kdirent *Buffer, size_t Size, off_t Offset, off_t Entries)
Definition: fs.h:326
int(* Ioctl)(struct Inode *Node, unsigned long Request, void *Argp)
Definition: fs.h:325
int(* Truncate)(struct Inode *Node, off_t Size)
Definition: fs.h:322
ssize_t(* Read)(struct Inode *Node, void *Buffer, size_t Size, off_t Offset)
Definition: fs.h:320
int(* Remove)(struct Inode *Parent, const char *Name)
Definition: fs.h:318
int(* MkDir)(struct Inode *Parent, const char *Name, mode_t Mode, struct Inode **Result)
Definition: fs.h:327
int(* SymLink)(struct Inode *Parent, const char *Name, const char *Target, struct Inode **Result)
Definition: fs.h:329
int(* Rename)(struct Inode *Parent, const char *OldName, const char *NewName)
Definition: fs.h:319
int(* Lookup)(struct Inode *Parent, const char *Name, struct Inode **Result)
Definition: fs.h:316
int(* Open)(struct Inode *Node, int Flags, mode_t Mode)
Definition: fs.h:323
int(* Close)(struct Inode *Node)
Definition: fs.h:324
int(* Create)(struct Inode *Parent, const char *Name, mode_t Mode, struct Inode **Result)
Definition: fs.h:317
ssize_t(* ReadLink)(struct Inode *Node, char *Buffer, size_t Size)
Definition: fs.h:330
int(* Stat)(struct Inode *Node, struct kstat *Stat)
Definition: fs.h:332
ssize_t(* Write)(struct Inode *Node, const void *Buffer, size_t Size, off_t Offset)
Definition: fs.h:321
off_t(* Seek)(struct Inode *Node, off_t Offset)
Definition: fs.h:331
int(* RmDir)(struct Inode *Parent, const char *Name)
Definition: fs.h:328
int(* DeleteInode)(struct FileSystemInfo *Info, struct Inode *Node)
Definition: fs.h:343
int(* AllocateInode)(struct FileSystemInfo *Info, struct Inode **Result)
Definition: fs.h:342
int(* Synchronize)(struct FileSystemInfo *Info, struct Inode *Node)
Definition: fs.h:355
int(* Destroy)(struct FileSystemInfo *Info)
Definition: fs.h:366