* [Qemu-devel] Sparse file support under windows
@ 2006-06-05 8:32 ZIGLIO, Frediano, VF-IT
2006-06-14 14:00 ` Fabrice Bellard
0 siblings, 1 reply; 5+ messages in thread
From: ZIGLIO, Frediano, VF-IT @ 2006-06-05 8:32 UTC (permalink / raw)
To: qemu-devel
[-- Attachment #1: Type: text/plain, Size: 289 bytes --]
Hi,
this patch add sparse file support under Windows (supported on NTFS).
It add a -s flag to qemu create and qemu convert. It contain also a new
implementation for ftruncate witch not fill manually space but only use
SetEndOfFile (so to handle correctly sparse file)
freddy77
[-- Attachment #2: sparse.diff --]
[-- Type: application/octet-stream, Size: 7681 bytes --]
diff -rub qemu-0.8.1.orig/block-qcow.c qemu-0.8.1/block-qcow.c
--- qemu-0.8.1.orig/block-qcow.c Wed May 3 13:32:58 2006
+++ qemu-0.8.1/block-qcow.c Wed May 31 15:39:59 2006
@@ -586,7 +586,7 @@
l1_size = ((total_size * 512) + (1LL << shift) - 1) >> shift;
header.l1_table_offset = cpu_to_be64(header_size);
- if (flags) {
+ if (flags & BDRV_ENCRYPTED) {
header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
} else {
header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
diff -rub qemu-0.8.1.orig/block.c qemu-0.8.1/block.c
--- qemu-0.8.1.orig/block.c Wed May 3 13:32:58 2006
+++ qemu-0.8.1/block.c Wed May 31 15:39:59 2006
@@ -753,18 +753,70 @@
close(s->fd);
}
+#ifdef _WIN32
+#include <windows.h>
+#include <winioctl.h>
+
+int qemu_ftruncate64(int fd, int64_t length)
+{
+ LARGE_INTEGER li;
+ LONG high;
+ HANDLE h;
+ BOOL res;
+
+ if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
+ return -1;
+
+ h = (HANDLE)_get_osfhandle(fd);
+
+ /* get current position, ftruncate do not change position */
+ li.HighPart = 0;
+ li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
+ if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
+ return -1;
+
+ high = length >> 32;
+ if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
+ return -1;
+ res = SetEndOfFile(h);
+
+ /* back to old position */
+ SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
+ return res ? 0 : -1;
+}
+
+static int set_sparse(int fd)
+{
+ DWORD returned;
+ return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
+ NULL, 0, NULL, 0, &returned, NULL);
+}
+#else
+#define set_sparse(fd) 1
+#endif
+
+
static int raw_create(const char *filename, int64_t total_size,
const char *backing_file, int flags)
{
int fd;
- if (flags || backing_file)
+ if ((flags & ~BDRV_FORCE_SPARSE) || backing_file)
return -ENOTSUP;
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
0644);
if (fd < 0)
return -EIO;
+
+ if (flags & BDRV_FORCE_SPARSE) {
+ if (!set_sparse(fd)) {
+ close(fd);
+ unlink(filename);
+ return -EIO;
+ }
+ }
+
ftruncate(fd, total_size * 512);
close(fd);
return 0;
diff -rub qemu-0.8.1.orig/qemu-img.c qemu-0.8.1/qemu-img.c
--- qemu-0.8.1.orig/qemu-img.c Wed May 3 13:32:58 2006
+++ qemu-0.8.1/qemu-img.c Wed May 31 15:39:59 2006
@@ -23,6 +23,10 @@
*/
#include "vl.h"
+#ifdef _WIN32
+#include <windows.h>
+#endif
+
void *get_mmap_addr(unsigned long size)
{
return NULL;
@@ -132,9 +136,9 @@
"QEMU disk image utility\n"
"\n"
"Command syntax:\n"
- " create [-e] [-b base_image] [-f fmt] filename [size]\n"
+ " create [-e] [-s] [-b base_image] [-f fmt] filename [size]\n"
" commit [-f fmt] filename\n"
- " convert [-c] [-e] [-f fmt] filename [-O output_fmt] output_filename\n"
+ " convert [-c] [-e] [-s] [-f fmt] filename [-O output_fmt] output_filename\n"
" info [-f fmt] filename\n"
"\n"
"Command parameters:\n"
@@ -148,6 +152,7 @@
" 'output_fmt' is the destination format\n"
" '-c' indicates that target image must be compressed (qcow format only)\n"
" '-e' indicates that the target image must be encrypted (qcow format only)\n"
+ " '-s' enable sparse file where needed (currently Windows)\n"
);
printf("\nSupported format:");
bdrv_iterate_format(format_print, NULL);
@@ -305,7 +310,7 @@
static int img_create(int argc, char **argv)
{
- int c, ret, encrypted;
+ int c, ret, create_flags;
const char *fmt = "raw";
const char *filename;
const char *base_filename = NULL;
@@ -313,9 +318,9 @@
const char *p;
BlockDriver *drv;
- encrypted = 0;
+ create_flags = 0;
for(;;) {
- c = getopt(argc, argv, "b:f:he");
+ c = getopt(argc, argv, "b:f:hes");
if (c == -1)
break;
switch(c) {
@@ -329,7 +334,10 @@
fmt = optarg;
break;
case 'e':
- encrypted = 1;
+ create_flags |= BDRV_ENCRYPTED;
+ break;
+ case 's':
+ create_flags |= BDRV_FORCE_SPARSE;
break;
}
}
@@ -363,14 +371,14 @@
error("Unknown file format '%s'", fmt);
printf("Formating '%s', fmt=%s",
filename, fmt);
- if (encrypted)
+ if (create_flags & BDRV_ENCRYPTED)
printf(", encrypted");
if (base_filename) {
printf(", backing_file=%s",
base_filename);
}
- printf(", size=%lld kB\n", (long long) (size / 1024));
- ret = bdrv_create(drv, filename, size / 512, base_filename, encrypted);
+ printf(", size=%" PRINTF_LL(d) " kB\n", (long long) (size / 1024));
+ ret = bdrv_create(drv, filename, size / 512, base_filename, create_flags);
if (ret < 0) {
if (ret == -ENOTSUP) {
error("Formatting or formatting option not supported for file format '%s'", fmt);
@@ -475,7 +483,7 @@
static int img_convert(int argc, char **argv)
{
- int c, ret, n, n1, compress, cluster_size, cluster_sectors, encrypt;
+ int c, ret, n, n1, compress, cluster_size, cluster_sectors, create_flags;
const char *filename, *fmt, *out_fmt, *out_filename;
BlockDriver *drv;
BlockDriverState *bs, *out_bs;
@@ -486,9 +494,9 @@
fmt = NULL;
out_fmt = "raw";
compress = 0;
- encrypt = 0;
+ create_flags = 0;
for(;;) {
- c = getopt(argc, argv, "f:O:hce");
+ c = getopt(argc, argv, "f:O:hces");
if (c == -1)
break;
switch(c) {
@@ -505,7 +513,10 @@
compress = 1;
break;
case 'e':
- encrypt = 1;
+ create_flags |= BDRV_ENCRYPTED;
+ break;
+ case 's':
+ create_flags |= BDRV_FORCE_SPARSE;
break;
}
}
@@ -523,12 +534,12 @@
error("Unknown file format '%s'", fmt);
if (compress && drv != &bdrv_qcow)
error("Compression not supported for this file format");
- if (encrypt && drv != &bdrv_qcow)
+ if ((create_flags & BDRV_ENCRYPTED) && drv != &bdrv_qcow)
error("Encryption not supported for this file format");
- if (compress && encrypt)
+ if (compress && (create_flags & BDRV_ENCRYPTED))
error("Compression and encryption not supported at the same time");
bdrv_get_geometry(bs, &total_sectors);
- ret = bdrv_create(drv, out_filename, total_sectors, NULL, encrypt);
+ ret = bdrv_create(drv, out_filename, total_sectors, NULL, create_flags);
if (ret < 0) {
if (ret == -ENOTSUP) {
error("Formatting not supported for file format '%s'", fmt);
diff -rub qemu-0.8.1.orig/vl.h qemu-0.8.1/vl.h
--- qemu-0.8.1.orig/vl.h Wed May 3 13:32:58 2006
+++ qemu-0.8.1/vl.h Wed May 31 15:39:59 2006
@@ -49,8 +49,8 @@
#ifdef _WIN32
#define lseek _lseeki64
#define ENOTSUP 4096
-/* XXX: find 64 bit version */
-#define ftruncate chsize
+extern int qemu_ftruncate64(int, int64_t);
+#define ftruncate qemu_ftruncate64
static inline char *realpath(const char *path, char *resolved_path)
{
@@ -503,6 +509,10 @@
#define BIOS_ATA_TRANSLATION_AUTO 0
#define BIOS_ATA_TRANSLATION_NONE 1
#define BIOS_ATA_TRANSLATION_LBA 2
+
+/* creation flags */
+#define BDRV_ENCRYPTED 1
+#define BDRV_FORCE_SPARSE 2
void bdrv_set_geometry_hint(BlockDriverState *bs,
int cyls, int heads, int secs);
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] Sparse file support under windows
2006-06-05 8:32 ZIGLIO, Frediano, VF-IT
@ 2006-06-14 14:00 ` Fabrice Bellard
0 siblings, 0 replies; 5+ messages in thread
From: Fabrice Bellard @ 2006-06-14 14:00 UTC (permalink / raw)
To: qemu-devel; +Cc: Frediano.Ziglio
OK for the idea, but why is a new option needed ? Sparse file usage
should be automatic if the filesystem supports it.
Regards,
Fabrice.
ZIGLIO, Frediano, VF-IT wrote:
> Hi,
> this patch add sparse file support under Windows (supported on NTFS).
> It add a -s flag to qemu create and qemu convert. It contain also a new
> implementation for ftruncate witch not fill manually space but only use
> SetEndOfFile (so to handle correctly sparse file)
>
> freddy77
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Qemu-devel mailing list
> Qemu-devel@nongnu.org
> http://lists.nongnu.org/mailman/listinfo/qemu-devel
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: [Qemu-devel] Sparse file support under windows
@ 2006-06-14 14:21 ZIGLIO, Frediano, VF-IT
2006-06-14 14:50 ` Fabrice Bellard
0 siblings, 1 reply; 5+ messages in thread
From: ZIGLIO, Frediano, VF-IT @ 2006-06-14 14:21 UTC (permalink / raw)
To: qemu-devel
Good question. As you like... I can remove option if you prefer. I don't
know how performs sparse file on windows so perhaps someone can still
prefer normal file for performance.
freddy77
>
> OK for the idea, but why is a new option needed ? Sparse file usage
> should be automatic if the filesystem supports it.
>
> Regards,
>
> Fabrice.
>
> ZIGLIO, Frediano, VF-IT wrote:
> > Hi,
> > this patch add sparse file support under Windows
> (supported on NTFS).
> > It add a -s flag to qemu create and qemu convert. It
> contain also a new
> > implementation for ftruncate witch not fill manually space
> but only use
> > SetEndOfFile (so to handle correctly sparse file)
> >
> > freddy77
> >
> >
> >
> >
> --------------------------------------------------------------
> ----------
> >
> > _______________________________________________
> > Qemu-devel mailing list
> > Qemu-devel@nongnu.org
> > http://lists.nongnu.org/mailman/listinfo/qemu-devel
>
>
>
> _______________________________________________
> Qemu-devel mailing list
> Qemu-devel@nongnu.org
> http://lists.nongnu.org/mailman/listinfo/qemu-devel
>
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] Sparse file support under windows
2006-06-14 14:21 [Qemu-devel] Sparse file support under windows ZIGLIO, Frediano, VF-IT
@ 2006-06-14 14:50 ` Fabrice Bellard
0 siblings, 0 replies; 5+ messages in thread
From: Fabrice Bellard @ 2006-06-14 14:50 UTC (permalink / raw)
To: qemu-devel
At least the default should be sparse files if the filesystem supports them.
Fabrice.
ZIGLIO, Frediano, VF-IT wrote:
> Good question. As you like... I can remove option if you prefer. I don't
> know how performs sparse file on windows so perhaps someone can still
> prefer normal file for performance.
>
> freddy77
>
>
>
>>OK for the idea, but why is a new option needed ? Sparse file usage
>>should be automatic if the filesystem supports it.
>>
>>Regards,
>>
>>Fabrice.
>>
>>ZIGLIO, Frediano, VF-IT wrote:
>>
>>>Hi,
>>> this patch add sparse file support under Windows
>>
>>(supported on NTFS).
>>
>>>It add a -s flag to qemu create and qemu convert. It
>>
>>contain also a new
>>
>>>implementation for ftruncate witch not fill manually space
>>
>>but only use
>>
>>>SetEndOfFile (so to handle correctly sparse file)
>>>
>>>freddy77
>>>
>>>
>>>
>>>
>>
>>--------------------------------------------------------------
>>----------
>>
>>>_______________________________________________
>>>Qemu-devel mailing list
>>>Qemu-devel@nongnu.org
>>>http://lists.nongnu.org/mailman/listinfo/qemu-devel
>>
>>
>>
>>_______________________________________________
>>Qemu-devel mailing list
>>Qemu-devel@nongnu.org
>>http://lists.nongnu.org/mailman/listinfo/qemu-devel
>>
>>
>
>
>
> _______________________________________________
> Qemu-devel mailing list
> Qemu-devel@nongnu.org
> http://lists.nongnu.org/mailman/listinfo/qemu-devel
>
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: [Qemu-devel] Sparse file support under windows
@ 2006-06-15 8:25 ZIGLIO, Frediano, VF-IT
0 siblings, 0 replies; 5+ messages in thread
From: ZIGLIO, Frediano, VF-IT @ 2006-06-15 8:25 UTC (permalink / raw)
To: qemu-devel
[-- Attachment #1: Type: text/plain, Size: 1956 bytes --]
Attached updated patch (this time against CVS HEAD)
bye
Frediano Ziglio
PS: Thanks for apply my patches
>
> At least the default should be sparse files if the filesystem
> supports them.
>
> Fabrice.
>
> ZIGLIO, Frediano, VF-IT wrote:
> > Good question. As you like... I can remove option if you
> prefer. I don't
> > know how performs sparse file on windows so perhaps someone
> can still
> > prefer normal file for performance.
> >
> > freddy77
> >
> >
> >
> >>OK for the idea, but why is a new option needed ? Sparse file usage
> >>should be automatic if the filesystem supports it.
> >>
> >>Regards,
> >>
> >>Fabrice.
> >>
> >>ZIGLIO, Frediano, VF-IT wrote:
> >>
> >>>Hi,
> >>> this patch add sparse file support under Windows
> >>
> >>(supported on NTFS).
> >>
> >>>It add a -s flag to qemu create and qemu convert. It
> >>
> >>contain also a new
> >>
> >>>implementation for ftruncate witch not fill manually space
> >>
> >>but only use
> >>
> >>>SetEndOfFile (so to handle correctly sparse file)
> >>>
> >>>freddy77
> >>>
> >>>
> >>>
> >>>
> >>
> >>--------------------------------------------------------------
> >>----------
> >>
> >>>_______________________________________________
> >>>Qemu-devel mailing list
> >>>Qemu-devel@nongnu.org
> >>>http://lists.nongnu.org/mailman/listinfo/qemu-devel
> >>
> >>
> >>
> >>_______________________________________________
> >>Qemu-devel mailing list
> >>Qemu-devel@nongnu.org
> >>http://lists.nongnu.org/mailman/listinfo/qemu-devel
> >>
> >>
> >
> >
> >
> > _______________________________________________
> > Qemu-devel mailing list
> > Qemu-devel@nongnu.org
> > http://lists.nongnu.org/mailman/listinfo/qemu-devel
> >
> >
>
>
>
> _______________________________________________
> Qemu-devel mailing list
> Qemu-devel@nongnu.org
> http://lists.nongnu.org/mailman/listinfo/qemu-devel
>
>
[-- Attachment #2: sparse.diff --]
[-- Type: application/octet-stream, Size: 2749 bytes --]
Index: block.c
===================================================================
RCS file: /sources/qemu/qemu/block.c,v
retrieving revision 1.27
diff -u -1 -0 -r1.27 block.c
--- block.c 4 Jun 2006 11:39:07 -0000 1.27
+++ block.c 15 Jun 2006 08:21:32 -0000
@@ -754,32 +754,75 @@
return -1;
return 0;
}
static void raw_close(BlockDriverState *bs)
{
BDRVRawState *s = bs->opaque;
close(s->fd);
}
+#ifdef _WIN32
+#include <windows.h>
+#include <winioctl.h>
+
+int qemu_ftruncate64(int fd, int64_t length)
+{
+ LARGE_INTEGER li;
+ LONG high;
+ HANDLE h;
+ BOOL res;
+
+ if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
+ return -1;
+
+ h = (HANDLE)_get_osfhandle(fd);
+
+ /* get current position, ftruncate do not change position */
+ li.HighPart = 0;
+ li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
+ if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
+ return -1;
+
+ high = length >> 32;
+ if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
+ return -1;
+ res = SetEndOfFile(h);
+
+ /* back to old position */
+ SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
+ return res ? 0 : -1;
+}
+
+static int set_sparse(int fd)
+{
+ DWORD returned;
+ return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
+ NULL, 0, NULL, 0, &returned, NULL);
+}
+#else
+#define set_sparse(fd) 1
+#endif
+
static int raw_create(const char *filename, int64_t total_size,
const char *backing_file, int flags)
{
int fd;
if (flags || backing_file)
return -ENOTSUP;
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
0644);
if (fd < 0)
return -EIO;
+ set_sparse(fd);
ftruncate(fd, total_size * 512);
close(fd);
return 0;
}
static void raw_flush(BlockDriverState *bs)
{
BDRVRawState *s = bs->opaque;
fsync(s->fd);
}
Index: vl.h
===================================================================
RCS file: /sources/qemu/qemu/vl.h,v
retrieving revision 1.128
diff -u -1 -0 -r1.128 vl.h
--- vl.h 14 Jun 2006 16:03:05 -0000 1.128
+++ vl.h 15 Jun 2006 08:21:39 -0000
@@ -43,22 +43,23 @@
#define O_LARGEFILE 0
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
#ifdef _WIN32
#define fsync _commit
#define lseek _lseeki64
#define ENOTSUP 4096
-/* XXX: find 64 bit version */
-#define ftruncate chsize
+extern int qemu_ftruncate64(int, int64_t);
+#define ftruncate qemu_ftruncate64
+
static inline char *realpath(const char *path, char *resolved_path)
{
_fullpath(resolved_path, path, _MAX_PATH);
return resolved_path;
}
#define PRId64 "I64d"
#endif
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2006-06-15 8:26 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-06-14 14:21 [Qemu-devel] Sparse file support under windows ZIGLIO, Frediano, VF-IT
2006-06-14 14:50 ` Fabrice Bellard
-- strict thread matches above, loose matches on Subject: below --
2006-06-15 8:25 ZIGLIO, Frediano, VF-IT
2006-06-05 8:32 ZIGLIO, Frediano, VF-IT
2006-06-14 14:00 ` Fabrice Bellard
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).