From c8cc1783ef26803aeb94747ff8704bbe6962628a Mon Sep 17 00:00:00 2001 From: Lunaixsky Date: Tue, 1 Apr 2025 19:11:46 +0100 Subject: [PATCH] allow specifiying access mode when creating twifs file node twifs: refactor the interface for easy twimap/twifs export cake: fix overflow of index guessing when freeing, causing false positive and possibly failing double free assertion --- lunaix-os/hal/ahci/hbadev_export.c | 29 ++--- lunaix-os/hal/bus/pci.c | 40 +++--- lunaix-os/hal/rtc/rtc_device.c | 4 +- lunaix-os/includes/lunaix/fs/twifs.h | 114 +++++++++++++---- lunaix-os/includes/lunaix/fs/twimap.h | 20 ++- lunaix-os/includes/lunaix/fs_acl.h | 10 +- lunaix-os/kernel/block/blk_mapping.c | 45 +++---- lunaix-os/kernel/device/devdb.c | 13 +- lunaix-os/kernel/ds/waitq.c | 6 +- lunaix-os/kernel/exe/exec.c | 13 +- lunaix-os/kernel/fs/fs_export.c | 23 ++-- lunaix-os/kernel/fs/fsm.c | 5 +- lunaix-os/kernel/fs/twifs/twifs.c | 173 +++++++++++++++++--------- lunaix-os/kernel/kprint/kprintf.c | 4 +- lunaix-os/kernel/lrud.c | 13 +- lunaix-os/kernel/mm/cake.c | 19 +-- lunaix-os/kernel/mm/cake_export.c | 71 +++++------ lunaix-os/kernel/time/clock.c | 45 ++++--- 18 files changed, 366 insertions(+), 281 deletions(-) diff --git a/lunaix-os/hal/ahci/hbadev_export.c b/lunaix-os/hal/ahci/hbadev_export.c index 3fea81d..f6c52bf 100644 --- a/lunaix-os/hal/ahci/hbadev_export.c +++ b/lunaix-os/hal/ahci/hbadev_export.c @@ -3,14 +3,14 @@ #include void -__blk_rd_serial(struct twimap* map) +__twimap_read_serial(struct twimap* map) { struct hba_device* hbadev = twimap_data(map, struct hba_device*); twimap_printf(map, "%s", hbadev->serial_num); } void -__blk_rd_last_status(struct twimap* map) +__twimap_read_last_status(struct twimap* map) { struct hba_device* hbadev = twimap_data(map, struct hba_device*); twimap_printf(map, @@ -21,21 +21,21 @@ __blk_rd_last_status(struct twimap* map) } void -__blk_rd_capabilities(struct twimap* map) +__twimap_read_capabilities(struct twimap* map) { struct hba_device* hbadev = twimap_data(map, struct hba_device*); twimap_printf(map, "%p", hbadev->capabilities); } void -__blk_rd_aoffset(struct twimap* map) +__twimap_read_alignment(struct twimap* map) { struct hba_device* hbadev = twimap_data(map, struct hba_device*); twimap_printf(map, "%d", hbadev->alignment_offset); } void -__blk_rd_wwid(struct twimap* map) +__twimap_read_wwid(struct twimap* map) { struct hba_device* hbadev = twimap_data(map, struct hba_device*); u32_t h = hbadev->wwn >> 32; @@ -53,18 +53,9 @@ ahci_fsexport(struct block_dev* bdev, void* fs_node) struct twifs_node* dev_root = (struct twifs_node*)fs_node; struct twimap* map; - map = twifs_mapping(dev_root, bdev->driver, "serial"); - map->read = __blk_rd_serial; - - map = twifs_mapping(dev_root, bdev->driver, "last_status"); - map->read = __blk_rd_last_status; - - map = twifs_mapping(dev_root, bdev->driver, "wwid"); - map->read = __blk_rd_wwid; - - map = twifs_mapping(dev_root, bdev->driver, "capabilities"); - map->read = __blk_rd_capabilities; - - map = twifs_mapping(dev_root, bdev->driver, "alignment_offset"); - map->read = __blk_rd_aoffset; + twimap_export_value(dev_root, serial, FSACL_aR, bdev->driver); + twimap_export_value(dev_root, last_status, FSACL_aR, bdev->driver); + twimap_export_value(dev_root, wwid, FSACL_aR, bdev->driver); + twimap_export_value(dev_root, capabilities, FSACL_aR, bdev->driver); + twimap_export_value(dev_root, alignment, FSACL_aR, bdev->driver); } \ No newline at end of file diff --git a/lunaix-os/hal/bus/pci.c b/lunaix-os/hal/bus/pci.c index 0543d0a..cbad179 100644 --- a/lunaix-os/hal/bus/pci.c +++ b/lunaix-os/hal/bus/pci.c @@ -371,7 +371,7 @@ pci_apply_command(struct pci_probe* probe, pci_reg_t cmd) } static void -__pci_read_cspace(struct twimap* map) +__twimap_read_config(struct twimap* map) { struct pci_probe* probe; @@ -388,7 +388,7 @@ __pci_read_cspace(struct twimap* map) /*---------- TwiFS interface definition ----------*/ static void -__pci_read_revid(struct twimap* map) +__twimap_read_revision(struct twimap* map) { struct pci_probe* probe; @@ -397,7 +397,7 @@ __pci_read_revid(struct twimap* map) } static void -__pci_read_class(struct twimap* map) +__twimap_read_class(struct twimap* map) { struct pci_probe* probe; @@ -406,7 +406,7 @@ __pci_read_class(struct twimap* map) } static void -__pci_read_devinfo(struct twimap* map) +__twimap_read_pci_devinfo(struct twimap* map) { struct pci_probe* probe; @@ -417,7 +417,7 @@ __pci_read_devinfo(struct twimap* map) } static void -__pci_bar_read(struct twimap* map) +__twimap_read_io_bases(struct twimap* map) { struct pci_probe* probe; int bar_index; @@ -448,7 +448,7 @@ __pci_bar_read(struct twimap* map) } static int -__pci_bar_gonext(struct twimap* map) +__twimap_gonext_io_bases(struct twimap* map) { if (twimap_index(map, int) >= 5) { return 0; @@ -458,7 +458,13 @@ __pci_bar_gonext(struct twimap* map) } static void -__pci_read_binding(struct twimap* map) +__twimap_reset_io_bases(struct twimap* map) +{ + map->index = 0; +} + +static void +__twimap_read_binding(struct twimap* map) { struct pci_probe* probe; struct devident* devid; @@ -499,21 +505,11 @@ pci_build_fsmapping() probe = changeling_reveal(pos, pci_probe_morpher); pci_dev = twifs_dir_node(pci_class, "%x", probe->loc); - map = twifs_mapping(pci_dev, probe, "config"); - map->read = __pci_read_cspace; - - map = twifs_mapping(pci_dev, probe, "revision"); - map->read = __pci_read_revid; - - map = twifs_mapping(pci_dev, probe, "class"); - map->read = __pci_read_class; - - map = twifs_mapping(pci_dev, probe, "binding"); - map->read = __pci_read_binding; - - map = twifs_mapping(pci_dev, probe, "io_bases"); - map->read = __pci_bar_read; - map->go_next = __pci_bar_gonext; + twimap_export_value(pci_dev, config, FSACL_aR, probe); + twimap_export_value(pci_dev, revision, FSACL_aR, probe); + twimap_export_value(pci_dev, class, FSACL_aR, probe); + twimap_export_value(pci_dev, binding, FSACL_aR, probe); + twimap_export_list (pci_dev, io_bases, FSACL_aR, probe); } } EXPORT_TWIFS_PLUGIN(pci_devs, pci_build_fsmapping); diff --git a/lunaix-os/hal/rtc/rtc_device.c b/lunaix-os/hal/rtc/rtc_device.c index 396c9bb..8676b3f 100644 --- a/lunaix-os/hal/rtc/rtc_device.c +++ b/lunaix-os/hal/rtc/rtc_device.c @@ -166,7 +166,9 @@ static void hwrtc_twifs_export(struct hwrtc_potens* pot) { const char* name = pot->rtc_proxy->name_val; - struct twimap* rtc_mapping = twifs_mapping(NULL, pot, name); + struct twimap* rtc_mapping; + + rtc_mapping = twifs_mapping(NULL, pot, FSACL_aR, name); rtc_mapping->read = __hwrtc_readinfo; } diff --git a/lunaix-os/includes/lunaix/fs/twifs.h b/lunaix-os/includes/lunaix/fs/twifs.h index 50579ae..733db19 100644 --- a/lunaix-os/includes/lunaix/fs/twifs.h +++ b/lunaix-os/includes/lunaix/fs/twifs.h @@ -6,26 +6,44 @@ #include #include +struct twifs_ops +{ + int (*write)(struct v_inode* inode, + void* buffer, + size_t len, + size_t fpos); + int (*read)(struct v_inode* inode, + void* buffer, + size_t len, + size_t fpos); +}; + struct twifs_node { struct hstr name; inode_t ino_id; void* data; u32_t itype; + int acl; + char name_val[VFS_NAME_MAXLEN]; struct llist_header children; struct llist_header siblings; - struct - { - int (*write)(struct v_inode* inode, - void* buffer, - size_t len, - size_t fpos); - int (*read)(struct v_inode* inode, - void* buffer, - size_t len, - size_t fpos); - } ops; + struct twifs_ops ops; +}; + +struct twifs_export +{ + char* name; + int acl; + struct twifs_ops ops; +}; + +struct twimap_export +{ + char* name; + int acl; + struct twimap_ops ops; }; #define twinode_getdata(inode, type) \ @@ -38,32 +56,78 @@ struct twifs_node #define EXPORT_TWIFS_PLUGIN(label, plg_init) \ export_ldga_el(twiplugin_inits, label, ptr_t, plg_init) -void -twifs_register_plugins(); +#define __twifs_export_base(name_, acl_) \ + .name=stringify(name_), .acl=(acl_) + +#define twifs_node_ro(name_, acl_) \ + struct twifs_export __twifs_exp_##name_ = \ + { __twifs_export_base(name_, acl_), \ + .ops = { .read = __twifs_read_##name_ }} + +#define twifs_node_rw(name_, acl_) \ + struct twifs_export __twifs_exp_##name_ = \ + { __twifs_export_base(name_, acl_), \ + .ops = { .read = __twifs_read_##name_, \ + .write = __twifs_write_##name_ }} + +#define twimap_value_export(name_, acl_) \ + struct twimap_export __twimap_exp_##name_ = \ + { __twifs_export_base(name_, acl_), \ + .ops = { .read = __twimap_read_##name_ }} + +#define twimap_list_export(name_, acl_) \ + struct twimap_export __twimap_exp_##name_ = \ + { __twifs_export_base(name_, acl_), \ + .ops = { .read = __twimap_read_##name_, \ + .go_next = __twimap_gonext_##name_, \ + .reset = __twimap_reset_##name_, }} + +#define twifs_export(parent, name_, data_) \ + twifs_basic_node_from(parent, &__twifs_exp_##name_, data_) + +#define twimap_export(parent, name_, data_) \ + twifs_mapped_node_from(parent, &__twimap_exp_##name_, data_) + +#define twifs_export_ro(parent, name_, acl_, data_) \ + ({ \ + twifs_node_ro(name_, acl_); \ + twifs_export(parent, name_, data_); \ + }) + +#define twimap_export_value(parent, name_, acl_, data_) \ + ({ \ + twimap_value_export(name_, acl_); \ + twimap_export(parent, name_, data_); \ + }) + +#define twimap_export_list(parent, name_, acl_, data_) \ + ({ \ + twimap_list_export(name_, acl_); \ + twimap_export(parent, name_, data_); \ + }) void -twifs_init(); +twifs_register_plugins(); struct twifs_node* twifs_file_node_vargs(struct twifs_node* parent, const char* fmt, va_list args); -struct twifs_node* -twifs_file_node(struct twifs_node* parent, const char* fmt, ...); - struct twifs_node* twifs_dir_node(struct twifs_node* parent, const char* fmt, ...); int twifs_rm_node(struct twifs_node* node); -struct twimap* -twifs_mapping(struct twifs_node* parent, void* data, const char* fmt, ...); +struct twifs_node* +twifs_basic_node_from(struct twifs_node* parent, + struct twifs_export* exp_def, void* data); -#define twimap_entry_simple(parent, name, data, read_cb) \ - ({ \ - struct twimap* map = twifs_mapping((parent), (data), (name)); \ - map->read = (read_cb); \ - map; \ - }) +struct twifs_node* +twifs_mapped_node_from(struct twifs_node* parent, + struct twimap_export* exp_def, void* data); + +struct twimap* +twifs_mapping(struct twifs_node* parent, + void* data, int acl, const char* fmt, ...); #endif /* __LUNAIX_TWIFS_H */ diff --git a/lunaix-os/includes/lunaix/fs/twimap.h b/lunaix-os/includes/lunaix/fs/twimap.h index c9d56a2..3086c63 100644 --- a/lunaix-os/includes/lunaix/fs/twimap.h +++ b/lunaix-os/includes/lunaix/fs/twimap.h @@ -8,15 +8,29 @@ extern struct v_file_ops twimap_file_ops; +#define __TWIMAP_OPS \ + void (*read)(struct twimap* mapping); \ + int (*go_next)(struct twimap* mapping); \ + void (*reset)(struct twimap* mapping); + +struct twimap; +struct twimap_ops +{ + __TWIMAP_OPS +}; + struct twimap { void* index; void* buffer; void* data; size_t size_acc; - void (*read)(struct twimap* mapping); - int (*go_next)(struct twimap* mapping); - void (*reset)(struct twimap* mapping); + union + { + struct twimap_ops ops; + struct { __TWIMAP_OPS }; + }; + }; int diff --git a/lunaix-os/includes/lunaix/fs_acl.h b/lunaix-os/includes/lunaix/fs_acl.h index fde7225..43ab5e2 100644 --- a/lunaix-os/includes/lunaix/fs_acl.h +++ b/lunaix-os/includes/lunaix/fs_acl.h @@ -41,13 +41,17 @@ #define FSACL_g_ 0 #define FSACL_o_ 0 -// any read +// any #define FSACL_aR (FSACL_uR | FSACL_gR | FSACL_oR) -// any write #define FSACL_aW (FSACL_uW | FSACL_gW | FSACL_oW) -// any execute #define FSACL_aX (FSACL_uX | FSACL_gX | FSACL_oX) +// user/group +#define FSACL_ugR (FSACL_uR | FSACL_gR) +#define FSACL_ugW (FSACL_uW | FSACL_gW) +#define FSACL_ugX (FSACL_uX | FSACL_gX) + + #define __fsacl_sel(scope, type) (FSACL_##scope##type) #define FSACL_u(r, w, x) \ (v__(__fsacl_sel(u, r)) | v__(__fsacl_sel(u, w)) | v__(__fsacl_sel(u, x))) diff --git a/lunaix-os/kernel/block/blk_mapping.c b/lunaix-os/kernel/block/blk_mapping.c index 7dae2b8..ddc360a 100644 --- a/lunaix-os/kernel/block/blk_mapping.c +++ b/lunaix-os/kernel/block/blk_mapping.c @@ -9,37 +9,37 @@ blk_mapping_init() blk_root = twifs_dir_node(NULL, "block"); } -void -__blk_rd_lblksz(struct twimap* map) +static void +__twimap_read_lblk_size(struct twimap* map) { struct block_dev* bdev = twimap_data(map, struct block_dev*); size_t lblksz = bdev->blk_size; twimap_printf(map, "%u", lblksz); } -void -__blk_rd_name(struct twimap* map) +static void +__twimap_read_name(struct twimap* map) { struct block_dev* bdev = twimap_data(map, struct block_dev*); twimap_printf(map, "%s", bdev->name); } -void -__blk_rd_start_lba(struct twimap* map) +static void +__twimap_read_lba_begin(struct twimap* map) { struct block_dev* bdev = twimap_data(map, struct block_dev*); twimap_printf(map, "%d", bdev->start_lba); } -void -__blk_rd_end_lba(struct twimap* map) +static void +__twimap_read_lba_end(struct twimap* map) { struct block_dev* bdev = twimap_data(map, struct block_dev*); twimap_printf(map, "%d", bdev->end_lba); } -void -__blk_rd_size(struct twimap* map) +static void +__twimap_read_size(struct twimap* map) { struct block_dev* bdev = twimap_data(map, struct block_dev*); twimap_printf( @@ -49,22 +49,15 @@ __blk_rd_size(struct twimap* map) void __map_internal(struct block_dev* bdev, void* fsnode) { - struct twifs_node* dev_root = (struct twifs_node*)fsnode; - - struct twimap* map = twifs_mapping(dev_root, bdev, "size"); - map->read = __blk_rd_size; - - map = twifs_mapping(dev_root, bdev, "lblk_size"); - map->read = __blk_rd_lblksz; - - map = twifs_mapping(dev_root, bdev, "name"); - map->read = __blk_rd_name; - - map = twifs_mapping(dev_root, bdev, "begin"); - map->read = __blk_rd_start_lba; - - map = twifs_mapping(dev_root, bdev, "end"); - map->read = __blk_rd_end_lba; + struct twifs_node* dev_root; + + dev_root = (struct twifs_node*)fsnode; + + twimap_export_value(dev_root, size, FSACL_aR, bdev); + twimap_export_value(dev_root, lblk_size, FSACL_aR, bdev); + twimap_export_value(dev_root, name, FSACL_aR, bdev); + twimap_export_value(dev_root, lba_begin, FSACL_aR, bdev); + twimap_export_value(dev_root, lba_end, FSACL_aR, bdev); } void diff --git a/lunaix-os/kernel/device/devdb.c b/lunaix-os/kernel/device/devdb.c index a41364d..42452f8 100644 --- a/lunaix-os/kernel/device/devdb.c +++ b/lunaix-os/kernel/device/devdb.c @@ -133,7 +133,7 @@ device_sysconf_load() } static int -__devdb_db_gonext(struct twimap* mapping) +__twimap_gonext_devtab(struct twimap* mapping) { struct device_def* current = twimap_index(mapping, struct device_def*); if (current->dev_list.next == &dev_registry_flat) { @@ -145,7 +145,7 @@ __devdb_db_gonext(struct twimap* mapping) } static void -__devdb_twifs_lsdb(struct twimap* mapping) +__twimap_read_devtab(struct twimap* mapping) { char flags[64]; struct device_def* def = twimap_index(mapping, struct device_def*); @@ -161,8 +161,8 @@ __devdb_twifs_lsdb(struct twimap* mapping) flags); } -void -__devdb_reset(struct twimap* map) +static void +__twimap_reset_devtab(struct twimap* map) { map->index = container_of(dev_registry_flat.next, struct device_def, dev_list); @@ -171,9 +171,6 @@ __devdb_reset(struct twimap* map) static void devdb_twifs_plugin() { - struct twimap* map = twifs_mapping(NULL, NULL, "devtab"); - map->reset = __devdb_reset; - map->read = __devdb_twifs_lsdb; - map->go_next = __devdb_db_gonext; + twimap_export_list(NULL, devtab, FSACL_aR, NULL); } EXPORT_TWIFS_PLUGIN(devdb, devdb_twifs_plugin); \ No newline at end of file diff --git a/lunaix-os/kernel/ds/waitq.c b/lunaix-os/kernel/ds/waitq.c index c102882..e67c44d 100644 --- a/lunaix-os/kernel/ds/waitq.c +++ b/lunaix-os/kernel/ds/waitq.c @@ -12,7 +12,7 @@ __try_wait(bool check_stall) if (waitq_empty(current_wq)) { return; } - + block_current_thread(); if (!check_stall) { @@ -54,7 +54,7 @@ pwait_check_stall(waitq_t* queue) void pwake_one(waitq_t* queue) { - if (llist_empty(&queue->waiters)) { + if (waitq_empty(queue)) { return; } @@ -69,7 +69,7 @@ pwake_one(waitq_t* queue) void pwake_all(waitq_t* queue) { - if (llist_empty(&queue->waiters)) { + if (waitq_empty(queue)) { return; } diff --git a/lunaix-os/kernel/exe/exec.c b/lunaix-os/kernel/exe/exec.c index 5ac00b3..3c1fb42 100644 --- a/lunaix-os/kernel/exe/exec.c +++ b/lunaix-os/kernel/exe/exec.c @@ -276,14 +276,8 @@ exec_kexecve(const char* filename, const char* argv[], const char* envp[]) return errno; } -__DEFINE_LXSYSCALL3(int, - execve, - const char*, - filename, - const char*, - argv[], - const char*, - envp[]) +__DEFINE_LXSYSCALL3(int, execve, const char*, filename, + const char*, argv[], const char*, envp[]) { int errno = 0; int acl; @@ -326,6 +320,5 @@ done: // Always yield the process that want execve! schedule(); - // this will never get executed! - return -1; + unreachable; } \ No newline at end of file diff --git a/lunaix-os/kernel/fs/fs_export.c b/lunaix-os/kernel/fs/fs_export.c index 1fedef6..452bcdf 100644 --- a/lunaix-os/kernel/fs/fs_export.c +++ b/lunaix-os/kernel/fs/fs_export.c @@ -4,8 +4,8 @@ extern struct llist_header all_mnts; -void -__mount_read(struct twimap* map) +static void +__twimap_read_mounts(struct twimap* map) { char path[512]; struct v_mount* mnt = twimap_index(map, struct v_mount*); @@ -20,8 +20,8 @@ __mount_read(struct twimap* map) twimap_printf(map, "\n"); } -int -__mount_next(struct twimap* map) +static int +__twimap_gonext_mounts(struct twimap* map) { struct v_mount* mnt = twimap_index(map, struct v_mount*); if (mnt->list.next == &all_mnts) { @@ -31,14 +31,14 @@ __mount_next(struct twimap* map) return 1; } -void -__mount_reset(struct twimap* map) +static void +__twimap_reset_mounts(struct twimap* map) { map->index = container_of(all_mnts.next, struct v_mount, list); } void -__version_rd(struct twimap* map) +__twimap_read_version(struct twimap* map) { twimap_printf(map, "Lunaix " @@ -49,12 +49,7 @@ __version_rd(struct twimap* map) void vfs_export_attributes() { - struct twimap* map = twifs_mapping(NULL, NULL, "mounts"); - map->read = __mount_read; - map->go_next = __mount_next; - map->reset = __mount_reset; - - map = twifs_mapping(NULL, NULL, "version"); - map->read = __version_rd; + twimap_export_list (NULL, mounts, FSACL_ugR, NULL); + twimap_export_value(NULL, version, FSACL_ugR, NULL); } EXPORT_TWIFS_PLUGIN(vfs_general, vfs_export_attributes); \ No newline at end of file diff --git a/lunaix-os/kernel/fs/fsm.c b/lunaix-os/kernel/fs/fsm.c index 722ffd0..8638905 100644 --- a/lunaix-os/kernel/fs/fsm.c +++ b/lunaix-os/kernel/fs/fsm.c @@ -80,7 +80,7 @@ fsm_itnext(struct fs_iter* iterator) } static void -read_fslist(struct twimap *mapping) +__twimap_read_fstab(struct twimap *mapping) { struct filesystem *pos, *n; llist_for_each(pos, n, &fs_flatlist, fs_flat) @@ -92,7 +92,6 @@ read_fslist(struct twimap *mapping) static void fstab_twifs_plugin() { - struct twimap* map = twifs_mapping(NULL, NULL, "fstab"); - map->read = read_fslist; + twimap_export_value(NULL, fstab, FSACL_ugR, NULL); } EXPORT_TWIFS_PLUGIN(fstab, fstab_twifs_plugin); \ No newline at end of file diff --git a/lunaix-os/kernel/fs/twifs/twifs.c b/lunaix-os/kernel/fs/twifs/twifs.c index 9d47495..2fdf023 100644 --- a/lunaix-os/kernel/fs/twifs/twifs.c +++ b/lunaix-os/kernel/fs/twifs/twifs.c @@ -21,19 +21,15 @@ #include static struct twifs_node* fs_root; - static struct cake_pile* twi_pile; - static volatile u32_t inode_id = 0; extern const struct v_file_ops twifs_file_ops; extern const struct v_inode_ops twifs_inode_ops; -struct twifs_node* +static struct twifs_node* __twifs_new_node(struct twifs_node* parent, - const char* name, - int name_len, - u32_t itype) + const char* name, int name_len, u32_t itype) { struct twifs_node* node = cake_grab(twi_pile); memset(node, 0, sizeof(*node)); @@ -53,7 +49,7 @@ __twifs_new_node(struct twifs_node* parent, return node; } -void +static void __twifs_init_inode(struct v_superblock* vsb, struct v_inode* inode) { inode->ops = (struct v_inode_ops*)&twifs_inode_ops; @@ -67,7 +63,7 @@ __twifs_init_inode(struct v_superblock* vsb, struct v_inode* inode) fsapi_inode_setowner(inode, 0, 0); } -int +static int __twifs_mount(struct v_superblock* vsb, struct v_dnode* mount_point) { vsb->ops.init_inode = __twifs_init_inode; @@ -86,13 +82,13 @@ __twifs_mount(struct v_superblock* vsb, struct v_dnode* mount_point) return 0; } -int +static int __twifs_unmount(struct v_superblock* vsb) { return 0; } -int +static int __twifs_fwrite(struct v_inode* inode, void* buffer, size_t len, size_t fpos) { struct twifs_node* twi_node = (struct twifs_node*)inode->data; @@ -102,13 +98,13 @@ __twifs_fwrite(struct v_inode* inode, void* buffer, size_t len, size_t fpos) return twi_node->ops.write(inode, buffer, len, fpos); } -int +static int __twifs_fwrite_pg(struct v_inode* inode, void* buffer, size_t fpos) { return __twifs_fwrite(inode, buffer, PAGE_SIZE, fpos); } -int +static int __twifs_fread(struct v_inode* inode, void* buffer, size_t len, size_t fpos) { struct twifs_node* twi_node = (struct twifs_node*)inode->data; @@ -118,13 +114,13 @@ __twifs_fread(struct v_inode* inode, void* buffer, size_t len, size_t fpos) return twi_node->ops.read(inode, buffer, len, fpos); } -int +static int __twifs_fread_pg(struct v_inode* inode, void* buffer, size_t fpos) { return __twifs_fread(inode, buffer, PAGE_SIZE, fpos); } -struct twifs_node* +static struct twifs_node* __twifs_get_node(struct twifs_node* parent, struct hstr* name) { if (!parent) @@ -140,7 +136,7 @@ __twifs_get_node(struct twifs_node* parent, struct hstr* name) return NULL; } -int +static int __twifs_dirlookup(struct v_inode* inode, struct v_dnode* dnode) { struct twifs_node* twi_node = (struct twifs_node*)inode->data; @@ -149,9 +145,9 @@ __twifs_dirlookup(struct v_inode* inode, struct v_dnode* dnode) return ENOTDIR; } - struct twifs_node* child_node = __twifs_get_node(twi_node, &dnode->name); - if (child_node) { - struct v_inode* child_inode = vfs_i_find(inode->sb, child_node->ino_id); + struct twifs_node* twinode = __twifs_get_node(twi_node, &dnode->name); + if (twinode) { + struct v_inode* child_inode = vfs_i_find(inode->sb, twinode->ino_id); if (child_inode) { goto done; } @@ -160,21 +156,25 @@ __twifs_dirlookup(struct v_inode* inode, struct v_dnode* dnode) return ENOENT; } - child_inode->id = child_node->ino_id; - child_inode->itype = child_node->itype; - child_inode->data = child_node; + child_inode->id = twinode->ino_id; + child_inode->itype = twinode->itype; + child_inode->data = twinode; + + if (twinode->acl != -1) { + fsapi_inode_setaccess(child_inode, twinode->acl); + } vfs_i_addhash(child_inode); done: - dnode->data = child_node->data; + dnode->data = twinode->data; vfs_assign_inode(dnode, child_inode); return 0; } return ENOENT; } -int +static int __twifs_iterate_dir(struct v_file* file, struct dir_context* dctx) { struct twifs_node* twi_node = (struct twifs_node*)(file->inode->data); @@ -197,7 +197,7 @@ __twifs_iterate_dir(struct v_file* file, struct dir_context* dctx) return 0; } -int +static int __twifs_openfile(struct v_inode* inode, struct v_file* file) { struct twifs_node* twi_node = (struct twifs_node*)inode->data; @@ -207,6 +207,30 @@ __twifs_openfile(struct v_inode* inode, struct v_file* file) return ENOTSUP; } +static inline struct twifs_node* +__twifs_create_node(struct twifs_node* parent, + const char* name, int acl) +{ + struct twifs_node* twi_node; + unsigned int size; + + size = strlen(name); + parent = parent ?: fs_root; + twi_node = __twifs_new_node(parent, name, size, F_FILE); + twi_node->acl = acl; + + return twi_node; +} + +static int +__twifs_twimap_file_read(struct v_inode* inode, + void* buf, size_t len, size_t fpos) +{ + struct twimap* map = twinode_getdata(inode, struct twimap*); + return twimap_read(map, buf, len, fpos); +} + + int twifs_rm_node(struct twifs_node* node) { @@ -228,78 +252,90 @@ twifs_file_node_vargs(struct twifs_node* parent, const char* fmt, va_list args) } struct twifs_node* -twifs_file_node(struct twifs_node* parent, const char* fmt, ...) +twifs_dir_node(struct twifs_node* parent, const char* fmt, ...) { + size_t len; + char buf[VFS_NAME_MAXLEN]; + struct twifs_node* twi_node; + va_list args; va_start(args, fmt); - struct twifs_node* twi_node = twifs_file_node_vargs(parent, fmt, args); + len = ksnprintfv(buf, fmt, VFS_NAME_MAXLEN, args); + twi_node = + __twifs_new_node(parent ? parent : fs_root, buf, len, VFS_IFDIR); + + twi_node->acl = FSACL_aR | FSACL_aX; va_end(args); return twi_node; } + struct twifs_node* -twifs_dir_node(struct twifs_node* parent, const char* fmt, ...) +twifs_basic_node_from(struct twifs_node* parent, + struct twifs_export* def, void* data) { - va_list args; - va_start(args, fmt); - - char buf[VFS_NAME_MAXLEN]; - size_t len = ksnprintfv(buf, fmt, VFS_NAME_MAXLEN, args); - struct twifs_node* twi_node = - __twifs_new_node(parent ? parent : fs_root, buf, len, VFS_IFDIR); - - va_end(args); + struct twifs_node* twi_node; + + twi_node = __twifs_create_node(parent, def->name, def->acl); + twi_node->ops = def->ops; + twi_node->data = data; return twi_node; } -void -twifs_init() +struct twifs_node* +twifs_mapped_node_from(struct twifs_node* parent, + struct twimap_export* def, void* data) { - struct filesystem* fs; - fs = fsapi_fs_declare("twifs", FSTYPE_PSEUDO | FSTYPE_ROFS); + struct twifs_node* twi_node; + struct twimap* map_; - fsapi_fs_set_mntops(fs, __twifs_mount, __twifs_unmount); - fsapi_fs_finalise(fs); + map_ = twimap_create(data); + twi_node = __twifs_create_node(parent, def->name, def->acl); - twi_pile = cake_new_pile("twifs_node", sizeof(struct twifs_node), 1, 0); - fs_root = twifs_dir_node(NULL, NULL, 0, 0); -} -EXPORT_FILE_SYSTEM(twifs, twifs_init); + twi_node->data = map_; + twi_node->ops.read = __twifs_twimap_file_read; + // twi_node->ops.write = __twifs_twimap_file_write; -void -twifs_register_plugins() -{ - ldga_invoke_fn0(twiplugin_inits); -} + if (def->ops.go_next) { + map_->ops.go_next = def->ops.go_next; + } -int -__twifs_twimap_file_read(struct v_inode* inode, - void* buf, - size_t len, - size_t fpos) -{ - struct twimap* map = twinode_getdata(inode, struct twimap*); - return twimap_read(map, buf, len, fpos); + if (def->ops.reset) { + map_->ops.reset = def->ops.reset; + } + + map_->ops.read = def->ops.read; + + return twi_node; } struct twimap* -twifs_mapping(struct twifs_node* parent, void* data, const char* fmt, ...) +twifs_mapping(struct twifs_node* parent, + void* data, int acl, const char* fmt, ...) { va_list args; va_start(args, fmt); struct twimap* map = twimap_create(data); struct twifs_node* node = twifs_file_node_vargs(parent, fmt, args); + node->ops.read = __twifs_twimap_file_read; node->data = map; + node->acl = acl; return map; } +void +twifs_register_plugins() +{ + ldga_invoke_fn0(twiplugin_inits); +} + const struct v_file_ops twifs_file_ops = { .close = default_file_close, .read = __twifs_fread, .read_page = __twifs_fread_pg, @@ -310,4 +346,19 @@ const struct v_file_ops twifs_file_ops = { .close = default_file_close, const struct v_inode_ops twifs_inode_ops = { .dir_lookup = __twifs_dirlookup, .mkdir = default_inode_mkdir, .rmdir = default_inode_rmdir, - .open = __twifs_openfile }; \ No newline at end of file + .open = __twifs_openfile }; + + +static void +twifs_init() +{ + struct filesystem* fs; + fs = fsapi_fs_declare("twifs", FSTYPE_PSEUDO | FSTYPE_ROFS); + + fsapi_fs_set_mntops(fs, __twifs_mount, __twifs_unmount); + fsapi_fs_finalise(fs); + + twi_pile = cake_new_pile("twifs_node", sizeof(struct twifs_node), 1, 0); + fs_root = twifs_dir_node(NULL, NULL, 0, 0); +} +EXPORT_FILE_SYSTEM(twifs, twifs_init); \ No newline at end of file diff --git a/lunaix-os/kernel/kprint/kprintf.c b/lunaix-os/kernel/kprint/kprintf.c index 376df36..dc46628 100644 --- a/lunaix-os/kernel/kprint/kprintf.c +++ b/lunaix-os/kernel/kprint/kprintf.c @@ -99,7 +99,7 @@ kprintf_v(const char* component, const char* fmt, ...) } static void -__twimap_kprintf_read(struct twimap* map) +__twimap_read_kmsg(struct twimap* map) { struct kp_records* __kprecs = twimap_data(map, struct kp_records*); @@ -119,7 +119,7 @@ __twimap_kprintf_read(struct twimap* map) static void kprintf_mapping_init() { - twimap_entry_simple(NULL, "kmsg", &kprecs, __twimap_kprintf_read); + twimap_export_value(NULL, kmsg, FSACL_ugR, &kprecs); } EXPORT_TWIFS_PLUGIN(kprintf, kprintf_mapping_init); diff --git a/lunaix-os/kernel/lrud.c b/lunaix-os/kernel/lrud.c index dc9ee34..0395e42 100644 --- a/lunaix-os/kernel/lrud.c +++ b/lunaix-os/kernel/lrud.c @@ -159,7 +159,7 @@ lru_remove(struct lru_zone* zone, struct lru_node* node) } static void -read_lrulist_entry(struct twimap* map) +__twimap_read_lru_pool(struct twimap* map) { struct lru_zone* zone; @@ -181,14 +181,14 @@ read_lrulist_entry(struct twimap* map) } static void -read_lrulist_reset(struct twimap* map) +__twimap_reset_lru_pool(struct twimap* map) { map->index = container_of(&zone_lead, struct lru_zone, zones); twimap_printf(map, "name, n_objs, hot, n_evt, n_half, n_full, status\n"); } static int -read_lrulist_next(struct twimap* map) +__twimap_gonext_lru_pool(struct twimap* map) { struct lru_zone* zone; struct llist_header* next; @@ -206,11 +206,6 @@ read_lrulist_next(struct twimap* map) static void lru_pool_twimappable() { - struct twimap* map; - - map = twifs_mapping(NULL, NULL, "lru_pool"); - map->read = read_lrulist_entry; - map->reset = read_lrulist_reset; - map->go_next = read_lrulist_next; + twimap_export_list(NULL, lru_pool, FSACL_aR, NULL); } EXPORT_TWIFS_PLUGIN(__lru_twimap, lru_pool_twimappable); \ No newline at end of file diff --git a/lunaix-os/kernel/mm/cake.c b/lunaix-os/kernel/mm/cake.c index b1e699f..179145a 100644 --- a/lunaix-os/kernel/mm/cake.c +++ b/lunaix-os/kernel/mm/cake.c @@ -303,7 +303,7 @@ cake_grab(struct cake_pile* pile) if (!pos) return NULL; - + void* piece; piece_t found_index, *fl_slot; @@ -337,7 +337,9 @@ int cake_release(struct cake_pile* pile, void* area) { piece_t piece_index; - size_t dsize = 0; + size_t dsize = 0, maybe_index; + + piece_t *fl_slot; struct cake_s *pos, *n; struct llist_header* hdrs[2] = { &pile->full, &pile->partial }; @@ -347,9 +349,12 @@ cake_release(struct cake_pile* pile, void* area) if (pos->first_piece > area) { continue; } - dsize = (ptr_t)(area - pos->first_piece); - piece_index = dsize / pile->piece_size; - if (piece_index < pile->pieces_per_cake) { + + dsize = __ptr(area - pos->first_piece); + maybe_index = dsize / pile->piece_size; + + if (maybe_index < pile->pieces_per_cake) { + piece_index = (piece_t)maybe_index; goto found; } } @@ -360,14 +365,12 @@ cake_release(struct cake_pile* pile, void* area) found: assert(!(dsize % pile->piece_size)); - piece_t *fl_slot; + assert_msg(piece_index != pos->next_free, "double free"); fl_slot = __fl_slot(pos, piece_index); *fl_slot = pos->next_free; pos->next_free = piece_index; - assert_msg(*fl_slot != pos->next_free, "double free"); - pos->used_pieces--; pile->alloced_pieces--; diff --git a/lunaix-os/kernel/mm/cake_export.c b/lunaix-os/kernel/mm/cake_export.c index e2ecb3a..cde255c 100644 --- a/lunaix-os/kernel/mm/cake_export.c +++ b/lunaix-os/kernel/mm/cake_export.c @@ -3,8 +3,8 @@ extern struct llist_header piles; -int -__cake_stat_gonext(struct twimap* map) +static int +__twimap_gonext_pinkiepie(struct twimap* map) { struct cake_pile* pile = twimap_index(map, struct cake_pile*); if (pile->piles.next == &piles) { @@ -14,15 +14,15 @@ __cake_stat_gonext(struct twimap* map) return 1; } -void -__cake_stat_reset(struct twimap* map) +static void +__twimap_reset_pinkiepie(struct twimap* map) { map->index = container_of(&piles, struct cake_pile, piles); twimap_printf(map, "name cakes pages size slices actives\n"); } -void -__cake_rd_stat(struct twimap* map) +static void +__twimap_read_pinkiepie(struct twimap* map) { struct cake_pile* pos = twimap_index(map, struct cake_pile*); twimap_printf(map, @@ -35,36 +35,36 @@ __cake_rd_stat(struct twimap* map) pos->alloced_pieces); } -void -__cake_rd_psize(struct twimap* map) +static void +__twimap_read_piece_size(struct twimap* map) { struct cake_pile* pile = twimap_data(map, struct cake_pile*); twimap_printf(map, "%u", pile->piece_size); } -void -__cake_rd_ccount(struct twimap* map) +static void +__twimap_read_cake_count(struct twimap* map) { struct cake_pile* pile = twimap_data(map, struct cake_pile*); twimap_printf(map, "%u", pile->cakes_count); } -void -__cake_rd_alloced(struct twimap* map) +static void +__twimap_read_grabbed(struct twimap* map) { struct cake_pile* pile = twimap_data(map, struct cake_pile*); twimap_printf(map, "%u", pile->alloced_pieces); } -void -__cake_rd_ppc(struct twimap* map) +static void +__twimap_read_pieces_per_cake(struct twimap* map) { struct cake_pile* pile = twimap_data(map, struct cake_pile*); twimap_printf(map, "%u", pile->pieces_per_cake); } -void -__cake_rd_ppg(struct twimap* map) +static void +__twimap_read_page_per_cake(struct twimap* map) { struct cake_pile* pile = twimap_data(map, struct cake_pile*); twimap_printf(map, "%u", pile->pg_per_cake); @@ -73,37 +73,28 @@ __cake_rd_ppg(struct twimap* map) void cake_export_pile(struct twifs_node* root, struct cake_pile* pile) { - struct twifs_node* pile_rt = twifs_dir_node(root, pile->pile_name); - - struct twimap* map = twifs_mapping(pile_rt, pile, "piece_size"); - map->read = __cake_rd_psize; - - map = twifs_mapping(pile_rt, pile, "cake_count"); - map->read = __cake_rd_ccount; - - map = twifs_mapping(pile_rt, pile, "grabbed"); - map->read = __cake_rd_alloced; - - map = twifs_mapping(pile_rt, pile, "pieces_per_cake"); - map->read = __cake_rd_ppc; - - map = twifs_mapping(pile_rt, pile, "page_per_cake"); - map->read = __cake_rd_ppg; + struct twifs_node* pile_rt; + + pile_rt = twifs_dir_node(root, pile->pile_name); + + twimap_export_value(pile_rt, piece_size, FSACL_ugR, pile); + twimap_export_value(pile_rt, cake_count, FSACL_ugR, pile); + twimap_export_value(pile_rt, grabbed, FSACL_ugR, pile); + twimap_export_value(pile_rt, pieces_per_cake, FSACL_ugR, pile); + twimap_export_value(pile_rt, page_per_cake, FSACL_ugR, pile); } void cake_export() { - struct twifs_node* cake_root = twifs_dir_node(NULL, "cake"); + struct cake_pile *pos, *n; + struct twifs_node* cake_root; + + cake_root = twifs_dir_node(NULL, "cake"); - struct twimap* map = twifs_mapping(cake_root, NULL, "pinkiepie"); - map->reset = __cake_stat_reset; - map->go_next = __cake_stat_gonext; - map->read = __cake_rd_stat; + twimap_export_list(cake_root, pinkiepie, FSACL_ugR, NULL); - struct cake_pile *pos, *n; - llist_for_each(pos, n, &piles, piles) - { + llist_for_each(pos, n, &piles, piles) { cake_export_pile(cake_root, pos); } } diff --git a/lunaix-os/kernel/time/clock.c b/lunaix-os/kernel/time/clock.c index a5b9dd8..93354bb 100644 --- a/lunaix-os/kernel/time/clock.c +++ b/lunaix-os/kernel/time/clock.c @@ -5,15 +5,15 @@ #include -void -__clock_read_systime(struct twimap* map) +static void +__twimap_read_systime(struct twimap* map) { ticks_t sys_time = clock_systime(); twimap_printf(map, "%u", sys_time); } -void -__clock_read_datetime(struct twimap* map) +static void +__twimap_read_datetime(struct twimap* map) { datetime_t dt; clock_walltime(&dt); @@ -23,29 +23,12 @@ __clock_read_datetime(struct twimap* map) dt.hour, dt.minute, dt.second); } -void -__clock_read_unix(struct twimap* map) +static void +__twimap_read_unixtime(struct twimap* map) { twimap_printf(map, "%u", clock_unixtime()); } -void -clock_build_mapping() -{ - struct twifs_node* root = twifs_dir_node(NULL, "clock"); - struct twimap* map; - - map = twifs_mapping(root, NULL, "systime"); - map->read = __clock_read_systime; - - map = twifs_mapping(root, NULL, "unix"); - map->read = __clock_read_unix; - - map = twifs_mapping(root, NULL, "datetime"); - map->read = __clock_read_datetime; -} -EXPORT_TWIFS_PLUGIN(sys_clock, clock_build_mapping); - time_t clock_unixtime() { @@ -75,4 +58,18 @@ void clock_walltime(datetime_t* datetime) { sysrtc->ops->get_walltime(sysrtc, datetime); -} \ No newline at end of file +} + +static void +clock_build_mapping() +{ + struct twifs_node* root; + struct twimap* map; + + root = twifs_dir_node(NULL, "clock"); + + twimap_export_value(root, systime, FSACL_ugR, NULL); + twimap_export_value(root, unixtime, FSACL_ugR, NULL); + twimap_export_value(root, datetime, FSACL_ugR, NULL); +} +EXPORT_TWIFS_PLUGIN(sys_clock, clock_build_mapping); -- 2.27.0