summaryrefslogtreecommitdiffstats
path: root/common/state/backend_bucket_direct.c
diff options
context:
space:
mode:
authorMarkus Pargmann <mpa@pengutronix.de>2016-07-06 10:19:43 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2016-07-08 08:59:31 +0200
commitc999b507da9891f22cf2a60105bffa0774eea082 (patch)
treedbc1a712b3ab6919758c1cffc6f979ba8c98d59f /common/state/backend_bucket_direct.c
parent3d33f178ccd7b0602b20c8fb37d7e57beed22e89 (diff)
downloadbarebox-c999b507da9891f22cf2a60105bffa0774eea082.tar.gz
barebox-c999b507da9891f22cf2a60105bffa0774eea082.tar.xz
state: Refactor state framework
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>
Diffstat (limited to 'common/state/backend_bucket_direct.c')
-rw-r--r--common/state/backend_bucket_direct.c180
1 files changed, 180 insertions, 0 deletions
diff --git a/common/state/backend_bucket_direct.c b/common/state/backend_bucket_direct.c
new file mode 100644
index 0000000000..08892f001e
--- /dev/null
+++ b/common/state/backend_bucket_direct.c
@@ -0,0 +1,180 @@
+/*
+ * 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 <fcntl.h>
+#include <fs.h>
+#include <libfile.h>
+#include <linux/kernel.h>
+#include <malloc.h>
+#include <printk.h>
+
+#include "state.h"
+
+struct state_backend_storage_bucket_direct {
+ struct state_backend_storage_bucket bucket;
+
+ ssize_t offset;
+ ssize_t max_size;
+
+ int fd;
+
+ struct device_d *dev;
+};
+
+struct state_backend_storage_bucket_direct_meta {
+ uint32_t magic;
+ uint32_t written_length;
+};
+static const uint32_t direct_magic = 0x2354fdf3;
+
+static inline struct state_backend_storage_bucket_direct
+ *get_bucket_direct(struct state_backend_storage_bucket *bucket)
+{
+ return container_of(bucket, struct state_backend_storage_bucket_direct,
+ bucket);
+}
+
+static int state_backend_bucket_direct_read(struct state_backend_storage_bucket
+ *bucket, uint8_t ** buf_out,
+ ssize_t * len_hint)
+{
+ struct state_backend_storage_bucket_direct *direct =
+ get_bucket_direct(bucket);
+ struct state_backend_storage_bucket_direct_meta meta;
+ ssize_t read_len;
+ uint8_t *buf;
+ int ret;
+
+ ret = lseek(direct->fd, direct->offset, SEEK_SET);
+ if (ret < 0) {
+ dev_err(direct->dev, "Failed to seek file, %d\n", ret);
+ return ret;
+ }
+ ret = read_full(direct->fd, &meta, sizeof(meta));
+ if (ret < 0) {
+ dev_err(direct->dev, "Failed to read meta data from file, %d\n", ret);
+ return ret;
+ }
+ if (meta.magic == direct_magic) {
+ read_len = meta.written_length;
+ } else {
+ if (*len_hint)
+ read_len = *len_hint;
+ else
+ read_len = direct->max_size;
+ ret = lseek(direct->fd, direct->offset, SEEK_SET);
+ if (ret < 0) {
+ dev_err(direct->dev, "Failed to seek file, %d\n", ret);
+ return ret;
+ }
+ }
+ if (direct->max_size)
+ read_len = min(read_len, direct->max_size);
+
+ buf = xmalloc(read_len);
+ if (!buf)
+ return -ENOMEM;
+
+ ret = read_full(direct->fd, buf, read_len);
+ if (ret < 0) {
+ dev_err(direct->dev, "Failed to read from file, %d\n", ret);
+ free(buf);
+ return ret;
+ }
+
+ *buf_out = buf;
+ *len_hint = read_len;
+
+ return 0;
+}
+
+static int state_backend_bucket_direct_write(struct state_backend_storage_bucket
+ *bucket, const uint8_t * buf,
+ ssize_t len)
+{
+ struct state_backend_storage_bucket_direct *direct =
+ get_bucket_direct(bucket);
+ int ret;
+ struct state_backend_storage_bucket_direct_meta meta;
+
+ if (direct->max_size && len > direct->max_size)
+ return -E2BIG;
+
+ ret = lseek(direct->fd, direct->offset, SEEK_SET);
+ if (ret < 0) {
+ dev_err(direct->dev, "Failed to seek file, %d\n", ret);
+ return ret;
+ }
+
+ meta.magic = direct_magic;
+ meta.written_length = len;
+ ret = write_full(direct->fd, &meta, sizeof(meta));
+ if (ret < 0) {
+ dev_err(direct->dev, "Failed to write metadata to file, %d\n", ret);
+ return ret;
+ }
+
+ ret = write_full(direct->fd, buf, len);
+ if (ret < 0) {
+ dev_err(direct->dev, "Failed to write file, %d\n", ret);
+ return ret;
+ }
+
+ ret = flush(direct->fd);
+ if (ret < 0) {
+ dev_err(direct->dev, "Failed to flush file, %d\n", ret);
+ return ret;
+ }
+
+ return 0;
+}
+
+static void state_backend_bucket_direct_free(struct
+ state_backend_storage_bucket
+ *bucket)
+{
+ struct state_backend_storage_bucket_direct *direct =
+ get_bucket_direct(bucket);
+
+ close(direct->fd);
+ free(direct);
+}
+
+int state_backend_bucket_direct_create(struct device_d *dev, const char *path,
+ struct state_backend_storage_bucket **bucket,
+ off_t offset, ssize_t max_size)
+{
+ int fd;
+ struct state_backend_storage_bucket_direct *direct;
+
+ fd = open(path, O_RDWR);
+ if (fd < 0) {
+ dev_err(dev, "Failed to open file '%s', %d\n", path, -errno);
+ close(fd);
+ return -errno;
+ }
+
+ direct = xzalloc(sizeof(*direct));
+ direct->offset = offset;
+ direct->max_size = max_size;
+ direct->fd = fd;
+ direct->dev = dev;
+
+ direct->bucket.read = state_backend_bucket_direct_read;
+ direct->bucket.write = state_backend_bucket_direct_write;
+ direct->bucket.free = state_backend_bucket_direct_free;
+ *bucket = &direct->bucket;
+
+ return 0;
+}