PatchworkOS  dbbdc99
A non-POSIX operating system.
Loading...
Searching...
No Matches
superblock.c
Go to the documentation of this file.
2
4#include <kernel/fs/vfs.h>
5#include <kernel/log/log.h>
6#include <kernel/mem/pmm.h>
7
8#include <stdlib.h>
9
10static void superblock_free(superblock_t* superblock)
11{
12 if (superblock == NULL)
13 {
14 return;
15 }
16
17 assert(atomic_load(&superblock->mountCount) == 0);
18
19 rwlock_write_acquire(&superblock->fs->lock);
20 list_remove(&superblock->entry);
21 rwlock_write_release(&superblock->fs->lock);
22
23 if (superblock->ops != NULL && superblock->ops->cleanup != NULL)
24 {
25 superblock->ops->cleanup(superblock);
26 }
27
28 superblock->root = NULL;
29
30 free(superblock);
31}
32
34{
35 if (fs == NULL)
36 {
37 errno = EINVAL;
38 return NULL;
39 }
40
41 superblock_t* superblock = malloc(sizeof(superblock_t));
42 if (superblock == NULL)
43 {
44 errno = ENOMEM;
45 return NULL;
46 }
47
48 ref_init(&superblock->ref, superblock_free);
49 list_entry_init(&superblock->entry);
50 superblock->id = vfs_id_get();
51 superblock->blockSize = PAGE_SIZE;
52 superblock->maxFileSize = UINT64_MAX;
53 superblock->data = NULL;
54 superblock->root = NULL;
55 superblock->ops = ops;
56 superblock->dentryOps = dentryOps;
57 superblock->vtable = NULL;
58 superblock->fs = fs;
59 atomic_init(&superblock->mountCount, 0);
60
62 list_push_back(&fs->superblocks, &superblock->entry);
64
65 return superblock;
66}
67
69{
70 atomic_fetch_add(&superblock->mountCount, 1);
71}
72
74{
75 if (atomic_fetch_sub(&superblock->mountCount, 1) == 1)
76 {
77 if (superblock->ops != NULL && superblock->ops->unmount != NULL)
78 {
79 superblock->ops->unmount(superblock);
80 }
81 }
82}
#define assert(expression)
Definition assert.h:29
static dentry_ops_t dentryOps
Definition devfs.c:27
static fb_ops_t ops
Definition gop.c:80
void superblock_inc_mount_count(superblock_t *superblock)
Increment the mount count of a superblock.
Definition superblock.c:68
superblock_t * superblock_new(filesystem_t *fs, const superblock_ops_t *ops, const dentry_ops_t *dentryOps)
Create a new superblock.
Definition superblock.c:33
void superblock_dec_mount_count(superblock_t *superblock)
Decrement the mount count of a superblock.
Definition superblock.c:73
static void rwlock_write_release(rwlock_t *lock)
Releases a rwlock from writing.
Definition rwlock.h:196
static void rwlock_write_acquire(rwlock_t *lock)
Acquires a rwlock for writing, blocking until it is available.
Definition rwlock.h:158
static void ref_init(ref_t *ref, void *callback)
Initialize a reference counter.
Definition ref.h:130
uint64_t vfs_id_get(void)
Generates a new unique ID, to be used for any VFS object.
Definition vfs.c:1236
#define EINVAL
Invalid argument.
Definition errno.h:142
#define ENOMEM
Out of memory.
Definition errno.h:92
#define errno
Error number variable.
Definition errno.h:27
static void list_remove(list_entry_t *entry)
Removes a list entry from its current list.
Definition list.h:290
static void list_push_back(list_t *list, list_entry_t *entry)
Pushes an entry to the end of the list.
Definition list.h:322
static void list_entry_init(list_entry_t *entry)
Initializes a list entry.
Definition list.h:173
#define NULL
Pointer error value.
Definition NULL.h:25
#define PAGE_SIZE
The size of a memory page in bytes.
Definition PAGE_SIZE.h:8
#define atomic_fetch_sub(object, operand)
Definition stdatomic.h:286
#define atomic_load(object)
Definition stdatomic.h:288
#define atomic_fetch_add(object, operand)
Definition stdatomic.h:283
#define atomic_init(obj, value)
Definition stdatomic.h:75
#define UINT64_MAX
Definition stdint.h:74
_PUBLIC void * malloc(size_t size)
Definition malloc.c:5
_PUBLIC void free(void *ptr)
Definition free.c:11
Dentry operations structure.
Definition dentry.h:122
Filesystem structure, represents a filesystem type, e.g. fat32, tmpfs, devfs, etc.
Definition filesystem.h:53
rwlock_t lock
Used internally.
Definition filesystem.h:57
list_t superblocks
Used internally.
Definition filesystem.h:56
Superblock operations structure.
Definition superblock.h:59
void(* cleanup)(superblock_t *superblock)
Definition superblock.h:64
void(* unmount)(superblock_t *superblock)
Definition superblock.h:68
Superblock structure.
Definition superblock.h:33
uint64_t blockSize
Definition superblock.h:37
uint64_t maxFileSize
Definition superblock.h:38
const superblock_ops_t * ops
Definition superblock.h:41
atomic_uint64_t mountCount
Definition superblock.h:51
filesystem_t * fs
Definition superblock.h:44
list_entry_t entry
Definition superblock.h:35
dentry_t * root
Root dentry of the filesystem, should not take a reference.
Definition superblock.h:40
const dentry_ops_t * dentryOps
Definition superblock.h:42
void * data
Definition superblock.h:39
const irp_vtable_t * vtable
Definition superblock.h:43
static void superblock_free(superblock_t *superblock)
Definition superblock.c:10