summaryrefslogtreecommitdiff
path: root/cmd/srv9p/static9p.c
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/srv9p/static9p.c')
-rw-r--r--cmd/srv9p/static9p.c156
1 files changed, 80 insertions, 76 deletions
diff --git a/cmd/srv9p/static9p.c b/cmd/srv9p/static9p.c
index f508ff1..47c07be 100644
--- a/cmd/srv9p/static9p.c
+++ b/cmd/srv9p/static9p.c
@@ -6,61 +6,63 @@
#include <assert.h>
+#include <libmisc/vcall.h>
+
#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 ************************************************************/
+/* common *********************************************************************/
-static struct lib9p_srv_file *static_metadata_clone(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) {
+static implements_lib9p_srv_file *static_common_clone(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) {
+ _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self);
+ assert(self);
assert(ctx);
- struct static_metadata *file = (struct static_metadata *)_file;
- assert(file);
- return &file->header;
+ return self;
}
-static void static_metadata_free(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) {
+static void static_common_free(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) {
+ _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self);
+ assert(self);
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)) {
+static uint32_t static_common_io(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, lib9p_o_t UNUSED(flags)) {
+ _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self);
+ assert(self);
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,
+static void static_common_wstat(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx,
struct lib9p_stat UNUSED(new)) {
+ _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self);
+ assert(self);
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) {
+static void static_common_remove(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) {
+ _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self);
+ assert(self);
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) {
+static struct lib9p_stat static_dir_stat(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) {
+ struct static_dir *self = VCALL_SELF(struct static_dir, implements_lib9p_srv_file, _self);
+ assert(self);
assert(ctx);
- struct static_dir *file = (struct static_dir *)_file;
- assert(file);
return (struct lib9p_stat){
.kern_type = 0,
@@ -68,32 +70,32 @@ static struct lib9p_stat static_dir_stat(struct lib9p_srv_ctx *ctx, struct lib9p
.file_qid = {
.type = LIB9P_QT_DIR,
.vers = 1,
- .path = file->metadata.pathnum,
+ .path = self->pathnum,
},
- .file_mode = LIB9P_DM_DIR | (file->metadata.perm & 0555),
- .file_atime = file->metadata.atime,
- .file_mtime = file->metadata.mtime,
+ .file_mode = LIB9P_DM_DIR | (self->perm & 0555),
+ .file_atime = self->atime,
+ .file_mtime = self->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_name = p9_str(self->name),
+ .file_owner_uid = p9_str(self->u_name),
+ .file_owner_gid = p9_str(self->g_name),
+ .file_last_modified_uid = p9_str(self->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,
+ .file_owner_n_uid = self->u_num,
+ .file_owner_n_gid = self->g_num,
+ .file_last_modified_n_uid = self->m_num,
};
}
-static struct lib9p_srv_file *static_dir_dopen(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_dir,
+static implements_lib9p_srv_file *static_dir_dopen(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx,
char *childname) {
+ struct static_dir *self = VCALL_SELF(struct static_dir, implements_lib9p_srv_file, _self);
+ assert(self);
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);
+ for (size_t i = 0; self->members[i]; i++) {
+ implements_lib9p_srv_file *filep = self->members[i];
+ struct lib9p_stat stat = VCALL(filep, stat, ctx);
if (lib9p_ctx_has_error(&ctx->basectx))
break;
lib9p_assert_stat(stat);
@@ -105,30 +107,30 @@ static struct lib9p_srv_file *static_dir_dopen(struct lib9p_srv_ctx *ctx, struct
return NULL;
}
-static struct lib9p_srv_file *static_dir_dcreate(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_dir,
+static implements_lib9p_srv_file *static_dir_dcreate(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx,
char *UNUSED(childname),
lib9p_dm_t UNUSED(perm), lib9p_o_t UNUSED(flags)) {
+ struct static_dir *self = VCALL_SELF(struct static_dir, implements_lib9p_srv_file, _self);
+ assert(self);
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,
+static size_t static_dir_dread(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx,
uint8_t *buf,
uint32_t byte_count,
size_t _obj_offset) {
+ struct static_dir *self = VCALL_SELF(struct static_dir, implements_lib9p_srv_file, _self);
+ assert(self);
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);
+ while (self->members[obj_offset]) {
+ implements_lib9p_srv_file *filep = self->members[obj_offset];
+ struct lib9p_stat stat = VCALL(filep, stat, ctx);
if (lib9p_ctx_has_error(&ctx->basectx))
break;
lib9p_assert_stat(stat);
@@ -147,13 +149,13 @@ static size_t static_dir_dread(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file
}
struct lib9p_srv_file_vtable static_dir_vtable = {
- .clone = static_metadata_clone,
- .free = static_metadata_free,
+ .clone = static_common_clone,
+ .free = static_common_free,
- .io = static_metadata_io,
+ .io = static_common_io,
.stat = static_dir_stat,
- .wstat = static_metadata_wstat,
- .remove = static_metadata_remove,
+ .wstat = static_common_wstat,
+ .remove = static_common_remove,
.dopen = static_dir_dopen,
.dcreate = static_dir_dcreate,
@@ -166,16 +168,18 @@ struct lib9p_srv_file_vtable static_dir_vtable = {
static inline size_t static_file_size(struct static_file *file) {
assert(file);
+#if __x86_64__
assert(file->data_start);
+#endif
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) {
+static struct lib9p_stat static_file_stat(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) {
+ struct static_file *self = VCALL_SELF(struct static_file, implements_lib9p_srv_file, _self);
+ assert(self);
assert(ctx);
- struct static_file *file = (struct static_file *)_file;
- assert(file);
return (struct lib9p_stat){
.kern_type = 0,
@@ -183,32 +187,32 @@ static struct lib9p_stat static_file_stat(struct lib9p_srv_ctx *ctx, struct lib9
.file_qid = {
.type = LIB9P_QT_FILE,
.vers = 1,
- .path = file->metadata.pathnum,
+ .path = self->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_mode = self->perm & 0444,
+ .file_atime = self->atime,
+ .file_mtime = self->mtime,
+ .file_size = (uint64_t)static_file_size(self),
+ .file_name = p9_str(self->name),
+ .file_owner_uid = p9_str(self->u_name),
+ .file_owner_gid = p9_str(self->g_name),
+ .file_last_modified_uid = p9_str(self->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,
+ .file_owner_n_uid = self->u_num,
+ .file_owner_n_gid = self->g_num,
+ .file_last_modified_n_uid = self->m_num,
};
}
-static uint32_t static_file_pread(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file,
+static uint32_t static_file_pread(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx,
void *buf,
uint32_t byte_count,
uint64_t byte_offset) {
+ struct static_file *self = VCALL_SELF(struct static_file, implements_lib9p_srv_file, _self);
+ assert(self);
assert(ctx);
- struct static_file *file = (struct static_file *)_file;
- assert(file);
- size_t data_size = static_file_size(file);
+ size_t data_size = static_file_size(self);
if (byte_offset > (uint64_t)data_size) {
lib9p_error(&ctx->basectx,
@@ -220,18 +224,18 @@ static uint32_t static_file_pread(struct lib9p_srv_ctx *ctx, struct lib9p_srv_fi
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);
+ memcpy(buf, &self->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,
+ .clone = static_common_clone,
+ .free = static_common_free,
- .io = static_metadata_io,
+ .io = static_common_io,
.stat = static_file_stat,
- .wstat = static_metadata_wstat,
- .remove = static_metadata_remove,
+ .wstat = static_common_wstat,
+ .remove = static_common_remove,
.pread = static_file_pread,
.pwrite = NULL,