2016-02-08 01:30:33 +08:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <ctype.h>
|
2019-04-15 18:51:28 +08:00
|
|
|
#include <dirent.h>
|
2016-02-08 01:30:33 +08:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libqrtr.h>
|
2019-04-15 18:51:28 +08:00
|
|
|
#include <limits.h>
|
|
|
|
#include <signal.h>
|
2016-02-08 01:30:33 +08:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2019-04-15 18:51:28 +08:00
|
|
|
#include <stdlib.h>
|
2016-02-08 01:30:33 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "qmi_rmtfs.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "rmtfs.h"
|
|
|
|
|
|
|
|
#define RMTFS_QMI_SERVICE 14
|
|
|
|
#define RMTFS_QMI_VERSION 1
|
|
|
|
#define RMTFS_QMI_INSTANCE 0
|
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
static struct rmtfs_mem *rmem;
|
2019-04-15 18:51:28 +08:00
|
|
|
static sig_atomic_t sig_int_count;
|
2016-02-08 01:30:33 +08:00
|
|
|
|
|
|
|
static bool dbgprintf_enabled;
|
|
|
|
static void dbgprintf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if (!dbgprintf_enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vprintf(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qmi_result_error(struct rmtfs_qmi_result *result, unsigned error)
|
|
|
|
{
|
|
|
|
/* Only propagate initial error */
|
|
|
|
if (result->result == QMI_RMTFS_RESULT_FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
result->result = QMI_RMTFS_RESULT_FAILURE;
|
|
|
|
result->error = error;
|
|
|
|
}
|
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
static void rmtfs_open(int sock, const struct qrtr_packet *pkt)
|
2016-02-08 01:30:33 +08:00
|
|
|
{
|
2018-02-01 10:09:35 +08:00
|
|
|
struct rmtfs_open_resp resp = {};
|
|
|
|
struct rmtfs_open_req req = {};
|
|
|
|
DEFINE_QRTR_PACKET(resp_buf, 256);
|
2019-07-13 11:48:12 +08:00
|
|
|
struct rmtfd *rmtfd;
|
2018-02-01 10:09:35 +08:00
|
|
|
unsigned int txn;
|
|
|
|
ssize_t len;
|
2016-02-08 13:33:54 +08:00
|
|
|
int caller_id = -1;
|
2016-02-08 01:30:33 +08:00
|
|
|
int ret;
|
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
ret = qmi_decode_message(&req, &txn, pkt, QMI_REQUEST,
|
|
|
|
QMI_RMTFS_OPEN, rmtfs_open_req_ei);
|
|
|
|
if (ret < 0) {
|
|
|
|
qmi_result_error(&resp.result, QMI_RMTFS_ERR_MALFORMED_MSG);
|
2016-02-08 01:30:33 +08:00
|
|
|
goto respond;
|
|
|
|
}
|
|
|
|
|
2019-07-13 11:48:12 +08:00
|
|
|
rmtfd = storage_open(pkt->node, req.path);
|
|
|
|
if (!rmtfd) {
|
2018-02-01 10:09:35 +08:00
|
|
|
qmi_result_error(&resp.result, QMI_RMTFS_ERR_INTERNAL);
|
2016-02-08 01:30:33 +08:00
|
|
|
goto respond;
|
|
|
|
}
|
|
|
|
|
2019-07-13 11:48:12 +08:00
|
|
|
caller_id = storage_get_caller_id(rmtfd);
|
2018-02-01 10:09:35 +08:00
|
|
|
resp.caller_id = caller_id;
|
|
|
|
resp.caller_id_valid = true;
|
2016-02-08 01:30:33 +08:00
|
|
|
|
|
|
|
respond:
|
2018-02-01 10:09:35 +08:00
|
|
|
dbgprintf("[RMTFS] open %s => %d (%d:%d)\n",
|
|
|
|
req.path, caller_id, resp.result.result, resp.result.error);
|
|
|
|
|
|
|
|
len = qmi_encode_message(&resp_buf,
|
|
|
|
QMI_RESPONSE, QMI_RMTFS_OPEN, txn, &resp,
|
|
|
|
rmtfs_open_resp_ei);
|
|
|
|
if (len < 0) {
|
|
|
|
fprintf(stderr, "[RMTFS] failed to encode open-response: %s\n",
|
|
|
|
strerror(-len));
|
|
|
|
return;
|
|
|
|
}
|
2016-02-08 01:30:33 +08:00
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
ret = qrtr_sendto(sock, pkt->node, pkt->port,
|
|
|
|
resp_buf.data, resp_buf.data_len);
|
2016-02-08 01:30:33 +08:00
|
|
|
if (ret < 0)
|
2018-02-01 10:09:35 +08:00
|
|
|
fprintf(stderr, "[RMTFS] failed to send open-response: %s\n",
|
|
|
|
strerror(-ret));
|
2016-02-08 01:30:33 +08:00
|
|
|
}
|
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
static void rmtfs_close(int sock, const struct qrtr_packet *pkt)
|
2016-02-08 01:30:33 +08:00
|
|
|
{
|
2018-02-01 10:09:35 +08:00
|
|
|
struct rmtfs_close_resp resp = {};
|
|
|
|
struct rmtfs_close_req req = {};
|
|
|
|
DEFINE_QRTR_PACKET(resp_buf, 256);
|
2019-07-13 11:48:12 +08:00
|
|
|
struct rmtfd *rmtfd;
|
2018-02-01 10:09:35 +08:00
|
|
|
unsigned int txn;
|
|
|
|
ssize_t len;
|
2016-02-08 01:30:33 +08:00
|
|
|
int ret;
|
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
ret = qmi_decode_message(&req, &txn, pkt, QMI_REQUEST,
|
|
|
|
QMI_RMTFS_CLOSE, rmtfs_close_req_ei);
|
|
|
|
if (ret < 0) {
|
|
|
|
qmi_result_error(&resp.result, QMI_RMTFS_ERR_MALFORMED_MSG);
|
2016-02-08 01:30:33 +08:00
|
|
|
goto respond;
|
|
|
|
}
|
|
|
|
|
2019-07-13 11:48:12 +08:00
|
|
|
rmtfd = storage_get(pkt->node, req.caller_id);
|
|
|
|
if (!rmtfd) {
|
2018-02-01 10:09:35 +08:00
|
|
|
qmi_result_error(&resp.result, QMI_RMTFS_ERR_INTERNAL);
|
2019-07-13 11:48:12 +08:00
|
|
|
goto respond;
|
2016-02-08 01:30:33 +08:00
|
|
|
}
|
|
|
|
|
2019-07-13 11:48:12 +08:00
|
|
|
storage_close(rmtfd);
|
2017-07-28 06:22:02 +08:00
|
|
|
rmtfs_mem_free(rmem);
|
2016-02-08 01:30:33 +08:00
|
|
|
|
|
|
|
respond:
|
2018-02-01 10:09:35 +08:00
|
|
|
dbgprintf("[RMTFS] close %s => %d (%d:%d)\n",
|
|
|
|
req.caller_id, resp.result.result, resp.result.error);
|
|
|
|
|
|
|
|
len = qmi_encode_message(&resp_buf,
|
|
|
|
QMI_RESPONSE, QMI_RMTFS_CLOSE, txn, &resp,
|
|
|
|
rmtfs_close_resp_ei);
|
|
|
|
if (len < 0) {
|
|
|
|
fprintf(stderr, "[RMTFS] failed to encode close-response: %s\n",
|
|
|
|
strerror(-len));
|
|
|
|
return;
|
|
|
|
}
|
2016-02-08 01:30:33 +08:00
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
ret = qrtr_sendto(sock, pkt->node, pkt->port,
|
|
|
|
resp_buf.data, resp_buf.data_len);
|
2016-02-08 01:30:33 +08:00
|
|
|
if (ret < 0)
|
2018-02-01 10:09:35 +08:00
|
|
|
fprintf(stderr, "[RMTFS] failed to send close-response: %s\n",
|
|
|
|
strerror(-ret));
|
2016-02-08 01:30:33 +08:00
|
|
|
}
|
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
static void rmtfs_iovec(int sock, struct qrtr_packet *pkt)
|
2016-02-08 01:30:33 +08:00
|
|
|
{
|
|
|
|
struct rmtfs_iovec_entry *entries;
|
2018-02-01 10:09:35 +08:00
|
|
|
struct rmtfs_iovec_resp resp = {};
|
|
|
|
struct rmtfs_iovec_req req = {};
|
|
|
|
DEFINE_QRTR_PACKET(resp_buf, 256);
|
2019-07-13 11:48:12 +08:00
|
|
|
struct rmtfd *rmtfd;
|
2018-02-01 10:09:35 +08:00
|
|
|
uint32_t caller_id = 0;
|
|
|
|
size_t num_entries = 0;
|
2019-07-13 03:10:37 +08:00
|
|
|
off_t sector_base;
|
2016-02-08 01:30:33 +08:00
|
|
|
uint8_t is_write;
|
2019-07-13 03:10:37 +08:00
|
|
|
off_t phys_base;
|
2018-02-01 10:09:35 +08:00
|
|
|
uint8_t force = 0;
|
2016-02-08 01:30:33 +08:00
|
|
|
unsigned txn;
|
2019-07-13 03:10:37 +08:00
|
|
|
off_t offset;
|
2018-02-01 10:09:35 +08:00
|
|
|
ssize_t len;
|
2016-02-08 01:30:33 +08:00
|
|
|
ssize_t n;
|
2017-07-28 14:03:19 +08:00
|
|
|
char buf[SECTOR_SIZE];
|
2016-02-08 01:30:33 +08:00
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
ret = qmi_decode_message(&req, &txn, pkt, QMI_REQUEST,
|
|
|
|
QMI_RMTFS_RW_IOVEC, rmtfs_iovec_req_ei);
|
2016-02-08 01:30:33 +08:00
|
|
|
if (ret < 0) {
|
2018-02-01 10:09:35 +08:00
|
|
|
qmi_result_error(&resp.result, QMI_RMTFS_ERR_MALFORMED_MSG);
|
2016-02-08 01:30:33 +08:00
|
|
|
goto respond;
|
|
|
|
}
|
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
caller_id = req.caller_id;
|
|
|
|
is_write = req.direction;
|
|
|
|
entries = req.iovec;
|
|
|
|
num_entries = req.iovec_len;
|
|
|
|
force = req.is_force_sync;
|
2016-02-08 01:30:33 +08:00
|
|
|
|
2019-07-13 11:48:12 +08:00
|
|
|
rmtfd = storage_get(pkt->node, caller_id);
|
|
|
|
if (!rmtfd) {
|
2016-02-08 01:30:33 +08:00
|
|
|
fprintf(stderr, "[RMTFS] iovec request for non-existing caller\n");
|
2018-02-01 10:09:35 +08:00
|
|
|
qmi_result_error(&resp.result, QMI_RMTFS_ERR_INTERNAL);
|
2016-02-08 01:30:33 +08:00
|
|
|
goto respond;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num_entries; i++) {
|
2019-07-13 03:10:37 +08:00
|
|
|
phys_base = entries[i].phys_offset;
|
|
|
|
sector_base = entries[i].sector_addr * SECTOR_SIZE;
|
|
|
|
offset = 0;
|
2016-02-08 01:30:33 +08:00
|
|
|
|
|
|
|
for (j = 0; j < entries[i].num_sector; j++) {
|
2017-07-28 14:03:19 +08:00
|
|
|
if (is_write) {
|
2019-07-13 03:10:37 +08:00
|
|
|
n = rmtfs_mem_read(rmem, phys_base + offset, buf, SECTOR_SIZE);
|
2018-12-08 06:48:33 +08:00
|
|
|
if (n == SECTOR_SIZE)
|
2019-07-13 11:48:12 +08:00
|
|
|
n = storage_pwrite(rmtfd, buf, n, sector_base + offset);
|
2017-07-28 14:03:19 +08:00
|
|
|
} else {
|
2019-07-13 11:48:12 +08:00
|
|
|
n = storage_pread(rmtfd, buf, SECTOR_SIZE, sector_base + offset);
|
2018-12-08 06:48:33 +08:00
|
|
|
if (n >= 0) {
|
|
|
|
if (n < SECTOR_SIZE)
|
|
|
|
memset(buf + n, 0, SECTOR_SIZE - n);
|
2019-07-13 03:10:37 +08:00
|
|
|
n = rmtfs_mem_write(rmem, phys_base + offset, buf, SECTOR_SIZE);
|
2018-12-08 06:48:33 +08:00
|
|
|
}
|
2017-07-28 14:03:19 +08:00
|
|
|
}
|
2016-02-08 01:30:33 +08:00
|
|
|
|
|
|
|
if (n != SECTOR_SIZE) {
|
|
|
|
fprintf(stderr, "[RMTFS] failed to %s sector %d\n",
|
|
|
|
is_write ? "write" : "read", entries[i].sector_addr + j);
|
2018-02-01 10:09:35 +08:00
|
|
|
qmi_result_error(&resp.result, QMI_RMTFS_ERR_INTERNAL);
|
2016-02-08 01:30:33 +08:00
|
|
|
goto respond;
|
|
|
|
}
|
|
|
|
|
2019-07-13 03:10:37 +08:00
|
|
|
offset += SECTOR_SIZE;
|
2016-02-08 01:30:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
respond:
|
|
|
|
dbgprintf("[RMTFS] iovec %d, %sforced => (%d:%d)\n", caller_id, force ? "" : "not ",
|
2018-02-01 10:09:35 +08:00
|
|
|
resp.result.result, resp.result.error);
|
2021-07-09 06:01:45 +08:00
|
|
|
|
|
|
|
if (is_write)
|
|
|
|
storage_sync(rmtfd);
|
|
|
|
|
2016-02-08 01:30:33 +08:00
|
|
|
for (i = 0; i < num_entries; i++) {
|
|
|
|
dbgprintf("[RMTFS] %s %d:%d 0x%x\n", is_write ? "write" : "read",
|
|
|
|
entries[i].sector_addr,
|
|
|
|
entries[i].num_sector,
|
|
|
|
entries[i].phys_offset);
|
|
|
|
}
|
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
len = qmi_encode_message(&resp_buf,
|
|
|
|
QMI_RESPONSE, QMI_RMTFS_RW_IOVEC, txn, &resp,
|
|
|
|
rmtfs_iovec_resp_ei);
|
|
|
|
if (len < 0) {
|
|
|
|
fprintf(stderr, "[RMTFS] failed to encode iovec-response: %s\n",
|
|
|
|
strerror(-len));
|
|
|
|
return;
|
|
|
|
}
|
2016-02-08 01:30:33 +08:00
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
ret = qrtr_sendto(sock, pkt->node, pkt->port,
|
|
|
|
resp_buf.data, resp_buf.data_len);
|
2016-02-08 01:30:33 +08:00
|
|
|
if (ret < 0)
|
2018-02-01 10:09:35 +08:00
|
|
|
fprintf(stderr, "[RMTFS] failed to send iovec-response: %s\n",
|
|
|
|
strerror(-ret));
|
2016-02-08 01:30:33 +08:00
|
|
|
}
|
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
static void rmtfs_alloc_buf(int sock, struct qrtr_packet *pkt)
|
2016-02-08 01:30:33 +08:00
|
|
|
{
|
2018-02-01 10:09:35 +08:00
|
|
|
struct rmtfs_alloc_buf_resp resp = {};
|
|
|
|
struct rmtfs_alloc_buf_req req = {};
|
|
|
|
DEFINE_QRTR_PACKET(resp_buf, 256);
|
|
|
|
uint32_t alloc_size = 0;
|
|
|
|
uint32_t caller_id = 0;
|
|
|
|
int64_t address = 0;
|
2016-02-08 01:30:33 +08:00
|
|
|
unsigned txn;
|
2018-02-01 10:09:35 +08:00
|
|
|
ssize_t len;
|
2016-02-08 01:30:33 +08:00
|
|
|
int ret;
|
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
ret = qmi_decode_message(&req, &txn, pkt, QMI_REQUEST,
|
|
|
|
QMI_RMTFS_ALLOC_BUFF, rmtfs_alloc_buf_req_ei);
|
2016-02-08 01:30:33 +08:00
|
|
|
if (ret < 0) {
|
2018-02-01 10:09:35 +08:00
|
|
|
qmi_result_error(&resp.result, QMI_RMTFS_ERR_MALFORMED_MSG);
|
2016-02-08 01:30:33 +08:00
|
|
|
goto respond;
|
|
|
|
}
|
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
caller_id = req.caller_id;
|
|
|
|
alloc_size = req.buff_size;
|
2016-02-08 01:30:33 +08:00
|
|
|
|
2017-07-28 06:22:02 +08:00
|
|
|
address = rmtfs_mem_alloc(rmem, alloc_size);
|
2018-02-01 10:09:35 +08:00
|
|
|
if (address < 0) {
|
|
|
|
qmi_result_error(&resp.result, QMI_RMTFS_ERR_INTERNAL);
|
|
|
|
goto respond;
|
|
|
|
}
|
2016-02-08 01:30:33 +08:00
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
resp.buff_address = address;
|
|
|
|
resp.buff_address_valid = true;
|
2016-02-08 01:30:33 +08:00
|
|
|
respond:
|
2018-02-01 10:09:35 +08:00
|
|
|
dbgprintf("[RMTFS] alloc %d, %d => 0x%lx (%d:%d)\n", caller_id, alloc_size, address, resp.result.result, resp.result.error);
|
|
|
|
|
|
|
|
len = qmi_encode_message(&resp_buf,
|
|
|
|
QMI_RESPONSE, QMI_RMTFS_ALLOC_BUFF, txn, &resp,
|
|
|
|
rmtfs_alloc_buf_resp_ei);
|
|
|
|
if (len < 0) {
|
|
|
|
fprintf(stderr, "[RMTFS] failed to encode alloc-buf-response: %s\n",
|
|
|
|
strerror(-len));
|
|
|
|
return;
|
|
|
|
}
|
2016-02-08 01:30:33 +08:00
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
ret = qrtr_sendto(sock, pkt->node, pkt->port,
|
|
|
|
resp_buf.data, resp_buf.data_len);
|
2016-02-08 01:30:33 +08:00
|
|
|
if (ret < 0)
|
2018-02-01 10:09:35 +08:00
|
|
|
fprintf(stderr, "[RMTFS] failed to send alloc-buf-response: %s\n",
|
|
|
|
strerror(-ret));
|
2016-02-08 01:30:33 +08:00
|
|
|
}
|
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
static void rmtfs_get_dev_error(int sock, struct qrtr_packet *pkt)
|
2016-02-08 01:30:33 +08:00
|
|
|
{
|
2018-02-01 10:09:35 +08:00
|
|
|
struct rmtfs_dev_error_resp resp = {};
|
|
|
|
struct rmtfs_dev_error_req req = {};
|
|
|
|
DEFINE_QRTR_PACKET(resp_buf, 256);
|
2019-07-13 11:48:12 +08:00
|
|
|
struct rmtfd *rmtfd;
|
2016-02-08 01:30:33 +08:00
|
|
|
unsigned txn;
|
2018-02-01 10:09:35 +08:00
|
|
|
ssize_t len;
|
2016-02-08 01:30:33 +08:00
|
|
|
int ret;
|
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
ret = qmi_decode_message(&req, &txn, pkt, QMI_REQUEST,
|
|
|
|
QMI_RMTFS_GET_DEV_ERROR,
|
|
|
|
rmtfs_dev_error_req_ei);
|
|
|
|
if (ret < 0) {
|
|
|
|
qmi_result_error(&resp.result, QMI_RMTFS_ERR_MALFORMED_MSG);
|
2016-02-08 01:30:33 +08:00
|
|
|
goto respond;
|
|
|
|
}
|
|
|
|
|
2019-07-13 11:48:12 +08:00
|
|
|
rmtfd = storage_get(pkt->node, req.caller_id);
|
|
|
|
if (rmtfd) {
|
2018-02-01 10:09:35 +08:00
|
|
|
qmi_result_error(&resp.result, QMI_RMTFS_ERR_INTERNAL);
|
2016-02-08 01:30:33 +08:00
|
|
|
goto respond;
|
|
|
|
}
|
|
|
|
|
2019-07-13 11:48:12 +08:00
|
|
|
resp.status = storage_get_error(rmtfd);
|
2018-02-01 10:09:35 +08:00
|
|
|
resp.status_valid = true;
|
2016-02-08 01:30:33 +08:00
|
|
|
|
|
|
|
respond:
|
2019-07-13 11:48:12 +08:00
|
|
|
dbgprintf("[RMTFS] dev_error %d => %d (%d:%d)\n", req.caller_id, resp.status, resp.result.result, resp.result.error);
|
2018-02-01 10:09:35 +08:00
|
|
|
|
|
|
|
len = qmi_encode_message(&resp_buf,
|
|
|
|
QMI_RESPONSE, QMI_RMTFS_GET_DEV_ERROR, txn,
|
|
|
|
&resp, rmtfs_dev_error_resp_ei);
|
|
|
|
if (len < 0) {
|
|
|
|
fprintf(stderr, "[RMTFS] failed to encode dev-error-response: %s\n",
|
|
|
|
strerror(-len));
|
|
|
|
return;
|
|
|
|
}
|
2016-02-08 01:30:33 +08:00
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
ret = qrtr_sendto(sock, pkt->node, pkt->port,
|
|
|
|
resp_buf.data, resp_buf.data_len);
|
2016-02-08 01:30:33 +08:00
|
|
|
if (ret < 0)
|
2018-02-01 10:09:35 +08:00
|
|
|
fprintf(stderr, "[RMTFS] failed to send dev-error-response: %s\n",
|
|
|
|
strerror(-ret));
|
2016-02-08 01:30:33 +08:00
|
|
|
}
|
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
static int rmtfs_bye(uint32_t node)
|
2017-06-08 00:53:25 +08:00
|
|
|
{
|
|
|
|
dbgprintf("[RMTFS] bye from %d\n", node);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
static int rmtfs_del_client(uint32_t node, uint32_t port)
|
2017-06-08 00:53:25 +08:00
|
|
|
{
|
|
|
|
dbgprintf("[RMTFS] del_client %d:%d\n", node, port);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-08 01:30:33 +08:00
|
|
|
static int handle_rmtfs(int sock)
|
|
|
|
{
|
|
|
|
struct sockaddr_qrtr sq;
|
2018-02-01 10:09:35 +08:00
|
|
|
struct qrtr_packet pkt;
|
|
|
|
unsigned int msg_id;
|
2016-02-08 01:30:33 +08:00
|
|
|
socklen_t sl;
|
|
|
|
char buf[4096];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
sl = sizeof(sq);
|
|
|
|
ret = recvfrom(sock, buf, sizeof(buf), 0, (void *)&sq, &sl);
|
|
|
|
if (ret < 0) {
|
2017-06-08 01:54:42 +08:00
|
|
|
ret = -errno;
|
|
|
|
if (ret != -ENETRESET)
|
|
|
|
fprintf(stderr, "[RMTFS] recvfrom failed: %d\n", ret);
|
2016-02-08 01:30:33 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbgprintf("[RMTFS] packet; from: %d:%d\n", sq.sq_node, sq.sq_port);
|
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
ret = qrtr_decode(&pkt, buf, ret, &sq);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "[RMTFS] unable to decode qrtr packet\n");
|
|
|
|
return ret;
|
2017-06-08 00:53:25 +08:00
|
|
|
}
|
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
switch (pkt.type) {
|
|
|
|
case QRTR_TYPE_BYE:
|
|
|
|
return rmtfs_bye(pkt.node);
|
|
|
|
case QRTR_TYPE_DEL_CLIENT:
|
|
|
|
return rmtfs_del_client(pkt.node, pkt.port);
|
|
|
|
case QRTR_TYPE_DATA:
|
|
|
|
ret = qmi_decode_header(&pkt, &msg_id);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
switch (msg_id) {
|
|
|
|
case QMI_RMTFS_OPEN:
|
|
|
|
rmtfs_open(sock, &pkt);
|
|
|
|
break;
|
|
|
|
case QMI_RMTFS_CLOSE:
|
|
|
|
rmtfs_close(sock, &pkt);
|
|
|
|
break;
|
|
|
|
case QMI_RMTFS_RW_IOVEC:
|
|
|
|
rmtfs_iovec(sock, &pkt);
|
|
|
|
break;
|
|
|
|
case QMI_RMTFS_ALLOC_BUFF:
|
|
|
|
rmtfs_alloc_buf(sock, &pkt);
|
|
|
|
break;
|
|
|
|
case QMI_RMTFS_GET_DEV_ERROR:
|
|
|
|
rmtfs_get_dev_error(sock, &pkt);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "[RMTFS] Unknown request: %d\n", msg_id);
|
|
|
|
break;
|
|
|
|
}
|
2016-02-08 01:30:33 +08:00
|
|
|
|
2018-02-01 10:09:35 +08:00
|
|
|
return 0;
|
2016-02-08 01:30:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-04-15 18:51:28 +08:00
|
|
|
static int sig_int_count;
|
|
|
|
|
|
|
|
static int run_rmtfs(int rprocfd)
|
2016-02-08 01:30:33 +08:00
|
|
|
{
|
2019-04-15 18:51:28 +08:00
|
|
|
bool sig_int_handled = false;
|
2016-02-08 01:30:33 +08:00
|
|
|
int rmtfs_fd;
|
2019-04-15 18:51:28 +08:00
|
|
|
fd_set rfds;
|
|
|
|
char done;
|
|
|
|
int nfds;
|
2018-02-01 10:28:17 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
rmtfs_fd = qrtr_open(RMTFS_QMI_SERVICE);
|
|
|
|
if (rmtfs_fd < 0) {
|
|
|
|
fprintf(stderr, "failed to create qrtr socket\n");
|
|
|
|
return rmtfs_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbgprintf("registering services\n");
|
|
|
|
|
|
|
|
ret = qrtr_publish(rmtfs_fd, RMTFS_QMI_SERVICE,
|
|
|
|
RMTFS_QMI_VERSION, RMTFS_QMI_INSTANCE);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "failed to publish rmtfs service");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-04-15 18:51:28 +08:00
|
|
|
if (rprocfd >= 0)
|
|
|
|
rproc_start();
|
|
|
|
|
2018-02-01 10:28:17 +08:00
|
|
|
for (;;) {
|
2020-11-21 13:21:05 +08:00
|
|
|
if (sig_int_count == 1 && !sig_int_handled) {
|
|
|
|
if (rprocfd < 0)
|
|
|
|
break;
|
|
|
|
|
2019-04-15 18:51:28 +08:00
|
|
|
rproc_stop();
|
|
|
|
sig_int_handled = true;
|
|
|
|
} else if (sig_int_count > 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(rmtfs_fd, &rfds);
|
|
|
|
if (rprocfd >= 0)
|
|
|
|
FD_SET(rprocfd, &rfds);
|
|
|
|
nfds = MAX(rmtfs_fd, rprocfd) + 1;
|
|
|
|
|
|
|
|
ret = select(nfds, &rfds, NULL, NULL, NULL);
|
2018-02-01 10:28:17 +08:00
|
|
|
if (ret < 0 && errno != EINTR)
|
|
|
|
break;
|
|
|
|
else if (ret < 0 && errno == EINTR)
|
|
|
|
continue;
|
|
|
|
|
2019-04-15 18:51:28 +08:00
|
|
|
if (rprocfd >= 0 && FD_ISSET(rprocfd, &rfds)) {
|
|
|
|
ret = read(rprocfd, &done, 1);
|
|
|
|
if (!ret || done == 'Y')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD_ISSET(rmtfs_fd, &rfds)) {
|
|
|
|
ret = handle_rmtfs(rmtfs_fd);
|
|
|
|
if (ret == -ENETRESET)
|
|
|
|
break;
|
|
|
|
}
|
2018-02-01 10:28:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
close(rmtfs_fd);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-04-15 18:51:28 +08:00
|
|
|
static void sig_int_handler(int signo)
|
|
|
|
{
|
|
|
|
sig_int_count++;
|
|
|
|
}
|
|
|
|
|
2018-02-01 10:28:17 +08:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2019-04-15 18:51:28 +08:00
|
|
|
struct sigaction action;
|
2019-07-18 03:21:05 +08:00
|
|
|
bool use_partitions = false;
|
2019-07-13 12:26:43 +08:00
|
|
|
bool read_only = false;
|
2019-04-15 18:51:28 +08:00
|
|
|
int rprocfd = -1;
|
2016-02-08 01:30:33 +08:00
|
|
|
int ret;
|
2018-12-08 08:03:43 +08:00
|
|
|
int option;
|
|
|
|
const char *storage_root = NULL;
|
|
|
|
|
2019-04-15 18:51:28 +08:00
|
|
|
while ((option = getopt(argc, argv, "o:Prsv")) != -1) {
|
2018-12-08 08:03:43 +08:00
|
|
|
switch (option) {
|
2020-02-24 15:53:55 +08:00
|
|
|
/*
|
|
|
|
* -o sets the directory where EFS images are stored,
|
|
|
|
* or sets the directory from where raw EFS partitions
|
|
|
|
* can be picked by-name when used with -P option.
|
|
|
|
*/
|
2018-12-08 08:03:43 +08:00
|
|
|
case 'o':
|
|
|
|
storage_root = optarg;
|
|
|
|
break;
|
|
|
|
|
2019-07-18 03:21:05 +08:00
|
|
|
/* -P to find and use raw EFS partitions */
|
|
|
|
case 'P':
|
|
|
|
use_partitions = true;
|
|
|
|
break;
|
|
|
|
|
2019-07-13 12:26:43 +08:00
|
|
|
/* -r to avoid writing to storage */
|
|
|
|
case 'r':
|
|
|
|
read_only = true;
|
|
|
|
break;
|
|
|
|
|
2019-04-15 18:51:28 +08:00
|
|
|
/* enable sync for the mss rproc instance */
|
|
|
|
case 's':
|
|
|
|
rprocfd = rproc_init();
|
2020-01-30 12:07:51 +08:00
|
|
|
if (rprocfd < 0) {
|
|
|
|
fprintf(stderr, "Failed to get rprocfd\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-04-15 18:51:28 +08:00
|
|
|
break;
|
|
|
|
|
2018-12-08 08:03:43 +08:00
|
|
|
/* -v is for verbose */
|
|
|
|
case 'v':
|
|
|
|
dbgprintf_enabled = 1;
|
|
|
|
break;
|
2016-02-08 01:30:33 +08:00
|
|
|
|
2018-12-08 08:03:43 +08:00
|
|
|
case '?':
|
|
|
|
fprintf(stderr, "Unknown option: -%c\n", option);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2016-02-08 01:30:33 +08:00
|
|
|
|
2019-04-15 18:51:28 +08:00
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
action.sa_handler = sig_int_handler;
|
|
|
|
action.sa_flags = 0;
|
|
|
|
|
|
|
|
sigaction(SIGINT, &action, NULL);
|
|
|
|
sigaction(SIGTERM, &action, NULL);
|
|
|
|
|
2017-07-28 06:22:02 +08:00
|
|
|
rmem = rmtfs_mem_open();
|
2017-07-28 09:39:21 +08:00
|
|
|
if (!rmem)
|
2016-02-08 01:30:33 +08:00
|
|
|
return 1;
|
|
|
|
|
2019-07-18 03:21:05 +08:00
|
|
|
ret = storage_init(storage_root, read_only, use_partitions);
|
2016-02-08 01:30:33 +08:00
|
|
|
if (ret) {
|
2017-07-28 06:20:24 +08:00
|
|
|
fprintf(stderr, "failed to initialize storage system\n");
|
2016-02-08 01:30:33 +08:00
|
|
|
goto close_rmtfs_mem;
|
|
|
|
}
|
|
|
|
|
2018-02-01 10:28:17 +08:00
|
|
|
do {
|
2019-04-15 18:51:28 +08:00
|
|
|
ret = run_rmtfs(rprocfd);
|
2018-02-01 10:28:17 +08:00
|
|
|
} while (ret == -ENETRESET);
|
|
|
|
|
2019-07-13 11:48:12 +08:00
|
|
|
storage_exit();
|
2016-02-08 01:30:33 +08:00
|
|
|
close_rmtfs_mem:
|
2017-07-28 06:22:02 +08:00
|
|
|
rmtfs_mem_close(rmem);
|
2016-02-08 01:30:33 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|