VirtualBox

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

Last change on this file since 96973 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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