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_DRV_VBOXHDD
|
---|
25 | #include <VBox/VBoxHDD.h>
|
---|
26 | #include "VDICore.h"
|
---|
27 | #include <VBox/err.h>
|
---|
28 |
|
---|
29 | #include <VBox/log.h>
|
---|
30 | #include <iprt/alloc.h>
|
---|
31 | #include <iprt/assert.h>
|
---|
32 | #include <iprt/uuid.h>
|
---|
33 | #include <iprt/file.h>
|
---|
34 | #include <iprt/string.h>
|
---|
35 | #include <iprt/asm.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | /*******************************************************************************
|
---|
39 | * Internal Functions *
|
---|
40 | *******************************************************************************/
|
---|
41 | static unsigned getPowerOfTwo(unsigned uNumber);
|
---|
42 | static void vdiInitPreHeader(PVDIPREHEADER pPreHdr);
|
---|
43 | static int vdiValidatePreHeader(PVDIPREHEADER pPreHdr);
|
---|
44 | static void vdiInitHeader(PVDIHEADER pHeader, VDIIMAGETYPE enmType, uint32_t fFlags,
|
---|
45 | const char *pszComment, uint64_t cbDisk, uint32_t cbBlock,
|
---|
46 | uint32_t cbBlockExtra);
|
---|
47 | static int vdiValidateHeader(PVDIHEADER pHeader);
|
---|
48 | static int vdiCreateImage(const char *pszFilename, VDIIMAGETYPE enmType, unsigned fFlags,
|
---|
49 | uint64_t cbSize, const char *pszComment, PVDIIMAGEDESC pParent,
|
---|
50 | PFNVMPROGRESS pfnProgress, void *pvUser);
|
---|
51 | static void vdiInitImageDesc(PVDIIMAGEDESC pImage);
|
---|
52 | static void vdiSetupImageDesc(PVDIIMAGEDESC pImage);
|
---|
53 | static int vdiOpenImage(PVDIIMAGEDESC *ppImage, const char *pszFilename, unsigned fOpen,
|
---|
54 | PVDIIMAGEDESC pParent);
|
---|
55 | static int vdiUpdateHeader(PVDIIMAGEDESC pImage);
|
---|
56 | static int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock);
|
---|
57 | static int vdiUpdateBlocks(PVDIIMAGEDESC pImage);
|
---|
58 | static void vdiSetModifiedFlag(PVDIIMAGEDESC pImage);
|
---|
59 | static void vdiResetModifiedFlag(PVDIIMAGEDESC pImage);
|
---|
60 | static void vdiDisableLastModifiedUpdate(PVDIIMAGEDESC pImage);
|
---|
61 | #if 0 /* unused */
|
---|
62 | static void vdiEnableLastModifiedUpdate(PVDIIMAGEDESC pImage);
|
---|
63 | #endif
|
---|
64 | static void vdiCloseImage(PVDIIMAGEDESC pImage);
|
---|
65 | static int vdiReadInBlock(PVDIIMAGEDESC pImage, unsigned uBlock, unsigned offRead,
|
---|
66 | size_t cbToRead, void *pvBuf);
|
---|
67 | static int vdiFillBlockByZeroes(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock);
|
---|
68 | static int vdiWriteInBlock(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock,
|
---|
69 | unsigned offWrite, size_t cbToWrite, const void *pvBuf);
|
---|
70 | static int vdiCopyBlock(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock);
|
---|
71 | static int vdiMergeImages(PVDIIMAGEDESC pImageFrom, PVDIIMAGEDESC pImageTo, bool fParentToChild,
|
---|
72 | PFNVMPROGRESS pfnProgress, void *pvUser);
|
---|
73 | static void vdiAddImageToList(PVDIDISK pDisk, PVDIIMAGEDESC pImage);
|
---|
74 | static void vdiRemoveImageFromList(PVDIDISK pDisk, PVDIIMAGEDESC pImage);
|
---|
75 | static PVDIIMAGEDESC vdiGetImageByNumber(PVDIDISK pDisk, int nImage);
|
---|
76 | static int vdiUpdateReadOnlyHeader(PVDIIMAGEDESC pImage);
|
---|
77 |
|
---|
78 | static int vdiCommitToImage(PVDIDISK pDisk, PVDIIMAGEDESC pDstImage,
|
---|
79 | PFNVMPROGRESS pfnProgress, void *pvUser);
|
---|
80 | static void vdiDumpImage(PVDIIMAGEDESC pImage);
|
---|
81 |
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * internal: return power of 2 or 0 if num error.
|
---|
85 | */
|
---|
86 | static unsigned getPowerOfTwo(unsigned uNumber)
|
---|
87 | {
|
---|
88 | if (uNumber == 0)
|
---|
89 | return 0;
|
---|
90 | unsigned uPower2 = 0;
|
---|
91 | while ((uNumber & 1) == 0)
|
---|
92 | {
|
---|
93 | uNumber >>= 1;
|
---|
94 | uPower2++;
|
---|
95 | }
|
---|
96 | return uNumber == 1 ? uPower2 : 0;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * internal: init HDD preheader.
|
---|
101 | */
|
---|
102 | static void vdiInitPreHeader(PVDIPREHEADER pPreHdr)
|
---|
103 | {
|
---|
104 | pPreHdr->u32Signature = VDI_IMAGE_SIGNATURE;
|
---|
105 | pPreHdr->u32Version = VDI_IMAGE_VERSION;
|
---|
106 | memset(pPreHdr->szFileInfo, 0, sizeof(pPreHdr->szFileInfo));
|
---|
107 | strncat(pPreHdr->szFileInfo, VDI_IMAGE_FILE_INFO, sizeof(pPreHdr->szFileInfo));
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * internal: check HDD preheader.
|
---|
112 | */
|
---|
113 | static int vdiValidatePreHeader(PVDIPREHEADER pPreHdr)
|
---|
114 | {
|
---|
115 | if (pPreHdr->u32Signature != VDI_IMAGE_SIGNATURE)
|
---|
116 | return VERR_VDI_INVALID_SIGNATURE;
|
---|
117 |
|
---|
118 | if ( VDI_GET_VERSION_MAJOR(pPreHdr->u32Version) != VDI_IMAGE_VERSION_MAJOR
|
---|
119 | && pPreHdr->u32Version != 0x00000002) /* old version. */
|
---|
120 | return VERR_VDI_UNSUPPORTED_VERSION;
|
---|
121 |
|
---|
122 | return VINF_SUCCESS;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * internal: init HDD header. Always use latest header version.
|
---|
127 | * @param pHeader Assumes it was initially initialized to all zeros.
|
---|
128 | */
|
---|
129 | static void vdiInitHeader(PVDIHEADER pHeader, VDIIMAGETYPE enmType, uint32_t fFlags,
|
---|
130 | const char *pszComment, uint64_t cbDisk, uint32_t cbBlock,
|
---|
131 | uint32_t cbBlockExtra)
|
---|
132 | {
|
---|
133 | pHeader->uVersion = VDI_IMAGE_VERSION;
|
---|
134 | pHeader->u.v1.cbHeader = sizeof(VDIHEADER1);
|
---|
135 | pHeader->u.v1.u32Type = (uint32_t)enmType;
|
---|
136 | pHeader->u.v1.fFlags = fFlags;
|
---|
137 | #ifdef VBOX_STRICT
|
---|
138 | char achZero[VDI_IMAGE_COMMENT_SIZE] = {0};
|
---|
139 | Assert(!memcmp(pHeader->u.v1.szComment, achZero, VDI_IMAGE_COMMENT_SIZE));
|
---|
140 | #endif
|
---|
141 | pHeader->u.v1.szComment[0] = '\0';
|
---|
142 | if (pszComment)
|
---|
143 | {
|
---|
144 | AssertMsg(strlen(pszComment) < sizeof(pHeader->u.v1.szComment),
|
---|
145 | ("HDD Comment is too long, cb=%d\n", strlen(pszComment)));
|
---|
146 | strncat(pHeader->u.v1.szComment, pszComment, sizeof(pHeader->u.v1.szComment));
|
---|
147 | }
|
---|
148 |
|
---|
149 | /* Mark the legacy geometry not-calculated. */
|
---|
150 | pHeader->u.v1.LegacyGeometry.cCylinders = 0;
|
---|
151 | pHeader->u.v1.LegacyGeometry.cHeads = 0;
|
---|
152 | pHeader->u.v1.LegacyGeometry.cSectors = 0;
|
---|
153 | pHeader->u.v1.LegacyGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
154 | pHeader->u.v1.u32Dummy = 0; /* used to be the translation value */
|
---|
155 |
|
---|
156 | pHeader->u.v1.cbDisk = cbDisk;
|
---|
157 | pHeader->u.v1.cbBlock = cbBlock;
|
---|
158 | pHeader->u.v1.cBlocks = (uint32_t)(cbDisk / cbBlock);
|
---|
159 | if (cbDisk % cbBlock)
|
---|
160 | pHeader->u.v1.cBlocks++;
|
---|
161 | pHeader->u.v1.cbBlockExtra = cbBlockExtra;
|
---|
162 | pHeader->u.v1.cBlocksAllocated = 0;
|
---|
163 |
|
---|
164 | /* Init offsets. */
|
---|
165 | pHeader->u.v1.offBlocks = RT_ALIGN_32(sizeof(VDIPREHEADER) + sizeof(VDIHEADER1), VDI_GEOMETRY_SECTOR_SIZE);
|
---|
166 | pHeader->u.v1.offData = RT_ALIGN_32(pHeader->u.v1.offBlocks + (pHeader->u.v1.cBlocks * sizeof(VDIIMAGEBLOCKPOINTER)), VDI_GEOMETRY_SECTOR_SIZE);
|
---|
167 |
|
---|
168 | /* Init uuids. */
|
---|
169 | RTUuidCreate(&pHeader->u.v1.uuidCreate);
|
---|
170 | RTUuidClear(&pHeader->u.v1.uuidModify);
|
---|
171 | RTUuidClear(&pHeader->u.v1.uuidLinkage);
|
---|
172 | RTUuidClear(&pHeader->u.v1.uuidParentModify);
|
---|
173 |
|
---|
174 | /* Mark LCHS geometry not-calculated. */
|
---|
175 | pHeader->u.v1plus.LCHSGeometry.cCylinders = 0;
|
---|
176 | pHeader->u.v1plus.LCHSGeometry.cHeads = 0;
|
---|
177 | pHeader->u.v1plus.LCHSGeometry.cSectors = 0;
|
---|
178 | pHeader->u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * internal: check HDD header.
|
---|
183 | */
|
---|
184 | static int vdiValidateHeader(PVDIHEADER pHeader)
|
---|
185 | {
|
---|
186 | /* Check verion-dependend header parameters. */
|
---|
187 | switch (GET_MAJOR_HEADER_VERSION(pHeader))
|
---|
188 | {
|
---|
189 | case 0:
|
---|
190 | {
|
---|
191 | /* Old header version. */
|
---|
192 | break;
|
---|
193 | }
|
---|
194 | case 1:
|
---|
195 | {
|
---|
196 | /* Current header version. */
|
---|
197 |
|
---|
198 | if (pHeader->u.v1.cbHeader < sizeof(VDIHEADER1))
|
---|
199 | {
|
---|
200 | LogRel(("VDI: v1 header size wrong (%d < %d)\n",
|
---|
201 | pHeader->u.v1.cbHeader, sizeof(VDIHEADER1)));
|
---|
202 | return VERR_VDI_INVALID_HEADER;
|
---|
203 | }
|
---|
204 |
|
---|
205 | if (getImageBlocksOffset(pHeader) < (sizeof(VDIPREHEADER) + sizeof(VDIHEADER1)))
|
---|
206 | {
|
---|
207 | LogRel(("VDI: v1 blocks offset wrong (%d < %d)\n",
|
---|
208 | getImageBlocksOffset(pHeader), sizeof(VDIPREHEADER) + sizeof(VDIHEADER1)));
|
---|
209 | return VERR_VDI_INVALID_HEADER;
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (getImageDataOffset(pHeader) < (getImageBlocksOffset(pHeader) + getImageBlocks(pHeader) * sizeof(VDIIMAGEBLOCKPOINTER)))
|
---|
213 | {
|
---|
214 | LogRel(("VDI: v1 image data offset wrong (%d < %d)\n",
|
---|
215 | getImageDataOffset(pHeader), getImageBlocksOffset(pHeader) + getImageBlocks(pHeader) * sizeof(VDIIMAGEBLOCKPOINTER)));
|
---|
216 | return VERR_VDI_INVALID_HEADER;
|
---|
217 | }
|
---|
218 |
|
---|
219 | if ( getImageType(pHeader) == VDI_IMAGE_TYPE_UNDO
|
---|
220 | || getImageType(pHeader) == VDI_IMAGE_TYPE_DIFF)
|
---|
221 | {
|
---|
222 | if (RTUuidIsNull(getImageParentUUID(pHeader)))
|
---|
223 | {
|
---|
224 | LogRel(("VDI: v1 uuid of parent is 0)\n"));
|
---|
225 | return VERR_VDI_INVALID_HEADER;
|
---|
226 | }
|
---|
227 | if (RTUuidIsNull(getImageParentModificationUUID(pHeader)))
|
---|
228 | {
|
---|
229 | LogRel(("VDI: v1 uuid of parent modification is 0\n"));
|
---|
230 | return VERR_VDI_INVALID_HEADER;
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | break;
|
---|
235 | }
|
---|
236 | default:
|
---|
237 | /* Unsupported. */
|
---|
238 | return VERR_VDI_UNSUPPORTED_VERSION;
|
---|
239 | }
|
---|
240 |
|
---|
241 | /* Check common header parameters. */
|
---|
242 |
|
---|
243 | bool fFailed = false;
|
---|
244 |
|
---|
245 | if ( getImageType(pHeader) < VDI_IMAGE_TYPE_FIRST
|
---|
246 | || getImageType(pHeader) > VDI_IMAGE_TYPE_LAST)
|
---|
247 | {
|
---|
248 | LogRel(("VDI: bad image type %d\n", getImageType(pHeader)));
|
---|
249 | fFailed = true;
|
---|
250 | }
|
---|
251 |
|
---|
252 | if (getImageFlags(pHeader) & ~VDI_IMAGE_FLAGS_MASK)
|
---|
253 | {
|
---|
254 | LogRel(("VDI: bad image flags %08x\n", getImageFlags(pHeader)));
|
---|
255 | fFailed = true;
|
---|
256 | }
|
---|
257 |
|
---|
258 | if ( getImageLCHSGeometry(pHeader)
|
---|
259 | && (getImageLCHSGeometry(pHeader))->cbSector != VDI_GEOMETRY_SECTOR_SIZE)
|
---|
260 | {
|
---|
261 | LogRel(("VDI: wrong sector size (%d != %d)\n",
|
---|
262 | (getImageLCHSGeometry(pHeader))->cbSector, VDI_GEOMETRY_SECTOR_SIZE));
|
---|
263 | fFailed = true;
|
---|
264 | }
|
---|
265 |
|
---|
266 | if ( getImageDiskSize(pHeader) == 0
|
---|
267 | || getImageBlockSize(pHeader) == 0
|
---|
268 | || getImageBlocks(pHeader) == 0
|
---|
269 | || getPowerOfTwo(getImageBlockSize(pHeader)) == 0)
|
---|
270 | {
|
---|
271 | LogRel(("VDI: wrong size (%lld, %d, %d, %d)\n",
|
---|
272 | getImageDiskSize(pHeader), getImageBlockSize(pHeader),
|
---|
273 | getImageBlocks(pHeader), getPowerOfTwo(getImageBlockSize(pHeader))));
|
---|
274 | fFailed = true;
|
---|
275 | }
|
---|
276 |
|
---|
277 | if (getImageBlocksAllocated(pHeader) > getImageBlocks(pHeader))
|
---|
278 | {
|
---|
279 | LogRel(("VDI: too many blocks allocated (%d > %d)\n"
|
---|
280 | " blocksize=%d disksize=%lld\n",
|
---|
281 | getImageBlocksAllocated(pHeader), getImageBlocks(pHeader),
|
---|
282 | getImageBlockSize(pHeader), getImageDiskSize(pHeader)));
|
---|
283 | fFailed = true;
|
---|
284 | }
|
---|
285 |
|
---|
286 | if ( getImageExtraBlockSize(pHeader) != 0
|
---|
287 | && getPowerOfTwo(getImageExtraBlockSize(pHeader)) == 0)
|
---|
288 | {
|
---|
289 | LogRel(("VDI: wrong extra size (%d, %d)\n",
|
---|
290 | getImageExtraBlockSize(pHeader), getPowerOfTwo(getImageExtraBlockSize(pHeader))));
|
---|
291 | fFailed = true;
|
---|
292 | }
|
---|
293 |
|
---|
294 | if ((uint64_t)getImageBlockSize(pHeader) * getImageBlocks(pHeader) < getImageDiskSize(pHeader))
|
---|
295 | {
|
---|
296 | LogRel(("VDI: wrong disk size (%d, %d, %lld)\n",
|
---|
297 | getImageBlockSize(pHeader), getImageBlocks(pHeader), getImageDiskSize(pHeader)));
|
---|
298 | fFailed = true;
|
---|
299 | }
|
---|
300 |
|
---|
301 | if (RTUuidIsNull(getImageCreationUUID(pHeader)))
|
---|
302 | {
|
---|
303 | LogRel(("VDI: uuid of creator is 0\n"));
|
---|
304 | fFailed = true;
|
---|
305 | }
|
---|
306 |
|
---|
307 | if (RTUuidIsNull(getImageModificationUUID(pHeader)))
|
---|
308 | {
|
---|
309 | LogRel(("VDI: uuid of modificator is 0\n"));
|
---|
310 | fFailed = true;
|
---|
311 | }
|
---|
312 |
|
---|
313 | return fFailed ? VERR_VDI_INVALID_HEADER : VINF_SUCCESS;
|
---|
314 | }
|
---|
315 |
|
---|
316 | /**
|
---|
317 | * internal: init VDIIMAGEDESC structure.
|
---|
318 | */
|
---|
319 | static void vdiInitImageDesc(PVDIIMAGEDESC pImage)
|
---|
320 | {
|
---|
321 | pImage->pPrev = NULL;
|
---|
322 | pImage->pNext = NULL;
|
---|
323 | pImage->File = NIL_RTFILE;
|
---|
324 | pImage->paBlocks = NULL;
|
---|
325 | }
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * internal: setup VDIIMAGEDESC structure by image header.
|
---|
329 | */
|
---|
330 | static void vdiSetupImageDesc(PVDIIMAGEDESC pImage)
|
---|
331 | {
|
---|
332 | pImage->fFlags = getImageFlags(&pImage->Header);
|
---|
333 | pImage->offStartBlocks = getImageBlocksOffset(&pImage->Header);
|
---|
334 | pImage->offStartData = getImageDataOffset(&pImage->Header);
|
---|
335 | pImage->uBlockMask = getImageBlockSize(&pImage->Header) - 1;
|
---|
336 | pImage->uShiftIndex2Offset =
|
---|
337 | pImage->uShiftOffset2Index = getPowerOfTwo(getImageBlockSize(&pImage->Header));
|
---|
338 | pImage->offStartBlockData = getImageExtraBlockSize(&pImage->Header);
|
---|
339 | if (pImage->offStartBlockData != 0)
|
---|
340 | pImage->uShiftIndex2Offset += getPowerOfTwo(pImage->offStartBlockData);
|
---|
341 | }
|
---|
342 |
|
---|
343 | /**
|
---|
344 | * internal: create image.
|
---|
345 | */
|
---|
346 | static int vdiCreateImage(const char *pszFilename, VDIIMAGETYPE enmType, unsigned fFlags,
|
---|
347 | uint64_t cbSize, const char *pszComment, PVDIIMAGEDESC pParent,
|
---|
348 | PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
349 | {
|
---|
350 | /* Check args. */
|
---|
351 | Assert(pszFilename);
|
---|
352 | Assert(enmType >= VDI_IMAGE_TYPE_FIRST && enmType <= VDI_IMAGE_TYPE_LAST);
|
---|
353 | Assert(!(fFlags & ~VDI_IMAGE_FLAGS_MASK));
|
---|
354 | Assert(cbSize);
|
---|
355 |
|
---|
356 | /* Special check for comment length. */
|
---|
357 | if ( pszComment
|
---|
358 | && strlen(pszComment) >= VDI_IMAGE_COMMENT_SIZE)
|
---|
359 | {
|
---|
360 | Log(("vdiCreateImage: pszComment is too long, cb=%d\n", strlen(pszComment)));
|
---|
361 | return VERR_VDI_COMMENT_TOO_LONG;
|
---|
362 | }
|
---|
363 |
|
---|
364 | if ( enmType == VDI_IMAGE_TYPE_UNDO
|
---|
365 | || enmType == VDI_IMAGE_TYPE_DIFF)
|
---|
366 | {
|
---|
367 | Assert(pParent);
|
---|
368 | if (VDI_GET_VERSION_MAJOR(pParent->PreHeader.u32Version) != VDI_IMAGE_VERSION_MAJOR)
|
---|
369 | {
|
---|
370 | /* Invalid parent image version. */
|
---|
371 | Log(("vdiCreateImage: unsupported parent version=%08X\n", pParent->PreHeader.u32Version));
|
---|
372 | return VERR_VDI_UNSUPPORTED_VERSION;
|
---|
373 | }
|
---|
374 |
|
---|
375 | /* get image params from the parent image. */
|
---|
376 | fFlags = getImageFlags(&pParent->Header);
|
---|
377 | cbSize = getImageDiskSize(&pParent->Header);
|
---|
378 | }
|
---|
379 |
|
---|
380 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
|
---|
381 | if (!pImage)
|
---|
382 | return VERR_NO_MEMORY;
|
---|
383 | vdiInitImageDesc(pImage);
|
---|
384 |
|
---|
385 | vdiInitPreHeader(&pImage->PreHeader);
|
---|
386 | vdiInitHeader(&pImage->Header, enmType, fFlags, pszComment, cbSize, VDI_IMAGE_DEFAULT_BLOCK_SIZE, 0);
|
---|
387 |
|
---|
388 | if ( enmType == VDI_IMAGE_TYPE_UNDO
|
---|
389 | || enmType == VDI_IMAGE_TYPE_DIFF)
|
---|
390 | {
|
---|
391 | /* Set up linkage information. */
|
---|
392 | pImage->Header.u.v1.uuidLinkage = *getImageCreationUUID(&pParent->Header);
|
---|
393 | pImage->Header.u.v1.uuidParentModify = *getImageModificationUUID(&pParent->Header);
|
---|
394 | }
|
---|
395 |
|
---|
396 | pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
|
---|
397 | if (!pImage->paBlocks)
|
---|
398 | {
|
---|
399 | RTMemFree(pImage);
|
---|
400 | return VERR_NO_MEMORY;
|
---|
401 | }
|
---|
402 |
|
---|
403 | if (enmType != VDI_IMAGE_TYPE_FIXED)
|
---|
404 | {
|
---|
405 | /* for growing images mark all blocks in paBlocks as free. */
|
---|
406 | for (unsigned i = 0; i < pImage->Header.u.v1.cBlocks; i++)
|
---|
407 | pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
|
---|
408 | }
|
---|
409 | else
|
---|
410 | {
|
---|
411 | /* for fixed images mark all blocks in paBlocks as allocated */
|
---|
412 | for (unsigned i = 0; i < pImage->Header.u.v1.cBlocks; i++)
|
---|
413 | pImage->paBlocks[i] = i;
|
---|
414 | pImage->Header.u.v1.cBlocksAllocated = pImage->Header.u.v1.cBlocks;
|
---|
415 | }
|
---|
416 |
|
---|
417 | /* Setup image parameters. */
|
---|
418 | vdiSetupImageDesc(pImage);
|
---|
419 |
|
---|
420 | /* create file */
|
---|
421 | int rc = RTFileOpen(&pImage->File,
|
---|
422 | pszFilename,
|
---|
423 | RTFILE_O_READWRITE | RTFILE_O_CREATE | RTFILE_O_DENY_ALL | RTFILE_O_NOT_CONTENT_INDEXED);
|
---|
424 | if (RT_SUCCESS(rc))
|
---|
425 | {
|
---|
426 | /* Lock image exclusively to close any wrong access by VDI API calls. */
|
---|
427 | uint64_t cbLock = pImage->offStartData
|
---|
428 | + ((uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset);
|
---|
429 |
|
---|
430 | if (enmType == VDI_IMAGE_TYPE_FIXED)
|
---|
431 | {
|
---|
432 | /* check the free space on the disk and leave early if there is not
|
---|
433 | * sufficient space available */
|
---|
434 | RTFOFF cbFree = 0;
|
---|
435 | rc = RTFsQuerySizes(pszFilename, NULL, &cbFree, NULL, NULL);
|
---|
436 | if (RT_SUCCESS(rc) /* ignore errors */ && ((uint64_t)cbFree < cbLock))
|
---|
437 | {
|
---|
438 | rc = VERR_DISK_FULL;
|
---|
439 | cbLock = 0;
|
---|
440 | goto l_create_failed;
|
---|
441 | }
|
---|
442 | }
|
---|
443 | #ifndef RT_OS_WINDOWS
|
---|
444 | rc = RTFileLock(pImage->File,
|
---|
445 | RTFILE_LOCK_WRITE | RTFILE_LOCK_IMMEDIATELY, 0, cbLock);
|
---|
446 | if (RT_FAILURE(rc))
|
---|
447 | {
|
---|
448 | cbLock = 0; /* Not locked. */
|
---|
449 | goto l_create_failed;
|
---|
450 | }
|
---|
451 | #endif
|
---|
452 | if (enmType == VDI_IMAGE_TYPE_FIXED)
|
---|
453 | {
|
---|
454 | /*
|
---|
455 | * Allocate & commit whole file if fixed image, it must be more
|
---|
456 | * effective than expanding file by write operations.
|
---|
457 | */
|
---|
458 | rc = RTFileSetSize(pImage->File, cbLock);
|
---|
459 | }
|
---|
460 | else
|
---|
461 | {
|
---|
462 | /* Set file size to hold header and blocks array. */
|
---|
463 | rc = RTFileSetSize(pImage->File, pImage->offStartData);
|
---|
464 | }
|
---|
465 | if (RT_FAILURE(rc))
|
---|
466 | goto l_create_failed;
|
---|
467 |
|
---|
468 | /* Generate image last-modify uuid */
|
---|
469 | RTUuidCreate(getImageModificationUUID(&pImage->Header));
|
---|
470 |
|
---|
471 | /* Write pre-header. */
|
---|
472 | rc = RTFileWrite(pImage->File, &pImage->PreHeader, sizeof(pImage->PreHeader), NULL);
|
---|
473 | if (RT_FAILURE(rc))
|
---|
474 | goto l_create_failed;
|
---|
475 |
|
---|
476 | /* Write header. */
|
---|
477 | rc = RTFileWrite(pImage->File, &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL);
|
---|
478 | if (RT_FAILURE(rc))
|
---|
479 | goto l_create_failed;
|
---|
480 |
|
---|
481 | /* Write blocks array. */
|
---|
482 | rc = RTFileSeek(pImage->File, pImage->offStartBlocks, RTFILE_SEEK_BEGIN, NULL);
|
---|
483 | if (RT_FAILURE(rc))
|
---|
484 | goto l_create_failed;
|
---|
485 | rc = RTFileWrite(pImage->File,
|
---|
486 | pImage->paBlocks,
|
---|
487 | getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
488 | NULL);
|
---|
489 | if (RT_FAILURE(rc))
|
---|
490 | goto l_create_failed;
|
---|
491 |
|
---|
492 | if (enmType == VDI_IMAGE_TYPE_FIXED)
|
---|
493 | {
|
---|
494 | /* Fill image with zeroes. We do this for every fixed-size image since on some systems
|
---|
495 | * (for example Windows Vista), it takes ages to write a block near the end of a sparse
|
---|
496 | * file and the guest could complain about an ATA timeout. */
|
---|
497 |
|
---|
498 | /** @todo Starting with Linux 2.6.23, there is an fallocate() system call.
|
---|
499 | * Currently supported file systems are ext4 and ocfs2. */
|
---|
500 |
|
---|
501 | rc = RTFileSeek(pImage->File, pImage->offStartData, RTFILE_SEEK_BEGIN, NULL);
|
---|
502 | if (RT_FAILURE(rc))
|
---|
503 | goto l_create_failed;
|
---|
504 |
|
---|
505 | /* Allocate a temporary zero-filled buffer. Use a bigger block size to optimize writing */
|
---|
506 | const size_t cbBuf = 128 * _1K;
|
---|
507 | void *pvBuf = RTMemTmpAllocZ(cbBuf);
|
---|
508 | if (pvBuf)
|
---|
509 | {
|
---|
510 | uint64_t cbFill = (uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset;
|
---|
511 | uint64_t cbDisk = cbFill;
|
---|
512 |
|
---|
513 | /* do loop to fill all image. */
|
---|
514 | while (cbFill > 0)
|
---|
515 | {
|
---|
516 | unsigned to_fill = (unsigned)RT_MIN(cbFill, cbBuf);
|
---|
517 |
|
---|
518 | rc = RTFileWrite(pImage->File, pvBuf, to_fill, NULL);
|
---|
519 | if (RT_FAILURE(rc))
|
---|
520 | break;
|
---|
521 |
|
---|
522 | cbFill -= to_fill;
|
---|
523 |
|
---|
524 | if (pfnProgress)
|
---|
525 | {
|
---|
526 | rc = pfnProgress(NULL /* WARNING! pVM=NULL */,
|
---|
527 | (unsigned)(((cbDisk - cbFill) * 100) / cbDisk),
|
---|
528 | pvUser);
|
---|
529 | if (RT_FAILURE(rc))
|
---|
530 | break;
|
---|
531 | }
|
---|
532 | }
|
---|
533 | RTMemTmpFree(pvBuf);
|
---|
534 | }
|
---|
535 | else
|
---|
536 | {
|
---|
537 | /* alloc error */
|
---|
538 | rc = VERR_NO_MEMORY;
|
---|
539 | }
|
---|
540 | }
|
---|
541 |
|
---|
542 | l_create_failed:
|
---|
543 |
|
---|
544 | #ifndef RT_OS_WINDOWS
|
---|
545 | if (cbLock)
|
---|
546 | RTFileUnlock(pImage->File, 0, cbLock);
|
---|
547 | #endif
|
---|
548 | RTFileClose(pImage->File);
|
---|
549 |
|
---|
550 | /* Delete image file if error occured while creating */
|
---|
551 | if (RT_FAILURE(rc))
|
---|
552 | RTFileDelete(pszFilename);
|
---|
553 | }
|
---|
554 |
|
---|
555 | RTMemFree(pImage->paBlocks);
|
---|
556 | RTMemFree(pImage);
|
---|
557 |
|
---|
558 | if ( RT_SUCCESS(rc)
|
---|
559 | && pfnProgress)
|
---|
560 | pfnProgress(NULL /* WARNING! pVM=NULL */, 100, pvUser);
|
---|
561 |
|
---|
562 | Log(("vdiCreateImage: done, filename=\"%s\", rc=%Rrc\n", pszFilename, rc));
|
---|
563 |
|
---|
564 | return rc;
|
---|
565 | }
|
---|
566 |
|
---|
567 | /**
|
---|
568 | * Open an image.
|
---|
569 | * @internal
|
---|
570 | */
|
---|
571 | static int vdiOpenImage(PVDIIMAGEDESC *ppImage, const char *pszFilename,
|
---|
572 | unsigned fOpen, PVDIIMAGEDESC pParent)
|
---|
573 | {
|
---|
574 | /*
|
---|
575 | * Validate input.
|
---|
576 | */
|
---|
577 | Assert(ppImage);
|
---|
578 | Assert(pszFilename);
|
---|
579 | Assert(!(fOpen & ~VDI_OPEN_FLAGS_MASK));
|
---|
580 |
|
---|
581 | PVDIIMAGEDESC pImage;
|
---|
582 | size_t cchFilename = strlen(pszFilename);
|
---|
583 | if (cchFilename >= sizeof(pImage->szFilename))
|
---|
584 | {
|
---|
585 | AssertMsgFailed(("filename=\"%s\" is too long (%d bytes)!\n", pszFilename, cchFilename));
|
---|
586 | return VERR_FILENAME_TOO_LONG;
|
---|
587 | }
|
---|
588 |
|
---|
589 | pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
|
---|
590 | if (!pImage)
|
---|
591 | return VERR_NO_MEMORY;
|
---|
592 | vdiInitImageDesc(pImage);
|
---|
593 |
|
---|
594 | memcpy(pImage->szFilename, pszFilename, cchFilename);
|
---|
595 | pImage->fOpen = fOpen;
|
---|
596 |
|
---|
597 | /*
|
---|
598 | * Open the image.
|
---|
599 | */
|
---|
600 | int rc = RTFileOpen(&pImage->File,
|
---|
601 | pImage->szFilename,
|
---|
602 | fOpen & VDI_OPEN_FLAGS_READONLY
|
---|
603 | ? RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE
|
---|
604 | : RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
|
---|
605 | if (RT_FAILURE(rc))
|
---|
606 | {
|
---|
607 | if (!(fOpen & VDI_OPEN_FLAGS_READONLY))
|
---|
608 | {
|
---|
609 | /* Try to open image for reading only. */
|
---|
610 | rc = RTFileOpen(&pImage->File,
|
---|
611 | pImage->szFilename,
|
---|
612 | RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
613 | if (RT_SUCCESS(rc))
|
---|
614 | pImage->fOpen |= VDI_OPEN_FLAGS_READONLY;
|
---|
615 | }
|
---|
616 | if (RT_FAILURE(rc))
|
---|
617 | {
|
---|
618 | RTMemFree(pImage);
|
---|
619 | return rc;
|
---|
620 | }
|
---|
621 | }
|
---|
622 | /* Set up current image r/w state. */
|
---|
623 | pImage->fReadOnly = !!(pImage->fOpen & VDI_OPEN_FLAGS_READONLY);
|
---|
624 |
|
---|
625 | #ifndef RT_OS_WINDOWS
|
---|
626 | /*
|
---|
627 | * Set initial file lock for reading header only.
|
---|
628 | * Length of lock doesn't matter, it just must include image header.
|
---|
629 | */
|
---|
630 | uint64_t cbLock = _1M;
|
---|
631 | rc = RTFileLock(pImage->File, RTFILE_LOCK_READ | RTFILE_LOCK_IMMEDIATELY, 0, cbLock);
|
---|
632 | if (RT_FAILURE(rc))
|
---|
633 | {
|
---|
634 | cbLock = 0;
|
---|
635 | goto l_open_failed;
|
---|
636 | }
|
---|
637 | #endif
|
---|
638 |
|
---|
639 | /* Read pre-header. */
|
---|
640 | rc = RTFileRead(pImage->File, &pImage->PreHeader, sizeof(pImage->PreHeader), NULL);
|
---|
641 | if (RT_FAILURE(rc))
|
---|
642 | goto l_open_failed;
|
---|
643 | rc = vdiValidatePreHeader(&pImage->PreHeader);
|
---|
644 | if (RT_FAILURE(rc))
|
---|
645 | goto l_open_failed;
|
---|
646 |
|
---|
647 | /* Read header. */
|
---|
648 | pImage->Header.uVersion = pImage->PreHeader.u32Version;
|
---|
649 | switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
|
---|
650 | {
|
---|
651 | case 0:
|
---|
652 | rc = RTFileRead(pImage->File, &pImage->Header.u.v0, sizeof(pImage->Header.u.v0), NULL);
|
---|
653 | break;
|
---|
654 | case 1:
|
---|
655 | switch (GET_MINOR_HEADER_VERSION(&pImage->Header))
|
---|
656 | {
|
---|
657 | case 1:
|
---|
658 | rc = RTFileRead(pImage->File, &pImage->Header.u.v1, sizeof(pImage->Header.u.v1), NULL);
|
---|
659 | /* Convert VDI 1.1 images to VDI 1.1+ on open in read/write
|
---|
660 | * mode. Conversion is harmless, as any VirtualBox version
|
---|
661 | * supporting VDI 1.1 doesn't touch fields it doesn't know
|
---|
662 | * about. And it accepts bigger headers. */
|
---|
663 | if ( RT_SUCCESS(rc)
|
---|
664 | && !pImage->fReadOnly
|
---|
665 | && pImage->Header.u.v1.cbHeader < sizeof(pImage->Header.u.v1plus))
|
---|
666 | {
|
---|
667 | pImage->Header.u.v1plus.cbHeader = sizeof(pImage->Header.u.v1plus);
|
---|
668 | /* Mark LCHS geometry not-calculated. */
|
---|
669 | pImage->Header.u.v1plus.LCHSGeometry.cCylinders = 0;
|
---|
670 | pImage->Header.u.v1plus.LCHSGeometry.cHeads = 0;
|
---|
671 | pImage->Header.u.v1plus.LCHSGeometry.cSectors = 0;
|
---|
672 | pImage->Header.u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
673 | }
|
---|
674 | else if ( RT_SUCCESS(rc)
|
---|
675 | && pImage->Header.u.v1.cbHeader >= sizeof(pImage->Header.u.v1plus))
|
---|
676 | {
|
---|
677 | /* Read the actual VDI 1.1+ header completely. */
|
---|
678 | rc = RTFileReadAt(pImage->File, sizeof(pImage->PreHeader), &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL);
|
---|
679 | }
|
---|
680 | break;
|
---|
681 | default:
|
---|
682 | rc = VERR_VDI_UNSUPPORTED_VERSION;
|
---|
683 | break;
|
---|
684 | }
|
---|
685 | break;
|
---|
686 | default:
|
---|
687 | rc = VERR_VDI_UNSUPPORTED_VERSION;
|
---|
688 | break;
|
---|
689 | }
|
---|
690 | if (RT_FAILURE(rc))
|
---|
691 | goto l_open_failed;
|
---|
692 |
|
---|
693 | rc = vdiValidateHeader(&pImage->Header);
|
---|
694 | if (RT_FAILURE(rc))
|
---|
695 | goto l_open_failed;
|
---|
696 |
|
---|
697 | /* Check diff image correctness. */
|
---|
698 | if (pParent)
|
---|
699 | {
|
---|
700 | if (pImage->PreHeader.u32Version != pParent->PreHeader.u32Version)
|
---|
701 | {
|
---|
702 | rc = VERR_VDI_IMAGES_VERSION_MISMATCH;
|
---|
703 | goto l_open_failed;
|
---|
704 | }
|
---|
705 |
|
---|
706 | if ( getImageType(&pImage->Header) != VDI_IMAGE_TYPE_UNDO
|
---|
707 | && getImageType(&pImage->Header) != VDI_IMAGE_TYPE_DIFF)
|
---|
708 | {
|
---|
709 | rc = VERR_VDI_WRONG_DIFF_IMAGE;
|
---|
710 | goto l_open_failed;
|
---|
711 | }
|
---|
712 |
|
---|
713 | if ( getImageDiskSize(&pImage->Header) != getImageDiskSize(&pParent->Header)
|
---|
714 | || getImageBlockSize(&pImage->Header) != getImageBlockSize(&pParent->Header)
|
---|
715 | || getImageBlocks(&pImage->Header) != getImageBlocks(&pParent->Header)
|
---|
716 | || getImageExtraBlockSize(&pImage->Header) != getImageExtraBlockSize(&pParent->Header))
|
---|
717 | {
|
---|
718 | rc = VERR_VDI_WRONG_DIFF_IMAGE;
|
---|
719 | goto l_open_failed;
|
---|
720 | }
|
---|
721 |
|
---|
722 | /* Check linkage data. */
|
---|
723 | if ( RTUuidCompare(getImageParentUUID(&pImage->Header),
|
---|
724 | getImageCreationUUID(&pParent->Header))
|
---|
725 | || RTUuidCompare(getImageParentModificationUUID(&pImage->Header),
|
---|
726 | getImageModificationUUID(&pParent->Header)))
|
---|
727 | {
|
---|
728 | rc = VERR_VDI_IMAGES_UUID_MISMATCH;
|
---|
729 | goto l_open_failed;
|
---|
730 | }
|
---|
731 | }
|
---|
732 |
|
---|
733 | /* Setup image parameters by header. */
|
---|
734 | vdiSetupImageDesc(pImage);
|
---|
735 |
|
---|
736 | /* reset modified flag into first-modified state. */
|
---|
737 | pImage->fModified = VDI_IMAGE_MODIFIED_FIRST;
|
---|
738 |
|
---|
739 | #ifndef RT_OS_WINDOWS
|
---|
740 | /* Image is validated, set working file lock on it. */
|
---|
741 | rc = RTFileUnlock(pImage->File, 0, cbLock);
|
---|
742 | AssertRC(rc);
|
---|
743 | cbLock = pImage->offStartData
|
---|
744 | + ((uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset);
|
---|
745 | rc = RTFileLock(pImage->File,
|
---|
746 | (pImage->fReadOnly) ?
|
---|
747 | RTFILE_LOCK_READ | RTFILE_LOCK_IMMEDIATELY :
|
---|
748 | RTFILE_LOCK_WRITE | RTFILE_LOCK_IMMEDIATELY,
|
---|
749 | 0,
|
---|
750 | cbLock);
|
---|
751 | if ( RT_FAILURE(rc)
|
---|
752 | && !pImage->fReadOnly)
|
---|
753 | {
|
---|
754 | /* Failed to lock image for writing, try read-only lock. */
|
---|
755 | rc = RTFileLock(pImage->File,
|
---|
756 | RTFILE_LOCK_READ | RTFILE_LOCK_IMMEDIATELY, 0, cbLock);
|
---|
757 | if (RT_SUCCESS(rc))
|
---|
758 | pImage->fReadOnly = true;
|
---|
759 | }
|
---|
760 | if (RT_FAILURE(rc))
|
---|
761 | {
|
---|
762 | cbLock = 0; /* Not locked. */
|
---|
763 | goto l_open_failed;
|
---|
764 | }
|
---|
765 | #endif
|
---|
766 |
|
---|
767 | /* Allocate memory for blocks array. */
|
---|
768 | pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
|
---|
769 | if (!pImage->paBlocks)
|
---|
770 | {
|
---|
771 | rc = VERR_NO_MEMORY;
|
---|
772 | goto l_open_failed;
|
---|
773 | }
|
---|
774 |
|
---|
775 | /* Read blocks array. */
|
---|
776 | rc = RTFileSeek(pImage->File, pImage->offStartBlocks, RTFILE_SEEK_BEGIN, NULL);
|
---|
777 | if (RT_FAILURE(rc))
|
---|
778 | goto l_open_failed;
|
---|
779 | rc = RTFileRead(pImage->File, pImage->paBlocks,
|
---|
780 | getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER), NULL);
|
---|
781 | if (RT_FAILURE(rc))
|
---|
782 | goto l_open_failed;
|
---|
783 |
|
---|
784 | /* all done. */
|
---|
785 | *ppImage = pImage;
|
---|
786 | return VINF_SUCCESS;
|
---|
787 |
|
---|
788 | l_open_failed:
|
---|
789 | /* Clean up. */
|
---|
790 | if (pImage->paBlocks)
|
---|
791 | RTMemFree(pImage->paBlocks);
|
---|
792 | #ifndef RT_OS_WINDOWS
|
---|
793 | if (cbLock)
|
---|
794 | RTFileUnlock(pImage->File, 0, cbLock);
|
---|
795 | #endif
|
---|
796 | RTFileClose(pImage->File);
|
---|
797 | RTMemFree(pImage);
|
---|
798 | Log(("vdiOpenImage: failed, filename=\"%s\", rc=%Rrc\n", pszFilename, rc));
|
---|
799 | return rc;
|
---|
800 | }
|
---|
801 |
|
---|
802 | /**
|
---|
803 | * internal: save header to file.
|
---|
804 | */
|
---|
805 | static int vdiUpdateHeader(PVDIIMAGEDESC pImage)
|
---|
806 | {
|
---|
807 | /* Seek to header start. */
|
---|
808 | int rc = RTFileSeek(pImage->File, sizeof(VDIPREHEADER), RTFILE_SEEK_BEGIN, NULL);
|
---|
809 | if (RT_SUCCESS(rc))
|
---|
810 | {
|
---|
811 | switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
|
---|
812 | {
|
---|
813 | case 0:
|
---|
814 | rc = RTFileWrite(pImage->File, &pImage->Header.u.v0, sizeof(pImage->Header.u.v0), NULL);
|
---|
815 | break;
|
---|
816 | case 1:
|
---|
817 | switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
|
---|
818 | {
|
---|
819 | case 1:
|
---|
820 | if (pImage->Header.u.v1plus.cbHeader < sizeof(pImage->Header.u.v1plus))
|
---|
821 | rc = RTFileWrite(pImage->File, &pImage->Header.u.v1, sizeof(pImage->Header.u.v1), NULL);
|
---|
822 | else
|
---|
823 | rc = RTFileWrite(pImage->File, &pImage->Header.u.v1plus, sizeof(pImage->Header.u.v1plus), NULL);
|
---|
824 | break;
|
---|
825 | default:
|
---|
826 | rc = VERR_VDI_UNSUPPORTED_VERSION;
|
---|
827 | break;
|
---|
828 | }
|
---|
829 | break;
|
---|
830 | default:
|
---|
831 | rc = VERR_VDI_UNSUPPORTED_VERSION;
|
---|
832 | break;
|
---|
833 | }
|
---|
834 | }
|
---|
835 | AssertMsgRC(rc, ("vdiUpdateHeader failed, filename=\"%s\" rc=%Rrc\n", pImage->szFilename, rc));
|
---|
836 | return rc;
|
---|
837 | }
|
---|
838 |
|
---|
839 | /**
|
---|
840 | * internal: save block pointer to file, save header to file.
|
---|
841 | */
|
---|
842 | static int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock)
|
---|
843 | {
|
---|
844 | /* Update image header. */
|
---|
845 | int rc = vdiUpdateHeader(pImage);
|
---|
846 | if (RT_SUCCESS(rc))
|
---|
847 | {
|
---|
848 | /* write only one block pointer. */
|
---|
849 | rc = RTFileSeek(pImage->File,
|
---|
850 | pImage->offStartBlocks + uBlock * sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
851 | RTFILE_SEEK_BEGIN,
|
---|
852 | NULL);
|
---|
853 | if (RT_SUCCESS(rc))
|
---|
854 | rc = RTFileWrite(pImage->File,
|
---|
855 | &pImage->paBlocks[uBlock],
|
---|
856 | sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
857 | NULL);
|
---|
858 | AssertMsgRC(rc, ("vdiUpdateBlockInfo failed to update block=%u, filename=\"%s\", rc=%Rrc\n",
|
---|
859 | uBlock, pImage->szFilename, rc));
|
---|
860 | }
|
---|
861 | return rc;
|
---|
862 | }
|
---|
863 |
|
---|
864 | /**
|
---|
865 | * internal: save blocks array to file, save header to file.
|
---|
866 | */
|
---|
867 | static int vdiUpdateBlocks(PVDIIMAGEDESC pImage)
|
---|
868 | {
|
---|
869 | /* Update image header. */
|
---|
870 | int rc = vdiUpdateHeader(pImage);
|
---|
871 | if (RT_SUCCESS(rc))
|
---|
872 | {
|
---|
873 | /* write the block pointers array. */
|
---|
874 | rc = RTFileSeek(pImage->File, pImage->offStartBlocks, RTFILE_SEEK_BEGIN, NULL);
|
---|
875 | if (RT_SUCCESS(rc))
|
---|
876 | rc = RTFileWrite(pImage->File,
|
---|
877 | pImage->paBlocks,
|
---|
878 | sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header),
|
---|
879 | NULL);
|
---|
880 | AssertMsgRC(rc, ("vdiUpdateBlocks failed, filename=\"%s\", rc=%Rrc\n",
|
---|
881 | pImage->szFilename, rc));
|
---|
882 | }
|
---|
883 | return rc;
|
---|
884 | }
|
---|
885 |
|
---|
886 | /**
|
---|
887 | * internal: mark image as modified, if this is the first change - update image header
|
---|
888 | * on disk with a new uuidModify value.
|
---|
889 | */
|
---|
890 | static void vdiSetModifiedFlag(PVDIIMAGEDESC pImage)
|
---|
891 | {
|
---|
892 | pImage->fModified |= VDI_IMAGE_MODIFIED_FLAG;
|
---|
893 | if (pImage->fModified & VDI_IMAGE_MODIFIED_FIRST)
|
---|
894 | {
|
---|
895 | pImage->fModified &= ~VDI_IMAGE_MODIFIED_FIRST;
|
---|
896 |
|
---|
897 | /* first modify - generate uuidModify and save to file. */
|
---|
898 | vdiResetModifiedFlag(pImage);
|
---|
899 |
|
---|
900 | if (!(pImage->fModified | VDI_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
|
---|
901 | {
|
---|
902 | /* save header to file,
|
---|
903 | * note: no rc checking.
|
---|
904 | */
|
---|
905 | vdiUpdateHeader(pImage);
|
---|
906 | }
|
---|
907 | }
|
---|
908 | }
|
---|
909 |
|
---|
910 | /**
|
---|
911 | * internal: generate new uuidModify if the image was changed.
|
---|
912 | */
|
---|
913 | static void vdiResetModifiedFlag(PVDIIMAGEDESC pImage)
|
---|
914 | {
|
---|
915 | if (pImage->fModified & VDI_IMAGE_MODIFIED_FLAG)
|
---|
916 | {
|
---|
917 | /* generate new last-modified uuid */
|
---|
918 | if (!(pImage->fModified | VDI_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
|
---|
919 | RTUuidCreate(getImageModificationUUID(&pImage->Header));
|
---|
920 |
|
---|
921 | pImage->fModified &= ~VDI_IMAGE_MODIFIED_FLAG;
|
---|
922 | }
|
---|
923 | }
|
---|
924 |
|
---|
925 | /**
|
---|
926 | * internal: disables updates of the last-modified UUID
|
---|
927 | * when performing image writes.
|
---|
928 | */
|
---|
929 | static void vdiDisableLastModifiedUpdate(PVDIIMAGEDESC pImage)
|
---|
930 | {
|
---|
931 | pImage->fModified |= VDI_IMAGE_MODIFIED_DISABLE_UUID_UPDATE;
|
---|
932 | }
|
---|
933 |
|
---|
934 | #if 0 /* unused */
|
---|
935 | /**
|
---|
936 | * internal: enables updates of the last-modified UUID
|
---|
937 | * when performing image writes.
|
---|
938 | */
|
---|
939 | static void vdiEnableLastModifiedUpdate(PVDIIMAGEDESC pImage)
|
---|
940 | {
|
---|
941 | pImage->fModified &= ~VDI_IMAGE_MODIFIED_DISABLE_UUID_UPDATE;
|
---|
942 | }
|
---|
943 | #endif
|
---|
944 |
|
---|
945 | /**
|
---|
946 | * Flush the image file to disk.
|
---|
947 | */
|
---|
948 | void VDIFlushImage(PVDIIMAGEDESC pImage)
|
---|
949 | {
|
---|
950 | if (!pImage->fReadOnly)
|
---|
951 | {
|
---|
952 | /* Update last-modified uuid if need. */
|
---|
953 | vdiResetModifiedFlag(pImage);
|
---|
954 |
|
---|
955 | /* Save header. */
|
---|
956 | int rc = vdiUpdateHeader(pImage);
|
---|
957 | AssertMsgRC(rc, ("vdiUpdateHeader() failed, filename=\"%s\", rc=%Rrc\n",
|
---|
958 | pImage->szFilename, rc));
|
---|
959 | RTFileFlush(pImage->File);
|
---|
960 | }
|
---|
961 | }
|
---|
962 |
|
---|
963 | /**
|
---|
964 | * internal: close image file.
|
---|
965 | */
|
---|
966 | static void vdiCloseImage(PVDIIMAGEDESC pImage)
|
---|
967 | {
|
---|
968 | /* Params checking. */
|
---|
969 | Assert(pImage);
|
---|
970 | Assert(pImage->File != NIL_RTFILE);
|
---|
971 |
|
---|
972 | VDIFlushImage(pImage);
|
---|
973 | #ifndef RT_OS_WINDOWS
|
---|
974 | RTFileUnlock(pImage->File,
|
---|
975 | 0,
|
---|
976 | pImage->offStartData
|
---|
977 | + ((uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset));
|
---|
978 | #endif
|
---|
979 | RTFileClose(pImage->File);
|
---|
980 |
|
---|
981 | /* free image resources */
|
---|
982 | RTMemFree(pImage->paBlocks);
|
---|
983 | RTMemFree(pImage);
|
---|
984 | }
|
---|
985 |
|
---|
986 | /**
|
---|
987 | * internal: read data inside image block.
|
---|
988 | *
|
---|
989 | * note: uBlock must be valid, readed data must not overlap block bounds.
|
---|
990 | */
|
---|
991 | static int vdiReadInBlock(PVDIIMAGEDESC pImage, unsigned uBlock, unsigned offRead,
|
---|
992 | size_t cbToRead, void *pvBuf)
|
---|
993 | {
|
---|
994 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
|
---|
995 | {
|
---|
996 | /* block present in image file */
|
---|
997 | uint64_t u64Offset = ((uint64_t)pImage->paBlocks[uBlock] << pImage->uShiftIndex2Offset)
|
---|
998 | + (pImage->offStartData + pImage->offStartBlockData + offRead);
|
---|
999 | int rc = RTFileSeek(pImage->File, u64Offset, RTFILE_SEEK_BEGIN, NULL);
|
---|
1000 | if (RT_SUCCESS(rc))
|
---|
1001 | rc = RTFileRead(pImage->File, pvBuf, cbToRead, NULL);
|
---|
1002 | if (RT_FAILURE(rc))
|
---|
1003 | Log(("vdiReadInBlock: rc=%Rrc filename=\"%s\" uBlock=%u offRead=%u cbToRead=%u u64Offset=%llu\n",
|
---|
1004 | rc, pImage->szFilename, uBlock, offRead, cbToRead, u64Offset));
|
---|
1005 | return rc;
|
---|
1006 | }
|
---|
1007 |
|
---|
1008 | /* Returns zeroes for both free and zero block types. */
|
---|
1009 | memset(pvBuf, 0, cbToRead);
|
---|
1010 | return VINF_SUCCESS;
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 | /**
|
---|
1014 | * Read data from virtual HDD.
|
---|
1015 | *
|
---|
1016 | * @returns VBox status code.
|
---|
1017 | * @param pDisk Pointer to VDI HDD container.
|
---|
1018 | * @param offStart Offset of first reading byte from start of disk.
|
---|
1019 | * @param pvBuf Pointer to buffer for reading data.
|
---|
1020 | * @param cbToRead Number of bytes to read.
|
---|
1021 | */
|
---|
1022 | VBOXDDU_DECL(int) VDIDiskRead(PVDIDISK pDisk, uint64_t offStart, void *pvBuf, size_t cbToRead)
|
---|
1023 | {
|
---|
1024 | /* sanity check */
|
---|
1025 | Assert(pDisk);
|
---|
1026 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
1027 |
|
---|
1028 | PVDIIMAGEDESC pImage = pDisk->pLast;
|
---|
1029 | Assert(pImage);
|
---|
1030 |
|
---|
1031 | /* Check params. */
|
---|
1032 | if ( offStart + cbToRead > getImageDiskSize(&pImage->Header)
|
---|
1033 | || cbToRead == 0)
|
---|
1034 | {
|
---|
1035 | AssertMsgFailed(("offStart=%llu cbToRead=%u\n", offStart, cbToRead));
|
---|
1036 | return VERR_INVALID_PARAMETER;
|
---|
1037 | }
|
---|
1038 |
|
---|
1039 | /* Calculate starting block number and offset inside it. */
|
---|
1040 | unsigned uBlock = (unsigned)(offStart >> pImage->uShiftOffset2Index);
|
---|
1041 | unsigned offRead = (unsigned)offStart & pImage->uBlockMask;
|
---|
1042 |
|
---|
1043 | /* Save block size here for speed optimization. */
|
---|
1044 | unsigned cbBlock = getImageBlockSize(&pImage->Header);
|
---|
1045 |
|
---|
1046 | /* loop through blocks */
|
---|
1047 | int rc;
|
---|
1048 | for (;;)
|
---|
1049 | {
|
---|
1050 | size_t to_read;
|
---|
1051 | if ((offRead + cbToRead) <= cbBlock)
|
---|
1052 | to_read = cbToRead;
|
---|
1053 | else
|
---|
1054 | to_read = cbBlock - offRead;
|
---|
1055 |
|
---|
1056 | if (pDisk->cImages > 1)
|
---|
1057 | {
|
---|
1058 | /* Differencing images are used, handle them. */
|
---|
1059 | pImage = pDisk->pLast;
|
---|
1060 |
|
---|
1061 | /* Search for image with allocated block. */
|
---|
1062 | while (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE)
|
---|
1063 | {
|
---|
1064 | pImage = pImage->pPrev;
|
---|
1065 | if (!pImage)
|
---|
1066 | {
|
---|
1067 | /* Block is not allocated in all images of chain. */
|
---|
1068 | pImage = pDisk->pLast;
|
---|
1069 | break;
|
---|
1070 | }
|
---|
1071 | }
|
---|
1072 | }
|
---|
1073 |
|
---|
1074 | rc = vdiReadInBlock(pImage, uBlock, offRead, to_read, pvBuf);
|
---|
1075 |
|
---|
1076 | cbToRead -= to_read;
|
---|
1077 | if ( cbToRead == 0
|
---|
1078 | || RT_FAILURE(rc))
|
---|
1079 | break;
|
---|
1080 |
|
---|
1081 | /* goto next block */
|
---|
1082 | uBlock++;
|
---|
1083 | offRead = 0;
|
---|
1084 | pvBuf = (char *)pvBuf + to_read;
|
---|
1085 | }
|
---|
1086 |
|
---|
1087 | return rc;
|
---|
1088 | }
|
---|
1089 |
|
---|
1090 | /**
|
---|
1091 | * internal: fill the whole block with zeroes.
|
---|
1092 | *
|
---|
1093 | * note: block id must be valid, block must be already allocated in file.
|
---|
1094 | * note: if pDisk is NULL, the default buffer size is used
|
---|
1095 | */
|
---|
1096 | static int vdiFillBlockByZeroes(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock)
|
---|
1097 | {
|
---|
1098 | int rc;
|
---|
1099 |
|
---|
1100 | /* seek to start of block in file. */
|
---|
1101 | uint64_t u64Offset = ((uint64_t)pImage->paBlocks[uBlock] << pImage->uShiftIndex2Offset)
|
---|
1102 | + (pImage->offStartData + pImage->offStartBlockData);
|
---|
1103 | rc = RTFileSeek(pImage->File, u64Offset, RTFILE_SEEK_BEGIN, NULL);
|
---|
1104 | if (RT_FAILURE(rc))
|
---|
1105 | {
|
---|
1106 | Log(("vdiFillBlockByZeroes: seek rc=%Rrc filename=\"%s\" uBlock=%u u64Offset=%llu\n",
|
---|
1107 | rc, pImage->szFilename, uBlock, u64Offset));
|
---|
1108 | return rc;
|
---|
1109 | }
|
---|
1110 |
|
---|
1111 | /* alloc tmp zero-filled buffer */
|
---|
1112 | void *pvBuf = RTMemTmpAllocZ(pDisk ? pDisk->cbBuf : VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
1113 | if (!pvBuf)
|
---|
1114 | return VERR_NO_MEMORY;
|
---|
1115 |
|
---|
1116 | unsigned cbFill = getImageBlockSize(&pImage->Header);
|
---|
1117 |
|
---|
1118 | /* do loop, because buffer size may be less then block size */
|
---|
1119 | while (cbFill > 0)
|
---|
1120 | {
|
---|
1121 | unsigned to_fill = RT_MIN(cbFill, pDisk ? pDisk->cbBuf : VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
1122 | rc = RTFileWrite(pImage->File, pvBuf, to_fill, NULL);
|
---|
1123 | if (RT_FAILURE(rc))
|
---|
1124 | {
|
---|
1125 | Log(("vdiFillBlockByZeroes: write rc=%Rrc filename=\"%s\" uBlock=%u u64Offset=%llu cbFill=%u to_fill=%u\n",
|
---|
1126 | rc, pImage->szFilename, uBlock, u64Offset, cbFill, to_fill));
|
---|
1127 | break;
|
---|
1128 | }
|
---|
1129 |
|
---|
1130 | cbFill -= to_fill;
|
---|
1131 | }
|
---|
1132 |
|
---|
1133 | RTMemTmpFree(pvBuf);
|
---|
1134 | return rc;
|
---|
1135 | }
|
---|
1136 |
|
---|
1137 | /**
|
---|
1138 | * internal: write data inside image block.
|
---|
1139 | *
|
---|
1140 | * note: uBlock must be valid, written data must not overlap block bounds.
|
---|
1141 | */
|
---|
1142 | static int vdiWriteInBlock(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock, unsigned offWrite, size_t cbToWrite, const void *pvBuf)
|
---|
1143 | {
|
---|
1144 | int rc;
|
---|
1145 |
|
---|
1146 | /* Check if we can write into file. */
|
---|
1147 | if (pImage->fReadOnly)
|
---|
1148 | {
|
---|
1149 | Log(("vdiWriteInBlock: failed, image \"%s\" is read-only!\n", pImage->szFilename));
|
---|
1150 | return VERR_WRITE_PROTECT;
|
---|
1151 | }
|
---|
1152 |
|
---|
1153 | /* This could be optimized a little (not setting it when writing zeroes
|
---|
1154 | * to a zeroed block). Won't buy us much, because it's very unlikely
|
---|
1155 | * that only such zero data block writes occur while the VDI is opened. */
|
---|
1156 | vdiSetModifiedFlag(pImage);
|
---|
1157 |
|
---|
1158 | if (!IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
|
---|
1159 | {
|
---|
1160 | if (!pDisk || !pDisk->fHonorZeroWrites)
|
---|
1161 | {
|
---|
1162 | /* If the destination block is unallocated at this point, it's either
|
---|
1163 | * a zero block or a block which hasn't been used so far (which also
|
---|
1164 | * means that it's a zero block. Don't need to write anything to this
|
---|
1165 | * block if the data consists of just zeroes. */
|
---|
1166 | Assert(cbToWrite % 4 == 0);
|
---|
1167 | if (ASMBitFirstSet((volatile void *)pvBuf, (uint32_t)cbToWrite * 8) == -1)
|
---|
1168 | {
|
---|
1169 | pImage->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
|
---|
1170 | return VINF_SUCCESS;
|
---|
1171 | }
|
---|
1172 | }
|
---|
1173 |
|
---|
1174 | /* need to allocate a new block in image file */
|
---|
1175 |
|
---|
1176 | /* expand file by one block */
|
---|
1177 | uint64_t u64Size = (((uint64_t)(getImageBlocksAllocated(&pImage->Header) + 1)) << pImage->uShiftIndex2Offset)
|
---|
1178 | + pImage->offStartData;
|
---|
1179 | rc = RTFileSetSize(pImage->File, u64Size);
|
---|
1180 | if (RT_FAILURE(rc))
|
---|
1181 | {
|
---|
1182 | Log(("vdiWriteInBlock: set size rc=%Rrc filename=\"%s\" uBlock=%u u64Size=%llu\n",
|
---|
1183 | rc, pImage->szFilename, uBlock, u64Size));
|
---|
1184 | return rc;
|
---|
1185 | }
|
---|
1186 |
|
---|
1187 | unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
|
---|
1188 | pImage->paBlocks[uBlock] = cBlocksAllocated;
|
---|
1189 | setImageBlocksAllocated(&pImage->Header, cBlocksAllocated + 1);
|
---|
1190 |
|
---|
1191 | if ( pImage->fFlags & VDI_IMAGE_FLAGS_ZERO_EXPAND
|
---|
1192 | || pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
|
---|
1193 | {
|
---|
1194 | /* Fill newly allocated block by zeroes. */
|
---|
1195 |
|
---|
1196 | if (offWrite || cbToWrite != getImageBlockSize(&pImage->Header))
|
---|
1197 | {
|
---|
1198 | rc = vdiFillBlockByZeroes(pDisk, pImage, uBlock);
|
---|
1199 | if (RT_FAILURE(rc))
|
---|
1200 | return rc;
|
---|
1201 | }
|
---|
1202 | }
|
---|
1203 |
|
---|
1204 | rc = vdiUpdateBlockInfo(pImage, uBlock);
|
---|
1205 | if (RT_FAILURE(rc))
|
---|
1206 | return rc;
|
---|
1207 | }
|
---|
1208 |
|
---|
1209 | /* Now block present in image file, write data inside it. */
|
---|
1210 | uint64_t u64Offset = ((uint64_t)pImage->paBlocks[uBlock] << pImage->uShiftIndex2Offset)
|
---|
1211 | + (pImage->offStartData + pImage->offStartBlockData + offWrite);
|
---|
1212 | rc = RTFileSeek(pImage->File, u64Offset, RTFILE_SEEK_BEGIN, NULL);
|
---|
1213 | if (RT_SUCCESS(rc))
|
---|
1214 | {
|
---|
1215 | rc = RTFileWrite(pImage->File, pvBuf, cbToWrite, NULL);
|
---|
1216 | if (RT_FAILURE(rc))
|
---|
1217 | Log(("vdiWriteInBlock: write rc=%Rrc filename=\"%s\" uBlock=%u offWrite=%u u64Offset=%llu cbToWrite=%u\n",
|
---|
1218 | rc, pImage->szFilename, uBlock, offWrite, u64Offset, cbToWrite));
|
---|
1219 | }
|
---|
1220 | else
|
---|
1221 | Log(("vdiWriteInBlock: seek rc=%Rrc filename=\"%s\" uBlock=%u offWrite=%u u64Offset=%llu\n",
|
---|
1222 | rc, pImage->szFilename, uBlock, offWrite, u64Offset));
|
---|
1223 |
|
---|
1224 | return rc;
|
---|
1225 | }
|
---|
1226 |
|
---|
1227 | /**
|
---|
1228 | * internal: copy data block from one (parent) image to last image.
|
---|
1229 | */
|
---|
1230 | static int vdiCopyBlock(PVDIDISK pDisk, PVDIIMAGEDESC pImage, unsigned uBlock)
|
---|
1231 | {
|
---|
1232 | Assert(pImage != pDisk->pLast);
|
---|
1233 |
|
---|
1234 | if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
|
---|
1235 | {
|
---|
1236 | /*
|
---|
1237 | * if src block is zero, set dst block to zero too.
|
---|
1238 | */
|
---|
1239 | pDisk->pLast->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
|
---|
1240 | return VINF_SUCCESS;
|
---|
1241 | }
|
---|
1242 |
|
---|
1243 | /* alloc tmp buffer */
|
---|
1244 | void *pvBuf = RTMemTmpAlloc(pDisk->cbBuf);
|
---|
1245 | if (!pvBuf)
|
---|
1246 | return VERR_NO_MEMORY;
|
---|
1247 |
|
---|
1248 | int rc = VINF_SUCCESS;
|
---|
1249 |
|
---|
1250 | unsigned cbCopy = getImageBlockSize(&pImage->Header);
|
---|
1251 | unsigned offCopy = 0;
|
---|
1252 |
|
---|
1253 | /* do loop, because buffer size may be less then block size */
|
---|
1254 | while (cbCopy > 0)
|
---|
1255 | {
|
---|
1256 | unsigned to_copy = RT_MIN(cbCopy, pDisk->cbBuf);
|
---|
1257 | rc = vdiReadInBlock(pImage, uBlock, offCopy, to_copy, pvBuf);
|
---|
1258 | if (RT_FAILURE(rc))
|
---|
1259 | break;
|
---|
1260 |
|
---|
1261 | rc = vdiWriteInBlock(pDisk, pDisk->pLast, uBlock, offCopy, to_copy, pvBuf);
|
---|
1262 | if (RT_FAILURE(rc))
|
---|
1263 | break;
|
---|
1264 |
|
---|
1265 | cbCopy -= to_copy;
|
---|
1266 | offCopy += to_copy;
|
---|
1267 | }
|
---|
1268 |
|
---|
1269 | RTMemTmpFree(pvBuf);
|
---|
1270 | return rc;
|
---|
1271 | }
|
---|
1272 |
|
---|
1273 | /**
|
---|
1274 | * Write data to virtual HDD.
|
---|
1275 | *
|
---|
1276 | * @returns VBox status code.
|
---|
1277 | * @param pDisk Pointer to VDI HDD container.
|
---|
1278 | * @param offStart Offset of first writing byte from start of HDD.
|
---|
1279 | * @param pvBuf Pointer to buffer of writing data.
|
---|
1280 | * @param cbToWrite Number of bytes to write.
|
---|
1281 | */
|
---|
1282 | VBOXDDU_DECL(int) VDIDiskWrite(PVDIDISK pDisk, uint64_t offStart, const void *pvBuf, size_t cbToWrite)
|
---|
1283 | {
|
---|
1284 | /* sanity check */
|
---|
1285 | Assert(pDisk);
|
---|
1286 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
1287 |
|
---|
1288 | PVDIIMAGEDESC pImage = pDisk->pLast;
|
---|
1289 | Assert(pImage);
|
---|
1290 |
|
---|
1291 | /* Check params. */
|
---|
1292 | if ( offStart + cbToWrite > getImageDiskSize(&pImage->Header)
|
---|
1293 | || cbToWrite == 0)
|
---|
1294 | {
|
---|
1295 | AssertMsgFailed(("offStart=%llu cbToWrite=%u\n", offStart, cbToWrite));
|
---|
1296 | return VERR_INVALID_PARAMETER;
|
---|
1297 | }
|
---|
1298 |
|
---|
1299 | /* Calculate starting block number and offset inside it. */
|
---|
1300 | unsigned uBlock = (unsigned)(offStart >> pImage->uShiftOffset2Index);
|
---|
1301 | unsigned offWrite = (unsigned)offStart & pImage->uBlockMask;
|
---|
1302 | unsigned cbBlock = getImageBlockSize(&pImage->Header);
|
---|
1303 |
|
---|
1304 | /* loop through blocks */
|
---|
1305 | int rc;
|
---|
1306 | for (;;)
|
---|
1307 | {
|
---|
1308 | size_t to_write;
|
---|
1309 | if (offWrite + cbToWrite <= cbBlock)
|
---|
1310 | to_write = cbToWrite;
|
---|
1311 | else
|
---|
1312 | to_write = cbBlock - offWrite;
|
---|
1313 |
|
---|
1314 | /* All callers write less than a VDI block right now (assuming
|
---|
1315 | * default VDI block size). So not worth optimizing for the case
|
---|
1316 | * where a full block is overwritten (no copying required).
|
---|
1317 | * Checking whether a block is all zeroes after the write is too
|
---|
1318 | * expensive (would require reading the rest of the block). */
|
---|
1319 |
|
---|
1320 | if (pDisk->cImages > 1)
|
---|
1321 | {
|
---|
1322 | /* Differencing images are used, handle them. */
|
---|
1323 |
|
---|
1324 | /* Search for image with allocated block. */
|
---|
1325 | while (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE)
|
---|
1326 | {
|
---|
1327 | pImage = pImage->pPrev;
|
---|
1328 | if (!pImage)
|
---|
1329 | {
|
---|
1330 | /* Block is not allocated in all images of chain. */
|
---|
1331 | pImage = pDisk->pLast;
|
---|
1332 | break;
|
---|
1333 | }
|
---|
1334 | }
|
---|
1335 |
|
---|
1336 | if (pImage != pDisk->pLast)
|
---|
1337 | {
|
---|
1338 | /* One of parent image has a block data, copy it into last image. */
|
---|
1339 | rc = vdiCopyBlock(pDisk, pImage, uBlock);
|
---|
1340 | if (RT_FAILURE(rc))
|
---|
1341 | break;
|
---|
1342 | pImage = pDisk->pLast;
|
---|
1343 | }
|
---|
1344 | }
|
---|
1345 |
|
---|
1346 | /* Actually write the data into block. */
|
---|
1347 | rc = vdiWriteInBlock(pDisk, pImage, uBlock, offWrite, to_write, pvBuf);
|
---|
1348 |
|
---|
1349 | cbToWrite -= to_write;
|
---|
1350 | if ( cbToWrite == 0
|
---|
1351 | || RT_FAILURE(rc))
|
---|
1352 | break;
|
---|
1353 |
|
---|
1354 | /* goto next block */
|
---|
1355 | uBlock++;
|
---|
1356 | offWrite = 0;
|
---|
1357 | pvBuf = (char *)pvBuf + to_write;
|
---|
1358 | }
|
---|
1359 |
|
---|
1360 | return rc;
|
---|
1361 | }
|
---|
1362 |
|
---|
1363 | /**
|
---|
1364 | * internal: commit one image to another, no changes to header, just
|
---|
1365 | * plain copy operation. Blocks that are not allocated in the source
|
---|
1366 | * image (i.e. inherited by its parent(s)) are not merged.
|
---|
1367 | *
|
---|
1368 | * @param pImageFrom source image
|
---|
1369 | * @param pImageTo target image (will receive all the modifications)
|
---|
1370 | * @param fParentToChild true if the source image is parent of the target one,
|
---|
1371 | * false of the target image is the parent of the source.
|
---|
1372 | * @param pfnProgress progress callback (NULL if not to be used)
|
---|
1373 | * @param pvUser user argument for the progress callback
|
---|
1374 | *
|
---|
1375 | * @note the target image has to be opened read/write
|
---|
1376 | * @note this method does not check whether merging is possible!
|
---|
1377 | */
|
---|
1378 | static int vdiMergeImages(PVDIIMAGEDESC pImageFrom, PVDIIMAGEDESC pImageTo, bool fParentToChild,
|
---|
1379 | PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
1380 | {
|
---|
1381 | Assert(pImageFrom);
|
---|
1382 | Assert(pImageTo);
|
---|
1383 |
|
---|
1384 | Log(("vdiMergeImages: merging from image \"%s\" to image \"%s\" (fParentToChild=%d)\n",
|
---|
1385 | pImageFrom->szFilename, pImageTo->szFilename, fParentToChild));
|
---|
1386 |
|
---|
1387 | /* alloc tmp buffer */
|
---|
1388 | void *pvBuf = RTMemTmpAlloc(VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
1389 | if (!pvBuf)
|
---|
1390 | return VERR_NO_MEMORY;
|
---|
1391 |
|
---|
1392 | int rc = VINF_SUCCESS;
|
---|
1393 |
|
---|
1394 | if (!fParentToChild)
|
---|
1395 | {
|
---|
1396 | /*
|
---|
1397 | * Commit the child image to the parent image.
|
---|
1398 | * Child is the source (from), parent is the target (to).
|
---|
1399 | */
|
---|
1400 |
|
---|
1401 | unsigned cBlocks = getImageBlocks(&pImageFrom->Header);
|
---|
1402 |
|
---|
1403 | for (unsigned uBlock = 0; uBlock < cBlocks; uBlock++)
|
---|
1404 | {
|
---|
1405 | /* only process blocks that are allocated in the source image */
|
---|
1406 | if (pImageFrom->paBlocks[uBlock] != VDI_IMAGE_BLOCK_FREE)
|
---|
1407 | {
|
---|
1408 | /* Found used block in source image, commit it. */
|
---|
1409 | if ( pImageFrom->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO
|
---|
1410 | && !IS_VDI_IMAGE_BLOCK_ALLOCATED(pImageTo->paBlocks[uBlock]))
|
---|
1411 | {
|
---|
1412 | /* Block is zero in the source image and not allocated in the target image. */
|
---|
1413 | pImageTo->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
|
---|
1414 | vdiSetModifiedFlag(pImageTo);
|
---|
1415 | }
|
---|
1416 | else
|
---|
1417 | {
|
---|
1418 | /* Block is not zero / allocated in source image. */
|
---|
1419 | unsigned cbCommit = getImageBlockSize(&pImageFrom->Header);
|
---|
1420 | unsigned offCommit = 0;
|
---|
1421 |
|
---|
1422 | /* do loop, because buffer size may be less then block size */
|
---|
1423 | while (cbCommit > 0)
|
---|
1424 | {
|
---|
1425 | unsigned cbToCopy = RT_MIN(cbCommit, VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
1426 |
|
---|
1427 | rc = vdiReadInBlock(pImageFrom, uBlock, offCommit, cbToCopy, pvBuf);
|
---|
1428 | if (RT_FAILURE(rc))
|
---|
1429 | break;
|
---|
1430 |
|
---|
1431 | rc = vdiWriteInBlock(NULL, pImageTo, uBlock, offCommit, cbToCopy, pvBuf);
|
---|
1432 | if (RT_FAILURE(rc))
|
---|
1433 | break;
|
---|
1434 |
|
---|
1435 | cbCommit -= cbToCopy;
|
---|
1436 | offCommit += cbToCopy;
|
---|
1437 | }
|
---|
1438 | if (RT_FAILURE(rc))
|
---|
1439 | break;
|
---|
1440 | }
|
---|
1441 | }
|
---|
1442 |
|
---|
1443 | if (pfnProgress)
|
---|
1444 | {
|
---|
1445 | pfnProgress(NULL /* WARNING! pVM=NULL */,
|
---|
1446 | (uBlock * 100) / cBlocks,
|
---|
1447 | pvUser);
|
---|
1448 | /* Note: commiting is non breakable operation, skipping rc here. */
|
---|
1449 | }
|
---|
1450 | }
|
---|
1451 | }
|
---|
1452 | else
|
---|
1453 | {
|
---|
1454 | /*
|
---|
1455 | * Commit the parent image to the child image.
|
---|
1456 | * Parent is the source (from), child is the target (to).
|
---|
1457 | */
|
---|
1458 |
|
---|
1459 | unsigned cBlocks = getImageBlocks(&pImageFrom->Header);
|
---|
1460 |
|
---|
1461 | for (unsigned uBlock = 0; uBlock < cBlocks; uBlock++)
|
---|
1462 | {
|
---|
1463 | /*
|
---|
1464 | * only process blocks that are allocated or zero in the source image
|
---|
1465 | * and NEITHER allocated NOR zero in the target image
|
---|
1466 | */
|
---|
1467 | if (pImageFrom->paBlocks[uBlock] != VDI_IMAGE_BLOCK_FREE &&
|
---|
1468 | pImageTo->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE)
|
---|
1469 | {
|
---|
1470 | /* Found used block in source image (but unused in target), commit it. */
|
---|
1471 | if ( pImageFrom->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
|
---|
1472 | {
|
---|
1473 | /* Block is zero in the source image and not allocated in the target image. */
|
---|
1474 | pImageTo->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
|
---|
1475 | vdiSetModifiedFlag(pImageTo);
|
---|
1476 | }
|
---|
1477 | else
|
---|
1478 | {
|
---|
1479 | /* Block is not zero / allocated in source image. */
|
---|
1480 | unsigned cbCommit = getImageBlockSize(&pImageFrom->Header);
|
---|
1481 | unsigned offCommit = 0;
|
---|
1482 |
|
---|
1483 | /* do loop, because buffer size may be less then block size */
|
---|
1484 | while (cbCommit > 0)
|
---|
1485 | {
|
---|
1486 | unsigned cbToCopy = RT_MIN(cbCommit, VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
1487 |
|
---|
1488 | rc = vdiReadInBlock(pImageFrom, uBlock, offCommit, cbToCopy, pvBuf);
|
---|
1489 | if (RT_FAILURE(rc))
|
---|
1490 | break;
|
---|
1491 |
|
---|
1492 | rc = vdiWriteInBlock(NULL, pImageTo, uBlock, offCommit, cbToCopy, pvBuf);
|
---|
1493 | if (RT_FAILURE(rc))
|
---|
1494 | break;
|
---|
1495 |
|
---|
1496 | cbCommit -= cbToCopy;
|
---|
1497 | offCommit += cbToCopy;
|
---|
1498 | }
|
---|
1499 | if (RT_FAILURE(rc))
|
---|
1500 | break;
|
---|
1501 | }
|
---|
1502 | }
|
---|
1503 |
|
---|
1504 | if (pfnProgress)
|
---|
1505 | {
|
---|
1506 | pfnProgress(NULL /* WARNING! pVM=NULL */,
|
---|
1507 | (uBlock * 100) / cBlocks,
|
---|
1508 | pvUser);
|
---|
1509 | /* Note: commiting is non breakable operation, skipping rc here. */
|
---|
1510 | }
|
---|
1511 | }
|
---|
1512 | }
|
---|
1513 |
|
---|
1514 | RTMemTmpFree(pvBuf);
|
---|
1515 | return rc;
|
---|
1516 | }
|
---|
1517 |
|
---|
1518 | /**
|
---|
1519 | * internal: commit last image(s) to selected previous image.
|
---|
1520 | * note: all images accessed across this call must be opened in R/W mode.
|
---|
1521 | * @remark Only used by tstVDI.
|
---|
1522 | */
|
---|
1523 | static int vdiCommitToImage(PVDIDISK pDisk, PVDIIMAGEDESC pDstImage,
|
---|
1524 | PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
1525 | {
|
---|
1526 | /* sanity check */
|
---|
1527 | Assert(pDisk);
|
---|
1528 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
1529 | Assert(pDstImage);
|
---|
1530 |
|
---|
1531 | PVDIIMAGEDESC pImage = pDisk->pLast;
|
---|
1532 | Assert(pImage);
|
---|
1533 | Log(("vdiCommitToImage: commiting from image \"%s\" to image \"%s\"\n",
|
---|
1534 | pImage->szFilename, pDstImage->szFilename));
|
---|
1535 | if (pDstImage == pImage)
|
---|
1536 | {
|
---|
1537 | Log(("vdiCommitToImage: attempt to commit to the same image!\n"));
|
---|
1538 | return VERR_VDI_NO_DIFF_IMAGES;
|
---|
1539 | }
|
---|
1540 |
|
---|
1541 | /* Scan images for pDstImage. */
|
---|
1542 | while (pImage && pImage != pDstImage)
|
---|
1543 | pImage = pImage->pPrev;
|
---|
1544 | if (!pImage)
|
---|
1545 | {
|
---|
1546 | AssertMsgFailed(("Invalid arguments: pDstImage is not in images chain\n"));
|
---|
1547 | return VERR_INVALID_PARAMETER;
|
---|
1548 | }
|
---|
1549 | pImage = pDisk->pLast;
|
---|
1550 |
|
---|
1551 | /* alloc tmp buffer */
|
---|
1552 | void *pvBuf = RTMemTmpAlloc(pDisk->cbBuf);
|
---|
1553 | if (!pvBuf)
|
---|
1554 | return VERR_NO_MEMORY;
|
---|
1555 |
|
---|
1556 | int rc = VINF_SUCCESS;
|
---|
1557 | unsigned cBlocks = getImageBlocks(&pImage->Header);
|
---|
1558 |
|
---|
1559 | for (unsigned uBlock = 0; uBlock < cBlocks; uBlock++)
|
---|
1560 | {
|
---|
1561 | pImage = pDisk->pLast;
|
---|
1562 |
|
---|
1563 | /* Find allocated block to commit. */
|
---|
1564 | while ( pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE
|
---|
1565 | && pImage != pDstImage)
|
---|
1566 | pImage = pImage->pPrev;
|
---|
1567 |
|
---|
1568 | if (pImage != pDstImage)
|
---|
1569 | {
|
---|
1570 | /* Found used block in diff image (pImage), commit it. */
|
---|
1571 | if ( pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO
|
---|
1572 | && !IS_VDI_IMAGE_BLOCK_ALLOCATED(pDstImage->paBlocks[uBlock]))
|
---|
1573 | {
|
---|
1574 | /* Block is zero in difference image and not allocated in primary image. */
|
---|
1575 | pDstImage->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
|
---|
1576 | vdiSetModifiedFlag(pDstImage);
|
---|
1577 | }
|
---|
1578 | else
|
---|
1579 | {
|
---|
1580 | /* Block is not zero / allocated in primary image. */
|
---|
1581 | unsigned cbCommit = getImageBlockSize(&pImage->Header);
|
---|
1582 | unsigned offCommit = 0;
|
---|
1583 |
|
---|
1584 | /* do loop, because buffer size may be less then block size */
|
---|
1585 | while (cbCommit > 0)
|
---|
1586 | {
|
---|
1587 | unsigned cbToCopy = RT_MIN(cbCommit, pDisk->cbBuf);
|
---|
1588 |
|
---|
1589 | rc = vdiReadInBlock(pImage, uBlock, offCommit, cbToCopy, pvBuf);
|
---|
1590 | if (RT_FAILURE(rc))
|
---|
1591 | break;
|
---|
1592 |
|
---|
1593 | rc = vdiWriteInBlock(pDisk, pDstImage, uBlock, offCommit, cbToCopy, pvBuf);
|
---|
1594 | if (RT_FAILURE(rc))
|
---|
1595 | break;
|
---|
1596 |
|
---|
1597 | cbCommit -= cbToCopy;
|
---|
1598 | offCommit += cbToCopy;
|
---|
1599 | }
|
---|
1600 | if (RT_FAILURE(rc))
|
---|
1601 | break;
|
---|
1602 | }
|
---|
1603 | pImage->paBlocks[uBlock] = VDI_IMAGE_BLOCK_FREE;
|
---|
1604 | }
|
---|
1605 |
|
---|
1606 | if (pfnProgress)
|
---|
1607 | {
|
---|
1608 | pfnProgress(NULL /* WARNING! pVM=NULL */,
|
---|
1609 | (uBlock * 100) / cBlocks,
|
---|
1610 | pvUser);
|
---|
1611 | /* Note: commiting is non breakable operation, skipping rc here. */
|
---|
1612 | }
|
---|
1613 | }
|
---|
1614 |
|
---|
1615 | RTMemTmpFree(pvBuf);
|
---|
1616 |
|
---|
1617 | /* Go forward and update linkage information. */
|
---|
1618 | for (pImage = pDstImage; pImage; pImage = pImage->pNext)
|
---|
1619 | {
|
---|
1620 | /* generate new last-modified uuid. */
|
---|
1621 | RTUuidCreate(getImageModificationUUID(&pImage->Header));
|
---|
1622 |
|
---|
1623 | /* fix up linkage. */
|
---|
1624 | if (pImage != pDstImage)
|
---|
1625 | *getImageParentModificationUUID(&pImage->Header) = *getImageModificationUUID(&pImage->pPrev->Header);
|
---|
1626 |
|
---|
1627 | /* reset modified flag. */
|
---|
1628 | pImage->fModified = 0;
|
---|
1629 | }
|
---|
1630 |
|
---|
1631 | /* Process committed images - truncate them. */
|
---|
1632 | for (pImage = pDisk->pLast; pImage != pDstImage; pImage = pImage->pPrev)
|
---|
1633 | {
|
---|
1634 | /* note: can't understand how to do error works here? */
|
---|
1635 |
|
---|
1636 | setImageBlocksAllocated(&pImage->Header, 0);
|
---|
1637 |
|
---|
1638 | /* Truncate file. */
|
---|
1639 | int rc2 = RTFileSetSize(pImage->File, pImage->offStartData);
|
---|
1640 | if (RT_FAILURE(rc2))
|
---|
1641 | {
|
---|
1642 | rc = rc2;
|
---|
1643 | Log(("vdiCommitToImage: set size (truncate) rc=%Rrc filename=\"%s\"\n",
|
---|
1644 | rc, pImage->szFilename));
|
---|
1645 | }
|
---|
1646 |
|
---|
1647 | /* Save header and blocks array. */
|
---|
1648 | rc2 = vdiUpdateBlocks(pImage);
|
---|
1649 | if (RT_FAILURE(rc2))
|
---|
1650 | {
|
---|
1651 | rc = rc2;
|
---|
1652 | Log(("vdiCommitToImage: update blocks and header rc=%Rrc filename=\"%s\"\n",
|
---|
1653 | rc, pImage->szFilename));
|
---|
1654 | }
|
---|
1655 | }
|
---|
1656 |
|
---|
1657 | if (pfnProgress)
|
---|
1658 | {
|
---|
1659 | pfnProgress(NULL /* WARNING! pVM=NULL */, 100, pvUser);
|
---|
1660 | /* Note: commiting is non breakable operation, skipping rc here. */
|
---|
1661 | }
|
---|
1662 |
|
---|
1663 | Log(("vdiCommitToImage: done, rc=%Rrc\n", rc));
|
---|
1664 |
|
---|
1665 | return rc;
|
---|
1666 | }
|
---|
1667 |
|
---|
1668 | /**
|
---|
1669 | * Checks if image is available and not broken, returns some useful image parameters if requested.
|
---|
1670 | *
|
---|
1671 | * @returns VBox status code.
|
---|
1672 | * @param pszFilename Name of the image file to check.
|
---|
1673 | * @param puVersion Where to store the version of image. NULL is ok.
|
---|
1674 | * @param penmType Where to store the type of image. NULL is ok.
|
---|
1675 | * @param pcbSize Where to store the size of image in bytes. NULL is ok.
|
---|
1676 | * @param pUuid Where to store the uuid of image creation. NULL is ok.
|
---|
1677 | * @param pParentUuid Where to store the UUID of the parent image. NULL is ok.
|
---|
1678 | * @param pszComment Where to store the comment string of image. NULL is ok.
|
---|
1679 | * @param cbComment The size of pszComment buffer. 0 is ok.
|
---|
1680 | */
|
---|
1681 | VBOXDDU_DECL(int) VDICheckImage(const char *pszFilename, unsigned *puVersion, PVDIIMAGETYPE penmType,
|
---|
1682 | uint64_t *pcbSize, PRTUUID pUuid, PRTUUID pParentUuid,
|
---|
1683 | char *pszComment, unsigned cbComment)
|
---|
1684 | {
|
---|
1685 | LogFlow(("VDICheckImage:\n"));
|
---|
1686 |
|
---|
1687 | /* Check arguments. */
|
---|
1688 | if ( !pszFilename
|
---|
1689 | || *pszFilename == '\0')
|
---|
1690 | {
|
---|
1691 | AssertMsgFailed(("Invalid arguments: pszFilename=%p\n", pszFilename));
|
---|
1692 | return VERR_INVALID_PARAMETER;
|
---|
1693 | }
|
---|
1694 |
|
---|
1695 | PVDIIMAGEDESC pImage;
|
---|
1696 | int rc = vdiOpenImage(&pImage, pszFilename, VDI_OPEN_FLAGS_READONLY, NULL);
|
---|
1697 | if (RT_SUCCESS(rc))
|
---|
1698 | {
|
---|
1699 | Log(("VDICheckImage: filename=\"%s\" version=%08X type=%X cbDisk=%llu uuid={%RTuuid}\n",
|
---|
1700 | pszFilename,
|
---|
1701 | pImage->PreHeader.u32Version,
|
---|
1702 | getImageType(&pImage->Header),
|
---|
1703 | getImageDiskSize(&pImage->Header),
|
---|
1704 | getImageCreationUUID(&pImage->Header)));
|
---|
1705 |
|
---|
1706 | if ( pszComment
|
---|
1707 | && cbComment > 0)
|
---|
1708 | {
|
---|
1709 | char *pszTmp = getImageComment(&pImage->Header);
|
---|
1710 | size_t cb = strlen(pszTmp);
|
---|
1711 | if (cbComment > cb)
|
---|
1712 | memcpy(pszComment, pszTmp, cb + 1);
|
---|
1713 | else
|
---|
1714 | rc = VERR_BUFFER_OVERFLOW;
|
---|
1715 | }
|
---|
1716 | if (RT_SUCCESS(rc))
|
---|
1717 | {
|
---|
1718 | if (puVersion)
|
---|
1719 | *puVersion = pImage->PreHeader.u32Version;
|
---|
1720 | if (penmType)
|
---|
1721 | *penmType = getImageType(&pImage->Header);
|
---|
1722 | if (pcbSize)
|
---|
1723 | *pcbSize = getImageDiskSize(&pImage->Header);
|
---|
1724 | if (pUuid)
|
---|
1725 | *pUuid = *getImageCreationUUID(&pImage->Header);
|
---|
1726 | if (pParentUuid)
|
---|
1727 | *pParentUuid = *getImageParentUUID(&pImage->Header);
|
---|
1728 | }
|
---|
1729 | vdiCloseImage(pImage);
|
---|
1730 | }
|
---|
1731 |
|
---|
1732 | LogFlow(("VDICheckImage: returns %Rrc\n", rc));
|
---|
1733 | return rc;
|
---|
1734 | }
|
---|
1735 |
|
---|
1736 | /**
|
---|
1737 | * Changes an image's comment string.
|
---|
1738 | *
|
---|
1739 | * @returns VBox status code.
|
---|
1740 | * @param pszFilename Name of the image file to operate on.
|
---|
1741 | * @param pszComment New comment string (UTF-8). NULL is allowed to reset the comment.
|
---|
1742 | */
|
---|
1743 | VBOXDDU_DECL(int) VDISetImageComment(const char *pszFilename, const char *pszComment)
|
---|
1744 | {
|
---|
1745 | LogFlow(("VDISetImageComment:\n"));
|
---|
1746 |
|
---|
1747 | /*
|
---|
1748 | * Validate arguments.
|
---|
1749 | */
|
---|
1750 | if ( !pszFilename
|
---|
1751 | || *pszFilename == '\0')
|
---|
1752 | {
|
---|
1753 | AssertMsgFailed(("Invalid arguments: pszFilename=%p\n", pszFilename));
|
---|
1754 | return VERR_INVALID_PARAMETER;
|
---|
1755 | }
|
---|
1756 |
|
---|
1757 | const size_t cchComment = pszComment ? strlen(pszComment) : 0;
|
---|
1758 | if (cchComment >= VDI_IMAGE_COMMENT_SIZE)
|
---|
1759 | {
|
---|
1760 | Log(("VDISetImageComment: pszComment is too long, %d bytes!\n", cchComment));
|
---|
1761 | return VERR_VDI_COMMENT_TOO_LONG;
|
---|
1762 | }
|
---|
1763 |
|
---|
1764 | /*
|
---|
1765 | * Open the image for updating.
|
---|
1766 | */
|
---|
1767 | PVDIIMAGEDESC pImage;
|
---|
1768 | int rc = vdiOpenImage(&pImage, pszFilename, VDI_OPEN_FLAGS_NORMAL, NULL);
|
---|
1769 | if (RT_FAILURE(rc))
|
---|
1770 | {
|
---|
1771 | Log(("VDISetImageComment: vdiOpenImage rc=%Rrc filename=\"%s\"!\n", rc, pszFilename));
|
---|
1772 | return rc;
|
---|
1773 | }
|
---|
1774 | if (!pImage->fReadOnly)
|
---|
1775 | {
|
---|
1776 | /* we don't support old style images */
|
---|
1777 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
1778 | {
|
---|
1779 | /*
|
---|
1780 | * Update the comment field, making sure to zero out all of the previous comment.
|
---|
1781 | */
|
---|
1782 | memset(pImage->Header.u.v1.szComment, '\0', VDI_IMAGE_COMMENT_SIZE);
|
---|
1783 | memcpy(pImage->Header.u.v1.szComment, pszComment, cchComment);
|
---|
1784 |
|
---|
1785 | /* write out new the header */
|
---|
1786 | rc = vdiUpdateHeader(pImage);
|
---|
1787 | AssertMsgRC(rc, ("vdiUpdateHeader() failed, filename=\"%s\", rc=%Rrc\n",
|
---|
1788 | pImage->szFilename, rc));
|
---|
1789 | }
|
---|
1790 | else
|
---|
1791 | {
|
---|
1792 | Log(("VDISetImageComment: Unsupported version!\n"));
|
---|
1793 | rc = VERR_VDI_UNSUPPORTED_VERSION;
|
---|
1794 | }
|
---|
1795 | }
|
---|
1796 | else
|
---|
1797 | {
|
---|
1798 | Log(("VDISetImageComment: image \"%s\" is opened as read-only!\n", pszFilename));
|
---|
1799 | rc = VERR_VDI_IMAGE_READ_ONLY;
|
---|
1800 | }
|
---|
1801 |
|
---|
1802 | vdiCloseImage(pImage);
|
---|
1803 | return rc;
|
---|
1804 | }
|
---|
1805 |
|
---|
1806 | /**
|
---|
1807 | * Creates a new base image file.
|
---|
1808 | *
|
---|
1809 | * @returns VBox status code.
|
---|
1810 | * @param pszFilename Name of the creating image file.
|
---|
1811 | * @param enmType Image type, only base image types are acceptable.
|
---|
1812 | * @param cbSize Image size in bytes.
|
---|
1813 | * @param pszComment Pointer to image comment. NULL is ok.
|
---|
1814 | * @param pfnProgress Progress callback. Optional.
|
---|
1815 | * @param pvUser User argument for the progress callback.
|
---|
1816 | */
|
---|
1817 | VBOXDDU_DECL(int) VDICreateBaseImage(const char *pszFilename, VDIIMAGETYPE enmType, uint64_t cbSize,
|
---|
1818 | const char *pszComment, PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
1819 | {
|
---|
1820 | LogFlow(("VDICreateBaseImage:\n"));
|
---|
1821 |
|
---|
1822 | /* Check arguments. */
|
---|
1823 | if ( !pszFilename
|
---|
1824 | || *pszFilename == '\0'
|
---|
1825 | || (enmType != VDI_IMAGE_TYPE_NORMAL && enmType != VDI_IMAGE_TYPE_FIXED)
|
---|
1826 | || cbSize < VDI_IMAGE_DEFAULT_BLOCK_SIZE)
|
---|
1827 | {
|
---|
1828 | AssertMsgFailed(("Invalid arguments: pszFilename=%p enmType=%x cbSize=%llu\n",
|
---|
1829 | pszFilename, enmType, cbSize));
|
---|
1830 | return VERR_INVALID_PARAMETER;
|
---|
1831 | }
|
---|
1832 |
|
---|
1833 | int rc = vdiCreateImage(pszFilename, enmType, VDI_IMAGE_FLAGS_DEFAULT, cbSize, pszComment, NULL,
|
---|
1834 | pfnProgress, pvUser);
|
---|
1835 | LogFlow(("VDICreateBaseImage: returns %Rrc for filename=\"%s\"\n", rc, pszFilename));
|
---|
1836 | return rc;
|
---|
1837 | }
|
---|
1838 |
|
---|
1839 | /**
|
---|
1840 | * Creates a differencing dynamically growing image file for specified parent image.
|
---|
1841 | *
|
---|
1842 | * @returns VBox status code.
|
---|
1843 | * @param pszFilename Name of the creating differencing image file.
|
---|
1844 | * @param pszParent Name of the parent image file. May be base or diff image type.
|
---|
1845 | * @param pszComment Pointer to image comment. NULL is ok.
|
---|
1846 | * @param pfnProgress Progress callback. Optional.
|
---|
1847 | * @param pvUser User argument for the progress callback.
|
---|
1848 | */
|
---|
1849 | VBOXDDU_DECL(int) VDICreateDifferenceImage(const char *pszFilename, const char *pszParent,
|
---|
1850 | const char *pszComment, PFNVMPROGRESS pfnProgress,
|
---|
1851 | void *pvUser)
|
---|
1852 | {
|
---|
1853 | LogFlow(("VDICreateDifferenceImage:\n"));
|
---|
1854 |
|
---|
1855 | /* Check arguments. */
|
---|
1856 | if ( !pszFilename
|
---|
1857 | || *pszFilename == '\0'
|
---|
1858 | || !pszParent
|
---|
1859 | || *pszParent == '\0')
|
---|
1860 | {
|
---|
1861 | AssertMsgFailed(("Invalid arguments: pszFilename=%p pszParent=%p\n",
|
---|
1862 | pszFilename, pszParent));
|
---|
1863 | return VERR_INVALID_PARAMETER;
|
---|
1864 | }
|
---|
1865 |
|
---|
1866 | PVDIIMAGEDESC pParent;
|
---|
1867 | int rc = vdiOpenImage(&pParent, pszParent, VDI_OPEN_FLAGS_READONLY, NULL);
|
---|
1868 | if (RT_SUCCESS(rc))
|
---|
1869 | {
|
---|
1870 | rc = vdiCreateImage(pszFilename, VDI_IMAGE_TYPE_DIFF, VDI_IMAGE_FLAGS_DEFAULT,
|
---|
1871 | getImageDiskSize(&pParent->Header), pszComment, pParent,
|
---|
1872 | pfnProgress, pvUser);
|
---|
1873 | vdiCloseImage(pParent);
|
---|
1874 | }
|
---|
1875 | LogFlow(("VDICreateDifferenceImage: returns %Rrc for filename=\"%s\"\n", rc, pszFilename));
|
---|
1876 | return rc;
|
---|
1877 | }
|
---|
1878 |
|
---|
1879 | /**
|
---|
1880 | * Deletes an image. Only valid image files can be deleted by this call.
|
---|
1881 | *
|
---|
1882 | * @returns VBox status code.
|
---|
1883 | * @param pszFilename Name of the image file to check.
|
---|
1884 | */
|
---|
1885 | VBOXDDU_DECL(int) VDIDeleteImage(const char *pszFilename)
|
---|
1886 | {
|
---|
1887 | LogFlow(("VDIDeleteImage:\n"));
|
---|
1888 | /* Check arguments. */
|
---|
1889 | if ( !pszFilename
|
---|
1890 | || *pszFilename == '\0')
|
---|
1891 | {
|
---|
1892 | AssertMsgFailed(("Invalid arguments: pszFilename=%p\n", pszFilename));
|
---|
1893 | return VERR_INVALID_PARAMETER;
|
---|
1894 | }
|
---|
1895 |
|
---|
1896 | int rc = VDICheckImage(pszFilename, NULL, NULL, NULL, NULL, NULL, NULL, 0);
|
---|
1897 | if (RT_SUCCESS(rc))
|
---|
1898 | rc = RTFileDelete(pszFilename);
|
---|
1899 |
|
---|
1900 | LogFlow(("VDIDeleteImage: returns %Rrc for filename=\"%s\"\n", rc, pszFilename));
|
---|
1901 | return rc;
|
---|
1902 | }
|
---|
1903 |
|
---|
1904 | /**
|
---|
1905 | * Makes a copy of image file with a new (other) creation uuid.
|
---|
1906 | *
|
---|
1907 | * @returns VBox status code.
|
---|
1908 | * @param pszDstFilename Name of the image file to create.
|
---|
1909 | * @param pszSrcFilename Name of the image file to copy from.
|
---|
1910 | * @param pszComment Pointer to image comment. If NULL specified comment
|
---|
1911 | * will be copied from source image.
|
---|
1912 | * @param pfnProgress Progress callback. Optional.
|
---|
1913 | * @param pvUser User argument for the progress callback.
|
---|
1914 | */
|
---|
1915 | VBOXDDU_DECL(int) VDICopyImage(const char *pszDstFilename, const char *pszSrcFilename,
|
---|
1916 | const char *pszComment, PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
1917 | {
|
---|
1918 | LogFlow(("VDICopyImage:\n"));
|
---|
1919 |
|
---|
1920 | /* Check arguments. */
|
---|
1921 | if ( !pszDstFilename
|
---|
1922 | || *pszDstFilename == '\0'
|
---|
1923 | || !pszSrcFilename
|
---|
1924 | || *pszSrcFilename == '\0')
|
---|
1925 | {
|
---|
1926 | AssertMsgFailed(("Invalid arguments: pszDstFilename=%p pszSrcFilename=%p\n",
|
---|
1927 | pszDstFilename, pszSrcFilename));
|
---|
1928 | return VERR_INVALID_PARAMETER;
|
---|
1929 | }
|
---|
1930 |
|
---|
1931 | /* Special check for comment length. */
|
---|
1932 | if ( pszComment
|
---|
1933 | && strlen(pszComment) >= VDI_IMAGE_COMMENT_SIZE)
|
---|
1934 | {
|
---|
1935 | Log(("VDICopyImage: pszComment is too long, cb=%d\n", strlen(pszComment)));
|
---|
1936 | return VERR_VDI_COMMENT_TOO_LONG;
|
---|
1937 | }
|
---|
1938 |
|
---|
1939 | PVDIIMAGEDESC pImage;
|
---|
1940 | int rc = vdiOpenImage(&pImage, pszSrcFilename, VDI_OPEN_FLAGS_READONLY, NULL);
|
---|
1941 | if (RT_FAILURE(rc))
|
---|
1942 | {
|
---|
1943 | Log(("VDICopyImage: src image \"%s\" open failed rc=%Rrc\n", pszSrcFilename, rc));
|
---|
1944 | return rc;
|
---|
1945 | }
|
---|
1946 |
|
---|
1947 | uint64_t cbFile = pImage->offStartData
|
---|
1948 | + ((uint64_t)getImageBlocksAllocated(&pImage->Header) << pImage->uShiftIndex2Offset);
|
---|
1949 |
|
---|
1950 | /* create file */
|
---|
1951 | RTFILE File;
|
---|
1952 | rc = RTFileOpen(&File,
|
---|
1953 | pszDstFilename,
|
---|
1954 | RTFILE_O_READWRITE | RTFILE_O_CREATE | RTFILE_O_DENY_ALL | RTFILE_O_NOT_CONTENT_INDEXED);
|
---|
1955 | if (RT_SUCCESS(rc))
|
---|
1956 | {
|
---|
1957 | #ifndef RT_OS_WINDOWS
|
---|
1958 | /* lock new image exclusively to close any wrong access by VDI API calls. */
|
---|
1959 | rc = RTFileLock(File, RTFILE_LOCK_WRITE | RTFILE_LOCK_IMMEDIATELY, 0, cbFile);
|
---|
1960 | if (RT_SUCCESS(rc))
|
---|
1961 | #endif
|
---|
1962 | {
|
---|
1963 | /* Set the size of a new file. */
|
---|
1964 | rc = RTFileSetSize(File, cbFile);
|
---|
1965 | if (RT_SUCCESS(rc))
|
---|
1966 | {
|
---|
1967 | /* A dirty trick - use original image data to fill the new image. */
|
---|
1968 | RTFILE oldFileHandle = pImage->File;
|
---|
1969 | pImage->File = File;
|
---|
1970 | pImage->fReadOnly = false;
|
---|
1971 |
|
---|
1972 | /* generate a new image creation uuid. */
|
---|
1973 | RTUuidCreate(getImageCreationUUID(&pImage->Header));
|
---|
1974 | /* generate a new image last-modified uuid. */
|
---|
1975 | RTUuidCreate(getImageModificationUUID(&pImage->Header));
|
---|
1976 | /* set image comment, if present. */
|
---|
1977 | if (pszComment)
|
---|
1978 | strncpy(getImageComment(&pImage->Header), pszComment, VDI_IMAGE_COMMENT_SIZE);
|
---|
1979 |
|
---|
1980 | /* Write the pre-header to new image. */
|
---|
1981 | rc = RTFileSeek(pImage->File, 0, RTFILE_SEEK_BEGIN, NULL);
|
---|
1982 | if (RT_SUCCESS(rc))
|
---|
1983 | rc = RTFileWrite(pImage->File,
|
---|
1984 | &pImage->PreHeader,
|
---|
1985 | sizeof(pImage->PreHeader),
|
---|
1986 | NULL);
|
---|
1987 |
|
---|
1988 | /* Write the header and the blocks array to new image. */
|
---|
1989 | if (RT_SUCCESS(rc))
|
---|
1990 | rc = vdiUpdateBlocks(pImage);
|
---|
1991 |
|
---|
1992 | pImage->File = oldFileHandle;
|
---|
1993 | pImage->fReadOnly = true;
|
---|
1994 |
|
---|
1995 | /* Seek to the data start in both images. */
|
---|
1996 | if (RT_SUCCESS(rc))
|
---|
1997 | rc = RTFileSeek(pImage->File,
|
---|
1998 | pImage->offStartData,
|
---|
1999 | RTFILE_SEEK_BEGIN,
|
---|
2000 | NULL);
|
---|
2001 | if (RT_SUCCESS(rc))
|
---|
2002 | rc = RTFileSeek(File,
|
---|
2003 | pImage->offStartData,
|
---|
2004 | RTFILE_SEEK_BEGIN,
|
---|
2005 | NULL);
|
---|
2006 |
|
---|
2007 | if (RT_SUCCESS(rc))
|
---|
2008 | {
|
---|
2009 | /* alloc tmp buffer */
|
---|
2010 | void *pvBuf = RTMemTmpAlloc(VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
2011 | if (pvBuf)
|
---|
2012 | {
|
---|
2013 | /* Main copy loop. */
|
---|
2014 | uint64_t cbData = cbFile - pImage->offStartData;
|
---|
2015 | unsigned cBlocks = (unsigned)(cbData / VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
2016 | unsigned c = 0;
|
---|
2017 |
|
---|
2018 | while (cbData)
|
---|
2019 | {
|
---|
2020 | unsigned cbToCopy = (unsigned)RT_MIN(cbData, VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
2021 |
|
---|
2022 | /* Read. */
|
---|
2023 | rc = RTFileRead(pImage->File, pvBuf, cbToCopy, NULL);
|
---|
2024 | if (RT_FAILURE(rc))
|
---|
2025 | break;
|
---|
2026 |
|
---|
2027 | /* Write. */
|
---|
2028 | rc = RTFileWrite(File, pvBuf, cbToCopy, NULL);
|
---|
2029 | if (RT_FAILURE(rc))
|
---|
2030 | break;
|
---|
2031 |
|
---|
2032 | if (pfnProgress)
|
---|
2033 | {
|
---|
2034 | c++;
|
---|
2035 | rc = pfnProgress(NULL /* WARNING! pVM=NULL */,
|
---|
2036 | (c * 100) / cBlocks,
|
---|
2037 | pvUser);
|
---|
2038 | if (RT_FAILURE(rc))
|
---|
2039 | break;
|
---|
2040 | }
|
---|
2041 | cbData -= cbToCopy;
|
---|
2042 | }
|
---|
2043 |
|
---|
2044 | RTMemTmpFree(pvBuf);
|
---|
2045 | }
|
---|
2046 | else
|
---|
2047 | rc = VERR_NO_MEMORY;
|
---|
2048 | }
|
---|
2049 | }
|
---|
2050 |
|
---|
2051 | #ifndef RT_OS_WINDOWS
|
---|
2052 | RTFileUnlock(File, 0, cbFile);
|
---|
2053 | #endif
|
---|
2054 | }
|
---|
2055 |
|
---|
2056 | RTFileClose(File);
|
---|
2057 |
|
---|
2058 | if (RT_FAILURE(rc))
|
---|
2059 | RTFileDelete(pszDstFilename);
|
---|
2060 |
|
---|
2061 | if (pfnProgress)
|
---|
2062 | pfnProgress(NULL /* WARNING! pVM=NULL */, 100, pvUser);
|
---|
2063 | }
|
---|
2064 |
|
---|
2065 | vdiCloseImage(pImage);
|
---|
2066 |
|
---|
2067 | LogFlow(("VDICopyImage: returns %Rrc for pszSrcFilename=\"%s\" pszDstFilename=\"%s\"\n",
|
---|
2068 | rc, pszSrcFilename, pszDstFilename));
|
---|
2069 | return rc;
|
---|
2070 | }
|
---|
2071 |
|
---|
2072 | /**
|
---|
2073 | * Shrinks growing image file by removing zeroed data blocks.
|
---|
2074 | *
|
---|
2075 | * @returns VBox status code.
|
---|
2076 | * @param pszFilename Name of the image file to shrink.
|
---|
2077 | * @param pfnProgress Progress callback. Optional.
|
---|
2078 | * @param pvUser User argument for the progress callback.
|
---|
2079 | */
|
---|
2080 | VBOXDDU_DECL(int) VDIShrinkImage(const char *pszFilename, PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
2081 | {
|
---|
2082 | LogFlow(("VDIShrinkImage:\n"));
|
---|
2083 |
|
---|
2084 | /* Check arguments. */
|
---|
2085 | if ( !pszFilename
|
---|
2086 | || *pszFilename == '\0')
|
---|
2087 | {
|
---|
2088 | AssertMsgFailed(("Invalid arguments: pszFilename=%p\n", pszFilename));
|
---|
2089 | return VERR_INVALID_PARAMETER;
|
---|
2090 | }
|
---|
2091 |
|
---|
2092 | PVDIIMAGEDESC pImage;
|
---|
2093 | int rc = vdiOpenImage(&pImage, pszFilename, VDI_OPEN_FLAGS_NORMAL, NULL);
|
---|
2094 | if (RT_FAILURE(rc))
|
---|
2095 | {
|
---|
2096 | Log(("VDIShrinkImage: vdiOpenImage rc=%Rrc filename=\"%s\"\n", rc, pszFilename));
|
---|
2097 | return rc;
|
---|
2098 | }
|
---|
2099 | if (pImage->fReadOnly)
|
---|
2100 | {
|
---|
2101 | Log(("VDIShrinkImage: image \"%s\" is opened as read-only!\n", pszFilename));
|
---|
2102 | vdiCloseImage(pImage);
|
---|
2103 | return VERR_VDI_IMAGE_READ_ONLY;
|
---|
2104 | }
|
---|
2105 |
|
---|
2106 | /* Do debug dump. */
|
---|
2107 | vdiDumpImage(pImage);
|
---|
2108 |
|
---|
2109 | /* Working data. */
|
---|
2110 | unsigned cbBlock = getImageBlockSize(&pImage->Header);
|
---|
2111 | unsigned cBlocks = getImageBlocks(&pImage->Header);
|
---|
2112 | unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
|
---|
2113 |
|
---|
2114 | uint64_t cbFile;
|
---|
2115 | rc = RTFileGetSize(pImage->File, &cbFile);
|
---|
2116 | if (RT_FAILURE(rc))
|
---|
2117 | {
|
---|
2118 | Log(("VDIShrinkImage: RTFileGetSize rc=%Rrc for file=\"%s\"\n", rc, pszFilename));
|
---|
2119 | vdiCloseImage(pImage);
|
---|
2120 | return rc;
|
---|
2121 | }
|
---|
2122 |
|
---|
2123 | uint64_t cbData = cbFile - pImage->offStartData;
|
---|
2124 | unsigned cBlocksAllocated2 = (unsigned)(cbData >> pImage->uShiftIndex2Offset);
|
---|
2125 | if (cbData != (uint64_t)cBlocksAllocated << pImage->uShiftIndex2Offset)
|
---|
2126 | Log(("VDIShrinkImage: invalid image file length, cbBlock=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
|
---|
2127 | cbBlock, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
|
---|
2128 |
|
---|
2129 | /* Allocate second blocks array for back resolving. */
|
---|
2130 | PVDIIMAGEBLOCKPOINTER paBlocks2 =
|
---|
2131 | (PVDIIMAGEBLOCKPOINTER)RTMemTmpAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * cBlocks);
|
---|
2132 | if (!paBlocks2)
|
---|
2133 | {
|
---|
2134 | Log(("VDIShrinkImage: failed to allocate paBlocks2 buffer (%u bytes)\n", sizeof(VDIIMAGEBLOCKPOINTER) * cBlocks));
|
---|
2135 | vdiCloseImage(pImage);
|
---|
2136 | return VERR_NO_MEMORY;
|
---|
2137 | }
|
---|
2138 |
|
---|
2139 | /* Init second blocks array. */
|
---|
2140 | for (unsigned n = 0; n < cBlocks; n++)
|
---|
2141 | paBlocks2[n] = VDI_IMAGE_BLOCK_FREE;
|
---|
2142 |
|
---|
2143 | /* Fill second blocks array, check for allocational errors. */
|
---|
2144 | for (unsigned n = 0; n < cBlocks; n++)
|
---|
2145 | {
|
---|
2146 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[n]))
|
---|
2147 | {
|
---|
2148 | unsigned uBlock = pImage->paBlocks[n];
|
---|
2149 | if (uBlock < cBlocksAllocated2)
|
---|
2150 | {
|
---|
2151 | if (paBlocks2[uBlock] == VDI_IMAGE_BLOCK_FREE)
|
---|
2152 | paBlocks2[uBlock] = n;
|
---|
2153 | else
|
---|
2154 | {
|
---|
2155 | Log(("VDIShrinkImage: block n=%u -> uBlock=%u is already in use!\n", n, uBlock));
|
---|
2156 | /* free second link to block. */
|
---|
2157 | pImage->paBlocks[n] = VDI_IMAGE_BLOCK_FREE;
|
---|
2158 | }
|
---|
2159 | }
|
---|
2160 | else
|
---|
2161 | {
|
---|
2162 | Log(("VDIShrinkImage: block n=%u -> uBlock=%u is out of blocks range! (cbBlock=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu)\n",
|
---|
2163 | n, uBlock, cbBlock, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
|
---|
2164 | /* free link to invalid block. */
|
---|
2165 | pImage->paBlocks[n] = VDI_IMAGE_BLOCK_FREE;
|
---|
2166 | }
|
---|
2167 | }
|
---|
2168 | }
|
---|
2169 |
|
---|
2170 | /* Allocate a working buffer for one block. */
|
---|
2171 | void *pvBuf = RTMemTmpAlloc(cbBlock);
|
---|
2172 | if (pvBuf)
|
---|
2173 | {
|
---|
2174 | /* Main voodoo loop, search holes and fill it. */
|
---|
2175 | unsigned uBlockWrite = 0;
|
---|
2176 | for (unsigned uBlock = 0; uBlock < cBlocksAllocated2; uBlock++)
|
---|
2177 | {
|
---|
2178 | if (paBlocks2[uBlock] != VDI_IMAGE_BLOCK_FREE)
|
---|
2179 | {
|
---|
2180 | /* Read the block from file and check for zeroes. */
|
---|
2181 | uint64_t u64Offset = ((uint64_t)uBlock << pImage->uShiftIndex2Offset)
|
---|
2182 | + (pImage->offStartData + pImage->offStartBlockData);
|
---|
2183 | rc = RTFileSeek(pImage->File, u64Offset, RTFILE_SEEK_BEGIN, NULL);
|
---|
2184 | if (RT_FAILURE(rc))
|
---|
2185 | {
|
---|
2186 | Log(("VDIShrinkImage: seek rc=%Rrc filename=\"%s\" uBlock=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
|
---|
2187 | rc, pImage->szFilename, uBlock, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
|
---|
2188 | break;
|
---|
2189 | }
|
---|
2190 | rc = RTFileRead(pImage->File, pvBuf, cbBlock, NULL);
|
---|
2191 | if (RT_FAILURE(rc))
|
---|
2192 | {
|
---|
2193 | Log(("VDIShrinkImage: read rc=%Rrc filename=\"%s\" cbBlock=%u uBlock=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
|
---|
2194 | rc, pImage->szFilename, cbBlock, uBlock, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
|
---|
2195 | break;
|
---|
2196 | }
|
---|
2197 |
|
---|
2198 | /* Check block for data. */
|
---|
2199 | Assert(cbBlock % 4 == 0);
|
---|
2200 | if (ASMBitFirstSet(pvBuf, cbBlock * 8) != -1)
|
---|
2201 | {
|
---|
2202 | /* Block has a data, may be it must be moved. */
|
---|
2203 | if (uBlockWrite < uBlock)
|
---|
2204 | {
|
---|
2205 | /* Move the block. */
|
---|
2206 | u64Offset = ((uint64_t)uBlockWrite << pImage->uShiftIndex2Offset)
|
---|
2207 | + (pImage->offStartData + pImage->offStartBlockData);
|
---|
2208 | rc = RTFileSeek(pImage->File, u64Offset, RTFILE_SEEK_BEGIN, NULL);
|
---|
2209 | if (RT_FAILURE(rc))
|
---|
2210 | {
|
---|
2211 | Log(("VDIShrinkImage: seek(2) rc=%Rrc filename=\"%s\" uBlockWrite=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
|
---|
2212 | rc, pImage->szFilename, uBlockWrite, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
|
---|
2213 | break;
|
---|
2214 | }
|
---|
2215 | rc = RTFileWrite(pImage->File, pvBuf, cbBlock, NULL);
|
---|
2216 | if (RT_FAILURE(rc))
|
---|
2217 | {
|
---|
2218 | Log(("VDIShrinkImage: write rc=%Rrc filename=\"%s\" cbBlock=%u uBlockWrite=%u cBlocks=%u cBlocksAllocated=%u cBlocksAllocated2=%u cbData=%llu\n",
|
---|
2219 | rc, pImage->szFilename, cbBlock, uBlockWrite, cBlocks, cBlocksAllocated, cBlocksAllocated2, cbData));
|
---|
2220 | break;
|
---|
2221 | }
|
---|
2222 | }
|
---|
2223 | /* Fix the block pointer. */
|
---|
2224 | pImage->paBlocks[paBlocks2[uBlock]] = uBlockWrite;
|
---|
2225 | uBlockWrite++;
|
---|
2226 | }
|
---|
2227 | else
|
---|
2228 | {
|
---|
2229 | Log(("VDIShrinkImage: found a zeroed block, uBlock=%u\n", uBlock));
|
---|
2230 |
|
---|
2231 | /* Fix the block pointer. */
|
---|
2232 | pImage->paBlocks[paBlocks2[uBlock]] = VDI_IMAGE_BLOCK_ZERO;
|
---|
2233 | }
|
---|
2234 | }
|
---|
2235 | else
|
---|
2236 | Log(("VDIShrinkImage: found an unused block, uBlock=%u\n", uBlock));
|
---|
2237 |
|
---|
2238 | if (pfnProgress)
|
---|
2239 | {
|
---|
2240 | pfnProgress(NULL /* WARNING! pVM=NULL */,
|
---|
2241 | (uBlock * 100) / cBlocksAllocated2,
|
---|
2242 | pvUser);
|
---|
2243 | /* Shrink is unbreakable operation! */
|
---|
2244 | }
|
---|
2245 | }
|
---|
2246 |
|
---|
2247 | RTMemTmpFree(pvBuf);
|
---|
2248 |
|
---|
2249 | if ( RT_SUCCESS(rc)
|
---|
2250 | && uBlockWrite < cBlocksAllocated2)
|
---|
2251 | {
|
---|
2252 | /* File size must be shrinked. */
|
---|
2253 | Log(("VDIShrinkImage: shrinking file size from %llu to %llu bytes\n",
|
---|
2254 | cbFile,
|
---|
2255 | pImage->offStartData + ((uint64_t)uBlockWrite << pImage->uShiftIndex2Offset)));
|
---|
2256 | rc = RTFileSetSize(pImage->File,
|
---|
2257 | pImage->offStartData + ((uint64_t)uBlockWrite << pImage->uShiftIndex2Offset));
|
---|
2258 | if (RT_FAILURE(rc))
|
---|
2259 | Log(("VDIShrinkImage: RTFileSetSize rc=%Rrc\n", rc));
|
---|
2260 | }
|
---|
2261 | cBlocksAllocated2 = uBlockWrite;
|
---|
2262 | }
|
---|
2263 | else
|
---|
2264 | {
|
---|
2265 | Log(("VDIShrinkImage: failed to allocate working buffer (%u bytes)\n", cbBlock));
|
---|
2266 | rc = VERR_NO_MEMORY;
|
---|
2267 | }
|
---|
2268 |
|
---|
2269 | /* Save header and blocks array. */
|
---|
2270 | if (RT_SUCCESS(rc))
|
---|
2271 | {
|
---|
2272 | setImageBlocksAllocated(&pImage->Header, cBlocksAllocated2);
|
---|
2273 | rc = vdiUpdateBlocks(pImage);
|
---|
2274 | if (pfnProgress)
|
---|
2275 | pfnProgress(NULL /* WARNING! pVM=NULL */, 100, pvUser);
|
---|
2276 | }
|
---|
2277 |
|
---|
2278 | /* Do debug dump. */
|
---|
2279 | vdiDumpImage(pImage);
|
---|
2280 |
|
---|
2281 | /* Clean up. */
|
---|
2282 | RTMemTmpFree(paBlocks2);
|
---|
2283 | vdiCloseImage(pImage);
|
---|
2284 |
|
---|
2285 | LogFlow(("VDIShrinkImage: returns %Rrc for filename=\"%s\"\n", rc, pszFilename));
|
---|
2286 | return rc;
|
---|
2287 | }
|
---|
2288 |
|
---|
2289 | /**
|
---|
2290 | * Converts image file from older VDI formats to current one.
|
---|
2291 | *
|
---|
2292 | * @returns VBox status code.
|
---|
2293 | * @param pszFilename Name of the image file to convert.
|
---|
2294 | * @param pfnProgress Progress callback. Optional.
|
---|
2295 | * @param pvUser User argument for the progress callback.
|
---|
2296 | * @remark Only used by vditool
|
---|
2297 | */
|
---|
2298 | VBOXDDU_DECL(int) VDIConvertImage(const char *pszFilename, PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
2299 | {
|
---|
2300 | LogFlow(("VDIConvertImage:\n"));
|
---|
2301 |
|
---|
2302 | /* Check arguments. */
|
---|
2303 | if ( !pszFilename
|
---|
2304 | || *pszFilename == '\0')
|
---|
2305 | {
|
---|
2306 | AssertMsgFailed(("Invalid arguments: pszFilename=%p\n", pszFilename));
|
---|
2307 | return VERR_INVALID_PARAMETER;
|
---|
2308 | }
|
---|
2309 |
|
---|
2310 | PVDIIMAGEDESC pImage;
|
---|
2311 | int rc = vdiOpenImage(&pImage, pszFilename, VDI_OPEN_FLAGS_NORMAL, NULL);
|
---|
2312 | if (RT_FAILURE(rc))
|
---|
2313 | {
|
---|
2314 | Log(("VDIConvertImage: vdiOpenImage rc=%Rrc filename=\"%s\"\n", rc, pszFilename));
|
---|
2315 | return rc;
|
---|
2316 | }
|
---|
2317 |
|
---|
2318 | VDIHEADER Header = {0};
|
---|
2319 | int off;
|
---|
2320 | uint64_t cbFile;
|
---|
2321 | uint64_t cbData;
|
---|
2322 |
|
---|
2323 | if (pImage->fReadOnly)
|
---|
2324 | {
|
---|
2325 | Log(("VDIConvertImage: image \"%s\" is opened as read-only!\n", pszFilename));
|
---|
2326 | rc = VERR_VDI_IMAGE_READ_ONLY;
|
---|
2327 | goto l_conversion_failed;
|
---|
2328 | }
|
---|
2329 |
|
---|
2330 | if (pImage->PreHeader.u32Version != 0x00000002)
|
---|
2331 | {
|
---|
2332 | Log(("VDIConvertImage: unsupported version=%08X filename=\"%s\"\n",
|
---|
2333 | pImage->PreHeader.u32Version, pszFilename));
|
---|
2334 | rc = VERR_VDI_UNSUPPORTED_VERSION;
|
---|
2335 | goto l_conversion_failed;
|
---|
2336 | }
|
---|
2337 |
|
---|
2338 | /* Build new version header from old one. */
|
---|
2339 | vdiInitHeader(&Header,
|
---|
2340 | getImageType(&pImage->Header),
|
---|
2341 | VDI_IMAGE_FLAGS_DEFAULT, /* Safety issue: Always use default flags. */
|
---|
2342 | getImageComment(&pImage->Header),
|
---|
2343 | getImageDiskSize(&pImage->Header),
|
---|
2344 | getImageBlockSize(&pImage->Header),
|
---|
2345 | 0);
|
---|
2346 | setImageBlocksAllocated(&Header, getImageBlocksAllocated(&pImage->Header));
|
---|
2347 | /* Set both the LCHSGeometry and LegacyGeometry. If they're wrong they
|
---|
2348 | * will be fixed later when the image is used. */
|
---|
2349 | if (getImageLCHSGeometry(&pImage->Header))
|
---|
2350 | {
|
---|
2351 | Header.u.v1.LegacyGeometry = *getImageLCHSGeometry(&pImage->Header);
|
---|
2352 | Header.u.v1plus.LCHSGeometry = *getImageLCHSGeometry(&pImage->Header);
|
---|
2353 | }
|
---|
2354 | *getImageCreationUUID(&Header) = *getImageCreationUUID(&pImage->Header);
|
---|
2355 | *getImageModificationUUID(&Header) = *getImageModificationUUID(&pImage->Header);
|
---|
2356 |
|
---|
2357 | /* Calc data offset. */
|
---|
2358 | off = getImageDataOffset(&Header) - getImageDataOffset(&pImage->Header);
|
---|
2359 | if (off <= 0)
|
---|
2360 | {
|
---|
2361 | rc = VERR_VDI_INVALID_HEADER;
|
---|
2362 | goto l_conversion_failed;
|
---|
2363 | }
|
---|
2364 |
|
---|
2365 | rc = RTFileGetSize(pImage->File, &cbFile);
|
---|
2366 | if (RT_FAILURE(rc))
|
---|
2367 | goto l_conversion_failed;
|
---|
2368 |
|
---|
2369 | /* Check file size. */
|
---|
2370 | cbData = cbFile - getImageDataOffset(&pImage->Header);
|
---|
2371 | if (cbData != (uint64_t)getImageBlocksAllocated(&pImage->Header) << pImage->uShiftIndex2Offset)
|
---|
2372 | {
|
---|
2373 | AssertMsgFailed(("Invalid file size, broken image?\n"));
|
---|
2374 | rc = VERR_VDI_INVALID_HEADER;
|
---|
2375 | goto l_conversion_failed;
|
---|
2376 | }
|
---|
2377 |
|
---|
2378 | /* Expand file. */
|
---|
2379 | rc = RTFileSetSize(pImage->File, cbFile + off);
|
---|
2380 | if (RT_FAILURE(rc))
|
---|
2381 | goto l_conversion_failed;
|
---|
2382 |
|
---|
2383 | if (cbData > 0)
|
---|
2384 | {
|
---|
2385 | /* Calc current file position to move data from. */
|
---|
2386 | uint64_t offFile;
|
---|
2387 | if (cbData > VDIDISK_DEFAULT_BUFFER_SIZE)
|
---|
2388 | offFile = cbFile - VDIDISK_DEFAULT_BUFFER_SIZE;
|
---|
2389 | else
|
---|
2390 | offFile = getImageDataOffset(&pImage->Header);
|
---|
2391 |
|
---|
2392 | unsigned cMoves = (unsigned)(cbData / VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
2393 | unsigned c = 0;
|
---|
2394 |
|
---|
2395 | /* alloc tmp buffer */
|
---|
2396 | void *pvBuf = RTMemTmpAlloc(VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
2397 | if (pvBuf)
|
---|
2398 | {
|
---|
2399 | /* Move data. */
|
---|
2400 | for (;;)
|
---|
2401 | {
|
---|
2402 | unsigned cbToMove = (unsigned)RT_MIN(cbData, VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
2403 |
|
---|
2404 | /* Read. */
|
---|
2405 | rc = RTFileSeek(pImage->File, offFile, RTFILE_SEEK_BEGIN, NULL);
|
---|
2406 | if (RT_FAILURE(rc))
|
---|
2407 | break;
|
---|
2408 | rc = RTFileRead(pImage->File, pvBuf, cbToMove, NULL);
|
---|
2409 | if (RT_FAILURE(rc))
|
---|
2410 | break;
|
---|
2411 |
|
---|
2412 | /* Write. */
|
---|
2413 | rc = RTFileSeek(pImage->File, offFile + off, RTFILE_SEEK_BEGIN, NULL);
|
---|
2414 | if (RT_FAILURE(rc))
|
---|
2415 | break;
|
---|
2416 | rc = RTFileWrite(pImage->File, pvBuf, cbToMove, NULL);
|
---|
2417 | if (RT_FAILURE(rc))
|
---|
2418 | break;
|
---|
2419 |
|
---|
2420 | if (pfnProgress)
|
---|
2421 | {
|
---|
2422 | c++;
|
---|
2423 | pfnProgress(NULL /* WARNING! pVM=NULL */,
|
---|
2424 | (c * 100) / cMoves,
|
---|
2425 | pvUser);
|
---|
2426 | /* Note: conversion is non breakable operation, skipping rc here. */
|
---|
2427 | }
|
---|
2428 |
|
---|
2429 | cbData -= cbToMove;
|
---|
2430 | if (cbData == 0)
|
---|
2431 | break;
|
---|
2432 |
|
---|
2433 | if (cbData > VDIDISK_DEFAULT_BUFFER_SIZE)
|
---|
2434 | offFile -= VDIDISK_DEFAULT_BUFFER_SIZE;
|
---|
2435 | else
|
---|
2436 | offFile = getImageDataOffset(&pImage->Header);
|
---|
2437 | }
|
---|
2438 |
|
---|
2439 | /* Fill the beginning of file with zeroes to wipe out old headers etc. */
|
---|
2440 | if (RT_SUCCESS(rc))
|
---|
2441 | {
|
---|
2442 | Assert(offFile + off <= VDIDISK_DEFAULT_BUFFER_SIZE);
|
---|
2443 | rc = RTFileSeek(pImage->File, 0, RTFILE_SEEK_BEGIN, NULL);
|
---|
2444 | if (RT_SUCCESS(rc))
|
---|
2445 | {
|
---|
2446 | memset(pvBuf, 0, (unsigned)offFile + off);
|
---|
2447 | rc = RTFileWrite(pImage->File, pvBuf, (unsigned)offFile + off, NULL);
|
---|
2448 | }
|
---|
2449 | }
|
---|
2450 |
|
---|
2451 | RTMemTmpFree(pvBuf);
|
---|
2452 | }
|
---|
2453 | else
|
---|
2454 | rc = VERR_NO_MEMORY;
|
---|
2455 |
|
---|
2456 | if (RT_FAILURE(rc))
|
---|
2457 | goto l_conversion_failed;
|
---|
2458 | }
|
---|
2459 |
|
---|
2460 | if (pfnProgress)
|
---|
2461 | {
|
---|
2462 | pfnProgress(NULL /* WARNING! pVM=NULL */, 100, pvUser);
|
---|
2463 | /* Note: conversion is non breakable operation, skipping rc here. */
|
---|
2464 | }
|
---|
2465 |
|
---|
2466 | /* Data moved, now we need to save new pre header, header and blocks array. */
|
---|
2467 |
|
---|
2468 | vdiInitPreHeader(&pImage->PreHeader);
|
---|
2469 | pImage->Header = Header;
|
---|
2470 |
|
---|
2471 | /* Setup image parameters by header. */
|
---|
2472 | vdiSetupImageDesc(pImage);
|
---|
2473 |
|
---|
2474 | /* Write pre-header. */
|
---|
2475 | rc = RTFileSeek(pImage->File, 0, RTFILE_SEEK_BEGIN, NULL);
|
---|
2476 | if (RT_FAILURE(rc))
|
---|
2477 | goto l_conversion_failed;
|
---|
2478 | rc = RTFileWrite(pImage->File, &pImage->PreHeader, sizeof(pImage->PreHeader), NULL);
|
---|
2479 | if (RT_FAILURE(rc))
|
---|
2480 | goto l_conversion_failed;
|
---|
2481 |
|
---|
2482 | /* Write header and blocks array. */
|
---|
2483 | rc = vdiUpdateBlocks(pImage);
|
---|
2484 |
|
---|
2485 | l_conversion_failed:
|
---|
2486 | vdiCloseImage(pImage);
|
---|
2487 |
|
---|
2488 | LogFlow(("VDIConvertImage: returns %Rrc for filename=\"%s\"\n", rc, pszFilename));
|
---|
2489 | return rc;
|
---|
2490 | }
|
---|
2491 |
|
---|
2492 | /**
|
---|
2493 | * Queries the image's UUID and parent UUIDs.
|
---|
2494 | *
|
---|
2495 | * @returns VBox status code.
|
---|
2496 | * @param pszFilename Name of the image file to operate on.
|
---|
2497 | * @param pUuid Where to store image UUID (can be NULL).
|
---|
2498 | * @param pModificationUuid Where to store modification UUID (can be NULL).
|
---|
2499 | * @param pParentUuuid Where to store parent UUID (can be NULL).
|
---|
2500 | * @param pParentModificationUuid Where to store parent modification UUID (can be NULL).
|
---|
2501 | */
|
---|
2502 | VBOXDDU_DECL(int) VDIGetImageUUIDs(const char *pszFilename,
|
---|
2503 | PRTUUID pUuid, PRTUUID pModificationUuid,
|
---|
2504 | PRTUUID pParentUuid, PRTUUID pParentModificationUuid)
|
---|
2505 | {
|
---|
2506 | LogFlow(("VDIGetImageUUIDs:\n"));
|
---|
2507 |
|
---|
2508 | /* Check arguments. */
|
---|
2509 | if ( !pszFilename
|
---|
2510 | || *pszFilename == '\0')
|
---|
2511 | {
|
---|
2512 | AssertMsgFailed(("Invalid arguments: pszFilename=%p\n", pszFilename));
|
---|
2513 | return VERR_INVALID_PARAMETER;
|
---|
2514 | }
|
---|
2515 |
|
---|
2516 | /*
|
---|
2517 | * Try open the specified image.
|
---|
2518 | */
|
---|
2519 | PVDIIMAGEDESC pImage;
|
---|
2520 | int rc = vdiOpenImage(&pImage, pszFilename, VDI_OPEN_FLAGS_NORMAL, NULL);
|
---|
2521 | if (RT_FAILURE(rc))
|
---|
2522 | {
|
---|
2523 | Log(("VDIGetImageUUIDs: vdiOpenImage rc=%Rrc filename=\"%s\"\n", rc, pszFilename));
|
---|
2524 | return rc;
|
---|
2525 | }
|
---|
2526 |
|
---|
2527 | /*
|
---|
2528 | * Query data.
|
---|
2529 | */
|
---|
2530 | if (pUuid)
|
---|
2531 | {
|
---|
2532 | PCRTUUID pTmpUuid = getImageCreationUUID(&pImage->Header);
|
---|
2533 | if (pTmpUuid)
|
---|
2534 | *pUuid = *pTmpUuid;
|
---|
2535 | else
|
---|
2536 | RTUuidClear(pUuid);
|
---|
2537 | }
|
---|
2538 | if (pModificationUuid)
|
---|
2539 | {
|
---|
2540 | PCRTUUID pTmpUuid = getImageModificationUUID(&pImage->Header);
|
---|
2541 | if (pTmpUuid)
|
---|
2542 | *pModificationUuid = *pTmpUuid;
|
---|
2543 | else
|
---|
2544 | RTUuidClear(pModificationUuid);
|
---|
2545 | }
|
---|
2546 | if (pParentUuid)
|
---|
2547 | {
|
---|
2548 | PCRTUUID pTmpUuid = getImageParentUUID(&pImage->Header);
|
---|
2549 | if (pTmpUuid)
|
---|
2550 | *pParentUuid = *pTmpUuid;
|
---|
2551 | else
|
---|
2552 | RTUuidClear(pParentUuid);
|
---|
2553 | }
|
---|
2554 | if (pParentModificationUuid)
|
---|
2555 | {
|
---|
2556 | PCRTUUID pTmpUuid = getImageParentModificationUUID(&pImage->Header);
|
---|
2557 | if (pTmpUuid)
|
---|
2558 | *pParentModificationUuid = *pTmpUuid;
|
---|
2559 | else
|
---|
2560 | RTUuidClear(pParentModificationUuid);
|
---|
2561 | }
|
---|
2562 |
|
---|
2563 | /*
|
---|
2564 | * Close the image.
|
---|
2565 | */
|
---|
2566 | vdiCloseImage(pImage);
|
---|
2567 |
|
---|
2568 | return VINF_SUCCESS;
|
---|
2569 | }
|
---|
2570 |
|
---|
2571 | /**
|
---|
2572 | * Changes the image's UUID and parent UUIDs.
|
---|
2573 | *
|
---|
2574 | * @returns VBox status code.
|
---|
2575 | * @param pszFilename Name of the image file to operate on.
|
---|
2576 | * @param pUuid Optional parameter, new UUID of the image.
|
---|
2577 | * @param pModificationUuid Optional parameter, new modification UUID of the image.
|
---|
2578 | * @param pParentUuuid Optional parameter, new parent UUID of the image.
|
---|
2579 | * @param pParentModificationUuid Optional parameter, new parent modification UUID of the image.
|
---|
2580 | */
|
---|
2581 | VBOXDDU_DECL(int) VDISetImageUUIDs(const char *pszFilename,
|
---|
2582 | PCRTUUID pUuid, PCRTUUID pModificationUuid,
|
---|
2583 | PCRTUUID pParentUuid, PCRTUUID pParentModificationUuid)
|
---|
2584 | {
|
---|
2585 | LogFlow(("VDISetImageUUIDs:\n"));
|
---|
2586 |
|
---|
2587 | /* Check arguments. */
|
---|
2588 | if ( !pszFilename
|
---|
2589 | || *pszFilename == '\0')
|
---|
2590 | {
|
---|
2591 | AssertMsgFailed(("Invalid arguments: pszFilename=%p\n", pszFilename));
|
---|
2592 | return VERR_INVALID_PARAMETER;
|
---|
2593 | }
|
---|
2594 |
|
---|
2595 | PVDIIMAGEDESC pImage;
|
---|
2596 | int rc = vdiOpenImage(&pImage, pszFilename, VDI_OPEN_FLAGS_NORMAL, NULL);
|
---|
2597 | if (RT_FAILURE(rc))
|
---|
2598 | {
|
---|
2599 | Log(("VDISetImageUUIDs: vdiOpenImage rc=%Rrc filename=\"%s\"\n", rc, pszFilename));
|
---|
2600 | return rc;
|
---|
2601 | }
|
---|
2602 | if (!pImage->fReadOnly)
|
---|
2603 | {
|
---|
2604 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
2605 | {
|
---|
2606 | if (pUuid)
|
---|
2607 | pImage->Header.u.v1.uuidCreate = *pUuid;
|
---|
2608 |
|
---|
2609 | if (pModificationUuid)
|
---|
2610 | pImage->Header.u.v1.uuidModify = *pModificationUuid;
|
---|
2611 |
|
---|
2612 | if (pParentUuid)
|
---|
2613 | pImage->Header.u.v1.uuidLinkage = *pParentUuid;
|
---|
2614 |
|
---|
2615 | if (pParentModificationUuid)
|
---|
2616 | pImage->Header.u.v1.uuidParentModify = *pParentModificationUuid;
|
---|
2617 |
|
---|
2618 | /* write out new header */
|
---|
2619 | rc = vdiUpdateHeader(pImage);
|
---|
2620 | AssertMsgRC(rc, ("vdiUpdateHeader() failed, filename=\"%s\", rc=%Rrc\n",
|
---|
2621 | pImage->szFilename, rc));
|
---|
2622 | }
|
---|
2623 | /* Make it possible to clone old VDIs. */
|
---|
2624 | else if ( GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0
|
---|
2625 | && !pParentUuid
|
---|
2626 | && !pParentModificationUuid)
|
---|
2627 | {
|
---|
2628 | if (pUuid)
|
---|
2629 | pImage->Header.u.v0.uuidCreate = *pUuid;
|
---|
2630 |
|
---|
2631 | if (pModificationUuid)
|
---|
2632 | pImage->Header.u.v0.uuidModify = *pModificationUuid;
|
---|
2633 |
|
---|
2634 | /* write out new header */
|
---|
2635 | rc = vdiUpdateHeader(pImage);
|
---|
2636 | AssertMsgRC(rc, ("vdiUpdateHeader() failed, filename=\"%s\", rc=%Rrc\n",
|
---|
2637 | pImage->szFilename, rc));
|
---|
2638 | }
|
---|
2639 | else
|
---|
2640 | {
|
---|
2641 | Log(("VDISetImageUUIDs: Version is not supported!\n"));
|
---|
2642 | rc = VERR_VDI_UNSUPPORTED_VERSION;
|
---|
2643 | }
|
---|
2644 | }
|
---|
2645 | else
|
---|
2646 | {
|
---|
2647 | Log(("VDISetImageUUIDs: image \"%s\" is opened as read-only!\n", pszFilename));
|
---|
2648 | rc = VERR_VDI_IMAGE_READ_ONLY;
|
---|
2649 | }
|
---|
2650 |
|
---|
2651 | vdiCloseImage(pImage);
|
---|
2652 | return rc;
|
---|
2653 | }
|
---|
2654 |
|
---|
2655 | /**
|
---|
2656 | * Merges two images having a parent/child relationship (both directions).
|
---|
2657 | *
|
---|
2658 | * @returns VBox status code.
|
---|
2659 | * @param pszFilenameFrom Name of the image file to merge from.
|
---|
2660 | * @param pszFilenameTo Name of the image file to merge into.
|
---|
2661 | * @param pfnProgress Progress callback. Optional. NULL if not to be used.
|
---|
2662 | * @param pvUser User argument for the progress callback.
|
---|
2663 | */
|
---|
2664 | VBOXDDU_DECL(int) VDIMergeImage(const char *pszFilenameFrom, const char *pszFilenameTo,
|
---|
2665 | PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
2666 | {
|
---|
2667 | LogFlow(("VDIMergeImage:\n"));
|
---|
2668 |
|
---|
2669 | /* Check arguments. */
|
---|
2670 | if ( !pszFilenameFrom
|
---|
2671 | || *pszFilenameFrom == '\0'
|
---|
2672 | || !pszFilenameTo
|
---|
2673 | || *pszFilenameTo == '\0')
|
---|
2674 | {
|
---|
2675 | AssertMsgFailed(("Invalid arguments: pszFilenameFrom=%p, pszFilenameTo=%p\n", pszFilenameFrom, pszFilenameTo));
|
---|
2676 | return VERR_INVALID_PARAMETER;
|
---|
2677 | }
|
---|
2678 |
|
---|
2679 | PVDIIMAGEDESC pImageFrom;
|
---|
2680 | int rc = vdiOpenImage(&pImageFrom, pszFilenameFrom, VDI_OPEN_FLAGS_READONLY, NULL);
|
---|
2681 | if (RT_FAILURE(rc))
|
---|
2682 | {
|
---|
2683 | Log(("VDIMergeImage: vdiOpenImage rc=%Rrc pstFilenameFrom=\"%s\"\n", rc, pszFilenameFrom));
|
---|
2684 | return rc;
|
---|
2685 | }
|
---|
2686 |
|
---|
2687 | PVDIIMAGEDESC pImageTo;
|
---|
2688 | rc = vdiOpenImage(&pImageTo, pszFilenameTo, VDI_OPEN_FLAGS_NORMAL, NULL);
|
---|
2689 | if (RT_FAILURE(rc))
|
---|
2690 | {
|
---|
2691 | Log(("VDIMergeImage: vdiOpenImage rc=%Rrc pszFilenameTo=\"%s\"\n", rc, pszFilenameTo));
|
---|
2692 | vdiCloseImage(pImageFrom);
|
---|
2693 | return rc;
|
---|
2694 | }
|
---|
2695 | if (pImageTo->fReadOnly)
|
---|
2696 | {
|
---|
2697 | Log(("VDIMergeImage: image \"%s\" is opened as read-only!\n", pszFilenameTo));
|
---|
2698 | vdiCloseImage(pImageFrom);
|
---|
2699 | vdiCloseImage(pImageTo);
|
---|
2700 | return VERR_VDI_IMAGE_READ_ONLY;
|
---|
2701 | }
|
---|
2702 |
|
---|
2703 | /*
|
---|
2704 | * when merging, we should not update the modification uuid of the target
|
---|
2705 | * image, because from the point of view of its children, it hasn't been
|
---|
2706 | * logically changed after the successful merge.
|
---|
2707 | */
|
---|
2708 | vdiDisableLastModifiedUpdate(pImageTo);
|
---|
2709 |
|
---|
2710 | /*
|
---|
2711 | * Check in which direction we merge
|
---|
2712 | */
|
---|
2713 |
|
---|
2714 | bool bParentToChild = false;
|
---|
2715 | if ( getImageParentUUID(&pImageFrom->Header)
|
---|
2716 | && !RTUuidCompare(getImageParentUUID(&pImageFrom->Header),
|
---|
2717 | getImageCreationUUID(&pImageTo->Header))
|
---|
2718 | && !RTUuidCompare(getImageParentModificationUUID(&pImageFrom->Header),
|
---|
2719 | getImageModificationUUID(&pImageTo->Header)))
|
---|
2720 | {
|
---|
2721 | /* we merge from a child to its parent */
|
---|
2722 | }
|
---|
2723 | else
|
---|
2724 | if ( getImageParentUUID(&pImageTo->Header)
|
---|
2725 | && !RTUuidCompare(getImageParentUUID(&pImageTo->Header),
|
---|
2726 | getImageCreationUUID(&pImageFrom->Header))
|
---|
2727 | && !RTUuidCompare(getImageParentModificationUUID(&pImageTo->Header),
|
---|
2728 | getImageModificationUUID(&pImageFrom->Header)))
|
---|
2729 | {
|
---|
2730 | /* we merge from a parent to its child */
|
---|
2731 | bParentToChild = true;
|
---|
2732 | }
|
---|
2733 | else
|
---|
2734 | {
|
---|
2735 | /* the images are not related, we can't merge! */
|
---|
2736 | Log(("VDIMergeImages: images do not have a parent/child or child/parent relationship!\n"));
|
---|
2737 | rc = VERR_VDI_IMAGES_UUID_MISMATCH;
|
---|
2738 | }
|
---|
2739 |
|
---|
2740 | rc = vdiMergeImages(pImageFrom, pImageTo, bParentToChild, pfnProgress, pvUser);
|
---|
2741 |
|
---|
2742 | if (pfnProgress)
|
---|
2743 | {
|
---|
2744 | pfnProgress(NULL /* WARNING! pVM=NULL */, 100, pvUser);
|
---|
2745 | /* Note: commiting is non breakable operation, skipping rc here. */
|
---|
2746 | }
|
---|
2747 |
|
---|
2748 | /* cleanup */
|
---|
2749 | vdiCloseImage(pImageFrom);
|
---|
2750 | vdiCloseImage(pImageTo);
|
---|
2751 |
|
---|
2752 | Log(("VDIMergeImage: done, returning with rc = %Rrc\n", rc));
|
---|
2753 | return rc;
|
---|
2754 | }
|
---|
2755 |
|
---|
2756 |
|
---|
2757 | /**
|
---|
2758 | * Initialize the VDIDISK structure.
|
---|
2759 | */
|
---|
2760 | void vdiInitVDIDisk(PVDIDISK pDisk)
|
---|
2761 | {
|
---|
2762 | Assert(pDisk);
|
---|
2763 | pDisk->u32Signature = VDIDISK_SIGNATURE;
|
---|
2764 | pDisk->cImages = 0;
|
---|
2765 | pDisk->pBase = NULL;
|
---|
2766 | pDisk->pLast = NULL;
|
---|
2767 | pDisk->cbBlock = VDI_IMAGE_DEFAULT_BLOCK_SIZE;
|
---|
2768 | pDisk->cbBuf = VDIDISK_DEFAULT_BUFFER_SIZE;
|
---|
2769 | pDisk->fHonorZeroWrites = false;
|
---|
2770 | }
|
---|
2771 |
|
---|
2772 | /**
|
---|
2773 | * internal: add image structure to the end of images list.
|
---|
2774 | */
|
---|
2775 | static void vdiAddImageToList(PVDIDISK pDisk, PVDIIMAGEDESC pImage)
|
---|
2776 | {
|
---|
2777 | pImage->pPrev = NULL;
|
---|
2778 | pImage->pNext = NULL;
|
---|
2779 |
|
---|
2780 | if (pDisk->pBase)
|
---|
2781 | {
|
---|
2782 | Assert(pDisk->cImages > 0);
|
---|
2783 | pImage->pPrev = pDisk->pLast;
|
---|
2784 | pDisk->pLast->pNext = pImage;
|
---|
2785 | pDisk->pLast = pImage;
|
---|
2786 | }
|
---|
2787 | else
|
---|
2788 | {
|
---|
2789 | Assert(pDisk->cImages == 0);
|
---|
2790 | pDisk->pBase = pImage;
|
---|
2791 | pDisk->pLast = pImage;
|
---|
2792 | }
|
---|
2793 |
|
---|
2794 | pDisk->cImages++;
|
---|
2795 | }
|
---|
2796 |
|
---|
2797 | /**
|
---|
2798 | * internal: remove image structure from the images list.
|
---|
2799 | */
|
---|
2800 | static void vdiRemoveImageFromList(PVDIDISK pDisk, PVDIIMAGEDESC pImage)
|
---|
2801 | {
|
---|
2802 | Assert(pDisk->cImages > 0);
|
---|
2803 |
|
---|
2804 | if (pImage->pPrev)
|
---|
2805 | pImage->pPrev->pNext = pImage->pNext;
|
---|
2806 | else
|
---|
2807 | pDisk->pBase = pImage->pNext;
|
---|
2808 |
|
---|
2809 | if (pImage->pNext)
|
---|
2810 | pImage->pNext->pPrev = pImage->pPrev;
|
---|
2811 | else
|
---|
2812 | pDisk->pLast = pImage->pPrev;
|
---|
2813 |
|
---|
2814 | pImage->pPrev = NULL;
|
---|
2815 | pImage->pNext = NULL;
|
---|
2816 |
|
---|
2817 | pDisk->cImages--;
|
---|
2818 | }
|
---|
2819 |
|
---|
2820 | /**
|
---|
2821 | * Allocates and initializes VDI HDD container.
|
---|
2822 | *
|
---|
2823 | * @returns Pointer to newly created HDD container with no one opened image file.
|
---|
2824 | * @returns NULL on failure, typically out of memory.
|
---|
2825 | */
|
---|
2826 | VBOXDDU_DECL(PVDIDISK) VDIDiskCreate(void)
|
---|
2827 | {
|
---|
2828 | PVDIDISK pDisk = (PVDIDISK)RTMemAllocZ(sizeof(VDIDISK));
|
---|
2829 | if (pDisk)
|
---|
2830 | vdiInitVDIDisk(pDisk);
|
---|
2831 | LogFlow(("VDIDiskCreate: returns pDisk=%X\n", pDisk));
|
---|
2832 | return pDisk;
|
---|
2833 | }
|
---|
2834 |
|
---|
2835 | /**
|
---|
2836 | * Destroys VDI HDD container. If container has opened image files they will be closed.
|
---|
2837 | *
|
---|
2838 | * @param pDisk Pointer to VDI HDD container.
|
---|
2839 | */
|
---|
2840 | VBOXDDU_DECL(void) VDIDiskDestroy(PVDIDISK pDisk)
|
---|
2841 | {
|
---|
2842 | LogFlow(("VDIDiskDestroy: pDisk=%X\n", pDisk));
|
---|
2843 | /* sanity check */
|
---|
2844 | Assert(pDisk);
|
---|
2845 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
2846 |
|
---|
2847 | if (pDisk)
|
---|
2848 | {
|
---|
2849 | VDIDiskCloseAllImages(pDisk);
|
---|
2850 | RTMemFree(pDisk);
|
---|
2851 | }
|
---|
2852 | }
|
---|
2853 |
|
---|
2854 | /**
|
---|
2855 | * Get working buffer size of VDI HDD container.
|
---|
2856 | *
|
---|
2857 | * @returns Working buffer size in bytes.
|
---|
2858 | */
|
---|
2859 | VBOXDDU_DECL(unsigned) VDIDiskGetBufferSize(PVDIDISK pDisk)
|
---|
2860 | {
|
---|
2861 | /* sanity check */
|
---|
2862 | Assert(pDisk);
|
---|
2863 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
2864 |
|
---|
2865 | LogFlow(("VDIDiskGetBufferSize: returns %u\n", pDisk->cbBuf));
|
---|
2866 | return pDisk->cbBuf;
|
---|
2867 | }
|
---|
2868 |
|
---|
2869 | /**
|
---|
2870 | * Get read/write mode of VDI HDD.
|
---|
2871 | *
|
---|
2872 | * @returns Disk ReadOnly status.
|
---|
2873 | * @returns true if no one VDI image is opened in HDD container.
|
---|
2874 | */
|
---|
2875 | VBOXDDU_DECL(bool) VDIDiskIsReadOnly(PVDIDISK pDisk)
|
---|
2876 | {
|
---|
2877 | /* sanity check */
|
---|
2878 | Assert(pDisk);
|
---|
2879 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
2880 |
|
---|
2881 | if (pDisk->pLast)
|
---|
2882 | {
|
---|
2883 | LogFlow(("VDIDiskIsReadOnly: returns %u\n", pDisk->pLast->fReadOnly));
|
---|
2884 | return pDisk->pLast->fReadOnly;
|
---|
2885 | }
|
---|
2886 |
|
---|
2887 | AssertMsgFailed(("No disk image is opened!\n"));
|
---|
2888 | return true;
|
---|
2889 | }
|
---|
2890 |
|
---|
2891 | /**
|
---|
2892 | * Get disk size of VDI HDD container.
|
---|
2893 | *
|
---|
2894 | * @returns Virtual disk size in bytes.
|
---|
2895 | * @returns 0 if no one VDI image is opened in HDD container.
|
---|
2896 | */
|
---|
2897 | VBOXDDU_DECL(uint64_t) VDIDiskGetSize(PVDIDISK pDisk)
|
---|
2898 | {
|
---|
2899 | /* sanity check */
|
---|
2900 | Assert(pDisk);
|
---|
2901 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
2902 |
|
---|
2903 | if (pDisk->pBase)
|
---|
2904 | {
|
---|
2905 | LogFlow(("VDIDiskGetSize: returns %llu\n", getImageDiskSize(&pDisk->pBase->Header)));
|
---|
2906 | return getImageDiskSize(&pDisk->pBase->Header);
|
---|
2907 | }
|
---|
2908 |
|
---|
2909 | AssertMsgFailed(("No disk image is opened!\n"));
|
---|
2910 | return 0;
|
---|
2911 | }
|
---|
2912 |
|
---|
2913 | /**
|
---|
2914 | * Get block size of VDI HDD container.
|
---|
2915 | *
|
---|
2916 | * @returns VDI image block size in bytes.
|
---|
2917 | * @returns 0 if no one VDI image is opened in HDD container.
|
---|
2918 | */
|
---|
2919 | VBOXDDU_DECL(unsigned) VDIDiskGetBlockSize(PVDIDISK pDisk)
|
---|
2920 | {
|
---|
2921 | /* sanity check */
|
---|
2922 | Assert(pDisk);
|
---|
2923 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
2924 |
|
---|
2925 | if (pDisk->pBase)
|
---|
2926 | {
|
---|
2927 | LogFlow(("VDIDiskGetBlockSize: returns %u\n", getImageBlockSize(&pDisk->pBase->Header)));
|
---|
2928 | return getImageBlockSize(&pDisk->pBase->Header);
|
---|
2929 | }
|
---|
2930 |
|
---|
2931 | AssertMsgFailed(("No disk image is opened!\n"));
|
---|
2932 | return 0;
|
---|
2933 | }
|
---|
2934 |
|
---|
2935 | /**
|
---|
2936 | * Get virtual disk LCHS geometry stored in image file.
|
---|
2937 | *
|
---|
2938 | * @returns VBox status code.
|
---|
2939 | * @returns VERR_VDI_NOT_OPENED if no one VDI image is opened in HDD container.
|
---|
2940 | * @returns VERR_VDI_GEOMETRY_NOT_SET if no geometry has been setted.
|
---|
2941 | * @param pDisk Pointer to VDI HDD container.
|
---|
2942 | * @param pLCHSGeometry Where to store LCHS geometry. Not NULL.
|
---|
2943 | */
|
---|
2944 | VBOXDDU_DECL(int) VDIDiskGetLCHSGeometry(PVDIDISK pDisk, PPDMMEDIAGEOMETRY pLCHSGeometry)
|
---|
2945 | {
|
---|
2946 | /* sanity check */
|
---|
2947 | Assert(pDisk);
|
---|
2948 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
2949 |
|
---|
2950 | if (pDisk->pBase)
|
---|
2951 | {
|
---|
2952 | int rc = VINF_SUCCESS;
|
---|
2953 | VDIDISKGEOMETRY DummyGeo = { 0, 0, 0, VDI_GEOMETRY_SECTOR_SIZE };
|
---|
2954 | PVDIDISKGEOMETRY pGeometry = getImageLCHSGeometry(&pDisk->pBase->Header);
|
---|
2955 | if (!pGeometry)
|
---|
2956 | pGeometry = &DummyGeo;
|
---|
2957 |
|
---|
2958 | LogFlow(("%s: C/H/S = %u/%u/%u\n",
|
---|
2959 | __FUNCTION__, pGeometry->cCylinders, pGeometry->cHeads, pGeometry->cSectors));
|
---|
2960 | if ( pGeometry->cCylinders > 0
|
---|
2961 | && pGeometry->cHeads > 0
|
---|
2962 | && pGeometry->cSectors > 0)
|
---|
2963 | {
|
---|
2964 | pLCHSGeometry->cCylinders = pGeometry->cCylinders;
|
---|
2965 | pLCHSGeometry->cHeads = pGeometry->cHeads;
|
---|
2966 | pLCHSGeometry->cSectors = pGeometry->cSectors;
|
---|
2967 | }
|
---|
2968 | else
|
---|
2969 | rc = VERR_VDI_GEOMETRY_NOT_SET;
|
---|
2970 |
|
---|
2971 | LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
|
---|
2972 | return rc;
|
---|
2973 | }
|
---|
2974 |
|
---|
2975 | AssertMsgFailed(("No disk image is opened!\n"));
|
---|
2976 | return VERR_VDI_NOT_OPENED;
|
---|
2977 | }
|
---|
2978 |
|
---|
2979 | /**
|
---|
2980 | * Store virtual disk LCHS geometry into base image file of HDD container.
|
---|
2981 | *
|
---|
2982 | * Note that in case of unrecoverable error all images of HDD container will be closed.
|
---|
2983 | *
|
---|
2984 | * @returns VBox status code.
|
---|
2985 | * @returns VERR_VDI_NOT_OPENED if no one VDI image is opened in HDD container.
|
---|
2986 | * @param pDisk Pointer to VDI HDD container.
|
---|
2987 | * @param pLCHSGeometry Where to load LCHS geometry from. Not NULL.
|
---|
2988 | */
|
---|
2989 | VBOXDDU_DECL(int) VDIDiskSetLCHSGeometry(PVDIDISK pDisk, PCPDMMEDIAGEOMETRY pLCHSGeometry)
|
---|
2990 | {
|
---|
2991 | LogFlow(("%s: C/H/S = %u/%u/%u\n", __FUNCTION__, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
|
---|
2992 | /* sanity check */
|
---|
2993 | Assert(pDisk);
|
---|
2994 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
2995 |
|
---|
2996 | if (pDisk->pBase)
|
---|
2997 | {
|
---|
2998 | int rc = VINF_SUCCESS;
|
---|
2999 | PVDIDISKGEOMETRY pGeometry = getImageLCHSGeometry(&pDisk->pBase->Header);
|
---|
3000 | if (pGeometry)
|
---|
3001 | {
|
---|
3002 | pGeometry->cCylinders = pLCHSGeometry->cCylinders;
|
---|
3003 | pGeometry->cHeads = pLCHSGeometry->cHeads;
|
---|
3004 | pGeometry->cSectors = pLCHSGeometry->cSectors;
|
---|
3005 | pGeometry->cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
3006 |
|
---|
3007 | /* Update header information in base image file. */
|
---|
3008 | rc = vdiUpdateReadOnlyHeader(pDisk->pBase);
|
---|
3009 | }
|
---|
3010 | LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
|
---|
3011 | return rc;
|
---|
3012 | }
|
---|
3013 |
|
---|
3014 | AssertMsgFailed(("No disk image is opened!\n"));
|
---|
3015 | return VERR_VDI_NOT_OPENED;
|
---|
3016 | }
|
---|
3017 |
|
---|
3018 | /**
|
---|
3019 | * Get number of opened images in HDD container.
|
---|
3020 | *
|
---|
3021 | * @returns Number of opened images for HDD container. 0 if no images is opened.
|
---|
3022 | * @param pDisk Pointer to VDI HDD container.
|
---|
3023 | */
|
---|
3024 | VBOXDDU_DECL(int) VDIDiskGetImagesCount(PVDIDISK pDisk)
|
---|
3025 | {
|
---|
3026 | /* sanity check */
|
---|
3027 | Assert(pDisk);
|
---|
3028 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3029 |
|
---|
3030 | LogFlow(("VDIDiskGetImagesCount: returns %d\n", pDisk->cImages));
|
---|
3031 | return pDisk->cImages;
|
---|
3032 | }
|
---|
3033 |
|
---|
3034 | static PVDIIMAGEDESC vdiGetImageByNumber(PVDIDISK pDisk, int nImage)
|
---|
3035 | {
|
---|
3036 | PVDIIMAGEDESC pImage = pDisk->pBase;
|
---|
3037 | while (pImage && nImage)
|
---|
3038 | {
|
---|
3039 | pImage = pImage->pNext;
|
---|
3040 | nImage--;
|
---|
3041 | }
|
---|
3042 | return pImage;
|
---|
3043 | }
|
---|
3044 |
|
---|
3045 | /**
|
---|
3046 | * Get version of opened image of HDD container.
|
---|
3047 | *
|
---|
3048 | * @returns VBox status code.
|
---|
3049 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3050 | * @param pDisk Pointer to VDI HDD container.
|
---|
3051 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
3052 | * @param puVersion Where to store the image version.
|
---|
3053 | */
|
---|
3054 | VBOXDDU_DECL(int) VDIDiskGetImageVersion(PVDIDISK pDisk, int nImage, unsigned *puVersion)
|
---|
3055 | {
|
---|
3056 | /* sanity check */
|
---|
3057 | Assert(pDisk);
|
---|
3058 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3059 | Assert(puVersion);
|
---|
3060 |
|
---|
3061 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3062 | Assert(pImage);
|
---|
3063 |
|
---|
3064 | if (pImage)
|
---|
3065 | {
|
---|
3066 | *puVersion = pImage->PreHeader.u32Version;
|
---|
3067 | LogFlow(("VDIDiskGetImageVersion: returns %08X\n", pImage->PreHeader.u32Version));
|
---|
3068 | return VINF_SUCCESS;
|
---|
3069 | }
|
---|
3070 |
|
---|
3071 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3072 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3073 | }
|
---|
3074 |
|
---|
3075 | /**
|
---|
3076 | * Get filename of opened image of HDD container.
|
---|
3077 | *
|
---|
3078 | * @returns VBox status code.
|
---|
3079 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3080 | * @returns VERR_BUFFER_OVERFLOW if pszFilename buffer too small to hold filename.
|
---|
3081 | * @param pDisk Pointer to VDI HDD container.
|
---|
3082 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
3083 | * @param pszFilename Where to store the image file name.
|
---|
3084 | * @param cbFilename Size of buffer pszFilename points to.
|
---|
3085 | */
|
---|
3086 | VBOXDDU_DECL(int) VDIDiskGetImageFilename(PVDIDISK pDisk, int nImage, char *pszFilename, unsigned cbFilename)
|
---|
3087 | {
|
---|
3088 | /* sanity check */
|
---|
3089 | Assert(pDisk);
|
---|
3090 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3091 | Assert(pszFilename);
|
---|
3092 |
|
---|
3093 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3094 | Assert(pImage);
|
---|
3095 |
|
---|
3096 | if (pImage)
|
---|
3097 | {
|
---|
3098 | size_t cb = strlen(pImage->szFilename);
|
---|
3099 | if (cb < cbFilename)
|
---|
3100 | {
|
---|
3101 | /* memcpy is much better than strncpy. */
|
---|
3102 | memcpy(pszFilename, pImage->szFilename, cb + 1);
|
---|
3103 | LogFlow(("VDIDiskGetImageFilename: returns VINF_SUCCESS, filename=\"%s\" nImage=%d\n",
|
---|
3104 | pszFilename, nImage));
|
---|
3105 | return VINF_SUCCESS;
|
---|
3106 | }
|
---|
3107 | else
|
---|
3108 | {
|
---|
3109 | AssertMsgFailed(("Out of buffer space, cbFilename=%d needed=%d\n", cbFilename, cb + 1));
|
---|
3110 | return VERR_BUFFER_OVERFLOW;
|
---|
3111 | }
|
---|
3112 | }
|
---|
3113 |
|
---|
3114 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3115 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3116 | }
|
---|
3117 |
|
---|
3118 | /**
|
---|
3119 | * Get the comment line of opened image of HDD container.
|
---|
3120 | *
|
---|
3121 | * @returns VBox status code.
|
---|
3122 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3123 | * @returns VERR_BUFFER_OVERFLOW if pszComment buffer too small to hold comment text.
|
---|
3124 | * @param pDisk Pointer to VDI HDD container.
|
---|
3125 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
3126 | * @param pszComment Where to store the comment string of image. NULL is ok.
|
---|
3127 | * @param cbComment The size of pszComment buffer. 0 is ok.
|
---|
3128 | */
|
---|
3129 | VBOXDDU_DECL(int) VDIDiskGetImageComment(PVDIDISK pDisk, int nImage, char *pszComment, unsigned cbComment)
|
---|
3130 | {
|
---|
3131 | /* sanity check */
|
---|
3132 | Assert(pDisk);
|
---|
3133 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3134 | Assert(pszComment);
|
---|
3135 |
|
---|
3136 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3137 | Assert(pImage);
|
---|
3138 |
|
---|
3139 | if (pImage)
|
---|
3140 | {
|
---|
3141 | char *pszTmp = getImageComment(&pImage->Header);
|
---|
3142 | size_t cb = strlen(pszTmp);
|
---|
3143 | if (cb < cbComment)
|
---|
3144 | {
|
---|
3145 | /* memcpy is much better than strncpy. */
|
---|
3146 | memcpy(pszComment, pszTmp, cb + 1);
|
---|
3147 | LogFlow(("VDIDiskGetImageComment: returns VINF_SUCCESS, comment=\"%s\" nImage=%d\n",
|
---|
3148 | pszTmp, nImage));
|
---|
3149 | return VINF_SUCCESS;
|
---|
3150 | }
|
---|
3151 | else
|
---|
3152 | {
|
---|
3153 | AssertMsgFailed(("Out of buffer space, cbComment=%d needed=%d\n", cbComment, cb + 1));
|
---|
3154 | return VERR_BUFFER_OVERFLOW;
|
---|
3155 | }
|
---|
3156 | }
|
---|
3157 |
|
---|
3158 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3159 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3160 | }
|
---|
3161 |
|
---|
3162 | /**
|
---|
3163 | * Get type of opened image of HDD container.
|
---|
3164 | *
|
---|
3165 | * @returns VBox status code.
|
---|
3166 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3167 | * @param pDisk Pointer to VDI HDD container.
|
---|
3168 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
3169 | * @param penmType Where to store the image type.
|
---|
3170 | */
|
---|
3171 | VBOXDDU_DECL(int) VDIDiskGetImageType(PVDIDISK pDisk, int nImage, PVDIIMAGETYPE penmType)
|
---|
3172 | {
|
---|
3173 | /* sanity check */
|
---|
3174 | Assert(pDisk);
|
---|
3175 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3176 | Assert(penmType);
|
---|
3177 |
|
---|
3178 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3179 | Assert(pImage);
|
---|
3180 |
|
---|
3181 | if (pImage)
|
---|
3182 | {
|
---|
3183 | *penmType = getImageType(&pImage->Header);
|
---|
3184 | LogFlow(("VDIDiskGetImageType: returns VINF_SUCCESS, type=%X nImage=%d\n",
|
---|
3185 | *penmType, nImage));
|
---|
3186 | return VINF_SUCCESS;
|
---|
3187 | }
|
---|
3188 |
|
---|
3189 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3190 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3191 | }
|
---|
3192 |
|
---|
3193 | /**
|
---|
3194 | * Get flags of opened image of HDD container.
|
---|
3195 | *
|
---|
3196 | * @returns VBox status code.
|
---|
3197 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3198 | * @param pDisk Pointer to VDI HDD container.
|
---|
3199 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
3200 | * @param pfFlags Where to store the image flags.
|
---|
3201 | */
|
---|
3202 | VBOXDDU_DECL(int) VDIDiskGetImageFlags(PVDIDISK pDisk, int nImage, unsigned *pfFlags)
|
---|
3203 | {
|
---|
3204 | /* sanity check */
|
---|
3205 | Assert(pDisk);
|
---|
3206 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3207 | Assert(pfFlags);
|
---|
3208 |
|
---|
3209 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3210 | Assert(pImage);
|
---|
3211 |
|
---|
3212 | if (pImage)
|
---|
3213 | {
|
---|
3214 | *pfFlags = getImageFlags(&pImage->Header);
|
---|
3215 | LogFlow(("VDIDiskGetImageFlags: returns VINF_SUCCESS, flags=%08X nImage=%d\n",
|
---|
3216 | *pfFlags, nImage));
|
---|
3217 | return VINF_SUCCESS;
|
---|
3218 | }
|
---|
3219 |
|
---|
3220 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3221 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3222 | }
|
---|
3223 |
|
---|
3224 | /**
|
---|
3225 | * Get Uuid of opened image of HDD container.
|
---|
3226 | *
|
---|
3227 | * @returns VBox status code.
|
---|
3228 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3229 | * @param pDisk Pointer to VDI HDD container.
|
---|
3230 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
3231 | * @param pUuid Where to store the image creation uuid.
|
---|
3232 | */
|
---|
3233 | VBOXDDU_DECL(int) VDIDiskGetImageUuid(PVDIDISK pDisk, int nImage, PRTUUID pUuid)
|
---|
3234 | {
|
---|
3235 | /* sanity check */
|
---|
3236 | Assert(pDisk);
|
---|
3237 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3238 | Assert(pUuid);
|
---|
3239 |
|
---|
3240 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3241 | Assert(pImage);
|
---|
3242 |
|
---|
3243 | if (pImage)
|
---|
3244 | {
|
---|
3245 | *pUuid = *getImageCreationUUID(&pImage->Header);
|
---|
3246 | LogFlow(("VDIDiskGetImageUuid: returns VINF_SUCCESS, uuid={%RTuuid} nImage=%d\n",
|
---|
3247 | pUuid, nImage));
|
---|
3248 | return VINF_SUCCESS;
|
---|
3249 | }
|
---|
3250 |
|
---|
3251 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3252 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3253 | }
|
---|
3254 |
|
---|
3255 | /**
|
---|
3256 | * Get last modification Uuid of opened image of HDD container.
|
---|
3257 | *
|
---|
3258 | * @returns VBox status code.
|
---|
3259 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3260 | * @param pDisk Pointer to VDI HDD container.
|
---|
3261 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
3262 | * @param pUuid Where to store the image modification uuid.
|
---|
3263 | */
|
---|
3264 | VBOXDDU_DECL(int) VDIDiskGetImageModificationUuid(PVDIDISK pDisk, int nImage, PRTUUID pUuid)
|
---|
3265 | {
|
---|
3266 | /* sanity check */
|
---|
3267 | Assert(pDisk);
|
---|
3268 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3269 | Assert(pUuid);
|
---|
3270 |
|
---|
3271 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3272 | Assert(pImage);
|
---|
3273 |
|
---|
3274 | if (pImage)
|
---|
3275 | {
|
---|
3276 | *pUuid = *getImageModificationUUID(&pImage->Header);
|
---|
3277 | LogFlow(("VDIDiskGetImageModificationUuid: returns VINF_SUCCESS, uuid={%RTuuid} nImage=%d\n",
|
---|
3278 | pUuid, nImage));
|
---|
3279 | return VINF_SUCCESS;
|
---|
3280 | }
|
---|
3281 |
|
---|
3282 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3283 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3284 | }
|
---|
3285 |
|
---|
3286 | /**
|
---|
3287 | * Get Uuid of opened image's parent image.
|
---|
3288 | *
|
---|
3289 | * @returns VBox status code.
|
---|
3290 | * @returns VERR_VDI_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
3291 | * @param pDisk Pointer to VDI HDD container.
|
---|
3292 | * @param nImage Image number, counts from 0. 0 is always base image of the container.
|
---|
3293 | * @param pUuid Where to store the image creation uuid.
|
---|
3294 | */
|
---|
3295 | VBOXDDU_DECL(int) VDIDiskGetParentImageUuid(PVDIDISK pDisk, int nImage, PRTUUID pUuid)
|
---|
3296 | {
|
---|
3297 | /* sanity check */
|
---|
3298 | Assert(pDisk);
|
---|
3299 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3300 | Assert(pUuid);
|
---|
3301 |
|
---|
3302 | PVDIIMAGEDESC pImage = vdiGetImageByNumber(pDisk, nImage);
|
---|
3303 | if (pImage)
|
---|
3304 | {
|
---|
3305 | *pUuid = *getImageParentUUID(&pImage->Header);
|
---|
3306 | LogFlow(("VDIDiskGetParentImageUuid: returns VINF_SUCCESS, *pUuid={%RTuuid} nImage=%d\n",
|
---|
3307 | pUuid, nImage));
|
---|
3308 | return VINF_SUCCESS;
|
---|
3309 | }
|
---|
3310 |
|
---|
3311 | AssertMsgFailed(("Image %d was not found (cImages=%d)\n", nImage, pDisk->cImages));
|
---|
3312 | return VERR_VDI_IMAGE_NOT_FOUND;
|
---|
3313 | }
|
---|
3314 |
|
---|
3315 | /**
|
---|
3316 | * Relock the image as read/write or read-only.
|
---|
3317 | */
|
---|
3318 | int vdiChangeImageMode(PVDIIMAGEDESC pImage, bool fReadOnly)
|
---|
3319 | {
|
---|
3320 | Assert(pImage);
|
---|
3321 |
|
---|
3322 | if ( !fReadOnly
|
---|
3323 | && pImage->fOpen & VDI_OPEN_FLAGS_READONLY)
|
---|
3324 | {
|
---|
3325 | /* Can't switch read-only opened image to read-write mode. */
|
---|
3326 | Log(("vdiChangeImageMode: can't switch r/o image to r/w mode, filename=\"%s\" fOpen=%X\n",
|
---|
3327 | pImage->szFilename, pImage->fOpen));
|
---|
3328 | return VERR_VDI_IMAGE_READ_ONLY;
|
---|
3329 | }
|
---|
3330 |
|
---|
3331 | /* Flush last image changes if was r/w mode. */
|
---|
3332 | VDIFlushImage(pImage);
|
---|
3333 |
|
---|
3334 | #ifdef RT_OS_WINDOWS
|
---|
3335 | int rc = VINF_SUCCESS;
|
---|
3336 | #else
|
---|
3337 | /* Change image locking. */
|
---|
3338 | uint64_t cbLock = pImage->offStartData
|
---|
3339 | + ((uint64_t)getImageBlocks(&pImage->Header) << pImage->uShiftIndex2Offset);
|
---|
3340 | int rc = RTFileChangeLock(pImage->File,
|
---|
3341 | (fReadOnly) ?
|
---|
3342 | RTFILE_LOCK_READ | RTFILE_LOCK_IMMEDIATELY :
|
---|
3343 | RTFILE_LOCK_WRITE | RTFILE_LOCK_IMMEDIATELY,
|
---|
3344 | 0,
|
---|
3345 | cbLock);
|
---|
3346 | #endif
|
---|
3347 | if (RT_SUCCESS(rc))
|
---|
3348 | {
|
---|
3349 | pImage->fReadOnly = fReadOnly;
|
---|
3350 | Log(("vdiChangeImageMode: Image \"%s\" mode changed to %s\n",
|
---|
3351 | pImage->szFilename, (pImage->fReadOnly) ? "read-only" : "read/write"));
|
---|
3352 | return VINF_SUCCESS;
|
---|
3353 | }
|
---|
3354 |
|
---|
3355 | #ifndef RT_OS_WINDOWS
|
---|
3356 | /* Check for the most bad error in the world. Damn! It must never happens in real life! */
|
---|
3357 | if (rc == VERR_FILE_LOCK_LOST)
|
---|
3358 | {
|
---|
3359 | /* And what we can do now?! */
|
---|
3360 | AssertMsgFailed(("Image lock has been lost for file=\"%s\"", pImage->szFilename));
|
---|
3361 | Log(("vdiChangeImageMode: image lock has been lost for file=\"%s\", blocking on r/o lock wait",
|
---|
3362 | pImage->szFilename));
|
---|
3363 |
|
---|
3364 | /* Try to obtain read lock in blocking mode. Maybe it's a very bad method. */
|
---|
3365 | rc = RTFileLock(pImage->File, RTFILE_LOCK_READ | RTFILE_LOCK_WAIT, 0, cbLock);
|
---|
3366 | AssertReleaseRC(rc);
|
---|
3367 |
|
---|
3368 | pImage->fReadOnly = false;
|
---|
3369 | if (pImage->fReadOnly != fReadOnly)
|
---|
3370 | rc = VERR_FILE_LOCK_VIOLATION;
|
---|
3371 | }
|
---|
3372 | #endif
|
---|
3373 |
|
---|
3374 | Log(("vdiChangeImageMode: Image \"%s\" mode change failed with rc=%Rrc, mode is %s\n",
|
---|
3375 | pImage->szFilename, rc, (pImage->fReadOnly) ? "read-only" : "read/write"));
|
---|
3376 |
|
---|
3377 | return rc;
|
---|
3378 | }
|
---|
3379 |
|
---|
3380 | /**
|
---|
3381 | * internal: try to save header in image file even if image is in read-only mode.
|
---|
3382 | */
|
---|
3383 | static int vdiUpdateReadOnlyHeader(PVDIIMAGEDESC pImage)
|
---|
3384 | {
|
---|
3385 | int rc = VINF_SUCCESS;
|
---|
3386 |
|
---|
3387 | if (pImage->fReadOnly)
|
---|
3388 | {
|
---|
3389 | rc = vdiChangeImageMode(pImage, false);
|
---|
3390 | if (RT_SUCCESS(rc))
|
---|
3391 | {
|
---|
3392 | VDIFlushImage(pImage);
|
---|
3393 | rc = vdiChangeImageMode(pImage, true);
|
---|
3394 | AssertReleaseRC(rc);
|
---|
3395 | }
|
---|
3396 | }
|
---|
3397 | else
|
---|
3398 | VDIFlushImage(pImage);
|
---|
3399 |
|
---|
3400 | return rc;
|
---|
3401 | }
|
---|
3402 |
|
---|
3403 | /**
|
---|
3404 | * Opens an image file.
|
---|
3405 | *
|
---|
3406 | * The first opened image file in a HDD container must have a base image type,
|
---|
3407 | * others (next opened images) must be a differencing or undo images.
|
---|
3408 | * Linkage is checked for differencing image to be in consistence with the previously opened image.
|
---|
3409 | * When a next differencing image is opened and the last image was opened in read/write access
|
---|
3410 | * mode, then the last image is reopened in read-only with deny write sharing mode. This allows
|
---|
3411 | * other processes to use images in read-only mode too.
|
---|
3412 | *
|
---|
3413 | * Note that the image can be opened in read-only mode if a read/write open is not possible.
|
---|
3414 | * Use VDIDiskIsReadOnly to check open mode.
|
---|
3415 | *
|
---|
3416 | * @returns VBox status code.
|
---|
3417 | * @param pDisk Pointer to VDI HDD container.
|
---|
3418 | * @param pszFilename Name of the image file to open.
|
---|
3419 | * @param fOpen Image file open mode, see VDI_OPEN_FLAGS_* constants.
|
---|
3420 | */
|
---|
3421 | VBOXDDU_DECL(int) VDIDiskOpenImage(PVDIDISK pDisk, const char *pszFilename, unsigned fOpen)
|
---|
3422 | {
|
---|
3423 | /* sanity check */
|
---|
3424 | Assert(pDisk);
|
---|
3425 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3426 |
|
---|
3427 | /* Check arguments. */
|
---|
3428 | if ( !pszFilename
|
---|
3429 | || *pszFilename == '\0'
|
---|
3430 | || (fOpen & ~VDI_OPEN_FLAGS_MASK))
|
---|
3431 | {
|
---|
3432 | AssertMsgFailed(("Invalid arguments: pszFilename=%p fOpen=%x\n", pszFilename, fOpen));
|
---|
3433 | return VERR_INVALID_PARAMETER;
|
---|
3434 | }
|
---|
3435 | LogFlow(("VDIDiskOpenImage: pszFilename=\"%s\" fOpen=%X\n", pszFilename, fOpen));
|
---|
3436 |
|
---|
3437 | PVDIIMAGEDESC pImage;
|
---|
3438 | int rc = vdiOpenImage(&pImage, pszFilename, fOpen, pDisk->pLast);
|
---|
3439 | if (RT_SUCCESS(rc))
|
---|
3440 | {
|
---|
3441 | if (pDisk->pLast)
|
---|
3442 | {
|
---|
3443 | /* Opening differencing image. */
|
---|
3444 | if (!pDisk->pLast->fReadOnly)
|
---|
3445 | {
|
---|
3446 | /*
|
---|
3447 | * Previous image is opened in read/write mode -> switch it into read-only.
|
---|
3448 | */
|
---|
3449 | rc = vdiChangeImageMode(pDisk->pLast, true);
|
---|
3450 | }
|
---|
3451 | }
|
---|
3452 | else
|
---|
3453 | {
|
---|
3454 | /* Opening base image, check its type. */
|
---|
3455 | if ( getImageType(&pImage->Header) != VDI_IMAGE_TYPE_NORMAL
|
---|
3456 | && getImageType(&pImage->Header) != VDI_IMAGE_TYPE_FIXED)
|
---|
3457 | {
|
---|
3458 | rc = VERR_VDI_INVALID_TYPE;
|
---|
3459 | }
|
---|
3460 | }
|
---|
3461 |
|
---|
3462 | if (RT_SUCCESS(rc))
|
---|
3463 | vdiAddImageToList(pDisk, pImage);
|
---|
3464 | else
|
---|
3465 | vdiCloseImage(pImage);
|
---|
3466 | }
|
---|
3467 |
|
---|
3468 | LogFlow(("VDIDiskOpenImage: returns %Rrc\n", rc));
|
---|
3469 | return rc;
|
---|
3470 | }
|
---|
3471 |
|
---|
3472 | /**
|
---|
3473 | * Closes the last opened image file in the HDD container. Leaves all changes inside it.
|
---|
3474 | * If previous image file was opened in read-only mode (that is normal) and closing image
|
---|
3475 | * was opened in read-write mode (the whole disk was in read-write mode) - the previous image
|
---|
3476 | * will be reopened in read/write mode.
|
---|
3477 | *
|
---|
3478 | * @param pDisk Pointer to VDI HDD container.
|
---|
3479 | */
|
---|
3480 | VBOXDDU_DECL(void) VDIDiskCloseImage(PVDIDISK pDisk)
|
---|
3481 | {
|
---|
3482 | /* sanity check */
|
---|
3483 | Assert(pDisk);
|
---|
3484 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3485 |
|
---|
3486 | PVDIIMAGEDESC pImage = pDisk->pLast;
|
---|
3487 | if (pImage)
|
---|
3488 | {
|
---|
3489 | LogFlow(("VDIDiskCloseImage: closing image \"%s\"\n", pImage->szFilename));
|
---|
3490 |
|
---|
3491 | bool fWasReadOnly = pImage->fReadOnly;
|
---|
3492 | vdiRemoveImageFromList(pDisk, pImage);
|
---|
3493 | vdiCloseImage(pImage);
|
---|
3494 |
|
---|
3495 | if ( !fWasReadOnly
|
---|
3496 | && pDisk->pLast
|
---|
3497 | && pDisk->pLast->fReadOnly
|
---|
3498 | && !(pDisk->pLast->fOpen & VDI_OPEN_FLAGS_READONLY))
|
---|
3499 | {
|
---|
3500 | /*
|
---|
3501 | * Closed image was opened in read/write mode, previous image was opened
|
---|
3502 | * in read-only mode, try to switch it into read/write.
|
---|
3503 | */
|
---|
3504 | int rc = vdiChangeImageMode(pDisk->pLast, false);
|
---|
3505 | NOREF(rc); /* gcc still hates unused variables... */
|
---|
3506 | }
|
---|
3507 |
|
---|
3508 | return;
|
---|
3509 | }
|
---|
3510 | AssertMsgFailed(("No images to close\n"));
|
---|
3511 | }
|
---|
3512 |
|
---|
3513 | /**
|
---|
3514 | * Closes all opened image files in HDD container.
|
---|
3515 | *
|
---|
3516 | * @param pDisk Pointer to VDI HDD container.
|
---|
3517 | */
|
---|
3518 | VBOXDDU_DECL(void) VDIDiskCloseAllImages(PVDIDISK pDisk)
|
---|
3519 | {
|
---|
3520 | LogFlow(("VDIDiskCloseAllImages:\n"));
|
---|
3521 | /* sanity check */
|
---|
3522 | Assert(pDisk);
|
---|
3523 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3524 |
|
---|
3525 | PVDIIMAGEDESC pImage = pDisk->pLast;
|
---|
3526 | while (pImage)
|
---|
3527 | {
|
---|
3528 | PVDIIMAGEDESC pPrev = pImage->pPrev;
|
---|
3529 | vdiRemoveImageFromList(pDisk, pImage);
|
---|
3530 | vdiCloseImage(pImage);
|
---|
3531 | pImage = pPrev;
|
---|
3532 | }
|
---|
3533 | Assert(pDisk->pLast == NULL);
|
---|
3534 | }
|
---|
3535 |
|
---|
3536 | /**
|
---|
3537 | * Commits last opened differencing/undo image file of HDD container to previous one.
|
---|
3538 | * If previous image file was opened in read-only mode (that must be always so) it is reopened
|
---|
3539 | * as read/write to do commit operation.
|
---|
3540 | * After successfull commit the previous image file again reopened in read-only mode, last opened
|
---|
3541 | * image file is cleared of data and remains open and active in HDD container.
|
---|
3542 | * If you want to delete image after commit you must do it manually by VDIDiskCloseImage and
|
---|
3543 | * VDIDeleteImage calls.
|
---|
3544 | *
|
---|
3545 | * Note that in case of unrecoverable error all images of HDD container will be closed.
|
---|
3546 | *
|
---|
3547 | * @returns VBox status code.
|
---|
3548 | * @param pDisk Pointer to VDI HDD container.
|
---|
3549 | * @param pfnProgress Progress callback. Optional.
|
---|
3550 | * @param pvUser User argument for the progress callback.
|
---|
3551 | * @remark Only used by tstVDI.
|
---|
3552 | */
|
---|
3553 | VBOXDDU_DECL(int) VDIDiskCommitLastDiff(PVDIDISK pDisk, PFNVMPROGRESS pfnProgress, void *pvUser)
|
---|
3554 | {
|
---|
3555 | LogFlow(("VDIDiskCommitLastDiff:\n"));
|
---|
3556 | /* sanity check */
|
---|
3557 | Assert(pDisk);
|
---|
3558 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3559 |
|
---|
3560 | int rc = VINF_SUCCESS;
|
---|
3561 | PVDIIMAGEDESC pImage = pDisk->pLast;
|
---|
3562 | if (!pImage)
|
---|
3563 | {
|
---|
3564 | AssertMsgFailed(("No disk image is opened!\n"));
|
---|
3565 | return VERR_VDI_NOT_OPENED;
|
---|
3566 | }
|
---|
3567 |
|
---|
3568 | if (pImage->fReadOnly)
|
---|
3569 | {
|
---|
3570 | AssertMsgFailed(("Image \"%s\" is read-only!\n", pImage->szFilename));
|
---|
3571 | return VERR_VDI_IMAGE_READ_ONLY;
|
---|
3572 | }
|
---|
3573 |
|
---|
3574 | if (!pImage->pPrev)
|
---|
3575 | {
|
---|
3576 | AssertMsgFailed(("No images to commit to!\n"));
|
---|
3577 | return VERR_VDI_NO_DIFF_IMAGES;
|
---|
3578 | }
|
---|
3579 |
|
---|
3580 | bool fWasReadOnly = pImage->pPrev->fReadOnly;
|
---|
3581 | if (fWasReadOnly)
|
---|
3582 | {
|
---|
3583 | /* Change previous image mode to r/w. */
|
---|
3584 | rc = vdiChangeImageMode(pImage->pPrev, false);
|
---|
3585 | if (RT_FAILURE(rc))
|
---|
3586 | {
|
---|
3587 | Log(("VDIDiskCommitLastDiff: can't switch previous image into r/w mode, rc=%Rrc\n", rc));
|
---|
3588 | return rc;
|
---|
3589 | }
|
---|
3590 | }
|
---|
3591 |
|
---|
3592 | rc = vdiCommitToImage(pDisk, pImage->pPrev, pfnProgress, pvUser);
|
---|
3593 | if (RT_SUCCESS(rc) && fWasReadOnly)
|
---|
3594 | {
|
---|
3595 | /* Change previous image mode back to r/o. */
|
---|
3596 | rc = vdiChangeImageMode(pImage->pPrev, true);
|
---|
3597 | }
|
---|
3598 |
|
---|
3599 | if (RT_FAILURE(rc))
|
---|
3600 | {
|
---|
3601 | /* Failed! Close all images, can't work with VHDD at all. */
|
---|
3602 | VDIDiskCloseAllImages(pDisk);
|
---|
3603 | AssertMsgFailed(("Fatal: commit failed, rc=%Rrc\n", rc));
|
---|
3604 | }
|
---|
3605 |
|
---|
3606 | return rc;
|
---|
3607 | }
|
---|
3608 |
|
---|
3609 | /**
|
---|
3610 | * Creates and opens a new differencing image file in HDD container.
|
---|
3611 | * See comments for VDIDiskOpenImage function about differencing images.
|
---|
3612 | *
|
---|
3613 | * @returns VBox status code.
|
---|
3614 | * @param pDisk Pointer to VDI HDD container.
|
---|
3615 | * @param pszFilename Name of the image file to create and open.
|
---|
3616 | * @param pszComment Pointer to image comment. NULL is ok.
|
---|
3617 | * @param pfnProgress Progress callback. Optional.
|
---|
3618 | * @param pvUser User argument for the progress callback.
|
---|
3619 | * @remark Only used by tstVDI.
|
---|
3620 | */
|
---|
3621 | VBOXDDU_DECL(int) VDIDiskCreateOpenDifferenceImage(PVDIDISK pDisk, const char *pszFilename,
|
---|
3622 | const char *pszComment, PFNVMPROGRESS pfnProgress,
|
---|
3623 | void *pvUser)
|
---|
3624 | {
|
---|
3625 | LogFlow(("VDIDiskCreateOpenDifferenceImage:\n"));
|
---|
3626 | /* sanity check */
|
---|
3627 | Assert(pDisk);
|
---|
3628 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3629 | Assert(pszFilename);
|
---|
3630 |
|
---|
3631 | if (!pDisk->pLast)
|
---|
3632 | {
|
---|
3633 | AssertMsgFailed(("No disk image is opened!\n"));
|
---|
3634 | return VERR_VDI_NOT_OPENED;
|
---|
3635 | }
|
---|
3636 |
|
---|
3637 | /* Flush last parent image changes if possible. */
|
---|
3638 | VDIFlushImage(pDisk->pLast);
|
---|
3639 |
|
---|
3640 | int rc = vdiCreateImage(pszFilename,
|
---|
3641 | VDI_IMAGE_TYPE_DIFF,
|
---|
3642 | VDI_IMAGE_FLAGS_DEFAULT,
|
---|
3643 | getImageDiskSize(&pDisk->pLast->Header),
|
---|
3644 | pszComment,
|
---|
3645 | pDisk->pLast,
|
---|
3646 | pfnProgress, pvUser);
|
---|
3647 | if (RT_SUCCESS(rc))
|
---|
3648 | {
|
---|
3649 | rc = VDIDiskOpenImage(pDisk, pszFilename, VDI_OPEN_FLAGS_NORMAL);
|
---|
3650 | if (RT_FAILURE(rc))
|
---|
3651 | VDIDeleteImage(pszFilename);
|
---|
3652 | }
|
---|
3653 | LogFlow(("VDIDiskCreateOpenDifferenceImage: returns %Rrc, filename=\"%s\"\n", rc, pszFilename));
|
---|
3654 | return rc;
|
---|
3655 | }
|
---|
3656 |
|
---|
3657 | /**
|
---|
3658 | * internal: debug image dump.
|
---|
3659 | *
|
---|
3660 | * @remark Only used by tstVDI.
|
---|
3661 | */
|
---|
3662 | static void vdiDumpImage(PVDIIMAGEDESC pImage)
|
---|
3663 | {
|
---|
3664 | RTLogPrintf("Dumping VDI image \"%s\" mode=%s fOpen=%X File=%08X\n",
|
---|
3665 | pImage->szFilename,
|
---|
3666 | (pImage->fReadOnly) ? "r/o" : "r/w",
|
---|
3667 | pImage->fOpen,
|
---|
3668 | pImage->File);
|
---|
3669 | RTLogPrintf("Header: Version=%08X Type=%X Flags=%X Size=%llu\n",
|
---|
3670 | pImage->PreHeader.u32Version,
|
---|
3671 | getImageType(&pImage->Header),
|
---|
3672 | getImageFlags(&pImage->Header),
|
---|
3673 | getImageDiskSize(&pImage->Header));
|
---|
3674 | RTLogPrintf("Header: cbBlock=%u cbBlockExtra=%u cBlocks=%u cBlocksAllocated=%u\n",
|
---|
3675 | getImageBlockSize(&pImage->Header),
|
---|
3676 | getImageExtraBlockSize(&pImage->Header),
|
---|
3677 | getImageBlocks(&pImage->Header),
|
---|
3678 | getImageBlocksAllocated(&pImage->Header));
|
---|
3679 | RTLogPrintf("Header: offBlocks=%u offData=%u\n",
|
---|
3680 | getImageBlocksOffset(&pImage->Header),
|
---|
3681 | getImageDataOffset(&pImage->Header));
|
---|
3682 | PVDIDISKGEOMETRY pg = getImageLCHSGeometry(&pImage->Header);
|
---|
3683 | if (pg)
|
---|
3684 | RTLogPrintf("Header: Geometry: C/H/S=%u/%u/%u cbSector=%u\n",
|
---|
3685 | pg->cCylinders, pg->cHeads, pg->cSectors, pg->cbSector);
|
---|
3686 | RTLogPrintf("Header: uuidCreation={%RTuuid}\n", getImageCreationUUID(&pImage->Header));
|
---|
3687 | RTLogPrintf("Header: uuidModification={%RTuuid}\n", getImageModificationUUID(&pImage->Header));
|
---|
3688 | RTLogPrintf("Header: uuidParent={%RTuuid}\n", getImageParentUUID(&pImage->Header));
|
---|
3689 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) >= 1)
|
---|
3690 | RTLogPrintf("Header: uuidParentModification={%RTuuid}\n", getImageParentModificationUUID(&pImage->Header));
|
---|
3691 | RTLogPrintf("Image: fFlags=%08X offStartBlocks=%u offStartData=%u\n",
|
---|
3692 | pImage->fFlags, pImage->offStartBlocks, pImage->offStartData);
|
---|
3693 | RTLogPrintf("Image: uBlockMask=%08X uShiftIndex2Offset=%u uShiftOffset2Index=%u offStartBlockData=%u\n",
|
---|
3694 | pImage->uBlockMask,
|
---|
3695 | pImage->uShiftIndex2Offset,
|
---|
3696 | pImage->uShiftOffset2Index,
|
---|
3697 | pImage->offStartBlockData);
|
---|
3698 |
|
---|
3699 | unsigned uBlock, cBlocksNotFree, cBadBlocks, cBlocks = getImageBlocks(&pImage->Header);
|
---|
3700 | for (uBlock=0, cBlocksNotFree=0, cBadBlocks=0; uBlock<cBlocks; uBlock++)
|
---|
3701 | {
|
---|
3702 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
|
---|
3703 | {
|
---|
3704 | cBlocksNotFree++;
|
---|
3705 | if (pImage->paBlocks[uBlock] >= cBlocks)
|
---|
3706 | cBadBlocks++;
|
---|
3707 | }
|
---|
3708 | }
|
---|
3709 | if (cBlocksNotFree != getImageBlocksAllocated(&pImage->Header))
|
---|
3710 | {
|
---|
3711 | RTLogPrintf("!! WARNING: %u blocks actually allocated (cBlocksAllocated=%u) !!\n",
|
---|
3712 | cBlocksNotFree, getImageBlocksAllocated(&pImage->Header));
|
---|
3713 | }
|
---|
3714 | if (cBadBlocks)
|
---|
3715 | {
|
---|
3716 | RTLogPrintf("!! WARNING: %u bad blocks found !!\n",
|
---|
3717 | cBadBlocks);
|
---|
3718 | }
|
---|
3719 | }
|
---|
3720 |
|
---|
3721 | /**
|
---|
3722 | * Debug helper - dumps all opened images of HDD container into the log file.
|
---|
3723 | *
|
---|
3724 | * @param pDisk Pointer to VDI HDD container.
|
---|
3725 | * @remark Only used by tstVDI and vditool
|
---|
3726 | */
|
---|
3727 | VBOXDDU_DECL(void) VDIDiskDumpImages(PVDIDISK pDisk)
|
---|
3728 | {
|
---|
3729 | /* sanity check */
|
---|
3730 | Assert(pDisk);
|
---|
3731 | AssertMsg(pDisk->u32Signature == VDIDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3732 |
|
---|
3733 | RTLogPrintf("--- Dumping VDI Disk, Images=%u\n", pDisk->cImages);
|
---|
3734 | for (PVDIIMAGEDESC pImage = pDisk->pBase; pImage; pImage = pImage->pNext)
|
---|
3735 | vdiDumpImage(pImage);
|
---|
3736 | }
|
---|
3737 |
|
---|