The manual
ginnOS
The reference: libc, the libraries and the kernel
Toggle main menu visibility
Loading...
Searching...
No Matches
block_cache.h
Go to the documentation of this file.
1
#pragma once
2
3
/**
4
* @file block_cache.h
5
* @brief a sector cache in front of a block device.
6
*
7
* a block_cache_t is itself a block device: reads are served from memory
8
* when the sectors are cached; writes go to the device at once (write
9
* through) and update the cached copies, so the disk is never behind.
10
*
11
* the cache holds BLOCK_CACHE_SETS * BLOCK_CACHE_WAYS sectors. a sector can
12
* be in one of the BLOCK_CACHE_WAYS slots of its set (lba % sets); a miss
13
* replaces the least recently used slot of the set.
14
*/
15
16
#include "
common/error.h
"
17
#include "
drivers/disk/block_device.h
"
18
#include "
kernel/sync/mutex.h
"
19
20
#define BLOCK_CACHE_SETS 256
21
#define BLOCK_CACHE_WAYS 4
22
#define BLOCK_CACHE_SECTOR 512
23
24
typedef
struct
25
{
26
uint32_t
lba
;
27
uint32_t
last_use
;
// value of the use counter at the last access
28
bool
valid
;
29
}
block_cache_slot_t
;
30
31
typedef
struct
32
{
33
block_device_t
block
;
// the cached device (hand this to the filesystem)
34
block_device_t
*
backing
;
// the device under the cache
35
block_cache_slot_t
slots
[
BLOCK_CACHE_SETS
][
BLOCK_CACHE_WAYS
];
36
uint8_t
*
data
;
// the sectors, slot by slot
37
uint32_t
use_counter
;
38
uint32_t
hits
;
// sectors served from memory
39
uint32_t
misses
;
// sectors read from the device
40
mutex_t
lock
;
41
}
block_cache_t
;
42
43
/**
44
* put a cache in front of a device with 512-byte sectors.
45
* @param cache the cache (lives as long as the device is used).
46
* @param backing the device to cache.
47
* @return KERR_OK, KERR_INVAL for another sector size, or KERR_NOMEM.
48
*/
49
kerr_t
block_cache_init
(
block_cache_t
*cache,
block_device_t
*backing);
50
51
/**
52
* the cache that serves the root filesystem, for /proc/meminfo (NULL
53
* before it is set up).
54
*/
55
extern
block_cache_t
*
block_cache_root
;
block_cache_init
kerr_t block_cache_init(block_cache_t *cache, block_device_t *backing)
put a cache in front of a device with 512-byte sectors.
Definition
block_cache.c:154
block_cache_root
block_cache_t * block_cache_root
the cache that serves the root filesystem, for /proc/meminfo (NULL before it is set up).
Definition
block_cache.c:6
BLOCK_CACHE_WAYS
#define BLOCK_CACHE_WAYS
Definition
block_cache.h:21
BLOCK_CACHE_SETS
#define BLOCK_CACHE_SETS
Definition
block_cache.h:20
block_device.h
block_device_t
struct block_device block_device_t
Definition
block_device.h:5
error.h
kerr_t
kerr_t
unified kernel error codes.
Definition
error.h:14
uint32_t
unsigned int uint32_t
Definition
fb.h:17
uint8_t
unsigned char uint8_t
Definition
fb.h:18
mutex.h
block_cache_slot_t
Definition
block_cache.h:25
block_cache_slot_t::last_use
uint32_t last_use
Definition
block_cache.h:27
block_cache_slot_t::lba
uint32_t lba
Definition
block_cache.h:26
block_cache_slot_t::valid
bool valid
Definition
block_cache.h:28
block_cache_t
Definition
block_cache.h:32
block_cache_t::block
block_device_t block
Definition
block_cache.h:33
block_cache_t::lock
mutex_t lock
Definition
block_cache.h:40
block_cache_t::backing
block_device_t * backing
Definition
block_cache.h:34
block_cache_t::use_counter
uint32_t use_counter
Definition
block_cache.h:37
block_cache_t::slots
block_cache_slot_t slots[BLOCK_CACHE_SETS][BLOCK_CACHE_WAYS]
Definition
block_cache.h:35
block_cache_t::data
uint8_t * data
Definition
block_cache.h:36
block_cache_t::hits
uint32_t hits
Definition
block_cache.h:38
block_cache_t::misses
uint32_t misses
Definition
block_cache.h:39
mutex_t
sleeping mutex for long critical sections in process context (filesystem and disk I/O).
Definition
mutex.h:20
src
kernel
fs
block_cache.h
Generated by
1.18.0