From eb4e0bc7dd140b356a62071bf8e0427fc0cee816 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Sat, 12 Oct 2024 17:53:05 -0600 Subject: get read working correctly --- cmd/srv9p/CMakeLists.txt | 37 ++++++- cmd/srv9p/main.c | 49 ++++++--- cmd/srv9p/static.c | 144 ------------------------ cmd/srv9p/static.h | 29 ----- cmd/srv9p/static.h.gen | 6 + cmd/srv9p/static/Documentation/x | 1 + cmd/srv9p/static/README.md | 1 + cmd/srv9p/static9p.c | 232 +++++++++++++++++++++++++++++++++++++++ cmd/srv9p/static9p.h | 41 +++++++ lib9p/include/lib9p/9p.h | 11 ++ lib9p/srv.c | 66 ++++++----- 11 files changed, 400 insertions(+), 217 deletions(-) delete mode 100644 cmd/srv9p/static.c delete mode 100644 cmd/srv9p/static.h create mode 100755 cmd/srv9p/static.h.gen create mode 100644 cmd/srv9p/static/Documentation/x create mode 100644 cmd/srv9p/static/README.md create mode 100644 cmd/srv9p/static9p.c create mode 100644 cmd/srv9p/static9p.h diff --git a/cmd/srv9p/CMakeLists.txt b/cmd/srv9p/CMakeLists.txt index 8aadb28..e9d4097 100644 --- a/cmd/srv9p/CMakeLists.txt +++ b/cmd/srv9p/CMakeLists.txt @@ -3,10 +3,16 @@ # Copyright (C) 2024 Luke T. Shumaker # SPDX-Licence-Identifier: AGPL-3.0-or-later +set(static_srcs + static/README.md + static/Documentation/x +) + add_executable(srv9p main.c - static.c + static9p.c ) +target_include_directories(srv9p PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) target_include_directories(srv9p PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/config) target_link_libraries(srv9p libcr @@ -14,3 +20,32 @@ target_link_libraries(srv9p libnetio lib9p ) + +set(static_objs) +foreach(static_src ${static_srcs}) + add_custom_command( + OUTPUT "${static_src}.obj" + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + COMMAND dirname -- ${CMAKE_CURRENT_BINARY_DIR}/${static_src}.obj | xargs mkdir -p -- + COMMAND ${CMAKE_LINKER} -r -b binary -o ${CMAKE_CURRENT_BINARY_DIR}/${static_src}.obj ${static_src} + DEPENDS "${static_src}" + ) + list(APPEND static_objs "${static_src}.obj") +endforeach() + +set_source_files_properties(${static_objs} PROPERTIES + EXTERNAL_OBJECT true + GENERATED true +) + +set_source_files_properties(static.h PROPERTIES + GENERATED true +) + +add_custom_command( + OUTPUT static.h + DEPENDS ${static_objs} static.h.gen + COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/static.h.gen ${static_objs} >static.h +) + +target_sources(srv9p PRIVATE ${static_objs} static.h) diff --git a/cmd/srv9p/main.c b/cmd/srv9p/main.c index 4aaae28..8609f36 100644 --- a/cmd/srv9p/main.c +++ b/cmd/srv9p/main.c @@ -5,6 +5,7 @@ #include #include +#include "static9p.h" #include "static.h" /* configuration **************************************************************/ @@ -19,26 +20,48 @@ #define UNUSED(name) /* name __attribute__((unused)) */ -static struct static_dir root = { - .header = { .vtable = &static_dir_vtable }, +static struct static_file readme = { + .metadata = { + .header = { .vtable = &static_file_vtable }, + + .u_name = "root", .u_num = 0, /* owner user */ + .g_name = "root", .g_num = 0, /* owner group */ + .m_name = "root", .m_num = 0, /* last-modified-by user */ - .u_name = "root", .u_num = 0, /* owner user */ - .g_name = "root", .g_num = 0, /* owner group */ - .m_name = "root", .m_num = 0, /* last-modified-by user */ + .pathnum = __COUNTER__, + .name = "README.md", + .perm = 0444, + .atime = 1728337905, + .mtime = 1728337904, + }, - .pathnum = __COUNTER__, - .name = "", - .perm = 0555, - .atime = 1728337905, - .mtime = 1728337904, + .data_start = _binary_static_README_md_start, + .data_end = _binary_static_README_md_end, +}; + +static struct static_dir root = { + .metadata = { + .header = { .vtable = &static_dir_vtable }, + + .u_name = "root", .u_num = 0, /* owner user */ + .g_name = "root", .g_num = 0, /* owner group */ + .m_name = "root", .m_num = 0, /* last-modified-by user */ + + .pathnum = __COUNTER__, + .name = "", + .perm = 0555, + .atime = 1728337905, + .mtime = 1728337904, + }, .members = { + &readme.metadata.header, NULL, }, }; -struct lib9p_srv_file *fs_root(struct lib9p_srv_ctx *UNUSED(ctx), char *UNUSED(treename)) { - return &root.header; +static struct lib9p_srv_file *get_root(struct lib9p_srv_ctx *UNUSED(ctx), char *UNUSED(treename)) { + return &root.metadata.header; } int main() { @@ -48,7 +71,7 @@ int main() { struct lib9p_srv srv = { .sockfd = sock, - .rootdir = fs_root, + .rootdir = get_root, }; for (int i = 0; i < CONFIG_NETIO_NUM_CONNS; i++) diff --git a/cmd/srv9p/static.c b/cmd/srv9p/static.c deleted file mode 100644 index a6da53f..0000000 --- a/cmd/srv9p/static.c +++ /dev/null @@ -1,144 +0,0 @@ -#include - -#include "static.h" - -#define UNUSED(name) /* name __attribute__((unused)) */ -#define p9_str(cstr) ((struct lib9p_s){ .len = strlen(cstr), .utf8 = cstr }) -#define p9_nulstr ((struct lib9p_s){ .len = 0, .utf8 = NULL }) - -/******************************************************************************/ - -static struct lib9p_srv_file *static_dir_clone(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) { - assert(ctx); - struct static_dir *file = (struct static_dir *)_file; - assert(file); - - return &file->header; -} - -static void static_dir_free(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) { - assert(ctx); - struct static_dir *file = (struct static_dir *)_file; - assert(file); - - /* do nothing */ -} - -static uint32_t static_dir_io(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file, lib9p_o_t UNUSED(flags)) { - assert(ctx); - struct static_dir *file = (struct static_dir *)_file; - assert(file); - - return 1; -} - -static struct lib9p_stat static_dir_stat(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) { - assert(ctx); - struct static_dir *file = (struct static_dir *)_file; - assert(file); - - return (struct lib9p_stat){ - .kern_type = 0, - .kern_dev = 0, - .file_qid = { - .type = LIB9P_QT_DIR, - .vers = 1, - .path = file->pathnum, - }, - .file_mode = LIB9P_DM_DIR | (file->perm & 0555), - .file_atime = file->atime, - .file_mtime = file->mtime, - .file_size = 0, - .file_name = p9_str(file->name), - .file_owner_uid = p9_str(file->u_name), - .file_owner_gid = p9_str(file->g_name), - .file_last_modified_uid = p9_str(file->m_name), - .file_extension = p9_nulstr, - .file_owner_n_uid = file->u_num, - .file_owner_n_gid = file->g_num, - .file_last_modified_n_uid = file->m_num, - }; -} - -static void static_dir_wstat(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file, - struct lib9p_stat UNUSED(new)) { - assert(ctx); - struct static_dir *file = (struct static_dir *)_file; - assert(file); - - lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem"); -} - -static void static_dir_remove(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) { - assert(ctx); - struct static_dir *file = (struct static_dir *)_file; - assert(file); - - lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem"); -} - - -static struct lib9p_srv_file *static_dir_dopen(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_dir, - char *UNUSED(childname)) { - assert(ctx); - struct static_dir *dir = (struct static_dir *)_dir; - assert(dir); - - lib9p_error(&ctx->basectx, LINUX_EFAULT, "TODO: dopen"); - return NULL; -} - -static struct lib9p_srv_file *static_dir_dcreate(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_dir, - char *UNUSED(childname), - lib9p_dm_t UNUSED(perm), lib9p_o_t UNUSED(flags)) { - assert(ctx); - struct static_dir *dir = (struct static_dir *)_dir; - assert(dir); - - lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem"); - return NULL; -} - -static size_t static_dir_dread(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_dir, - uint8_t *buf, - uint32_t byte_count, - size_t _obj_offset) { - assert(ctx); - struct static_dir *dir = (struct static_dir *)_dir; - assert(dir); - - uint32_t byte_offset = 0; - size_t obj_offset = _obj_offset; - while (dir->members[obj_offset]) { - struct lib9p_srv_file *filep = dir->members[obj_offset]; - struct lib9p_stat stat = filep->vtable->stat(ctx, filep); - if (lib9p_ctx_has_error(&ctx->basectx)) - break; - uint32_t nbytes = lib9p_marshal_stat(&ctx->basectx, byte_count-byte_offset, &stat, - &buf[byte_offset]); - if (!nbytes) { - if (obj_offset == _obj_offset) - lib9p_error(&ctx->basectx, - LINUX_ERANGE, "stat object does not fit into negotiated max message size"); - break; - } - byte_offset += nbytes; - obj_offset++; - } - return obj_offset - _obj_offset; -} - -struct lib9p_srv_file_vtable static_dir_vtable = { - .clone = static_dir_clone, - .free = static_dir_free, - - .io = static_dir_io, - .stat = static_dir_stat, - .wstat = static_dir_wstat, - .remove = static_dir_remove, - - .dopen = static_dir_dopen, - .dcreate = static_dir_dcreate, - - .dread = static_dir_dread, -}; diff --git a/cmd/srv9p/static.h b/cmd/srv9p/static.h deleted file mode 100644 index afb5a3f..0000000 --- a/cmd/srv9p/static.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef _STATIC_H_ -#define _STATIC_H_ - -#include - -extern struct lib9p_srv_file_vtable static_dir_vtable; - -struct static_dir { - struct lib9p_srv_file header; - - char *u_name; - uint32_t u_num; - char *g_name; - uint32_t g_num; - char *m_name; - uint32_t m_num; - - uint64_t pathnum; - char *name; - lib9p_dm_t perm; - uint32_t atime, mtime; - - /* NULL-terminated */ - struct lib9p_srv_file *members[]; -}; - -//extern struct lib9p_srv_io_dir_vtable static_dir_io_vtable; - -#endif /* _STATIC_H_ */ diff --git a/cmd/srv9p/static.h.gen b/cmd/srv9p/static.h.gen new file mode 100755 index 0000000..1c4d7b1 --- /dev/null +++ b/cmd/srv9p/static.h.gen @@ -0,0 +1,6 @@ +#!/bin/sh + +nm --format=posix "$@" | + sed -n -E \ + -e 's/(.*_(end|start)) D .*/extern char \1[];/p' \ + -e 's/(.*_size) A .*/extern size_t \1;/p' diff --git a/cmd/srv9p/static/Documentation/x b/cmd/srv9p/static/Documentation/x new file mode 100644 index 0000000..257cc56 --- /dev/null +++ b/cmd/srv9p/static/Documentation/x @@ -0,0 +1 @@ +foo diff --git a/cmd/srv9p/static/README.md b/cmd/srv9p/static/README.md new file mode 100644 index 0000000..af5626b --- /dev/null +++ b/cmd/srv9p/static/README.md @@ -0,0 +1 @@ +Hello, world! diff --git a/cmd/srv9p/static9p.c b/cmd/srv9p/static9p.c new file mode 100644 index 0000000..e20921d --- /dev/null +++ b/cmd/srv9p/static9p.c @@ -0,0 +1,232 @@ +#include + +#include "static9p.h" + +#define UNUSED(name) /* name __attribute__((unused)) */ +#define p9_str(cstr) ((struct lib9p_s){ .len = strlen(cstr), .utf8 = cstr }) +#define p9_nulstr ((struct lib9p_s){ .len = 0, .utf8 = NULL }) + +/* common metadata ************************************************************/ + +static struct lib9p_srv_file *static_metadata_clone(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) { + assert(ctx); + struct static_metadata *file = (struct static_metadata *)_file; + assert(file); + + return &file->header; +} + +static void static_metadata_free(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) { + assert(ctx); + struct static_metadata *file = (struct static_metadata *)_file; + assert(file); + + /* do nothing */ +} + +static uint32_t static_metadata_io(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file, lib9p_o_t UNUSED(flags)) { + assert(ctx); + struct static_metadata *file = (struct static_metadata *)_file; + assert(file); + + return 0; +} + +static void static_metadata_wstat(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file, + struct lib9p_stat UNUSED(new)) { + assert(ctx); + struct static_metadata *file = (struct static_metadata *)_file; + assert(file); + + lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem"); +} + +static void static_metadata_remove(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) { + assert(ctx); + struct static_metadata *file = (struct static_metadata *)_file; + assert(file); + + lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem"); +} + +/* dir ************************************************************************/ + +static struct lib9p_stat static_dir_stat(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) { + assert(ctx); + struct static_dir *file = (struct static_dir *)_file; + assert(file); + + return (struct lib9p_stat){ + .kern_type = 0, + .kern_dev = 0, + .file_qid = { + .type = LIB9P_QT_DIR, + .vers = 1, + .path = file->metadata.pathnum, + }, + .file_mode = LIB9P_DM_DIR | (file->metadata.perm & 0555), + .file_atime = file->metadata.atime, + .file_mtime = file->metadata.mtime, + .file_size = 0, + .file_name = p9_str(file->metadata.name), + .file_owner_uid = p9_str(file->metadata.u_name), + .file_owner_gid = p9_str(file->metadata.g_name), + .file_last_modified_uid = p9_str(file->metadata.m_name), + .file_extension = p9_nulstr, + .file_owner_n_uid = file->metadata.u_num, + .file_owner_n_gid = file->metadata.g_num, + .file_last_modified_n_uid = file->metadata.m_num, + }; +} + +static struct lib9p_srv_file *static_dir_dopen(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_dir, + char *childname) { + assert(ctx); + struct static_dir *dir = (struct static_dir *)_dir; + assert(dir); + + for (size_t i = 0; dir->members[i]; i++) { + struct lib9p_srv_file *filep = dir->members[i]; + struct lib9p_stat stat = filep->vtable->stat(ctx, filep); + if (lib9p_ctx_has_error(&ctx->basectx)) + break; + lib9p_assert_stat(stat); + if (strcmp(stat.file_name.utf8, childname) == 0) + return filep; + } + lib9p_error(&ctx->basectx, + LINUX_ENOENT, "no such file or directory"); + return NULL; +} + +static struct lib9p_srv_file *static_dir_dcreate(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_dir, + char *UNUSED(childname), + lib9p_dm_t UNUSED(perm), lib9p_o_t UNUSED(flags)) { + assert(ctx); + struct static_dir *dir = (struct static_dir *)_dir; + assert(dir); + + lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem"); + return NULL; +} + +static size_t static_dir_dread(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_dir, + uint8_t *buf, + uint32_t byte_count, + size_t _obj_offset) { + assert(ctx); + struct static_dir *dir = (struct static_dir *)_dir; + assert(dir); + + uint32_t byte_offset = 0; + size_t obj_offset = _obj_offset; + while (dir->members[obj_offset]) { + struct lib9p_srv_file *filep = dir->members[obj_offset]; + struct lib9p_stat stat = filep->vtable->stat(ctx, filep); + if (lib9p_ctx_has_error(&ctx->basectx)) + break; + lib9p_assert_stat(stat); + uint32_t nbytes = lib9p_marshal_stat(&ctx->basectx, byte_count-byte_offset, &stat, + &buf[byte_offset]); + if (!nbytes) { + if (obj_offset == _obj_offset) + lib9p_error(&ctx->basectx, + LINUX_ERANGE, "stat object does not fit into negotiated max message size"); + break; + } + byte_offset += nbytes; + obj_offset++; + } + return obj_offset - _obj_offset; +} + +struct lib9p_srv_file_vtable static_dir_vtable = { + .clone = static_metadata_clone, + .free = static_metadata_free, + + .io = static_metadata_io, + .stat = static_dir_stat, + .wstat = static_metadata_wstat, + .remove = static_metadata_remove, + + .dopen = static_dir_dopen, + .dcreate = static_dir_dcreate, + + .dread = static_dir_dread, +}; + +/* file ***********************************************************************/ + +static inline size_t static_file_size(struct static_file *file) { + assert(file); + + assert(file->data_start); + if (!file->data_end) + return file->data_size; + return (size_t)((uintptr_t)file->data_end - (uintptr_t)file->data_start); +} + +static struct lib9p_stat static_file_stat(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) { + assert(ctx); + struct static_file *file = (struct static_file *)_file; + assert(file); + + return (struct lib9p_stat){ + .kern_type = 0, + .kern_dev = 0, + .file_qid = { + .type = LIB9P_QT_FILE, + .vers = 1, + .path = file->metadata.pathnum, + }, + .file_mode = file->metadata.perm & 0444, + .file_atime = file->metadata.atime, + .file_mtime = file->metadata.mtime, + .file_size = (uint64_t)static_file_size(file), + .file_name = p9_str(file->metadata.name), + .file_owner_uid = p9_str(file->metadata.u_name), + .file_owner_gid = p9_str(file->metadata.g_name), + .file_last_modified_uid = p9_str(file->metadata.m_name), + .file_extension = p9_nulstr, + .file_owner_n_uid = file->metadata.u_num, + .file_owner_n_gid = file->metadata.g_num, + .file_last_modified_n_uid = file->metadata.m_num, + }; +} + +static uint32_t static_file_pread(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file, + void *buf, + uint32_t byte_count, + uint64_t byte_offset) { + assert(ctx); + struct static_file *file = (struct static_file *)_file; + assert(file); + + size_t data_size = static_file_size(file); + + if (byte_offset > (uint64_t)data_size) { + lib9p_error(&ctx->basectx, + LINUX_EINVAL, "offset is past end-of-file length"); + return 0; + } + + 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; + memcpy(buf, &file->data_start[beg_off], end_off-beg_off); + return (uint32_t)(end_off-beg_off); +} + +struct lib9p_srv_file_vtable static_file_vtable = { + .clone = static_metadata_clone, + .free = static_metadata_free, + + .io = static_metadata_io, + .stat = static_file_stat, + .wstat = static_metadata_wstat, + .remove = static_metadata_remove, + + .pread = static_file_pread, + .pwrite = NULL, +}; diff --git a/cmd/srv9p/static9p.h b/cmd/srv9p/static9p.h new file mode 100644 index 0000000..2d77138 --- /dev/null +++ b/cmd/srv9p/static9p.h @@ -0,0 +1,41 @@ +#ifndef _STATIC9P_H_ +#define _STATIC9P_H_ + +#include + +struct static_metadata { + struct lib9p_srv_file header; + + char *u_name; + uint32_t u_num; + char *g_name; + uint32_t g_num; + char *m_name; + uint32_t m_num; + + uint64_t pathnum; + char *name; + lib9p_dm_t perm; + uint32_t atime, mtime; +}; + +struct static_dir { + struct static_metadata metadata; + + /* NULL-terminated */ + struct lib9p_srv_file *members[]; +}; + + +struct static_file { + struct static_metadata metadata; + + char *data_start; /* must not be NULL */ + char *data_end; /* may be NULL, in which case data_size is used */ + size_t data_size; /* only used if .data_end==NULL */ +}; + +extern struct lib9p_srv_file_vtable static_dir_vtable; +extern struct lib9p_srv_file_vtable static_file_vtable; + +#endif /* _STATIC9P_H_ */ diff --git a/lib9p/include/lib9p/9p.h b/lib9p/include/lib9p/9p.h index 2511e3d..e7ddb15 100644 --- a/lib9p/include/lib9p/9p.h +++ b/lib9p/include/lib9p/9p.h @@ -56,6 +56,17 @@ static bool lib9p_ctx_has_error(struct lib9p_ctx *ctx) { return ctx->err_msg[0]; } +/** Assert that a `struct lib9p_stat` object looks valid. */ +static inline void lib9p_assert_stat(struct lib9p_stat stat) { + assert( ((bool)(stat.file_mode & LIB9P_DM_DIR )) == ((bool)(stat.file_qid.type & LIB9P_QT_DIR )) ); + assert( ((bool)(stat.file_mode & LIB9P_DM_APPEND )) == ((bool)(stat.file_qid.type & LIB9P_QT_APPEND )) ); + assert( ((bool)(stat.file_mode & LIB9P_DM_EXCL )) == ((bool)(stat.file_qid.type & LIB9P_QT_EXCL )) ); + assert( ((bool)(stat.file_mode & _LIB9P_DM_PLAN9_MOUNT)) == ((bool)(stat.file_qid.type & _LIB9P_QT_PLAN9_MOUNT)) ); + assert( ((bool)(stat.file_mode & LIB9P_DM_AUTH )) == ((bool)(stat.file_qid.type & LIB9P_QT_AUTH )) ); + assert( ((bool)(stat.file_mode & LIB9P_DM_TMP )) == ((bool)(stat.file_qid.type & LIB9P_QT_TMP )) ); + assert( (stat.file_size == 0) || !(stat.file_mode & LIB9P_DM_DIR) ); +} + /** Write an static error into ctx, return -1. */ int lib9p_error(struct lib9p_ctx *ctx, uint32_t linux_errno, char const *msg); /** Write a printf-style error into ctx, return -1. */ diff --git a/lib9p/srv.c b/lib9p/srv.c index 1f62e27..6953687 100644 --- a/lib9p/srv.c +++ b/lib9p/srv.c @@ -386,14 +386,14 @@ static void handle_message(struct _lib9p_srv_req *ctx) { } } -#define handler_common(ctx, req, resp) do { \ +#define util_handler_common(ctx, req, resp) do { \ assert(ctx); \ assert(req); \ assert(resp); \ resp->tag = req->tag; \ } while (0) -static inline bool check_perm(struct lib9p_srv_ctx *ctx, struct lib9p_stat *stat, uint8_t action) { +static inline bool util_check_perm(struct lib9p_srv_ctx *ctx, struct lib9p_stat *stat, uint8_t action) { assert(ctx); assert(stat); assert(action); @@ -404,12 +404,12 @@ static inline bool check_perm(struct lib9p_srv_ctx *ctx, struct lib9p_stat *stat return mode & action; } -static inline bool release(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *file) { +static inline bool util_release(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *file) { assert(file); file->_refcount--; if (file->_refcount == 0) { if (file->_parent_dir != file) - release(ctx, file->_parent_dir); + util_release(ctx, file->_parent_dir); file->vtable->free(ctx, file); } return lib9p_ctx_has_error(&ctx->basectx); @@ -418,7 +418,7 @@ static inline bool release(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *fil static void handle_Tversion(struct _lib9p_srv_req *ctx, struct lib9p_msg_Tversion *req, struct lib9p_msg_Rversion *resp) { - handler_common(ctx, req, resp); + util_handler_common(ctx, req, resp); enum lib9p_version version = LIB9P_VER_unknown; @@ -494,7 +494,7 @@ static void handle_Tversion(struct _lib9p_srv_req *ctx, static void handle_Tauth(struct _lib9p_srv_req *ctx, struct lib9p_msg_Tauth *req, struct lib9p_msg_Rauth *resp) { - handler_common(ctx, req, resp); + util_handler_common(ctx, req, resp); ctx->ctx.uid = req->n_uname; ctx->ctx.uname = req->uname.utf8; @@ -514,7 +514,7 @@ static void handle_Tauth(struct _lib9p_srv_req *ctx, static void handle_Tattach(struct _lib9p_srv_req *ctx, struct lib9p_msg_Tattach *req, struct lib9p_msg_Rattach *resp) { - handler_common(ctx, req, resp); + util_handler_common(ctx, req, resp); ctx->ctx.uid = req->n_uname; ctx->ctx.uname = req->uname.utf8; @@ -574,7 +574,7 @@ static void handle_Tattach(struct _lib9p_srv_req *ctx, })) { lib9p_error(&ctx->ctx.basectx, LINUX_EMFILE, "too many open files"); - release(&ctx->ctx, rootdir); + util_release(&ctx->ctx, rootdir); return; } @@ -585,6 +585,7 @@ static void handle_Tattach(struct _lib9p_srv_req *ctx, &(struct lib9p_msg_Rclunk){}); return; } + lib9p_assert_stat(stat); assert(stat.file_mode & LIB9P_DM_DIR); @@ -595,7 +596,7 @@ static void handle_Tattach(struct _lib9p_srv_req *ctx, static void handle_Tflush(struct _lib9p_srv_req *ctx, struct lib9p_msg_Tflush *req, struct lib9p_msg_Rflush *resp) { - handler_common(ctx, req, resp); + util_handler_common(ctx, req, resp); struct _lib9p_srv_req **oldreqp = reqmap_load(&ctx->parent_sess->reqs, req->oldtag); if (oldreqp) @@ -605,7 +606,7 @@ static void handle_Tflush(struct _lib9p_srv_req *ctx, static void handle_Twalk(struct _lib9p_srv_req *ctx, struct lib9p_msg_Twalk *req, struct lib9p_msg_Rwalk *resp) { - handler_common(ctx, req, resp); + util_handler_common(ctx, req, resp); struct _srv_fidinfo *fidinfo = fidmap_load(&ctx->parent_sess->fids, req->fid); if (!fidinfo) { @@ -632,7 +633,7 @@ static void handle_Twalk(struct _lib9p_srv_req *ctx, resp->wqid = (struct lib9p_qid *)(&resp[1]); for (resp->nwqid = 0; resp->nwqid < req->nwname; resp->nwqid++) { struct lib9p_srv_file *member; - if (strcmp(req->wname[resp->nwqid].utf8, "..")) { + if (strcmp(req->wname[resp->nwqid].utf8, "..") == 0) { member = dir->_parent_dir; } else { if (!isdir) { @@ -652,21 +653,22 @@ static void handle_Twalk(struct _lib9p_srv_req *ctx, struct lib9p_stat stat = member->vtable->stat(&ctx->ctx, member); if (lib9p_ctx_has_error(&ctx->ctx.basectx)) { - release(&ctx->ctx, member); /* presumption of taking ref-A failed */ + util_release(&ctx->ctx, member); /* presumption of taking ref-A failed */ break; } + lib9p_assert_stat(stat); isdir = stat.file_mode & LIB9P_DM_DIR; - if (isdir && !check_perm(&ctx->ctx, &stat, 0b001)) { + if (isdir && !util_check_perm(&ctx->ctx, &stat, 0b001)) { lib9p_error(&ctx->ctx.basectx, LINUX_EACCES, "you do not have execute permission on that directory"); - release(&ctx->ctx, member); /* presumption of taking ref-A failed */ + util_release(&ctx->ctx, member); /* presumption of taking ref-A failed */ break; } resp->wqid[resp->nwqid] = stat.file_qid; /* presumption of taking ref-A succeeded */ - release(&ctx->ctx, dir); + util_release(&ctx->ctx, dir); dir = member; } if (resp->nwqid == req->nwname) { @@ -681,12 +683,12 @@ static void handle_Twalk(struct _lib9p_srv_req *ctx, })) { lib9p_error(&ctx->ctx.basectx, LINUX_EMFILE, "too many open files"); - release(&ctx->ctx, dir); /* presumption of insertion failed */ + util_release(&ctx->ctx, dir); /* presumption of insertion failed */ } } else { assert(lib9p_ctx_has_error(&ctx->ctx.basectx)); if (req->newfid != req->fid) - release(&ctx->ctx, dir); /* presumption of insertion failed */ + util_release(&ctx->ctx, dir); /* presumption of insertion failed */ if (resp->nwqid > 0) lib9p_ctx_clear_error(&ctx->ctx.basectx); } @@ -695,7 +697,7 @@ static void handle_Twalk(struct _lib9p_srv_req *ctx, static void handle_Topen(struct _lib9p_srv_req *ctx, struct lib9p_msg_Topen *req, struct lib9p_msg_Ropen *resp) { - handler_common(ctx, req, resp); + util_handler_common(ctx, req, resp); /* Check that the FID is valid for this. */ struct _srv_fidinfo *fidinfo = fidmap_load(&ctx->parent_sess->fids, req->fid); @@ -729,7 +731,8 @@ static void handle_Topen(struct _lib9p_srv_req *ctx, struct lib9p_stat parent_stat = file->_parent_dir->vtable->stat(&ctx->ctx, file->_parent_dir); if (lib9p_ctx_has_error(&ctx->ctx.basectx)) return; - if (!check_perm(&ctx->ctx, &parent_stat, 0b010)) { + lib9p_assert_stat(parent_stat); + if (!util_check_perm(&ctx->ctx, &parent_stat, 0b010)) { lib9p_error(&ctx->ctx.basectx, LINUX_EACCES, "permission denied to remove-on-close"); return; @@ -739,6 +742,7 @@ static void handle_Topen(struct _lib9p_srv_req *ctx, struct lib9p_stat stat = file->vtable->stat(&ctx->ctx, file); if (lib9p_ctx_has_error(&ctx->ctx.basectx)) return; + lib9p_assert_stat(stat); if (stat.file_mode & LIB9P_QT_APPEND) reqmode = reqmode & ~LIB9P_O_TRUNC; uint8_t perm_bits = 0; @@ -760,7 +764,7 @@ static void handle_Topen(struct _lib9p_srv_req *ctx, fidflags = fidflags | FIDFLAG_OPEN_R; break; } - if (!check_perm(&ctx->ctx, &stat, perm_bits)) { + if (!util_check_perm(&ctx->ctx, &stat, perm_bits)) { lib9p_error(&ctx->ctx.basectx, LINUX_EACCES, "permission denied"); } @@ -779,7 +783,7 @@ static void handle_Topen(struct _lib9p_srv_req *ctx, static void handle_Tcreate(struct _lib9p_srv_req *ctx, struct lib9p_msg_Tcreate *req, struct lib9p_msg_Rcreate *resp) { - handler_common(ctx, req, resp); + util_handler_common(ctx, req, resp); lib9p_error(&ctx->ctx.basectx, LINUX_EOPNOTSUPP, "create not (yet?) implemented"); @@ -788,7 +792,7 @@ static void handle_Tcreate(struct _lib9p_srv_req *ctx, static void handle_Tread(struct _lib9p_srv_req *ctx, struct lib9p_msg_Tread *req, struct lib9p_msg_Rread *resp) { - handler_common(ctx, req, resp); + util_handler_common(ctx, req, resp); /* Check that the FID is valid for this. */ struct _srv_fidinfo *fidinfo = fidmap_load(&ctx->parent_sess->fids, req->fid); @@ -837,7 +841,7 @@ static void handle_Tread(struct _lib9p_srv_req *ctx, static void handle_Twrite(struct _lib9p_srv_req *ctx, struct lib9p_msg_Twrite *req, struct lib9p_msg_Rwrite *resp) { - handler_common(ctx, req, resp); + util_handler_common(ctx, req, resp); /* Check that the FID is valid for this. */ struct _srv_fidinfo *fidinfo = fidmap_load(&ctx->parent_sess->fids, req->fid); @@ -862,7 +866,7 @@ static void handle_Twrite(struct _lib9p_srv_req *ctx, static void handle_Tclunk(struct _lib9p_srv_req *ctx, struct lib9p_msg_Tclunk *req, struct lib9p_msg_Rclunk *resp) { - handler_common(ctx, req, resp); + util_handler_common(ctx, req, resp); struct _srv_fidinfo *fidinfo = fidmap_load(&ctx->parent_sess->fids, req->fid); if (!fidinfo) { @@ -884,7 +888,7 @@ static void handle_Tclunk(struct _lib9p_srv_req *ctx, static void handle_Tremove(struct _lib9p_srv_req *ctx, struct lib9p_msg_Tremove *req, struct lib9p_msg_Rremove *resp) { - handler_common(ctx, req, resp); + util_handler_common(ctx, req, resp); lib9p_error(&ctx->ctx.basectx, LINUX_EOPNOTSUPP, "remove not (yet?) implemented"); @@ -893,7 +897,7 @@ static void handle_Tremove(struct _lib9p_srv_req *ctx, static void handle_Tstat(struct _lib9p_srv_req *ctx, struct lib9p_msg_Tstat *req, struct lib9p_msg_Rstat *resp) { - handler_common(ctx, req, resp); + util_handler_common(ctx, req, resp); struct _srv_fidinfo *fidinfo = fidmap_load(&ctx->parent_sess->fids, req->fid); if (!fidinfo) { @@ -903,12 +907,14 @@ static void handle_Tstat(struct _lib9p_srv_req *ctx, } resp->stat = fidinfo->file->vtable->stat(&ctx->ctx, fidinfo->file); + if (!lib9p_ctx_has_error(&ctx->ctx.basectx)) + lib9p_assert_stat(resp->stat); } static void handle_Twstat(struct _lib9p_srv_req *ctx, struct lib9p_msg_Twstat *req, struct lib9p_msg_Rwstat *resp) { - handler_common(ctx, req, resp); + util_handler_common(ctx, req, resp); lib9p_error(&ctx->ctx.basectx, LINUX_EOPNOTSUPP, "wstat not (yet?) implemented"); @@ -918,7 +924,7 @@ static void handle_Twstat(struct _lib9p_srv_req *ctx, static void handle_Tsession(struct _lib9p_srv_req *ctx, struct lib9p_msg_Tsession *req, struct lib9p_msg_Rsession *resp) { - handler_common(ctx, req, resp); + util_handler_common(ctx, req, resp); lib9p_error(&ctx->ctx.basectx, LINUX_EOPNOTSUPP, "session not (yet?) implemented"); @@ -927,7 +933,7 @@ static void handle_Tsession(struct _lib9p_srv_req *ctx, static void handle_Tsread(struct _lib9p_srv_req *ctx, struct lib9p_msg_Tsread *req, struct lib9p_msg_Rsread *resp) { - handler_common(ctx, req, resp); + util_handler_common(ctx, req, resp); lib9p_error(&ctx->ctx.basectx, LINUX_EOPNOTSUPP, "sread not (yet?) implemented"); @@ -936,7 +942,7 @@ static void handle_Tsread(struct _lib9p_srv_req *ctx, static void handle_Tswrite(struct _lib9p_srv_req *ctx, struct lib9p_msg_Tswrite *req, struct lib9p_msg_Rswrite *resp) { - handler_common(ctx, req, resp); + util_handler_common(ctx, req, resp); lib9p_error(&ctx->ctx.basectx, LINUX_EOPNOTSUPP, "swrite not (yet?) implemented"); -- cgit v1.2.3-2-g168b