kolibrios/kernel/trunk/umka/vdisk.c
Magomed Kostoev (mkostoevr) e92d2fb87b [KERNEL][UMKA] Make it compile and run on Windows
git-svn-id: svn://kolibrios.org@9342 a494cfbc-eb01-0410-851d-a64ba20cac60
2021-11-27 23:28:38 +00:00

102 lines
2.9 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "umka.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, 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));
return NULL;
}
fseek(f, 0, SEEK_END);
off_t fsize = ftell(f);
fseek(f, 0, SEEK_SET);
size_t sect_size = 512;
if (strstr(fname, "s4096") != NULL || strstr(fname, "s4k") != NULL) {
sect_size = 4096;
}
vdisk_t *vdisk = (vdisk_t*)malloc(sizeof(vdisk_t));
*vdisk = (vdisk_t){.file = f,
.sect_size = sect_size,
.sect_cnt = (uint64_t)fsize / sect_size,
.cache_size = cache_size,
.adjust_cache_size = adjust_cache_size};
return vdisk;
}
STDCALL void
vdisk_close(void *userdata) {
COVERAGE_OFF();
vdisk_t *vdisk = userdata;
fclose(vdisk->file);
free(vdisk);
COVERAGE_ON();
}
STDCALL int
vdisk_read(void *userdata, void *buffer, off_t startsector,
size_t *numsectors) {
COVERAGE_OFF();
vdisk_t *vdisk = userdata;
fseek(vdisk->file, startsector * vdisk->sect_size, SEEK_SET);
fread(buffer, *numsectors * vdisk->sect_size, 1, vdisk->file);
COVERAGE_ON();
return ERROR_SUCCESS;
}
STDCALL int
vdisk_write(void *userdata, void *buffer, off_t startsector,
size_t *numsectors) {
COVERAGE_OFF();
vdisk_t *vdisk = userdata;
fseek(vdisk->file, startsector * vdisk->sect_size, SEEK_SET);
fwrite(buffer, *numsectors * vdisk->sect_size, 1, vdisk->file);
COVERAGE_ON();
return ERROR_SUCCESS;
}
STDCALL int
vdisk_querymedia(void *userdata, diskmediainfo_t *minfo) {
COVERAGE_OFF();
vdisk_t *vdisk = userdata;
minfo->flags = 0u;
minfo->sector_size = vdisk->sect_size;
minfo->capacity = vdisk->sect_cnt;
COVERAGE_ON();
return ERROR_SUCCESS;
}
STDCALL 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,
};