AquilaOS documentation
How it works, and its code
Toggle main menu visibility
Loading...
Searching...
No Matches
bcache.c
Go to the documentation of this file.
1
6
7
#include <
core/system.h
>
8
#include <
fs/bcache.h
>
9
10
MALLOC_DEFINE
(M_BCACHE,
"bcache"
,
"block cache structure"
);
11
MALLOC_DEFINE
(M_CACHE_BLOCK,
"cache block"
,
"cached block structure"
);
12
14
struct
cache_block
{
15
off_t
off
;
/* its number */
16
void
*
data
;
/* its contents */
17
18
int
dirty
;
/* changed since it was read (not written back yet) */
19
};
20
22
static
int
bcache_eq
(
void
*_a,
void
*_b)
23
{
24
struct
cache_block
*a = (
struct
cache_block
*) _a;
25
off_t
*b = (
off_t
*) _b;
26
27
return
a->
off
== *b;
28
}
29
35
void
bcache_init
(
struct
bcache
*
bcache
)
36
{
37
bcache
->
hashmap
= hashmap_new(0,
bcache_eq
);
38
}
39
48
int
bcache_insert
(
struct
bcache
*
bcache
,
uint64_t
off
,
void
*
data
)
49
{
50
if
(!
bcache
)
51
return
-EINVAL;
52
53
if
(!
bcache
->
hashmap
)
54
bcache_init
(
bcache
);
55
56
struct
cache_block
*block =
kmalloc
(
sizeof
(
struct
cache_block
), &M_CACHE_BLOCK,
M_ZERO
);
57
if
(!block)
return
-ENOMEM;
58
59
block->
off
=
off
;
60
block->
data
=
data
;
61
62
hash_t
hash = hashmap_digest(&
off
,
sizeof
(
off
));
63
return
hashmap_insert(
bcache
->
hashmap
, hash, block);
64
}
65
73
int
bcache_remove
(
struct
bcache
*
bcache
,
uint64_t
off
)
74
{
75
if
(!
bcache
|| !
bcache
->
hashmap
)
76
return
-1;
77
78
hash_t
hash = hashmap_digest(&
off
,
sizeof
(
off
));
79
80
struct
hashmap_node
*node = hashmap_lookup(
bcache
->
hashmap
,
hash
, &off);
81
82
if
(node) {
83
void
*block = node->
entry
;
84
node->
entry
=
NULL
;
85
kfree
(block);
86
hashmap_node_remove(
bcache
->
hashmap
, node);
87
return
0;
88
}
89
90
return
-1;
91
}
92
100
void
*
bcache_find
(
struct
bcache
*
bcache
,
uint64_t
off)
101
{
102
if
(!
bcache
|| !
bcache
->
hashmap
)
103
return
NULL
;
104
105
hash_t
hash
= hashmap_digest(&off,
sizeof
(off));
106
107
struct
hashmap_node
*node = hashmap_lookup(
bcache
->
hashmap
,
hash
, &off);
108
109
if
(node) {
110
struct
cache_block
*block = (
struct
cache_block
*) node->
entry
;
111
return
block->
data
;
112
}
113
114
return
NULL
;
115
}
116
124
void
bcache_dirty
(
struct
bcache
*
bcache
,
uint64_t
off
)
125
{
126
if
(!
bcache
|| !
bcache
->
hashmap
)
127
return
;
128
129
hash_t
hash = hashmap_digest(&
off
,
sizeof
(
off
));
130
131
struct
hashmap_node
*node = hashmap_lookup(
bcache
->
hashmap
,
hash
, &off);
132
133
if
(node) {
134
struct
cache_block
*block = (
struct
cache_block
*) node->
entry
;
135
block->
dirty
= 1;
136
}
137
}
bcache_remove
int bcache_remove(struct bcache *bcache, uint64_t off)
Takes a block out of a cache.
Definition
bcache.c:73
bcache_dirty
void bcache_dirty(struct bcache *bcache, uint64_t off)
Marks a cached block as changed.
Definition
bcache.c:124
bcache_insert
int bcache_insert(struct bcache *bcache, uint64_t off, void *data)
Adds a block to a cache, which keeps its memory from now on.
Definition
bcache.c:48
bcache_eq
static int bcache_eq(void *_a, void *_b)
Says whether a cached block is the one whose number _b points to.
Definition
bcache.c:22
bcache_find
void * bcache_find(struct bcache *bcache, uint64_t off)
Finds a cached block.
Definition
bcache.c:100
bcache_init
void bcache_init(struct bcache *bcache)
Sets up an empty block cache.
Definition
bcache.c:35
bcache.h
hash_t
uintptr_t hash_t
Definition
hashmap.h:22
system.h
MALLOC_DEFINE
#define MALLOC_DEFINE(type, _name, _desc)
Definition
kvmem.h:30
kmalloc
void * kmalloc(size_t size, struct malloc_type *type, int flags)
Allocates memory for one of the kernel's structures.
Definition
kvmem.c:123
M_ZERO
#define M_ZERO
Definition
kvmem.h:26
kfree
void kfree(void *)
Frees an object that kmalloc() gave.
Definition
kvmem.c:146
NULL
#define NULL
Definition
stddef.h:27
uint64_t
unsigned long long int uint64_t
Definition
stddef.h:23
bcache
A filesystem's block cache (fs/bcache.c): its blocks that were read, by their offset.
Definition
bcache.h:9
bcache::hashmap
struct hashmap * hashmap
Definition
bcache.h:10
cache_block
A cached block.
Definition
bcache.c:14
cache_block::off
off_t off
Definition
bcache.c:15
cache_block::data
void * data
Definition
bcache.c:16
cache_block::dirty
int dirty
Definition
bcache.c:18
hashmap_node
A hash table's node: an entry, with its key's hash.
Definition
hashmap.h:43
hashmap_node::hash
hash_t hash
Definition
hashmap.h:44
hashmap_node::entry
void * entry
Definition
hashmap.h:45
off_t
long int off_t
Definition
types.h:10
kernel
fs
bcache.c
Generated by
1.18.0