VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/VBoxHDD.cpp@ 19223

Last change on this file since 19223 was 19199, checked in by vboxsync, 16 years ago

Storage/VBoxHDD: fix bug leading to incorrectly ignoring writes. leads to data corruption when snapshots are used and the data written is the same as of the parent image.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 135.3 KB
Line 
1/* $Id: VBoxHDD.cpp 19199 2009-04-27 09:39:51Z vboxsync $ */
2/** @file
3 * VBoxHDD - VBox HDD Container implementation.
4 */
5
6/*
7 * Copyright (C) 2006-2008 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25#define LOG_GROUP LOG_GROUP_VD
26#include <VBox/VBoxHDD.h>
27#include <VBox/err.h>
28#include <VBox/sup.h>
29#include <VBox/log.h>
30
31#include <iprt/alloc.h>
32#include <iprt/assert.h>
33#include <iprt/uuid.h>
34#include <iprt/file.h>
35#include <iprt/string.h>
36#include <iprt/asm.h>
37#include <iprt/ldr.h>
38#include <iprt/dir.h>
39#include <iprt/path.h>
40#include <iprt/param.h>
41
42#include "VBoxHDD-Internal.h"
43
44
45#define VBOXHDDDISK_SIGNATURE 0x6f0e2a7d
46
47/** Buffer size used for merging images. */
48#define VD_MERGE_BUFFER_SIZE (16 * _1M)
49
50/**
51 * VBox HDD Container image descriptor.
52 */
53typedef struct VDIMAGE
54{
55 /** Link to parent image descriptor, if any. */
56 struct VDIMAGE *pPrev;
57 /** Link to child image descriptor, if any. */
58 struct VDIMAGE *pNext;
59 /** Container base filename. (UTF-8) */
60 char *pszFilename;
61 /** Data managed by the backend which keeps the actual info. */
62 void *pvBackendData;
63 /** Cached sanitized image flags. */
64 unsigned uImageFlags;
65 /** Image open flags (only those handled generically in this code and which
66 * the backends will never ever see). */
67 unsigned uOpenFlags;
68
69 /** Function pointers for the various backend methods. */
70 PCVBOXHDDBACKEND Backend;
71
72 /** Pointer to list of VD interfaces, per-image. */
73 PVDINTERFACE pVDIfsImage;
74} VDIMAGE, *PVDIMAGE;
75
76/**
77 * uModified bit flags.
78 */
79#define VD_IMAGE_MODIFIED_FLAG RT_BIT(0)
80#define VD_IMAGE_MODIFIED_FIRST RT_BIT(1)
81#define VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE RT_BIT(2)
82
83
84/**
85 * VBox HDD Container main structure, private part.
86 */
87struct VBOXHDD
88{
89 /** Structure signature (VBOXHDDDISK_SIGNATURE). */
90 uint32_t u32Signature;
91
92 /** Number of opened images. */
93 unsigned cImages;
94
95 /** Base image. */
96 PVDIMAGE pBase;
97
98 /** Last opened image in the chain.
99 * The same as pBase if only one image is used. */
100 PVDIMAGE pLast;
101
102 /** Flags representing the modification state. */
103 unsigned uModified;
104
105 /** Cached size of this disk. */
106 uint64_t cbSize;
107 /** Cached PCHS geometry for this disk. */
108 PDMMEDIAGEOMETRY PCHSGeometry;
109 /** Cached LCHS geometry for this disk. */
110 PDMMEDIAGEOMETRY LCHSGeometry;
111
112 /** Pointer to list of VD interfaces, per-disk. */
113 PVDINTERFACE pVDIfsDisk;
114 /** Pointer to the common interface structure for error reporting. */
115 PVDINTERFACE pInterfaceError;
116 /** Pointer to the error interface we use if available. */
117 PVDINTERFACEERROR pInterfaceErrorCallbacks;
118};
119
120
121/**
122 * VBox parent read descriptor, used internally for compaction.
123 */
124typedef struct VDPARENTSTATEDESC
125{
126 /** Pointer to disk descriptor. */
127 PVBOXHDD pDisk;
128 /** Pointer to image descriptor. */
129 PVDIMAGE pImage;
130} VDPARENTSTATEDESC, *PVDPARENTSTATEDESC;
131
132
133extern VBOXHDDBACKEND g_RawBackend;
134extern VBOXHDDBACKEND g_VmdkBackend;
135extern VBOXHDDBACKEND g_VDIBackend;
136extern VBOXHDDBACKEND g_VhdBackend;
137#ifdef VBOX_WITH_ISCSI
138extern VBOXHDDBACKEND g_ISCSIBackend;
139#endif
140
141static unsigned g_cBackends = 0;
142static PVBOXHDDBACKEND *g_apBackends = NULL;
143static PVBOXHDDBACKEND aStaticBackends[] =
144{
145 &g_RawBackend,
146 &g_VmdkBackend,
147 &g_VDIBackend,
148 &g_VhdBackend
149#ifdef VBOX_WITH_ISCSI
150 ,&g_ISCSIBackend
151#endif
152};
153
154/**
155 * internal: add several backends.
156 */
157static int vdAddBackends(PVBOXHDDBACKEND *ppBackends, unsigned cBackends)
158{
159 PVBOXHDDBACKEND *pTmp = (PVBOXHDDBACKEND*)RTMemRealloc(g_apBackends,
160 (g_cBackends + cBackends) * sizeof(PVBOXHDDBACKEND));
161 if (RT_UNLIKELY(!pTmp))
162 return VERR_NO_MEMORY;
163 g_apBackends = pTmp;
164 memcpy(&g_apBackends[g_cBackends], ppBackends, cBackends * sizeof(PVBOXHDDBACKEND));
165 g_cBackends += cBackends;
166 return VINF_SUCCESS;
167}
168
169/**
170 * internal: add single backend.
171 */
172DECLINLINE(int) vdAddBackend(PVBOXHDDBACKEND pBackend)
173{
174 return vdAddBackends(&pBackend, 1);
175}
176
177/**
178 * internal: issue error message.
179 */
180static int vdError(PVBOXHDD pDisk, int rc, RT_SRC_POS_DECL,
181 const char *pszFormat, ...)
182{
183 va_list va;
184 va_start(va, pszFormat);
185 if (pDisk->pInterfaceErrorCallbacks)
186 pDisk->pInterfaceErrorCallbacks->pfnError(pDisk->pInterfaceError->pvUser, rc, RT_SRC_POS_ARGS, pszFormat, va);
187 va_end(va);
188 return rc;
189}
190
191/**
192 * internal: find image format backend.
193 */
194static int vdFindBackend(const char *pszBackend, PCVBOXHDDBACKEND *ppBackend)
195{
196 int rc = VINF_SUCCESS;
197 PCVBOXHDDBACKEND pBackend = NULL;
198
199 if (!g_apBackends)
200 VDInit();
201
202 for (unsigned i = 0; i < g_cBackends; i++)
203 {
204 if (!RTStrICmp(pszBackend, g_apBackends[i]->pszBackendName))
205 {
206 pBackend = g_apBackends[i];
207 break;
208 }
209 }
210 *ppBackend = pBackend;
211 return rc;
212}
213
214/**
215 * internal: add image structure to the end of images list.
216 */
217static void vdAddImageToList(PVBOXHDD pDisk, PVDIMAGE pImage)
218{
219 pImage->pPrev = NULL;
220 pImage->pNext = NULL;
221
222 if (pDisk->pBase)
223 {
224 Assert(pDisk->cImages > 0);
225 pImage->pPrev = pDisk->pLast;
226 pDisk->pLast->pNext = pImage;
227 pDisk->pLast = pImage;
228 }
229 else
230 {
231 Assert(pDisk->cImages == 0);
232 pDisk->pBase = pImage;
233 pDisk->pLast = pImage;
234 }
235
236 pDisk->cImages++;
237}
238
239/**
240 * internal: remove image structure from the images list.
241 */
242static void vdRemoveImageFromList(PVBOXHDD pDisk, PVDIMAGE pImage)
243{
244 Assert(pDisk->cImages > 0);
245
246 if (pImage->pPrev)
247 pImage->pPrev->pNext = pImage->pNext;
248 else
249 pDisk->pBase = pImage->pNext;
250
251 if (pImage->pNext)
252 pImage->pNext->pPrev = pImage->pPrev;
253 else
254 pDisk->pLast = pImage->pPrev;
255
256 pImage->pPrev = NULL;
257 pImage->pNext = NULL;
258
259 pDisk->cImages--;
260}
261
262/**
263 * internal: find image by index into the images list.
264 */
265static PVDIMAGE vdGetImageByNumber(PVBOXHDD pDisk, unsigned nImage)
266{
267 PVDIMAGE pImage = pDisk->pBase;
268 if (nImage == VD_LAST_IMAGE)
269 return pDisk->pLast;
270 while (pImage && nImage)
271 {
272 pImage = pImage->pNext;
273 nImage--;
274 }
275 return pImage;
276}
277
278/**
279 * internal: read the specified amount of data in whatever blocks the backend
280 * will give us.
281 */
282static int vdReadHelper(PVBOXHDD pDisk, PVDIMAGE pImage, uint64_t uOffset,
283 void *pvBuf, size_t cbRead)
284{
285 int rc;
286 size_t cbThisRead;
287
288 /* Loop until all read. */
289 do
290 {
291 /* Search for image with allocated block. Do not attempt to read more
292 * than the previous reads marked as valid. Otherwise this would return
293 * stale data when different block sizes are used for the images. */
294 cbThisRead = cbRead;
295 rc = VERR_VD_BLOCK_FREE;
296 for (PVDIMAGE pCurrImage = pImage;
297 pCurrImage != NULL && rc == VERR_VD_BLOCK_FREE;
298 pCurrImage = pCurrImage->pPrev)
299 {
300 rc = pCurrImage->Backend->pfnRead(pCurrImage->pvBackendData,
301 uOffset, pvBuf, cbThisRead,
302 &cbThisRead);
303 }
304
305 /* No image in the chain contains the data for the block. */
306 if (rc == VERR_VD_BLOCK_FREE)
307 {
308 memset(pvBuf, '\0', cbThisRead);
309 rc = VINF_SUCCESS;
310 }
311
312 cbRead -= cbThisRead;
313 uOffset += cbThisRead;
314 pvBuf = (char *)pvBuf + cbThisRead;
315 } while (cbRead != 0 && RT_SUCCESS(rc));
316
317 return rc;
318}
319
320/**
321 * internal: parent image read wrapper for compacting.
322 */
323static int vdParentRead(void *pvUser, uint64_t uOffset, void *pvBuf,
324 size_t cbRead)
325{
326 PVDPARENTSTATEDESC pParentState = (PVDPARENTSTATEDESC)pvUser;
327 return vdReadHelper(pParentState->pDisk, pParentState->pImage, uOffset,
328 pvBuf, cbRead);
329}
330
331/**
332 * internal: mark the disk as not modified.
333 */
334static void vdResetModifiedFlag(PVBOXHDD pDisk)
335{
336 if (pDisk->uModified & VD_IMAGE_MODIFIED_FLAG)
337 {
338 /* generate new last-modified uuid */
339 if (!(pDisk->uModified & VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
340 {
341 RTUUID Uuid;
342
343 RTUuidCreate(&Uuid);
344 pDisk->pLast->Backend->pfnSetModificationUuid(pDisk->pLast->pvBackendData,
345 &Uuid);
346 }
347
348 pDisk->uModified &= ~VD_IMAGE_MODIFIED_FLAG;
349 }
350}
351
352/**
353 * internal: mark the disk as modified.
354 */
355static void vdSetModifiedFlag(PVBOXHDD pDisk)
356{
357 pDisk->uModified |= VD_IMAGE_MODIFIED_FLAG;
358 if (pDisk->uModified & VD_IMAGE_MODIFIED_FIRST)
359 {
360 pDisk->uModified &= ~VD_IMAGE_MODIFIED_FIRST;
361
362 /* First modify, so create a UUID and ensure it's written to disk. */
363 vdResetModifiedFlag(pDisk);
364
365 if (!(pDisk->uModified | VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
366 pDisk->pLast->Backend->pfnFlush(pDisk->pLast->pvBackendData);
367 }
368}
369
370/**
371 * internal: write a complete block (only used for diff images), taking the
372 * remaining data from parent images. This implementation does not optimize
373 * anything (except that it tries to read only that portions from parent
374 * images that are really needed).
375 */
376static int vdWriteHelperStandard(PVBOXHDD pDisk, PVDIMAGE pImage,
377 uint64_t uOffset, size_t cbWrite,
378 size_t cbThisWrite, size_t cbPreRead,
379 size_t cbPostRead, const void *pvBuf,
380 void *pvTmp)
381{
382 int rc = VINF_SUCCESS;
383
384 /* Read the data that goes before the write to fill the block. */
385 if (cbPreRead)
386 {
387 rc = vdReadHelper(pDisk, pImage, uOffset - cbPreRead, pvTmp,
388 cbPreRead);
389 if (RT_FAILURE(rc))
390 return rc;
391 }
392
393 /* Copy the data to the right place in the buffer. */
394 memcpy((char *)pvTmp + cbPreRead, pvBuf, cbThisWrite);
395
396 /* Read the data that goes after the write to fill the block. */
397 if (cbPostRead)
398 {
399 /* If we have data to be written, use that instead of reading
400 * data from the image. */
401 size_t cbWriteCopy;
402 if (cbWrite > cbThisWrite)
403 cbWriteCopy = RT_MIN(cbWrite - cbThisWrite, cbPostRead);
404 else
405 cbWriteCopy = 0;
406 /* Figure out how much we cannnot read from the image, because
407 * the last block to write might exceed the nominal size of the
408 * image for technical reasons. */
409 size_t cbFill;
410 if (uOffset + cbThisWrite + cbPostRead > pDisk->cbSize)
411 cbFill = uOffset + cbThisWrite + cbPostRead - pDisk->cbSize;
412 else
413 cbFill = 0;
414 /* The rest must be read from the image. */
415 size_t cbReadImage = cbPostRead - cbWriteCopy - cbFill;
416
417 /* Now assemble the remaining data. */
418 if (cbWriteCopy)
419 memcpy((char *)pvTmp + cbPreRead + cbThisWrite,
420 (char *)pvBuf + cbThisWrite, cbWriteCopy);
421 if (cbReadImage)
422 rc = vdReadHelper(pDisk, pImage,
423 uOffset + cbThisWrite + cbWriteCopy,
424 (char *)pvTmp + cbPreRead + cbThisWrite + cbWriteCopy,
425 cbReadImage);
426 if (RT_FAILURE(rc))
427 return rc;
428 /* Zero out the remainder of this block. Will never be visible, as this
429 * is beyond the limit of the image. */
430 if (cbFill)
431 memset((char *)pvTmp + cbPreRead + cbThisWrite + cbWriteCopy + cbReadImage,
432 '\0', cbFill);
433 }
434
435 /* Write the full block to the virtual disk. */
436 rc = pImage->Backend->pfnWrite(pImage->pvBackendData,
437 uOffset - cbPreRead, pvTmp,
438 cbPreRead + cbThisWrite + cbPostRead,
439 NULL, &cbPreRead, &cbPostRead, 0);
440 Assert(rc != VERR_VD_BLOCK_FREE);
441 Assert(cbPreRead == 0);
442 Assert(cbPostRead == 0);
443
444 return rc;
445}
446
447/**
448 * internal: write a complete block (only used for diff images), taking the
449 * remaining data from parent images. This implementation optimizes out writes
450 * that do not change the data relative to the state as of the parent images.
451 * All backends which support differential/growing images support this.
452 */
453static int vdWriteHelperOptimized(PVBOXHDD pDisk, PVDIMAGE pImage,
454 uint64_t uOffset, size_t cbWrite,
455 size_t cbThisWrite, size_t cbPreRead,
456 size_t cbPostRead, const void *pvBuf,
457 void *pvTmp)
458{
459 size_t cbFill = 0;
460 size_t cbWriteCopy = 0;
461 size_t cbReadImage = 0;
462 int rc;
463
464 if (cbPostRead)
465 {
466 /* Figure out how much we cannnot read from the image, because
467 * the last block to write might exceed the nominal size of the
468 * image for technical reasons. */
469 if (uOffset + cbThisWrite + cbPostRead > pDisk->cbSize)
470 cbFill = uOffset + cbThisWrite + cbPostRead - pDisk->cbSize;
471
472 /* If we have data to be written, use that instead of reading
473 * data from the image. */
474 if (cbWrite > cbThisWrite)
475 cbWriteCopy = RT_MIN(cbWrite - cbThisWrite, cbPostRead);
476
477 /* The rest must be read from the image. */
478 cbReadImage = cbPostRead - cbWriteCopy - cbFill;
479 }
480
481 /* Read the entire data of the block so that we can compare whether it will
482 * be modified by the write or not. */
483 rc = vdReadHelper(pDisk, pImage, uOffset - cbPreRead, pvTmp,
484 cbPreRead + cbThisWrite + cbPostRead - cbFill);
485 if (RT_FAILURE(rc))
486 return rc;
487
488 /* Check if the write would modify anything in this block. */
489 if ( !memcmp((char *)pvTmp + cbPreRead, pvBuf, cbThisWrite)
490 && (!cbWriteCopy || !memcmp((char *)pvTmp + cbPreRead + cbThisWrite,
491 (char *)pvBuf + cbThisWrite, cbWriteCopy)))
492 {
493 /* Block is completely unchanged, so no need to write anything. */
494 return VINF_SUCCESS;
495 }
496
497 /* Copy the data to the right place in the buffer. */
498 memcpy((char *)pvTmp + cbPreRead, pvBuf, cbThisWrite);
499
500 /* Handle the data that goes after the write to fill the block. */
501 if (cbPostRead)
502 {
503 /* Now assemble the remaining data. */
504 if (cbWriteCopy)
505 memcpy((char *)pvTmp + cbPreRead + cbThisWrite,
506 (char *)pvBuf + cbThisWrite, cbWriteCopy);
507 /* Zero out the remainder of this block. Will never be visible, as this
508 * is beyond the limit of the image. */
509 if (cbFill)
510 memset((char *)pvTmp + cbPreRead + cbThisWrite + cbWriteCopy + cbReadImage,
511 '\0', cbFill);
512 }
513
514 /* Write the full block to the virtual disk. */
515 rc = pImage->Backend->pfnWrite(pImage->pvBackendData,
516 uOffset - cbPreRead, pvTmp,
517 cbPreRead + cbThisWrite + cbPostRead,
518 NULL, &cbPreRead, &cbPostRead, 0);
519 Assert(rc != VERR_VD_BLOCK_FREE);
520 Assert(cbPreRead == 0);
521 Assert(cbPostRead == 0);
522
523 return rc;
524}
525
526/**
527 * internal: write buffer to the image, taking care of block boundaries and
528 * write optimizations.
529 */
530static int vdWriteHelper(PVBOXHDD pDisk, PVDIMAGE pImage, uint64_t uOffset,
531 const void *pvBuf, size_t cbWrite)
532{
533 int rc;
534 unsigned fWrite;
535 size_t cbThisWrite;
536 size_t cbPreRead, cbPostRead;
537
538 /* Loop until all written. */
539 do
540 {
541 /* Try to write the possibly partial block to the last opened image.
542 * This works when the block is already allocated in this image or
543 * if it is a full-block write (and allocation isn't suppressed below).
544 * For image formats which don't support zero blocks, it's beneficial
545 * to avoid unnecessarily allocating unchanged blocks. This prevents
546 * unwanted expanding of images. VMDK is an example. */
547 cbThisWrite = cbWrite;
548 fWrite = (pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME)
549 ? 0 : VD_WRITE_NO_ALLOC;
550 rc = pImage->Backend->pfnWrite(pImage->pvBackendData, uOffset, pvBuf,
551 cbThisWrite, &cbThisWrite, &cbPreRead,
552 &cbPostRead, fWrite);
553 if (rc == VERR_VD_BLOCK_FREE)
554 {
555 void *pvTmp = RTMemTmpAlloc(cbPreRead + cbThisWrite + cbPostRead);
556 AssertBreakStmt(VALID_PTR(pvTmp), rc = VERR_NO_MEMORY);
557
558 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME))
559 {
560 /* Optimized write, suppress writing to a so far unallocated
561 * block if the data is in fact not changed. */
562 rc = vdWriteHelperOptimized(pDisk, pImage, uOffset, cbWrite,
563 cbThisWrite, cbPreRead, cbPostRead,
564 pvBuf, pvTmp);
565 }
566 else
567 {
568 /* Normal write, not optimized in any way. The block will
569 * be written no matter what. This will usually (unless the
570 * backend has some further optimization enabled) cause the
571 * block to be allocated. */
572 rc = vdWriteHelperStandard(pDisk, pImage, uOffset, cbWrite,
573 cbThisWrite, cbPreRead, cbPostRead,
574 pvBuf, pvTmp);
575 }
576 RTMemTmpFree(pvTmp);
577 if (RT_FAILURE(rc))
578 break;
579 }
580
581 cbWrite -= cbThisWrite;
582 uOffset += cbThisWrite;
583 pvBuf = (char *)pvBuf + cbThisWrite;
584 } while (cbWrite != 0 && RT_SUCCESS(rc));
585
586 return rc;
587}
588
589
590/**
591 * internal: scans plugin directory and loads the backends have been found.
592 */
593static int vdLoadDynamicBackends()
594{
595 int rc = VINF_SUCCESS;
596 PRTDIR pPluginDir = NULL;
597
598 /* Enumerate plugin backends. */
599 char szPath[RTPATH_MAX];
600 rc = RTPathAppPrivateArch(szPath, sizeof(szPath));
601 if (RT_FAILURE(rc))
602 return rc;
603
604 /* To get all entries with VBoxHDD as prefix. */
605 char *pszPluginFilter;
606 rc = RTStrAPrintf(&pszPluginFilter, "%s/%s*", szPath,
607 VBOX_HDDFORMAT_PLUGIN_PREFIX);
608 if (RT_FAILURE(rc))
609 {
610 rc = VERR_NO_MEMORY;
611 return rc;
612 }
613
614 PRTDIRENTRYEX pPluginDirEntry = NULL;
615 size_t cbPluginDirEntry = sizeof(RTDIRENTRYEX);
616 /* The plugins are in the same directory as the other shared libs. */
617 rc = RTDirOpenFiltered(&pPluginDir, pszPluginFilter, RTDIRFILTER_WINNT);
618 if (RT_FAILURE(rc))
619 {
620 /* On Windows the above immediately signals that there are no
621 * files matching, while on other platforms enumerating the
622 * files below fails. Either way: no plugins. */
623 goto out;
624 }
625
626 pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(sizeof(RTDIRENTRYEX));
627 if (!pPluginDirEntry)
628 {
629 rc = VERR_NO_MEMORY;
630 goto out;
631 }
632
633 while ((rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING)) != VERR_NO_MORE_FILES)
634 {
635 RTLDRMOD hPlugin = NIL_RTLDRMOD;
636 PFNVBOXHDDFORMATLOAD pfnHDDFormatLoad = NULL;
637 PVBOXHDDBACKEND pBackend = NULL;
638 char *pszPluginPath = NULL;
639
640 if (rc == VERR_BUFFER_OVERFLOW)
641 {
642 /* allocate new buffer. */
643 RTMemFree(pPluginDirEntry);
644 pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(cbPluginDirEntry);
645 /* Retry. */
646 rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING);
647 if (RT_FAILURE(rc))
648 break;
649 }
650 else if (RT_FAILURE(rc))
651 break;
652
653 /* We got the new entry. */
654 if (!RTFS_IS_FILE(pPluginDirEntry->Info.Attr.fMode))
655 continue;
656
657 /* Prepend the path to the libraries. */
658 rc = RTStrAPrintf(&pszPluginPath, "%s/%s", szPath, pPluginDirEntry->szName);
659 if (RT_FAILURE(rc))
660 {
661 rc = VERR_NO_MEMORY;
662 break;
663 }
664
665 rc = SUPR3HardenedLdrLoad(pszPluginPath, &hPlugin);
666 if (RT_SUCCESS(rc))
667 {
668 rc = RTLdrGetSymbol(hPlugin, VBOX_HDDFORMAT_LOAD_NAME, (void**)&pfnHDDFormatLoad);
669 if (RT_FAILURE(rc) || !pfnHDDFormatLoad)
670 {
671 LogFunc(("error resolving the entry point %s in plugin %s, rc=%Rrc, pfnHDDFormat=%#p\n", VBOX_HDDFORMAT_LOAD_NAME, pPluginDirEntry->szName, rc, pfnHDDFormatLoad));
672 if (RT_SUCCESS(rc))
673 rc = VERR_SYMBOL_NOT_FOUND;
674 }
675
676 if (RT_SUCCESS(rc))
677 {
678 /* Get the function table. */
679 rc = pfnHDDFormatLoad(&pBackend);
680 if (RT_SUCCESS(rc) && pBackend->cbSize == sizeof(VBOXHDDBACKEND))
681 {
682 pBackend->hPlugin = hPlugin;
683 vdAddBackend(pBackend);
684 }
685 else
686 LogFunc(("ignored plugin '%s': pBackend->cbSize=%d rc=%Rrc\n", pszPluginPath, pBackend->cbSize, rc));
687 }
688 else
689 LogFunc(("ignored plugin '%s': rc=%Rrc\n", pszPluginPath, rc));
690
691 if (RT_FAILURE(rc))
692 RTLdrClose(hPlugin);
693 }
694 RTStrFree(pszPluginPath);
695 }
696out:
697 if (rc == VERR_NO_MORE_FILES)
698 rc = VINF_SUCCESS;
699 RTStrFree(pszPluginFilter);
700 if (pPluginDirEntry)
701 RTMemFree(pPluginDirEntry);
702 if (pPluginDir)
703 RTDirClose(pPluginDir);
704 return rc;
705}
706
707/**
708 * Initializes HDD backends.
709 *
710 * @returns VBox status code.
711 */
712VBOXDDU_DECL(int) VDInit(void)
713{
714 int rc = vdAddBackends(aStaticBackends, RT_ELEMENTS(aStaticBackends));
715 if (RT_SUCCESS(rc))
716 rc = vdLoadDynamicBackends();
717 LogRel(("VDInit finished\n"));
718 return rc;
719}
720
721/**
722 * Destroys loaded HDD backends.
723 *
724 * @returns VBox status code.
725 */
726VBOXDDU_DECL(int) VDShutdown(void)
727{
728 PVBOXHDDBACKEND *pBackends = g_apBackends;
729 unsigned cBackends = g_cBackends;
730
731 if (!pBackends)
732 return VERR_INTERNAL_ERROR;
733
734 g_cBackends = 0;
735 g_apBackends = NULL;
736
737 for (unsigned i = 0; i < cBackends; i++)
738 if (pBackends[i]->hPlugin != NIL_RTLDRMOD)
739 RTLdrClose(pBackends[i]->hPlugin);
740
741 RTMemFree(pBackends);
742 return VINF_SUCCESS;
743}
744
745
746
747/**
748 * Lists all HDD backends and their capabilities in a caller-provided buffer.
749 *
750 * @todo this code contains memory leaks, inconsistent (and probably buggy)
751 * allocation, and it lacks documentation what the caller needs to free.
752 *
753 * @returns VBox status code.
754 * VERR_BUFFER_OVERFLOW if not enough space is passed.
755 * @param cEntriesAlloc Number of list entries available.
756 * @param pEntries Pointer to array for the entries.
757 * @param pcEntriesUsed Number of entries returned.
758 */
759VBOXDDU_DECL(int) VDBackendInfo(unsigned cEntriesAlloc, PVDBACKENDINFO pEntries,
760 unsigned *pcEntriesUsed)
761{
762 int rc = VINF_SUCCESS;
763 PRTDIR pPluginDir = NULL;
764 unsigned cEntries = 0;
765
766 LogFlowFunc(("cEntriesAlloc=%u pEntries=%#p pcEntriesUsed=%#p\n", cEntriesAlloc, pEntries, pcEntriesUsed));
767 /* Check arguments. */
768 AssertMsgReturn(cEntriesAlloc,
769 ("cEntriesAlloc=%u\n", cEntriesAlloc),
770 VERR_INVALID_PARAMETER);
771 AssertMsgReturn(VALID_PTR(pEntries),
772 ("pEntries=%#p\n", pEntries),
773 VERR_INVALID_PARAMETER);
774 AssertMsgReturn(VALID_PTR(pcEntriesUsed),
775 ("pcEntriesUsed=%#p\n", pcEntriesUsed),
776 VERR_INVALID_PARAMETER);
777 if (!g_apBackends)
778 VDInit();
779
780 if (cEntriesAlloc < g_cBackends)
781 {
782 *pcEntriesUsed = g_cBackends;
783 return VERR_BUFFER_OVERFLOW;
784 }
785
786 for (unsigned i = 0; i < g_cBackends; i++)
787 {
788 pEntries[i].pszBackend = g_apBackends[i]->pszBackendName;
789 pEntries[i].uBackendCaps = g_apBackends[i]->uBackendCaps;
790 pEntries[i].papszFileExtensions = g_apBackends[i]->papszFileExtensions;
791 pEntries[i].paConfigInfo = g_apBackends[i]->paConfigInfo;
792 pEntries[i].pfnComposeLocation = g_apBackends[i]->pfnComposeLocation;
793 pEntries[i].pfnComposeName = g_apBackends[i]->pfnComposeName;
794 }
795
796 LogFlowFunc(("returns %Rrc *pcEntriesUsed=%u\n", rc, cEntries));
797 *pcEntriesUsed = g_cBackends;
798 return rc;
799}
800
801/**
802 * Lists the capablities of a backend indentified by its name.
803 * Free all returned names with RTStrFree() when you no longer need them.
804 *
805 * @returns VBox status code.
806 * @param pszBackend The backend name.
807 * @param pEntries Pointer to an entry.
808 */
809VBOXDDU_DECL(int) VDBackendInfoOne(const char *pszBackend, PVDBACKENDINFO pEntry)
810{
811 LogFlowFunc(("pszBackend=%#p pEntry=%#p\n", pszBackend, pEntry));
812 /* Check arguments. */
813 AssertMsgReturn(VALID_PTR(pszBackend),
814 ("pszBackend=%#p\n", pszBackend),
815 VERR_INVALID_PARAMETER);
816 AssertMsgReturn(VALID_PTR(pEntry),
817 ("pEntry=%#p\n", pEntry),
818 VERR_INVALID_PARAMETER);
819 if (!g_apBackends)
820 VDInit();
821
822 /* Go through loaded backends. */
823 for (unsigned i = 0; i < g_cBackends; i++)
824 {
825 if (!RTStrICmp(pszBackend, g_apBackends[i]->pszBackendName))
826 {
827 pEntry->pszBackend = g_apBackends[i]->pszBackendName;
828 pEntry->uBackendCaps = g_apBackends[i]->uBackendCaps;
829 pEntry->papszFileExtensions = g_apBackends[i]->papszFileExtensions;
830 pEntry->paConfigInfo = g_apBackends[i]->paConfigInfo;
831 return VINF_SUCCESS;
832 }
833 }
834
835 return VERR_NOT_FOUND;
836}
837
838/**
839 * Allocates and initializes an empty HDD container.
840 * No image files are opened.
841 *
842 * @returns VBox status code.
843 * @param pVDIfsDisk Pointer to the per-disk VD interface list.
844 * @param ppDisk Where to store the reference to HDD container.
845 */
846VBOXDDU_DECL(int) VDCreate(PVDINTERFACE pVDIfsDisk, PVBOXHDD *ppDisk)
847{
848 int rc = VINF_SUCCESS;
849 PVBOXHDD pDisk = NULL;
850
851 LogFlowFunc(("pVDIfsDisk=%#p\n", pVDIfsDisk));
852 do
853 {
854 /* Check arguments. */
855 AssertMsgBreakStmt(VALID_PTR(ppDisk),
856 ("ppDisk=%#p\n", ppDisk),
857 rc = VERR_INVALID_PARAMETER);
858
859 pDisk = (PVBOXHDD)RTMemAllocZ(sizeof(VBOXHDD));
860 if (pDisk)
861 {
862 pDisk->u32Signature = VBOXHDDDISK_SIGNATURE;
863 pDisk->cImages = 0;
864 pDisk->pBase = NULL;
865 pDisk->pLast = NULL;
866 pDisk->cbSize = 0;
867 pDisk->PCHSGeometry.cCylinders = 0;
868 pDisk->PCHSGeometry.cHeads = 0;
869 pDisk->PCHSGeometry.cSectors = 0;
870 pDisk->LCHSGeometry.cCylinders = 0;
871 pDisk->LCHSGeometry.cHeads = 0;
872 pDisk->LCHSGeometry.cSectors = 0;
873 pDisk->pVDIfsDisk = pVDIfsDisk;
874 pDisk->pInterfaceError = NULL;
875 pDisk->pInterfaceErrorCallbacks = NULL;
876
877 pDisk->pInterfaceError = VDInterfaceGet(pVDIfsDisk, VDINTERFACETYPE_ERROR);
878 if (pDisk->pInterfaceError)
879 pDisk->pInterfaceErrorCallbacks = VDGetInterfaceError(pDisk->pInterfaceError);
880 *ppDisk = pDisk;
881 }
882 else
883 {
884 rc = VERR_NO_MEMORY;
885 break;
886 }
887 } while (0);
888
889 LogFlowFunc(("returns %Rrc (pDisk=%#p)\n", rc, pDisk));
890 return rc;
891}
892
893/**
894 * Destroys HDD container.
895 * If container has opened image files they will be closed.
896 *
897 * @param pDisk Pointer to HDD container.
898 */
899VBOXDDU_DECL(void) VDDestroy(PVBOXHDD pDisk)
900{
901 LogFlowFunc(("pDisk=%#p\n", pDisk));
902 do
903 {
904 /* sanity check */
905 AssertPtrBreak(pDisk);
906 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
907 VDCloseAll(pDisk);
908 RTMemFree(pDisk);
909 } while (0);
910 LogFlowFunc(("returns\n"));
911}
912
913/**
914 * Try to get the backend name which can use this image.
915 *
916 * @returns VBox status code.
917 * VINF_SUCCESS if a plugin was found.
918 * ppszFormat contains the string which can be used as backend name.
919 * VERR_NOT_SUPPORTED if no backend was found.
920 * @param pszFilename Name of the image file for which the backend is queried.
921 * @param ppszFormat Receives pointer of the UTF-8 string which contains the format name.
922 * The returned pointer must be freed using RTStrFree().
923 */
924VBOXDDU_DECL(int) VDGetFormat(const char *pszFilename, char **ppszFormat)
925{
926 int rc = VERR_NOT_SUPPORTED;
927
928 LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
929 /* Check arguments. */
930 AssertMsgReturn(VALID_PTR(pszFilename) && *pszFilename,
931 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
932 VERR_INVALID_PARAMETER);
933 AssertMsgReturn(VALID_PTR(ppszFormat),
934 ("ppszFormat=%#p\n", ppszFormat),
935 VERR_INVALID_PARAMETER);
936
937 if (!g_apBackends)
938 VDInit();
939
940 /* Find the backend supporting this file format. */
941 for (unsigned i = 0; i < g_cBackends; i++)
942 {
943 if (g_apBackends[i]->pfnCheckIfValid)
944 {
945 rc = g_apBackends[i]->pfnCheckIfValid(pszFilename);
946 if ( RT_SUCCESS(rc)
947 /* The correct backend has been found, but there is a small
948 * incompatibility so that the file cannot be used. Stop here
949 * and signal success - the actual open will of course fail,
950 * but that will create a really sensible error message. */
951 || ( rc != VERR_VD_GEN_INVALID_HEADER
952 && rc != VERR_VD_VDI_INVALID_HEADER
953 && rc != VERR_VD_VMDK_INVALID_HEADER
954 && rc != VERR_VD_ISCSI_INVALID_HEADER
955 && rc != VERR_VD_VHD_INVALID_HEADER
956 && rc != VERR_VD_RAW_INVALID_HEADER))
957 {
958 /* Copy the name into the new string. */
959 char *pszFormat = RTStrDup(g_apBackends[i]->pszBackendName);
960 if (!pszFormat)
961 {
962 rc = VERR_NO_MEMORY;
963 break;
964 }
965 *ppszFormat = pszFormat;
966 rc = VINF_SUCCESS;
967 break;
968 }
969 rc = VERR_NOT_SUPPORTED;
970 }
971 }
972
973 LogFlowFunc(("returns %Rrc *ppszFormat=\"%s\"\n", rc, *ppszFormat));
974 return rc;
975}
976
977/**
978 * Opens an image file.
979 *
980 * The first opened image file in HDD container must have a base image type,
981 * others (next opened images) must be a differencing or undo images.
982 * Linkage is checked for differencing image to be in consistence with the previously opened image.
983 * When another differencing image is opened and the last image was opened in read/write access
984 * mode, then the last image is reopened in read-only with deny write sharing mode. This allows
985 * other processes to use images in read-only mode too.
986 *
987 * Note that the image is opened in read-only mode if a read/write open is not possible.
988 * Use VDIsReadOnly to check open mode.
989 *
990 * @returns VBox status code.
991 * @param pDisk Pointer to HDD container.
992 * @param pszBackend Name of the image file backend to use.
993 * @param pszFilename Name of the image file to open.
994 * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
995 * @param pVDIfsImage Pointer to the per-image VD interface list.
996 */
997VBOXDDU_DECL(int) VDOpen(PVBOXHDD pDisk, const char *pszBackend,
998 const char *pszFilename, unsigned uOpenFlags,
999 PVDINTERFACE pVDIfsImage)
1000{
1001 int rc = VINF_SUCCESS;
1002 PVDIMAGE pImage = NULL;
1003
1004 LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uOpenFlags=%#x, pVDIfsImage=%#p\n",
1005 pDisk, pszBackend, pszFilename, uOpenFlags, pVDIfsImage));
1006 do
1007 {
1008 /* sanity check */
1009 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
1010 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
1011
1012 /* Check arguments. */
1013 AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
1014 ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
1015 rc = VERR_INVALID_PARAMETER);
1016 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
1017 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
1018 rc = VERR_INVALID_PARAMETER);
1019 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
1020 ("uOpenFlags=%#x\n", uOpenFlags),
1021 rc = VERR_INVALID_PARAMETER);
1022
1023 /* Set up image descriptor. */
1024 pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
1025 if (!pImage)
1026 {
1027 rc = VERR_NO_MEMORY;
1028 break;
1029 }
1030 pImage->pszFilename = RTStrDup(pszFilename);
1031 if (!pImage->pszFilename)
1032 {
1033 rc = VERR_NO_MEMORY;
1034 break;
1035 }
1036 pImage->pVDIfsImage = pVDIfsImage;
1037
1038 rc = vdFindBackend(pszBackend, &pImage->Backend);
1039 if (RT_FAILURE(rc))
1040 break;
1041 if (!pImage->Backend)
1042 {
1043 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
1044 N_("VD: unknown backend name '%s'"), pszBackend);
1045 break;
1046 }
1047
1048 pImage->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
1049 rc = pImage->Backend->pfnOpen(pImage->pszFilename,
1050 uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
1051 pDisk->pVDIfsDisk,
1052 pImage->pVDIfsImage,
1053 &pImage->pvBackendData);
1054 /* If the open in read-write mode failed, retry in read-only mode. */
1055 if (RT_FAILURE(rc))
1056 {
1057 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY)
1058 && (rc == VERR_ACCESS_DENIED
1059 || rc == VERR_PERMISSION_DENIED
1060 || rc == VERR_WRITE_PROTECT
1061 || rc == VERR_SHARING_VIOLATION
1062 || rc == VERR_FILE_LOCK_FAILED))
1063 rc = pImage->Backend->pfnOpen(pImage->pszFilename,
1064 (uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME)
1065 | VD_OPEN_FLAGS_READONLY,
1066 pDisk->pVDIfsDisk,
1067 pImage->pVDIfsImage,
1068 &pImage->pvBackendData);
1069 if (RT_FAILURE(rc))
1070 {
1071 rc = vdError(pDisk, rc, RT_SRC_POS,
1072 N_("VD: error opening image file '%s'"), pszFilename);
1073 break;
1074 }
1075 }
1076
1077 unsigned uImageFlags;
1078 uImageFlags = pImage->Backend->pfnGetImageFlags(pImage->pvBackendData);
1079 /* Check image type. As the image itself has only partial knowledge
1080 * whether it's a base image or not, this info is derived here. The
1081 * base image can be fixed or normal, all others must be normal or
1082 * diff images. Some image formats don't distinguish between normal
1083 * and diff images, so this must be corrected here. */
1084 if (RT_FAILURE(rc))
1085 uImageFlags = VD_IMAGE_FLAGS_NONE;
1086 if ( RT_SUCCESS(rc)
1087 && !(uOpenFlags & VD_OPEN_FLAGS_INFO))
1088 {
1089 if ( pDisk->cImages == 0
1090 && (uImageFlags & VD_IMAGE_FLAGS_DIFF))
1091 {
1092 rc = VERR_VD_INVALID_TYPE;
1093 break;
1094 }
1095 else if (pDisk->cImages != 0)
1096 {
1097 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
1098 {
1099 rc = VERR_VD_INVALID_TYPE;
1100 break;
1101 }
1102 else
1103 uImageFlags |= VD_IMAGE_FLAGS_DIFF;
1104 }
1105 }
1106 pImage->uImageFlags = uImageFlags;
1107
1108 /* Force sane optimization settings. It's not worth avoiding writes
1109 * to fixed size images. The overhead would have almost no payback. */
1110 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
1111 pImage->uOpenFlags |= VD_OPEN_FLAGS_HONOR_SAME;
1112
1113 /** @todo optionally check UUIDs */
1114
1115 int rc2;
1116
1117 /* Cache disk information. */
1118 pDisk->cbSize = pImage->Backend->pfnGetSize(pImage->pvBackendData);
1119
1120 /* Cache PCHS geometry. */
1121 rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pvBackendData,
1122 &pDisk->PCHSGeometry);
1123 if (RT_FAILURE(rc2))
1124 {
1125 pDisk->PCHSGeometry.cCylinders = 0;
1126 pDisk->PCHSGeometry.cHeads = 0;
1127 pDisk->PCHSGeometry.cSectors = 0;
1128 }
1129 else
1130 {
1131 /* Make sure the PCHS geometry is properly clipped. */
1132 pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
1133 pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
1134 pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
1135 }
1136
1137 /* Cache LCHS geometry. */
1138 rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pvBackendData,
1139 &pDisk->LCHSGeometry);
1140 if (RT_FAILURE(rc2))
1141 {
1142 pDisk->LCHSGeometry.cCylinders = 0;
1143 pDisk->LCHSGeometry.cHeads = 0;
1144 pDisk->LCHSGeometry.cSectors = 0;
1145 }
1146 else
1147 {
1148 /* Make sure the LCHS geometry is properly clipped. */
1149 pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
1150 pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
1151 }
1152
1153 if (pDisk->cImages != 0)
1154 {
1155 /* Switch previous image to read-only mode. */
1156 unsigned uOpenFlagsPrevImg;
1157 uOpenFlagsPrevImg = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pvBackendData);
1158 if (!(uOpenFlagsPrevImg & VD_OPEN_FLAGS_READONLY))
1159 {
1160 uOpenFlagsPrevImg |= VD_OPEN_FLAGS_READONLY;
1161 rc = pDisk->pLast->Backend->pfnSetOpenFlags(pDisk->pLast->pvBackendData, uOpenFlagsPrevImg);
1162 }
1163 }
1164
1165 if (RT_SUCCESS(rc))
1166 {
1167 /* Image successfully opened, make it the last image. */
1168 vdAddImageToList(pDisk, pImage);
1169 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
1170 pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
1171 }
1172 else
1173 {
1174 /* Error detected, but image opened. Close image. */
1175 int rc2;
1176 rc2 = pImage->Backend->pfnClose(pImage->pvBackendData, false);
1177 AssertRC(rc2);
1178 pImage->pvBackendData = NULL;
1179 }
1180 } while (0);
1181
1182 if (RT_FAILURE(rc))
1183 {
1184 if (pImage)
1185 {
1186 if (pImage->pszFilename)
1187 RTStrFree(pImage->pszFilename);
1188 RTMemFree(pImage);
1189 }
1190 }
1191
1192 LogFlowFunc(("returns %Rrc\n", rc));
1193 return rc;
1194}
1195
1196/**
1197 * Creates and opens a new base image file.
1198 *
1199 * @returns VBox status code.
1200 * @param pDisk Pointer to HDD container.
1201 * @param pszBackend Name of the image file backend to use.
1202 * @param pszFilename Name of the image file to create.
1203 * @param cbSize Image size in bytes.
1204 * @param uImageFlags Flags specifying special image features.
1205 * @param pszComment Pointer to image comment. NULL is ok.
1206 * @param pPCHSGeometry Pointer to physical disk geometry <= (16383,16,63). Not NULL.
1207 * @param pLCHSGeometry Pointer to logical disk geometry <= (x,255,63). Not NULL.
1208 * @param pUuid New UUID of the image. If NULL, a new UUID is created.
1209 * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
1210 * @param pVDIfsImage Pointer to the per-image VD interface list.
1211 * @param pVDIfsOperation Pointer to the per-operation VD interface list.
1212 */
1213VBOXDDU_DECL(int) VDCreateBase(PVBOXHDD pDisk, const char *pszBackend,
1214 const char *pszFilename, uint64_t cbSize,
1215 unsigned uImageFlags, const char *pszComment,
1216 PCPDMMEDIAGEOMETRY pPCHSGeometry,
1217 PCPDMMEDIAGEOMETRY pLCHSGeometry,
1218 PCRTUUID pUuid, unsigned uOpenFlags,
1219 PVDINTERFACE pVDIfsImage,
1220 PVDINTERFACE pVDIfsOperation)
1221{
1222 int rc = VINF_SUCCESS;
1223 PVDIMAGE pImage = NULL;
1224 RTUUID uuid;
1225
1226 LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" PCHS=%u/%u/%u LCHS=%u/%u/%u Uuid=%RTuuid uOpenFlags=%#x pVDIfsImage=%#p pVDIfsOperation=%#p\n",
1227 pDisk, pszBackend, pszFilename, cbSize, uImageFlags, pszComment,
1228 pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
1229 pPCHSGeometry->cSectors, pLCHSGeometry->cCylinders,
1230 pLCHSGeometry->cHeads, pLCHSGeometry->cSectors, pUuid,
1231 uOpenFlags, pVDIfsImage, pVDIfsOperation));
1232
1233 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
1234 VDINTERFACETYPE_PROGRESS);
1235 PVDINTERFACEPROGRESS pCbProgress = NULL;
1236 if (pIfProgress)
1237 pCbProgress = VDGetInterfaceProgress(pIfProgress);
1238
1239 do
1240 {
1241 /* sanity check */
1242 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
1243 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
1244
1245 /* Check arguments. */
1246 AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
1247 ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
1248 rc = VERR_INVALID_PARAMETER);
1249 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
1250 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
1251 rc = VERR_INVALID_PARAMETER);
1252 AssertMsgBreakStmt(cbSize,
1253 ("cbSize=%llu\n", cbSize),
1254 rc = VERR_INVALID_PARAMETER);
1255 AssertMsgBreakStmt( ((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0)
1256 || ((uImageFlags & (VD_IMAGE_FLAGS_FIXED | VD_IMAGE_FLAGS_DIFF)) != VD_IMAGE_FLAGS_FIXED),
1257 ("uImageFlags=%#x\n", uImageFlags),
1258 rc = VERR_INVALID_PARAMETER);
1259 /* The PCHS geometry fields may be 0 to leave it for later. */
1260 AssertMsgBreakStmt( VALID_PTR(pPCHSGeometry)
1261 && pPCHSGeometry->cCylinders <= 16383
1262 && pPCHSGeometry->cHeads <= 16
1263 && pPCHSGeometry->cSectors <= 63,
1264 ("pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pPCHSGeometry,
1265 pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
1266 pPCHSGeometry->cSectors),
1267 rc = VERR_INVALID_PARAMETER);
1268 /* The LCHS geometry fields may be 0 to leave it to later autodetection. */
1269 AssertMsgBreakStmt( VALID_PTR(pLCHSGeometry)
1270 && pLCHSGeometry->cHeads <= 255
1271 && pLCHSGeometry->cSectors <= 63,
1272 ("pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pLCHSGeometry,
1273 pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads,
1274 pLCHSGeometry->cSectors),
1275 rc = VERR_INVALID_PARAMETER);
1276 /* The UUID may be NULL. */
1277 AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
1278 ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
1279 rc = VERR_INVALID_PARAMETER);
1280 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
1281 ("uOpenFlags=%#x\n", uOpenFlags),
1282 rc = VERR_INVALID_PARAMETER);
1283
1284 /* Check state. */
1285 AssertMsgBreakStmt(pDisk->cImages == 0,
1286 ("Create base image cannot be done with other images open\n"),
1287 rc = VERR_VD_INVALID_STATE);
1288
1289 /* Set up image descriptor. */
1290 pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
1291 if (!pImage)
1292 {
1293 rc = VERR_NO_MEMORY;
1294 break;
1295 }
1296 pImage->pszFilename = RTStrDup(pszFilename);
1297 if (!pImage->pszFilename)
1298 {
1299 rc = VERR_NO_MEMORY;
1300 break;
1301 }
1302 pImage->pVDIfsImage = pVDIfsImage;
1303
1304 rc = vdFindBackend(pszBackend, &pImage->Backend);
1305 if (RT_FAILURE(rc))
1306 break;
1307 if (!pImage->Backend)
1308 {
1309 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
1310 N_("VD: unknown backend name '%s'"), pszBackend);
1311 break;
1312 }
1313
1314 /* Create UUID if the caller didn't specify one. */
1315 if (!pUuid)
1316 {
1317 rc = RTUuidCreate(&uuid);
1318 if (RT_FAILURE(rc))
1319 {
1320 rc = vdError(pDisk, rc, RT_SRC_POS,
1321 N_("VD: cannot generate UUID for image '%s'"),
1322 pszFilename);
1323 break;
1324 }
1325 pUuid = &uuid;
1326 }
1327
1328 pImage->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
1329 uImageFlags &= ~VD_IMAGE_FLAGS_DIFF;
1330 rc = pImage->Backend->pfnCreate(pImage->pszFilename, cbSize,
1331 uImageFlags, pszComment, pPCHSGeometry,
1332 pLCHSGeometry, pUuid,
1333 uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
1334 0, 99,
1335 pDisk->pVDIfsDisk,
1336 pImage->pVDIfsImage,
1337 pVDIfsOperation,
1338 &pImage->pvBackendData);
1339
1340 if (RT_SUCCESS(rc))
1341 {
1342 pImage->uImageFlags = uImageFlags;
1343
1344 /* Force sane optimization settings. It's not worth avoiding writes
1345 * to fixed size images. The overhead would have almost no payback. */
1346 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
1347 pImage->uOpenFlags |= VD_OPEN_FLAGS_HONOR_SAME;
1348
1349 /** @todo optionally check UUIDs */
1350
1351 int rc2;
1352
1353 /* Cache disk information. */
1354 pDisk->cbSize = pImage->Backend->pfnGetSize(pImage->pvBackendData);
1355
1356 /* Cache PCHS geometry. */
1357 rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pvBackendData,
1358 &pDisk->PCHSGeometry);
1359 if (RT_FAILURE(rc2))
1360 {
1361 pDisk->PCHSGeometry.cCylinders = 0;
1362 pDisk->PCHSGeometry.cHeads = 0;
1363 pDisk->PCHSGeometry.cSectors = 0;
1364 }
1365 else
1366 {
1367 /* Make sure the CHS geometry is properly clipped. */
1368 pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
1369 pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
1370 pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
1371 }
1372
1373 /* Cache LCHS geometry. */
1374 rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pvBackendData,
1375 &pDisk->LCHSGeometry);
1376 if (RT_FAILURE(rc2))
1377 {
1378 pDisk->LCHSGeometry.cCylinders = 0;
1379 pDisk->LCHSGeometry.cHeads = 0;
1380 pDisk->LCHSGeometry.cSectors = 0;
1381 }
1382 else
1383 {
1384 /* Make sure the CHS geometry is properly clipped. */
1385 pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
1386 pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
1387 }
1388 }
1389
1390 if (RT_SUCCESS(rc))
1391 {
1392 /* Image successfully opened, make it the last image. */
1393 vdAddImageToList(pDisk, pImage);
1394 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
1395 pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
1396 }
1397 else
1398 {
1399 /* Error detected, but image opened. Close and delete image. */
1400 int rc2;
1401 rc2 = pImage->Backend->pfnClose(pImage->pvBackendData, true);
1402 AssertRC(rc2);
1403 pImage->pvBackendData = NULL;
1404 }
1405 } while (0);
1406
1407 if (RT_FAILURE(rc))
1408 {
1409 if (pImage)
1410 {
1411 if (pImage->pszFilename)
1412 RTStrFree(pImage->pszFilename);
1413 RTMemFree(pImage);
1414 }
1415 }
1416
1417 if (RT_SUCCESS(rc) && pCbProgress && pCbProgress->pfnProgress)
1418 pCbProgress->pfnProgress(NULL /* WARNING! pVM=NULL */, 100,
1419 pIfProgress->pvUser);
1420
1421 LogFlowFunc(("returns %Rrc\n", rc));
1422 return rc;
1423}
1424
1425/**
1426 * Creates and opens a new differencing image file in HDD container.
1427 * See comments for VDOpen function about differencing images.
1428 *
1429 * @returns VBox status code.
1430 * @param pDisk Pointer to HDD container.
1431 * @param pszBackend Name of the image file backend to use.
1432 * @param pszFilename Name of the differencing image file to create.
1433 * @param uImageFlags Flags specifying special image features.
1434 * @param pszComment Pointer to image comment. NULL is ok.
1435 * @param pUuid New UUID of the image. If NULL, a new UUID is created.
1436 * @param pParentUuid New parent UUID of the image. If NULL, the UUID is queried automatically.
1437 * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
1438 * @param pVDIfsImage Pointer to the per-image VD interface list.
1439 * @param pVDIfsOperation Pointer to the per-operation VD interface list.
1440 */
1441VBOXDDU_DECL(int) VDCreateDiff(PVBOXHDD pDisk, const char *pszBackend,
1442 const char *pszFilename, unsigned uImageFlags,
1443 const char *pszComment, PCRTUUID pUuid,
1444 PCRTUUID pParentUuid, unsigned uOpenFlags,
1445 PVDINTERFACE pVDIfsImage,
1446 PVDINTERFACE pVDIfsOperation)
1447{
1448 int rc = VINF_SUCCESS;
1449 PVDIMAGE pImage = NULL;
1450 RTUUID uuid;
1451
1452 LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uImageFlags=%#x pszComment=\"%s\" Uuid=%RTuuid ParentUuid=%RTuuid uOpenFlags=%#x pVDIfsImage=%#p pVDIfsOperation=%#p\n",
1453 pDisk, pszBackend, pszFilename, uImageFlags, pszComment, pUuid, pParentUuid, uOpenFlags,
1454 pVDIfsImage, pVDIfsOperation));
1455
1456 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
1457 VDINTERFACETYPE_PROGRESS);
1458 PVDINTERFACEPROGRESS pCbProgress = NULL;
1459 if (pIfProgress)
1460 pCbProgress = VDGetInterfaceProgress(pIfProgress);
1461
1462 do
1463 {
1464 /* sanity check */
1465 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
1466 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
1467
1468 /* Check arguments. */
1469 AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
1470 ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
1471 rc = VERR_INVALID_PARAMETER);
1472 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
1473 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
1474 rc = VERR_INVALID_PARAMETER);
1475 AssertMsgBreakStmt((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0,
1476 ("uImageFlags=%#x\n", uImageFlags),
1477 rc = VERR_INVALID_PARAMETER);
1478 /* The UUID may be NULL. */
1479 AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
1480 ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
1481 rc = VERR_INVALID_PARAMETER);
1482 /* The parent UUID may be NULL. */
1483 AssertMsgBreakStmt(pParentUuid == NULL || VALID_PTR(pParentUuid),
1484 ("pParentUuid=%#p ParentUUID=%RTuuid\n", pParentUuid, pParentUuid),
1485 rc = VERR_INVALID_PARAMETER);
1486 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
1487 ("uOpenFlags=%#x\n", uOpenFlags),
1488 rc = VERR_INVALID_PARAMETER);
1489
1490 /* Check state. */
1491 AssertMsgBreakStmt(pDisk->cImages != 0,
1492 ("Create diff image cannot be done without other images open\n"),
1493 rc = VERR_VD_INVALID_STATE);
1494
1495 /* Set up image descriptor. */
1496 pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
1497 if (!pImage)
1498 {
1499 rc = VERR_NO_MEMORY;
1500 break;
1501 }
1502 pImage->pszFilename = RTStrDup(pszFilename);
1503 if (!pImage->pszFilename)
1504 {
1505 rc = VERR_NO_MEMORY;
1506 break;
1507 }
1508
1509 rc = vdFindBackend(pszBackend, &pImage->Backend);
1510 if (RT_FAILURE(rc))
1511 break;
1512 if (!pImage->Backend)
1513 {
1514 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
1515 N_("VD: unknown backend name '%s'"), pszBackend);
1516 break;
1517 }
1518
1519 /* Create UUID if the caller didn't specify one. */
1520 if (!pUuid)
1521 {
1522 rc = RTUuidCreate(&uuid);
1523 if (RT_FAILURE(rc))
1524 {
1525 rc = vdError(pDisk, rc, RT_SRC_POS,
1526 N_("VD: cannot generate UUID for image '%s'"),
1527 pszFilename);
1528 break;
1529 }
1530 pUuid = &uuid;
1531 }
1532
1533 pImage->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
1534 rc = pImage->Backend->pfnCreate(pImage->pszFilename, pDisk->cbSize,
1535 uImageFlags, pszComment,
1536 &pDisk->PCHSGeometry,
1537 &pDisk->LCHSGeometry, pUuid,
1538 uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
1539 0, 99,
1540 pDisk->pVDIfsDisk,
1541 pImage->pVDIfsImage,
1542 pVDIfsOperation,
1543 &pImage->pvBackendData);
1544
1545 if (RT_SUCCESS(rc) && pDisk->cImages != 0)
1546 {
1547 pImage->uImageFlags |= VD_IMAGE_FLAGS_DIFF;
1548
1549 /* Switch previous image to read-only mode. */
1550 unsigned uOpenFlagsPrevImg;
1551 uOpenFlagsPrevImg = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pvBackendData);
1552 if (!(uOpenFlagsPrevImg & VD_OPEN_FLAGS_READONLY))
1553 {
1554 uOpenFlagsPrevImg |= VD_OPEN_FLAGS_READONLY;
1555 rc = pDisk->pLast->Backend->pfnSetOpenFlags(pDisk->pLast->pvBackendData, uOpenFlagsPrevImg);
1556 }
1557 }
1558
1559 if (RT_SUCCESS(rc))
1560 {
1561 RTUUID Uuid;
1562 RTTIMESPEC ts;
1563 int rc2;
1564
1565 if (pParentUuid && !RTUuidIsNull(pParentUuid))
1566 {
1567 Uuid = *pParentUuid;
1568 pImage->Backend->pfnSetParentUuid(pImage->pvBackendData, &Uuid);
1569 }
1570 else
1571 {
1572 rc2 = pDisk->pLast->Backend->pfnGetUuid(pDisk->pLast->pvBackendData,
1573 &Uuid);
1574 if (RT_SUCCESS(rc2))
1575 pImage->Backend->pfnSetParentUuid(pImage->pvBackendData, &Uuid);
1576 }
1577 rc2 = pDisk->pLast->Backend->pfnGetModificationUuid(pDisk->pLast->pvBackendData,
1578 &Uuid);
1579 if (RT_SUCCESS(rc2))
1580 pImage->Backend->pfnSetParentModificationUuid(pImage->pvBackendData,
1581 &Uuid);
1582 rc2 = pDisk->pLast->Backend->pfnGetTimeStamp(pDisk->pLast->pvBackendData,
1583 &ts);
1584 if (RT_SUCCESS(rc2))
1585 pImage->Backend->pfnSetParentTimeStamp(pImage->pvBackendData, &ts);
1586
1587 rc2 = pImage->Backend->pfnSetParentFilename(pImage->pvBackendData, pDisk->pLast->pszFilename);
1588 }
1589
1590 if (RT_SUCCESS(rc))
1591 {
1592 /** @todo optionally check UUIDs */
1593 }
1594
1595 if (RT_SUCCESS(rc))
1596 {
1597 /* Image successfully opened, make it the last image. */
1598 vdAddImageToList(pDisk, pImage);
1599 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
1600 pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
1601 }
1602 else
1603 {
1604 /* Error detected, but image opened. Close and delete image. */
1605 int rc2;
1606 rc2 = pImage->Backend->pfnClose(pImage->pvBackendData, true);
1607 AssertRC(rc2);
1608 pImage->pvBackendData = NULL;
1609 }
1610 } while (0);
1611
1612 if (RT_FAILURE(rc))
1613 {
1614 if (pImage)
1615 {
1616 if (pImage->pszFilename)
1617 RTStrFree(pImage->pszFilename);
1618 RTMemFree(pImage);
1619 }
1620 }
1621
1622 if (RT_SUCCESS(rc) && pCbProgress && pCbProgress->pfnProgress)
1623 pCbProgress->pfnProgress(NULL /* WARNING! pVM=NULL */, 100,
1624 pIfProgress->pvUser);
1625
1626 LogFlowFunc(("returns %Rrc\n", rc));
1627 return rc;
1628}
1629
1630/**
1631 * Merges two images (not necessarily with direct parent/child relationship).
1632 * As a side effect the source image and potentially the other images which
1633 * are also merged to the destination are deleted from both the disk and the
1634 * images in the HDD container.
1635 *
1636 * @returns VBox status code.
1637 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
1638 * @param pDisk Pointer to HDD container.
1639 * @param nImageFrom Name of the image file to merge from.
1640 * @param nImageTo Name of the image file to merge to.
1641 * @param pVDIfsOperation Pointer to the per-operation VD interface list.
1642 */
1643VBOXDDU_DECL(int) VDMerge(PVBOXHDD pDisk, unsigned nImageFrom,
1644 unsigned nImageTo, PVDINTERFACE pVDIfsOperation)
1645{
1646 int rc = VINF_SUCCESS;
1647 void *pvBuf = NULL;
1648
1649 LogFlowFunc(("pDisk=%#p nImageFrom=%u nImageTo=%u pVDIfsOperation=%#p\n",
1650 pDisk, nImageFrom, nImageTo, pVDIfsOperation));
1651
1652 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
1653 VDINTERFACETYPE_PROGRESS);
1654 PVDINTERFACEPROGRESS pCbProgress = NULL;
1655 if (pIfProgress)
1656 pCbProgress = VDGetInterfaceProgress(pIfProgress);
1657
1658 do
1659 {
1660 /* sanity check */
1661 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
1662 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
1663
1664 PVDIMAGE pImageFrom = vdGetImageByNumber(pDisk, nImageFrom);
1665 PVDIMAGE pImageTo = vdGetImageByNumber(pDisk, nImageTo);
1666 if (!pImageFrom || !pImageTo)
1667 {
1668 rc = VERR_VD_IMAGE_NOT_FOUND;
1669 break;
1670 }
1671 AssertBreakStmt(pImageFrom != pImageTo, rc = VERR_INVALID_PARAMETER);
1672
1673 /* Make sure destination image is writable. */
1674 unsigned uOpenFlags = pImageTo->Backend->pfnGetOpenFlags(pImageTo->pvBackendData);
1675 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
1676 {
1677 uOpenFlags &= ~VD_OPEN_FLAGS_READONLY;
1678 rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pvBackendData,
1679 uOpenFlags);
1680 if (RT_FAILURE(rc))
1681 break;
1682 }
1683
1684 /* Get size of destination image. */
1685 uint64_t cbSize = pImageTo->Backend->pfnGetSize(pImageTo->pvBackendData);
1686
1687 /* Allocate tmp buffer. */
1688 pvBuf = RTMemTmpAlloc(VD_MERGE_BUFFER_SIZE);
1689 if (!pvBuf)
1690 {
1691 rc = VERR_NO_MEMORY;
1692 break;
1693 }
1694
1695 /* Merging is done directly on the images itself. This potentially
1696 * causes trouble if the disk is full in the middle of operation. */
1697 /** @todo write alternative implementation which works with temporary
1698 * images (which is safer, but requires even more space). Also has the
1699 * drawback that merging into a raw disk parent simply isn't possible
1700 * this way (but in that case disk full isn't really a problem). */
1701 if (nImageFrom < nImageTo)
1702 {
1703 /* Merge parent state into child. This means writing all not
1704 * allocated blocks in the destination image which are allocated in
1705 * the images to be merged. */
1706 uint64_t uOffset = 0;
1707 uint64_t cbRemaining = cbSize;
1708 do
1709 {
1710 size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
1711 rc = pImageTo->Backend->pfnRead(pImageTo->pvBackendData,
1712 uOffset, pvBuf, cbThisRead,
1713 &cbThisRead);
1714 if (rc == VERR_VD_BLOCK_FREE)
1715 {
1716 /* Search for image with allocated block. Do not attempt to
1717 * read more than the previous reads marked as valid.
1718 * Otherwise this would return stale data when different
1719 * block sizes are used for the images. */
1720 for (PVDIMAGE pCurrImage = pImageTo->pPrev;
1721 pCurrImage != NULL && pCurrImage != pImageFrom->pPrev && rc == VERR_VD_BLOCK_FREE;
1722 pCurrImage = pCurrImage->pPrev)
1723 {
1724 rc = pCurrImage->Backend->pfnRead(pCurrImage->pvBackendData,
1725 uOffset, pvBuf,
1726 cbThisRead,
1727 &cbThisRead);
1728 }
1729
1730 if (rc != VERR_VD_BLOCK_FREE)
1731 {
1732 if (RT_FAILURE(rc))
1733 break;
1734 rc = vdWriteHelper(pDisk, pImageTo, uOffset, pvBuf,
1735 cbThisRead);
1736 if (RT_FAILURE(rc))
1737 break;
1738 }
1739 else
1740 rc = VINF_SUCCESS;
1741 }
1742 else if (RT_FAILURE(rc))
1743 break;
1744
1745 uOffset += cbThisRead;
1746 cbRemaining -= cbThisRead;
1747
1748 if (pCbProgress && pCbProgress->pfnProgress)
1749 {
1750 rc = pCbProgress->pfnProgress(NULL /* WARNING! pVM=NULL */,
1751 uOffset * 99 / cbSize,
1752 pIfProgress->pvUser);
1753 if (RT_FAILURE(rc))
1754 break;
1755 }
1756 } while (uOffset < cbSize);
1757 }
1758 else
1759 {
1760 /* Merge child state into parent. This means writing all blocks
1761 * which are allocated in the image up to the source image to the
1762 * destination image. */
1763 uint64_t uOffset = 0;
1764 uint64_t cbRemaining = cbSize;
1765 do
1766 {
1767 size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
1768 rc = VERR_VD_BLOCK_FREE;
1769 /* Search for image with allocated block. Do not attempt to
1770 * read more than the previous reads marked as valid. Otherwise
1771 * this would return stale data when different block sizes are
1772 * used for the images. */
1773 for (PVDIMAGE pCurrImage = pImageFrom;
1774 pCurrImage != NULL && pCurrImage != pImageTo && rc == VERR_VD_BLOCK_FREE;
1775 pCurrImage = pCurrImage->pPrev)
1776 {
1777 rc = pCurrImage->Backend->pfnRead(pCurrImage->pvBackendData,
1778 uOffset, pvBuf,
1779 cbThisRead, &cbThisRead);
1780 }
1781
1782 if (rc != VERR_VD_BLOCK_FREE)
1783 {
1784 if (RT_FAILURE(rc))
1785 break;
1786 rc = vdWriteHelper(pDisk, pImageTo, uOffset, pvBuf,
1787 cbThisRead);
1788 if (RT_FAILURE(rc))
1789 break;
1790 }
1791 else
1792 rc = VINF_SUCCESS;
1793
1794 uOffset += cbThisRead;
1795 cbRemaining -= cbThisRead;
1796
1797 if (pCbProgress && pCbProgress->pfnProgress)
1798 {
1799 rc = pCbProgress->pfnProgress(NULL /* WARNING! pVM=NULL */,
1800 uOffset * 99 / cbSize,
1801 pIfProgress->pvUser);
1802 if (RT_FAILURE(rc))
1803 break;
1804 }
1805 } while (uOffset < cbSize);
1806 }
1807
1808 /* Update parent UUID so that image chain is consistent. */
1809 RTUUID Uuid;
1810 if (nImageFrom < nImageTo)
1811 {
1812 if (pImageTo->pPrev)
1813 {
1814 rc = pImageTo->Backend->pfnGetUuid(pImageTo->pPrev->pvBackendData,
1815 &Uuid);
1816 AssertRC(rc);
1817 }
1818 else
1819 RTUuidClear(&Uuid);
1820 rc = pImageTo->Backend->pfnSetParentUuid(pImageTo->pvBackendData,
1821 &Uuid);
1822 AssertRC(rc);
1823 }
1824 else
1825 {
1826 if (pImageFrom->pNext)
1827 {
1828 rc = pImageTo->Backend->pfnGetUuid(pImageTo->pvBackendData,
1829 &Uuid);
1830 AssertRC(rc);
1831 rc = pImageFrom->Backend->pfnSetParentUuid(pImageFrom->pNext,
1832 &Uuid);
1833 AssertRC(rc);
1834 }
1835 }
1836
1837 /* Make sure destination image is back to read only if necessary. */
1838 if (pImageTo != pDisk->pLast && pImageFrom != pDisk->pLast)
1839 {
1840 uOpenFlags = pImageTo->Backend->pfnGetOpenFlags(pImageTo->pvBackendData);
1841 uOpenFlags |= VD_OPEN_FLAGS_READONLY;
1842 rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pvBackendData,
1843 uOpenFlags);
1844 if (RT_FAILURE(rc))
1845 break;
1846 }
1847
1848 /* Delete the no longer needed images. */
1849 PVDIMAGE pImg = pImageFrom, pTmp;
1850 while (pImg != pImageTo)
1851 {
1852 if (nImageFrom < nImageTo)
1853 pTmp = pImg->pNext;
1854 else
1855 pTmp = pImg->pPrev;
1856 vdRemoveImageFromList(pDisk, pImg);
1857 pImg->Backend->pfnClose(pImg->pvBackendData, true);
1858 RTMemFree(pImg->pszFilename);
1859 RTMemFree(pImg);
1860 pImg = pTmp;
1861 }
1862 } while (0);
1863
1864 if (pvBuf)
1865 RTMemTmpFree(pvBuf);
1866
1867 if (RT_SUCCESS(rc) && pCbProgress && pCbProgress->pfnProgress)
1868 pCbProgress->pfnProgress(NULL /* WARNING! pVM=NULL */, 100,
1869 pIfProgress->pvUser);
1870
1871 LogFlowFunc(("returns %Rrc\n", rc));
1872 return rc;
1873}
1874
1875/**
1876 * Copies an image from one HDD container to another.
1877 * The copy is opened in the target HDD container.
1878 * It is possible to convert between different image formats, because the
1879 * backend for the destination may be different from the source.
1880 * If both the source and destination reference the same HDD container,
1881 * then the image is moved (by copying/deleting or renaming) to the new location.
1882 * The source container is unchanged if the move operation fails, otherwise
1883 * the image at the new location is opened in the same way as the old one was.
1884 *
1885 * @returns VBox status code.
1886 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
1887 * @param pDiskFrom Pointer to source HDD container.
1888 * @param nImage Image number, counts from 0. 0 is always base image of container.
1889 * @param pDiskTo Pointer to destination HDD container.
1890 * @param pszBackend Name of the image file backend to use.
1891 * @param pszFilename New name of the image (may be NULL if pDiskFrom == pDiskTo).
1892 * @param fMoveByRename If true, attempt to perform a move by renaming (if successful the new size is ignored).
1893 * @param cbSize New image size (0 means leave unchanged).
1894 * @param uImageFlags Flags specifying special destination image features.
1895 * @param pDstUuid New UUID of the destination image. If NULL, a new UUID is created.
1896 * This parameter is used if and only if a true copy is created.
1897 * In all rename/move cases the UUIDs are copied over.
1898 * @param pVDIfsOperation Pointer to the per-operation VD interface list.
1899 * @param pDstVDIfsImage Pointer to the per-image VD interface list, for the
1900 * destination image.
1901 * @param pDstVDIfsOperation Pointer to the per-image VD interface list,
1902 * for the destination image.
1903 */
1904VBOXDDU_DECL(int) VDCopy(PVBOXHDD pDiskFrom, unsigned nImage, PVBOXHDD pDiskTo,
1905 const char *pszBackend, const char *pszFilename,
1906 bool fMoveByRename, uint64_t cbSize,
1907 unsigned uImageFlags, PCRTUUID pDstUuid,
1908 PVDINTERFACE pVDIfsOperation,
1909 PVDINTERFACE pDstVDIfsImage,
1910 PVDINTERFACE pDstVDIfsOperation)
1911{
1912 int rc, rc2 = VINF_SUCCESS;
1913 void *pvBuf = NULL;
1914 PVDIMAGE pImageTo = NULL;
1915
1916 LogFlowFunc(("pDiskFrom=%#p nImage=%u pDiskTo=%#p pszBackend=\"%s\" pszFilename=\"%s\" fMoveByRename=%d cbSize=%llu pVDIfsOperation=%#p pDstVDIfsImage=%#p pDstVDIfsOperation=%#p\n",
1917 pDiskFrom, nImage, pDiskTo, pszBackend, pszFilename, fMoveByRename, cbSize, pVDIfsOperation, pDstVDIfsImage, pDstVDIfsOperation));
1918
1919 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
1920 VDINTERFACETYPE_PROGRESS);
1921 PVDINTERFACEPROGRESS pCbProgress = NULL;
1922 if (pIfProgress)
1923 pCbProgress = VDGetInterfaceProgress(pIfProgress);
1924
1925 PVDINTERFACE pDstIfProgress = VDInterfaceGet(pDstVDIfsOperation,
1926 VDINTERFACETYPE_PROGRESS);
1927 PVDINTERFACEPROGRESS pDstCbProgress = NULL;
1928 if (pDstIfProgress)
1929 pDstCbProgress = VDGetInterfaceProgress(pDstIfProgress);
1930
1931 do {
1932 /* Check arguments. */
1933 AssertMsgBreakStmt(VALID_PTR(pDiskFrom), ("pDiskFrom=%#p\n", pDiskFrom),
1934 rc = VERR_INVALID_PARAMETER);
1935 AssertMsg(pDiskFrom->u32Signature == VBOXHDDDISK_SIGNATURE,
1936 ("u32Signature=%08x\n", pDiskFrom->u32Signature));
1937
1938 PVDIMAGE pImageFrom = vdGetImageByNumber(pDiskFrom, nImage);
1939 AssertPtrBreakStmt(pImageFrom, rc = VERR_VD_IMAGE_NOT_FOUND);
1940 AssertMsgBreakStmt(VALID_PTR(pDiskTo), ("pDiskTo=%#p\n", pDiskTo),
1941 rc = VERR_INVALID_PARAMETER);
1942 AssertMsg(pDiskTo->u32Signature == VBOXHDDDISK_SIGNATURE,
1943 ("u32Signature=%08x\n", pDiskTo->u32Signature));
1944
1945 /* Move the image. */
1946 if (pDiskFrom == pDiskTo)
1947 {
1948 /* Rename only works when backends are the same. */
1949 if ( fMoveByRename
1950 && !RTStrICmp(pszBackend, pImageFrom->Backend->pszBackendName))
1951 {
1952 rc = pImageFrom->Backend->pfnRename(pImageFrom->pvBackendData, pszFilename ? pszFilename : pImageFrom->pszFilename);
1953 break;
1954 }
1955
1956 /** @todo Moving (including shrinking/growing) of the image is
1957 * requested, but the rename attempt failed or it wasn't possible.
1958 * Must now copy image to temp location. */
1959 AssertReleaseMsgFailed(("VDCopy: moving by copy/delete not implemented\n"));
1960 }
1961
1962 /* When moving an image pszFilename is allowed to be NULL, so do the parameter check here. */
1963 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
1964 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
1965 rc = VERR_INVALID_PARAMETER);
1966
1967 uint64_t cbSizeFrom;
1968 cbSizeFrom = pImageFrom->Backend->pfnGetSize(pImageFrom->pvBackendData);
1969 if (cbSizeFrom == 0)
1970 {
1971 rc = VERR_VD_VALUE_NOT_FOUND;
1972 break;
1973 }
1974
1975 if (cbSize == 0)
1976 cbSize = cbSizeFrom;
1977
1978 PDMMEDIAGEOMETRY PCHSGeometryFrom = {0, 0, 0};
1979 PDMMEDIAGEOMETRY LCHSGeometryFrom = {0, 0, 0};
1980 pImageFrom->Backend->pfnGetPCHSGeometry(pImageFrom->pvBackendData, &PCHSGeometryFrom);
1981 pImageFrom->Backend->pfnGetLCHSGeometry(pImageFrom->pvBackendData, &LCHSGeometryFrom);
1982
1983 RTUUID ImageUuid, ImageModificationUuid;
1984 RTUUID ParentUuid, ParentModificationUuid;
1985 if (pDiskFrom != pDiskTo)
1986 {
1987 if (pDstUuid)
1988 ImageUuid = *pDstUuid;
1989 else
1990 RTUuidCreate(&ImageUuid);
1991 }
1992 else
1993 {
1994 rc = pImageFrom->Backend->pfnGetUuid(pImageFrom->pvBackendData, &ImageUuid);
1995 if (RT_FAILURE(rc))
1996 RTUuidCreate(&ImageUuid);
1997 }
1998 rc = pImageFrom->Backend->pfnGetModificationUuid(pImageFrom->pvBackendData, &ImageModificationUuid);
1999 if (RT_FAILURE(rc))
2000 RTUuidClear(&ImageModificationUuid);
2001 rc = pImageFrom->Backend->pfnGetParentUuid(pImageFrom->pvBackendData, &ParentUuid);
2002 if (RT_FAILURE(rc))
2003 RTUuidClear(&ParentUuid);
2004 rc = pImageFrom->Backend->pfnGetParentModificationUuid(pImageFrom->pvBackendData, &ParentModificationUuid);
2005 if (RT_FAILURE(rc))
2006 RTUuidClear(&ParentModificationUuid);
2007
2008 char szComment[1024];
2009 rc = pImageFrom->Backend->pfnGetComment(pImageFrom->pvBackendData, szComment, sizeof(szComment));
2010 if (RT_FAILURE(rc))
2011 szComment[0] = '\0';
2012 else
2013 szComment[sizeof(szComment) - 1] = '\0';
2014
2015 unsigned uOpenFlagsFrom;
2016 uOpenFlagsFrom = pImageFrom->Backend->pfnGetOpenFlags(pImageFrom->pvBackendData);
2017
2018 /* Create destination image with the properties of the source image. */
2019 /** @todo replace the VDCreateDiff/VDCreateBase calls by direct
2020 * calls to the backend. Unifies the code and reduces the API
2021 * dependencies. */
2022 if (uImageFlags & VD_IMAGE_FLAGS_DIFF)
2023 {
2024 rc = VDCreateDiff(pDiskTo, pszBackend, pszFilename, uImageFlags,
2025 szComment, &ImageUuid, &ParentUuid, uOpenFlagsFrom & ~VD_OPEN_FLAGS_READONLY, NULL, NULL);
2026 } else {
2027 /** @todo Please, review this! It's an ugly hack I think... */
2028 if (!RTStrICmp(pszBackend, "RAW"))
2029 uImageFlags |= VD_IMAGE_FLAGS_FIXED;
2030
2031 rc = VDCreateBase(pDiskTo, pszBackend, pszFilename, cbSize,
2032 uImageFlags, szComment,
2033 &PCHSGeometryFrom, &LCHSGeometryFrom,
2034 NULL, uOpenFlagsFrom & ~VD_OPEN_FLAGS_READONLY, NULL, NULL);
2035 if (RT_SUCCESS(rc) && !RTUuidIsNull(&ImageUuid))
2036 pDiskTo->pLast->Backend->pfnSetUuid(pDiskTo->pLast->pvBackendData, &ImageUuid);
2037 if (RT_SUCCESS(rc) && !RTUuidIsNull(&ParentUuid))
2038 pDiskTo->pLast->Backend->pfnSetParentUuid(pDiskTo->pLast->pvBackendData, &ParentUuid);
2039 }
2040 if (RT_FAILURE(rc))
2041 break;
2042
2043 pImageTo = pDiskTo->pLast;
2044 AssertPtrBreakStmt(pImageTo, rc = VERR_VD_IMAGE_NOT_FOUND);
2045
2046 /* Allocate tmp buffer. */
2047 pvBuf = RTMemTmpAlloc(VD_MERGE_BUFFER_SIZE);
2048 if (!pvBuf)
2049 {
2050 rc = VERR_NO_MEMORY;
2051 break;
2052 }
2053
2054 /* Copy the data. */
2055 uint64_t uOffset = 0;
2056 uint64_t cbRemaining = cbSize;
2057
2058 do
2059 {
2060 size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
2061
2062 rc = vdReadHelper(pDiskFrom, pImageFrom, uOffset, pvBuf,
2063 cbThisRead);
2064 if (RT_FAILURE(rc))
2065 break;
2066
2067 rc = vdWriteHelper(pDiskTo, pImageTo, uOffset, pvBuf,
2068 cbThisRead);
2069 if (RT_FAILURE(rc))
2070 break;
2071
2072 uOffset += cbThisRead;
2073 cbRemaining -= cbThisRead;
2074
2075 if (pCbProgress && pCbProgress->pfnProgress)
2076 {
2077 rc = pCbProgress->pfnProgress(NULL /* WARNING! pVM=NULL */,
2078 uOffset * 99 / cbSize,
2079 pIfProgress->pvUser);
2080 if (RT_FAILURE(rc))
2081 break;
2082 }
2083 if (pDstCbProgress && pDstCbProgress->pfnProgress)
2084 {
2085 rc = pDstCbProgress->pfnProgress(NULL /* WARNING! pVM=NULL */,
2086 uOffset * 99 / cbSize,
2087 pDstIfProgress->pvUser);
2088 if (RT_FAILURE(rc))
2089 break;
2090 }
2091 } while (uOffset < cbSize);
2092
2093 if (RT_SUCCESS(rc))
2094 {
2095 pImageTo->Backend->pfnSetModificationUuid(pImageTo->pvBackendData, &ImageModificationUuid);
2096 pImageTo->Backend->pfnGetParentModificationUuid(pImageTo->pvBackendData, &ParentModificationUuid);
2097 }
2098 } while (0);
2099
2100 if (RT_FAILURE(rc) && pImageTo)
2101 {
2102 /* Error detected, but new image created. Remove image from list. */
2103 vdRemoveImageFromList(pDiskTo, pImageTo);
2104
2105 /* Close and delete image. */
2106 rc2 = pImageTo->Backend->pfnClose(pImageTo->pvBackendData, true);
2107 AssertRC(rc2);
2108 pImageTo->pvBackendData = NULL;
2109
2110 /* Free remaining resources. */
2111 if (pImageTo->pszFilename)
2112 RTStrFree(pImageTo->pszFilename);
2113
2114 RTMemFree(pImageTo);
2115 }
2116
2117 if (pvBuf)
2118 RTMemTmpFree(pvBuf);
2119
2120 if (RT_SUCCESS(rc))
2121 {
2122 if (pCbProgress && pCbProgress->pfnProgress)
2123 pCbProgress->pfnProgress(NULL /* WARNING! pVM=NULL */, 100,
2124 pIfProgress->pvUser);
2125 if (pDstCbProgress && pDstCbProgress->pfnProgress)
2126 pDstCbProgress->pfnProgress(NULL /* WARNING! pVM=NULL */, 100,
2127 pDstIfProgress->pvUser);
2128 }
2129
2130 LogFlowFunc(("returns %Rrc\n", rc));
2131 return rc;
2132}
2133
2134/**
2135 * Optimizes the storage consumption of an image. Typically the unused blocks
2136 * have to be wiped with zeroes to achieve a substantial reduced storage use.
2137 * Another optimization done is reordering the image blocks, which can provide
2138 * a significant performance boost, as reads and writes tend to use less random
2139 * file offsets.
2140 *
2141 * @return VBox status code.
2142 * @return VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
2143 * @return VERR_VD_IMAGE_READ_ONLY if image is not writable.
2144 * @return VERR_NOT_SUPPORTED if this kind of image can be compacted, but
2145 * the code for this isn't implemented yet.
2146 * @param pDisk Pointer to HDD container.
2147 * @param nImage Image number, counts from 0. 0 is always base image of container.
2148 * @param pVDIfsOperation Pointer to the per-operation VD interface list.
2149 */
2150VBOXDDU_DECL(int) VDCompact(PVBOXHDD pDisk, unsigned nImage,
2151 PVDINTERFACE pVDIfsOperation)
2152{
2153 int rc;
2154 void *pvBuf = NULL;
2155 void *pvTmp = NULL;
2156
2157 LogFlowFunc(("pDisk=%#p nImage=%u pVDIfsOperation=%#p\n",
2158 pDisk, nImage, pVDIfsOperation));
2159
2160 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
2161 VDINTERFACETYPE_PROGRESS);
2162 PVDINTERFACEPROGRESS pCbProgress = NULL;
2163 if (pIfProgress)
2164 pCbProgress = VDGetInterfaceProgress(pIfProgress);
2165
2166 do {
2167 /* Check arguments. */
2168 AssertMsgBreakStmt(VALID_PTR(pDisk), ("pDisk=%#p\n", pDisk),
2169 rc = VERR_INVALID_PARAMETER);
2170 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE,
2171 ("u32Signature=%08x\n", pDisk->u32Signature));
2172
2173 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
2174 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
2175
2176 /* If there is no compact callback for not file based backends then
2177 * the backend doesn't need compaction. No need to make much fuss about
2178 * this. For file based ones signal this as not yet supported. */
2179 if (!pImage->Backend->pfnCompact)
2180 {
2181 if (pImage->Backend->uBackendCaps & VD_CAP_FILE)
2182 rc = VERR_NOT_SUPPORTED;
2183 else
2184 rc = VINF_SUCCESS;
2185 break;
2186 }
2187
2188 /* Insert interface for reading parent state into per-operation list,
2189 * if there is a parent image. */
2190 VDINTERFACE IfOpParent;
2191 VDINTERFACEPARENTSTATE ParentCb;
2192 VDPARENTSTATEDESC ParentUser;
2193 if (pImage->pPrev)
2194 {
2195 ParentCb.cbSize = sizeof(ParentCb);
2196 ParentCb.enmInterface = VDINTERFACETYPE_PARENTSTATE;
2197 ParentCb.pfnParentRead = vdParentRead;
2198 ParentUser.pDisk = pDisk;
2199 ParentUser.pImage = pImage->pPrev;
2200 rc = VDInterfaceAdd(&IfOpParent, "VDCompact_ParentState", VDINTERFACETYPE_PARENTSTATE,
2201 &ParentCb, &ParentUser, &pVDIfsOperation);
2202 AssertRC(rc);
2203 }
2204
2205 rc = pImage->Backend->pfnCompact(pImage->pvBackendData,
2206 0, 99,
2207 pVDIfsOperation);
2208 } while (0);
2209
2210 if (pvBuf)
2211 RTMemTmpFree(pvBuf);
2212 if (pvTmp)
2213 RTMemTmpFree(pvTmp);
2214
2215 if (RT_SUCCESS(rc))
2216 {
2217 if (pCbProgress && pCbProgress->pfnProgress)
2218 pCbProgress->pfnProgress(NULL /* WARNING! pVM=NULL */, 100,
2219 pIfProgress->pvUser);
2220 }
2221
2222 LogFlowFunc(("returns %Rrc\n", rc));
2223 return rc;
2224}
2225
2226/**
2227 * Closes the last opened image file in HDD container.
2228 * If previous image file was opened in read-only mode (that is normal) and closing image
2229 * was opened in read-write mode (the whole disk was in read-write mode) - the previous image
2230 * will be reopened in read/write mode.
2231 *
2232 * @returns VBox status code.
2233 * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
2234 * @param pDisk Pointer to HDD container.
2235 * @param fDelete If true, delete the image from the host disk.
2236 */
2237VBOXDDU_DECL(int) VDClose(PVBOXHDD pDisk, bool fDelete)
2238{
2239 int rc = VINF_SUCCESS;
2240
2241 LogFlowFunc(("pDisk=%#p fDelete=%d\n", pDisk, fDelete));
2242 do
2243 {
2244 /* sanity check */
2245 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2246 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2247
2248 PVDIMAGE pImage = pDisk->pLast;
2249 if (!pImage)
2250 {
2251 rc = VERR_VD_NOT_OPENED;
2252 break;
2253 }
2254 unsigned uOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pvBackendData);
2255 /* Remove image from list of opened images. */
2256 vdRemoveImageFromList(pDisk, pImage);
2257 /* Close (and optionally delete) image. */
2258 rc = pImage->Backend->pfnClose(pImage->pvBackendData, fDelete);
2259 /* Free remaining resources related to the image. */
2260 RTStrFree(pImage->pszFilename);
2261 RTMemFree(pImage);
2262
2263 pImage = pDisk->pLast;
2264 if (!pImage)
2265 break;
2266
2267 /* If disk was previously in read/write mode, make sure it will stay
2268 * like this (if possible) after closing this image. Set the open flags
2269 * accordingly. */
2270 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
2271 {
2272 uOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pvBackendData);
2273 uOpenFlags &= ~ VD_OPEN_FLAGS_READONLY;
2274 rc = pImage->Backend->pfnSetOpenFlags(pImage->pvBackendData, uOpenFlags);
2275 }
2276
2277 int rc2;
2278
2279 /* Cache disk information. */
2280 pDisk->cbSize = pImage->Backend->pfnGetSize(pImage->pvBackendData);
2281
2282 /* Cache PCHS geometry. */
2283 rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pvBackendData,
2284 &pDisk->PCHSGeometry);
2285 if (RT_FAILURE(rc2))
2286 {
2287 pDisk->PCHSGeometry.cCylinders = 0;
2288 pDisk->PCHSGeometry.cHeads = 0;
2289 pDisk->PCHSGeometry.cSectors = 0;
2290 }
2291 else
2292 {
2293 /* Make sure the PCHS geometry is properly clipped. */
2294 pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
2295 pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
2296 pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
2297 }
2298
2299 /* Cache LCHS geometry. */
2300 rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pvBackendData,
2301 &pDisk->LCHSGeometry);
2302 if (RT_FAILURE(rc2))
2303 {
2304 pDisk->LCHSGeometry.cCylinders = 0;
2305 pDisk->LCHSGeometry.cHeads = 0;
2306 pDisk->LCHSGeometry.cSectors = 0;
2307 }
2308 else
2309 {
2310 /* Make sure the LCHS geometry is properly clipped. */
2311 pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
2312 pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
2313 }
2314 } while (0);
2315
2316 LogFlowFunc(("returns %Rrc\n", rc));
2317 return rc;
2318}
2319
2320/**
2321 * Closes all opened image files in HDD container.
2322 *
2323 * @returns VBox status code.
2324 * @param pDisk Pointer to HDD container.
2325 */
2326VBOXDDU_DECL(int) VDCloseAll(PVBOXHDD pDisk)
2327{
2328 int rc = VINF_SUCCESS;
2329
2330 LogFlowFunc(("pDisk=%#p\n", pDisk));
2331 do
2332 {
2333 /* sanity check */
2334 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2335 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2336
2337 PVDIMAGE pImage = pDisk->pLast;
2338 while (VALID_PTR(pImage))
2339 {
2340 PVDIMAGE pPrev = pImage->pPrev;
2341 /* Remove image from list of opened images. */
2342 vdRemoveImageFromList(pDisk, pImage);
2343 /* Close image. */
2344 int rc2 = pImage->Backend->pfnClose(pImage->pvBackendData, false);
2345 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
2346 rc = rc2;
2347 /* Free remaining resources related to the image. */
2348 RTStrFree(pImage->pszFilename);
2349 RTMemFree(pImage);
2350 pImage = pPrev;
2351 }
2352 Assert(!VALID_PTR(pDisk->pLast));
2353 } while (0);
2354
2355 LogFlowFunc(("returns %Rrc\n", rc));
2356 return rc;
2357}
2358
2359/**
2360 * Read data from virtual HDD.
2361 *
2362 * @returns VBox status code.
2363 * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
2364 * @param pDisk Pointer to HDD container.
2365 * @param uOffset Offset of first reading byte from start of disk.
2366 * @param pvBuf Pointer to buffer for reading data.
2367 * @param cbRead Number of bytes to read.
2368 */
2369VBOXDDU_DECL(int) VDRead(PVBOXHDD pDisk, uint64_t uOffset, void *pvBuf,
2370 size_t cbRead)
2371{
2372 int rc;
2373
2374 LogFlowFunc(("pDisk=%#p uOffset=%llu pvBuf=%p cbRead=%zu\n",
2375 pDisk, uOffset, pvBuf, cbRead));
2376 do
2377 {
2378 /* sanity check */
2379 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2380 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2381
2382 /* Check arguments. */
2383 AssertMsgBreakStmt(VALID_PTR(pvBuf),
2384 ("pvBuf=%#p\n", pvBuf),
2385 rc = VERR_INVALID_PARAMETER);
2386 AssertMsgBreakStmt(cbRead,
2387 ("cbRead=%zu\n", cbRead),
2388 rc = VERR_INVALID_PARAMETER);
2389 AssertMsgBreakStmt(uOffset + cbRead <= pDisk->cbSize,
2390 ("uOffset=%llu cbRead=%zu pDisk->cbSize=%llu\n",
2391 uOffset, cbRead, pDisk->cbSize),
2392 rc = VERR_INVALID_PARAMETER);
2393
2394 PVDIMAGE pImage = pDisk->pLast;
2395 AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
2396
2397 rc = vdReadHelper(pDisk, pImage, uOffset, pvBuf, cbRead);
2398 } while (0);
2399
2400 LogFlowFunc(("returns %Rrc\n", rc));
2401 return rc;
2402}
2403
2404/**
2405 * Write data to virtual HDD.
2406 *
2407 * @returns VBox status code.
2408 * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
2409 * @param pDisk Pointer to HDD container.
2410 * @param uOffset Offset of the first byte being
2411 * written from start of disk.
2412 * @param pvBuf Pointer to buffer for writing data.
2413 * @param cbWrite Number of bytes to write.
2414 */
2415VBOXDDU_DECL(int) VDWrite(PVBOXHDD pDisk, uint64_t uOffset, const void *pvBuf,
2416 size_t cbWrite)
2417{
2418 int rc = VINF_SUCCESS;
2419
2420 LogFlowFunc(("pDisk=%#p uOffset=%llu pvBuf=%p cbWrite=%zu\n",
2421 pDisk, uOffset, pvBuf, cbWrite));
2422 do
2423 {
2424 /* sanity check */
2425 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2426 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2427
2428 /* Check arguments. */
2429 AssertMsgBreakStmt(VALID_PTR(pvBuf),
2430 ("pvBuf=%#p\n", pvBuf),
2431 rc = VERR_INVALID_PARAMETER);
2432 AssertMsgBreakStmt(cbWrite,
2433 ("cbWrite=%zu\n", cbWrite),
2434 rc = VERR_INVALID_PARAMETER);
2435 AssertMsgBreakStmt(uOffset + cbWrite <= pDisk->cbSize,
2436 ("uOffset=%llu cbWrite=%zu pDisk->cbSize=%llu\n",
2437 uOffset, cbWrite, pDisk->cbSize),
2438 rc = VERR_INVALID_PARAMETER);
2439
2440 PVDIMAGE pImage = pDisk->pLast;
2441 AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
2442
2443 vdSetModifiedFlag(pDisk);
2444 rc = vdWriteHelper(pDisk, pImage, uOffset, pvBuf, cbWrite);
2445 } while (0);
2446
2447 LogFlowFunc(("returns %Rrc\n", rc));
2448 return rc;
2449}
2450
2451/**
2452 * Make sure the on disk representation of a virtual HDD is up to date.
2453 *
2454 * @returns VBox status code.
2455 * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
2456 * @param pDisk Pointer to HDD container.
2457 */
2458VBOXDDU_DECL(int) VDFlush(PVBOXHDD pDisk)
2459{
2460 int rc = VINF_SUCCESS;
2461
2462 LogFlowFunc(("pDisk=%#p\n", pDisk));
2463 do
2464 {
2465 /* sanity check */
2466 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2467 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2468
2469 PVDIMAGE pImage = pDisk->pLast;
2470 AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
2471
2472 vdResetModifiedFlag(pDisk);
2473 rc = pImage->Backend->pfnFlush(pImage->pvBackendData);
2474 } while (0);
2475
2476 LogFlowFunc(("returns %Rrc\n", rc));
2477 return rc;
2478}
2479
2480/**
2481 * Get number of opened images in HDD container.
2482 *
2483 * @returns Number of opened images for HDD container. 0 if no images have been opened.
2484 * @param pDisk Pointer to HDD container.
2485 */
2486VBOXDDU_DECL(unsigned) VDGetCount(PVBOXHDD pDisk)
2487{
2488 unsigned cImages;
2489
2490 LogFlowFunc(("pDisk=%#p\n", pDisk));
2491 do
2492 {
2493 /* sanity check */
2494 AssertPtrBreakStmt(pDisk, cImages = 0);
2495 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2496
2497 cImages = pDisk->cImages;
2498 } while (0);
2499
2500 LogFlowFunc(("returns %u\n", cImages));
2501 return cImages;
2502}
2503
2504/**
2505 * Get read/write mode of HDD container.
2506 *
2507 * @returns Virtual disk ReadOnly status.
2508 * @returns true if no image is opened in HDD container.
2509 * @param pDisk Pointer to HDD container.
2510 */
2511VBOXDDU_DECL(bool) VDIsReadOnly(PVBOXHDD pDisk)
2512{
2513 bool fReadOnly;
2514
2515 LogFlowFunc(("pDisk=%#p\n", pDisk));
2516 do
2517 {
2518 /* sanity check */
2519 AssertPtrBreakStmt(pDisk, fReadOnly = false);
2520 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2521
2522 PVDIMAGE pImage = pDisk->pLast;
2523 AssertPtrBreakStmt(pImage, fReadOnly = true);
2524
2525 unsigned uOpenFlags;
2526 uOpenFlags = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pvBackendData);
2527 fReadOnly = !!(uOpenFlags & VD_OPEN_FLAGS_READONLY);
2528 } while (0);
2529
2530 LogFlowFunc(("returns %d\n", fReadOnly));
2531 return fReadOnly;
2532}
2533
2534/**
2535 * Get total capacity of an image in HDD container.
2536 *
2537 * @returns Virtual disk size in bytes.
2538 * @returns 0 if no image with specified number was not opened.
2539 * @param pDisk Pointer to HDD container.
2540 * @param nImage Image number, counds from 0. 0 is always base image of container.
2541 */
2542VBOXDDU_DECL(uint64_t) VDGetSize(PVBOXHDD pDisk, unsigned nImage)
2543{
2544 uint64_t cbSize;
2545
2546 LogFlowFunc(("pDisk=%#p nImage=%u\n", pDisk, nImage));
2547 do
2548 {
2549 /* sanity check */
2550 AssertPtrBreakStmt(pDisk, cbSize = 0);
2551 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2552
2553 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
2554 AssertPtrBreakStmt(pImage, cbSize = 0);
2555 cbSize = pImage->Backend->pfnGetSize(pImage->pvBackendData);
2556 } while (0);
2557
2558 LogFlowFunc(("returns %llu\n", cbSize));
2559 return cbSize;
2560}
2561
2562/**
2563 * Get total file size of an image in HDD container.
2564 *
2565 * @returns Virtual disk size in bytes.
2566 * @returns 0 if no image is opened in HDD container.
2567 * @param pDisk Pointer to HDD container.
2568 * @param nImage Image number, counts from 0. 0 is always base image of container.
2569 */
2570VBOXDDU_DECL(uint64_t) VDGetFileSize(PVBOXHDD pDisk, unsigned nImage)
2571{
2572 uint64_t cbSize;
2573
2574 LogFlowFunc(("pDisk=%#p nImage=%u\n", pDisk, nImage));
2575 do
2576 {
2577 /* sanity check */
2578 AssertPtrBreakStmt(pDisk, cbSize = 0);
2579 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2580
2581 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
2582 AssertPtrBreakStmt(pImage, cbSize = 0);
2583 cbSize = pImage->Backend->pfnGetFileSize(pImage->pvBackendData);
2584 } while (0);
2585
2586 LogFlowFunc(("returns %llu\n", cbSize));
2587 return cbSize;
2588}
2589
2590/**
2591 * Get virtual disk PCHS geometry stored in HDD container.
2592 *
2593 * @returns VBox status code.
2594 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
2595 * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
2596 * @param pDisk Pointer to HDD container.
2597 * @param nImage Image number, counts from 0. 0 is always base image of container.
2598 * @param pPCHSGeometry Where to store PCHS geometry. Not NULL.
2599 */
2600VBOXDDU_DECL(int) VDGetPCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
2601 PPDMMEDIAGEOMETRY pPCHSGeometry)
2602{
2603 int rc = VINF_SUCCESS;
2604
2605 LogFlowFunc(("pDisk=%#p nImage=%u pPCHSGeometry=%#p\n",
2606 pDisk, nImage, pPCHSGeometry));
2607 do
2608 {
2609 /* sanity check */
2610 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2611 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2612
2613 /* Check arguments. */
2614 AssertMsgBreakStmt(VALID_PTR(pPCHSGeometry),
2615 ("pPCHSGeometry=%#p\n", pPCHSGeometry),
2616 rc = VERR_INVALID_PARAMETER);
2617
2618 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
2619 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
2620
2621 if (pImage == pDisk->pLast)
2622 {
2623 /* Use cached information if possible. */
2624 if (pDisk->PCHSGeometry.cCylinders != 0)
2625 *pPCHSGeometry = pDisk->PCHSGeometry;
2626 else
2627 rc = VERR_VD_GEOMETRY_NOT_SET;
2628 }
2629 else
2630 rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pvBackendData,
2631 pPCHSGeometry);
2632 } while (0);
2633
2634 LogFlowFunc(("%s: %Rrc (PCHS=%u/%u/%u)\n", __FUNCTION__, rc,
2635 pDisk->PCHSGeometry.cCylinders, pDisk->PCHSGeometry.cHeads,
2636 pDisk->PCHSGeometry.cSectors));
2637 return rc;
2638}
2639
2640/**
2641 * Store virtual disk PCHS geometry in HDD container.
2642 *
2643 * Note that in case of unrecoverable error all images in HDD container will be closed.
2644 *
2645 * @returns VBox status code.
2646 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
2647 * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
2648 * @param pDisk Pointer to HDD container.
2649 * @param nImage Image number, counts from 0. 0 is always base image of container.
2650 * @param pPCHSGeometry Where to load PCHS geometry from. Not NULL.
2651 */
2652VBOXDDU_DECL(int) VDSetPCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
2653 PCPDMMEDIAGEOMETRY pPCHSGeometry)
2654{
2655 int rc = VINF_SUCCESS;
2656
2657 LogFlowFunc(("pDisk=%#p nImage=%u pPCHSGeometry=%#p PCHS=%u/%u/%u\n",
2658 pDisk, nImage, pPCHSGeometry, pPCHSGeometry->cCylinders,
2659 pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2660 do
2661 {
2662 /* sanity check */
2663 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2664 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2665
2666 /* Check arguments. */
2667 AssertMsgBreakStmt( VALID_PTR(pPCHSGeometry)
2668 && pPCHSGeometry->cCylinders <= 16383
2669 && pPCHSGeometry->cHeads <= 16
2670 && pPCHSGeometry->cSectors <= 63,
2671 ("pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pPCHSGeometry,
2672 pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
2673 pPCHSGeometry->cSectors),
2674 rc = VERR_INVALID_PARAMETER);
2675
2676 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
2677 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
2678
2679 if (pImage == pDisk->pLast)
2680 {
2681 if ( pPCHSGeometry->cCylinders != pDisk->PCHSGeometry.cCylinders
2682 || pPCHSGeometry->cHeads != pDisk->PCHSGeometry.cHeads
2683 || pPCHSGeometry->cSectors != pDisk->PCHSGeometry.cSectors)
2684 {
2685 /* Only update geometry if it is changed. Avoids similar checks
2686 * in every backend. Most of the time the new geometry is set
2687 * to the previous values, so no need to go through the hassle
2688 * of updating an image which could be opened in read-only mode
2689 * right now. */
2690 rc = pImage->Backend->pfnSetPCHSGeometry(pImage->pvBackendData,
2691 pPCHSGeometry);
2692
2693 /* Cache new geometry values in any case. */
2694 int rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pvBackendData,
2695 &pDisk->PCHSGeometry);
2696 if (RT_FAILURE(rc2))
2697 {
2698 pDisk->PCHSGeometry.cCylinders = 0;
2699 pDisk->PCHSGeometry.cHeads = 0;
2700 pDisk->PCHSGeometry.cSectors = 0;
2701 }
2702 else
2703 {
2704 /* Make sure the CHS geometry is properly clipped. */
2705 pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 255);
2706 pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
2707 }
2708 }
2709 }
2710 else
2711 {
2712 PDMMEDIAGEOMETRY PCHS;
2713 rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pvBackendData,
2714 &PCHS);
2715 if ( RT_FAILURE(rc)
2716 || pPCHSGeometry->cCylinders != PCHS.cCylinders
2717 || pPCHSGeometry->cHeads != PCHS.cHeads
2718 || pPCHSGeometry->cSectors != PCHS.cSectors)
2719 {
2720 /* Only update geometry if it is changed. Avoids similar checks
2721 * in every backend. Most of the time the new geometry is set
2722 * to the previous values, so no need to go through the hassle
2723 * of updating an image which could be opened in read-only mode
2724 * right now. */
2725 rc = pImage->Backend->pfnSetPCHSGeometry(pImage->pvBackendData,
2726 pPCHSGeometry);
2727 }
2728 }
2729 } while (0);
2730
2731 LogFlowFunc(("returns %Rrc\n", rc));
2732 return rc;
2733}
2734
2735/**
2736 * Get virtual disk LCHS geometry stored in HDD container.
2737 *
2738 * @returns VBox status code.
2739 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
2740 * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
2741 * @param pDisk Pointer to HDD container.
2742 * @param nImage Image number, counts from 0. 0 is always base image of container.
2743 * @param pLCHSGeometry Where to store LCHS geometry. Not NULL.
2744 */
2745VBOXDDU_DECL(int) VDGetLCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
2746 PPDMMEDIAGEOMETRY pLCHSGeometry)
2747{
2748 int rc = VINF_SUCCESS;
2749
2750 LogFlowFunc(("pDisk=%#p nImage=%u pLCHSGeometry=%#p\n",
2751 pDisk, nImage, pLCHSGeometry));
2752 do
2753 {
2754 /* sanity check */
2755 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2756 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2757
2758 /* Check arguments. */
2759 AssertMsgBreakStmt(VALID_PTR(pLCHSGeometry),
2760 ("pLCHSGeometry=%#p\n", pLCHSGeometry),
2761 rc = VERR_INVALID_PARAMETER);
2762
2763 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
2764 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
2765
2766 if (pImage == pDisk->pLast)
2767 {
2768 /* Use cached information if possible. */
2769 if (pDisk->LCHSGeometry.cCylinders != 0)
2770 *pLCHSGeometry = pDisk->LCHSGeometry;
2771 else
2772 rc = VERR_VD_GEOMETRY_NOT_SET;
2773 }
2774 else
2775 rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pvBackendData,
2776 pLCHSGeometry);
2777 } while (0);
2778
2779 LogFlowFunc((": %Rrc (LCHS=%u/%u/%u)\n", rc,
2780 pDisk->LCHSGeometry.cCylinders, pDisk->LCHSGeometry.cHeads,
2781 pDisk->LCHSGeometry.cSectors));
2782 return rc;
2783}
2784
2785/**
2786 * Store virtual disk LCHS geometry in HDD container.
2787 *
2788 * Note that in case of unrecoverable error all images in HDD container will be closed.
2789 *
2790 * @returns VBox status code.
2791 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
2792 * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
2793 * @param pDisk Pointer to HDD container.
2794 * @param nImage Image number, counts from 0. 0 is always base image of container.
2795 * @param pLCHSGeometry Where to load LCHS geometry from. Not NULL.
2796 */
2797VBOXDDU_DECL(int) VDSetLCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
2798 PCPDMMEDIAGEOMETRY pLCHSGeometry)
2799{
2800 int rc = VINF_SUCCESS;
2801
2802 LogFlowFunc(("pDisk=%#p nImage=%u pLCHSGeometry=%#p LCHS=%u/%u/%u\n",
2803 pDisk, nImage, pLCHSGeometry, pLCHSGeometry->cCylinders,
2804 pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2805 do
2806 {
2807 /* sanity check */
2808 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2809 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2810
2811 /* Check arguments. */
2812 AssertMsgBreakStmt( VALID_PTR(pLCHSGeometry)
2813 && pLCHSGeometry->cHeads <= 255
2814 && pLCHSGeometry->cSectors <= 63,
2815 ("pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pLCHSGeometry,
2816 pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads,
2817 pLCHSGeometry->cSectors),
2818 rc = VERR_INVALID_PARAMETER);
2819
2820 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
2821 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
2822
2823 if (pImage == pDisk->pLast)
2824 {
2825 if ( pLCHSGeometry->cCylinders != pDisk->LCHSGeometry.cCylinders
2826 || pLCHSGeometry->cHeads != pDisk->LCHSGeometry.cHeads
2827 || pLCHSGeometry->cSectors != pDisk->LCHSGeometry.cSectors)
2828 {
2829 /* Only update geometry if it is changed. Avoids similar checks
2830 * in every backend. Most of the time the new geometry is set
2831 * to the previous values, so no need to go through the hassle
2832 * of updating an image which could be opened in read-only mode
2833 * right now. */
2834 rc = pImage->Backend->pfnSetLCHSGeometry(pImage->pvBackendData,
2835 pLCHSGeometry);
2836
2837 /* Cache new geometry values in any case. */
2838 int rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pvBackendData,
2839 &pDisk->LCHSGeometry);
2840 if (RT_FAILURE(rc2))
2841 {
2842 pDisk->LCHSGeometry.cCylinders = 0;
2843 pDisk->LCHSGeometry.cHeads = 0;
2844 pDisk->LCHSGeometry.cSectors = 0;
2845 }
2846 else
2847 {
2848 /* Make sure the CHS geometry is properly clipped. */
2849 pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
2850 pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
2851 }
2852 }
2853 }
2854 else
2855 {
2856 PDMMEDIAGEOMETRY LCHS;
2857 rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pvBackendData,
2858 &LCHS);
2859 if ( RT_FAILURE(rc)
2860 || pLCHSGeometry->cCylinders != LCHS.cCylinders
2861 || pLCHSGeometry->cHeads != LCHS.cHeads
2862 || pLCHSGeometry->cSectors != LCHS.cSectors)
2863 {
2864 /* Only update geometry if it is changed. Avoids similar checks
2865 * in every backend. Most of the time the new geometry is set
2866 * to the previous values, so no need to go through the hassle
2867 * of updating an image which could be opened in read-only mode
2868 * right now. */
2869 rc = pImage->Backend->pfnSetLCHSGeometry(pImage->pvBackendData,
2870 pLCHSGeometry);
2871 }
2872 }
2873 } while (0);
2874
2875 LogFlowFunc(("returns %Rrc\n", rc));
2876 return rc;
2877}
2878
2879/**
2880 * Get version of image in HDD container.
2881 *
2882 * @returns VBox status code.
2883 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
2884 * @param pDisk Pointer to HDD container.
2885 * @param nImage Image number, counts from 0. 0 is always base image of container.
2886 * @param puVersion Where to store the image version.
2887 */
2888VBOXDDU_DECL(int) VDGetVersion(PVBOXHDD pDisk, unsigned nImage,
2889 unsigned *puVersion)
2890{
2891 int rc = VINF_SUCCESS;
2892
2893 LogFlowFunc(("pDisk=%#p nImage=%u puVersion=%#p\n",
2894 pDisk, nImage, puVersion));
2895 do
2896 {
2897 /* sanity check */
2898 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2899 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2900
2901 /* Check arguments. */
2902 AssertMsgBreakStmt(VALID_PTR(puVersion),
2903 ("puVersion=%#p\n", puVersion),
2904 rc = VERR_INVALID_PARAMETER);
2905
2906 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
2907 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
2908
2909 *puVersion = pImage->Backend->pfnGetVersion(pImage->pvBackendData);
2910 } while (0);
2911
2912 LogFlowFunc(("returns %Rrc uVersion=%#x\n", rc, *puVersion));
2913 return rc;
2914}
2915
2916/**
2917 * List the capabilities of image backend in HDD container.
2918 *
2919 * @returns VBox status code.
2920 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
2921 * @param pDisk Pointer to the HDD container.
2922 * @param nImage Image number, counts from 0. 0 is always base image of container.
2923 * @param pbackendInfo Where to store the backend information.
2924 */
2925VBOXDDU_DECL(int) VDBackendInfoSingle(PVBOXHDD pDisk, unsigned nImage,
2926 PVDBACKENDINFO pBackendInfo)
2927{
2928 int rc = VINF_SUCCESS;
2929
2930 LogFlowFunc(("pDisk=%#p nImage=%u pBackendInfo=%#p\n",
2931 pDisk, nImage, pBackendInfo));
2932 do
2933 {
2934 /* sanity check */
2935 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2936 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2937
2938 /* Check arguments. */
2939 AssertMsgBreakStmt(VALID_PTR(pBackendInfo),
2940 ("pBackendInfo=%#p\n", pBackendInfo),
2941 rc = VERR_INVALID_PARAMETER);
2942
2943 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
2944 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
2945
2946 pBackendInfo->pszBackend = RTStrDup(pImage->Backend->pszBackendName);
2947 pBackendInfo->uBackendCaps = pImage->Backend->uBackendCaps;
2948 pBackendInfo->papszFileExtensions = pImage->Backend->papszFileExtensions;
2949 pBackendInfo->paConfigInfo = pImage->Backend->paConfigInfo;
2950 } while (0);
2951
2952 LogFlowFunc(("returns %Rrc\n", rc));
2953 return rc;
2954}
2955
2956/**
2957 * Get flags of image in HDD container.
2958 *
2959 * @returns VBox status code.
2960 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
2961 * @param pDisk Pointer to HDD container.
2962 * @param nImage Image number, counts from 0. 0 is always base image of container.
2963 * @param puImageFlags Where to store the image flags.
2964 */
2965VBOXDDU_DECL(int) VDGetImageFlags(PVBOXHDD pDisk, unsigned nImage,
2966 unsigned *puImageFlags)
2967{
2968 int rc = VINF_SUCCESS;
2969
2970 LogFlowFunc(("pDisk=%#p nImage=%u puImageFlags=%#p\n",
2971 pDisk, nImage, puImageFlags));
2972 do
2973 {
2974 /* sanity check */
2975 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2976 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2977
2978 /* Check arguments. */
2979 AssertMsgBreakStmt(VALID_PTR(puImageFlags),
2980 ("puImageFlags=%#p\n", puImageFlags),
2981 rc = VERR_INVALID_PARAMETER);
2982
2983 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
2984 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
2985
2986 *puImageFlags = pImage->Backend->pfnGetImageFlags(pImage->pvBackendData);
2987 } while (0);
2988
2989 LogFlowFunc(("returns %Rrc uImageFlags=%#x\n", rc, *puImageFlags));
2990 return rc;
2991}
2992
2993/**
2994 * Get open flags of image in HDD container.
2995 *
2996 * @returns VBox status code.
2997 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
2998 * @param pDisk Pointer to HDD container.
2999 * @param nImage Image number, counts from 0. 0 is always base image of container.
3000 * @param puOpenFlags Where to store the image open flags.
3001 */
3002VBOXDDU_DECL(int) VDGetOpenFlags(PVBOXHDD pDisk, unsigned nImage,
3003 unsigned *puOpenFlags)
3004{
3005 int rc = VINF_SUCCESS;
3006
3007 LogFlowFunc(("pDisk=%#p nImage=%u puOpenFlags=%#p\n",
3008 pDisk, nImage, puOpenFlags));
3009 do
3010 {
3011 /* sanity check */
3012 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3013 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3014
3015 /* Check arguments. */
3016 AssertMsgBreakStmt(VALID_PTR(puOpenFlags),
3017 ("puOpenFlags=%#p\n", puOpenFlags),
3018 rc = VERR_INVALID_PARAMETER);
3019
3020 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3021 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3022
3023 *puOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pvBackendData);
3024 } while (0);
3025
3026 LogFlowFunc(("returns %Rrc uOpenFlags=%#x\n", rc, *puOpenFlags));
3027 return rc;
3028}
3029
3030/**
3031 * Set open flags of image in HDD container.
3032 * This operation may cause file locking changes and/or files being reopened.
3033 * Note that in case of unrecoverable error all images in HDD container will be closed.
3034 *
3035 * @returns VBox status code.
3036 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
3037 * @param pDisk Pointer to HDD container.
3038 * @param nImage Image number, counts from 0. 0 is always base image of container.
3039 * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
3040 */
3041VBOXDDU_DECL(int) VDSetOpenFlags(PVBOXHDD pDisk, unsigned nImage,
3042 unsigned uOpenFlags)
3043{
3044 int rc;
3045
3046 LogFlowFunc(("pDisk=%#p uOpenFlags=%#u\n", pDisk, uOpenFlags));
3047 do
3048 {
3049 /* sanity check */
3050 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3051 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3052
3053 /* Check arguments. */
3054 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
3055 ("uOpenFlags=%#x\n", uOpenFlags),
3056 rc = VERR_INVALID_PARAMETER);
3057
3058 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3059 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3060
3061 rc = pImage->Backend->pfnSetOpenFlags(pImage->pvBackendData,
3062 uOpenFlags);
3063 } while (0);
3064
3065 LogFlowFunc(("returns %Rrc\n", rc));
3066 return rc;
3067}
3068
3069/**
3070 * Get base filename of image in HDD container. Some image formats use
3071 * other filenames as well, so don't use this for anything but informational
3072 * purposes.
3073 *
3074 * @returns VBox status code.
3075 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
3076 * @returns VERR_BUFFER_OVERFLOW if pszFilename buffer too small to hold filename.
3077 * @param pDisk Pointer to HDD container.
3078 * @param nImage Image number, counts from 0. 0 is always base image of container.
3079 * @param pszFilename Where to store the image file name.
3080 * @param cbFilename Size of buffer pszFilename points to.
3081 */
3082VBOXDDU_DECL(int) VDGetFilename(PVBOXHDD pDisk, unsigned nImage,
3083 char *pszFilename, unsigned cbFilename)
3084{
3085 int rc;
3086
3087 LogFlowFunc(("pDisk=%#p nImage=%u pszFilename=%#p cbFilename=%u\n",
3088 pDisk, nImage, pszFilename, cbFilename));
3089 do
3090 {
3091 /* sanity check */
3092 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3093 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3094
3095 /* Check arguments. */
3096 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
3097 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
3098 rc = VERR_INVALID_PARAMETER);
3099 AssertMsgBreakStmt(cbFilename,
3100 ("cbFilename=%u\n", cbFilename),
3101 rc = VERR_INVALID_PARAMETER);
3102
3103 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3104 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3105
3106 size_t cb = strlen(pImage->pszFilename);
3107 if (cb <= cbFilename)
3108 {
3109 strcpy(pszFilename, pImage->pszFilename);
3110 rc = VINF_SUCCESS;
3111 }
3112 else
3113 {
3114 strncpy(pszFilename, pImage->pszFilename, cbFilename - 1);
3115 pszFilename[cbFilename - 1] = '\0';
3116 rc = VERR_BUFFER_OVERFLOW;
3117 }
3118 } while (0);
3119
3120 LogFlowFunc(("returns %Rrc, pszFilename=\"%s\"\n", rc, pszFilename));
3121 return rc;
3122}
3123
3124/**
3125 * Get the comment line of image in HDD container.
3126 *
3127 * @returns VBox status code.
3128 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
3129 * @returns VERR_BUFFER_OVERFLOW if pszComment buffer too small to hold comment text.
3130 * @param pDisk Pointer to HDD container.
3131 * @param nImage Image number, counts from 0. 0 is always base image of container.
3132 * @param pszComment Where to store the comment string of image. NULL is ok.
3133 * @param cbComment The size of pszComment buffer. 0 is ok.
3134 */
3135VBOXDDU_DECL(int) VDGetComment(PVBOXHDD pDisk, unsigned nImage,
3136 char *pszComment, unsigned cbComment)
3137{
3138 int rc;
3139
3140 LogFlowFunc(("pDisk=%#p nImage=%u pszComment=%#p cbComment=%u\n",
3141 pDisk, nImage, pszComment, cbComment));
3142 do
3143 {
3144 /* sanity check */
3145 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3146 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3147
3148 /* Check arguments. */
3149 AssertMsgBreakStmt(VALID_PTR(pszComment),
3150 ("pszComment=%#p \"%s\"\n", pszComment, pszComment),
3151 rc = VERR_INVALID_PARAMETER);
3152 AssertMsgBreakStmt(cbComment,
3153 ("cbComment=%u\n", cbComment),
3154 rc = VERR_INVALID_PARAMETER);
3155
3156 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3157 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3158
3159 rc = pImage->Backend->pfnGetComment(pImage->pvBackendData, pszComment,
3160 cbComment);
3161 } while (0);
3162
3163 LogFlowFunc(("returns %Rrc, pszComment=\"%s\"\n", rc, pszComment));
3164 return rc;
3165}
3166
3167/**
3168 * Changes the comment line of image in HDD container.
3169 *
3170 * @returns VBox status code.
3171 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
3172 * @param pDisk Pointer to HDD container.
3173 * @param nImage Image number, counts from 0. 0 is always base image of container.
3174 * @param pszComment New comment string (UTF-8). NULL is allowed to reset the comment.
3175 */
3176VBOXDDU_DECL(int) VDSetComment(PVBOXHDD pDisk, unsigned nImage,
3177 const char *pszComment)
3178{
3179 int rc;
3180
3181 LogFlowFunc(("pDisk=%#p nImage=%u pszComment=%#p \"%s\"\n",
3182 pDisk, nImage, pszComment, pszComment));
3183 do
3184 {
3185 /* sanity check */
3186 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3187 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3188
3189 /* Check arguments. */
3190 AssertMsgBreakStmt(VALID_PTR(pszComment) || pszComment == NULL,
3191 ("pszComment=%#p \"%s\"\n", pszComment, pszComment),
3192 rc = VERR_INVALID_PARAMETER);
3193
3194 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3195 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3196
3197 rc = pImage->Backend->pfnSetComment(pImage->pvBackendData, pszComment);
3198 } while (0);
3199
3200 LogFlowFunc(("returns %Rrc\n", rc));
3201 return rc;
3202}
3203
3204
3205/**
3206 * Get UUID of image in HDD container.
3207 *
3208 * @returns VBox status code.
3209 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
3210 * @param pDisk Pointer to HDD container.
3211 * @param nImage Image number, counts from 0. 0 is always base image of container.
3212 * @param pUuid Where to store the image creation UUID.
3213 */
3214VBOXDDU_DECL(int) VDGetUuid(PVBOXHDD pDisk, unsigned nImage, PRTUUID pUuid)
3215{
3216 int rc;
3217
3218 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
3219 do
3220 {
3221 /* sanity check */
3222 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3223 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3224
3225 /* Check arguments. */
3226 AssertMsgBreakStmt(VALID_PTR(pUuid),
3227 ("pUuid=%#p\n", pUuid),
3228 rc = VERR_INVALID_PARAMETER);
3229
3230 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3231 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3232
3233 rc = pImage->Backend->pfnGetUuid(pImage->pvBackendData, pUuid);
3234 } while (0);
3235
3236 LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
3237 return rc;
3238}
3239
3240/**
3241 * Set the image's UUID. Should not be used by normal applications.
3242 *
3243 * @returns VBox status code.
3244 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
3245 * @param pDisk Pointer to HDD container.
3246 * @param nImage Image number, counts from 0. 0 is always base image of container.
3247 * @param pUuid New UUID of the image. If NULL, a new UUID is created.
3248 */
3249VBOXDDU_DECL(int) VDSetUuid(PVBOXHDD pDisk, unsigned nImage, PCRTUUID pUuid)
3250{
3251 int rc;
3252
3253 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
3254 pDisk, nImage, pUuid, pUuid));
3255 do
3256 {
3257 /* sanity check */
3258 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3259 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3260
3261 AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
3262 ("pUuid=%#p\n", pUuid),
3263 rc = VERR_INVALID_PARAMETER);
3264
3265 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3266 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3267
3268 RTUUID Uuid;
3269 if (!pUuid)
3270 {
3271 RTUuidCreate(&Uuid);
3272 pUuid = &Uuid;
3273 }
3274 rc = pImage->Backend->pfnSetUuid(pImage->pvBackendData, pUuid);
3275 } while (0);
3276
3277 LogFlowFunc(("returns %Rrc\n", rc));
3278 return rc;
3279}
3280
3281/**
3282 * Get last modification UUID of image in HDD container.
3283 *
3284 * @returns VBox status code.
3285 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
3286 * @param pDisk Pointer to HDD container.
3287 * @param nImage Image number, counts from 0. 0 is always base image of container.
3288 * @param pUuid Where to store the image modification UUID.
3289 */
3290VBOXDDU_DECL(int) VDGetModificationUuid(PVBOXHDD pDisk, unsigned nImage, PRTUUID pUuid)
3291{
3292 int rc = VINF_SUCCESS;
3293
3294 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
3295 do
3296 {
3297 /* sanity check */
3298 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3299 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3300
3301 /* Check arguments. */
3302 AssertMsgBreakStmt(VALID_PTR(pUuid),
3303 ("pUuid=%#p\n", pUuid),
3304 rc = VERR_INVALID_PARAMETER);
3305
3306 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3307 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3308
3309 rc = pImage->Backend->pfnGetModificationUuid(pImage->pvBackendData,
3310 pUuid);
3311 } while (0);
3312
3313 LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
3314 return rc;
3315}
3316
3317/**
3318 * Set the image's last modification UUID. Should not be used by normal applications.
3319 *
3320 * @returns VBox status code.
3321 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
3322 * @param pDisk Pointer to HDD container.
3323 * @param nImage Image number, counts from 0. 0 is always base image of container.
3324 * @param pUuid New modification UUID of the image. If NULL, a new UUID is created.
3325 */
3326VBOXDDU_DECL(int) VDSetModificationUuid(PVBOXHDD pDisk, unsigned nImage, PCRTUUID pUuid)
3327{
3328 int rc;
3329
3330 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
3331 pDisk, nImage, pUuid, pUuid));
3332 do
3333 {
3334 /* sanity check */
3335 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3336 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3337
3338 /* Check arguments. */
3339 AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
3340 ("pUuid=%#p\n", pUuid),
3341 rc = VERR_INVALID_PARAMETER);
3342
3343 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3344 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3345
3346 RTUUID Uuid;
3347 if (!pUuid)
3348 {
3349 RTUuidCreate(&Uuid);
3350 pUuid = &Uuid;
3351 }
3352 rc = pImage->Backend->pfnSetModificationUuid(pImage->pvBackendData,
3353 pUuid);
3354 } while (0);
3355
3356 LogFlowFunc(("returns %Rrc\n", rc));
3357 return rc;
3358}
3359
3360/**
3361 * Get parent UUID of image in HDD container.
3362 *
3363 * @returns VBox status code.
3364 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
3365 * @param pDisk Pointer to HDD container.
3366 * @param nImage Image number, counts from 0. 0 is always base image of container.
3367 * @param pUuid Where to store the parent image UUID.
3368 */
3369VBOXDDU_DECL(int) VDGetParentUuid(PVBOXHDD pDisk, unsigned nImage,
3370 PRTUUID pUuid)
3371{
3372 int rc = VINF_SUCCESS;
3373
3374 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
3375 do
3376 {
3377 /* sanity check */
3378 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3379 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3380
3381 /* Check arguments. */
3382 AssertMsgBreakStmt(VALID_PTR(pUuid),
3383 ("pUuid=%#p\n", pUuid),
3384 rc = VERR_INVALID_PARAMETER);
3385
3386 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3387 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3388
3389 rc = pImage->Backend->pfnGetParentUuid(pImage->pvBackendData, pUuid);
3390 } while (0);
3391
3392 LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
3393 return rc;
3394}
3395
3396/**
3397 * Set the image's parent UUID. Should not be used by normal applications.
3398 *
3399 * @returns VBox status code.
3400 * @param pDisk Pointer to HDD container.
3401 * @param nImage Image number, counts from 0. 0 is always base image of container.
3402 * @param pUuid New parent UUID of the image. If NULL, a new UUID is created.
3403 */
3404VBOXDDU_DECL(int) VDSetParentUuid(PVBOXHDD pDisk, unsigned nImage,
3405 PCRTUUID pUuid)
3406{
3407 int rc;
3408
3409 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
3410 pDisk, nImage, pUuid, pUuid));
3411 do
3412 {
3413 /* sanity check */
3414 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3415 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3416
3417 /* Check arguments. */
3418 AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
3419 ("pUuid=%#p\n", pUuid),
3420 rc = VERR_INVALID_PARAMETER);
3421
3422 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3423 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3424
3425 RTUUID Uuid;
3426 if (!pUuid)
3427 {
3428 RTUuidCreate(&Uuid);
3429 pUuid = &Uuid;
3430 }
3431 rc = pImage->Backend->pfnSetParentUuid(pImage->pvBackendData, pUuid);
3432 } while (0);
3433
3434 LogFlowFunc(("returns %Rrc\n", rc));
3435 return rc;
3436}
3437
3438
3439/**
3440 * Debug helper - dumps all opened images in HDD container into the log file.
3441 *
3442 * @param pDisk Pointer to HDD container.
3443 */
3444VBOXDDU_DECL(void) VDDumpImages(PVBOXHDD pDisk)
3445{
3446 do
3447 {
3448 /* sanity check */
3449 AssertPtrBreak(pDisk);
3450 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3451
3452 RTLogPrintf("--- Dumping VD Disk, Images=%u\n", pDisk->cImages);
3453 for (PVDIMAGE pImage = pDisk->pBase; pImage; pImage = pImage->pNext)
3454 {
3455 RTLogPrintf("Dumping VD image \"%s\" (Backend=%s)\n",
3456 pImage->pszFilename, pImage->Backend->pszBackendName);
3457 pImage->Backend->pfnDump(pImage->pvBackendData);
3458 }
3459 } while (0);
3460}
3461
3462/**
3463 * Query if asynchronous operations are supported for this disk.
3464 *
3465 * @returns VBox status code.
3466 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
3467 * @param pDisk Pointer to the HDD container.
3468 * @param nImage Image number, counts from 0. 0 is always base image of container.
3469 * @param pfAIOSupported Where to store if async IO is supported.
3470 */
3471VBOXDDU_DECL(int) VDImageIsAsyncIOSupported(PVBOXHDD pDisk, unsigned nImage, bool *pfAIOSupported)
3472{
3473 int rc = VINF_SUCCESS;
3474
3475 LogFlowFunc(("pDisk=%#p nImage=%u pfAIOSupported=%#p\n", pDisk, nImage, pfAIOSupported));
3476 do
3477 {
3478 /* sanity check */
3479 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3480 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3481
3482 /* Check arguments. */
3483 AssertMsgBreakStmt(VALID_PTR(pfAIOSupported),
3484 ("pfAIOSupported=%#p\n", pfAIOSupported),
3485 rc = VERR_INVALID_PARAMETER);
3486
3487 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3488 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3489
3490 if (pImage->Backend->uBackendCaps & VD_CAP_ASYNC)
3491 *pfAIOSupported = pImage->Backend->pfnIsAsyncIOSupported(pImage->pvBackendData);
3492 else
3493 *pfAIOSupported = false;
3494 } while (0);
3495
3496 LogFlowFunc(("returns %Rrc, fAIOSupported=%u\n", rc, *pfAIOSupported));
3497 return rc;
3498}
3499
3500/**
3501 * Start a asynchronous read request.
3502 *
3503 * @returns VBox status code.
3504 * @param pDisk Pointer to the HDD container.
3505 * @param uOffset The offset of the virtual disk to read from.
3506 * @param cbRead How many bytes to read.
3507 * @param paSeg Pointer to an array of segments.
3508 * @param cSeg Number of segments in the array.
3509 * @param pvUser User data which is passed on completion
3510 */
3511VBOXDDU_DECL(int) VDAsyncRead(PVBOXHDD pDisk, uint64_t uOffset, size_t cbRead,
3512 PPDMDATASEG paSeg, unsigned cSeg,
3513 void *pvUser)
3514{
3515 int rc = VERR_VD_BLOCK_FREE;
3516
3517 LogFlowFunc(("pDisk=%#p uOffset=%llu paSeg=%p cSeg=%u cbRead=%zu\n",
3518 pDisk, uOffset, paSeg, cSeg, cbRead));
3519 do
3520 {
3521 /* sanity check */
3522 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3523 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3524
3525 /* Check arguments. */
3526 AssertMsgBreakStmt(cbRead,
3527 ("cbRead=%zu\n", cbRead),
3528 rc = VERR_INVALID_PARAMETER);
3529 AssertMsgBreakStmt(uOffset + cbRead <= pDisk->cbSize,
3530 ("uOffset=%llu cbRead=%zu pDisk->cbSize=%llu\n",
3531 uOffset, cbRead, pDisk->cbSize),
3532 rc = VERR_INVALID_PARAMETER);
3533 AssertMsgBreakStmt(VALID_PTR(paSeg),
3534 ("paSeg=%#p\n", paSeg),
3535 rc = VERR_INVALID_PARAMETER);
3536 AssertMsgBreakStmt(cSeg,
3537 ("cSeg=%zu\n", cSeg),
3538 rc = VERR_INVALID_PARAMETER);
3539
3540
3541 PVDIMAGE pImage = pDisk->pLast;
3542 AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
3543
3544 /* @todo: This does not work for images which do not have all meta data in memory. */
3545 for (PVDIMAGE pCurrImage = pImage;
3546 pCurrImage != NULL && rc == VERR_VD_BLOCK_FREE;
3547 pCurrImage = pCurrImage->pPrev)
3548 {
3549 rc = pCurrImage->Backend->pfnAsyncRead(pCurrImage->pvBackendData,
3550 uOffset, cbRead, paSeg, cSeg,
3551 pvUser);
3552 }
3553
3554 /* No image in the chain contains the data for the block. */
3555 if (rc == VERR_VD_BLOCK_FREE)
3556 {
3557 for (unsigned i = 0; i < cSeg && (cbRead > 0); i++)
3558 {
3559 memset(paSeg[i].pvSeg, '\0', paSeg[i].cbSeg);
3560 cbRead -= paSeg[i].cbSeg;
3561 }
3562 /* Request finished without the need to enqueue a async I/O request. Tell caller. */
3563 rc = VINF_VD_ASYNC_IO_FINISHED;
3564 }
3565
3566 } while (0);
3567
3568 LogFlowFunc(("returns %Rrc\n", rc));
3569 return rc;
3570}
3571
3572
3573/**
3574 * Start a asynchronous write request.
3575 *
3576 * @returns VBox status code.
3577 * @param pDisk Pointer to the HDD container.
3578 * @param uOffset The offset of the virtual disk to write to.
3579 * @param cbWrtie How many bytes to write.
3580 * @param paSeg Pointer to an array of segments.
3581 * @param cSeg Number of segments in the array.
3582 * @param pvUser User data which is passed on completion.
3583 */
3584VBOXDDU_DECL(int) VDAsyncWrite(PVBOXHDD pDisk, uint64_t uOffset, size_t cbWrite,
3585 PPDMDATASEG paSeg, unsigned cSeg,
3586 void *pvUser)
3587{
3588 int rc;
3589
3590 LogFlowFunc(("pDisk=%#p uOffset=%llu paSeg=%p cSeg=%u cbWrite=%zu\n",
3591 pDisk, uOffset, paSeg, cSeg, cbWrite));
3592 do
3593 {
3594 /* sanity check */
3595 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3596 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3597
3598 /* Check arguments. */
3599 AssertMsgBreakStmt(cbWrite,
3600 ("cbWrite=%zu\n", cbWrite),
3601 rc = VERR_INVALID_PARAMETER);
3602 AssertMsgBreakStmt(uOffset + cbWrite <= pDisk->cbSize,
3603 ("uOffset=%llu cbWrite=%zu pDisk->cbSize=%llu\n",
3604 uOffset, cbWrite, pDisk->cbSize),
3605 rc = VERR_INVALID_PARAMETER);
3606 AssertMsgBreakStmt(VALID_PTR(paSeg),
3607 ("paSeg=%#p\n", paSeg),
3608 rc = VERR_INVALID_PARAMETER);
3609 AssertMsgBreakStmt(cSeg,
3610 ("cSeg=%zu\n", cSeg),
3611 rc = VERR_INVALID_PARAMETER);
3612
3613
3614 PVDIMAGE pImage = pDisk->pLast;
3615 AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
3616
3617 vdSetModifiedFlag(pDisk);
3618 rc = pImage->Backend->pfnAsyncWrite(pImage->pvBackendData,
3619 uOffset, cbWrite,
3620 paSeg, cSeg, pvUser);
3621 } while (0);
3622
3623 LogFlowFunc(("returns %Rrc\n", rc));
3624 return rc;
3625
3626}
3627
3628#if 0
3629/** @copydoc VBOXHDDBACKEND::pfnComposeLocation */
3630int genericFileComposeLocation(PVDINTERFACE pConfig, char **pszLocation)
3631{
3632 return NULL;
3633}
3634
3635
3636/** @copydoc VBOXHDDBACKEND::pfnComposeName */
3637int genericFileComposeName(PVDINTERFACE pConfig, char **pszName)
3638{
3639 return NULL;
3640}
3641#endif
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