summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd/sbc_harness/fs_harness_flash_bin.c15
-rw-r--r--cmd/sbc_harness/fs_harness_uptime_txt.c11
-rw-r--r--lib9p/srv.c53
-rw-r--r--lib9p/srv_include/lib9p/srv.h23
-rw-r--r--lib9p/tests/test_server/fs_flush.c13
-rw-r--r--lib9p/tests/test_server/fs_shutdown.c4
-rw-r--r--lib9p/tests/test_server/fs_whoami.c11
-rw-r--r--lib9p_util/static.c11
8 files changed, 80 insertions, 61 deletions
diff --git a/cmd/sbc_harness/fs_harness_flash_bin.c b/cmd/sbc_harness/fs_harness_flash_bin.c
index 9a5bb2f..e5c3026 100644
--- a/cmd/sbc_harness/fs_harness_flash_bin.c
+++ b/cmd/sbc_harness/fs_harness_flash_bin.c
@@ -221,13 +221,13 @@ static void flash_file_iofree(struct flash_file *self) {
ab_flash_finalize(self->wbuf.dat);
}
-static iovec_or_error flash_file_pread(struct flash_file *self, struct lib9p_srv_ctx *ctx,
- uint32_t byte_count, uint64_t byte_offset) {
+static error flash_file_pread(struct flash_file *self, struct lib9p_srv_ctx *ctx,
+ lo_interface io_writer dst, uint64_t byte_offset, uint32_t byte_count) {
assert(self);
assert(ctx);
if (byte_offset > DATA_SIZE)
- return ERROR_NEW_ERR(iovec, error_new(E_POSIX_EINVAL, "offset is past the chip size"));
+ return error_new(E_POSIX_EINVAL, "offset is past the chip size");
/* Assume that somewhere down the line the iovec we return
* will be passed to DMA. We don't want the DMA engine to hit
@@ -237,9 +237,7 @@ static iovec_or_error flash_file_pread(struct flash_file *self, struct lib9p_srv
* DMA engine can only have a DREQ on one side of the channel.
*/
if (byte_offset == DATA_SIZE)
- return ERROR_NEW_VAL(iovec, ((struct iovec){
- .iov_len = 0,
- }));
+ return io_write(dst, NULL, 0).err;
size_t sector_base = LM_ROUND_DOWN(byte_offset, FLASH_SECTOR_SIZE);
if (byte_offset + byte_count > sector_base + FLASH_SECTOR_SIZE)
byte_count = (sector_base + FLASH_SECTOR_SIZE) - byte_offset;
@@ -251,10 +249,7 @@ static iovec_or_error flash_file_pread(struct flash_file *self, struct lib9p_srv
memcpy(self->rbuf.dat, DATA_START+sector_base, FLASH_SECTOR_SIZE);
}
- return ERROR_NEW_VAL(iovec, ((struct iovec){
- .iov_base = &self->rbuf.dat[byte_offset-sector_base],
- .iov_len = byte_count,
- }));
+ return io_write(dst, &self->rbuf.dat[byte_offset-sector_base], byte_count).err;
}
/* TODO: Short/corrupt writes are dangerous. This should either (1)
diff --git a/cmd/sbc_harness/fs_harness_uptime_txt.c b/cmd/sbc_harness/fs_harness_uptime_txt.c
index 4d35385..9fa6bcc 100644
--- a/cmd/sbc_harness/fs_harness_uptime_txt.c
+++ b/cmd/sbc_harness/fs_harness_uptime_txt.c
@@ -107,8 +107,8 @@ static struct lib9p_qid uptime_fio_ioqid(struct uptime_fio *self) {
return uptime_file_qid(self->parent);
}
-static iovec_or_error uptime_fio_pread(struct uptime_fio *self, struct lib9p_srv_ctx *ctx,
- uint32_t byte_count, uint64_t byte_offset) {
+static error uptime_fio_pread(struct uptime_fio *self, struct lib9p_srv_ctx *ctx,
+ lo_interface io_writer dst, uint64_t byte_offset, uint32_t byte_count) {
assert(self);
assert(ctx);
@@ -118,16 +118,13 @@ static iovec_or_error uptime_fio_pread(struct uptime_fio *self, struct lib9p_srv
}
if (byte_offset > (uint64_t)self->buf_len)
- return ERROR_NEW_ERR(iovec, error_new(E_POSIX_EINVAL, "offset is past end-of-file length"));
+ return error_new(E_POSIX_EINVAL, "offset is past end-of-file length");
size_t beg_off = (size_t)byte_offset;
size_t end_off = beg_off + (size_t)byte_count;
if (end_off > self->buf_len)
end_off = self->buf_len;
- return ERROR_NEW_VAL(iovec, ((struct iovec){
- .iov_base = &self->buf[beg_off],
- .iov_len = end_off-beg_off,
- }));
+ return io_write(dst, &self->buf[beg_off], end_off-beg_off).err;
}
static uint32_t_or_error uptime_fio_pwrite(struct uptime_fio *self, struct lib9p_srv_ctx *ctx,
diff --git a/lib9p/srv.c b/lib9p/srv.c
index ecdf169..03d7b93 100644
--- a/lib9p/srv.c
+++ b/lib9p/srv.c
@@ -1185,6 +1185,11 @@ static void handle_Tread(struct srv_req *ctx,
/* TODO: serialize simultaneous reads to the same FID */
+ /* req->count <= CONFIG_9P_SRV_MAX_MSG_SIZE <= CONFIG_9P_SRV_MAX_HOSTMSG_SIZE <= SIZE_MAX */
+ assert(req->count <= SIZE_MAX);
+ /* req->offset is u64, uoff is u64 */
+ static_assert(req->offset <= UOFF_MAX);
+
if (req->count > ctx->basectx.max_msg_size - lib9p_version_min_Rread_size(ctx->basectx.version))
req->count = ctx->basectx.max_msg_size - lib9p_version_min_Rread_size(ctx->basectx.version);
@@ -1219,20 +1224,44 @@ static void handle_Tread(struct srv_req *ctx,
ctx->user = srv_userid_decref(ctx->user);
}
-static void handle_read_file(struct srv_req *ctx, struct srv_fidinfo *fidinfo, uint64_t offset, uint32_t count) {
- iovec_or_error r = LO_CALL(fidinfo->file.io, pread, ctx, count, offset);
- if (r.is_err) {
- srv_respond(ctx, read, NULL, r.err);
- return;
- }
+struct rread_writer {
+ struct srv_req *ctx;
+ size_t count;
+ bool written;
+
+};
+LO_IMPLEMENTATION_STATIC(io_writer, struct rread_writer, rread);
+
+static size_t_and_error rread_writev(struct rread_writer *self, const struct iovec *iov, int iovcnt) {
+ assert(self);
+ assert(!self->written);
+ assert(iovcnt == 1);
+ assert(iov);
+ assert(iov->iov_len <= self->count);
+
struct lib9p_msg_Rread resp = {
- .tag = ctx->tag,
- .count = r.iovec.iov_len,
- .data = r.iovec.iov_base,
+ .tag = self->ctx->tag,
+ .count = iov->iov_len,
+ .data = iov->iov_base,
};
- if (resp.count > count)
- resp.count = count;
- srv_respond(ctx, read, &resp, ERROR_NULL);
+
+ srv_respond(self->ctx, read, &resp, ERROR_NULL);
+
+ self->written = true;
+ return ERROR_AND(size_t, iov->iov_len, ERROR_NULL);
+}
+
+static void handle_read_file(struct srv_req *ctx, struct srv_fidinfo *fidinfo, uint64_t offset, uint32_t count) {
+ struct rread_writer _writer = {
+ .ctx = ctx,
+ .count = (size_t) count,
+ .written = false,
+ };
+ lo_interface io_writer writer = LO_BOX(io_writer, &_writer);
+ error err = LO_CALL(fidinfo->file.io, pread, ctx, writer, offset, count);
+ assert(ERROR_IS_NULL(err) == _writer.written);
+ if (!ERROR_IS_NULL(err))
+ srv_respond(ctx, read, NULL, err);
}
#if _LIB9P_ENABLE_stat
diff --git a/lib9p/srv_include/lib9p/srv.h b/lib9p/srv_include/lib9p/srv.h
index 6b7fd60..f4e6733 100644
--- a/lib9p/srv_include/lib9p/srv.h
+++ b/lib9p/srv_include/lib9p/srv.h
@@ -106,22 +106,29 @@ struct lib9p_srv_dirent {
typedef struct lib9p_srv_dirent lib9p_srv_dirent;
DECLARE_ERROR_OR(lib9p_srv_dirent);
-/* FIXME: I don't like that the pointer returned by pread() has to
- * remain live after it returns. Perhaps a `respond()`-callback? But
- * that just reads as gross in C.
- *
- * FIXME: It would be nice if pread() could return more than 1 iovec.
+/* FIXME: It would be nice if pread() could return more than 1 iovec. This
+ * API allows it, but for the "just-1-iovec" requirement inherited from
+ * io_preader_to. We enforce this requirement because otherwise we wouldn't
+ * know at compile-time how big the iovec array in lib9p_Rmsg_send_buf needs
+ * to be.
*/
#define lib9p_srv_fio_LO_IFACE /*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/ \
LO_FUNC(struct lib9p_qid , ioqid ) \
LO_FUNC(void , iofree ) \
LO_FUNC(uint32_t , iounit ) \
- LO_FUNC(iovec_or_error , pread , struct lib9p_srv_ctx *, \
- uint32_t byte_count, \
- uint64_t byte_offset) \
+ /** \
+ * This is similar to io_preader_to->pread_to, and must follow the \
+ * same requirements. \
+ */ \
+ LO_FUNC(error , pread , struct lib9p_srv_ctx *, \
+ lo_interface io_writer dst, \
+ uint64_t src_offset, \
+ uint32_t count) \
/** \
* If the file was append-only when fopen()ed, then byte_offset will \
* always be 0. \
+ * \
+ * This similar to io_pwrite, but a short-write is not an error. \
*/ \
LO_FUNC(uint32_t_or_error , pwrite , struct lib9p_srv_ctx *, \
void *buf, \
diff --git a/lib9p/tests/test_server/fs_flush.c b/lib9p/tests/test_server/fs_flush.c
index 41156ba..0ae905f 100644
--- a/lib9p/tests/test_server/fs_flush.c
+++ b/lib9p/tests/test_server/fs_flush.c
@@ -93,8 +93,8 @@ static uint32_t_or_error flush_fio_pwrite(struct flush_fio *LM_UNUSED(self),
assert_notreached("not writable");
}
-static iovec_or_error flush_fio_pread(struct flush_fio *self, struct lib9p_srv_ctx *ctx,
- uint32_t byte_count, uint64_t LM_UNUSED(byte_offset)) {
+static error flush_fio_pread(struct flush_fio *self, struct lib9p_srv_ctx *ctx,
+ lo_interface io_writer dst, uint64_t LM_UNUSED(src_offset), uint32_t byte_count) {
assert(self);
assert(ctx);
@@ -114,14 +114,11 @@ static iovec_or_error flush_fio_pread(struct flush_fio *self, struct lib9p_srv_c
/* Return */
switch (self->parent->flush_behavior) {
case FLUSH_READ:
- return ERROR_NEW_VAL(iovec, ((struct iovec){
- .iov_base = "Sloth\n",
- .iov_len = 6 < byte_count ? 6 : byte_count,
- }));
+ return io_write(dst, "Sloth\n", 6 < byte_count ? 6 : byte_count).err;
case FLUSH_ERROR:
- return ERROR_NEW_ERR(iovec, error_new(E_POSIX_EAGAIN, "request canceled by flush"));
+ return error_new(E_POSIX_EAGAIN, "request canceled by flush");
case FLUSH_SILENT:
- return ERROR_NEW_ERR(iovec, error_new(E_POSIX_ECANCELED, "request canceled by flush"));
+ return error_new(E_POSIX_ECANCELED, "request canceled by flush");
default:
assert_notreached("invalid flush_behavior");
}
diff --git a/lib9p/tests/test_server/fs_shutdown.c b/lib9p/tests/test_server/fs_shutdown.c
index 1afbaf3..22aca9e 100644
--- a/lib9p/tests/test_server/fs_shutdown.c
+++ b/lib9p/tests/test_server/fs_shutdown.c
@@ -96,7 +96,7 @@ static uint32_t_or_error shutdown_fio_pwrite(struct shutdown_fio *self, struct l
LO_CALL(LO_BOX(net_stream_listener, &self->parent->listeners[i]), close);
return ERROR_NEW_VAL(uint32_t, byte_count);
}
-static iovec_or_error shutdown_fio_pread(struct shutdown_fio *LM_UNUSED(self), struct lib9p_srv_ctx *LM_UNUSED(ctx),
- uint32_t LM_UNUSED(byte_count), uint64_t LM_UNUSED(byte_offset)) {
+static error shutdown_fio_pread(struct shutdown_fio *LM_UNUSED(self), struct lib9p_srv_ctx *LM_UNUSED(ctx),
+ lo_interface io_writer LM_UNUSED(dst), uint64_t LM_UNUSED(src_offset), uint32_t LM_UNUSED(count)) {
assert_notreached("not readable");
}
diff --git a/lib9p/tests/test_server/fs_whoami.c b/lib9p/tests/test_server/fs_whoami.c
index a282cae..5a8382a 100644
--- a/lib9p/tests/test_server/fs_whoami.c
+++ b/lib9p/tests/test_server/fs_whoami.c
@@ -120,8 +120,8 @@ static uint32_t_or_error whoami_fio_pwrite(struct whoami_fio *LM_UNUSED(self),
uint64_t LM_UNUSED(offset)) {
assert_notreached("not writable");
}
-static iovec_or_error whoami_fio_pread(struct whoami_fio *self, struct lib9p_srv_ctx *ctx,
- uint32_t byte_count, uint64_t byte_offset) {
+static error whoami_fio_pread(struct whoami_fio *self, struct lib9p_srv_ctx *ctx,
+ lo_interface io_writer dst, uint64_t byte_offset, uint32_t byte_count) {
assert(self);
assert(ctx);
@@ -134,15 +134,12 @@ static iovec_or_error whoami_fio_pread(struct whoami_fio *self, struct lib9p_srv
ctx->user->num, ctx->user->name.len, ctx->user->name.utf8);
if (byte_offset > (uint64_t)data_size)
- return ERROR_NEW_ERR(iovec, error_new(E_POSIX_EINVAL, "offset is past end-of-file length"));
+ return error_new(E_POSIX_EINVAL, "offset is past end-of-file length");
size_t beg_off = (size_t)byte_offset;
size_t end_off = beg_off + (size_t)byte_count;
if (end_off > data_size)
end_off = data_size;
- return ERROR_NEW_VAL(iovec, ((struct iovec){
- .iov_base = &self->buf[beg_off],
- .iov_len = end_off-beg_off,
- }));
+ return io_write(dst, &self->buf[beg_off], end_off-beg_off).err;
}
diff --git a/lib9p_util/static.c b/lib9p_util/static.c
index d8b8ffc..6071e03 100644
--- a/lib9p_util/static.c
+++ b/lib9p_util/static.c
@@ -202,24 +202,21 @@ static uint32_t util9p_static_fio_iounit(struct util9p_static_file *self) {
assert(self);
return 0;
}
-static iovec_or_error util9p_static_fio_pread(struct util9p_static_file *self, struct lib9p_srv_ctx *ctx,
- uint32_t byte_count, uint64_t byte_offset) {
+static error util9p_static_fio_pread(struct util9p_static_file *self, struct lib9p_srv_ctx *ctx,
+ lo_interface io_writer dst, uint64_t byte_offset, uint32_t byte_count) {
assert(self);
assert(ctx);
size_t data_size = util9p_static_file_size(self);
if (byte_offset > (uint64_t)data_size)
- return ERROR_NEW_ERR(iovec, error_new(E_POSIX_EINVAL, "offset is past end-of-file length"));
+ return error_new(E_POSIX_EINVAL, "offset is past end-of-file length");
size_t beg_off = (size_t)byte_offset;
size_t end_off = beg_off + (size_t)byte_count;
if (end_off > data_size)
end_off = data_size;
- return ERROR_NEW_VAL(iovec, ((struct iovec){
- .iov_base = &self->data_start[beg_off],
- .iov_len = end_off-beg_off,
- }));
+ return io_write(dst, &self->data_start[beg_off], end_off-beg_off).err;
}
static uint32_t_or_error util9p_static_fio_pwrite(struct util9p_static_file *self, struct lib9p_srv_ctx *ctx,
void *LM_UNUSED(buf),