VirtualBox

source: vbox/trunk/src/VBox/Storage/QCOW.cpp@ 85724

Last change on this file since 85724 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 99.6 KB
Line 
1/* $Id: QCOW.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * QCOW - QCOW Disk image.
4 */
5
6/*
7 * Copyright (C) 2011-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_VD_QCOW
23#include <VBox/vd-plugin.h>
24#include <VBox/err.h>
25
26#include <VBox/log.h>
27#include <iprt/asm.h>
28#include <iprt/assert.h>
29#include <iprt/string.h>
30#include <iprt/alloc.h>
31#include <iprt/path.h>
32#include <iprt/list.h>
33#include <iprt/zip.h>
34
35#include "VDBackends.h"
36#include "VDBackendsInline.h"
37
38/** @page pg_storage_qcow QCOW Storage Backend
39 * The QCOW backend implements support for the qemu copy on write format (short QCOW).
40 *
41 * The official specification for qcow is available at
42 * https://github.com/qemu/qemu/blob/master/docs/interop/qcow2.txt version 2 and 3.
43 * For version 1 there is no official specification available but the format is described
44 * at http://people.gnome.org/~markmc/qcow-image-format-version-1.html.
45 *
46 * Missing things to implement:
47 * - v2 image creation and handling of the reference count table. (Blocker to enable support for V2 images)
48 * - cluster encryption
49 * - cluster compression
50 * - compaction
51 * - resizing
52 */
53
54
55/*********************************************************************************************************************************
56* Structures in a QCOW image, big endian *
57*********************************************************************************************************************************/
58
59#pragma pack(1) /* Completely unnecessary. */
60typedef struct QCowHeader
61{
62 /** Magic value. */
63 uint32_t u32Magic;
64 /** Version of the image. */
65 uint32_t u32Version;
66 /** Version dependent data. */
67 union
68 {
69 /** Version 1. */
70 struct
71 {
72 /** Backing file offset. */
73 uint64_t u64BackingFileOffset;
74 /** Size of the backing file. */
75 uint32_t u32BackingFileSize;
76 /** mtime (Modification time?) - can be ignored. */
77 uint32_t u32MTime;
78 /** Logical size of the image in bytes. */
79 uint64_t u64Size;
80 /** Number of bits in the virtual offset used as a cluster offset. */
81 uint8_t u8ClusterBits;
82 /** Number of bits in the virtual offset used for the L2 index. */
83 uint8_t u8L2Bits;
84 /** Padding because the header is not packed in the original source. */
85 uint16_t u16Padding;
86 /** Used cryptographic method. */
87 uint32_t u32CryptMethod;
88 /** Offset of the L1 table in the image in bytes. */
89 uint64_t u64L1TableOffset;
90 } v1;
91 /** Version 2 (and also containing extensions for version 3). */
92 struct
93 {
94 /** Backing file offset. */
95 uint64_t u64BackingFileOffset;
96 /** Size of the backing file. */
97 uint32_t u32BackingFileSize;
98 /** Number of bits in the virtual offset used as a cluster offset. */
99 uint32_t u32ClusterBits;
100 /** Logical size of the image. */
101 uint64_t u64Size;
102 /** Used cryptographic method. */
103 uint32_t u32CryptMethod;
104 /** Size of the L1 table in entries (each 8bytes big). */
105 uint32_t u32L1Size;
106 /** Offset of the L1 table in the image in bytes. */
107 uint64_t u64L1TableOffset;
108 /** Start of the refcount table in the image. */
109 uint64_t u64RefcountTableOffset;
110 /** Size of the refcount table in clusters. */
111 uint32_t u32RefcountTableClusters;
112 /** Number of snapshots in the image. */
113 uint32_t u32NbSnapshots;
114 /** Offset of the first snapshot header in the image. */
115 uint64_t u64SnapshotsOffset;
116 /** Version 3 additional data. */
117 struct
118 {
119 /** Incompatible features. */
120 uint64_t u64IncompatFeat;
121 /** Compatible features. */
122 uint64_t u64CompatFeat;
123 /** Autoclear features. */
124 uint64_t u64AutoClrFeat;
125 /** Width in bits of a reference count block. */
126 uint32_t u32RefCntWidth;
127 /** Lenght of the header structure in bytes (for the header extensions). */
128 uint32_t u32HdrLenBytes;
129 } v3;
130 } v2;
131 } Version;
132} QCowHeader;
133#pragma pack()
134/** Pointer to a on disk QCOW header. */
135typedef QCowHeader *PQCowHeader;
136
137/** QCOW magic value. */
138#define QCOW_MAGIC UINT32_C(0x514649fb) /* QFI\0xfb */
139/** Size of the V1 header. */
140#define QCOW_V1_HDR_SIZE (48)
141/** Size of the V2 header. */
142#define QCOW_V2_HDR_SIZE (72)
143
144/** Cluster is compressed flag for QCOW images. */
145#define QCOW_V1_COMPRESSED_FLAG RT_BIT_64(63)
146
147/** Copied flag for QCOW2 images. */
148#define QCOW_V2_COPIED_FLAG RT_BIT_64(63)
149/** Cluster is compressed flag for QCOW2 images. */
150#define QCOW_V2_COMPRESSED_FLAG RT_BIT_64(62)
151/** The mask for extracting the offset from either the L1 or L2 table. */
152#define QCOW_V2_TBL_OFFSET_MASK UINT64_C(0x00fffffffffffe00)
153
154/** Incompatible feature: Dirty bit, reference count may be inconsistent. */
155#define QCOW_V3_INCOMPAT_FEAT_F_DIRTY RT_BIT_64(0)
156/** Incompatible feature: Image is corrupt and needs repair. */
157#define QCOW_V3_INCOMPAT_FEAT_F_CORRUPT RT_BIT_64(1)
158/** Incompatible feature: External data file. */
159#define QCOW_V3_INCOMPAT_FEAT_F_EXTERNAL_DATA RT_BIT_64(2)
160/** The incompatible features we support currently. */
161#define QCOW_V3_INCOMPAT_FEAT_SUPPORTED_MASK UINT64_C(0x0)
162
163/** Compatible feature: Lazy reference counters. */
164#define QCOW_V3_COMPAT_FEAT_F_LAZY_REF_COUNT RT_BIT_64(0)
165/** The compatible features we support currently. */
166#define QCOW_V3_COMPAT_FEAT_SUPPORTED_MASK UINT64_C(0x0)
167
168/** Auto clear feature: Bitmaps extension. */
169#define QCOW_V3_AUTOCLR_FEAT_F_BITMAPS RT_BIT_64(0)
170/** Auto clear feature: The external data file is raw image which can be accessed standalone. */
171#define QCOW_V3_AUTOCLR_FEAT_F_EXT_RAW_DATA RT_BIT_64(1)
172/** The autoclear features we support currently. */
173#define QCOW_V3_AUTOCLR_FEAT_SUPPORTED_MASK UINT64_C(0x0)
174
175
176/*********************************************************************************************************************************
177* Constants And Macros, Structures and Typedefs *
178*********************************************************************************************************************************/
179
180/**
181 * QCOW L2 cache entry.
182 */
183typedef struct QCOWL2CACHEENTRY
184{
185 /** List node for the search list. */
186 RTLISTNODE NodeSearch;
187 /** List node for the LRU list. */
188 RTLISTNODE NodeLru;
189 /** Reference counter. */
190 uint32_t cRefs;
191 /** The offset of the L2 table, used as search key. */
192 uint64_t offL2Tbl;
193 /** Pointer to the cached L2 table. */
194 uint64_t *paL2Tbl;
195} QCOWL2CACHEENTRY, *PQCOWL2CACHEENTRY;
196
197/** Maximum amount of memory the cache is allowed to use. */
198#define QCOW_L2_CACHE_MEMORY_MAX (2*_1M)
199
200/** QCOW default cluster size for image version 2. */
201#define QCOW2_CLUSTER_SIZE_DEFAULT (64*_1K)
202/** QCOW default cluster size for image version 1. */
203#define QCOW_CLUSTER_SIZE_DEFAULT (4*_1K)
204/** QCOW default L2 table size in clusters. */
205#define QCOW_L2_CLUSTERS_DEFAULT (1)
206
207/**
208 * QCOW image data structure.
209 */
210typedef struct QCOWIMAGE
211{
212 /** Image name. */
213 const char *pszFilename;
214 /** Storage handle. */
215 PVDIOSTORAGE pStorage;
216
217 /** Pointer to the per-disk VD interface list. */
218 PVDINTERFACE pVDIfsDisk;
219 /** Pointer to the per-image VD interface list. */
220 PVDINTERFACE pVDIfsImage;
221 /** Error interface. */
222 PVDINTERFACEERROR pIfError;
223 /** I/O interface. */
224 PVDINTERFACEIOINT pIfIo;
225
226 /** Open flags passed by VBoxHD layer. */
227 unsigned uOpenFlags;
228 /** Image flags defined during creation or determined during open. */
229 unsigned uImageFlags;
230 /** Total size of the image. */
231 uint64_t cbSize;
232 /** Physical geometry of this image. */
233 VDGEOMETRY PCHSGeometry;
234 /** Logical geometry of this image. */
235 VDGEOMETRY LCHSGeometry;
236
237 /** Image version. */
238 unsigned uVersion;
239 /** MTime field - used only to preserve value in opened images, unmodified otherwise. */
240 uint32_t MTime;
241
242 /** Filename of the backing file if any. */
243 char *pszBackingFilename;
244 /** Offset of the filename in the image. */
245 uint64_t offBackingFilename;
246 /** Size of the backing filename excluding \0. */
247 uint32_t cbBackingFilename;
248
249 /** Next offset of a new cluster, aligned to sector size. */
250 uint64_t offNextCluster;
251 /** Cluster size in bytes. */
252 uint32_t cbCluster;
253 /** Number of bits in the virtual offset used as the cluster offset. */
254 uint32_t cClusterBits;
255 /** Bitmask to extract the offset from a compressed cluster descriptor. */
256 uint64_t fMaskCompressedClusterOffset;
257 /** Bitmask to extract the sector count from a compressed cluster descriptor. */
258 uint64_t fMaskCompressedClusterSectors;
259 /** Number of bits to shift the sector count to the right to get the final value. */
260 uint32_t cBitsShiftRCompressedClusterSectors;
261 /** Number of entries in the L1 table. */
262 uint32_t cL1TableEntries;
263 /** Size of an L1 rounded to the next cluster size. */
264 uint32_t cbL1Table;
265 /** Pointer to the L1 table. */
266 uint64_t *paL1Table;
267 /** Offset of the L1 table. */
268 uint64_t offL1Table;
269
270 /** Size of the L2 table in bytes. */
271 uint32_t cbL2Table;
272 /** Number of entries in the L2 table. */
273 uint32_t cL2TableEntries;
274 /** Memory occupied by the L2 table cache. */
275 size_t cbL2Cache;
276 /** The sorted L2 entry list used for searching. */
277 RTLISTNODE ListSearch;
278 /** The LRU L2 entry list used for eviction. */
279 RTLISTNODE ListLru;
280
281 /** Offset of the refcount table. */
282 uint64_t offRefcountTable;
283 /** Size of the refcount table in bytes. */
284 uint32_t cbRefcountTable;
285 /** Number of entries in the refcount table. */
286 uint32_t cRefcountTableEntries;
287 /** Pointer to the refcount table. */
288 uint64_t *paRefcountTable;
289
290 /** Offset mask for a cluster. */
291 uint64_t fOffsetMask;
292 /** Number of bits to shift to get the L1 index. */
293 uint32_t cL1Shift;
294 /** L2 table mask to get the L2 index. */
295 uint64_t fL2Mask;
296 /** Number of bits to shift to get the L2 index. */
297 uint32_t cL2Shift;
298
299 /** Size of compressed cluster buffer. */
300 size_t cbCompCluster;
301 /** Compressed cluster buffer. */
302 void *pvCompCluster;
303 /** Buffer to hold the uncompressed data. */
304 void *pvCluster;
305
306 /** Pointer to the L2 table we are currently allocating
307 * (can be only one at a time). */
308 PQCOWL2CACHEENTRY pL2TblAlloc;
309 /** The static region list. */
310 VDREGIONLIST RegionList;
311} QCOWIMAGE, *PQCOWIMAGE;
312
313/**
314 * State of the async cluster allocation.
315 */
316typedef enum QCOWCLUSTERASYNCALLOCSTATE
317{
318 /** Invalid. */
319 QCOWCLUSTERASYNCALLOCSTATE_INVALID = 0,
320 /** L2 table allocation. */
321 QCOWCLUSTERASYNCALLOCSTATE_L2_ALLOC,
322 /** Link L2 table into L1. */
323 QCOWCLUSTERASYNCALLOCSTATE_L2_LINK,
324 /** Allocate user data cluster. */
325 QCOWCLUSTERASYNCALLOCSTATE_USER_ALLOC,
326 /** Link user data cluster. */
327 QCOWCLUSTERASYNCALLOCSTATE_USER_LINK,
328 /** 32bit blowup. */
329 QCOWCLUSTERASYNCALLOCSTATE_32BIT_HACK = 0x7fffffff
330} QCOWCLUSTERASYNCALLOCSTATE, *PQCOWCLUSTERASYNCALLOCSTATE;
331
332/**
333 * Data needed to track async cluster allocation.
334 */
335typedef struct QCOWCLUSTERASYNCALLOC
336{
337 /** The state of the cluster allocation. */
338 QCOWCLUSTERASYNCALLOCSTATE enmAllocState;
339 /** Old image size to rollback in case of an error. */
340 uint64_t offNextClusterOld;
341 /** L1 index to link if any. */
342 uint32_t idxL1;
343 /** L2 index to link, required in any case. */
344 uint32_t idxL2;
345 /** Start offset of the allocated cluster. */
346 uint64_t offClusterNew;
347 /** L2 cache entry if a L2 table is allocated. */
348 PQCOWL2CACHEENTRY pL2Entry;
349 /** Number of bytes to write. */
350 size_t cbToWrite;
351} QCOWCLUSTERASYNCALLOC, *PQCOWCLUSTERASYNCALLOC;
352
353
354/*********************************************************************************************************************************
355* Static Variables *
356*********************************************************************************************************************************/
357
358/** NULL-terminated array of supported file extensions. */
359static const VDFILEEXTENSION s_aQCowFileExtensions[] =
360{
361 {"qcow", VDTYPE_HDD},
362 {"qcow2", VDTYPE_HDD},
363 {NULL, VDTYPE_INVALID}
364};
365
366
367/*********************************************************************************************************************************
368* Internal Functions *
369*********************************************************************************************************************************/
370
371/**
372 * Return power of 2 or 0 if num error.
373 *
374 * @returns The power of 2 or 0 if the given number is not a power of 2.
375 * @param u32 The number.
376 */
377static uint32_t qcowGetPowerOfTwo(uint32_t u32)
378{
379 if (u32 == 0)
380 return 0;
381 uint32_t uPower2 = 0;
382 while ((u32 & 1) == 0)
383 {
384 u32 >>= 1;
385 uPower2++;
386 }
387 return u32 == 1 ? uPower2 : 0;
388}
389
390
391/**
392 * Converts the image header to the host endianess and performs basic checks.
393 *
394 * @returns Whether the given header is valid or not.
395 * @param pHeader Pointer to the header to convert.
396 */
397static bool qcowHdrConvertToHostEndianess(PQCowHeader pHeader)
398{
399 pHeader->u32Magic = RT_BE2H_U32(pHeader->u32Magic);
400 pHeader->u32Version = RT_BE2H_U32(pHeader->u32Version);
401
402 if (pHeader->u32Magic != QCOW_MAGIC)
403 return false;
404
405 if (pHeader->u32Version == 1)
406 {
407 pHeader->Version.v1.u64BackingFileOffset = RT_BE2H_U64(pHeader->Version.v1.u64BackingFileOffset);
408 pHeader->Version.v1.u32BackingFileSize = RT_BE2H_U32(pHeader->Version.v1.u32BackingFileSize);
409 pHeader->Version.v1.u32MTime = RT_BE2H_U32(pHeader->Version.v1.u32MTime);
410 pHeader->Version.v1.u64Size = RT_BE2H_U64(pHeader->Version.v1.u64Size);
411 pHeader->Version.v1.u32CryptMethod = RT_BE2H_U32(pHeader->Version.v1.u32CryptMethod);
412 pHeader->Version.v1.u64L1TableOffset = RT_BE2H_U64(pHeader->Version.v1.u64L1TableOffset);
413 }
414 else if (pHeader->u32Version == 2 || pHeader->u32Version == 3)
415 {
416 pHeader->Version.v2.u64BackingFileOffset = RT_BE2H_U64(pHeader->Version.v2.u64BackingFileOffset);
417 pHeader->Version.v2.u32BackingFileSize = RT_BE2H_U32(pHeader->Version.v2.u32BackingFileSize);
418 pHeader->Version.v2.u32ClusterBits = RT_BE2H_U32(pHeader->Version.v2.u32ClusterBits);
419 pHeader->Version.v2.u64Size = RT_BE2H_U64(pHeader->Version.v2.u64Size);
420 pHeader->Version.v2.u32CryptMethod = RT_BE2H_U32(pHeader->Version.v2.u32CryptMethod);
421 pHeader->Version.v2.u32L1Size = RT_BE2H_U32(pHeader->Version.v2.u32L1Size);
422 pHeader->Version.v2.u64L1TableOffset = RT_BE2H_U64(pHeader->Version.v2.u64L1TableOffset);
423 pHeader->Version.v2.u64RefcountTableOffset = RT_BE2H_U64(pHeader->Version.v2.u64RefcountTableOffset);
424 pHeader->Version.v2.u32RefcountTableClusters = RT_BE2H_U32(pHeader->Version.v2.u32RefcountTableClusters);
425 pHeader->Version.v2.u32NbSnapshots = RT_BE2H_U32(pHeader->Version.v2.u32NbSnapshots);
426 pHeader->Version.v2.u64SnapshotsOffset = RT_BE2H_U64(pHeader->Version.v2.u64SnapshotsOffset);
427
428 if (pHeader->u32Version == 3)
429 {
430 pHeader->Version.v2.v3.u64IncompatFeat = RT_BE2H_U64(pHeader->Version.v2.v3.u64IncompatFeat);
431 pHeader->Version.v2.v3.u64CompatFeat = RT_BE2H_U64(pHeader->Version.v2.v3.u64CompatFeat);
432 pHeader->Version.v2.v3.u64AutoClrFeat = RT_BE2H_U64(pHeader->Version.v2.v3.u64AutoClrFeat);
433 pHeader->Version.v2.v3.u32RefCntWidth = RT_BE2H_U32(pHeader->Version.v2.v3.u32RefCntWidth);
434 pHeader->Version.v2.v3.u32HdrLenBytes = RT_BE2H_U32(pHeader->Version.v2.v3.u32HdrLenBytes);
435 }
436 }
437 else
438 return false;
439
440 return true;
441}
442
443/**
444 * Creates a QCOW header from the given image state.
445 *
446 * @returns nothing.
447 * @param pImage Image instance data.
448 * @param pHeader Pointer to the header to convert.
449 * @param pcbHeader Where to store the size of the header to write.
450 */
451static void qcowHdrConvertFromHostEndianess(PQCOWIMAGE pImage, PQCowHeader pHeader,
452 size_t *pcbHeader)
453{
454 memset(pHeader, 0, sizeof(QCowHeader));
455
456 pHeader->u32Magic = RT_H2BE_U32(QCOW_MAGIC);
457 pHeader->u32Version = RT_H2BE_U32(pImage->uVersion);
458 if (pImage->uVersion == 1)
459 {
460 pHeader->Version.v1.u64BackingFileOffset = RT_H2BE_U64(pImage->offBackingFilename);
461 pHeader->Version.v1.u32BackingFileSize = RT_H2BE_U32(pImage->cbBackingFilename);
462 pHeader->Version.v1.u32MTime = RT_H2BE_U32(pImage->MTime);
463 pHeader->Version.v1.u64Size = RT_H2BE_U64(pImage->cbSize);
464 pHeader->Version.v1.u8ClusterBits = (uint8_t)qcowGetPowerOfTwo(pImage->cbCluster);
465 pHeader->Version.v1.u8L2Bits = (uint8_t)qcowGetPowerOfTwo(pImage->cL2TableEntries);
466 pHeader->Version.v1.u32CryptMethod = RT_H2BE_U32(0);
467 pHeader->Version.v1.u64L1TableOffset = RT_H2BE_U64(pImage->offL1Table);
468 *pcbHeader = QCOW_V1_HDR_SIZE;
469 }
470 else if (pImage->uVersion == 2)
471 {
472 pHeader->Version.v2.u64BackingFileOffset = RT_H2BE_U64(pImage->offBackingFilename);
473 pHeader->Version.v2.u32BackingFileSize = RT_H2BE_U32(pImage->cbBackingFilename);
474 pHeader->Version.v2.u32ClusterBits = RT_H2BE_U32(qcowGetPowerOfTwo(pImage->cbCluster));
475 pHeader->Version.v2.u64Size = RT_H2BE_U64(pImage->cbSize);
476 pHeader->Version.v2.u32CryptMethod = RT_H2BE_U32(0);
477 pHeader->Version.v2.u32L1Size = RT_H2BE_U32(pImage->cL1TableEntries);
478 pHeader->Version.v2.u64L1TableOffset = RT_H2BE_U64(pImage->offL1Table);
479 pHeader->Version.v2.u64RefcountTableOffset = RT_H2BE_U64(pImage->offRefcountTable);
480 pHeader->Version.v2.u32RefcountTableClusters = RT_H2BE_U32(pImage->cbRefcountTable / pImage->cbCluster);
481 pHeader->Version.v2.u32NbSnapshots = RT_H2BE_U32(0);
482 pHeader->Version.v2.u64SnapshotsOffset = RT_H2BE_U64((uint64_t)0);
483 *pcbHeader = QCOW_V2_HDR_SIZE;
484 }
485 else
486 AssertMsgFailed(("Invalid version of the QCOW image format %d\n", pImage->uVersion));
487}
488
489/**
490 * Convert table entries from little endian to host endianess.
491 *
492 * @returns nothing.
493 * @param paTbl Pointer to the table.
494 * @param cEntries Number of entries in the table.
495 */
496static void qcowTableConvertToHostEndianess(uint64_t *paTbl, uint32_t cEntries)
497{
498 while(cEntries-- > 0)
499 {
500 *paTbl = RT_BE2H_U64(*paTbl);
501 paTbl++;
502 }
503}
504
505/**
506 * Convert table entries from host to little endian format.
507 *
508 * @returns nothing.
509 * @param paTblImg Pointer to the table which will store the little endian table.
510 * @param paTbl The source table to convert.
511 * @param cEntries Number of entries in the table.
512 */
513static void qcowTableConvertFromHostEndianess(uint64_t *paTblImg, uint64_t *paTbl,
514 uint32_t cEntries)
515{
516 while(cEntries-- > 0)
517 {
518 *paTblImg = RT_H2BE_U64(*paTbl);
519 paTbl++;
520 paTblImg++;
521 }
522}
523
524/**
525 * Creates the L2 table cache.
526 *
527 * @returns VBox status code.
528 * @param pImage The image instance data.
529 */
530static int qcowL2TblCacheCreate(PQCOWIMAGE pImage)
531{
532 pImage->cbL2Cache = 0;
533 RTListInit(&pImage->ListSearch);
534 RTListInit(&pImage->ListLru);
535
536 return VINF_SUCCESS;
537}
538
539/**
540 * Destroys the L2 table cache.
541 *
542 * @returns nothing.
543 * @param pImage The image instance data.
544 */
545static void qcowL2TblCacheDestroy(PQCOWIMAGE pImage)
546{
547 PQCOWL2CACHEENTRY pL2Entry;
548 PQCOWL2CACHEENTRY pL2Next;
549 RTListForEachSafe(&pImage->ListSearch, pL2Entry, pL2Next, QCOWL2CACHEENTRY, NodeSearch)
550 {
551 Assert(!pL2Entry->cRefs);
552
553 RTListNodeRemove(&pL2Entry->NodeSearch);
554 RTMemPageFree(pL2Entry->paL2Tbl, pImage->cbL2Table);
555 RTMemFree(pL2Entry);
556 }
557
558 pImage->cbL2Cache = 0;
559 RTListInit(&pImage->ListSearch);
560 RTListInit(&pImage->ListLru);
561}
562
563/**
564 * Returns the L2 table matching the given offset or NULL if none could be found.
565 *
566 * @returns Pointer to the L2 table cache entry or NULL.
567 * @param pImage The image instance data.
568 * @param offL2Tbl Offset of the L2 table to search for.
569 */
570static PQCOWL2CACHEENTRY qcowL2TblCacheRetain(PQCOWIMAGE pImage, uint64_t offL2Tbl)
571{
572 if ( pImage->pL2TblAlloc
573 && pImage->pL2TblAlloc->offL2Tbl == offL2Tbl)
574 {
575 pImage->pL2TblAlloc->cRefs++;
576 return pImage->pL2TblAlloc;
577 }
578
579 PQCOWL2CACHEENTRY pL2Entry;
580 RTListForEach(&pImage->ListSearch, pL2Entry, QCOWL2CACHEENTRY, NodeSearch)
581 {
582 if (pL2Entry->offL2Tbl == offL2Tbl)
583 break;
584 }
585
586 if (!RTListNodeIsDummy(&pImage->ListSearch, pL2Entry, QCOWL2CACHEENTRY, NodeSearch))
587 {
588 /* Update LRU list. */
589 RTListNodeRemove(&pL2Entry->NodeLru);
590 RTListPrepend(&pImage->ListLru, &pL2Entry->NodeLru);
591 pL2Entry->cRefs++;
592 return pL2Entry;
593 }
594
595 return NULL;
596}
597
598/**
599 * Releases a L2 table cache entry.
600 *
601 * @returns nothing.
602 * @param pL2Entry The L2 cache entry.
603 */
604static void qcowL2TblCacheEntryRelease(PQCOWL2CACHEENTRY pL2Entry)
605{
606 Assert(pL2Entry->cRefs > 0);
607 pL2Entry->cRefs--;
608}
609
610/**
611 * Allocates a new L2 table from the cache evicting old entries if required.
612 *
613 * @returns Pointer to the L2 cache entry or NULL.
614 * @param pImage The image instance data.
615 */
616static PQCOWL2CACHEENTRY qcowL2TblCacheEntryAlloc(PQCOWIMAGE pImage)
617{
618 PQCOWL2CACHEENTRY pL2Entry = NULL;
619
620 if (pImage->cbL2Cache + pImage->cbL2Table <= QCOW_L2_CACHE_MEMORY_MAX)
621 {
622 /* Add a new entry. */
623 pL2Entry = (PQCOWL2CACHEENTRY)RTMemAllocZ(sizeof(QCOWL2CACHEENTRY));
624 if (pL2Entry)
625 {
626 pL2Entry->paL2Tbl = (uint64_t *)RTMemPageAllocZ(pImage->cbL2Table);
627 if (RT_UNLIKELY(!pL2Entry->paL2Tbl))
628 {
629 RTMemFree(pL2Entry);
630 pL2Entry = NULL;
631 }
632 else
633 {
634 pL2Entry->cRefs = 1;
635 pImage->cbL2Cache += pImage->cbL2Table;
636 }
637 }
638 }
639 else
640 {
641 /* Evict the last not in use entry and use it */
642 Assert(!RTListIsEmpty(&pImage->ListLru));
643
644 RTListForEachReverse(&pImage->ListLru, pL2Entry, QCOWL2CACHEENTRY, NodeLru)
645 {
646 if (!pL2Entry->cRefs)
647 break;
648 }
649
650 if (!RTListNodeIsDummy(&pImage->ListSearch, pL2Entry, QCOWL2CACHEENTRY, NodeSearch))
651 {
652 RTListNodeRemove(&pL2Entry->NodeSearch);
653 RTListNodeRemove(&pL2Entry->NodeLru);
654 pL2Entry->offL2Tbl = 0;
655 pL2Entry->cRefs = 1;
656 }
657 else
658 pL2Entry = NULL;
659 }
660
661 return pL2Entry;
662}
663
664/**
665 * Frees a L2 table cache entry.
666 *
667 * @returns nothing.
668 * @param pImage The image instance data.
669 * @param pL2Entry The L2 cache entry to free.
670 */
671static void qcowL2TblCacheEntryFree(PQCOWIMAGE pImage, PQCOWL2CACHEENTRY pL2Entry)
672{
673 Assert(!pL2Entry->cRefs);
674 RTMemPageFree(pL2Entry->paL2Tbl, pImage->cbL2Table);
675 RTMemFree(pL2Entry);
676
677 pImage->cbL2Cache -= pImage->cbL2Table;
678}
679
680/**
681 * Inserts an entry in the L2 table cache.
682 *
683 * @returns nothing.
684 * @param pImage The image instance data.
685 * @param pL2Entry The L2 cache entry to insert.
686 */
687static void qcowL2TblCacheEntryInsert(PQCOWIMAGE pImage, PQCOWL2CACHEENTRY pL2Entry)
688{
689 Assert(pL2Entry->offL2Tbl > 0);
690
691 /* Insert at the top of the LRU list. */
692 RTListPrepend(&pImage->ListLru, &pL2Entry->NodeLru);
693
694 if (RTListIsEmpty(&pImage->ListSearch))
695 {
696 RTListAppend(&pImage->ListSearch, &pL2Entry->NodeSearch);
697 }
698 else
699 {
700 /* Insert into search list. */
701 PQCOWL2CACHEENTRY pIt;
702 pIt = RTListGetFirst(&pImage->ListSearch, QCOWL2CACHEENTRY, NodeSearch);
703 if (pIt->offL2Tbl > pL2Entry->offL2Tbl)
704 RTListPrepend(&pImage->ListSearch, &pL2Entry->NodeSearch);
705 else
706 {
707 bool fInserted = false;
708
709 RTListForEach(&pImage->ListSearch, pIt, QCOWL2CACHEENTRY, NodeSearch)
710 {
711 Assert(pIt->offL2Tbl != pL2Entry->offL2Tbl);
712 if (pIt->offL2Tbl < pL2Entry->offL2Tbl)
713 {
714 RTListNodeInsertAfter(&pIt->NodeSearch, &pL2Entry->NodeSearch);
715 fInserted = true;
716 break;
717 }
718 }
719 Assert(fInserted);
720 }
721 }
722}
723
724/**
725 * Fetches the L2 from the given offset trying the LRU cache first and
726 * reading it from the image after a cache miss.
727 *
728 * @returns VBox status code.
729 * @param pImage Image instance data.
730 * @param pIoCtx The I/O context.
731 * @param offL2Tbl The offset of the L2 table in the image.
732 * @param ppL2Entry Where to store the L2 table on success.
733 */
734static int qcowL2TblCacheFetch(PQCOWIMAGE pImage, PVDIOCTX pIoCtx, uint64_t offL2Tbl,
735 PQCOWL2CACHEENTRY *ppL2Entry)
736{
737 int rc = VINF_SUCCESS;
738
739 /* Try to fetch the L2 table from the cache first. */
740 PQCOWL2CACHEENTRY pL2Entry = qcowL2TblCacheRetain(pImage, offL2Tbl);
741 if (!pL2Entry)
742 {
743 pL2Entry = qcowL2TblCacheEntryAlloc(pImage);
744
745 if (pL2Entry)
746 {
747 /* Read from the image. */
748 PVDMETAXFER pMetaXfer;
749
750 pL2Entry->offL2Tbl = offL2Tbl;
751 rc = vdIfIoIntFileReadMeta(pImage->pIfIo, pImage->pStorage,
752 offL2Tbl, pL2Entry->paL2Tbl,
753 pImage->cbL2Table, pIoCtx,
754 &pMetaXfer, NULL, NULL);
755 if (RT_SUCCESS(rc))
756 {
757 vdIfIoIntMetaXferRelease(pImage->pIfIo, pMetaXfer);
758#if defined(RT_LITTLE_ENDIAN)
759 qcowTableConvertToHostEndianess(pL2Entry->paL2Tbl, pImage->cL2TableEntries);
760#endif
761 qcowL2TblCacheEntryInsert(pImage, pL2Entry);
762 }
763 else
764 {
765 qcowL2TblCacheEntryRelease(pL2Entry);
766 qcowL2TblCacheEntryFree(pImage, pL2Entry);
767 }
768 }
769 else
770 rc = VERR_NO_MEMORY;
771 }
772
773 if (RT_SUCCESS(rc))
774 *ppL2Entry = pL2Entry;
775
776 return rc;
777}
778
779/**
780 * Sets the L1, L2 and offset bitmasks and L1 and L2 bit shift members.
781 *
782 * @returns nothing.
783 * @param pImage The image instance data.
784 */
785static void qcowTableMasksInit(PQCOWIMAGE pImage)
786{
787 uint32_t cClusterBits, cL2TableBits;
788
789 cClusterBits = qcowGetPowerOfTwo(pImage->cbCluster);
790 cL2TableBits = qcowGetPowerOfTwo(pImage->cL2TableEntries);
791
792 Assert(cClusterBits + cL2TableBits < 64);
793
794 pImage->fOffsetMask = ((uint64_t)pImage->cbCluster - 1);
795 pImage->fL2Mask = ((uint64_t)pImage->cL2TableEntries - 1) << cClusterBits;
796 pImage->cL2Shift = cClusterBits;
797 pImage->cL1Shift = cClusterBits + cL2TableBits;
798}
799
800/**
801 * Converts a given logical offset into the
802 *
803 * @returns nothing.
804 * @param pImage The image instance data.
805 * @param off The logical offset to convert.
806 * @param pidxL1 Where to store the index in the L1 table on success.
807 * @param pidxL2 Where to store the index in the L2 table on success.
808 * @param poffCluster Where to store the offset in the cluster on success.
809 */
810DECLINLINE(void) qcowConvertLogicalOffset(PQCOWIMAGE pImage, uint64_t off, uint32_t *pidxL1,
811 uint32_t *pidxL2, uint32_t *poffCluster)
812{
813 AssertPtr(pidxL1);
814 AssertPtr(pidxL2);
815 AssertPtr(poffCluster);
816
817 *poffCluster = off & pImage->fOffsetMask;
818 *pidxL1 = off >> pImage->cL1Shift;
819 *pidxL2 = (off & pImage->fL2Mask) >> pImage->cL2Shift;
820}
821
822/**
823 * Converts Cluster size to a byte size.
824 *
825 * @returns Number of bytes derived from the given number of clusters.
826 * @param pImage The image instance data.
827 * @param cClusters The clusters to convert.
828 */
829DECLINLINE(uint64_t) qcowCluster2Byte(PQCOWIMAGE pImage, uint64_t cClusters)
830{
831 return cClusters * pImage->cbCluster;
832}
833
834/**
835 * Converts number of bytes to cluster size rounding to the next cluster.
836 *
837 * @returns Number of bytes derived from the given number of clusters.
838 * @param pImage The image instance data.
839 * @param cb Number of bytes to convert.
840 */
841DECLINLINE(uint64_t) qcowByte2Cluster(PQCOWIMAGE pImage, uint64_t cb)
842{
843 return cb / pImage->cbCluster + (cb % pImage->cbCluster ? 1 : 0);
844}
845
846/**
847 * Allocates a new cluster in the image.
848 *
849 * @returns The start offset of the new cluster in the image.
850 * @param pImage The image instance data.
851 * @param cClusters Number of clusters to allocate.
852 */
853DECLINLINE(uint64_t) qcowClusterAllocate(PQCOWIMAGE pImage, uint32_t cClusters)
854{
855 uint64_t offCluster;
856
857 offCluster = pImage->offNextCluster;
858 pImage->offNextCluster += cClusters*pImage->cbCluster;
859
860 return offCluster;
861}
862
863/**
864 * Returns the real image offset for a given cluster or an error if the cluster is not
865 * yet allocated.
866 *
867 * @returns VBox status code.
868 * VERR_VD_BLOCK_FREE if the cluster is not yet allocated.
869 * @param pImage The image instance data.
870 * @param pIoCtx The I/O context.
871 * @param idxL1 The L1 index.
872 * @param idxL2 The L2 index.
873 * @param offCluster Offset inside the cluster.
874 * @param poffImage Where to store the image offset on success.
875 * @param pfCompressed Where to store the flag whether the cluster is compressed on success.
876 * @param pcbCompressed Where to store the size of the compressed cluster in bytes on success.
877 * Only valid when the cluster comrpessed flag is true.
878 */
879static int qcowConvertToImageOffset(PQCOWIMAGE pImage, PVDIOCTX pIoCtx,
880 uint32_t idxL1, uint32_t idxL2,
881 uint32_t offCluster, uint64_t *poffImage,
882 bool *pfCompressed, size_t *pcbCompressed)
883{
884 int rc = VERR_VD_BLOCK_FREE;
885
886 AssertReturn(idxL1 < pImage->cL1TableEntries, VERR_INVALID_PARAMETER);
887 AssertReturn(idxL2 < pImage->cL2TableEntries, VERR_INVALID_PARAMETER);
888
889 if (pImage->paL1Table[idxL1])
890 {
891 PQCOWL2CACHEENTRY pL2Entry;
892
893 uint64_t offL2Tbl = pImage->paL1Table[idxL1];
894 if (pImage->uVersion == 2)
895 offL2Tbl &= QCOW_V2_TBL_OFFSET_MASK;
896 rc = qcowL2TblCacheFetch(pImage, pIoCtx, offL2Tbl, &pL2Entry);
897 if (RT_SUCCESS(rc))
898 {
899 /* Get real file offset. */
900 if (pL2Entry->paL2Tbl[idxL2])
901 {
902 uint64_t off = pL2Entry->paL2Tbl[idxL2];
903
904 /* Strip flags */
905 if (pImage->uVersion == 2)
906 {
907 if (RT_UNLIKELY(off & QCOW_V2_COMPRESSED_FLAG))
908 {
909 size_t cCompressedClusterSectors = ((off & pImage->fMaskCompressedClusterSectors) >> pImage->cBitsShiftRCompressedClusterSectors);
910 uint64_t offImage = off & pImage->fMaskCompressedClusterOffset;
911
912 *pfCompressed = true;
913 *poffImage = offImage;
914 *pcbCompressed = (cCompressedClusterSectors + 1) * 512 - (offImage & 511ULL);
915 }
916 else
917 {
918 off &= QCOW_V2_TBL_OFFSET_MASK;
919
920 *pfCompressed = false;
921 *poffImage = off + offCluster;
922 }
923 }
924 else
925 {
926 if (RT_UNLIKELY(off & QCOW_V1_COMPRESSED_FLAG))
927 {
928 size_t cCompressedClusterSectors = (off & pImage->fMaskCompressedClusterSectors) >> pImage->cBitsShiftRCompressedClusterSectors;
929
930 *pfCompressed = true;
931 *poffImage = off & pImage->fMaskCompressedClusterOffset;
932 *pcbCompressed = cCompressedClusterSectors * 512; /* Only additional sectors */
933 /* Add remaining bytes of the sector the offset starts in. */
934 *pcbCompressed += 512 - RT_ALIGN_64(*poffImage, 512) - *poffImage;
935 }
936 else
937 {
938 off &= ~QCOW_V1_COMPRESSED_FLAG;
939
940 *pfCompressed = false;
941 *poffImage = off + offCluster;
942 }
943 }
944 }
945 else
946 rc = VERR_VD_BLOCK_FREE;
947
948 qcowL2TblCacheEntryRelease(pL2Entry);
949 }
950 }
951
952 return rc;
953}
954
955/**
956 * Write the given table to image converting to the image endianess if required.
957 *
958 * @returns VBox status code.
959 * @param pImage The image instance data.
960 * @param pIoCtx The I/O context.
961 * @param offTbl The offset the table should be written to.
962 * @param paTbl The table to write.
963 * @param cbTbl Size of the table in bytes.
964 * @param cTblEntries Number entries in the table.
965 * @param pfnComplete Callback called when the write completes.
966 * @param pvUser Opaque user data to pass in the completion callback.
967 */
968static int qcowTblWrite(PQCOWIMAGE pImage, PVDIOCTX pIoCtx, uint64_t offTbl, uint64_t *paTbl,
969 size_t cbTbl, unsigned cTblEntries,
970 PFNVDXFERCOMPLETED pfnComplete, void *pvUser)
971{
972 int rc = VINF_SUCCESS;
973
974#if defined(RT_LITTLE_ENDIAN)
975 uint64_t *paTblImg = (uint64_t *)RTMemAllocZ(cbTbl);
976 if (paTblImg)
977 {
978 qcowTableConvertFromHostEndianess(paTblImg, paTbl, cTblEntries);
979 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
980 offTbl, paTblImg, cbTbl,
981 pIoCtx, pfnComplete, pvUser);
982 RTMemFree(paTblImg);
983 }
984 else
985 rc = VERR_NO_MEMORY;
986#else
987 /* Write table directly. */
988 RT_NOREF(cTblEntries);
989 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
990 offTbl, paTbl, cbTbl, pIoCtx,
991 pfnComplete, pvUser);
992#endif
993
994 return rc;
995}
996
997/**
998 * Internal. Flush image data to disk.
999 */
1000static int qcowFlushImage(PQCOWIMAGE pImage)
1001{
1002 int rc = VINF_SUCCESS;
1003
1004 if ( pImage->pStorage
1005 && !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1006 && pImage->cbL1Table)
1007 {
1008 QCowHeader Header;
1009
1010#if defined(RT_LITTLE_ENDIAN)
1011 uint64_t *paL1TblImg = (uint64_t *)RTMemAllocZ(pImage->cbL1Table);
1012 if (paL1TblImg)
1013 {
1014 qcowTableConvertFromHostEndianess(paL1TblImg, pImage->paL1Table,
1015 pImage->cL1TableEntries);
1016 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
1017 pImage->offL1Table, paL1TblImg,
1018 pImage->cbL1Table);
1019 RTMemFree(paL1TblImg);
1020 }
1021 else
1022 rc = VERR_NO_MEMORY;
1023#else
1024 /* Write L1 table directly. */
1025 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, pImage->offL1Table,
1026 pImage->paL1Table, pImage->cbL1Table);
1027#endif
1028 if (RT_SUCCESS(rc))
1029 {
1030 /* Write header. */
1031 size_t cbHeader = 0;
1032 qcowHdrConvertFromHostEndianess(pImage, &Header, &cbHeader);
1033 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, 0, &Header,
1034 cbHeader);
1035 if (RT_SUCCESS(rc))
1036 rc = vdIfIoIntFileFlushSync(pImage->pIfIo, pImage->pStorage);
1037 }
1038 }
1039
1040 return rc;
1041}
1042
1043/**
1044 * Internal. Free all allocated space for representing an image except pImage,
1045 * and optionally delete the image from disk.
1046 */
1047static int qcowFreeImage(PQCOWIMAGE pImage, bool fDelete)
1048{
1049 int rc = VINF_SUCCESS;
1050
1051 /* Freeing a never allocated image (e.g. because the open failed) is
1052 * not signalled as an error. After all nothing bad happens. */
1053 if (pImage)
1054 {
1055 if (pImage->pStorage)
1056 {
1057 /* No point updating the file that is deleted anyway. */
1058 if (!fDelete)
1059 qcowFlushImage(pImage);
1060
1061 rc = vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
1062 pImage->pStorage = NULL;
1063 }
1064
1065 if (pImage->paRefcountTable)
1066 RTMemFree(pImage->paRefcountTable);
1067 pImage->paRefcountTable = NULL;
1068
1069 if (pImage->paL1Table)
1070 RTMemFree(pImage->paL1Table);
1071
1072 if (pImage->pszBackingFilename)
1073 {
1074 RTStrFree(pImage->pszBackingFilename);
1075 pImage->pszBackingFilename = NULL;
1076 }
1077
1078 if (pImage->pvCompCluster)
1079 {
1080 RTMemFree(pImage->pvCompCluster);
1081 pImage->pvCompCluster = NULL;
1082 pImage->cbCompCluster = 0;
1083 }
1084
1085 if (pImage->pvCluster)
1086 {
1087 RTMemFree(pImage->pvCluster);
1088 pImage->pvCluster = NULL;
1089 }
1090
1091 qcowL2TblCacheDestroy(pImage);
1092
1093 if (fDelete && pImage->pszFilename)
1094 vdIfIoIntFileDelete(pImage->pIfIo, pImage->pszFilename);
1095 }
1096
1097 LogFlowFunc(("returns %Rrc\n", rc));
1098 return rc;
1099}
1100
1101/**
1102 * Validates the header.
1103 *
1104 * @returns VBox status code.
1105 * @param pImage Image backend instance data.
1106 * @param pHdr The header to validate.
1107 * @param cbFile The image file size in bytes.
1108 */
1109static int qcowHdrValidate(PQCOWIMAGE pImage, PQCowHeader pHdr, uint64_t cbFile)
1110{
1111 if (pHdr->u32Version == 1)
1112 {
1113 /* Check that the backing filename is contained in the file. */
1114 if (pHdr->Version.v1.u64BackingFileOffset + pHdr->Version.v1.u32BackingFileSize > cbFile)
1115 return vdIfError(pImage->pIfError, VERR_INVALID_STATE, RT_SRC_POS,
1116 N_("QCOW: Backing file offset and size exceed size of image '%s' (%u vs %u)"),
1117 pImage->pszFilename, pHdr->Version.v1.u64BackingFileOffset + pHdr->Version.v1.u32BackingFileSize,
1118 cbFile);
1119
1120 /* Check that the cluster bits indicate at least a 512byte sector size. */
1121 if (RT_BIT_32(pHdr->Version.v1.u8ClusterBits) < 512)
1122 return vdIfError(pImage->pIfError, VERR_INVALID_STATE, RT_SRC_POS,
1123 N_("QCOW: Cluster size is too small for image '%s' (%u vs %u)"),
1124 pImage->pszFilename, RT_BIT_32(pHdr->Version.v1.u8ClusterBits), 512);
1125
1126 /*
1127 * Check for possible overflow when multiplying cluster size and L2 entry count because it is used
1128 * to calculate the number of L1 table entries later on.
1129 */
1130 if (RT_BIT_32(pHdr->Version.v1.u8L2Bits) * RT_BIT_32(pHdr->Version.v1.u8ClusterBits) == 0)
1131 return vdIfError(pImage->pIfError, VERR_INVALID_STATE, RT_SRC_POS,
1132 N_("QCOW: Overflow during L1 table size calculation for image '%s'"),
1133 pImage->pszFilename);
1134 }
1135 else if (pHdr->u32Version == 2 || pHdr->u32Version == 3)
1136 {
1137 /* Check that the backing filename is contained in the file. */
1138 if (pHdr->Version.v2.u64BackingFileOffset + pHdr->Version.v2.u32BackingFileSize > cbFile)
1139 return vdIfError(pImage->pIfError, VERR_INVALID_STATE, RT_SRC_POS,
1140 N_("QCOW: Backing file offset and size exceed size of image '%s' (%u vs %u)"),
1141 pImage->pszFilename, pHdr->Version.v2.u64BackingFileOffset + pHdr->Version.v2.u32BackingFileSize,
1142 cbFile);
1143
1144 /* Check that the cluster bits indicate at least a 512byte sector size. */
1145 if (RT_BIT_32(pHdr->Version.v2.u32ClusterBits) < 512)
1146 return vdIfError(pImage->pIfError, VERR_INVALID_STATE, RT_SRC_POS,
1147 N_("QCOW: Cluster size is too small for image '%s' (%u vs %u)"),
1148 pImage->pszFilename, RT_BIT_32(pHdr->Version.v2.u32ClusterBits), 512);
1149
1150 /* Some additional checks for v3 images. */
1151 if (pHdr->u32Version == 3)
1152 {
1153 if (pHdr->Version.v2.v3.u32RefCntWidth > 6)
1154 return vdIfError(pImage->pIfError, VERR_INVALID_STATE, RT_SRC_POS,
1155 N_("QCOW: Reference count width too big for image '%s' (%u vs %u)"),
1156 pImage->pszFilename, RT_BIT_32(pHdr->Version.v2.v3.u32RefCntWidth), 6);
1157 }
1158 }
1159 else
1160 return vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1161 N_("QCOW: Version %u in image '%s' is not supported"),
1162 pHdr->u32Version, pImage->pszFilename);
1163
1164 return VINF_SUCCESS;
1165}
1166
1167/**
1168 * Internal: Open an image, constructing all necessary data structures.
1169 */
1170static int qcowOpenImage(PQCOWIMAGE pImage, unsigned uOpenFlags)
1171{
1172 pImage->uOpenFlags = uOpenFlags;
1173
1174 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
1175 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
1176 AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
1177
1178 int rc = qcowL2TblCacheCreate(pImage);
1179 if (RT_SUCCESS(rc))
1180 {
1181 /* Open the image. */
1182 rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename,
1183 VDOpenFlagsToFileOpenFlags(uOpenFlags,
1184 false /* fCreate */),
1185 &pImage->pStorage);
1186 if (RT_SUCCESS(rc))
1187 {
1188 uint64_t cbFile;
1189 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
1190 if ( RT_SUCCESS(rc)
1191 && cbFile > sizeof(QCowHeader))
1192 {
1193 QCowHeader Header;
1194
1195 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, 0, &Header, sizeof(Header));
1196 if ( RT_SUCCESS(rc)
1197 && qcowHdrConvertToHostEndianess(&Header))
1198 {
1199 pImage->offNextCluster = RT_ALIGN_64(cbFile, 512); /* Align image to sector boundary. */
1200 Assert(pImage->offNextCluster >= cbFile);
1201
1202 rc = qcowHdrValidate(pImage, &Header, cbFile);
1203 if (RT_SUCCESS(rc))
1204 {
1205 if (Header.u32Version == 1)
1206 {
1207 if (!Header.Version.v1.u32CryptMethod)
1208 {
1209 pImage->uVersion = 1;
1210 pImage->offBackingFilename = Header.Version.v1.u64BackingFileOffset;
1211 pImage->cbBackingFilename = Header.Version.v1.u32BackingFileSize;
1212 pImage->MTime = Header.Version.v1.u32MTime;
1213 pImage->cbSize = Header.Version.v1.u64Size;
1214 pImage->cClusterBits = Header.Version.v1.u8ClusterBits;
1215 pImage->cbCluster = RT_BIT_32(Header.Version.v1.u8ClusterBits);
1216 pImage->cL2TableEntries = RT_BIT_32(Header.Version.v1.u8L2Bits);
1217 pImage->cbL2Table = RT_ALIGN_64(pImage->cL2TableEntries * sizeof(uint64_t), pImage->cbCluster);
1218 pImage->offL1Table = Header.Version.v1.u64L1TableOffset;
1219 pImage->cL1TableEntries = pImage->cbSize / (pImage->cbCluster * pImage->cL2TableEntries);
1220 if (pImage->cbSize % (pImage->cbCluster * pImage->cL2TableEntries))
1221 pImage->cL1TableEntries++;
1222 }
1223 else
1224 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1225 N_("QCow: Encrypted image '%s' is not supported"),
1226 pImage->pszFilename);
1227 }
1228 else if (Header.u32Version == 2 || Header.u32Version == 3)
1229 {
1230 if (Header.Version.v2.u32CryptMethod)
1231 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1232 N_("QCow: Encrypted image '%s' is not supported"),
1233 pImage->pszFilename);
1234 else if (Header.Version.v2.u32NbSnapshots)
1235 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1236 N_("QCow: Image '%s' contains snapshots which is not supported"),
1237 pImage->pszFilename);
1238 else
1239 {
1240 pImage->uVersion = 2;
1241 pImage->offBackingFilename = Header.Version.v2.u64BackingFileOffset;
1242 pImage->cbBackingFilename = Header.Version.v2.u32BackingFileSize;
1243 pImage->cbSize = Header.Version.v2.u64Size;
1244 pImage->cClusterBits = Header.Version.v2.u32ClusterBits;
1245 pImage->cbCluster = RT_BIT_32(Header.Version.v2.u32ClusterBits);
1246 pImage->cL2TableEntries = pImage->cbCluster / sizeof(uint64_t);
1247 pImage->cbL2Table = pImage->cbCluster;
1248 pImage->offL1Table = Header.Version.v2.u64L1TableOffset;
1249 pImage->cL1TableEntries = Header.Version.v2.u32L1Size;
1250 pImage->offRefcountTable = Header.Version.v2.u64RefcountTableOffset;
1251 pImage->cbRefcountTable = qcowCluster2Byte(pImage, Header.Version.v2.u32RefcountTableClusters);
1252 pImage->cRefcountTableEntries = pImage->cbRefcountTable / sizeof(uint64_t);
1253
1254 /* Init the masks to extract offset and sector count from a compressed cluster descriptor. */
1255 uint32_t cBitsCompressedClusterOffset = 62 - (pImage->cClusterBits - 8);
1256 pImage->fMaskCompressedClusterOffset = RT_BIT_64(cBitsCompressedClusterOffset) - 1;
1257 pImage->fMaskCompressedClusterSectors = (RT_BIT_64(62) - 1) & ~pImage->fMaskCompressedClusterOffset;
1258 pImage->cBitsShiftRCompressedClusterSectors = cBitsCompressedClusterOffset;
1259
1260 if (Header.u32Version == 3)
1261 {
1262 if (Header.Version.v2.v3.u64IncompatFeat & ~QCOW_V3_INCOMPAT_FEAT_SUPPORTED_MASK)
1263 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1264 N_("QCow: Image '%s' contains unsupported incompatible features (%llx vs %llx)"),
1265 pImage->pszFilename, Header.Version.v2.v3.u64IncompatFeat, QCOW_V3_INCOMPAT_FEAT_SUPPORTED_MASK);
1266
1267 /** @todo Auto clear features need to be reset as soon as write support is added. */
1268 }
1269 }
1270 }
1271 else
1272 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1273 N_("QCow: Image '%s' uses version %u which is not supported"),
1274 pImage->pszFilename, Header.u32Version);
1275
1276 if (RT_SUCCESS(rc))
1277 {
1278 pImage->cbL1Table = RT_ALIGN_64(pImage->cL1TableEntries * sizeof(uint64_t), pImage->cbCluster);
1279 if ((uint64_t)pImage->cbL1Table != RT_ALIGN_64(pImage->cL1TableEntries * sizeof(uint64_t), pImage->cbCluster))
1280 rc = vdIfError(pImage->pIfError, VERR_INVALID_STATE, RT_SRC_POS,
1281 N_("QCOW: L1 table size overflow in image '%s'"),
1282 pImage->pszFilename);
1283 }
1284 }
1285
1286 /** @todo Check that there are no compressed clusters in the image
1287 * (by traversing the L2 tables and checking each offset).
1288 * Refuse to open such images.
1289 */
1290
1291 if ( RT_SUCCESS(rc)
1292 && pImage->cbBackingFilename
1293 && pImage->offBackingFilename)
1294 {
1295 /* Load backing filename from image. */
1296 pImage->pszBackingFilename = RTStrAlloc(pImage->cbBackingFilename + 1); /* +1 for \0 terminator. */
1297 if (pImage->pszBackingFilename)
1298 {
1299 RT_BZERO(pImage->pszBackingFilename, pImage->cbBackingFilename + 1);
1300 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
1301 pImage->offBackingFilename, pImage->pszBackingFilename,
1302 pImage->cbBackingFilename);
1303 if (RT_SUCCESS(rc))
1304 rc = RTStrValidateEncoding(pImage->pszBackingFilename);
1305 }
1306 else
1307 rc = VERR_NO_STR_MEMORY;
1308 }
1309
1310 if ( RT_SUCCESS(rc)
1311 && pImage->cbRefcountTable
1312 && pImage->offRefcountTable)
1313 {
1314 /* Load refcount table. */
1315 Assert(pImage->cRefcountTableEntries);
1316 pImage->paRefcountTable = (uint64_t *)RTMemAllocZ(pImage->cbRefcountTable);
1317 if (RT_LIKELY(pImage->paRefcountTable))
1318 {
1319 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
1320 pImage->offRefcountTable, pImage->paRefcountTable,
1321 pImage->cbRefcountTable);
1322 if (RT_SUCCESS(rc))
1323 qcowTableConvertToHostEndianess(pImage->paRefcountTable,
1324 pImage->cRefcountTableEntries);
1325 else
1326 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1327 N_("QCow: Reading refcount table of image '%s' failed"),
1328 pImage->pszFilename);
1329 }
1330 else
1331 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
1332 N_("QCow: Allocating memory for refcount table of image '%s' failed"),
1333 pImage->pszFilename);
1334 }
1335
1336 if (RT_SUCCESS(rc))
1337 {
1338 qcowTableMasksInit(pImage);
1339
1340 /* Allocate L1 table. */
1341 pImage->paL1Table = (uint64_t *)RTMemAllocZ(pImage->cbL1Table);
1342 if (pImage->paL1Table)
1343 {
1344 /* Read from the image. */
1345 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
1346 pImage->offL1Table, pImage->paL1Table,
1347 pImage->cbL1Table);
1348 if (RT_SUCCESS(rc))
1349 qcowTableConvertToHostEndianess(pImage->paL1Table, pImage->cL1TableEntries);
1350 else
1351 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1352 N_("QCow: Reading the L1 table for image '%s' failed"),
1353 pImage->pszFilename);
1354 }
1355 else
1356 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
1357 N_("QCow: Out of memory allocating L1 table for image '%s'"),
1358 pImage->pszFilename);
1359 }
1360 }
1361 else if (RT_SUCCESS(rc))
1362 rc = VERR_VD_GEN_INVALID_HEADER;
1363 }
1364 else if (RT_SUCCESS(rc))
1365 rc = VERR_VD_GEN_INVALID_HEADER;
1366 }
1367 /* else: Do NOT signal an appropriate error here, as the VD layer has the
1368 * choice of retrying the open if it failed. */
1369 }
1370 else
1371 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1372 N_("Qcow: Creating the L2 table cache for image '%s' failed"),
1373 pImage->pszFilename);
1374
1375 if (RT_SUCCESS(rc))
1376 {
1377 PVDREGIONDESC pRegion = &pImage->RegionList.aRegions[0];
1378 pImage->RegionList.fFlags = 0;
1379 pImage->RegionList.cRegions = 1;
1380
1381 pRegion->offRegion = 0; /* Disk start. */
1382 pRegion->cbBlock = 512;
1383 pRegion->enmDataForm = VDREGIONDATAFORM_RAW;
1384 pRegion->enmMetadataForm = VDREGIONMETADATAFORM_NONE;
1385 pRegion->cbData = 512;
1386 pRegion->cbMetadata = 0;
1387 pRegion->cRegionBlocksOrBytes = pImage->cbSize;
1388 }
1389 else
1390 qcowFreeImage(pImage, false);
1391 return rc;
1392}
1393
1394/**
1395 * Internal: Create a qcow image.
1396 */
1397static int qcowCreateImage(PQCOWIMAGE pImage, uint64_t cbSize,
1398 unsigned uImageFlags, const char *pszComment,
1399 PCVDGEOMETRY pPCHSGeometry,
1400 PCVDGEOMETRY pLCHSGeometry, unsigned uOpenFlags,
1401 PVDINTERFACEPROGRESS pIfProgress,
1402 unsigned uPercentStart, unsigned uPercentSpan)
1403{
1404 RT_NOREF1(pszComment);
1405 int rc;
1406 int32_t fOpen;
1407
1408 if (!(uImageFlags & VD_IMAGE_FLAGS_FIXED))
1409 {
1410 rc = qcowL2TblCacheCreate(pImage);
1411 if (RT_SUCCESS(rc))
1412 {
1413 pImage->uOpenFlags = uOpenFlags & ~VD_OPEN_FLAGS_READONLY;
1414 pImage->uImageFlags = uImageFlags;
1415 pImage->PCHSGeometry = *pPCHSGeometry;
1416 pImage->LCHSGeometry = *pLCHSGeometry;
1417 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
1418 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
1419 AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
1420
1421 /* Create image file. */
1422 fOpen = VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags, true /* fCreate */);
1423 rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename, fOpen, &pImage->pStorage);
1424 if (RT_SUCCESS(rc))
1425 {
1426 /* Init image state. */
1427 pImage->uVersion = 1; /* We create only version 1 images at the moment. */
1428 pImage->cbSize = cbSize;
1429 pImage->cbCluster = QCOW_CLUSTER_SIZE_DEFAULT;
1430 pImage->cbL2Table = qcowCluster2Byte(pImage, QCOW_L2_CLUSTERS_DEFAULT);
1431 pImage->cL2TableEntries = pImage->cbL2Table / sizeof(uint64_t);
1432 pImage->cL1TableEntries = cbSize / (pImage->cbCluster * pImage->cL2TableEntries);
1433 if (cbSize % (pImage->cbCluster * pImage->cL2TableEntries))
1434 pImage->cL1TableEntries++;
1435 pImage->cbL1Table = RT_ALIGN_64(pImage->cL1TableEntries * sizeof(uint64_t), pImage->cbCluster);
1436 pImage->offL1Table = QCOW_V1_HDR_SIZE;
1437 pImage->cbBackingFilename = 0;
1438 pImage->offBackingFilename = 0;
1439 pImage->offNextCluster = RT_ALIGN_64(QCOW_V1_HDR_SIZE + pImage->cbL1Table, pImage->cbCluster);
1440 qcowTableMasksInit(pImage);
1441
1442 /* Init L1 table. */
1443 pImage->paL1Table = (uint64_t *)RTMemAllocZ(pImage->cbL1Table);
1444 if (RT_LIKELY(pImage->paL1Table))
1445 {
1446 if (RT_SUCCESS(rc))
1447 vdIfProgress(pIfProgress, uPercentStart + uPercentSpan * 98 / 100);
1448
1449 rc = qcowFlushImage(pImage);
1450 if (RT_SUCCESS(rc))
1451 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, pImage->offNextCluster);
1452 }
1453 else
1454 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS, N_("QCow: cannot allocate memory for L1 table of image '%s'"),
1455 pImage->pszFilename);
1456 }
1457 else
1458 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("QCow: cannot create image '%s'"), pImage->pszFilename);
1459 }
1460 else
1461 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("QCow: Failed to create L2 cache for image '%s'"),
1462 pImage->pszFilename);
1463 }
1464 else
1465 rc = vdIfError(pImage->pIfError, VERR_VD_INVALID_TYPE, RT_SRC_POS, N_("QCow: cannot create fixed image '%s'"), pImage->pszFilename);
1466
1467 if (RT_SUCCESS(rc))
1468 vdIfProgress(pIfProgress, uPercentStart + uPercentSpan);
1469
1470 if (RT_SUCCESS(rc))
1471 {
1472 PVDREGIONDESC pRegion = &pImage->RegionList.aRegions[0];
1473 pImage->RegionList.fFlags = 0;
1474 pImage->RegionList.cRegions = 1;
1475
1476 pRegion->offRegion = 0; /* Disk start. */
1477 pRegion->cbBlock = 512;
1478 pRegion->enmDataForm = VDREGIONDATAFORM_RAW;
1479 pRegion->enmMetadataForm = VDREGIONMETADATAFORM_NONE;
1480 pRegion->cbData = 512;
1481 pRegion->cbMetadata = 0;
1482 pRegion->cRegionBlocksOrBytes = pImage->cbSize;
1483 }
1484 else
1485 qcowFreeImage(pImage, rc != VERR_ALREADY_EXISTS);
1486 return rc;
1487}
1488
1489/**
1490 * Rollback anything done during async cluster allocation.
1491 *
1492 * @returns VBox status code.
1493 * @param pImage The image instance data.
1494 * @param pIoCtx The I/O context.
1495 * @param pClusterAlloc The cluster allocation to rollback.
1496 */
1497static int qcowAsyncClusterAllocRollback(PQCOWIMAGE pImage, PVDIOCTX pIoCtx, PQCOWCLUSTERASYNCALLOC pClusterAlloc)
1498{
1499 RT_NOREF1(pIoCtx);
1500 int rc = VINF_SUCCESS;
1501
1502 switch (pClusterAlloc->enmAllocState)
1503 {
1504 case QCOWCLUSTERASYNCALLOCSTATE_L2_ALLOC:
1505 case QCOWCLUSTERASYNCALLOCSTATE_L2_LINK:
1506 {
1507 /* Revert the L1 table entry */
1508 pImage->paL1Table[pClusterAlloc->idxL1] = 0;
1509 pImage->pL2TblAlloc = NULL;
1510
1511 /* Assumption right now is that the L1 table is not modified on storage if the link fails. */
1512 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, pClusterAlloc->offNextClusterOld);
1513 qcowL2TblCacheEntryRelease(pClusterAlloc->pL2Entry); /* Release L2 cache entry. */
1514 Assert(!pClusterAlloc->pL2Entry->cRefs);
1515 qcowL2TblCacheEntryFree(pImage, pClusterAlloc->pL2Entry); /* Free it, it is not in the cache yet. */
1516 break;
1517 }
1518 case QCOWCLUSTERASYNCALLOCSTATE_USER_ALLOC:
1519 case QCOWCLUSTERASYNCALLOCSTATE_USER_LINK:
1520 {
1521 /* Assumption right now is that the L2 table is not modified if the link fails. */
1522 pClusterAlloc->pL2Entry->paL2Tbl[pClusterAlloc->idxL2] = 0;
1523 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, pClusterAlloc->offNextClusterOld);
1524 qcowL2TblCacheEntryRelease(pClusterAlloc->pL2Entry); /* Release L2 cache entry. */
1525 break;
1526 }
1527 default:
1528 AssertMsgFailed(("Invalid cluster allocation state %d\n", pClusterAlloc->enmAllocState));
1529 rc = VERR_INVALID_STATE;
1530 }
1531
1532 RTMemFree(pClusterAlloc);
1533 return rc;
1534}
1535
1536/**
1537 * Updates the state of the async cluster allocation.
1538 *
1539 * @returns VBox status code.
1540 * @param pBackendData The opaque backend data.
1541 * @param pIoCtx I/O context associated with this request.
1542 * @param pvUser Opaque user data passed during a read/write request.
1543 * @param rcReq Status code for the completed request.
1544 */
1545static DECLCALLBACK(int) qcowAsyncClusterAllocUpdate(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
1546{
1547 int rc = VINF_SUCCESS;
1548 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
1549 PQCOWCLUSTERASYNCALLOC pClusterAlloc = (PQCOWCLUSTERASYNCALLOC)pvUser;
1550
1551 if (RT_FAILURE(rcReq))
1552 return qcowAsyncClusterAllocRollback(pImage, pIoCtx, pClusterAlloc);
1553
1554 AssertPtr(pClusterAlloc->pL2Entry);
1555
1556 switch (pClusterAlloc->enmAllocState)
1557 {
1558 case QCOWCLUSTERASYNCALLOCSTATE_L2_ALLOC:
1559 {
1560 /* Update the link in the in memory L1 table now. */
1561 pImage->paL1Table[pClusterAlloc->idxL1] = pClusterAlloc->pL2Entry->offL2Tbl;
1562
1563 /* Update the link in the on disk L1 table now. */
1564 pClusterAlloc->enmAllocState = QCOWCLUSTERASYNCALLOCSTATE_L2_LINK;
1565 rc = qcowTblWrite(pImage, pIoCtx, pImage->offL1Table, pImage->paL1Table,
1566 pImage->cbL1Table, pImage->cL1TableEntries,
1567 qcowAsyncClusterAllocUpdate, pClusterAlloc);
1568 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1569 break;
1570 else if (RT_FAILURE(rc))
1571 {
1572 /* Rollback. */
1573 qcowAsyncClusterAllocRollback(pImage, pIoCtx, pClusterAlloc);
1574 break;
1575 }
1576 /* Success, fall through. */
1577 }
1578 RT_FALL_THRU();
1579 case QCOWCLUSTERASYNCALLOCSTATE_L2_LINK:
1580 {
1581 /* L2 link updated in L1 , save L2 entry in cache and allocate new user data cluster. */
1582 uint64_t offData = qcowClusterAllocate(pImage, 1);
1583
1584 pImage->pL2TblAlloc = NULL;
1585 qcowL2TblCacheEntryInsert(pImage, pClusterAlloc->pL2Entry);
1586
1587 pClusterAlloc->enmAllocState = QCOWCLUSTERASYNCALLOCSTATE_USER_ALLOC;
1588 pClusterAlloc->offNextClusterOld = offData;
1589 pClusterAlloc->offClusterNew = offData;
1590
1591 /* Write data. */
1592 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
1593 offData, pIoCtx, pClusterAlloc->cbToWrite,
1594 qcowAsyncClusterAllocUpdate, pClusterAlloc);
1595 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1596 break;
1597 else if (RT_FAILURE(rc))
1598 {
1599 qcowAsyncClusterAllocRollback(pImage, pIoCtx, pClusterAlloc);
1600 RTMemFree(pClusterAlloc);
1601 break;
1602 }
1603 }
1604 RT_FALL_THRU();
1605 case QCOWCLUSTERASYNCALLOCSTATE_USER_ALLOC:
1606 {
1607 pClusterAlloc->enmAllocState = QCOWCLUSTERASYNCALLOCSTATE_USER_LINK;
1608 pClusterAlloc->pL2Entry->paL2Tbl[pClusterAlloc->idxL2] = pClusterAlloc->offClusterNew;
1609
1610 /* Link L2 table and update it. */
1611 rc = qcowTblWrite(pImage, pIoCtx, pImage->paL1Table[pClusterAlloc->idxL1],
1612 pClusterAlloc->pL2Entry->paL2Tbl,
1613 pImage->cbL2Table, pImage->cL2TableEntries,
1614 qcowAsyncClusterAllocUpdate, pClusterAlloc);
1615 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1616 break;
1617 else if (RT_FAILURE(rc))
1618 {
1619 qcowAsyncClusterAllocRollback(pImage, pIoCtx, pClusterAlloc);
1620 RTMemFree(pClusterAlloc);
1621 break;
1622 }
1623 }
1624 RT_FALL_THRU();
1625 case QCOWCLUSTERASYNCALLOCSTATE_USER_LINK:
1626 {
1627 /* Everything done without errors, signal completion. */
1628 qcowL2TblCacheEntryRelease(pClusterAlloc->pL2Entry);
1629 RTMemFree(pClusterAlloc);
1630 rc = VINF_SUCCESS;
1631 break;
1632 }
1633 default:
1634 AssertMsgFailed(("Invalid async cluster allocation state %d\n",
1635 pClusterAlloc->enmAllocState));
1636 }
1637
1638 return rc;
1639}
1640
1641/**
1642 * Reads a compressed cluster, inflates it and copies the amount of data requested
1643 * into the given I/O context.
1644 *
1645 * @returns VBox status code.
1646 * @param pImage The image instance data.
1647 * @param pIoCtx The I/O context.
1648 * @param offCluster Where to start reading in the uncompressed cluster.
1649 * @param cbToRead How much to read in the uncomrpessed cluster.
1650 * @param offFile Offset where the compressed cluster is stored in the image.
1651 * @param cbCompressedCluster Size of the comrpessed cluster in bytes.
1652 */
1653static int qcowReadCompressedCluster(PQCOWIMAGE pImage, PVDIOCTX pIoCtx,
1654 uint32_t offCluster, size_t cbToRead,
1655 uint64_t offFile, size_t cbCompressedCluster)
1656{
1657 int rc = VINF_SUCCESS;
1658
1659 AssertReturn(!(pImage->uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO), VERR_NOT_SUPPORTED); /* Only synchronous I/O supported so far. */
1660
1661 if (cbCompressedCluster > pImage->cbCompCluster)
1662 {
1663 void *pvCompClusterNew = RTMemRealloc(pImage->pvCompCluster, cbCompressedCluster);
1664 if (RT_LIKELY(pvCompClusterNew))
1665 {
1666 pImage->pvCompCluster = pvCompClusterNew;
1667 pImage->cbCompCluster = cbCompressedCluster;
1668 }
1669 else
1670 rc = VERR_NO_MEMORY;
1671 }
1672
1673 if (RT_SUCCESS(rc))
1674 {
1675 rc = vdIfIoIntFileReadMeta(pImage->pIfIo, pImage->pStorage,
1676 offFile, pImage->pvCompCluster,
1677 cbCompressedCluster, NULL,
1678 NULL, NULL, NULL);
1679 if (RT_SUCCESS(rc))
1680 {
1681 if (!pImage->pvCluster)
1682 {
1683 pImage->pvCluster = RTMemAllocZ(pImage->cbCluster);
1684 if (!pImage->pvCluster)
1685 rc = VERR_NO_MEMORY;
1686 }
1687
1688 if (RT_SUCCESS(rc))
1689 {
1690 size_t cbDecomp = 0;
1691
1692 rc = RTZipBlockDecompress(RTZIPTYPE_ZLIB_NO_HEADER, 0 /*fFlags*/,
1693 pImage->pvCompCluster, cbCompressedCluster, NULL,
1694 pImage->pvCluster, pImage->cbCluster, &cbDecomp);
1695 if (RT_SUCCESS(rc))
1696 {
1697 Assert(cbDecomp == pImage->cbCluster);
1698 vdIfIoIntIoCtxCopyTo(pImage->pIfIo, pIoCtx,
1699 (uint8_t *)pImage->pvCluster + offCluster,
1700 cbToRead);
1701 }
1702 }
1703 }
1704 }
1705
1706 return rc;
1707}
1708
1709/** @copydoc VDIMAGEBACKEND::pfnProbe */
1710static DECLCALLBACK(int) qcowProbe(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
1711 PVDINTERFACE pVDIfsImage, VDTYPE enmDesiredType, VDTYPE *penmType)
1712{
1713 RT_NOREF(pVDIfsDisk, enmDesiredType);
1714 LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p\n", pszFilename, pVDIfsDisk, pVDIfsImage));
1715 PVDIOSTORAGE pStorage = NULL;
1716 uint64_t cbFile;
1717 int rc = VINF_SUCCESS;
1718
1719 /* Get I/O interface. */
1720 PVDINTERFACEIOINT pIfIo = VDIfIoIntGet(pVDIfsImage);
1721 AssertPtrReturn(pIfIo, VERR_INVALID_PARAMETER);
1722 AssertReturn((VALID_PTR(pszFilename) && *pszFilename), VERR_INVALID_PARAMETER);
1723
1724 /*
1725 * Open the file and read the footer.
1726 */
1727 rc = vdIfIoIntFileOpen(pIfIo, pszFilename,
1728 VDOpenFlagsToFileOpenFlags(VD_OPEN_FLAGS_READONLY,
1729 false /* fCreate */),
1730 &pStorage);
1731 if (RT_SUCCESS(rc))
1732 {
1733 rc = vdIfIoIntFileGetSize(pIfIo, pStorage, &cbFile);
1734 if ( RT_SUCCESS(rc)
1735 && cbFile > sizeof(QCowHeader))
1736 {
1737 QCowHeader Header;
1738
1739 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, 0, &Header, sizeof(Header));
1740 if ( RT_SUCCESS(rc)
1741 && qcowHdrConvertToHostEndianess(&Header))
1742 *penmType = VDTYPE_HDD;
1743 else
1744 rc = VERR_VD_GEN_INVALID_HEADER;
1745 }
1746 else
1747 rc = VERR_VD_GEN_INVALID_HEADER;
1748 }
1749
1750 if (pStorage)
1751 vdIfIoIntFileClose(pIfIo, pStorage);
1752
1753 LogFlowFunc(("returns %Rrc\n", rc));
1754 return rc;
1755}
1756
1757/** @copydoc VDIMAGEBACKEND::pfnOpen */
1758static DECLCALLBACK(int) qcowOpen(const char *pszFilename, unsigned uOpenFlags,
1759 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1760 VDTYPE enmType, void **ppBackendData)
1761{
1762 RT_NOREF1(enmType); /**< @todo r=klaus make use of the type info. */
1763
1764 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p enmType=%u ppBackendData=%#p\n",
1765 pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, enmType, ppBackendData));
1766 int rc;
1767
1768 /* Check open flags. All valid flags are supported. */
1769 AssertReturn(!(uOpenFlags & ~VD_OPEN_FLAGS_MASK), VERR_INVALID_PARAMETER);
1770 AssertReturn((VALID_PTR(pszFilename) && *pszFilename), VERR_INVALID_PARAMETER);
1771
1772 PQCOWIMAGE pImage = (PQCOWIMAGE)RTMemAllocZ(RT_UOFFSETOF(QCOWIMAGE, RegionList.aRegions[1]));
1773 if (RT_LIKELY(pImage))
1774 {
1775 pImage->pszFilename = pszFilename;
1776 pImage->pStorage = NULL;
1777 pImage->pVDIfsDisk = pVDIfsDisk;
1778 pImage->pVDIfsImage = pVDIfsImage;
1779
1780 rc = qcowOpenImage(pImage, uOpenFlags);
1781 if (RT_SUCCESS(rc))
1782 *ppBackendData = pImage;
1783 else
1784 RTMemFree(pImage);
1785 }
1786 else
1787 rc = VERR_NO_MEMORY;
1788
1789 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1790 return rc;
1791}
1792
1793/** @copydoc VDIMAGEBACKEND::pfnCreate */
1794static DECLCALLBACK(int) qcowCreate(const char *pszFilename, uint64_t cbSize,
1795 unsigned uImageFlags, const char *pszComment,
1796 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
1797 PCRTUUID pUuid, unsigned uOpenFlags,
1798 unsigned uPercentStart, unsigned uPercentSpan,
1799 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1800 PVDINTERFACE pVDIfsOperation, VDTYPE enmType,
1801 void **ppBackendData)
1802{
1803 RT_NOREF1(pUuid);
1804 LogFlowFunc(("pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" pPCHSGeometry=%#p pLCHSGeometry=%#p Uuid=%RTuuid uOpenFlags=%#x uPercentStart=%u uPercentSpan=%u pVDIfsDisk=%#p pVDIfsImage=%#p pVDIfsOperation=%#p enmType=%u ppBackendData=%#p\n",
1805 pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData));
1806 int rc;
1807
1808 /* Check the VD container type. */
1809 if (enmType != VDTYPE_HDD)
1810 return VERR_VD_INVALID_TYPE;
1811
1812 /* Check open flags. All valid flags are supported. */
1813 AssertReturn(!(uOpenFlags & ~VD_OPEN_FLAGS_MASK), VERR_INVALID_PARAMETER);
1814 AssertReturn( VALID_PTR(pszFilename)
1815 && *pszFilename
1816 && VALID_PTR(pPCHSGeometry)
1817 && VALID_PTR(pLCHSGeometry), VERR_INVALID_PARAMETER);
1818
1819 PQCOWIMAGE pImage = (PQCOWIMAGE)RTMemAllocZ(RT_UOFFSETOF(QCOWIMAGE, RegionList.aRegions[1]));
1820 if (RT_LIKELY(pImage))
1821 {
1822 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
1823
1824 pImage->pszFilename = pszFilename;
1825 pImage->pStorage = NULL;
1826 pImage->pVDIfsDisk = pVDIfsDisk;
1827 pImage->pVDIfsImage = pVDIfsImage;
1828
1829 rc = qcowCreateImage(pImage, cbSize, uImageFlags, pszComment,
1830 pPCHSGeometry, pLCHSGeometry, uOpenFlags,
1831 pIfProgress, uPercentStart, uPercentSpan);
1832 if (RT_SUCCESS(rc))
1833 {
1834 /* So far the image is opened in read/write mode. Make sure the
1835 * image is opened in read-only mode if the caller requested that. */
1836 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
1837 {
1838 qcowFreeImage(pImage, false);
1839 rc = qcowOpenImage(pImage, uOpenFlags);
1840 }
1841
1842 if (RT_SUCCESS(rc))
1843 *ppBackendData = pImage;
1844 }
1845
1846 if (RT_FAILURE(rc))
1847 RTMemFree(pImage);
1848 }
1849 else
1850 rc = VERR_NO_MEMORY;
1851
1852 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1853 return rc;
1854}
1855
1856/** @copydoc VDIMAGEBACKEND::pfnRename */
1857static DECLCALLBACK(int) qcowRename(void *pBackendData, const char *pszFilename)
1858{
1859 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
1860 int rc = VINF_SUCCESS;
1861 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
1862
1863 /* Check arguments. */
1864 AssertReturn((pImage && pszFilename && *pszFilename), VERR_INVALID_PARAMETER);
1865
1866 /* Close the image. */
1867 rc = qcowFreeImage(pImage, false);
1868 if (RT_SUCCESS(rc))
1869 {
1870 /* Rename the file. */
1871 rc = vdIfIoIntFileMove(pImage->pIfIo, pImage->pszFilename, pszFilename, 0);
1872 if (RT_SUCCESS(rc))
1873 {
1874 /* Update pImage with the new information. */
1875 pImage->pszFilename = pszFilename;
1876
1877 /* Open the old image with new name. */
1878 rc = qcowOpenImage(pImage, pImage->uOpenFlags);
1879 }
1880 else
1881 {
1882 /* The move failed, try to reopen the original image. */
1883 int rc2 = qcowOpenImage(pImage, pImage->uOpenFlags);
1884 if (RT_FAILURE(rc2))
1885 rc = rc2;
1886 }
1887 }
1888
1889 LogFlowFunc(("returns %Rrc\n", rc));
1890 return rc;
1891}
1892
1893/** @copydoc VDIMAGEBACKEND::pfnClose */
1894static DECLCALLBACK(int) qcowClose(void *pBackendData, bool fDelete)
1895{
1896 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
1897 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
1898
1899 int rc = qcowFreeImage(pImage, fDelete);
1900 RTMemFree(pImage);
1901
1902 LogFlowFunc(("returns %Rrc\n", rc));
1903 return rc;
1904}
1905
1906static DECLCALLBACK(int) qcowRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
1907 PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
1908{
1909 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
1910 pBackendData, uOffset, pIoCtx, cbToRead, pcbActuallyRead));
1911 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
1912 uint32_t offCluster = 0;
1913 uint32_t idxL1 = 0;
1914 uint32_t idxL2 = 0;
1915 uint64_t offFile = 0;
1916 int rc;
1917
1918 AssertPtr(pImage);
1919 Assert(uOffset % 512 == 0);
1920 Assert(cbToRead % 512 == 0);
1921 AssertReturn((VALID_PTR(pIoCtx) && cbToRead), VERR_INVALID_PARAMETER);
1922 AssertReturn(uOffset + cbToRead <= pImage->cbSize, VERR_INVALID_PARAMETER);
1923
1924 qcowConvertLogicalOffset(pImage, uOffset, &idxL1, &idxL2, &offCluster);
1925
1926 /* Clip read size to remain in the cluster. */
1927 cbToRead = RT_MIN(cbToRead, pImage->cbCluster - offCluster);
1928
1929 /* Get offset in image. */
1930 bool fCompressedCluster = false;
1931 size_t cbCompressedCluster = 0;
1932 rc = qcowConvertToImageOffset(pImage, pIoCtx, idxL1, idxL2, offCluster,
1933 &offFile, &fCompressedCluster, &cbCompressedCluster);
1934 if (RT_SUCCESS(rc))
1935 {
1936 if (!fCompressedCluster)
1937 rc = vdIfIoIntFileReadUser(pImage->pIfIo, pImage->pStorage, offFile,
1938 pIoCtx, cbToRead);
1939 else
1940 rc = qcowReadCompressedCluster(pImage, pIoCtx, offCluster, cbToRead, offFile, cbCompressedCluster);
1941 }
1942
1943 if ( ( RT_SUCCESS(rc)
1944 || rc == VERR_VD_BLOCK_FREE
1945 || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1946 && pcbActuallyRead)
1947 *pcbActuallyRead = cbToRead;
1948
1949 LogFlowFunc(("returns %Rrc\n", rc));
1950 return rc;
1951}
1952
1953static DECLCALLBACK(int) qcowWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
1954 PVDIOCTX pIoCtx, size_t *pcbWriteProcess, size_t *pcbPreRead,
1955 size_t *pcbPostRead, unsigned fWrite)
1956{
1957 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
1958 pBackendData, uOffset, pIoCtx, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
1959 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
1960 uint32_t offCluster = 0;
1961 uint32_t idxL1 = 0;
1962 uint32_t idxL2 = 0;
1963 uint64_t offImage = 0;
1964 int rc = VINF_SUCCESS;
1965
1966 AssertPtr(pImage);
1967 Assert(!(uOffset % 512));
1968 Assert(!(cbToWrite % 512));
1969 AssertReturn((VALID_PTR(pIoCtx) && cbToWrite), VERR_INVALID_PARAMETER);
1970 AssertReturn(uOffset + cbToWrite <= pImage->cbSize, VERR_INVALID_PARAMETER);
1971
1972 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1973 {
1974 /* Convert offset to L1, L2 index and cluster offset. */
1975 qcowConvertLogicalOffset(pImage, uOffset, &idxL1, &idxL2, &offCluster);
1976
1977 /* Clip write size to remain in the cluster. */
1978 cbToWrite = RT_MIN(cbToWrite, pImage->cbCluster - offCluster);
1979 Assert(!(cbToWrite % 512));
1980
1981 /* Get offset in image. */
1982 bool fCompressedCluster = false;
1983 size_t cbCompressedCluster = 0;
1984 rc = qcowConvertToImageOffset(pImage, pIoCtx, idxL1, idxL2, offCluster,
1985 &offImage, &fCompressedCluster, &cbCompressedCluster);
1986 if (RT_SUCCESS(rc))
1987 {
1988 if (!fCompressedCluster)
1989 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
1990 offImage, pIoCtx, cbToWrite, NULL, NULL);
1991 else
1992 rc = VERR_NOT_SUPPORTED; /** @todo Support writing compressed clusters */
1993 }
1994 else if (rc == VERR_VD_BLOCK_FREE)
1995 {
1996 if ( cbToWrite == pImage->cbCluster
1997 && !(fWrite & VD_WRITE_NO_ALLOC))
1998 {
1999 PQCOWL2CACHEENTRY pL2Entry = NULL;
2000
2001 /* Full cluster write to previously unallocated cluster.
2002 * Allocate cluster and write data. */
2003 Assert(!offCluster);
2004
2005 do
2006 {
2007 /* Check if we have to allocate a new cluster for L2 tables. */
2008 if (!pImage->paL1Table[idxL1])
2009 {
2010 uint64_t offL2Tbl;
2011 PQCOWCLUSTERASYNCALLOC pL2ClusterAlloc = NULL;
2012
2013 /* Allocate new async cluster allocation state. */
2014 pL2ClusterAlloc = (PQCOWCLUSTERASYNCALLOC)RTMemAllocZ(sizeof(QCOWCLUSTERASYNCALLOC));
2015 if (RT_UNLIKELY(!pL2ClusterAlloc))
2016 {
2017 rc = VERR_NO_MEMORY;
2018 break;
2019 }
2020
2021 pL2Entry = qcowL2TblCacheEntryAlloc(pImage);
2022 if (!pL2Entry)
2023 {
2024 rc = VERR_NO_MEMORY;
2025 RTMemFree(pL2ClusterAlloc);
2026 break;
2027 }
2028
2029 offL2Tbl = qcowClusterAllocate(pImage, qcowByte2Cluster(pImage, pImage->cbL2Table));
2030 pL2Entry->offL2Tbl = offL2Tbl;
2031 memset(pL2Entry->paL2Tbl, 0, pImage->cbL2Table);
2032
2033 pL2ClusterAlloc->enmAllocState = QCOWCLUSTERASYNCALLOCSTATE_L2_ALLOC;
2034 pL2ClusterAlloc->offNextClusterOld = offL2Tbl;
2035 pL2ClusterAlloc->offClusterNew = offL2Tbl;
2036 pL2ClusterAlloc->idxL1 = idxL1;
2037 pL2ClusterAlloc->idxL2 = idxL2;
2038 pL2ClusterAlloc->cbToWrite = cbToWrite;
2039 pL2ClusterAlloc->pL2Entry = pL2Entry;
2040
2041 pImage->pL2TblAlloc = pL2Entry;
2042
2043 LogFlowFunc(("Allocating new L2 table at cluster offset %llu\n", offL2Tbl));
2044
2045 /*
2046 * Write the L2 table first and link to the L1 table afterwards.
2047 * If something unexpected happens the worst case which can happen
2048 * is a leak of some clusters.
2049 */
2050 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
2051 offL2Tbl, pL2Entry->paL2Tbl, pImage->cbL2Table, pIoCtx,
2052 qcowAsyncClusterAllocUpdate, pL2ClusterAlloc);
2053 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
2054 break;
2055 else if (RT_FAILURE(rc))
2056 {
2057 RTMemFree(pL2ClusterAlloc);
2058 qcowL2TblCacheEntryFree(pImage, pL2Entry);
2059 break;
2060 }
2061
2062 rc = qcowAsyncClusterAllocUpdate(pImage, pIoCtx, pL2ClusterAlloc, rc);
2063 }
2064 else
2065 {
2066 LogFlowFunc(("Fetching L2 table at cluster offset %llu\n", pImage->paL1Table[idxL1]));
2067
2068 rc = qcowL2TblCacheFetch(pImage, pIoCtx, pImage->paL1Table[idxL1],
2069 &pL2Entry);
2070 if (RT_SUCCESS(rc))
2071 {
2072 PQCOWCLUSTERASYNCALLOC pDataClusterAlloc = NULL;
2073
2074 /* Allocate new async cluster allocation state. */
2075 pDataClusterAlloc = (PQCOWCLUSTERASYNCALLOC)RTMemAllocZ(sizeof(QCOWCLUSTERASYNCALLOC));
2076 if (RT_UNLIKELY(!pDataClusterAlloc))
2077 {
2078 rc = VERR_NO_MEMORY;
2079 break;
2080 }
2081
2082 /* Allocate new cluster for the data. */
2083 uint64_t offData = qcowClusterAllocate(pImage, 1);
2084
2085 pDataClusterAlloc->enmAllocState = QCOWCLUSTERASYNCALLOCSTATE_USER_ALLOC;
2086 pDataClusterAlloc->offNextClusterOld = offData;
2087 pDataClusterAlloc->offClusterNew = offData;
2088 pDataClusterAlloc->idxL1 = idxL1;
2089 pDataClusterAlloc->idxL2 = idxL2;
2090 pDataClusterAlloc->cbToWrite = cbToWrite;
2091 pDataClusterAlloc->pL2Entry = pL2Entry;
2092
2093 /* Write data. */
2094 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
2095 offData, pIoCtx, cbToWrite,
2096 qcowAsyncClusterAllocUpdate, pDataClusterAlloc);
2097 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
2098 break;
2099 else if (RT_FAILURE(rc))
2100 {
2101 RTMemFree(pDataClusterAlloc);
2102 break;
2103 }
2104
2105 rc = qcowAsyncClusterAllocUpdate(pImage, pIoCtx, pDataClusterAlloc, rc);
2106 }
2107 }
2108
2109 } while (0);
2110
2111 *pcbPreRead = 0;
2112 *pcbPostRead = 0;
2113 }
2114 else
2115 {
2116 /* Trying to do a partial write to an unallocated cluster. Don't do
2117 * anything except letting the upper layer know what to do. */
2118 *pcbPreRead = offCluster;
2119 *pcbPostRead = pImage->cbCluster - cbToWrite - *pcbPreRead;
2120 }
2121 }
2122
2123 if (pcbWriteProcess)
2124 *pcbWriteProcess = cbToWrite;
2125 }
2126 else
2127 rc = VERR_VD_IMAGE_READ_ONLY;
2128
2129 LogFlowFunc(("returns %Rrc\n", rc));
2130 return rc;
2131}
2132
2133static DECLCALLBACK(int) qcowFlush(void *pBackendData, PVDIOCTX pIoCtx)
2134{
2135 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2136 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2137 int rc = VINF_SUCCESS;
2138
2139 AssertPtr(pImage);
2140 AssertPtrReturn(pIoCtx, VERR_INVALID_PARAMETER);
2141
2142 if ( pImage->pStorage
2143 && !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2144 {
2145 QCowHeader Header;
2146
2147 rc = qcowTblWrite(pImage, pIoCtx, pImage->offL1Table, pImage->paL1Table,
2148 pImage->cbL1Table, pImage->cL1TableEntries, NULL, NULL);
2149 if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
2150 {
2151 /* Write header. */
2152 size_t cbHeader = 0;
2153 qcowHdrConvertFromHostEndianess(pImage, &Header, &cbHeader);
2154 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
2155 0, &Header, cbHeader,
2156 pIoCtx, NULL, NULL);
2157 if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
2158 rc = vdIfIoIntFileFlush(pImage->pIfIo, pImage->pStorage,
2159 pIoCtx, NULL, NULL);
2160 }
2161 }
2162
2163 LogFlowFunc(("returns %Rrc\n", rc));
2164 return rc;
2165}
2166
2167/** @copydoc VDIMAGEBACKEND::pfnGetVersion */
2168static DECLCALLBACK(unsigned) qcowGetVersion(void *pBackendData)
2169{
2170 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2171 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2172
2173 AssertPtrReturn(pImage, 0);
2174
2175 return pImage->uVersion;
2176}
2177
2178/** @copydoc VDIMAGEBACKEND::pfnGetFileSize */
2179static DECLCALLBACK(uint64_t) qcowGetFileSize(void *pBackendData)
2180{
2181 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2182 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2183 uint64_t cb = 0;
2184
2185 AssertPtrReturn(pImage, 0);
2186
2187 uint64_t cbFile;
2188 if (pImage->pStorage)
2189 {
2190 int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
2191 if (RT_SUCCESS(rc))
2192 cb += cbFile;
2193 }
2194
2195 LogFlowFunc(("returns %lld\n", cb));
2196 return cb;
2197}
2198
2199/** @copydoc VDIMAGEBACKEND::pfnGetPCHSGeometry */
2200static DECLCALLBACK(int) qcowGetPCHSGeometry(void *pBackendData, PVDGEOMETRY pPCHSGeometry)
2201{
2202 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
2203 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2204 int rc = VINF_SUCCESS;
2205
2206 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
2207
2208 if (pImage->PCHSGeometry.cCylinders)
2209 *pPCHSGeometry = pImage->PCHSGeometry;
2210 else
2211 rc = VERR_VD_GEOMETRY_NOT_SET;
2212
2213 LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2214 return rc;
2215}
2216
2217/** @copydoc VDIMAGEBACKEND::pfnSetPCHSGeometry */
2218static DECLCALLBACK(int) qcowSetPCHSGeometry(void *pBackendData, PCVDGEOMETRY pPCHSGeometry)
2219{
2220 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n",
2221 pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2222 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2223 int rc = VINF_SUCCESS;
2224
2225 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
2226
2227 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2228 rc = VERR_VD_IMAGE_READ_ONLY;
2229 else
2230 pImage->PCHSGeometry = *pPCHSGeometry;
2231
2232 LogFlowFunc(("returns %Rrc\n", rc));
2233 return rc;
2234}
2235
2236/** @copydoc VDIMAGEBACKEND::pfnGetLCHSGeometry */
2237static DECLCALLBACK(int) qcowGetLCHSGeometry(void *pBackendData, PVDGEOMETRY pLCHSGeometry)
2238{
2239 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
2240 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2241 int rc = VINF_SUCCESS;
2242
2243 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
2244
2245 if (pImage->LCHSGeometry.cCylinders)
2246 *pLCHSGeometry = pImage->LCHSGeometry;
2247 else
2248 rc = VERR_VD_GEOMETRY_NOT_SET;
2249
2250 LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders,
2251 pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2252 return rc;
2253}
2254
2255/** @copydoc VDIMAGEBACKEND::pfnSetLCHSGeometry */
2256static DECLCALLBACK(int) qcowSetLCHSGeometry(void *pBackendData, PCVDGEOMETRY pLCHSGeometry)
2257{
2258 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData,
2259 pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2260 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2261 int rc = VINF_SUCCESS;
2262
2263 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
2264
2265 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2266 rc = VERR_VD_IMAGE_READ_ONLY;
2267 else
2268 pImage->LCHSGeometry = *pLCHSGeometry;
2269
2270 LogFlowFunc(("returns %Rrc\n", rc));
2271 return rc;
2272}
2273
2274/** @copydoc VDIMAGEBACKEND::pfnQueryRegions */
2275static DECLCALLBACK(int) qcowQueryRegions(void *pBackendData, PCVDREGIONLIST *ppRegionList)
2276{
2277 LogFlowFunc(("pBackendData=%#p ppRegionList=%#p\n", pBackendData, ppRegionList));
2278 PQCOWIMAGE pThis = (PQCOWIMAGE)pBackendData;
2279
2280 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2281
2282 *ppRegionList = &pThis->RegionList;
2283 LogFlowFunc(("returns %Rrc\n", VINF_SUCCESS));
2284 return VINF_SUCCESS;
2285}
2286
2287/** @copydoc VDIMAGEBACKEND::pfnRegionListRelease */
2288static DECLCALLBACK(void) qcowRegionListRelease(void *pBackendData, PCVDREGIONLIST pRegionList)
2289{
2290 RT_NOREF1(pRegionList);
2291 LogFlowFunc(("pBackendData=%#p pRegionList=%#p\n", pBackendData, pRegionList));
2292 PQCOWIMAGE pThis = (PQCOWIMAGE)pBackendData;
2293 AssertPtr(pThis); RT_NOREF(pThis);
2294
2295 /* Nothing to do here. */
2296}
2297
2298/** @copydoc VDIMAGEBACKEND::pfnGetImageFlags */
2299static DECLCALLBACK(unsigned) qcowGetImageFlags(void *pBackendData)
2300{
2301 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2302 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2303
2304 AssertPtrReturn(pImage, 0);
2305
2306 LogFlowFunc(("returns %#x\n", pImage->uImageFlags));
2307 return pImage->uImageFlags;
2308}
2309
2310/** @copydoc VDIMAGEBACKEND::pfnGetOpenFlags */
2311static DECLCALLBACK(unsigned) qcowGetOpenFlags(void *pBackendData)
2312{
2313 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2314 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2315
2316 AssertPtrReturn(pImage, 0);
2317
2318 LogFlowFunc(("returns %#x\n", pImage->uOpenFlags));
2319 return pImage->uOpenFlags;
2320}
2321
2322/** @copydoc VDIMAGEBACKEND::pfnSetOpenFlags */
2323static DECLCALLBACK(int) qcowSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
2324{
2325 LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
2326 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2327 int rc = VINF_SUCCESS;
2328
2329 /* Image must be opened and the new flags must be valid. */
2330 if (!pImage || (uOpenFlags & ~( VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO
2331 | VD_OPEN_FLAGS_ASYNC_IO | VD_OPEN_FLAGS_SHAREABLE
2332 | VD_OPEN_FLAGS_SEQUENTIAL | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)))
2333 rc = VERR_INVALID_PARAMETER;
2334 else
2335 {
2336 /* Implement this operation via reopening the image. */
2337 rc = qcowFreeImage(pImage, false);
2338 if (RT_SUCCESS(rc))
2339 rc = qcowOpenImage(pImage, uOpenFlags);
2340 }
2341
2342 LogFlowFunc(("returns %Rrc\n", rc));
2343 return rc;
2344}
2345
2346/** @copydoc VDIMAGEBACKEND::pfnGetComment */
2347VD_BACKEND_CALLBACK_GET_COMMENT_DEF_NOT_SUPPORTED(qcowGetComment);
2348
2349/** @copydoc VDIMAGEBACKEND::pfnSetComment */
2350VD_BACKEND_CALLBACK_SET_COMMENT_DEF_NOT_SUPPORTED(qcowSetComment, PQCOWIMAGE);
2351
2352/** @copydoc VDIMAGEBACKEND::pfnGetUuid */
2353VD_BACKEND_CALLBACK_GET_UUID_DEF_NOT_SUPPORTED(qcowGetUuid);
2354
2355/** @copydoc VDIMAGEBACKEND::pfnSetUuid */
2356VD_BACKEND_CALLBACK_SET_UUID_DEF_NOT_SUPPORTED(qcowSetUuid, PQCOWIMAGE);
2357
2358/** @copydoc VDIMAGEBACKEND::pfnGetModificationUuid */
2359VD_BACKEND_CALLBACK_GET_UUID_DEF_NOT_SUPPORTED(qcowGetModificationUuid);
2360
2361/** @copydoc VDIMAGEBACKEND::pfnSetModificationUuid */
2362VD_BACKEND_CALLBACK_SET_UUID_DEF_NOT_SUPPORTED(qcowSetModificationUuid, PQCOWIMAGE);
2363
2364/** @copydoc VDIMAGEBACKEND::pfnGetParentUuid */
2365VD_BACKEND_CALLBACK_GET_UUID_DEF_NOT_SUPPORTED(qcowGetParentUuid);
2366
2367/** @copydoc VDIMAGEBACKEND::pfnSetParentUuid */
2368VD_BACKEND_CALLBACK_SET_UUID_DEF_NOT_SUPPORTED(qcowSetParentUuid, PQCOWIMAGE);
2369
2370/** @copydoc VDIMAGEBACKEND::pfnGetParentModificationUuid */
2371VD_BACKEND_CALLBACK_GET_UUID_DEF_NOT_SUPPORTED(qcowGetParentModificationUuid);
2372
2373/** @copydoc VDIMAGEBACKEND::pfnSetParentModificationUuid */
2374VD_BACKEND_CALLBACK_SET_UUID_DEF_NOT_SUPPORTED(qcowSetParentModificationUuid, PQCOWIMAGE);
2375
2376/** @copydoc VDIMAGEBACKEND::pfnDump */
2377static DECLCALLBACK(void) qcowDump(void *pBackendData)
2378{
2379 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2380
2381 AssertPtrReturnVoid(pImage);
2382 vdIfErrorMessage(pImage->pIfError, "Header: Geometry PCHS=%u/%u/%u LCHS=%u/%u/%u cbSector=%llu\n",
2383 pImage->PCHSGeometry.cCylinders, pImage->PCHSGeometry.cHeads, pImage->PCHSGeometry.cSectors,
2384 pImage->LCHSGeometry.cCylinders, pImage->LCHSGeometry.cHeads, pImage->LCHSGeometry.cSectors,
2385 pImage->cbSize / 512);
2386}
2387
2388/** @copydoc VDIMAGEBACKEND::pfnGetParentFilename */
2389static DECLCALLBACK(int) qcowGetParentFilename(void *pBackendData, char **ppszParentFilename)
2390{
2391 int rc = VINF_SUCCESS;
2392 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2393
2394 AssertPtr(pImage);
2395 if (pImage)
2396 if (pImage->pszBackingFilename)
2397 *ppszParentFilename = RTStrDup(pImage->pszBackingFilename);
2398 else
2399 rc = VERR_NOT_SUPPORTED;
2400 else
2401 rc = VERR_VD_NOT_OPENED;
2402
2403 LogFlowFunc(("returns %Rrc\n", rc));
2404 return rc;
2405}
2406
2407/** @copydoc VDIMAGEBACKEND::pfnSetParentFilename */
2408static DECLCALLBACK(int) qcowSetParentFilename(void *pBackendData, const char *pszParentFilename)
2409{
2410 int rc = VINF_SUCCESS;
2411 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2412
2413 AssertPtr(pImage);
2414 if (pImage)
2415 {
2416 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2417 rc = VERR_VD_IMAGE_READ_ONLY;
2418 else if ( pImage->pszBackingFilename
2419 && (strlen(pszParentFilename) > pImage->cbBackingFilename))
2420 rc = VERR_NOT_SUPPORTED; /* The new filename is longer than the old one. */
2421 else
2422 {
2423 if (pImage->pszBackingFilename)
2424 RTStrFree(pImage->pszBackingFilename);
2425 pImage->pszBackingFilename = RTStrDup(pszParentFilename);
2426 if (!pImage->pszBackingFilename)
2427 rc = VERR_NO_STR_MEMORY;
2428 else
2429 {
2430 if (!pImage->offBackingFilename)
2431 {
2432 /* Allocate new cluster. */
2433 uint64_t offData = qcowClusterAllocate(pImage, 1);
2434
2435 Assert((offData & UINT32_MAX) == offData);
2436 pImage->offBackingFilename = (uint32_t)offData;
2437 pImage->cbBackingFilename = (uint32_t)strlen(pszParentFilename);
2438 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage,
2439 offData + pImage->cbCluster);
2440 }
2441
2442 if (RT_SUCCESS(rc))
2443 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
2444 pImage->offBackingFilename,
2445 pImage->pszBackingFilename,
2446 strlen(pImage->pszBackingFilename));
2447 }
2448 }
2449 }
2450 else
2451 rc = VERR_VD_NOT_OPENED;
2452
2453 LogFlowFunc(("returns %Rrc\n", rc));
2454 return rc;
2455}
2456
2457
2458
2459const VDIMAGEBACKEND g_QCowBackend =
2460{
2461 /* u32Version */
2462 VD_IMGBACKEND_VERSION,
2463 /* pszBackendName */
2464 "QCOW",
2465 /* uBackendCaps */
2466 VD_CAP_FILE | VD_CAP_VFS | VD_CAP_CREATE_DYNAMIC | VD_CAP_DIFF | VD_CAP_ASYNC,
2467 /* paFileExtensions */
2468 s_aQCowFileExtensions,
2469 /* paConfigInfo */
2470 NULL,
2471 /* pfnProbe */
2472 qcowProbe,
2473 /* pfnOpen */
2474 qcowOpen,
2475 /* pfnCreate */
2476 qcowCreate,
2477 /* pfnRename */
2478 qcowRename,
2479 /* pfnClose */
2480 qcowClose,
2481 /* pfnRead */
2482 qcowRead,
2483 /* pfnWrite */
2484 qcowWrite,
2485 /* pfnFlush */
2486 qcowFlush,
2487 /* pfnDiscard */
2488 NULL,
2489 /* pfnGetVersion */
2490 qcowGetVersion,
2491 /* pfnGetFileSize */
2492 qcowGetFileSize,
2493 /* pfnGetPCHSGeometry */
2494 qcowGetPCHSGeometry,
2495 /* pfnSetPCHSGeometry */
2496 qcowSetPCHSGeometry,
2497 /* pfnGetLCHSGeometry */
2498 qcowGetLCHSGeometry,
2499 /* pfnSetLCHSGeometry */
2500 qcowSetLCHSGeometry,
2501 /* pfnQueryRegions */
2502 qcowQueryRegions,
2503 /* pfnRegionListRelease */
2504 qcowRegionListRelease,
2505 /* pfnGetImageFlags */
2506 qcowGetImageFlags,
2507 /* pfnGetOpenFlags */
2508 qcowGetOpenFlags,
2509 /* pfnSetOpenFlags */
2510 qcowSetOpenFlags,
2511 /* pfnGetComment */
2512 qcowGetComment,
2513 /* pfnSetComment */
2514 qcowSetComment,
2515 /* pfnGetUuid */
2516 qcowGetUuid,
2517 /* pfnSetUuid */
2518 qcowSetUuid,
2519 /* pfnGetModificationUuid */
2520 qcowGetModificationUuid,
2521 /* pfnSetModificationUuid */
2522 qcowSetModificationUuid,
2523 /* pfnGetParentUuid */
2524 qcowGetParentUuid,
2525 /* pfnSetParentUuid */
2526 qcowSetParentUuid,
2527 /* pfnGetParentModificationUuid */
2528 qcowGetParentModificationUuid,
2529 /* pfnSetParentModificationUuid */
2530 qcowSetParentModificationUuid,
2531 /* pfnDump */
2532 qcowDump,
2533 /* pfnGetTimestamp */
2534 NULL,
2535 /* pfnGetParentTimestamp */
2536 NULL,
2537 /* pfnSetParentTimestamp */
2538 NULL,
2539 /* pfnGetParentFilename */
2540 qcowGetParentFilename,
2541 /* pfnSetParentFilename */
2542 qcowSetParentFilename,
2543 /* pfnComposeLocation */
2544 genericFileComposeLocation,
2545 /* pfnComposeName */
2546 genericFileComposeName,
2547 /* pfnCompact */
2548 NULL,
2549 /* pfnResize */
2550 NULL,
2551 /* pfnRepair */
2552 NULL,
2553 /* pfnTraverseMetadata */
2554 NULL,
2555 /* u32VersionEnd */
2556 VD_IMGBACKEND_VERSION
2557};
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette