diff options
-rw-r--r-- | cmd/sbc_harness/fs_harness_flash_bin.c | 189 | ||||
-rw-r--r-- | cmd/sbc_harness/fs_harness_flash_bin.h | 14 | ||||
-rw-r--r-- | cmd/sbc_harness/fs_harness_uptime_txt.c | 11 | ||||
-rw-r--r-- | lib9p/srv.c | 263 | ||||
-rw-r--r-- | lib9p/srv_include/lib9p/srv.h | 26 | ||||
-rw-r--r-- | lib9p/tests/test_server/fs_flush.c | 13 | ||||
-rw-r--r-- | lib9p/tests/test_server/fs_shutdown.c | 4 | ||||
-rw-r--r-- | lib9p/tests/test_server/fs_whoami.c | 11 | ||||
-rw-r--r-- | lib9p_util/static.c | 11 | ||||
-rw-r--r-- | libhw_generic/include/libhw/generic/io.h | 134 | ||||
-rw-r--r-- | libmisc/include/libmisc/alloc.h | 7 |
11 files changed, 445 insertions, 238 deletions
diff --git a/cmd/sbc_harness/fs_harness_flash_bin.c b/cmd/sbc_harness/fs_harness_flash_bin.c index 9a5bb2f..07675d1 100644 --- a/cmd/sbc_harness/fs_harness_flash_bin.c +++ b/cmd/sbc_harness/fs_harness_flash_bin.c @@ -26,8 +26,8 @@ LO_IMPLEMENTATION_STATIC(lib9p_srv_fio, struct flash_file, flash_file); static_assert(DATA_SIZE % FLASH_SECTOR_SIZE == 0); static_assert(DATA_HSIZE % FLASH_SECTOR_SIZE == 0); -/* There are some memcpy()s (and memcmp()s?) in here that can (and - * arguably should) be replaced with SSI DMA. */ +/* There are some memcpy()s (and memcmp()s?) in here that maybe should + * be replaced with SSI DMA. */ /* ab_flash_* (mid-level utilities for our A/B write scheme) ******************/ @@ -111,7 +111,7 @@ static void ab_flash_initialize(uint8_t *buf) { * sector in the lower half, but this function writes to the upper * half). * - * @param pos : start-position of the sector to write to, must be in the upper half of the flash + * @param pos : start-position of the sector to write to * @param dat : the FLASH_SECTOR_SIZE bytes to write */ static void ab_flash_write_sector(size_t pos, uint8_t *dat) { @@ -131,6 +131,103 @@ static void ab_flash_write_sector(size_t pos, uint8_t *dat) { log_debugln("... written"); } +/* io_preader_to, io_pwriter, io_closer ***************************************/ + +LO_IMPLEMENTATION_STATIC(io_preader_to, struct flashio, flashio); +LO_IMPLEMENTATION_STATIC(io_pwriter, struct flashio, flashio); +LO_IMPLEMENTATION_STATIC(io_closer, struct flashio, flashio); + +/** read from anywhere on the chip */ +static size_t_and_error flashio_pread_to(struct flashio *self, lo_interface io_writer dst, uoff_t _src_offset, size_t count) { + assert(self); + + if (_src_offset > DATA_SIZE) + return ERROR_AND(size_t, 0, error_new(E_POSIX_EINVAL, "offset is past the chip size")); + size_t src_offset = (size_t) _src_offset; + + if (src_offset == DATA_SIZE) + return ERROR_AND(size_t, 0, error_new(E_EOF)); + + /* Assume that somewhere down the line the pointer we pass to + * io_write() will be passed to DMA. We don't want the DMA + * engine to hit (slow) XIP (for instance, this can cause + * reads/writes to the SSP to get out of sync with eachother). + * So, copy the data to a buffer in (fast) RAM first. It's + * lame that the DMA engine can only have a DREQ on one side + * of the channel. + */ + size_t sector_base = LM_ROUND_DOWN(src_offset, FLASH_SECTOR_SIZE); + if (src_offset + count > sector_base + FLASH_SECTOR_SIZE) + count = (sector_base + FLASH_SECTOR_SIZE) - src_offset; + assert(src_offset + count <= DATA_SIZE); + + if (!self->rbuf.ok || self->rbuf.pos != sector_base) { + self->rbuf.ok = true; + self->rbuf.pos = sector_base; + memcpy(self->rbuf.dat, DATA_START+sector_base, FLASH_SECTOR_SIZE); + } + + return io_write(dst, &self->rbuf.dat[src_offset-sector_base], count); +} + +/** takes offsets in the lower half, writes to the upper half */ +static size_t_and_error flashio_pwritev(struct flashio *self, const struct iovec *iov, int iovcnt, uoff_t _offset) { + assert(self); + assert(iov); + assert(iovcnt > 0); + + size_t total_count = 0; + for (int i = 0; i < iovcnt; i++) + total_count += iov[i].iov_len; + assert(total_count > 0); + + if (_offset >= DATA_HSIZE) + return ERROR_AND(size_t, 0, error_new(E_POSIX_ENOSPC, "cannot write past half the chip size")); + size_t offset = (size_t) _offset; + + size_t total_done = 0; + for (int i = 0; i < iovcnt; i++) { + size_t iov_done = 0; + while (iov_done < iov[i].iov_len) { + if (offset >= DATA_HSIZE) + return ERROR_AND(size_t, total_done, error_new(E_POSIX_ENOSPC, "cannot write past half the chip size")); + size_t sector_base = LM_ROUND_DOWN(offset, FLASH_SECTOR_SIZE); + size_t len = iov[i].iov_len - iov_done; + if (offset + len > sector_base + FLASH_SECTOR_SIZE) + len = (sector_base + FLASH_SECTOR_SIZE) - offset; + assert(offset + len <= DATA_HSIZE); + + if (self->wbuf.ok && self->wbuf.pos != sector_base) + ab_flash_write_sector(self->wbuf.pos, self->wbuf.dat); + if (!self->wbuf.ok || self->wbuf.pos != sector_base) { + self->wbuf.ok = true; + self->wbuf.pos = sector_base; + if (len != FLASH_SECTOR_SIZE) + /* Don't bother with a read if we're just going to overwrite it. */ + memcpy(self->wbuf.dat, DATA_START+DATA_HSIZE+sector_base, FLASH_SECTOR_SIZE); + } + memcpy(&self->wbuf.dat[offset-sector_base], iov[i].iov_base+iov_done, len); + total_done += len; + iov_done += len; + offset += len; + self->written = true; + } + } + return ERROR_AND(size_t, total_done, ERROR_NULL); +} + +static error flashio_close(struct flashio *self) { + assert(self); + + if (self->wbuf.ok) + ab_flash_write_sector(self->wbuf.pos, self->wbuf.dat); + + if (self->written) + ab_flash_finalize(self->wbuf.dat); + + return ERROR_NULL; +} + /* srv_file *******************************************************************/ void flash_file_free(struct flash_file *self) { @@ -179,23 +276,18 @@ error flash_file_remove(struct flash_file *self, struct lib9p_srv_ctx *ctx) { LIB9P_SRV_NOTDIR(, struct flash_file, flash_file); lib9p_srv_fio_or_error flash_file_fopen(struct flash_file *self, struct lib9p_srv_ctx *ctx, - bool rd, bool wr, bool trunc) { + bool LM_UNUSED(rd), bool wr, bool trunc) { assert(self); assert(ctx); - if (rd) { - self->rbuf.ok = false; - } - + memset(&self->io, 0, sizeof(self->io)); if (wr) { if (trunc) { - ab_flash_initialize_zero(self->wbuf.dat); - self->written = true; + ab_flash_initialize_zero(self->io.wbuf.dat); + self->io.written = true; } else { - ab_flash_initialize(self->wbuf.dat); - self->written = false; + ab_flash_initialize(self->io.wbuf.dat); } - self->wbuf.ok = false; } return ERROR_NEW_VAL(lib9p_srv_fio, LO_BOX(lib9p_srv_fio, self)); @@ -214,47 +306,16 @@ static uint32_t flash_file_iounit(struct flash_file *self) { static void flash_file_iofree(struct flash_file *self) { assert(self); - if (self->wbuf.ok) - ab_flash_write_sector(self->wbuf.pos, self->wbuf.dat); - - if (self->written) - ab_flash_finalize(self->wbuf.dat); + error err = flashio_close(&self->io); + assert(ERROR_IS_NULL(err)); } -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")); - - /* Assume that somewhere down the line the iovec we return - * will be passed to DMA. We don't want the DMA engine to hit - * (slow) XIP (for instance, this can cause reads/writes to - * the SSP to get out of sync with eachother), so copy the - * data to a buffer in (fast) RAM first. It's lame that the - * 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, - })); - 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; - assert(byte_offset + byte_count <= DATA_SIZE); - - if (!self->rbuf.ok || self->rbuf.pos != sector_base) { - self->rbuf.ok = true; - self->rbuf.pos = sector_base; - 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 flashio_pread_to(&self->io, dst, byte_offset, byte_count).err; } /* TODO: Short/corrupt writes are dangerous. This should either (1) @@ -267,28 +328,10 @@ static uint32_t_or_error flash_file_pwrite(struct flash_file *self, struct lib9p assert(self); assert(ctx); - if (byte_offset > DATA_HSIZE) - return ERROR_NEW_ERR(uint32_t, error_new(E_POSIX_EINVAL, "offset is past half the chip size")); - if (byte_count == 0) - return ERROR_NEW_VAL(uint32_t, 0); - if (byte_offset == DATA_HSIZE) - return ERROR_NEW_ERR(uint32_t, error_new(E_POSIX_EINVAL, "offset is at half the chip size")); - - 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; - assert(byte_offset + byte_count < DATA_HSIZE); - - if (self->wbuf.ok && self->wbuf.pos != sector_base) - ab_flash_write_sector(self->wbuf.pos, self->wbuf.dat); - if (!self->wbuf.ok || self->wbuf.pos != sector_base) { - self->wbuf.ok = true; - self->wbuf.pos = sector_base; - if (byte_count != FLASH_SECTOR_SIZE) - memcpy(self->wbuf.dat, DATA_START+DATA_HSIZE+sector_base, FLASH_SECTOR_SIZE); - } - memcpy(&self->wbuf.dat[byte_offset-sector_base], buf, byte_count); - - self->written = true; - return ERROR_NEW_VAL(uint32_t, byte_count); + size_t_and_error r = flashio_pwritev(&self->io, + &((struct iovec){.iov_base = buf, .iov_len = byte_count}), 1, + byte_offset); + if (r.size_t == 0 && !ERROR_IS_NULL(r.err)) + return ERROR_NEW_ERR(uint32_t, r.err); + return ERROR_NEW_VAL(uint32_t, (uint32_t) r.size_t); } diff --git a/cmd/sbc_harness/fs_harness_flash_bin.h b/cmd/sbc_harness/fs_harness_flash_bin.h index 148a446..050f4a7 100644 --- a/cmd/sbc_harness/fs_harness_flash_bin.h +++ b/cmd/sbc_harness/fs_harness_flash_bin.h @@ -11,17 +11,21 @@ #include <lib9p/srv.h> -struct flash_file { - char *name; - uint64_t pathnum; - - BEGIN_PRIVATE(FS_HARNESS_FLASH_BIN); +struct flashio { bool written; struct { bool ok; size_t pos; uint8_t dat[FLASH_SECTOR_SIZE]; } wbuf, rbuf; +}; + +struct flash_file { + char *name; + uint64_t pathnum; + + BEGIN_PRIVATE(FS_HARNESS_FLASH_BIN); + struct flashio io; END_PRIVATE(FS_HARNESS_FLASH_BIN); }; LO_IMPLEMENTATION_H(lib9p_srv_file, struct flash_file, flash_file); 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 e938dcb..03d7b93 100644 --- a/lib9p/srv.c +++ b/lib9p/srv.c @@ -1175,25 +1175,33 @@ static inline struct lib9p_stat srv_stat_to_net_stat(struct lib9p_srv_stat in) { }; } +static void handle_read_file(struct srv_req *ctx, struct srv_fidinfo *fidinfo, uint64_t offset, uint32_t count); +static void handle_read_dir(struct srv_req *ctx, struct srv_fidinfo *fidinfo, uint64_t offset, uint32_t count); + static void handle_Tread(struct srv_req *ctx, struct lib9p_msg_Tread *req) { - srv_handler_common(ctx, read, req); - char *heap = NULL; + assert(ctx); + assert(req); /* 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); /* Check that the FID is valid for this. */ struct srv_fidinfo *fidinfo = map_load(&ctx->parent_sess->fids, req->fid); if (!fidinfo) { - err = error_new(E_POSIX_EBADF, "bad file number ", req->fid); - goto tread_return; + srv_respond(ctx, read, NULL, error_new(E_POSIX_EBADF, "bad file number ", req->fid)); + return; } if (!(fidinfo->flags & FIDFLAG_OPEN_R)) { - err = error_new(E_POSIX_EINVAL, "FID not open for reading"); - goto tread_return; + srv_respond(ctx, read, NULL, error_new(E_POSIX_EINVAL, "FID not open for reading")); + return; } /* Do it. */ @@ -1201,119 +1209,174 @@ static void handle_Tread(struct srv_req *ctx, switch (fidinfo->type) { case SRV_FILETYPE_DIR: #if _LIB9P_ENABLE_stat - /* Seek. */ - if (req->offset == 0) { - fidinfo->dir.idx = 0; - fidinfo->dir.off = 0; - fidinfo->dir.buffered_dirent = (struct lib9p_srv_dirent){}; - } else if (req->offset != fidinfo->dir.off) { - err = error_new(E_POSIX_EINVAL, "invalid offset (must be 0 or ", fidinfo->dir.off, "): ", req->offset); - goto tread_return; - } - /* Read. */ - resp.data = heap = malloc(req->count); - resp.count = 0; - struct srv_pathinfo *dir_pathinfo = NULL; - for (;;) { - lo_interface lib9p_srv_file member_file = {}; - struct lib9p_srv_dirent member_dirent; - if (fidinfo->dir.buffered_dirent.name.len) { - member_dirent = fidinfo->dir.buffered_dirent; - } else { - lib9p_srv_dirent_or_error member_dirent_r; - member_dirent_r = LO_CALL(fidinfo->dir.io, dread, ctx, fidinfo->dir.idx); - if (member_dirent_r.is_err) { - if (!resp.count) { - err = member_dirent_r.err; - goto tread_return; - } - error_cleanup(&member_dirent_r.err); + handle_read_dir(ctx, fidinfo, req->offset, req->count); +#else + assert_notreached("Tread for directory on protocol version without that"); +#endif + break; + case SRV_FILETYPE_FILE: + handle_read_file(ctx, fidinfo, req->offset, req->count); + break; + case SRV_FILETYPE_AUTH: + assert_notreached("TODO: auth not yet implemented"); + break; + } + ctx->user = srv_userid_decref(ctx->user); +} + +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 = self->ctx->tag, + .count = iov->iov_len, + .data = iov->iov_base, + }; + + 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 +static void handle_read_dir(struct srv_req *ctx, struct srv_fidinfo *fidinfo, uint64_t offset, uint32_t count) { + /* Seek. */ + if (offset == 0) { + fidinfo->dir.idx = 0; + fidinfo->dir.off = 0; + fidinfo->dir.buffered_dirent = (struct lib9p_srv_dirent){}; + } else if (offset != fidinfo->dir.off) { + srv_respond(ctx, read, NULL, error_new(E_POSIX_EINVAL, "invalid offset (must be 0 or ", fidinfo->dir.off, "): ", offset)); + return; + } + + /* Allocate. */ + [[gnu::cleanup(heap_cleanup)]] void *heap = NULL; + struct lib9p_msg_Rread resp = { + .tag = ctx->tag, + .data = heap = malloc(count), + .count = 0, + }; + + /* Read. */ + struct srv_pathinfo *dir_pathinfo = NULL; + for (;;) { + /* 1. Call ->dread() to get `member_dirent`. */ + struct lib9p_srv_dirent member_dirent; + if (fidinfo->dir.buffered_dirent.name.len) { + member_dirent = fidinfo->dir.buffered_dirent; + } else { + lib9p_srv_dirent_or_error r = LO_CALL(fidinfo->dir.io, dread, ctx, fidinfo->dir.idx); + if (r.is_err) { + if (resp.count) { + /* Just do a short-read; discard the error. */ + error_cleanup(&r.err); break; } - member_dirent = member_dirent_r.lib9p_srv_dirent; + srv_respond(ctx, read, NULL, r.err); + return; } - if (!member_dirent.name.len) - break; - struct lib9p_srv_stat member_stat; + member_dirent = r.lib9p_srv_dirent; + } + if (!member_dirent.name.len) + /* end-of-directory */ + break; + + /* 2. Call ->dwalk() to get the `member_file` object to call ->stat() on. */ + lo_interface lib9p_srv_file member_file; + bool free_member_file; + { struct srv_pathinfo *member_pathinfo = map_load(&ctx->parent_sess->paths, member_dirent.qid.path); if (member_pathinfo) { - lib9p_srv_stat_or_error r = LO_CALL(member_pathinfo->file, stat, ctx); - if (r.is_err) { - err = r.err; - goto member_err; - } - member_stat = r.lib9p_srv_stat; + member_file = member_pathinfo->file; + free_member_file = false; } else { if (!dir_pathinfo) dir_pathinfo = map_load(&ctx->parent_sess->paths, fidinfo->path); assert(dir_pathinfo); - lib9p_srv_file_or_error file_r = LO_CALL(dir_pathinfo->file, dwalk, ctx, member_dirent.name); - if (file_r.is_err) { - err = file_r.err; - goto member_err; - } - member_file = file_r.lib9p_srv_file; - lib9p_srv_stat_or_error stat_r = LO_CALL(member_file, stat, ctx); - if (stat_r.is_err) { - err = stat_r.err; - goto member_err; + lib9p_srv_file_or_error r = LO_CALL(dir_pathinfo->file, dwalk, ctx, member_dirent.name); + if (r.is_err) { + if (resp.count) { + /* Just do a short-read; discard the error. */ + error_cleanup(&r.err); + break; + } + srv_respond(ctx, read, NULL, r.err); + return; } - member_stat = stat_r.lib9p_srv_stat; - } - if (false) { - member_err: - if (!LO_IS_NULL(member_file)) - LO_CALL(member_file, free); - if (!resp.count) - goto tread_return; - error_cleanup(&err); - break; + member_file = r.lib9p_srv_file; + free_member_file = true; } - lib9p_srv_stat_assert(member_stat); - struct lib9p_stat member_netstat = srv_stat_to_net_stat(member_stat); - uint32_t nbytes = lib9p_stat_marshal(&ctx->basectx, req->count-resp.count, &member_netstat, - (uint8_t *)&resp.data[resp.count]); - if (!LO_IS_NULL(member_file)) + } + + /* 3. Call ->stat() to get `member_stat``. */ + struct lib9p_srv_stat member_stat; + { + lib9p_srv_stat_or_error r = LO_CALL(member_file, stat, ctx); + if (free_member_file) LO_CALL(member_file, free); - if (!nbytes) { - if (!resp.count) { - err = error_new(E_POSIX_ERANGE, "stat object does not fit into negotiated max message size"); - goto tread_return; + if (r.is_err) { + if (resp.count) { + /* Just do a short-read; discard the error. */ + error_cleanup(&r.err); + break; } + srv_respond(ctx, read, NULL, r.err); + return; + } + member_stat = r.lib9p_srv_stat; + lib9p_srv_stat_assert(member_stat); + } + + /* 4. Encode `member_stat` into `resp.data` and increment `resp.count`. */ + struct lib9p_stat member_netstat = srv_stat_to_net_stat(member_stat); + uint32_t nbytes = lib9p_stat_marshal(&ctx->basectx, count-resp.count, &member_netstat, + (uint8_t *)&resp.data[resp.count]); + if (!nbytes) { + if (resp.count) { + /* Just do a short-read; discard the error. + * But save the member_dirent for next time. */ fidinfo->dir.buffered_dirent = member_dirent; break; } - resp.count += nbytes; - fidinfo->dir.idx++; - fidinfo->dir.off += nbytes; - fidinfo->dir.buffered_dirent = (struct lib9p_srv_dirent){}; - } -#else - assert_notreached("Tread for directory on protocol version without that"); -#endif - break; - case SRV_FILETYPE_FILE: - iovec_or_error iov = LO_CALL(fidinfo->file.io, pread, ctx, req->count, req->offset); - if (iov.is_err) { - err = iov.err; - } else { - resp.count = iov.iovec.iov_len; - resp.data = iov.iovec.iov_base; - if (resp.count > req->count) - resp.count = req->count; + srv_respond(ctx, read, NULL, + error_new(E_POSIX_ERANGE, "stat object does not fit into negotiated max message size")); + return; } - break; - case SRV_FILETYPE_AUTH: - assert_notreached("TODO: auth not yet implemented"); - break; + resp.count += nbytes; + fidinfo->dir.idx++; + fidinfo->dir.off += nbytes; + fidinfo->dir.buffered_dirent = (struct lib9p_srv_dirent){}; } - tread_return: - if (ctx->user) - ctx->user = srv_userid_decref(ctx->user); - srv_respond(ctx, read, &resp, err); - if (heap) - free(heap); + srv_respond(ctx, read, &resp, ERROR_NULL); } +#endif static void handle_Twrite(struct srv_req *ctx, struct lib9p_msg_Twrite *req) { diff --git a/lib9p/srv_include/lib9p/srv.h b/lib9p/srv_include/lib9p/srv.h index 1114950..f4e6733 100644 --- a/lib9p/srv_include/lib9p/srv.h +++ b/lib9p/srv_include/lib9p/srv.h @@ -99,9 +99,6 @@ void lib9p_srv_stat_assert(struct lib9p_srv_stat stat); /* interface definitions ******************************************************/ -typedef struct iovec iovec; -DECLARE_ERROR_OR(iovec); - struct lib9p_srv_dirent { struct lib9p_qid qid; struct lib9p_s name; @@ -109,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), diff --git a/libhw_generic/include/libhw/generic/io.h b/libhw_generic/include/libhw/generic/io.h index ebbd6cb..c615932 100644 --- a/libhw_generic/include/libhw/generic/io.h +++ b/libhw_generic/include/libhw/generic/io.h @@ -7,14 +7,23 @@ #ifndef _LIBHW_GENERIC_IO_H_ #define _LIBHW_GENERIC_IO_H_ -#include <stddef.h> /* for size_t */ -#include <stdint.h> /* for uintptr_t */ +#include <stddef.h> /* for size_t */ +#include <stdint.h> /* for uintptr_t, uint{n}_t, UINT{n}_MAX */ #include <libmisc/error.h> #include <libmisc/obj.h> /* structs ********************************************************************/ +/* uoff_t ==========================================================*/ + +typedef uint64_t uoff_t; +#define UOFF_MAX UINT64_MAX +DECLARE_ERROR_OR(uoff_t); +DECLARE_ERROR_AND(uoff_t); + +/* iovec ===========================================================*/ + #if __unix__ #include <sys/uio.h> #else @@ -23,6 +32,11 @@ struct iovec { size_t iov_len; }; #endif +typedef struct iovec iovec; +DECLARE_ERROR_OR(iovec); +DECLARE_ERROR_AND(iovec); + +/* duplex_iovec ====================================================*/ #define IOVEC_DISCARD ((void*)(~((uintptr_t)0))) @@ -36,8 +50,11 @@ struct duplex_iovec { void *iov_write_from; size_t iov_len; }; +typedef struct duplex_iovec duplex_iovec; +DECLARE_ERROR_OR(duplex_iovec); +DECLARE_ERROR_AND(duplex_iovec); -/* utilities ******************************************************************/ +/* iovec utilities ************************************************************/ /* If byte_max_cnt == 0, then there is no maximum. */ @@ -57,9 +74,27 @@ void io_slice_wr_to_duplex(struct duplex_iovec *dst, const struct iovec *src, in /* basic interfaces ***********************************************************/ +/* + * Conventions: + * + * - Naming: + * + The "p"[osition] prefix means an explicit `uoff_t offset`, + * instead of using an internal cursor. + * + The "v"[ec] suffix means a sequence of iovecs, instead of a + * simple buf+len. + * + * - Errors: + * + Short *reads* are *not* errors (and so return size_t *or* + * error). + * + Short *writes* *are* errors (and so return size_t *and* + * (possibly-null) error). + */ + +/* read ============================================================*/ + /** * Return bytes-read on success. A short read is *not* an error - * (unlike writev). + * (unlike `write` methods). * * It is invalid to call readv when the sum length of iovecs is 0. */ @@ -70,7 +105,22 @@ LO_INTERFACE(io_reader); #define io_read(r, buf, count) LO_CALL(r, readv, &((struct iovec){.iov_base = buf, .iov_len = count}), 1) /** - * Return bytes-written. A short write *is* an error (unlike readv) + * Returns bytes-read on success. A short read is *not* an error + * (unlike `write` methods). + * + * It is invalid to call preadv when the sum length of iovecs is 0. + */ +#define io_preader_LO_IFACE \ + LO_FUNC(size_t_or_error, preadv, const struct iovec *iov, int iovcnt, uoff_t offset) +LO_INTERFACE(io_preader); +#define io_preadv(r, iov, iovcnt, off) LO_CALL(r, preadv, iov, iovcnt, off) +#define io_pread(r, buf, count, off) LO_CALL(r, preadv, &((struct iovec){.iov_base = buf, .iov_len = count}), 1, off) + +/* write ===========================================================*/ + +/** + * Return bytes-written. A short write *is* an error (unlike `read` + * methods). * * Writes are *not* guaranteed to be atomic, so if you have concurrent * writers then you should arrange for a mutex to protect the writer. @@ -83,21 +133,26 @@ LO_INTERFACE(io_writer); #define io_writev(w, iov, iovcnt) LO_CALL(w, writev, iov, iovcnt) #define io_write(w, buf, count) LO_CALL(w, writev, &((struct iovec){.iov_base = buf, .iov_len = count}), 1) -#define io_closer_LO_IFACE \ - LO_FUNC(error, close) -LO_INTERFACE(io_closer); -#define io_close(c) LO_CALL(c, close) +/** + * Returns bytes-written. A short write *is* an error (unlike `read` + * methods). + * + * Writes are *not* guaranteed to be atomic, so if you have concurrent + * writers then you should arrange for a mutex to protect the writer. + * + * It is invalid to call writev when the sum length of iovecs is 0. + */ +#define io_pwriter_LO_IFACE \ + LO_FUNC(size_t_and_error, pwritev, const struct iovec *iov, int iovcnt, uoff_t offset) +LO_INTERFACE(io_pwriter); +#define io_pwritev(r, iov, iovcnt, off) LO_CALL(r, pwritev, iov, iovcnt, off) +#define io_pwrite(r, buf, count, off) LO_CALL(r, pwritev, &((struct iovec){.iov_base = buf, .iov_len = count}), 1, off) -#define io_bidi_closer_LO_IFACE \ - LO_NEST(io_closer) \ - LO_FUNC(error, close_read) \ - LO_FUNC(error, close_write) -LO_INTERFACE(io_bidi_closer); -#define io_close_read(c) LO_CALL(c, close_read) -#define io_close_write(c) LO_CALL(c, close_write) +/* readwrite =======================================================*/ /** - * A short read/write *is* an error (like writev and unlike readv). + * A short read/write *is* an error (like `write` methods and unlike + * `read` methods). * * Reads/writes are *not* guaranteed to be atomic, so if you have * concurrent readers/writers then you should arrange for a mutex to @@ -108,10 +163,53 @@ LO_INTERFACE(io_bidi_closer); #define io_duplex_readwriter_LO_IFACE \ LO_FUNC(size_t_and_error, readwritev, const struct duplex_iovec *iov, int iovcnt) LO_INTERFACE(io_duplex_readwriter); - #define io_readwritev(rw, iov, iovcnt) \ LO_CALL(rw, readwritev, iov, iovcnt) +/* read then write =================================================*/ + +/** + * LO_CALL(src, pread_to, dst, src_offset, count) is functionally: + * + * size_t_and_error copy(lo_interface io_writer dst, lo_interface io_preader src, uoff_t src_offset, size_t count) { + * buf = malloc(count); + * size_t_or_error read_r = io_pread(src, buf, count, src_offset); + * if (read_r.is_err) + * return ERROR_AND(size_t, 0, read_r.err); + * size_t_and_error write_r = io_write(dst, buf, read_r.size_t); + * free(buf); + * return write_r; + * } + * + * except that it does not need to allocate a buffer. That is: It + * allows the reader to dump its data directly to the writer. This + * allows us to save a copy when the reader already has an in-memory + * buffer containing the data. + * + * The above code defines when a short read/write is an error or not. + * + * It must call writev() exactly 0-or-1 times, and if it does call it, + * then it must be called with exactly 1 iovec. + */ +#define io_preader_to_LO_IFACE \ + LO_FUNC(size_t_and_error, pread_to, lo_interface io_writer dst, uoff_t src_offset, size_t count) +LO_INTERFACE(io_preader_to); + +/* close ===========================================================*/ + +#define io_closer_LO_IFACE \ + LO_FUNC(error, close) +LO_INTERFACE(io_closer); +#define io_close(c) LO_CALL(c, close) + +#define io_bidi_closer_LO_IFACE \ + LO_NEST(io_closer) \ + LO_FUNC(error, close_read) \ + LO_FUNC(error, close_write) +LO_INTERFACE(io_bidi_closer); +#define io_close_read(c) LO_CALL(c, close_read) +#define io_close_write(c) LO_CALL(c, close_write) + /* aggregate interfaces *******************************************************/ #define io_readwriter_LO_IFACE \ diff --git a/libmisc/include/libmisc/alloc.h b/libmisc/include/libmisc/alloc.h index afddbce..34becdb 100644 --- a/libmisc/include/libmisc/alloc.h +++ b/libmisc/include/libmisc/alloc.h @@ -23,4 +23,11 @@ #define heap_alloc(N, TYP) ((TYP *)calloc(N, sizeof(TYP))) +static inline void heap_cleanup(void **ptrptr) { + if (!ptrptr) + return; + free(*ptrptr); + *ptrptr = NULL; +} + #endif /* _LIBMISC_ALLOC_H_ */ |