From: Arkadiy <arkaisp2021@gmail.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, f4bug@amsat.org, mreitz@redhat.com,
NDNF <arkaisp2021@gmail.com>, Arkasha <ivanovrkasha@gmail.com>,
pavel.dovgaluk@ispras.ru
Subject: [PATCH V2] block: increased maximum size of vvfat devices
Date: Wed, 17 Mar 2021 17:39:45 +0300 [thread overview]
Message-ID: <20210317143945.15140-1-arkaisp2021@gmail.com> (raw)
From: NDNF <arkaisp2021@gmail.com>
This fixes the problem of the impossibility to create
FAT16 disks larger than 504 mb:
The change CHS made it possible to obtain a larger disk.
Also, auto-detection of disk parameters was added depending
on the volume of the connected files:
The size of all folders and files on the created disk is calculated
and the size of the FAT table is added.
This size allows to choose the future size of the FAT drive
from the standard limitations.
Signed-off-by: Arkasha <ivanovrkasha@gmail.com>
---
block/vvfat.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 85 insertions(+), 9 deletions(-)
diff --git a/block/vvfat.c b/block/vvfat.c
index 54807f82ca..b9e3d7e003 100644
--- a/block/vvfat.c
+++ b/block/vvfat.c
@@ -932,8 +932,6 @@ static int init_directories(BDRVVVFATState* s,
/* Now build FAT, and write back information into directory */
init_fat(s);
- /* TODO: if there are more entries, bootsector has to be adjusted! */
- s->root_entries = 0x02 * 0x10 * s->sectors_per_cluster;
s->cluster_count=sector2cluster(s, s->sector_count);
mapping = array_get_next(&(s->mapping));
@@ -1133,11 +1131,52 @@ static void vvfat_parse_filename(const char *filename, QDict *options,
qdict_put_bool(options, "rw", rw);
}
+static bool check_size(uint32_t offset_to_bootsector, int cyls, int heads,
+ int secs, uint8_t sectors_per_cluster, int fat_type,
+ uint64_t sum, uint64_t size_disk)
+{
+ uint32_t sector_count = cyls * heads * secs - offset_to_bootsector;
+ int i = 1 + sectors_per_cluster * 0x200 * 8 / fat_type;
+ uint16_t sectors_per_fat = (sector_count + i) / i;
+ /*size + FAT1 and FAT2 table size*/
+ if ((sum + sectors_per_fat * 512 * 2) < size_disk) {
+ return true;
+ }
+ return false;
+}
+
+static uint64_t find_size(const char *dirname, unsigned int cluster)
+{
+ uint64_t sum = 0;
+ DIR *dir = opendir(dirname);
+ struct dirent *entry;
+ while ((entry = readdir(dir))) {
+ uint16_t length = strlen(dirname) + 2 + strlen(entry->d_name);
+ char *buffer;
+ struct stat st;
+ buffer = g_malloc(length);
+ snprintf(buffer, length, "%s/%s", dirname, entry->d_name);
+ if (stat(buffer, &st) < 0) {
+ g_free(buffer);
+ continue;
+ }
+ if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")
+ && S_ISDIR(st.st_mode)) {
+ sum += find_size(buffer, cluster);
+ }
+ g_free(buffer);
+ sum += (st.st_size + cluster - 1) / (cluster) * (cluster);
+ }
+ closedir(dir);
+ return sum;
+}
+
static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
Error **errp)
{
BDRVVVFATState *s = bs->opaque;
int cyls, heads, secs;
+ uint64_t size_disk;
bool floppy;
const char *dirname, *label;
QemuOpts *opts;
@@ -1163,6 +1202,28 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
s->fat_type = qemu_opt_get_number(opts, "fat-type", 0);
floppy = qemu_opt_get_bool(opts, "floppy", false);
+ uint64_t sum = 0;
+ if (floppy) {
+ if (!s->fat_type) {
+ s->sectors_per_cluster = 2;
+ } else {
+ s->sectors_per_cluster = 1;
+ }
+ } else if (s->fat_type == 12) {
+ s->offset_to_bootsector = 0x3f;
+ s->sectors_per_cluster = 0x10;
+ } else {
+ s->offset_to_bootsector = 0x3f;
+ /* LATER TODO: if FAT32, adjust */
+ s->sectors_per_cluster = 0x80;
+ }
+
+ sum += find_size(dirname, s->sectors_per_cluster * 0x200);
+ /* TODO: if there are more entries, bootsector has to be adjusted! */
+ s->root_entries = 0x02 * 0x10 * s->sectors_per_cluster;
+ /*File size + boot sector size + root directory size*/
+ sum += 512 + s->root_entries * 32;
+
memset(s->volume_label, ' ', sizeof(s->volume_label));
label = qemu_opt_get(opts, "label");
if (label) {
@@ -1182,22 +1243,40 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
if (!s->fat_type) {
s->fat_type = 12;
secs = 36;
- s->sectors_per_cluster = 2;
} else {
secs = s->fat_type == 12 ? 18 : 36;
- s->sectors_per_cluster = 1;
}
cyls = 80;
heads = 2;
} else {
- /* 32MB or 504MB disk*/
if (!s->fat_type) {
s->fat_type = 16;
}
- s->offset_to_bootsector = 0x3f;
+ size_disk = 528482304;
cyls = s->fat_type == 12 ? 64 : 1024;
heads = 16;
secs = 63;
+ if (!check_size(s->offset_to_bootsector, cyls, heads, secs,
+ s->sectors_per_cluster, s->fat_type, sum,
+ size_disk)) {
+ if (s->fat_type > 12) {
+ size_disk = 4294950912;
+ cyls = 8322;
+ heads = 16;
+ secs = 63;
+
+ } else {
+ fprintf(stderr, "Requires Fat16 or Fat32\n");
+ return -2;
+ }
+ if (!check_size(s->offset_to_bootsector, cyls, heads,
+ secs, s->sectors_per_cluster,
+ s->fat_type, sum, size_disk)) {
+ fprintf(stderr, "Folder is larger than %f GB\n",
+ (float)size_disk / 1073741824);
+ return -2;
+ }
+ }
}
switch (s->fat_type) {
@@ -1216,9 +1295,6 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
s->bs = bs;
- /* LATER TODO: if FAT32, adjust */
- s->sectors_per_cluster=0x10;
-
s->current_cluster=0xffffffff;
s->qcow = NULL;
--
2.17.1
next reply other threads:[~2021-03-17 15:19 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-17 14:39 Arkadiy [this message]
-- strict thread matches above, loose matches on Subject: below --
2021-03-17 12:54 [PATCH v2] block: increased maximum size of vvfat devices Arkadiy
2021-03-17 13:24 ` no-reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210317143945.15140-1-arkaisp2021@gmail.com \
--to=arkaisp2021@gmail.com \
--cc=f4bug@amsat.org \
--cc=ivanovrkasha@gmail.com \
--cc=kwolf@redhat.com \
--cc=mreitz@redhat.com \
--cc=pavel.dovgaluk@ispras.ru \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).