The manual
ginnOS
The reference: libc, the libraries and the kernel
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"
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
24typedef struct
25{
27 uint32_t last_use; // value of the use counter at the last access
28 bool valid;
30
31typedef 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
36 uint8_t *data; // the sectors, slot by slot
38 uint32_t hits; // sectors served from memory
39 uint32_t misses; // sectors read from the device
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 */
50
51/**
52 * the cache that serves the root filesystem, for /proc/meminfo (NULL
53 * before it is set up).
54 */
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_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
#define BLOCK_CACHE_WAYS
Definition block_cache.h:21
#define BLOCK_CACHE_SETS
Definition block_cache.h:20
struct block_device block_device_t
Definition block_device.h:5
kerr_t
unified kernel error codes.
Definition error.h:14
unsigned int uint32_t
Definition fb.h:17
unsigned char uint8_t
Definition fb.h:18
Definition block_cache.h:25
uint32_t last_use
Definition block_cache.h:27
uint32_t lba
Definition block_cache.h:26
bool valid
Definition block_cache.h:28
Definition block_cache.h:32
block_device_t block
Definition block_cache.h:33
mutex_t lock
Definition block_cache.h:40
block_device_t * backing
Definition block_cache.h:34
uint32_t use_counter
Definition block_cache.h:37
block_cache_slot_t slots[BLOCK_CACHE_SETS][BLOCK_CACHE_WAYS]
Definition block_cache.h:35
uint8_t * data
Definition block_cache.h:36
uint32_t hits
Definition block_cache.h:38
uint32_t misses
Definition block_cache.h:39
sleeping mutex for long critical sections in process context (filesystem and disk I/O).
Definition mutex.h:20