VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/VDIHDDCore.cpp@ 33271

Last change on this file since 33271 was 33271, checked in by vboxsync, 14 years ago

Storage/VDI: Make the code dealing with comments bullet-proof, including the case where the image doesn't have the zero termination.

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