1 | /** @file
|
---|
2 | * Virtual Disk Image (VDI), Core Code.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
17 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
18 | * additional information or have any questions.
|
---|
19 | */
|
---|
20 |
|
---|
21 | /*******************************************************************************
|
---|
22 | * Header Files *
|
---|
23 | *******************************************************************************/
|
---|
24 | #define LOG_GROUP LOG_GROUP_VD_VDI
|
---|
25 | #include "VBoxHDD-newInternal.h"
|
---|
26 | #define VBOX_VDICORE_VD /* Signal that the header is included from here. */
|
---|
27 | #include "VDICore.h"
|
---|
28 | #include <VBox/err.h>
|
---|
29 |
|
---|
30 | #include <VBox/log.h>
|
---|
31 | #include <iprt/alloc.h>
|
---|
32 | #include <iprt/assert.h>
|
---|
33 | #include <iprt/uuid.h>
|
---|
34 | #include <iprt/file.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 | #include <iprt/asm.h>
|
---|
37 |
|
---|
38 | #define VDI_IMAGE_DEFAULT_BLOCK_SIZE _1M
|
---|
39 |
|
---|
40 | /*******************************************************************************
|
---|
41 | * Static Variables *
|
---|
42 | *******************************************************************************/
|
---|
43 |
|
---|
44 | /** NULL-terminated array of supported file extensions. */
|
---|
45 | static const char *const s_apszVdiFileExtensions[] =
|
---|
46 | {
|
---|
47 | "vdi",
|
---|
48 | NULL
|
---|
49 | };
|
---|
50 |
|
---|
51 | /*******************************************************************************
|
---|
52 | * Internal Functions *
|
---|
53 | *******************************************************************************/
|
---|
54 | static unsigned getPowerOfTwo(unsigned uNumber);
|
---|
55 | static void vdiInitPreHeader(PVDIPREHEADER pPreHdr);
|
---|
56 | static int vdiValidatePreHeader(PVDIPREHEADER pPreHdr);
|
---|
57 | static void vdiInitHeader(PVDIHEADER pHeader, VDIMAGETYPE enmType,
|
---|
58 | uint32_t uImageFlags, const char *pszComment,
|
---|
59 | uint64_t cbDisk, uint32_t cbBlock,
|
---|
60 | uint32_t cbBlockExtra);
|
---|
61 | static int vdiValidateHeader(PVDIHEADER pHeader);
|
---|
62 | static void vdiSetupImageDesc(PVDIIMAGEDESC pImage);
|
---|
63 | static int vdiUpdateHeader(PVDIIMAGEDESC pImage);
|
---|
64 | static int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock);
|
---|
65 | static void vdiFreeImage(PVDIIMAGEDESC pImage, bool fDelete);
|
---|
66 |
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Internal: signal an error to the frontend.
|
---|
70 | */
|
---|
71 | DECLINLINE(int) vdiError(PVDIIMAGEDESC pImage, int rc, RT_SRC_POS_DECL,
|
---|
72 | const char *pszFormat, ...)
|
---|
73 | {
|
---|
74 | va_list va;
|
---|
75 | va_start(va, pszFormat);
|
---|
76 | if (pImage->pInterfaceError && pImage->pInterfaceErrorCallbacks)
|
---|
77 | pImage->pInterfaceErrorCallbacks->pfnError(pImage->pInterfaceError->pvUser,
|
---|
78 | rc, RT_SRC_POS_ARGS,
|
---|
79 | pszFormat, va);
|
---|
80 | va_end(va);
|
---|
81 | return rc;
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * internal: return power of 2 or 0 if num error.
|
---|
87 | */
|
---|
88 | static unsigned getPowerOfTwo(unsigned uNumber)
|
---|
89 | {
|
---|
90 | if (uNumber == 0)
|
---|
91 | return 0;
|
---|
92 | unsigned uPower2 = 0;
|
---|
93 | while ((uNumber & 1) == 0)
|
---|
94 | {
|
---|
95 | uNumber >>= 1;
|
---|
96 | uPower2++;
|
---|
97 | }
|
---|
98 | return uNumber == 1 ? uPower2 : 0;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Internal: Init VDI preheader.
|
---|
104 | */
|
---|
105 | static void vdiInitPreHeader(PVDIPREHEADER pPreHdr)
|
---|
106 | {
|
---|
107 | pPreHdr->u32Signature = VDI_IMAGE_SIGNATURE;
|
---|
108 | pPreHdr->u32Version = VDI_IMAGE_VERSION;
|
---|
109 | memset(pPreHdr->szFileInfo, 0, sizeof(pPreHdr->szFileInfo));
|
---|
110 | strncat(pPreHdr->szFileInfo, VDI_IMAGE_FILE_INFO, sizeof(pPreHdr->szFileInfo));
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Internal: check VDI preheader.
|
---|
115 | */
|
---|
116 | static int vdiValidatePreHeader(PVDIPREHEADER pPreHdr)
|
---|
117 | {
|
---|
118 | if (pPreHdr->u32Signature != VDI_IMAGE_SIGNATURE)
|
---|
119 | return VERR_VDI_INVALID_SIGNATURE;
|
---|
120 |
|
---|
121 | if ( VDI_GET_VERSION_MAJOR(pPreHdr->u32Version) != VDI_IMAGE_VERSION_MAJOR
|
---|
122 | && pPreHdr->u32Version != 0x00000002) /* old version. */
|
---|
123 | return VERR_VDI_UNSUPPORTED_VERSION;
|
---|
124 |
|
---|
125 | return VINF_SUCCESS;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Internal: Init VDI header. Always use latest header version.
|
---|
130 | * @param pHeader Assumes it was initially initialized to all zeros.
|
---|
131 | */
|
---|
132 | static void vdiInitHeader(PVDIHEADER pHeader, VDIMAGETYPE enmType,
|
---|
133 | uint32_t uImageFlags, const char *pszComment,
|
---|
134 | uint64_t cbDisk, uint32_t cbBlock,
|
---|
135 | uint32_t cbBlockExtra)
|
---|
136 | {
|
---|
137 | pHeader->uVersion = VDI_IMAGE_VERSION;
|
---|
138 | pHeader->u.v1.cbHeader = sizeof(VDIHEADER1);
|
---|
139 | pHeader->u.v1.u32Type = (uint32_t)( enmType == VD_IMAGE_TYPE_NORMAL
|
---|
140 | ? VDI_IMAGE_TYPE_NORMAL
|
---|
141 | : VDI_IMAGE_TYPE_DIFF);
|
---|
142 | pHeader->u.v1.fFlags = (uImageFlags & VD_VDI_IMAGE_FLAGS_ZERO_EXPAND) ? 1 : 0;
|
---|
143 | #ifdef VBOX_STRICT
|
---|
144 | char achZero[VDI_IMAGE_COMMENT_SIZE] = {0};
|
---|
145 | Assert(!memcmp(pHeader->u.v1.szComment, achZero, VDI_IMAGE_COMMENT_SIZE));
|
---|
146 | #endif
|
---|
147 | pHeader->u.v1.szComment[0] = '\0';
|
---|
148 | if (pszComment)
|
---|
149 | {
|
---|
150 | AssertMsg(strlen(pszComment) < sizeof(pHeader->u.v1.szComment),
|
---|
151 | ("HDD Comment is too long, cb=%d\n", strlen(pszComment)));
|
---|
152 | strncat(pHeader->u.v1.szComment, pszComment, sizeof(pHeader->u.v1.szComment));
|
---|
153 | }
|
---|
154 |
|
---|
155 | /* Mark the legacy geometry not-calculated. */
|
---|
156 | pHeader->u.v1.LegacyGeometry.cCylinders = 0;
|
---|
157 | pHeader->u.v1.LegacyGeometry.cHeads = 0;
|
---|
158 | pHeader->u.v1.LegacyGeometry.cSectors = 0;
|
---|
159 | pHeader->u.v1.LegacyGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
160 | pHeader->u.v1.u32Dummy = 0; /* used to be the translation value */
|
---|
161 |
|
---|
162 | pHeader->u.v1.cbDisk = cbDisk;
|
---|
163 | pHeader->u.v1.cbBlock = cbBlock;
|
---|
164 | pHeader->u.v1.cBlocks = (uint32_t)(cbDisk / cbBlock);
|
---|
165 | if (cbDisk % cbBlock)
|
---|
166 | pHeader->u.v1.cBlocks++;
|
---|
167 | pHeader->u.v1.cbBlockExtra = cbBlockExtra;
|
---|
168 | pHeader->u.v1.cBlocksAllocated = 0;
|
---|
169 |
|
---|
170 | /* Init offsets. */
|
---|
171 | pHeader->u.v1.offBlocks = RT_ALIGN_32(sizeof(VDIPREHEADER) + sizeof(VDIHEADER1), VDI_GEOMETRY_SECTOR_SIZE);
|
---|
172 | pHeader->u.v1.offData = RT_ALIGN_32(pHeader->u.v1.offBlocks + (pHeader->u.v1.cBlocks * sizeof(VDIIMAGEBLOCKPOINTER)), VDI_GEOMETRY_SECTOR_SIZE);
|
---|
173 |
|
---|
174 | /* Init uuids. */
|
---|
175 | RTUuidCreate(&pHeader->u.v1.uuidCreate);
|
---|
176 | RTUuidClear(&pHeader->u.v1.uuidModify);
|
---|
177 | RTUuidClear(&pHeader->u.v1.uuidLinkage);
|
---|
178 | RTUuidClear(&pHeader->u.v1.uuidParentModify);
|
---|
179 |
|
---|
180 | /* Mark LCHS geometry not-calculated. */
|
---|
181 | pHeader->u.v1plus.LCHSGeometry.cCylinders = 0;
|
---|
182 | pHeader->u.v1plus.LCHSGeometry.cHeads = 0;
|
---|
183 | pHeader->u.v1plus.LCHSGeometry.cSectors = 0;
|
---|
184 | pHeader->u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Internal: Check VDI header.
|
---|
189 | */
|
---|
190 | static int vdiValidateHeader(PVDIHEADER pHeader)
|
---|
191 | {
|
---|
192 | /* Check version-dependend header parameters. */
|
---|
193 | switch (GET_MAJOR_HEADER_VERSION(pHeader))
|
---|
194 | {
|
---|
195 | case 0:
|
---|
196 | {
|
---|
197 | /* Old header version. */
|
---|
198 | break;
|
---|
199 | }
|
---|
200 | case 1:
|
---|
201 | {
|
---|
202 | /* Current header version. */
|
---|
203 |
|
---|
204 | if (pHeader->u.v1.cbHeader < sizeof(VDIHEADER1))
|
---|
205 | {
|
---|
206 | LogRel(("VDI: v1 header size wrong (%d < %d)\n",
|
---|
207 | pHeader->u.v1.cbHeader, sizeof(VDIHEADER1)));
|
---|
208 | return VERR_VDI_INVALID_HEADER;
|
---|
209 | }
|
---|
210 |
|
---|
211 | if (getImageBlocksOffset(pHeader) < (sizeof(VDIPREHEADER) + sizeof(VDIHEADER1)))
|
---|
212 | {
|
---|
213 | LogRel(("VDI: v1 blocks offset wrong (%d < %d)\n",
|
---|
214 | getImageBlocksOffset(pHeader), sizeof(VDIPREHEADER) + sizeof(VDIHEADER1)));
|
---|
215 | return VERR_VDI_INVALID_HEADER;
|
---|
216 | }
|
---|
217 |
|
---|
218 | if (getImageDataOffset(pHeader) < (getImageBlocksOffset(pHeader) + getImageBlocks(pHeader) * sizeof(VDIIMAGEBLOCKPOINTER)))
|
---|
219 | {
|
---|
220 | LogRel(("VDI: v1 image data offset wrong (%d < %d)\n",
|
---|
221 | getImageDataOffset(pHeader), getImageBlocksOffset(pHeader) + getImageBlocks(pHeader) * sizeof(VDIIMAGEBLOCKPOINTER)));
|
---|
222 | return VERR_VDI_INVALID_HEADER;
|
---|
223 | }
|
---|
224 |
|
---|
225 | break;
|
---|
226 | }
|
---|
227 | default:
|
---|
228 | /* Unsupported. */
|
---|
229 | return VERR_VDI_UNSUPPORTED_VERSION;
|
---|
230 | }
|
---|
231 |
|
---|
232 | /* Check common header parameters. */
|
---|
233 |
|
---|
234 | bool fFailed = false;
|
---|
235 |
|
---|
236 | if ( getImageType(pHeader) < VDI_IMAGE_TYPE_FIRST
|
---|
237 | || getImageType(pHeader) > VDI_IMAGE_TYPE_LAST)
|
---|
238 | {
|
---|
239 | LogRel(("VDI: bad image type %d\n", getImageType(pHeader)));
|
---|
240 | fFailed = true;
|
---|
241 | }
|
---|
242 |
|
---|
243 | if (getImageFlags(pHeader) & ~VD_VDI_IMAGE_FLAGS_MASK)
|
---|
244 | {
|
---|
245 | LogRel(("VDI: bad image flags %08x\n", getImageFlags(pHeader)));
|
---|
246 | fFailed = true;
|
---|
247 | }
|
---|
248 |
|
---|
249 | if ( getImageLCHSGeometry(pHeader)
|
---|
250 | && (getImageLCHSGeometry(pHeader))->cbSector != VDI_GEOMETRY_SECTOR_SIZE)
|
---|
251 | {
|
---|
252 | LogRel(("VDI: wrong sector size (%d != %d)\n",
|
---|
253 | (getImageLCHSGeometry(pHeader))->cbSector, VDI_GEOMETRY_SECTOR_SIZE));
|
---|
254 | fFailed = true;
|
---|
255 | }
|
---|
256 |
|
---|
257 | if ( getImageDiskSize(pHeader) == 0
|
---|
258 | || getImageBlockSize(pHeader) == 0
|
---|
259 | || getImageBlocks(pHeader) == 0
|
---|
260 | || getPowerOfTwo(getImageBlockSize(pHeader)) == 0)
|
---|
261 | {
|
---|
262 | LogRel(("VDI: wrong size (%lld, %d, %d, %d)\n",
|
---|
263 | getImageDiskSize(pHeader), getImageBlockSize(pHeader),
|
---|
264 | getImageBlocks(pHeader), getPowerOfTwo(getImageBlockSize(pHeader))));
|
---|
265 | fFailed = true;
|
---|
266 | }
|
---|
267 |
|
---|
268 | if (getImageBlocksAllocated(pHeader) > getImageBlocks(pHeader))
|
---|
269 | {
|
---|
270 | LogRel(("VDI: too many blocks allocated (%d > %d)\n"
|
---|
271 | " blocksize=%d disksize=%lld\n",
|
---|
272 | getImageBlocksAllocated(pHeader), getImageBlocks(pHeader),
|
---|
273 | getImageBlockSize(pHeader), getImageDiskSize(pHeader)));
|
---|
274 | fFailed = true;
|
---|
275 | }
|
---|
276 |
|
---|
277 | if ( getImageExtraBlockSize(pHeader) != 0
|
---|
278 | && getPowerOfTwo(getImageExtraBlockSize(pHeader)) == 0)
|
---|
279 | {
|
---|
280 | LogRel(("VDI: wrong extra size (%d, %d)\n",
|
---|
281 | getImageExtraBlockSize(pHeader), getPowerOfTwo(getImageExtraBlockSize(pHeader))));
|
---|
282 | fFailed = true;
|
---|
283 | }
|
---|
284 |
|
---|
285 | if ((uint64_t)getImageBlockSize(pHeader) * getImageBlocks(pHeader) < getImageDiskSize(pHeader))
|
---|
286 | {
|
---|
287 | LogRel(("VDI: wrong disk size (%d, %d, %lld)\n",
|
---|
288 | getImageBlockSize(pHeader), getImageBlocks(pHeader), getImageDiskSize(pHeader)));
|
---|
289 | fFailed = true;
|
---|
290 | }
|
---|
291 |
|
---|
292 | if (RTUuidIsNull(getImageCreationUUID(pHeader)))
|
---|
293 | {
|
---|
294 | LogRel(("VDI: uuid of creator is 0\n"));
|
---|
295 | fFailed = true;
|
---|
296 | }
|
---|
297 |
|
---|
298 | if (RTUuidIsNull(getImageModificationUUID(pHeader)))
|
---|
299 | {
|
---|
300 | LogRel(("VDI: uuid of modificator is 0\n"));
|
---|
301 | fFailed = true;
|
---|
302 | }
|
---|
303 |
|
---|
304 | return fFailed ? VERR_VDI_INVALID_HEADER : VINF_SUCCESS;
|
---|
305 | }
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * Internal: Set up VDIIMAGEDESC structure by image header.
|
---|
309 | */
|
---|
310 | static void vdiSetupImageDesc(PVDIIMAGEDESC pImage)
|
---|
311 | {
|
---|
312 | pImage->uImageFlags = getImageFlags(&pImage->Header);
|
---|
313 | pImage->offStartBlocks = getImageBlocksOffset(&pImage->Header);
|
---|
314 | pImage->offStartData = getImageDataOffset(&pImage->Header);
|
---|
315 | pImage->uBlockMask = getImageBlockSize(&pImage->Header) - 1;
|
---|
316 | pImage->uShiftOffset2Index = getPowerOfTwo(getImageBlockSize(&pImage->Header));
|
---|
317 | pImage->offStartBlockData = getImageExtraBlockSize(&pImage->Header);
|
---|
318 | pImage->cbTotalBlockData = pImage->offStartBlockData
|
---|
319 | + getImageBlockSize(&pImage->Header);
|
---|
320 | }
|
---|
321 |
|
---|
322 | /**
|
---|
323 | * Internal: Create VDI image file.
|
---|
324 | */
|
---|
325 | static int vdiCreateImage(PVDIIMAGEDESC pImage, VDIMAGETYPE enmType,
|
---|
326 | uint64_t cbSize, unsigned uImageFlags,
|
---|
327 | const char *pszComment,
|
---|
328 | PCPDMMEDIAGEOMETRY pPCHSGeometry,
|
---|
329 | PCPDMMEDIAGEOMETRY pLCHSGeometry, PCRTUUID pUuid,
|
---|
330 | PFNVMPROGRESS pfnProgress, void *pvUser,
|
---|
331 | unsigned uPercentStart, unsigned uPercentSpan)
|
---|
332 | {
|
---|
333 | int rc;
|
---|
334 | RTFILE File;
|
---|
335 | uint64_t cbTotal;
|
---|
336 | uint64_t cbFill;
|
---|
337 | uint64_t uOff;
|
---|
338 |
|
---|
339 | /* Special check for comment length. */
|
---|
340 | if ( VALID_PTR(pszComment)
|
---|
341 | && strlen(pszComment) >= VDI_IMAGE_COMMENT_SIZE)
|
---|
342 | {
|
---|
343 | rc = vdiError(pImage, VERR_VDI_COMMENT_TOO_LONG, RT_SRC_POS, N_("VDI: comment is too long for '%s'"), pImage->pszFilename);
|
---|
344 | goto out;
|
---|
345 | }
|
---|
346 | Assert(VALID_PTR(pPCHSGeometry));
|
---|
347 | Assert(VALID_PTR(pLCHSGeometry));
|
---|
348 |
|
---|
349 | pImage->pInterfaceError = VDInterfaceGet(pImage->pVDIfsDisk, VDINTERFACETYPE_ERROR);
|
---|
350 | if (pImage->pInterfaceError)
|
---|
351 | pImage->pInterfaceErrorCallbacks = VDGetInterfaceError(pImage->pInterfaceError);
|
---|
352 |
|
---|
353 | vdiInitPreHeader(&pImage->PreHeader);
|
---|
354 | vdiInitHeader(&pImage->Header, enmType, uImageFlags, pszComment, cbSize, VDI_IMAGE_DEFAULT_BLOCK_SIZE, 0);
|
---|
355 | /* Save PCHS geometry. Not much work, and makes the flow of information
|
---|
356 | * quite a bit clearer - relying on the higher level isn't obvious. */
|
---|
357 | pImage->PCHSGeometry = *pPCHSGeometry;
|
---|
358 | /* Set LCHS geometry (legacy geometry is ignored for the current 1.1+). */
|
---|
359 | pImage->Header.u.v1plus.LCHSGeometry.cCylinders = pLCHSGeometry->cCylinders;
|
---|
360 | pImage->Header.u.v1plus.LCHSGeometry.cHeads = pLCHSGeometry->cHeads;
|
---|
361 | pImage->Header.u.v1plus.LCHSGeometry.cSectors = pLCHSGeometry->cSectors;
|
---|
362 | pImage->Header.u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
363 |
|
---|
364 | pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
|
---|
365 | if (!pImage->paBlocks)
|
---|
366 | {
|
---|
367 | rc = VERR_NO_MEMORY;
|
---|
368 | goto out;
|
---|
369 | }
|
---|
370 |
|
---|
371 | if (enmType != VD_IMAGE_TYPE_FIXED)
|
---|
372 | {
|
---|
373 | /* for growing images mark all blocks in paBlocks as free. */
|
---|
374 | for (unsigned i = 0; i < pImage->Header.u.v1.cBlocks; i++)
|
---|
375 | pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
|
---|
376 | }
|
---|
377 | else
|
---|
378 | {
|
---|
379 | /* for fixed images mark all blocks in paBlocks as allocated */
|
---|
380 | for (unsigned i = 0; i < pImage->Header.u.v1.cBlocks; i++)
|
---|
381 | pImage->paBlocks[i] = i;
|
---|
382 | pImage->Header.u.v1.cBlocksAllocated = pImage->Header.u.v1.cBlocks;
|
---|
383 | }
|
---|
384 |
|
---|
385 | /* Setup image parameters. */
|
---|
386 | vdiSetupImageDesc(pImage);
|
---|
387 |
|
---|
388 | /* Create image file. */
|
---|
389 | rc = RTFileOpen(&File, pImage->pszFilename,
|
---|
390 | RTFILE_O_READWRITE | RTFILE_O_CREATE | RTFILE_O_DENY_ALL);
|
---|
391 | if (RT_FAILURE(rc))
|
---|
392 | {
|
---|
393 | rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: cannot create image '%s'"), pImage->pszFilename);
|
---|
394 | goto out;
|
---|
395 | }
|
---|
396 | pImage->File = File;
|
---|
397 |
|
---|
398 | cbTotal = pImage->offStartData
|
---|
399 | + (uint64_t)getImageBlocks(&pImage->Header) * pImage->cbTotalBlockData;
|
---|
400 |
|
---|
401 | if (enmType == VD_IMAGE_TYPE_FIXED)
|
---|
402 | {
|
---|
403 | /* Check the free space on the disk and leave early if there is not
|
---|
404 | * sufficient space available. */
|
---|
405 | RTFOFF cbFree = 0;
|
---|
406 | rc = RTFsQuerySizes(pImage->pszFilename, NULL, &cbFree, NULL, NULL);
|
---|
407 | if (RT_SUCCESS(rc) /* ignore errors */ && ((uint64_t)cbFree < cbTotal))
|
---|
408 | {
|
---|
409 | rc = vdiError(pImage, VERR_DISK_FULL, RT_SRC_POS, N_("VDI: disk would overflow creating image '%s'"), pImage->pszFilename);
|
---|
410 | goto out;
|
---|
411 | }
|
---|
412 | }
|
---|
413 |
|
---|
414 | if (enmType == VD_IMAGE_TYPE_FIXED)
|
---|
415 | {
|
---|
416 | /*
|
---|
417 | * Allocate & commit whole file if fixed image, it must be more
|
---|
418 | * effective than expanding file by write operations.
|
---|
419 | */
|
---|
420 | rc = RTFileSetSize(File, cbTotal);
|
---|
421 | }
|
---|
422 | else
|
---|
423 | {
|
---|
424 | /* Set file size to hold header and blocks array. */
|
---|
425 | rc = RTFileSetSize(pImage->File, pImage->offStartData);
|
---|
426 | }
|
---|
427 | if (RT_FAILURE(rc))
|
---|
428 | {
|
---|
429 | rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: setting image size failed for '%s'"), pImage->pszFilename);
|
---|
430 | goto out;
|
---|
431 | }
|
---|
432 |
|
---|
433 | /* Use specified image uuid */
|
---|
434 | *getImageCreationUUID(&pImage->Header) = *pUuid;
|
---|
435 |
|
---|
436 | /* Generate image last-modify uuid */
|
---|
437 | RTUuidCreate(getImageModificationUUID(&pImage->Header));
|
---|
438 |
|
---|
439 | /* Write pre-header. */
|
---|
440 | rc = RTFileWriteAt(File, 0, &pImage->PreHeader, sizeof(pImage->PreHeader), NULL);
|
---|
441 | if (RT_FAILURE(rc))
|
---|
442 | {
|
---|
443 | rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing pre-header failed for '%s'"), pImage->pszFilename);
|
---|
444 | goto out;
|
---|
445 | }
|
---|
446 |
|
---|
447 | /* Write header. */
|
---|
448 | rc = RTFileWriteAt(File, sizeof(pImage->PreHeader), &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL);
|
---|
449 | if (RT_FAILURE(rc))
|
---|
450 | {
|
---|
451 | rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing header failed for '%s'"), pImage->pszFilename);
|
---|
452 | goto out;
|
---|
453 | }
|
---|
454 |
|
---|
455 | rc = RTFileWriteAt(File, pImage->offStartBlocks,
|
---|
456 | pImage->paBlocks,
|
---|
457 | getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
458 | NULL);
|
---|
459 | if (RT_FAILURE(rc))
|
---|
460 | {
|
---|
461 | rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing block pointers failed for '%s'"), pImage->pszFilename);
|
---|
462 | goto out;
|
---|
463 | }
|
---|
464 |
|
---|
465 | if (enmType == VD_IMAGE_TYPE_FIXED)
|
---|
466 | {
|
---|
467 | /* Fill image with zeroes. We do this for every fixed-size image since on some systems
|
---|
468 | * (for example Windows Vista), it takes ages to write a block near the end of a sparse
|
---|
469 | * file and the guest could complain about an ATA timeout. */
|
---|
470 |
|
---|
471 | /** @todo Starting with Linux 2.6.23, there is an fallocate() system call.
|
---|
472 | * Currently supported file systems are ext4 and ocfs2. */
|
---|
473 |
|
---|
474 | /* Allocate a temporary zero-filled buffer. Use a bigger block size to optimize writing */
|
---|
475 | const size_t cbBuf = 128 * _1K;
|
---|
476 | void *pvBuf = RTMemTmpAllocZ(cbBuf);
|
---|
477 | if (!pvBuf)
|
---|
478 | {
|
---|
479 | rc = VERR_NO_MEMORY;
|
---|
480 | goto out;
|
---|
481 | }
|
---|
482 |
|
---|
483 | cbFill = (uint64_t)getImageBlocks(&pImage->Header) * pImage->cbTotalBlockData;
|
---|
484 | uOff = 0;
|
---|
485 | /* Write data to all image blocks. */
|
---|
486 | while (uOff < cbFill)
|
---|
487 | {
|
---|
488 | unsigned cbChunk = (unsigned)RT_MIN(cbFill, cbBuf);
|
---|
489 |
|
---|
490 | rc = RTFileWriteAt(File, pImage->offStartData + uOff,
|
---|
491 | pvBuf, cbChunk, NULL);
|
---|
492 | if (RT_FAILURE(rc))
|
---|
493 | {
|
---|
494 | rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: writing block failed for '%s'"), pImage->pszFilename);
|
---|
495 | goto out;
|
---|
496 | }
|
---|
497 |
|
---|
498 | uOff += cbChunk;
|
---|
499 |
|
---|
500 | if (pfnProgress)
|
---|
501 | {
|
---|
502 | rc = pfnProgress(NULL /* WARNING! pVM=NULL */,
|
---|
503 | uPercentStart + uOff * uPercentSpan / cbFill,
|
---|
504 | pvUser);
|
---|
505 | if (RT_FAILURE(rc))
|
---|
506 | goto out;
|
---|
507 | }
|
---|
508 | }
|
---|
509 | RTMemTmpFree(pvBuf);
|
---|
510 | }
|
---|
511 |
|
---|
512 | out:
|
---|
513 | if (RT_SUCCESS(rc) && pfnProgress)
|
---|
514 | pfnProgress(NULL /* WARNING! pVM=NULL */,
|
---|
515 | uPercentStart + uPercentSpan, pvUser);
|
---|
516 |
|
---|
517 | if (RT_FAILURE(rc))
|
---|
518 | vdiFreeImage(pImage, rc != VERR_ALREADY_EXISTS);
|
---|
519 | return rc;
|
---|
520 | }
|
---|
521 |
|
---|
522 | /**
|
---|
523 | * Internal: Open a VDI image.
|
---|
524 | */
|
---|
525 | static int vdiOpenImage(PVDIIMAGEDESC pImage, unsigned uOpenFlags)
|
---|
526 | {
|
---|
527 | int rc;
|
---|
528 | RTFILE File;
|
---|
529 |
|
---|
530 | if (uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO)
|
---|
531 | return VERR_NOT_SUPPORTED;
|
---|
532 |
|
---|
533 | pImage->uOpenFlags = uOpenFlags;
|
---|
534 |
|
---|
535 | pImage->pInterfaceError = VDInterfaceGet(pImage->pVDIfsDisk, VDINTERFACETYPE_ERROR);
|
---|
536 | if (pImage->pInterfaceError)
|
---|
537 | pImage->pInterfaceErrorCallbacks = VDGetInterfaceError(pImage->pInterfaceError);
|
---|
538 |
|
---|
539 | /*
|
---|
540 | * Open the image.
|
---|
541 | */
|
---|
542 | rc = RTFileOpen(&File, pImage->pszFilename,
|
---|
543 | uOpenFlags & VD_OPEN_FLAGS_READONLY
|
---|
544 | ? RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE
|
---|
545 | : RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
546 | if (RT_FAILURE(rc))
|
---|
547 | {
|
---|
548 | /* Do NOT signal an appropriate error here, as the VD layer has the
|
---|
549 | * choice of retrying the open if it failed. */
|
---|
550 | goto out;
|
---|
551 | }
|
---|
552 | pImage->File = File;
|
---|
553 |
|
---|
554 | /* Read pre-header. */
|
---|
555 | rc = RTFileReadAt(File, 0, &pImage->PreHeader, sizeof(pImage->PreHeader),
|
---|
556 | NULL);
|
---|
557 | if (RT_FAILURE(rc))
|
---|
558 | {
|
---|
559 | rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading pre-header in '%s'"), pImage->pszFilename);
|
---|
560 | goto out;
|
---|
561 | }
|
---|
562 | rc = vdiValidatePreHeader(&pImage->PreHeader);
|
---|
563 | if (RT_FAILURE(rc))
|
---|
564 | {
|
---|
565 | rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: invalid pre-header in '%s'"), pImage->pszFilename);
|
---|
566 | goto out;
|
---|
567 | }
|
---|
568 |
|
---|
569 | /* Read header. */
|
---|
570 | pImage->Header.uVersion = pImage->PreHeader.u32Version;
|
---|
571 | switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
|
---|
572 | {
|
---|
573 | case 0:
|
---|
574 | rc = RTFileReadAt(File, sizeof(pImage->PreHeader),
|
---|
575 | &pImage->Header.u.v0, sizeof(pImage->Header.u.v0),
|
---|
576 | NULL);
|
---|
577 | if (RT_FAILURE(rc))
|
---|
578 | {
|
---|
579 | rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading v0 header in '%s'"), pImage->pszFilename);
|
---|
580 | goto out;
|
---|
581 | }
|
---|
582 | break;
|
---|
583 | case 1:
|
---|
584 | rc = RTFileReadAt(File, sizeof(pImage->PreHeader),
|
---|
585 | &pImage->Header.u.v1, sizeof(pImage->Header.u.v1),
|
---|
586 | NULL);
|
---|
587 | if (RT_FAILURE(rc))
|
---|
588 | {
|
---|
589 | rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading v1 header in '%s'"), pImage->pszFilename);
|
---|
590 | goto out;
|
---|
591 | }
|
---|
592 | /* Convert VDI 1.1 images to VDI 1.1+ on open in read/write mode.
|
---|
593 | * Conversion is harmless, as any VirtualBox version supporting VDI
|
---|
594 | * 1.1 doesn't touch fields it doesn't know about. */
|
---|
595 | if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
596 | && GET_MINOR_HEADER_VERSION(&pImage->Header) == 1
|
---|
597 | && pImage->Header.u.v1.cbHeader < sizeof(pImage->Header.u.v1plus))
|
---|
598 | {
|
---|
599 | pImage->Header.u.v1plus.cbHeader = sizeof(pImage->Header.u.v1plus);
|
---|
600 | /* Mark LCHS geometry not-calculated. */
|
---|
601 | pImage->Header.u.v1plus.LCHSGeometry.cCylinders = 0;
|
---|
602 | pImage->Header.u.v1plus.LCHSGeometry.cHeads = 0;
|
---|
603 | pImage->Header.u.v1plus.LCHSGeometry.cSectors = 0;
|
---|
604 | pImage->Header.u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
605 | }
|
---|
606 | else if (pImage->Header.u.v1.cbHeader >= sizeof(pImage->Header.u.v1plus))
|
---|
607 | {
|
---|
608 | /* Read the actual VDI 1.1+ header completely. */
|
---|
609 | rc = RTFileReadAt(File, sizeof(pImage->PreHeader), &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL);
|
---|
610 | if (RT_FAILURE(rc))
|
---|
611 | {
|
---|
612 | rc = vdiError(pImage, rc, RT_SRC_POS, N_("VDI: error reading v1.1+ header in '%s'"), pImage->pszFilename);
|
---|
613 | goto out;
|
---|
614 | }
|
---|
615 | }
|
---|
616 | break;
|
---|
617 | default:
|
---|
618 | rc = vdiError(pImage, VERR_VDI_UNSUPPORTED_VERSION, RT_SRC_POS, N_("VDI: unsupported major version %u in '%s'"), GET_MAJOR_HEADER_VERSION(&pImage->Header), pImage->pszFilename);
|
---|
619 | goto out;
|
---|
620 | }
|
---|
621 |
|
---|
622 | rc = vdiValidateHeader(&pImage->Header);
|
---|
623 | if (RT_FAILURE(rc))
|
---|
624 | {
|
---|
625 | rc = vdiError(pImage, VERR_VDI_UNSUPPORTED_VERSION, RT_SRC_POS, N_("VDI: invalid header in '%s'"), pImage->pszFilename);
|
---|
626 | goto out;
|
---|
627 | }
|
---|
628 |
|
---|
629 | /* Setup image parameters by header. */
|
---|
630 | vdiSetupImageDesc(pImage);
|
---|
631 |
|
---|
632 | /* Allocate memory for blocks array. */
|
---|
633 | pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
|
---|
634 | if (!pImage->paBlocks)
|
---|
635 | {
|
---|
636 | rc = VERR_NO_MEMORY;
|
---|
637 | goto out;
|
---|
638 | }
|
---|
639 |
|
---|
640 | /* Read blocks array. */
|
---|
641 | rc = RTFileReadAt(pImage->File, pImage->offStartBlocks, pImage->paBlocks,
|
---|
642 | getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
643 | NULL);
|
---|
644 |
|
---|
645 | out:
|
---|
646 | if (RT_FAILURE(rc))
|
---|
647 | vdiFreeImage(pImage, false);
|
---|
648 | return rc;
|
---|
649 | }
|
---|
650 |
|
---|
651 | /**
|
---|
652 | * Internal: Save header to file.
|
---|
653 | */
|
---|
654 | static int vdiUpdateHeader(PVDIIMAGEDESC pImage)
|
---|
655 | {
|
---|
656 | int rc;
|
---|
657 | switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
|
---|
658 | {
|
---|
659 | case 0:
|
---|
660 | rc = RTFileWriteAt(pImage->File, sizeof(VDIPREHEADER), &pImage->Header.u.v0, sizeof(pImage->Header.u.v0), NULL);
|
---|
661 | break;
|
---|
662 | case 1:
|
---|
663 | if (pImage->Header.u.v1plus.cbHeader < sizeof(pImage->Header.u.v1plus))
|
---|
664 | rc = RTFileWriteAt(pImage->File, sizeof(VDIPREHEADER), &pImage->Header.u.v1, sizeof(pImage->Header.u.v1), NULL);
|
---|
665 | else
|
---|
666 | rc = RTFileWriteAt(pImage->File, sizeof(VDIPREHEADER), &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL);
|
---|
667 | break;
|
---|
668 | default:
|
---|
669 | rc = VERR_VDI_UNSUPPORTED_VERSION;
|
---|
670 | break;
|
---|
671 | }
|
---|
672 | AssertMsgRC(rc, ("vdiUpdateHeader failed, filename=\"%s\" rc=%Rrc\n", pImage->pszFilename, rc));
|
---|
673 | return rc;
|
---|
674 | }
|
---|
675 |
|
---|
676 | /**
|
---|
677 | * Internal: Save block pointer to file, save header to file.
|
---|
678 | */
|
---|
679 | static int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock)
|
---|
680 | {
|
---|
681 | /* Update image header. */
|
---|
682 | int rc = vdiUpdateHeader(pImage);
|
---|
683 | if (RT_SUCCESS(rc))
|
---|
684 | {
|
---|
685 | /* write only one block pointer. */
|
---|
686 | rc = RTFileWriteAt(pImage->File,
|
---|
687 | pImage->offStartBlocks + uBlock * sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
688 | &pImage->paBlocks[uBlock],
|
---|
689 | sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
690 | NULL);
|
---|
691 | AssertMsgRC(rc, ("vdiUpdateBlockInfo failed to update block=%u, filename=\"%s\", rc=%Rrc\n",
|
---|
692 | uBlock, pImage->pszFilename, rc));
|
---|
693 | }
|
---|
694 | return rc;
|
---|
695 | }
|
---|
696 |
|
---|
697 | /**
|
---|
698 | * Internal: Flush the image file to disk.
|
---|
699 | */
|
---|
700 | static void vdiFlushImage(PVDIIMAGEDESC pImage)
|
---|
701 | {
|
---|
702 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
703 | {
|
---|
704 | /* Save header. */
|
---|
705 | int rc = vdiUpdateHeader(pImage);
|
---|
706 | AssertMsgRC(rc, ("vdiUpdateHeader() failed, filename=\"%s\", rc=%Rrc\n",
|
---|
707 | pImage->pszFilename, rc));
|
---|
708 | RTFileFlush(pImage->File);
|
---|
709 | }
|
---|
710 | }
|
---|
711 |
|
---|
712 | /**
|
---|
713 | * Internal: Free all allocated space for representing an image, and optionally
|
---|
714 | * delete the image from disk.
|
---|
715 | */
|
---|
716 | static void vdiFreeImage(PVDIIMAGEDESC pImage, bool fDelete)
|
---|
717 | {
|
---|
718 | Assert(VALID_PTR(pImage));
|
---|
719 |
|
---|
720 | if (pImage->File != NIL_RTFILE)
|
---|
721 | {
|
---|
722 | vdiFlushImage(pImage);
|
---|
723 | RTFileClose(pImage->File);
|
---|
724 | pImage->File = NIL_RTFILE;
|
---|
725 | }
|
---|
726 | if (pImage->paBlocks)
|
---|
727 | {
|
---|
728 | RTMemFree(pImage->paBlocks);
|
---|
729 | pImage->paBlocks = NULL;
|
---|
730 | }
|
---|
731 | if (fDelete && pImage->pszFilename)
|
---|
732 | RTFileDelete(pImage->pszFilename);
|
---|
733 | }
|
---|
734 |
|
---|
735 |
|
---|
736 | /** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
|
---|
737 | static int vdiCheckIfValid(const char *pszFilename)
|
---|
738 | {
|
---|
739 | LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
|
---|
740 | int rc = VINF_SUCCESS;
|
---|
741 | PVDIIMAGEDESC pImage;
|
---|
742 |
|
---|
743 | if ( !VALID_PTR(pszFilename)
|
---|
744 | || !*pszFilename)
|
---|
745 | {
|
---|
746 | rc = VERR_INVALID_PARAMETER;
|
---|
747 | goto out;
|
---|
748 | }
|
---|
749 |
|
---|
750 | pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
|
---|
751 | if (!pImage)
|
---|
752 | {
|
---|
753 | rc = VERR_NO_MEMORY;
|
---|
754 | goto out;
|
---|
755 | }
|
---|
756 | pImage->pszFilename = pszFilename;
|
---|
757 | pImage->File = NIL_RTFILE;
|
---|
758 | pImage->paBlocks = NULL;
|
---|
759 | pImage->pVDIfsDisk = NULL;
|
---|
760 |
|
---|
761 | rc = vdiOpenImage(pImage, VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_READONLY);
|
---|
762 | vdiFreeImage(pImage, false);
|
---|
763 |
|
---|
764 | out:
|
---|
765 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
766 | return rc;
|
---|
767 | }
|
---|
768 |
|
---|
769 | /** @copydoc VBOXHDDBACKEND::pfnOpen */
|
---|
770 | static int vdiOpen(const char *pszFilename, unsigned uOpenFlags,
|
---|
771 | PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
|
---|
772 | void **ppBackendData)
|
---|
773 | {
|
---|
774 | LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, ppBackendData));
|
---|
775 | int rc;
|
---|
776 | PVDIIMAGEDESC pImage;
|
---|
777 |
|
---|
778 | /* Check open flags. All valid flags are supported. */
|
---|
779 | if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
|
---|
780 | {
|
---|
781 | rc = VERR_INVALID_PARAMETER;
|
---|
782 | goto out;
|
---|
783 | }
|
---|
784 |
|
---|
785 | /* Check remaining arguments. */
|
---|
786 | if ( !VALID_PTR(pszFilename)
|
---|
787 | || !*pszFilename)
|
---|
788 | {
|
---|
789 | rc = VERR_INVALID_PARAMETER;
|
---|
790 | goto out;
|
---|
791 | }
|
---|
792 |
|
---|
793 | pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
|
---|
794 | if (!pImage)
|
---|
795 | {
|
---|
796 | rc = VERR_NO_MEMORY;
|
---|
797 | goto out;
|
---|
798 | }
|
---|
799 | pImage->pszFilename = pszFilename;
|
---|
800 | pImage->File = NIL_RTFILE;
|
---|
801 | pImage->paBlocks = NULL;
|
---|
802 | pImage->pVDIfsDisk = pVDIfsDisk;
|
---|
803 |
|
---|
804 | rc = vdiOpenImage(pImage, uOpenFlags);
|
---|
805 | if (RT_SUCCESS(rc))
|
---|
806 | *ppBackendData = pImage;
|
---|
807 |
|
---|
808 | out:
|
---|
809 | LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
|
---|
810 | return rc;
|
---|
811 | }
|
---|
812 |
|
---|
813 | /** @copydoc VBOXHDDBACKEND::pfnCreate */
|
---|
814 | static int vdiCreate(const char *pszFilename, VDIMAGETYPE enmType,
|
---|
815 | uint64_t cbSize, unsigned uImageFlags,
|
---|
816 | const char *pszComment,
|
---|
817 | PCPDMMEDIAGEOMETRY pPCHSGeometry,
|
---|
818 | PCPDMMEDIAGEOMETRY pLCHSGeometry, PCRTUUID pUuid,
|
---|
819 | unsigned uOpenFlags, unsigned uPercentStart,
|
---|
820 | unsigned uPercentSpan, PVDINTERFACE pVDIfsDisk,
|
---|
821 | PVDINTERFACE pVDIfsImage, PVDINTERFACE pVDIfsOperation,
|
---|
822 | void **ppBackendData)
|
---|
823 | {
|
---|
824 | LogFlowFunc(("pszFilename=\"%s\" enmType=%d 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, enmType, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, ppBackendData));
|
---|
825 | int rc;
|
---|
826 | PVDIIMAGEDESC pImage;
|
---|
827 |
|
---|
828 | PFNVMPROGRESS pfnProgress = NULL;
|
---|
829 | void *pvUser = NULL;
|
---|
830 | PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
|
---|
831 | VDINTERFACETYPE_PROGRESS);
|
---|
832 | PVDINTERFACEPROGRESS pCbProgress = NULL;
|
---|
833 | if (pIfProgress)
|
---|
834 | {
|
---|
835 | pCbProgress = VDGetInterfaceProgress(pIfProgress);
|
---|
836 | pfnProgress = pCbProgress->pfnProgress;
|
---|
837 | pvUser = pIfProgress->pvUser;
|
---|
838 | }
|
---|
839 |
|
---|
840 | /* Check open flags. All valid flags are supported. */
|
---|
841 | if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
|
---|
842 | {
|
---|
843 | rc = VERR_INVALID_PARAMETER;
|
---|
844 | goto out;
|
---|
845 | }
|
---|
846 |
|
---|
847 | /* Check remaining arguments. */
|
---|
848 | if ( !VALID_PTR(pszFilename)
|
---|
849 | || !*pszFilename
|
---|
850 | || (enmType != VD_IMAGE_TYPE_NORMAL && enmType != VD_IMAGE_TYPE_FIXED
|
---|
851 | && enmType != VD_IMAGE_TYPE_DIFF)
|
---|
852 | || cbSize < VDI_IMAGE_DEFAULT_BLOCK_SIZE
|
---|
853 | || !VALID_PTR(pPCHSGeometry)
|
---|
854 | || !VALID_PTR(pLCHSGeometry))
|
---|
855 | {
|
---|
856 | rc = VERR_INVALID_PARAMETER;
|
---|
857 | goto out;
|
---|
858 | }
|
---|
859 |
|
---|
860 | pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
|
---|
861 | if (!pImage)
|
---|
862 | {
|
---|
863 | rc = VERR_NO_MEMORY;
|
---|
864 | goto out;
|
---|
865 | }
|
---|
866 | pImage->pszFilename = pszFilename;
|
---|
867 | pImage->File = NIL_RTFILE;
|
---|
868 | pImage->paBlocks = NULL;
|
---|
869 | pImage->pVDIfsDisk = pVDIfsDisk;
|
---|
870 |
|
---|
871 | rc = vdiCreateImage(pImage, enmType, cbSize, uImageFlags, pszComment,
|
---|
872 | pPCHSGeometry, pLCHSGeometry, pUuid,
|
---|
873 | pfnProgress, pvUser, uPercentStart, uPercentSpan);
|
---|
874 | if (RT_SUCCESS(rc))
|
---|
875 | {
|
---|
876 | /* So far the image is opened in read/write mode. Make sure the
|
---|
877 | * image is opened in read-only mode if the caller requested that. */
|
---|
878 | if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
879 | {
|
---|
880 | vdiFreeImage(pImage, false);
|
---|
881 | rc = vdiOpenImage(pImage, uOpenFlags);
|
---|
882 | if (RT_FAILURE(rc))
|
---|
883 | goto out;
|
---|
884 | }
|
---|
885 | *ppBackendData = pImage;
|
---|
886 | }
|
---|
887 |
|
---|
888 | out:
|
---|
889 | LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
|
---|
890 | return rc;
|
---|
891 | }
|
---|
892 |
|
---|
893 | /** @copydoc VBOXHDDBACKEND::pfnRename */
|
---|
894 | static int vdiRename(void *pBackendData, const char *pszFilename)
|
---|
895 | {
|
---|
896 | LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
|
---|
897 |
|
---|
898 | int rc = VINF_SUCCESS;
|
---|
899 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
900 |
|
---|
901 | /* Check arguments. */
|
---|
902 | if ( !pImage
|
---|
903 | || !pszFilename
|
---|
904 | || !*pszFilename)
|
---|
905 | {
|
---|
906 | rc = VERR_INVALID_PARAMETER;
|
---|
907 | goto out;
|
---|
908 | }
|
---|
909 |
|
---|
910 | /* Close the image. */
|
---|
911 | vdiFreeImage(pImage, false);
|
---|
912 |
|
---|
913 | /* Rename the file. */
|
---|
914 | rc = RTFileMove(pImage->pszFilename, pszFilename, 0);
|
---|
915 | if (RT_FAILURE(rc))
|
---|
916 | {
|
---|
917 | /* The move failed, try to reopen the original image. */
|
---|
918 | int rc2 = vdiOpenImage(pImage, pImage->uOpenFlags);
|
---|
919 | if (RT_FAILURE(rc2))
|
---|
920 | rc = rc2;
|
---|
921 |
|
---|
922 | goto out;
|
---|
923 | }
|
---|
924 |
|
---|
925 | /* Update pImage with the new information. */
|
---|
926 | pImage->pszFilename = pszFilename;
|
---|
927 |
|
---|
928 | /* Open the new image. */
|
---|
929 | rc = vdiOpenImage(pImage, pImage->uOpenFlags);
|
---|
930 | if (RT_FAILURE(rc))
|
---|
931 | goto out;
|
---|
932 |
|
---|
933 | out:
|
---|
934 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
935 | return rc;
|
---|
936 | }
|
---|
937 |
|
---|
938 | /** @copydoc VBOXHDDBACKEND::pfnClose */
|
---|
939 | static int vdiClose(void *pBackendData, bool fDelete)
|
---|
940 | {
|
---|
941 | LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
|
---|
942 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
943 | int rc = VINF_SUCCESS;
|
---|
944 |
|
---|
945 | /* Freeing a never allocated image (e.g. because the open failed) is
|
---|
946 | * not signalled as an error. After all nothing bad happens. */
|
---|
947 | if (pImage)
|
---|
948 | vdiFreeImage(pImage, fDelete);
|
---|
949 |
|
---|
950 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
951 | return rc;
|
---|
952 | }
|
---|
953 |
|
---|
954 | /** @copydoc VBOXHDDBACKEND::pfnRead */
|
---|
955 | static int vdiRead(void *pBackendData, uint64_t uOffset, void *pvBuf,
|
---|
956 | size_t cbToRead, size_t *pcbActuallyRead)
|
---|
957 | {
|
---|
958 | LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToRead=%zu pcbActuallyRead=%#p\n", pBackendData, uOffset, pvBuf, cbToRead, pcbActuallyRead));
|
---|
959 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
960 | unsigned uBlock;
|
---|
961 | unsigned offRead;
|
---|
962 | int rc;
|
---|
963 |
|
---|
964 | Assert(VALID_PTR(pImage));
|
---|
965 | Assert(!(uOffset % 512));
|
---|
966 | Assert(!(cbToRead % 512));
|
---|
967 |
|
---|
968 | if ( uOffset + cbToRead > getImageDiskSize(&pImage->Header)
|
---|
969 | || !VALID_PTR(pvBuf)
|
---|
970 | || !cbToRead)
|
---|
971 | {
|
---|
972 | rc = VERR_INVALID_PARAMETER;
|
---|
973 | goto out;
|
---|
974 | }
|
---|
975 |
|
---|
976 | /* Calculate starting block number and offset inside it. */
|
---|
977 | uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
|
---|
978 | offRead = (unsigned)uOffset & pImage->uBlockMask;
|
---|
979 |
|
---|
980 | /* Clip read range to at most the rest of the block. */
|
---|
981 | cbToRead = RT_MIN(cbToRead, getImageBlockSize(&pImage->Header) - offRead);
|
---|
982 | Assert(!(cbToRead % 512));
|
---|
983 |
|
---|
984 | if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE)
|
---|
985 | rc = VERR_VDI_BLOCK_FREE;
|
---|
986 | else if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
|
---|
987 | {
|
---|
988 | memset(pvBuf, 0, cbToRead);
|
---|
989 | rc = VINF_SUCCESS;
|
---|
990 | }
|
---|
991 | else
|
---|
992 | {
|
---|
993 | /* Block present in image file, read relevant data. */
|
---|
994 | uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
|
---|
995 | + (pImage->offStartData + pImage->offStartBlockData + offRead);
|
---|
996 | rc = RTFileReadAt(pImage->File, u64Offset, pvBuf, cbToRead, NULL);
|
---|
997 | }
|
---|
998 |
|
---|
999 | if (RT_SUCCESS(rc))
|
---|
1000 | *pcbActuallyRead = cbToRead;
|
---|
1001 |
|
---|
1002 | out:
|
---|
1003 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1004 | return rc;
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | /**@copydoc VBOXHDDBACKEND::pfnWrite */
|
---|
1008 | static int vdiWrite(void *pBackendData, uint64_t uOffset, const void *pvBuf,
|
---|
1009 | size_t cbToWrite, size_t *pcbWriteProcess,
|
---|
1010 | size_t *pcbPreRead, size_t *pcbPostRead, unsigned fWrite)
|
---|
1011 | {
|
---|
1012 | LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n", pBackendData, uOffset, pvBuf, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
|
---|
1013 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1014 | unsigned uBlock;
|
---|
1015 | unsigned offWrite;
|
---|
1016 | int rc = VINF_SUCCESS;
|
---|
1017 |
|
---|
1018 | Assert(VALID_PTR(pImage));
|
---|
1019 | Assert(!(uOffset % 512));
|
---|
1020 | Assert(!(cbToWrite % 512));
|
---|
1021 |
|
---|
1022 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
1023 | {
|
---|
1024 | rc = VERR_VDI_IMAGE_READ_ONLY;
|
---|
1025 | goto out;
|
---|
1026 | }
|
---|
1027 |
|
---|
1028 | if (!VALID_PTR(pvBuf) || !cbToWrite)
|
---|
1029 | {
|
---|
1030 | rc = VERR_INVALID_PARAMETER;
|
---|
1031 | goto out;
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | /* No size check here, will do that later. For dynamic images which are
|
---|
1035 | * not multiples of the block size in length, this would prevent writing to
|
---|
1036 | * the last grain. */
|
---|
1037 |
|
---|
1038 | /* Calculate starting block number and offset inside it. */
|
---|
1039 | uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
|
---|
1040 | offWrite = (unsigned)uOffset & pImage->uBlockMask;
|
---|
1041 |
|
---|
1042 | /* Clip write range to at most the rest of the block. */
|
---|
1043 | cbToWrite = RT_MIN(cbToWrite, getImageBlockSize(&pImage->Header) - offWrite);
|
---|
1044 | Assert(!(cbToWrite % 512));
|
---|
1045 |
|
---|
1046 | if (!IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
|
---|
1047 | {
|
---|
1048 | /* Block is either free or zero. */
|
---|
1049 | if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_ZEROES)
|
---|
1050 | && ( pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO
|
---|
1051 | || cbToWrite == getImageBlockSize(&pImage->Header)))
|
---|
1052 | {
|
---|
1053 | /* If the destination block is unallocated at this point, it's
|
---|
1054 | * either a zero block or a block which hasn't been used so far
|
---|
1055 | * (which also means that it's a zero block. Don't need to write
|
---|
1056 | * anything to this block if the data consists of just zeroes. */
|
---|
1057 | Assert(!(cbToWrite % 4));
|
---|
1058 | Assert(cbToWrite * 8 <= UINT32_MAX);
|
---|
1059 | if (ASMBitFirstSet((volatile void *)pvBuf, (uint32_t)cbToWrite * 8) == -1)
|
---|
1060 | {
|
---|
1061 | pImage->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
|
---|
1062 | goto out;
|
---|
1063 | }
|
---|
1064 | }
|
---|
1065 |
|
---|
1066 | if (cbToWrite == getImageBlockSize(&pImage->Header))
|
---|
1067 | {
|
---|
1068 | /* Full block write to previously unallocated block.
|
---|
1069 | * Allocate block and write data. */
|
---|
1070 | Assert(!offWrite);
|
---|
1071 | unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
|
---|
1072 | uint64_t u64Offset = (uint64_t)cBlocksAllocated * pImage->cbTotalBlockData
|
---|
1073 | + (pImage->offStartData + pImage->offStartBlockData);
|
---|
1074 | rc = RTFileWriteAt(pImage->File, u64Offset, pvBuf, cbToWrite, NULL);
|
---|
1075 | if (RT_FAILURE(rc))
|
---|
1076 | goto out;
|
---|
1077 | pImage->paBlocks[uBlock] = cBlocksAllocated;
|
---|
1078 | setImageBlocksAllocated(&pImage->Header, cBlocksAllocated + 1);
|
---|
1079 |
|
---|
1080 | rc = vdiUpdateBlockInfo(pImage, uBlock);
|
---|
1081 | if (RT_FAILURE(rc))
|
---|
1082 | goto out;
|
---|
1083 |
|
---|
1084 | *pcbPreRead = 0;
|
---|
1085 | *pcbPostRead = 0;
|
---|
1086 | }
|
---|
1087 | else
|
---|
1088 | {
|
---|
1089 | /* Trying to do a partial write to an unallocated block. Don't do
|
---|
1090 | * anything except letting the upper layer know what to do. */
|
---|
1091 | *pcbPreRead = offWrite % getImageBlockSize(&pImage->Header);
|
---|
1092 | *pcbPostRead = getImageBlockSize(&pImage->Header) - cbToWrite - *pcbPreRead;
|
---|
1093 | rc = VERR_VDI_BLOCK_FREE;
|
---|
1094 | }
|
---|
1095 | }
|
---|
1096 | else
|
---|
1097 | {
|
---|
1098 | /* Block present in image file, write relevant data. */
|
---|
1099 | uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
|
---|
1100 | + (pImage->offStartData + pImage->offStartBlockData + offWrite);
|
---|
1101 | rc = RTFileWriteAt(pImage->File, u64Offset, pvBuf, cbToWrite, NULL);
|
---|
1102 | }
|
---|
1103 | if (pcbWriteProcess)
|
---|
1104 | *pcbWriteProcess = cbToWrite;
|
---|
1105 |
|
---|
1106 | out:
|
---|
1107 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1108 | return rc;
|
---|
1109 | }
|
---|
1110 |
|
---|
1111 | /** @copydoc VBOXHDDBACKEND::pfnFlush */
|
---|
1112 | static int vdiFlush(void *pBackendData)
|
---|
1113 | {
|
---|
1114 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
1115 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1116 | int rc = VINF_SUCCESS;
|
---|
1117 |
|
---|
1118 | Assert(pImage);
|
---|
1119 |
|
---|
1120 | vdiFlushImage(pImage);
|
---|
1121 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1122 | return rc;
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 | /** @copydoc VBOXHDDBACKEND::pfnGetVersion */
|
---|
1126 | static unsigned vdiGetVersion(void *pBackendData)
|
---|
1127 | {
|
---|
1128 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
1129 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1130 | unsigned uVersion;
|
---|
1131 |
|
---|
1132 | Assert(VALID_PTR(pImage));
|
---|
1133 |
|
---|
1134 | if (pImage)
|
---|
1135 | uVersion = pImage->PreHeader.u32Version;
|
---|
1136 | else
|
---|
1137 | uVersion = 0;
|
---|
1138 |
|
---|
1139 | LogFlowFunc(("returns %#x\n", uVersion));
|
---|
1140 | return uVersion;
|
---|
1141 | }
|
---|
1142 |
|
---|
1143 | /** @copydoc VBOXHDDBACKEND::pfnGetImageType */
|
---|
1144 | static int vdiGetImageType(void *pBackendData, PVDIMAGETYPE penmImageType)
|
---|
1145 | {
|
---|
1146 | LogFlowFunc(("pBackendData=%#p penmImageType=%#p\n", pBackendData, penmImageType));
|
---|
1147 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1148 | int rc = VINF_SUCCESS;
|
---|
1149 |
|
---|
1150 | Assert(VALID_PTR(pImage));
|
---|
1151 | Assert(VALID_PTR(penmImageType));
|
---|
1152 |
|
---|
1153 | if (pImage)
|
---|
1154 | *penmImageType = getImageType(&pImage->Header) == VDI_IMAGE_TYPE_NORMAL
|
---|
1155 | ? VD_IMAGE_TYPE_NORMAL
|
---|
1156 | : VD_IMAGE_TYPE_DIFF;
|
---|
1157 | else
|
---|
1158 | rc = VERR_VDI_NOT_OPENED;
|
---|
1159 |
|
---|
1160 | LogFlowFunc(("returns %Rrc enmImageType=%u\n", rc, *penmImageType));
|
---|
1161 | return rc;
|
---|
1162 | }
|
---|
1163 |
|
---|
1164 | /** @copydoc VBOXHDDBACKEND::pfnGetSize */
|
---|
1165 | static uint64_t vdiGetSize(void *pBackendData)
|
---|
1166 | {
|
---|
1167 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
1168 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1169 | uint64_t cbSize;
|
---|
1170 |
|
---|
1171 | Assert(VALID_PTR(pImage));
|
---|
1172 |
|
---|
1173 | if (pImage)
|
---|
1174 | cbSize = getImageDiskSize(&pImage->Header);
|
---|
1175 | else
|
---|
1176 | cbSize = 0;
|
---|
1177 |
|
---|
1178 | LogFlowFunc(("returns %llu\n", cbSize));
|
---|
1179 | return cbSize;
|
---|
1180 | }
|
---|
1181 |
|
---|
1182 | /** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
|
---|
1183 | static uint64_t vdiGetFileSize(void *pBackendData)
|
---|
1184 | {
|
---|
1185 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
1186 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1187 | uint64_t cb = 0;
|
---|
1188 |
|
---|
1189 | Assert(VALID_PTR(pImage));
|
---|
1190 |
|
---|
1191 | if (pImage)
|
---|
1192 | {
|
---|
1193 | uint64_t cbFile;
|
---|
1194 | if (pImage->File != NIL_RTFILE)
|
---|
1195 | {
|
---|
1196 | int rc = RTFileGetSize(pImage->File, &cbFile);
|
---|
1197 | if (RT_SUCCESS(rc))
|
---|
1198 | cb += cbFile;
|
---|
1199 | }
|
---|
1200 | }
|
---|
1201 |
|
---|
1202 | LogFlowFunc(("returns %lld\n", cb));
|
---|
1203 | return cb;
|
---|
1204 | }
|
---|
1205 |
|
---|
1206 | /** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
|
---|
1207 | static int vdiGetPCHSGeometry(void *pBackendData,
|
---|
1208 | PPDMMEDIAGEOMETRY pPCHSGeometry)
|
---|
1209 | {
|
---|
1210 | LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
|
---|
1211 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1212 | int rc;
|
---|
1213 |
|
---|
1214 | Assert(VALID_PTR(pImage));
|
---|
1215 |
|
---|
1216 | if (pImage)
|
---|
1217 | {
|
---|
1218 | if (pImage->PCHSGeometry.cCylinders)
|
---|
1219 | {
|
---|
1220 | *pPCHSGeometry = pImage->PCHSGeometry;
|
---|
1221 | rc = VINF_SUCCESS;
|
---|
1222 | }
|
---|
1223 | else
|
---|
1224 | rc = VERR_VDI_GEOMETRY_NOT_SET;
|
---|
1225 | }
|
---|
1226 | else
|
---|
1227 | rc = VERR_VDI_NOT_OPENED;
|
---|
1228 |
|
---|
1229 | LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
|
---|
1230 | return rc;
|
---|
1231 | }
|
---|
1232 |
|
---|
1233 | /** @copydoc VBOXHDDBACKEND::pfnSetPCHSGeometry */
|
---|
1234 | static int vdiSetPCHSGeometry(void *pBackendData,
|
---|
1235 | PCPDMMEDIAGEOMETRY pPCHSGeometry)
|
---|
1236 | {
|
---|
1237 | LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
|
---|
1238 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1239 | int rc;
|
---|
1240 |
|
---|
1241 | Assert(VALID_PTR(pImage));
|
---|
1242 |
|
---|
1243 | if (pImage)
|
---|
1244 | {
|
---|
1245 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
1246 | {
|
---|
1247 | rc = VERR_VDI_IMAGE_READ_ONLY;
|
---|
1248 | goto out;
|
---|
1249 | }
|
---|
1250 |
|
---|
1251 | pImage->PCHSGeometry = *pPCHSGeometry;
|
---|
1252 | rc = VINF_SUCCESS;
|
---|
1253 | }
|
---|
1254 | else
|
---|
1255 | rc = VERR_VDI_NOT_OPENED;
|
---|
1256 |
|
---|
1257 | out:
|
---|
1258 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1259 | return rc;
|
---|
1260 | }
|
---|
1261 |
|
---|
1262 | /** @copydoc VBOXHDDBACKEND::pfnGetLCHSGeometry */
|
---|
1263 | static int vdiGetLCHSGeometry(void *pBackendData,
|
---|
1264 | PPDMMEDIAGEOMETRY pLCHSGeometry)
|
---|
1265 | {
|
---|
1266 | LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
|
---|
1267 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1268 | int rc;
|
---|
1269 |
|
---|
1270 | Assert(VALID_PTR(pImage));
|
---|
1271 |
|
---|
1272 | if (pImage)
|
---|
1273 | {
|
---|
1274 | VDIDISKGEOMETRY DummyGeo = { 0, 0, 0, VDI_GEOMETRY_SECTOR_SIZE };
|
---|
1275 | PVDIDISKGEOMETRY pGeometry = getImageLCHSGeometry(&pImage->Header);
|
---|
1276 | if (!pGeometry)
|
---|
1277 | pGeometry = &DummyGeo;
|
---|
1278 |
|
---|
1279 | if ( pGeometry->cCylinders > 0
|
---|
1280 | && pGeometry->cHeads > 0
|
---|
1281 | && pGeometry->cSectors > 0)
|
---|
1282 | {
|
---|
1283 | pLCHSGeometry->cCylinders = pGeometry->cCylinders;
|
---|
1284 | pLCHSGeometry->cHeads = pGeometry->cHeads;
|
---|
1285 | pLCHSGeometry->cSectors = pGeometry->cSectors;
|
---|
1286 | rc = VINF_SUCCESS;
|
---|
1287 | }
|
---|
1288 | else
|
---|
1289 | rc = VERR_VDI_GEOMETRY_NOT_SET;
|
---|
1290 | }
|
---|
1291 | else
|
---|
1292 | rc = VERR_VDI_NOT_OPENED;
|
---|
1293 |
|
---|
1294 | LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
|
---|
1295 | return rc;
|
---|
1296 | }
|
---|
1297 |
|
---|
1298 | /** @copydoc VBOXHDDBACKEND::pfnSetLCHSGeometry */
|
---|
1299 | static int vdiSetLCHSGeometry(void *pBackendData,
|
---|
1300 | PCPDMMEDIAGEOMETRY pLCHSGeometry)
|
---|
1301 | {
|
---|
1302 | LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
|
---|
1303 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1304 | PVDIDISKGEOMETRY pGeometry;
|
---|
1305 | int rc;
|
---|
1306 |
|
---|
1307 | Assert(VALID_PTR(pImage));
|
---|
1308 |
|
---|
1309 | if (pImage)
|
---|
1310 | {
|
---|
1311 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
1312 | {
|
---|
1313 | rc = VERR_VDI_IMAGE_READ_ONLY;
|
---|
1314 | goto out;
|
---|
1315 | }
|
---|
1316 |
|
---|
1317 | pGeometry = getImageLCHSGeometry(&pImage->Header);
|
---|
1318 | if (pGeometry)
|
---|
1319 | {
|
---|
1320 | pGeometry->cCylinders = pLCHSGeometry->cCylinders;
|
---|
1321 | pGeometry->cHeads = pLCHSGeometry->cHeads;
|
---|
1322 | pGeometry->cSectors = pLCHSGeometry->cSectors;
|
---|
1323 | pGeometry->cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
1324 |
|
---|
1325 | /* Update header information in base image file. */
|
---|
1326 | vdiFlushImage(pImage);
|
---|
1327 | }
|
---|
1328 | rc = VINF_SUCCESS;
|
---|
1329 | }
|
---|
1330 | else
|
---|
1331 | rc = VERR_VDI_NOT_OPENED;
|
---|
1332 |
|
---|
1333 | out:
|
---|
1334 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1335 | return rc;
|
---|
1336 | }
|
---|
1337 |
|
---|
1338 | /** @copydoc VBOXHDDBACKEND::pfnGetImageFlags */
|
---|
1339 | static unsigned vdiGetImageFlags(void *pBackendData)
|
---|
1340 | {
|
---|
1341 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
1342 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1343 | unsigned uImageFlags;
|
---|
1344 |
|
---|
1345 | Assert(VALID_PTR(pImage));
|
---|
1346 |
|
---|
1347 | if (pImage)
|
---|
1348 | uImageFlags = pImage->uImageFlags;
|
---|
1349 | else
|
---|
1350 | uImageFlags = 0;
|
---|
1351 |
|
---|
1352 | LogFlowFunc(("returns %#x\n", uImageFlags));
|
---|
1353 | return uImageFlags;
|
---|
1354 | }
|
---|
1355 |
|
---|
1356 | /** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
|
---|
1357 | static unsigned vdiGetOpenFlags(void *pBackendData)
|
---|
1358 | {
|
---|
1359 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
1360 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1361 | unsigned uOpenFlags;
|
---|
1362 |
|
---|
1363 | Assert(VALID_PTR(pImage));
|
---|
1364 |
|
---|
1365 | if (pImage)
|
---|
1366 | uOpenFlags = pImage->uOpenFlags;
|
---|
1367 | else
|
---|
1368 | uOpenFlags = 0;
|
---|
1369 |
|
---|
1370 | LogFlowFunc(("returns %#x\n", uOpenFlags));
|
---|
1371 | return uOpenFlags;
|
---|
1372 | }
|
---|
1373 |
|
---|
1374 | /** @copydoc VBOXHDDBACKEND::pfnSetOpenFlags */
|
---|
1375 | static int vdiSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
|
---|
1376 | {
|
---|
1377 | LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
|
---|
1378 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1379 | int rc;
|
---|
1380 | const char *pszFilename;
|
---|
1381 |
|
---|
1382 | /* Image must be opened and the new flags must be valid. Just readonly flag
|
---|
1383 | * is supported. */
|
---|
1384 | if (!pImage || uOpenFlags & ~VD_OPEN_FLAGS_READONLY)
|
---|
1385 | {
|
---|
1386 | rc = VERR_INVALID_PARAMETER;
|
---|
1387 | goto out;
|
---|
1388 | }
|
---|
1389 |
|
---|
1390 | /* Implement this operation via reopening the image. */
|
---|
1391 | pszFilename = pImage->pszFilename;
|
---|
1392 | vdiFreeImage(pImage, false);
|
---|
1393 | rc = vdiOpenImage(pImage, uOpenFlags);
|
---|
1394 |
|
---|
1395 | out:
|
---|
1396 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1397 | return rc;
|
---|
1398 | }
|
---|
1399 |
|
---|
1400 | /** @copydoc VBOXHDDBACKEND::pfnGetComment */
|
---|
1401 | static int vdiGetComment(void *pBackendData, char *pszComment,
|
---|
1402 | size_t cbComment)
|
---|
1403 | {
|
---|
1404 | LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
|
---|
1405 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1406 | int rc = VINF_SUCCESS;
|
---|
1407 |
|
---|
1408 | Assert(VALID_PTR(pImage));
|
---|
1409 |
|
---|
1410 | if (pImage)
|
---|
1411 | {
|
---|
1412 | char *pszTmp = getImageComment(&pImage->Header);
|
---|
1413 | unsigned cb = strlen(pszTmp);
|
---|
1414 | if (cb < cbComment)
|
---|
1415 | {
|
---|
1416 | /* memcpy is much better than strncpy. */
|
---|
1417 | memcpy(pszComment, pszTmp, cb + 1);
|
---|
1418 | }
|
---|
1419 | else
|
---|
1420 | rc = VERR_BUFFER_OVERFLOW;
|
---|
1421 | }
|
---|
1422 | else
|
---|
1423 | rc = VERR_VDI_NOT_OPENED;
|
---|
1424 |
|
---|
1425 | LogFlowFunc(("returns %Rrc comment=\"%s\"\n", rc, pszComment));
|
---|
1426 | return rc;
|
---|
1427 | }
|
---|
1428 |
|
---|
1429 | /** @copydoc VBOXHDDBACKEND::pfnGetComment */
|
---|
1430 | static int vdiSetComment(void *pBackendData, const char *pszComment)
|
---|
1431 | {
|
---|
1432 | LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
|
---|
1433 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1434 | int rc;
|
---|
1435 |
|
---|
1436 | Assert(VALID_PTR(pImage));
|
---|
1437 |
|
---|
1438 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
1439 | {
|
---|
1440 | rc = VERR_VDI_IMAGE_READ_ONLY;
|
---|
1441 | goto out;
|
---|
1442 | }
|
---|
1443 |
|
---|
1444 | if (pImage)
|
---|
1445 | {
|
---|
1446 | size_t cchComment = pszComment ? strlen(pszComment) : 0;
|
---|
1447 | if (cchComment >= VDI_IMAGE_COMMENT_SIZE)
|
---|
1448 | {
|
---|
1449 | LogFunc(("pszComment is too long, %d bytes!\n", cchComment));
|
---|
1450 | rc = VERR_VDI_COMMENT_TOO_LONG;
|
---|
1451 | goto out;
|
---|
1452 | }
|
---|
1453 |
|
---|
1454 | /* we don't support old style images */
|
---|
1455 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
1456 | {
|
---|
1457 | /*
|
---|
1458 | * Update the comment field, making sure to zero out all of the previous comment.
|
---|
1459 | */
|
---|
1460 | memset(pImage->Header.u.v1.szComment, '\0', VDI_IMAGE_COMMENT_SIZE);
|
---|
1461 | memcpy(pImage->Header.u.v1.szComment, pszComment, cchComment);
|
---|
1462 |
|
---|
1463 | /* write out new the header */
|
---|
1464 | rc = vdiUpdateHeader(pImage);
|
---|
1465 | }
|
---|
1466 | else
|
---|
1467 | rc = VERR_VDI_UNSUPPORTED_VERSION;
|
---|
1468 | }
|
---|
1469 | else
|
---|
1470 | rc = VERR_VDI_NOT_OPENED;
|
---|
1471 |
|
---|
1472 | out:
|
---|
1473 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1474 | return rc;
|
---|
1475 | }
|
---|
1476 |
|
---|
1477 | /** @copydoc VBOXHDDBACKEND::pfnGetUuid */
|
---|
1478 | static int vdiGetUuid(void *pBackendData, PRTUUID pUuid)
|
---|
1479 | {
|
---|
1480 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
1481 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1482 | int rc;
|
---|
1483 |
|
---|
1484 | Assert(VALID_PTR(pImage));
|
---|
1485 |
|
---|
1486 | if (pImage)
|
---|
1487 | {
|
---|
1488 | *pUuid = *getImageCreationUUID(&pImage->Header);
|
---|
1489 | rc = VINF_SUCCESS;
|
---|
1490 | }
|
---|
1491 | else
|
---|
1492 | rc = VERR_VDI_NOT_OPENED;
|
---|
1493 |
|
---|
1494 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
|
---|
1495 | return rc;
|
---|
1496 | }
|
---|
1497 |
|
---|
1498 | /** @copydoc VBOXHDDBACKEND::pfnSetUuid */
|
---|
1499 | static int vdiSetUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
1500 | {
|
---|
1501 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
1502 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1503 | int rc = VINF_SUCCESS;
|
---|
1504 |
|
---|
1505 | Assert(VALID_PTR(pImage));
|
---|
1506 |
|
---|
1507 | if (pImage)
|
---|
1508 | {
|
---|
1509 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
1510 | {
|
---|
1511 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
1512 | pImage->Header.u.v1.uuidCreate = *pUuid;
|
---|
1513 | /* Make it possible to clone old VDIs. */
|
---|
1514 | else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
|
---|
1515 | pImage->Header.u.v0.uuidCreate = *pUuid;
|
---|
1516 | else
|
---|
1517 | {
|
---|
1518 | LogFunc(("Version is not supported!\n"));
|
---|
1519 | rc = VERR_VDI_UNSUPPORTED_VERSION;
|
---|
1520 | }
|
---|
1521 | }
|
---|
1522 | else
|
---|
1523 | rc = VERR_VDI_IMAGE_READ_ONLY;
|
---|
1524 | }
|
---|
1525 | else
|
---|
1526 | rc = VERR_VDI_NOT_OPENED;
|
---|
1527 |
|
---|
1528 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1529 | return rc;
|
---|
1530 | }
|
---|
1531 |
|
---|
1532 | /** @copydoc VBOXHDDBACKEND::pfnGetModificationUuid */
|
---|
1533 | static int vdiGetModificationUuid(void *pBackendData, PRTUUID pUuid)
|
---|
1534 | {
|
---|
1535 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
1536 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1537 | int rc;
|
---|
1538 |
|
---|
1539 | Assert(VALID_PTR(pImage));
|
---|
1540 |
|
---|
1541 | if (pImage)
|
---|
1542 | {
|
---|
1543 | *pUuid = *getImageModificationUUID(&pImage->Header);
|
---|
1544 | rc = VINF_SUCCESS;
|
---|
1545 | }
|
---|
1546 | else
|
---|
1547 | rc = VERR_VDI_NOT_OPENED;
|
---|
1548 |
|
---|
1549 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
|
---|
1550 | return rc;
|
---|
1551 | }
|
---|
1552 |
|
---|
1553 | /** @copydoc VBOXHDDBACKEND::pfnSetModificationUuid */
|
---|
1554 | static int vdiSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
1555 | {
|
---|
1556 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
1557 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1558 | int rc = VINF_SUCCESS;
|
---|
1559 |
|
---|
1560 | Assert(VALID_PTR(pImage));
|
---|
1561 |
|
---|
1562 | if (pImage)
|
---|
1563 | {
|
---|
1564 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
1565 | {
|
---|
1566 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
1567 | pImage->Header.u.v1.uuidModify = *pUuid;
|
---|
1568 | /* Make it possible to clone old VDIs. */
|
---|
1569 | else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
|
---|
1570 | pImage->Header.u.v0.uuidModify = *pUuid;
|
---|
1571 | else
|
---|
1572 | {
|
---|
1573 | LogFunc(("Version is not supported!\n"));
|
---|
1574 | rc = VERR_VDI_UNSUPPORTED_VERSION;
|
---|
1575 | }
|
---|
1576 | }
|
---|
1577 | else
|
---|
1578 | rc = VERR_VDI_IMAGE_READ_ONLY;
|
---|
1579 | }
|
---|
1580 | else
|
---|
1581 | rc = VERR_VDI_NOT_OPENED;
|
---|
1582 |
|
---|
1583 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1584 | return rc;
|
---|
1585 | }
|
---|
1586 |
|
---|
1587 | /** @copydoc VBOXHDDBACKEND::pfnGetParentUuid */
|
---|
1588 | static int vdiGetParentUuid(void *pBackendData, PRTUUID pUuid)
|
---|
1589 | {
|
---|
1590 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
1591 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1592 | int rc;
|
---|
1593 |
|
---|
1594 | Assert(VALID_PTR(pImage));
|
---|
1595 |
|
---|
1596 | if (pImage)
|
---|
1597 | {
|
---|
1598 | *pUuid = *getImageParentUUID(&pImage->Header);
|
---|
1599 | rc = VINF_SUCCESS;
|
---|
1600 | }
|
---|
1601 | else
|
---|
1602 | rc = VERR_VDI_NOT_OPENED;
|
---|
1603 |
|
---|
1604 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
|
---|
1605 | return rc;
|
---|
1606 | }
|
---|
1607 |
|
---|
1608 | /** @copydoc VBOXHDDBACKEND::pfnSetParentUuid */
|
---|
1609 | static int vdiSetParentUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
1610 | {
|
---|
1611 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
1612 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1613 | int rc = VINF_SUCCESS;
|
---|
1614 |
|
---|
1615 | Assert(VALID_PTR(pImage));
|
---|
1616 |
|
---|
1617 | if (pImage)
|
---|
1618 | {
|
---|
1619 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
1620 | {
|
---|
1621 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
1622 | pImage->Header.u.v1.uuidLinkage = *pUuid;
|
---|
1623 | /* Make it possible to clone old VDIs. */
|
---|
1624 | else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
|
---|
1625 | pImage->Header.u.v0.uuidLinkage = *pUuid;
|
---|
1626 | else
|
---|
1627 | {
|
---|
1628 | LogFunc(("Version is not supported!\n"));
|
---|
1629 | rc = VERR_VDI_UNSUPPORTED_VERSION;
|
---|
1630 | }
|
---|
1631 | }
|
---|
1632 | else
|
---|
1633 | rc = VERR_VDI_IMAGE_READ_ONLY;
|
---|
1634 | }
|
---|
1635 | else
|
---|
1636 | rc = VERR_VDI_NOT_OPENED;
|
---|
1637 |
|
---|
1638 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1639 | return rc;
|
---|
1640 | }
|
---|
1641 |
|
---|
1642 | /** @copydoc VBOXHDDBACKEND::pfnGetParentModificationUuid */
|
---|
1643 | static int vdiGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
|
---|
1644 | {
|
---|
1645 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
1646 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1647 | int rc;
|
---|
1648 |
|
---|
1649 | Assert(VALID_PTR(pImage));
|
---|
1650 |
|
---|
1651 | if (pImage)
|
---|
1652 | {
|
---|
1653 | *pUuid = *getImageParentModificationUUID(&pImage->Header);
|
---|
1654 | rc = VINF_SUCCESS;
|
---|
1655 | }
|
---|
1656 | else
|
---|
1657 | rc = VERR_VDI_NOT_OPENED;
|
---|
1658 |
|
---|
1659 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
|
---|
1660 | return rc;
|
---|
1661 | }
|
---|
1662 |
|
---|
1663 | /** @copydoc VBOXHDDBACKEND::pfnSetParentModificationUuid */
|
---|
1664 | static int vdiSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
1665 | {
|
---|
1666 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
1667 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1668 | int rc = VINF_SUCCESS;
|
---|
1669 |
|
---|
1670 | Assert(VALID_PTR(pImage));
|
---|
1671 |
|
---|
1672 | if (pImage)
|
---|
1673 | {
|
---|
1674 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
1675 | {
|
---|
1676 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
1677 | pImage->Header.u.v1.uuidParentModify = *pUuid;
|
---|
1678 | else
|
---|
1679 | {
|
---|
1680 | LogFunc(("Version is not supported!\n"));
|
---|
1681 | rc = VERR_VDI_UNSUPPORTED_VERSION;
|
---|
1682 | }
|
---|
1683 | }
|
---|
1684 | else
|
---|
1685 | rc = VERR_VDI_IMAGE_READ_ONLY;
|
---|
1686 | }
|
---|
1687 | else
|
---|
1688 | rc = VERR_VDI_NOT_OPENED;
|
---|
1689 |
|
---|
1690 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1691 | return rc;
|
---|
1692 | }
|
---|
1693 |
|
---|
1694 | /** @copydoc VBOXHDDBACKEND::pfnDump */
|
---|
1695 | static void vdiDump(void *pBackendData)
|
---|
1696 | {
|
---|
1697 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1698 |
|
---|
1699 | RTLogPrintf("Dumping VDI image \"%s\" mode=%s uOpenFlags=%X File=%08X\n",
|
---|
1700 | pImage->pszFilename,
|
---|
1701 | (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY) ? "r/o" : "r/w",
|
---|
1702 | pImage->uOpenFlags,
|
---|
1703 | pImage->File);
|
---|
1704 | RTLogPrintf("Header: Version=%08X Type=%X Flags=%X Size=%llu\n",
|
---|
1705 | pImage->PreHeader.u32Version,
|
---|
1706 | getImageType(&pImage->Header),
|
---|
1707 | getImageFlags(&pImage->Header),
|
---|
1708 | getImageDiskSize(&pImage->Header));
|
---|
1709 | RTLogPrintf("Header: cbBlock=%u cbBlockExtra=%u cBlocks=%u cBlocksAllocated=%u\n",
|
---|
1710 | getImageBlockSize(&pImage->Header),
|
---|
1711 | getImageExtraBlockSize(&pImage->Header),
|
---|
1712 | getImageBlocks(&pImage->Header),
|
---|
1713 | getImageBlocksAllocated(&pImage->Header));
|
---|
1714 | RTLogPrintf("Header: offBlocks=%u offData=%u\n",
|
---|
1715 | getImageBlocksOffset(&pImage->Header),
|
---|
1716 | getImageDataOffset(&pImage->Header));
|
---|
1717 | PVDIDISKGEOMETRY pg = getImageLCHSGeometry(&pImage->Header);
|
---|
1718 | if (pg)
|
---|
1719 | RTLogPrintf("Header: Geometry: C/H/S=%u/%u/%u cbSector=%u\n",
|
---|
1720 | pg->cCylinders, pg->cHeads, pg->cSectors, pg->cbSector);
|
---|
1721 | RTLogPrintf("Header: uuidCreation={%RTuuid}\n", getImageCreationUUID(&pImage->Header));
|
---|
1722 | RTLogPrintf("Header: uuidModification={%RTuuid}\n", getImageModificationUUID(&pImage->Header));
|
---|
1723 | RTLogPrintf("Header: uuidParent={%RTuuid}\n", getImageParentUUID(&pImage->Header));
|
---|
1724 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) >= 1)
|
---|
1725 | RTLogPrintf("Header: uuidParentModification={%RTuuid}\n", getImageParentModificationUUID(&pImage->Header));
|
---|
1726 | RTLogPrintf("Image: fFlags=%08X offStartBlocks=%u offStartData=%u\n",
|
---|
1727 | pImage->uImageFlags, pImage->offStartBlocks, pImage->offStartData);
|
---|
1728 | RTLogPrintf("Image: uBlockMask=%08X cbTotalBlockData=%u uShiftOffset2Index=%u offStartBlockData=%u\n",
|
---|
1729 | pImage->uBlockMask,
|
---|
1730 | pImage->cbTotalBlockData,
|
---|
1731 | pImage->uShiftOffset2Index,
|
---|
1732 | pImage->offStartBlockData);
|
---|
1733 |
|
---|
1734 | unsigned uBlock, cBlocksNotFree, cBadBlocks, cBlocks = getImageBlocks(&pImage->Header);
|
---|
1735 | for (uBlock=0, cBlocksNotFree=0, cBadBlocks=0; uBlock<cBlocks; uBlock++)
|
---|
1736 | {
|
---|
1737 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
|
---|
1738 | {
|
---|
1739 | cBlocksNotFree++;
|
---|
1740 | if (pImage->paBlocks[uBlock] >= cBlocks)
|
---|
1741 | cBadBlocks++;
|
---|
1742 | }
|
---|
1743 | }
|
---|
1744 | if (cBlocksNotFree != getImageBlocksAllocated(&pImage->Header))
|
---|
1745 | {
|
---|
1746 | RTLogPrintf("!! WARNING: %u blocks actually allocated (cBlocksAllocated=%u) !!\n",
|
---|
1747 | cBlocksNotFree, getImageBlocksAllocated(&pImage->Header));
|
---|
1748 | }
|
---|
1749 | if (cBadBlocks)
|
---|
1750 | {
|
---|
1751 | RTLogPrintf("!! WARNING: %u bad blocks found !!\n",
|
---|
1752 | cBadBlocks);
|
---|
1753 | }
|
---|
1754 | }
|
---|
1755 |
|
---|
1756 | static int vdiGetTimeStamp(void *pvBackendData, PRTTIMESPEC pTimeStamp)
|
---|
1757 | {
|
---|
1758 | int rc = VERR_NOT_IMPLEMENTED;
|
---|
1759 | LogFlow(("%s: returned %Rrc\n", __FUNCTION__, rc));
|
---|
1760 | return rc;
|
---|
1761 | }
|
---|
1762 |
|
---|
1763 | static int vdiGetParentTimeStamp(void *pvBackendData, PRTTIMESPEC pTimeStamp)
|
---|
1764 | {
|
---|
1765 | int rc = VERR_NOT_IMPLEMENTED;
|
---|
1766 | LogFlow(("%s: returned %Rrc\n", __FUNCTION__, rc));
|
---|
1767 | return rc;
|
---|
1768 | }
|
---|
1769 |
|
---|
1770 | static int vdiSetParentTimeStamp(void *pvBackendData, PCRTTIMESPEC pTimeStamp)
|
---|
1771 | {
|
---|
1772 | int rc = VERR_NOT_IMPLEMENTED;
|
---|
1773 | LogFlow(("%s: returned %Rrc\n", __FUNCTION__, rc));
|
---|
1774 | return rc;
|
---|
1775 | }
|
---|
1776 |
|
---|
1777 | static int vdiGetParentFilename(void *pvBackendData, char **ppszParentFilename)
|
---|
1778 | {
|
---|
1779 | int rc = VERR_NOT_IMPLEMENTED;
|
---|
1780 | LogFlow(("%s: returned %Rrc\n", __FUNCTION__, rc));
|
---|
1781 | return rc;
|
---|
1782 | }
|
---|
1783 |
|
---|
1784 | static int vdiSetParentFilename(void *pvBackendData, const char *pszParentFilename)
|
---|
1785 | {
|
---|
1786 | int rc = VERR_NOT_IMPLEMENTED;
|
---|
1787 | LogFlow(("%s: returned %Rrc\n", __FUNCTION__, rc));
|
---|
1788 | return rc;
|
---|
1789 | }
|
---|
1790 |
|
---|
1791 | static bool vdiIsAsyncIOSupported(void *pvBackendData)
|
---|
1792 | {
|
---|
1793 | return false;
|
---|
1794 | }
|
---|
1795 |
|
---|
1796 | static int vdiAsyncRead(void *pvBackendData, uint64_t uOffset, size_t cbRead,
|
---|
1797 | PPDMDATASEG paSeg, unsigned cSeg, void *pvUser)
|
---|
1798 | {
|
---|
1799 | int rc = VERR_NOT_IMPLEMENTED;
|
---|
1800 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1801 | return rc;
|
---|
1802 | }
|
---|
1803 |
|
---|
1804 | static int vdiAsyncWrite(void *pvBackendData, uint64_t uOffset, size_t cbWrite,
|
---|
1805 | PPDMDATASEG paSeg, unsigned cSeg, void *pvUser)
|
---|
1806 | {
|
---|
1807 | int rc = VERR_NOT_IMPLEMENTED;
|
---|
1808 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1809 | return rc;
|
---|
1810 | }
|
---|
1811 |
|
---|
1812 |
|
---|
1813 | VBOXHDDBACKEND g_VDIBackend =
|
---|
1814 | {
|
---|
1815 | /* pszBackendName */
|
---|
1816 | "VDI",
|
---|
1817 | /* cbSize */
|
---|
1818 | sizeof(VBOXHDDBACKEND),
|
---|
1819 | /* uBackendCaps */
|
---|
1820 | VD_CAP_UUID | VD_CAP_CREATE_FIXED | VD_CAP_CREATE_DYNAMIC
|
---|
1821 | | VD_CAP_CREATE_SPLIT_2G | VD_CAP_DIFF | VD_CAP_FILE,
|
---|
1822 | /* papszFileExtensions */
|
---|
1823 | s_apszVdiFileExtensions,
|
---|
1824 | /* paConfigInfo */
|
---|
1825 | NULL,
|
---|
1826 | /* pfnCheckIfValid */
|
---|
1827 | vdiCheckIfValid,
|
---|
1828 | /* pfnOpen */
|
---|
1829 | vdiOpen,
|
---|
1830 | /* pfnCreate */
|
---|
1831 | vdiCreate,
|
---|
1832 | /* pfnRename */
|
---|
1833 | vdiRename,
|
---|
1834 | /* pfnClose */
|
---|
1835 | vdiClose,
|
---|
1836 | /* pfnRead */
|
---|
1837 | vdiRead,
|
---|
1838 | /* pfnWrite */
|
---|
1839 | vdiWrite,
|
---|
1840 | /* pfnFlush */
|
---|
1841 | vdiFlush,
|
---|
1842 | /* pfnGetVersion */
|
---|
1843 | vdiGetVersion,
|
---|
1844 | /* pfnGetImageType */
|
---|
1845 | vdiGetImageType,
|
---|
1846 | /* pfnGetSize */
|
---|
1847 | vdiGetSize,
|
---|
1848 | /* pfnGetFileSize */
|
---|
1849 | vdiGetFileSize,
|
---|
1850 | /* pfnGetPCHSGeometry */
|
---|
1851 | vdiGetPCHSGeometry,
|
---|
1852 | /* pfnSetPCHSGeometry */
|
---|
1853 | vdiSetPCHSGeometry,
|
---|
1854 | /* pfnGetLCHSGeometry */
|
---|
1855 | vdiGetLCHSGeometry,
|
---|
1856 | /* pfnSetLCHSGeometry */
|
---|
1857 | vdiSetLCHSGeometry,
|
---|
1858 | /* pfnGetImageFlags */
|
---|
1859 | vdiGetImageFlags,
|
---|
1860 | /* pfnGetOpenFlags */
|
---|
1861 | vdiGetOpenFlags,
|
---|
1862 | /* pfnSetOpenFlags */
|
---|
1863 | vdiSetOpenFlags,
|
---|
1864 | /* pfnGetComment */
|
---|
1865 | vdiGetComment,
|
---|
1866 | /* pfnSetComment */
|
---|
1867 | vdiSetComment,
|
---|
1868 | /* pfnGetUuid */
|
---|
1869 | vdiGetUuid,
|
---|
1870 | /* pfnSetUuid */
|
---|
1871 | vdiSetUuid,
|
---|
1872 | /* pfnGetModificationUuid */
|
---|
1873 | vdiGetModificationUuid,
|
---|
1874 | /* pfnSetModificationUuid */
|
---|
1875 | vdiSetModificationUuid,
|
---|
1876 | /* pfnGetParentUuid */
|
---|
1877 | vdiGetParentUuid,
|
---|
1878 | /* pfnSetParentUuid */
|
---|
1879 | vdiSetParentUuid,
|
---|
1880 | /* pfnGetParentModificationUuid */
|
---|
1881 | vdiGetParentModificationUuid,
|
---|
1882 | /* pfnSetParentModificationUuid */
|
---|
1883 | vdiSetParentModificationUuid,
|
---|
1884 | /* pfnDump */
|
---|
1885 | vdiDump,
|
---|
1886 | /* pfnGetTimeStamp */
|
---|
1887 | vdiGetTimeStamp,
|
---|
1888 | /* pfnGetParentTimeStamp */
|
---|
1889 | vdiGetParentTimeStamp,
|
---|
1890 | /* pfnSetParentTimeStamp */
|
---|
1891 | vdiSetParentTimeStamp,
|
---|
1892 | /* pfnGetParentFilename */
|
---|
1893 | vdiGetParentFilename,
|
---|
1894 | /* pfnSetParentFilename */
|
---|
1895 | vdiSetParentFilename,
|
---|
1896 | /* pfnIsAsyncIOSupported */
|
---|
1897 | vdiIsAsyncIOSupported,
|
---|
1898 | /* pfnAsyncRead */
|
---|
1899 | vdiAsyncRead,
|
---|
1900 | /* pfnAsyncWrite */
|
---|
1901 | vdiAsyncWrite
|
---|
1902 | };
|
---|
1903 |
|
---|