Voxia OS v0.0.1
Hobby Project Operating System Targeting x86-64
Loading...
Searching...
No Matches
cache.c
Go to the documentation of this file.
1// Copyright (c) 2025 Mohammad Arfan
2
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21#include "autoconf.h"
22#include "init/init.h"
23#include "libk/serial.h"
24#include "llist.h"
25#include "type.h"
26#include <hash.h>
27#include <str.h>
28#include <type.h>
29#include <vfs/cache.h>
30#include <vfs/dentry.h>
31
32static struct vfs_cache* cache_ = 0;
33
34INIT(VfsCache) {
36 serial2_printf("cache at 0x%x\n", cache_);
37}
38
40 struct vfs_cache* cache =
41 (struct vfs_cache*)kalloc(sizeof(struct vfs_cache));
42 memset(cache, 0, sizeof(struct vfs_cache));
43 cache->count = 0;
44 __atomic_clear(&cache->lock, __ATOMIC_RELAXED);
45 serial2_printf("create cache at 0x%x\n", cache);
46 return cache;
47}
48
49void hlist_add_head(struct hlist_node* n, struct hlist_head* h) {
50 struct hlist_node* first = h->first;
51 n->next = first;
52 n->prev = NULL;
53 if (first)
54 first->prev = n;
55 h->first = n;
56}
57
58static void hlist_del(struct hlist_node* n, struct hlist_head* h) {
59 if (n->prev) {
60 n->prev->next = n->next;
61 } else {
62 h->first = n->next;
63 }
64
65 if (n->next) {
66 n->next->prev = n->prev;
67 }
68
69 n->next = n->prev = NULL;
70}
71
72__attribute__((always_inline)) void vfs_cache_insert(struct vfs_cache* cache,
73 struct dentry* dentry) {
74 while (__atomic_test_and_set(&cache->lock, __ATOMIC_ACQUIRE))
75 ;
76
77 auto idx = dentry->hash & (VFS_CACHE_SIZE - 1);
78
80 __atomic_fetch_add(&cache->count, 1, __ATOMIC_RELAXED);
81
82 bool has_parent = dentry->parent != NULL;
83 if (has_parent)
85
86 __atomic_clear(&cache->lock, __ATOMIC_RELEASE); // release dulu
87
88 if (!has_parent)
89 LOG_DEBUG("VFS", "'%s' has no parent", dentry->name->c_str);
90}
91
93 const char* name) {
95 auto idx = h & (VFS_CACHE_SIZE - 1);
96
97 while (__atomic_test_and_set(&cache->lock, __ATOMIC_ACQUIRE))
98 ;
99
100 struct hlist_head* bucket = &cache->buckets[idx];
101
102 for (struct hlist_node* pos = bucket->first; pos != NULL;
103 pos = pos->next) {
104 struct dentry* d = container_of(pos, struct dentry, hash_node);
105 if (d->parent == parent && strcmp(d->name->c_str, name) == 0) {
106 __atomic_clear(&cache->lock, __ATOMIC_RELEASE);
107 return d;
108 }
109 }
110
111 __atomic_clear(&cache->lock, __ATOMIC_RELEASE);
112
113 return 0;
114}
115
116void cache_remove(struct vfs_cache* cache, struct dentry* dentry) {
117 while (__atomic_test_and_set(&cache->lock, __ATOMIC_ACQUIRE))
118 ;
119
121 hlist_del(&dentry->hash_node, &cache->buckets[idx]);
122 __atomic_fetch_sub(&cache->count, 1, __ATOMIC_RELAXED);
123
124 __atomic_clear(&cache->lock, __ATOMIC_RELEASE);
125}
126
struct SDT h
Definition acpi.h:0
struct vfs_cache * create_vfs_cache()
Definition cache.c:39
static struct vfs_cache * cache_
Definition cache.c:32
void cache_remove(struct vfs_cache *cache, struct dentry *dentry)
Definition cache.c:116
static void hlist_del(struct hlist_node *n, struct hlist_head *h)
Definition cache.c:58
void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
Definition cache.c:49
struct dentry * cache_lookup(struct vfs_cache *cache, struct dentry *parent, const char *name)
Definition cache.c:92
struct vfs_cache * get_root_cache()
void vfs_cache_insert(struct vfs_cache *cache, struct dentry *dentry)
#define VFS_CACHE_SIZE
Definition cache.h:6
kstring name
Definition dentry.h:5
uint32_t hash_dentry(const char *name, dentry_ptr parent)
Definition dentry.c:24
dentry_ptr parent
Definition dentry.h:7
struct hlist_node hash_node
Definition dentry.h:8
uint64_t pos
Definition fd.h:4
typedef __attribute__
Definition msi.c:47
#define INIT(fn)
Definition init.h:26
void serial2_printf(const char *fmt,...)
void * kalloc(size_t size)
static void llist_add_tail(struct llist_head *new_, struct llist_head *head)
Definition llist.h:32
struct process_node cache
Definition process.h:10
#define LOG_DEBUG(mod, fmt,...)
Definition serial.h:22
int strcmp(const char *s1, const char *s2)
void memset(void *ptr, int value, size_t num)
struct hlist_node hash_node
Definition dentry.h:35
kstring name
Definition dentry.h:32
dentry_ptr parent
Definition dentry.h:34
uint32_t hash
Definition dentry.h:30
struct llist_head child_list
Definition dentry.h:27
struct hlist_node * next
Definition cache.h:9
struct hlist_node * prev
Definition cache.h:10
char * c_str
Definition string.h:12
static thread_bucket_t bucket
Definition thread.c:11
#define NULL
Definition type.h:76
unsigned int uint32_t
Definition type.h:19
#define KERNEL_API
Definition type.h:93
#define container_of(ptr, type, member)
Definition type.h:108
uint16_t idx
Definition virtio-gpu.hpp:1