From 17bfb0ef0a48330b1d54e61fe3c30d83528d2d90 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Wed, 25 Jan 2023 20:16:22 +0100 Subject: style: typedef structs, shorter namespaces I've wanted to do this for a while, and since I've just had a relatively large refactor commit (pcpy), this is as good of a time as any. Typedefing structs was mostly inspired by Plan 9's coding style. It makes some lines of code much shorter at basically no expense. Everything related to userland kept old-style struct definitions, so as not to force that style onto other people. I also considered changing SCREAMING_ENUM_FIELDS to NicerLookingCamelcase, but I didn't, just in case that'd be confusing. --- src/kernel/vfs/mount.c | 22 +++++++++++----------- src/kernel/vfs/mount.h | 21 +++++++++++---------- src/kernel/vfs/procfs.c | 38 +++++++++++++++++++------------------- src/kernel/vfs/procfs.h | 2 +- src/kernel/vfs/request.c | 46 +++++++++++++++++++++++----------------------- src/kernel/vfs/request.h | 48 ++++++++++++++++++++++++------------------------ 6 files changed, 89 insertions(+), 88 deletions(-) (limited to 'src/kernel/vfs') diff --git a/src/kernel/vfs/mount.c b/src/kernel/vfs/mount.c index 2815bb9..2518989 100644 --- a/src/kernel/vfs/mount.c +++ b/src/kernel/vfs/mount.c @@ -3,22 +3,22 @@ #include #include -static struct vfs_mount *mount_root = NULL; +static VfsMount *mount_root = NULL; -struct vfs_mount *vfs_mount_seed(void) { +VfsMount *vfs_mount_seed(void) { return mount_root; } -void vfs_root_register(const char *path, void (*accept)(struct vfs_request *)) { - struct vfs_backend *backend = kmalloc(sizeof *backend); - struct vfs_mount *mount = kmalloc(sizeof *mount); - *backend = (struct vfs_backend) { +void vfs_root_register(const char *path, void (*accept)(VfsReq *)) { + VfsBackend *backend = kmalloc(sizeof *backend); + VfsMount *mount = kmalloc(sizeof *mount); + *backend = (VfsBackend) { .is_user = false, .usehcnt = 1, .provhcnt = 1, .kern.accept = accept, }; - *mount = (struct vfs_mount){ + *mount = (VfsMount){ .prev = mount_root, .prefix = path, .prefix_len = strlen(path), @@ -29,8 +29,8 @@ void vfs_root_register(const char *path, void (*accept)(struct vfs_request *)) { } -struct vfs_mount *vfs_mount_resolve( - struct vfs_mount *top, const char *path, size_t path_len) +VfsMount *vfs_mount_resolve( + VfsMount *top, const char *path, size_t path_len) { for (; top; top = top->prev) { if (top->prefix_len > path_len) @@ -49,12 +49,12 @@ struct vfs_mount *vfs_mount_resolve( return top; } -void vfs_mount_remref(struct vfs_mount *mnt) { +void vfs_mount_remref(VfsMount *mnt) { assert(mnt); assert(mnt->refs > 0); if (--(mnt->refs) > 0) return; - struct vfs_mount *prev = mnt->prev; + VfsMount *prev = mnt->prev; if (mnt->backend) { vfs_backend_refdown(mnt->backend, true); } diff --git a/src/kernel/vfs/mount.h b/src/kernel/vfs/mount.h index 6efdaa7..2e10dec 100644 --- a/src/kernel/vfs/mount.h +++ b/src/kernel/vfs/mount.h @@ -1,24 +1,25 @@ #pragma once +#include #include #include -struct vfs_mount { - struct vfs_mount *prev; +struct VfsMount { + VfsMount *prev; const char *prefix; size_t prefix_len; bool prefix_owned; - struct vfs_backend *backend; + VfsBackend *backend; size_t refs; /* counts all references, atm from: - * - struct vfs_mount - * - struct proc + * - VfsMount + * - Proc */ }; // prepares init's filesystem view -struct vfs_mount *vfs_mount_seed(void); -struct vfs_mount *vfs_mount_resolve( - struct vfs_mount *top, const char *path, size_t path_len); +VfsMount *vfs_mount_seed(void); +VfsMount *vfs_mount_resolve( + VfsMount *top, const char *path, size_t path_len); /** Decrements the reference count, potentially freeing the mount. */ -void vfs_mount_remref(struct vfs_mount *mnt); +void vfs_mount_remref(VfsMount *mnt); -void vfs_root_register(const char *path, void (*accept)(struct vfs_request *)); +void vfs_root_register(const char *path, void (*accept)(VfsReq *)); diff --git a/src/kernel/vfs/procfs.c b/src/kernel/vfs/procfs.c index 78dad0d..2a8dd93 100644 --- a/src/kernel/vfs/procfs.c +++ b/src/kernel/vfs/procfs.c @@ -18,14 +18,14 @@ struct phandle { enum phandle_type type; }; -static struct phandle *openpath(const char *path, size_t len, struct process *root); -static struct process *findgid(uint32_t gid, struct process *root); -static void procfs_accept(struct vfs_request *req); -static void procfs_cleanup(struct vfs_backend *be); +static struct phandle *openpath(const char *path, size_t len, Proc *root); +static Proc *findgid(uint32_t gid, Proc *root); +static void procfs_accept(VfsReq *req); +static void procfs_cleanup(VfsBackend *be); static int isdigit(int c); static struct phandle * -openpath(const char *path, size_t len, struct process *p) +openpath(const char *path, size_t len, Proc *p) { struct phandle *h; enum phandle_type type; @@ -71,21 +71,21 @@ openpath(const char *path, size_t len, struct process *p) return h; } -static struct process * -findgid(uint32_t gid, struct process *root) +static Proc * +findgid(uint32_t gid, Proc *root) { - for (struct process *p = root; p; p = process_next(p, root)) { + for (Proc *p = root; p; p = proc_next(p, root)) { if (p->globalid == gid) return p; } return NULL; } static void -procfs_accept(struct vfs_request *req) +procfs_accept(VfsReq *req) { - struct process *root = req->backend->kern.data; + Proc *root = req->backend->kern.data; struct phandle *h = (__force void*)req->id; - struct process *p; + Proc *p; char buf[512]; assert(root); if (req->type == VFSOP_OPEN) { @@ -110,7 +110,7 @@ procfs_accept(struct vfs_request *req) } pos += snprintf(buf + pos, 512 - pos, "intr")+1; pos += snprintf(buf + pos, 512 - pos, "mem")+1; - for (struct process *iter = p->child; iter; iter = iter->sibling) { + for (Proc *iter = p->child; iter; iter = iter->sibling) { assert(pos < 512); // processes could possibly be identified by unique identifiers instead // e.g. an encrypted gid, or just a randomly generated one @@ -135,7 +135,7 @@ procfs_accept(struct vfs_request *req) ); vfsreq_finish_short(req, res); } else if (req->type == VFSOP_WRITE && h->type == PhIntr) { - process_intr(p); + proc_intr(p); vfsreq_finish_short(req, req->input.len); } else if (req->type == VFSOP_CLOSE) { kfree(h); @@ -146,9 +146,9 @@ procfs_accept(struct vfs_request *req) } static void -procfs_cleanup(struct vfs_backend *be) +procfs_cleanup(VfsBackend *be) { - struct process *p = be->kern.data; + Proc *p = be->kern.data; assert(p); p->refcount--; } @@ -158,11 +158,11 @@ isdigit(int c) { return '0' <= c && c <= '9'; } -struct vfs_backend * -procfs_backend(struct process *proc) +VfsBackend * +procfs_backend(Proc *proc) { - struct vfs_backend *be = kzalloc(sizeof(struct vfs_backend)); - *be = (struct vfs_backend) { + VfsBackend *be = kzalloc(sizeof(VfsBackend)); + *be = (VfsBackend) { .is_user = false, .provhcnt = 1, .usehcnt = 1, diff --git a/src/kernel/vfs/procfs.h b/src/kernel/vfs/procfs.h index 5ee4e96..4fb8c84 100644 --- a/src/kernel/vfs/procfs.h +++ b/src/kernel/vfs/procfs.h @@ -1,4 +1,4 @@ #pragma once #include -struct vfs_backend *procfs_backend(struct process *proc); +VfsBackend *procfs_backend(Proc *proc); diff --git a/src/kernel/vfs/request.c b/src/kernel/vfs/request.c index c98913a..7e5877d 100644 --- a/src/kernel/vfs/request.c +++ b/src/kernel/vfs/request.c @@ -7,12 +7,12 @@ #include #include -static void vfs_backend_user_accept(struct vfs_request *req); +static void vfs_backend_user_accept(VfsReq *req); -void vfsreq_create(struct vfs_request req_) { - struct vfs_request *req; +void vfsreq_create(VfsReq req_) { + VfsReq *req; if (req_.caller) { - process_transition(req_.caller, PS_WAITS4FS); + proc_setstate(req_.caller, PS_WAITS4FS); if (!req_.caller->reqslot) req_.caller->reqslot = kmalloc(sizeof *req); req = req_.caller->reqslot; @@ -34,7 +34,7 @@ void vfsreq_create(struct vfs_request req_) { // TODO if i add a handle field to vfs_request, check ->readable ->writeable here if (req->backend && req->backend->provhcnt) { - struct vfs_request **iter = &req->backend->queue; + VfsReq **iter = &req->backend->queue; while (*iter != NULL) // find free spot in queue iter = &(*iter)->queue_next; *iter = req; @@ -44,11 +44,11 @@ void vfsreq_create(struct vfs_request req_) { } } -void vfsreq_finish(struct vfs_request *req, char __user *stored, long ret, - int flags, struct process *handler) +void vfsreq_finish(VfsReq *req, char __user *stored, long ret, + int flags, Proc *handler) { if (req->type == VFSOP_OPEN && ret >= 0) { - struct handle *h; + Handle *h; if (!(flags & FSR_DELEGATE)) { /* default behavior - create a new handle for the file, wrap the id */ h = handle_init(HANDLE_FILE); @@ -60,14 +60,14 @@ void vfsreq_finish(struct vfs_request *req, char __user *stored, long ret, } else { /* delegating - moving a handle to the caller */ assert(handler); - h = process_handle_take(handler, ret); + h = proc_hid_take(handler, ret); // TODO don't ignore OPEN_RO } if (h) { // TODO write tests for caller getting killed while opening a file if (!req->caller) panic_unimplemented(); - ret = process_handle_put(req->caller, h); + ret = proc_handle_put(req->caller, h); if (ret < 0) ret = -EMFILE; } else { ret = -1; @@ -83,14 +83,14 @@ void vfsreq_finish(struct vfs_request *req, char __user *stored, long ret, if (req->caller) { assert(req->caller->state == PS_WAITS4FS); regs_savereturn(&req->caller->regs, ret); - process_transition(req->caller, PS_RUNNING); + proc_setstate(req->caller, PS_RUNNING); } else { kfree(req); } } -void vfs_backend_tryaccept(struct vfs_backend *backend) { - struct vfs_request *req = backend->queue; +void vfs_backend_tryaccept(VfsBackend *backend) { + VfsReq *req = backend->queue; if (!req) return; if (backend->is_user && !backend->user.handler) return; @@ -103,8 +103,8 @@ void vfs_backend_tryaccept(struct vfs_backend *backend) { } } -static void vfs_backend_user_accept(struct vfs_request *req) { - struct process *handler; +static void vfs_backend_user_accept(VfsReq *req) { + Proc *handler; struct ufs_request res = {0}; int len; @@ -143,26 +143,26 @@ static void vfs_backend_user_accept(struct vfs_request *req) { panic_unimplemented(); } - struct handle *h; - handle_t hid = process_handle_init(handler, HANDLE_FS_REQ, &h); + Handle *h; + hid_t hid = proc_handle_init(handler, HANDLE_FS_REQ, &h); if (hid < 0) panic_unimplemented(); h->req = req; - process_transition(handler, PS_RUNNING); + proc_setstate(handler, PS_RUNNING); regs_savereturn(&handler->regs, hid); req->backend->user.handler = NULL; return; } -void vfs_backend_refdown(struct vfs_backend *b, bool use) { +void vfs_backend_refdown(VfsBackend *b, bool use) { size_t *field = use ? &b->usehcnt : &b->provhcnt; assert(b); assert(0 < *field); *field -= 1; if (b->provhcnt == 0 && use == false) { - struct vfs_request *q = b->queue; + VfsReq *q = b->queue; while (q) { - struct vfs_request *q2 = q->queue_next; + VfsReq *q2 = q->queue_next; vfsreq_finish_short(q, -1); q = q2; } @@ -173,11 +173,11 @@ void vfs_backend_refdown(struct vfs_backend *b, bool use) { b->kern.cleanup(b); } if (b->is_user && b->user.handler) { - struct process *p = b->user.handler; + Proc *p = b->user.handler; b->user.handler = NULL; assert(p->state == PS_WAITS4REQUEST); regs_savereturn(&p->regs, -EPIPE); - process_transition(p, PS_RUNNING); + proc_setstate(p, PS_RUNNING); } } if (b->usehcnt == 0 && b->provhcnt == 0) { diff --git a/src/kernel/vfs/request.h b/src/kernel/vfs/request.h index 4d7fa01..182202c 100644 --- a/src/kernel/vfs/request.h +++ b/src/kernel/vfs/request.h @@ -1,38 +1,38 @@ #pragma once -#include +#include #include #include -// describes something which can act as an access function -struct vfs_backend { +/* describes something which can act as an access function */ +struct VfsBackend { /* amount of using references - * struct vfs_mount - * struct vfs_request - * struct handle + * VfsMount + * VfsReq + * Handle * once it reaches 0, it'll never increase */ - size_t usehcnt; /* struct vfs_mount */ + size_t usehcnt; /* VfsMount */ /* amount of providing references - * struct process + * Proc * 0 - orphaned, will never increase */ size_t provhcnt; - struct vfs_request *queue; + VfsReq *queue; bool is_user; union { struct { - struct process *handler; + Proc *handler; } user; struct { - void (*accept)(struct vfs_request *); - void (*cleanup)(struct vfs_backend *); + void (*accept)(VfsReq *); + void (*cleanup)(VfsBackend *); void *data; } kern; }; }; -// describes an in-process vfs call -struct vfs_request { - enum vfs_operation type; +/* describes an in-progress vfs call */ +struct VfsReq { + enum vfs_op type; struct { bool kern; // if false: use .buf ; if true: use .buf_kern union { @@ -54,26 +54,26 @@ struct vfs_request { /* if caller != NULL, owned by it - don't free, the allocation will be reused * if caller == NULL, free on finish */ - struct process *caller; - struct vfs_backend *backend; + Proc *caller; + VfsBackend *backend; - struct vfs_request *queue_next; - struct vfs_request *postqueue_next; /* used by kernel backends */ + VfsReq *queue_next; + VfsReq *postqueue_next; /* used by kernel backends */ /* only one of these queues is in use at a given moment, they could * be merged into a single field */ }; /** Assigns the vfs_request to the caller, and dispatches the call */ -void vfsreq_create(struct vfs_request); -void vfsreq_finish(struct vfs_request*, char __user *stored, long ret, int flags, struct process *handler); +void vfsreq_create(VfsReq); +void vfsreq_finish(VfsReq*, char __user *stored, long ret, int flags, Proc *handler); -static inline void vfsreq_finish_short(struct vfs_request *req, long ret) { +static inline void vfsreq_finish_short(VfsReq *req, long ret) { vfsreq_finish(req, (void __user *)ret, ret, 0, NULL); } /** Try to accept an enqueued request */ -void vfs_backend_tryaccept(struct vfs_backend *); +void vfs_backend_tryaccept(VfsBackend *); // TODO the bool arg is confusing. maybe this should just be a function // that verified the refcount and potentially frees the backend -void vfs_backend_refdown(struct vfs_backend *, bool use); +void vfs_backend_refdown(VfsBackend *, bool use); -- cgit v1.2.3