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
|
|
|
|
|
|
|
|
struct partition {
|
|
|
|
const char *path;
|
|
|
|
const char *actual;
|
|
|
|
};
|
|
|
|
|
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;
|
2016-02-08 01:30:33 +08:00
|
|
|
};
|
|
|
|
|
2018-12-08 08:03:43 +08:00
|
|
|
static const char *storage_dir = "/boot";
|
|
|
|
|
2016-02-08 01:30:33 +08:00
|
|
|
static const struct partition partition_table[] = {
|
2018-12-08 08:03:43 +08:00
|
|
|
{ "/boot/modem_fs1", "modem_fs1" },
|
|
|
|
{ "/boot/modem_fs2", "modem_fs2" },
|
|
|
|
{ "/boot/modem_fsc", "modem_fsc" },
|
|
|
|
{ "/boot/modem_fsg", "modem_fsg" },
|
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 11:48:12 +08:00
|
|
|
int storage_init(const char *storage_root)
|
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;
|
|
|
|
|
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;
|
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;
|
2018-12-08 08:03:43 +08:00
|
|
|
size_t pathlen;
|
2016-02-08 01:30:33 +08:00
|
|
|
int saved_errno;
|
|
|
|
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-13 11:26:48 +08:00
|
|
|
if (rmtfds[i].fd != -1 &&
|
|
|
|
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-13 11:26:48 +08:00
|
|
|
if (rmtfds[i].fd == -1) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-12-08 08:03:43 +08:00
|
|
|
pathlen = strlen(storage_dir) + strlen(part->actual) + 2;
|
|
|
|
fspath = alloca(pathlen);
|
|
|
|
snprintf(fspath, pathlen, "%s/%s", storage_dir, part->actual);
|
|
|
|
fd = open(fspath, O_RDWR);
|
2016-02-08 01:30:33 +08:00
|
|
|
if (fd < 0) {
|
|
|
|
saved_errno = errno;
|
|
|
|
fprintf(stderr, "[storage] failed to open '%s' (requested '%s'): %s\n",
|
2018-12-08 08:03:43 +08:00
|
|
|
fspath, part->path, strerror(saved_errno));
|
2019-07-13 11:48:12 +08:00
|
|
|
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->fd = fd;
|
|
|
|
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
|
|
|
{
|
2019-07-13 11:26:48 +08:00
|
|
|
close(rmtfd->fd);
|
|
|
|
rmtfd->fd = -1;
|
|
|
|
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;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_CALLERS; i++) {
|
2019-07-13 11:26:48 +08:00
|
|
|
if (rmtfds[i].fd >= 0)
|
|
|
|
close(rmtfds[i].fd);
|
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 11:48:12 +08:00
|
|
|
return pread(rmtfd->fd, buf, nbyte, offset);
|
2019-07-13 03:10:37 +08:00
|
|
|
}
|
|
|
|
|
2019-07-13 11:48:12 +08:00
|
|
|
ssize_t storage_pwrite(const struct rmtfd *rmtfd, const void *buf, size_t nbyte, off_t offset)
|
2019-07-13 03:10:37 +08:00
|
|
|
{
|
2019-07-13 11:48:12 +08:00
|
|
|
return pwrite(rmtfd->fd, buf, nbyte, offset);
|
2019-07-13 03:10:37 +08:00
|
|
|
}
|
|
|
|
|