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