1 | /* $Id: VDI.cpp 44528 2013-02-04 14:27:54Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Virtual Disk Image (VDI), Core Code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2013 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*******************************************************************************
|
---|
19 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 | #define LOG_GROUP LOG_GROUP_VD_VDI
|
---|
22 | #include <VBox/vd-plugin.h>
|
---|
23 | #include "VDICore.h"
|
---|
24 | #include <VBox/err.h>
|
---|
25 |
|
---|
26 | #include <VBox/log.h>
|
---|
27 | #include <iprt/alloc.h>
|
---|
28 | #include <iprt/assert.h>
|
---|
29 | #include <iprt/uuid.h>
|
---|
30 | #include <iprt/string.h>
|
---|
31 | #include <iprt/asm.h>
|
---|
32 |
|
---|
33 | #define VDI_IMAGE_DEFAULT_BLOCK_SIZE _1M
|
---|
34 |
|
---|
35 | /** Macros for endianess conversion. */
|
---|
36 | #define SET_ENDIAN_U32(conv, u32) (conv == VDIECONV_H2F ? RT_H2LE_U32(u32) : RT_LE2H_U32(u32))
|
---|
37 | #define SET_ENDIAN_U64(conv, u64) (conv == VDIECONV_H2F ? RT_H2LE_U64(u64) : RT_LE2H_U64(u64))
|
---|
38 |
|
---|
39 | /*******************************************************************************
|
---|
40 | * Static Variables *
|
---|
41 | *******************************************************************************/
|
---|
42 |
|
---|
43 | /** NULL-terminated array of supported file extensions. */
|
---|
44 | static const VDFILEEXTENSION s_aVdiFileExtensions[] =
|
---|
45 | {
|
---|
46 | {"vdi", VDTYPE_HDD},
|
---|
47 | {NULL, VDTYPE_INVALID}
|
---|
48 | };
|
---|
49 |
|
---|
50 | /*******************************************************************************
|
---|
51 | * Internal Functions *
|
---|
52 | *******************************************************************************/
|
---|
53 | static unsigned getPowerOfTwo(unsigned uNumber);
|
---|
54 | static void vdiInitPreHeader(PVDIPREHEADER pPreHdr);
|
---|
55 | static int vdiValidatePreHeader(PVDIPREHEADER pPreHdr);
|
---|
56 | static void vdiInitHeader(PVDIHEADER pHeader, uint32_t uImageFlags,
|
---|
57 | const char *pszComment, uint64_t cbDisk,
|
---|
58 | uint32_t cbBlock, uint32_t cbBlockExtra);
|
---|
59 | static int vdiValidateHeader(PVDIHEADER pHeader);
|
---|
60 | static void vdiSetupImageDesc(PVDIIMAGEDESC pImage);
|
---|
61 | static int vdiUpdateHeader(PVDIIMAGEDESC pImage);
|
---|
62 | static int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock);
|
---|
63 | static int vdiUpdateHeaderAsync(PVDIIMAGEDESC pImage, PVDIOCTX pIoCtx);
|
---|
64 | static int vdiUpdateBlockInfoAsync(PVDIIMAGEDESC pImage, unsigned uBlock, PVDIOCTX pIoCtx,
|
---|
65 | bool fUpdateHdr);
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Internal: Convert the PreHeader fields to the appropriate endianess.
|
---|
69 | * @param enmConv Direction of the conversion.
|
---|
70 | * @param pPreHdrConv Where to store the converted pre header.
|
---|
71 | * @param pPreHdr PreHeader pointer.
|
---|
72 | */
|
---|
73 | static void vdiConvPreHeaderEndianess(VDIECONV enmConv, PVDIPREHEADER pPreHdrConv,
|
---|
74 | PVDIPREHEADER pPreHdr)
|
---|
75 | {
|
---|
76 | memcpy(pPreHdrConv->szFileInfo, pPreHdr->szFileInfo, sizeof(pPreHdr->szFileInfo));
|
---|
77 | pPreHdrConv->u32Signature = SET_ENDIAN_U32(enmConv, pPreHdr->u32Signature);
|
---|
78 | pPreHdrConv->u32Version = SET_ENDIAN_U32(enmConv, pPreHdr->u32Version);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Internal: Convert the VDIDISKGEOMETRY fields to the appropriate endianess.
|
---|
83 | * @param enmConv Direction of the conversion.
|
---|
84 | * @param pDiskGeoConv Where to store the converted geometry.
|
---|
85 | * @param pDiskGeo Pointer to the disk geometry to convert.
|
---|
86 | */
|
---|
87 | static void vdiConvGeometryEndianess(VDIECONV enmConv, PVDIDISKGEOMETRY pDiskGeoConv,
|
---|
88 | PVDIDISKGEOMETRY pDiskGeo)
|
---|
89 | {
|
---|
90 | pDiskGeoConv->cCylinders = SET_ENDIAN_U32(enmConv, pDiskGeo->cCylinders);
|
---|
91 | pDiskGeoConv->cHeads = SET_ENDIAN_U32(enmConv, pDiskGeo->cHeads);
|
---|
92 | pDiskGeoConv->cSectors = SET_ENDIAN_U32(enmConv, pDiskGeo->cSectors);
|
---|
93 | pDiskGeoConv->cbSector = SET_ENDIAN_U32(enmConv, pDiskGeo->cbSector);
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Internal: Convert the Header - version 0 fields to the appropriate endianess.
|
---|
98 | * @param enmConv Direction of the conversion.
|
---|
99 | * @param pHdrConv Where to store the converted header.
|
---|
100 | * @param pHdr Pointer to the version 0 header.
|
---|
101 | */
|
---|
102 | static void vdiConvHeaderEndianessV0(VDIECONV enmConv, PVDIHEADER0 pHdrConv,
|
---|
103 | PVDIHEADER0 pHdr)
|
---|
104 | {
|
---|
105 | pHdrConv->u32Type = SET_ENDIAN_U32(enmConv, pHdr->u32Type);
|
---|
106 | pHdrConv->fFlags = SET_ENDIAN_U32(enmConv, pHdr->fFlags);
|
---|
107 | vdiConvGeometryEndianess(enmConv, &pHdrConv->LegacyGeometry, &pHdr->LegacyGeometry);
|
---|
108 | pHdrConv->cbDisk = SET_ENDIAN_U64(enmConv, pHdr->cbDisk);
|
---|
109 | pHdrConv->cbBlock = SET_ENDIAN_U32(enmConv, pHdr->cbBlock);
|
---|
110 | pHdrConv->cBlocks = SET_ENDIAN_U32(enmConv, pHdr->cBlocks);
|
---|
111 | pHdrConv->cBlocksAllocated = SET_ENDIAN_U32(enmConv, pHdr->cBlocksAllocated);
|
---|
112 | /* Don't convert the RTUUID fields. */
|
---|
113 | pHdrConv->uuidCreate = pHdr->uuidCreate;
|
---|
114 | pHdrConv->uuidModify = pHdr->uuidModify;
|
---|
115 | pHdrConv->uuidLinkage = pHdr->uuidLinkage;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Internal: Set the Header - version 1 fields to the appropriate endianess.
|
---|
120 | * @param enmConv Direction of the conversion.
|
---|
121 | * @param pHdrConv Where to store the converted header.
|
---|
122 | * @param pHdr Version 1 Header pointer.
|
---|
123 | */
|
---|
124 | static void vdiConvHeaderEndianessV1(VDIECONV enmConv, PVDIHEADER1 pHdrConv,
|
---|
125 | PVDIHEADER1 pHdr)
|
---|
126 | {
|
---|
127 | pHdrConv->cbHeader = SET_ENDIAN_U32(enmConv, pHdr->cbHeader);
|
---|
128 | pHdrConv->u32Type = SET_ENDIAN_U32(enmConv, pHdr->u32Type);
|
---|
129 | pHdrConv->fFlags = SET_ENDIAN_U32(enmConv, pHdr->fFlags);
|
---|
130 | pHdrConv->offBlocks = SET_ENDIAN_U32(enmConv, pHdr->offBlocks);
|
---|
131 | pHdrConv->offData = SET_ENDIAN_U32(enmConv, pHdr->offData);
|
---|
132 | vdiConvGeometryEndianess(enmConv, &pHdrConv->LegacyGeometry, &pHdr->LegacyGeometry);
|
---|
133 | pHdrConv->u32Dummy = SET_ENDIAN_U32(enmConv, pHdr->u32Dummy);
|
---|
134 | pHdrConv->cbDisk = SET_ENDIAN_U64(enmConv, pHdr->cbDisk);
|
---|
135 | pHdrConv->cbBlock = SET_ENDIAN_U32(enmConv, pHdr->cbBlock);
|
---|
136 | pHdrConv->cbBlockExtra = SET_ENDIAN_U32(enmConv, pHdr->cbBlockExtra);
|
---|
137 | pHdrConv->cBlocks = SET_ENDIAN_U32(enmConv, pHdr->cBlocks);
|
---|
138 | pHdrConv->cBlocksAllocated = SET_ENDIAN_U32(enmConv, pHdr->cBlocksAllocated);
|
---|
139 | /* Don't convert the RTUUID fields. */
|
---|
140 | pHdrConv->uuidCreate = pHdr->uuidCreate;
|
---|
141 | pHdrConv->uuidModify = pHdr->uuidModify;
|
---|
142 | pHdrConv->uuidLinkage = pHdr->uuidLinkage;
|
---|
143 | pHdrConv->uuidParentModify = pHdr->uuidParentModify;
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Internal: Set the Header - version 1plus fields to the appropriate endianess.
|
---|
148 | * @param enmConv Direction of the conversion.
|
---|
149 | * @param pHdrConv Where to store the converted header.
|
---|
150 | * @param pHdr Version 1+ Header pointer.
|
---|
151 | */
|
---|
152 | static void vdiConvHeaderEndianessV1p(VDIECONV enmConv, PVDIHEADER1PLUS pHdrConv,
|
---|
153 | PVDIHEADER1PLUS pHdr)
|
---|
154 | {
|
---|
155 | pHdrConv->cbHeader = SET_ENDIAN_U32(enmConv, pHdr->cbHeader);
|
---|
156 | pHdrConv->u32Type = SET_ENDIAN_U32(enmConv, pHdr->u32Type);
|
---|
157 | pHdrConv->fFlags = SET_ENDIAN_U32(enmConv, pHdr->fFlags);
|
---|
158 | pHdrConv->offBlocks = SET_ENDIAN_U32(enmConv, pHdr->offBlocks);
|
---|
159 | pHdrConv->offData = SET_ENDIAN_U32(enmConv, pHdr->offData);
|
---|
160 | vdiConvGeometryEndianess(enmConv, &pHdrConv->LegacyGeometry, &pHdr->LegacyGeometry);
|
---|
161 | pHdrConv->u32Dummy = SET_ENDIAN_U32(enmConv, pHdr->u32Dummy);
|
---|
162 | pHdrConv->cbDisk = SET_ENDIAN_U64(enmConv, pHdr->cbDisk);
|
---|
163 | pHdrConv->cbBlock = SET_ENDIAN_U32(enmConv, pHdr->cbBlock);
|
---|
164 | pHdrConv->cbBlockExtra = SET_ENDIAN_U32(enmConv, pHdr->cbBlockExtra);
|
---|
165 | pHdrConv->cBlocks = SET_ENDIAN_U32(enmConv, pHdr->cBlocks);
|
---|
166 | pHdrConv->cBlocksAllocated = SET_ENDIAN_U32(enmConv, pHdr->cBlocksAllocated);
|
---|
167 | /* Don't convert the RTUUID fields. */
|
---|
168 | pHdrConv->uuidCreate = pHdr->uuidCreate;
|
---|
169 | pHdrConv->uuidModify = pHdr->uuidModify;
|
---|
170 | pHdrConv->uuidLinkage = pHdr->uuidLinkage;
|
---|
171 | pHdrConv->uuidParentModify = pHdr->uuidParentModify;
|
---|
172 | vdiConvGeometryEndianess(enmConv, &pHdrConv->LCHSGeometry, &pHdr->LCHSGeometry);
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Internal: Set the appropriate endianess on all the Blocks pointed.
|
---|
177 | * @param enmConv Direction of the conversion.
|
---|
178 | * @param paBlocks Pointer to the block array.
|
---|
179 | * @param cEntries Number of entries in the block array.
|
---|
180 | *
|
---|
181 | * @note Unlike the other conversion functions this method does an in place conversion
|
---|
182 | * to avoid temporary memory allocations when writing the block array.
|
---|
183 | */
|
---|
184 | static void vdiConvBlocksEndianess(VDIECONV enmConv, PVDIIMAGEBLOCKPOINTER paBlocks,
|
---|
185 | unsigned cEntries)
|
---|
186 | {
|
---|
187 | for (unsigned i = 0; i < cEntries; i++)
|
---|
188 | paBlocks[i] = SET_ENDIAN_U32(enmConv, paBlocks[i]);
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Internal: Flush the image file to disk.
|
---|
193 | */
|
---|
194 | static void vdiFlushImage(PVDIIMAGEDESC pImage)
|
---|
195 | {
|
---|
196 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
197 | {
|
---|
198 | /* Save header. */
|
---|
199 | int rc = vdiUpdateHeader(pImage);
|
---|
200 | AssertMsgRC(rc, ("vdiUpdateHeader() failed, filename=\"%s\", rc=%Rrc\n",
|
---|
201 | pImage->pszFilename, rc));
|
---|
202 | vdIfIoIntFileFlushSync(pImage->pIfIo, pImage->pStorage);
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Internal: Free all allocated space for representing an image, and optionally
|
---|
208 | * delete the image from disk.
|
---|
209 | */
|
---|
210 | static int vdiFreeImage(PVDIIMAGEDESC pImage, bool fDelete)
|
---|
211 | {
|
---|
212 | int rc = VINF_SUCCESS;
|
---|
213 |
|
---|
214 | /* Freeing a never allocated image (e.g. because the open failed) is
|
---|
215 | * not signalled as an error. After all nothing bad happens. */
|
---|
216 | if (pImage)
|
---|
217 | {
|
---|
218 | if (pImage->pStorage)
|
---|
219 | {
|
---|
220 | /* No point updating the file that is deleted anyway. */
|
---|
221 | if (!fDelete)
|
---|
222 | vdiFlushImage(pImage);
|
---|
223 |
|
---|
224 | vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
|
---|
225 | pImage->pStorage = NULL;
|
---|
226 | }
|
---|
227 |
|
---|
228 | if (pImage->paBlocks)
|
---|
229 | {
|
---|
230 | RTMemFree(pImage->paBlocks);
|
---|
231 | pImage->paBlocks = NULL;
|
---|
232 | }
|
---|
233 |
|
---|
234 | if (pImage->paBlocksRev)
|
---|
235 | {
|
---|
236 | RTMemFree(pImage->paBlocksRev);
|
---|
237 | pImage->paBlocksRev = NULL;
|
---|
238 | }
|
---|
239 |
|
---|
240 | if (fDelete && pImage->pszFilename)
|
---|
241 | vdIfIoIntFileDelete(pImage->pIfIo, pImage->pszFilename);
|
---|
242 | }
|
---|
243 |
|
---|
244 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
245 | return rc;
|
---|
246 | }
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * internal: return power of 2 or 0 if num error.
|
---|
250 | */
|
---|
251 | static unsigned getPowerOfTwo(unsigned uNumber)
|
---|
252 | {
|
---|
253 | if (uNumber == 0)
|
---|
254 | return 0;
|
---|
255 | unsigned uPower2 = 0;
|
---|
256 | while ((uNumber & 1) == 0)
|
---|
257 | {
|
---|
258 | uNumber >>= 1;
|
---|
259 | uPower2++;
|
---|
260 | }
|
---|
261 | return uNumber == 1 ? uPower2 : 0;
|
---|
262 | }
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Internal: Init VDI preheader.
|
---|
266 | */
|
---|
267 | static void vdiInitPreHeader(PVDIPREHEADER pPreHdr)
|
---|
268 | {
|
---|
269 | pPreHdr->u32Signature = VDI_IMAGE_SIGNATURE;
|
---|
270 | pPreHdr->u32Version = VDI_IMAGE_VERSION;
|
---|
271 | memset(pPreHdr->szFileInfo, 0, sizeof(pPreHdr->szFileInfo));
|
---|
272 | strncat(pPreHdr->szFileInfo, VDI_IMAGE_FILE_INFO, sizeof(pPreHdr->szFileInfo)-1);
|
---|
273 | }
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Internal: check VDI preheader.
|
---|
277 | */
|
---|
278 | static int vdiValidatePreHeader(PVDIPREHEADER pPreHdr)
|
---|
279 | {
|
---|
280 | if (pPreHdr->u32Signature != VDI_IMAGE_SIGNATURE)
|
---|
281 | return VERR_VD_VDI_INVALID_HEADER;
|
---|
282 |
|
---|
283 | if ( VDI_GET_VERSION_MAJOR(pPreHdr->u32Version) != VDI_IMAGE_VERSION_MAJOR
|
---|
284 | && pPreHdr->u32Version != 0x00000002) /* old version. */
|
---|
285 | return VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
286 |
|
---|
287 | return VINF_SUCCESS;
|
---|
288 | }
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * Internal: translate VD image flags to VDI image type enum.
|
---|
292 | */
|
---|
293 | static VDIIMAGETYPE vdiTranslateImageFlags2VDI(unsigned uImageFlags)
|
---|
294 | {
|
---|
295 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
296 | return VDI_IMAGE_TYPE_FIXED;
|
---|
297 | else if (uImageFlags & VD_IMAGE_FLAGS_DIFF)
|
---|
298 | return VDI_IMAGE_TYPE_DIFF;
|
---|
299 | else
|
---|
300 | return VDI_IMAGE_TYPE_NORMAL;
|
---|
301 | }
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * Internal: translate VDI image type enum to VD image type enum.
|
---|
305 | */
|
---|
306 | static unsigned vdiTranslateVDI2ImageFlags(VDIIMAGETYPE enmType)
|
---|
307 | {
|
---|
308 | switch (enmType)
|
---|
309 | {
|
---|
310 | case VDI_IMAGE_TYPE_NORMAL:
|
---|
311 | return VD_IMAGE_FLAGS_NONE;
|
---|
312 | case VDI_IMAGE_TYPE_FIXED:
|
---|
313 | return VD_IMAGE_FLAGS_FIXED;
|
---|
314 | case VDI_IMAGE_TYPE_DIFF:
|
---|
315 | return VD_IMAGE_FLAGS_DIFF;
|
---|
316 | default:
|
---|
317 | AssertMsgFailed(("invalid VDIIMAGETYPE enmType=%d\n", (int)enmType));
|
---|
318 | return VD_IMAGE_FLAGS_NONE;
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | /**
|
---|
323 | * Internal: Init VDI header. Always use latest header version.
|
---|
324 | * @param pHeader Assumes it was initially initialized to all zeros.
|
---|
325 | */
|
---|
326 | static void vdiInitHeader(PVDIHEADER pHeader, uint32_t uImageFlags,
|
---|
327 | const char *pszComment, uint64_t cbDisk,
|
---|
328 | uint32_t cbBlock, uint32_t cbBlockExtra,
|
---|
329 | uint32_t cbDataAlign)
|
---|
330 | {
|
---|
331 | pHeader->uVersion = VDI_IMAGE_VERSION;
|
---|
332 | pHeader->u.v1plus.cbHeader = sizeof(VDIHEADER1PLUS);
|
---|
333 | pHeader->u.v1plus.u32Type = (uint32_t)vdiTranslateImageFlags2VDI(uImageFlags);
|
---|
334 | pHeader->u.v1plus.fFlags = (uImageFlags & VD_VDI_IMAGE_FLAGS_ZERO_EXPAND) ? 1 : 0;
|
---|
335 | #ifdef VBOX_STRICT
|
---|
336 | char achZero[VDI_IMAGE_COMMENT_SIZE] = {0};
|
---|
337 | Assert(!memcmp(pHeader->u.v1plus.szComment, achZero, VDI_IMAGE_COMMENT_SIZE));
|
---|
338 | #endif
|
---|
339 | pHeader->u.v1plus.szComment[0] = '\0';
|
---|
340 | if (pszComment)
|
---|
341 | {
|
---|
342 | AssertMsg(strlen(pszComment) < sizeof(pHeader->u.v1plus.szComment),
|
---|
343 | ("HDD Comment is too long, cb=%d\n", strlen(pszComment)));
|
---|
344 | strncat(pHeader->u.v1plus.szComment, pszComment, sizeof(pHeader->u.v1plus.szComment)-1);
|
---|
345 | }
|
---|
346 |
|
---|
347 | /* Mark the legacy geometry not-calculated. */
|
---|
348 | pHeader->u.v1plus.LegacyGeometry.cCylinders = 0;
|
---|
349 | pHeader->u.v1plus.LegacyGeometry.cHeads = 0;
|
---|
350 | pHeader->u.v1plus.LegacyGeometry.cSectors = 0;
|
---|
351 | pHeader->u.v1plus.LegacyGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
352 | pHeader->u.v1plus.u32Dummy = 0; /* used to be the translation value */
|
---|
353 |
|
---|
354 | pHeader->u.v1plus.cbDisk = cbDisk;
|
---|
355 | pHeader->u.v1plus.cbBlock = cbBlock;
|
---|
356 | pHeader->u.v1plus.cBlocks = (uint32_t)(cbDisk / cbBlock);
|
---|
357 | if (cbDisk % cbBlock)
|
---|
358 | pHeader->u.v1plus.cBlocks++;
|
---|
359 | pHeader->u.v1plus.cbBlockExtra = cbBlockExtra;
|
---|
360 | pHeader->u.v1plus.cBlocksAllocated = 0;
|
---|
361 |
|
---|
362 | /* Init offsets. */
|
---|
363 | pHeader->u.v1plus.offBlocks = RT_ALIGN_32(sizeof(VDIPREHEADER) + sizeof(VDIHEADER1PLUS), cbDataAlign);
|
---|
364 | pHeader->u.v1plus.offData = RT_ALIGN_32(pHeader->u.v1plus.offBlocks + (pHeader->u.v1plus.cBlocks * sizeof(VDIIMAGEBLOCKPOINTER)), cbDataAlign);
|
---|
365 |
|
---|
366 | /* Init uuids. */
|
---|
367 | RTUuidCreate(&pHeader->u.v1plus.uuidCreate);
|
---|
368 | RTUuidClear(&pHeader->u.v1plus.uuidModify);
|
---|
369 | RTUuidClear(&pHeader->u.v1plus.uuidLinkage);
|
---|
370 | RTUuidClear(&pHeader->u.v1plus.uuidParentModify);
|
---|
371 |
|
---|
372 | /* Mark LCHS geometry not-calculated. */
|
---|
373 | pHeader->u.v1plus.LCHSGeometry.cCylinders = 0;
|
---|
374 | pHeader->u.v1plus.LCHSGeometry.cHeads = 0;
|
---|
375 | pHeader->u.v1plus.LCHSGeometry.cSectors = 0;
|
---|
376 | pHeader->u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
377 | }
|
---|
378 |
|
---|
379 | /**
|
---|
380 | * Internal: Check VDI header.
|
---|
381 | */
|
---|
382 | static int vdiValidateHeader(PVDIHEADER pHeader)
|
---|
383 | {
|
---|
384 | /* Check version-dependent header parameters. */
|
---|
385 | switch (GET_MAJOR_HEADER_VERSION(pHeader))
|
---|
386 | {
|
---|
387 | case 0:
|
---|
388 | {
|
---|
389 | /* Old header version. */
|
---|
390 | break;
|
---|
391 | }
|
---|
392 | case 1:
|
---|
393 | {
|
---|
394 | /* Current header version. */
|
---|
395 |
|
---|
396 | if (pHeader->u.v1.cbHeader < sizeof(VDIHEADER1))
|
---|
397 | {
|
---|
398 | LogRel(("VDI: v1 header size wrong (%d < %d)\n",
|
---|
399 | pHeader->u.v1.cbHeader, sizeof(VDIHEADER1)));
|
---|
400 | return VERR_VD_VDI_INVALID_HEADER;
|
---|
401 | }
|
---|
402 |
|
---|
403 | if (getImageBlocksOffset(pHeader) < (sizeof(VDIPREHEADER) + sizeof(VDIHEADER1)))
|
---|
404 | {
|
---|
405 | LogRel(("VDI: v1 blocks offset wrong (%d < %d)\n",
|
---|
406 | getImageBlocksOffset(pHeader), sizeof(VDIPREHEADER) + sizeof(VDIHEADER1)));
|
---|
407 | return VERR_VD_VDI_INVALID_HEADER;
|
---|
408 | }
|
---|
409 |
|
---|
410 | if (getImageDataOffset(pHeader) < (getImageBlocksOffset(pHeader) + getImageBlocks(pHeader) * sizeof(VDIIMAGEBLOCKPOINTER)))
|
---|
411 | {
|
---|
412 | LogRel(("VDI: v1 image data offset wrong (%d < %d)\n",
|
---|
413 | getImageDataOffset(pHeader), getImageBlocksOffset(pHeader) + getImageBlocks(pHeader) * sizeof(VDIIMAGEBLOCKPOINTER)));
|
---|
414 | return VERR_VD_VDI_INVALID_HEADER;
|
---|
415 | }
|
---|
416 |
|
---|
417 | break;
|
---|
418 | }
|
---|
419 | default:
|
---|
420 | /* Unsupported. */
|
---|
421 | return VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
422 | }
|
---|
423 |
|
---|
424 | /* Check common header parameters. */
|
---|
425 |
|
---|
426 | bool fFailed = false;
|
---|
427 |
|
---|
428 | if ( getImageType(pHeader) < VDI_IMAGE_TYPE_FIRST
|
---|
429 | || getImageType(pHeader) > VDI_IMAGE_TYPE_LAST)
|
---|
430 | {
|
---|
431 | LogRel(("VDI: bad image type %d\n", getImageType(pHeader)));
|
---|
432 | fFailed = true;
|
---|
433 | }
|
---|
434 |
|
---|
435 | if (getImageFlags(pHeader) & ~VD_VDI_IMAGE_FLAGS_MASK)
|
---|
436 | {
|
---|
437 | LogRel(("VDI: bad image flags %08x\n", getImageFlags(pHeader)));
|
---|
438 | fFailed = true;
|
---|
439 | }
|
---|
440 |
|
---|
441 | if ( getImageLCHSGeometry(pHeader)
|
---|
442 | && (getImageLCHSGeometry(pHeader))->cbSector != VDI_GEOMETRY_SECTOR_SIZE)
|
---|
443 | {
|
---|
444 | LogRel(("VDI: wrong sector size (%d != %d)\n",
|
---|
445 | (getImageLCHSGeometry(pHeader))->cbSector, VDI_GEOMETRY_SECTOR_SIZE));
|
---|
446 | fFailed = true;
|
---|
447 | }
|
---|
448 |
|
---|
449 | if ( getImageDiskSize(pHeader) == 0
|
---|
450 | || getImageBlockSize(pHeader) == 0
|
---|
451 | || getImageBlocks(pHeader) == 0
|
---|
452 | || getPowerOfTwo(getImageBlockSize(pHeader)) == 0)
|
---|
453 | {
|
---|
454 | LogRel(("VDI: wrong size (%lld, %d, %d, %d)\n",
|
---|
455 | getImageDiskSize(pHeader), getImageBlockSize(pHeader),
|
---|
456 | getImageBlocks(pHeader), getPowerOfTwo(getImageBlockSize(pHeader))));
|
---|
457 | fFailed = true;
|
---|
458 | }
|
---|
459 |
|
---|
460 | if (getImageBlocksAllocated(pHeader) > getImageBlocks(pHeader))
|
---|
461 | {
|
---|
462 | LogRel(("VDI: too many blocks allocated (%d > %d)\n"
|
---|
463 | " blocksize=%d disksize=%lld\n",
|
---|
464 | getImageBlocksAllocated(pHeader), getImageBlocks(pHeader),
|
---|
465 | getImageBlockSize(pHeader), getImageDiskSize(pHeader)));
|
---|
466 | fFailed = true;
|
---|
467 | }
|
---|
468 |
|
---|
469 | if ( getImageExtraBlockSize(pHeader) != 0
|
---|
470 | && getPowerOfTwo(getImageExtraBlockSize(pHeader)) == 0)
|
---|
471 | {
|
---|
472 | LogRel(("VDI: wrong extra size (%d, %d)\n",
|
---|
473 | getImageExtraBlockSize(pHeader), getPowerOfTwo(getImageExtraBlockSize(pHeader))));
|
---|
474 | fFailed = true;
|
---|
475 | }
|
---|
476 |
|
---|
477 | if ((uint64_t)getImageBlockSize(pHeader) * getImageBlocks(pHeader) < getImageDiskSize(pHeader))
|
---|
478 | {
|
---|
479 | LogRel(("VDI: wrong disk size (%d, %d, %lld)\n",
|
---|
480 | getImageBlockSize(pHeader), getImageBlocks(pHeader), getImageDiskSize(pHeader)));
|
---|
481 | fFailed = true;
|
---|
482 | }
|
---|
483 |
|
---|
484 | if (RTUuidIsNull(getImageCreationUUID(pHeader)))
|
---|
485 | {
|
---|
486 | LogRel(("VDI: uuid of creator is 0\n"));
|
---|
487 | fFailed = true;
|
---|
488 | }
|
---|
489 |
|
---|
490 | if (RTUuidIsNull(getImageModificationUUID(pHeader)))
|
---|
491 | {
|
---|
492 | LogRel(("VDI: uuid of modifier is 0\n"));
|
---|
493 | fFailed = true;
|
---|
494 | }
|
---|
495 |
|
---|
496 | return fFailed ? VERR_VD_VDI_INVALID_HEADER : VINF_SUCCESS;
|
---|
497 | }
|
---|
498 |
|
---|
499 | /**
|
---|
500 | * Internal: Set up VDIIMAGEDESC structure by image header.
|
---|
501 | */
|
---|
502 | static void vdiSetupImageDesc(PVDIIMAGEDESC pImage)
|
---|
503 | {
|
---|
504 | pImage->uImageFlags = getImageFlags(&pImage->Header);
|
---|
505 | pImage->uImageFlags |= vdiTranslateVDI2ImageFlags(getImageType(&pImage->Header));
|
---|
506 | pImage->offStartBlocks = getImageBlocksOffset(&pImage->Header);
|
---|
507 | pImage->offStartData = getImageDataOffset(&pImage->Header);
|
---|
508 | pImage->uBlockMask = getImageBlockSize(&pImage->Header) - 1;
|
---|
509 | pImage->uShiftOffset2Index = getPowerOfTwo(getImageBlockSize(&pImage->Header));
|
---|
510 | pImage->offStartBlockData = getImageExtraBlockSize(&pImage->Header);
|
---|
511 | pImage->cbTotalBlockData = pImage->offStartBlockData
|
---|
512 | + getImageBlockSize(&pImage->Header);
|
---|
513 | }
|
---|
514 |
|
---|
515 | /**
|
---|
516 | * Internal: Create VDI image file.
|
---|
517 | */
|
---|
518 | static int vdiCreateImage(PVDIIMAGEDESC pImage, uint64_t cbSize,
|
---|
519 | unsigned uImageFlags, const char *pszComment,
|
---|
520 | PCVDGEOMETRY pPCHSGeometry,
|
---|
521 | PCVDGEOMETRY pLCHSGeometry, PCRTUUID pUuid,
|
---|
522 | unsigned uOpenFlags, PFNVDPROGRESS pfnProgress,
|
---|
523 | void *pvUser, unsigned uPercentStart,
|
---|
524 | unsigned uPercentSpan, PVDINTERFACECONFIG pIfCfg)
|
---|
525 | {
|
---|
526 | int rc;
|
---|
527 | uint64_t cbTotal;
|
---|
528 | uint64_t cbFill;
|
---|
529 | uint64_t uOff;
|
---|
530 | uint32_t cbDataAlign = VDI_DATA_ALIGN;
|
---|
531 |
|
---|
532 | AssertPtr(pPCHSGeometry);
|
---|
533 | AssertPtr(pLCHSGeometry);
|
---|
534 |
|
---|
535 | pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
|
---|
536 | pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
|
---|
537 | AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
|
---|
538 |
|
---|
539 | /* Special check for comment length. */
|
---|
540 | if ( VALID_PTR(pszComment)
|
---|
541 | && strlen(pszComment) >= VDI_IMAGE_COMMENT_SIZE)
|
---|
542 | {
|
---|
543 | rc = vdIfError(pImage->pIfError, VERR_VD_VDI_COMMENT_TOO_LONG, RT_SRC_POS,
|
---|
544 | N_("VDI: comment is too long for '%s'"), pImage->pszFilename);
|
---|
545 | goto out;
|
---|
546 | }
|
---|
547 |
|
---|
548 | if (pIfCfg)
|
---|
549 | {
|
---|
550 | rc = VDCFGQueryU32Def(pIfCfg, "DataAlignment", &cbDataAlign, VDI_DATA_ALIGN);
|
---|
551 | if (RT_FAILURE(rc))
|
---|
552 | {
|
---|
553 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
|
---|
554 | N_("VDI: Getting data alignment for '%s' failed (%Rrc)"), pImage->pszFilename);
|
---|
555 | goto out;
|
---|
556 | }
|
---|
557 | }
|
---|
558 |
|
---|
559 | vdiInitPreHeader(&pImage->PreHeader);
|
---|
560 | vdiInitHeader(&pImage->Header, uImageFlags, pszComment, cbSize, VDI_IMAGE_DEFAULT_BLOCK_SIZE, 0,
|
---|
561 | cbDataAlign);
|
---|
562 | /* Save PCHS geometry. Not much work, and makes the flow of information
|
---|
563 | * quite a bit clearer - relying on the higher level isn't obvious. */
|
---|
564 | pImage->PCHSGeometry = *pPCHSGeometry;
|
---|
565 | /* Set LCHS geometry (legacy geometry is ignored for the current 1.1+). */
|
---|
566 | pImage->Header.u.v1plus.LCHSGeometry.cCylinders = pLCHSGeometry->cCylinders;
|
---|
567 | pImage->Header.u.v1plus.LCHSGeometry.cHeads = pLCHSGeometry->cHeads;
|
---|
568 | pImage->Header.u.v1plus.LCHSGeometry.cSectors = pLCHSGeometry->cSectors;
|
---|
569 | pImage->Header.u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
570 |
|
---|
571 | pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
|
---|
572 | if (!pImage->paBlocks)
|
---|
573 | {
|
---|
574 | rc = VERR_NO_MEMORY;
|
---|
575 | goto out;
|
---|
576 | }
|
---|
577 |
|
---|
578 | if (!(uImageFlags & VD_IMAGE_FLAGS_FIXED))
|
---|
579 | {
|
---|
580 | /* for growing images mark all blocks in paBlocks as free. */
|
---|
581 | for (unsigned i = 0; i < pImage->Header.u.v1.cBlocks; i++)
|
---|
582 | pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
|
---|
583 | }
|
---|
584 | else
|
---|
585 | {
|
---|
586 | /* for fixed images mark all blocks in paBlocks as allocated */
|
---|
587 | for (unsigned i = 0; i < pImage->Header.u.v1.cBlocks; i++)
|
---|
588 | pImage->paBlocks[i] = i;
|
---|
589 | pImage->Header.u.v1.cBlocksAllocated = pImage->Header.u.v1.cBlocks;
|
---|
590 | }
|
---|
591 |
|
---|
592 | /* Setup image parameters. */
|
---|
593 | vdiSetupImageDesc(pImage);
|
---|
594 |
|
---|
595 | /* Create image file. */
|
---|
596 | rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename,
|
---|
597 | VDOpenFlagsToFileOpenFlags(uOpenFlags & ~VD_OPEN_FLAGS_READONLY,
|
---|
598 | true /* fCreate */),
|
---|
599 | &pImage->pStorage);
|
---|
600 | if (RT_FAILURE(rc))
|
---|
601 | {
|
---|
602 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: cannot create image '%s'"),
|
---|
603 | pImage->pszFilename);
|
---|
604 | goto out;
|
---|
605 | }
|
---|
606 |
|
---|
607 | cbTotal = pImage->offStartData
|
---|
608 | + (uint64_t)getImageBlocks(&pImage->Header) * pImage->cbTotalBlockData;
|
---|
609 |
|
---|
610 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
611 | {
|
---|
612 | /* Check the free space on the disk and leave early if there is not
|
---|
613 | * sufficient space available. */
|
---|
614 | int64_t cbFree = 0;
|
---|
615 | rc = vdIfIoIntFileGetFreeSpace(pImage->pIfIo, pImage->pszFilename, &cbFree);
|
---|
616 | if (RT_SUCCESS(rc) /* ignore errors */ && ((uint64_t)cbFree < cbTotal))
|
---|
617 | {
|
---|
618 | rc = vdIfError(pImage->pIfError, VERR_DISK_FULL, RT_SRC_POS,
|
---|
619 | N_("VDI: disk would overflow creating image '%s'"), pImage->pszFilename);
|
---|
620 | goto out;
|
---|
621 | }
|
---|
622 | }
|
---|
623 |
|
---|
624 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
625 | {
|
---|
626 | /*
|
---|
627 | * Allocate & commit whole file if fixed image, it must be more
|
---|
628 | * effective than expanding file by write operations.
|
---|
629 | */
|
---|
630 | rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, cbTotal);
|
---|
631 | pImage->cbImage = cbTotal;
|
---|
632 | }
|
---|
633 | else
|
---|
634 | {
|
---|
635 | /* Set file size to hold header and blocks array. */
|
---|
636 | rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, pImage->offStartData);
|
---|
637 | pImage->cbImage = pImage->offStartData;
|
---|
638 | }
|
---|
639 | if (RT_FAILURE(rc))
|
---|
640 | {
|
---|
641 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: setting image size failed for '%s'"),
|
---|
642 | pImage->pszFilename);
|
---|
643 | goto out;
|
---|
644 | }
|
---|
645 |
|
---|
646 | /* Use specified image uuid */
|
---|
647 | *getImageCreationUUID(&pImage->Header) = *pUuid;
|
---|
648 |
|
---|
649 | /* Generate image last-modify uuid */
|
---|
650 | RTUuidCreate(getImageModificationUUID(&pImage->Header));
|
---|
651 |
|
---|
652 | /* Write pre-header. */
|
---|
653 | VDIPREHEADER PreHeader;
|
---|
654 | vdiConvPreHeaderEndianess(VDIECONV_H2F, &PreHeader, &pImage->PreHeader);
|
---|
655 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, 0,
|
---|
656 | &PreHeader, sizeof(PreHeader));
|
---|
657 | if (RT_FAILURE(rc))
|
---|
658 | {
|
---|
659 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: writing pre-header failed for '%s'"),
|
---|
660 | pImage->pszFilename);
|
---|
661 | goto out;
|
---|
662 | }
|
---|
663 |
|
---|
664 | /* Write header. */
|
---|
665 | VDIHEADER1PLUS Hdr;
|
---|
666 | vdiConvHeaderEndianessV1p(VDIECONV_H2F, &Hdr, &pImage->Header.u.v1plus);
|
---|
667 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, sizeof(pImage->PreHeader),
|
---|
668 | &Hdr, sizeof(Hdr));
|
---|
669 | if (RT_FAILURE(rc))
|
---|
670 | {
|
---|
671 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: writing header failed for '%s'"),
|
---|
672 | pImage->pszFilename);
|
---|
673 | goto out;
|
---|
674 | }
|
---|
675 |
|
---|
676 | vdiConvBlocksEndianess(VDIECONV_H2F, pImage->paBlocks, getImageBlocks(&pImage->Header));
|
---|
677 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, pImage->offStartBlocks, pImage->paBlocks,
|
---|
678 | getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER));
|
---|
679 | vdiConvBlocksEndianess(VDIECONV_F2H, pImage->paBlocks, getImageBlocks(&pImage->Header));
|
---|
680 | if (RT_FAILURE(rc))
|
---|
681 | {
|
---|
682 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: writing block pointers failed for '%s'"),
|
---|
683 | pImage->pszFilename);
|
---|
684 | goto out;
|
---|
685 | }
|
---|
686 |
|
---|
687 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
688 | {
|
---|
689 | /* Fill image with zeroes. We do this for every fixed-size image since on some systems
|
---|
690 | * (for example Windows Vista), it takes ages to write a block near the end of a sparse
|
---|
691 | * file and the guest could complain about an ATA timeout. */
|
---|
692 |
|
---|
693 | /** @todo Starting with Linux 2.6.23, there is an fallocate() system call.
|
---|
694 | * Currently supported file systems are ext4 and ocfs2. */
|
---|
695 |
|
---|
696 | /* Allocate a temporary zero-filled buffer. Use a bigger block size to optimize writing */
|
---|
697 | const size_t cbBuf = 128 * _1K;
|
---|
698 | void *pvBuf = RTMemTmpAllocZ(cbBuf);
|
---|
699 | if (!pvBuf)
|
---|
700 | {
|
---|
701 | rc = VERR_NO_MEMORY;
|
---|
702 | goto out;
|
---|
703 | }
|
---|
704 |
|
---|
705 | cbFill = (uint64_t)getImageBlocks(&pImage->Header) * pImage->cbTotalBlockData;
|
---|
706 | uOff = 0;
|
---|
707 | /* Write data to all image blocks. */
|
---|
708 | while (uOff < cbFill)
|
---|
709 | {
|
---|
710 | unsigned cbChunk = (unsigned)RT_MIN(cbFill, cbBuf);
|
---|
711 |
|
---|
712 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, pImage->offStartData + uOff,
|
---|
713 | pvBuf, cbChunk);
|
---|
714 | if (RT_FAILURE(rc))
|
---|
715 | {
|
---|
716 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: writing block failed for '%s'"), pImage->pszFilename);
|
---|
717 | RTMemTmpFree(pvBuf);
|
---|
718 | goto out;
|
---|
719 | }
|
---|
720 |
|
---|
721 | uOff += cbChunk;
|
---|
722 |
|
---|
723 | if (pfnProgress)
|
---|
724 | {
|
---|
725 | rc = pfnProgress(pvUser,
|
---|
726 | uPercentStart + uOff * uPercentSpan / cbFill);
|
---|
727 | if (RT_FAILURE(rc))
|
---|
728 | goto out;
|
---|
729 | }
|
---|
730 | }
|
---|
731 | RTMemTmpFree(pvBuf);
|
---|
732 | }
|
---|
733 |
|
---|
734 | out:
|
---|
735 | if (RT_SUCCESS(rc) && pfnProgress)
|
---|
736 | pfnProgress(pvUser, uPercentStart + uPercentSpan);
|
---|
737 |
|
---|
738 | if (RT_FAILURE(rc))
|
---|
739 | vdiFreeImage(pImage, rc != VERR_ALREADY_EXISTS);
|
---|
740 | return rc;
|
---|
741 | }
|
---|
742 |
|
---|
743 | /**
|
---|
744 | * Internal: Open a VDI image.
|
---|
745 | */
|
---|
746 | static int vdiOpenImage(PVDIIMAGEDESC pImage, unsigned uOpenFlags)
|
---|
747 | {
|
---|
748 | int rc;
|
---|
749 |
|
---|
750 | pImage->uOpenFlags = uOpenFlags;
|
---|
751 |
|
---|
752 | pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
|
---|
753 | pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
|
---|
754 | AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
|
---|
755 |
|
---|
756 | /*
|
---|
757 | * Open the image.
|
---|
758 | */
|
---|
759 | rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename,
|
---|
760 | VDOpenFlagsToFileOpenFlags(uOpenFlags, false /* fCreate */),
|
---|
761 | &pImage->pStorage);
|
---|
762 | if (RT_FAILURE(rc))
|
---|
763 | {
|
---|
764 | /* Do NOT signal an appropriate error here, as the VD layer has the
|
---|
765 | * choice of retrying the open if it failed. */
|
---|
766 | goto out;
|
---|
767 | }
|
---|
768 |
|
---|
769 | /* Get file size. */
|
---|
770 | rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage,
|
---|
771 | &pImage->cbImage);
|
---|
772 | if (RT_FAILURE(rc))
|
---|
773 | {
|
---|
774 | vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: error getting the image size in '%s'"), pImage->pszFilename);
|
---|
775 | rc = VERR_VD_VDI_INVALID_HEADER;
|
---|
776 | goto out;
|
---|
777 | }
|
---|
778 |
|
---|
779 | /* Read pre-header. */
|
---|
780 | VDIPREHEADER PreHeader;
|
---|
781 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, 0,
|
---|
782 | &PreHeader, sizeof(PreHeader));
|
---|
783 | if (RT_FAILURE(rc))
|
---|
784 | {
|
---|
785 | vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: error reading pre-header in '%s'"), pImage->pszFilename);
|
---|
786 | rc = VERR_VD_VDI_INVALID_HEADER;
|
---|
787 | goto out;
|
---|
788 | }
|
---|
789 | vdiConvPreHeaderEndianess(VDIECONV_F2H, &pImage->PreHeader, &PreHeader);
|
---|
790 | rc = vdiValidatePreHeader(&pImage->PreHeader);
|
---|
791 | if (RT_FAILURE(rc))
|
---|
792 | {
|
---|
793 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: invalid pre-header in '%s'"), pImage->pszFilename);
|
---|
794 | goto out;
|
---|
795 | }
|
---|
796 |
|
---|
797 | /* Read header. */
|
---|
798 | pImage->Header.uVersion = pImage->PreHeader.u32Version;
|
---|
799 | switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
|
---|
800 | {
|
---|
801 | case 0:
|
---|
802 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, sizeof(pImage->PreHeader),
|
---|
803 | &pImage->Header.u.v0, sizeof(pImage->Header.u.v0));
|
---|
804 | if (RT_FAILURE(rc))
|
---|
805 | {
|
---|
806 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: error reading v0 header in '%s'"), pImage->pszFilename);
|
---|
807 | goto out;
|
---|
808 | }
|
---|
809 | vdiConvHeaderEndianessV0(VDIECONV_F2H, &pImage->Header.u.v0, &pImage->Header.u.v0);
|
---|
810 | break;
|
---|
811 | case 1:
|
---|
812 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, sizeof(pImage->PreHeader),
|
---|
813 | &pImage->Header.u.v1, sizeof(pImage->Header.u.v1));
|
---|
814 | if (RT_FAILURE(rc))
|
---|
815 | {
|
---|
816 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: error reading v1 header in '%s'"), pImage->pszFilename);
|
---|
817 | goto out;
|
---|
818 | }
|
---|
819 | vdiConvHeaderEndianessV1(VDIECONV_F2H, &pImage->Header.u.v1, &pImage->Header.u.v1);
|
---|
820 | /* Convert VDI 1.1 images to VDI 1.1+ on open in read/write mode.
|
---|
821 | * Conversion is harmless, as any VirtualBox version supporting VDI
|
---|
822 | * 1.1 doesn't touch fields it doesn't know about. */
|
---|
823 | if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
824 | && GET_MINOR_HEADER_VERSION(&pImage->Header) == 1
|
---|
825 | && pImage->Header.u.v1.cbHeader < sizeof(pImage->Header.u.v1plus))
|
---|
826 | {
|
---|
827 | pImage->Header.u.v1plus.cbHeader = sizeof(pImage->Header.u.v1plus);
|
---|
828 | /* Mark LCHS geometry not-calculated. */
|
---|
829 | pImage->Header.u.v1plus.LCHSGeometry.cCylinders = 0;
|
---|
830 | pImage->Header.u.v1plus.LCHSGeometry.cHeads = 0;
|
---|
831 | pImage->Header.u.v1plus.LCHSGeometry.cSectors = 0;
|
---|
832 | pImage->Header.u.v1plus.LCHSGeometry.cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
833 | }
|
---|
834 | else if (pImage->Header.u.v1.cbHeader >= sizeof(pImage->Header.u.v1plus))
|
---|
835 | {
|
---|
836 | /* Read the actual VDI 1.1+ header completely. */
|
---|
837 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, sizeof(pImage->PreHeader),
|
---|
838 | &pImage->Header.u.v1plus,
|
---|
839 | sizeof(pImage->Header.u.v1plus));
|
---|
840 | if (RT_FAILURE(rc))
|
---|
841 | {
|
---|
842 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: error reading v1.1+ header in '%s'"), pImage->pszFilename);
|
---|
843 | goto out;
|
---|
844 | }
|
---|
845 | vdiConvHeaderEndianessV1p(VDIECONV_F2H, &pImage->Header.u.v1plus, &pImage->Header.u.v1plus);
|
---|
846 | }
|
---|
847 | break;
|
---|
848 | default:
|
---|
849 | rc = vdIfError(pImage->pIfError, VERR_VD_VDI_UNSUPPORTED_VERSION, RT_SRC_POS, N_("VDI: unsupported major version %u in '%s'"), GET_MAJOR_HEADER_VERSION(&pImage->Header), pImage->pszFilename);
|
---|
850 | goto out;
|
---|
851 | }
|
---|
852 |
|
---|
853 | rc = vdiValidateHeader(&pImage->Header);
|
---|
854 | if (RT_FAILURE(rc))
|
---|
855 | {
|
---|
856 | rc = vdIfError(pImage->pIfError, VERR_VD_VDI_INVALID_HEADER, RT_SRC_POS, N_("VDI: invalid header in '%s'"), pImage->pszFilename);
|
---|
857 | goto out;
|
---|
858 | }
|
---|
859 |
|
---|
860 | /* Setup image parameters by header. */
|
---|
861 | vdiSetupImageDesc(pImage);
|
---|
862 |
|
---|
863 | /* Allocate memory for blocks array. */
|
---|
864 | pImage->paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&pImage->Header));
|
---|
865 | if (!pImage->paBlocks)
|
---|
866 | {
|
---|
867 | rc = VERR_NO_MEMORY;
|
---|
868 | goto out;
|
---|
869 | }
|
---|
870 |
|
---|
871 | /* Read blocks array. */
|
---|
872 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, pImage->offStartBlocks, pImage->paBlocks,
|
---|
873 | getImageBlocks(&pImage->Header) * sizeof(VDIIMAGEBLOCKPOINTER));
|
---|
874 | if (RT_FAILURE(rc))
|
---|
875 | {
|
---|
876 | rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VDI: Error reading the block table in '%s'"), pImage->pszFilename);
|
---|
877 | goto out;
|
---|
878 | }
|
---|
879 | vdiConvBlocksEndianess(VDIECONV_F2H, pImage->paBlocks, getImageBlocks(&pImage->Header));
|
---|
880 |
|
---|
881 | if (uOpenFlags & VD_OPEN_FLAGS_DISCARD)
|
---|
882 | {
|
---|
883 | /*
|
---|
884 | * Create the back resolving table for discards.
|
---|
885 | * any error or inconsistency results in a fail because this might
|
---|
886 | * get us into trouble later on.
|
---|
887 | */
|
---|
888 | pImage->paBlocksRev = (unsigned *)RTMemAllocZ(sizeof(unsigned) * getImageBlocks(&pImage->Header));
|
---|
889 | if (pImage->paBlocksRev)
|
---|
890 | {
|
---|
891 | unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
|
---|
892 | unsigned cBlocks = getImageBlocks(&pImage->Header);
|
---|
893 |
|
---|
894 | for (unsigned i = 0; i < cBlocks; i++)
|
---|
895 | pImage->paBlocksRev[i] = VDI_IMAGE_BLOCK_FREE;
|
---|
896 |
|
---|
897 | for (unsigned i = 0; i < cBlocks; i++)
|
---|
898 | {
|
---|
899 | VDIIMAGEBLOCKPOINTER ptrBlock = pImage->paBlocks[i];
|
---|
900 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(ptrBlock))
|
---|
901 | {
|
---|
902 | if (ptrBlock < cBlocksAllocated)
|
---|
903 | {
|
---|
904 | if (pImage->paBlocksRev[ptrBlock] == VDI_IMAGE_BLOCK_FREE)
|
---|
905 | pImage->paBlocksRev[ptrBlock] = i;
|
---|
906 | else
|
---|
907 | {
|
---|
908 | rc = VERR_VD_VDI_INVALID_HEADER;
|
---|
909 | break;
|
---|
910 | }
|
---|
911 | }
|
---|
912 | else
|
---|
913 | {
|
---|
914 | rc = VERR_VD_VDI_INVALID_HEADER;
|
---|
915 | break;
|
---|
916 | }
|
---|
917 | }
|
---|
918 | }
|
---|
919 | }
|
---|
920 | else
|
---|
921 | rc = VERR_NO_MEMORY;
|
---|
922 | }
|
---|
923 |
|
---|
924 | out:
|
---|
925 | if (RT_FAILURE(rc))
|
---|
926 | vdiFreeImage(pImage, false);
|
---|
927 | return rc;
|
---|
928 | }
|
---|
929 |
|
---|
930 | /**
|
---|
931 | * Internal: Save header to file.
|
---|
932 | */
|
---|
933 | static int vdiUpdateHeader(PVDIIMAGEDESC pImage)
|
---|
934 | {
|
---|
935 | int rc;
|
---|
936 | switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
|
---|
937 | {
|
---|
938 | case 0:
|
---|
939 | {
|
---|
940 | VDIHEADER0 Hdr;
|
---|
941 | vdiConvHeaderEndianessV0(VDIECONV_H2F, &Hdr, &pImage->Header.u.v0);
|
---|
942 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, sizeof(VDIPREHEADER),
|
---|
943 | &Hdr, sizeof(Hdr));
|
---|
944 | break;
|
---|
945 | }
|
---|
946 | case 1:
|
---|
947 | if (pImage->Header.u.v1plus.cbHeader < sizeof(pImage->Header.u.v1plus))
|
---|
948 | {
|
---|
949 | VDIHEADER1 Hdr;
|
---|
950 | vdiConvHeaderEndianessV1(VDIECONV_H2F, &Hdr, &pImage->Header.u.v1);
|
---|
951 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, sizeof(VDIPREHEADER),
|
---|
952 | &Hdr, sizeof(Hdr));
|
---|
953 | }
|
---|
954 | else
|
---|
955 | {
|
---|
956 | VDIHEADER1PLUS Hdr;
|
---|
957 | vdiConvHeaderEndianessV1p(VDIECONV_H2F, &Hdr, &pImage->Header.u.v1plus);
|
---|
958 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, sizeof(VDIPREHEADER),
|
---|
959 | &Hdr, sizeof(Hdr));
|
---|
960 | }
|
---|
961 | break;
|
---|
962 | default:
|
---|
963 | rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
964 | break;
|
---|
965 | }
|
---|
966 | AssertMsgRC(rc, ("vdiUpdateHeader failed, filename=\"%s\" rc=%Rrc\n", pImage->pszFilename, rc));
|
---|
967 | return rc;
|
---|
968 | }
|
---|
969 |
|
---|
970 | /**
|
---|
971 | * Internal: Save header to file - async version.
|
---|
972 | */
|
---|
973 | static int vdiUpdateHeaderAsync(PVDIIMAGEDESC pImage, PVDIOCTX pIoCtx)
|
---|
974 | {
|
---|
975 | int rc;
|
---|
976 | switch (GET_MAJOR_HEADER_VERSION(&pImage->Header))
|
---|
977 | {
|
---|
978 | case 0:
|
---|
979 | {
|
---|
980 | VDIHEADER0 Hdr;
|
---|
981 | vdiConvHeaderEndianessV0(VDIECONV_H2F, &Hdr, &pImage->Header.u.v0);
|
---|
982 | rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
|
---|
983 | sizeof(VDIPREHEADER), &Hdr, sizeof(Hdr),
|
---|
984 | pIoCtx, NULL, NULL);
|
---|
985 | break;
|
---|
986 | }
|
---|
987 | case 1:
|
---|
988 | if (pImage->Header.u.v1plus.cbHeader < sizeof(pImage->Header.u.v1plus))
|
---|
989 | {
|
---|
990 | VDIHEADER1 Hdr;
|
---|
991 | vdiConvHeaderEndianessV1(VDIECONV_H2F, &Hdr, &pImage->Header.u.v1);
|
---|
992 | rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
|
---|
993 | sizeof(VDIPREHEADER), &Hdr, sizeof(Hdr),
|
---|
994 | pIoCtx, NULL, NULL);
|
---|
995 | }
|
---|
996 | else
|
---|
997 | {
|
---|
998 | VDIHEADER1PLUS Hdr;
|
---|
999 | vdiConvHeaderEndianessV1p(VDIECONV_H2F, &Hdr, &pImage->Header.u.v1plus);
|
---|
1000 | rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
|
---|
1001 | sizeof(VDIPREHEADER), &Hdr, sizeof(Hdr),
|
---|
1002 | pIoCtx, NULL, NULL);
|
---|
1003 | }
|
---|
1004 | break;
|
---|
1005 | default:
|
---|
1006 | rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
1007 | break;
|
---|
1008 | }
|
---|
1009 | AssertMsg(RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS,
|
---|
1010 | ("vdiUpdateHeader failed, filename=\"%s\" rc=%Rrc\n", pImage->pszFilename, rc));
|
---|
1011 | return rc;
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | /**
|
---|
1015 | * Internal: Save block pointer to file, save header to file.
|
---|
1016 | */
|
---|
1017 | static int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock)
|
---|
1018 | {
|
---|
1019 | /* Update image header. */
|
---|
1020 | int rc = vdiUpdateHeader(pImage);
|
---|
1021 | if (RT_SUCCESS(rc))
|
---|
1022 | {
|
---|
1023 | /* write only one block pointer. */
|
---|
1024 | VDIIMAGEBLOCKPOINTER ptrBlock = RT_H2LE_U32(pImage->paBlocks[uBlock]);
|
---|
1025 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
|
---|
1026 | pImage->offStartBlocks + uBlock * sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
1027 | &ptrBlock, sizeof(VDIIMAGEBLOCKPOINTER));
|
---|
1028 | AssertMsgRC(rc, ("vdiUpdateBlockInfo failed to update block=%u, filename=\"%s\", rc=%Rrc\n",
|
---|
1029 | uBlock, pImage->pszFilename, rc));
|
---|
1030 | }
|
---|
1031 | return rc;
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | /**
|
---|
1035 | * Internal: Save block pointer to file, save header to file - async version.
|
---|
1036 | */
|
---|
1037 | static int vdiUpdateBlockInfoAsync(PVDIIMAGEDESC pImage, unsigned uBlock,
|
---|
1038 | PVDIOCTX pIoCtx, bool fUpdateHdr)
|
---|
1039 | {
|
---|
1040 | int rc = VINF_SUCCESS;
|
---|
1041 |
|
---|
1042 | /* Update image header. */
|
---|
1043 | if (fUpdateHdr)
|
---|
1044 | rc = vdiUpdateHeaderAsync(pImage, pIoCtx);
|
---|
1045 |
|
---|
1046 | if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
1047 | {
|
---|
1048 | /* write only one block pointer. */
|
---|
1049 | VDIIMAGEBLOCKPOINTER ptrBlock = RT_H2LE_U32(pImage->paBlocks[uBlock]);
|
---|
1050 | rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
|
---|
1051 | pImage->offStartBlocks + uBlock * sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
1052 | &ptrBlock, sizeof(VDIIMAGEBLOCKPOINTER),
|
---|
1053 | pIoCtx, NULL, NULL);
|
---|
1054 | AssertMsg(RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS,
|
---|
1055 | ("vdiUpdateBlockInfo failed to update block=%u, filename=\"%s\", rc=%Rrc\n",
|
---|
1056 | uBlock, pImage->pszFilename, rc));
|
---|
1057 | }
|
---|
1058 | return rc;
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | /**
|
---|
1062 | * Internal: Flush the image file to disk - async version.
|
---|
1063 | */
|
---|
1064 | static int vdiFlushImageIoCtx(PVDIIMAGEDESC pImage, PVDIOCTX pIoCtx)
|
---|
1065 | {
|
---|
1066 | int rc = VINF_SUCCESS;
|
---|
1067 |
|
---|
1068 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
1069 | {
|
---|
1070 | /* Save header. */
|
---|
1071 | rc = vdiUpdateHeaderAsync(pImage, pIoCtx);
|
---|
1072 | AssertMsg(RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS,
|
---|
1073 | ("vdiUpdateHeaderAsync() failed, filename=\"%s\", rc=%Rrc\n",
|
---|
1074 | pImage->pszFilename, rc));
|
---|
1075 | rc = vdIfIoIntFileFlush(pImage->pIfIo, pImage->pStorage, pIoCtx, NULL, NULL);
|
---|
1076 | AssertMsg(RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS,
|
---|
1077 | ("Flushing data to disk failed rc=%Rrc\n", rc));
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | return rc;
|
---|
1081 | }
|
---|
1082 |
|
---|
1083 | /**
|
---|
1084 | * Completion callback for meta/userdata reads or writes.
|
---|
1085 | *
|
---|
1086 | * @return VBox status code.
|
---|
1087 | * VINF_SUCCESS if everything was successful and the transfer can continue.
|
---|
1088 | * VERR_VD_ASYNC_IO_IN_PROGRESS if there is another data transfer pending.
|
---|
1089 | * @param pBackendData The opaque backend data.
|
---|
1090 | * @param pIoCtx I/O context associated with this request.
|
---|
1091 | * @param pvUser Opaque user data passed during a read/write request.
|
---|
1092 | * @param rcReq Status code for the completed request.
|
---|
1093 | */
|
---|
1094 | static DECLCALLBACK(int) vdiDiscardBlockAsyncUpdate(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
|
---|
1095 | {
|
---|
1096 | int rc = VINF_SUCCESS;
|
---|
1097 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1098 | PVDIBLOCKDISCARDASYNC pDiscardAsync = (PVDIBLOCKDISCARDASYNC)pvUser;
|
---|
1099 |
|
---|
1100 | switch (pDiscardAsync->enmState)
|
---|
1101 | {
|
---|
1102 | case VDIBLOCKDISCARDSTATE_READ_BLOCK:
|
---|
1103 | {
|
---|
1104 | PVDMETAXFER pMetaXfer;
|
---|
1105 | uint64_t u64Offset = (uint64_t)pDiscardAsync->idxLastBlock * pImage->cbTotalBlockData + pImage->offStartData;
|
---|
1106 | rc = vdIfIoIntFileReadMeta(pImage->pIfIo, pImage->pStorage, u64Offset,
|
---|
1107 | pDiscardAsync->pvBlock, pImage->cbTotalBlockData, pIoCtx,
|
---|
1108 | &pMetaXfer, vdiDiscardBlockAsyncUpdate, pDiscardAsync);
|
---|
1109 | if (RT_FAILURE(rc))
|
---|
1110 | break;
|
---|
1111 |
|
---|
1112 | /* Release immediately and go to next step. */
|
---|
1113 | vdIfIoIntMetaXferRelease(pImage->pIfIo, pMetaXfer);
|
---|
1114 | pDiscardAsync->enmState = VDIBLOCKDISCARDSTATE_WRITE_BLOCK;
|
---|
1115 | }
|
---|
1116 | case VDIBLOCKDISCARDSTATE_WRITE_BLOCK:
|
---|
1117 | {
|
---|
1118 | /* Block read complete. Write to the new location (discarded block). */
|
---|
1119 | uint64_t u64Offset = (uint64_t)pDiscardAsync->ptrBlockDiscard * pImage->cbTotalBlockData + pImage->offStartData;
|
---|
1120 | rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage, u64Offset,
|
---|
1121 | pDiscardAsync->pvBlock, pImage->cbTotalBlockData, pIoCtx,
|
---|
1122 | vdiDiscardBlockAsyncUpdate, pDiscardAsync);
|
---|
1123 |
|
---|
1124 | pDiscardAsync->enmState = VDIBLOCKDISCARDSTATE_UPDATE_METADATA;
|
---|
1125 | if (RT_FAILURE(rc))
|
---|
1126 | break;
|
---|
1127 | }
|
---|
1128 | case VDIBLOCKDISCARDSTATE_UPDATE_METADATA:
|
---|
1129 | {
|
---|
1130 | int rc2;
|
---|
1131 | uint64_t cbImage;
|
---|
1132 |
|
---|
1133 | /* Block write complete. Update metadata. */
|
---|
1134 | pImage->paBlocksRev[pDiscardAsync->idxLastBlock] = VDI_IMAGE_BLOCK_FREE;
|
---|
1135 | pImage->paBlocks[pDiscardAsync->uBlock] = VDI_IMAGE_BLOCK_ZERO;
|
---|
1136 |
|
---|
1137 | if (pDiscardAsync->idxLastBlock != pDiscardAsync->ptrBlockDiscard)
|
---|
1138 | {
|
---|
1139 | pImage->paBlocks[pDiscardAsync->uBlockLast] = pDiscardAsync->ptrBlockDiscard;
|
---|
1140 | pImage->paBlocksRev[pDiscardAsync->ptrBlockDiscard] = pDiscardAsync->uBlockLast;
|
---|
1141 |
|
---|
1142 | rc = vdiUpdateBlockInfoAsync(pImage, pDiscardAsync->uBlockLast, pIoCtx, false /* fUpdateHdr */);
|
---|
1143 | if ( RT_FAILURE(rc)
|
---|
1144 | && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
1145 | break;
|
---|
1146 | }
|
---|
1147 |
|
---|
1148 | setImageBlocksAllocated(&pImage->Header, pDiscardAsync->idxLastBlock);
|
---|
1149 | rc = vdiUpdateBlockInfoAsync(pImage, pDiscardAsync->uBlock, pIoCtx, true /* fUpdateHdr */);
|
---|
1150 | if ( RT_FAILURE(rc)
|
---|
1151 | && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
1152 | break;
|
---|
1153 |
|
---|
1154 | pImage->cbImage -= pImage->cbTotalBlockData;
|
---|
1155 | LogFlowFunc(("Set new size %llu\n", pImage->cbImage));
|
---|
1156 | rc2 = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, pImage->cbImage);
|
---|
1157 | if (RT_FAILURE(rc2))
|
---|
1158 | rc = rc2;
|
---|
1159 |
|
---|
1160 | /* Free discard state. */
|
---|
1161 | RTMemFree(pDiscardAsync->pvBlock);
|
---|
1162 | RTMemFree(pDiscardAsync);
|
---|
1163 | break;
|
---|
1164 | }
|
---|
1165 | default:
|
---|
1166 | AssertMsgFailed(("Invalid state %d\n", pDiscardAsync->enmState));
|
---|
1167 | }
|
---|
1168 |
|
---|
1169 | if (rc == VERR_VD_NOT_ENOUGH_METADATA)
|
---|
1170 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1171 |
|
---|
1172 | return rc;
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | /**
|
---|
1176 | * Internal: Discard a whole block from the image filling the created hole with
|
---|
1177 | * data from another block - async I/O version.
|
---|
1178 | *
|
---|
1179 | * @returns VBox status code.
|
---|
1180 | * @param pImage VDI image instance data.
|
---|
1181 | * @param pIoCtx I/O context associated with this request.
|
---|
1182 | * @param uBlock The block to discard.
|
---|
1183 | * @param pvBlock Memory to use for the I/O.
|
---|
1184 | */
|
---|
1185 | static int vdiDiscardBlockAsync(PVDIIMAGEDESC pImage, PVDIOCTX pIoCtx,
|
---|
1186 | unsigned uBlock, void *pvBlock)
|
---|
1187 | {
|
---|
1188 | int rc = VINF_SUCCESS;
|
---|
1189 | PVDIBLOCKDISCARDASYNC pDiscardAsync = NULL;
|
---|
1190 |
|
---|
1191 | LogFlowFunc(("pImage=%#p uBlock=%u pvBlock=%#p\n",
|
---|
1192 | pImage, uBlock, pvBlock));
|
---|
1193 |
|
---|
1194 | pDiscardAsync = (PVDIBLOCKDISCARDASYNC)RTMemAllocZ(sizeof(VDIBLOCKDISCARDASYNC));
|
---|
1195 | if (RT_UNLIKELY(!pDiscardAsync))
|
---|
1196 | return VERR_NO_MEMORY;
|
---|
1197 |
|
---|
1198 | /* Init block discard state. */
|
---|
1199 | pDiscardAsync->uBlock = uBlock;
|
---|
1200 | pDiscardAsync->pvBlock = pvBlock;
|
---|
1201 | pDiscardAsync->ptrBlockDiscard = pImage->paBlocks[uBlock];
|
---|
1202 | pDiscardAsync->idxLastBlock = getImageBlocksAllocated(&pImage->Header) - 1;
|
---|
1203 | pDiscardAsync->uBlockLast = pImage->paBlocksRev[pDiscardAsync->idxLastBlock];
|
---|
1204 |
|
---|
1205 | /*
|
---|
1206 | * The block is empty, remove it.
|
---|
1207 | * Read the last block of the image first.
|
---|
1208 | */
|
---|
1209 | if (pDiscardAsync->idxLastBlock != pDiscardAsync->ptrBlockDiscard)
|
---|
1210 | {
|
---|
1211 | LogFlowFunc(("Moving block [%u]=%u into [%u]=%u\n",
|
---|
1212 | pDiscardAsync->uBlockLast, pDiscardAsync->idxLastBlock,
|
---|
1213 | uBlock, pImage->paBlocks[uBlock]));
|
---|
1214 | pDiscardAsync->enmState = VDIBLOCKDISCARDSTATE_READ_BLOCK;
|
---|
1215 | }
|
---|
1216 | else
|
---|
1217 | {
|
---|
1218 | pDiscardAsync->enmState = VDIBLOCKDISCARDSTATE_UPDATE_METADATA; /* Start immediately to shrink the image. */
|
---|
1219 | LogFlowFunc(("Discard last block [%u]=%u\n", uBlock, pImage->paBlocks[uBlock]));
|
---|
1220 | }
|
---|
1221 |
|
---|
1222 | /* Call the update callback directly. */
|
---|
1223 | rc = vdiDiscardBlockAsyncUpdate(pImage, pIoCtx, pDiscardAsync, VINF_SUCCESS);
|
---|
1224 |
|
---|
1225 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
1226 | return rc;
|
---|
1227 | }
|
---|
1228 |
|
---|
1229 | /**
|
---|
1230 | * Internal: Creates a allocation bitmap from the given data.
|
---|
1231 | * Sectors which contain only 0 are marked as unallocated and sectors with
|
---|
1232 | * other data as allocated.
|
---|
1233 | *
|
---|
1234 | * @returns Pointer to the allocation bitmap or NULL on failure.
|
---|
1235 | * @param pvData The data to create the allocation bitmap for.
|
---|
1236 | * @param cbData Number of bytes in the buffer.
|
---|
1237 | */
|
---|
1238 | static void *vdiAllocationBitmapCreate(void *pvData, size_t cbData)
|
---|
1239 | {
|
---|
1240 | unsigned cSectors = cbData / 512;
|
---|
1241 | unsigned uSectorCur = 0;
|
---|
1242 | void *pbmAllocationBitmap = NULL;
|
---|
1243 |
|
---|
1244 | Assert(!(cbData % 512));
|
---|
1245 | Assert(!(cSectors % 8));
|
---|
1246 |
|
---|
1247 | pbmAllocationBitmap = RTMemAllocZ(cSectors / 8);
|
---|
1248 | if (!pbmAllocationBitmap)
|
---|
1249 | return NULL;
|
---|
1250 |
|
---|
1251 | while (uSectorCur < cSectors)
|
---|
1252 | {
|
---|
1253 | int idxSet = ASMBitFirstSet((uint8_t *)pvData + uSectorCur * 512, cbData * 8);
|
---|
1254 |
|
---|
1255 | if (idxSet != -1)
|
---|
1256 | {
|
---|
1257 | unsigned idxSectorAlloc = idxSet / 8 / 512;
|
---|
1258 | ASMBitSet(pbmAllocationBitmap, uSectorCur + idxSectorAlloc);
|
---|
1259 |
|
---|
1260 | uSectorCur += idxSectorAlloc + 1;
|
---|
1261 | cbData -= (idxSectorAlloc + 1) * 512;
|
---|
1262 | }
|
---|
1263 | else
|
---|
1264 | break;
|
---|
1265 | }
|
---|
1266 |
|
---|
1267 | return pbmAllocationBitmap;
|
---|
1268 | }
|
---|
1269 |
|
---|
1270 |
|
---|
1271 | /**
|
---|
1272 | * Updates the state of the async cluster allocation.
|
---|
1273 | *
|
---|
1274 | * @returns VBox status code.
|
---|
1275 | * @param pBackendData The opaque backend data.
|
---|
1276 | * @param pIoCtx I/O context associated with this request.
|
---|
1277 | * @param pvUser Opaque user data passed during a read/write request.
|
---|
1278 | * @param rcReq Status code for the completed request.
|
---|
1279 | */
|
---|
1280 | static DECLCALLBACK(int) vdiBlockAllocUpdate(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
|
---|
1281 | {
|
---|
1282 | int rc = VINF_SUCCESS;
|
---|
1283 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1284 | PVDIASYNCBLOCKALLOC pBlockAlloc = (PVDIASYNCBLOCKALLOC)pvUser;
|
---|
1285 |
|
---|
1286 | if (RT_SUCCESS(rcReq))
|
---|
1287 | {
|
---|
1288 | pImage->cbImage += pImage->cbTotalBlockData;
|
---|
1289 | pImage->paBlocks[pBlockAlloc->uBlock] = pBlockAlloc->cBlocksAllocated;
|
---|
1290 |
|
---|
1291 | if (pImage->paBlocksRev)
|
---|
1292 | pImage->paBlocksRev[pBlockAlloc->cBlocksAllocated] = pBlockAlloc->uBlock;
|
---|
1293 |
|
---|
1294 | setImageBlocksAllocated(&pImage->Header, pBlockAlloc->cBlocksAllocated + 1);
|
---|
1295 | rc = vdiUpdateBlockInfoAsync(pImage, pBlockAlloc->uBlock, pIoCtx,
|
---|
1296 | true /* fUpdateHdr */);
|
---|
1297 | }
|
---|
1298 | /* else: I/O error don't update the block table. */
|
---|
1299 |
|
---|
1300 | RTMemFree(pBlockAlloc);
|
---|
1301 | return rc;
|
---|
1302 | }
|
---|
1303 |
|
---|
1304 | /** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
|
---|
1305 | static int vdiCheckIfValid(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
|
---|
1306 | PVDINTERFACE pVDIfsImage, VDTYPE *penmType)
|
---|
1307 | {
|
---|
1308 | LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
|
---|
1309 | int rc = VINF_SUCCESS;
|
---|
1310 | PVDIIMAGEDESC pImage;
|
---|
1311 |
|
---|
1312 | if ( !VALID_PTR(pszFilename)
|
---|
1313 | || !*pszFilename)
|
---|
1314 | {
|
---|
1315 | rc = VERR_INVALID_PARAMETER;
|
---|
1316 | goto out;
|
---|
1317 | }
|
---|
1318 |
|
---|
1319 | pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
|
---|
1320 | if (!pImage)
|
---|
1321 | {
|
---|
1322 | rc = VERR_NO_MEMORY;
|
---|
1323 | goto out;
|
---|
1324 | }
|
---|
1325 | pImage->pszFilename = pszFilename;
|
---|
1326 | pImage->pStorage = NULL;
|
---|
1327 | pImage->paBlocks = NULL;
|
---|
1328 | pImage->pVDIfsDisk = pVDIfsDisk;
|
---|
1329 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
1330 |
|
---|
1331 | rc = vdiOpenImage(pImage, VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_READONLY);
|
---|
1332 | vdiFreeImage(pImage, false);
|
---|
1333 | RTMemFree(pImage);
|
---|
1334 |
|
---|
1335 | if (RT_SUCCESS(rc))
|
---|
1336 | *penmType = VDTYPE_HDD;
|
---|
1337 |
|
---|
1338 | out:
|
---|
1339 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1340 | return rc;
|
---|
1341 | }
|
---|
1342 |
|
---|
1343 | /** @copydoc VBOXHDDBACKEND::pfnOpen */
|
---|
1344 | static int vdiOpen(const char *pszFilename, unsigned uOpenFlags,
|
---|
1345 | PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
|
---|
1346 | VDTYPE enmType, void **ppBackendData)
|
---|
1347 | {
|
---|
1348 | LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, ppBackendData));
|
---|
1349 | int rc;
|
---|
1350 | PVDIIMAGEDESC pImage;
|
---|
1351 |
|
---|
1352 | /* Check open flags. All valid flags are supported. */
|
---|
1353 | if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
|
---|
1354 | {
|
---|
1355 | rc = VERR_INVALID_PARAMETER;
|
---|
1356 | goto out;
|
---|
1357 | }
|
---|
1358 |
|
---|
1359 | /* Check remaining arguments. */
|
---|
1360 | if ( !VALID_PTR(pszFilename)
|
---|
1361 | || !*pszFilename)
|
---|
1362 | {
|
---|
1363 | rc = VERR_INVALID_PARAMETER;
|
---|
1364 | goto out;
|
---|
1365 | }
|
---|
1366 |
|
---|
1367 | pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
|
---|
1368 | if (!pImage)
|
---|
1369 | {
|
---|
1370 | rc = VERR_NO_MEMORY;
|
---|
1371 | goto out;
|
---|
1372 | }
|
---|
1373 | pImage->pszFilename = pszFilename;
|
---|
1374 | pImage->pStorage = NULL;
|
---|
1375 | pImage->paBlocks = NULL;
|
---|
1376 | pImage->pVDIfsDisk = pVDIfsDisk;
|
---|
1377 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
1378 |
|
---|
1379 | rc = vdiOpenImage(pImage, uOpenFlags);
|
---|
1380 | if (RT_SUCCESS(rc))
|
---|
1381 | *ppBackendData = pImage;
|
---|
1382 | else
|
---|
1383 | RTMemFree(pImage);
|
---|
1384 |
|
---|
1385 | out:
|
---|
1386 | LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
|
---|
1387 | return rc;
|
---|
1388 | }
|
---|
1389 |
|
---|
1390 | /** @copydoc VBOXHDDBACKEND::pfnCreate */
|
---|
1391 | static int vdiCreate(const char *pszFilename, uint64_t cbSize,
|
---|
1392 | unsigned uImageFlags, const char *pszComment,
|
---|
1393 | PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
|
---|
1394 | PCRTUUID pUuid, unsigned uOpenFlags,
|
---|
1395 | unsigned uPercentStart, unsigned uPercentSpan,
|
---|
1396 | PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
|
---|
1397 | PVDINTERFACE pVDIfsOperation, void **ppBackendData)
|
---|
1398 | {
|
---|
1399 | LogFlowFunc(("pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" pPCHSGeometry=%#p pLCHSGeometry=%#p Uuid=%RTuuid uOpenFlags=%#x uPercentStart=%u uPercentSpan=%u pVDIfsDisk=%#p pVDIfsImage=%#p pVDIfsOperation=%#p ppBackendData=%#p\n", pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, ppBackendData));
|
---|
1400 | int rc;
|
---|
1401 | PVDIIMAGEDESC pImage;
|
---|
1402 |
|
---|
1403 | PFNVDPROGRESS pfnProgress = NULL;
|
---|
1404 | void *pvUser = NULL;
|
---|
1405 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
1406 | if (pIfProgress)
|
---|
1407 | {
|
---|
1408 | pfnProgress = pIfProgress->pfnProgress;
|
---|
1409 | pvUser = pIfProgress->Core.pvUser;
|
---|
1410 | }
|
---|
1411 |
|
---|
1412 | PVDINTERFACECONFIG pIfCfg = VDIfConfigGet(pVDIfsOperation);
|
---|
1413 |
|
---|
1414 | /* Check the image flags. */
|
---|
1415 | if ((uImageFlags & ~VD_VDI_IMAGE_FLAGS_MASK) != 0)
|
---|
1416 | {
|
---|
1417 | rc = VERR_VD_INVALID_TYPE;
|
---|
1418 | goto out;
|
---|
1419 | }
|
---|
1420 |
|
---|
1421 | /* Check open flags. All valid flags are supported. */
|
---|
1422 | if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
|
---|
1423 | {
|
---|
1424 | rc = VERR_INVALID_PARAMETER;
|
---|
1425 | goto out;
|
---|
1426 | }
|
---|
1427 |
|
---|
1428 | /* Check size. Maximum 4PB-3M. No tricks with adjusting the 1M block size
|
---|
1429 | * so far, which would extend the size. */
|
---|
1430 | cbSize = RT_ALIGN_64(cbSize, _1M);
|
---|
1431 | if ( !cbSize
|
---|
1432 | || cbSize >= _1P * 4 - _1M * 3)
|
---|
1433 | {
|
---|
1434 | rc = VERR_VD_INVALID_SIZE;
|
---|
1435 | goto out;
|
---|
1436 | }
|
---|
1437 |
|
---|
1438 | /* Check remaining arguments. */
|
---|
1439 | if ( !VALID_PTR(pszFilename)
|
---|
1440 | || !*pszFilename
|
---|
1441 | || cbSize < VDI_IMAGE_DEFAULT_BLOCK_SIZE
|
---|
1442 | || !VALID_PTR(pPCHSGeometry)
|
---|
1443 | || !VALID_PTR(pLCHSGeometry))
|
---|
1444 | {
|
---|
1445 | rc = VERR_INVALID_PARAMETER;
|
---|
1446 | goto out;
|
---|
1447 | }
|
---|
1448 |
|
---|
1449 | pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
|
---|
1450 | if (!pImage)
|
---|
1451 | {
|
---|
1452 | rc = VERR_NO_MEMORY;
|
---|
1453 | goto out;
|
---|
1454 | }
|
---|
1455 | pImage->pszFilename = pszFilename;
|
---|
1456 | pImage->pStorage = NULL;
|
---|
1457 | pImage->paBlocks = NULL;
|
---|
1458 | pImage->pVDIfsDisk = pVDIfsDisk;
|
---|
1459 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
1460 |
|
---|
1461 | rc = vdiCreateImage(pImage, cbSize, uImageFlags, pszComment,
|
---|
1462 | pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags,
|
---|
1463 | pfnProgress, pvUser, uPercentStart, uPercentSpan,
|
---|
1464 | pIfCfg);
|
---|
1465 | if (RT_SUCCESS(rc))
|
---|
1466 | {
|
---|
1467 | /* So far the image is opened in read/write mode. Make sure the
|
---|
1468 | * image is opened in read-only mode if the caller requested that. */
|
---|
1469 | if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
1470 | {
|
---|
1471 | vdiFreeImage(pImage, false);
|
---|
1472 | rc = vdiOpenImage(pImage, uOpenFlags);
|
---|
1473 | if (RT_FAILURE(rc))
|
---|
1474 | {
|
---|
1475 | RTMemFree(pImage);
|
---|
1476 | goto out;
|
---|
1477 | }
|
---|
1478 | }
|
---|
1479 | *ppBackendData = pImage;
|
---|
1480 | }
|
---|
1481 | else
|
---|
1482 | RTMemFree(pImage);
|
---|
1483 |
|
---|
1484 | out:
|
---|
1485 | LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
|
---|
1486 | return rc;
|
---|
1487 | }
|
---|
1488 |
|
---|
1489 | /** @copydoc VBOXHDDBACKEND::pfnRename */
|
---|
1490 | static int vdiRename(void *pBackendData, const char *pszFilename)
|
---|
1491 | {
|
---|
1492 | LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
|
---|
1493 |
|
---|
1494 | int rc = VINF_SUCCESS;
|
---|
1495 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1496 |
|
---|
1497 | /* Check arguments. */
|
---|
1498 | if ( !pImage
|
---|
1499 | || !pszFilename
|
---|
1500 | || !*pszFilename)
|
---|
1501 | {
|
---|
1502 | rc = VERR_INVALID_PARAMETER;
|
---|
1503 | goto out;
|
---|
1504 | }
|
---|
1505 |
|
---|
1506 | /* Close the image. */
|
---|
1507 | vdiFreeImage(pImage, false);
|
---|
1508 |
|
---|
1509 | /* Rename the file. */
|
---|
1510 | rc = vdIfIoIntFileMove(pImage->pIfIo, pImage->pszFilename, pszFilename, 0);
|
---|
1511 | if (RT_FAILURE(rc))
|
---|
1512 | {
|
---|
1513 | /* The move failed, try to reopen the original image. */
|
---|
1514 | int rc2 = vdiOpenImage(pImage, pImage->uOpenFlags);
|
---|
1515 | if (RT_FAILURE(rc2))
|
---|
1516 | rc = rc2;
|
---|
1517 |
|
---|
1518 | goto out;
|
---|
1519 | }
|
---|
1520 |
|
---|
1521 | /* Update pImage with the new information. */
|
---|
1522 | pImage->pszFilename = pszFilename;
|
---|
1523 |
|
---|
1524 | /* Open the new image. */
|
---|
1525 | rc = vdiOpenImage(pImage, pImage->uOpenFlags);
|
---|
1526 | if (RT_FAILURE(rc))
|
---|
1527 | goto out;
|
---|
1528 |
|
---|
1529 | out:
|
---|
1530 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1531 | return rc;
|
---|
1532 | }
|
---|
1533 |
|
---|
1534 | /** @copydoc VBOXHDDBACKEND::pfnClose */
|
---|
1535 | static int vdiClose(void *pBackendData, bool fDelete)
|
---|
1536 | {
|
---|
1537 | LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
|
---|
1538 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1539 | int rc;
|
---|
1540 |
|
---|
1541 | rc = vdiFreeImage(pImage, fDelete);
|
---|
1542 | RTMemFree(pImage);
|
---|
1543 |
|
---|
1544 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1545 | return rc;
|
---|
1546 | }
|
---|
1547 |
|
---|
1548 | static int vdiRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
|
---|
1549 | PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
|
---|
1550 | {
|
---|
1551 | LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
|
---|
1552 | pBackendData, uOffset, pIoCtx, cbToRead, pcbActuallyRead));
|
---|
1553 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1554 | unsigned uBlock;
|
---|
1555 | unsigned offRead;
|
---|
1556 | int rc;
|
---|
1557 |
|
---|
1558 | AssertPtr(pImage);
|
---|
1559 | Assert(!(uOffset % 512));
|
---|
1560 | Assert(!(cbToRead % 512));
|
---|
1561 |
|
---|
1562 | if ( uOffset + cbToRead > getImageDiskSize(&pImage->Header)
|
---|
1563 | || !VALID_PTR(pIoCtx)
|
---|
1564 | || !cbToRead)
|
---|
1565 | {
|
---|
1566 | rc = VERR_INVALID_PARAMETER;
|
---|
1567 | goto out;
|
---|
1568 | }
|
---|
1569 |
|
---|
1570 | /* Calculate starting block number and offset inside it. */
|
---|
1571 | uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
|
---|
1572 | offRead = (unsigned)uOffset & pImage->uBlockMask;
|
---|
1573 |
|
---|
1574 | /* Clip read range to at most the rest of the block. */
|
---|
1575 | cbToRead = RT_MIN(cbToRead, getImageBlockSize(&pImage->Header) - offRead);
|
---|
1576 | Assert(!(cbToRead % 512));
|
---|
1577 |
|
---|
1578 | if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE)
|
---|
1579 | rc = VERR_VD_BLOCK_FREE;
|
---|
1580 | else if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
|
---|
1581 | {
|
---|
1582 | size_t cbSet;
|
---|
1583 |
|
---|
1584 | cbSet = vdIfIoIntIoCtxSet(pImage->pIfIo, pIoCtx, 0, cbToRead);
|
---|
1585 | Assert(cbSet == cbToRead);
|
---|
1586 |
|
---|
1587 | rc = VINF_SUCCESS;
|
---|
1588 | }
|
---|
1589 | else
|
---|
1590 | {
|
---|
1591 | /* Block present in image file, read relevant data. */
|
---|
1592 | uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
|
---|
1593 | + (pImage->offStartData + pImage->offStartBlockData + offRead);
|
---|
1594 |
|
---|
1595 | if (u64Offset + cbToRead <= pImage->cbImage)
|
---|
1596 | rc = vdIfIoIntFileReadUser(pImage->pIfIo, pImage->pStorage, u64Offset,
|
---|
1597 | pIoCtx, cbToRead);
|
---|
1598 | else
|
---|
1599 | {
|
---|
1600 | LogRel(("VDI: Out of range access (%llu) in image %s, image size %llu\n",
|
---|
1601 | u64Offset, pImage->pszFilename, pImage->cbImage));
|
---|
1602 | vdIfIoIntIoCtxSet(pImage->pIfIo, pIoCtx, 0, cbToRead);
|
---|
1603 | rc = VERR_VD_READ_OUT_OF_RANGE;
|
---|
1604 | }
|
---|
1605 | }
|
---|
1606 |
|
---|
1607 | if (pcbActuallyRead)
|
---|
1608 | *pcbActuallyRead = cbToRead;
|
---|
1609 |
|
---|
1610 | out:
|
---|
1611 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1612 | return rc;
|
---|
1613 | }
|
---|
1614 |
|
---|
1615 | static int vdiWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
|
---|
1616 | PVDIOCTX pIoCtx, size_t *pcbWriteProcess, size_t *pcbPreRead,
|
---|
1617 | size_t *pcbPostRead, unsigned fWrite)
|
---|
1618 | {
|
---|
1619 | LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
|
---|
1620 | pBackendData, uOffset, pIoCtx, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
|
---|
1621 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1622 | unsigned uBlock;
|
---|
1623 | unsigned offWrite;
|
---|
1624 | int rc = VINF_SUCCESS;
|
---|
1625 |
|
---|
1626 | AssertPtr(pImage);
|
---|
1627 | Assert(!(uOffset % 512));
|
---|
1628 | Assert(!(cbToWrite % 512));
|
---|
1629 |
|
---|
1630 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
1631 | {
|
---|
1632 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
1633 | goto out;
|
---|
1634 | }
|
---|
1635 |
|
---|
1636 | if (!VALID_PTR(pIoCtx) || !cbToWrite)
|
---|
1637 | {
|
---|
1638 | rc = VERR_INVALID_PARAMETER;
|
---|
1639 | goto out;
|
---|
1640 | }
|
---|
1641 |
|
---|
1642 | /* No size check here, will do that later. For dynamic images which are
|
---|
1643 | * not multiples of the block size in length, this would prevent writing to
|
---|
1644 | * the last block. */
|
---|
1645 |
|
---|
1646 | /* Calculate starting block number and offset inside it. */
|
---|
1647 | uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
|
---|
1648 | offWrite = (unsigned)uOffset & pImage->uBlockMask;
|
---|
1649 |
|
---|
1650 | /* Clip write range to at most the rest of the block. */
|
---|
1651 | cbToWrite = RT_MIN(cbToWrite, getImageBlockSize(&pImage->Header) - offWrite);
|
---|
1652 | Assert(!(cbToWrite % 512));
|
---|
1653 |
|
---|
1654 | do
|
---|
1655 | {
|
---|
1656 | if (!IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
|
---|
1657 | {
|
---|
1658 | /* Block is either free or zero. */
|
---|
1659 | if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_ZEROES)
|
---|
1660 | && ( pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO
|
---|
1661 | || cbToWrite == getImageBlockSize(&pImage->Header)))
|
---|
1662 | {
|
---|
1663 | /* If the destination block is unallocated at this point, it's
|
---|
1664 | * either a zero block or a block which hasn't been used so far
|
---|
1665 | * (which also means that it's a zero block. Don't need to write
|
---|
1666 | * anything to this block if the data consists of just zeroes. */
|
---|
1667 | if (vdIfIoIntIoCtxIsZero(pImage->pIfIo, pIoCtx, cbToWrite, true))
|
---|
1668 | {
|
---|
1669 | pImage->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
|
---|
1670 | *pcbPreRead = 0;
|
---|
1671 | *pcbPostRead = 0;
|
---|
1672 | break;
|
---|
1673 | }
|
---|
1674 | }
|
---|
1675 |
|
---|
1676 | if ( cbToWrite == getImageBlockSize(&pImage->Header)
|
---|
1677 | && !(fWrite & VD_WRITE_NO_ALLOC))
|
---|
1678 | {
|
---|
1679 | /* Full block write to previously unallocated block.
|
---|
1680 | * Allocate block and write data. */
|
---|
1681 | Assert(!offWrite);
|
---|
1682 | PVDIASYNCBLOCKALLOC pBlockAlloc = (PVDIASYNCBLOCKALLOC)RTMemAllocZ(sizeof(VDIASYNCBLOCKALLOC));
|
---|
1683 | if (!pBlockAlloc)
|
---|
1684 | {
|
---|
1685 | rc = VERR_NO_MEMORY;
|
---|
1686 | break;
|
---|
1687 | }
|
---|
1688 |
|
---|
1689 | unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
|
---|
1690 | uint64_t u64Offset = (uint64_t)cBlocksAllocated * pImage->cbTotalBlockData
|
---|
1691 | + (pImage->offStartData + pImage->offStartBlockData);
|
---|
1692 |
|
---|
1693 | pBlockAlloc->cBlocksAllocated = cBlocksAllocated;
|
---|
1694 | pBlockAlloc->uBlock = uBlock;
|
---|
1695 |
|
---|
1696 | *pcbPreRead = 0;
|
---|
1697 | *pcbPostRead = 0;
|
---|
1698 |
|
---|
1699 | rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
|
---|
1700 | u64Offset, pIoCtx, cbToWrite,
|
---|
1701 | vdiBlockAllocUpdate, pBlockAlloc);
|
---|
1702 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
1703 | break;
|
---|
1704 | else if (RT_FAILURE(rc))
|
---|
1705 | {
|
---|
1706 | RTMemFree(pBlockAlloc);
|
---|
1707 | break;
|
---|
1708 | }
|
---|
1709 |
|
---|
1710 | rc = vdiBlockAllocUpdate(pImage, pIoCtx, pBlockAlloc, rc);
|
---|
1711 | }
|
---|
1712 | else
|
---|
1713 | {
|
---|
1714 | /* Trying to do a partial write to an unallocated block. Don't do
|
---|
1715 | * anything except letting the upper layer know what to do. */
|
---|
1716 | *pcbPreRead = offWrite % getImageBlockSize(&pImage->Header);
|
---|
1717 | *pcbPostRead = getImageBlockSize(&pImage->Header) - cbToWrite - *pcbPreRead;
|
---|
1718 | rc = VERR_VD_BLOCK_FREE;
|
---|
1719 | }
|
---|
1720 | }
|
---|
1721 | else
|
---|
1722 | {
|
---|
1723 | /* Block present in image file, write relevant data. */
|
---|
1724 | uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
|
---|
1725 | + (pImage->offStartData + pImage->offStartBlockData + offWrite);
|
---|
1726 | rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
|
---|
1727 | u64Offset, pIoCtx, cbToWrite, NULL, NULL);
|
---|
1728 | }
|
---|
1729 | } while (0);
|
---|
1730 |
|
---|
1731 | if (pcbWriteProcess)
|
---|
1732 | *pcbWriteProcess = cbToWrite;
|
---|
1733 |
|
---|
1734 | out:
|
---|
1735 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1736 | return rc;
|
---|
1737 | }
|
---|
1738 |
|
---|
1739 | static int vdiFlush(void *pBackendData, PVDIOCTX pIoCtx)
|
---|
1740 | {
|
---|
1741 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
1742 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1743 | int rc = VINF_SUCCESS;
|
---|
1744 |
|
---|
1745 | Assert(pImage);
|
---|
1746 |
|
---|
1747 | rc = vdiFlushImageIoCtx(pImage, pIoCtx);
|
---|
1748 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1749 | return rc;
|
---|
1750 | }
|
---|
1751 |
|
---|
1752 | /** @copydoc VBOXHDDBACKEND::pfnGetVersion */
|
---|
1753 | static unsigned vdiGetVersion(void *pBackendData)
|
---|
1754 | {
|
---|
1755 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
1756 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1757 | unsigned uVersion;
|
---|
1758 |
|
---|
1759 | AssertPtr(pImage);
|
---|
1760 |
|
---|
1761 | if (pImage)
|
---|
1762 | uVersion = pImage->PreHeader.u32Version;
|
---|
1763 | else
|
---|
1764 | uVersion = 0;
|
---|
1765 |
|
---|
1766 | LogFlowFunc(("returns %#x\n", uVersion));
|
---|
1767 | return uVersion;
|
---|
1768 | }
|
---|
1769 |
|
---|
1770 | /** @copydoc VBOXHDDBACKEND::pfnGetSize */
|
---|
1771 | static uint64_t vdiGetSize(void *pBackendData)
|
---|
1772 | {
|
---|
1773 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
1774 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1775 | uint64_t cbSize;
|
---|
1776 |
|
---|
1777 | AssertPtr(pImage);
|
---|
1778 |
|
---|
1779 | if (pImage)
|
---|
1780 | cbSize = getImageDiskSize(&pImage->Header);
|
---|
1781 | else
|
---|
1782 | cbSize = 0;
|
---|
1783 |
|
---|
1784 | LogFlowFunc(("returns %llu\n", cbSize));
|
---|
1785 | return cbSize;
|
---|
1786 | }
|
---|
1787 |
|
---|
1788 | /** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
|
---|
1789 | static uint64_t vdiGetFileSize(void *pBackendData)
|
---|
1790 | {
|
---|
1791 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
1792 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1793 | uint64_t cb = 0;
|
---|
1794 |
|
---|
1795 | AssertPtr(pImage);
|
---|
1796 |
|
---|
1797 | if (pImage)
|
---|
1798 | {
|
---|
1799 | uint64_t cbFile;
|
---|
1800 | if (pImage->pStorage)
|
---|
1801 | {
|
---|
1802 | int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
|
---|
1803 | if (RT_SUCCESS(rc))
|
---|
1804 | cb += cbFile;
|
---|
1805 | }
|
---|
1806 | }
|
---|
1807 |
|
---|
1808 | LogFlowFunc(("returns %lld\n", cb));
|
---|
1809 | return cb;
|
---|
1810 | }
|
---|
1811 |
|
---|
1812 | /** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
|
---|
1813 | static int vdiGetPCHSGeometry(void *pBackendData, PVDGEOMETRY pPCHSGeometry)
|
---|
1814 | {
|
---|
1815 | LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
|
---|
1816 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1817 | int rc;
|
---|
1818 |
|
---|
1819 | AssertPtr(pImage);
|
---|
1820 |
|
---|
1821 | if (pImage)
|
---|
1822 | {
|
---|
1823 | if (pImage->PCHSGeometry.cCylinders)
|
---|
1824 | {
|
---|
1825 | *pPCHSGeometry = pImage->PCHSGeometry;
|
---|
1826 | rc = VINF_SUCCESS;
|
---|
1827 | }
|
---|
1828 | else
|
---|
1829 | rc = VERR_VD_GEOMETRY_NOT_SET;
|
---|
1830 | }
|
---|
1831 | else
|
---|
1832 | rc = VERR_VD_NOT_OPENED;
|
---|
1833 |
|
---|
1834 | LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
|
---|
1835 | return rc;
|
---|
1836 | }
|
---|
1837 |
|
---|
1838 | /** @copydoc VBOXHDDBACKEND::pfnSetPCHSGeometry */
|
---|
1839 | static int vdiSetPCHSGeometry(void *pBackendData, PCVDGEOMETRY pPCHSGeometry)
|
---|
1840 | {
|
---|
1841 | LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
|
---|
1842 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1843 | int rc;
|
---|
1844 |
|
---|
1845 | AssertPtr(pImage);
|
---|
1846 |
|
---|
1847 | if (pImage)
|
---|
1848 | {
|
---|
1849 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
1850 | {
|
---|
1851 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
1852 | goto out;
|
---|
1853 | }
|
---|
1854 |
|
---|
1855 | pImage->PCHSGeometry = *pPCHSGeometry;
|
---|
1856 | rc = VINF_SUCCESS;
|
---|
1857 | }
|
---|
1858 | else
|
---|
1859 | rc = VERR_VD_NOT_OPENED;
|
---|
1860 |
|
---|
1861 | out:
|
---|
1862 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1863 | return rc;
|
---|
1864 | }
|
---|
1865 |
|
---|
1866 | /** @copydoc VBOXHDDBACKEND::pfnGetLCHSGeometry */
|
---|
1867 | static int vdiGetLCHSGeometry(void *pBackendData, PVDGEOMETRY pLCHSGeometry)
|
---|
1868 | {
|
---|
1869 | LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
|
---|
1870 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1871 | int rc;
|
---|
1872 |
|
---|
1873 | AssertPtr(pImage);
|
---|
1874 |
|
---|
1875 | if (pImage)
|
---|
1876 | {
|
---|
1877 | VDIDISKGEOMETRY DummyGeo = { 0, 0, 0, VDI_GEOMETRY_SECTOR_SIZE };
|
---|
1878 | PVDIDISKGEOMETRY pGeometry = getImageLCHSGeometry(&pImage->Header);
|
---|
1879 | if (!pGeometry)
|
---|
1880 | pGeometry = &DummyGeo;
|
---|
1881 |
|
---|
1882 | if ( pGeometry->cCylinders > 0
|
---|
1883 | && pGeometry->cHeads > 0
|
---|
1884 | && pGeometry->cSectors > 0)
|
---|
1885 | {
|
---|
1886 | pLCHSGeometry->cCylinders = pGeometry->cCylinders;
|
---|
1887 | pLCHSGeometry->cHeads = pGeometry->cHeads;
|
---|
1888 | pLCHSGeometry->cSectors = pGeometry->cSectors;
|
---|
1889 | rc = VINF_SUCCESS;
|
---|
1890 | }
|
---|
1891 | else
|
---|
1892 | rc = VERR_VD_GEOMETRY_NOT_SET;
|
---|
1893 | }
|
---|
1894 | else
|
---|
1895 | rc = VERR_VD_NOT_OPENED;
|
---|
1896 |
|
---|
1897 | LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
|
---|
1898 | return rc;
|
---|
1899 | }
|
---|
1900 |
|
---|
1901 | /** @copydoc VBOXHDDBACKEND::pfnSetLCHSGeometry */
|
---|
1902 | static int vdiSetLCHSGeometry(void *pBackendData, PCVDGEOMETRY pLCHSGeometry)
|
---|
1903 | {
|
---|
1904 | LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
|
---|
1905 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1906 | PVDIDISKGEOMETRY pGeometry;
|
---|
1907 | int rc;
|
---|
1908 |
|
---|
1909 | AssertPtr(pImage);
|
---|
1910 |
|
---|
1911 | if (pImage)
|
---|
1912 | {
|
---|
1913 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
1914 | {
|
---|
1915 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
1916 | goto out;
|
---|
1917 | }
|
---|
1918 |
|
---|
1919 | pGeometry = getImageLCHSGeometry(&pImage->Header);
|
---|
1920 | if (pGeometry)
|
---|
1921 | {
|
---|
1922 | pGeometry->cCylinders = pLCHSGeometry->cCylinders;
|
---|
1923 | pGeometry->cHeads = pLCHSGeometry->cHeads;
|
---|
1924 | pGeometry->cSectors = pLCHSGeometry->cSectors;
|
---|
1925 | pGeometry->cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
1926 |
|
---|
1927 | /* Update header information in base image file. */
|
---|
1928 | vdiFlushImage(pImage);
|
---|
1929 | }
|
---|
1930 | rc = VINF_SUCCESS;
|
---|
1931 | }
|
---|
1932 | else
|
---|
1933 | rc = VERR_VD_NOT_OPENED;
|
---|
1934 |
|
---|
1935 | out:
|
---|
1936 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
1937 | return rc;
|
---|
1938 | }
|
---|
1939 |
|
---|
1940 | /** @copydoc VBOXHDDBACKEND::pfnGetImageFlags */
|
---|
1941 | static unsigned vdiGetImageFlags(void *pBackendData)
|
---|
1942 | {
|
---|
1943 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
1944 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1945 | unsigned uImageFlags;
|
---|
1946 |
|
---|
1947 | AssertPtr(pImage);
|
---|
1948 |
|
---|
1949 | if (pImage)
|
---|
1950 | uImageFlags = pImage->uImageFlags;
|
---|
1951 | else
|
---|
1952 | uImageFlags = 0;
|
---|
1953 |
|
---|
1954 | LogFlowFunc(("returns %#x\n", uImageFlags));
|
---|
1955 | return uImageFlags;
|
---|
1956 | }
|
---|
1957 |
|
---|
1958 | /** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
|
---|
1959 | static unsigned vdiGetOpenFlags(void *pBackendData)
|
---|
1960 | {
|
---|
1961 | LogFlowFunc(("pBackendData=%#p\n", pBackendData));
|
---|
1962 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1963 | unsigned uOpenFlags;
|
---|
1964 |
|
---|
1965 | AssertPtr(pImage);
|
---|
1966 |
|
---|
1967 | if (pImage)
|
---|
1968 | uOpenFlags = pImage->uOpenFlags;
|
---|
1969 | else
|
---|
1970 | uOpenFlags = 0;
|
---|
1971 |
|
---|
1972 | LogFlowFunc(("returns %#x\n", uOpenFlags));
|
---|
1973 | return uOpenFlags;
|
---|
1974 | }
|
---|
1975 |
|
---|
1976 | /** @copydoc VBOXHDDBACKEND::pfnSetOpenFlags */
|
---|
1977 | static int vdiSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
|
---|
1978 | {
|
---|
1979 | LogFlowFunc(("pBackendData=%#p uOpenFlags=%#x\n", pBackendData, uOpenFlags));
|
---|
1980 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
1981 | int rc;
|
---|
1982 | const char *pszFilename;
|
---|
1983 |
|
---|
1984 | /* Image must be opened and the new flags must be valid. */
|
---|
1985 | if (!pImage || (uOpenFlags & ~( VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO
|
---|
1986 | | VD_OPEN_FLAGS_ASYNC_IO | VD_OPEN_FLAGS_SHAREABLE
|
---|
1987 | | VD_OPEN_FLAGS_SEQUENTIAL | VD_OPEN_FLAGS_DISCARD
|
---|
1988 | | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)))
|
---|
1989 | {
|
---|
1990 | rc = VERR_INVALID_PARAMETER;
|
---|
1991 | goto out;
|
---|
1992 | }
|
---|
1993 |
|
---|
1994 | /* Implement this operation via reopening the image. */
|
---|
1995 | pszFilename = pImage->pszFilename;
|
---|
1996 | rc = vdiFreeImage(pImage, false);
|
---|
1997 | if (RT_FAILURE(rc))
|
---|
1998 | goto out;
|
---|
1999 | rc = vdiOpenImage(pImage, uOpenFlags);
|
---|
2000 |
|
---|
2001 | out:
|
---|
2002 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
2003 | return rc;
|
---|
2004 | }
|
---|
2005 |
|
---|
2006 | /** @copydoc VBOXHDDBACKEND::pfnGetComment */
|
---|
2007 | static int vdiGetComment(void *pBackendData, char *pszComment,
|
---|
2008 | size_t cbComment)
|
---|
2009 | {
|
---|
2010 | LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
|
---|
2011 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
2012 | int rc = VINF_SUCCESS;
|
---|
2013 |
|
---|
2014 | AssertPtr(pImage);
|
---|
2015 |
|
---|
2016 | if (pImage)
|
---|
2017 | {
|
---|
2018 | char *pszTmp = getImageComment(&pImage->Header);
|
---|
2019 | /* Make this foolproof even if the image doesn't have the zero
|
---|
2020 | * termination. With some luck the repaired header will be saved. */
|
---|
2021 | size_t cb = RTStrNLen(pszTmp, VDI_IMAGE_COMMENT_SIZE);
|
---|
2022 | if (cb == VDI_IMAGE_COMMENT_SIZE)
|
---|
2023 | {
|
---|
2024 | pszTmp[VDI_IMAGE_COMMENT_SIZE-1] = '\0';
|
---|
2025 | cb--;
|
---|
2026 | }
|
---|
2027 | if (cb < cbComment)
|
---|
2028 | {
|
---|
2029 | /* memcpy is much better than strncpy. */
|
---|
2030 | memcpy(pszComment, pszTmp, cb + 1);
|
---|
2031 | }
|
---|
2032 | else
|
---|
2033 | rc = VERR_BUFFER_OVERFLOW;
|
---|
2034 | }
|
---|
2035 | else
|
---|
2036 | rc = VERR_VD_NOT_OPENED;
|
---|
2037 |
|
---|
2038 | LogFlowFunc(("returns %Rrc comment=\"%s\"\n", rc, pszComment));
|
---|
2039 | return rc;
|
---|
2040 | }
|
---|
2041 |
|
---|
2042 | /** @copydoc VBOXHDDBACKEND::pfnGetComment */
|
---|
2043 | static int vdiSetComment(void *pBackendData, const char *pszComment)
|
---|
2044 | {
|
---|
2045 | LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
|
---|
2046 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
2047 | int rc;
|
---|
2048 |
|
---|
2049 | AssertPtr(pImage);
|
---|
2050 |
|
---|
2051 | if (pImage)
|
---|
2052 | {
|
---|
2053 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
2054 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
2055 | else
|
---|
2056 | {
|
---|
2057 | size_t cchComment = pszComment ? strlen(pszComment) : 0;
|
---|
2058 | if (cchComment >= VDI_IMAGE_COMMENT_SIZE)
|
---|
2059 | {
|
---|
2060 | LogFunc(("pszComment is too long, %d bytes!\n", cchComment));
|
---|
2061 | rc = VERR_VD_VDI_COMMENT_TOO_LONG;
|
---|
2062 | goto out;
|
---|
2063 | }
|
---|
2064 |
|
---|
2065 | /* we don't support old style images */
|
---|
2066 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
2067 | {
|
---|
2068 | /*
|
---|
2069 | * Update the comment field, making sure to zero out all of the previous comment.
|
---|
2070 | */
|
---|
2071 | memset(pImage->Header.u.v1.szComment, '\0', VDI_IMAGE_COMMENT_SIZE);
|
---|
2072 | memcpy(pImage->Header.u.v1.szComment, pszComment, cchComment);
|
---|
2073 |
|
---|
2074 | /* write out new the header */
|
---|
2075 | rc = vdiUpdateHeader(pImage);
|
---|
2076 | }
|
---|
2077 | else
|
---|
2078 | rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
2079 | }
|
---|
2080 | }
|
---|
2081 | else
|
---|
2082 | rc = VERR_VD_NOT_OPENED;
|
---|
2083 |
|
---|
2084 | out:
|
---|
2085 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
2086 | return rc;
|
---|
2087 | }
|
---|
2088 |
|
---|
2089 | /** @copydoc VBOXHDDBACKEND::pfnGetUuid */
|
---|
2090 | static int vdiGetUuid(void *pBackendData, PRTUUID pUuid)
|
---|
2091 | {
|
---|
2092 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
2093 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
2094 | int rc;
|
---|
2095 |
|
---|
2096 | AssertPtr(pImage);
|
---|
2097 |
|
---|
2098 | if (pImage)
|
---|
2099 | {
|
---|
2100 | *pUuid = *getImageCreationUUID(&pImage->Header);
|
---|
2101 | rc = VINF_SUCCESS;
|
---|
2102 | }
|
---|
2103 | else
|
---|
2104 | rc = VERR_VD_NOT_OPENED;
|
---|
2105 |
|
---|
2106 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
|
---|
2107 | return rc;
|
---|
2108 | }
|
---|
2109 |
|
---|
2110 | /** @copydoc VBOXHDDBACKEND::pfnSetUuid */
|
---|
2111 | static int vdiSetUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
2112 | {
|
---|
2113 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
2114 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
2115 | int rc = VINF_SUCCESS;
|
---|
2116 |
|
---|
2117 | AssertPtr(pImage);
|
---|
2118 |
|
---|
2119 | if (pImage)
|
---|
2120 | {
|
---|
2121 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
2122 | {
|
---|
2123 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
2124 | pImage->Header.u.v1.uuidCreate = *pUuid;
|
---|
2125 | /* Make it possible to clone old VDIs. */
|
---|
2126 | else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
|
---|
2127 | pImage->Header.u.v0.uuidCreate = *pUuid;
|
---|
2128 | else
|
---|
2129 | {
|
---|
2130 | LogFunc(("Version is not supported!\n"));
|
---|
2131 | rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
2132 | }
|
---|
2133 | }
|
---|
2134 | else
|
---|
2135 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
2136 | }
|
---|
2137 | else
|
---|
2138 | rc = VERR_VD_NOT_OPENED;
|
---|
2139 |
|
---|
2140 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
2141 | return rc;
|
---|
2142 | }
|
---|
2143 |
|
---|
2144 | /** @copydoc VBOXHDDBACKEND::pfnGetModificationUuid */
|
---|
2145 | static int vdiGetModificationUuid(void *pBackendData, PRTUUID pUuid)
|
---|
2146 | {
|
---|
2147 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
2148 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
2149 | int rc;
|
---|
2150 |
|
---|
2151 | AssertPtr(pImage);
|
---|
2152 |
|
---|
2153 | if (pImage)
|
---|
2154 | {
|
---|
2155 | *pUuid = *getImageModificationUUID(&pImage->Header);
|
---|
2156 | rc = VINF_SUCCESS;
|
---|
2157 | }
|
---|
2158 | else
|
---|
2159 | rc = VERR_VD_NOT_OPENED;
|
---|
2160 |
|
---|
2161 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
|
---|
2162 | return rc;
|
---|
2163 | }
|
---|
2164 |
|
---|
2165 | /** @copydoc VBOXHDDBACKEND::pfnSetModificationUuid */
|
---|
2166 | static int vdiSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
2167 | {
|
---|
2168 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
2169 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
2170 | int rc = VINF_SUCCESS;
|
---|
2171 |
|
---|
2172 | AssertPtr(pImage);
|
---|
2173 |
|
---|
2174 | if (pImage)
|
---|
2175 | {
|
---|
2176 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
2177 | {
|
---|
2178 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
2179 | pImage->Header.u.v1.uuidModify = *pUuid;
|
---|
2180 | /* Make it possible to clone old VDIs. */
|
---|
2181 | else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
|
---|
2182 | pImage->Header.u.v0.uuidModify = *pUuid;
|
---|
2183 | else
|
---|
2184 | {
|
---|
2185 | LogFunc(("Version is not supported!\n"));
|
---|
2186 | rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
2187 | }
|
---|
2188 | }
|
---|
2189 | else
|
---|
2190 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
2191 | }
|
---|
2192 | else
|
---|
2193 | rc = VERR_VD_NOT_OPENED;
|
---|
2194 |
|
---|
2195 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
2196 | return rc;
|
---|
2197 | }
|
---|
2198 |
|
---|
2199 | /** @copydoc VBOXHDDBACKEND::pfnGetParentUuid */
|
---|
2200 | static int vdiGetParentUuid(void *pBackendData, PRTUUID pUuid)
|
---|
2201 | {
|
---|
2202 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
2203 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
2204 | int rc;
|
---|
2205 |
|
---|
2206 | AssertPtr(pImage);
|
---|
2207 |
|
---|
2208 | if (pImage)
|
---|
2209 | {
|
---|
2210 | *pUuid = *getImageParentUUID(&pImage->Header);
|
---|
2211 | rc = VINF_SUCCESS;
|
---|
2212 | }
|
---|
2213 | else
|
---|
2214 | rc = VERR_VD_NOT_OPENED;
|
---|
2215 |
|
---|
2216 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
|
---|
2217 | return rc;
|
---|
2218 | }
|
---|
2219 |
|
---|
2220 | /** @copydoc VBOXHDDBACKEND::pfnSetParentUuid */
|
---|
2221 | static int vdiSetParentUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
2222 | {
|
---|
2223 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
2224 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
2225 | int rc = VINF_SUCCESS;
|
---|
2226 |
|
---|
2227 | AssertPtr(pImage);
|
---|
2228 |
|
---|
2229 | if (pImage)
|
---|
2230 | {
|
---|
2231 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
2232 | {
|
---|
2233 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
2234 | pImage->Header.u.v1.uuidLinkage = *pUuid;
|
---|
2235 | /* Make it possible to clone old VDIs. */
|
---|
2236 | else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
|
---|
2237 | pImage->Header.u.v0.uuidLinkage = *pUuid;
|
---|
2238 | else
|
---|
2239 | {
|
---|
2240 | LogFunc(("Version is not supported!\n"));
|
---|
2241 | rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
2242 | }
|
---|
2243 | }
|
---|
2244 | else
|
---|
2245 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
2246 | }
|
---|
2247 | else
|
---|
2248 | rc = VERR_VD_NOT_OPENED;
|
---|
2249 |
|
---|
2250 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
2251 | return rc;
|
---|
2252 | }
|
---|
2253 |
|
---|
2254 | /** @copydoc VBOXHDDBACKEND::pfnGetParentModificationUuid */
|
---|
2255 | static int vdiGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
|
---|
2256 | {
|
---|
2257 | LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
|
---|
2258 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
2259 | int rc;
|
---|
2260 |
|
---|
2261 | AssertPtr(pImage);
|
---|
2262 |
|
---|
2263 | if (pImage)
|
---|
2264 | {
|
---|
2265 | *pUuid = *getImageParentModificationUUID(&pImage->Header);
|
---|
2266 | rc = VINF_SUCCESS;
|
---|
2267 | }
|
---|
2268 | else
|
---|
2269 | rc = VERR_VD_NOT_OPENED;
|
---|
2270 |
|
---|
2271 | LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
|
---|
2272 | return rc;
|
---|
2273 | }
|
---|
2274 |
|
---|
2275 | /** @copydoc VBOXHDDBACKEND::pfnSetParentModificationUuid */
|
---|
2276 | static int vdiSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
|
---|
2277 | {
|
---|
2278 | LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
|
---|
2279 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
2280 | int rc = VINF_SUCCESS;
|
---|
2281 |
|
---|
2282 | AssertPtr(pImage);
|
---|
2283 |
|
---|
2284 | if (pImage)
|
---|
2285 | {
|
---|
2286 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
2287 | {
|
---|
2288 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
|
---|
2289 | pImage->Header.u.v1.uuidParentModify = *pUuid;
|
---|
2290 | else
|
---|
2291 | {
|
---|
2292 | LogFunc(("Version is not supported!\n"));
|
---|
2293 | rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
|
---|
2294 | }
|
---|
2295 | }
|
---|
2296 | else
|
---|
2297 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
2298 | }
|
---|
2299 | else
|
---|
2300 | rc = VERR_VD_NOT_OPENED;
|
---|
2301 |
|
---|
2302 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
2303 | return rc;
|
---|
2304 | }
|
---|
2305 |
|
---|
2306 | /** @copydoc VBOXHDDBACKEND::pfnDump */
|
---|
2307 | static void vdiDump(void *pBackendData)
|
---|
2308 | {
|
---|
2309 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
2310 |
|
---|
2311 | vdIfErrorMessage(pImage->pIfError, "Dumping VDI image \"%s\" mode=%s uOpenFlags=%X File=%#p\n",
|
---|
2312 | pImage->pszFilename,
|
---|
2313 | (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY) ? "r/o" : "r/w",
|
---|
2314 | pImage->uOpenFlags,
|
---|
2315 | pImage->pStorage);
|
---|
2316 | vdIfErrorMessage(pImage->pIfError, "Header: Version=%08X Type=%X Flags=%X Size=%llu\n",
|
---|
2317 | pImage->PreHeader.u32Version,
|
---|
2318 | getImageType(&pImage->Header),
|
---|
2319 | getImageFlags(&pImage->Header),
|
---|
2320 | getImageDiskSize(&pImage->Header));
|
---|
2321 | vdIfErrorMessage(pImage->pIfError, "Header: cbBlock=%u cbBlockExtra=%u cBlocks=%u cBlocksAllocated=%u\n",
|
---|
2322 | getImageBlockSize(&pImage->Header),
|
---|
2323 | getImageExtraBlockSize(&pImage->Header),
|
---|
2324 | getImageBlocks(&pImage->Header),
|
---|
2325 | getImageBlocksAllocated(&pImage->Header));
|
---|
2326 | vdIfErrorMessage(pImage->pIfError, "Header: offBlocks=%u offData=%u\n",
|
---|
2327 | getImageBlocksOffset(&pImage->Header),
|
---|
2328 | getImageDataOffset(&pImage->Header));
|
---|
2329 | PVDIDISKGEOMETRY pg = getImageLCHSGeometry(&pImage->Header);
|
---|
2330 | if (pg)
|
---|
2331 | vdIfErrorMessage(pImage->pIfError, "Header: Geometry: C/H/S=%u/%u/%u cbSector=%u\n",
|
---|
2332 | pg->cCylinders, pg->cHeads, pg->cSectors, pg->cbSector);
|
---|
2333 | vdIfErrorMessage(pImage->pIfError, "Header: uuidCreation={%RTuuid}\n", getImageCreationUUID(&pImage->Header));
|
---|
2334 | vdIfErrorMessage(pImage->pIfError, "Header: uuidModification={%RTuuid}\n", getImageModificationUUID(&pImage->Header));
|
---|
2335 | vdIfErrorMessage(pImage->pIfError, "Header: uuidParent={%RTuuid}\n", getImageParentUUID(&pImage->Header));
|
---|
2336 | if (GET_MAJOR_HEADER_VERSION(&pImage->Header) >= 1)
|
---|
2337 | vdIfErrorMessage(pImage->pIfError, "Header: uuidParentModification={%RTuuid}\n", getImageParentModificationUUID(&pImage->Header));
|
---|
2338 | vdIfErrorMessage(pImage->pIfError, "Image: fFlags=%08X offStartBlocks=%u offStartData=%u\n",
|
---|
2339 | pImage->uImageFlags, pImage->offStartBlocks, pImage->offStartData);
|
---|
2340 | vdIfErrorMessage(pImage->pIfError, "Image: uBlockMask=%08X cbTotalBlockData=%u uShiftOffset2Index=%u offStartBlockData=%u\n",
|
---|
2341 | pImage->uBlockMask,
|
---|
2342 | pImage->cbTotalBlockData,
|
---|
2343 | pImage->uShiftOffset2Index,
|
---|
2344 | pImage->offStartBlockData);
|
---|
2345 |
|
---|
2346 | unsigned uBlock, cBlocksNotFree, cBadBlocks, cBlocks = getImageBlocks(&pImage->Header);
|
---|
2347 | for (uBlock=0, cBlocksNotFree=0, cBadBlocks=0; uBlock<cBlocks; uBlock++)
|
---|
2348 | {
|
---|
2349 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
|
---|
2350 | {
|
---|
2351 | cBlocksNotFree++;
|
---|
2352 | if (pImage->paBlocks[uBlock] >= cBlocks)
|
---|
2353 | cBadBlocks++;
|
---|
2354 | }
|
---|
2355 | }
|
---|
2356 | if (cBlocksNotFree != getImageBlocksAllocated(&pImage->Header))
|
---|
2357 | {
|
---|
2358 | vdIfErrorMessage(pImage->pIfError, "!! WARNING: %u blocks actually allocated (cBlocksAllocated=%u) !!\n",
|
---|
2359 | cBlocksNotFree, getImageBlocksAllocated(&pImage->Header));
|
---|
2360 | }
|
---|
2361 | if (cBadBlocks)
|
---|
2362 | {
|
---|
2363 | vdIfErrorMessage(pImage->pIfError, "!! WARNING: %u bad blocks found !!\n",
|
---|
2364 | cBadBlocks);
|
---|
2365 | }
|
---|
2366 | }
|
---|
2367 |
|
---|
2368 | /** @copydoc VBOXHDDBACKEND::pfnCompact */
|
---|
2369 | static int vdiCompact(void *pBackendData, unsigned uPercentStart,
|
---|
2370 | unsigned uPercentSpan, PVDINTERFACE pVDIfsDisk,
|
---|
2371 | PVDINTERFACE pVDIfsImage, PVDINTERFACE pVDIfsOperation)
|
---|
2372 | {
|
---|
2373 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
2374 | int rc = VINF_SUCCESS;
|
---|
2375 | void *pvBuf = NULL, *pvTmp = NULL;
|
---|
2376 | unsigned *paBlocks2 = NULL;
|
---|
2377 |
|
---|
2378 | int (*pfnParentRead)(void *, uint64_t, void *, size_t) = NULL;
|
---|
2379 | void *pvParent = NULL;
|
---|
2380 | PVDINTERFACEPARENTSTATE pIfParentState = VDIfParentStateGet(pVDIfsOperation);
|
---|
2381 | if (pIfParentState)
|
---|
2382 | {
|
---|
2383 | pfnParentRead = pIfParentState->pfnParentRead;
|
---|
2384 | pvParent = pIfParentState->Core.pvUser;
|
---|
2385 | }
|
---|
2386 |
|
---|
2387 | PFNVDPROGRESS pfnProgress = NULL;
|
---|
2388 | void *pvUser = NULL;
|
---|
2389 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
2390 |
|
---|
2391 | PVDINTERFACEQUERYRANGEUSE pIfQueryRangeUse = VDIfQueryRangeUseGet(pVDIfsOperation);
|
---|
2392 |
|
---|
2393 | do {
|
---|
2394 | AssertBreakStmt(pImage, rc = VERR_INVALID_PARAMETER);
|
---|
2395 |
|
---|
2396 | AssertBreakStmt(!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY),
|
---|
2397 | rc = VERR_VD_IMAGE_READ_ONLY);
|
---|
2398 |
|
---|
2399 | unsigned cBlocks;
|
---|
2400 | unsigned cBlocksToMove = 0;
|
---|
2401 | size_t cbBlock;
|
---|
2402 | cBlocks = getImageBlocks(&pImage->Header);
|
---|
2403 | cbBlock = getImageBlockSize(&pImage->Header);
|
---|
2404 | if (pfnParentRead)
|
---|
2405 | {
|
---|
2406 | pvBuf = RTMemTmpAlloc(cbBlock);
|
---|
2407 | AssertBreakStmt(VALID_PTR(pvBuf), rc = VERR_NO_MEMORY);
|
---|
2408 | }
|
---|
2409 | pvTmp = RTMemTmpAlloc(cbBlock);
|
---|
2410 | AssertBreakStmt(VALID_PTR(pvTmp), rc = VERR_NO_MEMORY);
|
---|
2411 |
|
---|
2412 | uint64_t cbFile;
|
---|
2413 | rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
|
---|
2414 | AssertRCBreak(rc);
|
---|
2415 | unsigned cBlocksAllocated = (unsigned)((cbFile - pImage->offStartData - pImage->offStartBlockData) >> pImage->uShiftOffset2Index);
|
---|
2416 | if (cBlocksAllocated == 0)
|
---|
2417 | {
|
---|
2418 | /* No data blocks in this image, no need to compact. */
|
---|
2419 | rc = VINF_SUCCESS;
|
---|
2420 | break;
|
---|
2421 | }
|
---|
2422 |
|
---|
2423 | /* Allocate block array for back resolving. */
|
---|
2424 | paBlocks2 = (unsigned *)RTMemAlloc(sizeof(unsigned *) * cBlocksAllocated);
|
---|
2425 | AssertBreakStmt(VALID_PTR(paBlocks2), rc = VERR_NO_MEMORY);
|
---|
2426 | /* Fill out back resolving, check/fix allocation errors before
|
---|
2427 | * compacting the image, just to be on the safe side. Update the
|
---|
2428 | * image contents straight away, as this enables cancelling. */
|
---|
2429 | for (unsigned i = 0; i < cBlocksAllocated; i++)
|
---|
2430 | paBlocks2[i] = VDI_IMAGE_BLOCK_FREE;
|
---|
2431 | rc = VINF_SUCCESS;
|
---|
2432 | for (unsigned i = 0; i < cBlocks; i++)
|
---|
2433 | {
|
---|
2434 | VDIIMAGEBLOCKPOINTER ptrBlock = pImage->paBlocks[i];
|
---|
2435 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(ptrBlock))
|
---|
2436 | {
|
---|
2437 | if (ptrBlock < cBlocksAllocated)
|
---|
2438 | {
|
---|
2439 | if (paBlocks2[ptrBlock] == VDI_IMAGE_BLOCK_FREE)
|
---|
2440 | paBlocks2[ptrBlock] = i;
|
---|
2441 | else
|
---|
2442 | {
|
---|
2443 | LogFunc(("Freed cross-linked block %u in file \"%s\"\n",
|
---|
2444 | i, pImage->pszFilename));
|
---|
2445 | pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
|
---|
2446 | rc = vdiUpdateBlockInfo(pImage, i);
|
---|
2447 | if (RT_FAILURE(rc))
|
---|
2448 | break;
|
---|
2449 | }
|
---|
2450 | }
|
---|
2451 | else
|
---|
2452 | {
|
---|
2453 | LogFunc(("Freed out of bounds reference for block %u in file \"%s\"\n",
|
---|
2454 | i, pImage->pszFilename));
|
---|
2455 | pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
|
---|
2456 | rc = vdiUpdateBlockInfo(pImage, i);
|
---|
2457 | if (RT_FAILURE(rc))
|
---|
2458 | break;
|
---|
2459 | }
|
---|
2460 | }
|
---|
2461 | }
|
---|
2462 | if (RT_FAILURE(rc))
|
---|
2463 | break;
|
---|
2464 |
|
---|
2465 | /* Find redundant information and update the block pointers
|
---|
2466 | * accordingly, creating bubbles. Keep disk up to date, as this
|
---|
2467 | * enables cancelling. */
|
---|
2468 | for (unsigned i = 0; i < cBlocks; i++)
|
---|
2469 | {
|
---|
2470 | VDIIMAGEBLOCKPOINTER ptrBlock = pImage->paBlocks[i];
|
---|
2471 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(ptrBlock))
|
---|
2472 | {
|
---|
2473 | /* Block present in image file, read relevant data. */
|
---|
2474 | uint64_t u64Offset = (uint64_t)ptrBlock * pImage->cbTotalBlockData
|
---|
2475 | + (pImage->offStartData + pImage->offStartBlockData);
|
---|
2476 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, u64Offset, pvTmp, cbBlock);
|
---|
2477 | if (RT_FAILURE(rc))
|
---|
2478 | break;
|
---|
2479 |
|
---|
2480 | if (ASMBitFirstSet((volatile void *)pvTmp, (uint32_t)cbBlock * 8) == -1)
|
---|
2481 | {
|
---|
2482 | pImage->paBlocks[i] = VDI_IMAGE_BLOCK_ZERO;
|
---|
2483 | rc = vdiUpdateBlockInfo(pImage, i);
|
---|
2484 | if (RT_FAILURE(rc))
|
---|
2485 | break;
|
---|
2486 | paBlocks2[ptrBlock] = VDI_IMAGE_BLOCK_FREE;
|
---|
2487 | /* Adjust progress info, one block to be relocated. */
|
---|
2488 | cBlocksToMove++;
|
---|
2489 | }
|
---|
2490 | else if (pfnParentRead)
|
---|
2491 | {
|
---|
2492 | rc = pfnParentRead(pvParent, (uint64_t)i * cbBlock, pvBuf, cbBlock);
|
---|
2493 | if (RT_FAILURE(rc))
|
---|
2494 | break;
|
---|
2495 | if (!memcmp(pvTmp, pvBuf, cbBlock))
|
---|
2496 | {
|
---|
2497 | pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
|
---|
2498 | rc = vdiUpdateBlockInfo(pImage, i);
|
---|
2499 | if (RT_FAILURE(rc))
|
---|
2500 | break;
|
---|
2501 | paBlocks2[ptrBlock] = VDI_IMAGE_BLOCK_FREE;
|
---|
2502 | /* Adjust progress info, one block to be relocated. */
|
---|
2503 | cBlocksToMove++;
|
---|
2504 | }
|
---|
2505 | }
|
---|
2506 | }
|
---|
2507 |
|
---|
2508 | /* Check if the range is in use if the block is still allocated. */
|
---|
2509 | ptrBlock = pImage->paBlocks[i];
|
---|
2510 | if ( IS_VDI_IMAGE_BLOCK_ALLOCATED(ptrBlock)
|
---|
2511 | && pIfQueryRangeUse)
|
---|
2512 | {
|
---|
2513 | bool fUsed = true;
|
---|
2514 |
|
---|
2515 | rc = vdIfQueryRangeUse(pIfQueryRangeUse, (uint64_t)i * cbBlock, cbBlock, &fUsed);
|
---|
2516 | if (RT_FAILURE(rc))
|
---|
2517 | break;
|
---|
2518 | if (!fUsed)
|
---|
2519 | {
|
---|
2520 | pImage->paBlocks[i] = VDI_IMAGE_BLOCK_ZERO;
|
---|
2521 | rc = vdiUpdateBlockInfo(pImage, i);
|
---|
2522 | if (RT_FAILURE(rc))
|
---|
2523 | break;
|
---|
2524 | paBlocks2[ptrBlock] = VDI_IMAGE_BLOCK_FREE;
|
---|
2525 | /* Adjust progress info, one block to be relocated. */
|
---|
2526 | cBlocksToMove++;
|
---|
2527 | }
|
---|
2528 | }
|
---|
2529 |
|
---|
2530 | if (pIfProgress && pIfProgress->pfnProgress)
|
---|
2531 | {
|
---|
2532 | rc = pIfProgress->pfnProgress(pIfProgress->Core.pvUser,
|
---|
2533 | (uint64_t)i * uPercentSpan / (cBlocks + cBlocksToMove) + uPercentStart);
|
---|
2534 | if (RT_FAILURE(rc))
|
---|
2535 | break;
|
---|
2536 | }
|
---|
2537 | }
|
---|
2538 | if (RT_FAILURE(rc))
|
---|
2539 | break;
|
---|
2540 |
|
---|
2541 | /* Fill bubbles with other data (if available). */
|
---|
2542 | unsigned cBlocksMoved = 0;
|
---|
2543 | unsigned uBlockUsedPos = cBlocksAllocated;
|
---|
2544 | for (unsigned i = 0; i < cBlocksAllocated; i++)
|
---|
2545 | {
|
---|
2546 | unsigned uBlock = paBlocks2[i];
|
---|
2547 | if (uBlock == VDI_IMAGE_BLOCK_FREE)
|
---|
2548 | {
|
---|
2549 | unsigned uBlockData = VDI_IMAGE_BLOCK_FREE;
|
---|
2550 | while (uBlockUsedPos > i && uBlockData == VDI_IMAGE_BLOCK_FREE)
|
---|
2551 | {
|
---|
2552 | uBlockUsedPos--;
|
---|
2553 | uBlockData = paBlocks2[uBlockUsedPos];
|
---|
2554 | }
|
---|
2555 | /* Terminate early if there is no block which needs copying. */
|
---|
2556 | if (uBlockUsedPos == i)
|
---|
2557 | break;
|
---|
2558 | uint64_t u64Offset = (uint64_t)uBlockUsedPos * pImage->cbTotalBlockData
|
---|
2559 | + (pImage->offStartData + pImage->offStartBlockData);
|
---|
2560 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, u64Offset,
|
---|
2561 | pvTmp, cbBlock);
|
---|
2562 | u64Offset = (uint64_t)i * pImage->cbTotalBlockData
|
---|
2563 | + (pImage->offStartData + pImage->offStartBlockData);
|
---|
2564 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, u64Offset,
|
---|
2565 | pvTmp, cbBlock);
|
---|
2566 | pImage->paBlocks[uBlockData] = i;
|
---|
2567 | setImageBlocksAllocated(&pImage->Header, cBlocksAllocated - cBlocksMoved);
|
---|
2568 | rc = vdiUpdateBlockInfo(pImage, uBlockData);
|
---|
2569 | if (RT_FAILURE(rc))
|
---|
2570 | break;
|
---|
2571 | paBlocks2[i] = uBlockData;
|
---|
2572 | paBlocks2[uBlockUsedPos] = VDI_IMAGE_BLOCK_FREE;
|
---|
2573 | cBlocksMoved++;
|
---|
2574 | }
|
---|
2575 |
|
---|
2576 | if (pIfProgress && pIfProgress->pfnProgress)
|
---|
2577 | {
|
---|
2578 | rc = pIfProgress->pfnProgress(pIfProgress->Core.pvUser,
|
---|
2579 | (uint64_t)(cBlocks + cBlocksMoved) * uPercentSpan / (cBlocks + cBlocksToMove) + uPercentStart);
|
---|
2580 |
|
---|
2581 | if (RT_FAILURE(rc))
|
---|
2582 | break;
|
---|
2583 | }
|
---|
2584 | }
|
---|
2585 | if (RT_FAILURE(rc))
|
---|
2586 | break;
|
---|
2587 |
|
---|
2588 | /* Update image header. */
|
---|
2589 | setImageBlocksAllocated(&pImage->Header, uBlockUsedPos);
|
---|
2590 | vdiUpdateHeader(pImage);
|
---|
2591 |
|
---|
2592 | /* Truncate the image to the proper size to finish compacting. */
|
---|
2593 | rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage,
|
---|
2594 | (uint64_t)uBlockUsedPos * pImage->cbTotalBlockData
|
---|
2595 | + pImage->offStartData + pImage->offStartBlockData);
|
---|
2596 | } while (0);
|
---|
2597 |
|
---|
2598 | if (paBlocks2)
|
---|
2599 | RTMemTmpFree(paBlocks2);
|
---|
2600 | if (pvTmp)
|
---|
2601 | RTMemTmpFree(pvTmp);
|
---|
2602 | if (pvBuf)
|
---|
2603 | RTMemTmpFree(pvBuf);
|
---|
2604 |
|
---|
2605 | if (RT_SUCCESS(rc) && pIfProgress && pIfProgress->pfnProgress)
|
---|
2606 | {
|
---|
2607 | pIfProgress->pfnProgress(pIfProgress->Core.pvUser,
|
---|
2608 | uPercentStart + uPercentSpan);
|
---|
2609 | }
|
---|
2610 |
|
---|
2611 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
2612 | return rc;
|
---|
2613 | }
|
---|
2614 |
|
---|
2615 |
|
---|
2616 | /** @copydoc VBOXHDDBACKEND::pfnResize */
|
---|
2617 | static int vdiResize(void *pBackendData, uint64_t cbSize,
|
---|
2618 | PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
|
---|
2619 | unsigned uPercentStart, unsigned uPercentSpan,
|
---|
2620 | PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
|
---|
2621 | PVDINTERFACE pVDIfsOperation)
|
---|
2622 | {
|
---|
2623 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
2624 | int rc = VINF_SUCCESS;
|
---|
2625 |
|
---|
2626 | PFNVDPROGRESS pfnProgress = NULL;
|
---|
2627 | void *pvUser = NULL;
|
---|
2628 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
2629 |
|
---|
2630 | /*
|
---|
2631 | * Making the image smaller is not supported at the moment.
|
---|
2632 | * Resizing is also not supported for fixed size images and
|
---|
2633 | * very old images.
|
---|
2634 | */
|
---|
2635 | /** @todo implement making the image smaller, it is the responsibility of
|
---|
2636 | * the user to know what he's doing. */
|
---|
2637 | if ( cbSize < getImageDiskSize(&pImage->Header)
|
---|
2638 | || GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0
|
---|
2639 | || pImage->uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
2640 | rc = VERR_NOT_SUPPORTED;
|
---|
2641 | else if (cbSize > getImageDiskSize(&pImage->Header))
|
---|
2642 | {
|
---|
2643 | unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header); /** < Blocks currently allocated, doesn't change during resize */
|
---|
2644 | uint32_t cBlocksNew = cbSize / getImageBlockSize(&pImage->Header); /** < New number of blocks in the image after the resize */
|
---|
2645 | if (cbSize % getImageBlockSize(&pImage->Header))
|
---|
2646 | cBlocksNew++;
|
---|
2647 |
|
---|
2648 | uint32_t cBlocksOld = getImageBlocks(&pImage->Header); /** < Number of blocks before the resize. */
|
---|
2649 | uint64_t cbBlockspaceNew = cBlocksNew * sizeof(VDIIMAGEBLOCKPOINTER); /** < Required space for the block array after the resize. */
|
---|
2650 | uint64_t offStartDataNew = RT_ALIGN_32(pImage->offStartBlocks + cbBlockspaceNew, VDI_DATA_ALIGN); /** < New start offset for block data after the resize */
|
---|
2651 |
|
---|
2652 | if ( pImage->offStartData != offStartDataNew
|
---|
2653 | && cBlocksAllocated > 0)
|
---|
2654 | {
|
---|
2655 | /* Calculate how many sectors need to be relocated. */
|
---|
2656 | uint64_t cbOverlapping = offStartDataNew - pImage->offStartData;
|
---|
2657 | unsigned cBlocksReloc = cbOverlapping / getImageBlockSize(&pImage->Header);
|
---|
2658 | if (cbOverlapping % getImageBlockSize(&pImage->Header))
|
---|
2659 | cBlocksReloc++;
|
---|
2660 |
|
---|
2661 | /* Since only full blocks can be relocated the new data start is
|
---|
2662 | * determined by moving it block by block. */
|
---|
2663 | cBlocksReloc = RT_MIN(cBlocksReloc, cBlocksAllocated);
|
---|
2664 | offStartDataNew = pImage->offStartData;
|
---|
2665 |
|
---|
2666 | /* Do the relocation. */
|
---|
2667 | LogFlow(("Relocating %u blocks\n", cBlocksReloc));
|
---|
2668 |
|
---|
2669 | /*
|
---|
2670 | * Get the blocks we need to relocate first, they are appended to the end
|
---|
2671 | * of the image.
|
---|
2672 | */
|
---|
2673 | void *pvBuf = NULL, *pvZero = NULL;
|
---|
2674 | do
|
---|
2675 | {
|
---|
2676 | /* Allocate data buffer. */
|
---|
2677 | pvBuf = RTMemAllocZ(pImage->cbTotalBlockData);
|
---|
2678 | if (!pvBuf)
|
---|
2679 | {
|
---|
2680 | rc = VERR_NO_MEMORY;
|
---|
2681 | break;
|
---|
2682 | }
|
---|
2683 |
|
---|
2684 | /* Allocate buffer for overwriting with zeroes. */
|
---|
2685 | pvZero = RTMemAllocZ(pImage->cbTotalBlockData);
|
---|
2686 | if (!pvZero)
|
---|
2687 | {
|
---|
2688 | rc = VERR_NO_MEMORY;
|
---|
2689 | break;
|
---|
2690 | }
|
---|
2691 |
|
---|
2692 | for (unsigned i = 0; i < cBlocksReloc; i++)
|
---|
2693 | {
|
---|
2694 | /* Search the index in the block table. */
|
---|
2695 | for (unsigned idxBlock = 0; idxBlock < cBlocksOld; idxBlock++)
|
---|
2696 | {
|
---|
2697 | if (!pImage->paBlocks[idxBlock])
|
---|
2698 | {
|
---|
2699 | /* Read data and append to the end of the image. */
|
---|
2700 | rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
|
---|
2701 | offStartDataNew, pvBuf,
|
---|
2702 | pImage->cbTotalBlockData);
|
---|
2703 | if (RT_FAILURE(rc))
|
---|
2704 | break;
|
---|
2705 |
|
---|
2706 | uint64_t offBlockAppend;
|
---|
2707 | rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &offBlockAppend);
|
---|
2708 | if (RT_FAILURE(rc))
|
---|
2709 | break;
|
---|
2710 |
|
---|
2711 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
|
---|
2712 | offBlockAppend, pvBuf,
|
---|
2713 | pImage->cbTotalBlockData);
|
---|
2714 | if (RT_FAILURE(rc))
|
---|
2715 | break;
|
---|
2716 |
|
---|
2717 | /* Zero out the old block area. */
|
---|
2718 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
|
---|
2719 | offStartDataNew, pvZero,
|
---|
2720 | pImage->cbTotalBlockData);
|
---|
2721 | if (RT_FAILURE(rc))
|
---|
2722 | break;
|
---|
2723 |
|
---|
2724 | /* Update block counter. */
|
---|
2725 | pImage->paBlocks[idxBlock] = cBlocksAllocated - 1;
|
---|
2726 |
|
---|
2727 | /*
|
---|
2728 | * Decrease the block number of all other entries in the array.
|
---|
2729 | * They were moved one block to the front.
|
---|
2730 | * Doing it as a separate step iterating over the array again
|
---|
2731 | * because an error while relocating the block might end up
|
---|
2732 | * in a corrupted image otherwise.
|
---|
2733 | */
|
---|
2734 | for (unsigned idxBlock2 = 0; idxBlock2 < cBlocksOld; idxBlock2++)
|
---|
2735 | {
|
---|
2736 | if ( idxBlock2 != idxBlock
|
---|
2737 | && IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[idxBlock2]))
|
---|
2738 | pImage->paBlocks[idxBlock2]--;
|
---|
2739 | }
|
---|
2740 |
|
---|
2741 | /* Continue with the next block. */
|
---|
2742 | break;
|
---|
2743 | }
|
---|
2744 | }
|
---|
2745 |
|
---|
2746 | if (RT_FAILURE(rc))
|
---|
2747 | break;
|
---|
2748 |
|
---|
2749 | offStartDataNew += pImage->cbTotalBlockData;
|
---|
2750 | }
|
---|
2751 | } while (0);
|
---|
2752 |
|
---|
2753 | if (pvBuf)
|
---|
2754 | RTMemFree(pvBuf);
|
---|
2755 | if (pvZero)
|
---|
2756 | RTMemFree(pvZero);
|
---|
2757 | }
|
---|
2758 |
|
---|
2759 | /*
|
---|
2760 | * We need to update the new offsets for the image data in the out of memory
|
---|
2761 | * case too because we relocated the blocks already.
|
---|
2762 | */
|
---|
2763 | pImage->offStartData = offStartDataNew;
|
---|
2764 | setImageDataOffset(&pImage->Header, offStartDataNew);
|
---|
2765 |
|
---|
2766 | /*
|
---|
2767 | * Relocation done, expand the block array and update the header with
|
---|
2768 | * the new data.
|
---|
2769 | */
|
---|
2770 | if (RT_SUCCESS(rc))
|
---|
2771 | {
|
---|
2772 | PVDIIMAGEBLOCKPOINTER paBlocksNew = (PVDIIMAGEBLOCKPOINTER)RTMemRealloc(pImage->paBlocks, cbBlockspaceNew);
|
---|
2773 | if (paBlocksNew)
|
---|
2774 | {
|
---|
2775 | pImage->paBlocks = paBlocksNew;
|
---|
2776 |
|
---|
2777 | /* Mark the new blocks as unallocated. */
|
---|
2778 | for (unsigned idxBlock = cBlocksOld; idxBlock < cBlocksNew; idxBlock++)
|
---|
2779 | pImage->paBlocks[idxBlock] = VDI_IMAGE_BLOCK_FREE;
|
---|
2780 | }
|
---|
2781 | else
|
---|
2782 | rc = VERR_NO_MEMORY;
|
---|
2783 |
|
---|
2784 | /* Write the block array before updating the rest. */
|
---|
2785 | vdiConvBlocksEndianess(VDIECONV_H2F, pImage->paBlocks, cBlocksNew);
|
---|
2786 | rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, pImage->offStartBlocks,
|
---|
2787 | pImage->paBlocks, cbBlockspaceNew);
|
---|
2788 | vdiConvBlocksEndianess(VDIECONV_F2H, pImage->paBlocks, cBlocksNew);
|
---|
2789 |
|
---|
2790 | if (RT_SUCCESS(rc))
|
---|
2791 | {
|
---|
2792 | /* Update size and new block count. */
|
---|
2793 | setImageDiskSize(&pImage->Header, cbSize);
|
---|
2794 | setImageBlocks(&pImage->Header, cBlocksNew);
|
---|
2795 | /* Update geometry. */
|
---|
2796 | pImage->PCHSGeometry = *pPCHSGeometry;
|
---|
2797 | pImage->cbImage = cbSize;
|
---|
2798 |
|
---|
2799 | PVDIDISKGEOMETRY pGeometry = getImageLCHSGeometry(&pImage->Header);
|
---|
2800 | if (pGeometry)
|
---|
2801 | {
|
---|
2802 | pGeometry->cCylinders = pLCHSGeometry->cCylinders;
|
---|
2803 | pGeometry->cHeads = pLCHSGeometry->cHeads;
|
---|
2804 | pGeometry->cSectors = pLCHSGeometry->cSectors;
|
---|
2805 | pGeometry->cbSector = VDI_GEOMETRY_SECTOR_SIZE;
|
---|
2806 | }
|
---|
2807 | }
|
---|
2808 | }
|
---|
2809 |
|
---|
2810 | /* Update header information in base image file. */
|
---|
2811 | vdiFlushImage(pImage);
|
---|
2812 | }
|
---|
2813 | /* Same size doesn't change the image at all. */
|
---|
2814 |
|
---|
2815 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
2816 | return rc;
|
---|
2817 | }
|
---|
2818 |
|
---|
2819 | /** @copydoc VBOXHDDBACKEND::pfnDiscard */
|
---|
2820 | static DECLCALLBACK(int) vdiDiscard(void *pBackendData, PVDIOCTX pIoCtx,
|
---|
2821 | uint64_t uOffset, size_t cbDiscard,
|
---|
2822 | size_t *pcbPreAllocated,
|
---|
2823 | size_t *pcbPostAllocated,
|
---|
2824 | size_t *pcbActuallyDiscarded,
|
---|
2825 | void **ppbmAllocationBitmap,
|
---|
2826 | unsigned fDiscard)
|
---|
2827 | {
|
---|
2828 | PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
|
---|
2829 | unsigned uBlock;
|
---|
2830 | unsigned offDiscard;
|
---|
2831 | int rc = VINF_SUCCESS;
|
---|
2832 | void *pvBlock = NULL;
|
---|
2833 |
|
---|
2834 | LogFlowFunc(("pBackendData=%#p pIoCtx=%#p uOffset=%llu cbDiscard=%zu pcbPreAllocated=%#p pcbPostAllocated=%#p pcbActuallyDiscarded=%#p ppbmAllocationBitmap=%#p fDiscard=%#x\n",
|
---|
2835 | pBackendData, pIoCtx, uOffset, cbDiscard, pcbPreAllocated, pcbPostAllocated, pcbActuallyDiscarded, ppbmAllocationBitmap, fDiscard));
|
---|
2836 |
|
---|
2837 | AssertPtr(pImage);
|
---|
2838 | Assert(!(uOffset % 512));
|
---|
2839 | Assert(!(cbDiscard % 512));
|
---|
2840 |
|
---|
2841 | AssertMsgReturn(!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY),
|
---|
2842 | ("Image is readonly\n"), VERR_VD_IMAGE_READ_ONLY);
|
---|
2843 | AssertMsgReturn( uOffset + cbDiscard <= getImageDiskSize(&pImage->Header)
|
---|
2844 | && cbDiscard,
|
---|
2845 | ("Invalid parameters uOffset=%llu cbDiscard=%zu\n",
|
---|
2846 | uOffset, cbDiscard),
|
---|
2847 | VERR_INVALID_PARAMETER);
|
---|
2848 |
|
---|
2849 | do
|
---|
2850 | {
|
---|
2851 | AssertMsgBreakStmt(!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY),
|
---|
2852 | ("Image is opened readonly\n"),
|
---|
2853 | rc = VERR_VD_IMAGE_READ_ONLY);
|
---|
2854 |
|
---|
2855 | AssertMsgBreakStmt(cbDiscard,
|
---|
2856 | ("cbDiscard=%u\n", cbDiscard),
|
---|
2857 | rc = VERR_INVALID_PARAMETER);
|
---|
2858 |
|
---|
2859 | /* Calculate starting block number and offset inside it. */
|
---|
2860 | uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
|
---|
2861 | offDiscard = (unsigned)uOffset & pImage->uBlockMask;
|
---|
2862 |
|
---|
2863 | /* Clip range to at most the rest of the block. */
|
---|
2864 | cbDiscard = RT_MIN(cbDiscard, getImageBlockSize(&pImage->Header) - offDiscard);
|
---|
2865 | Assert(!(cbDiscard % 512));
|
---|
2866 |
|
---|
2867 | if (pcbPreAllocated)
|
---|
2868 | *pcbPreAllocated = 0;
|
---|
2869 |
|
---|
2870 | if (pcbPostAllocated)
|
---|
2871 | *pcbPostAllocated = 0;
|
---|
2872 |
|
---|
2873 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
|
---|
2874 | {
|
---|
2875 | uint8_t *pbBlockData;
|
---|
2876 | size_t cbPreAllocated, cbPostAllocated;
|
---|
2877 |
|
---|
2878 | cbPreAllocated = offDiscard % getImageBlockSize(&pImage->Header);
|
---|
2879 | cbPostAllocated = getImageBlockSize(&pImage->Header) - cbDiscard - cbPreAllocated;
|
---|
2880 |
|
---|
2881 | /* Read the block data. */
|
---|
2882 | pvBlock = RTMemAlloc(pImage->cbTotalBlockData);
|
---|
2883 | if (!pvBlock)
|
---|
2884 | {
|
---|
2885 | rc = VERR_NO_MEMORY;
|
---|
2886 | break;
|
---|
2887 | }
|
---|
2888 |
|
---|
2889 | if (!cbPreAllocated && !cbPostAllocated)
|
---|
2890 | {
|
---|
2891 | /*
|
---|
2892 | * Discarding a whole block, don't check for allocated sectors.
|
---|
2893 | * It is possible to just remove the whole block which avoids
|
---|
2894 | * one read and checking the whole block for data.
|
---|
2895 | */
|
---|
2896 | rc = vdiDiscardBlockAsync(pImage, pIoCtx, uBlock, pvBlock);
|
---|
2897 | }
|
---|
2898 | else if (fDiscard & VD_DISCARD_MARK_UNUSED)
|
---|
2899 | {
|
---|
2900 | /* Just zero out the given range. */
|
---|
2901 | memset(pvBlock, 0, cbDiscard);
|
---|
2902 |
|
---|
2903 | uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData + pImage->offStartData + offDiscard;
|
---|
2904 | rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
|
---|
2905 | u64Offset, pvBlock, cbDiscard, pIoCtx,
|
---|
2906 | NULL, NULL);
|
---|
2907 | RTMemFree(pvBlock);
|
---|
2908 | }
|
---|
2909 | else
|
---|
2910 | {
|
---|
2911 | /*
|
---|
2912 | * Read complete block as metadata, the I/O context has no memory buffer
|
---|
2913 | * and we need to access the content directly anyway.
|
---|
2914 | */
|
---|
2915 | PVDMETAXFER pMetaXfer;
|
---|
2916 | pbBlockData = (uint8_t *)pvBlock + pImage->offStartBlockData;
|
---|
2917 |
|
---|
2918 | uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData + pImage->offStartData;
|
---|
2919 | rc = vdIfIoIntFileReadMeta(pImage->pIfIo, pImage->pStorage, u64Offset,
|
---|
2920 | pbBlockData, pImage->cbTotalBlockData,
|
---|
2921 | pIoCtx, &pMetaXfer, NULL, NULL);
|
---|
2922 | if (RT_FAILURE(rc))
|
---|
2923 | {
|
---|
2924 | RTMemFree(pvBlock);
|
---|
2925 | break;
|
---|
2926 | }
|
---|
2927 |
|
---|
2928 | vdIfIoIntMetaXferRelease(pImage->pIfIo, pMetaXfer);
|
---|
2929 |
|
---|
2930 | /* Clear data. */
|
---|
2931 | memset(pbBlockData + offDiscard , 0, cbDiscard);
|
---|
2932 |
|
---|
2933 | Assert(!(cbDiscard % 4));
|
---|
2934 | Assert(getImageBlockSize(&pImage->Header) * 8 <= UINT32_MAX);
|
---|
2935 | if (ASMBitFirstSet((volatile void *)pbBlockData, getImageBlockSize(&pImage->Header) * 8) == -1)
|
---|
2936 | rc = vdiDiscardBlockAsync(pImage, pIoCtx, uBlock, pvBlock);
|
---|
2937 | else
|
---|
2938 | {
|
---|
2939 | /* Block has data, create allocation bitmap. */
|
---|
2940 | *pcbPreAllocated = cbPreAllocated;
|
---|
2941 | *pcbPostAllocated = cbPostAllocated;
|
---|
2942 | *ppbmAllocationBitmap = vdiAllocationBitmapCreate(pbBlockData, getImageBlockSize(&pImage->Header));
|
---|
2943 | if (RT_UNLIKELY(!*ppbmAllocationBitmap))
|
---|
2944 | rc = VERR_NO_MEMORY;
|
---|
2945 | else
|
---|
2946 | rc = VERR_VD_DISCARD_ALIGNMENT_NOT_MET;
|
---|
2947 |
|
---|
2948 | RTMemFree(pvBlock);
|
---|
2949 | }
|
---|
2950 | } /* if: no complete block discarded */
|
---|
2951 | } /* if: Block is allocated. */
|
---|
2952 | /* else: nothing to do. */
|
---|
2953 | } while (0);
|
---|
2954 |
|
---|
2955 | if (pcbActuallyDiscarded)
|
---|
2956 | *pcbActuallyDiscarded = cbDiscard;
|
---|
2957 |
|
---|
2958 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
2959 | return rc;
|
---|
2960 | }
|
---|
2961 |
|
---|
2962 | /** @copydoc VBOXHDDBACKEND::pfnRepair */
|
---|
2963 | static DECLCALLBACK(int) vdiRepair(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
|
---|
2964 | PVDINTERFACE pVDIfsImage, uint32_t fFlags)
|
---|
2965 | {
|
---|
2966 | LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p\n", pszFilename, pVDIfsDisk, pVDIfsImage));
|
---|
2967 | int rc;
|
---|
2968 | PVDINTERFACEERROR pIfError;
|
---|
2969 | PVDINTERFACEIOINT pIfIo;
|
---|
2970 | PVDIOSTORAGE pStorage;
|
---|
2971 | uint64_t cbFile;
|
---|
2972 | PVDIIMAGEBLOCKPOINTER paBlocks = NULL;
|
---|
2973 | uint32_t *pu32BlockBitmap = NULL;
|
---|
2974 | VDIPREHEADER PreHdr;
|
---|
2975 | VDIHEADER Hdr;
|
---|
2976 |
|
---|
2977 | pIfIo = VDIfIoIntGet(pVDIfsImage);
|
---|
2978 | AssertPtrReturn(pIfIo, VERR_INVALID_PARAMETER);
|
---|
2979 |
|
---|
2980 | pIfError = VDIfErrorGet(pVDIfsDisk);
|
---|
2981 |
|
---|
2982 | do
|
---|
2983 | {
|
---|
2984 | bool fRepairHdr = false;
|
---|
2985 | bool fRepairBlockArray = false;
|
---|
2986 |
|
---|
2987 | rc = vdIfIoIntFileOpen(pIfIo, pszFilename,
|
---|
2988 | VDOpenFlagsToFileOpenFlags( fFlags & VD_REPAIR_DRY_RUN
|
---|
2989 | ? VD_OPEN_FLAGS_READONLY
|
---|
2990 | : 0,
|
---|
2991 | false /* fCreate */),
|
---|
2992 | &pStorage);
|
---|
2993 | if (RT_FAILURE(rc))
|
---|
2994 | {
|
---|
2995 | rc = vdIfError(pIfError, rc, RT_SRC_POS, "VDI: Failed to open image \"%s\"", pszFilename);
|
---|
2996 | break;
|
---|
2997 | }
|
---|
2998 |
|
---|
2999 | rc = vdIfIoIntFileGetSize(pIfIo, pStorage, &cbFile);
|
---|
3000 | if (RT_FAILURE(rc))
|
---|
3001 | {
|
---|
3002 | rc = vdIfError(pIfError, rc, RT_SRC_POS, "VDI: Failed to query image size");
|
---|
3003 | break;
|
---|
3004 | }
|
---|
3005 |
|
---|
3006 | /* Read pre-header. */
|
---|
3007 | rc = vdIfIoIntFileReadSync(pIfIo, pStorage, 0, &PreHdr, sizeof(PreHdr));
|
---|
3008 | if (RT_FAILURE(rc))
|
---|
3009 | {
|
---|
3010 | rc = vdIfError(pIfError, rc, RT_SRC_POS, N_("VDI: Error reading pre-header in '%s'"), pszFilename);
|
---|
3011 | break;
|
---|
3012 | }
|
---|
3013 | vdiConvPreHeaderEndianess(VDIECONV_F2H, &PreHdr, &PreHdr);
|
---|
3014 | rc = vdiValidatePreHeader(&PreHdr);
|
---|
3015 | if (RT_FAILURE(rc))
|
---|
3016 | {
|
---|
3017 | rc = vdIfError(pIfError, VERR_VD_IMAGE_REPAIR_IMPOSSIBLE, RT_SRC_POS,
|
---|
3018 | N_("VDI: invalid pre-header in '%s'"), pszFilename);
|
---|
3019 | break;
|
---|
3020 | }
|
---|
3021 |
|
---|
3022 | /* Read header. */
|
---|
3023 | Hdr.uVersion = PreHdr.u32Version;
|
---|
3024 | switch (GET_MAJOR_HEADER_VERSION(&Hdr))
|
---|
3025 | {
|
---|
3026 | case 0:
|
---|
3027 | rc = vdIfIoIntFileReadSync(pIfIo, pStorage, sizeof(PreHdr),
|
---|
3028 | &Hdr.u.v0, sizeof(Hdr.u.v0));
|
---|
3029 | if (RT_FAILURE(rc))
|
---|
3030 | rc = vdIfError(pIfError, rc, RT_SRC_POS, N_("VDI: error reading v0 header in '%s'"),
|
---|
3031 | pszFilename);
|
---|
3032 | vdiConvHeaderEndianessV0(VDIECONV_F2H, &Hdr.u.v0, &Hdr.u.v0);
|
---|
3033 | break;
|
---|
3034 | case 1:
|
---|
3035 | rc = vdIfIoIntFileReadSync(pIfIo, pStorage, sizeof(PreHdr),
|
---|
3036 | &Hdr.u.v1, sizeof(Hdr.u.v1));
|
---|
3037 | if (RT_FAILURE(rc))
|
---|
3038 | {
|
---|
3039 | rc = vdIfError(pIfError, rc, RT_SRC_POS, N_("VDI: error reading v1 header in '%s'"),
|
---|
3040 | pszFilename);
|
---|
3041 | }
|
---|
3042 | vdiConvHeaderEndianessV1(VDIECONV_F2H, &Hdr.u.v1, &Hdr.u.v1);
|
---|
3043 | if (Hdr.u.v1.cbHeader >= sizeof(Hdr.u.v1plus))
|
---|
3044 | {
|
---|
3045 | /* Read the VDI 1.1+ header completely. */
|
---|
3046 | rc = vdIfIoIntFileReadSync(pIfIo, pStorage, sizeof(PreHdr),
|
---|
3047 | &Hdr.u.v1plus, sizeof(Hdr.u.v1plus));
|
---|
3048 | if (RT_FAILURE(rc))
|
---|
3049 | rc = vdIfError(pIfError, rc, RT_SRC_POS, N_("VDI: error reading v1.1+ header in '%s'"),
|
---|
3050 | pszFilename);
|
---|
3051 | vdiConvHeaderEndianessV1p(VDIECONV_F2H, &Hdr.u.v1plus, &Hdr.u.v1plus);
|
---|
3052 | }
|
---|
3053 | break;
|
---|
3054 | default:
|
---|
3055 | rc = vdIfError(pIfError, VERR_VD_IMAGE_REPAIR_IMPOSSIBLE, RT_SRC_POS,
|
---|
3056 | N_("VDI: unsupported major version %u in '%s'"),
|
---|
3057 | GET_MAJOR_HEADER_VERSION(&Hdr), pszFilename);
|
---|
3058 | break;
|
---|
3059 | }
|
---|
3060 |
|
---|
3061 | if (RT_SUCCESS(rc))
|
---|
3062 | {
|
---|
3063 | rc = vdiValidateHeader(&Hdr);
|
---|
3064 | if (RT_FAILURE(rc))
|
---|
3065 | {
|
---|
3066 | rc = vdIfError(pIfError, VERR_VD_IMAGE_REPAIR_IMPOSSIBLE, RT_SRC_POS,
|
---|
3067 | N_("VDI: invalid header in '%s'"), pszFilename);
|
---|
3068 | break;
|
---|
3069 | }
|
---|
3070 | }
|
---|
3071 |
|
---|
3072 | /* Setup image parameters by header. */
|
---|
3073 | uint64_t offStartBlocks, offStartData;
|
---|
3074 | size_t cbTotalBlockData;
|
---|
3075 |
|
---|
3076 | offStartBlocks = getImageBlocksOffset(&Hdr);
|
---|
3077 | offStartData = getImageDataOffset(&Hdr);
|
---|
3078 | cbTotalBlockData = getImageExtraBlockSize(&Hdr) + getImageBlockSize(&Hdr);
|
---|
3079 |
|
---|
3080 | /* Allocate memory for blocks array. */
|
---|
3081 | paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&Hdr));
|
---|
3082 | if (!paBlocks)
|
---|
3083 | {
|
---|
3084 | rc = vdIfError(pIfError, VERR_NO_MEMORY, RT_SRC_POS,
|
---|
3085 | "Failed to allocate memory for block array");
|
---|
3086 | break;
|
---|
3087 | }
|
---|
3088 |
|
---|
3089 | /* Read blocks array. */
|
---|
3090 | rc = vdIfIoIntFileReadSync(pIfIo, pStorage, offStartBlocks, paBlocks,
|
---|
3091 | getImageBlocks(&Hdr) * sizeof(VDIIMAGEBLOCKPOINTER));
|
---|
3092 | if (RT_FAILURE(rc))
|
---|
3093 | {
|
---|
3094 | rc = vdIfError(pIfError, VERR_VD_IMAGE_REPAIR_IMPOSSIBLE, RT_SRC_POS,
|
---|
3095 | "Failed to read block array (at %llu), %Rrc",
|
---|
3096 | offStartBlocks, rc);
|
---|
3097 | break;
|
---|
3098 | }
|
---|
3099 | vdiConvBlocksEndianess(VDIECONV_F2H, paBlocks, getImageBlocks(&Hdr));
|
---|
3100 |
|
---|
3101 | pu32BlockBitmap = (uint32_t *)RTMemAllocZ(RT_ALIGN_Z(getImageBlocks(&Hdr) / 8, 4));
|
---|
3102 | if (!pu32BlockBitmap)
|
---|
3103 | {
|
---|
3104 | rc = vdIfError(pIfError, VERR_NO_MEMORY, RT_SRC_POS,
|
---|
3105 | "Failed to allocate memory for block bitmap");
|
---|
3106 | break;
|
---|
3107 | }
|
---|
3108 |
|
---|
3109 | for (uint32_t i = 0; i < getImageBlocks(&Hdr); i++)
|
---|
3110 | {
|
---|
3111 | if (IS_VDI_IMAGE_BLOCK_ALLOCATED(paBlocks[i]))
|
---|
3112 | {
|
---|
3113 | uint64_t offBlock = (uint64_t)paBlocks[i] * cbTotalBlockData
|
---|
3114 | + offStartData;
|
---|
3115 |
|
---|
3116 | /*
|
---|
3117 | * Check that the offsets are valid (inside of the image) and
|
---|
3118 | * that there are no double references.
|
---|
3119 | */
|
---|
3120 | if (offBlock + cbTotalBlockData > cbFile)
|
---|
3121 | {
|
---|
3122 | vdIfErrorMessage(pIfError, "Entry %u points to invalid offset %llu, clearing\n",
|
---|
3123 | i, offBlock);
|
---|
3124 | paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
|
---|
3125 | fRepairBlockArray = true;
|
---|
3126 | }
|
---|
3127 | else if (ASMBitTestAndSet(pu32BlockBitmap, paBlocks[i]))
|
---|
3128 | {
|
---|
3129 | vdIfErrorMessage(pIfError, "Entry %u points to an already referenced data block, clearing\n",
|
---|
3130 | i);
|
---|
3131 | paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
|
---|
3132 | fRepairBlockArray = true;
|
---|
3133 | }
|
---|
3134 | }
|
---|
3135 | }
|
---|
3136 |
|
---|
3137 | /* Write repaired structures now. */
|
---|
3138 | if (!fRepairBlockArray)
|
---|
3139 | vdIfErrorMessage(pIfError, "VDI image is in a consistent state, no repair required\n");
|
---|
3140 | else if (!(fFlags & VD_REPAIR_DRY_RUN))
|
---|
3141 | {
|
---|
3142 | vdIfErrorMessage(pIfError, "Writing repaired block allocation table...\n");
|
---|
3143 |
|
---|
3144 | vdiConvBlocksEndianess(VDIECONV_H2F, paBlocks, getImageBlocks(&Hdr));
|
---|
3145 | rc = vdIfIoIntFileWriteSync(pIfIo, pStorage, offStartBlocks, paBlocks,
|
---|
3146 | getImageBlocks(&Hdr) * sizeof(VDIIMAGEBLOCKPOINTER));
|
---|
3147 | if (RT_FAILURE(rc))
|
---|
3148 | {
|
---|
3149 | rc = vdIfError(pIfError, VERR_VD_IMAGE_REPAIR_IMPOSSIBLE, RT_SRC_POS,
|
---|
3150 | "Could not write repaired block allocation table (at %llu), %Rrc",
|
---|
3151 | offStartBlocks, rc);
|
---|
3152 | break;
|
---|
3153 | }
|
---|
3154 | }
|
---|
3155 |
|
---|
3156 | vdIfErrorMessage(pIfError, "Corrupted VDI image repaired successfully\n");
|
---|
3157 | } while(0);
|
---|
3158 |
|
---|
3159 | if (paBlocks)
|
---|
3160 | RTMemFree(paBlocks);
|
---|
3161 |
|
---|
3162 | if (pu32BlockBitmap)
|
---|
3163 | RTMemFree(pu32BlockBitmap);
|
---|
3164 |
|
---|
3165 | if (pStorage)
|
---|
3166 | vdIfIoIntFileClose(pIfIo, pStorage);
|
---|
3167 |
|
---|
3168 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
3169 | return rc;
|
---|
3170 | }
|
---|
3171 |
|
---|
3172 | VBOXHDDBACKEND g_VDIBackend =
|
---|
3173 | {
|
---|
3174 | /* pszBackendName */
|
---|
3175 | "VDI",
|
---|
3176 | /* cbSize */
|
---|
3177 | sizeof(VBOXHDDBACKEND),
|
---|
3178 | /* uBackendCaps */
|
---|
3179 | VD_CAP_UUID | VD_CAP_CREATE_FIXED | VD_CAP_CREATE_DYNAMIC
|
---|
3180 | | VD_CAP_DIFF | VD_CAP_FILE | VD_CAP_ASYNC | VD_CAP_VFS | VD_CAP_DISCARD,
|
---|
3181 | /* paFileExtensions */
|
---|
3182 | s_aVdiFileExtensions,
|
---|
3183 | /* paConfigInfo */
|
---|
3184 | NULL,
|
---|
3185 | /* hPlugin */
|
---|
3186 | NIL_RTLDRMOD,
|
---|
3187 | /* pfnCheckIfValid */
|
---|
3188 | vdiCheckIfValid,
|
---|
3189 | /* pfnOpen */
|
---|
3190 | vdiOpen,
|
---|
3191 | /* pfnCreate */
|
---|
3192 | vdiCreate,
|
---|
3193 | /* pfnRename */
|
---|
3194 | vdiRename,
|
---|
3195 | /* pfnClose */
|
---|
3196 | vdiClose,
|
---|
3197 | /* pfnRead */
|
---|
3198 | vdiRead,
|
---|
3199 | /* pfnWrite */
|
---|
3200 | vdiWrite,
|
---|
3201 | /* pfnFlush */
|
---|
3202 | vdiFlush,
|
---|
3203 | /* pfnDiscard */
|
---|
3204 | vdiDiscard,
|
---|
3205 | /* pfnGetVersion */
|
---|
3206 | vdiGetVersion,
|
---|
3207 | /* pfnGetSize */
|
---|
3208 | vdiGetSize,
|
---|
3209 | /* pfnGetFileSize */
|
---|
3210 | vdiGetFileSize,
|
---|
3211 | /* pfnGetPCHSGeometry */
|
---|
3212 | vdiGetPCHSGeometry,
|
---|
3213 | /* pfnSetPCHSGeometry */
|
---|
3214 | vdiSetPCHSGeometry,
|
---|
3215 | /* pfnGetLCHSGeometry */
|
---|
3216 | vdiGetLCHSGeometry,
|
---|
3217 | /* pfnSetLCHSGeometry */
|
---|
3218 | vdiSetLCHSGeometry,
|
---|
3219 | /* pfnGetImageFlags */
|
---|
3220 | vdiGetImageFlags,
|
---|
3221 | /* pfnGetOpenFlags */
|
---|
3222 | vdiGetOpenFlags,
|
---|
3223 | /* pfnSetOpenFlags */
|
---|
3224 | vdiSetOpenFlags,
|
---|
3225 | /* pfnGetComment */
|
---|
3226 | vdiGetComment,
|
---|
3227 | /* pfnSetComment */
|
---|
3228 | vdiSetComment,
|
---|
3229 | /* pfnGetUuid */
|
---|
3230 | vdiGetUuid,
|
---|
3231 | /* pfnSetUuid */
|
---|
3232 | vdiSetUuid,
|
---|
3233 | /* pfnGetModificationUuid */
|
---|
3234 | vdiGetModificationUuid,
|
---|
3235 | /* pfnSetModificationUuid */
|
---|
3236 | vdiSetModificationUuid,
|
---|
3237 | /* pfnGetParentUuid */
|
---|
3238 | vdiGetParentUuid,
|
---|
3239 | /* pfnSetParentUuid */
|
---|
3240 | vdiSetParentUuid,
|
---|
3241 | /* pfnGetParentModificationUuid */
|
---|
3242 | vdiGetParentModificationUuid,
|
---|
3243 | /* pfnSetParentModificationUuid */
|
---|
3244 | vdiSetParentModificationUuid,
|
---|
3245 | /* pfnDump */
|
---|
3246 | vdiDump,
|
---|
3247 | /* pfnGetTimeStamp */
|
---|
3248 | NULL,
|
---|
3249 | /* pfnGetParentTimeStamp */
|
---|
3250 | NULL,
|
---|
3251 | /* pfnSetParentTimeStamp */
|
---|
3252 | NULL,
|
---|
3253 | /* pfnGetParentFilename */
|
---|
3254 | NULL,
|
---|
3255 | /* pfnSetParentFilename */
|
---|
3256 | NULL,
|
---|
3257 | /* pfnComposeLocation */
|
---|
3258 | genericFileComposeLocation,
|
---|
3259 | /* pfnComposeName */
|
---|
3260 | genericFileComposeName,
|
---|
3261 | /* pfnCompact */
|
---|
3262 | vdiCompact,
|
---|
3263 | /* pfnResize */
|
---|
3264 | vdiResize,
|
---|
3265 | /* pfnRepair */
|
---|
3266 | vdiRepair
|
---|
3267 | };
|
---|