PatchworkOS  966e257
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/mem/pmm.h>
6
7#include <stdlib.h>
8
9static void superblock_free(superblock_t* superblock)
10{
11 if (superblock == NULL)
12 {
13 return;
14 }
15
16 assert(atomic_load(&superblock->mountCount) == 0);
17
18 rwlock_write_acquire(&superblock->fs->lock);
19 list_remove(&superblock->fs->superblocks, &superblock->entry);
20 rwlock_write_release(&superblock->fs->lock);
21
22 if (superblock->ops != NULL && superblock->ops->cleanup != NULL)
23 {
24 superblock->ops->cleanup(superblock);
25 }
26
27 if (superblock->root != NULL)
28 {
29 UNREF(superblock->root);
30 }
31
32 free(superblock);
33}
34
35superblock_t* superblock_new(filesystem_t* fs, const char* deviceName, const superblock_ops_t* ops,
37{
38 superblock_t* superblock = malloc(sizeof(superblock_t));
39 if (superblock == NULL)
40 {
41 return NULL;
42 }
43
44 ref_init(&superblock->ref, superblock_free);
45 list_entry_init(&superblock->entry);
46 superblock->id = vfs_id_get();
47 superblock->blockSize = PAGE_SIZE;
48 superblock->maxFileSize = UINT64_MAX;
49 superblock->private = NULL;
50 superblock->root = NULL;
51 superblock->ops = ops;
52 superblock->dentryOps = dentryOps;
53 strncpy(superblock->deviceName, deviceName, MAX_NAME - 1);
54 superblock->deviceName[MAX_NAME - 1] = '\0';
55 superblock->fs = fs;
56 atomic_init(&superblock->mountCount, 0);
57 return superblock;
58}
59
61{
62 atomic_fetch_add(&superblock->mountCount, 1);
63}
64
66{
67 if (atomic_fetch_sub(&superblock->mountCount, 1) == 1)
68 {
69 if (superblock->ops != NULL && superblock->ops->unmount != NULL)
70 {
71 superblock->ops->unmount(superblock);
72 }
73 }
74}
#define MAX_NAME
Maximum length of names.
Definition MAX_NAME.h:11
#define assert(expression)
Definition assert.h:29
void superblock_inc_mount_count(superblock_t *superblock)
Increment the mount count of a superblock.
Definition superblock.c:60
superblock_t * superblock_new(filesystem_t *fs, const char *deviceName, const superblock_ops_t *ops, const dentry_ops_t *dentryOps)
Create a new superblock.
Definition superblock.c:35
void superblock_dec_mount_count(superblock_t *superblock)
Decrement the mount count of a superblock.
Definition superblock.c:65
void rwlock_write_acquire(rwlock_t *lock)
Acquires a rwlock for writing, blocking until it is available.
Definition rwlock.c:62
void rwlock_write_release(rwlock_t *lock)
Releases a rwlock from writing.
Definition rwlock.c:109
static void ref_init(ref_t *ref, void *free)
Initialize a reference counter.
Definition ref.h:92
#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
static void list_remove(list_t *list, list_entry_t *entry)
Removes a list entry from its current list.
Definition list.h:315
static void list_entry_init(list_entry_t *entry)
Initializes a list entry.
Definition list.h:182
#define PAGE_SIZE
The size of a memory page in bytes.
Definition proc.h:106
#define NULL
Pointer error value.
Definition NULL.h:23
static socket_family_ops_t ops
Definition local.c:505
static dentry_ops_t dentryOps
Definition ramfs.c:181
#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
_PUBLIC char * strncpy(char *_RESTRICT s1, const char *_RESTRICT s2, size_t n)
Definition strncpy.c:3
Dentry operations structure.
Definition dentry.h:72
Filesystem structure, represents a filesystem type, e.g. fat32, ramfs, sysfs, etc.
Definition filesystem.h:31
rwlock_t lock
Used internally.
Definition filesystem.h:34
list_t superblocks
Used internally.
Definition filesystem.h:33
Superblock operations structure.
Definition superblock.h:70
void(* cleanup)(superblock_t *superblock)
Definition superblock.h:85
void(* unmount)(superblock_t *superblock)
Definition superblock.h:89
Superblock structure.
Definition superblock.h:44
uint64_t blockSize
Definition superblock.h:48
uint64_t maxFileSize
Definition superblock.h:49
const superblock_ops_t * ops
Definition superblock.h:52
atomic_uint64_t mountCount
Definition superblock.h:62
filesystem_t * fs
Definition superblock.h:54
list_entry_t entry
Definition superblock.h:46
dentry_t * root
Definition superblock.h:51
const dentry_ops_t * dentryOps
Definition superblock.h:53
superblock_id_t id
Definition superblock.h:47
char deviceName[MAX_NAME]
Definition superblock.h:55
void * private
Definition superblock.h:50
static void superblock_free(superblock_t *superblock)
Definition superblock.c:9