VirtualBox

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

Last change on this file since 92651 was 90802, checked in by vboxsync, 3 years ago

Storage: More VALID_PTR -> RT_VALID_PTR/AssertPtr.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 99.8 KB
Line 
1/* $Id: QCOW.cpp 90802 2021-08-23 19:08:27Z 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 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
1723 AssertReturn(*pszFilename != '\0', VERR_INVALID_PARAMETER);
1724
1725 /*
1726 * Open the file and read the footer.
1727 */
1728 rc = vdIfIoIntFileOpen(pIfIo, pszFilename,
1729 VDOpenFlagsToFileOpenFlags(VD_OPEN_FLAGS_READONLY,
1730 false /* fCreate */),
1731 &pStorage);
1732 if (RT_SUCCESS(rc))
1733 {
1734 rc = vdIfIoIntFileGetSize(pIfIo, pStorage, &cbFile);
1735 if ( RT_SUCCESS(rc)
1736 && cbFile > sizeof(QCowHeader))
1737 {
1738 QCowHeader Header;
1739
1740 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, 0, &Header, sizeof(Header));
1741 if ( RT_SUCCESS(rc)
1742 && qcowHdrConvertToHostEndianess(&Header))
1743 *penmType = VDTYPE_HDD;
1744 else
1745 rc = VERR_VD_GEN_INVALID_HEADER;
1746 }
1747 else
1748 rc = VERR_VD_GEN_INVALID_HEADER;
1749 }
1750
1751 if (pStorage)
1752 vdIfIoIntFileClose(pIfIo, pStorage);
1753
1754 LogFlowFunc(("returns %Rrc\n", rc));
1755 return rc;
1756}
1757
1758/** @copydoc VDIMAGEBACKEND::pfnOpen */
1759static DECLCALLBACK(int) qcowOpen(const char *pszFilename, unsigned uOpenFlags,
1760 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1761 VDTYPE enmType, void **ppBackendData)
1762{
1763 RT_NOREF1(enmType); /**< @todo r=klaus make use of the type info. */
1764
1765 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p enmType=%u ppBackendData=%#p\n",
1766 pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, enmType, ppBackendData));
1767 int rc;
1768
1769 /* Check open flags. All valid flags are supported. */
1770 AssertReturn(!(uOpenFlags & ~VD_OPEN_FLAGS_MASK), VERR_INVALID_PARAMETER);
1771 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
1772 AssertReturn(*pszFilename != '\0', VERR_INVALID_PARAMETER);
1773
1774
1775 PQCOWIMAGE pImage = (PQCOWIMAGE)RTMemAllocZ(RT_UOFFSETOF(QCOWIMAGE, RegionList.aRegions[1]));
1776 if (RT_LIKELY(pImage))
1777 {
1778 pImage->pszFilename = pszFilename;
1779 pImage->pStorage = NULL;
1780 pImage->pVDIfsDisk = pVDIfsDisk;
1781 pImage->pVDIfsImage = pVDIfsImage;
1782
1783 rc = qcowOpenImage(pImage, uOpenFlags);
1784 if (RT_SUCCESS(rc))
1785 *ppBackendData = pImage;
1786 else
1787 RTMemFree(pImage);
1788 }
1789 else
1790 rc = VERR_NO_MEMORY;
1791
1792 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1793 return rc;
1794}
1795
1796/** @copydoc VDIMAGEBACKEND::pfnCreate */
1797static DECLCALLBACK(int) qcowCreate(const char *pszFilename, uint64_t cbSize,
1798 unsigned uImageFlags, const char *pszComment,
1799 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
1800 PCRTUUID pUuid, unsigned uOpenFlags,
1801 unsigned uPercentStart, unsigned uPercentSpan,
1802 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1803 PVDINTERFACE pVDIfsOperation, VDTYPE enmType,
1804 void **ppBackendData)
1805{
1806 RT_NOREF1(pUuid);
1807 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",
1808 pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData));
1809 int rc;
1810
1811 /* Check the VD container type. */
1812 if (enmType != VDTYPE_HDD)
1813 return VERR_VD_INVALID_TYPE;
1814
1815 /* Check open flags. All valid flags are supported. */
1816 AssertReturn(!(uOpenFlags & ~VD_OPEN_FLAGS_MASK), VERR_INVALID_PARAMETER);
1817 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
1818 AssertReturn(*pszFilename != '\0', VERR_INVALID_PARAMETER);
1819 AssertPtrReturn(pPCHSGeometry, VERR_INVALID_POINTER);
1820 AssertPtrReturn(pLCHSGeometry, VERR_INVALID_POINTER);
1821
1822 PQCOWIMAGE pImage = (PQCOWIMAGE)RTMemAllocZ(RT_UOFFSETOF(QCOWIMAGE, RegionList.aRegions[1]));
1823 if (RT_LIKELY(pImage))
1824 {
1825 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
1826
1827 pImage->pszFilename = pszFilename;
1828 pImage->pStorage = NULL;
1829 pImage->pVDIfsDisk = pVDIfsDisk;
1830 pImage->pVDIfsImage = pVDIfsImage;
1831
1832 rc = qcowCreateImage(pImage, cbSize, uImageFlags, pszComment,
1833 pPCHSGeometry, pLCHSGeometry, uOpenFlags,
1834 pIfProgress, uPercentStart, uPercentSpan);
1835 if (RT_SUCCESS(rc))
1836 {
1837 /* So far the image is opened in read/write mode. Make sure the
1838 * image is opened in read-only mode if the caller requested that. */
1839 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
1840 {
1841 qcowFreeImage(pImage, false);
1842 rc = qcowOpenImage(pImage, uOpenFlags);
1843 }
1844
1845 if (RT_SUCCESS(rc))
1846 *ppBackendData = pImage;
1847 }
1848
1849 if (RT_FAILURE(rc))
1850 RTMemFree(pImage);
1851 }
1852 else
1853 rc = VERR_NO_MEMORY;
1854
1855 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1856 return rc;
1857}
1858
1859/** @copydoc VDIMAGEBACKEND::pfnRename */
1860static DECLCALLBACK(int) qcowRename(void *pBackendData, const char *pszFilename)
1861{
1862 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
1863 int rc = VINF_SUCCESS;
1864 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
1865
1866 /* Check arguments. */
1867 AssertReturn((pImage && pszFilename && *pszFilename), VERR_INVALID_PARAMETER);
1868
1869 /* Close the image. */
1870 rc = qcowFreeImage(pImage, false);
1871 if (RT_SUCCESS(rc))
1872 {
1873 /* Rename the file. */
1874 rc = vdIfIoIntFileMove(pImage->pIfIo, pImage->pszFilename, pszFilename, 0);
1875 if (RT_SUCCESS(rc))
1876 {
1877 /* Update pImage with the new information. */
1878 pImage->pszFilename = pszFilename;
1879
1880 /* Open the old image with new name. */
1881 rc = qcowOpenImage(pImage, pImage->uOpenFlags);
1882 }
1883 else
1884 {
1885 /* The move failed, try to reopen the original image. */
1886 int rc2 = qcowOpenImage(pImage, pImage->uOpenFlags);
1887 if (RT_FAILURE(rc2))
1888 rc = rc2;
1889 }
1890 }
1891
1892 LogFlowFunc(("returns %Rrc\n", rc));
1893 return rc;
1894}
1895
1896/** @copydoc VDIMAGEBACKEND::pfnClose */
1897static DECLCALLBACK(int) qcowClose(void *pBackendData, bool fDelete)
1898{
1899 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
1900 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
1901
1902 int rc = qcowFreeImage(pImage, fDelete);
1903 RTMemFree(pImage);
1904
1905 LogFlowFunc(("returns %Rrc\n", rc));
1906 return rc;
1907}
1908
1909static DECLCALLBACK(int) qcowRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
1910 PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
1911{
1912 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
1913 pBackendData, uOffset, pIoCtx, cbToRead, pcbActuallyRead));
1914 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
1915 uint32_t offCluster = 0;
1916 uint32_t idxL1 = 0;
1917 uint32_t idxL2 = 0;
1918 uint64_t offFile = 0;
1919 int rc;
1920
1921 AssertPtr(pImage);
1922 Assert(uOffset % 512 == 0);
1923 Assert(cbToRead % 512 == 0);
1924 AssertPtrReturn(pIoCtx, VERR_INVALID_POINTER);
1925 AssertReturn(cbToRead, VERR_INVALID_PARAMETER);
1926 AssertReturn(uOffset + cbToRead <= pImage->cbSize, VERR_INVALID_PARAMETER);
1927
1928 qcowConvertLogicalOffset(pImage, uOffset, &idxL1, &idxL2, &offCluster);
1929
1930 /* Clip read size to remain in the cluster. */
1931 cbToRead = RT_MIN(cbToRead, pImage->cbCluster - offCluster);
1932
1933 /* Get offset in image. */
1934 bool fCompressedCluster = false;
1935 size_t cbCompressedCluster = 0;
1936 rc = qcowConvertToImageOffset(pImage, pIoCtx, idxL1, idxL2, offCluster,
1937 &offFile, &fCompressedCluster, &cbCompressedCluster);
1938 if (RT_SUCCESS(rc))
1939 {
1940 if (!fCompressedCluster)
1941 rc = vdIfIoIntFileReadUser(pImage->pIfIo, pImage->pStorage, offFile,
1942 pIoCtx, cbToRead);
1943 else
1944 rc = qcowReadCompressedCluster(pImage, pIoCtx, offCluster, cbToRead, offFile, cbCompressedCluster);
1945 }
1946
1947 if ( ( RT_SUCCESS(rc)
1948 || rc == VERR_VD_BLOCK_FREE
1949 || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1950 && pcbActuallyRead)
1951 *pcbActuallyRead = cbToRead;
1952
1953 LogFlowFunc(("returns %Rrc\n", rc));
1954 return rc;
1955}
1956
1957static DECLCALLBACK(int) qcowWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
1958 PVDIOCTX pIoCtx, size_t *pcbWriteProcess, size_t *pcbPreRead,
1959 size_t *pcbPostRead, unsigned fWrite)
1960{
1961 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
1962 pBackendData, uOffset, pIoCtx, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
1963 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
1964 uint32_t offCluster = 0;
1965 uint32_t idxL1 = 0;
1966 uint32_t idxL2 = 0;
1967 uint64_t offImage = 0;
1968 int rc = VINF_SUCCESS;
1969
1970 AssertPtr(pImage);
1971 Assert(!(uOffset % 512));
1972 Assert(!(cbToWrite % 512));
1973 AssertPtrReturn(pIoCtx, VERR_INVALID_POINTER);
1974 AssertReturn(cbToWrite, VERR_INVALID_PARAMETER);
1975 AssertReturn(uOffset + cbToWrite <= pImage->cbSize, VERR_INVALID_PARAMETER);
1976
1977 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1978 {
1979 /* Convert offset to L1, L2 index and cluster offset. */
1980 qcowConvertLogicalOffset(pImage, uOffset, &idxL1, &idxL2, &offCluster);
1981
1982 /* Clip write size to remain in the cluster. */
1983 cbToWrite = RT_MIN(cbToWrite, pImage->cbCluster - offCluster);
1984 Assert(!(cbToWrite % 512));
1985
1986 /* Get offset in image. */
1987 bool fCompressedCluster = false;
1988 size_t cbCompressedCluster = 0;
1989 rc = qcowConvertToImageOffset(pImage, pIoCtx, idxL1, idxL2, offCluster,
1990 &offImage, &fCompressedCluster, &cbCompressedCluster);
1991 if (RT_SUCCESS(rc))
1992 {
1993 if (!fCompressedCluster)
1994 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
1995 offImage, pIoCtx, cbToWrite, NULL, NULL);
1996 else
1997 rc = VERR_NOT_SUPPORTED; /** @todo Support writing compressed clusters */
1998 }
1999 else if (rc == VERR_VD_BLOCK_FREE)
2000 {
2001 if ( cbToWrite == pImage->cbCluster
2002 && !(fWrite & VD_WRITE_NO_ALLOC))
2003 {
2004 PQCOWL2CACHEENTRY pL2Entry = NULL;
2005
2006 /* Full cluster write to previously unallocated cluster.
2007 * Allocate cluster and write data. */
2008 Assert(!offCluster);
2009
2010 do
2011 {
2012 /* Check if we have to allocate a new cluster for L2 tables. */
2013 if (!pImage->paL1Table[idxL1])
2014 {
2015 uint64_t offL2Tbl;
2016 PQCOWCLUSTERASYNCALLOC pL2ClusterAlloc = NULL;
2017
2018 /* Allocate new async cluster allocation state. */
2019 pL2ClusterAlloc = (PQCOWCLUSTERASYNCALLOC)RTMemAllocZ(sizeof(QCOWCLUSTERASYNCALLOC));
2020 if (RT_UNLIKELY(!pL2ClusterAlloc))
2021 {
2022 rc = VERR_NO_MEMORY;
2023 break;
2024 }
2025
2026 pL2Entry = qcowL2TblCacheEntryAlloc(pImage);
2027 if (!pL2Entry)
2028 {
2029 rc = VERR_NO_MEMORY;
2030 RTMemFree(pL2ClusterAlloc);
2031 break;
2032 }
2033
2034 offL2Tbl = qcowClusterAllocate(pImage, qcowByte2Cluster(pImage, pImage->cbL2Table));
2035 pL2Entry->offL2Tbl = offL2Tbl;
2036 memset(pL2Entry->paL2Tbl, 0, pImage->cbL2Table);
2037
2038 pL2ClusterAlloc->enmAllocState = QCOWCLUSTERASYNCALLOCSTATE_L2_ALLOC;
2039 pL2ClusterAlloc->offNextClusterOld = offL2Tbl;
2040 pL2ClusterAlloc->offClusterNew = offL2Tbl;
2041 pL2ClusterAlloc->idxL1 = idxL1;
2042 pL2ClusterAlloc->idxL2 = idxL2;
2043 pL2ClusterAlloc->cbToWrite = cbToWrite;
2044 pL2ClusterAlloc->pL2Entry = pL2Entry;
2045
2046 pImage->pL2TblAlloc = pL2Entry;
2047
2048 LogFlowFunc(("Allocating new L2 table at cluster offset %llu\n", offL2Tbl));
2049
2050 /*
2051 * Write the L2 table first and link to the L1 table afterwards.
2052 * If something unexpected happens the worst case which can happen
2053 * is a leak of some clusters.
2054 */
2055 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
2056 offL2Tbl, pL2Entry->paL2Tbl, pImage->cbL2Table, pIoCtx,
2057 qcowAsyncClusterAllocUpdate, pL2ClusterAlloc);
2058 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
2059 break;
2060 else if (RT_FAILURE(rc))
2061 {
2062 RTMemFree(pL2ClusterAlloc);
2063 qcowL2TblCacheEntryFree(pImage, pL2Entry);
2064 break;
2065 }
2066
2067 rc = qcowAsyncClusterAllocUpdate(pImage, pIoCtx, pL2ClusterAlloc, rc);
2068 }
2069 else
2070 {
2071 LogFlowFunc(("Fetching L2 table at cluster offset %llu\n", pImage->paL1Table[idxL1]));
2072
2073 rc = qcowL2TblCacheFetch(pImage, pIoCtx, pImage->paL1Table[idxL1],
2074 &pL2Entry);
2075 if (RT_SUCCESS(rc))
2076 {
2077 PQCOWCLUSTERASYNCALLOC pDataClusterAlloc = NULL;
2078
2079 /* Allocate new async cluster allocation state. */
2080 pDataClusterAlloc = (PQCOWCLUSTERASYNCALLOC)RTMemAllocZ(sizeof(QCOWCLUSTERASYNCALLOC));
2081 if (RT_UNLIKELY(!pDataClusterAlloc))
2082 {
2083 rc = VERR_NO_MEMORY;
2084 break;
2085 }
2086
2087 /* Allocate new cluster for the data. */
2088 uint64_t offData = qcowClusterAllocate(pImage, 1);
2089
2090 pDataClusterAlloc->enmAllocState = QCOWCLUSTERASYNCALLOCSTATE_USER_ALLOC;
2091 pDataClusterAlloc->offNextClusterOld = offData;
2092 pDataClusterAlloc->offClusterNew = offData;
2093 pDataClusterAlloc->idxL1 = idxL1;
2094 pDataClusterAlloc->idxL2 = idxL2;
2095 pDataClusterAlloc->cbToWrite = cbToWrite;
2096 pDataClusterAlloc->pL2Entry = pL2Entry;
2097
2098 /* Write data. */
2099 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
2100 offData, pIoCtx, cbToWrite,
2101 qcowAsyncClusterAllocUpdate, pDataClusterAlloc);
2102 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
2103 break;
2104 else if (RT_FAILURE(rc))
2105 {
2106 RTMemFree(pDataClusterAlloc);
2107 break;
2108 }
2109
2110 rc = qcowAsyncClusterAllocUpdate(pImage, pIoCtx, pDataClusterAlloc, rc);
2111 }
2112 }
2113
2114 } while (0);
2115
2116 *pcbPreRead = 0;
2117 *pcbPostRead = 0;
2118 }
2119 else
2120 {
2121 /* Trying to do a partial write to an unallocated cluster. Don't do
2122 * anything except letting the upper layer know what to do. */
2123 *pcbPreRead = offCluster;
2124 *pcbPostRead = pImage->cbCluster - cbToWrite - *pcbPreRead;
2125 }
2126 }
2127
2128 if (pcbWriteProcess)
2129 *pcbWriteProcess = cbToWrite;
2130 }
2131 else
2132 rc = VERR_VD_IMAGE_READ_ONLY;
2133
2134 LogFlowFunc(("returns %Rrc\n", rc));
2135 return rc;
2136}
2137
2138static DECLCALLBACK(int) qcowFlush(void *pBackendData, PVDIOCTX pIoCtx)
2139{
2140 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2141 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2142 int rc = VINF_SUCCESS;
2143
2144 AssertPtr(pImage);
2145 AssertPtrReturn(pIoCtx, VERR_INVALID_PARAMETER);
2146
2147 if ( pImage->pStorage
2148 && !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2149 {
2150 QCowHeader Header;
2151
2152 rc = qcowTblWrite(pImage, pIoCtx, pImage->offL1Table, pImage->paL1Table,
2153 pImage->cbL1Table, pImage->cL1TableEntries, NULL, NULL);
2154 if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
2155 {
2156 /* Write header. */
2157 size_t cbHeader = 0;
2158 qcowHdrConvertFromHostEndianess(pImage, &Header, &cbHeader);
2159 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
2160 0, &Header, cbHeader,
2161 pIoCtx, NULL, NULL);
2162 if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
2163 rc = vdIfIoIntFileFlush(pImage->pIfIo, pImage->pStorage,
2164 pIoCtx, NULL, NULL);
2165 }
2166 }
2167
2168 LogFlowFunc(("returns %Rrc\n", rc));
2169 return rc;
2170}
2171
2172/** @copydoc VDIMAGEBACKEND::pfnGetVersion */
2173static DECLCALLBACK(unsigned) qcowGetVersion(void *pBackendData)
2174{
2175 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2176 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2177
2178 AssertPtrReturn(pImage, 0);
2179
2180 return pImage->uVersion;
2181}
2182
2183/** @copydoc VDIMAGEBACKEND::pfnGetFileSize */
2184static DECLCALLBACK(uint64_t) qcowGetFileSize(void *pBackendData)
2185{
2186 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2187 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2188 uint64_t cb = 0;
2189
2190 AssertPtrReturn(pImage, 0);
2191
2192 uint64_t cbFile;
2193 if (pImage->pStorage)
2194 {
2195 int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
2196 if (RT_SUCCESS(rc))
2197 cb += cbFile;
2198 }
2199
2200 LogFlowFunc(("returns %lld\n", cb));
2201 return cb;
2202}
2203
2204/** @copydoc VDIMAGEBACKEND::pfnGetPCHSGeometry */
2205static DECLCALLBACK(int) qcowGetPCHSGeometry(void *pBackendData, PVDGEOMETRY pPCHSGeometry)
2206{
2207 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
2208 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2209 int rc = VINF_SUCCESS;
2210
2211 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
2212
2213 if (pImage->PCHSGeometry.cCylinders)
2214 *pPCHSGeometry = pImage->PCHSGeometry;
2215 else
2216 rc = VERR_VD_GEOMETRY_NOT_SET;
2217
2218 LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2219 return rc;
2220}
2221
2222/** @copydoc VDIMAGEBACKEND::pfnSetPCHSGeometry */
2223static DECLCALLBACK(int) qcowSetPCHSGeometry(void *pBackendData, PCVDGEOMETRY pPCHSGeometry)
2224{
2225 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n",
2226 pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2227 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2228 int rc = VINF_SUCCESS;
2229
2230 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
2231
2232 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2233 rc = VERR_VD_IMAGE_READ_ONLY;
2234 else
2235 pImage->PCHSGeometry = *pPCHSGeometry;
2236
2237 LogFlowFunc(("returns %Rrc\n", rc));
2238 return rc;
2239}
2240
2241/** @copydoc VDIMAGEBACKEND::pfnGetLCHSGeometry */
2242static DECLCALLBACK(int) qcowGetLCHSGeometry(void *pBackendData, PVDGEOMETRY pLCHSGeometry)
2243{
2244 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
2245 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2246 int rc = VINF_SUCCESS;
2247
2248 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
2249
2250 if (pImage->LCHSGeometry.cCylinders)
2251 *pLCHSGeometry = pImage->LCHSGeometry;
2252 else
2253 rc = VERR_VD_GEOMETRY_NOT_SET;
2254
2255 LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders,
2256 pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2257 return rc;
2258}
2259
2260/** @copydoc VDIMAGEBACKEND::pfnSetLCHSGeometry */
2261static DECLCALLBACK(int) qcowSetLCHSGeometry(void *pBackendData, PCVDGEOMETRY pLCHSGeometry)
2262{
2263 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData,
2264 pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2265 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2266 int rc = VINF_SUCCESS;
2267
2268 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
2269
2270 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2271 rc = VERR_VD_IMAGE_READ_ONLY;
2272 else
2273 pImage->LCHSGeometry = *pLCHSGeometry;
2274
2275 LogFlowFunc(("returns %Rrc\n", rc));
2276 return rc;
2277}
2278
2279/** @copydoc VDIMAGEBACKEND::pfnQueryRegions */
2280static DECLCALLBACK(int) qcowQueryRegions(void *pBackendData, PCVDREGIONLIST *ppRegionList)
2281{
2282 LogFlowFunc(("pBackendData=%#p ppRegionList=%#p\n", pBackendData, ppRegionList));
2283 PQCOWIMAGE pThis = (PQCOWIMAGE)pBackendData;
2284
2285 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2286
2287 *ppRegionList = &pThis->RegionList;
2288 LogFlowFunc(("returns %Rrc\n", VINF_SUCCESS));
2289 return VINF_SUCCESS;
2290}
2291
2292/** @copydoc VDIMAGEBACKEND::pfnRegionListRelease */
2293static DECLCALLBACK(void) qcowRegionListRelease(void *pBackendData, PCVDREGIONLIST pRegionList)
2294{
2295 RT_NOREF1(pRegionList);
2296 LogFlowFunc(("pBackendData=%#p pRegionList=%#p\n", pBackendData, pRegionList));
2297 PQCOWIMAGE pThis = (PQCOWIMAGE)pBackendData;
2298 AssertPtr(pThis); RT_NOREF(pThis);
2299
2300 /* Nothing to do here. */
2301}
2302
2303/** @copydoc VDIMAGEBACKEND::pfnGetImageFlags */
2304static DECLCALLBACK(unsigned) qcowGetImageFlags(void *pBackendData)
2305{
2306 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2307 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2308
2309 AssertPtrReturn(pImage, 0);
2310
2311 LogFlowFunc(("returns %#x\n", pImage->uImageFlags));
2312 return pImage->uImageFlags;
2313}
2314
2315/** @copydoc VDIMAGEBACKEND::pfnGetOpenFlags */
2316static DECLCALLBACK(unsigned) qcowGetOpenFlags(void *pBackendData)
2317{
2318 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2319 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2320
2321 AssertPtrReturn(pImage, 0);
2322
2323 LogFlowFunc(("returns %#x\n", pImage->uOpenFlags));
2324 return pImage->uOpenFlags;
2325}
2326
2327/** @copydoc VDIMAGEBACKEND::pfnSetOpenFlags */
2328static DECLCALLBACK(int) qcowSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
2329{
2330 LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
2331 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2332 int rc = VINF_SUCCESS;
2333
2334 /* Image must be opened and the new flags must be valid. */
2335 if (!pImage || (uOpenFlags & ~( VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO
2336 | VD_OPEN_FLAGS_ASYNC_IO | VD_OPEN_FLAGS_SHAREABLE
2337 | VD_OPEN_FLAGS_SEQUENTIAL | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)))
2338 rc = VERR_INVALID_PARAMETER;
2339 else
2340 {
2341 /* Implement this operation via reopening the image. */
2342 rc = qcowFreeImage(pImage, false);
2343 if (RT_SUCCESS(rc))
2344 rc = qcowOpenImage(pImage, uOpenFlags);
2345 }
2346
2347 LogFlowFunc(("returns %Rrc\n", rc));
2348 return rc;
2349}
2350
2351/** @copydoc VDIMAGEBACKEND::pfnGetComment */
2352VD_BACKEND_CALLBACK_GET_COMMENT_DEF_NOT_SUPPORTED(qcowGetComment);
2353
2354/** @copydoc VDIMAGEBACKEND::pfnSetComment */
2355VD_BACKEND_CALLBACK_SET_COMMENT_DEF_NOT_SUPPORTED(qcowSetComment, PQCOWIMAGE);
2356
2357/** @copydoc VDIMAGEBACKEND::pfnGetUuid */
2358VD_BACKEND_CALLBACK_GET_UUID_DEF_NOT_SUPPORTED(qcowGetUuid);
2359
2360/** @copydoc VDIMAGEBACKEND::pfnSetUuid */
2361VD_BACKEND_CALLBACK_SET_UUID_DEF_NOT_SUPPORTED(qcowSetUuid, PQCOWIMAGE);
2362
2363/** @copydoc VDIMAGEBACKEND::pfnGetModificationUuid */
2364VD_BACKEND_CALLBACK_GET_UUID_DEF_NOT_SUPPORTED(qcowGetModificationUuid);
2365
2366/** @copydoc VDIMAGEBACKEND::pfnSetModificationUuid */
2367VD_BACKEND_CALLBACK_SET_UUID_DEF_NOT_SUPPORTED(qcowSetModificationUuid, PQCOWIMAGE);
2368
2369/** @copydoc VDIMAGEBACKEND::pfnGetParentUuid */
2370VD_BACKEND_CALLBACK_GET_UUID_DEF_NOT_SUPPORTED(qcowGetParentUuid);
2371
2372/** @copydoc VDIMAGEBACKEND::pfnSetParentUuid */
2373VD_BACKEND_CALLBACK_SET_UUID_DEF_NOT_SUPPORTED(qcowSetParentUuid, PQCOWIMAGE);
2374
2375/** @copydoc VDIMAGEBACKEND::pfnGetParentModificationUuid */
2376VD_BACKEND_CALLBACK_GET_UUID_DEF_NOT_SUPPORTED(qcowGetParentModificationUuid);
2377
2378/** @copydoc VDIMAGEBACKEND::pfnSetParentModificationUuid */
2379VD_BACKEND_CALLBACK_SET_UUID_DEF_NOT_SUPPORTED(qcowSetParentModificationUuid, PQCOWIMAGE);
2380
2381/** @copydoc VDIMAGEBACKEND::pfnDump */
2382static DECLCALLBACK(void) qcowDump(void *pBackendData)
2383{
2384 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2385
2386 AssertPtrReturnVoid(pImage);
2387 vdIfErrorMessage(pImage->pIfError, "Header: Geometry PCHS=%u/%u/%u LCHS=%u/%u/%u cbSector=%llu\n",
2388 pImage->PCHSGeometry.cCylinders, pImage->PCHSGeometry.cHeads, pImage->PCHSGeometry.cSectors,
2389 pImage->LCHSGeometry.cCylinders, pImage->LCHSGeometry.cHeads, pImage->LCHSGeometry.cSectors,
2390 pImage->cbSize / 512);
2391}
2392
2393/** @copydoc VDIMAGEBACKEND::pfnGetParentFilename */
2394static DECLCALLBACK(int) qcowGetParentFilename(void *pBackendData, char **ppszParentFilename)
2395{
2396 int rc = VINF_SUCCESS;
2397 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2398
2399 AssertPtr(pImage);
2400 if (pImage)
2401 if (pImage->pszBackingFilename)
2402 *ppszParentFilename = RTStrDup(pImage->pszBackingFilename);
2403 else
2404 rc = VERR_NOT_SUPPORTED;
2405 else
2406 rc = VERR_VD_NOT_OPENED;
2407
2408 LogFlowFunc(("returns %Rrc\n", rc));
2409 return rc;
2410}
2411
2412/** @copydoc VDIMAGEBACKEND::pfnSetParentFilename */
2413static DECLCALLBACK(int) qcowSetParentFilename(void *pBackendData, const char *pszParentFilename)
2414{
2415 int rc = VINF_SUCCESS;
2416 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2417
2418 AssertPtr(pImage);
2419 if (pImage)
2420 {
2421 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2422 rc = VERR_VD_IMAGE_READ_ONLY;
2423 else if ( pImage->pszBackingFilename
2424 && (strlen(pszParentFilename) > pImage->cbBackingFilename))
2425 rc = VERR_NOT_SUPPORTED; /* The new filename is longer than the old one. */
2426 else
2427 {
2428 if (pImage->pszBackingFilename)
2429 RTStrFree(pImage->pszBackingFilename);
2430 pImage->pszBackingFilename = RTStrDup(pszParentFilename);
2431 if (!pImage->pszBackingFilename)
2432 rc = VERR_NO_STR_MEMORY;
2433 else
2434 {
2435 if (!pImage->offBackingFilename)
2436 {
2437 /* Allocate new cluster. */
2438 uint64_t offData = qcowClusterAllocate(pImage, 1);
2439
2440 Assert((offData & UINT32_MAX) == offData);
2441 pImage->offBackingFilename = (uint32_t)offData;
2442 pImage->cbBackingFilename = (uint32_t)strlen(pszParentFilename);
2443 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage,
2444 offData + pImage->cbCluster);
2445 }
2446
2447 if (RT_SUCCESS(rc))
2448 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
2449 pImage->offBackingFilename,
2450 pImage->pszBackingFilename,
2451 strlen(pImage->pszBackingFilename));
2452 }
2453 }
2454 }
2455 else
2456 rc = VERR_VD_NOT_OPENED;
2457
2458 LogFlowFunc(("returns %Rrc\n", rc));
2459 return rc;
2460}
2461
2462
2463
2464const VDIMAGEBACKEND g_QCowBackend =
2465{
2466 /* u32Version */
2467 VD_IMGBACKEND_VERSION,
2468 /* pszBackendName */
2469 "QCOW",
2470 /* uBackendCaps */
2471 VD_CAP_FILE | VD_CAP_VFS | VD_CAP_CREATE_DYNAMIC | VD_CAP_DIFF | VD_CAP_ASYNC,
2472 /* paFileExtensions */
2473 s_aQCowFileExtensions,
2474 /* paConfigInfo */
2475 NULL,
2476 /* pfnProbe */
2477 qcowProbe,
2478 /* pfnOpen */
2479 qcowOpen,
2480 /* pfnCreate */
2481 qcowCreate,
2482 /* pfnRename */
2483 qcowRename,
2484 /* pfnClose */
2485 qcowClose,
2486 /* pfnRead */
2487 qcowRead,
2488 /* pfnWrite */
2489 qcowWrite,
2490 /* pfnFlush */
2491 qcowFlush,
2492 /* pfnDiscard */
2493 NULL,
2494 /* pfnGetVersion */
2495 qcowGetVersion,
2496 /* pfnGetFileSize */
2497 qcowGetFileSize,
2498 /* pfnGetPCHSGeometry */
2499 qcowGetPCHSGeometry,
2500 /* pfnSetPCHSGeometry */
2501 qcowSetPCHSGeometry,
2502 /* pfnGetLCHSGeometry */
2503 qcowGetLCHSGeometry,
2504 /* pfnSetLCHSGeometry */
2505 qcowSetLCHSGeometry,
2506 /* pfnQueryRegions */
2507 qcowQueryRegions,
2508 /* pfnRegionListRelease */
2509 qcowRegionListRelease,
2510 /* pfnGetImageFlags */
2511 qcowGetImageFlags,
2512 /* pfnGetOpenFlags */
2513 qcowGetOpenFlags,
2514 /* pfnSetOpenFlags */
2515 qcowSetOpenFlags,
2516 /* pfnGetComment */
2517 qcowGetComment,
2518 /* pfnSetComment */
2519 qcowSetComment,
2520 /* pfnGetUuid */
2521 qcowGetUuid,
2522 /* pfnSetUuid */
2523 qcowSetUuid,
2524 /* pfnGetModificationUuid */
2525 qcowGetModificationUuid,
2526 /* pfnSetModificationUuid */
2527 qcowSetModificationUuid,
2528 /* pfnGetParentUuid */
2529 qcowGetParentUuid,
2530 /* pfnSetParentUuid */
2531 qcowSetParentUuid,
2532 /* pfnGetParentModificationUuid */
2533 qcowGetParentModificationUuid,
2534 /* pfnSetParentModificationUuid */
2535 qcowSetParentModificationUuid,
2536 /* pfnDump */
2537 qcowDump,
2538 /* pfnGetTimestamp */
2539 NULL,
2540 /* pfnGetParentTimestamp */
2541 NULL,
2542 /* pfnSetParentTimestamp */
2543 NULL,
2544 /* pfnGetParentFilename */
2545 qcowGetParentFilename,
2546 /* pfnSetParentFilename */
2547 qcowSetParentFilename,
2548 /* pfnComposeLocation */
2549 genericFileComposeLocation,
2550 /* pfnComposeName */
2551 genericFileComposeName,
2552 /* pfnCompact */
2553 NULL,
2554 /* pfnResize */
2555 NULL,
2556 /* pfnRepair */
2557 NULL,
2558 /* pfnTraverseMetadata */
2559 NULL,
2560 /* u32VersionEnd */
2561 VD_IMGBACKEND_VERSION
2562};
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