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