Zephyr API Documentation 4.5.0-rc1
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
zms.h
Go to the documentation of this file.
1/* Copyright (c) 2018 Laczen
2 * Copyright (c) 2024 BayLibre SAS
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * ZMS: Zephyr Memory Storage
7 */
8
14
15#ifndef ZEPHYR_INCLUDE_KVSS_ZMS_H_
16#define ZEPHYR_INCLUDE_KVSS_ZMS_H_
17
18#include <sys/types.h>
20#include <zephyr/kernel.h>
21#include <zephyr/device.h>
22#include <zephyr/toolchain.h>
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
36
42
53
59#if CONFIG_ZMS_ID_64BIT
60typedef uint64_t zms_id_t;
61#else
63#endif
64
74#if CONFIG_ZMS_ID_64BIT
75#define ZMS_DATA_IN_ATE_SIZE 4
76#else
77#define ZMS_DATA_IN_ATE_SIZE 8
78#endif
79
81#define ZMS_ITER_ID_MIN ((zms_id_t)0)
82
84#define ZMS_ITER_ID_MAX ((zms_id_t)~(zms_id_t)0)
85
87#define ZMS_ITER_MASK_ALL ZMS_ITER_ID_MAX
88
91
93struct zms_fs {
115 bool ready;
121 const struct device *flash_device;
125 size_t ate_size;
126#if CONFIG_ZMS_LOOKUP_CACHE
127#if CONFIG_ZMS_LOOKUP_CACHE_MANUAL
129 uint64_t *lookup_cache;
131 size_t lookup_cache_size;
132#else
134 uint64_t lookup_cache[CONFIG_ZMS_LOOKUP_CACHE_SIZE];
135#endif
136#endif
137};
138
162
164#define ZMS_ITER_CONFIG_DEFAULT \
165 { \
166 .mask_id = ZMS_ITER_MASK_ALL, \
167 .min_id = ZMS_ITER_ID_MIN, \
168 .max_id = ZMS_ITER_ID_MAX, \
169 .use_mask = false, \
170 .use_range = false, \
171 .use_predicate = false, \
172 .predicate_func = NULL, \
173 }
174
202
206
212
230int zms_mount(struct zms_fs *fs);
231
245int zms_mount_force(struct zms_fs *fs);
246
259int zms_clear(struct zms_fs *fs);
260
284ssize_t zms_write(struct zms_fs *fs, zms_id_t id, const void *data, size_t len);
285
298int zms_delete(struct zms_fs *fs, zms_id_t id);
299
317ssize_t zms_read(struct zms_fs *fs, zms_id_t id, void *data, size_t len);
318
338ssize_t zms_read_hist(struct zms_fs *fs, zms_id_t id, void *data, size_t len, uint32_t cnt);
339
355
371
383
401
402#if CONFIG_ZMS_LOOKUP_CACHE_MANUAL
417int zms_set_lookup_cache(struct zms_fs *fs, uint64_t *buffer, size_t size);
418#endif
419
434int zms_get_num_cycles(struct zms_fs *fs, uint32_t *cycles);
435
448int zms_get_sector_num_cycles(struct zms_fs *fs, uint32_t sector, uint32_t *cycles);
449
481int zms_iter_init_with_config(const struct zms_fs *fs, struct zms_iter *iter,
482 const struct zms_iter_config *config);
483
500int zms_iter_init(const struct zms_fs *fs, struct zms_iter *iter);
501
540int zms_iter_next(struct zms_fs *fs, struct zms_iter *iter, zms_id_t *id, size_t *len, void *data,
541 size_t data_len);
542
585int zms_iter_next_all(struct zms_fs *fs, struct zms_iter *iter, zms_id_t *id, size_t *len,
586 void *data, size_t data_len);
587
591
592#ifdef __cplusplus
593}
594#endif
595
596#endif /* ZEPHYR_INCLUDE_KVSS_ZMS_H_ */
APIs and macros for the Zephyr device model.
Main header file for Flash driver API.
#define BIT(n)
Unsigned integer with bit position n set (signed in assembly language).
Definition util_macro.h:44
uint32_t zms_id_t
ID type used in the ZMS API.
Definition zms.h:62
zms_mount_flags
Mount options for zms_mount and zms_mount_force.
Definition zms.h:44
bool(* zms_iter_predicate_t)(zms_id_t id)
Iterator predicate callback type used by zms_iter_init_with_config.
Definition zms.h:90
@ ZMS_MOUNT_FLAG_NO_FORMAT
Do not format erased media during mount.
Definition zms.h:51
ssize_t zms_calc_free_space(struct zms_fs *fs)
Calculate the available free space in the file system.
ssize_t zms_write(struct zms_fs *fs, zms_id_t id, const void *data, size_t len)
Write an entry to the file system.
int zms_iter_init_with_config(const struct zms_fs *fs, struct zms_iter *iter, const struct zms_iter_config *config)
Initialise a ZMS iterator with filtering configuration.
int zms_get_num_cycles(struct zms_fs *fs, uint32_t *cycles)
Return the maximum sector recycle count across all sectors.
int zms_get_sector_num_cycles(struct zms_fs *fs, uint32_t sector, uint32_t *cycles)
Return the recycle count for a specific sector.
int zms_delete(struct zms_fs *fs, zms_id_t id)
Delete an entry from the file system.
ssize_t zms_read_hist(struct zms_fs *fs, zms_id_t id, void *data, size_t len, uint32_t cnt)
Read a history entry from the file system.
ssize_t zms_active_sector_free_space(struct zms_fs *fs)
Tell how much contiguous free space remains in the currently active ZMS sector.
int zms_iter_init(const struct zms_fs *fs, struct zms_iter *iter)
Initialise a ZMS iterator.
ssize_t zms_get_data_length(struct zms_fs *fs, zms_id_t id)
Gets the length of the data that is stored in an entry with a given id.
ssize_t zms_read(struct zms_fs *fs, zms_id_t id, void *data, size_t len)
Read an entry from the file system.
int zms_sector_use_next(struct zms_fs *fs)
Close the currently active sector and switch to the next one.
int zms_mount(struct zms_fs *fs)
Mount a ZMS file system onto the device specified in fs.
int zms_iter_next_all(struct zms_fs *fs, struct zms_iter *iter, zms_id_t *id, size_t *len, void *data, size_t data_len)
Advance the iterator to the next matching ATE.
int zms_mount_force(struct zms_fs *fs)
Mount a ZMS file system onto the device specified in fs, wiping the partition if mounting fails the f...
int zms_iter_next(struct zms_fs *fs, struct zms_iter *iter, zms_id_t *id, size_t *len, void *data, size_t data_len)
Advance the iterator to the next live entry.
int zms_clear(struct zms_fs *fs)
Clear the ZMS file system from device.
Public kernel APIs.
__SIZE_TYPE__ ssize_t
Definition types.h:28
__INTPTR_TYPE__ off_t
Definition types.h:36
#define bool
Definition stdbool.h:13
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
Runtime device structure (in ROM) per driver instance.
Definition device.h:543
Kernel mutex structure.
Definition kernel.h:3500
Zephyr Memory Storage file system structure.
Definition zms.h:93
const struct flash_parameters * flash_parameters
Flash memory parameters structure.
Definition zms.h:123
uint64_t data_wra
Data write address.
Definition zms.h:103
const struct device * flash_device
Flash device runtime structure.
Definition zms.h:121
bool lock_initialized
Flag indicating that zms_lock has been initialized.
Definition zms.h:117
uint32_t mount_flags
Mount behavior flags from zms_mount_flags.
Definition zms.h:111
uint64_t ate_wra
Allocation Table Entry (ATE) write address.
Definition zms.h:101
size_t ate_size
Size of an Allocation Table Entry.
Definition zms.h:125
uint32_t sector_size
Storage system is split into sectors.
Definition zms.h:107
uint8_t sector_cycle
Current cycle counter of the active sector (pointed to by ate_wra).
Definition zms.h:113
uint32_t sector_count
Number of sectors in the file system.
Definition zms.h:109
off_t offset
File system offset in flash.
Definition zms.h:95
bool ready
Flag indicating if the file system is initialized.
Definition zms.h:115
struct k_mutex zms_lock
Mutex used to lock flash writes.
Definition zms.h:119
Iterator configuration for zms_iter_init_with_config.
Definition zms.h:146
bool use_range
Enable inclusive range-based filtering.
Definition zms.h:156
zms_iter_predicate_t predicate_func
Predicate callback used when use_predicate is true.
Definition zms.h:160
bool use_predicate
Enable callback-based filtering.
Definition zms.h:158
zms_id_t min_id
Minimum accepted ID when use_range is true.
Definition zms.h:150
bool use_mask
Enable mask-based filtering.
Definition zms.h:154
zms_id_t max_id
Maximum accepted ID when use_range is true.
Definition zms.h:152
zms_id_t mask_id
Mask applied when use_mask is true.
Definition zms.h:148
Opaque iterator state for zms_iter_next and zms_iter_next_all.
Definition zms.h:182
zms_iter_predicate_t predicate_func
Optional callback used to filter IDs.
Definition zms.h:194
zms_id_t max_id
Inclusive maximum ID accepted by the iterator.
Definition zms.h:192
int previous_sector_num
Sector of the last ATE seen, cached to avoid re-reading the cycle below every step.
Definition zms.h:198
zms_id_t mask_id
ID mask used to filter returned entries.
Definition zms.h:188
uint8_t current_cycle
Cycle count of the sector identified by previous_sector_num.
Definition zms.h:200
uint64_t end_addr
Snapshot of ate_wra taken at iterator initialization time.
Definition zms.h:186
zms_id_t min_id
Inclusive minimum ID accepted by the iterator.
Definition zms.h:190
uint64_t walk_addr
Current walk address in the ATE ring.
Definition zms.h:184
bool exhausted
True once the walk has completed the full ring.
Definition zms.h:196
Macros to abstract toolchain specific capabilities.