From cea3923666ab7753af2dff91858f01965731a675 Mon Sep 17 00:00:00 2001 From: Ivan Baravy Date: Thu, 7 May 2020 04:41:08 +0300 Subject: [PATCH] Allow per-vdisk cache adjusting settings. Also, move vdisk_functions callback structure to vdisk.c. --- kolibri.h | 2 +- umka_fuse.c | 13 +------------ umka_shell.c | 18 ++++-------------- vdisk.c | 28 +++++++++++++++++++++++----- vdisk.h | 4 +++- 5 files changed, 32 insertions(+), 33 deletions(-) diff --git a/kolibri.h b/kolibri.h index 82e1bcb..849b068 100644 --- a/kolibri.h +++ b/kolibri.h @@ -111,7 +111,7 @@ typedef struct { int (*read)(void *userdata, void *buffer, off_t startsector, size_t *numsectors) __attribute__((__stdcall__)); int (*write)(void *userdata, void *buffer, off_t startsector, size_t *numsectors) __attribute__((__stdcall__)); int (*flush)(void *userdata) __attribute__((__stdcall__)); - unsigned int (*adjust_cache_size)(void *userdata, uint32_t suggested_size) __attribute__((__stdcall__)); + unsigned int (*adjust_cache_size)(void *userdata, size_t suggested_size) __attribute__((__stdcall__)); } diskfunc_t; struct disk_t { diff --git a/umka_fuse.c b/umka_fuse.c index e035286..7c96866 100644 --- a/umka_fuse.c +++ b/umka_fuse.c @@ -123,24 +123,13 @@ static struct fuse_operations umka_oper = { .read = umka_read, }; -diskfunc_t vdisk_functions = { - .strucsize = sizeof(diskfunc_t), - .close = vdisk_close, - .closemedia = NULL, - .querymedia = vdisk_querymedia, - .read = vdisk_read, - .write = vdisk_write, - .flush = NULL, - .adjust_cache_size = NULL, - }; - int main(int argc, char *argv[]) { if (argc != 3) { printf("usage: umka_fuse dir img\n"); exit(1); } kos_init(); - void *userdata = vdisk_init(argv[2], 0u); + void *userdata = vdisk_init(argv[2], 1, 0u); void *vdisk = disk_add(&vdisk_functions, "hd0", userdata, 0); disk_media_changed(vdisk, 1); return fuse_main(argc-1, argv, &umka_oper, NULL); diff --git a/umka_shell.c b/umka_shell.c index 66ebdfc..8072306 100644 --- a/umka_shell.c +++ b/umka_shell.c @@ -50,17 +50,6 @@ #define DEFAULT_READDIR_ENCODING UTF8 #define DEFAULT_PATH_ENCODING UTF8 -diskfunc_t vdisk_functions = { - .strucsize = sizeof(diskfunc_t), - .close = vdisk_close, - .closemedia = NULL, - .querymedia = vdisk_querymedia, - .read = vdisk_read, - .write = vdisk_write, - .flush = NULL, - .adjust_cache_size = NULL, - }; - net_device_t vnet = { .device_type = NET_TYPE_ETH, .mtu = 1514, @@ -275,7 +264,8 @@ void shell_disk_add(int argc, char **argv) { puts(usage); return; } - unsigned cache_size = 0u; + size_t cache_size = 0; + int adjust_cache_size = 0; int opt; optind = 1; const char *file_name = argv[optind++]; @@ -284,7 +274,7 @@ void shell_disk_add(int argc, char **argv) { switch (opt) { case 'c': cache_size = strtoul(optarg, NULL, 0); - vdisk_functions.adjust_cache_size = vdisk_adjust_cache_size; + adjust_cache_size = 1; break; default: puts(usage); @@ -292,7 +282,7 @@ void shell_disk_add(int argc, char **argv) { } } - void *userdata = vdisk_init(file_name, cache_size); + void *userdata = vdisk_init(file_name, adjust_cache_size, cache_size); if (userdata) { COVERAGE_ON(); void *vdisk = disk_add(&vdisk_functions, disk_name, userdata, 0); diff --git a/vdisk.c b/vdisk.c index 2d1eb81..c3f53c4 100644 --- a/vdisk.c +++ b/vdisk.c @@ -5,15 +5,17 @@ #include #include "kolibri.h" #include "trace.h" +#include "vdisk.h" typedef struct { FILE *file; uint32_t sect_size; uint64_t sect_cnt; unsigned cache_size; + int adjust_cache_size; } vdisk_t; -void *vdisk_init(const char *fname, unsigned cache_size) { +void *vdisk_init(const char *fname, int adjust_cache_size, size_t cache_size) { FILE *f = fopen(fname, "r+"); if (!f) { printf("vdisk: can't open file '%s': %s\n", fname, strerror(errno)); @@ -30,7 +32,8 @@ void *vdisk_init(const char *fname, unsigned cache_size) { *vdisk = (vdisk_t){.file = f, .sect_size = sect_size, .sect_cnt = (uint64_t)fsize / sect_size, - .cache_size = cache_size}; + .cache_size = cache_size, + .adjust_cache_size = adjust_cache_size}; return vdisk; } @@ -77,7 +80,22 @@ int vdisk_querymedia(void *userdata, diskmediainfo_t *minfo) { } __attribute__((__stdcall__)) -unsigned vdisk_adjust_cache_size(vdisk_t *vdisk, unsigned suggested_size) { - (void)suggested_size; - return vdisk->cache_size; +size_t vdisk_adjust_cache_size(void *userdata, size_t suggested_size) { + vdisk_t *vdisk = userdata; + if (vdisk->adjust_cache_size) { + return vdisk->cache_size; + } else { + return suggested_size; + } } + +diskfunc_t vdisk_functions = { + .strucsize = sizeof(diskfunc_t), + .close = vdisk_close, + .closemedia = NULL, + .querymedia = vdisk_querymedia, + .read = vdisk_read, + .write = vdisk_write, + .flush = NULL, + .adjust_cache_size = vdisk_adjust_cache_size, + }; diff --git a/vdisk.h b/vdisk.h index 1f54ed6..b80e159 100644 --- a/vdisk.h +++ b/vdisk.h @@ -5,7 +5,7 @@ #include #include "kolibri.h" -void *vdisk_init(const char *fname, unsigned cache_size); +void *vdisk_init(const char *fname, int adjust_cache_size, size_t cache_size); __attribute__((__stdcall__)) void vdisk_close(void *userdata); @@ -24,4 +24,6 @@ int vdisk_querymedia(void *userdata, diskmediainfo_t *minfo); __attribute__((__stdcall__)) unsigned int vdisk_adjust_cache_size(void *userdata, unsigned suggested_size); +extern diskfunc_t vdisk_functions; + #endif // VDISK_H_INCLUDED