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