2022-06-27 19:36:56 +02:00
|
|
|
/*
|
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
|
|
|
UMKa - User-Mode KolibriOS developer tools
|
|
|
|
vdisk - virtual disk
|
|
|
|
|
2023-01-05 06:20:40 +01:00
|
|
|
Copyright (C) 2020-2023 Ivan Baravy <dunkaist@gmail.com>
|
2022-06-27 19:36:56 +02:00
|
|
|
Copyright (C) 2021 Magomed Kostoev <mkostoevr@yandex.ru>
|
|
|
|
*/
|
|
|
|
|
2023-01-04 01:15:40 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <inttypes.h>
|
2020-02-20 04:41:53 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2020-05-07 03:57:01 +02:00
|
|
|
#include "umka.h"
|
2020-03-11 04:02:33 +01:00
|
|
|
#include "trace.h"
|
2020-05-07 03:41:08 +02:00
|
|
|
#include "vdisk.h"
|
2023-01-04 01:15:40 +01:00
|
|
|
#include "vdisk/raw.h"
|
|
|
|
#include "vdisk/qcow2.h"
|
2020-02-20 04:41:53 +01:00
|
|
|
|
2021-11-03 15:28:03 +01:00
|
|
|
STDCALL int
|
|
|
|
vdisk_querymedia(void *userdata, diskmediainfo_t *minfo) {
|
2020-03-11 04:02:33 +01:00
|
|
|
COVERAGE_OFF();
|
2023-01-04 01:15:40 +01:00
|
|
|
struct vdisk *disk = userdata;
|
2020-02-20 04:41:53 +01:00
|
|
|
minfo->flags = 0u;
|
2023-01-04 01:15:40 +01:00
|
|
|
minfo->sector_size = disk->sect_size;
|
|
|
|
minfo->capacity = disk->sect_cnt;
|
2020-03-11 04:02:33 +01:00
|
|
|
COVERAGE_ON();
|
2020-02-20 04:41:53 +01:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
2020-02-21 03:21:09 +01:00
|
|
|
|
2021-11-03 15:28:03 +01:00
|
|
|
STDCALL size_t
|
|
|
|
vdisk_adjust_cache_size(void *userdata, size_t suggested_size) {
|
2023-01-04 01:15:40 +01:00
|
|
|
struct vdisk *disk = userdata;
|
|
|
|
if (disk->adjust_cache_size) {
|
|
|
|
return disk->cache_size;
|
2020-05-07 03:41:08 +02:00
|
|
|
} else {
|
|
|
|
return suggested_size;
|
|
|
|
}
|
2020-02-21 03:21:09 +01:00
|
|
|
}
|
2020-05-07 03:41:08 +02:00
|
|
|
|
2023-01-04 01:15:40 +01:00
|
|
|
struct vdisk*
|
|
|
|
vdisk_init(const char *fname, int adjust_cache_size, size_t cache_size) {
|
2023-01-04 02:24:35 +01:00
|
|
|
size_t fname_len = strlen(fname);
|
|
|
|
size_t dot_raw_len = strlen(RAW_SUFFIX);
|
|
|
|
size_t dot_qcow2_len = strlen(QCOW2_SUFFIX);
|
2023-01-04 01:15:40 +01:00
|
|
|
struct vdisk *disk;
|
2023-01-04 02:24:35 +01:00
|
|
|
if (fname_len > dot_raw_len
|
|
|
|
&& !strcmp(fname + fname_len - dot_raw_len, RAW_SUFFIX)) {
|
2023-01-04 01:15:40 +01:00
|
|
|
disk = (struct vdisk*)vdisk_init_raw(fname);
|
2023-01-04 02:24:35 +01:00
|
|
|
} else if (fname_len > dot_qcow2_len
|
2023-01-05 06:20:40 +01:00
|
|
|
&& !strcmp(fname + fname_len - dot_qcow2_len, QCOW2_SUFFIX)) {
|
2023-01-04 01:15:40 +01:00
|
|
|
disk = (struct vdisk*)vdisk_init_qcow2(fname);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "[vdisk] file has unknown format: %s\n", fname);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
disk->diskfunc.closemedia = NULL;
|
|
|
|
disk->diskfunc.querymedia = vdisk_querymedia;
|
|
|
|
disk->diskfunc.flush = NULL;
|
|
|
|
disk->diskfunc.adjust_cache_size = vdisk_adjust_cache_size;
|
|
|
|
disk->adjust_cache_size = adjust_cache_size;
|
|
|
|
disk->cache_size = cache_size;
|
|
|
|
return disk;
|
|
|
|
}
|