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