2019-07-13 12:26:43 +08:00
|
|
|
#include <sys/stat.h>
|
2016-02-08 01:30:33 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "rmtfs.h"
|
|
|
|
|
|
|
|
#define MAX_CALLERS 10
|
2019-07-13 12:26:43 +08:00
|
|
|
#define STORAGE_MAX_SIZE (16 * 1024 * 1024)
|
|
|
|
|
2019-07-18 03:21:05 +08:00
|
|
|
#define BY_PARTLABEL_PATH "/dev/disk/by-partlabel"
|
|
|
|
|
2019-07-13 12:26:43 +08:00
|
|
|
#define MIN(x, y) ((x) < (y) ? (x) : (y))
|
2016-02-08 01:30:33 +08:00
|
|
|
|
|
|
|
struct partition {
|
|
|
|
const char *path;
|
|
|
|
const char *actual;
|
2019-07-18 03:21:05 +08:00
|
|
|
const char *partlabel;
|
2016-02-08 01:30:33 +08:00
|
|
|
};
|
|
|
|
|
2019-07-13 11:26:48 +08:00
|
|
|
struct rmtfd {
|
2016-02-08 01:30:33 +08:00
|
|
|
unsigned id;
|
|
|
|
unsigned node;
|
|
|
|
int fd;
|
|
|
|
unsigned dev_error;
|
2016-11-22 03:12:12 +08:00
|
|
|
const struct partition *partition;
|
2019-07-13 12:26:43 +08:00
|
|
|
|
|
|
|
void *shadow_buf;
|
|
|
|
size_t shadow_len;
|
2016-02-08 01:30:33 +08:00
|
|
|
};
|
|
|
|
|
2018-12-08 08:03:43 +08:00
|
|
|
static const char *storage_dir = "/boot";
|
2019-07-13 12:26:43 +08:00
|
|
|
static int storage_read_only;
|
2019-07-18 03:21:05 +08:00
|
|
|
static int storage_use_partitions;
|
2018-12-08 08:03:43 +08:00
|
|
|
|
2016-02-08 01:30:33 +08:00
|
|
|
static const struct partition partition_table[] = {
|
2019-07-18 03:21:05 +08:00
|
|
|
{ "/boot/modem_fs1", "modem_fs1", "modemst1" },
|
|
|
|
{ "/boot/modem_fs2", "modem_fs2", "modemst2" },
|
|
|
|
{ "/boot/modem_fsc", "modem_fsc", "fsc" },
|
|
|
|
{ "/boot/modem_fsg", "modem_fsg", "fsg" },
|
2020-11-12 04:06:52 +08:00
|
|
|
{ "/boot/modem_tunning", "modem_tunning", "tunning" },
|
2016-02-08 01:30:33 +08:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2019-07-13 11:26:48 +08:00
|
|
|
static struct rmtfd rmtfds[MAX_CALLERS];
|
2016-02-08 01:30:33 +08:00
|
|
|
|
2019-07-13 12:26:43 +08:00
|
|
|
static int storage_populate_shadow_buf(struct rmtfd *rmtfd, const char *file);
|
|
|
|
|
2019-07-18 03:21:05 +08:00
|
|
|
int storage_init(const char *storage_root, bool read_only, bool use_partitions)
|
2016-02-08 01:30:33 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2018-12-08 08:03:43 +08:00
|
|
|
if (storage_root)
|
|
|
|
storage_dir = storage_root;
|
|
|
|
|
2019-07-18 03:21:05 +08:00
|
|
|
if (use_partitions) {
|
2020-02-24 15:53:55 +08:00
|
|
|
if (!storage_root)
|
|
|
|
storage_dir = BY_PARTLABEL_PATH;
|
2019-07-18 03:21:05 +08:00
|
|
|
storage_use_partitions = true;
|
|
|
|
}
|
|
|
|
|
2019-07-13 12:26:43 +08:00
|
|
|
storage_read_only = read_only;
|
|
|
|
|
2016-02-08 01:30:33 +08:00
|
|
|
for (i = 0; i < MAX_CALLERS; i++) {
|
2019-07-13 11:26:48 +08:00
|
|
|
rmtfds[i].id = i;
|
|
|
|
rmtfds[i].fd = -1;
|
2019-07-29 04:11:53 +08:00
|
|
|
rmtfds[i].shadow_buf = NULL;
|
2016-02-08 01:30:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-13 11:48:12 +08:00
|
|
|
struct rmtfd *storage_open(unsigned node, const char *path)
|
2016-02-08 01:30:33 +08:00
|
|
|
{
|
2018-12-08 08:03:43 +08:00
|
|
|
char *fspath;
|
2016-02-08 01:30:33 +08:00
|
|
|
const struct partition *part;
|
2019-07-13 11:26:48 +08:00
|
|
|
struct rmtfd *rmtfd = NULL;
|
2019-07-18 03:21:05 +08:00
|
|
|
const char *file;
|
2018-12-08 08:03:43 +08:00
|
|
|
size_t pathlen;
|
2016-02-08 01:30:33 +08:00
|
|
|
int saved_errno;
|
2019-07-13 12:26:43 +08:00
|
|
|
int ret;
|
2016-02-08 01:30:33 +08:00
|
|
|
int fd;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (part = partition_table; part->path; part++) {
|
|
|
|
if (strcmp(part->path, path) == 0)
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "[RMTFS storage] request for unknown partition '%s', rejecting\n", path);
|
2019-07-13 11:48:12 +08:00
|
|
|
return NULL;
|
2016-02-08 01:30:33 +08:00
|
|
|
|
|
|
|
found:
|
2016-11-22 03:12:12 +08:00
|
|
|
/* Check if this node already has the requested path open */
|
|
|
|
for (i = 0; i < MAX_CALLERS; i++) {
|
2019-07-29 04:11:53 +08:00
|
|
|
if ((rmtfds[i].fd != -1 || rmtfds[i].shadow_buf) &&
|
2019-07-13 11:26:48 +08:00
|
|
|
rmtfds[i].node == node &&
|
|
|
|
rmtfds[i].partition == part)
|
2019-07-13 11:48:12 +08:00
|
|
|
return &rmtfds[i];
|
2016-11-22 03:12:12 +08:00
|
|
|
}
|
|
|
|
|
2016-02-08 01:30:33 +08:00
|
|
|
for (i = 0; i < MAX_CALLERS; i++) {
|
2019-07-29 04:11:53 +08:00
|
|
|
if (rmtfds[i].fd == -1 && !rmtfds[i].shadow_buf) {
|
2019-07-13 11:26:48 +08:00
|
|
|
rmtfd = &rmtfds[i];
|
2016-02-08 01:30:33 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-07-13 11:26:48 +08:00
|
|
|
if (!rmtfd) {
|
|
|
|
fprintf(stderr, "[storage] out of free rmtfd handles\n");
|
2019-07-13 11:48:12 +08:00
|
|
|
return NULL;
|
2016-02-08 01:30:33 +08:00
|
|
|
}
|
|
|
|
|
2019-07-18 03:21:05 +08:00
|
|
|
if (storage_use_partitions)
|
|
|
|
file = part->partlabel;
|
|
|
|
else
|
|
|
|
file = part->actual;
|
|
|
|
|
|
|
|
pathlen = strlen(storage_dir) + strlen(file) + 2;
|
2018-12-08 08:03:43 +08:00
|
|
|
fspath = alloca(pathlen);
|
2019-07-18 03:21:05 +08:00
|
|
|
snprintf(fspath, pathlen, "%s/%s", storage_dir, file);
|
2019-07-13 12:26:43 +08:00
|
|
|
if (!storage_read_only) {
|
2021-07-09 06:01:45 +08:00
|
|
|
fd = open(fspath, O_RDWR);
|
2019-07-13 12:26:43 +08:00
|
|
|
if (fd < 0) {
|
|
|
|
saved_errno = errno;
|
|
|
|
fprintf(stderr, "[storage] failed to open '%s' (requested '%s'): %s\n",
|
|
|
|
fspath, part->path, strerror(saved_errno));
|
|
|
|
errno = saved_errno;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
rmtfd->fd = fd;
|
|
|
|
rmtfd->shadow_len = 0;
|
|
|
|
} else {
|
|
|
|
ret = storage_populate_shadow_buf(rmtfd, fspath);
|
|
|
|
if (ret < 0) {
|
|
|
|
saved_errno = errno;
|
|
|
|
fprintf(stderr, "[storage] failed to open '%s' (requested '%s'): %s\n",
|
|
|
|
fspath, part->path, strerror(saved_errno));
|
|
|
|
errno = saved_errno;
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-02-08 01:30:33 +08:00
|
|
|
}
|
|
|
|
|
2019-07-13 11:26:48 +08:00
|
|
|
rmtfd->node = node;
|
|
|
|
rmtfd->partition = part;
|
2016-02-08 01:30:33 +08:00
|
|
|
|
2019-07-13 11:48:12 +08:00
|
|
|
return rmtfd;
|
2016-02-08 01:30:33 +08:00
|
|
|
}
|
|
|
|
|
2019-07-13 11:48:12 +08:00
|
|
|
void storage_close(struct rmtfd *rmtfd)
|
2016-02-08 01:30:33 +08:00
|
|
|
{
|
2020-12-08 00:43:18 +08:00
|
|
|
if (rmtfd->fd >= 0) {
|
|
|
|
close(rmtfd->fd);
|
|
|
|
rmtfd->fd = -1;
|
|
|
|
}
|
2019-07-13 12:26:43 +08:00
|
|
|
|
|
|
|
free(rmtfd->shadow_buf);
|
|
|
|
rmtfd->shadow_buf = NULL;
|
|
|
|
rmtfd->shadow_len = 0;
|
|
|
|
|
2019-07-13 11:26:48 +08:00
|
|
|
rmtfd->partition = NULL;
|
2016-02-08 01:30:33 +08:00
|
|
|
}
|
|
|
|
|
2019-07-13 11:48:12 +08:00
|
|
|
struct rmtfd *storage_get(unsigned node, int caller_id)
|
2016-02-08 01:30:33 +08:00
|
|
|
{
|
2019-07-13 11:26:48 +08:00
|
|
|
struct rmtfd *rmtfd;
|
2016-02-08 01:30:33 +08:00
|
|
|
|
|
|
|
if (caller_id >= MAX_CALLERS)
|
2019-07-13 11:48:12 +08:00
|
|
|
return NULL;
|
2016-02-08 01:30:33 +08:00
|
|
|
|
2019-07-13 11:26:48 +08:00
|
|
|
rmtfd = &rmtfds[caller_id];
|
|
|
|
if (rmtfd->node != node)
|
2019-07-13 11:48:12 +08:00
|
|
|
return NULL;
|
2016-02-08 01:30:33 +08:00
|
|
|
|
2019-07-13 11:48:12 +08:00
|
|
|
return rmtfd;
|
2016-02-08 01:30:33 +08:00
|
|
|
}
|
|
|
|
|
2019-07-13 11:48:12 +08:00
|
|
|
int storage_get_caller_id(const struct rmtfd *rmtfd)
|
2016-02-08 01:30:33 +08:00
|
|
|
{
|
2019-07-13 11:48:12 +08:00
|
|
|
return rmtfd->id;
|
|
|
|
}
|
2016-02-08 01:30:33 +08:00
|
|
|
|
2019-07-13 11:48:12 +08:00
|
|
|
int storage_get_error(const struct rmtfd *rmtfd)
|
|
|
|
{
|
2019-07-13 11:26:48 +08:00
|
|
|
return rmtfd->dev_error;
|
2016-02-08 01:30:33 +08:00
|
|
|
}
|
|
|
|
|
2019-07-13 11:48:12 +08:00
|
|
|
void storage_exit(void)
|
2016-02-08 01:30:33 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2020-12-08 00:43:18 +08:00
|
|
|
for (i = 0; i < MAX_CALLERS; i++)
|
|
|
|
storage_close(&rmtfds[i]);
|
2016-02-08 01:30:33 +08:00
|
|
|
}
|
|
|
|
|
2019-07-13 11:48:12 +08:00
|
|
|
ssize_t storage_pread(const struct rmtfd *rmtfd, void *buf, size_t nbyte, off_t offset)
|
2019-07-13 03:10:37 +08:00
|
|
|
{
|
2019-07-13 12:26:43 +08:00
|
|
|
ssize_t n;
|
|
|
|
|
|
|
|
if (!storage_read_only) {
|
|
|
|
n = pread(rmtfd->fd, buf, nbyte, offset);
|
|
|
|
} else {
|
2022-03-22 00:13:19 +08:00
|
|
|
n = MIN((ssize_t)nbyte, (ssize_t)rmtfd->shadow_len - offset);
|
2019-07-13 12:26:43 +08:00
|
|
|
if (n > 0)
|
2020-03-03 14:35:48 +08:00
|
|
|
memcpy(buf, (char*)rmtfd->shadow_buf + offset, n);
|
2019-07-13 12:26:43 +08:00
|
|
|
else
|
|
|
|
n = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n < nbyte)
|
2020-03-03 14:35:48 +08:00
|
|
|
memset((char*)buf + n, 0, nbyte - n);
|
2019-07-13 12:26:43 +08:00
|
|
|
|
|
|
|
return nbyte;
|
2019-07-13 03:10:37 +08:00
|
|
|
}
|
|
|
|
|
2019-07-13 12:26:43 +08:00
|
|
|
ssize_t storage_pwrite(struct rmtfd *rmtfd, const void *buf, size_t nbyte, off_t offset)
|
2019-07-13 03:10:37 +08:00
|
|
|
{
|
2019-07-13 12:26:43 +08:00
|
|
|
size_t new_len = offset + nbyte;
|
|
|
|
void *new_buf;
|
|
|
|
|
|
|
|
if (!storage_read_only)
|
|
|
|
return pwrite(rmtfd->fd, buf, nbyte, offset);
|
|
|
|
|
|
|
|
if (new_len >= STORAGE_MAX_SIZE) {
|
|
|
|
fprintf(stderr, "write to %zd bytes exceededs max size\n", new_len);
|
|
|
|
errno = -EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_len > rmtfd->shadow_len) {
|
|
|
|
new_buf = realloc(rmtfd->shadow_buf, new_len);
|
|
|
|
if (!new_buf) {
|
|
|
|
errno = -ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rmtfd->shadow_buf = new_buf;
|
|
|
|
rmtfd->shadow_len = new_len;
|
|
|
|
}
|
|
|
|
|
2020-03-03 14:35:48 +08:00
|
|
|
memcpy((char*)rmtfd->shadow_buf + offset, buf, nbyte);
|
2019-07-13 12:26:43 +08:00
|
|
|
|
|
|
|
return nbyte;
|
2019-07-13 03:10:37 +08:00
|
|
|
}
|
|
|
|
|
2021-07-09 06:01:45 +08:00
|
|
|
int storage_sync(struct rmtfd *rmtfd)
|
|
|
|
{
|
|
|
|
if (storage_read_only)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return fdatasync(rmtfd->fd);
|
|
|
|
}
|
|
|
|
|
2019-07-13 12:26:43 +08:00
|
|
|
static int storage_populate_shadow_buf(struct rmtfd *rmtfd, const char *file)
|
|
|
|
{
|
|
|
|
ssize_t len;
|
|
|
|
ssize_t n;
|
|
|
|
void *buf;
|
|
|
|
int ret;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open(file, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
len = lseek(fd, 0, SEEK_END);
|
|
|
|
if (len < 0) {
|
|
|
|
ret = -1;
|
|
|
|
goto err_close_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
|
|
|
|
buf = calloc(1, len);
|
|
|
|
if (!buf) {
|
|
|
|
ret = -1;
|
|
|
|
goto err_close_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = read(fd, buf, len);
|
|
|
|
if (n < 0) {
|
|
|
|
ret = -1;
|
|
|
|
goto err_close_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
rmtfd->shadow_buf = buf;
|
|
|
|
rmtfd->shadow_len = n;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
err_close_fd:
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|