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