The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
block_device.h
Go to the documentation of this file.
1#pragma once
2
3#include "common/stdint.h"
4
6
7typedef bool (*BlockReadFn)(block_device_t *device, uint32_t startBlock, uint8_t blockCount,
8 void *dest);
9typedef bool (*BlockWriteFn)(block_device_t *device, uint32_t startBlock, uint8_t blockCount,
10 const void *src);
12typedef bool (*BlockTrimFn)(block_device_t *device, uint32_t startBlock, uint32_t blockCount);
13
14/**
15 * block device abstraction for reading blocks from a storage device.
16 */
18{
19 uint16_t bytes_per_block; // number of bytes in each block (sector) of the device.
20 uint32_t total_blocks; // total addressable blocks on the device (0 if unknown).
21 void *context; // pointer to device-specific context data (e.g., ATA device structure).
22 BlockReadFn read_blocks; // function pointer to the block read function for the device.
23 BlockWriteFn write_blocks; // function pointer to the block write function for the device.
24 BlockFlushFn flush; // optional: push cached writes to media, or NULL if not cached.
25 BlockTrimFn trim; // optional: hint that blocks are unused (SSD/virtual), or NULL.
26};
27
28/**
29 * read blocks from a block device.
30 * @param device initialized block device backend.
31 * @param startBlock starting block number to read from.
32 * @param blockCount number of blocks to read.
33 * @param dest destination buffer to store the read data.
34 * @return true on success. false on failure.
35 */
36bool block_device_read(block_device_t *device, uint32_t startBlock, uint8_t blockCount, void *dest);
37
38/**
39 * write blocks to a block device.
40 * @param device initialized block device backend.
41 * @param startBlock starting block number to write to.
42 * @param blockCount number of blocks to write.
43 * @param src source buffer containing the data to write.
44 * @return true on success. false on failure.
45 */
46bool block_device_write(block_device_t *device, uint32_t startBlock, uint8_t blockCount,
47 const void *src);
48
49/**
50 * flush any cached writes to the underlying media.
51 * devices without a write-back cache leave the flush op NULL, this wrapper
52 * treats that as a successful no-op so callers can flush unconditionally.
53 * @param device initialized block device backend.
54 * @return true on success (including "nothing to flush"), false on I/O error.
55 */
57
58/**
59 * hint that a range of blocks is no longer in use (TRIM/discard).
60 * this is only an optimization for SSDs and virtual disks, devices that do
61 * not support it leave the trim op NULL and this wrapper is a no-op success.
62 * @param device initialized block device backend.
63 * @param startBlock first block in the range.
64 * @param blockCount number of blocks in the range.
65 * @return true on success (including "not supported"), false on error.
66 */
67bool block_device_trim(block_device_t *device, uint32_t startBlock, uint32_t blockCount);
bool block_device_read(block_device_t *device, uint32_t startBlock, uint8_t blockCount, void *dest)
read blocks from a block device.
Definition block_device.c:3
bool block_device_trim(block_device_t *device, uint32_t startBlock, uint32_t blockCount)
hint that a range of blocks is no longer in use (TRIM/discard).
Definition block_device.c:40
bool block_device_write(block_device_t *device, uint32_t startBlock, uint8_t blockCount, const void *src)
write blocks to a block device.
Definition block_device.c:13
bool block_device_flush(block_device_t *device)
flush any cached writes to the underlying media.
Definition block_device.c:24
struct block_device block_device_t
Definition block_device.h:5
bool(*) BlockReadFn(block_device_t *device, uint32_t startBlock, uint8_t blockCount, void *dest)
Definition block_device.h:7
bool(*) BlockTrimFn(block_device_t *device, uint32_t startBlock, uint32_t blockCount)
Definition block_device.h:12
bool(*) BlockWriteFn(block_device_t *device, uint32_t startBlock, uint8_t blockCount, const void *src)
Definition block_device.h:9
bool(*) BlockFlushFn(block_device_t *device)
Definition block_device.h:11
unsigned int uint32_t
Definition fb.h:17
unsigned char uint8_t
Definition fb.h:18
unsigned short uint16_t
Definition stdint.h:13
#define bool
Definition stdbool.h:8
block device abstraction for reading blocks from a storage device.
Definition block_device.h:18
BlockFlushFn flush
Definition block_device.h:24
BlockTrimFn trim
Definition block_device.h:25
uint32_t total_blocks
Definition block_device.h:20
BlockReadFn read_blocks
Definition block_device.h:22
uint16_t bytes_per_block
Definition block_device.h:19
void * context
Definition block_device.h:21
BlockWriteFn write_blocks
Definition block_device.h:23
a registered device.
Definition device.h:114