VirtualBox

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

Last change on this file since 44529 was 44529, checked in by vboxsync, 12 years ago

header (C) fixes

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