The state framework grew organically over the time. Unfortunately the architecture and abstractions disappeared during this period. This patch refactors the framework to recreate the abstractions. The main focus was the backend with its storage. The main use-case was to offer better NAND support with less erase cycles and interchangeable data formats (dtb,raw). The general architecture now has a backend which consists of a data format and storage. The storage consists of multiple storage buckets each holding exactly one copy of the state data. A data format describes a data serialization for the state framework. This can be either dtb or raw. A storage bucket is a storage location which is used to store any data. There is a (new) circular type which writes changes behind the last written data and therefore reduces the number of erases. The other type is a direct bucket which writes directly to a storage offset for all non-erase storage. Furthermore this patch splits up all classes into different files in a subdirectory. This is currently all in one patch as I can't see a good way to split the changes up without having a non-working state framework in between. The following diagram shows the new architecture roughly: .----------. | state | '----------' | | v .----------------------------. | state_backend | |----------------------------| | + state_load(*state); | | + state_save(*state); | | + state_backend_init(...); | | | | | '----------------------------' | | The format describes | | how the state data | '-------------> is serialized | .--------------------------------------------. | | state_backend_format <INTERFACE> | | |--------------------------------------------| | | + verify(*format, magic, *buf, len); | | | + pack(*format, *state, **buf, len); | | | + unpack(*format, *state, *buf, len); | | | + get_packed_len(*format, *state); | | | + free(*format); | | '--------------------------------------------' | ^ ^ | * * | * * | .--------------------. .--------------------. | | backend_format_dtb | | backend_format_raw | | '--------------------' '--------------------' | | | v .----------------------------------------------------------. | state_backend_storage | |----------------------------------------------------------| | + init(...); | | + free(*storage); | | + read(*storage, *format, magic, **buf, *len, len_hint); | | + write(*storage, *buf, len); | | + restore_consistency(*storage, *buf, len); | '----------------------------------------------------------' | The backend storage is responsible to manage multiple data copies and distribute them onto several buckets. Read data is verified against the given format to ensure that the read data is correct. | | | | | v .------------------------------------------. | state_backend_storage_bucket <INTERFACE> | |------------------------------------------| | + init(*bucket); | | + write(*bucket, *buf, len); | | + read(*bucket, **buf, len_hint); | | + free(*bucket); | '------------------------------------------' ^ ^ ^ * * * * * * A storage bucket represents*exactly one data copy at one data location. A circular b*cket writes any new data to the end of the bucket (for *educed erases on NAND). A direct bucket directly writ*s at one location. * * * * * * * * * .-----------------------. * .-------------------------. | backend_bucket_direct | * | backend_bucket_circular | '-----------------------' * '-------------------------' ^ * ^ | * | | * | | * | | .-----------------------. | '--| backend_bucket_cached |---' '-----------------------' A backend_bucket_cached is a transparent bucket that directly uses another bucket as backend device and caches all accesses. Signed-off-by: Markus Pargmann <mpa@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
156 lines
3.6 KiB
C
156 lines
3.6 KiB
C
/*
|
|
* Copyright (C) 2016 Pengutronix, Markus Pargmann <mpa@pengutronix.de>
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* version 2, as published by the Free Software Foundation.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
*/
|
|
|
|
#include <common.h>
|
|
#include "state.h"
|
|
|
|
struct state_backend_storage_bucket_cache {
|
|
struct state_backend_storage_bucket bucket;
|
|
|
|
struct state_backend_storage_bucket *raw;
|
|
|
|
u8 *data;
|
|
ssize_t data_len;
|
|
bool force_write;
|
|
|
|
/* For outputs */
|
|
struct device_d *dev;
|
|
};
|
|
|
|
static inline struct state_backend_storage_bucket_cache
|
|
*get_bucket_cache(struct state_backend_storage_bucket *bucket)
|
|
{
|
|
return container_of(bucket,
|
|
struct state_backend_storage_bucket_cache,
|
|
bucket);
|
|
}
|
|
|
|
static inline void state_backend_bucket_cache_drop(
|
|
struct state_backend_storage_bucket_cache *cache)
|
|
{
|
|
if (cache->data) {
|
|
free(cache->data);
|
|
cache->data = NULL;
|
|
cache->data_len = 0;
|
|
}
|
|
}
|
|
|
|
static int state_backend_bucket_cache_fill(
|
|
struct state_backend_storage_bucket_cache *cache)
|
|
{
|
|
int ret;
|
|
|
|
ret = cache->raw->read(cache->raw, &cache->data, &cache->data_len);
|
|
if (ret == -EUCLEAN)
|
|
cache->force_write = true;
|
|
else if (ret)
|
|
return ret;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int state_backend_bucket_cache_read(struct state_backend_storage_bucket *bucket,
|
|
uint8_t ** buf_out,
|
|
ssize_t * len_hint)
|
|
{
|
|
struct state_backend_storage_bucket_cache *cache =
|
|
get_bucket_cache(bucket);
|
|
int ret;
|
|
|
|
if (!cache->data) {
|
|
ret = state_backend_bucket_cache_fill(cache);
|
|
if (ret)
|
|
return ret;
|
|
}
|
|
|
|
if (cache->data) {
|
|
*buf_out = xmemdup(cache->data, cache->data_len);
|
|
if (!*buf_out)
|
|
return -ENOMEM;
|
|
*len_hint = cache->data_len;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int state_backend_bucket_cache_write(struct state_backend_storage_bucket *bucket,
|
|
const uint8_t * buf, ssize_t len)
|
|
{
|
|
struct state_backend_storage_bucket_cache *cache =
|
|
get_bucket_cache(bucket);
|
|
int ret;
|
|
|
|
if (!cache->force_write) {
|
|
if (!cache->data)
|
|
ret = state_backend_bucket_cache_fill(cache);
|
|
|
|
if (cache->data_len == len && !memcmp(cache->data, buf, len))
|
|
return 0;
|
|
}
|
|
|
|
state_backend_bucket_cache_drop(cache);
|
|
|
|
ret = cache->raw->write(cache->raw, buf, len);
|
|
if (ret)
|
|
return ret;
|
|
|
|
cache->data = xmemdup(buf, len);
|
|
cache->data_len = len;
|
|
return 0;
|
|
}
|
|
|
|
static int state_backend_bucket_cache_init(
|
|
struct state_backend_storage_bucket *bucket)
|
|
{
|
|
struct state_backend_storage_bucket_cache *cache =
|
|
get_bucket_cache(bucket);
|
|
|
|
if (cache->raw->init) {
|
|
return cache->raw->init(cache->raw);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void state_backend_bucket_cache_free(
|
|
struct state_backend_storage_bucket *bucket)
|
|
{
|
|
struct state_backend_storage_bucket_cache *cache =
|
|
get_bucket_cache(bucket);
|
|
|
|
state_backend_bucket_cache_drop(cache);
|
|
cache->raw->free(cache->raw);
|
|
free(cache);
|
|
}
|
|
|
|
int state_backend_bucket_cached_create(struct device_d *dev,
|
|
struct state_backend_storage_bucket *raw,
|
|
struct state_backend_storage_bucket **out)
|
|
{
|
|
struct state_backend_storage_bucket_cache *cache;
|
|
|
|
cache = xzalloc(sizeof(*cache));
|
|
cache->raw = raw;
|
|
cache->dev = dev;
|
|
|
|
cache->bucket.free = state_backend_bucket_cache_free;
|
|
cache->bucket.read = state_backend_bucket_cache_read;
|
|
cache->bucket.write = state_backend_bucket_cache_write;
|
|
cache->bucket.init = state_backend_bucket_cache_init;
|
|
|
|
*out = &cache->bucket;
|
|
|
|
return 0;
|
|
}
|