VirtualBox

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

Last change on this file since 42640 was 40106, checked in by vboxsync, 13 years ago

Storage/QCOW: Missing break

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 95.8 KB
Line 
1/* $Id: QCOW.cpp 40106 2012-02-13 18:48:25Z vboxsync $ */
2/** @file
3 * QCOW - QCOW Disk image.
4 */
5
6/*
7 * Copyright (C) 2011 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 offL2Tbl The offset of the L2 table in the image.
687 * @param ppL2Entry Where to store the L2 table on success.
688 */
689static int qcowL2TblCacheFetch(PQCOWIMAGE pImage, uint64_t offL2Tbl, PQCOWL2CACHEENTRY *ppL2Entry)
690{
691 int rc = VINF_SUCCESS;
692
693 LogFlowFunc(("pImage=%#p offL2Tbl=%llu ppL2Entry=%#p\n", pImage, offL2Tbl, ppL2Entry));
694
695 /* Try to fetch the L2 table from the cache first. */
696 PQCOWL2CACHEENTRY pL2Entry = qcowL2TblCacheRetain(pImage, offL2Tbl);
697 if (!pL2Entry)
698 {
699 LogFlowFunc(("Reading L2 table from image\n"));
700 pL2Entry = qcowL2TblCacheEntryAlloc(pImage);
701
702 if (pL2Entry)
703 {
704 /* Read from the image. */
705 pL2Entry->offL2Tbl = offL2Tbl;
706 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, offL2Tbl,
707 pL2Entry->paL2Tbl, pImage->cbL2Table, NULL);
708 if (RT_SUCCESS(rc))
709 {
710#if defined(RT_LITTLE_ENDIAN)
711 qcowTableConvertToHostEndianess(pL2Entry->paL2Tbl, pImage->cL2TableEntries);
712#endif
713 qcowL2TblCacheEntryInsert(pImage, pL2Entry);
714 }
715 else
716 {
717 qcowL2TblCacheEntryRelease(pL2Entry);
718 qcowL2TblCacheEntryFree(pImage, pL2Entry);
719 }
720 }
721 else
722 rc = VERR_NO_MEMORY;
723 }
724
725 if (RT_SUCCESS(rc))
726 *ppL2Entry = pL2Entry;
727
728 LogFlowFunc(("returns rc=%Rrc\n", rc));
729 return rc;
730}
731
732/**
733 * Fetches the L2 from the given offset trying the LRU cache first and
734 * reading it from the image after a cache miss - version for async I/O.
735 *
736 * @returns VBox status code.
737 * @param pImage Image instance data.
738 * @param pIoCtx The I/O context.
739 * @param offL2Tbl The offset of the L2 table in the image.
740 * @param ppL2Entry Where to store the L2 table on success.
741 */
742static int qcowL2TblCacheFetchAsync(PQCOWIMAGE pImage, PVDIOCTX pIoCtx,
743 uint64_t offL2Tbl, PQCOWL2CACHEENTRY *ppL2Entry)
744{
745 int rc = VINF_SUCCESS;
746
747 /* Try to fetch the L2 table from the cache first. */
748 PQCOWL2CACHEENTRY pL2Entry = qcowL2TblCacheRetain(pImage, offL2Tbl);
749 if (!pL2Entry)
750 {
751 pL2Entry = qcowL2TblCacheEntryAlloc(pImage);
752
753 if (pL2Entry)
754 {
755 /* Read from the image. */
756 PVDMETAXFER pMetaXfer;
757
758 pL2Entry->offL2Tbl = offL2Tbl;
759 rc = vdIfIoIntFileReadMetaAsync(pImage->pIfIo, pImage->pStorage,
760 offL2Tbl, pL2Entry->paL2Tbl,
761 pImage->cbL2Table, pIoCtx,
762 &pMetaXfer, NULL, NULL);
763 if (RT_SUCCESS(rc))
764 {
765 vdIfIoIntMetaXferRelease(pImage->pIfIo, pMetaXfer);
766#if defined(RT_LITTLE_ENDIAN)
767 qcowTableConvertToHostEndianess(pL2Entry->paL2Tbl, pImage->cL2TableEntries);
768#endif
769 qcowL2TblCacheEntryInsert(pImage, pL2Entry);
770 }
771 else
772 {
773 qcowL2TblCacheEntryRelease(pL2Entry);
774 qcowL2TblCacheEntryFree(pImage, pL2Entry);
775 }
776 }
777 else
778 rc = VERR_NO_MEMORY;
779 }
780
781 if (RT_SUCCESS(rc))
782 *ppL2Entry = pL2Entry;
783
784 return rc;
785}
786
787/**
788 * Sets the L1, L2 and offset bitmasks and L1 and L2 bit shift members.
789 *
790 * @returns nothing.
791 * @param pImage The image instance data.
792 */
793static void qcowTableMasksInit(PQCOWIMAGE pImage)
794{
795 uint32_t cClusterBits, cL2TableBits;
796
797 cClusterBits = qcowGetPowerOfTwo(pImage->cbCluster);
798 cL2TableBits = qcowGetPowerOfTwo(pImage->cL2TableEntries);
799
800 Assert(cClusterBits + cL2TableBits < 64);
801
802 pImage->fOffsetMask = ((uint64_t)pImage->cbCluster - 1);
803 pImage->fL2Mask = ((uint64_t)pImage->cL2TableEntries - 1) << cClusterBits;
804 pImage->cL2Shift = cClusterBits;
805 pImage->cL1Shift = cClusterBits + cL2TableBits;
806}
807
808/**
809 * Converts a given logical offset into the
810 *
811 * @returns nothing.
812 * @param pImage The image instance data.
813 * @param off The logical offset to convert.
814 * @param pidxL1 Where to store the index in the L1 table on success.
815 * @param pidxL2 Where to store the index in the L2 table on success.
816 * @param poffCluster Where to store the offset in the cluster on success.
817 */
818DECLINLINE(void) qcowConvertLogicalOffset(PQCOWIMAGE pImage, uint64_t off, uint32_t *pidxL1,
819 uint32_t *pidxL2, uint32_t *poffCluster)
820{
821 AssertPtr(pidxL1);
822 AssertPtr(pidxL2);
823 AssertPtr(poffCluster);
824
825 *poffCluster = off & pImage->fOffsetMask;
826 *pidxL1 = off >> pImage->cL1Shift;
827 *pidxL2 = (off & pImage->fL2Mask) >> pImage->cL2Shift;
828}
829
830/**
831 * Converts Cluster size to a byte size.
832 *
833 * @returns Number of bytes derived from the given number of clusters.
834 * @param pImage The image instance data.
835 * @param cClusters The clusters to convert.
836 */
837DECLINLINE(uint64_t) qcowCluster2Byte(PQCOWIMAGE pImage, uint64_t cClusters)
838{
839 return cClusters * pImage->cbCluster;
840}
841
842/**
843 * Converts number of bytes to cluster size rounding to the next cluster.
844 *
845 * @returns Number of bytes derived from the given number of clusters.
846 * @param pImage The image instance data.
847 * @param cb Number of bytes to convert.
848 */
849DECLINLINE(uint64_t) qcowByte2Cluster(PQCOWIMAGE pImage, uint64_t cb)
850{
851 return cb / pImage->cbCluster + (cb % pImage->cbCluster ? 1 : 0);
852}
853
854/**
855 * Allocates a new cluster in the image.
856 *
857 * @returns The start offset of the new cluster in the image.
858 * @param pImage The image instance data.
859 * @param cCLusters Number of clusters to allocate.
860 */
861DECLINLINE(uint64_t) qcowClusterAllocate(PQCOWIMAGE pImage, uint32_t cClusters)
862{
863 uint64_t offCluster;
864
865 offCluster = pImage->offNextCluster;
866 pImage->offNextCluster += cClusters*pImage->cbCluster;
867
868 return offCluster;
869}
870
871/**
872 * Returns the real image offset for a given cluster or an error if the cluster is not
873 * yet allocated.
874 *
875 * @returns VBox status code.
876 * VERR_VD_BLOCK_FREE if the cluster is not yet allocated.
877 * @param pImage The image instance data.
878 * @param idxL1 The L1 index.
879 * @param idxL2 The L2 index.
880 * @param offCluster Offset inside the cluster.
881 * @param poffImage Where to store the image offset on success;
882 */
883static int qcowConvertToImageOffset(PQCOWIMAGE pImage, uint32_t idxL1, uint32_t idxL2,
884 uint32_t offCluster, uint64_t *poffImage)
885{
886 int rc = VERR_VD_BLOCK_FREE;
887 LogFlowFunc(("pImage=%#p idxL1=%u idxL2=%u offCluster=%u poffImage=%#p\n",
888 pImage, idxL1, idxL2, offCluster, poffImage));
889
890 AssertReturn(idxL1 < pImage->cL1TableEntries, VERR_INVALID_PARAMETER);
891 AssertReturn(idxL2 < pImage->cL2TableEntries, VERR_INVALID_PARAMETER);
892
893 if (pImage->paL1Table[idxL1])
894 {
895 PQCOWL2CACHEENTRY pL2Entry;
896
897 rc = qcowL2TblCacheFetch(pImage, pImage->paL1Table[idxL1], &pL2Entry);
898 if (RT_SUCCESS(rc))
899 {
900 LogFlowFunc(("cluster start offset %llu\n", pL2Entry->paL2Tbl[idxL2]));
901 /* Get real file offset. */
902 if (pL2Entry->paL2Tbl[idxL2])
903 {
904 uint64_t off = pL2Entry->paL2Tbl[idxL2];
905
906 /* Strip flags */
907 if (pImage->uVersion == 2)
908 {
909 if (RT_UNLIKELY(off & QCOW_V2_COMPRESSED_FLAG))
910 rc = VERR_NOT_SUPPORTED;
911 else
912 off &= ~(QCOW_V2_COMPRESSED_FLAG | QCOW_V2_COPIED_FLAG);
913 }
914 else
915 {
916 if (RT_UNLIKELY(off & QCOW_V1_COMPRESSED_FLAG))
917 rc = VERR_NOT_SUPPORTED;
918 else
919 off &= ~QCOW_V1_COMPRESSED_FLAG;
920 }
921
922 *poffImage = off + offCluster;
923 }
924 else
925 rc = VERR_VD_BLOCK_FREE;
926
927 qcowL2TblCacheEntryRelease(pL2Entry);
928 }
929 }
930
931 LogFlowFunc(("returns rc=%Rrc\n", rc));
932 return rc;
933}
934
935/**
936 * Returns the real image offset for a given cluster or an error if the cluster is not
937 * yet allocated- version for async I/O.
938 *
939 * @returns VBox status code.
940 * VERR_VD_BLOCK_FREE if the cluster is not yet allocated.
941 * @param pImage The image instance data.
942 * @param pIoCtx The I/O context.
943 * @param idxL1 The L1 index.
944 * @param idxL2 The L2 index.
945 * @param offCluster Offset inside the cluster.
946 * @param poffImage Where to store the image offset on success;
947 */
948static int qcowConvertToImageOffsetAsync(PQCOWIMAGE pImage, PVDIOCTX pIoCtx,
949 uint32_t idxL1, uint32_t idxL2,
950 uint32_t offCluster, uint64_t *poffImage)
951{
952 int rc = VERR_VD_BLOCK_FREE;
953
954 AssertReturn(idxL1 < pImage->cL1TableEntries, VERR_INVALID_PARAMETER);
955 AssertReturn(idxL2 < pImage->cL2TableEntries, VERR_INVALID_PARAMETER);
956
957 if (pImage->paL1Table[idxL1])
958 {
959 PQCOWL2CACHEENTRY pL2Entry;
960
961 rc = qcowL2TblCacheFetchAsync(pImage, pIoCtx, pImage->paL1Table[idxL1],
962 &pL2Entry);
963 if (RT_SUCCESS(rc))
964 {
965 /* Get real file offset. */
966 if (pL2Entry->paL2Tbl[idxL2])
967 {
968 uint64_t off = pL2Entry->paL2Tbl[idxL2];
969
970 /* Strip flags */
971 if (pImage->uVersion == 2)
972 {
973 if (RT_UNLIKELY(off & QCOW_V2_COMPRESSED_FLAG))
974 rc = VERR_NOT_SUPPORTED;
975 else
976 off &= ~(QCOW_V2_COMPRESSED_FLAG | QCOW_V2_COPIED_FLAG);
977 }
978 else
979 {
980 if (RT_UNLIKELY(off & QCOW_V1_COMPRESSED_FLAG))
981 rc = VERR_NOT_SUPPORTED;
982 else
983 off &= ~QCOW_V1_COMPRESSED_FLAG;
984 }
985
986 *poffImage = off + offCluster;
987 }
988 else
989 rc = VERR_VD_BLOCK_FREE;
990
991 qcowL2TblCacheEntryRelease(pL2Entry);
992 }
993 }
994
995 return rc;
996}
997
998
999/**
1000 * Internal. Flush image data to disk.
1001 */
1002static int qcowFlushImage(PQCOWIMAGE pImage)
1003{
1004 int rc = VINF_SUCCESS;
1005
1006 if ( pImage->pStorage
1007 && !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1008 && pImage->cbL1Table)
1009 {
1010 QCowHeader Header;
1011
1012#if defined(RT_LITTLE_ENDIAN)
1013 uint64_t *paL1TblImg = (uint64_t *)RTMemAllocZ(pImage->cbL1Table);
1014 if (paL1TblImg)
1015 {
1016 qcowTableConvertFromHostEndianess(paL1TblImg, pImage->paL1Table,
1017 pImage->cL1TableEntries);
1018 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
1019 pImage->offL1Table, paL1TblImg,
1020 pImage->cbL1Table, NULL);
1021 RTMemFree(paL1TblImg);
1022 }
1023 else
1024 rc = VERR_NO_MEMORY;
1025#else
1026 /* Write L1 table directly. */
1027 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, pImage->offL1Table,
1028 pImage->paL1Table, pImage->cbL1Table, NULL);
1029#endif
1030 if (RT_SUCCESS(rc))
1031 {
1032 /* Write header. */
1033 size_t cbHeader = 0;
1034 qcowHdrConvertFromHostEndianess(pImage, &Header, &cbHeader);
1035 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, 0, &Header,
1036 cbHeader, NULL);
1037 if (RT_SUCCESS(rc))
1038 rc = vdIfIoIntFileFlushSync(pImage->pIfIo, pImage->pStorage);
1039 }
1040 }
1041
1042 return rc;
1043}
1044
1045/**
1046 * Flush image data to disk - version for async I/O.
1047 *
1048 * @returns VBox status code.
1049 * @param pImage The image instance data.
1050 * @param pIoCtx The I/o context
1051 */
1052static int qcowFlushImageAsync(PQCOWIMAGE pImage, PVDIOCTX pIoCtx)
1053{
1054 int rc = VINF_SUCCESS;
1055
1056 if ( pImage->pStorage
1057 && !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1058 {
1059 QCowHeader Header;
1060
1061#if defined(RT_LITTLE_ENDIAN)
1062 uint64_t *paL1TblImg = (uint64_t *)RTMemAllocZ(pImage->cbL1Table);
1063 if (paL1TblImg)
1064 {
1065 qcowTableConvertFromHostEndianess(paL1TblImg, pImage->paL1Table,
1066 pImage->cL1TableEntries);
1067 rc = vdIfIoIntFileWriteMetaAsync(pImage->pIfIo, pImage->pStorage,
1068 pImage->offL1Table, paL1TblImg,
1069 pImage->cbL1Table, pIoCtx, NULL, NULL);
1070 RTMemFree(paL1TblImg);
1071 }
1072 else
1073 rc = VERR_NO_MEMORY;
1074#else
1075 /* Write L1 table directly. */
1076 rc = vdIfIoIntFileWriteMetaAsync(pImage->pIfIo, pImage->pStorage,
1077 pImage->offL1Table, pImage->paL1Table,
1078 pImage->cbL1Table, pIoCtx, NULL, NULL);
1079#endif
1080 if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1081 {
1082 /* Write header. */
1083 size_t cbHeader = 0;
1084 qcowHdrConvertFromHostEndianess(pImage, &Header, &cbHeader);
1085 rc = vdIfIoIntFileWriteMetaAsync(pImage->pIfIo, pImage->pStorage,
1086 0, &Header, cbHeader,
1087 pIoCtx, NULL, NULL);
1088 if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1089 rc = vdIfIoIntFileFlushAsync(pImage->pIfIo, pImage->pStorage,
1090 pIoCtx, NULL, NULL);
1091 }
1092 }
1093
1094 return rc;
1095}
1096
1097/**
1098 * Internal. Free all allocated space for representing an image except pImage,
1099 * and optionally delete the image from disk.
1100 */
1101static int qcowFreeImage(PQCOWIMAGE pImage, bool fDelete)
1102{
1103 int rc = VINF_SUCCESS;
1104
1105 /* Freeing a never allocated image (e.g. because the open failed) is
1106 * not signalled as an error. After all nothing bad happens. */
1107 if (pImage)
1108 {
1109 if (pImage->pStorage)
1110 {
1111 /* No point updating the file that is deleted anyway. */
1112 if (!fDelete)
1113 qcowFlushImage(pImage);
1114
1115 vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
1116 pImage->pStorage = NULL;
1117 }
1118
1119 if (pImage->paL1Table)
1120 RTMemFree(pImage->paL1Table);
1121
1122 if (pImage->pszBackingFilename)
1123 RTMemFree(pImage->pszBackingFilename);
1124
1125 qcowL2TblCacheDestroy(pImage);
1126
1127 if (fDelete && pImage->pszFilename)
1128 vdIfIoIntFileDelete(pImage->pIfIo, pImage->pszFilename);
1129 }
1130
1131 LogFlowFunc(("returns %Rrc\n", rc));
1132 return rc;
1133}
1134
1135/**
1136 * Internal: Open an image, constructing all necessary data structures.
1137 */
1138static int qcowOpenImage(PQCOWIMAGE pImage, unsigned uOpenFlags)
1139{
1140 int rc;
1141
1142 pImage->uOpenFlags = uOpenFlags;
1143
1144 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
1145 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
1146 AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
1147
1148 /*
1149 * Open the image.
1150 */
1151 rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename,
1152 VDOpenFlagsToFileOpenFlags(uOpenFlags,
1153 false /* fCreate */),
1154 &pImage->pStorage);
1155 if (RT_FAILURE(rc))
1156 {
1157 /* Do NOT signal an appropriate error here, as the VD layer has the
1158 * choice of retrying the open if it failed. */
1159 goto out;
1160 }
1161
1162 uint64_t cbFile;
1163 QCowHeader Header;
1164 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
1165 if (RT_FAILURE(rc))
1166 goto out;
1167 if (cbFile > sizeof(Header))
1168 {
1169 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, 0, &Header, sizeof(Header), NULL);
1170 if ( RT_SUCCESS(rc)
1171 && qcowHdrConvertToHostEndianess(&Header))
1172 {
1173 pImage->offNextCluster = RT_ALIGN_64(cbFile, 512); /* Align image to sector boundary. */
1174 Assert(pImage->offNextCluster >= cbFile);
1175
1176 rc = qcowL2TblCacheCreate(pImage);
1177 AssertRC(rc);
1178
1179 if (Header.u32Version == 1)
1180 {
1181 if (!Header.Version.v1.u32CryptMethod)
1182 {
1183 pImage->uVersion = 1;
1184 pImage->offBackingFilename = Header.Version.v1.u64BackingFileOffset;
1185 pImage->cbBackingFilename = Header.Version.v1.u32BackingFileSize;
1186 pImage->MTime = Header.Version.v1.u32MTime;
1187 pImage->cbSize = Header.Version.v1.u64Size;
1188 pImage->cbCluster = RT_BIT_32(Header.Version.v1.u8ClusterBits);
1189 pImage->cL2TableEntries = RT_BIT_32(Header.Version.v1.u8L2Bits);
1190 pImage->cbL2Table = RT_ALIGN_64(pImage->cL2TableEntries * sizeof(uint64_t), pImage->cbCluster);
1191 pImage->offL1Table = Header.Version.v1.u64L1TableOffset;
1192 pImage->cL1TableEntries = pImage->cbSize / (pImage->cbCluster * pImage->cL2TableEntries);
1193 if (pImage->cbSize % (pImage->cbCluster * pImage->cL2TableEntries))
1194 pImage->cL1TableEntries++;
1195 pImage->cbL1Table = RT_ALIGN_64(pImage->cL1TableEntries * sizeof(uint64_t), pImage->cbCluster);
1196 }
1197 else
1198 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1199 N_("QCow: Encrypted image '%s' is not supported"),
1200 pImage->pszFilename);
1201 }
1202 else if (Header.u32Version == 2)
1203 {
1204 if (Header.Version.v2.u32CryptMethod)
1205 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1206 N_("QCow: Encrypted image '%s' is not supported"),
1207 pImage->pszFilename);
1208 else if (Header.Version.v2.u32NbSnapshots)
1209 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1210 N_("QCow: Image '%s' contains snapshots which is not supported"),
1211 pImage->pszFilename);
1212 else
1213 {
1214 pImage->uVersion = 2;
1215 pImage->offBackingFilename = Header.Version.v2.u64BackingFileOffset;
1216 pImage->cbBackingFilename = Header.Version.v2.u32BackingFileSize;
1217 pImage->cbSize = Header.Version.v2.u64Size;
1218 pImage->cbCluster = RT_BIT_32(Header.Version.v2.u32ClusterBits);
1219 pImage->cL2TableEntries = pImage->cbCluster / sizeof(uint64_t);
1220 pImage->cbL2Table = pImage->cbCluster;
1221 pImage->offL1Table = Header.Version.v2.u64L1TableOffset;
1222 pImage->cL1TableEntries = Header.Version.v2.u32L1Size;
1223 pImage->cbL1Table = RT_ALIGN_64(pImage->cL1TableEntries * sizeof(uint64_t), pImage->cbCluster);
1224 pImage->offRefcountTable = Header.Version.v2.u64RefcountTableOffset;
1225 pImage->cbRefcountTable = qcowCluster2Byte(pImage, Header.Version.v2.u32RefcountTableClusters);
1226 pImage->cRefcountTableEntries = pImage->cbRefcountTable / sizeof(uint64_t);
1227 }
1228 }
1229 else
1230 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1231 N_("QCow: Image '%s' uses version %u which is not supported"),
1232 pImage->pszFilename, Header.u32Version);
1233
1234 /** @todo: Check that there are no compressed clusters in the image
1235 * (by traversing the L2 tables and checking each offset).
1236 * Refuse to open such images.
1237 */
1238
1239 if ( RT_SUCCESS(rc)
1240 && pImage->cbBackingFilename
1241 && pImage->offBackingFilename)
1242 {
1243 /* Load backing filename from image. */
1244 pImage->pszFilename = (char *)RTMemAllocZ(pImage->cbBackingFilename + 1); /* +1 for \0 terminator. */
1245 if (pImage->pszFilename)
1246 {
1247 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
1248 pImage->offBackingFilename, pImage->pszBackingFilename,
1249 pImage->cbBackingFilename, NULL);
1250 }
1251 else
1252 rc = VERR_NO_MEMORY;
1253 }
1254
1255 if ( RT_SUCCESS(rc)
1256 && pImage->cbRefcountTable
1257 && pImage->offRefcountTable)
1258 {
1259 /* Load refcount table. */
1260 Assert(pImage->cRefcountTableEntries);
1261 pImage->paRefcountTable = (uint64_t *)RTMemAllocZ(pImage->cbRefcountTable);
1262 if (RT_LIKELY(pImage->paRefcountTable))
1263 {
1264 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
1265 pImage->offRefcountTable, pImage->paRefcountTable,
1266 pImage->cbRefcountTable, NULL);
1267 if (RT_SUCCESS(rc))
1268 qcowTableConvertToHostEndianess(pImage->paRefcountTable,
1269 pImage->cRefcountTableEntries);
1270 else
1271 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1272 N_("QCow: Reading refcount table of image '%s' failed"),
1273 pImage->pszFilename);
1274 }
1275 else
1276 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
1277 N_("QCow: Allocating memory for refcount table of image '%s' failed"),
1278 pImage->pszFilename);
1279 }
1280
1281 if (RT_SUCCESS(rc))
1282 {
1283 qcowTableMasksInit(pImage);
1284
1285 /* Allocate L1 table. */
1286 pImage->paL1Table = (uint64_t *)RTMemAllocZ(pImage->cbL1Table);
1287 if (pImage->paL1Table)
1288 {
1289 /* Read from the image. */
1290 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
1291 pImage->offL1Table, pImage->paL1Table,
1292 pImage->cbL1Table, NULL);
1293 if (RT_SUCCESS(rc))
1294 qcowTableConvertToHostEndianess(pImage->paL1Table, pImage->cL1TableEntries);
1295 else
1296 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1297 N_("QCow: Reading the L1 table for image '%s' failed"),
1298 pImage->pszFilename);
1299 }
1300 else
1301 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
1302 N_("QCow: Out of memory allocating L1 table for image '%s'"),
1303 pImage->pszFilename);
1304 }
1305 }
1306 else if (RT_SUCCESS(rc))
1307 rc = VERR_VD_GEN_INVALID_HEADER;
1308 }
1309 else
1310 rc = VERR_VD_GEN_INVALID_HEADER;
1311
1312out:
1313 if (RT_FAILURE(rc))
1314 qcowFreeImage(pImage, false);
1315 return rc;
1316}
1317
1318/**
1319 * Internal: Create a qcow image.
1320 */
1321static int qcowCreateImage(PQCOWIMAGE pImage, uint64_t cbSize,
1322 unsigned uImageFlags, const char *pszComment,
1323 PCVDGEOMETRY pPCHSGeometry,
1324 PCVDGEOMETRY pLCHSGeometry, unsigned uOpenFlags,
1325 PFNVDPROGRESS pfnProgress, void *pvUser,
1326 unsigned uPercentStart, unsigned uPercentSpan)
1327{
1328 int rc;
1329 int32_t fOpen;
1330
1331 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
1332 {
1333 rc = vdIfError(pImage->pIfError, VERR_VD_INVALID_TYPE, RT_SRC_POS, N_("QCow: cannot create fixed image '%s'"), pImage->pszFilename);
1334 goto out;
1335 }
1336
1337 pImage->uOpenFlags = uOpenFlags & ~VD_OPEN_FLAGS_READONLY;
1338 pImage->uImageFlags = uImageFlags;
1339 pImage->PCHSGeometry = *pPCHSGeometry;
1340 pImage->LCHSGeometry = *pLCHSGeometry;
1341
1342 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
1343 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
1344 AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
1345
1346 /* Create image file. */
1347 fOpen = VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags, true /* fCreate */);
1348 rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename, fOpen, &pImage->pStorage);
1349 if (RT_FAILURE(rc))
1350 {
1351 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("QCow: cannot create image '%s'"), pImage->pszFilename);
1352 goto out;
1353 }
1354
1355 /* Init image state. */
1356 pImage->uVersion = 1; /* We create only version 1 images at the moment. */
1357 pImage->cbSize = cbSize;
1358 pImage->cbCluster = QCOW_CLUSTER_SIZE_DEFAULT;
1359 pImage->cbL2Table = qcowCluster2Byte(pImage, QCOW_L2_CLUSTERS_DEFAULT);
1360 pImage->cL2TableEntries = pImage->cbL2Table / sizeof(uint64_t);
1361 pImage->cL1TableEntries = cbSize / (pImage->cbCluster * pImage->cL2TableEntries);
1362 if (cbSize % (pImage->cbCluster * pImage->cL2TableEntries))
1363 pImage->cL1TableEntries++;
1364 pImage->cbL1Table = pImage->cL1TableEntries * sizeof(uint64_t);
1365 pImage->offL1Table = QCOW_V1_HDR_SIZE;
1366 pImage->cbBackingFilename = 0;
1367 pImage->offBackingFilename = 0;
1368 pImage->offNextCluster = RT_ALIGN_64(QCOW_V1_HDR_SIZE + pImage->cbL1Table, pImage->cbCluster);
1369 qcowTableMasksInit(pImage);
1370
1371 /* Init L1 table. */
1372 pImage->paL1Table = (uint64_t *)RTMemAllocZ(pImage->cbL1Table);
1373 if (!pImage->paL1Table)
1374 {
1375 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS, N_("QCow: cannot allocate memory for L1 table of image '%s'"),
1376 pImage->pszFilename);
1377 goto out;
1378 }
1379
1380 rc = qcowL2TblCacheCreate(pImage);
1381 if (RT_FAILURE(rc))
1382 {
1383 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("QCow: Failed to create L2 cache for image '%s'"),
1384 pImage->pszFilename);
1385 goto out;
1386 }
1387
1388 if (RT_SUCCESS(rc) && pfnProgress)
1389 pfnProgress(pvUser, uPercentStart + uPercentSpan * 98 / 100);
1390
1391 rc = qcowFlushImage(pImage);
1392 if (RT_SUCCESS(rc))
1393 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, pImage->offNextCluster);
1394
1395out:
1396 if (RT_SUCCESS(rc) && pfnProgress)
1397 pfnProgress(pvUser, uPercentStart + uPercentSpan);
1398
1399 if (RT_FAILURE(rc))
1400 qcowFreeImage(pImage, rc != VERR_ALREADY_EXISTS);
1401 return rc;
1402}
1403
1404/**
1405 * Rollback anything done during async cluster allocation.
1406 *
1407 * @returns VBox status code.
1408 * @param pImage The image instance data.
1409 * @param pIoCtx The I/O context.
1410 * @param pClusterAlloc The cluster allocation to rollback.
1411 */
1412static int qcowAsyncClusterAllocRollback(PQCOWIMAGE pImage, PVDIOCTX pIoCtx, PQCOWCLUSTERASYNCALLOC pClusterAlloc)
1413{
1414 int rc = VINF_SUCCESS;
1415
1416 switch (pClusterAlloc->enmAllocState)
1417 {
1418 case QCOWCLUSTERASYNCALLOCSTATE_L2_ALLOC:
1419 case QCOWCLUSTERASYNCALLOCSTATE_L2_LINK:
1420 {
1421 /* Assumption right now is that the L1 table is not modified if the link fails. */
1422 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, pClusterAlloc->offNextClusterOld);
1423 qcowL2TblCacheEntryRelease(pClusterAlloc->pL2Entry); /* Release L2 cache entry. */
1424 qcowL2TblCacheEntryFree(pImage, pClusterAlloc->pL2Entry); /* Free it, it is not in the cache yet. */
1425 }
1426 case QCOWCLUSTERASYNCALLOCSTATE_USER_ALLOC:
1427 case QCOWCLUSTERASYNCALLOCSTATE_USER_LINK:
1428 {
1429 /* Assumption right now is that the L2 table is not modified if the link fails. */
1430 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, pClusterAlloc->offNextClusterOld);
1431 qcowL2TblCacheEntryRelease(pClusterAlloc->pL2Entry); /* Release L2 cache entry. */
1432 break;
1433 }
1434 default:
1435 AssertMsgFailed(("Invalid cluster allocation state %d\n", pClusterAlloc->enmAllocState));
1436 rc = VERR_INVALID_STATE;
1437 }
1438
1439 RTMemFree(pClusterAlloc);
1440 return rc;
1441}
1442
1443/**
1444 * Updates the state of the async cluster allocation.
1445 *
1446 * @returns VBox status code.
1447 * @param pBackendData The opaque backend data.
1448 * @param pIoCtx I/O context associated with this request.
1449 * @param pvUser Opaque user data passed during a read/write request.
1450 * @param rcReq Status code for the completed request.
1451 */
1452static DECLCALLBACK(int) qcowAsyncClusterAllocUpdate(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
1453{
1454 int rc = VINF_SUCCESS;
1455 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
1456 PQCOWCLUSTERASYNCALLOC pClusterAlloc = (PQCOWCLUSTERASYNCALLOC)pvUser;
1457
1458 if (RT_FAILURE(rcReq))
1459 return qcowAsyncClusterAllocRollback(pImage, pIoCtx, pClusterAlloc);
1460
1461 AssertPtr(pClusterAlloc->pL2Entry);
1462
1463 switch (pClusterAlloc->enmAllocState)
1464 {
1465 case QCOWCLUSTERASYNCALLOCSTATE_L2_ALLOC:
1466 {
1467 uint64_t offUpdateLe = RT_H2BE_U64(pClusterAlloc->pL2Entry->offL2Tbl);
1468
1469 /* Update the link in the on disk L1 table now. */
1470 pClusterAlloc->enmAllocState = QCOWCLUSTERASYNCALLOCSTATE_L2_LINK;
1471 rc = vdIfIoIntFileWriteMetaAsync(pImage->pIfIo, pImage->pStorage,
1472 pImage->offL1Table + pClusterAlloc->idxL1*sizeof(uint64_t),
1473 &offUpdateLe, sizeof(uint64_t), pIoCtx,
1474 qcowAsyncClusterAllocUpdate, pClusterAlloc);
1475 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1476 break;
1477 else if (RT_FAILURE(rc))
1478 {
1479 /* Rollback. */
1480 qcowAsyncClusterAllocRollback(pImage, pIoCtx, pClusterAlloc);
1481 break;
1482 }
1483 /* Success, fall through. */
1484 }
1485 case QCOWCLUSTERASYNCALLOCSTATE_L2_LINK:
1486 {
1487 /* L2 link updated in L1 , save L2 entry in cache and allocate new user data cluster. */
1488 uint64_t offData = qcowClusterAllocate(pImage, 1);
1489
1490 /* Update the link in the in memory L1 table now. */
1491 pImage->paL1Table[pClusterAlloc->idxL1] = pClusterAlloc->pL2Entry->offL2Tbl;
1492 qcowL2TblCacheEntryInsert(pImage, pClusterAlloc->pL2Entry);
1493
1494 pClusterAlloc->enmAllocState = QCOWCLUSTERASYNCALLOCSTATE_USER_ALLOC;
1495 pClusterAlloc->offNextClusterOld = offData;
1496 pClusterAlloc->offClusterNew = offData;
1497
1498 /* Write data. */
1499 rc = vdIfIoIntFileWriteUserAsync(pImage->pIfIo, pImage->pStorage,
1500 offData, pIoCtx, pClusterAlloc->cbToWrite,
1501 qcowAsyncClusterAllocUpdate, pClusterAlloc);
1502 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1503 break;
1504 else if (RT_FAILURE(rc))
1505 {
1506 qcowAsyncClusterAllocRollback(pImage, pIoCtx, pClusterAlloc);
1507 RTMemFree(pClusterAlloc);
1508 break;
1509 }
1510 }
1511 case QCOWCLUSTERASYNCALLOCSTATE_USER_ALLOC:
1512 {
1513 uint64_t offUpdateLe = RT_H2BE_U64(pClusterAlloc->offClusterNew);
1514
1515 pClusterAlloc->enmAllocState = QCOWCLUSTERASYNCALLOCSTATE_USER_LINK;
1516
1517 /* Link L2 table and update it. */
1518 rc = vdIfIoIntFileWriteMetaAsync(pImage->pIfIo, pImage->pStorage,
1519 pImage->paL1Table[pClusterAlloc->idxL1] + pClusterAlloc->idxL2*sizeof(uint64_t),
1520 &offUpdateLe, sizeof(uint64_t), pIoCtx,
1521 qcowAsyncClusterAllocUpdate, pClusterAlloc);
1522 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1523 break;
1524 else if (RT_FAILURE(rc))
1525 {
1526 qcowAsyncClusterAllocRollback(pImage, pIoCtx, pClusterAlloc);
1527 RTMemFree(pClusterAlloc);
1528 break;
1529 }
1530 }
1531 case QCOWCLUSTERASYNCALLOCSTATE_USER_LINK:
1532 {
1533 /* Everything done without errors, signal completion. */
1534 pClusterAlloc->pL2Entry->paL2Tbl[pClusterAlloc->idxL2] = pClusterAlloc->offClusterNew;
1535 qcowL2TblCacheEntryRelease(pClusterAlloc->pL2Entry);
1536 RTMemFree(pClusterAlloc);
1537 rc = VINF_SUCCESS;
1538 break;
1539 }
1540 default:
1541 AssertMsgFailed(("Invalid async cluster allocation state %d\n",
1542 pClusterAlloc->enmAllocState));
1543 }
1544
1545 return rc;
1546}
1547
1548/** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
1549static int qcowCheckIfValid(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
1550 PVDINTERFACE pVDIfsImage, VDTYPE *penmType)
1551{
1552 LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p\n", pszFilename, pVDIfsDisk, pVDIfsImage));
1553 PVDIOSTORAGE pStorage = NULL;
1554 uint64_t cbFile;
1555 int rc = VINF_SUCCESS;
1556
1557 /* Get I/O interface. */
1558 PVDINTERFACEIOINT pIfIo = VDIfIoIntGet(pVDIfsImage);
1559 AssertPtrReturn(pIfIo, VERR_INVALID_PARAMETER);
1560
1561 if ( !VALID_PTR(pszFilename)
1562 || !*pszFilename)
1563 {
1564 rc = VERR_INVALID_PARAMETER;
1565 goto out;
1566 }
1567
1568 /*
1569 * Open the file and read the footer.
1570 */
1571 rc = vdIfIoIntFileOpen(pIfIo, pszFilename,
1572 VDOpenFlagsToFileOpenFlags(VD_OPEN_FLAGS_READONLY,
1573 false /* fCreate */),
1574 &pStorage);
1575 if (RT_SUCCESS(rc))
1576 rc = vdIfIoIntFileGetSize(pIfIo, pStorage, &cbFile);
1577
1578 if ( RT_SUCCESS(rc)
1579 && cbFile > sizeof(QCowHeader))
1580 {
1581 QCowHeader Header;
1582
1583 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, 0, &Header, sizeof(Header), NULL);
1584 if ( RT_SUCCESS(rc)
1585 && qcowHdrConvertToHostEndianess(&Header))
1586 {
1587 *penmType = VDTYPE_HDD;
1588 rc = VINF_SUCCESS;
1589 }
1590 else
1591 rc = VERR_VD_GEN_INVALID_HEADER;
1592 }
1593 else
1594 rc = VERR_VD_GEN_INVALID_HEADER;
1595
1596 if (pStorage)
1597 vdIfIoIntFileClose(pIfIo, pStorage);
1598
1599out:
1600 LogFlowFunc(("returns %Rrc\n", rc));
1601 return rc;
1602}
1603
1604/** @copydoc VBOXHDDBACKEND::pfnOpen */
1605static int qcowOpen(const char *pszFilename, unsigned uOpenFlags,
1606 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1607 VDTYPE enmType, void **ppBackendData)
1608{
1609 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, ppBackendData));
1610 int rc;
1611 PQCOWIMAGE pImage;
1612
1613 /* Check open flags. All valid flags are supported. */
1614 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
1615 {
1616 rc = VERR_INVALID_PARAMETER;
1617 goto out;
1618 }
1619
1620 /* Check remaining arguments. */
1621 if ( !VALID_PTR(pszFilename)
1622 || !*pszFilename)
1623 {
1624 rc = VERR_INVALID_PARAMETER;
1625 goto out;
1626 }
1627
1628
1629 pImage = (PQCOWIMAGE)RTMemAllocZ(sizeof(QCOWIMAGE));
1630 if (!pImage)
1631 {
1632 rc = VERR_NO_MEMORY;
1633 goto out;
1634 }
1635 pImage->pszFilename = pszFilename;
1636 pImage->pStorage = NULL;
1637 pImage->pVDIfsDisk = pVDIfsDisk;
1638 pImage->pVDIfsImage = pVDIfsImage;
1639
1640 rc = qcowOpenImage(pImage, uOpenFlags);
1641 if (RT_SUCCESS(rc))
1642 *ppBackendData = pImage;
1643 else
1644 RTMemFree(pImage);
1645
1646out:
1647 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1648 return rc;
1649}
1650
1651/** @copydoc VBOXHDDBACKEND::pfnCreate */
1652static int qcowCreate(const char *pszFilename, uint64_t cbSize,
1653 unsigned uImageFlags, const char *pszComment,
1654 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
1655 PCRTUUID pUuid, unsigned uOpenFlags,
1656 unsigned uPercentStart, unsigned uPercentSpan,
1657 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1658 PVDINTERFACE pVDIfsOperation, void **ppBackendData)
1659{
1660 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",
1661 pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, ppBackendData));
1662 int rc;
1663 PQCOWIMAGE pImage;
1664
1665 PFNVDPROGRESS pfnProgress = NULL;
1666 void *pvUser = NULL;
1667 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
1668 if (pIfProgress)
1669 {
1670 pfnProgress = pIfProgress->pfnProgress;
1671 pvUser = pIfProgress->Core.pvUser;
1672 }
1673
1674 /* Check open flags. All valid flags are supported. */
1675 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
1676 {
1677 rc = VERR_INVALID_PARAMETER;
1678 goto out;
1679 }
1680
1681 /* Check remaining arguments. */
1682 if ( !VALID_PTR(pszFilename)
1683 || !*pszFilename
1684 || !VALID_PTR(pPCHSGeometry)
1685 || !VALID_PTR(pLCHSGeometry))
1686 {
1687 rc = VERR_INVALID_PARAMETER;
1688 goto out;
1689 }
1690
1691 pImage = (PQCOWIMAGE)RTMemAllocZ(sizeof(QCOWIMAGE));
1692 if (!pImage)
1693 {
1694 rc = VERR_NO_MEMORY;
1695 goto out;
1696 }
1697 pImage->pszFilename = pszFilename;
1698 pImage->pStorage = NULL;
1699 pImage->pVDIfsDisk = pVDIfsDisk;
1700 pImage->pVDIfsImage = pVDIfsImage;
1701
1702 rc = qcowCreateImage(pImage, cbSize, uImageFlags, pszComment,
1703 pPCHSGeometry, pLCHSGeometry, uOpenFlags,
1704 pfnProgress, pvUser, uPercentStart, uPercentSpan);
1705 if (RT_SUCCESS(rc))
1706 {
1707 /* So far the image is opened in read/write mode. Make sure the
1708 * image is opened in read-only mode if the caller requested that. */
1709 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
1710 {
1711 qcowFreeImage(pImage, false);
1712 rc = qcowOpenImage(pImage, uOpenFlags);
1713 if (RT_FAILURE(rc))
1714 {
1715 RTMemFree(pImage);
1716 goto out;
1717 }
1718 }
1719 *ppBackendData = pImage;
1720 }
1721 else
1722 RTMemFree(pImage);
1723
1724out:
1725 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1726 return rc;
1727}
1728
1729/** @copydoc VBOXHDDBACKEND::pfnRename */
1730static int qcowRename(void *pBackendData, const char *pszFilename)
1731{
1732 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
1733 int rc = VINF_SUCCESS;
1734 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
1735
1736 /* Check arguments. */
1737 if ( !pImage
1738 || !pszFilename
1739 || !*pszFilename)
1740 {
1741 rc = VERR_INVALID_PARAMETER;
1742 goto out;
1743 }
1744
1745 /* Close the image. */
1746 rc = qcowFreeImage(pImage, false);
1747 if (RT_FAILURE(rc))
1748 goto out;
1749
1750 /* Rename the file. */
1751 rc = vdIfIoIntFileMove(pImage->pIfIo, pImage->pszFilename, pszFilename, 0);
1752 if (RT_FAILURE(rc))
1753 {
1754 /* The move failed, try to reopen the original image. */
1755 int rc2 = qcowOpenImage(pImage, pImage->uOpenFlags);
1756 if (RT_FAILURE(rc2))
1757 rc = rc2;
1758
1759 goto out;
1760 }
1761
1762 /* Update pImage with the new information. */
1763 pImage->pszFilename = pszFilename;
1764
1765 /* Open the old image with new name. */
1766 rc = qcowOpenImage(pImage, pImage->uOpenFlags);
1767 if (RT_FAILURE(rc))
1768 goto out;
1769
1770out:
1771 LogFlowFunc(("returns %Rrc\n", rc));
1772 return rc;
1773}
1774
1775/** @copydoc VBOXHDDBACKEND::pfnClose */
1776static int qcowClose(void *pBackendData, bool fDelete)
1777{
1778 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
1779 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
1780 int rc;
1781
1782 rc = qcowFreeImage(pImage, fDelete);
1783 RTMemFree(pImage);
1784
1785 LogFlowFunc(("returns %Rrc\n", rc));
1786 return rc;
1787}
1788
1789/** @copydoc VBOXHDDBACKEND::pfnRead */
1790static int qcowRead(void *pBackendData, uint64_t uOffset, void *pvBuf,
1791 size_t cbToRead, size_t *pcbActuallyRead)
1792{
1793 LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
1794 pBackendData, uOffset, pvBuf, cbToRead, pcbActuallyRead));
1795 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
1796 uint32_t offCluster = 0;
1797 uint32_t idxL1 = 0;
1798 uint32_t idxL2 = 0;
1799 uint64_t offFile = 0;
1800 int rc;
1801
1802 AssertPtr(pImage);
1803 Assert(uOffset % 512 == 0);
1804 Assert(cbToRead % 512 == 0);
1805
1806 if ( uOffset + cbToRead > pImage->cbSize
1807 || cbToRead == 0)
1808 {
1809 rc = VERR_INVALID_PARAMETER;
1810 goto out;
1811 }
1812
1813 qcowConvertLogicalOffset(pImage, uOffset, &idxL1, &idxL2, &offCluster);
1814 LogFlowFunc(("idxL1=%u idxL2=%u offCluster=%u\n", idxL1, idxL2, offCluster));
1815
1816 /* Clip read size to remain in the cluster. */
1817 cbToRead = RT_MIN(cbToRead, pImage->cbCluster - offCluster);
1818
1819 /* Get offset in image. */
1820 rc = qcowConvertToImageOffset(pImage, idxL1, idxL2, offCluster, &offFile);
1821 if (RT_SUCCESS(rc))
1822 {
1823 LogFlowFunc(("offFile=%llu\n", offFile));
1824 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, offFile,
1825 pvBuf, cbToRead, NULL);
1826 }
1827
1828 if ( (RT_SUCCESS(rc) || rc == VERR_VD_BLOCK_FREE)
1829 && pcbActuallyRead)
1830 *pcbActuallyRead = cbToRead;
1831
1832out:
1833 LogFlowFunc(("returns %Rrc\n", rc));
1834 return rc;
1835}
1836
1837/** @copydoc VBOXHDDBACKEND::pfnWrite */
1838static int qcowWrite(void *pBackendData, uint64_t uOffset, const void *pvBuf,
1839 size_t cbToWrite, size_t *pcbWriteProcess,
1840 size_t *pcbPreRead, size_t *pcbPostRead, unsigned fWrite)
1841{
1842 LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
1843 pBackendData, uOffset, pvBuf, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
1844 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
1845 uint32_t offCluster = 0;
1846 uint32_t idxL1 = 0;
1847 uint32_t idxL2 = 0;
1848 uint64_t offImage = 0;
1849 int rc;
1850
1851 AssertPtr(pImage);
1852 Assert(uOffset % 512 == 0);
1853 Assert(cbToWrite % 512 == 0);
1854
1855 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1856 {
1857 rc = VERR_VD_IMAGE_READ_ONLY;
1858 goto out;
1859 }
1860
1861 if ( uOffset + cbToWrite > pImage->cbSize
1862 || cbToWrite == 0)
1863 {
1864 rc = VERR_INVALID_PARAMETER;
1865 goto out;
1866 }
1867
1868 /* Convert offset to L1, L2 index and cluster offset. */
1869 qcowConvertLogicalOffset(pImage, uOffset, &idxL1, &idxL2, &offCluster);
1870
1871 /* Clip write size to remain in the cluster. */
1872 cbToWrite = RT_MIN(cbToWrite, pImage->cbCluster - offCluster);
1873 Assert(!(cbToWrite % 512));
1874
1875 /* Get offset in image. */
1876 rc = qcowConvertToImageOffset(pImage, idxL1, idxL2, offCluster, &offImage);
1877 if (RT_SUCCESS(rc))
1878 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, offImage,
1879 pvBuf, cbToWrite, NULL);
1880 else if (rc == VERR_VD_BLOCK_FREE)
1881 {
1882 if ( cbToWrite == pImage->cbCluster
1883 && !(fWrite & VD_WRITE_NO_ALLOC))
1884 {
1885 PQCOWL2CACHEENTRY pL2Entry = NULL;
1886
1887 /* Full cluster write to previously unallocated cluster.
1888 * Allocate cluster and write data. */
1889 Assert(!offCluster);
1890
1891 do
1892 {
1893 uint64_t idxUpdateLe = 0;
1894
1895 /* Check if we have to allocate a new cluster for L2 tables. */
1896 if (!pImage->paL1Table[idxL1])
1897 {
1898 uint64_t offL2Tbl = qcowClusterAllocate(pImage, qcowByte2Cluster(pImage, pImage->cbL2Table));
1899
1900 pL2Entry = qcowL2TblCacheEntryAlloc(pImage);
1901 if (!pL2Entry)
1902 {
1903 rc = VERR_NO_MEMORY;
1904 break;
1905 }
1906
1907 pL2Entry->offL2Tbl = offL2Tbl;
1908 memset(pL2Entry->paL2Tbl, 0, pImage->cbL2Table);
1909 qcowL2TblCacheEntryInsert(pImage, pL2Entry);
1910
1911 /*
1912 * Write the L2 table first and link to the L1 table afterwards.
1913 * If something unexpected happens the worst case which can happen
1914 * is a leak of some clusters.
1915 */
1916 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, offL2Tbl,
1917 pL2Entry->paL2Tbl, pImage->cbL2Table, NULL);
1918 if (RT_FAILURE(rc))
1919 break;
1920
1921 /* Write the L1 link now. */
1922 pImage->paL1Table[idxL1] = offL2Tbl;
1923 idxUpdateLe = RT_H2BE_U64(offL2Tbl);
1924 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
1925 pImage->offL1Table + idxL1*sizeof(uint64_t),
1926 &idxUpdateLe, sizeof(uint64_t), NULL);
1927 if (RT_FAILURE(rc))
1928 break;
1929 }
1930 else
1931 rc = qcowL2TblCacheFetch(pImage, pImage->paL1Table[idxL1], &pL2Entry);
1932
1933 if (RT_SUCCESS(rc))
1934 {
1935 /* Allocate new cluster for the data. */
1936 uint64_t offData = qcowClusterAllocate(pImage, 1);
1937
1938 /* Write data. */
1939 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
1940 offData, pvBuf, cbToWrite, NULL);
1941 if (RT_FAILURE(rc))
1942 break;
1943
1944 /* Link L2 table and update it. */
1945 pL2Entry->paL2Tbl[idxL2] = offData;
1946 idxUpdateLe = RT_H2BE_U64(offData);
1947 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
1948 pImage->paL1Table[idxL1] + idxL2*sizeof(uint64_t),
1949 &idxUpdateLe, sizeof(uint64_t), NULL);
1950 qcowL2TblCacheEntryRelease(pL2Entry);
1951 }
1952
1953 } while (0);
1954
1955 *pcbPreRead = 0;
1956 *pcbPostRead = 0;
1957 }
1958 else
1959 {
1960 /* Trying to do a partial write to an unallocated cluster. Don't do
1961 * anything except letting the upper layer know what to do. */
1962 *pcbPreRead = offCluster;
1963 *pcbPostRead = pImage->cbCluster - cbToWrite - *pcbPreRead;
1964 }
1965 }
1966
1967 if (pcbWriteProcess)
1968 *pcbWriteProcess = cbToWrite;
1969
1970out:
1971 LogFlowFunc(("returns %Rrc\n", rc));
1972 return rc;
1973}
1974
1975/** @copydoc VBOXHDDBACKEND::pfnFlush */
1976static int qcowFlush(void *pBackendData)
1977{
1978 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1979 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
1980 int rc;
1981
1982 rc = qcowFlushImage(pImage);
1983 LogFlowFunc(("returns %Rrc\n", rc));
1984 return rc;
1985}
1986
1987/** @copydoc VBOXHDDBACKEND::pfnGetVersion */
1988static unsigned qcowGetVersion(void *pBackendData)
1989{
1990 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1991 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
1992
1993 AssertPtr(pImage);
1994
1995 if (pImage)
1996 return pImage->uVersion;
1997 else
1998 return 0;
1999}
2000
2001/** @copydoc VBOXHDDBACKEND::pfnGetSize */
2002static uint64_t qcowGetSize(void *pBackendData)
2003{
2004 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2005 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2006 uint64_t cb = 0;
2007
2008 AssertPtr(pImage);
2009
2010 if (pImage && pImage->pStorage)
2011 cb = pImage->cbSize;
2012
2013 LogFlowFunc(("returns %llu\n", cb));
2014 return cb;
2015}
2016
2017/** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
2018static uint64_t qcowGetFileSize(void *pBackendData)
2019{
2020 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2021 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2022 uint64_t cb = 0;
2023
2024 AssertPtr(pImage);
2025
2026 if (pImage)
2027 {
2028 uint64_t cbFile;
2029 if (pImage->pStorage)
2030 {
2031 int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
2032 if (RT_SUCCESS(rc))
2033 cb += cbFile;
2034 }
2035 }
2036
2037 LogFlowFunc(("returns %lld\n", cb));
2038 return cb;
2039}
2040
2041/** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
2042static int qcowGetPCHSGeometry(void *pBackendData,
2043 PVDGEOMETRY pPCHSGeometry)
2044{
2045 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
2046 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2047 int rc;
2048
2049 AssertPtr(pImage);
2050
2051 if (pImage)
2052 {
2053 if (pImage->PCHSGeometry.cCylinders)
2054 {
2055 *pPCHSGeometry = pImage->PCHSGeometry;
2056 rc = VINF_SUCCESS;
2057 }
2058 else
2059 rc = VERR_VD_GEOMETRY_NOT_SET;
2060 }
2061 else
2062 rc = VERR_VD_NOT_OPENED;
2063
2064 LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2065 return rc;
2066}
2067
2068/** @copydoc VBOXHDDBACKEND::pfnSetPCHSGeometry */
2069static int qcowSetPCHSGeometry(void *pBackendData,
2070 PCVDGEOMETRY pPCHSGeometry)
2071{
2072 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2073 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2074 int rc;
2075
2076 AssertPtr(pImage);
2077
2078 if (pImage)
2079 {
2080 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2081 {
2082 rc = VERR_VD_IMAGE_READ_ONLY;
2083 goto out;
2084 }
2085
2086 pImage->PCHSGeometry = *pPCHSGeometry;
2087 rc = VINF_SUCCESS;
2088 }
2089 else
2090 rc = VERR_VD_NOT_OPENED;
2091
2092out:
2093 LogFlowFunc(("returns %Rrc\n", rc));
2094 return rc;
2095}
2096
2097/** @copydoc VBOXHDDBACKEND::pfnGetLCHSGeometry */
2098static int qcowGetLCHSGeometry(void *pBackendData,
2099 PVDGEOMETRY pLCHSGeometry)
2100{
2101 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
2102 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2103 int rc;
2104
2105 AssertPtr(pImage);
2106
2107 if (pImage)
2108 {
2109 if (pImage->LCHSGeometry.cCylinders)
2110 {
2111 *pLCHSGeometry = pImage->LCHSGeometry;
2112 rc = VINF_SUCCESS;
2113 }
2114 else
2115 rc = VERR_VD_GEOMETRY_NOT_SET;
2116 }
2117 else
2118 rc = VERR_VD_NOT_OPENED;
2119
2120 LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2121 return rc;
2122}
2123
2124/** @copydoc VBOXHDDBACKEND::pfnSetLCHSGeometry */
2125static int qcowSetLCHSGeometry(void *pBackendData,
2126 PCVDGEOMETRY pLCHSGeometry)
2127{
2128 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2129 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2130 int rc;
2131
2132 AssertPtr(pImage);
2133
2134 if (pImage)
2135 {
2136 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2137 {
2138 rc = VERR_VD_IMAGE_READ_ONLY;
2139 goto out;
2140 }
2141
2142 pImage->LCHSGeometry = *pLCHSGeometry;
2143 rc = VINF_SUCCESS;
2144 }
2145 else
2146 rc = VERR_VD_NOT_OPENED;
2147
2148out:
2149 LogFlowFunc(("returns %Rrc\n", rc));
2150 return rc;
2151}
2152
2153/** @copydoc VBOXHDDBACKEND::pfnGetImageFlags */
2154static unsigned qcowGetImageFlags(void *pBackendData)
2155{
2156 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2157 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2158 unsigned uImageFlags;
2159
2160 AssertPtr(pImage);
2161
2162 if (pImage)
2163 uImageFlags = pImage->uImageFlags;
2164 else
2165 uImageFlags = 0;
2166
2167 LogFlowFunc(("returns %#x\n", uImageFlags));
2168 return uImageFlags;
2169}
2170
2171/** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
2172static unsigned qcowGetOpenFlags(void *pBackendData)
2173{
2174 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2175 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2176 unsigned uOpenFlags;
2177
2178 AssertPtr(pImage);
2179
2180 if (pImage)
2181 uOpenFlags = pImage->uOpenFlags;
2182 else
2183 uOpenFlags = 0;
2184
2185 LogFlowFunc(("returns %#x\n", uOpenFlags));
2186 return uOpenFlags;
2187}
2188
2189/** @copydoc VBOXHDDBACKEND::pfnSetOpenFlags */
2190static int qcowSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
2191{
2192 LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
2193 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2194 int rc;
2195
2196 /* Image must be opened and the new flags must be valid. */
2197 if (!pImage || (uOpenFlags & ~(VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_ASYNC_IO)))
2198 {
2199 rc = VERR_INVALID_PARAMETER;
2200 goto out;
2201 }
2202
2203 /* Implement this operation via reopening the image. */
2204 rc = qcowFreeImage(pImage, false);
2205 if (RT_FAILURE(rc))
2206 goto out;
2207 rc = qcowOpenImage(pImage, uOpenFlags);
2208
2209out:
2210 LogFlowFunc(("returns %Rrc\n", rc));
2211 return rc;
2212}
2213
2214/** @copydoc VBOXHDDBACKEND::pfnGetComment */
2215static int qcowGetComment(void *pBackendData, char *pszComment,
2216 size_t cbComment)
2217{
2218 LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
2219 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2220 int rc;
2221
2222 AssertPtr(pImage);
2223
2224 if (pImage)
2225 rc = VERR_NOT_SUPPORTED;
2226 else
2227 rc = VERR_VD_NOT_OPENED;
2228
2229 LogFlowFunc(("returns %Rrc comment='%s'\n", rc, pszComment));
2230 return rc;
2231}
2232
2233/** @copydoc VBOXHDDBACKEND::pfnSetComment */
2234static int qcowSetComment(void *pBackendData, const char *pszComment)
2235{
2236 LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
2237 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2238 int rc;
2239
2240 AssertPtr(pImage);
2241
2242 if (pImage)
2243 {
2244 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2245 rc = VERR_VD_IMAGE_READ_ONLY;
2246 else
2247 rc = VERR_NOT_SUPPORTED;
2248 }
2249 else
2250 rc = VERR_VD_NOT_OPENED;
2251
2252 LogFlowFunc(("returns %Rrc\n", rc));
2253 return rc;
2254}
2255
2256/** @copydoc VBOXHDDBACKEND::pfnGetUuid */
2257static int qcowGetUuid(void *pBackendData, PRTUUID pUuid)
2258{
2259 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2260 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2261 int rc;
2262
2263 AssertPtr(pImage);
2264
2265 if (pImage)
2266 rc = VERR_NOT_SUPPORTED;
2267 else
2268 rc = VERR_VD_NOT_OPENED;
2269
2270 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2271 return rc;
2272}
2273
2274/** @copydoc VBOXHDDBACKEND::pfnSetUuid */
2275static int qcowSetUuid(void *pBackendData, PCRTUUID pUuid)
2276{
2277 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2278 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2279 int rc;
2280
2281 LogFlowFunc(("%RTuuid\n", pUuid));
2282 AssertPtr(pImage);
2283
2284 if (pImage)
2285 {
2286 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2287 rc = VERR_NOT_SUPPORTED;
2288 else
2289 rc = VERR_VD_IMAGE_READ_ONLY;
2290 }
2291 else
2292 rc = VERR_VD_NOT_OPENED;
2293
2294 LogFlowFunc(("returns %Rrc\n", rc));
2295 return rc;
2296}
2297
2298/** @copydoc VBOXHDDBACKEND::pfnGetModificationUuid */
2299static int qcowGetModificationUuid(void *pBackendData, PRTUUID pUuid)
2300{
2301 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2302 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2303 int rc;
2304
2305 AssertPtr(pImage);
2306
2307 if (pImage)
2308 rc = VERR_NOT_SUPPORTED;
2309 else
2310 rc = VERR_VD_NOT_OPENED;
2311
2312 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2313 return rc;
2314}
2315
2316/** @copydoc VBOXHDDBACKEND::pfnSetModificationUuid */
2317static int qcowSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
2318{
2319 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2320 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2321 int rc;
2322
2323 AssertPtr(pImage);
2324
2325 if (pImage)
2326 {
2327 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2328 rc = VERR_NOT_SUPPORTED;
2329 else
2330 rc = VERR_VD_IMAGE_READ_ONLY;
2331 }
2332 else
2333 rc = VERR_VD_NOT_OPENED;
2334
2335 LogFlowFunc(("returns %Rrc\n", rc));
2336 return rc;
2337}
2338
2339/** @copydoc VBOXHDDBACKEND::pfnGetParentUuid */
2340static int qcowGetParentUuid(void *pBackendData, PRTUUID pUuid)
2341{
2342 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2343 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2344 int rc;
2345
2346 AssertPtr(pImage);
2347
2348 if (pImage)
2349 rc = VERR_NOT_SUPPORTED;
2350 else
2351 rc = VERR_VD_NOT_OPENED;
2352
2353 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2354 return rc;
2355}
2356
2357/** @copydoc VBOXHDDBACKEND::pfnSetParentUuid */
2358static int qcowSetParentUuid(void *pBackendData, PCRTUUID pUuid)
2359{
2360 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2361 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2362 int rc;
2363
2364 AssertPtr(pImage);
2365
2366 if (pImage)
2367 {
2368 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2369 rc = VERR_NOT_SUPPORTED;
2370 else
2371 rc = VERR_VD_IMAGE_READ_ONLY;
2372 }
2373 else
2374 rc = VERR_VD_NOT_OPENED;
2375
2376 LogFlowFunc(("returns %Rrc\n", rc));
2377 return rc;
2378}
2379
2380/** @copydoc VBOXHDDBACKEND::pfnGetParentModificationUuid */
2381static int qcowGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
2382{
2383 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2384 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2385 int rc;
2386
2387 AssertPtr(pImage);
2388
2389 if (pImage)
2390 rc = VERR_NOT_SUPPORTED;
2391 else
2392 rc = VERR_VD_NOT_OPENED;
2393
2394 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2395 return rc;
2396}
2397
2398/** @copydoc VBOXHDDBACKEND::pfnSetParentModificationUuid */
2399static int qcowSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
2400{
2401 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2402 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2403 int rc;
2404
2405 AssertPtr(pImage);
2406
2407 if (pImage)
2408 {
2409 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2410 rc = VERR_NOT_SUPPORTED;
2411 else
2412 rc = VERR_VD_IMAGE_READ_ONLY;
2413 }
2414 else
2415 rc = VERR_VD_NOT_OPENED;
2416
2417 LogFlowFunc(("returns %Rrc\n", rc));
2418 return rc;
2419}
2420
2421/** @copydoc VBOXHDDBACKEND::pfnDump */
2422static void qcowDump(void *pBackendData)
2423{
2424 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2425
2426 AssertPtr(pImage);
2427 if (pImage)
2428 {
2429 vdIfErrorMessage(pImage->pIfError, "Header: Geometry PCHS=%u/%u/%u LCHS=%u/%u/%u cSector=%llu\n",
2430 pImage->PCHSGeometry.cCylinders, pImage->PCHSGeometry.cHeads, pImage->PCHSGeometry.cSectors,
2431 pImage->LCHSGeometry.cCylinders, pImage->LCHSGeometry.cHeads, pImage->LCHSGeometry.cSectors,
2432 pImage->cbSize / 512);
2433 }
2434}
2435
2436/** @copydoc VBOXHDDBACKEND::pfnGetParentFilename */
2437static int qcowGetParentFilename(void *pBackendData, char **ppszParentFilename)
2438{
2439 int rc = VINF_SUCCESS;
2440 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2441
2442 AssertPtr(pImage);
2443 if (pImage)
2444 if (pImage->pszFilename)
2445 *ppszParentFilename = RTStrDup(pImage->pszBackingFilename);
2446 else
2447 rc = VERR_NOT_SUPPORTED;
2448 else
2449 rc = VERR_VD_NOT_OPENED;
2450
2451 LogFlowFunc(("returns %Rrc\n", rc));
2452 return rc;
2453}
2454
2455/** @copydoc VBOXHDDBACKEND::pfnSetParentFilename */
2456static int qcowSetParentFilename(void *pBackendData, const char *pszParentFilename)
2457{
2458 int rc = VINF_SUCCESS;
2459 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2460
2461 AssertPtr(pImage);
2462 if (pImage)
2463 {
2464 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2465 rc = VERR_VD_IMAGE_READ_ONLY;
2466 else if ( pImage->pszBackingFilename
2467 && (strlen(pszParentFilename) > pImage->cbBackingFilename))
2468 rc = VERR_NOT_SUPPORTED; /* The new filename is longer than the old one. */
2469 else
2470 {
2471 if (pImage->pszBackingFilename)
2472 RTStrFree(pImage->pszBackingFilename);
2473 pImage->pszBackingFilename = RTStrDup(pszParentFilename);
2474 if (!pImage->pszBackingFilename)
2475 rc = VERR_NO_MEMORY;
2476 else
2477 {
2478 if (!pImage->offBackingFilename)
2479 {
2480 /* Allocate new cluster. */
2481 uint64_t offData = qcowClusterAllocate(pImage, 1);
2482
2483 Assert((offData & UINT32_MAX) == offData);
2484 pImage->offBackingFilename = (uint32_t)offData;
2485 pImage->cbBackingFilename = strlen(pszParentFilename);
2486 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage,
2487 offData + pImage->cbCluster);
2488 }
2489
2490 if (RT_SUCCESS(rc))
2491 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
2492 pImage->offBackingFilename,
2493 pImage->pszBackingFilename,
2494 strlen(pImage->pszBackingFilename),
2495 NULL);
2496 }
2497 }
2498 }
2499 else
2500 rc = VERR_VD_NOT_OPENED;
2501
2502 LogFlowFunc(("returns %Rrc\n", rc));
2503 return rc;
2504}
2505
2506static int qcowAsyncRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
2507 PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
2508{
2509 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
2510 pBackendData, uOffset, pIoCtx, cbToRead, pcbActuallyRead));
2511 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2512 uint32_t offCluster = 0;
2513 uint32_t idxL1 = 0;
2514 uint32_t idxL2 = 0;
2515 uint64_t offFile = 0;
2516 int rc;
2517
2518 AssertPtr(pImage);
2519 Assert(uOffset % 512 == 0);
2520 Assert(cbToRead % 512 == 0);
2521
2522 if (!VALID_PTR(pIoCtx) || !cbToRead)
2523 {
2524 rc = VERR_INVALID_PARAMETER;
2525 goto out;
2526 }
2527
2528 if ( uOffset + cbToRead > pImage->cbSize
2529 || cbToRead == 0)
2530 {
2531 rc = VERR_INVALID_PARAMETER;
2532 goto out;
2533 }
2534
2535 qcowConvertLogicalOffset(pImage, uOffset, &idxL1, &idxL2, &offCluster);
2536
2537 /* Clip read size to remain in the cluster. */
2538 cbToRead = RT_MIN(cbToRead, pImage->cbCluster - offCluster);
2539
2540 /* Get offset in image. */
2541 rc = qcowConvertToImageOffsetAsync(pImage, pIoCtx, idxL1, idxL2, offCluster,
2542 &offFile);
2543 if (RT_SUCCESS(rc))
2544 rc = vdIfIoIntFileReadUserAsync(pImage->pIfIo, pImage->pStorage, offFile,
2545 pIoCtx, cbToRead);
2546
2547 if ( ( RT_SUCCESS(rc)
2548 || rc == VERR_VD_BLOCK_FREE
2549 || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
2550 && pcbActuallyRead)
2551 *pcbActuallyRead = cbToRead;
2552
2553out:
2554 LogFlowFunc(("returns %Rrc\n", rc));
2555 return rc;
2556}
2557
2558static int qcowAsyncWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
2559 PVDIOCTX pIoCtx,
2560 size_t *pcbWriteProcess, size_t *pcbPreRead,
2561 size_t *pcbPostRead, unsigned fWrite)
2562{
2563 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
2564 pBackendData, uOffset, pIoCtx, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
2565 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2566 uint32_t offCluster = 0;
2567 uint32_t idxL1 = 0;
2568 uint32_t idxL2 = 0;
2569 uint64_t offImage = 0;
2570 int rc = VINF_SUCCESS;
2571
2572 AssertPtr(pImage);
2573 Assert(!(uOffset % 512));
2574 Assert(!(cbToWrite % 512));
2575
2576 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2577 {
2578 rc = VERR_VD_IMAGE_READ_ONLY;
2579 goto out;
2580 }
2581
2582 if (!VALID_PTR(pIoCtx) || !cbToWrite)
2583 {
2584 rc = VERR_INVALID_PARAMETER;
2585 goto out;
2586 }
2587
2588 if ( uOffset + cbToWrite > pImage->cbSize
2589 || cbToWrite == 0)
2590 {
2591 rc = VERR_INVALID_PARAMETER;
2592 goto out;
2593 }
2594
2595 /* Convert offset to L1, L2 index and cluster offset. */
2596 qcowConvertLogicalOffset(pImage, uOffset, &idxL1, &idxL2, &offCluster);
2597
2598 /* Clip write size to remain in the cluster. */
2599 cbToWrite = RT_MIN(cbToWrite, pImage->cbCluster - offCluster);
2600 Assert(!(cbToWrite % 512));
2601
2602 /* Get offset in image. */
2603 rc = qcowConvertToImageOffsetAsync(pImage, pIoCtx, idxL1, idxL2, offCluster,
2604 &offImage);
2605 if (RT_SUCCESS(rc))
2606 rc = vdIfIoIntFileWriteUserAsync(pImage->pIfIo, pImage->pStorage,
2607 offImage, pIoCtx, cbToWrite, NULL, NULL);
2608 else if (rc == VERR_VD_BLOCK_FREE)
2609 {
2610 if ( cbToWrite == pImage->cbCluster
2611 && !(fWrite & VD_WRITE_NO_ALLOC))
2612 {
2613 PQCOWL2CACHEENTRY pL2Entry = NULL;
2614
2615 /* Full cluster write to previously unallocated cluster.
2616 * Allocate cluster and write data. */
2617 Assert(!offCluster);
2618
2619 do
2620 {
2621 uint64_t idxUpdateLe = 0;
2622
2623 /* Check if we have to allocate a new cluster for L2 tables. */
2624 if (!pImage->paL1Table[idxL1])
2625 {
2626 uint64_t offL2Tbl;
2627 PQCOWCLUSTERASYNCALLOC pL2ClusterAlloc = NULL;
2628
2629 /* Allocate new async cluster allocation state. */
2630 pL2ClusterAlloc = (PQCOWCLUSTERASYNCALLOC)RTMemAllocZ(sizeof(QCOWCLUSTERASYNCALLOC));
2631 if (RT_UNLIKELY(!pL2ClusterAlloc))
2632 {
2633 rc = VERR_NO_MEMORY;
2634 break;
2635 }
2636
2637 pL2Entry = qcowL2TblCacheEntryAlloc(pImage);
2638 if (!pL2Entry)
2639 {
2640 rc = VERR_NO_MEMORY;
2641 RTMemFree(pL2ClusterAlloc);
2642 break;
2643 }
2644
2645 offL2Tbl = qcowClusterAllocate(pImage, qcowByte2Cluster(pImage, pImage->cbL2Table));
2646 pL2Entry->offL2Tbl = offL2Tbl;
2647 memset(pL2Entry->paL2Tbl, 0, pImage->cbL2Table);
2648
2649 pL2ClusterAlloc->enmAllocState = QCOWCLUSTERASYNCALLOCSTATE_L2_ALLOC;
2650 pL2ClusterAlloc->offNextClusterOld = offL2Tbl;
2651 pL2ClusterAlloc->offClusterNew = offL2Tbl;
2652 pL2ClusterAlloc->idxL1 = idxL1;
2653 pL2ClusterAlloc->idxL2 = idxL2;
2654 pL2ClusterAlloc->cbToWrite = cbToWrite;
2655 pL2ClusterAlloc->pL2Entry = pL2Entry;
2656
2657 /*
2658 * Write the L2 table first and link to the L1 table afterwards.
2659 * If something unexpected happens the worst case which can happen
2660 * is a leak of some clusters.
2661 */
2662 rc = vdIfIoIntFileWriteMetaAsync(pImage->pIfIo, pImage->pStorage,
2663 offL2Tbl, pL2Entry->paL2Tbl, pImage->cbL2Table, pIoCtx,
2664 qcowAsyncClusterAllocUpdate, pL2ClusterAlloc);
2665 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
2666 break;
2667 else if (RT_FAILURE(rc))
2668 {
2669 RTMemFree(pL2ClusterAlloc);
2670 qcowL2TblCacheEntryFree(pImage, pL2Entry);
2671 break;
2672 }
2673
2674 rc = qcowAsyncClusterAllocUpdate(pImage, pIoCtx, pL2ClusterAlloc, rc);
2675 }
2676 else
2677 {
2678 rc = qcowL2TblCacheFetchAsync(pImage, pIoCtx, pImage->paL1Table[idxL1],
2679 &pL2Entry);
2680
2681 if (RT_SUCCESS(rc))
2682 {
2683 PQCOWCLUSTERASYNCALLOC pDataClusterAlloc = NULL;
2684
2685 /* Allocate new async cluster allocation state. */
2686 pDataClusterAlloc = (PQCOWCLUSTERASYNCALLOC)RTMemAllocZ(sizeof(QCOWCLUSTERASYNCALLOC));
2687 if (RT_UNLIKELY(!pDataClusterAlloc))
2688 {
2689 rc = VERR_NO_MEMORY;
2690 break;
2691 }
2692
2693 /* Allocate new cluster for the data. */
2694 uint64_t offData = qcowClusterAllocate(pImage, 1);
2695
2696 pDataClusterAlloc->enmAllocState = QCOWCLUSTERASYNCALLOCSTATE_USER_ALLOC;
2697 pDataClusterAlloc->offNextClusterOld = offData;
2698 pDataClusterAlloc->offClusterNew = offData;
2699 pDataClusterAlloc->idxL1 = idxL1;
2700 pDataClusterAlloc->idxL2 = idxL2;
2701 pDataClusterAlloc->cbToWrite = cbToWrite;
2702 pDataClusterAlloc->pL2Entry = pL2Entry;
2703
2704 /* Write data. */
2705 rc = vdIfIoIntFileWriteUserAsync(pImage->pIfIo, pImage->pStorage,
2706 offData, pIoCtx, cbToWrite,
2707 qcowAsyncClusterAllocUpdate, pDataClusterAlloc);
2708 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
2709 break;
2710 else if (RT_FAILURE(rc))
2711 {
2712 RTMemFree(pDataClusterAlloc);
2713 break;
2714 }
2715
2716 rc = qcowAsyncClusterAllocUpdate(pImage, pIoCtx, pDataClusterAlloc, rc);
2717 }
2718 }
2719
2720 } while (0);
2721
2722 *pcbPreRead = 0;
2723 *pcbPostRead = 0;
2724 }
2725 else
2726 {
2727 /* Trying to do a partial write to an unallocated cluster. Don't do
2728 * anything except letting the upper layer know what to do. */
2729 *pcbPreRead = offCluster;
2730 *pcbPostRead = pImage->cbCluster - cbToWrite - *pcbPreRead;
2731 }
2732 }
2733
2734 if (pcbWriteProcess)
2735 *pcbWriteProcess = cbToWrite;
2736
2737
2738out:
2739 LogFlowFunc(("returns %Rrc\n", rc));
2740 return rc;
2741}
2742
2743static int qcowAsyncFlush(void *pBackendData, PVDIOCTX pIoCtx)
2744{
2745 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2746 PQCOWIMAGE pImage = (PQCOWIMAGE)pBackendData;
2747 int rc = VINF_SUCCESS;
2748
2749 Assert(pImage);
2750
2751 if (VALID_PTR(pIoCtx))
2752 rc = qcowFlushImageAsync(pImage, pIoCtx);
2753 else
2754 rc = VERR_INVALID_PARAMETER;
2755
2756 LogFlowFunc(("returns %Rrc\n", rc));
2757 return rc;
2758}
2759
2760VBOXHDDBACKEND g_QCowBackend =
2761{
2762 /* pszBackendName */
2763 "QCOW",
2764 /* cbSize */
2765 sizeof(VBOXHDDBACKEND),
2766 /* uBackendCaps */
2767 VD_CAP_FILE | VD_CAP_VFS | VD_CAP_CREATE_DYNAMIC | VD_CAP_DIFF | VD_CAP_ASYNC,
2768 /* paFileExtensions */
2769 s_aQCowFileExtensions,
2770 /* paConfigInfo */
2771 NULL,
2772 /* hPlugin */
2773 NIL_RTLDRMOD,
2774 /* pfnCheckIfValid */
2775 qcowCheckIfValid,
2776 /* pfnOpen */
2777 qcowOpen,
2778 /* pfnCreate */
2779 qcowCreate,
2780 /* pfnRename */
2781 qcowRename,
2782 /* pfnClose */
2783 qcowClose,
2784 /* pfnRead */
2785 qcowRead,
2786 /* pfnWrite */
2787 qcowWrite,
2788 /* pfnFlush */
2789 qcowFlush,
2790 /* pfnGetVersion */
2791 qcowGetVersion,
2792 /* pfnGetSize */
2793 qcowGetSize,
2794 /* pfnGetFileSize */
2795 qcowGetFileSize,
2796 /* pfnGetPCHSGeometry */
2797 qcowGetPCHSGeometry,
2798 /* pfnSetPCHSGeometry */
2799 qcowSetPCHSGeometry,
2800 /* pfnGetLCHSGeometry */
2801 qcowGetLCHSGeometry,
2802 /* pfnSetLCHSGeometry */
2803 qcowSetLCHSGeometry,
2804 /* pfnGetImageFlags */
2805 qcowGetImageFlags,
2806 /* pfnGetOpenFlags */
2807 qcowGetOpenFlags,
2808 /* pfnSetOpenFlags */
2809 qcowSetOpenFlags,
2810 /* pfnGetComment */
2811 qcowGetComment,
2812 /* pfnSetComment */
2813 qcowSetComment,
2814 /* pfnGetUuid */
2815 qcowGetUuid,
2816 /* pfnSetUuid */
2817 qcowSetUuid,
2818 /* pfnGetModificationUuid */
2819 qcowGetModificationUuid,
2820 /* pfnSetModificationUuid */
2821 qcowSetModificationUuid,
2822 /* pfnGetParentUuid */
2823 qcowGetParentUuid,
2824 /* pfnSetParentUuid */
2825 qcowSetParentUuid,
2826 /* pfnGetParentModificationUuid */
2827 qcowGetParentModificationUuid,
2828 /* pfnSetParentModificationUuid */
2829 qcowSetParentModificationUuid,
2830 /* pfnDump */
2831 qcowDump,
2832 /* pfnGetTimeStamp */
2833 NULL,
2834 /* pfnGetParentTimeStamp */
2835 NULL,
2836 /* pfnSetParentTimeStamp */
2837 NULL,
2838 /* pfnGetParentFilename */
2839 qcowGetParentFilename,
2840 /* pfnSetParentFilename */
2841 qcowSetParentFilename,
2842 /* pfnAsyncRead */
2843 qcowAsyncRead,
2844 /* pfnAsyncWrite */
2845 qcowAsyncWrite,
2846 /* pfnAsyncFlush */
2847 qcowAsyncFlush,
2848 /* pfnComposeLocation */
2849 genericFileComposeLocation,
2850 /* pfnComposeName */
2851 genericFileComposeName,
2852 /* pfnCompact */
2853 NULL,
2854 /* pfnResize */
2855 NULL,
2856 /* pfnDiscard */
2857 NULL,
2858 /* pfnAsyncDiscard */
2859 NULL,
2860 /* pfnRepair */
2861 NULL
2862};
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