PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
inode.h
Go to the documentation of this file.
1#pragma once
2
3#include <kernel/fs/path.h>
4#include <kernel/sync/mutex.h>
5#include <kernel/utils/map.h>
6#include <kernel/utils/ref.h>
7
8#include <stdatomic.h>
9#include <stdint.h>
10#include <sys/io.h>
11#include <sys/proc.h>
12#include <time.h>
13
14typedef struct inode inode_t;
15typedef struct inode_ops inode_ops_t;
16typedef struct superblock superblock_t;
17typedef struct file_ops file_ops_t;
18typedef struct dentry dentry_t;
19
20/**
21 * @brief Index node.
22 * @defgroup kernel_fs_inode Inode
23 * @ingroup kernel_fs
24 *
25 * A inode represents the actual data and metadata of a file. It is referenced by dentries, which represent the name or
26 * "location" of the file but a inode can appear in multiple dentries due to hardlinks or mounts.
27 *
28 * @note Despite the name inodes are in no way "nodes" in any kind of tree structure, that would be the dentries.
29 *
30 * ## Synchronization
31 *
32 * Inodes have an additional purpose within the Virtual File System (VFS) as they act as the primary means of synchronization. All dentries synchronize upon their inodes mutex, open files synchronize upon the mutex of the underlying inode and operations like create, remove, etc synchronize upon the inode mutex of the parent directory.
33 *
34 * @todo Implement actually writing/syncing dirty inodes, for now inodes should use the notify functions but they will
35 * never actually be "cleaned."
36 *
37 * @{
38 */
39
40/**
41 * @brief Inode flags.
42 * @enum inode_flags_t
43 */
44typedef enum
45{
46 INODE_NONE = 0, ///< None
48
49/**
50 * @brief Inode structure.
51 * @struct inode_t
52 *
53 * Inodes are owned by the filesystem, not the VFS.
54 */
55typedef struct inode
56{
58 inode_number_t number; ///< Constant after creation.
59 inode_type_t type; ///< Constant after creation.
61 _Atomic(uint64_t) dentryCount; ///< The number of dentries pointing to this inode.
64 time_t accessTime; ///< Unix time stamp for the last inode access.
65 time_t modifyTime; ///< Unix time stamp for last file content alteration.
66 time_t changeTime; ///< Unix time stamp for the last file metadata alteration.
67 time_t createTime; ///< Unix time stamp for the inode creation.
68 void* private;
69 superblock_t* superblock; ///< Constant after creation.
70 const inode_ops_t* ops; ///< Constant after creation.
71 const file_ops_t* fileOps; ///< Constant after creation.
73} inode_t;
74
75/**
76 * @brief Inode operations structure.
77 * @struct inode_ops_t
78 *
79 * Note that the inodes mutex will be acquired by the vfs.
80 */
81typedef struct inode_ops
82{
83 /**
84 * @brief Look up a dentry in a directory inode.
85 *
86 * Should set the target dentry to be positive (give it an inode), if the entry does not exist the operation
87 * should still return success but leave the dentry negative.
88 *
89 * @param dir The directory inode to look in.
90 * @param target The dentry to look up.
91 * @return On success, `0`. On failure, returns `ERR` and `errno` is set.
92 */
93 uint64_t (*lookup)(inode_t* dir, dentry_t* target);
94 /**
95 * @brief Handles both directories and files depending on mode.
96 *
97 * Takes in a negative dentry and creates the corresponding inode to make the dentry positive.
98 *
99 * @param dir The directory inode to create the entry in.
100 * @param target The negative dentry to create.
101 * @param mode The mode to create the entry with.
102 * @return On success, `0`. On failure, returns `ERR` and `errno` is set.
103 */
104 uint64_t (*create)(inode_t* dir, dentry_t* target, mode_t mode);
105 /**
106 * @brief Set the inode size to zero.
107 *
108 * @param target The inode to truncate.
109 */
110 void (*truncate)(inode_t* target);
111 /**
112 * @brief Make the same file inode appear twice in the filesystem.
113 *
114 * @param dir The directory inode to create the link in.
115 * @param old The existing dentry containing the inode to link to.
116 * @param new The negative dentry to store the same inode as old.
117 * @return On success, `0`. On failure, returns `ERR` and `errno` is set.
118 */
119 uint64_t (*link)(inode_t* dir, dentry_t* old, dentry_t* new);
120 /**
121 * @brief Remove a file or directory.
122 *
123 * @param dir The directory inode containing the target.
124 * @param target The dentry to remove.
125 * @param mode The mode for removal.
126 * @return On success, `0`. On failure, returns `ERR` and `errno` is set.
127 */
128 uint64_t (*remove)(inode_t* dir, dentry_t* target, mode_t mode);
129 /**
130 * @brief Cleanup function called when the inode is being freed.
131 *
132 * @param inode The inode being freed.
133 */
134 void (*cleanup)(inode_t* inode);
136
137/**
138 * @brief Create a new inode.
139 *
140 * This DOES add the inode to the inode cache. It also does not associate the inode with a dentry, that is done when a
141 * dentry is made positive with `dentry_make_positive()`.
142 *
143 * There is no `inode_free()` instead use `UNREF()`.
144 *
145 * @param superblock The superblock the inode belongs to.
146 * @param number The inode number, for a generic filesystem `vfs_id_get()` can be used.
147 * @param type The inode type.
148 * @param ops The inode operations.
149 * @param fileOps The file operations for files opened on this inode.
150 * @return On success, the new inode. On failure, returns `NULL` and `errno` is set.
151 */
152inode_t* inode_new(superblock_t* superblock, inode_number_t number, inode_type_t type, const inode_ops_t* ops,
153 const file_ops_t* fileOps);
154
155/**
156 * @brief Notify the inode that it has been accessed.
157 *
158 * This updates the access time.
159 *
160 * @param inode The inode to notify.
161 */
162void inode_notify_access(inode_t* inode);
163
164/**
165 * @brief Notify the inode that its content has been modified.
166 *
167 * This updates the modify time and change time.
168 *
169 * @param inode The inode to notify.
170 */
171void inode_notify_modify(inode_t* inode);
172
173/**
174 * @brief Notify the inode that its metadata has changed.
175 *
176 * This updates the change time.
177 *
178 * @param inode The inode to notify.
179 */
180void inode_notify_change(inode_t* inode);
181
182/**
183 * @brief Truncate the inode.
184 *
185 * The filesystem should implement the actual truncation in the inode ops truncate function, this is just a helper to
186 * call it.
187 *
188 * @param inode The inode to truncate.
189 */
190void inode_truncate(inode_t* inode);
191
192/** @} */
void inode_notify_change(inode_t *inode)
Notify the inode that its metadata has changed.
Definition inode.c:109
void inode_notify_modify(inode_t *inode)
Notify the inode that its content has been modified.
Definition inode.c:97
void inode_notify_access(inode_t *inode)
Notify the inode that it has been accessed.
Definition inode.c:85
inode_flags_t
Inode flags.
Definition inode.h:45
void inode_truncate(inode_t *inode)
Truncate the inode.
Definition inode.c:120
inode_t * inode_new(superblock_t *superblock, inode_number_t number, inode_type_t type, const inode_ops_t *ops, const file_ops_t *fileOps)
Create a new inode.
Definition inode.c:41
@ INODE_NONE
None.
Definition inode.h:46
mode_t
Path flags and permissions.
Definition path.h:74
inode_type_t
Inode type enum.
Definition io.h:313
uint64_t inode_number_t
Inode number enum.
Definition io.h:322
uint64_t link(const char *oldPath, const char *newPath)
System call for creating a hardlink.
Definition link.c:9
static socket_family_ops_t ops
Definition local.c:505
static file_ops_t fileOps
Definition ramfs.c:84
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
_PUBLIC int remove(const char *filename)
Definition remove.c:6
Directory entry structure.
Definition dentry.h:84
File operations structure.
Definition file.h:54
Inode operations structure.
Definition inode.h:82
Inode structure.
Definition inode.h:56
uint64_t blocks
Definition inode.h:63
mutex_t mutex
Definition inode.h:72
ref_t ref
Definition inode.h:57
time_t accessTime
Unix time stamp for the last inode access.
Definition inode.h:64
time_t createTime
Unix time stamp for the inode creation.
Definition inode.h:67
time_t modifyTime
Unix time stamp for last file content alteration.
Definition inode.h:65
inode_type_t type
Constant after creation.
Definition inode.h:59
inode_number_t number
Constant after creation.
Definition inode.h:58
superblock_t * superblock
Constant after creation.
Definition inode.h:69
const inode_ops_t * ops
Constant after creation.
Definition inode.h:70
uint64_t size
Definition inode.h:62
inode_flags_t flags
Definition inode.h:60
_Atomic(uint64_t) dentryCount
The number of dentries pointing to this inode.
time_t changeTime
Unix time stamp for the last file metadata alteration.
Definition inode.h:66
const file_ops_t * fileOps
Constant after creation.
Definition inode.h:71
Mutex structure.
Definition mutex.h:41
Reference counting structure.
Definition ref.h:30
Superblock structure.
Definition superblock.h:44
long long unsigned time_t
Definition time_t.h:4