rmtfs: Use pread/pwrite for storage

Instead of relying on an initial lseek, use pread/pwrite. This creates a
cleaner interface towards the storage.c implementation, allowing us to
provide a memory-only implementation of the backing storage.

Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
This commit is contained in:
Bjorn Andersson 2019-07-12 12:10:37 -07:00
parent cfb76ff6ee
commit 886608484b
3 changed files with 23 additions and 14 deletions

25
rmtfs.c
View File

@ -141,12 +141,14 @@ static void rmtfs_iovec(int sock, struct qrtr_packet *pkt)
struct rmtfs_iovec_resp resp = {}; struct rmtfs_iovec_resp resp = {};
struct rmtfs_iovec_req req = {}; struct rmtfs_iovec_req req = {};
DEFINE_QRTR_PACKET(resp_buf, 256); DEFINE_QRTR_PACKET(resp_buf, 256);
unsigned long phys_offset;
uint32_t caller_id = 0; uint32_t caller_id = 0;
size_t num_entries = 0; size_t num_entries = 0;
off_t sector_base;
uint8_t is_write; uint8_t is_write;
off_t phys_base;
uint8_t force = 0; uint8_t force = 0;
unsigned txn; unsigned txn;
off_t offset;
ssize_t len; ssize_t len;
ssize_t n; ssize_t n;
char buf[SECTOR_SIZE]; char buf[SECTOR_SIZE];
@ -176,26 +178,21 @@ static void rmtfs_iovec(int sock, struct qrtr_packet *pkt)
} }
for (i = 0; i < num_entries; i++) { for (i = 0; i < num_entries; i++) {
phys_offset = entries[i].phys_offset; phys_base = entries[i].phys_offset;
sector_base = entries[i].sector_addr * SECTOR_SIZE;
n = lseek(fd, entries[i].sector_addr * SECTOR_SIZE, SEEK_SET); offset = 0;
if (n < 0) {
fprintf(stderr, "[RMTFS] failed to seek sector %d\n", entries[i].sector_addr);
qmi_result_error(&resp.result, QMI_RMTFS_ERR_INTERNAL);
goto respond;
}
for (j = 0; j < entries[i].num_sector; j++) { for (j = 0; j < entries[i].num_sector; j++) {
if (is_write) { if (is_write) {
n = rmtfs_mem_read(rmem, phys_offset, buf, SECTOR_SIZE); n = rmtfs_mem_read(rmem, phys_base + offset, buf, SECTOR_SIZE);
if (n == SECTOR_SIZE) if (n == SECTOR_SIZE)
n = write(fd, buf, n); n = storage_pwrite(fd, buf, n, sector_base + offset);
} else { } else {
n = read(fd, buf, SECTOR_SIZE); n = storage_pread(fd, buf, SECTOR_SIZE, sector_base + offset);
if (n >= 0) { if (n >= 0) {
if (n < SECTOR_SIZE) if (n < SECTOR_SIZE)
memset(buf + n, 0, SECTOR_SIZE - n); memset(buf + n, 0, SECTOR_SIZE - n);
n = rmtfs_mem_write(rmem, phys_offset, buf, SECTOR_SIZE); n = rmtfs_mem_write(rmem, phys_base + offset, buf, SECTOR_SIZE);
} }
} }
@ -206,7 +203,7 @@ static void rmtfs_iovec(int sock, struct qrtr_packet *pkt)
goto respond; goto respond;
} }
phys_offset += SECTOR_SIZE; offset += SECTOR_SIZE;
} }
} }

View File

@ -29,5 +29,7 @@ int storage_put(unsigned node, int caller_id);
int storage_get_handle(unsigned node, int caller_id); int storage_get_handle(unsigned node, int caller_id);
int storage_get_error(unsigned node, int caller_id); int storage_get_error(unsigned node, int caller_id);
void storage_close(void); void storage_close(void);
ssize_t storage_pread(int fildes, void *buf, size_t nbyte, off_t offset);
ssize_t storage_pwrite(int fildes, const void *buf, size_t nbyte, off_t offset);
#endif #endif

View File

@ -161,3 +161,13 @@ void storage_close(void)
} }
} }
ssize_t storage_pread(int fildes, void *buf, size_t nbyte, off_t offset)
{
return pread(fildes, buf, nbyte, offset);
}
ssize_t storage_pwrite(int fildes, const void *buf, size_t nbyte, off_t offset)
{
return pwrite(fildes, buf, nbyte, offset);
}