PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
mount.c
Go to the documentation of this file.
1#include <kernel/fs/mount.h>
2
3#include <kernel/fs/vfs.h>
4
5#include <stdlib.h>
6
7static void mount_free(mount_t* mount)
8{
9 if (mount == NULL)
10 {
11 return;
12 }
13
14 if (mount->superblock != NULL)
15 {
18 }
19
20 if (mount->target != NULL)
21 {
24 }
25
26 if (mount->source != NULL)
27 {
29 }
30
31 if (mount->parent != NULL)
32 {
34 }
35
36 free(mount);
37}
38
39mount_t* mount_new(superblock_t* superblock, dentry_t* source, dentry_t* target, mount_t* parent, mode_t mode)
40{
41 if (superblock == NULL || source == NULL || (target != NULL && parent == NULL))
42 {
43 errno = EINVAL;
44 return NULL;
45 }
46
47 if (!dentry_is_positive(source) || (target != NULL && !dentry_is_positive(target)))
48 {
49 errno = ENOENT;
50 return NULL;
51 }
52
54 {
55 errno = ENOTDIR;
56 return NULL;
57 }
58
59 if (!(mode & MODE_DIRECTORY) && dentry_is_dir(source))
60 {
61 errno = EISDIR;
62 return NULL;
63 }
64
65 mount_t* mount = malloc(sizeof(mount_t));
66 if (mount == NULL)
67 {
68 errno = ENOMEM;
69 return NULL;
70 }
71
73 mount->id = vfs_id_get();
75 if (target != NULL)
76 {
77 mount->target = REF(target);
78 atomic_fetch_add_explicit(&target->mountCount, 1, memory_order_relaxed);
79 }
80 else
81 {
82 mount->target = NULL;
83 }
85 mount->superblock = REF(superblock);
86 mount->parent = parent != NULL ? REF(parent) : NULL;
87 mount->mode = mode;
88
89 return mount;
90}
bool dentry_is_positive(dentry_t *dentry)
Check if a dentry is positive.
Definition dentry.c:243
bool dentry_is_dir(dentry_t *dentry)
Check if the inode associated with a dentry is a directory.
Definition dentry.c:268
bool dentry_is_file(dentry_t *dentry)
Check if the inode associated with a dentry is a file.
Definition dentry.c:253
mount_t * mount_new(superblock_t *superblock, dentry_t *source, dentry_t *target, mount_t *parent, mode_t mode)
Create a new mount.
Definition mount.c:39
mode_t
Path flags and permissions.
Definition path.h:74
@ MODE_DIRECTORY
Definition path.h:84
void superblock_inc_mount_count(superblock_t *superblock)
Increment the mount count of a superblock.
Definition superblock.c:60
void superblock_dec_mount_count(superblock_t *superblock)
Decrement the mount count of a superblock.
Definition superblock.c:65
static void ref_init(ref_t *ref, void *free)
Initialize a reference counter.
Definition ref.h:92
#define REF(ptr)
Increment reference count.
Definition ref.h:65
#define UNREF(ptr)
Decrement reference count.
Definition ref.h:80
uint64_t vfs_id_get(void)
Generates a new unique ID, to be used for any VFS object.
Definition vfs.c:765
#define ENOENT
No such file or directory.
Definition errno.h:42
#define EINVAL
Invalid argument.
Definition errno.h:142
#define ENOMEM
Out of memory.
Definition errno.h:92
#define ENOTDIR
Not a directory.
Definition errno.h:132
#define errno
Error number variable.
Definition errno.h:27
#define EISDIR
Is a directory.
Definition errno.h:137
#define NULL
Pointer error value.
Definition NULL.h:23
static clock_source_t source
Structure to describe the HPET to the sys time subsystem.
Definition hpet.c:192
static void mount_free(mount_t *mount)
Definition mount.c:7
static mount_t * mount
Definition ramfs.c:28
@ memory_order_relaxed
Definition stdatomic.h:116
#define atomic_fetch_add_explicit(object, operand, order)
Definition stdatomic.h:259
#define atomic_fetch_sub_explicit(object, operand, order)
Definition stdatomic.h:262
_PUBLIC void * malloc(size_t size)
Definition malloc.c:5
_PUBLIC void free(void *ptr)
Definition free.c:11
Directory entry structure.
Definition dentry.h:84
Mount structure.
Definition mount.h:44
superblock_t * superblock
The superblock of the mounted filesystem.
Definition mount.h:50
dentry_t * target
The dentry which the source is mounted to, can be NULL for the root filesystem.
Definition mount.h:49
mode_t mode
Specifies the maximum permissions for this mount and if it is a directory or a file.
Definition mount.h:52
mount_t * parent
The parent mount, can be NULL for the root filesystem.
Definition mount.h:51
dentry_t * source
The dentry to appear at target once mounted, usually the root dentry of the mounted filesystem.
Definition mount.h:48
ref_t ref
Definition mount.h:45
mount_id_t id
Definition mount.h:46
Superblock structure.
Definition superblock.h:44