1 | /** @file
|
---|
2 | * Virtual Disk Image (VDI), Core Code.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | */
|
---|
16 |
|
---|
17 | /*******************************************************************************
|
---|
18 | * Header Files *
|
---|
19 | *******************************************************************************/
|
---|
20 | #define LOG_GROUP LOG_GROUP_VD_VDI
|
---|
21 | #include <VBox/VBoxHDD-Plugin.h>
|
---|
22 | #define VBOX_VDICORE_VD /* Signal that the header is included from here. */
|
---|
23 | #include "VDICore.h"
|
---|
24 | #include <VBox/err.h>
|
---|
25 |
|
---|
26 | #include <VBox/log.h>
|
---|
27 | #include <iprt/alloc.h>
|
---|
28 | #include <iprt/assert.h>
|
---|
29 | #include <iprt/uuid.h>
|
---|
30 | #include <iprt/file.h>
|
---|
31 | #include <iprt/string.h>
|
---|
32 | #include <iprt/asm.h>
|
---|
33 |
|
---|
34 | #define VDI_IMAGE_DEFAULT_BLOCK_SIZE _1M
|
---|
35 |
|
---|
36 | /*******************************************************************************
|
---|
37 | * Static Variables *
|
---|
38 | *******************************************************************************/
|
---|
39 |
|
---|
40 | /** NULL-terminated array of supported file extensions. */
|
---|
41 | static const char *const s_apszVdiFileExtensions[] =
|
---|
42 | {
|
---|
43 | "vdi",
|
---|
44 | NULL
|
---|
45 | };
|
---|
46 |
|
---|
47 | /*******************************************************************************
|
---|
48 | * Internal Functions *
|
---|
49 | *******************************************************************************/
|
---|
50 | static unsigned getPowerOfTwo(unsigned uNumber);
|
---|
51 | static void vdiInitPreHeader(PVDIPREHEADER pPreHdr);
|
---|
52 | static int vdiValidatePreHeader(PVDIPREHEADER pPreHdr);
|
---|
53 | static void vdiInitHeader(PVDIHEADER pHeader, uint32_t uImageFlags,
|
---|
54 | const char *pszComment, uint64_t cbDisk,
|
---|
55 | uint32_t cbBlock, uint32_t cbBlockExtra);
|
---|
56 | static int vdiValidateHeader(PVDIHEADER pHeader);
|
---|
57 | static void vdiSetupImageDesc(PVDIIMAGEDESC pImage);
|
---|
58 | static int vdiUpdateHeader(PVDIIMAGEDESC pImage);
|
---|
59 | static int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock);
|
---|
60 | static void vdiFreeImage(PVDIIMAGEDESC pImage, bool fDelete);
|
---|
61 | static int vdiUpdateHeaderAsync(PVDIIMAGEDESC pImage, PVDIOCTX pIoCtx);
|
---|
62 | static int vdiUpdateBlockInfoAsync(PVDIIMAGEDESC pImage, unsigned uBlock, PVDIOCTX pIoCtx);
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Internal: signal an error to the frontend.
|
---|
66 | */
|
---|
67 | DECLINLINE(int) vdiError(PVDIIMAGEDESC pImage, int rc, RT_SRC_POS_DECL,
|
---|
68 | const char *pszFormat, ...)
|
---|
69 | {
|
---|
70 | va_list va;
|
---|
71 | va_start(va, pszFormat);
|
---|
72 | if (pImage->pInterfaceError && pImage->pInterfaceErrorCallbacks)
|
---|
73 | pImage->pInterfaceErrorCallbacks->pfnError(pImage->pInterfaceError->pvUser,
|
---|
74 | rc, RT_SRC_POS_ARGS,
|
---|
75 | pszFormat, va);
|
---|
76 | va_end(va);
|
---|
77 | return rc;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | static int vdiFileOpen(PVDIIMAGEDESC pImage, bool fReadonly, bool fCreate)
|
---|
82 | {
|
---|
83 | int rc = VINF_SUCCESS;
|
---|
84 |
|
---|
85 | AssertMsg(!(fReadonly && fCreate), ("Image can't be opened readonly whilebeing created\n"));
|
---|
86 |
|
---|
87 | #ifndef VBOX_WITH_NEW_IO_CODE
|
---|
88 | uint32_t fOpen = fReadonly ? RTFILE_O_READ | RTFILE_O_DENY_NONE
|
---|
89 | : RTFILE_O_READWRITE | RTFILE_O_DENY_WRITE;
|
---|
90 |
|
---|
91 | if (fCreate)
|
---|
92 | fOpen |= RTFILE_O_CREATE;
|
---|
93 | else
|
---|
94 | fOpen |= RTFILE_O_OPEN;
|
---|
95 |
|
---|
96 | rc = RTFileOpen(&pImage->File, pImage->pszFilename, fOpen);
|
---|
97 | #else
|
---|
98 | unsigned uOpenFlags = fReadonly ? VD_INTERFACEASYNCIO_OPEN_FLAGS_READONLY : 0;
|
---|
99 |
|
---|
100 | if (fCreate)
|
---|
101 | uOpenFlags |= VD_INTERFACEASYNCIO_OPEN_FLAGS_CREATE;
|
---|
102 |
|
---|
103 | rc = pImage->pInterfaceIOCallbacks->pfnOpen(pImage->pInterfaceIO->pvUser,
|
---|
104 | pImage->pszFilename,
|
---|
105 | uOpenFlags,
|
---|
106 | &pImage->pStorage);
|
---|
107 | #endif
|
---|
108 |
|
---|
109 | return rc;
|
---|
110 | }
|
---|
111 |
|
---|
112 | static int vdiFileClose(PVDIIMAGEDESC pImage)
|
---|
113 | {
|
---|
114 | int rc = VINF_SUCCESS;
|
---|
115 |
|
---|
116 | #ifndef VBOX_WITH_NEW_IO_CODE
|
---|
117 | if (pImage->File != NIL_RTFILE)
|
---|
118 | rc = RTFileClose(pImage->File);
|
---|
119 |
|
---|
120 | pImage->File = NIL_RTFILE;
|
---|
121 | #else
|
---|
122 | rc = pImage->pInterfaceIOCallbacks->pfnClose(pImage->pInterfaceIO->pvUser,
|
---|
123 | pImage->pStorage);
|
---|
124 |
|
---|
125 | pImage->pStorage = NULL;
|
---|
126 | #endif
|
---|
127 |
|
---|
128 | return rc;
|
---|
129 | }
|
---|
130 |
|
---|
131 | static int vdiFileFlushSync(PVDIIMAGEDESC pImage)
|
---|
132 | {
|
---|
133 | int rc = VINF_SUCCESS;
|
---|
134 |
|
---|
135 | #ifndef VBOX_WITH_NEW_IO_CODE
|
---|
136 | rc = RTFileFlush(pImage->File);
|
---|
137 | #else
|
---|
138 | rc = pImage->pInterfaceIOCallbacks->pfnFlushSync(pImage->pInterfaceIO->pvUser,
|
---|
139 | pImage->pStorage);
|
---|
140 | #endif
|
---|
141 |
|
---|
142 | return rc;
|
---|
143 | }
|
---|
144 |
|
---|
145 | static int vdiFileGetSize(PVDIIMAGEDESC pImage, uint64_t *pcbSize)
|
---|
146 | {
|
---|
147 | int rc = VINF_SUCCESS;
|
---|
148 |
|
---|
149 | #ifndef VBOX_WITH_NEW_IO_CODE
|
---|
150 | rc = RTFileGetSize(pImage->File, pcbSize);
|
---|
151 | #else
|
---|
152 | rc = pImage->pInterfaceIOCallbacks->pfnGetSize(pImage->pInterfaceIO->pvUser,
|
---|
153 | pImage->pStorage,
|
---|
154 | pcbSize);
|
---|
155 | #endif
|
---|
156 |
|
---|
157 | return rc;
|
---|
158 | }
|
---|
159 |
|
---|
160 | static int vdiFileSetSize(PVDIIMAGEDESC pImage, uint64_t cbSize)
|
---|
161 | {
|
---|
162 | int rc = VINF_SUCCESS;
|
---|
163 |
|
---|
164 | #ifndef VBOX_WITH_NEW_IO_CODE
|
---|
165 | rc = RTFileSetSize(pImage->File, cbSize);
|
---|
166 | #else
|
---|
167 | rc = pImage->pInterfaceIOCallbacks->pfnSetSize(pImage->pInterfaceIO->pvUser,
|
---|
168 | pImage->pStorage,
|
---|
169 | cbSize);
|
---|
170 | #endif
|
---|
171 |
|
---|
172 | return rc;
|
---|
173 | }
|
---|
174 |
|
---|
175 | static int vdiFileWriteSync(PVDIIMAGEDESC pImage, uint64_t off, const void *pcvBuf, size_t cbWrite, size_t *pcbWritten)
|
---|
176 | {
|
---|
177 | int rc = VINF_SUCCESS;
|
---|
178 |
|
---|
179 | #ifndef VBOX_WITH_NEW_IO_CODE
|
---|
180 | rc = RTFileWriteAt(pImage->File, off, pcvBuf, cbWrite, pcbWritten);
|
---|
181 | #else
|
---|
182 | rc = pImage->pInterfaceIOCallbacks->pfnWriteSync(pImage->pInterfaceIO->pvUser,
|
---|
183 | pImage->pStorage,
|
---|
184 | off, cbWrite, pcvBuf,
|
---|
185 | pcbWritten);
|
---|
186 | #endif
|
---|
187 |
|
---|
188 | return rc;
|
---|
189 | }
|
---|
190 |
|
---|
191 | static int vdiFileReadSync(PVDIIMAGEDESC pImage, uint64_t off, void *pvBuf, size_t cbRead, size_t *pcbRead)
|
---|
192 | {
|
---|
193 | int rc = VINF_SUCCESS;
|
---|
194 |
|
---|
195 | #ifndef VBOX_WITH_NEW_IO_CODE
|
---|
196 | rc = RTFileReadAt(pImage->File, off, pvBuf, cbRead, pcbRead);
|
---|
197 | #else
|
---|
198 | rc = pImage->pInterfaceIOCallbacks->pfnReadSync(pImage->pInterfaceIO->pvUser,
|
---|
199 | pImage->pStorage,
|
---|
200 | off, cbRead, pvBuf,
|
---|
201 | pcbRead);
|
---|
202 | #endif
|
---|
203 |
|
---|
204 | return rc;
|
---|
205 | }
|
---|
206 |
|
---|
207 | static int vdiFileWriteMetaAsync(PVDIIMAGEDESC pImage, uint64_t off, void *pcvBuf, size_t cbWrite,
|
---|
208 | PVDIOCTX pIoCtx, PFNVDMETACOMPLETED pfnMetaComplete, void *pvMetaUser)
|
---|
209 | {
|
---|
210 | return pImage->pInterfaceIOCallbacks->pfnWriteMetaAsync(pImage->pInterfaceIO->pvUser,
|
---|
211 | pImage->pStorage,
|
---|
212 | off, pcvBuf, cbWrite, pIoCtx,
|
---|
213 | pfnMetaComplete, pvMetaUser);
|
---|
214 | }
|
---|
215 |
|
---|
216 | static int vdiFileReadMetaAsync(PVDIIMAGEDESC pImage, uint64_t off, void *pvBuf, size_t cbRead,
|
---|
217 | PVDIOCTX pIoCtx, PFNVDMETACOMPLETED pfnMetaComplete, void *pvMetaUser)
|
---|
218 | {
|
---|
219 | return pImage->pInterfaceIOCallbacks->pfnReadMetaAsync(pImage->pInterfaceIO->pvUser,
|
---|
220 | pImage->pStorage,
|
---|
221 | off, pvBuf, cbRead, pIoCtx,
|
---|
222 | pfnMetaComplete, pvMetaUser);
|
---|
223 | }
|
---|
224 |
|
---|
225 | static bool vdiFileOpened(PVDIIMAGEDESC pImage)
|
---|
226 | {
|
---|
227 | #ifndef VBOX_WITH_NEW_IO_CODE
|
---|
228 | return pImage->File != NIL_RTFILE;
|
---|
229 | #else
|
---|
230 | return pImage->pStorage != NULL;
|
---|
231 | #endif
|
---|
232 | }
|
---|
233 |
|
---|
234 | static int vdiFileFlushAsync(PVDIIMAGEDESC pImage, PVDIOCTX pIoCtx)
|
---|
235 | {
|
---|
236 | return pImage->pInterfaceIOCallbacks->pfnFlushAsync(pImage->pInterfaceIO->pvUser,
|
---|
237 | pImage->pStorage, pIoCtx);
|
---|
238 | }
|
---|
239 |
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * internal: return power of 2 or 0 if num error.
|
---|
243 | */
|
---|
244 | static unsigned getPowerOfTwo(unsigned uNumber)
|
---|
245 | {
|
---|
246 | if (uNumber == 0)
|
---|
247 | return 0;
|
---|
248 | unsigned uPower2 = 0;
|
---|
249 | while ((uNumber & 1) == 0)
|
---|
250 | {
|
---|
251 | uNumber >>= 1;
|
---|
252 | uPower2++;
|
---|
253 | }
|
---|
254 | return uNumber == 1 ? uPower2 : 0;
|
---|
255 | }
|
---|
256 |
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Internal: Init VDI preheader.
|
---|
260 | */
|
---|
261 | static void vdiInitPreHeader(PVDIPREHEADER pPreHdr)
|
---|
262 | {
|
---|
263 | pPreHdr->u32Signature = VDI_IMAGE_SIGNATURE;
|
---|
264 | pPreHdr->u32Version = VDI_IMAGE_VERSION;
|
---|
265 | memset(pPreHdr->szFileInfo, 0, sizeof(pPreHdr->szFileInfo));
|
---|
266 | strncat(pPreHdr->szFileInfo, VDI_IMAGE_FILE_INFO, sizeof(pPreHdr->szFileInfo));
|
---|
267 | }
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Internal: check VDI preheader.
|
---|
271 | */
|
---|
272 | static int vdiValidatePreHeader(PVDIPREHEADER pPreHdr)
|
---|
273 | {
|
---|
274 | if (pPreHdr->u32Signature != VDI_IMAGE_SIGNATURE)
|
---|
275 | return VERR_VD_VDI_INVALID_HEADER;
|
---|
276 |
|
---|
277 | if ( VDI_GET_VERSION_MAJOR(pPreHdr->u32Version) != VDI_IMAGE_VERSION_MAJOR
|
---|
278 | && pPreHdr->u32Version != 0x00000002) /* old version. */
|
---|
279 | return VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
280 |
|
---|
281 | return VINF_SUCCESS;
|
---|
282 | }
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * Internal: translate VD image flags to VDI image type enum.
|
---|
286 | */
|
---|
287 | static VDIIMAGETYPE vdiTranslateImageFlags2VDI(unsigned uImageFlags)
|
---|
288 | {
|
---|
289 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
290 | return VDI_IMAGE_TYPE_FIXED;
|
---|
291 | else if (uImageFlags & VD_IMAGE_FLAGS_DIFF)
|
---|
292 | return VDI_IMAGE_TYPE_DIFF;
|
---|
293 | else
|
---|
294 | return VDI_IMAGE_TYPE_NORMAL;
|
---|
295 | }
|
---|
296 |
|
---|
297 | /**
|
---|
298 | * Internal: translate VDI image type enum to VD image type enum.
|
---|
299 | */
|
---|
300 | static unsigned vdiTranslateVDI2ImageFlags(VDIIMAGETYPE enmType)
|
---|
301 | {
|
---|
302 | switch (enmType)
|
---|
303 | {
|
---|
304 | case VDI_IMAGE_TYPE_NORMAL:
|
---|
305 | return VD_IMAGE_FLAGS_NONE;
|
---|
306 | case VDI_IMAGE_TYPE_FIXED:
|
---|
307 | return VD_IMAGE_FLAGS_FIXED;
|
---|
308 | case VDI_IMAGE_TYPE_DIFF:
|
---|
309 | return VD_IMAGE_FLAGS_DIFF;
|
---|
310 | default:
|
---|
311 | AssertMsgFailed(("invalid VDIIMAGETYPE enmType=%d\n", (int)enmType));
|
---|
312 | return VD_IMAGE_FLAGS_NONE;
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 | /**
|
---|
317 | * Internal: Init VDI header. Always use latest header version.
|
---|
318 | * @param pHeader Assumes it was initially initialized to all zeros.
|
---|
319 | */
|
---|
320 | static void vdiInitHeader(PVDIHEADER pHeader, uint32_t uImageFlags,
|
---|
321 | const char *pszComment, uint64_t cbDisk,
|
---|
322 | uint32_t cbBlock, uint32_t cbBlockExtra)
|
---|
323 | {
|
---|
324 | pHeader->uVersion = VDI_IMAGE_VERSION;
|
---|
325 | pHeader->u.v1.cbHeader = sizeof(VDIHEADER1);
|
---|
326 | pHeader->u.v1.u32Type = (uint32_t)vdiTranslateImageFlags2VDI(uImageFlags);
|
---|
327 | pHeader->u.v1.fFlags = (uImageFlags & VD_VDI_IMAGE_FLAGS_ZERO_EXPAND) ? 1 : 0;
|
---|
328 | #ifdef VBOX_STRICT
|
---|
329 | char achZero[VDI_IMAGE_COMMENT_SIZE] = {0};
|
---|
330 | Assert(!memcmp(pHeader->u.v1.szComment, achZero, VDI_IMAGE_COMMENT_SIZE));
|
---|
331 | #endif
|
---|
332 | pHeader->u.v1.szComment[0] = '\0';
|
---|
333 | if (pszComment)
|
---|
334 | {
|
---|
335 | AssertMsg(strlen(pszComment) < sizeof(pHeader->u.v1.szComment),
|
---|
336 | ("HDD Comment is too long, cb=%d\n", strlen(pszComment)));
|
---|
337 | strncat(pHeader->u.v1.szComment, pszComment, sizeof(pHeader->u.v1.szComment));
|
---|
338 | }
|
---|
339 |
|
---|
340 | /* Mark the legacy geometry not-calculated. */
|
---|
341 | pHeader->u.v1.LegacyGeometry.cCylinders = 0;
|
---|
342 | pHeader->u.v1.LegacyGeometry.cHeads = 0;
|
---|
343 | pHeader->u.v1.LegacyGeometry.cSectors = 0;
|
---|
344 | pHeader->u.v1.LegacyGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
345 | pHeader->u.v1.u32Dummy = 0; /* used to be the translation value */
|
---|
346 |
|
---|
347 | pHeader->u.v1.cbDisk = cbDisk;
|
---|
348 | pHeader->u.v1.cbBlock = cbBlock;
|
---|
349 | pHeader->u.v1.cBlocks = (uint32_t)(cbDisk / cbBlock);
|
---|
350 | if (cbDisk % cbBlock)
|
---|
351 | pHeader->u.v1.cBlocks++;
|
---|
352 | pHeader->u.v1.cbBlockExtra = cbBlockExtra;
|
---|
353 | pHeader->u.v1.cBlocksAllocated = 0;
|
---|
354 |
|
---|
355 | /* Init offsets. */
|
---|
356 | pHeader->u.v1.offBlocks = RT_ALIGN_32(sizeof(VDIPREHEADER) + sizeof(VDIHEADER1), VDI_GEOMETRY_SECTOR_SIZE);
|
---|
357 | pHeader->u.v1.offData = RT_ALIGN_32(pHeader->u.v1.offBlocks + (pHeader->u.v1.cBlocks * sizeof(VDIIMAGEBLOCKPOINTER)), VDI_GEOMETRY_SECTOR_SIZE);
|
---|
358 |
|
---|
359 | /* Init uuids. */
|
---|
360 | RTUuidCreate(&pHeader->u.v1.uuidCreate);
|
---|
361 | RTUuidClear(&pHeader->u.v1.uuidModify);
|
---|
362 | RTUuidClear(&pHeader->u.v1.uuidLinkage);
|
---|
363 | RTUuidClear(&pHeader->u.v1.uuidParentModify);
|
---|
364 |
|
---|
365 | /* Mark LCHS geometry not-calculated. */
|
---|
366 | pHeader->u.v1plus.LCHSGeometry.cCylinders = 0;
|
---|
367 | pHeader->u.v1plus.LCHSGeometry.cHeads = 0;
|
---|
368 | pHeader->u.v1plus.LCHSGeometry.cSectors = 0;
|
---|
369 | pHeader->u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
370 | }
|
---|
371 |
|
---|
372 | /**
|
---|
373 | * Internal: Check VDI header.
|
---|
374 | */
|
---|
375 | static int vdiValidateHeader(PVDIHEADER pHeader)
|
---|
376 | {
|
---|
377 | /* Check version-dependend header parameters. */
|
---|
378 | switch (GET_MAJOR_HEADER_VERSION(pHeader))
|
---|
379 | {
|
---|
380 | case 0:
|
---|
381 | {
|
---|
382 | /* Old header version. */
|
---|
383 | break;
|
---|
384 | }
|
---|
385 | case 1:
|
---|
386 | {
|
---|
387 | /* Current header version. */
|
---|
388 |
|
---|
389 | if (pHeader->u.v1.cbHeader < sizeof(VDIHEADER1))
|
---|
390 | {
|
---|
391 | LogRel(("VDI: v1 header size wrong (%d < %d)\n",
|
---|
392 | pHeader->u.v1.cbHeader, sizeof(VDIHEADER1)));
|
---|
393 | return VERR_VD_VDI_INVALID_HEADER;
|
---|
394 | }
|
---|
395 |
|
---|
396 | if (getImageBlocksOffset(pHeader) < (sizeof(VDIPREHEADER) + sizeof(VDIHEADER1)))
|
---|
397 | {
|
---|
398 | LogRel(("VDI: v1 blocks offset wrong (%d < %d)\n",
|
---|
399 | getImageBlocksOffset(pHeader), sizeof(VDIPREHEADER) + sizeof(VDIHEADER1)));
|
---|
400 | return VERR_VD_VDI_INVALID_HEADER;
|
---|
401 | }
|
---|
402 |
|
---|
403 | if (getImageDataOffset(pHeader) < (getImageBlocksOffset(pHeader) + getImageBlocks(pHeader) * sizeof(VDIIMAGEBLOCKPOINTER)))
|
---|
404 | {
|
---|
405 | LogRel(("VDI: v1 image data offset wrong (%d < %d)\n",
|
---|
406 | getImageDataOffset(pHeader), getImageBlocksOffset(pHeader) + getImageBlocks(pHeader) * sizeof(VDIIMAGEBLOCKPOINTER)));
|
---|
407 | return VERR_VD_VDI_INVALID_HEADER;
|
---|
408 | }
|
---|
409 |
|
---|
410 | break;
|
---|
411 | }
|
---|
412 | default:
|
---|
413 | /* Unsupported. */
|
---|
414 | return VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
415 | }
|
---|
416 |
|
---|
417 | /* Check common header parameters. */
|
---|
418 |
|
---|
419 | bool fFailed = false;
|
---|
420 |
|
---|
421 | if ( getImageType(pHeader) < VDI_IMAGE_TYPE_FIRST
|
---|
422 | || getImageType(pHeader) > VDI_IMAGE_TYPE_LAST)
|
---|
423 | {
|
---|
424 | LogRel(("VDI: bad image type %d\n", getImageType(pHeader)));
|
---|
425 | fFailed = true;
|
---|
426 | }
|
---|
427 |
|
---|
428 | if (getImageFlags(pHeader) & ~VD_VDI_IMAGE_FLAGS_MASK)
|
---|
429 | {
|
---|
430 | LogRel(("VDI: bad image flags %08x\n", getImageFlags(pHeader)));
|
---|
431 | fFailed = true;
|
---|
432 | }
|
---|
433 |
|
---|
434 | if ( getImageLCHSGeometry(pHeader)
|
---|
435 | && (getImageLCHSGeometry(pHeader))->cbSector != VDI_GEOMETRY_SECTOR_SIZE)
|
---|
436 | {
|
---|
437 | LogRel(("VDI: wrong sector size (%d != %d)\n",
|
---|
438 | (getImageLCHSGeometry(pHeader))->cbSector, VDI_GEOMETRY_SECTOR_SIZE));
|
---|
439 | fFailed = true;
|
---|
440 | }
|
---|
441 |
|
---|
442 | if ( getImageDiskSize(pHeader) == 0
|
---|
443 | || getImageBlockSize(pHeader) == 0
|
---|
444 | || getImageBlocks(pHeader) == 0
|
---|
445 | || getPowerOfTwo(getImageBlockSize(pHeader)) == 0)
|
---|
446 | {
|
---|
447 | LogRel(("VDI: wrong size (%lld, %d, %d, %d)\n",
|
---|
448 | getImageDiskSize(pHeader), getImageBlockSize(pHeader),
|
---|
449 | getImageBlocks(pHeader), getPowerOfTwo(getImageBlockSize(pHeader))));
|
---|
450 | fFailed = true;
|
---|
451 | }
|
---|
452 |
|
---|
453 | if (getImageBlocksAllocated(pHeader) > getImageBlocks(pHeader))
|
---|
454 | {
|
---|
455 | LogRel(("VDI: too many blocks allocated (%d > %d)\n"
|
---|
456 | " blocksize=%d disksize=%lld\n",
|
---|
457 | getImageBlocksAllocated(pHeader), getImageBlocks(pHeader),
|
---|
458 | getImageBlockSize(pHeader), getImageDiskSize(pHeader)));
|
---|
459 | fFailed = true;
|
---|
460 | }
|
---|
461 |
|
---|
462 | if ( getImageExtraBlockSize(pHeader) != 0
|
---|
463 | && getPowerOfTwo(getImageExtraBlockSize(pHeader)) == 0)
|
---|
464 | {
|
---|
465 | LogRel(("VDI: wrong extra size (%d, %d)\n",
|
---|
466 | getImageExtraBlockSize(pHeader), getPowerOfTwo(getImageExtraBlockSize(pHeader))));
|
---|
467 | fFailed = true;
|
---|
468 | }
|
---|
469 |
|
---|
470 | if ((uint64_t)getImageBlockSize(pHeader) * getImageBlocks(pHeader) < getImageDiskSize(pHeader))
|
---|
471 | {
|
---|
472 | LogRel(("VDI: wrong disk size (%d, %d, %lld)\n",
|
---|
473 | getImageBlockSize(pHeader), getImageBlocks(pHeader), getImageDiskSize(pHeader)));
|
---|
474 | fFailed = true;
|
---|
475 | }
|
---|
476 |
|
---|
477 | if (RTUuidIsNull(getImageCreationUUID(pHeader)))
|
---|
478 | {
|
---|
479 | LogRel(("VDI: uuid of creator is 0\n"));
|
---|
480 | fFailed = true;
|
---|
481 | }
|
---|
482 |
|
---|
483 | if (RTUuidIsNull(getImageModificationUUID(pHeader)))
|
---|
484 | {
|
---|
485 | LogRel(("VDI: uuid of modificator is 0\n"));
|
---|
486 | fFailed = true;
|
---|
487 | }
|
---|
488 |
|
---|
489 | return fFailed ? VERR_VD_VDI_INVALID_HEADER : VINF_SUCCESS;
|
---|
490 | }
|
---|
491 |
|
---|
492 | /**
|
---|
493 | * Internal: Set up VDIIMAGEDESC structure by image header.
|
---|
494 | */
|
---|
495 | static void vdiSetupImageDesc(PVDIIMAGEDESC pImage)
|
---|
496 | {
|
---|
497 | pImage->uImageFlags = getImageFlags(&pImage->Header);
|
---|
498 | pImage->uImageFlags |= vdiTranslateVDI2ImageFlags(getImageType(&pImage->Header));
|
---|
499 | pImage->offStartBlocks = getImageBlocksOffset(&pImage->Header);
|
---|
500 | pImage->offStartData = getImageDataOffset(&pImage->Header);
|
---|
501 | pImage->uBlockMask = getImageBlockSize(&pImage->Header) - 1;
|
---|
502 | pImage->uShiftOffset2Index = getPowerOfTwo(getImageBlockSize(&pImage->Header));
|
---|
503 | pImage->offStartBlockData = getImageExtraBlockSize(&pImage->Header);
|
---|
504 | pImage->cbTotalBlockData = pImage->offStartBlockData
|
---|
505 | + getImageBlockSize(&pImage->Header);
|
---|
506 | }
|
---|
507 |
|
---|
508 | /**
|
---|
509 | * Internal: Create VDI image file.
|
---|
510 | */
|
---|
511 | static int vdiCreateImage(PVDIIMAGEDESC pImage, uint64_t cbSize,
|
---|
512 | unsigned uImageFlags, const char *pszComment,
|
---|
513 | PCPDMMEDIAGEOMETRY pPCHSGeometry,
|
---|
514 | PCPDMMEDIAGEOMETRY pLCHSGeometry, PCRTUUID pUuid,
|
---|
515 | PFNVDPROGRESS pfnProgress, void *pvUser,
|
---|
516 | unsigned uPercentStart, unsigned uPercentSpan)
|
---|
517 | {
|
---|
518 | int rc;
|
---|
519 | uint64_t cbTotal;
|
---|
520 | uint64_t cbFill;
|
---|
521 | uint64_t uOff;
|
---|
522 |
|
---|
523 | /* Special check for comment length. */
|
---|
524 | if ( VALID_PTR(pszComment)
|
---|
525 | && strlen(pszComment) >= VDI_IMAGE_COMMENT_SIZE)
|
---|
526 | {
|
---|
527 | rc = vdiError(pImage, VERR_VD_VDI_COMMENT_TOO_LONG, RT_SRC_POS, N_("VDI: comment is too long for '%s'"), pImage->pszFilename);
|
---|
528 | goto out;
|
---|
529 | }
|
---|
530 | AssertPtr(pPCHSGeometry);
|
---|
531 | AssertPtr(pLCHSGeometry);
|
---|
532 |
|
---|
533 | pImage->pInterfaceError = VDInterfaceGet(pImage->pVDIfsDisk, VDINTERFACETYPE_ERROR);
|
---|
534 | if (pImage->pInterfaceError)
|
---|
535 | pImage->pInterfaceErrorCallbacks = VDGetInterfaceError(pImage->pInterfaceError);
|
---|
536 |
|
---|
537 | #ifdef VBOX_WITH_NEW_IO_CODE
|
---|
538 | /* Try to get I/O interface. */
|
---|
539 | pImage->pInterfaceIO = VDInterfaceGet(pImage->pVDIfsImage, VDINTERFACETYPE_IO);
|
---|
540 | AssertPtr(pImage->pInterfaceIO);
|
---|
541 | pImage->pInterfaceIOCallbacks = VDGetInterfaceIO(pImage->pInterfaceIO);
|
---|
542 | AssertPtr(pImage->pInterfaceIOCallbacks);
|
---|
543 | #endif
|
---|
544 |
|
---|
545 | vdiInitPreHeader(&pImage->PreHeader);
|
---|
546 | vdiInitHeader(&pImage->Header, uImageFlags, pszComment, cbSize, VDI_IMAGE_DEFAULT_BLOCK_SIZE, 0);
|
---|
547 | /* Save PCHS geometry. Not much work, and makes the flow of information
|
---|
548 | * quite a bit clearer - relying on the higher level isn't obvious. */
|
---|
549 | pImage->PCHSGeometry = *pPCHSGeometry;
|
---|
550 | /* Set LCHS geometry (legacy geometry is ignored for the current 1.1+). */
|
---|
551 | pImage->Header.u.v1plus.LCHSGeometry.cCylinders = pLCHSGeometry->cCylinders;
|
---|
552 | pImage->Header.u.v1plus.LCHSGeometry.cHeads = pLCHSGeometry->cHeads;
|
---|
553 | pImage->Header.u.v1plus.LCHSGeometry.cSectors = pLCHSGeometry->cSectors;
|
---|
554 | pImage->Header.u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
555 |
|
---|
556 | pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
|
---|
557 | if (!pImage->paBlocks)
|
---|
558 | {
|
---|
559 | rc = VERR_NO_MEMORY;
|
---|
560 | goto out;
|
---|
561 | }
|
---|
562 |
|
---|
563 | if (!(uImageFlags & VD_IMAGE_FLAGS_FIXED))
|
---|
564 | {
|
---|
565 | /* for growing images mark all blocks in paBlocks as free. */
|
---|
566 | for (unsigned i = 0; i < pImage->Header.u.v1.cBlocks; i++)
|
---|
567 | pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
|
---|
568 | }
|
---|
569 | else
|
---|
570 | {
|
---|
571 | /* for fixed images mark all blocks in paBlocks as allocated */
|
---|
572 | for (unsigned i = 0; i < pImage->Header.u.v1.cBlocks; i++)
|
---|
573 | pImage->paBlocks[i] = i;
|
---|
574 | pImage->Header.u.v1.cBlocksAllocated = pImage->Header.u.v1.cBlocks;
|
---|
575 | }
|
---|
576 |
|
---|
577 | /* Setup image parameters. */
|
---|
578 | vdiSetupImageDesc(pImage);
|
---|
579 |
|
---|
580 | /* Create image file. */
|
---|
581 | rc = vdiFileOpen(pImage, false /* fReadonly */, true /* fCreate */);
|
---|
582 | if (RT_FAILURE(rc))
|
---|
583 | {
|
---|
584 | rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: cannot create image '%s'"), pImage->pszFilename);
|
---|
585 | goto out;
|
---|
586 | }
|
---|
587 |
|
---|
588 | cbTotal = pImage->offStartData
|
---|
589 | + (uint64_t)getImageBlocks(&pImage->Header) * pImage->cbTotalBlockData;
|
---|
590 |
|
---|
591 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
592 | {
|
---|
593 | /* Check the free space on the disk and leave early if there is not
|
---|
594 | * sufficient space available. */
|
---|
595 | RTFOFF cbFree = 0;
|
---|
596 | rc = RTFsQuerySizes(pImage->pszFilename, NULL, &cbFree, NULL, NULL);
|
---|
597 | if (RT_SUCCESS(rc) /* ignore errors */ && ((uint64_t)cbFree < cbTotal))
|
---|
598 | {
|
---|
599 | rc = vdiError(pImage, VERR_DISK_FULL, RT_SRC_POS, N_("VDI: disk would overflow creating image '%s'"), pImage->pszFilename);
|
---|
600 | goto out;
|
---|
601 | }
|
---|
602 | }
|
---|
603 |
|
---|
604 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
605 | {
|
---|
606 | /*
|
---|
607 | * Allocate & commit whole file if fixed image, it must be more
|
---|
608 | * effective than expanding file by write operations.
|
---|
609 | */
|
---|
610 | rc = vdiFileSetSize(pImage, cbTotal);
|
---|
611 | }
|
---|
612 | else
|
---|
613 | {
|
---|
614 | /* Set file size to hold header and blocks array. */
|
---|
615 | rc = vdiFileSetSize(pImage, pImage->offStartData);
|
---|
616 | }
|
---|
617 | if (RT_FAILURE(rc))
|
---|
618 | {
|
---|
619 | rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: setting image size failed for '%s'"), pImage->pszFilename);
|
---|
620 | goto out;
|
---|
621 | }
|
---|
622 |
|
---|
623 | /* Use specified image uuid */
|
---|
624 | *getImageCreationUUID(&pImage->Header) = *pUuid;
|
---|
625 |
|
---|
626 | /* Generate image last-modify uuid */
|
---|
627 | RTUuidCreate(getImageModificationUUID(&pImage->Header));
|
---|
628 |
|
---|
629 | /* Write pre-header. */
|
---|
630 | rc = vdiFileWriteSync(pImage, 0, &pImage->PreHeader, sizeof(pImage->PreHeader), NULL);
|
---|
631 | if (RT_FAILURE(rc))
|
---|
632 | {
|
---|
633 | rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing pre-header failed for '%s'"), pImage->pszFilename);
|
---|
634 | goto out;
|
---|
635 | }
|
---|
636 |
|
---|
637 | /* Write header. */
|
---|
638 | rc = vdiFileWriteSync(pImage, sizeof(pImage->PreHeader), &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL);
|
---|
639 | if (RT_FAILURE(rc))
|
---|
640 | {
|
---|
641 | rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing header failed for '%s'"), pImage->pszFilename);
|
---|
642 | goto out;
|
---|
643 | }
|
---|
644 |
|
---|
645 | rc = vdiFileWriteSync(pImage, pImage->offStartBlocks,
|
---|
646 | pImage->paBlocks,
|
---|
647 | getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
648 | NULL);
|
---|
649 | if (RT_FAILURE(rc))
|
---|
650 | {
|
---|
651 | rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing block pointers failed for '%s'"), pImage->pszFilename);
|
---|
652 | goto out;
|
---|
653 | }
|
---|
654 |
|
---|
655 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
656 | {
|
---|
657 | /* Fill image with zeroes. We do this for every fixed-size image since on some systems
|
---|
658 | * (for example Windows Vista), it takes ages to write a block near the end of a sparse
|
---|
659 | * file and the guest could complain about an ATA timeout. */
|
---|
660 |
|
---|
661 | /** @todo Starting with Linux 2.6.23, there is an fallocate() system call.
|
---|
662 | * Currently supported file systems are ext4 and ocfs2. */
|
---|
663 |
|
---|
664 | /* Allocate a temporary zero-filled buffer. Use a bigger block size to optimize writing */
|
---|
665 | const size_t cbBuf = 128 * _1K;
|
---|
666 | void *pvBuf = RTMemTmpAllocZ(cbBuf);
|
---|
667 | if (!pvBuf)
|
---|
668 | {
|
---|
669 | rc = VERR_NO_MEMORY;
|
---|
670 | goto out;
|
---|
671 | }
|
---|
672 |
|
---|
673 | cbFill = (uint64_t)getImageBlocks(&pImage->Header) * pImage->cbTotalBlockData;
|
---|
674 | uOff = 0;
|
---|
675 | /* Write data to all image blocks. */
|
---|
676 | while (uOff < cbFill)
|
---|
677 | {
|
---|
678 | unsigned cbChunk = (unsigned)RT_MIN(cbFill, cbBuf);
|
---|
679 |
|
---|
680 | rc = vdiFileWriteSync(pImage, pImage->offStartData + uOff,
|
---|
681 | pvBuf, cbChunk, NULL);
|
---|
682 | if (RT_FAILURE(rc))
|
---|
683 | {
|
---|
684 | rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing block failed for '%s'"), pImage->pszFilename);
|
---|
685 | goto out;
|
---|
686 | }
|
---|
687 |
|
---|
688 | uOff += cbChunk;
|
---|
689 |
|
---|
690 | if (pfnProgress)
|
---|
691 | {
|
---|
692 | rc = pfnProgress(pvUser,
|
---|
693 | uPercentStart + uOff * uPercentSpan / cbFill);
|
---|
694 | if (RT_FAILURE(rc))
|
---|
695 | goto out;
|
---|
696 | }
|
---|
697 | }
|
---|
698 | RTMemTmpFree(pvBuf);
|
---|
699 | }
|
---|
700 |
|
---|
701 | out:
|
---|
702 | if (RT_SUCCESS(rc) && pfnProgress)
|
---|
703 | pfnProgress(pvUser, uPercentStart + uPercentSpan);
|
---|
704 |
|
---|
705 | if (RT_FAILURE(rc))
|
---|
706 | vdiFreeImage(pImage, rc != VERR_ALREADY_EXISTS);
|
---|
707 | return rc;
|
---|
708 | }
|
---|
709 |
|
---|
710 | /**
|
---|
711 | * Internal: Open a VDI image.
|
---|
712 | */
|
---|
713 | static int vdiOpenImage(PVDIIMAGEDESC pImage, unsigned uOpenFlags)
|
---|
714 | {
|
---|
715 | int rc;
|
---|
716 |
|
---|
717 | pImage->uOpenFlags = uOpenFlags;
|
---|
718 |
|
---|
719 | pImage->pInterfaceError = VDInterfaceGet(pImage->pVDIfsDisk, VDINTERFACETYPE_ERROR);
|
---|
720 | if (pImage->pInterfaceError)
|
---|
721 | pImage->pInterfaceErrorCallbacks = VDGetInterfaceError(pImage->pInterfaceError);
|
---|
722 |
|
---|
723 | #ifdef VBOX_WITH_NEW_IO_CODE
|
---|
724 | /* Try to get I/O interface. */
|
---|
725 | pImage->pInterfaceIO = VDInterfaceGet(pImage->pVDIfsImage, VDINTERFACETYPE_IO);
|
---|
726 | AssertPtr(pImage->pInterfaceIO);
|
---|
727 | pImage->pInterfaceIOCallbacks = VDGetInterfaceIO(pImage->pInterfaceIO);
|
---|
728 | AssertPtr(pImage->pInterfaceIOCallbacks);
|
---|
729 | #endif
|
---|
730 |
|
---|
731 | /*
|
---|
732 | * Open the image.
|
---|
733 | */
|
---|
734 | rc = vdiFileOpen(pImage, !!(uOpenFlags & VD_OPEN_FLAGS_READONLY), false /* fCreate */);
|
---|
735 | if (RT_FAILURE(rc))
|
---|
736 | {
|
---|
737 | /* Do NOT signal an appropriate error here, as the VD layer has the
|
---|
738 | * choice of retrying the open if it failed. */
|
---|
739 | goto out;
|
---|
740 | }
|
---|
741 |
|
---|
742 | /* Read pre-header. */
|
---|
743 | rc = vdiFileReadSync(pImage, 0, &pImage->PreHeader, sizeof(pImage->PreHeader),
|
---|
744 | NULL);
|
---|
745 | if (RT_FAILURE(rc))
|
---|
746 | {
|
---|
747 | rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading pre-header in '%s'"), pImage->pszFilename);
|
---|
748 | goto out;
|
---|
749 | }
|
---|
750 | rc = vdiValidatePreHeader(&pImage->PreHeader);
|
---|
751 | if (RT_FAILURE(rc))
|
---|
752 | {
|
---|
753 | rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: invalid pre-header in '%s'"), pImage->pszFilename);
|
---|
754 | goto out;
|
---|
755 | }
|
---|
756 |
|
---|
757 | /* Read header. */
|
---|
758 | pImage->Header.uVersion = pImage->PreHeader.u32Version;
|
---|
759 | switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
|
---|
760 | {
|
---|
761 | case 0:
|
---|
762 | rc = vdiFileReadSync(pImage, sizeof(pImage->PreHeader),
|
---|
763 | &pImage->Header.u.v0, sizeof(pImage->Header.u.v0),
|
---|
764 | NULL);
|
---|
765 | if (RT_FAILURE(rc))
|
---|
766 | {
|
---|
767 | rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading v0 header in '%s'"), pImage->pszFilename);
|
---|
768 | goto out;
|
---|
769 | }
|
---|
770 | break;
|
---|
771 | case 1:
|
---|
772 | rc = vdiFileReadSync(pImage, sizeof(pImage->PreHeader),
|
---|
773 | &pImage->Header.u.v1, sizeof(pImage->Header.u.v1),
|
---|
774 | NULL);
|
---|
775 | if (RT_FAILURE(rc))
|
---|
776 | {
|
---|
777 | rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading v1 header in '%s'"), pImage->pszFilename);
|
---|
778 | goto out;
|
---|
779 | }
|
---|
780 | /* Convert VDI 1.1 images to VDI 1.1+ on open in read/write mode.
|
---|
781 | * Conversion is harmless, as any VirtualBox version supporting VDI
|
---|
782 | * 1.1 doesn't touch fields it doesn't know about. */
|
---|
783 | if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
784 | && GET_MINOR_HEADER_VERSION(&pImage->Header) == 1
|
---|
785 | && pImage->Header.u.v1.cbHeader < sizeof(pImage->Header.u.v1plus))
|
---|
786 | {
|
---|
787 | pImage->Header.u.v1plus.cbHeader = sizeof(pImage->Header.u.v1plus);
|
---|
788 | /* Mark LCHS geometry not-calculated. */
|
---|
789 | pImage->Header.u.v1plus.LCHSGeometry.cCylinders = 0;
|
---|
790 | pImage->Header.u.v1plus.LCHSGeometry.cHeads = 0;
|
---|
791 | pImage->Header.u.v1plus.LCHSGeometry.cSectors = 0;
|
---|
792 | pImage->Header.u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
793 | }
|
---|
794 | else if (pImage->Header.u.v1.cbHeader >= sizeof(pImage->Header.u.v1plus))
|
---|
795 | {
|
---|
796 | /* Read the actual VDI 1.1+ header completely. */
|
---|
797 | rc = vdiFileReadSync(pImage, sizeof(pImage->PreHeader), &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL);
|
---|
798 | if (RT_FAILURE(rc))
|
---|
799 | {
|
---|
800 | rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading v1.1+ header in '%s'"), pImage->pszFilename);
|
---|
801 | goto out;
|
---|
802 | }
|
---|
803 | }
|
---|
804 | break;
|
---|
805 | default:
|
---|
806 | rc = vdiError(pImage, VERR_VD_VDI_UNSUPPORTED_VERSION, RT_SRC_POS, N_("VDI: unsupported major version %u in '%s'"), GET_MAJOR_HEADER_VERSION(&pImage->Header), pImage->pszFilename);
|
---|
807 | goto out;
|
---|
808 | }
|
---|
809 |
|
---|
810 | rc = vdiValidateHeader(&pImage->Header);
|
---|
811 | if (RT_FAILURE(rc))
|
---|
812 | {
|
---|
813 | rc = vdiError(pImage, VERR_VD_VDI_INVALID_HEADER, RT_SRC_POS, N_("VDI: invalid header in '%s'"), pImage->pszFilename);
|
---|
814 | goto out;
|
---|
815 | }
|
---|
816 |
|
---|
817 | /* Setup image parameters by header. */
|
---|
818 | vdiSetupImageDesc(pImage);
|
---|
819 |
|
---|
820 | /* Allocate memory for blocks array. */
|
---|
821 | pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
|
---|
822 | if (!pImage->paBlocks)
|
---|
823 | {
|
---|
824 | rc = VERR_NO_MEMORY;
|
---|
825 | goto out;
|
---|
826 | }
|
---|
827 |
|
---|
828 | /* Read blocks array. */
|
---|
829 | rc = vdiFileReadSync(pImage, pImage->offStartBlocks, pImage->paBlocks,
|
---|
830 | getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
831 | NULL);
|
---|
832 |
|
---|
833 | out:
|
---|
834 | if (RT_FAILURE(rc))
|
---|
835 | vdiFreeImage(pImage, false);
|
---|
836 | return rc;
|
---|
837 | }
|
---|
838 |
|
---|
839 | /**
|
---|
840 | * Internal: Save header to file.
|
---|
841 | */
|
---|
842 | static int vdiUpdateHeader(PVDIIMAGEDESC pImage)
|
---|
843 | {
|
---|
844 | int rc;
|
---|
845 | switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
|
---|
846 | {
|
---|
847 | case 0:
|
---|
848 | rc = vdiFileWriteSync(pImage, sizeof(VDIPREHEADER), &pImage->Header.u.v0, sizeof(pImage->Header.u.v0), NULL);
|
---|
849 | break;
|
---|
850 | case 1:
|
---|
851 | if (pImage->Header.u.v1plus.cbHeader < sizeof(pImage->Header.u.v1plus))
|
---|
852 | rc = vdiFileWriteSync(pImage, sizeof(VDIPREHEADER), &pImage->Header.u.v1, sizeof(pImage->Header.u.v1), NULL);
|
---|
853 | else
|
---|
854 | rc = vdiFileWriteSync(pImage, sizeof(VDIPREHEADER), &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL);
|
---|
855 | break;
|
---|
856 | default:
|
---|
857 | rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
858 | break;
|
---|
859 | }
|
---|
860 | AssertMsgRC(rc, ("vdiUpdateHeader failed, filename=\"%s\" rc=%Rrc\n", pImage->pszFilename, rc));
|
---|
861 | return rc;
|
---|
862 | }
|
---|
863 |
|
---|
864 | /**
|
---|
865 | * Internal: Save header to file - async version.
|
---|
866 | */
|
---|
867 | static int vdiUpdateHeaderAsync(PVDIIMAGEDESC pImage, PVDIOCTX pIoCtx)
|
---|
868 | {
|
---|
869 | int rc;
|
---|
870 | switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
|
---|
871 | {
|
---|
872 | case 0:
|
---|
873 | rc = vdiFileWriteMetaAsync(pImage, sizeof(VDIPREHEADER),
|
---|
874 | &pImage->Header.u.v0, sizeof(pImage->Header.u.v0),
|
---|
875 | pIoCtx, NULL, NULL);
|
---|
876 | break;
|
---|
877 | case 1:
|
---|
878 | if (pImage->Header.u.v1plus.cbHeader < sizeof(pImage->Header.u.v1plus))
|
---|
879 | rc = vdiFileWriteMetaAsync(pImage, sizeof(VDIPREHEADER),
|
---|
880 | &pImage->Header.u.v1, sizeof(pImage->Header.u.v1),
|
---|
881 | pIoCtx, NULL, NULL);
|
---|
882 | else
|
---|
883 | rc = vdiFileWriteMetaAsync(pImage, sizeof(VDIPREHEADER),
|
---|
884 | &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus),
|
---|
885 | pIoCtx, NULL, NULL);
|
---|
886 | break;
|
---|
887 | default:
|
---|
888 | rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
889 | break;
|
---|
890 | }
|
---|
891 | AssertMsgRC(rc, ("vdiUpdateHeader failed, filename=\"%s\" rc=%Rrc\n", pImage->pszFilename, rc));
|
---|
892 | return rc;
|
---|
893 | }
|
---|
894 |
|
---|
895 | /**
|
---|
896 | * Internal: Save block pointer to file, save header to file.
|
---|
897 | */
|
---|
898 | static int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock)
|
---|
899 | {
|
---|
900 | /* Update image header. */
|
---|
901 | int rc = vdiUpdateHeader(pImage);
|
---|
902 | if (RT_SUCCESS(rc))
|
---|
903 | {
|
---|
904 | /* write only one block pointer. */
|
---|
905 | rc = vdiFileWriteSync(pImage,
|
---|
906 | pImage->offStartBlocks + uBlock * sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
907 | &pImage->paBlocks[uBlock],
|
---|
908 | sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
909 | NULL);
|
---|
910 | AssertMsgRC(rc, ("vdiUpdateBlockInfo failed to update block=%u, filename=\"%s\", rc=%Rrc\n",
|
---|
911 | uBlock, pImage->pszFilename, rc));
|
---|
912 | }
|
---|
913 | return rc;
|
---|
914 | }
|
---|
915 |
|
---|
916 | /**
|
---|
917 | * Internal: Save block pointer to file, save header to file - async version.
|
---|
918 | */
|
---|
919 | static int vdiUpdateBlockInfoAsync(PVDIIMAGEDESC pImage, unsigned uBlock,
|
---|
920 | PVDIOCTX pIoCtx)
|
---|
921 | {
|
---|
922 | /* Update image header. */
|
---|
923 | int rc = vdiUpdateHeaderAsync(pImage, pIoCtx);
|
---|
924 | if (RT_SUCCESS(rc))
|
---|
925 | {
|
---|
926 | /* write only one block pointer. */
|
---|
927 | rc = vdiFileWriteMetaAsync(pImage,
|
---|
928 | pImage->offStartBlocks + uBlock * sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
929 | &pImage->paBlocks[uBlock],
|
---|
930 | sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
931 | pIoCtx,
|
---|
932 | NULL, NULL);
|
---|
933 | AssertMsgRC(rc, ("vdiUpdateBlockInfo failed to update block=%u, filename=\"%s\", rc=%Rrc\n",
|
---|
934 | uBlock, pImage->pszFilename, rc));
|
---|
935 | }
|
---|
936 | return rc;
|
---|
937 | }
|
---|
938 |
|
---|
939 | /**
|
---|
940 | * Internal: Flush the image file to disk.
|
---|
941 | */
|
---|
942 | static void vdiFlushImage(PVDIIMAGEDESC pImage)
|
---|
943 | {
|
---|
944 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
945 | {
|
---|
946 | /* Save header. */
|
---|
947 | int rc = vdiUpdateHeader(pImage);
|
---|
948 | AssertMsgRC(rc, ("vdiUpdateHeader() failed, filename=\"%s\", rc=%Rrc\n",
|
---|
949 | pImage->pszFilename, rc));
|
---|
950 | vdiFileFlushSync(pImage);
|
---|
951 | }
|
---|
952 | }
|
---|
953 |
|
---|
954 | /**
|
---|
955 | * Internal: Flush the image file to disk - async version.
|
---|
956 | */
|
---|
957 | static void vdiFlushImageAsync(PVDIIMAGEDESC pImage, PVDIOCTX pIoCtx)
|
---|
958 | {
|
---|
959 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
960 | {
|
---|
961 | /* Save header. */
|
---|
962 | int rc = vdiUpdateHeaderAsync(pImage, pIoCtx);
|
---|
963 | AssertMsgRC(rc, ("vdiUpdateHeaderAsync() failed, filename=\"%s\", rc=%Rrc\n",
|
---|
964 | pImage->pszFilename, rc));
|
---|
965 | rc = vdiFileFlushAsync(pImage, pIoCtx);
|
---|
966 | AssertMsgRC(rc, ("Flushing data to disk failed rc=%Rrc\n", rc));
|
---|
967 | }
|
---|
968 | }
|
---|
969 |
|
---|
970 | /**
|
---|
971 | * Internal: Free all allocated space for representing an image, and optionally
|
---|
972 | * delete the image from disk.
|
---|
973 | */
|
---|
974 | static void vdiFreeImage(PVDIIMAGEDESC pImage, bool fDelete)
|
---|
975 | {
|
---|
976 | AssertPtr(pImage);
|
---|
977 |
|
---|
978 | if (vdiFileOpened(pImage))
|
---|
979 | {
|
---|
980 | vdiFlushImage(pImage);
|
---|
981 | vdiFileClose(pImage);
|
---|
982 | }
|
---|
983 | if (pImage->paBlocks)
|
---|
984 | {
|
---|
985 | RTMemFree(pImage->paBlocks);
|
---|
986 | pImage->paBlocks = NULL;
|
---|
987 | }
|
---|
988 | if (fDelete && pImage->pszFilename)
|
---|
989 | RTFileDelete(pImage->pszFilename);
|
---|
990 | }
|
---|
991 |
|
---|
992 |
|
---|
993 | /** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
|
---|
994 | static int vdiCheckIfValid(const char *pszFilename, PVDINTERFACE pVDIfsDisk)
|
---|
995 | {
|
---|
996 | LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
|
---|
997 | int rc = VINF_SUCCESS;
|
---|
998 | PVDIIMAGEDESC pImage;
|
---|
999 |
|
---|
1000 | if ( !VALID_PTR(pszFilename)
|
---|
1001 | || !*pszFilename)
|
---|
1002 | {
|
---|
1003 | rc = VERR_INVALID_PARAMETER;
|
---|
1004 | goto out;
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
|
---|
1008 | if (!pImage)
|
---|
1009 | {
|
---|
1010 | rc = VERR_NO_MEMORY;
|
---|
1011 | goto out;
|
---|
1012 | }
|
---|
1013 | pImage->pszFilename = pszFilename;
|
---|
1014 | #ifndef VBOX_WITH_NEW_IO_CODE
|
---|
1015 | pImage->File = NIL_RTFILE;
|
---|
1016 | #else
|
---|
1017 | pImage->pStorage = NULL;
|
---|
1018 | #endif
|
---|
1019 | pImage->paBlocks = NULL;
|
---|
1020 | pImage->pVDIfsDisk = pVDIfsDisk;
|
---|
1021 | /* For the async I/O interface which is normally retrieved from the image interface list. */
|
---|
1022 | pImage->pVDIfsImage = pVDIfsDisk;
|
---|
1023 |
|
---|
1024 | rc = vdiOpenImage(pImage, VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_READONLY);
|
---|
1025 | vdiFreeImage(pImage, false);
|
---|
1026 | RTMemFree(pImage);
|
---|
1027 |
|
---|
1028 | out:
|
---|
1029 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1030 | return rc;
|
---|
1031 | }
|
---|
1032 |
|
---|
1033 | /** @copydoc VBOXHDDBACKEND::pfnOpen */
|
---|
1034 | static int vdiOpen(const char *pszFilename, unsigned uOpenFlags,
|
---|
1035 | PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
|
---|
1036 | void **ppBackendData)
|
---|
1037 | {
|
---|
1038 | LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, ppBackendData));
|
---|
1039 | int rc;
|
---|
1040 | PVDIIMAGEDESC pImage;
|
---|
1041 |
|
---|
1042 | /* Check open flags. All valid flags are supported. */
|
---|
1043 | if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
|
---|
1044 | {
|
---|
1045 | rc = VERR_INVALID_PARAMETER;
|
---|
1046 | goto out;
|
---|
1047 | }
|
---|
1048 |
|
---|
1049 | /* Check remaining arguments. */
|
---|
1050 | if ( !VALID_PTR(pszFilename)
|
---|
1051 | || !*pszFilename)
|
---|
1052 | {
|
---|
1053 | rc = VERR_INVALID_PARAMETER;
|
---|
1054 | goto out;
|
---|
1055 | }
|
---|
1056 |
|
---|
1057 | pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
|
---|
1058 | if (!pImage)
|
---|
1059 | {
|
---|
1060 | rc = VERR_NO_MEMORY;
|
---|
1061 | goto out;
|
---|
1062 | }
|
---|
1063 | pImage->pszFilename = pszFilename;
|
---|
1064 | #ifndef VBOX_WITH_NEW_IO_CODE
|
---|
1065 | pImage->File = NIL_RTFILE;
|
---|
1066 | #else
|
---|
1067 | pImage->pStorage = NULL;
|
---|
1068 | #endif
|
---|
1069 | pImage->paBlocks = NULL;
|
---|
1070 | pImage->pVDIfsDisk = pVDIfsDisk;
|
---|
1071 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
1072 |
|
---|
1073 | rc = vdiOpenImage(pImage, uOpenFlags);
|
---|
1074 | if (RT_SUCCESS(rc))
|
---|
1075 | *ppBackendData = pImage;
|
---|
1076 |
|
---|
1077 | out:
|
---|
1078 | LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
|
---|
1079 | return rc;
|
---|
1080 | }
|
---|
1081 |
|
---|
1082 | /** @copydoc VBOXHDDBACKEND::pfnCreate */
|
---|
1083 | static int vdiCreate(const char *pszFilename, uint64_t cbSize,
|
---|
1084 | unsigned uImageFlags, const char *pszComment,
|
---|
1085 | PCPDMMEDIAGEOMETRY pPCHSGeometry,
|
---|
1086 | PCPDMMEDIAGEOMETRY pLCHSGeometry, PCRTUUID pUuid,
|
---|
1087 | unsigned uOpenFlags, unsigned uPercentStart,
|
---|
1088 | unsigned uPercentSpan, PVDINTERFACE pVDIfsDisk,
|
---|
1089 | PVDINTERFACE pVDIfsImage, PVDINTERFACE pVDIfsOperation,
|
---|
1090 | void **ppBackendData)
|
---|
1091 | {
|
---|
1092 | 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", pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, ppBackendData));
|
---|
1093 | int rc;
|
---|
1094 | PVDIIMAGEDESC pImage;
|
---|
1095 |
|
---|
1096 | PFNVDPROGRESS pfnProgress = NULL;
|
---|
1097 | void *pvUser = NULL;
|
---|
1098 | PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
|
---|
1099 | VDINTERFACETYPE_PROGRESS);
|
---|
1100 | PVDINTERFACEPROGRESS pCbProgress = NULL;
|
---|
1101 | if (pIfProgress)
|
---|
1102 | {
|
---|
1103 | pCbProgress = VDGetInterfaceProgress(pIfProgress);
|
---|
1104 | if (pCbProgress)
|
---|
1105 | pfnProgress = pCbProgress->pfnProgress;
|
---|
1106 | pvUser = pIfProgress->pvUser;
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | /* Check open flags. All valid flags are supported. */
|
---|
1110 | if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
|
---|
1111 | {
|
---|
1112 | rc = VERR_INVALID_PARAMETER;
|
---|
1113 | goto out;
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | /* Check size. Maximum 2PB-3M (to be on the safe side). No tricks with
|
---|
1117 | * adjusting the 1M block size so far, which would extend the size. */
|
---|
1118 | if ( !cbSize
|
---|
1119 | || cbSize >= _1P * 2 - _1M * 3)
|
---|
1120 | {
|
---|
1121 | rc = VERR_VD_INVALID_SIZE;
|
---|
1122 | goto out;
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 | /* Check remaining arguments. */
|
---|
1126 | if ( !VALID_PTR(pszFilename)
|
---|
1127 | || !*pszFilename
|
---|
1128 | || cbSize < VDI_IMAGE_DEFAULT_BLOCK_SIZE
|
---|
1129 | || !VALID_PTR(pPCHSGeometry)
|
---|
1130 | || !VALID_PTR(pLCHSGeometry))
|
---|
1131 | {
|
---|
1132 | rc = VERR_INVALID_PARAMETER;
|
---|
1133 | goto out;
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 | pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
|
---|
1137 | if (!pImage)
|
---|
1138 | {
|
---|
1139 | rc = VERR_NO_MEMORY;
|
---|
1140 | goto out;
|
---|
1141 | }
|
---|
1142 | pImage->pszFilename = pszFilename;
|
---|
1143 | #ifndef VBOX_WITH_NEW_IO_CODE
|
---|
1144 | pImage->File = NIL_RTFILE;
|
---|
1145 | #else
|
---|
1146 | pImage->pStorage = NULL;
|
---|
1147 | #endif
|
---|
1148 | pImage->paBlocks = NULL;
|
---|
1149 | pImage->pVDIfsDisk = pVDIfsDisk;
|
---|
1150 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
1151 |
|
---|
1152 | rc = vdiCreateImage(pImage, cbSize, uImageFlags, pszComment,
|
---|
1153 | pPCHSGeometry, pLCHSGeometry, pUuid,
|
---|
1154 | pfnProgress, pvUser, uPercentStart, uPercentSpan);
|
---|
1155 | if (RT_SUCCESS(rc))
|
---|
1156 | {
|
---|
1157 | /* So far the image is opened in read/write mode. Make sure the
|
---|
1158 | * image is opened in read-only mode if the caller requested that. */
|
---|
1159 | if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
1160 | {
|
---|
1161 | vdiFreeImage(pImage, false);
|
---|
1162 | rc = vdiOpenImage(pImage, uOpenFlags);
|
---|
1163 | if (RT_FAILURE(rc))
|
---|
1164 | goto out;
|
---|
1165 | }
|
---|
1166 | *ppBackendData = pImage;
|
---|
1167 | }
|
---|
1168 | else
|
---|
1169 | RTMemFree(pImage);
|
---|
1170 |
|
---|
1171 | out:
|
---|
1172 | LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
|
---|
1173 | return rc;
|
---|
1174 | }
|
---|
1175 |
|
---|
1176 | /** @copydoc VBOXHDDBACKEND::pfnRename */
|
---|
1177 | static int vdiRename(void *pBackendData, const char *pszFilename)
|
---|
1178 | {
|
---|
1179 | LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
|
---|
1180 |
|
---|
1181 | int rc = VINF_SUCCESS;
|
---|
1182 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1183 |
|
---|
1184 | /* Check arguments. */
|
---|
1185 | if ( !pImage
|
---|
1186 | || !pszFilename
|
---|
1187 | || !*pszFilename)
|
---|
1188 | {
|
---|
1189 | rc = VERR_INVALID_PARAMETER;
|
---|
1190 | goto out;
|
---|
1191 | }
|
---|
1192 |
|
---|
1193 | /* Close the image. */
|
---|
1194 | vdiFreeImage(pImage, false);
|
---|
1195 |
|
---|
1196 | /* Rename the file. */
|
---|
1197 | rc = RTFileMove(pImage->pszFilename, pszFilename, 0);
|
---|
1198 | if (RT_FAILURE(rc))
|
---|
1199 | {
|
---|
1200 | /* The move failed, try to reopen the original image. */
|
---|
1201 | int rc2 = vdiOpenImage(pImage, pImage->uOpenFlags);
|
---|
1202 | if (RT_FAILURE(rc2))
|
---|
1203 | rc = rc2;
|
---|
1204 |
|
---|
1205 | goto out;
|
---|
1206 | }
|
---|
1207 |
|
---|
1208 | /* Update pImage with the new information. */
|
---|
1209 | pImage->pszFilename = pszFilename;
|
---|
1210 |
|
---|
1211 | /* Open the new image. */
|
---|
1212 | rc = vdiOpenImage(pImage, pImage->uOpenFlags);
|
---|
1213 | if (RT_FAILURE(rc))
|
---|
1214 | goto out;
|
---|
1215 |
|
---|
1216 | out:
|
---|
1217 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1218 | return rc;
|
---|
1219 | }
|
---|
1220 |
|
---|
1221 | /** @copydoc VBOXHDDBACKEND::pfnClose */
|
---|
1222 | static int vdiClose(void *pBackendData, bool fDelete)
|
---|
1223 | {
|
---|
1224 | LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
|
---|
1225 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1226 | int rc = VINF_SUCCESS;
|
---|
1227 |
|
---|
1228 | /* Freeing a never allocated image (e.g. because the open failed) is
|
---|
1229 | * not signalled as an error. After all nothing bad happens. */
|
---|
1230 | if (pImage)
|
---|
1231 | {
|
---|
1232 | vdiFreeImage(pImage, fDelete);
|
---|
1233 | RTMemFree(pImage);
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1237 | return rc;
|
---|
1238 | }
|
---|
1239 |
|
---|
1240 | /** @copydoc VBOXHDDBACKEND::pfnRead */
|
---|
1241 | static int vdiRead(void *pBackendData, uint64_t uOffset, void *pvBuf,
|
---|
1242 | size_t cbToRead, size_t *pcbActuallyRead)
|
---|
1243 | {
|
---|
1244 | LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToRead=%zu pcbActuallyRead=%#p\n", pBackendData, uOffset, pvBuf, cbToRead, pcbActuallyRead));
|
---|
1245 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1246 | unsigned uBlock;
|
---|
1247 | unsigned offRead;
|
---|
1248 | int rc;
|
---|
1249 |
|
---|
1250 | AssertPtr(pImage);
|
---|
1251 | Assert(!(uOffset % 512));
|
---|
1252 | Assert(!(cbToRead % 512));
|
---|
1253 |
|
---|
1254 | if ( uOffset + cbToRead > getImageDiskSize(&pImage->Header)
|
---|
1255 | || !VALID_PTR(pvBuf)
|
---|
1256 | || !cbToRead)
|
---|
1257 | {
|
---|
1258 | rc = VERR_INVALID_PARAMETER;
|
---|
1259 | goto out;
|
---|
1260 | }
|
---|
1261 |
|
---|
1262 | /* Calculate starting block number and offset inside it. */
|
---|
1263 | uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
|
---|
1264 | offRead = (unsigned)uOffset & pImage->uBlockMask;
|
---|
1265 |
|
---|
1266 | /* Clip read range to at most the rest of the block. */
|
---|
1267 | cbToRead = RT_MIN(cbToRead, getImageBlockSize(&pImage->Header) - offRead);
|
---|
1268 | Assert(!(cbToRead % 512));
|
---|
1269 |
|
---|
1270 | if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE)
|
---|
1271 | rc = VERR_VD_BLOCK_FREE;
|
---|
1272 | else if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
|
---|
1273 | {
|
---|
1274 | memset(pvBuf, 0, cbToRead);
|
---|
1275 | rc = VINF_SUCCESS;
|
---|
1276 | }
|
---|
1277 | else
|
---|
1278 | {
|
---|
1279 | /* Block present in image file, read relevant data. */
|
---|
1280 | uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
|
---|
1281 | + (pImage->offStartData + pImage->offStartBlockData + offRead);
|
---|
1282 | rc = vdiFileReadSync(pImage, u64Offset, pvBuf, cbToRead, NULL);
|
---|
1283 | }
|
---|
1284 |
|
---|
1285 | if (pcbActuallyRead)
|
---|
1286 | *pcbActuallyRead = cbToRead;
|
---|
1287 |
|
---|
1288 | out:
|
---|
1289 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1290 | return rc;
|
---|
1291 | }
|
---|
1292 |
|
---|
1293 | /**@copydoc VBOXHDDBACKEND::pfnWrite */
|
---|
1294 | static int vdiWrite(void *pBackendData, uint64_t uOffset, const void *pvBuf,
|
---|
1295 | size_t cbToWrite, size_t *pcbWriteProcess,
|
---|
1296 | size_t *pcbPreRead, size_t *pcbPostRead, unsigned fWrite)
|
---|
1297 | {
|
---|
1298 | LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n", pBackendData, uOffset, pvBuf, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
|
---|
1299 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1300 | unsigned uBlock;
|
---|
1301 | unsigned offWrite;
|
---|
1302 | int rc = VINF_SUCCESS;
|
---|
1303 |
|
---|
1304 | AssertPtr(pImage);
|
---|
1305 | Assert(!(uOffset % 512));
|
---|
1306 | Assert(!(cbToWrite % 512));
|
---|
1307 |
|
---|
1308 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
1309 | {
|
---|
1310 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
1311 | goto out;
|
---|
1312 | }
|
---|
1313 |
|
---|
1314 | if (!VALID_PTR(pvBuf) || !cbToWrite)
|
---|
1315 | {
|
---|
1316 | rc = VERR_INVALID_PARAMETER;
|
---|
1317 | goto out;
|
---|
1318 | }
|
---|
1319 |
|
---|
1320 | /* No size check here, will do that later. For dynamic images which are
|
---|
1321 | * not multiples of the block size in length, this would prevent writing to
|
---|
1322 | * the last grain. */
|
---|
1323 |
|
---|
1324 | /* Calculate starting block number and offset inside it. */
|
---|
1325 | uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
|
---|
1326 | offWrite = (unsigned)uOffset & pImage->uBlockMask;
|
---|
1327 |
|
---|
1328 | /* Clip write range to at most the rest of the block. */
|
---|
1329 | cbToWrite = RT_MIN(cbToWrite, getImageBlockSize(&pImage->Header) - offWrite);
|
---|
1330 | Assert(!(cbToWrite % 512));
|
---|
1331 |
|
---|
1332 | do
|
---|
1333 | {
|
---|
1334 | if (!IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
|
---|
1335 | {
|
---|
1336 | /* Block is either free or zero. */
|
---|
1337 | if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_ZEROES)
|
---|
1338 | && ( pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO
|
---|
1339 | || cbToWrite == getImageBlockSize(&pImage->Header)))
|
---|
1340 | {
|
---|
1341 | /* If the destination block is unallocated at this point, it's
|
---|
1342 | * either a zero block or a block which hasn't been used so far
|
---|
1343 | * (which also means that it's a zero block. Don't need to write
|
---|
1344 | * anything to this block if the data consists of just zeroes. */
|
---|
1345 | Assert(!(cbToWrite % 4));
|
---|
1346 | Assert(cbToWrite * 8 <= UINT32_MAX);
|
---|
1347 | if (ASMBitFirstSet((volatile void *)pvBuf, (uint32_t)cbToWrite * 8) == -1)
|
---|
1348 | {
|
---|
1349 | pImage->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
|
---|
1350 | break;
|
---|
1351 | }
|
---|
1352 | }
|
---|
1353 |
|
---|
1354 | if (cbToWrite == getImageBlockSize(&pImage->Header))
|
---|
1355 | {
|
---|
1356 | /* Full block write to previously unallocated block.
|
---|
1357 | * Allocate block and write data. */
|
---|
1358 | Assert(!offWrite);
|
---|
1359 | unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
|
---|
1360 | uint64_t u64Offset = (uint64_t)cBlocksAllocated * pImage->cbTotalBlockData
|
---|
1361 | + (pImage->offStartData + pImage->offStartBlockData);
|
---|
1362 | rc = vdiFileWriteSync(pImage, u64Offset, pvBuf, cbToWrite, NULL);
|
---|
1363 | if (RT_FAILURE(rc))
|
---|
1364 | goto out;
|
---|
1365 | pImage->paBlocks[uBlock] = cBlocksAllocated;
|
---|
1366 | setImageBlocksAllocated(&pImage->Header, cBlocksAllocated + 1);
|
---|
1367 |
|
---|
1368 | rc = vdiUpdateBlockInfo(pImage, uBlock);
|
---|
1369 | if (RT_FAILURE(rc))
|
---|
1370 | goto out;
|
---|
1371 |
|
---|
1372 | *pcbPreRead = 0;
|
---|
1373 | *pcbPostRead = 0;
|
---|
1374 | }
|
---|
1375 | else
|
---|
1376 | {
|
---|
1377 | /* Trying to do a partial write to an unallocated block. Don't do
|
---|
1378 | * anything except letting the upper layer know what to do. */
|
---|
1379 | *pcbPreRead = offWrite % getImageBlockSize(&pImage->Header);
|
---|
1380 | *pcbPostRead = getImageBlockSize(&pImage->Header) - cbToWrite - *pcbPreRead;
|
---|
1381 | rc = VERR_VD_BLOCK_FREE;
|
---|
1382 | }
|
---|
1383 | }
|
---|
1384 | else
|
---|
1385 | {
|
---|
1386 | /* Block present in image file, write relevant data. */
|
---|
1387 | uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
|
---|
1388 | + (pImage->offStartData + pImage->offStartBlockData + offWrite);
|
---|
1389 | rc = vdiFileWriteSync(pImage, u64Offset, pvBuf, cbToWrite, NULL);
|
---|
1390 | }
|
---|
1391 | } while (0);
|
---|
1392 |
|
---|
1393 | if (pcbWriteProcess)
|
---|
1394 | *pcbWriteProcess = cbToWrite;
|
---|
1395 |
|
---|
1396 | out:
|
---|
1397 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1398 | return rc;
|
---|
1399 | }
|
---|
1400 |
|
---|
1401 | /** @copydoc VBOXHDDBACKEND::pfnFlush */
|
---|
1402 | static int vdiFlush(void *pBackendData)
|
---|
1403 | {
|
---|
1404 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
1405 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1406 | int rc = VINF_SUCCESS;
|
---|
1407 |
|
---|
1408 | Assert(pImage);
|
---|
1409 |
|
---|
1410 | vdiFlushImage(pImage);
|
---|
1411 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1412 | return rc;
|
---|
1413 | }
|
---|
1414 |
|
---|
1415 | /** @copydoc VBOXHDDBACKEND::pfnGetVersion */
|
---|
1416 | static unsigned vdiGetVersion(void *pBackendData)
|
---|
1417 | {
|
---|
1418 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
1419 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1420 | unsigned uVersion;
|
---|
1421 |
|
---|
1422 | AssertPtr(pImage);
|
---|
1423 |
|
---|
1424 | if (pImage)
|
---|
1425 | uVersion = pImage->PreHeader.u32Version;
|
---|
1426 | else
|
---|
1427 | uVersion = 0;
|
---|
1428 |
|
---|
1429 | LogFlowFunc(("returns %#x\n", uVersion));
|
---|
1430 | return uVersion;
|
---|
1431 | }
|
---|
1432 |
|
---|
1433 | /** @copydoc VBOXHDDBACKEND::pfnGetSize */
|
---|
1434 | static uint64_t vdiGetSize(void *pBackendData)
|
---|
1435 | {
|
---|
1436 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
1437 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1438 | uint64_t cbSize;
|
---|
1439 |
|
---|
1440 | AssertPtr(pImage);
|
---|
1441 |
|
---|
1442 | if (pImage)
|
---|
1443 | cbSize = getImageDiskSize(&pImage->Header);
|
---|
1444 | else
|
---|
1445 | cbSize = 0;
|
---|
1446 |
|
---|
1447 | LogFlowFunc(("returns %llu\n", cbSize));
|
---|
1448 | return cbSize;
|
---|
1449 | }
|
---|
1450 |
|
---|
1451 | /** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
|
---|
1452 | static uint64_t vdiGetFileSize(void *pBackendData)
|
---|
1453 | {
|
---|
1454 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
1455 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1456 | uint64_t cb = 0;
|
---|
1457 |
|
---|
1458 | AssertPtr(pImage);
|
---|
1459 |
|
---|
1460 | if (pImage)
|
---|
1461 | {
|
---|
1462 | uint64_t cbFile;
|
---|
1463 | if (vdiFileOpened(pImage))
|
---|
1464 | {
|
---|
1465 | int rc = vdiFileGetSize(pImage, &cbFile);
|
---|
1466 | if (RT_SUCCESS(rc))
|
---|
1467 | cb += cbFile;
|
---|
1468 | }
|
---|
1469 | }
|
---|
1470 |
|
---|
1471 | LogFlowFunc(("returns %lld\n", cb));
|
---|
1472 | return cb;
|
---|
1473 | }
|
---|
1474 |
|
---|
1475 | /** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
|
---|
1476 | static int vdiGetPCHSGeometry(void *pBackendData,
|
---|
1477 | PPDMMEDIAGEOMETRY pPCHSGeometry)
|
---|
1478 | {
|
---|
1479 | LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
|
---|
1480 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1481 | int rc;
|
---|
1482 |
|
---|
1483 | AssertPtr(pImage);
|
---|
1484 |
|
---|
1485 | if (pImage)
|
---|
1486 | {
|
---|
1487 | if (pImage->PCHSGeometry.cCylinders)
|
---|
1488 | {
|
---|
1489 | *pPCHSGeometry = pImage->PCHSGeometry;
|
---|
1490 | rc = VINF_SUCCESS;
|
---|
1491 | }
|
---|
1492 | else
|
---|
1493 | rc = VERR_VD_GEOMETRY_NOT_SET;
|
---|
1494 | }
|
---|
1495 | else
|
---|
1496 | rc = VERR_VD_NOT_OPENED;
|
---|
1497 |
|
---|
1498 | LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
|
---|
1499 | return rc;
|
---|
1500 | }
|
---|
1501 |
|
---|
1502 | /** @copydoc VBOXHDDBACKEND::pfnSetPCHSGeometry */
|
---|
1503 | static int vdiSetPCHSGeometry(void *pBackendData,
|
---|
1504 | PCPDMMEDIAGEOMETRY pPCHSGeometry)
|
---|
1505 | {
|
---|
1506 | LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
|
---|
1507 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1508 | int rc;
|
---|
1509 |
|
---|
1510 | AssertPtr(pImage);
|
---|
1511 |
|
---|
1512 | if (pImage)
|
---|
1513 | {
|
---|
1514 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
1515 | {
|
---|
1516 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
1517 | goto out;
|
---|
1518 | }
|
---|
1519 |
|
---|
1520 | pImage->PCHSGeometry = *pPCHSGeometry;
|
---|
1521 | rc = VINF_SUCCESS;
|
---|
1522 | }
|
---|
1523 | else
|
---|
1524 | rc = VERR_VD_NOT_OPENED;
|
---|
1525 |
|
---|
1526 | out:
|
---|
1527 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1528 | return rc;
|
---|
1529 | }
|
---|
1530 |
|
---|
1531 | /** @copydoc VBOXHDDBACKEND::pfnGetLCHSGeometry */
|
---|
1532 | static int vdiGetLCHSGeometry(void *pBackendData,
|
---|
1533 | PPDMMEDIAGEOMETRY pLCHSGeometry)
|
---|
1534 | {
|
---|
1535 | LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
|
---|
1536 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1537 | int rc;
|
---|
1538 |
|
---|
1539 | AssertPtr(pImage);
|
---|
1540 |
|
---|
1541 | if (pImage)
|
---|
1542 | {
|
---|
1543 | VDIDISKGEOMETRY DummyGeo = { 0, 0, 0, VDI_GEOMETRY_SECTOR_SIZE };
|
---|
1544 | PVDIDISKGEOMETRY pGeometry = getImageLCHSGeometry(&pImage->Header);
|
---|
1545 | if (!pGeometry)
|
---|
1546 | pGeometry = &DummyGeo;
|
---|
1547 |
|
---|
1548 | if ( pGeometry->cCylinders > 0
|
---|
1549 | && pGeometry->cHeads > 0
|
---|
1550 | && pGeometry->cSectors > 0)
|
---|
1551 | {
|
---|
1552 | pLCHSGeometry->cCylinders = pGeometry->cCylinders;
|
---|
1553 | pLCHSGeometry->cHeads = pGeometry->cHeads;
|
---|
1554 | pLCHSGeometry->cSectors = pGeometry->cSectors;
|
---|
1555 | rc = VINF_SUCCESS;
|
---|
1556 | }
|
---|
1557 | else
|
---|
1558 | rc = VERR_VD_GEOMETRY_NOT_SET;
|
---|
1559 | }
|
---|
1560 | else
|
---|
1561 | rc = VERR_VD_NOT_OPENED;
|
---|
1562 |
|
---|
1563 | LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
|
---|
1564 | return rc;
|
---|
1565 | }
|
---|
1566 |
|
---|
1567 | /** @copydoc VBOXHDDBACKEND::pfnSetLCHSGeometry */
|
---|
1568 | static int vdiSetLCHSGeometry(void *pBackendData,
|
---|
1569 | PCPDMMEDIAGEOMETRY pLCHSGeometry)
|
---|
1570 | {
|
---|
1571 | LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
|
---|
1572 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1573 | PVDIDISKGEOMETRY pGeometry;
|
---|
1574 | int rc;
|
---|
1575 |
|
---|
1576 | AssertPtr(pImage);
|
---|
1577 |
|
---|
1578 | if (pImage)
|
---|
1579 | {
|
---|
1580 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
1581 | {
|
---|
1582 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
1583 | goto out;
|
---|
1584 | }
|
---|
1585 |
|
---|
1586 | pGeometry = getImageLCHSGeometry(&pImage->Header);
|
---|
1587 | if (pGeometry)
|
---|
1588 | {
|
---|
1589 | pGeometry->cCylinders = pLCHSGeometry->cCylinders;
|
---|
1590 | pGeometry->cHeads = pLCHSGeometry->cHeads;
|
---|
1591 | pGeometry->cSectors = pLCHSGeometry->cSectors;
|
---|
1592 | pGeometry->cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
1593 |
|
---|
1594 | /* Update header information in base image file. */
|
---|
1595 | vdiFlushImage(pImage);
|
---|
1596 | }
|
---|
1597 | rc = VINF_SUCCESS;
|
---|
1598 | }
|
---|
1599 | else
|
---|
1600 | rc = VERR_VD_NOT_OPENED;
|
---|
1601 |
|
---|
1602 | out:
|
---|
1603 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1604 | return rc;
|
---|
1605 | }
|
---|
1606 |
|
---|
1607 | /** @copydoc VBOXHDDBACKEND::pfnGetImageFlags */
|
---|
1608 | static unsigned vdiGetImageFlags(void *pBackendData)
|
---|
1609 | {
|
---|
1610 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
1611 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1612 | unsigned uImageFlags;
|
---|
1613 |
|
---|
1614 | AssertPtr(pImage);
|
---|
1615 |
|
---|
1616 | if (pImage)
|
---|
1617 | uImageFlags = pImage->uImageFlags;
|
---|
1618 | else
|
---|
1619 | uImageFlags = 0;
|
---|
1620 |
|
---|
1621 | LogFlowFunc(("returns %#x\n", uImageFlags));
|
---|
1622 | return uImageFlags;
|
---|
1623 | }
|
---|
1624 |
|
---|
1625 | /** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
|
---|
1626 | static unsigned vdiGetOpenFlags(void *pBackendData)
|
---|
1627 | {
|
---|
1628 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
1629 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1630 | unsigned uOpenFlags;
|
---|
1631 |
|
---|
1632 | AssertPtr(pImage);
|
---|
1633 |
|
---|
1634 | if (pImage)
|
---|
1635 | uOpenFlags = pImage->uOpenFlags;
|
---|
1636 | else
|
---|
1637 | uOpenFlags = 0;
|
---|
1638 |
|
---|
1639 | LogFlowFunc(("returns %#x\n", uOpenFlags));
|
---|
1640 | return uOpenFlags;
|
---|
1641 | }
|
---|
1642 |
|
---|
1643 | /** @copydoc VBOXHDDBACKEND::pfnSetOpenFlags */
|
---|
1644 | static int vdiSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
|
---|
1645 | {
|
---|
1646 | LogFlowFunc(("pBackendData=%#p uOpenFlags=%#x\n", pBackendData, uOpenFlags));
|
---|
1647 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1648 | int rc;
|
---|
1649 | const char *pszFilename;
|
---|
1650 |
|
---|
1651 | /* Image must be opened and the new flags must be valid. Just readonly and
|
---|
1652 | * info flags are supported. */
|
---|
1653 | if (!pImage || (uOpenFlags & ~(VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_ASYNC_IO)))
|
---|
1654 | {
|
---|
1655 | rc = VERR_INVALID_PARAMETER;
|
---|
1656 | goto out;
|
---|
1657 | }
|
---|
1658 |
|
---|
1659 | /* Implement this operation via reopening the image. */
|
---|
1660 | pszFilename = pImage->pszFilename;
|
---|
1661 | vdiFreeImage(pImage, false);
|
---|
1662 | rc = vdiOpenImage(pImage, uOpenFlags);
|
---|
1663 |
|
---|
1664 | out:
|
---|
1665 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1666 | return rc;
|
---|
1667 | }
|
---|
1668 |
|
---|
1669 | /** @copydoc VBOXHDDBACKEND::pfnGetComment */
|
---|
1670 | static int vdiGetComment(void *pBackendData, char *pszComment,
|
---|
1671 | size_t cbComment)
|
---|
1672 | {
|
---|
1673 | LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
|
---|
1674 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1675 | int rc = VINF_SUCCESS;
|
---|
1676 |
|
---|
1677 | AssertPtr(pImage);
|
---|
1678 |
|
---|
1679 | if (pImage)
|
---|
1680 | {
|
---|
1681 | char *pszTmp = getImageComment(&pImage->Header);
|
---|
1682 | size_t cb = strlen(pszTmp);
|
---|
1683 | if (cb < cbComment)
|
---|
1684 | {
|
---|
1685 | /* memcpy is much better than strncpy. */
|
---|
1686 | memcpy(pszComment, pszTmp, cb + 1);
|
---|
1687 | }
|
---|
1688 | else
|
---|
1689 | rc = VERR_BUFFER_OVERFLOW;
|
---|
1690 | }
|
---|
1691 | else
|
---|
1692 | rc = VERR_VD_NOT_OPENED;
|
---|
1693 |
|
---|
1694 | LogFlowFunc(("returns %Rrc comment=\"%s\"\n", rc, pszComment));
|
---|
1695 | return rc;
|
---|
1696 | }
|
---|
1697 |
|
---|
1698 | /** @copydoc VBOXHDDBACKEND::pfnGetComment */
|
---|
1699 | static int vdiSetComment(void *pBackendData, const char *pszComment)
|
---|
1700 | {
|
---|
1701 | LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
|
---|
1702 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1703 | int rc;
|
---|
1704 |
|
---|
1705 | AssertPtr(pImage);
|
---|
1706 |
|
---|
1707 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
1708 | {
|
---|
1709 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
1710 | goto out;
|
---|
1711 | }
|
---|
1712 |
|
---|
1713 | if (pImage)
|
---|
1714 | {
|
---|
1715 | size_t cchComment = pszComment ? strlen(pszComment) : 0;
|
---|
1716 | if (cchComment >= VDI_IMAGE_COMMENT_SIZE)
|
---|
1717 | {
|
---|
1718 | LogFunc(("pszComment is too long, %d bytes!\n", cchComment));
|
---|
1719 | rc = VERR_VD_VDI_COMMENT_TOO_LONG;
|
---|
1720 | goto out;
|
---|
1721 | }
|
---|
1722 |
|
---|
1723 | /* we don't support old style images */
|
---|
1724 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
1725 | {
|
---|
1726 | /*
|
---|
1727 | * Update the comment field, making sure to zero out all of the previous comment.
|
---|
1728 | */
|
---|
1729 | memset(pImage->Header.u.v1.szComment, '\0', VDI_IMAGE_COMMENT_SIZE);
|
---|
1730 | memcpy(pImage->Header.u.v1.szComment, pszComment, cchComment);
|
---|
1731 |
|
---|
1732 | /* write out new the header */
|
---|
1733 | rc = vdiUpdateHeader(pImage);
|
---|
1734 | }
|
---|
1735 | else
|
---|
1736 | rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
1737 | }
|
---|
1738 | else
|
---|
1739 | rc = VERR_VD_NOT_OPENED;
|
---|
1740 |
|
---|
1741 | out:
|
---|
1742 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1743 | return rc;
|
---|
1744 | }
|
---|
1745 |
|
---|
1746 | /** @copydoc VBOXHDDBACKEND::pfnGetUuid */
|
---|
1747 | static int vdiGetUuid(void *pBackendData, PRTUUID pUuid)
|
---|
1748 | {
|
---|
1749 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
1750 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1751 | int rc;
|
---|
1752 |
|
---|
1753 | AssertPtr(pImage);
|
---|
1754 |
|
---|
1755 | if (pImage)
|
---|
1756 | {
|
---|
1757 | *pUuid = *getImageCreationUUID(&pImage->Header);
|
---|
1758 | rc = VINF_SUCCESS;
|
---|
1759 | }
|
---|
1760 | else
|
---|
1761 | rc = VERR_VD_NOT_OPENED;
|
---|
1762 |
|
---|
1763 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
|
---|
1764 | return rc;
|
---|
1765 | }
|
---|
1766 |
|
---|
1767 | /** @copydoc VBOXHDDBACKEND::pfnSetUuid */
|
---|
1768 | static int vdiSetUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
1769 | {
|
---|
1770 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
1771 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1772 | int rc = VINF_SUCCESS;
|
---|
1773 |
|
---|
1774 | AssertPtr(pImage);
|
---|
1775 |
|
---|
1776 | if (pImage)
|
---|
1777 | {
|
---|
1778 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
1779 | {
|
---|
1780 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
1781 | pImage->Header.u.v1.uuidCreate = *pUuid;
|
---|
1782 | /* Make it possible to clone old VDIs. */
|
---|
1783 | else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
|
---|
1784 | pImage->Header.u.v0.uuidCreate = *pUuid;
|
---|
1785 | else
|
---|
1786 | {
|
---|
1787 | LogFunc(("Version is not supported!\n"));
|
---|
1788 | rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
1789 | }
|
---|
1790 | }
|
---|
1791 | else
|
---|
1792 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
1793 | }
|
---|
1794 | else
|
---|
1795 | rc = VERR_VD_NOT_OPENED;
|
---|
1796 |
|
---|
1797 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1798 | return rc;
|
---|
1799 | }
|
---|
1800 |
|
---|
1801 | /** @copydoc VBOXHDDBACKEND::pfnGetModificationUuid */
|
---|
1802 | static int vdiGetModificationUuid(void *pBackendData, PRTUUID pUuid)
|
---|
1803 | {
|
---|
1804 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
1805 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1806 | int rc;
|
---|
1807 |
|
---|
1808 | AssertPtr(pImage);
|
---|
1809 |
|
---|
1810 | if (pImage)
|
---|
1811 | {
|
---|
1812 | *pUuid = *getImageModificationUUID(&pImage->Header);
|
---|
1813 | rc = VINF_SUCCESS;
|
---|
1814 | }
|
---|
1815 | else
|
---|
1816 | rc = VERR_VD_NOT_OPENED;
|
---|
1817 |
|
---|
1818 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
|
---|
1819 | return rc;
|
---|
1820 | }
|
---|
1821 |
|
---|
1822 | /** @copydoc VBOXHDDBACKEND::pfnSetModificationUuid */
|
---|
1823 | static int vdiSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
1824 | {
|
---|
1825 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
1826 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1827 | int rc = VINF_SUCCESS;
|
---|
1828 |
|
---|
1829 | AssertPtr(pImage);
|
---|
1830 |
|
---|
1831 | if (pImage)
|
---|
1832 | {
|
---|
1833 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
1834 | {
|
---|
1835 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
1836 | pImage->Header.u.v1.uuidModify = *pUuid;
|
---|
1837 | /* Make it possible to clone old VDIs. */
|
---|
1838 | else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
|
---|
1839 | pImage->Header.u.v0.uuidModify = *pUuid;
|
---|
1840 | else
|
---|
1841 | {
|
---|
1842 | LogFunc(("Version is not supported!\n"));
|
---|
1843 | rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
1844 | }
|
---|
1845 | }
|
---|
1846 | else
|
---|
1847 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
1848 | }
|
---|
1849 | else
|
---|
1850 | rc = VERR_VD_NOT_OPENED;
|
---|
1851 |
|
---|
1852 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1853 | return rc;
|
---|
1854 | }
|
---|
1855 |
|
---|
1856 | /** @copydoc VBOXHDDBACKEND::pfnGetParentUuid */
|
---|
1857 | static int vdiGetParentUuid(void *pBackendData, PRTUUID pUuid)
|
---|
1858 | {
|
---|
1859 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
1860 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1861 | int rc;
|
---|
1862 |
|
---|
1863 | AssertPtr(pImage);
|
---|
1864 |
|
---|
1865 | if (pImage)
|
---|
1866 | {
|
---|
1867 | *pUuid = *getImageParentUUID(&pImage->Header);
|
---|
1868 | rc = VINF_SUCCESS;
|
---|
1869 | }
|
---|
1870 | else
|
---|
1871 | rc = VERR_VD_NOT_OPENED;
|
---|
1872 |
|
---|
1873 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
|
---|
1874 | return rc;
|
---|
1875 | }
|
---|
1876 |
|
---|
1877 | /** @copydoc VBOXHDDBACKEND::pfnSetParentUuid */
|
---|
1878 | static int vdiSetParentUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
1879 | {
|
---|
1880 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
1881 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1882 | int rc = VINF_SUCCESS;
|
---|
1883 |
|
---|
1884 | AssertPtr(pImage);
|
---|
1885 |
|
---|
1886 | if (pImage)
|
---|
1887 | {
|
---|
1888 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
1889 | {
|
---|
1890 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
1891 | pImage->Header.u.v1.uuidLinkage = *pUuid;
|
---|
1892 | /* Make it possible to clone old VDIs. */
|
---|
1893 | else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
|
---|
1894 | pImage->Header.u.v0.uuidLinkage = *pUuid;
|
---|
1895 | else
|
---|
1896 | {
|
---|
1897 | LogFunc(("Version is not supported!\n"));
|
---|
1898 | rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
1899 | }
|
---|
1900 | }
|
---|
1901 | else
|
---|
1902 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
1903 | }
|
---|
1904 | else
|
---|
1905 | rc = VERR_VD_NOT_OPENED;
|
---|
1906 |
|
---|
1907 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1908 | return rc;
|
---|
1909 | }
|
---|
1910 |
|
---|
1911 | /** @copydoc VBOXHDDBACKEND::pfnGetParentModificationUuid */
|
---|
1912 | static int vdiGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
|
---|
1913 | {
|
---|
1914 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
1915 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1916 | int rc;
|
---|
1917 |
|
---|
1918 | AssertPtr(pImage);
|
---|
1919 |
|
---|
1920 | if (pImage)
|
---|
1921 | {
|
---|
1922 | *pUuid = *getImageParentModificationUUID(&pImage->Header);
|
---|
1923 | rc = VINF_SUCCESS;
|
---|
1924 | }
|
---|
1925 | else
|
---|
1926 | rc = VERR_VD_NOT_OPENED;
|
---|
1927 |
|
---|
1928 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
|
---|
1929 | return rc;
|
---|
1930 | }
|
---|
1931 |
|
---|
1932 | /** @copydoc VBOXHDDBACKEND::pfnSetParentModificationUuid */
|
---|
1933 | static int vdiSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
1934 | {
|
---|
1935 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
1936 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1937 | int rc = VINF_SUCCESS;
|
---|
1938 |
|
---|
1939 | AssertPtr(pImage);
|
---|
1940 |
|
---|
1941 | if (pImage)
|
---|
1942 | {
|
---|
1943 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
1944 | {
|
---|
1945 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
1946 | pImage->Header.u.v1.uuidParentModify = *pUuid;
|
---|
1947 | else
|
---|
1948 | {
|
---|
1949 | LogFunc(("Version is not supported!\n"));
|
---|
1950 | rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
1951 | }
|
---|
1952 | }
|
---|
1953 | else
|
---|
1954 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
1955 | }
|
---|
1956 | else
|
---|
1957 | rc = VERR_VD_NOT_OPENED;
|
---|
1958 |
|
---|
1959 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1960 | return rc;
|
---|
1961 | }
|
---|
1962 |
|
---|
1963 | /** @copydoc VBOXHDDBACKEND::pfnDump */
|
---|
1964 | static void vdiDump(void *pBackendData)
|
---|
1965 | {
|
---|
1966 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1967 |
|
---|
1968 | #ifndef VBOX_WITH_NEW_IO_CODE
|
---|
1969 | pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Dumping VDI image \"%s\" mode=%s uOpenFlags=%X File=%08X\n",
|
---|
1970 | pImage->pszFilename,
|
---|
1971 | (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY) ? "r/o" : "r/w",
|
---|
1972 | pImage->uOpenFlags,
|
---|
1973 | pImage->File);
|
---|
1974 | #else
|
---|
1975 | pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Dumping VDI image \"%s\" mode=%s uOpenFlags=%X File=%#p\n",
|
---|
1976 | pImage->pszFilename,
|
---|
1977 | (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY) ? "r/o" : "r/w",
|
---|
1978 | pImage->uOpenFlags,
|
---|
1979 | pImage->pStorage);
|
---|
1980 | #endif
|
---|
1981 | pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Header: Version=%08X Type=%X Flags=%X Size=%llu\n",
|
---|
1982 | pImage->PreHeader.u32Version,
|
---|
1983 | getImageType(&pImage->Header),
|
---|
1984 | getImageFlags(&pImage->Header),
|
---|
1985 | getImageDiskSize(&pImage->Header));
|
---|
1986 | pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Header: cbBlock=%u cbBlockExtra=%u cBlocks=%u cBlocksAllocated=%u\n",
|
---|
1987 | getImageBlockSize(&pImage->Header),
|
---|
1988 | getImageExtraBlockSize(&pImage->Header),
|
---|
1989 | getImageBlocks(&pImage->Header),
|
---|
1990 | getImageBlocksAllocated(&pImage->Header));
|
---|
1991 | pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Header: offBlocks=%u offData=%u\n",
|
---|
1992 | getImageBlocksOffset(&pImage->Header),
|
---|
1993 | getImageDataOffset(&pImage->Header));
|
---|
1994 | PVDIDISKGEOMETRY pg = getImageLCHSGeometry(&pImage->Header);
|
---|
1995 | if (pg)
|
---|
1996 | pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Header: Geometry: C/H/S=%u/%u/%u cbSector=%u\n",
|
---|
1997 | pg->cCylinders, pg->cHeads, pg->cSectors, pg->cbSector);
|
---|
1998 | pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Header: uuidCreation={%RTuuid}\n", getImageCreationUUID(&pImage->Header));
|
---|
1999 | pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Header: uuidModification={%RTuuid}\n", getImageModificationUUID(&pImage->Header));
|
---|
2000 | pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Header: uuidParent={%RTuuid}\n", getImageParentUUID(&pImage->Header));
|
---|
2001 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) >= 1)
|
---|
2002 | pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Header: uuidParentModification={%RTuuid}\n", getImageParentModificationUUID(&pImage->Header));
|
---|
2003 | pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Image: fFlags=%08X offStartBlocks=%u offStartData=%u\n",
|
---|
2004 | pImage->uImageFlags, pImage->offStartBlocks, pImage->offStartData);
|
---|
2005 | pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "Image: uBlockMask=%08X cbTotalBlockData=%u uShiftOffset2Index=%u offStartBlockData=%u\n",
|
---|
2006 | pImage->uBlockMask,
|
---|
2007 | pImage->cbTotalBlockData,
|
---|
2008 | pImage->uShiftOffset2Index,
|
---|
2009 | pImage->offStartBlockData);
|
---|
2010 |
|
---|
2011 | unsigned uBlock, cBlocksNotFree, cBadBlocks, cBlocks = getImageBlocks(&pImage->Header);
|
---|
2012 | for (uBlock=0, cBlocksNotFree=0, cBadBlocks=0; uBlock<cBlocks; uBlock++)
|
---|
2013 | {
|
---|
2014 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
|
---|
2015 | {
|
---|
2016 | cBlocksNotFree++;
|
---|
2017 | if (pImage->paBlocks[uBlock] >= cBlocks)
|
---|
2018 | cBadBlocks++;
|
---|
2019 | }
|
---|
2020 | }
|
---|
2021 | if (cBlocksNotFree != getImageBlocksAllocated(&pImage->Header))
|
---|
2022 | {
|
---|
2023 | pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "!! WARNING: %u blocks actually allocated (cBlocksAllocated=%u) !!\n",
|
---|
2024 | cBlocksNotFree, getImageBlocksAllocated(&pImage->Header));
|
---|
2025 | }
|
---|
2026 | if (cBadBlocks)
|
---|
2027 | {
|
---|
2028 | pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser, "!! WARNING: %u bad blocks found !!\n",
|
---|
2029 | cBadBlocks);
|
---|
2030 | }
|
---|
2031 | }
|
---|
2032 |
|
---|
2033 | static int vdiGetTimeStamp(void *pBackendData, PRTTIMESPEC pTimeStamp)
|
---|
2034 | {
|
---|
2035 | int rc = VERR_NOT_IMPLEMENTED;
|
---|
2036 | LogFlow(("%s: returned %Rrc\n", __FUNCTION__, rc));
|
---|
2037 | return rc;
|
---|
2038 | }
|
---|
2039 |
|
---|
2040 | static int vdiGetParentTimeStamp(void *pBackendData, PRTTIMESPEC pTimeStamp)
|
---|
2041 | {
|
---|
2042 | int rc = VERR_NOT_IMPLEMENTED;
|
---|
2043 | LogFlow(("%s: returned %Rrc\n", __FUNCTION__, rc));
|
---|
2044 | return rc;
|
---|
2045 | }
|
---|
2046 |
|
---|
2047 | static int vdiSetParentTimeStamp(void *pBackendData, PCRTTIMESPEC pTimeStamp)
|
---|
2048 | {
|
---|
2049 | int rc = VERR_NOT_IMPLEMENTED;
|
---|
2050 | LogFlow(("%s: returned %Rrc\n", __FUNCTION__, rc));
|
---|
2051 | return rc;
|
---|
2052 | }
|
---|
2053 |
|
---|
2054 | static int vdiGetParentFilename(void *pBackendData, char **ppszParentFilename)
|
---|
2055 | {
|
---|
2056 | int rc = VERR_NOT_IMPLEMENTED;
|
---|
2057 | LogFlow(("%s: returned %Rrc\n", __FUNCTION__, rc));
|
---|
2058 | return rc;
|
---|
2059 | }
|
---|
2060 |
|
---|
2061 | static int vdiSetParentFilename(void *pBackendData, const char *pszParentFilename)
|
---|
2062 | {
|
---|
2063 | int rc = VERR_NOT_IMPLEMENTED;
|
---|
2064 | LogFlow(("%s: returned %Rrc\n", __FUNCTION__, rc));
|
---|
2065 | return rc;
|
---|
2066 | }
|
---|
2067 |
|
---|
2068 | static bool vdiIsAsyncIOSupported(void *pBackendData)
|
---|
2069 | {
|
---|
2070 | return true;
|
---|
2071 | }
|
---|
2072 |
|
---|
2073 | static int vdiAsyncRead(void *pvBackendData, uint64_t uOffset, size_t cbToRead,
|
---|
2074 | PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
|
---|
2075 | {
|
---|
2076 | LogFlowFunc(("pvBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
|
---|
2077 | pvBackendData, uOffset, pIoCtx, cbToRead, pcbActuallyRead));
|
---|
2078 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pvBackendData;
|
---|
2079 | unsigned uBlock;
|
---|
2080 | unsigned offRead;
|
---|
2081 | int rc;
|
---|
2082 |
|
---|
2083 | AssertPtr(pImage);
|
---|
2084 | Assert(!(uOffset % 512));
|
---|
2085 | Assert(!(cbToRead % 512));
|
---|
2086 |
|
---|
2087 | if ( uOffset + cbToRead > getImageDiskSize(&pImage->Header)
|
---|
2088 | || !VALID_PTR(pIoCtx)
|
---|
2089 | || !cbToRead)
|
---|
2090 | {
|
---|
2091 | rc = VERR_INVALID_PARAMETER;
|
---|
2092 | goto out;
|
---|
2093 | }
|
---|
2094 |
|
---|
2095 | /* Calculate starting block number and offset inside it. */
|
---|
2096 | uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
|
---|
2097 | offRead = (unsigned)uOffset & pImage->uBlockMask;
|
---|
2098 |
|
---|
2099 | /* Clip read range to at most the rest of the block. */
|
---|
2100 | cbToRead = RT_MIN(cbToRead, getImageBlockSize(&pImage->Header) - offRead);
|
---|
2101 | Assert(!(cbToRead % 512));
|
---|
2102 |
|
---|
2103 | if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE)
|
---|
2104 | rc = VERR_VD_BLOCK_FREE;
|
---|
2105 | else if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
|
---|
2106 | {
|
---|
2107 | size_t cbSet;
|
---|
2108 |
|
---|
2109 | cbSet = pImage->pInterfaceIOCallbacks->pfnIoCtxSet(pImage->pInterfaceIO->pvUser,
|
---|
2110 | pIoCtx, 0, cbToRead);
|
---|
2111 | Assert(cbSet == cbToRead);
|
---|
2112 |
|
---|
2113 | rc = VINF_SUCCESS;
|
---|
2114 | }
|
---|
2115 | else
|
---|
2116 | {
|
---|
2117 | /* Block present in image file, read relevant data. */
|
---|
2118 | uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
|
---|
2119 | + (pImage->offStartData + pImage->offStartBlockData + offRead);
|
---|
2120 | rc = pImage->pInterfaceIOCallbacks->pfnReadUserAsync(pImage->pInterfaceIO->pvUser,
|
---|
2121 | pImage->pStorage,
|
---|
2122 | u64Offset,
|
---|
2123 | pIoCtx, cbToRead);
|
---|
2124 | }
|
---|
2125 |
|
---|
2126 | if (pcbActuallyRead)
|
---|
2127 | *pcbActuallyRead = cbToRead;
|
---|
2128 |
|
---|
2129 | out:
|
---|
2130 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
2131 | return rc;
|
---|
2132 | }
|
---|
2133 |
|
---|
2134 | static int vdiAsyncWrite(void *pvBackendData, uint64_t uOffset, size_t cbToWrite,
|
---|
2135 | PVDIOCTX pIoCtx,
|
---|
2136 | size_t *pcbWriteProcess, size_t *pcbPreRead,
|
---|
2137 | size_t *pcbPostRead, unsigned fWrite)
|
---|
2138 | {
|
---|
2139 | LogFlowFunc(("pvBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
|
---|
2140 | pvBackendData, uOffset, pIoCtx, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
|
---|
2141 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pvBackendData;
|
---|
2142 | unsigned uBlock;
|
---|
2143 | unsigned offWrite;
|
---|
2144 | int rc = VINF_SUCCESS;
|
---|
2145 |
|
---|
2146 | AssertPtr(pImage);
|
---|
2147 | Assert(!(uOffset % 512));
|
---|
2148 | Assert(!(cbToWrite % 512));
|
---|
2149 |
|
---|
2150 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
2151 | {
|
---|
2152 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
2153 | goto out;
|
---|
2154 | }
|
---|
2155 |
|
---|
2156 | if (!VALID_PTR(pIoCtx) || !cbToWrite)
|
---|
2157 | {
|
---|
2158 | rc = VERR_INVALID_PARAMETER;
|
---|
2159 | goto out;
|
---|
2160 | }
|
---|
2161 |
|
---|
2162 | /* No size check here, will do that later. For dynamic images which are
|
---|
2163 | * not multiples of the block size in length, this would prevent writing to
|
---|
2164 | * the last grain. */
|
---|
2165 |
|
---|
2166 | /* Calculate starting block number and offset inside it. */
|
---|
2167 | uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
|
---|
2168 | offWrite = (unsigned)uOffset & pImage->uBlockMask;
|
---|
2169 |
|
---|
2170 | /* Clip write range to at most the rest of the block. */
|
---|
2171 | cbToWrite = RT_MIN(cbToWrite, getImageBlockSize(&pImage->Header) - offWrite);
|
---|
2172 | Assert(!(cbToWrite % 512));
|
---|
2173 |
|
---|
2174 | do
|
---|
2175 | {
|
---|
2176 | if (!IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
|
---|
2177 | {
|
---|
2178 | /* Block is either free or zero. */
|
---|
2179 | if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_ZEROES)
|
---|
2180 | && ( pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO
|
---|
2181 | || cbToWrite == getImageBlockSize(&pImage->Header)))
|
---|
2182 | {
|
---|
2183 | #if 0 /** @todo Provide interface to check an I/O context for a specific value */
|
---|
2184 | /* If the destination block is unallocated at this point, it's
|
---|
2185 | * either a zero block or a block which hasn't been used so far
|
---|
2186 | * (which also means that it's a zero block. Don't need to write
|
---|
2187 | * anything to this block if the data consists of just zeroes. */
|
---|
2188 | Assert(!(cbToWrite % 4));
|
---|
2189 | Assert(cbToWrite * 8 <= UINT32_MAX);
|
---|
2190 | if (ASMBitFirstSet((volatile void *)pvBuf, (uint32_t)cbToWrite * 8) == -1)
|
---|
2191 | {
|
---|
2192 | pImage->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
|
---|
2193 | break;
|
---|
2194 | }
|
---|
2195 | #endif
|
---|
2196 | }
|
---|
2197 |
|
---|
2198 | if (cbToWrite == getImageBlockSize(&pImage->Header))
|
---|
2199 | {
|
---|
2200 | /* Full block write to previously unallocated block.
|
---|
2201 | * Allocate block and write data. */
|
---|
2202 | Assert(!offWrite);
|
---|
2203 | unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
|
---|
2204 | uint64_t u64Offset = (uint64_t)cBlocksAllocated * pImage->cbTotalBlockData
|
---|
2205 | + (pImage->offStartData + pImage->offStartBlockData);
|
---|
2206 | rc = pImage->pInterfaceIOCallbacks->pfnWriteUserAsync(pImage->pInterfaceIO->pvUser,
|
---|
2207 | pImage->pStorage,
|
---|
2208 | u64Offset,
|
---|
2209 | pIoCtx, cbToWrite);
|
---|
2210 | if (RT_UNLIKELY(RT_FAILURE_NP(rc) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)))
|
---|
2211 | goto out;
|
---|
2212 | pImage->paBlocks[uBlock] = cBlocksAllocated;
|
---|
2213 | setImageBlocksAllocated(&pImage->Header, cBlocksAllocated + 1);
|
---|
2214 |
|
---|
2215 | rc = vdiUpdateBlockInfoAsync(pImage, uBlock, pIoCtx);
|
---|
2216 | if (RT_FAILURE(rc))
|
---|
2217 | goto out;
|
---|
2218 |
|
---|
2219 | *pcbPreRead = 0;
|
---|
2220 | *pcbPostRead = 0;
|
---|
2221 | }
|
---|
2222 | else
|
---|
2223 | {
|
---|
2224 | /* Trying to do a partial write to an unallocated block. Don't do
|
---|
2225 | * anything except letting the upper layer know what to do. */
|
---|
2226 | *pcbPreRead = offWrite % getImageBlockSize(&pImage->Header);
|
---|
2227 | *pcbPostRead = getImageBlockSize(&pImage->Header) - cbToWrite - *pcbPreRead;
|
---|
2228 | rc = VERR_VD_BLOCK_FREE;
|
---|
2229 | }
|
---|
2230 | }
|
---|
2231 | else
|
---|
2232 | {
|
---|
2233 | /* Block present in image file, write relevant data. */
|
---|
2234 | uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
|
---|
2235 | + (pImage->offStartData + pImage->offStartBlockData + offWrite);
|
---|
2236 | rc = pImage->pInterfaceIOCallbacks->pfnWriteUserAsync(pImage->pInterfaceIO->pvUser,
|
---|
2237 | pImage->pStorage,
|
---|
2238 | u64Offset,
|
---|
2239 | pIoCtx, cbToWrite);
|
---|
2240 | }
|
---|
2241 | } while (0);
|
---|
2242 |
|
---|
2243 | if (pcbWriteProcess)
|
---|
2244 | *pcbWriteProcess = cbToWrite;
|
---|
2245 |
|
---|
2246 | out:
|
---|
2247 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
2248 | return rc;
|
---|
2249 | }
|
---|
2250 |
|
---|
2251 | static int vdiAsyncFlush(void *pvBackendData, PVDIOCTX pIoCtx)
|
---|
2252 | {
|
---|
2253 | LogFlowFunc(("pBackendData=%#p\n", pvBackendData));
|
---|
2254 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pvBackendData;
|
---|
2255 | int rc = VINF_SUCCESS;
|
---|
2256 |
|
---|
2257 | Assert(pImage);
|
---|
2258 |
|
---|
2259 | vdiFlushImageAsync(pImage, pIoCtx);
|
---|
2260 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
2261 | return rc;
|
---|
2262 | }
|
---|
2263 |
|
---|
2264 | /** @copydoc VBOXHDDBACKEND::pfnCompact */
|
---|
2265 | static int vdiCompact(void *pBackendData, unsigned uPercentStart,
|
---|
2266 | unsigned uPercentSpan, PVDINTERFACE pVDIfsDisk,
|
---|
2267 | PVDINTERFACE pVDIfsImage, PVDINTERFACE pVDIfsOperation)
|
---|
2268 | {
|
---|
2269 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
2270 | int rc = VINF_SUCCESS;
|
---|
2271 | void *pvBuf = NULL, *pvTmp = NULL;
|
---|
2272 | unsigned *paBlocks2 = NULL;
|
---|
2273 |
|
---|
2274 | int (*pfnParentRead)(void *, uint64_t, void *, size_t) = NULL;
|
---|
2275 | void *pvParent = NULL;
|
---|
2276 | PVDINTERFACE pIfParentState = VDInterfaceGet(pVDIfsOperation,
|
---|
2277 | VDINTERFACETYPE_PARENTSTATE);
|
---|
2278 | PVDINTERFACEPARENTSTATE pCbParentState = NULL;
|
---|
2279 | if (pIfParentState)
|
---|
2280 | {
|
---|
2281 | pCbParentState = VDGetInterfaceParentState(pIfParentState);
|
---|
2282 | if (pCbParentState)
|
---|
2283 | pfnParentRead = pCbParentState->pfnParentRead;
|
---|
2284 | pvParent = pIfParentState->pvUser;
|
---|
2285 | }
|
---|
2286 |
|
---|
2287 | PFNVDPROGRESS pfnProgress = NULL;
|
---|
2288 | void *pvUser = NULL;
|
---|
2289 | PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
|
---|
2290 | VDINTERFACETYPE_PROGRESS);
|
---|
2291 | PVDINTERFACEPROGRESS pCbProgress = NULL;
|
---|
2292 | if (pIfProgress)
|
---|
2293 | {
|
---|
2294 | pCbProgress = VDGetInterfaceProgress(pIfProgress);
|
---|
2295 | if (pCbProgress)
|
---|
2296 | pfnProgress = pCbProgress->pfnProgress;
|
---|
2297 | pvUser = pIfProgress->pvUser;
|
---|
2298 | }
|
---|
2299 |
|
---|
2300 | do {
|
---|
2301 | AssertBreakStmt(pImage, rc = VERR_INVALID_PARAMETER);
|
---|
2302 |
|
---|
2303 | AssertBreakStmt(!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY),
|
---|
2304 | rc = VERR_VD_IMAGE_READ_ONLY);
|
---|
2305 |
|
---|
2306 | unsigned cBlocks;
|
---|
2307 | unsigned cBlocksToMove = 0;
|
---|
2308 | size_t cbBlock;
|
---|
2309 | cBlocks = getImageBlocks(&pImage->Header);
|
---|
2310 | cbBlock = getImageBlockSize(&pImage->Header);
|
---|
2311 | if (pfnParentRead)
|
---|
2312 | {
|
---|
2313 | pvBuf = RTMemTmpAlloc(cbBlock);
|
---|
2314 | AssertBreakStmt(VALID_PTR(pvBuf), rc = VERR_NO_MEMORY);
|
---|
2315 | }
|
---|
2316 | pvTmp = RTMemTmpAlloc(cbBlock);
|
---|
2317 | AssertBreakStmt(VALID_PTR(pvTmp), rc = VERR_NO_MEMORY);
|
---|
2318 |
|
---|
2319 | uint64_t cbFile;
|
---|
2320 | rc = vdiFileGetSize(pImage, &cbFile);
|
---|
2321 | AssertRCBreak(rc);
|
---|
2322 | unsigned cBlocksAllocated = (unsigned)((cbFile - pImage->offStartData - pImage->offStartBlockData) >> pImage->uShiftOffset2Index);
|
---|
2323 | if (cBlocksAllocated == 0)
|
---|
2324 | {
|
---|
2325 | /* No data blocks in this image, no need to compact. */
|
---|
2326 | rc = VINF_SUCCESS;
|
---|
2327 | break;
|
---|
2328 | }
|
---|
2329 |
|
---|
2330 | /* Allocate block array for back resolving. */
|
---|
2331 | paBlocks2 = (unsigned *)RTMemAlloc(sizeof(unsigned *) * cBlocksAllocated);
|
---|
2332 | AssertBreakStmt(VALID_PTR(paBlocks2), rc = VERR_NO_MEMORY);
|
---|
2333 | /* Fill out back resolving, check/fix allocation errors before
|
---|
2334 | * compacting the image, just to be on the safe side. Update the
|
---|
2335 | * image contents straight away, as this enables cancelling. */
|
---|
2336 | for (unsigned i = 0; i < cBlocksAllocated; i++)
|
---|
2337 | paBlocks2[i] = VDI_IMAGE_BLOCK_FREE;
|
---|
2338 | rc = VINF_SUCCESS;
|
---|
2339 | for (unsigned i = 0; i < cBlocks; i++)
|
---|
2340 | {
|
---|
2341 | VDIIMAGEBLOCKPOINTER ptrBlock = pImage->paBlocks[i];
|
---|
2342 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(ptrBlock))
|
---|
2343 | {
|
---|
2344 | if (ptrBlock < cBlocksAllocated)
|
---|
2345 | {
|
---|
2346 | if (paBlocks2[ptrBlock] == VDI_IMAGE_BLOCK_FREE)
|
---|
2347 | paBlocks2[ptrBlock] = i;
|
---|
2348 | else
|
---|
2349 | {
|
---|
2350 | LogFunc(("Freed cross-linked block %u in file \"%s\"\n",
|
---|
2351 | i, pImage->pszFilename));
|
---|
2352 | pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
|
---|
2353 | rc = vdiUpdateBlockInfo(pImage, i);
|
---|
2354 | if (RT_FAILURE(rc))
|
---|
2355 | break;
|
---|
2356 | }
|
---|
2357 | }
|
---|
2358 | else
|
---|
2359 | {
|
---|
2360 | LogFunc(("Freed out of bounds reference for block %u in file \"%s\"\n",
|
---|
2361 | i, pImage->pszFilename));
|
---|
2362 | pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
|
---|
2363 | rc = vdiUpdateBlockInfo(pImage, i);
|
---|
2364 | if (RT_FAILURE(rc))
|
---|
2365 | break;
|
---|
2366 | }
|
---|
2367 | }
|
---|
2368 | }
|
---|
2369 | if (RT_FAILURE(rc))
|
---|
2370 | break;
|
---|
2371 |
|
---|
2372 | /* Find redundant information and update the block pointers
|
---|
2373 | * accordingly, creating bubbles. Keep disk up to date, as this
|
---|
2374 | * enables cancelling. */
|
---|
2375 | for (unsigned i = 0; i < cBlocks; i++)
|
---|
2376 | {
|
---|
2377 | VDIIMAGEBLOCKPOINTER ptrBlock = pImage->paBlocks[i];
|
---|
2378 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(ptrBlock))
|
---|
2379 | {
|
---|
2380 | /* Block present in image file, read relevant data. */
|
---|
2381 | uint64_t u64Offset = (uint64_t)ptrBlock * pImage->cbTotalBlockData
|
---|
2382 | + (pImage->offStartData + pImage->offStartBlockData);
|
---|
2383 | rc = vdiFileReadSync(pImage, u64Offset, pvTmp, cbBlock, NULL);
|
---|
2384 | if (RT_FAILURE(rc))
|
---|
2385 | break;
|
---|
2386 |
|
---|
2387 | if (ASMBitFirstSet((volatile void *)pvTmp, (uint32_t)cbBlock * 8) == -1)
|
---|
2388 | {
|
---|
2389 | pImage->paBlocks[i] = VDI_IMAGE_BLOCK_ZERO;
|
---|
2390 | rc = vdiUpdateBlockInfo(pImage, i);
|
---|
2391 | if (RT_FAILURE(rc))
|
---|
2392 | break;
|
---|
2393 | paBlocks2[ptrBlock] = VDI_IMAGE_BLOCK_FREE;
|
---|
2394 | /* Adjust progress info, one block to be relocated. */
|
---|
2395 | cBlocksToMove++;
|
---|
2396 | }
|
---|
2397 | else if (pfnParentRead)
|
---|
2398 | {
|
---|
2399 | rc = pfnParentRead(pvParent, i * cbBlock, pvBuf, cbBlock);
|
---|
2400 | if (RT_FAILURE(rc))
|
---|
2401 | break;
|
---|
2402 | if (!memcmp(pvTmp, pvBuf, cbBlock))
|
---|
2403 | {
|
---|
2404 | pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
|
---|
2405 | rc = vdiUpdateBlockInfo(pImage, i);
|
---|
2406 | if (RT_FAILURE(rc))
|
---|
2407 | break;
|
---|
2408 | paBlocks2[ptrBlock] = VDI_IMAGE_BLOCK_FREE;
|
---|
2409 | /* Adjust progress info, one block to be relocated. */
|
---|
2410 | cBlocksToMove++;
|
---|
2411 | }
|
---|
2412 | }
|
---|
2413 | }
|
---|
2414 |
|
---|
2415 | if (pCbProgress && pCbProgress->pfnProgress)
|
---|
2416 | {
|
---|
2417 | rc = pCbProgress->pfnProgress(pIfProgress->pvUser,
|
---|
2418 | (uint64_t)i * uPercentSpan / (cBlocks + cBlocksToMove) + uPercentStart);
|
---|
2419 | if (RT_FAILURE(rc))
|
---|
2420 | break;
|
---|
2421 | }
|
---|
2422 | }
|
---|
2423 | if (RT_FAILURE(rc))
|
---|
2424 | break;
|
---|
2425 |
|
---|
2426 | /* Fill bubbles with other data (if available). */
|
---|
2427 | unsigned cBlocksMoved = 0;
|
---|
2428 | unsigned uBlockUsedPos = cBlocksAllocated;
|
---|
2429 | for (unsigned i = 0; i < cBlocksAllocated; i++)
|
---|
2430 | {
|
---|
2431 | unsigned uBlock = paBlocks2[i];
|
---|
2432 | if (uBlock == VDI_IMAGE_BLOCK_FREE)
|
---|
2433 | {
|
---|
2434 | unsigned uBlockData = VDI_IMAGE_BLOCK_FREE;
|
---|
2435 | while (uBlockUsedPos > i && uBlockData == VDI_IMAGE_BLOCK_FREE)
|
---|
2436 | {
|
---|
2437 | uBlockUsedPos--;
|
---|
2438 | uBlockData = paBlocks2[uBlockUsedPos];
|
---|
2439 | }
|
---|
2440 | /* Terminate early if there is no block which needs copying. */
|
---|
2441 | if (uBlockUsedPos == i)
|
---|
2442 | break;
|
---|
2443 | uint64_t u64Offset = (uint64_t)uBlockUsedPos * pImage->cbTotalBlockData
|
---|
2444 | + (pImage->offStartData + pImage->offStartBlockData);
|
---|
2445 | rc = vdiFileReadSync(pImage, u64Offset, pvTmp, cbBlock, NULL);
|
---|
2446 | u64Offset = (uint64_t)i * pImage->cbTotalBlockData
|
---|
2447 | + (pImage->offStartData + pImage->offStartBlockData);
|
---|
2448 | rc = vdiFileWriteSync(pImage, u64Offset, pvTmp, cbBlock, NULL);
|
---|
2449 | pImage->paBlocks[uBlockData] = i;
|
---|
2450 | setImageBlocksAllocated(&pImage->Header, cBlocksAllocated - cBlocksMoved);
|
---|
2451 | rc = vdiUpdateBlockInfo(pImage, uBlockData);
|
---|
2452 | if (RT_FAILURE(rc))
|
---|
2453 | break;
|
---|
2454 | paBlocks2[i] = uBlockData;
|
---|
2455 | paBlocks2[uBlockUsedPos] = VDI_IMAGE_BLOCK_FREE;
|
---|
2456 | cBlocksMoved++;
|
---|
2457 | }
|
---|
2458 |
|
---|
2459 | if (pCbProgress && pCbProgress->pfnProgress)
|
---|
2460 | {
|
---|
2461 | rc = pCbProgress->pfnProgress(pIfProgress->pvUser,
|
---|
2462 | (uint64_t)(cBlocks + cBlocksMoved) * uPercentSpan / (cBlocks + cBlocksToMove) + uPercentStart);
|
---|
2463 |
|
---|
2464 | if (RT_FAILURE(rc))
|
---|
2465 | break;
|
---|
2466 | }
|
---|
2467 | }
|
---|
2468 | if (RT_FAILURE(rc))
|
---|
2469 | break;
|
---|
2470 |
|
---|
2471 | /* Update image header. */
|
---|
2472 | setImageBlocksAllocated(&pImage->Header, uBlockUsedPos);
|
---|
2473 | vdiUpdateHeader(pImage);
|
---|
2474 |
|
---|
2475 | /* Truncate the image to the proper size to finish compacting. */
|
---|
2476 | rc = vdiFileSetSize(pImage,
|
---|
2477 | (uint64_t)uBlockUsedPos * pImage->cbTotalBlockData
|
---|
2478 | + (pImage->offStartData + pImage->offStartBlockData));
|
---|
2479 | } while (0);
|
---|
2480 |
|
---|
2481 | if (paBlocks2)
|
---|
2482 | RTMemTmpFree(paBlocks2);
|
---|
2483 | if (pvTmp)
|
---|
2484 | RTMemTmpFree(pvTmp);
|
---|
2485 | if (pvBuf)
|
---|
2486 | RTMemTmpFree(pvBuf);
|
---|
2487 |
|
---|
2488 | if (RT_SUCCESS(rc) && pCbProgress && pCbProgress->pfnProgress)
|
---|
2489 | {
|
---|
2490 | pCbProgress->pfnProgress(pIfProgress->pvUser,
|
---|
2491 | uPercentStart + uPercentSpan);
|
---|
2492 | }
|
---|
2493 |
|
---|
2494 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
2495 | return rc;
|
---|
2496 | }
|
---|
2497 |
|
---|
2498 |
|
---|
2499 | VBOXHDDBACKEND g_VDIBackend =
|
---|
2500 | {
|
---|
2501 | /* pszBackendName */
|
---|
2502 | "VDI",
|
---|
2503 | /* cbSize */
|
---|
2504 | sizeof(VBOXHDDBACKEND),
|
---|
2505 | /* uBackendCaps */
|
---|
2506 | VD_CAP_UUID | VD_CAP_CREATE_FIXED | VD_CAP_CREATE_DYNAMIC
|
---|
2507 | | VD_CAP_DIFF | VD_CAP_FILE | VD_CAP_ASYNC,
|
---|
2508 | /* papszFileExtensions */
|
---|
2509 | s_apszVdiFileExtensions,
|
---|
2510 | /* paConfigInfo */
|
---|
2511 | NULL,
|
---|
2512 | /* hPlugin */
|
---|
2513 | NIL_RTLDRMOD,
|
---|
2514 | /* pfnCheckIfValid */
|
---|
2515 | vdiCheckIfValid,
|
---|
2516 | /* pfnOpen */
|
---|
2517 | vdiOpen,
|
---|
2518 | /* pfnCreate */
|
---|
2519 | vdiCreate,
|
---|
2520 | /* pfnRename */
|
---|
2521 | vdiRename,
|
---|
2522 | /* pfnClose */
|
---|
2523 | vdiClose,
|
---|
2524 | /* pfnRead */
|
---|
2525 | vdiRead,
|
---|
2526 | /* pfnWrite */
|
---|
2527 | vdiWrite,
|
---|
2528 | /* pfnFlush */
|
---|
2529 | vdiFlush,
|
---|
2530 | /* pfnGetVersion */
|
---|
2531 | vdiGetVersion,
|
---|
2532 | /* pfnGetSize */
|
---|
2533 | vdiGetSize,
|
---|
2534 | /* pfnGetFileSize */
|
---|
2535 | vdiGetFileSize,
|
---|
2536 | /* pfnGetPCHSGeometry */
|
---|
2537 | vdiGetPCHSGeometry,
|
---|
2538 | /* pfnSetPCHSGeometry */
|
---|
2539 | vdiSetPCHSGeometry,
|
---|
2540 | /* pfnGetLCHSGeometry */
|
---|
2541 | vdiGetLCHSGeometry,
|
---|
2542 | /* pfnSetLCHSGeometry */
|
---|
2543 | vdiSetLCHSGeometry,
|
---|
2544 | /* pfnGetImageFlags */
|
---|
2545 | vdiGetImageFlags,
|
---|
2546 | /* pfnGetOpenFlags */
|
---|
2547 | vdiGetOpenFlags,
|
---|
2548 | /* pfnSetOpenFlags */
|
---|
2549 | vdiSetOpenFlags,
|
---|
2550 | /* pfnGetComment */
|
---|
2551 | vdiGetComment,
|
---|
2552 | /* pfnSetComment */
|
---|
2553 | vdiSetComment,
|
---|
2554 | /* pfnGetUuid */
|
---|
2555 | vdiGetUuid,
|
---|
2556 | /* pfnSetUuid */
|
---|
2557 | vdiSetUuid,
|
---|
2558 | /* pfnGetModificationUuid */
|
---|
2559 | vdiGetModificationUuid,
|
---|
2560 | /* pfnSetModificationUuid */
|
---|
2561 | vdiSetModificationUuid,
|
---|
2562 | /* pfnGetParentUuid */
|
---|
2563 | vdiGetParentUuid,
|
---|
2564 | /* pfnSetParentUuid */
|
---|
2565 | vdiSetParentUuid,
|
---|
2566 | /* pfnGetParentModificationUuid */
|
---|
2567 | vdiGetParentModificationUuid,
|
---|
2568 | /* pfnSetParentModificationUuid */
|
---|
2569 | vdiSetParentModificationUuid,
|
---|
2570 | /* pfnDump */
|
---|
2571 | vdiDump,
|
---|
2572 | /* pfnGetTimeStamp */
|
---|
2573 | vdiGetTimeStamp,
|
---|
2574 | /* pfnGetParentTimeStamp */
|
---|
2575 | vdiGetParentTimeStamp,
|
---|
2576 | /* pfnSetParentTimeStamp */
|
---|
2577 | vdiSetParentTimeStamp,
|
---|
2578 | /* pfnGetParentFilename */
|
---|
2579 | vdiGetParentFilename,
|
---|
2580 | /* pfnSetParentFilename */
|
---|
2581 | vdiSetParentFilename,
|
---|
2582 | /* pfnIsAsyncIOSupported */
|
---|
2583 | vdiIsAsyncIOSupported,
|
---|
2584 | /* pfnAsyncRead */
|
---|
2585 | vdiAsyncRead,
|
---|
2586 | /* pfnAsyncWrite */
|
---|
2587 | vdiAsyncWrite,
|
---|
2588 | /* pfnAsyncFlush */
|
---|
2589 | vdiAsyncFlush,
|
---|
2590 | /* pfnComposeLocation */
|
---|
2591 | genericFileComposeLocation,
|
---|
2592 | /* pfnComposeName */
|
---|
2593 | genericFileComposeName,
|
---|
2594 | /* pfnCompact */
|
---|
2595 | vdiCompact
|
---|
2596 | };
|
---|
2597 |
|
---|