VirtualBox

source: vbox/trunk/src/VBox/Storage/VDI.cpp@ 44233

Last change on this file since 44233 was 44233, checked in by vboxsync, 12 years ago

Storage: Preparations for the sync/async I/O unification

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 133.6 KB
Line 
1/* $Id: VDI.cpp 44233 2013-01-04 20:39:56Z vboxsync $ */
2/** @file
3 * Virtual Disk Image (VDI), Core Code.
4 */
5
6/*
7 * Copyright (C) 2006-2011 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. */
44static const VDFILEEXTENSION s_aVdiFileExtensions[] =
45{
46 {"vdi", VDTYPE_HDD},
47 {NULL, VDTYPE_INVALID}
48};
49
50/*******************************************************************************
51* Internal Functions *
52*******************************************************************************/
53static unsigned getPowerOfTwo(unsigned uNumber);
54static void vdiInitPreHeader(PVDIPREHEADER pPreHdr);
55static int vdiValidatePreHeader(PVDIPREHEADER pPreHdr);
56static void vdiInitHeader(PVDIHEADER pHeader, uint32_t uImageFlags,
57 const char *pszComment, uint64_t cbDisk,
58 uint32_t cbBlock, uint32_t cbBlockExtra);
59static int vdiValidateHeader(PVDIHEADER pHeader);
60static void vdiSetupImageDesc(PVDIIMAGEDESC pImage);
61static int vdiUpdateHeader(PVDIIMAGEDESC pImage);
62static int vdiUpdateBlockInfo(PVDIIMAGEDESC pImage, unsigned uBlock);
63static int vdiUpdateHeaderAsync(PVDIIMAGEDESC pImage, PVDIOCTX pIoCtx);
64static 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 */
73static 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 */
87static 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 */
102static 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 */
124static 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 */
152static 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 */
184static 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 */
194static 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 */
210static 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 */
251static 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 */
267static 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 */
278static 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 */
293static 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 */
306static 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 */
326static 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 */
382static 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 */
502static 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 */
518static 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
734out:
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 */
746static 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
924out:
925 if (RT_FAILURE(rc))
926 vdiFreeImage(pImage, false);
927 return rc;
928}
929
930/**
931 * Internal: Save header to file.
932 */
933static 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 */
973static 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 */
1017static 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 */
1037static 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 */
1064static int vdiFlushImageAsync(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 * Internal: Discard a whole block from the image filling the created hole with
1085 * data from another block.
1086 *
1087 * @returns VBox status code.
1088 * @param pImage VDI image instance data.
1089 * @param uBlock The block to discard.
1090 * @param pvBlock Memory to use for the I/O.
1091 */
1092static int vdiDiscardBlock(PVDIIMAGEDESC pImage, unsigned uBlock, void *pvBlock)
1093{
1094 int rc = VINF_SUCCESS;
1095 uint64_t cbImage;
1096 unsigned idxLastBlock = getImageBlocksAllocated(&pImage->Header) - 1;
1097 unsigned uBlockLast = pImage->paBlocksRev[idxLastBlock];
1098 VDIIMAGEBLOCKPOINTER ptrBlockDiscard = pImage->paBlocks[uBlock];
1099
1100 LogFlowFunc(("pImage=%#p uBlock=%u pvBlock=%#p\n",
1101 pImage, uBlock, pvBlock));
1102
1103 pImage->paBlocksRev[idxLastBlock] = VDI_IMAGE_BLOCK_FREE;
1104
1105 do
1106 {
1107 /*
1108 * The block is empty, remove it.
1109 * Read the last block of the image first.
1110 */
1111 if (idxLastBlock != ptrBlockDiscard)
1112 {
1113 uint64_t u64Offset;
1114
1115 LogFlowFunc(("Moving block [%u]=%u into [%u]=%u\n",
1116 uBlockLast, idxLastBlock, uBlock, pImage->paBlocks[uBlock]));
1117
1118 u64Offset = (uint64_t)idxLastBlock * pImage->cbTotalBlockData + pImage->offStartData;
1119 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, u64Offset,
1120 pvBlock, pImage->cbTotalBlockData);
1121 if (RT_FAILURE(rc))
1122 break;
1123
1124 /* Write to the now unallocated block. */
1125 u64Offset = (uint64_t)ptrBlockDiscard * pImage->cbTotalBlockData + pImage->offStartData;
1126 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, u64Offset,
1127 pvBlock, pImage->cbTotalBlockData);
1128 if (RT_FAILURE(rc))
1129 break;
1130
1131 /* Update block and reverse block tables. */
1132 pImage->paBlocks[uBlockLast] = ptrBlockDiscard;
1133 pImage->paBlocksRev[ptrBlockDiscard] = uBlockLast;
1134 rc = vdiUpdateBlockInfo(pImage, uBlockLast);
1135 if (RT_FAILURE(rc))
1136 break;
1137 }
1138 else
1139 LogFlowFunc(("Discard last block [%u]=%u\n", uBlock, pImage->paBlocks[uBlock]));
1140
1141 pImage->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
1142
1143 /* Update the block pointers. */
1144 setImageBlocksAllocated(&pImage->Header, idxLastBlock);
1145 rc = vdiUpdateBlockInfo(pImage, uBlock);
1146 if (RT_FAILURE(rc))
1147 break;
1148
1149 pImage->cbImage -= pImage->cbTotalBlockData;
1150 LogFlowFunc(("Set new size %llu\n", pImage->cbImage));
1151 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, pImage->cbImage);
1152 } while (0);
1153
1154 LogFlowFunc(("returns rc=%Rrc\n", rc));
1155 return rc;
1156}
1157
1158/**
1159 * Completion callback for meta/userdata reads or writes.
1160 *
1161 * @return VBox status code.
1162 * VINF_SUCCESS if everything was successful and the transfer can continue.
1163 * VERR_VD_ASYNC_IO_IN_PROGRESS if there is another data transfer pending.
1164 * @param pBackendData The opaque backend data.
1165 * @param pIoCtx I/O context associated with this request.
1166 * @param pvUser Opaque user data passed during a read/write request.
1167 * @param rcReq Status code for the completed request.
1168 */
1169static DECLCALLBACK(int) vdiDiscardBlockAsyncUpdate(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
1170{
1171 int rc = VINF_SUCCESS;
1172 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1173 PVDIBLOCKDISCARDASYNC pDiscardAsync = (PVDIBLOCKDISCARDASYNC)pvUser;
1174
1175 switch (pDiscardAsync->enmState)
1176 {
1177 case VDIBLOCKDISCARDSTATE_READ_BLOCK:
1178 {
1179 PVDMETAXFER pMetaXfer;
1180 uint64_t u64Offset = (uint64_t)pDiscardAsync->idxLastBlock * pImage->cbTotalBlockData + pImage->offStartData;
1181 rc = vdIfIoIntFileReadMeta(pImage->pIfIo, pImage->pStorage, u64Offset,
1182 pDiscardAsync->pvBlock, pImage->cbTotalBlockData, pIoCtx,
1183 &pMetaXfer, vdiDiscardBlockAsyncUpdate, pDiscardAsync);
1184 if (RT_FAILURE(rc))
1185 break;
1186
1187 /* Release immediately and go to next step. */
1188 vdIfIoIntMetaXferRelease(pImage->pIfIo, pMetaXfer);
1189 pDiscardAsync->enmState = VDIBLOCKDISCARDSTATE_WRITE_BLOCK;
1190 }
1191 case VDIBLOCKDISCARDSTATE_WRITE_BLOCK:
1192 {
1193 /* Block read complete. Write to the new location (discarded block). */
1194 uint64_t u64Offset = (uint64_t)pDiscardAsync->ptrBlockDiscard * pImage->cbTotalBlockData + pImage->offStartData;
1195 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage, u64Offset,
1196 pDiscardAsync->pvBlock, pImage->cbTotalBlockData, pIoCtx,
1197 vdiDiscardBlockAsyncUpdate, pDiscardAsync);
1198
1199 pDiscardAsync->enmState = VDIBLOCKDISCARDSTATE_UPDATE_METADATA;
1200 if (RT_FAILURE(rc))
1201 break;
1202 }
1203 case VDIBLOCKDISCARDSTATE_UPDATE_METADATA:
1204 {
1205 int rc2;
1206 uint64_t cbImage;
1207
1208 /* Block write complete. Update metadata. */
1209 pImage->paBlocksRev[pDiscardAsync->idxLastBlock] = VDI_IMAGE_BLOCK_FREE;
1210 pImage->paBlocks[pDiscardAsync->uBlock] = VDI_IMAGE_BLOCK_ZERO;
1211
1212 if (pDiscardAsync->idxLastBlock != pDiscardAsync->ptrBlockDiscard)
1213 {
1214 pImage->paBlocks[pDiscardAsync->uBlockLast] = pDiscardAsync->ptrBlockDiscard;
1215 pImage->paBlocksRev[pDiscardAsync->ptrBlockDiscard] = pDiscardAsync->uBlockLast;
1216
1217 rc = vdiUpdateBlockInfoAsync(pImage, pDiscardAsync->uBlockLast, pIoCtx, false /* fUpdateHdr */);
1218 if ( RT_FAILURE(rc)
1219 && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
1220 break;
1221 }
1222
1223 setImageBlocksAllocated(&pImage->Header, pDiscardAsync->idxLastBlock);
1224 rc = vdiUpdateBlockInfoAsync(pImage, pDiscardAsync->uBlock, pIoCtx, true /* fUpdateHdr */);
1225 if ( RT_FAILURE(rc)
1226 && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
1227 break;
1228
1229 pImage->cbImage -= pImage->cbTotalBlockData;
1230 LogFlowFunc(("Set new size %llu\n", pImage->cbImage));
1231 rc2 = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, pImage->cbImage);
1232 if (RT_FAILURE(rc2))
1233 rc = rc2;
1234
1235 /* Free discard state. */
1236 RTMemFree(pDiscardAsync->pvBlock);
1237 RTMemFree(pDiscardAsync);
1238 break;
1239 }
1240 default:
1241 AssertMsgFailed(("Invalid state %d\n", pDiscardAsync->enmState));
1242 }
1243
1244 if (rc == VERR_VD_NOT_ENOUGH_METADATA)
1245 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
1246
1247 return rc;
1248}
1249
1250/**
1251 * Internal: Discard a whole block from the image filling the created hole with
1252 * data from another block - async I/O version.
1253 *
1254 * @returns VBox status code.
1255 * @param pImage VDI image instance data.
1256 * @param pIoCtx I/O context associated with this request.
1257 * @param uBlock The block to discard.
1258 * @param pvBlock Memory to use for the I/O.
1259 */
1260static int vdiDiscardBlockAsync(PVDIIMAGEDESC pImage, PVDIOCTX pIoCtx,
1261 unsigned uBlock, void *pvBlock)
1262{
1263 int rc = VINF_SUCCESS;
1264 PVDIBLOCKDISCARDASYNC pDiscardAsync = NULL;
1265
1266 LogFlowFunc(("pImage=%#p uBlock=%u pvBlock=%#p\n",
1267 pImage, uBlock, pvBlock));
1268
1269 pDiscardAsync = (PVDIBLOCKDISCARDASYNC)RTMemAllocZ(sizeof(VDIBLOCKDISCARDASYNC));
1270 if (RT_UNLIKELY(!pDiscardAsync))
1271 return VERR_NO_MEMORY;
1272
1273 /* Init block discard state. */
1274 pDiscardAsync->uBlock = uBlock;
1275 pDiscardAsync->pvBlock = pvBlock;
1276 pDiscardAsync->ptrBlockDiscard = pImage->paBlocks[uBlock];
1277 pDiscardAsync->idxLastBlock = getImageBlocksAllocated(&pImage->Header) - 1;
1278 pDiscardAsync->uBlockLast = pImage->paBlocksRev[pDiscardAsync->idxLastBlock];
1279
1280 /*
1281 * The block is empty, remove it.
1282 * Read the last block of the image first.
1283 */
1284 if (pDiscardAsync->idxLastBlock != pDiscardAsync->ptrBlockDiscard)
1285 {
1286 LogFlowFunc(("Moving block [%u]=%u into [%u]=%u\n",
1287 pDiscardAsync->uBlockLast, pDiscardAsync->idxLastBlock,
1288 uBlock, pImage->paBlocks[uBlock]));
1289 pDiscardAsync->enmState = VDIBLOCKDISCARDSTATE_READ_BLOCK;
1290 }
1291 else
1292 {
1293 pDiscardAsync->enmState = VDIBLOCKDISCARDSTATE_UPDATE_METADATA; /* Start immediately to shrink the image. */
1294 LogFlowFunc(("Discard last block [%u]=%u\n", uBlock, pImage->paBlocks[uBlock]));
1295 }
1296
1297 /* Call the update callback directly. */
1298 rc = vdiDiscardBlockAsyncUpdate(pImage, pIoCtx, pDiscardAsync, VINF_SUCCESS);
1299
1300 LogFlowFunc(("returns rc=%Rrc\n", rc));
1301 return rc;
1302}
1303
1304/**
1305 * Internal: Creates a allocation bitmap from the given data.
1306 * Sectors which contain only 0 are marked as unallocated and sectors with
1307 * other data as allocated.
1308 *
1309 * @returns Pointer to the allocation bitmap or NULL on failure.
1310 * @param pvData The data to create the allocation bitmap for.
1311 * @param cbData Number of bytes in the buffer.
1312 */
1313static void *vdiAllocationBitmapCreate(void *pvData, size_t cbData)
1314{
1315 unsigned cSectors = cbData / 512;
1316 unsigned uSectorCur = 0;
1317 void *pbmAllocationBitmap = NULL;
1318
1319 Assert(!(cbData % 512));
1320 Assert(!(cSectors % 8));
1321
1322 pbmAllocationBitmap = RTMemAllocZ(cSectors / 8);
1323 if (!pbmAllocationBitmap)
1324 return NULL;
1325
1326 while (uSectorCur < cSectors)
1327 {
1328 int idxSet = ASMBitFirstSet((uint8_t *)pvData + uSectorCur * 512, cbData * 8);
1329
1330 if (idxSet != -1)
1331 {
1332 unsigned idxSectorAlloc = idxSet / 8 / 512;
1333 ASMBitSet(pbmAllocationBitmap, uSectorCur + idxSectorAlloc);
1334
1335 uSectorCur += idxSectorAlloc + 1;
1336 cbData -= (idxSectorAlloc + 1) * 512;
1337 }
1338 else
1339 break;
1340 }
1341
1342 return pbmAllocationBitmap;
1343}
1344
1345
1346/**
1347 * Updates the state of the async cluster allocation.
1348 *
1349 * @returns VBox status code.
1350 * @param pBackendData The opaque backend data.
1351 * @param pIoCtx I/O context associated with this request.
1352 * @param pvUser Opaque user data passed during a read/write request.
1353 * @param rcReq Status code for the completed request.
1354 */
1355static DECLCALLBACK(int) vdiAsyncBlockAllocUpdate(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
1356{
1357 int rc = VINF_SUCCESS;
1358 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1359 PVDIASYNCBLOCKALLOC pBlockAlloc = (PVDIASYNCBLOCKALLOC)pvUser;
1360
1361 if (RT_SUCCESS(rcReq))
1362 {
1363 pImage->cbImage += pImage->cbTotalBlockData;
1364 pImage->paBlocks[pBlockAlloc->uBlock] = pBlockAlloc->cBlocksAllocated;
1365
1366 if (pImage->paBlocksRev)
1367 pImage->paBlocksRev[pBlockAlloc->cBlocksAllocated] = pBlockAlloc->uBlock;
1368
1369 setImageBlocksAllocated(&pImage->Header, pBlockAlloc->cBlocksAllocated + 1);
1370 rc = vdiUpdateBlockInfoAsync(pImage, pBlockAlloc->uBlock, pIoCtx,
1371 true /* fUpdateHdr */);
1372 }
1373 /* else: I/O error don't update the block table. */
1374
1375 RTMemFree(pBlockAlloc);
1376 return rc;
1377}
1378
1379/** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
1380static int vdiCheckIfValid(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
1381 PVDINTERFACE pVDIfsImage, VDTYPE *penmType)
1382{
1383 LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
1384 int rc = VINF_SUCCESS;
1385 PVDIIMAGEDESC pImage;
1386
1387 if ( !VALID_PTR(pszFilename)
1388 || !*pszFilename)
1389 {
1390 rc = VERR_INVALID_PARAMETER;
1391 goto out;
1392 }
1393
1394 pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
1395 if (!pImage)
1396 {
1397 rc = VERR_NO_MEMORY;
1398 goto out;
1399 }
1400 pImage->pszFilename = pszFilename;
1401 pImage->pStorage = NULL;
1402 pImage->paBlocks = NULL;
1403 pImage->pVDIfsDisk = pVDIfsDisk;
1404 pImage->pVDIfsImage = pVDIfsImage;
1405
1406 rc = vdiOpenImage(pImage, VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_READONLY);
1407 vdiFreeImage(pImage, false);
1408 RTMemFree(pImage);
1409
1410 if (RT_SUCCESS(rc))
1411 *penmType = VDTYPE_HDD;
1412
1413out:
1414 LogFlowFunc(("returns %Rrc\n", rc));
1415 return rc;
1416}
1417
1418/** @copydoc VBOXHDDBACKEND::pfnOpen */
1419static int vdiOpen(const char *pszFilename, unsigned uOpenFlags,
1420 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1421 VDTYPE enmType, void **ppBackendData)
1422{
1423 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, ppBackendData));
1424 int rc;
1425 PVDIIMAGEDESC pImage;
1426
1427 /* Check open flags. All valid flags are supported. */
1428 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
1429 {
1430 rc = VERR_INVALID_PARAMETER;
1431 goto out;
1432 }
1433
1434 /* Check remaining arguments. */
1435 if ( !VALID_PTR(pszFilename)
1436 || !*pszFilename)
1437 {
1438 rc = VERR_INVALID_PARAMETER;
1439 goto out;
1440 }
1441
1442 pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
1443 if (!pImage)
1444 {
1445 rc = VERR_NO_MEMORY;
1446 goto out;
1447 }
1448 pImage->pszFilename = pszFilename;
1449 pImage->pStorage = NULL;
1450 pImage->paBlocks = NULL;
1451 pImage->pVDIfsDisk = pVDIfsDisk;
1452 pImage->pVDIfsImage = pVDIfsImage;
1453
1454 rc = vdiOpenImage(pImage, uOpenFlags);
1455 if (RT_SUCCESS(rc))
1456 *ppBackendData = pImage;
1457 else
1458 RTMemFree(pImage);
1459
1460out:
1461 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1462 return rc;
1463}
1464
1465/** @copydoc VBOXHDDBACKEND::pfnCreate */
1466static int vdiCreate(const char *pszFilename, uint64_t cbSize,
1467 unsigned uImageFlags, const char *pszComment,
1468 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
1469 PCRTUUID pUuid, unsigned uOpenFlags,
1470 unsigned uPercentStart, unsigned uPercentSpan,
1471 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1472 PVDINTERFACE pVDIfsOperation, void **ppBackendData)
1473{
1474 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));
1475 int rc;
1476 PVDIIMAGEDESC pImage;
1477
1478 PFNVDPROGRESS pfnProgress = NULL;
1479 void *pvUser = NULL;
1480 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
1481 if (pIfProgress)
1482 {
1483 pfnProgress = pIfProgress->pfnProgress;
1484 pvUser = pIfProgress->Core.pvUser;
1485 }
1486
1487 PVDINTERFACECONFIG pIfCfg = VDIfConfigGet(pVDIfsOperation);
1488
1489 /* Check the image flags. */
1490 if ((uImageFlags & ~VD_VDI_IMAGE_FLAGS_MASK) != 0)
1491 {
1492 rc = VERR_VD_INVALID_TYPE;
1493 goto out;
1494 }
1495
1496 /* Check open flags. All valid flags are supported. */
1497 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
1498 {
1499 rc = VERR_INVALID_PARAMETER;
1500 goto out;
1501 }
1502
1503 /* Check size. Maximum 4PB-3M. No tricks with adjusting the 1M block size
1504 * so far, which would extend the size. */
1505 cbSize = RT_ALIGN_64(cbSize, _1M);
1506 if ( !cbSize
1507 || cbSize >= _1P * 4 - _1M * 3)
1508 {
1509 rc = VERR_VD_INVALID_SIZE;
1510 goto out;
1511 }
1512
1513 /* Check remaining arguments. */
1514 if ( !VALID_PTR(pszFilename)
1515 || !*pszFilename
1516 || cbSize < VDI_IMAGE_DEFAULT_BLOCK_SIZE
1517 || !VALID_PTR(pPCHSGeometry)
1518 || !VALID_PTR(pLCHSGeometry))
1519 {
1520 rc = VERR_INVALID_PARAMETER;
1521 goto out;
1522 }
1523
1524 pImage = (PVDIIMAGEDESC)RTMemAllocZ(sizeof(VDIIMAGEDESC));
1525 if (!pImage)
1526 {
1527 rc = VERR_NO_MEMORY;
1528 goto out;
1529 }
1530 pImage->pszFilename = pszFilename;
1531 pImage->pStorage = NULL;
1532 pImage->paBlocks = NULL;
1533 pImage->pVDIfsDisk = pVDIfsDisk;
1534 pImage->pVDIfsImage = pVDIfsImage;
1535
1536 rc = vdiCreateImage(pImage, cbSize, uImageFlags, pszComment,
1537 pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags,
1538 pfnProgress, pvUser, uPercentStart, uPercentSpan,
1539 pIfCfg);
1540 if (RT_SUCCESS(rc))
1541 {
1542 /* So far the image is opened in read/write mode. Make sure the
1543 * image is opened in read-only mode if the caller requested that. */
1544 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
1545 {
1546 vdiFreeImage(pImage, false);
1547 rc = vdiOpenImage(pImage, uOpenFlags);
1548 if (RT_FAILURE(rc))
1549 {
1550 RTMemFree(pImage);
1551 goto out;
1552 }
1553 }
1554 *ppBackendData = pImage;
1555 }
1556 else
1557 RTMemFree(pImage);
1558
1559out:
1560 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1561 return rc;
1562}
1563
1564/** @copydoc VBOXHDDBACKEND::pfnRename */
1565static int vdiRename(void *pBackendData, const char *pszFilename)
1566{
1567 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
1568
1569 int rc = VINF_SUCCESS;
1570 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1571
1572 /* Check arguments. */
1573 if ( !pImage
1574 || !pszFilename
1575 || !*pszFilename)
1576 {
1577 rc = VERR_INVALID_PARAMETER;
1578 goto out;
1579 }
1580
1581 /* Close the image. */
1582 vdiFreeImage(pImage, false);
1583
1584 /* Rename the file. */
1585 rc = vdIfIoIntFileMove(pImage->pIfIo, pImage->pszFilename, pszFilename, 0);
1586 if (RT_FAILURE(rc))
1587 {
1588 /* The move failed, try to reopen the original image. */
1589 int rc2 = vdiOpenImage(pImage, pImage->uOpenFlags);
1590 if (RT_FAILURE(rc2))
1591 rc = rc2;
1592
1593 goto out;
1594 }
1595
1596 /* Update pImage with the new information. */
1597 pImage->pszFilename = pszFilename;
1598
1599 /* Open the new image. */
1600 rc = vdiOpenImage(pImage, pImage->uOpenFlags);
1601 if (RT_FAILURE(rc))
1602 goto out;
1603
1604out:
1605 LogFlowFunc(("returns %Rrc\n", rc));
1606 return rc;
1607}
1608
1609/** @copydoc VBOXHDDBACKEND::pfnClose */
1610static int vdiClose(void *pBackendData, bool fDelete)
1611{
1612 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
1613 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1614 int rc;
1615
1616 rc = vdiFreeImage(pImage, fDelete);
1617 RTMemFree(pImage);
1618
1619 LogFlowFunc(("returns %Rrc\n", rc));
1620 return rc;
1621}
1622
1623/** @copydoc VBOXHDDBACKEND::pfnRead */
1624static int vdiRead(void *pBackendData, uint64_t uOffset, void *pvBuf,
1625 size_t cbToRead, size_t *pcbActuallyRead)
1626{
1627 LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToRead=%zu pcbActuallyRead=%#p\n", pBackendData, uOffset, pvBuf, cbToRead, pcbActuallyRead));
1628 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1629 unsigned uBlock;
1630 unsigned offRead;
1631 int rc;
1632
1633 AssertPtr(pImage);
1634 Assert(!(uOffset % 512));
1635 Assert(!(cbToRead % 512));
1636
1637 if ( uOffset + cbToRead > getImageDiskSize(&pImage->Header)
1638 || !VALID_PTR(pvBuf)
1639 || !cbToRead)
1640 {
1641 rc = VERR_INVALID_PARAMETER;
1642 goto out;
1643 }
1644
1645 /* Calculate starting block number and offset inside it. */
1646 uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
1647 offRead = (unsigned)uOffset & pImage->uBlockMask;
1648
1649 /* Clip read range to at most the rest of the block. */
1650 cbToRead = RT_MIN(cbToRead, getImageBlockSize(&pImage->Header) - offRead);
1651 Assert(!(cbToRead % 512));
1652
1653 if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE)
1654 rc = VERR_VD_BLOCK_FREE;
1655 else if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
1656 {
1657 memset(pvBuf, 0, cbToRead);
1658 rc = VINF_SUCCESS;
1659 }
1660 else
1661 {
1662 /* Block present in image file, read relevant data. */
1663 uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
1664 + (pImage->offStartData + pImage->offStartBlockData + offRead);
1665
1666 if (u64Offset + cbToRead <= pImage->cbImage)
1667 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, u64Offset,
1668 pvBuf, cbToRead);
1669 else
1670 {
1671 LogRel(("VDI: Out of range access (%llu) in image %s, image size %llu\n",
1672 u64Offset, pImage->pszFilename, pImage->cbImage));
1673 memset(pvBuf, 0, cbToRead);
1674 rc = VERR_VD_READ_OUT_OF_RANGE;
1675 }
1676 }
1677
1678 if (pcbActuallyRead)
1679 *pcbActuallyRead = cbToRead;
1680
1681out:
1682 LogFlowFunc(("returns %Rrc\n", rc));
1683 return rc;
1684}
1685
1686/**@copydoc VBOXHDDBACKEND::pfnWrite */
1687static int vdiWrite(void *pBackendData, uint64_t uOffset, const void *pvBuf,
1688 size_t cbToWrite, size_t *pcbWriteProcess,
1689 size_t *pcbPreRead, size_t *pcbPostRead, unsigned fWrite)
1690{
1691 LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n", pBackendData, uOffset, pvBuf, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
1692 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1693 unsigned uBlock;
1694 unsigned offWrite;
1695 int rc = VINF_SUCCESS;
1696
1697 AssertPtr(pImage);
1698 Assert(!(uOffset % 512));
1699 Assert(!(cbToWrite % 512));
1700
1701 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1702 {
1703 rc = VERR_VD_IMAGE_READ_ONLY;
1704 goto out;
1705 }
1706
1707 if (!VALID_PTR(pvBuf) || !cbToWrite)
1708 {
1709 rc = VERR_INVALID_PARAMETER;
1710 goto out;
1711 }
1712
1713 /* No size check here, will do that later. For dynamic images which are
1714 * not multiples of the block size in length, this would prevent writing to
1715 * the last block. */
1716
1717 /* Calculate starting block number and offset inside it. */
1718 uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
1719 offWrite = (unsigned)uOffset & pImage->uBlockMask;
1720
1721 /* Clip write range to at most the rest of the block. */
1722 cbToWrite = RT_MIN(cbToWrite, getImageBlockSize(&pImage->Header) - offWrite);
1723 Assert(!(cbToWrite % 512));
1724
1725 do
1726 {
1727 if (!IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
1728 {
1729 /* Block is either free or zero. */
1730 if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_ZEROES)
1731 && ( pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO
1732 || cbToWrite == getImageBlockSize(&pImage->Header)))
1733 {
1734 /* If the destination block is unallocated at this point, it's
1735 * either a zero block or a block which hasn't been used so far
1736 * (which also means that it's a zero block. Don't need to write
1737 * anything to this block if the data consists of just zeroes. */
1738 Assert(!(cbToWrite % 4));
1739 Assert(cbToWrite * 8 <= UINT32_MAX);
1740 if (ASMBitFirstSet((volatile void *)pvBuf, (uint32_t)cbToWrite * 8) == -1)
1741 {
1742 pImage->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
1743 *pcbPreRead = 0;
1744 *pcbPostRead = 0;
1745 break;
1746 }
1747 }
1748
1749 if ( cbToWrite == getImageBlockSize(&pImage->Header)
1750 && !(fWrite & VD_WRITE_NO_ALLOC))
1751 {
1752 /* Full block write to previously unallocated block.
1753 * Allocate block and write data. */
1754 Assert(!offWrite);
1755 unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
1756 uint64_t u64Offset = (uint64_t)cBlocksAllocated * pImage->cbTotalBlockData
1757 + (pImage->offStartData + pImage->offStartBlockData);
1758 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
1759 u64Offset, pvBuf, cbToWrite);
1760 if (RT_FAILURE(rc))
1761 goto out;
1762 pImage->paBlocks[uBlock] = cBlocksAllocated;
1763
1764 if (pImage->paBlocksRev)
1765 pImage->paBlocksRev[cBlocksAllocated] = uBlock;
1766
1767 setImageBlocksAllocated(&pImage->Header, cBlocksAllocated + 1);
1768
1769 rc = vdiUpdateBlockInfo(pImage, uBlock);
1770 if (RT_FAILURE(rc))
1771 goto out;
1772
1773 pImage->cbImage += cbToWrite;
1774 *pcbPreRead = 0;
1775 *pcbPostRead = 0;
1776 }
1777 else
1778 {
1779 /* Trying to do a partial write to an unallocated block. Don't do
1780 * anything except letting the upper layer know what to do. */
1781 *pcbPreRead = offWrite % getImageBlockSize(&pImage->Header);
1782 *pcbPostRead = getImageBlockSize(&pImage->Header) - cbToWrite - *pcbPreRead;
1783 rc = VERR_VD_BLOCK_FREE;
1784 }
1785 }
1786 else
1787 {
1788 /* Block present in image file, write relevant data. */
1789 uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
1790 + (pImage->offStartData + pImage->offStartBlockData + offWrite);
1791 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, u64Offset,
1792 pvBuf, cbToWrite);
1793 }
1794 } while (0);
1795
1796 if (pcbWriteProcess)
1797 *pcbWriteProcess = cbToWrite;
1798
1799out:
1800 LogFlowFunc(("returns %Rrc\n", rc));
1801 return rc;
1802}
1803
1804/** @copydoc VBOXHDDBACKEND::pfnFlush */
1805static int vdiFlush(void *pBackendData)
1806{
1807 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1808 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1809 int rc = VINF_SUCCESS;
1810
1811 Assert(pImage);
1812
1813 vdiFlushImage(pImage);
1814 LogFlowFunc(("returns %Rrc\n", rc));
1815 return rc;
1816}
1817
1818/** @copydoc VBOXHDDBACKEND::pfnGetVersion */
1819static unsigned vdiGetVersion(void *pBackendData)
1820{
1821 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1822 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1823 unsigned uVersion;
1824
1825 AssertPtr(pImage);
1826
1827 if (pImage)
1828 uVersion = pImage->PreHeader.u32Version;
1829 else
1830 uVersion = 0;
1831
1832 LogFlowFunc(("returns %#x\n", uVersion));
1833 return uVersion;
1834}
1835
1836/** @copydoc VBOXHDDBACKEND::pfnGetSize */
1837static uint64_t vdiGetSize(void *pBackendData)
1838{
1839 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1840 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1841 uint64_t cbSize;
1842
1843 AssertPtr(pImage);
1844
1845 if (pImage)
1846 cbSize = getImageDiskSize(&pImage->Header);
1847 else
1848 cbSize = 0;
1849
1850 LogFlowFunc(("returns %llu\n", cbSize));
1851 return cbSize;
1852}
1853
1854/** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
1855static uint64_t vdiGetFileSize(void *pBackendData)
1856{
1857 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1858 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1859 uint64_t cb = 0;
1860
1861 AssertPtr(pImage);
1862
1863 if (pImage)
1864 {
1865 uint64_t cbFile;
1866 if (pImage->pStorage)
1867 {
1868 int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
1869 if (RT_SUCCESS(rc))
1870 cb += cbFile;
1871 }
1872 }
1873
1874 LogFlowFunc(("returns %lld\n", cb));
1875 return cb;
1876}
1877
1878/** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
1879static int vdiGetPCHSGeometry(void *pBackendData, PVDGEOMETRY pPCHSGeometry)
1880{
1881 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
1882 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1883 int rc;
1884
1885 AssertPtr(pImage);
1886
1887 if (pImage)
1888 {
1889 if (pImage->PCHSGeometry.cCylinders)
1890 {
1891 *pPCHSGeometry = pImage->PCHSGeometry;
1892 rc = VINF_SUCCESS;
1893 }
1894 else
1895 rc = VERR_VD_GEOMETRY_NOT_SET;
1896 }
1897 else
1898 rc = VERR_VD_NOT_OPENED;
1899
1900 LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
1901 return rc;
1902}
1903
1904/** @copydoc VBOXHDDBACKEND::pfnSetPCHSGeometry */
1905static int vdiSetPCHSGeometry(void *pBackendData, PCVDGEOMETRY pPCHSGeometry)
1906{
1907 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
1908 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1909 int rc;
1910
1911 AssertPtr(pImage);
1912
1913 if (pImage)
1914 {
1915 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1916 {
1917 rc = VERR_VD_IMAGE_READ_ONLY;
1918 goto out;
1919 }
1920
1921 pImage->PCHSGeometry = *pPCHSGeometry;
1922 rc = VINF_SUCCESS;
1923 }
1924 else
1925 rc = VERR_VD_NOT_OPENED;
1926
1927out:
1928 LogFlowFunc(("returns %Rrc\n", rc));
1929 return rc;
1930}
1931
1932/** @copydoc VBOXHDDBACKEND::pfnGetLCHSGeometry */
1933static int vdiGetLCHSGeometry(void *pBackendData, PVDGEOMETRY pLCHSGeometry)
1934{
1935 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
1936 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1937 int rc;
1938
1939 AssertPtr(pImage);
1940
1941 if (pImage)
1942 {
1943 VDIDISKGEOMETRY DummyGeo = { 0, 0, 0, VDI_GEOMETRY_SECTOR_SIZE };
1944 PVDIDISKGEOMETRY pGeometry = getImageLCHSGeometry(&pImage->Header);
1945 if (!pGeometry)
1946 pGeometry = &DummyGeo;
1947
1948 if ( pGeometry->cCylinders > 0
1949 && pGeometry->cHeads > 0
1950 && pGeometry->cSectors > 0)
1951 {
1952 pLCHSGeometry->cCylinders = pGeometry->cCylinders;
1953 pLCHSGeometry->cHeads = pGeometry->cHeads;
1954 pLCHSGeometry->cSectors = pGeometry->cSectors;
1955 rc = VINF_SUCCESS;
1956 }
1957 else
1958 rc = VERR_VD_GEOMETRY_NOT_SET;
1959 }
1960 else
1961 rc = VERR_VD_NOT_OPENED;
1962
1963 LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
1964 return rc;
1965}
1966
1967/** @copydoc VBOXHDDBACKEND::pfnSetLCHSGeometry */
1968static int vdiSetLCHSGeometry(void *pBackendData, PCVDGEOMETRY pLCHSGeometry)
1969{
1970 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
1971 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
1972 PVDIDISKGEOMETRY pGeometry;
1973 int rc;
1974
1975 AssertPtr(pImage);
1976
1977 if (pImage)
1978 {
1979 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1980 {
1981 rc = VERR_VD_IMAGE_READ_ONLY;
1982 goto out;
1983 }
1984
1985 pGeometry = getImageLCHSGeometry(&pImage->Header);
1986 if (pGeometry)
1987 {
1988 pGeometry->cCylinders = pLCHSGeometry->cCylinders;
1989 pGeometry->cHeads = pLCHSGeometry->cHeads;
1990 pGeometry->cSectors = pLCHSGeometry->cSectors;
1991 pGeometry->cbSector = VDI_GEOMETRY_SECTOR_SIZE;
1992
1993 /* Update header information in base image file. */
1994 vdiFlushImage(pImage);
1995 }
1996 rc = VINF_SUCCESS;
1997 }
1998 else
1999 rc = VERR_VD_NOT_OPENED;
2000
2001out:
2002 LogFlowFunc(("returns %Rrc\n", rc));
2003 return rc;
2004}
2005
2006/** @copydoc VBOXHDDBACKEND::pfnGetImageFlags */
2007static unsigned vdiGetImageFlags(void *pBackendData)
2008{
2009 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2010 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2011 unsigned uImageFlags;
2012
2013 AssertPtr(pImage);
2014
2015 if (pImage)
2016 uImageFlags = pImage->uImageFlags;
2017 else
2018 uImageFlags = 0;
2019
2020 LogFlowFunc(("returns %#x\n", uImageFlags));
2021 return uImageFlags;
2022}
2023
2024/** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
2025static unsigned vdiGetOpenFlags(void *pBackendData)
2026{
2027 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2028 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2029 unsigned uOpenFlags;
2030
2031 AssertPtr(pImage);
2032
2033 if (pImage)
2034 uOpenFlags = pImage->uOpenFlags;
2035 else
2036 uOpenFlags = 0;
2037
2038 LogFlowFunc(("returns %#x\n", uOpenFlags));
2039 return uOpenFlags;
2040}
2041
2042/** @copydoc VBOXHDDBACKEND::pfnSetOpenFlags */
2043static int vdiSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
2044{
2045 LogFlowFunc(("pBackendData=%#p uOpenFlags=%#x\n", pBackendData, uOpenFlags));
2046 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2047 int rc;
2048 const char *pszFilename;
2049
2050 /* Image must be opened and the new flags must be valid. */
2051 if (!pImage || (uOpenFlags & ~( VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO
2052 | VD_OPEN_FLAGS_ASYNC_IO | VD_OPEN_FLAGS_SHAREABLE
2053 | VD_OPEN_FLAGS_SEQUENTIAL | VD_OPEN_FLAGS_DISCARD
2054 | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)))
2055 {
2056 rc = VERR_INVALID_PARAMETER;
2057 goto out;
2058 }
2059
2060 /* Implement this operation via reopening the image. */
2061 pszFilename = pImage->pszFilename;
2062 rc = vdiFreeImage(pImage, false);
2063 if (RT_FAILURE(rc))
2064 goto out;
2065 rc = vdiOpenImage(pImage, uOpenFlags);
2066
2067out:
2068 LogFlowFunc(("returns %Rrc\n", rc));
2069 return rc;
2070}
2071
2072/** @copydoc VBOXHDDBACKEND::pfnGetComment */
2073static int vdiGetComment(void *pBackendData, char *pszComment,
2074 size_t cbComment)
2075{
2076 LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
2077 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2078 int rc = VINF_SUCCESS;
2079
2080 AssertPtr(pImage);
2081
2082 if (pImage)
2083 {
2084 char *pszTmp = getImageComment(&pImage->Header);
2085 /* Make this foolproof even if the image doesn't have the zero
2086 * termination. With some luck the repaired header will be saved. */
2087 size_t cb = RTStrNLen(pszTmp, VDI_IMAGE_COMMENT_SIZE);
2088 if (cb == VDI_IMAGE_COMMENT_SIZE)
2089 {
2090 pszTmp[VDI_IMAGE_COMMENT_SIZE-1] = '\0';
2091 cb--;
2092 }
2093 if (cb < cbComment)
2094 {
2095 /* memcpy is much better than strncpy. */
2096 memcpy(pszComment, pszTmp, cb + 1);
2097 }
2098 else
2099 rc = VERR_BUFFER_OVERFLOW;
2100 }
2101 else
2102 rc = VERR_VD_NOT_OPENED;
2103
2104 LogFlowFunc(("returns %Rrc comment=\"%s\"\n", rc, pszComment));
2105 return rc;
2106}
2107
2108/** @copydoc VBOXHDDBACKEND::pfnGetComment */
2109static int vdiSetComment(void *pBackendData, const char *pszComment)
2110{
2111 LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
2112 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2113 int rc;
2114
2115 AssertPtr(pImage);
2116
2117 if (pImage)
2118 {
2119 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2120 rc = VERR_VD_IMAGE_READ_ONLY;
2121 else
2122 {
2123 size_t cchComment = pszComment ? strlen(pszComment) : 0;
2124 if (cchComment >= VDI_IMAGE_COMMENT_SIZE)
2125 {
2126 LogFunc(("pszComment is too long, %d bytes!\n", cchComment));
2127 rc = VERR_VD_VDI_COMMENT_TOO_LONG;
2128 goto out;
2129 }
2130
2131 /* we don't support old style images */
2132 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
2133 {
2134 /*
2135 * Update the comment field, making sure to zero out all of the previous comment.
2136 */
2137 memset(pImage->Header.u.v1.szComment, '\0', VDI_IMAGE_COMMENT_SIZE);
2138 memcpy(pImage->Header.u.v1.szComment, pszComment, cchComment);
2139
2140 /* write out new the header */
2141 rc = vdiUpdateHeader(pImage);
2142 }
2143 else
2144 rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
2145 }
2146 }
2147 else
2148 rc = VERR_VD_NOT_OPENED;
2149
2150out:
2151 LogFlowFunc(("returns %Rrc\n", rc));
2152 return rc;
2153}
2154
2155/** @copydoc VBOXHDDBACKEND::pfnGetUuid */
2156static int vdiGetUuid(void *pBackendData, PRTUUID pUuid)
2157{
2158 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2159 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2160 int rc;
2161
2162 AssertPtr(pImage);
2163
2164 if (pImage)
2165 {
2166 *pUuid = *getImageCreationUUID(&pImage->Header);
2167 rc = VINF_SUCCESS;
2168 }
2169 else
2170 rc = VERR_VD_NOT_OPENED;
2171
2172 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2173 return rc;
2174}
2175
2176/** @copydoc VBOXHDDBACKEND::pfnSetUuid */
2177static int vdiSetUuid(void *pBackendData, PCRTUUID pUuid)
2178{
2179 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2180 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2181 int rc = VINF_SUCCESS;
2182
2183 AssertPtr(pImage);
2184
2185 if (pImage)
2186 {
2187 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2188 {
2189 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
2190 pImage->Header.u.v1.uuidCreate = *pUuid;
2191 /* Make it possible to clone old VDIs. */
2192 else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
2193 pImage->Header.u.v0.uuidCreate = *pUuid;
2194 else
2195 {
2196 LogFunc(("Version is not supported!\n"));
2197 rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
2198 }
2199 }
2200 else
2201 rc = VERR_VD_IMAGE_READ_ONLY;
2202 }
2203 else
2204 rc = VERR_VD_NOT_OPENED;
2205
2206 LogFlowFunc(("returns %Rrc\n", rc));
2207 return rc;
2208}
2209
2210/** @copydoc VBOXHDDBACKEND::pfnGetModificationUuid */
2211static int vdiGetModificationUuid(void *pBackendData, PRTUUID pUuid)
2212{
2213 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2214 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2215 int rc;
2216
2217 AssertPtr(pImage);
2218
2219 if (pImage)
2220 {
2221 *pUuid = *getImageModificationUUID(&pImage->Header);
2222 rc = VINF_SUCCESS;
2223 }
2224 else
2225 rc = VERR_VD_NOT_OPENED;
2226
2227 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2228 return rc;
2229}
2230
2231/** @copydoc VBOXHDDBACKEND::pfnSetModificationUuid */
2232static int vdiSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
2233{
2234 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2235 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2236 int rc = VINF_SUCCESS;
2237
2238 AssertPtr(pImage);
2239
2240 if (pImage)
2241 {
2242 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2243 {
2244 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
2245 pImage->Header.u.v1.uuidModify = *pUuid;
2246 /* Make it possible to clone old VDIs. */
2247 else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
2248 pImage->Header.u.v0.uuidModify = *pUuid;
2249 else
2250 {
2251 LogFunc(("Version is not supported!\n"));
2252 rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
2253 }
2254 }
2255 else
2256 rc = VERR_VD_IMAGE_READ_ONLY;
2257 }
2258 else
2259 rc = VERR_VD_NOT_OPENED;
2260
2261 LogFlowFunc(("returns %Rrc\n", rc));
2262 return rc;
2263}
2264
2265/** @copydoc VBOXHDDBACKEND::pfnGetParentUuid */
2266static int vdiGetParentUuid(void *pBackendData, PRTUUID pUuid)
2267{
2268 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2269 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2270 int rc;
2271
2272 AssertPtr(pImage);
2273
2274 if (pImage)
2275 {
2276 *pUuid = *getImageParentUUID(&pImage->Header);
2277 rc = VINF_SUCCESS;
2278 }
2279 else
2280 rc = VERR_VD_NOT_OPENED;
2281
2282 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2283 return rc;
2284}
2285
2286/** @copydoc VBOXHDDBACKEND::pfnSetParentUuid */
2287static int vdiSetParentUuid(void *pBackendData, PCRTUUID pUuid)
2288{
2289 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2290 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2291 int rc = VINF_SUCCESS;
2292
2293 AssertPtr(pImage);
2294
2295 if (pImage)
2296 {
2297 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2298 {
2299 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
2300 pImage->Header.u.v1.uuidLinkage = *pUuid;
2301 /* Make it possible to clone old VDIs. */
2302 else if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0)
2303 pImage->Header.u.v0.uuidLinkage = *pUuid;
2304 else
2305 {
2306 LogFunc(("Version is not supported!\n"));
2307 rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
2308 }
2309 }
2310 else
2311 rc = VERR_VD_IMAGE_READ_ONLY;
2312 }
2313 else
2314 rc = VERR_VD_NOT_OPENED;
2315
2316 LogFlowFunc(("returns %Rrc\n", rc));
2317 return rc;
2318}
2319
2320/** @copydoc VBOXHDDBACKEND::pfnGetParentModificationUuid */
2321static int vdiGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
2322{
2323 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2324 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2325 int rc;
2326
2327 AssertPtr(pImage);
2328
2329 if (pImage)
2330 {
2331 *pUuid = *getImageParentModificationUUID(&pImage->Header);
2332 rc = VINF_SUCCESS;
2333 }
2334 else
2335 rc = VERR_VD_NOT_OPENED;
2336
2337 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2338 return rc;
2339}
2340
2341/** @copydoc VBOXHDDBACKEND::pfnSetParentModificationUuid */
2342static int vdiSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
2343{
2344 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2345 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2346 int rc = VINF_SUCCESS;
2347
2348 AssertPtr(pImage);
2349
2350 if (pImage)
2351 {
2352 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2353 {
2354 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) == 1)
2355 pImage->Header.u.v1.uuidParentModify = *pUuid;
2356 else
2357 {
2358 LogFunc(("Version is not supported!\n"));
2359 rc = VERR_VD_VDI_UNSUPPORTED_VERSION;
2360 }
2361 }
2362 else
2363 rc = VERR_VD_IMAGE_READ_ONLY;
2364 }
2365 else
2366 rc = VERR_VD_NOT_OPENED;
2367
2368 LogFlowFunc(("returns %Rrc\n", rc));
2369 return rc;
2370}
2371
2372/** @copydoc VBOXHDDBACKEND::pfnDump */
2373static void vdiDump(void *pBackendData)
2374{
2375 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2376
2377 vdIfErrorMessage(pImage->pIfError, "Dumping VDI image \"%s\" mode=%s uOpenFlags=%X File=%#p\n",
2378 pImage->pszFilename,
2379 (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY) ? "r/o" : "r/w",
2380 pImage->uOpenFlags,
2381 pImage->pStorage);
2382 vdIfErrorMessage(pImage->pIfError, "Header: Version=%08X Type=%X Flags=%X Size=%llu\n",
2383 pImage->PreHeader.u32Version,
2384 getImageType(&pImage->Header),
2385 getImageFlags(&pImage->Header),
2386 getImageDiskSize(&pImage->Header));
2387 vdIfErrorMessage(pImage->pIfError, "Header: cbBlock=%u cbBlockExtra=%u cBlocks=%u cBlocksAllocated=%u\n",
2388 getImageBlockSize(&pImage->Header),
2389 getImageExtraBlockSize(&pImage->Header),
2390 getImageBlocks(&pImage->Header),
2391 getImageBlocksAllocated(&pImage->Header));
2392 vdIfErrorMessage(pImage->pIfError, "Header: offBlocks=%u offData=%u\n",
2393 getImageBlocksOffset(&pImage->Header),
2394 getImageDataOffset(&pImage->Header));
2395 PVDIDISKGEOMETRY pg = getImageLCHSGeometry(&pImage->Header);
2396 if (pg)
2397 vdIfErrorMessage(pImage->pIfError, "Header: Geometry: C/H/S=%u/%u/%u cbSector=%u\n",
2398 pg->cCylinders, pg->cHeads, pg->cSectors, pg->cbSector);
2399 vdIfErrorMessage(pImage->pIfError, "Header: uuidCreation={%RTuuid}\n", getImageCreationUUID(&pImage->Header));
2400 vdIfErrorMessage(pImage->pIfError, "Header: uuidModification={%RTuuid}\n", getImageModificationUUID(&pImage->Header));
2401 vdIfErrorMessage(pImage->pIfError, "Header: uuidParent={%RTuuid}\n", getImageParentUUID(&pImage->Header));
2402 if (GET_MAJOR_HEADER_VERSION(&pImage->Header) >= 1)
2403 vdIfErrorMessage(pImage->pIfError, "Header: uuidParentModification={%RTuuid}\n", getImageParentModificationUUID(&pImage->Header));
2404 vdIfErrorMessage(pImage->pIfError, "Image: fFlags=%08X offStartBlocks=%u offStartData=%u\n",
2405 pImage->uImageFlags, pImage->offStartBlocks, pImage->offStartData);
2406 vdIfErrorMessage(pImage->pIfError, "Image: uBlockMask=%08X cbTotalBlockData=%u uShiftOffset2Index=%u offStartBlockData=%u\n",
2407 pImage->uBlockMask,
2408 pImage->cbTotalBlockData,
2409 pImage->uShiftOffset2Index,
2410 pImage->offStartBlockData);
2411
2412 unsigned uBlock, cBlocksNotFree, cBadBlocks, cBlocks = getImageBlocks(&pImage->Header);
2413 for (uBlock=0, cBlocksNotFree=0, cBadBlocks=0; uBlock<cBlocks; uBlock++)
2414 {
2415 if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
2416 {
2417 cBlocksNotFree++;
2418 if (pImage->paBlocks[uBlock] >= cBlocks)
2419 cBadBlocks++;
2420 }
2421 }
2422 if (cBlocksNotFree != getImageBlocksAllocated(&pImage->Header))
2423 {
2424 vdIfErrorMessage(pImage->pIfError, "!! WARNING: %u blocks actually allocated (cBlocksAllocated=%u) !!\n",
2425 cBlocksNotFree, getImageBlocksAllocated(&pImage->Header));
2426 }
2427 if (cBadBlocks)
2428 {
2429 vdIfErrorMessage(pImage->pIfError, "!! WARNING: %u bad blocks found !!\n",
2430 cBadBlocks);
2431 }
2432}
2433
2434static int vdiAsyncRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
2435 PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
2436{
2437 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
2438 pBackendData, uOffset, pIoCtx, cbToRead, pcbActuallyRead));
2439 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2440 unsigned uBlock;
2441 unsigned offRead;
2442 int rc;
2443
2444 AssertPtr(pImage);
2445 Assert(!(uOffset % 512));
2446 Assert(!(cbToRead % 512));
2447
2448 if ( uOffset + cbToRead > getImageDiskSize(&pImage->Header)
2449 || !VALID_PTR(pIoCtx)
2450 || !cbToRead)
2451 {
2452 rc = VERR_INVALID_PARAMETER;
2453 goto out;
2454 }
2455
2456 /* Calculate starting block number and offset inside it. */
2457 uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
2458 offRead = (unsigned)uOffset & pImage->uBlockMask;
2459
2460 /* Clip read range to at most the rest of the block. */
2461 cbToRead = RT_MIN(cbToRead, getImageBlockSize(&pImage->Header) - offRead);
2462 Assert(!(cbToRead % 512));
2463
2464 if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_FREE)
2465 rc = VERR_VD_BLOCK_FREE;
2466 else if (pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO)
2467 {
2468 size_t cbSet;
2469
2470 cbSet = vdIfIoIntIoCtxSet(pImage->pIfIo, pIoCtx, 0, cbToRead);
2471 Assert(cbSet == cbToRead);
2472
2473 rc = VINF_SUCCESS;
2474 }
2475 else
2476 {
2477 /* Block present in image file, read relevant data. */
2478 uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
2479 + (pImage->offStartData + pImage->offStartBlockData + offRead);
2480
2481 if (u64Offset + cbToRead <= pImage->cbImage)
2482 rc = vdIfIoIntFileReadUser(pImage->pIfIo, pImage->pStorage, u64Offset,
2483 pIoCtx, cbToRead);
2484 else
2485 {
2486 LogRel(("VDI: Out of range access (%llu) in image %s, image size %llu\n",
2487 u64Offset, pImage->pszFilename, pImage->cbImage));
2488 vdIfIoIntIoCtxSet(pImage->pIfIo, pIoCtx, 0, cbToRead);
2489 rc = VERR_VD_READ_OUT_OF_RANGE;
2490 }
2491 }
2492
2493 if (pcbActuallyRead)
2494 *pcbActuallyRead = cbToRead;
2495
2496out:
2497 LogFlowFunc(("returns %Rrc\n", rc));
2498 return rc;
2499}
2500
2501static int vdiAsyncWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
2502 PVDIOCTX pIoCtx,
2503 size_t *pcbWriteProcess, size_t *pcbPreRead,
2504 size_t *pcbPostRead, unsigned fWrite)
2505{
2506 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
2507 pBackendData, uOffset, pIoCtx, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
2508 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2509 unsigned uBlock;
2510 unsigned offWrite;
2511 int rc = VINF_SUCCESS;
2512
2513 AssertPtr(pImage);
2514 Assert(!(uOffset % 512));
2515 Assert(!(cbToWrite % 512));
2516
2517 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2518 {
2519 rc = VERR_VD_IMAGE_READ_ONLY;
2520 goto out;
2521 }
2522
2523 if (!VALID_PTR(pIoCtx) || !cbToWrite)
2524 {
2525 rc = VERR_INVALID_PARAMETER;
2526 goto out;
2527 }
2528
2529 /* No size check here, will do that later. For dynamic images which are
2530 * not multiples of the block size in length, this would prevent writing to
2531 * the last block. */
2532
2533 /* Calculate starting block number and offset inside it. */
2534 uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
2535 offWrite = (unsigned)uOffset & pImage->uBlockMask;
2536
2537 /* Clip write range to at most the rest of the block. */
2538 cbToWrite = RT_MIN(cbToWrite, getImageBlockSize(&pImage->Header) - offWrite);
2539 Assert(!(cbToWrite % 512));
2540
2541 do
2542 {
2543 if (!IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
2544 {
2545 /* Block is either free or zero. */
2546 if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_ZEROES)
2547 && ( pImage->paBlocks[uBlock] == VDI_IMAGE_BLOCK_ZERO
2548 || cbToWrite == getImageBlockSize(&pImage->Header)))
2549 {
2550#if 0 /** @todo Provide interface to check an I/O context for a specific value */
2551 /* If the destination block is unallocated at this point, it's
2552 * either a zero block or a block which hasn't been used so far
2553 * (which also means that it's a zero block. Don't need to write
2554 * anything to this block if the data consists of just zeroes. */
2555 Assert(!(cbToWrite % 4));
2556 Assert(cbToWrite * 8 <= UINT32_MAX);
2557 if (ASMBitFirstSet((volatile void *)pvBuf, (uint32_t)cbToWrite * 8) == -1)
2558 {
2559 pImage->paBlocks[uBlock] = VDI_IMAGE_BLOCK_ZERO;
2560 break;
2561 }
2562#endif
2563 }
2564
2565 if ( cbToWrite == getImageBlockSize(&pImage->Header)
2566 && !(fWrite & VD_WRITE_NO_ALLOC))
2567 {
2568 /* Full block write to previously unallocated block.
2569 * Allocate block and write data. */
2570 Assert(!offWrite);
2571 PVDIASYNCBLOCKALLOC pBlockAlloc = (PVDIASYNCBLOCKALLOC)RTMemAllocZ(sizeof(VDIASYNCBLOCKALLOC));
2572 if (!pBlockAlloc)
2573 {
2574 rc = VERR_NO_MEMORY;
2575 break;
2576 }
2577
2578 unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header);
2579 uint64_t u64Offset = (uint64_t)cBlocksAllocated * pImage->cbTotalBlockData
2580 + (pImage->offStartData + pImage->offStartBlockData);
2581
2582 pBlockAlloc->cBlocksAllocated = cBlocksAllocated;
2583 pBlockAlloc->uBlock = uBlock;
2584
2585 *pcbPreRead = 0;
2586 *pcbPostRead = 0;
2587
2588 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
2589 u64Offset, pIoCtx, cbToWrite,
2590 vdiAsyncBlockAllocUpdate, pBlockAlloc);
2591 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
2592 break;
2593 else if (RT_FAILURE(rc))
2594 {
2595 RTMemFree(pBlockAlloc);
2596 break;
2597 }
2598
2599 rc = vdiAsyncBlockAllocUpdate(pImage, pIoCtx, pBlockAlloc, rc);
2600 }
2601 else
2602 {
2603 /* Trying to do a partial write to an unallocated block. Don't do
2604 * anything except letting the upper layer know what to do. */
2605 *pcbPreRead = offWrite % getImageBlockSize(&pImage->Header);
2606 *pcbPostRead = getImageBlockSize(&pImage->Header) - cbToWrite - *pcbPreRead;
2607 rc = VERR_VD_BLOCK_FREE;
2608 }
2609 }
2610 else
2611 {
2612 /* Block present in image file, write relevant data. */
2613 uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData
2614 + (pImage->offStartData + pImage->offStartBlockData + offWrite);
2615 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
2616 u64Offset, pIoCtx, cbToWrite, NULL, NULL);
2617 }
2618 } while (0);
2619
2620 if (pcbWriteProcess)
2621 *pcbWriteProcess = cbToWrite;
2622
2623out:
2624 LogFlowFunc(("returns %Rrc\n", rc));
2625 return rc;
2626}
2627
2628static int vdiAsyncFlush(void *pBackendData, PVDIOCTX pIoCtx)
2629{
2630 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2631 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2632 int rc = VINF_SUCCESS;
2633
2634 Assert(pImage);
2635
2636 rc = vdiFlushImageAsync(pImage, pIoCtx);
2637 LogFlowFunc(("returns %Rrc\n", rc));
2638 return rc;
2639}
2640
2641/** @copydoc VBOXHDDBACKEND::pfnCompact */
2642static int vdiCompact(void *pBackendData, unsigned uPercentStart,
2643 unsigned uPercentSpan, PVDINTERFACE pVDIfsDisk,
2644 PVDINTERFACE pVDIfsImage, PVDINTERFACE pVDIfsOperation)
2645{
2646 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2647 int rc = VINF_SUCCESS;
2648 void *pvBuf = NULL, *pvTmp = NULL;
2649 unsigned *paBlocks2 = NULL;
2650
2651 int (*pfnParentRead)(void *, uint64_t, void *, size_t) = NULL;
2652 void *pvParent = NULL;
2653 PVDINTERFACEPARENTSTATE pIfParentState = VDIfParentStateGet(pVDIfsOperation);
2654 if (pIfParentState)
2655 {
2656 pfnParentRead = pIfParentState->pfnParentRead;
2657 pvParent = pIfParentState->Core.pvUser;
2658 }
2659
2660 PFNVDPROGRESS pfnProgress = NULL;
2661 void *pvUser = NULL;
2662 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
2663
2664 PVDINTERFACEQUERYRANGEUSE pIfQueryRangeUse = VDIfQueryRangeUseGet(pVDIfsOperation);
2665
2666 do {
2667 AssertBreakStmt(pImage, rc = VERR_INVALID_PARAMETER);
2668
2669 AssertBreakStmt(!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY),
2670 rc = VERR_VD_IMAGE_READ_ONLY);
2671
2672 unsigned cBlocks;
2673 unsigned cBlocksToMove = 0;
2674 size_t cbBlock;
2675 cBlocks = getImageBlocks(&pImage->Header);
2676 cbBlock = getImageBlockSize(&pImage->Header);
2677 if (pfnParentRead)
2678 {
2679 pvBuf = RTMemTmpAlloc(cbBlock);
2680 AssertBreakStmt(VALID_PTR(pvBuf), rc = VERR_NO_MEMORY);
2681 }
2682 pvTmp = RTMemTmpAlloc(cbBlock);
2683 AssertBreakStmt(VALID_PTR(pvTmp), rc = VERR_NO_MEMORY);
2684
2685 uint64_t cbFile;
2686 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
2687 AssertRCBreak(rc);
2688 unsigned cBlocksAllocated = (unsigned)((cbFile - pImage->offStartData - pImage->offStartBlockData) >> pImage->uShiftOffset2Index);
2689 if (cBlocksAllocated == 0)
2690 {
2691 /* No data blocks in this image, no need to compact. */
2692 rc = VINF_SUCCESS;
2693 break;
2694 }
2695
2696 /* Allocate block array for back resolving. */
2697 paBlocks2 = (unsigned *)RTMemAlloc(sizeof(unsigned *) * cBlocksAllocated);
2698 AssertBreakStmt(VALID_PTR(paBlocks2), rc = VERR_NO_MEMORY);
2699 /* Fill out back resolving, check/fix allocation errors before
2700 * compacting the image, just to be on the safe side. Update the
2701 * image contents straight away, as this enables cancelling. */
2702 for (unsigned i = 0; i < cBlocksAllocated; i++)
2703 paBlocks2[i] = VDI_IMAGE_BLOCK_FREE;
2704 rc = VINF_SUCCESS;
2705 for (unsigned i = 0; i < cBlocks; i++)
2706 {
2707 VDIIMAGEBLOCKPOINTER ptrBlock = pImage->paBlocks[i];
2708 if (IS_VDI_IMAGE_BLOCK_ALLOCATED(ptrBlock))
2709 {
2710 if (ptrBlock < cBlocksAllocated)
2711 {
2712 if (paBlocks2[ptrBlock] == VDI_IMAGE_BLOCK_FREE)
2713 paBlocks2[ptrBlock] = i;
2714 else
2715 {
2716 LogFunc(("Freed cross-linked block %u in file \"%s\"\n",
2717 i, pImage->pszFilename));
2718 pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
2719 rc = vdiUpdateBlockInfo(pImage, i);
2720 if (RT_FAILURE(rc))
2721 break;
2722 }
2723 }
2724 else
2725 {
2726 LogFunc(("Freed out of bounds reference for block %u in file \"%s\"\n",
2727 i, pImage->pszFilename));
2728 pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
2729 rc = vdiUpdateBlockInfo(pImage, i);
2730 if (RT_FAILURE(rc))
2731 break;
2732 }
2733 }
2734 }
2735 if (RT_FAILURE(rc))
2736 break;
2737
2738 /* Find redundant information and update the block pointers
2739 * accordingly, creating bubbles. Keep disk up to date, as this
2740 * enables cancelling. */
2741 for (unsigned i = 0; i < cBlocks; i++)
2742 {
2743 VDIIMAGEBLOCKPOINTER ptrBlock = pImage->paBlocks[i];
2744 if (IS_VDI_IMAGE_BLOCK_ALLOCATED(ptrBlock))
2745 {
2746 /* Block present in image file, read relevant data. */
2747 uint64_t u64Offset = (uint64_t)ptrBlock * pImage->cbTotalBlockData
2748 + (pImage->offStartData + pImage->offStartBlockData);
2749 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, u64Offset, pvTmp, cbBlock);
2750 if (RT_FAILURE(rc))
2751 break;
2752
2753 if (ASMBitFirstSet((volatile void *)pvTmp, (uint32_t)cbBlock * 8) == -1)
2754 {
2755 pImage->paBlocks[i] = VDI_IMAGE_BLOCK_ZERO;
2756 rc = vdiUpdateBlockInfo(pImage, i);
2757 if (RT_FAILURE(rc))
2758 break;
2759 paBlocks2[ptrBlock] = VDI_IMAGE_BLOCK_FREE;
2760 /* Adjust progress info, one block to be relocated. */
2761 cBlocksToMove++;
2762 }
2763 else if (pfnParentRead)
2764 {
2765 rc = pfnParentRead(pvParent, (uint64_t)i * cbBlock, pvBuf, cbBlock);
2766 if (RT_FAILURE(rc))
2767 break;
2768 if (!memcmp(pvTmp, pvBuf, cbBlock))
2769 {
2770 pImage->paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
2771 rc = vdiUpdateBlockInfo(pImage, i);
2772 if (RT_FAILURE(rc))
2773 break;
2774 paBlocks2[ptrBlock] = VDI_IMAGE_BLOCK_FREE;
2775 /* Adjust progress info, one block to be relocated. */
2776 cBlocksToMove++;
2777 }
2778 }
2779 }
2780
2781 /* Check if the range is in use if the block is still allocated. */
2782 ptrBlock = pImage->paBlocks[i];
2783 if ( IS_VDI_IMAGE_BLOCK_ALLOCATED(ptrBlock)
2784 && pIfQueryRangeUse)
2785 {
2786 bool fUsed = true;
2787
2788 rc = vdIfQueryRangeUse(pIfQueryRangeUse, (uint64_t)i * cbBlock, cbBlock, &fUsed);
2789 if (RT_FAILURE(rc))
2790 break;
2791 if (!fUsed)
2792 {
2793 pImage->paBlocks[i] = VDI_IMAGE_BLOCK_ZERO;
2794 rc = vdiUpdateBlockInfo(pImage, i);
2795 if (RT_FAILURE(rc))
2796 break;
2797 paBlocks2[ptrBlock] = VDI_IMAGE_BLOCK_FREE;
2798 /* Adjust progress info, one block to be relocated. */
2799 cBlocksToMove++;
2800 }
2801 }
2802
2803 if (pIfProgress && pIfProgress->pfnProgress)
2804 {
2805 rc = pIfProgress->pfnProgress(pIfProgress->Core.pvUser,
2806 (uint64_t)i * uPercentSpan / (cBlocks + cBlocksToMove) + uPercentStart);
2807 if (RT_FAILURE(rc))
2808 break;
2809 }
2810 }
2811 if (RT_FAILURE(rc))
2812 break;
2813
2814 /* Fill bubbles with other data (if available). */
2815 unsigned cBlocksMoved = 0;
2816 unsigned uBlockUsedPos = cBlocksAllocated;
2817 for (unsigned i = 0; i < cBlocksAllocated; i++)
2818 {
2819 unsigned uBlock = paBlocks2[i];
2820 if (uBlock == VDI_IMAGE_BLOCK_FREE)
2821 {
2822 unsigned uBlockData = VDI_IMAGE_BLOCK_FREE;
2823 while (uBlockUsedPos > i && uBlockData == VDI_IMAGE_BLOCK_FREE)
2824 {
2825 uBlockUsedPos--;
2826 uBlockData = paBlocks2[uBlockUsedPos];
2827 }
2828 /* Terminate early if there is no block which needs copying. */
2829 if (uBlockUsedPos == i)
2830 break;
2831 uint64_t u64Offset = (uint64_t)uBlockUsedPos * pImage->cbTotalBlockData
2832 + (pImage->offStartData + pImage->offStartBlockData);
2833 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, u64Offset,
2834 pvTmp, cbBlock);
2835 u64Offset = (uint64_t)i * pImage->cbTotalBlockData
2836 + (pImage->offStartData + pImage->offStartBlockData);
2837 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, u64Offset,
2838 pvTmp, cbBlock);
2839 pImage->paBlocks[uBlockData] = i;
2840 setImageBlocksAllocated(&pImage->Header, cBlocksAllocated - cBlocksMoved);
2841 rc = vdiUpdateBlockInfo(pImage, uBlockData);
2842 if (RT_FAILURE(rc))
2843 break;
2844 paBlocks2[i] = uBlockData;
2845 paBlocks2[uBlockUsedPos] = VDI_IMAGE_BLOCK_FREE;
2846 cBlocksMoved++;
2847 }
2848
2849 if (pIfProgress && pIfProgress->pfnProgress)
2850 {
2851 rc = pIfProgress->pfnProgress(pIfProgress->Core.pvUser,
2852 (uint64_t)(cBlocks + cBlocksMoved) * uPercentSpan / (cBlocks + cBlocksToMove) + uPercentStart);
2853
2854 if (RT_FAILURE(rc))
2855 break;
2856 }
2857 }
2858 if (RT_FAILURE(rc))
2859 break;
2860
2861 /* Update image header. */
2862 setImageBlocksAllocated(&pImage->Header, uBlockUsedPos);
2863 vdiUpdateHeader(pImage);
2864
2865 /* Truncate the image to the proper size to finish compacting. */
2866 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage,
2867 (uint64_t)uBlockUsedPos * pImage->cbTotalBlockData
2868 + pImage->offStartData + pImage->offStartBlockData);
2869 } while (0);
2870
2871 if (paBlocks2)
2872 RTMemTmpFree(paBlocks2);
2873 if (pvTmp)
2874 RTMemTmpFree(pvTmp);
2875 if (pvBuf)
2876 RTMemTmpFree(pvBuf);
2877
2878 if (RT_SUCCESS(rc) && pIfProgress && pIfProgress->pfnProgress)
2879 {
2880 pIfProgress->pfnProgress(pIfProgress->Core.pvUser,
2881 uPercentStart + uPercentSpan);
2882 }
2883
2884 LogFlowFunc(("returns %Rrc\n", rc));
2885 return rc;
2886}
2887
2888
2889/** @copydoc VBOXHDDBACKEND::pfnResize */
2890static int vdiResize(void *pBackendData, uint64_t cbSize,
2891 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
2892 unsigned uPercentStart, unsigned uPercentSpan,
2893 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
2894 PVDINTERFACE pVDIfsOperation)
2895{
2896 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
2897 int rc = VINF_SUCCESS;
2898
2899 PFNVDPROGRESS pfnProgress = NULL;
2900 void *pvUser = NULL;
2901 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
2902
2903 /*
2904 * Making the image smaller is not supported at the moment.
2905 * Resizing is also not supported for fixed size images and
2906 * very old images.
2907 */
2908 /** @todo implement making the image smaller, it is the responsibility of
2909 * the user to know what he's doing. */
2910 if ( cbSize < getImageDiskSize(&pImage->Header)
2911 || GET_MAJOR_HEADER_VERSION(&pImage->Header) == 0
2912 || pImage->uImageFlags & VD_IMAGE_FLAGS_FIXED)
2913 rc = VERR_NOT_SUPPORTED;
2914 else if (cbSize > getImageDiskSize(&pImage->Header))
2915 {
2916 unsigned cBlocksAllocated = getImageBlocksAllocated(&pImage->Header); /** < Blocks currently allocated, doesn't change during resize */
2917 uint32_t cBlocksNew = cbSize / getImageBlockSize(&pImage->Header); /** < New number of blocks in the image after the resize */
2918 if (cbSize % getImageBlockSize(&pImage->Header))
2919 cBlocksNew++;
2920
2921 uint32_t cBlocksOld = getImageBlocks(&pImage->Header); /** < Number of blocks before the resize. */
2922 uint64_t cbBlockspaceNew = cBlocksNew * sizeof(VDIIMAGEBLOCKPOINTER); /** < Required space for the block array after the resize. */
2923 uint64_t offStartDataNew = RT_ALIGN_32(pImage->offStartBlocks + cbBlockspaceNew, VDI_DATA_ALIGN); /** < New start offset for block data after the resize */
2924
2925 if ( pImage->offStartData != offStartDataNew
2926 && cBlocksAllocated > 0)
2927 {
2928 /* Calculate how many sectors need to be relocated. */
2929 uint64_t cbOverlapping = offStartDataNew - pImage->offStartData;
2930 unsigned cBlocksReloc = cbOverlapping / getImageBlockSize(&pImage->Header);
2931 if (cbOverlapping % getImageBlockSize(&pImage->Header))
2932 cBlocksReloc++;
2933
2934 /* Since only full blocks can be relocated the new data start is
2935 * determined by moving it block by block. */
2936 cBlocksReloc = RT_MIN(cBlocksReloc, cBlocksAllocated);
2937 offStartDataNew = pImage->offStartData;
2938
2939 /* Do the relocation. */
2940 LogFlow(("Relocating %u blocks\n", cBlocksReloc));
2941
2942 /*
2943 * Get the blocks we need to relocate first, they are appended to the end
2944 * of the image.
2945 */
2946 void *pvBuf = NULL, *pvZero = NULL;
2947 do
2948 {
2949 /* Allocate data buffer. */
2950 pvBuf = RTMemAllocZ(pImage->cbTotalBlockData);
2951 if (!pvBuf)
2952 {
2953 rc = VERR_NO_MEMORY;
2954 break;
2955 }
2956
2957 /* Allocate buffer for overwriting with zeroes. */
2958 pvZero = RTMemAllocZ(pImage->cbTotalBlockData);
2959 if (!pvZero)
2960 {
2961 rc = VERR_NO_MEMORY;
2962 break;
2963 }
2964
2965 for (unsigned i = 0; i < cBlocksReloc; i++)
2966 {
2967 /* Search the index in the block table. */
2968 for (unsigned idxBlock = 0; idxBlock < cBlocksOld; idxBlock++)
2969 {
2970 if (!pImage->paBlocks[idxBlock])
2971 {
2972 /* Read data and append to the end of the image. */
2973 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
2974 offStartDataNew, pvBuf,
2975 pImage->cbTotalBlockData);
2976 if (RT_FAILURE(rc))
2977 break;
2978
2979 uint64_t offBlockAppend;
2980 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &offBlockAppend);
2981 if (RT_FAILURE(rc))
2982 break;
2983
2984 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
2985 offBlockAppend, pvBuf,
2986 pImage->cbTotalBlockData);
2987 if (RT_FAILURE(rc))
2988 break;
2989
2990 /* Zero out the old block area. */
2991 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
2992 offStartDataNew, pvZero,
2993 pImage->cbTotalBlockData);
2994 if (RT_FAILURE(rc))
2995 break;
2996
2997 /* Update block counter. */
2998 pImage->paBlocks[idxBlock] = cBlocksAllocated - 1;
2999
3000 /*
3001 * Decrease the block number of all other entries in the array.
3002 * They were moved one block to the front.
3003 * Doing it as a separate step iterating over the array again
3004 * because an error while relocating the block might end up
3005 * in a corrupted image otherwise.
3006 */
3007 for (unsigned idxBlock2 = 0; idxBlock2 < cBlocksOld; idxBlock2++)
3008 {
3009 if ( idxBlock2 != idxBlock
3010 && IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[idxBlock2]))
3011 pImage->paBlocks[idxBlock2]--;
3012 }
3013
3014 /* Continue with the next block. */
3015 break;
3016 }
3017 }
3018
3019 if (RT_FAILURE(rc))
3020 break;
3021
3022 offStartDataNew += pImage->cbTotalBlockData;
3023 }
3024 } while (0);
3025
3026 if (pvBuf)
3027 RTMemFree(pvBuf);
3028 if (pvZero)
3029 RTMemFree(pvZero);
3030 }
3031
3032 /*
3033 * We need to update the new offsets for the image data in the out of memory
3034 * case too because we relocated the blocks already.
3035 */
3036 pImage->offStartData = offStartDataNew;
3037 setImageDataOffset(&pImage->Header, offStartDataNew);
3038
3039 /*
3040 * Relocation done, expand the block array and update the header with
3041 * the new data.
3042 */
3043 if (RT_SUCCESS(rc))
3044 {
3045 PVDIIMAGEBLOCKPOINTER paBlocksNew = (PVDIIMAGEBLOCKPOINTER)RTMemRealloc(pImage->paBlocks, cbBlockspaceNew);
3046 if (paBlocksNew)
3047 {
3048 pImage->paBlocks = paBlocksNew;
3049
3050 /* Mark the new blocks as unallocated. */
3051 for (unsigned idxBlock = cBlocksOld; idxBlock < cBlocksNew; idxBlock++)
3052 pImage->paBlocks[idxBlock] = VDI_IMAGE_BLOCK_FREE;
3053 }
3054 else
3055 rc = VERR_NO_MEMORY;
3056
3057 /* Write the block array before updating the rest. */
3058 vdiConvBlocksEndianess(VDIECONV_H2F, pImage->paBlocks, cBlocksNew);
3059 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, pImage->offStartBlocks,
3060 pImage->paBlocks, cbBlockspaceNew);
3061 vdiConvBlocksEndianess(VDIECONV_F2H, pImage->paBlocks, cBlocksNew);
3062
3063 if (RT_SUCCESS(rc))
3064 {
3065 /* Update size and new block count. */
3066 setImageDiskSize(&pImage->Header, cbSize);
3067 setImageBlocks(&pImage->Header, cBlocksNew);
3068 /* Update geometry. */
3069 pImage->PCHSGeometry = *pPCHSGeometry;
3070 pImage->cbImage = cbSize;
3071
3072 PVDIDISKGEOMETRY pGeometry = getImageLCHSGeometry(&pImage->Header);
3073 if (pGeometry)
3074 {
3075 pGeometry->cCylinders = pLCHSGeometry->cCylinders;
3076 pGeometry->cHeads = pLCHSGeometry->cHeads;
3077 pGeometry->cSectors = pLCHSGeometry->cSectors;
3078 pGeometry->cbSector = VDI_GEOMETRY_SECTOR_SIZE;
3079 }
3080 }
3081 }
3082
3083 /* Update header information in base image file. */
3084 vdiFlushImage(pImage);
3085 }
3086 /* Same size doesn't change the image at all. */
3087
3088 LogFlowFunc(("returns %Rrc\n", rc));
3089 return rc;
3090}
3091
3092
3093/** @copydoc VBOXHDDBACKEND::pfnDiscard */
3094static DECLCALLBACK(int) vdiDiscard(void *pBackendData,
3095 uint64_t uOffset, size_t cbDiscard,
3096 size_t *pcbPreAllocated,
3097 size_t *pcbPostAllocated,
3098 size_t *pcbActuallyDiscarded,
3099 void **ppbmAllocationBitmap,
3100 unsigned fDiscard)
3101{
3102 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
3103 unsigned uBlock;
3104 unsigned offDiscard;
3105 int rc = VINF_SUCCESS;
3106 void *pvBlock = NULL;
3107
3108 LogFlowFunc(("pBackendData=%#p uOffset=%llu cbDiscard=%zu pcbPreAllocated=%#p pcbPostAllocated=%#p pcbActuallyDiscarded=%#p ppbmAllocationBitmap=%#p fDiscard=%#x\n",
3109 pBackendData, uOffset, cbDiscard, pcbPreAllocated, pcbPostAllocated, pcbActuallyDiscarded, ppbmAllocationBitmap, fDiscard));
3110
3111 AssertPtr(pImage);
3112 Assert(!(uOffset % 512));
3113 Assert(!(cbDiscard % 512));
3114
3115 AssertMsgReturn(!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY),
3116 ("Image is readonly\n"), VERR_VD_IMAGE_READ_ONLY);
3117 AssertMsgReturn( uOffset + cbDiscard <= getImageDiskSize(&pImage->Header)
3118 && cbDiscard,
3119 ("Invalid parameters uOffset=%llu cbDiscard=%zu\n",
3120 uOffset, cbDiscard),
3121 VERR_INVALID_PARAMETER);
3122
3123 do
3124 {
3125 AssertMsgBreakStmt(!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY),
3126 ("Image is opened readonly\n"),
3127 rc = VERR_VD_IMAGE_READ_ONLY);
3128
3129 AssertMsgBreakStmt(cbDiscard,
3130 ("cbDiscard=%u\n", cbDiscard),
3131 rc = VERR_INVALID_PARAMETER);
3132
3133 /* Calculate starting block number and offset inside it. */
3134 uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
3135 offDiscard = (unsigned)uOffset & pImage->uBlockMask;
3136
3137 /* Clip range to at most the rest of the block. */
3138 cbDiscard = RT_MIN(cbDiscard, getImageBlockSize(&pImage->Header) - offDiscard);
3139 Assert(!(cbDiscard % 512));
3140
3141 if (pcbPreAllocated)
3142 *pcbPreAllocated = 0;
3143
3144 if (pcbPostAllocated)
3145 *pcbPostAllocated = 0;
3146
3147 if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
3148 {
3149 uint8_t *pbBlockData;
3150 size_t cbPreAllocated, cbPostAllocated;
3151
3152 cbPreAllocated = offDiscard % getImageBlockSize(&pImage->Header);
3153 cbPostAllocated = getImageBlockSize(&pImage->Header) - cbDiscard - cbPreAllocated;
3154
3155 /* Read the block data. */
3156 pvBlock = RTMemAlloc(pImage->cbTotalBlockData);
3157 if (!pvBlock)
3158 {
3159 rc = VERR_NO_MEMORY;
3160 break;
3161 }
3162
3163 if (!cbPreAllocated && !cbPostAllocated)
3164 {
3165 /*
3166 * Discarding a whole block, don't check for allocated sectors.
3167 * It is possible to just remove the whole block which avoids
3168 * one read and checking the whole block for data.
3169 */
3170 rc = vdiDiscardBlock(pImage, uBlock, pvBlock);
3171 }
3172 else
3173 {
3174 /* Read data. */
3175 pbBlockData = (uint8_t *)pvBlock + pImage->offStartBlockData;
3176
3177 uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData + pImage->offStartData;
3178 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, u64Offset,
3179 pvBlock, pImage->cbTotalBlockData);
3180 if (RT_FAILURE(rc))
3181 break;
3182
3183 /* Clear data. */
3184 memset(pbBlockData + offDiscard , 0, cbDiscard);
3185
3186 Assert(!(cbDiscard % 4));
3187 Assert(cbDiscard * 8 <= UINT32_MAX);
3188 if (ASMBitFirstSet((volatile void *)pbBlockData, getImageBlockSize(&pImage->Header) * 8) == -1)
3189 rc = vdiDiscardBlock(pImage, uBlock, pvBlock);
3190 else if (fDiscard & VD_DISCARD_MARK_UNUSED)
3191 {
3192 /* Write changed data to the image. */
3193 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, u64Offset + offDiscard,
3194 pbBlockData + offDiscard, cbDiscard);
3195 }
3196 else
3197 {
3198 /* Block has data, create allocation bitmap. */
3199 *pcbPreAllocated = cbPreAllocated;
3200 *pcbPostAllocated = cbPostAllocated;
3201 *ppbmAllocationBitmap = vdiAllocationBitmapCreate(pbBlockData, getImageBlockSize(&pImage->Header));
3202 if (RT_UNLIKELY(!*ppbmAllocationBitmap))
3203 rc = VERR_NO_MEMORY;
3204 else
3205 rc = VERR_VD_DISCARD_ALIGNMENT_NOT_MET;
3206 }
3207 } /* if: no complete block discarded */
3208 } /* if: Block is allocated. */
3209 /* else: nothing to do. */
3210 } while (0);
3211
3212 if (pcbActuallyDiscarded)
3213 *pcbActuallyDiscarded = cbDiscard;
3214
3215 if (pvBlock)
3216 RTMemFree(pvBlock);
3217
3218 LogFlowFunc(("returns %Rrc\n", rc));
3219 return rc;
3220}
3221
3222/** @copydoc VBOXHDDBACKEND::pfnDiscard */
3223static DECLCALLBACK(int) vdiAsyncDiscard(void *pBackendData, PVDIOCTX pIoCtx,
3224 uint64_t uOffset, size_t cbDiscard,
3225 size_t *pcbPreAllocated,
3226 size_t *pcbPostAllocated,
3227 size_t *pcbActuallyDiscarded,
3228 void **ppbmAllocationBitmap,
3229 unsigned fDiscard)
3230{
3231 PVDIIMAGEDESC pImage = (PVDIIMAGEDESC)pBackendData;
3232 unsigned uBlock;
3233 unsigned offDiscard;
3234 int rc = VINF_SUCCESS;
3235 void *pvBlock = NULL;
3236
3237 LogFlowFunc(("pBackendData=%#p pIoCtx=%#p uOffset=%llu cbDiscard=%zu pcbPreAllocated=%#p pcbPostAllocated=%#p pcbActuallyDiscarded=%#p ppbmAllocationBitmap=%#p fDiscard=%#x\n",
3238 pBackendData, pIoCtx, uOffset, cbDiscard, pcbPreAllocated, pcbPostAllocated, pcbActuallyDiscarded, ppbmAllocationBitmap, fDiscard));
3239
3240 AssertPtr(pImage);
3241 Assert(!(uOffset % 512));
3242 Assert(!(cbDiscard % 512));
3243
3244 AssertMsgReturn(!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY),
3245 ("Image is readonly\n"), VERR_VD_IMAGE_READ_ONLY);
3246 AssertMsgReturn( uOffset + cbDiscard <= getImageDiskSize(&pImage->Header)
3247 && cbDiscard,
3248 ("Invalid parameters uOffset=%llu cbDiscard=%zu\n",
3249 uOffset, cbDiscard),
3250 VERR_INVALID_PARAMETER);
3251
3252 do
3253 {
3254 AssertMsgBreakStmt(!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY),
3255 ("Image is opened readonly\n"),
3256 rc = VERR_VD_IMAGE_READ_ONLY);
3257
3258 AssertMsgBreakStmt(cbDiscard,
3259 ("cbDiscard=%u\n", cbDiscard),
3260 rc = VERR_INVALID_PARAMETER);
3261
3262 /* Calculate starting block number and offset inside it. */
3263 uBlock = (unsigned)(uOffset >> pImage->uShiftOffset2Index);
3264 offDiscard = (unsigned)uOffset & pImage->uBlockMask;
3265
3266 /* Clip range to at most the rest of the block. */
3267 cbDiscard = RT_MIN(cbDiscard, getImageBlockSize(&pImage->Header) - offDiscard);
3268 Assert(!(cbDiscard % 512));
3269
3270 if (pcbPreAllocated)
3271 *pcbPreAllocated = 0;
3272
3273 if (pcbPostAllocated)
3274 *pcbPostAllocated = 0;
3275
3276 if (IS_VDI_IMAGE_BLOCK_ALLOCATED(pImage->paBlocks[uBlock]))
3277 {
3278 uint8_t *pbBlockData;
3279 size_t cbPreAllocated, cbPostAllocated;
3280
3281 cbPreAllocated = offDiscard % getImageBlockSize(&pImage->Header);
3282 cbPostAllocated = getImageBlockSize(&pImage->Header) - cbDiscard - cbPreAllocated;
3283
3284 /* Read the block data. */
3285 pvBlock = RTMemAlloc(pImage->cbTotalBlockData);
3286 if (!pvBlock)
3287 {
3288 rc = VERR_NO_MEMORY;
3289 break;
3290 }
3291
3292 if (!cbPreAllocated && !cbPostAllocated)
3293 {
3294 /*
3295 * Discarding a whole block, don't check for allocated sectors.
3296 * It is possible to just remove the whole block which avoids
3297 * one read and checking the whole block for data.
3298 */
3299 rc = vdiDiscardBlockAsync(pImage, pIoCtx, uBlock, pvBlock);
3300 }
3301 else if (fDiscard & VD_DISCARD_MARK_UNUSED)
3302 {
3303 /* Just zero out the given range. */
3304 memset(pvBlock, 0, cbDiscard);
3305
3306 uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData + pImage->offStartData + offDiscard;
3307 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
3308 u64Offset, pvBlock, cbDiscard, pIoCtx,
3309 NULL, NULL);
3310 RTMemFree(pvBlock);
3311 }
3312 else
3313 {
3314 /*
3315 * Read complete block as metadata, the I/O context has no memory buffer
3316 * and we need to access the content directly anyway.
3317 */
3318 PVDMETAXFER pMetaXfer;
3319 pbBlockData = (uint8_t *)pvBlock + pImage->offStartBlockData;
3320
3321 uint64_t u64Offset = (uint64_t)pImage->paBlocks[uBlock] * pImage->cbTotalBlockData + pImage->offStartData;
3322 rc = vdIfIoIntFileReadMeta(pImage->pIfIo, pImage->pStorage, u64Offset,
3323 pbBlockData, pImage->cbTotalBlockData,
3324 pIoCtx, &pMetaXfer, NULL, NULL);
3325 if (RT_FAILURE(rc))
3326 {
3327 RTMemFree(pvBlock);
3328 break;
3329 }
3330
3331 vdIfIoIntMetaXferRelease(pImage->pIfIo, pMetaXfer);
3332
3333 /* Clear data. */
3334 memset(pbBlockData + offDiscard , 0, cbDiscard);
3335
3336 Assert(!(cbDiscard % 4));
3337 Assert(getImageBlockSize(&pImage->Header) * 8 <= UINT32_MAX);
3338 if (ASMBitFirstSet((volatile void *)pbBlockData, getImageBlockSize(&pImage->Header) * 8) == -1)
3339 rc = vdiDiscardBlockAsync(pImage, pIoCtx, uBlock, pvBlock);
3340 else
3341 {
3342 /* Block has data, create allocation bitmap. */
3343 *pcbPreAllocated = cbPreAllocated;
3344 *pcbPostAllocated = cbPostAllocated;
3345 *ppbmAllocationBitmap = vdiAllocationBitmapCreate(pbBlockData, getImageBlockSize(&pImage->Header));
3346 if (RT_UNLIKELY(!*ppbmAllocationBitmap))
3347 rc = VERR_NO_MEMORY;
3348 else
3349 rc = VERR_VD_DISCARD_ALIGNMENT_NOT_MET;
3350
3351 RTMemFree(pvBlock);
3352 }
3353 } /* if: no complete block discarded */
3354 } /* if: Block is allocated. */
3355 /* else: nothing to do. */
3356 } while (0);
3357
3358 if (pcbActuallyDiscarded)
3359 *pcbActuallyDiscarded = cbDiscard;
3360
3361 LogFlowFunc(("returns %Rrc\n", rc));
3362 return rc;
3363}
3364
3365/** @copydoc VBOXHDDBACKEND::pfnRepair */
3366static DECLCALLBACK(int) vdiRepair(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
3367 PVDINTERFACE pVDIfsImage, uint32_t fFlags)
3368{
3369 LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p\n", pszFilename, pVDIfsDisk, pVDIfsImage));
3370 int rc;
3371 PVDINTERFACEERROR pIfError;
3372 PVDINTERFACEIOINT pIfIo;
3373 PVDIOSTORAGE pStorage;
3374 uint64_t cbFile;
3375 PVDIIMAGEBLOCKPOINTER paBlocks = NULL;
3376 uint32_t *pu32BlockBitmap = NULL;
3377 VDIPREHEADER PreHdr;
3378 VDIHEADER Hdr;
3379
3380 pIfIo = VDIfIoIntGet(pVDIfsImage);
3381 AssertPtrReturn(pIfIo, VERR_INVALID_PARAMETER);
3382
3383 pIfError = VDIfErrorGet(pVDIfsDisk);
3384
3385 do
3386 {
3387 bool fRepairHdr = false;
3388 bool fRepairBlockArray = false;
3389
3390 rc = vdIfIoIntFileOpen(pIfIo, pszFilename,
3391 VDOpenFlagsToFileOpenFlags( fFlags & VD_REPAIR_DRY_RUN
3392 ? VD_OPEN_FLAGS_READONLY
3393 : 0,
3394 false /* fCreate */),
3395 &pStorage);
3396 if (RT_FAILURE(rc))
3397 {
3398 rc = vdIfError(pIfError, rc, RT_SRC_POS, "VDI: Failed to open image \"%s\"", pszFilename);
3399 break;
3400 }
3401
3402 rc = vdIfIoIntFileGetSize(pIfIo, pStorage, &cbFile);
3403 if (RT_FAILURE(rc))
3404 {
3405 rc = vdIfError(pIfError, rc, RT_SRC_POS, "VDI: Failed to query image size");
3406 break;
3407 }
3408
3409 /* Read pre-header. */
3410 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, 0, &PreHdr, sizeof(PreHdr));
3411 if (RT_FAILURE(rc))
3412 {
3413 rc = vdIfError(pIfError, rc, RT_SRC_POS, N_("VDI: Error reading pre-header in '%s'"), pszFilename);
3414 break;
3415 }
3416 vdiConvPreHeaderEndianess(VDIECONV_F2H, &PreHdr, &PreHdr);
3417 rc = vdiValidatePreHeader(&PreHdr);
3418 if (RT_FAILURE(rc))
3419 {
3420 rc = vdIfError(pIfError, VERR_VD_IMAGE_REPAIR_IMPOSSIBLE, RT_SRC_POS,
3421 N_("VDI: invalid pre-header in '%s'"), pszFilename);
3422 break;
3423 }
3424
3425 /* Read header. */
3426 Hdr.uVersion = PreHdr.u32Version;
3427 switch (GET_MAJOR_HEADER_VERSION(&Hdr))
3428 {
3429 case 0:
3430 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, sizeof(PreHdr),
3431 &Hdr.u.v0, sizeof(Hdr.u.v0));
3432 if (RT_FAILURE(rc))
3433 rc = vdIfError(pIfError, rc, RT_SRC_POS, N_("VDI: error reading v0 header in '%s'"),
3434 pszFilename);
3435 vdiConvHeaderEndianessV0(VDIECONV_F2H, &Hdr.u.v0, &Hdr.u.v0);
3436 break;
3437 case 1:
3438 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, sizeof(PreHdr),
3439 &Hdr.u.v1, sizeof(Hdr.u.v1));
3440 if (RT_FAILURE(rc))
3441 {
3442 rc = vdIfError(pIfError, rc, RT_SRC_POS, N_("VDI: error reading v1 header in '%s'"),
3443 pszFilename);
3444 }
3445 vdiConvHeaderEndianessV1(VDIECONV_F2H, &Hdr.u.v1, &Hdr.u.v1);
3446 if (Hdr.u.v1.cbHeader >= sizeof(Hdr.u.v1plus))
3447 {
3448 /* Read the VDI 1.1+ header completely. */
3449 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, sizeof(PreHdr),
3450 &Hdr.u.v1plus, sizeof(Hdr.u.v1plus));
3451 if (RT_FAILURE(rc))
3452 rc = vdIfError(pIfError, rc, RT_SRC_POS, N_("VDI: error reading v1.1+ header in '%s'"),
3453 pszFilename);
3454 vdiConvHeaderEndianessV1p(VDIECONV_F2H, &Hdr.u.v1plus, &Hdr.u.v1plus);
3455 }
3456 break;
3457 default:
3458 rc = vdIfError(pIfError, VERR_VD_IMAGE_REPAIR_IMPOSSIBLE, RT_SRC_POS,
3459 N_("VDI: unsupported major version %u in '%s'"),
3460 GET_MAJOR_HEADER_VERSION(&Hdr), pszFilename);
3461 break;
3462 }
3463
3464 if (RT_SUCCESS(rc))
3465 {
3466 rc = vdiValidateHeader(&Hdr);
3467 if (RT_FAILURE(rc))
3468 {
3469 rc = vdIfError(pIfError, VERR_VD_IMAGE_REPAIR_IMPOSSIBLE, RT_SRC_POS,
3470 N_("VDI: invalid header in '%s'"), pszFilename);
3471 break;
3472 }
3473 }
3474
3475 /* Setup image parameters by header. */
3476 uint64_t offStartBlocks, offStartData;
3477 size_t cbTotalBlockData;
3478
3479 offStartBlocks = getImageBlocksOffset(&Hdr);
3480 offStartData = getImageDataOffset(&Hdr);
3481 cbTotalBlockData = getImageExtraBlockSize(&Hdr) + getImageBlockSize(&Hdr);
3482
3483 /* Allocate memory for blocks array. */
3484 paBlocks = (PVDIIMAGEBLOCKPOINTER)RTMemAlloc(sizeof(VDIIMAGEBLOCKPOINTER) * getImageBlocks(&Hdr));
3485 if (!paBlocks)
3486 {
3487 rc = vdIfError(pIfError, VERR_NO_MEMORY, RT_SRC_POS,
3488 "Failed to allocate memory for block array");
3489 break;
3490 }
3491
3492 /* Read blocks array. */
3493 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, offStartBlocks, paBlocks,
3494 getImageBlocks(&Hdr) * sizeof(VDIIMAGEBLOCKPOINTER));
3495 if (RT_FAILURE(rc))
3496 {
3497 rc = vdIfError(pIfError, VERR_VD_IMAGE_REPAIR_IMPOSSIBLE, RT_SRC_POS,
3498 "Failed to read block array (at %llu), %Rrc",
3499 offStartBlocks, rc);
3500 break;
3501 }
3502 vdiConvBlocksEndianess(VDIECONV_F2H, paBlocks, getImageBlocks(&Hdr));
3503
3504 pu32BlockBitmap = (uint32_t *)RTMemAllocZ(RT_ALIGN_Z(getImageBlocks(&Hdr) / 8, 4));
3505 if (!pu32BlockBitmap)
3506 {
3507 rc = vdIfError(pIfError, VERR_NO_MEMORY, RT_SRC_POS,
3508 "Failed to allocate memory for block bitmap");
3509 break;
3510 }
3511
3512 for (uint32_t i = 0; i < getImageBlocks(&Hdr); i++)
3513 {
3514 if (IS_VDI_IMAGE_BLOCK_ALLOCATED(paBlocks[i]))
3515 {
3516 uint64_t offBlock = (uint64_t)paBlocks[i] * cbTotalBlockData
3517 + offStartData;
3518
3519 /*
3520 * Check that the offsets are valid (inside of the image) and
3521 * that there are no double references.
3522 */
3523 if (offBlock + cbTotalBlockData > cbFile)
3524 {
3525 vdIfErrorMessage(pIfError, "Entry %u points to invalid offset %llu, clearing\n",
3526 i, offBlock);
3527 paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
3528 fRepairBlockArray = true;
3529 }
3530 else if (ASMBitTestAndSet(pu32BlockBitmap, paBlocks[i]))
3531 {
3532 vdIfErrorMessage(pIfError, "Entry %u points to an already referenced data block, clearing\n",
3533 i);
3534 paBlocks[i] = VDI_IMAGE_BLOCK_FREE;
3535 fRepairBlockArray = true;
3536 }
3537 }
3538 }
3539
3540 /* Write repaired structures now. */
3541 if (!fRepairBlockArray)
3542 vdIfErrorMessage(pIfError, "VDI image is in a consistent state, no repair required\n");
3543 else if (!(fFlags & VD_REPAIR_DRY_RUN))
3544 {
3545 vdIfErrorMessage(pIfError, "Writing repaired block allocation table...\n");
3546
3547 vdiConvBlocksEndianess(VDIECONV_H2F, paBlocks, getImageBlocks(&Hdr));
3548 rc = vdIfIoIntFileWriteSync(pIfIo, pStorage, offStartBlocks, paBlocks,
3549 getImageBlocks(&Hdr) * sizeof(VDIIMAGEBLOCKPOINTER));
3550 if (RT_FAILURE(rc))
3551 {
3552 rc = vdIfError(pIfError, VERR_VD_IMAGE_REPAIR_IMPOSSIBLE, RT_SRC_POS,
3553 "Could not write repaired block allocation table (at %llu), %Rrc",
3554 offStartBlocks, rc);
3555 break;
3556 }
3557 }
3558
3559 vdIfErrorMessage(pIfError, "Corrupted VDI image repaired successfully\n");
3560 } while(0);
3561
3562 if (paBlocks)
3563 RTMemFree(paBlocks);
3564
3565 if (pu32BlockBitmap)
3566 RTMemFree(pu32BlockBitmap);
3567
3568 if (pStorage)
3569 vdIfIoIntFileClose(pIfIo, pStorage);
3570
3571 LogFlowFunc(("returns %Rrc\n", rc));
3572 return rc;
3573}
3574
3575VBOXHDDBACKEND g_VDIBackend =
3576{
3577 /* pszBackendName */
3578 "VDI",
3579 /* cbSize */
3580 sizeof(VBOXHDDBACKEND),
3581 /* uBackendCaps */
3582 VD_CAP_UUID | VD_CAP_CREATE_FIXED | VD_CAP_CREATE_DYNAMIC
3583 | VD_CAP_DIFF | VD_CAP_FILE | VD_CAP_ASYNC | VD_CAP_VFS | VD_CAP_DISCARD,
3584 /* paFileExtensions */
3585 s_aVdiFileExtensions,
3586 /* paConfigInfo */
3587 NULL,
3588 /* hPlugin */
3589 NIL_RTLDRMOD,
3590 /* pfnCheckIfValid */
3591 vdiCheckIfValid,
3592 /* pfnOpen */
3593 vdiOpen,
3594 /* pfnCreate */
3595 vdiCreate,
3596 /* pfnRename */
3597 vdiRename,
3598 /* pfnClose */
3599 vdiClose,
3600 /* pfnRead */
3601 vdiRead,
3602 /* pfnWrite */
3603 vdiWrite,
3604 /* pfnFlush */
3605 vdiFlush,
3606 /* pfnGetVersion */
3607 vdiGetVersion,
3608 /* pfnGetSize */
3609 vdiGetSize,
3610 /* pfnGetFileSize */
3611 vdiGetFileSize,
3612 /* pfnGetPCHSGeometry */
3613 vdiGetPCHSGeometry,
3614 /* pfnSetPCHSGeometry */
3615 vdiSetPCHSGeometry,
3616 /* pfnGetLCHSGeometry */
3617 vdiGetLCHSGeometry,
3618 /* pfnSetLCHSGeometry */
3619 vdiSetLCHSGeometry,
3620 /* pfnGetImageFlags */
3621 vdiGetImageFlags,
3622 /* pfnGetOpenFlags */
3623 vdiGetOpenFlags,
3624 /* pfnSetOpenFlags */
3625 vdiSetOpenFlags,
3626 /* pfnGetComment */
3627 vdiGetComment,
3628 /* pfnSetComment */
3629 vdiSetComment,
3630 /* pfnGetUuid */
3631 vdiGetUuid,
3632 /* pfnSetUuid */
3633 vdiSetUuid,
3634 /* pfnGetModificationUuid */
3635 vdiGetModificationUuid,
3636 /* pfnSetModificationUuid */
3637 vdiSetModificationUuid,
3638 /* pfnGetParentUuid */
3639 vdiGetParentUuid,
3640 /* pfnSetParentUuid */
3641 vdiSetParentUuid,
3642 /* pfnGetParentModificationUuid */
3643 vdiGetParentModificationUuid,
3644 /* pfnSetParentModificationUuid */
3645 vdiSetParentModificationUuid,
3646 /* pfnDump */
3647 vdiDump,
3648 /* pfnGetTimeStamp */
3649 NULL,
3650 /* pfnGetParentTimeStamp */
3651 NULL,
3652 /* pfnSetParentTimeStamp */
3653 NULL,
3654 /* pfnGetParentFilename */
3655 NULL,
3656 /* pfnSetParentFilename */
3657 NULL,
3658 /* pfnAsyncRead */
3659 vdiAsyncRead,
3660 /* pfnAsyncWrite */
3661 vdiAsyncWrite,
3662 /* pfnAsyncFlush */
3663 vdiAsyncFlush,
3664 /* pfnComposeLocation */
3665 genericFileComposeLocation,
3666 /* pfnComposeName */
3667 genericFileComposeName,
3668 /* pfnCompact */
3669 vdiCompact,
3670 /* pfnResize */
3671 vdiResize,
3672 /* pfnDiscard */
3673 vdiDiscard,
3674 /* pfnAsyncDiscard */
3675 vdiAsyncDiscard,
3676 /* pfnRepair */
3677 vdiRepair
3678};
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette