VirtualBox

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

Last change on this file since 20833 was 20418, checked in by vboxsync, 16 years ago

VBoxHDD: no checks for 16383 cylinders anymore

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 135.1 KB
Line 
1/* $Id: VBoxHDD.cpp 20418 2009-06-09 08:04:46Z 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->cHeads <= 16
1262 && pPCHSGeometry->cSectors <= 63,
1263 ("pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pPCHSGeometry,
1264 pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
1265 pPCHSGeometry->cSectors),
1266 rc = VERR_INVALID_PARAMETER);
1267 /* The LCHS geometry fields may be 0 to leave it to later autodetection. */
1268 AssertMsgBreakStmt( VALID_PTR(pLCHSGeometry)
1269 && pLCHSGeometry->cHeads <= 255
1270 && pLCHSGeometry->cSectors <= 63,
1271 ("pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pLCHSGeometry,
1272 pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads,
1273 pLCHSGeometry->cSectors),
1274 rc = VERR_INVALID_PARAMETER);
1275 /* The UUID may be NULL. */
1276 AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
1277 ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
1278 rc = VERR_INVALID_PARAMETER);
1279 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
1280 ("uOpenFlags=%#x\n", uOpenFlags),
1281 rc = VERR_INVALID_PARAMETER);
1282
1283 /* Check state. */
1284 AssertMsgBreakStmt(pDisk->cImages == 0,
1285 ("Create base image cannot be done with other images open\n"),
1286 rc = VERR_VD_INVALID_STATE);
1287
1288 /* Set up image descriptor. */
1289 pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
1290 if (!pImage)
1291 {
1292 rc = VERR_NO_MEMORY;
1293 break;
1294 }
1295 pImage->pszFilename = RTStrDup(pszFilename);
1296 if (!pImage->pszFilename)
1297 {
1298 rc = VERR_NO_MEMORY;
1299 break;
1300 }
1301 pImage->pVDIfsImage = pVDIfsImage;
1302
1303 rc = vdFindBackend(pszBackend, &pImage->Backend);
1304 if (RT_FAILURE(rc))
1305 break;
1306 if (!pImage->Backend)
1307 {
1308 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
1309 N_("VD: unknown backend name '%s'"), pszBackend);
1310 break;
1311 }
1312
1313 /* Create UUID if the caller didn't specify one. */
1314 if (!pUuid)
1315 {
1316 rc = RTUuidCreate(&uuid);
1317 if (RT_FAILURE(rc))
1318 {
1319 rc = vdError(pDisk, rc, RT_SRC_POS,
1320 N_("VD: cannot generate UUID for image '%s'"),
1321 pszFilename);
1322 break;
1323 }
1324 pUuid = &uuid;
1325 }
1326
1327 pImage->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
1328 uImageFlags &= ~VD_IMAGE_FLAGS_DIFF;
1329 rc = pImage->Backend->pfnCreate(pImage->pszFilename, cbSize,
1330 uImageFlags, pszComment, pPCHSGeometry,
1331 pLCHSGeometry, pUuid,
1332 uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
1333 0, 99,
1334 pDisk->pVDIfsDisk,
1335 pImage->pVDIfsImage,
1336 pVDIfsOperation,
1337 &pImage->pvBackendData);
1338
1339 if (RT_SUCCESS(rc))
1340 {
1341 pImage->uImageFlags = uImageFlags;
1342
1343 /* Force sane optimization settings. It's not worth avoiding writes
1344 * to fixed size images. The overhead would have almost no payback. */
1345 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
1346 pImage->uOpenFlags |= VD_OPEN_FLAGS_HONOR_SAME;
1347
1348 /** @todo optionally check UUIDs */
1349
1350 int rc2;
1351
1352 /* Cache disk information. */
1353 pDisk->cbSize = pImage->Backend->pfnGetSize(pImage->pvBackendData);
1354
1355 /* Cache PCHS geometry. */
1356 rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pvBackendData,
1357 &pDisk->PCHSGeometry);
1358 if (RT_FAILURE(rc2))
1359 {
1360 pDisk->PCHSGeometry.cCylinders = 0;
1361 pDisk->PCHSGeometry.cHeads = 0;
1362 pDisk->PCHSGeometry.cSectors = 0;
1363 }
1364 else
1365 {
1366 /* Make sure the CHS geometry is properly clipped. */
1367 pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
1368 pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
1369 pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
1370 }
1371
1372 /* Cache LCHS geometry. */
1373 rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pvBackendData,
1374 &pDisk->LCHSGeometry);
1375 if (RT_FAILURE(rc2))
1376 {
1377 pDisk->LCHSGeometry.cCylinders = 0;
1378 pDisk->LCHSGeometry.cHeads = 0;
1379 pDisk->LCHSGeometry.cSectors = 0;
1380 }
1381 else
1382 {
1383 /* Make sure the CHS geometry is properly clipped. */
1384 pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
1385 pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
1386 }
1387 }
1388
1389 if (RT_SUCCESS(rc))
1390 {
1391 /* Image successfully opened, make it the last image. */
1392 vdAddImageToList(pDisk, pImage);
1393 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
1394 pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
1395 }
1396 else
1397 {
1398 /* Error detected, but image opened. Close and delete image. */
1399 int rc2;
1400 rc2 = pImage->Backend->pfnClose(pImage->pvBackendData, true);
1401 AssertRC(rc2);
1402 pImage->pvBackendData = NULL;
1403 }
1404 } while (0);
1405
1406 if (RT_FAILURE(rc))
1407 {
1408 if (pImage)
1409 {
1410 if (pImage->pszFilename)
1411 RTStrFree(pImage->pszFilename);
1412 RTMemFree(pImage);
1413 }
1414 }
1415
1416 if (RT_SUCCESS(rc) && pCbProgress && pCbProgress->pfnProgress)
1417 pCbProgress->pfnProgress(NULL /* WARNING! pVM=NULL */, 100,
1418 pIfProgress->pvUser);
1419
1420 LogFlowFunc(("returns %Rrc\n", rc));
1421 return rc;
1422}
1423
1424/**
1425 * Creates and opens a new differencing image file in HDD container.
1426 * See comments for VDOpen function about differencing images.
1427 *
1428 * @returns VBox status code.
1429 * @param pDisk Pointer to HDD container.
1430 * @param pszBackend Name of the image file backend to use.
1431 * @param pszFilename Name of the differencing image file to create.
1432 * @param uImageFlags Flags specifying special image features.
1433 * @param pszComment Pointer to image comment. NULL is ok.
1434 * @param pUuid New UUID of the image. If NULL, a new UUID is created.
1435 * @param pParentUuid New parent UUID of the image. If NULL, the UUID is queried automatically.
1436 * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
1437 * @param pVDIfsImage Pointer to the per-image VD interface list.
1438 * @param pVDIfsOperation Pointer to the per-operation VD interface list.
1439 */
1440VBOXDDU_DECL(int) VDCreateDiff(PVBOXHDD pDisk, const char *pszBackend,
1441 const char *pszFilename, unsigned uImageFlags,
1442 const char *pszComment, PCRTUUID pUuid,
1443 PCRTUUID pParentUuid, unsigned uOpenFlags,
1444 PVDINTERFACE pVDIfsImage,
1445 PVDINTERFACE pVDIfsOperation)
1446{
1447 int rc = VINF_SUCCESS;
1448 PVDIMAGE pImage = NULL;
1449 RTUUID uuid;
1450
1451 LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uImageFlags=%#x pszComment=\"%s\" Uuid=%RTuuid ParentUuid=%RTuuid uOpenFlags=%#x pVDIfsImage=%#p pVDIfsOperation=%#p\n",
1452 pDisk, pszBackend, pszFilename, uImageFlags, pszComment, pUuid, pParentUuid, uOpenFlags,
1453 pVDIfsImage, pVDIfsOperation));
1454
1455 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
1456 VDINTERFACETYPE_PROGRESS);
1457 PVDINTERFACEPROGRESS pCbProgress = NULL;
1458 if (pIfProgress)
1459 pCbProgress = VDGetInterfaceProgress(pIfProgress);
1460
1461 do
1462 {
1463 /* sanity check */
1464 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
1465 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
1466
1467 /* Check arguments. */
1468 AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
1469 ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
1470 rc = VERR_INVALID_PARAMETER);
1471 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
1472 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
1473 rc = VERR_INVALID_PARAMETER);
1474 AssertMsgBreakStmt((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0,
1475 ("uImageFlags=%#x\n", uImageFlags),
1476 rc = VERR_INVALID_PARAMETER);
1477 /* The UUID may be NULL. */
1478 AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
1479 ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
1480 rc = VERR_INVALID_PARAMETER);
1481 /* The parent UUID may be NULL. */
1482 AssertMsgBreakStmt(pParentUuid == NULL || VALID_PTR(pParentUuid),
1483 ("pParentUuid=%#p ParentUUID=%RTuuid\n", pParentUuid, pParentUuid),
1484 rc = VERR_INVALID_PARAMETER);
1485 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
1486 ("uOpenFlags=%#x\n", uOpenFlags),
1487 rc = VERR_INVALID_PARAMETER);
1488
1489 /* Check state. */
1490 AssertMsgBreakStmt(pDisk->cImages != 0,
1491 ("Create diff image cannot be done without other images open\n"),
1492 rc = VERR_VD_INVALID_STATE);
1493
1494 /* Set up image descriptor. */
1495 pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
1496 if (!pImage)
1497 {
1498 rc = VERR_NO_MEMORY;
1499 break;
1500 }
1501 pImage->pszFilename = RTStrDup(pszFilename);
1502 if (!pImage->pszFilename)
1503 {
1504 rc = VERR_NO_MEMORY;
1505 break;
1506 }
1507
1508 rc = vdFindBackend(pszBackend, &pImage->Backend);
1509 if (RT_FAILURE(rc))
1510 break;
1511 if (!pImage->Backend)
1512 {
1513 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
1514 N_("VD: unknown backend name '%s'"), pszBackend);
1515 break;
1516 }
1517
1518 /* Create UUID if the caller didn't specify one. */
1519 if (!pUuid)
1520 {
1521 rc = RTUuidCreate(&uuid);
1522 if (RT_FAILURE(rc))
1523 {
1524 rc = vdError(pDisk, rc, RT_SRC_POS,
1525 N_("VD: cannot generate UUID for image '%s'"),
1526 pszFilename);
1527 break;
1528 }
1529 pUuid = &uuid;
1530 }
1531
1532 pImage->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
1533 rc = pImage->Backend->pfnCreate(pImage->pszFilename, pDisk->cbSize,
1534 uImageFlags, pszComment,
1535 &pDisk->PCHSGeometry,
1536 &pDisk->LCHSGeometry, pUuid,
1537 uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
1538 0, 99,
1539 pDisk->pVDIfsDisk,
1540 pImage->pVDIfsImage,
1541 pVDIfsOperation,
1542 &pImage->pvBackendData);
1543
1544 if (RT_SUCCESS(rc) && pDisk->cImages != 0)
1545 {
1546 pImage->uImageFlags |= VD_IMAGE_FLAGS_DIFF;
1547
1548 /* Switch previous image to read-only mode. */
1549 unsigned uOpenFlagsPrevImg;
1550 uOpenFlagsPrevImg = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pvBackendData);
1551 if (!(uOpenFlagsPrevImg & VD_OPEN_FLAGS_READONLY))
1552 {
1553 uOpenFlagsPrevImg |= VD_OPEN_FLAGS_READONLY;
1554 rc = pDisk->pLast->Backend->pfnSetOpenFlags(pDisk->pLast->pvBackendData, uOpenFlagsPrevImg);
1555 }
1556 }
1557
1558 if (RT_SUCCESS(rc))
1559 {
1560 RTUUID Uuid;
1561 RTTIMESPEC ts;
1562 int rc2;
1563
1564 if (pParentUuid && !RTUuidIsNull(pParentUuid))
1565 {
1566 Uuid = *pParentUuid;
1567 pImage->Backend->pfnSetParentUuid(pImage->pvBackendData, &Uuid);
1568 }
1569 else
1570 {
1571 rc2 = pDisk->pLast->Backend->pfnGetUuid(pDisk->pLast->pvBackendData,
1572 &Uuid);
1573 if (RT_SUCCESS(rc2))
1574 pImage->Backend->pfnSetParentUuid(pImage->pvBackendData, &Uuid);
1575 }
1576 rc2 = pDisk->pLast->Backend->pfnGetModificationUuid(pDisk->pLast->pvBackendData,
1577 &Uuid);
1578 if (RT_SUCCESS(rc2))
1579 pImage->Backend->pfnSetParentModificationUuid(pImage->pvBackendData,
1580 &Uuid);
1581 rc2 = pDisk->pLast->Backend->pfnGetTimeStamp(pDisk->pLast->pvBackendData,
1582 &ts);
1583 if (RT_SUCCESS(rc2))
1584 pImage->Backend->pfnSetParentTimeStamp(pImage->pvBackendData, &ts);
1585
1586 rc2 = pImage->Backend->pfnSetParentFilename(pImage->pvBackendData, pDisk->pLast->pszFilename);
1587 }
1588
1589 if (RT_SUCCESS(rc))
1590 {
1591 /** @todo optionally check UUIDs */
1592 }
1593
1594 if (RT_SUCCESS(rc))
1595 {
1596 /* Image successfully opened, make it the last image. */
1597 vdAddImageToList(pDisk, pImage);
1598 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
1599 pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
1600 }
1601 else
1602 {
1603 /* Error detected, but image opened. Close and delete image. */
1604 int rc2;
1605 rc2 = pImage->Backend->pfnClose(pImage->pvBackendData, true);
1606 AssertRC(rc2);
1607 pImage->pvBackendData = NULL;
1608 }
1609 } while (0);
1610
1611 if (RT_FAILURE(rc))
1612 {
1613 if (pImage)
1614 {
1615 if (pImage->pszFilename)
1616 RTStrFree(pImage->pszFilename);
1617 RTMemFree(pImage);
1618 }
1619 }
1620
1621 if (RT_SUCCESS(rc) && pCbProgress && pCbProgress->pfnProgress)
1622 pCbProgress->pfnProgress(NULL /* WARNING! pVM=NULL */, 100,
1623 pIfProgress->pvUser);
1624
1625 LogFlowFunc(("returns %Rrc\n", rc));
1626 return rc;
1627}
1628
1629/**
1630 * Merges two images (not necessarily with direct parent/child relationship).
1631 * As a side effect the source image and potentially the other images which
1632 * are also merged to the destination are deleted from both the disk and the
1633 * images in the HDD container.
1634 *
1635 * @returns VBox status code.
1636 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
1637 * @param pDisk Pointer to HDD container.
1638 * @param nImageFrom Name of the image file to merge from.
1639 * @param nImageTo Name of the image file to merge to.
1640 * @param pVDIfsOperation Pointer to the per-operation VD interface list.
1641 */
1642VBOXDDU_DECL(int) VDMerge(PVBOXHDD pDisk, unsigned nImageFrom,
1643 unsigned nImageTo, PVDINTERFACE pVDIfsOperation)
1644{
1645 int rc = VINF_SUCCESS;
1646 void *pvBuf = NULL;
1647
1648 LogFlowFunc(("pDisk=%#p nImageFrom=%u nImageTo=%u pVDIfsOperation=%#p\n",
1649 pDisk, nImageFrom, nImageTo, pVDIfsOperation));
1650
1651 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
1652 VDINTERFACETYPE_PROGRESS);
1653 PVDINTERFACEPROGRESS pCbProgress = NULL;
1654 if (pIfProgress)
1655 pCbProgress = VDGetInterfaceProgress(pIfProgress);
1656
1657 do
1658 {
1659 /* sanity check */
1660 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
1661 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
1662
1663 PVDIMAGE pImageFrom = vdGetImageByNumber(pDisk, nImageFrom);
1664 PVDIMAGE pImageTo = vdGetImageByNumber(pDisk, nImageTo);
1665 if (!pImageFrom || !pImageTo)
1666 {
1667 rc = VERR_VD_IMAGE_NOT_FOUND;
1668 break;
1669 }
1670 AssertBreakStmt(pImageFrom != pImageTo, rc = VERR_INVALID_PARAMETER);
1671
1672 /* Make sure destination image is writable. */
1673 unsigned uOpenFlags = pImageTo->Backend->pfnGetOpenFlags(pImageTo->pvBackendData);
1674 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
1675 {
1676 uOpenFlags &= ~VD_OPEN_FLAGS_READONLY;
1677 rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pvBackendData,
1678 uOpenFlags);
1679 if (RT_FAILURE(rc))
1680 break;
1681 }
1682
1683 /* Get size of destination image. */
1684 uint64_t cbSize = pImageTo->Backend->pfnGetSize(pImageTo->pvBackendData);
1685
1686 /* Allocate tmp buffer. */
1687 pvBuf = RTMemTmpAlloc(VD_MERGE_BUFFER_SIZE);
1688 if (!pvBuf)
1689 {
1690 rc = VERR_NO_MEMORY;
1691 break;
1692 }
1693
1694 /* Merging is done directly on the images itself. This potentially
1695 * causes trouble if the disk is full in the middle of operation. */
1696 /** @todo write alternative implementation which works with temporary
1697 * images (which is safer, but requires even more space). Also has the
1698 * drawback that merging into a raw disk parent simply isn't possible
1699 * this way (but in that case disk full isn't really a problem). */
1700 if (nImageFrom < nImageTo)
1701 {
1702 /* Merge parent state into child. This means writing all not
1703 * allocated blocks in the destination image which are allocated in
1704 * the images to be merged. */
1705 uint64_t uOffset = 0;
1706 uint64_t cbRemaining = cbSize;
1707 do
1708 {
1709 size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
1710 rc = pImageTo->Backend->pfnRead(pImageTo->pvBackendData,
1711 uOffset, pvBuf, cbThisRead,
1712 &cbThisRead);
1713 if (rc == VERR_VD_BLOCK_FREE)
1714 {
1715 /* Search for image with allocated block. Do not attempt to
1716 * read more than the previous reads marked as valid.
1717 * Otherwise this would return stale data when different
1718 * block sizes are used for the images. */
1719 for (PVDIMAGE pCurrImage = pImageTo->pPrev;
1720 pCurrImage != NULL && pCurrImage != pImageFrom->pPrev && rc == VERR_VD_BLOCK_FREE;
1721 pCurrImage = pCurrImage->pPrev)
1722 {
1723 rc = pCurrImage->Backend->pfnRead(pCurrImage->pvBackendData,
1724 uOffset, pvBuf,
1725 cbThisRead,
1726 &cbThisRead);
1727 }
1728
1729 if (rc != VERR_VD_BLOCK_FREE)
1730 {
1731 if (RT_FAILURE(rc))
1732 break;
1733 rc = vdWriteHelper(pDisk, pImageTo, uOffset, pvBuf,
1734 cbThisRead);
1735 if (RT_FAILURE(rc))
1736 break;
1737 }
1738 else
1739 rc = VINF_SUCCESS;
1740 }
1741 else if (RT_FAILURE(rc))
1742 break;
1743
1744 uOffset += cbThisRead;
1745 cbRemaining -= cbThisRead;
1746
1747 if (pCbProgress && pCbProgress->pfnProgress)
1748 {
1749 rc = pCbProgress->pfnProgress(NULL /* WARNING! pVM=NULL */,
1750 uOffset * 99 / cbSize,
1751 pIfProgress->pvUser);
1752 if (RT_FAILURE(rc))
1753 break;
1754 }
1755 } while (uOffset < cbSize);
1756 }
1757 else
1758 {
1759 /* Merge child state into parent. This means writing all blocks
1760 * which are allocated in the image up to the source image to the
1761 * destination image. */
1762 uint64_t uOffset = 0;
1763 uint64_t cbRemaining = cbSize;
1764 do
1765 {
1766 size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
1767 rc = VERR_VD_BLOCK_FREE;
1768 /* Search for image with allocated block. Do not attempt to
1769 * read more than the previous reads marked as valid. Otherwise
1770 * this would return stale data when different block sizes are
1771 * used for the images. */
1772 for (PVDIMAGE pCurrImage = pImageFrom;
1773 pCurrImage != NULL && pCurrImage != pImageTo && rc == VERR_VD_BLOCK_FREE;
1774 pCurrImage = pCurrImage->pPrev)
1775 {
1776 rc = pCurrImage->Backend->pfnRead(pCurrImage->pvBackendData,
1777 uOffset, pvBuf,
1778 cbThisRead, &cbThisRead);
1779 }
1780
1781 if (rc != VERR_VD_BLOCK_FREE)
1782 {
1783 if (RT_FAILURE(rc))
1784 break;
1785 rc = vdWriteHelper(pDisk, pImageTo, uOffset, pvBuf,
1786 cbThisRead);
1787 if (RT_FAILURE(rc))
1788 break;
1789 }
1790 else
1791 rc = VINF_SUCCESS;
1792
1793 uOffset += cbThisRead;
1794 cbRemaining -= cbThisRead;
1795
1796 if (pCbProgress && pCbProgress->pfnProgress)
1797 {
1798 rc = pCbProgress->pfnProgress(NULL /* WARNING! pVM=NULL */,
1799 uOffset * 99 / cbSize,
1800 pIfProgress->pvUser);
1801 if (RT_FAILURE(rc))
1802 break;
1803 }
1804 } while (uOffset < cbSize);
1805 }
1806
1807 /* Update parent UUID so that image chain is consistent. */
1808 RTUUID Uuid;
1809 if (nImageFrom < nImageTo)
1810 {
1811 if (pImageTo->pPrev)
1812 {
1813 rc = pImageTo->Backend->pfnGetUuid(pImageTo->pPrev->pvBackendData,
1814 &Uuid);
1815 AssertRC(rc);
1816 }
1817 else
1818 RTUuidClear(&Uuid);
1819 rc = pImageTo->Backend->pfnSetParentUuid(pImageTo->pvBackendData,
1820 &Uuid);
1821 AssertRC(rc);
1822 }
1823 else
1824 {
1825 if (pImageFrom->pNext)
1826 {
1827 rc = pImageTo->Backend->pfnGetUuid(pImageTo->pvBackendData,
1828 &Uuid);
1829 AssertRC(rc);
1830 rc = pImageFrom->Backend->pfnSetParentUuid(pImageFrom->pNext,
1831 &Uuid);
1832 AssertRC(rc);
1833 }
1834 }
1835
1836 /* Make sure destination image is back to read only if necessary. */
1837 if (pImageTo != pDisk->pLast && pImageFrom != pDisk->pLast)
1838 {
1839 uOpenFlags = pImageTo->Backend->pfnGetOpenFlags(pImageTo->pvBackendData);
1840 uOpenFlags |= VD_OPEN_FLAGS_READONLY;
1841 rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pvBackendData,
1842 uOpenFlags);
1843 if (RT_FAILURE(rc))
1844 break;
1845 }
1846
1847 /* Delete the no longer needed images. */
1848 PVDIMAGE pImg = pImageFrom, pTmp;
1849 while (pImg != pImageTo)
1850 {
1851 if (nImageFrom < nImageTo)
1852 pTmp = pImg->pNext;
1853 else
1854 pTmp = pImg->pPrev;
1855 vdRemoveImageFromList(pDisk, pImg);
1856 pImg->Backend->pfnClose(pImg->pvBackendData, true);
1857 RTMemFree(pImg->pszFilename);
1858 RTMemFree(pImg);
1859 pImg = pTmp;
1860 }
1861 } while (0);
1862
1863 if (pvBuf)
1864 RTMemTmpFree(pvBuf);
1865
1866 if (RT_SUCCESS(rc) && pCbProgress && pCbProgress->pfnProgress)
1867 pCbProgress->pfnProgress(NULL /* WARNING! pVM=NULL */, 100,
1868 pIfProgress->pvUser);
1869
1870 LogFlowFunc(("returns %Rrc\n", rc));
1871 return rc;
1872}
1873
1874/**
1875 * Copies an image from one HDD container to another.
1876 * The copy is opened in the target HDD container.
1877 * It is possible to convert between different image formats, because the
1878 * backend for the destination may be different from the source.
1879 * If both the source and destination reference the same HDD container,
1880 * then the image is moved (by copying/deleting or renaming) to the new location.
1881 * The source container is unchanged if the move operation fails, otherwise
1882 * the image at the new location is opened in the same way as the old one was.
1883 *
1884 * @returns VBox status code.
1885 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
1886 * @param pDiskFrom Pointer to source HDD container.
1887 * @param nImage Image number, counts from 0. 0 is always base image of container.
1888 * @param pDiskTo Pointer to destination HDD container.
1889 * @param pszBackend Name of the image file backend to use.
1890 * @param pszFilename New name of the image (may be NULL if pDiskFrom == pDiskTo).
1891 * @param fMoveByRename If true, attempt to perform a move by renaming (if successful the new size is ignored).
1892 * @param cbSize New image size (0 means leave unchanged).
1893 * @param uImageFlags Flags specifying special destination image features.
1894 * @param pDstUuid New UUID of the destination image. If NULL, a new UUID is created.
1895 * This parameter is used if and only if a true copy is created.
1896 * In all rename/move cases the UUIDs are copied over.
1897 * @param pVDIfsOperation Pointer to the per-operation VD interface list.
1898 * @param pDstVDIfsImage Pointer to the per-image VD interface list, for the
1899 * destination image.
1900 * @param pDstVDIfsOperation Pointer to the per-image VD interface list,
1901 * for the destination image.
1902 */
1903VBOXDDU_DECL(int) VDCopy(PVBOXHDD pDiskFrom, unsigned nImage, PVBOXHDD pDiskTo,
1904 const char *pszBackend, const char *pszFilename,
1905 bool fMoveByRename, uint64_t cbSize,
1906 unsigned uImageFlags, PCRTUUID pDstUuid,
1907 PVDINTERFACE pVDIfsOperation,
1908 PVDINTERFACE pDstVDIfsImage,
1909 PVDINTERFACE pDstVDIfsOperation)
1910{
1911 int rc, rc2 = VINF_SUCCESS;
1912 void *pvBuf = NULL;
1913 PVDIMAGE pImageTo = NULL;
1914
1915 LogFlowFunc(("pDiskFrom=%#p nImage=%u pDiskTo=%#p pszBackend=\"%s\" pszFilename=\"%s\" fMoveByRename=%d cbSize=%llu pVDIfsOperation=%#p pDstVDIfsImage=%#p pDstVDIfsOperation=%#p\n",
1916 pDiskFrom, nImage, pDiskTo, pszBackend, pszFilename, fMoveByRename, cbSize, pVDIfsOperation, pDstVDIfsImage, pDstVDIfsOperation));
1917
1918 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
1919 VDINTERFACETYPE_PROGRESS);
1920 PVDINTERFACEPROGRESS pCbProgress = NULL;
1921 if (pIfProgress)
1922 pCbProgress = VDGetInterfaceProgress(pIfProgress);
1923
1924 PVDINTERFACE pDstIfProgress = VDInterfaceGet(pDstVDIfsOperation,
1925 VDINTERFACETYPE_PROGRESS);
1926 PVDINTERFACEPROGRESS pDstCbProgress = NULL;
1927 if (pDstIfProgress)
1928 pDstCbProgress = VDGetInterfaceProgress(pDstIfProgress);
1929
1930 do {
1931 /* Check arguments. */
1932 AssertMsgBreakStmt(VALID_PTR(pDiskFrom), ("pDiskFrom=%#p\n", pDiskFrom),
1933 rc = VERR_INVALID_PARAMETER);
1934 AssertMsg(pDiskFrom->u32Signature == VBOXHDDDISK_SIGNATURE,
1935 ("u32Signature=%08x\n", pDiskFrom->u32Signature));
1936
1937 PVDIMAGE pImageFrom = vdGetImageByNumber(pDiskFrom, nImage);
1938 AssertPtrBreakStmt(pImageFrom, rc = VERR_VD_IMAGE_NOT_FOUND);
1939 AssertMsgBreakStmt(VALID_PTR(pDiskTo), ("pDiskTo=%#p\n", pDiskTo),
1940 rc = VERR_INVALID_PARAMETER);
1941 AssertMsg(pDiskTo->u32Signature == VBOXHDDDISK_SIGNATURE,
1942 ("u32Signature=%08x\n", pDiskTo->u32Signature));
1943
1944 /* Move the image. */
1945 if (pDiskFrom == pDiskTo)
1946 {
1947 /* Rename only works when backends are the same. */
1948 if ( fMoveByRename
1949 && !RTStrICmp(pszBackend, pImageFrom->Backend->pszBackendName))
1950 {
1951 rc = pImageFrom->Backend->pfnRename(pImageFrom->pvBackendData, pszFilename ? pszFilename : pImageFrom->pszFilename);
1952 break;
1953 }
1954
1955 /** @todo Moving (including shrinking/growing) of the image is
1956 * requested, but the rename attempt failed or it wasn't possible.
1957 * Must now copy image to temp location. */
1958 AssertReleaseMsgFailed(("VDCopy: moving by copy/delete not implemented\n"));
1959 }
1960
1961 /* When moving an image pszFilename is allowed to be NULL, so do the parameter check here. */
1962 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
1963 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
1964 rc = VERR_INVALID_PARAMETER);
1965
1966 uint64_t cbSizeFrom;
1967 cbSizeFrom = pImageFrom->Backend->pfnGetSize(pImageFrom->pvBackendData);
1968 if (cbSizeFrom == 0)
1969 {
1970 rc = VERR_VD_VALUE_NOT_FOUND;
1971 break;
1972 }
1973
1974 if (cbSize == 0)
1975 cbSize = cbSizeFrom;
1976
1977 PDMMEDIAGEOMETRY PCHSGeometryFrom = {0, 0, 0};
1978 PDMMEDIAGEOMETRY LCHSGeometryFrom = {0, 0, 0};
1979 pImageFrom->Backend->pfnGetPCHSGeometry(pImageFrom->pvBackendData, &PCHSGeometryFrom);
1980 pImageFrom->Backend->pfnGetLCHSGeometry(pImageFrom->pvBackendData, &LCHSGeometryFrom);
1981
1982 RTUUID ImageUuid, ImageModificationUuid;
1983 RTUUID ParentUuid, ParentModificationUuid;
1984 if (pDiskFrom != pDiskTo)
1985 {
1986 if (pDstUuid)
1987 ImageUuid = *pDstUuid;
1988 else
1989 RTUuidCreate(&ImageUuid);
1990 }
1991 else
1992 {
1993 rc = pImageFrom->Backend->pfnGetUuid(pImageFrom->pvBackendData, &ImageUuid);
1994 if (RT_FAILURE(rc))
1995 RTUuidCreate(&ImageUuid);
1996 }
1997 rc = pImageFrom->Backend->pfnGetModificationUuid(pImageFrom->pvBackendData, &ImageModificationUuid);
1998 if (RT_FAILURE(rc))
1999 RTUuidClear(&ImageModificationUuid);
2000 rc = pImageFrom->Backend->pfnGetParentUuid(pImageFrom->pvBackendData, &ParentUuid);
2001 if (RT_FAILURE(rc))
2002 RTUuidClear(&ParentUuid);
2003 rc = pImageFrom->Backend->pfnGetParentModificationUuid(pImageFrom->pvBackendData, &ParentModificationUuid);
2004 if (RT_FAILURE(rc))
2005 RTUuidClear(&ParentModificationUuid);
2006
2007 char szComment[1024];
2008 rc = pImageFrom->Backend->pfnGetComment(pImageFrom->pvBackendData, szComment, sizeof(szComment));
2009 if (RT_FAILURE(rc))
2010 szComment[0] = '\0';
2011 else
2012 szComment[sizeof(szComment) - 1] = '\0';
2013
2014 unsigned uOpenFlagsFrom;
2015 uOpenFlagsFrom = pImageFrom->Backend->pfnGetOpenFlags(pImageFrom->pvBackendData);
2016
2017 /* Create destination image with the properties of the source image. */
2018 /** @todo replace the VDCreateDiff/VDCreateBase calls by direct
2019 * calls to the backend. Unifies the code and reduces the API
2020 * dependencies. */
2021 if (uImageFlags & VD_IMAGE_FLAGS_DIFF)
2022 {
2023 rc = VDCreateDiff(pDiskTo, pszBackend, pszFilename, uImageFlags,
2024 szComment, &ImageUuid, &ParentUuid, uOpenFlagsFrom & ~VD_OPEN_FLAGS_READONLY, NULL, NULL);
2025 } else {
2026 /** @todo Please, review this! It's an ugly hack I think... */
2027 if (!RTStrICmp(pszBackend, "RAW"))
2028 uImageFlags |= VD_IMAGE_FLAGS_FIXED;
2029
2030 rc = VDCreateBase(pDiskTo, pszBackend, pszFilename, cbSize,
2031 uImageFlags, szComment,
2032 &PCHSGeometryFrom, &LCHSGeometryFrom,
2033 NULL, uOpenFlagsFrom & ~VD_OPEN_FLAGS_READONLY, NULL, NULL);
2034 if (RT_SUCCESS(rc) && !RTUuidIsNull(&ImageUuid))
2035 pDiskTo->pLast->Backend->pfnSetUuid(pDiskTo->pLast->pvBackendData, &ImageUuid);
2036 if (RT_SUCCESS(rc) && !RTUuidIsNull(&ParentUuid))
2037 pDiskTo->pLast->Backend->pfnSetParentUuid(pDiskTo->pLast->pvBackendData, &ParentUuid);
2038 }
2039 if (RT_FAILURE(rc))
2040 break;
2041
2042 pImageTo = pDiskTo->pLast;
2043 AssertPtrBreakStmt(pImageTo, rc = VERR_VD_IMAGE_NOT_FOUND);
2044
2045 /* Allocate tmp buffer. */
2046 pvBuf = RTMemTmpAlloc(VD_MERGE_BUFFER_SIZE);
2047 if (!pvBuf)
2048 {
2049 rc = VERR_NO_MEMORY;
2050 break;
2051 }
2052
2053 /* Copy the data. */
2054 uint64_t uOffset = 0;
2055 uint64_t cbRemaining = cbSize;
2056
2057 do
2058 {
2059 size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
2060
2061 rc = vdReadHelper(pDiskFrom, pImageFrom, uOffset, pvBuf,
2062 cbThisRead);
2063 if (RT_FAILURE(rc))
2064 break;
2065
2066 rc = vdWriteHelper(pDiskTo, pImageTo, uOffset, pvBuf,
2067 cbThisRead);
2068 if (RT_FAILURE(rc))
2069 break;
2070
2071 uOffset += cbThisRead;
2072 cbRemaining -= cbThisRead;
2073
2074 if (pCbProgress && pCbProgress->pfnProgress)
2075 {
2076 rc = pCbProgress->pfnProgress(NULL /* WARNING! pVM=NULL */,
2077 uOffset * 99 / cbSize,
2078 pIfProgress->pvUser);
2079 if (RT_FAILURE(rc))
2080 break;
2081 }
2082 if (pDstCbProgress && pDstCbProgress->pfnProgress)
2083 {
2084 rc = pDstCbProgress->pfnProgress(NULL /* WARNING! pVM=NULL */,
2085 uOffset * 99 / cbSize,
2086 pDstIfProgress->pvUser);
2087 if (RT_FAILURE(rc))
2088 break;
2089 }
2090 } while (uOffset < cbSize);
2091
2092 if (RT_SUCCESS(rc))
2093 {
2094 pImageTo->Backend->pfnSetModificationUuid(pImageTo->pvBackendData, &ImageModificationUuid);
2095 pImageTo->Backend->pfnGetParentModificationUuid(pImageTo->pvBackendData, &ParentModificationUuid);
2096 }
2097 } while (0);
2098
2099 if (RT_FAILURE(rc) && pImageTo)
2100 {
2101 /* Error detected, but new image created. Remove image from list. */
2102 vdRemoveImageFromList(pDiskTo, pImageTo);
2103
2104 /* Close and delete image. */
2105 rc2 = pImageTo->Backend->pfnClose(pImageTo->pvBackendData, true);
2106 AssertRC(rc2);
2107 pImageTo->pvBackendData = NULL;
2108
2109 /* Free remaining resources. */
2110 if (pImageTo->pszFilename)
2111 RTStrFree(pImageTo->pszFilename);
2112
2113 RTMemFree(pImageTo);
2114 }
2115
2116 if (pvBuf)
2117 RTMemTmpFree(pvBuf);
2118
2119 if (RT_SUCCESS(rc))
2120 {
2121 if (pCbProgress && pCbProgress->pfnProgress)
2122 pCbProgress->pfnProgress(NULL /* WARNING! pVM=NULL */, 100,
2123 pIfProgress->pvUser);
2124 if (pDstCbProgress && pDstCbProgress->pfnProgress)
2125 pDstCbProgress->pfnProgress(NULL /* WARNING! pVM=NULL */, 100,
2126 pDstIfProgress->pvUser);
2127 }
2128
2129 LogFlowFunc(("returns %Rrc\n", rc));
2130 return rc;
2131}
2132
2133/**
2134 * Optimizes the storage consumption of an image. Typically the unused blocks
2135 * have to be wiped with zeroes to achieve a substantial reduced storage use.
2136 * Another optimization done is reordering the image blocks, which can provide
2137 * a significant performance boost, as reads and writes tend to use less random
2138 * file offsets.
2139 *
2140 * @return VBox status code.
2141 * @return VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
2142 * @return VERR_VD_IMAGE_READ_ONLY if image is not writable.
2143 * @return VERR_NOT_SUPPORTED if this kind of image can be compacted, but
2144 * the code for this isn't implemented yet.
2145 * @param pDisk Pointer to HDD container.
2146 * @param nImage Image number, counts from 0. 0 is always base image of container.
2147 * @param pVDIfsOperation Pointer to the per-operation VD interface list.
2148 */
2149VBOXDDU_DECL(int) VDCompact(PVBOXHDD pDisk, unsigned nImage,
2150 PVDINTERFACE pVDIfsOperation)
2151{
2152 int rc;
2153 void *pvBuf = NULL;
2154 void *pvTmp = NULL;
2155
2156 LogFlowFunc(("pDisk=%#p nImage=%u pVDIfsOperation=%#p\n",
2157 pDisk, nImage, pVDIfsOperation));
2158
2159 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
2160 VDINTERFACETYPE_PROGRESS);
2161 PVDINTERFACEPROGRESS pCbProgress = NULL;
2162 if (pIfProgress)
2163 pCbProgress = VDGetInterfaceProgress(pIfProgress);
2164
2165 do {
2166 /* Check arguments. */
2167 AssertMsgBreakStmt(VALID_PTR(pDisk), ("pDisk=%#p\n", pDisk),
2168 rc = VERR_INVALID_PARAMETER);
2169 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE,
2170 ("u32Signature=%08x\n", pDisk->u32Signature));
2171
2172 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
2173 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
2174
2175 /* If there is no compact callback for not file based backends then
2176 * the backend doesn't need compaction. No need to make much fuss about
2177 * this. For file based ones signal this as not yet supported. */
2178 if (!pImage->Backend->pfnCompact)
2179 {
2180 if (pImage->Backend->uBackendCaps & VD_CAP_FILE)
2181 rc = VERR_NOT_SUPPORTED;
2182 else
2183 rc = VINF_SUCCESS;
2184 break;
2185 }
2186
2187 /* Insert interface for reading parent state into per-operation list,
2188 * if there is a parent image. */
2189 VDINTERFACE IfOpParent;
2190 VDINTERFACEPARENTSTATE ParentCb;
2191 VDPARENTSTATEDESC ParentUser;
2192 if (pImage->pPrev)
2193 {
2194 ParentCb.cbSize = sizeof(ParentCb);
2195 ParentCb.enmInterface = VDINTERFACETYPE_PARENTSTATE;
2196 ParentCb.pfnParentRead = vdParentRead;
2197 ParentUser.pDisk = pDisk;
2198 ParentUser.pImage = pImage->pPrev;
2199 rc = VDInterfaceAdd(&IfOpParent, "VDCompact_ParentState", VDINTERFACETYPE_PARENTSTATE,
2200 &ParentCb, &ParentUser, &pVDIfsOperation);
2201 AssertRC(rc);
2202 }
2203
2204 rc = pImage->Backend->pfnCompact(pImage->pvBackendData,
2205 0, 99,
2206 pVDIfsOperation);
2207 } while (0);
2208
2209 if (pvBuf)
2210 RTMemTmpFree(pvBuf);
2211 if (pvTmp)
2212 RTMemTmpFree(pvTmp);
2213
2214 if (RT_SUCCESS(rc))
2215 {
2216 if (pCbProgress && pCbProgress->pfnProgress)
2217 pCbProgress->pfnProgress(NULL /* WARNING! pVM=NULL */, 100,
2218 pIfProgress->pvUser);
2219 }
2220
2221 LogFlowFunc(("returns %Rrc\n", rc));
2222 return rc;
2223}
2224
2225/**
2226 * Closes the last opened image file in HDD container.
2227 * If previous image file was opened in read-only mode (that is normal) and closing image
2228 * was opened in read-write mode (the whole disk was in read-write mode) - the previous image
2229 * will be reopened in read/write mode.
2230 *
2231 * @returns VBox status code.
2232 * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
2233 * @param pDisk Pointer to HDD container.
2234 * @param fDelete If true, delete the image from the host disk.
2235 */
2236VBOXDDU_DECL(int) VDClose(PVBOXHDD pDisk, bool fDelete)
2237{
2238 int rc = VINF_SUCCESS;
2239
2240 LogFlowFunc(("pDisk=%#p fDelete=%d\n", pDisk, fDelete));
2241 do
2242 {
2243 /* sanity check */
2244 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2245 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2246
2247 PVDIMAGE pImage = pDisk->pLast;
2248 if (!pImage)
2249 {
2250 rc = VERR_VD_NOT_OPENED;
2251 break;
2252 }
2253 unsigned uOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pvBackendData);
2254 /* Remove image from list of opened images. */
2255 vdRemoveImageFromList(pDisk, pImage);
2256 /* Close (and optionally delete) image. */
2257 rc = pImage->Backend->pfnClose(pImage->pvBackendData, fDelete);
2258 /* Free remaining resources related to the image. */
2259 RTStrFree(pImage->pszFilename);
2260 RTMemFree(pImage);
2261
2262 pImage = pDisk->pLast;
2263 if (!pImage)
2264 break;
2265
2266 /* If disk was previously in read/write mode, make sure it will stay
2267 * like this (if possible) after closing this image. Set the open flags
2268 * accordingly. */
2269 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
2270 {
2271 uOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pvBackendData);
2272 uOpenFlags &= ~ VD_OPEN_FLAGS_READONLY;
2273 rc = pImage->Backend->pfnSetOpenFlags(pImage->pvBackendData, uOpenFlags);
2274 }
2275
2276 int rc2;
2277
2278 /* Cache disk information. */
2279 pDisk->cbSize = pImage->Backend->pfnGetSize(pImage->pvBackendData);
2280
2281 /* Cache PCHS geometry. */
2282 rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pvBackendData,
2283 &pDisk->PCHSGeometry);
2284 if (RT_FAILURE(rc2))
2285 {
2286 pDisk->PCHSGeometry.cCylinders = 0;
2287 pDisk->PCHSGeometry.cHeads = 0;
2288 pDisk->PCHSGeometry.cSectors = 0;
2289 }
2290 else
2291 {
2292 /* Make sure the PCHS geometry is properly clipped. */
2293 pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
2294 pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
2295 pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
2296 }
2297
2298 /* Cache LCHS geometry. */
2299 rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pvBackendData,
2300 &pDisk->LCHSGeometry);
2301 if (RT_FAILURE(rc2))
2302 {
2303 pDisk->LCHSGeometry.cCylinders = 0;
2304 pDisk->LCHSGeometry.cHeads = 0;
2305 pDisk->LCHSGeometry.cSectors = 0;
2306 }
2307 else
2308 {
2309 /* Make sure the LCHS geometry is properly clipped. */
2310 pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
2311 pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
2312 }
2313 } while (0);
2314
2315 LogFlowFunc(("returns %Rrc\n", rc));
2316 return rc;
2317}
2318
2319/**
2320 * Closes all opened image files in HDD container.
2321 *
2322 * @returns VBox status code.
2323 * @param pDisk Pointer to HDD container.
2324 */
2325VBOXDDU_DECL(int) VDCloseAll(PVBOXHDD pDisk)
2326{
2327 int rc = VINF_SUCCESS;
2328
2329 LogFlowFunc(("pDisk=%#p\n", pDisk));
2330 do
2331 {
2332 /* sanity check */
2333 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2334 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2335
2336 PVDIMAGE pImage = pDisk->pLast;
2337 while (VALID_PTR(pImage))
2338 {
2339 PVDIMAGE pPrev = pImage->pPrev;
2340 /* Remove image from list of opened images. */
2341 vdRemoveImageFromList(pDisk, pImage);
2342 /* Close image. */
2343 int rc2 = pImage->Backend->pfnClose(pImage->pvBackendData, false);
2344 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
2345 rc = rc2;
2346 /* Free remaining resources related to the image. */
2347 RTStrFree(pImage->pszFilename);
2348 RTMemFree(pImage);
2349 pImage = pPrev;
2350 }
2351 Assert(!VALID_PTR(pDisk->pLast));
2352 } while (0);
2353
2354 LogFlowFunc(("returns %Rrc\n", rc));
2355 return rc;
2356}
2357
2358/**
2359 * Read data from virtual HDD.
2360 *
2361 * @returns VBox status code.
2362 * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
2363 * @param pDisk Pointer to HDD container.
2364 * @param uOffset Offset of first reading byte from start of disk.
2365 * @param pvBuf Pointer to buffer for reading data.
2366 * @param cbRead Number of bytes to read.
2367 */
2368VBOXDDU_DECL(int) VDRead(PVBOXHDD pDisk, uint64_t uOffset, void *pvBuf,
2369 size_t cbRead)
2370{
2371 int rc;
2372
2373 LogFlowFunc(("pDisk=%#p uOffset=%llu pvBuf=%p cbRead=%zu\n",
2374 pDisk, uOffset, pvBuf, cbRead));
2375 do
2376 {
2377 /* sanity check */
2378 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2379 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2380
2381 /* Check arguments. */
2382 AssertMsgBreakStmt(VALID_PTR(pvBuf),
2383 ("pvBuf=%#p\n", pvBuf),
2384 rc = VERR_INVALID_PARAMETER);
2385 AssertMsgBreakStmt(cbRead,
2386 ("cbRead=%zu\n", cbRead),
2387 rc = VERR_INVALID_PARAMETER);
2388 AssertMsgBreakStmt(uOffset + cbRead <= pDisk->cbSize,
2389 ("uOffset=%llu cbRead=%zu pDisk->cbSize=%llu\n",
2390 uOffset, cbRead, pDisk->cbSize),
2391 rc = VERR_INVALID_PARAMETER);
2392
2393 PVDIMAGE pImage = pDisk->pLast;
2394 AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
2395
2396 rc = vdReadHelper(pDisk, pImage, uOffset, pvBuf, cbRead);
2397 } while (0);
2398
2399 LogFlowFunc(("returns %Rrc\n", rc));
2400 return rc;
2401}
2402
2403/**
2404 * Write data to virtual HDD.
2405 *
2406 * @returns VBox status code.
2407 * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
2408 * @param pDisk Pointer to HDD container.
2409 * @param uOffset Offset of the first byte being
2410 * written from start of disk.
2411 * @param pvBuf Pointer to buffer for writing data.
2412 * @param cbWrite Number of bytes to write.
2413 */
2414VBOXDDU_DECL(int) VDWrite(PVBOXHDD pDisk, uint64_t uOffset, const void *pvBuf,
2415 size_t cbWrite)
2416{
2417 int rc = VINF_SUCCESS;
2418
2419 LogFlowFunc(("pDisk=%#p uOffset=%llu pvBuf=%p cbWrite=%zu\n",
2420 pDisk, uOffset, pvBuf, cbWrite));
2421 do
2422 {
2423 /* sanity check */
2424 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2425 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2426
2427 /* Check arguments. */
2428 AssertMsgBreakStmt(VALID_PTR(pvBuf),
2429 ("pvBuf=%#p\n", pvBuf),
2430 rc = VERR_INVALID_PARAMETER);
2431 AssertMsgBreakStmt(cbWrite,
2432 ("cbWrite=%zu\n", cbWrite),
2433 rc = VERR_INVALID_PARAMETER);
2434 AssertMsgBreakStmt(uOffset + cbWrite <= pDisk->cbSize,
2435 ("uOffset=%llu cbWrite=%zu pDisk->cbSize=%llu\n",
2436 uOffset, cbWrite, pDisk->cbSize),
2437 rc = VERR_INVALID_PARAMETER);
2438
2439 PVDIMAGE pImage = pDisk->pLast;
2440 AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
2441
2442 vdSetModifiedFlag(pDisk);
2443 rc = vdWriteHelper(pDisk, pImage, uOffset, pvBuf, cbWrite);
2444 } while (0);
2445
2446 LogFlowFunc(("returns %Rrc\n", rc));
2447 return rc;
2448}
2449
2450/**
2451 * Make sure the on disk representation of a virtual HDD is up to date.
2452 *
2453 * @returns VBox status code.
2454 * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
2455 * @param pDisk Pointer to HDD container.
2456 */
2457VBOXDDU_DECL(int) VDFlush(PVBOXHDD pDisk)
2458{
2459 int rc = VINF_SUCCESS;
2460
2461 LogFlowFunc(("pDisk=%#p\n", pDisk));
2462 do
2463 {
2464 /* sanity check */
2465 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2466 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2467
2468 PVDIMAGE pImage = pDisk->pLast;
2469 AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
2470
2471 vdResetModifiedFlag(pDisk);
2472 rc = pImage->Backend->pfnFlush(pImage->pvBackendData);
2473 } while (0);
2474
2475 LogFlowFunc(("returns %Rrc\n", rc));
2476 return rc;
2477}
2478
2479/**
2480 * Get number of opened images in HDD container.
2481 *
2482 * @returns Number of opened images for HDD container. 0 if no images have been opened.
2483 * @param pDisk Pointer to HDD container.
2484 */
2485VBOXDDU_DECL(unsigned) VDGetCount(PVBOXHDD pDisk)
2486{
2487 unsigned cImages;
2488
2489 LogFlowFunc(("pDisk=%#p\n", pDisk));
2490 do
2491 {
2492 /* sanity check */
2493 AssertPtrBreakStmt(pDisk, cImages = 0);
2494 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2495
2496 cImages = pDisk->cImages;
2497 } while (0);
2498
2499 LogFlowFunc(("returns %u\n", cImages));
2500 return cImages;
2501}
2502
2503/**
2504 * Get read/write mode of HDD container.
2505 *
2506 * @returns Virtual disk ReadOnly status.
2507 * @returns true if no image is opened in HDD container.
2508 * @param pDisk Pointer to HDD container.
2509 */
2510VBOXDDU_DECL(bool) VDIsReadOnly(PVBOXHDD pDisk)
2511{
2512 bool fReadOnly;
2513
2514 LogFlowFunc(("pDisk=%#p\n", pDisk));
2515 do
2516 {
2517 /* sanity check */
2518 AssertPtrBreakStmt(pDisk, fReadOnly = false);
2519 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2520
2521 PVDIMAGE pImage = pDisk->pLast;
2522 AssertPtrBreakStmt(pImage, fReadOnly = true);
2523
2524 unsigned uOpenFlags;
2525 uOpenFlags = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pvBackendData);
2526 fReadOnly = !!(uOpenFlags & VD_OPEN_FLAGS_READONLY);
2527 } while (0);
2528
2529 LogFlowFunc(("returns %d\n", fReadOnly));
2530 return fReadOnly;
2531}
2532
2533/**
2534 * Get total capacity of an image in HDD container.
2535 *
2536 * @returns Virtual disk size in bytes.
2537 * @returns 0 if no image with specified number was not opened.
2538 * @param pDisk Pointer to HDD container.
2539 * @param nImage Image number, counds from 0. 0 is always base image of container.
2540 */
2541VBOXDDU_DECL(uint64_t) VDGetSize(PVBOXHDD pDisk, unsigned nImage)
2542{
2543 uint64_t cbSize;
2544
2545 LogFlowFunc(("pDisk=%#p nImage=%u\n", pDisk, nImage));
2546 do
2547 {
2548 /* sanity check */
2549 AssertPtrBreakStmt(pDisk, cbSize = 0);
2550 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2551
2552 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
2553 AssertPtrBreakStmt(pImage, cbSize = 0);
2554 cbSize = pImage->Backend->pfnGetSize(pImage->pvBackendData);
2555 } while (0);
2556
2557 LogFlowFunc(("returns %llu\n", cbSize));
2558 return cbSize;
2559}
2560
2561/**
2562 * Get total file size of an image in HDD container.
2563 *
2564 * @returns Virtual disk size in bytes.
2565 * @returns 0 if no image is opened in HDD container.
2566 * @param pDisk Pointer to HDD container.
2567 * @param nImage Image number, counts from 0. 0 is always base image of container.
2568 */
2569VBOXDDU_DECL(uint64_t) VDGetFileSize(PVBOXHDD pDisk, unsigned nImage)
2570{
2571 uint64_t cbSize;
2572
2573 LogFlowFunc(("pDisk=%#p nImage=%u\n", pDisk, nImage));
2574 do
2575 {
2576 /* sanity check */
2577 AssertPtrBreakStmt(pDisk, cbSize = 0);
2578 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2579
2580 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
2581 AssertPtrBreakStmt(pImage, cbSize = 0);
2582 cbSize = pImage->Backend->pfnGetFileSize(pImage->pvBackendData);
2583 } while (0);
2584
2585 LogFlowFunc(("returns %llu\n", cbSize));
2586 return cbSize;
2587}
2588
2589/**
2590 * Get virtual disk PCHS geometry stored in HDD container.
2591 *
2592 * @returns VBox status code.
2593 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
2594 * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
2595 * @param pDisk Pointer to HDD container.
2596 * @param nImage Image number, counts from 0. 0 is always base image of container.
2597 * @param pPCHSGeometry Where to store PCHS geometry. Not NULL.
2598 */
2599VBOXDDU_DECL(int) VDGetPCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
2600 PPDMMEDIAGEOMETRY pPCHSGeometry)
2601{
2602 int rc = VINF_SUCCESS;
2603
2604 LogFlowFunc(("pDisk=%#p nImage=%u pPCHSGeometry=%#p\n",
2605 pDisk, nImage, pPCHSGeometry));
2606 do
2607 {
2608 /* sanity check */
2609 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2610 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2611
2612 /* Check arguments. */
2613 AssertMsgBreakStmt(VALID_PTR(pPCHSGeometry),
2614 ("pPCHSGeometry=%#p\n", pPCHSGeometry),
2615 rc = VERR_INVALID_PARAMETER);
2616
2617 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
2618 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
2619
2620 if (pImage == pDisk->pLast)
2621 {
2622 /* Use cached information if possible. */
2623 if (pDisk->PCHSGeometry.cCylinders != 0)
2624 *pPCHSGeometry = pDisk->PCHSGeometry;
2625 else
2626 rc = VERR_VD_GEOMETRY_NOT_SET;
2627 }
2628 else
2629 rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pvBackendData,
2630 pPCHSGeometry);
2631 } while (0);
2632
2633 LogFlowFunc(("%s: %Rrc (PCHS=%u/%u/%u)\n", __FUNCTION__, rc,
2634 pDisk->PCHSGeometry.cCylinders, pDisk->PCHSGeometry.cHeads,
2635 pDisk->PCHSGeometry.cSectors));
2636 return rc;
2637}
2638
2639/**
2640 * Store virtual disk PCHS geometry in HDD container.
2641 *
2642 * Note that in case of unrecoverable error all images in HDD container will be closed.
2643 *
2644 * @returns VBox status code.
2645 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
2646 * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
2647 * @param pDisk Pointer to HDD container.
2648 * @param nImage Image number, counts from 0. 0 is always base image of container.
2649 * @param pPCHSGeometry Where to load PCHS geometry from. Not NULL.
2650 */
2651VBOXDDU_DECL(int) VDSetPCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
2652 PCPDMMEDIAGEOMETRY pPCHSGeometry)
2653{
2654 int rc = VINF_SUCCESS;
2655
2656 LogFlowFunc(("pDisk=%#p nImage=%u pPCHSGeometry=%#p PCHS=%u/%u/%u\n",
2657 pDisk, nImage, pPCHSGeometry, pPCHSGeometry->cCylinders,
2658 pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2659 do
2660 {
2661 /* sanity check */
2662 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2663 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2664
2665 /* Check arguments. */
2666 AssertMsgBreakStmt( VALID_PTR(pPCHSGeometry)
2667 && pPCHSGeometry->cHeads <= 16
2668 && pPCHSGeometry->cSectors <= 63,
2669 ("pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pPCHSGeometry,
2670 pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
2671 pPCHSGeometry->cSectors),
2672 rc = VERR_INVALID_PARAMETER);
2673
2674 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
2675 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
2676
2677 if (pImage == pDisk->pLast)
2678 {
2679 if ( pPCHSGeometry->cCylinders != pDisk->PCHSGeometry.cCylinders
2680 || pPCHSGeometry->cHeads != pDisk->PCHSGeometry.cHeads
2681 || pPCHSGeometry->cSectors != pDisk->PCHSGeometry.cSectors)
2682 {
2683 /* Only update geometry if it is changed. Avoids similar checks
2684 * in every backend. Most of the time the new geometry is set
2685 * to the previous values, so no need to go through the hassle
2686 * of updating an image which could be opened in read-only mode
2687 * right now. */
2688 rc = pImage->Backend->pfnSetPCHSGeometry(pImage->pvBackendData,
2689 pPCHSGeometry);
2690
2691 /* Cache new geometry values in any case. */
2692 int rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pvBackendData,
2693 &pDisk->PCHSGeometry);
2694 if (RT_FAILURE(rc2))
2695 {
2696 pDisk->PCHSGeometry.cCylinders = 0;
2697 pDisk->PCHSGeometry.cHeads = 0;
2698 pDisk->PCHSGeometry.cSectors = 0;
2699 }
2700 else
2701 {
2702 /* Make sure the CHS geometry is properly clipped. */
2703 pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 255);
2704 pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
2705 }
2706 }
2707 }
2708 else
2709 {
2710 PDMMEDIAGEOMETRY PCHS;
2711 rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pvBackendData,
2712 &PCHS);
2713 if ( RT_FAILURE(rc)
2714 || pPCHSGeometry->cCylinders != PCHS.cCylinders
2715 || pPCHSGeometry->cHeads != PCHS.cHeads
2716 || pPCHSGeometry->cSectors != PCHS.cSectors)
2717 {
2718 /* Only update geometry if it is changed. Avoids similar checks
2719 * in every backend. Most of the time the new geometry is set
2720 * to the previous values, so no need to go through the hassle
2721 * of updating an image which could be opened in read-only mode
2722 * right now. */
2723 rc = pImage->Backend->pfnSetPCHSGeometry(pImage->pvBackendData,
2724 pPCHSGeometry);
2725 }
2726 }
2727 } while (0);
2728
2729 LogFlowFunc(("returns %Rrc\n", rc));
2730 return rc;
2731}
2732
2733/**
2734 * Get virtual disk LCHS geometry stored in HDD container.
2735 *
2736 * @returns VBox status code.
2737 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
2738 * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
2739 * @param pDisk Pointer to HDD container.
2740 * @param nImage Image number, counts from 0. 0 is always base image of container.
2741 * @param pLCHSGeometry Where to store LCHS geometry. Not NULL.
2742 */
2743VBOXDDU_DECL(int) VDGetLCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
2744 PPDMMEDIAGEOMETRY pLCHSGeometry)
2745{
2746 int rc = VINF_SUCCESS;
2747
2748 LogFlowFunc(("pDisk=%#p nImage=%u pLCHSGeometry=%#p\n",
2749 pDisk, nImage, pLCHSGeometry));
2750 do
2751 {
2752 /* sanity check */
2753 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2754 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2755
2756 /* Check arguments. */
2757 AssertMsgBreakStmt(VALID_PTR(pLCHSGeometry),
2758 ("pLCHSGeometry=%#p\n", pLCHSGeometry),
2759 rc = VERR_INVALID_PARAMETER);
2760
2761 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
2762 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
2763
2764 if (pImage == pDisk->pLast)
2765 {
2766 /* Use cached information if possible. */
2767 if (pDisk->LCHSGeometry.cCylinders != 0)
2768 *pLCHSGeometry = pDisk->LCHSGeometry;
2769 else
2770 rc = VERR_VD_GEOMETRY_NOT_SET;
2771 }
2772 else
2773 rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pvBackendData,
2774 pLCHSGeometry);
2775 } while (0);
2776
2777 LogFlowFunc((": %Rrc (LCHS=%u/%u/%u)\n", rc,
2778 pDisk->LCHSGeometry.cCylinders, pDisk->LCHSGeometry.cHeads,
2779 pDisk->LCHSGeometry.cSectors));
2780 return rc;
2781}
2782
2783/**
2784 * Store virtual disk LCHS geometry in HDD container.
2785 *
2786 * Note that in case of unrecoverable error all images in HDD container will be closed.
2787 *
2788 * @returns VBox status code.
2789 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
2790 * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
2791 * @param pDisk Pointer to HDD container.
2792 * @param nImage Image number, counts from 0. 0 is always base image of container.
2793 * @param pLCHSGeometry Where to load LCHS geometry from. Not NULL.
2794 */
2795VBOXDDU_DECL(int) VDSetLCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
2796 PCPDMMEDIAGEOMETRY pLCHSGeometry)
2797{
2798 int rc = VINF_SUCCESS;
2799
2800 LogFlowFunc(("pDisk=%#p nImage=%u pLCHSGeometry=%#p LCHS=%u/%u/%u\n",
2801 pDisk, nImage, pLCHSGeometry, pLCHSGeometry->cCylinders,
2802 pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2803 do
2804 {
2805 /* sanity check */
2806 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2807 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2808
2809 /* Check arguments. */
2810 AssertMsgBreakStmt( VALID_PTR(pLCHSGeometry)
2811 && pLCHSGeometry->cHeads <= 255
2812 && pLCHSGeometry->cSectors <= 63,
2813 ("pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pLCHSGeometry,
2814 pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads,
2815 pLCHSGeometry->cSectors),
2816 rc = VERR_INVALID_PARAMETER);
2817
2818 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
2819 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
2820
2821 if (pImage == pDisk->pLast)
2822 {
2823 if ( pLCHSGeometry->cCylinders != pDisk->LCHSGeometry.cCylinders
2824 || pLCHSGeometry->cHeads != pDisk->LCHSGeometry.cHeads
2825 || pLCHSGeometry->cSectors != pDisk->LCHSGeometry.cSectors)
2826 {
2827 /* Only update geometry if it is changed. Avoids similar checks
2828 * in every backend. Most of the time the new geometry is set
2829 * to the previous values, so no need to go through the hassle
2830 * of updating an image which could be opened in read-only mode
2831 * right now. */
2832 rc = pImage->Backend->pfnSetLCHSGeometry(pImage->pvBackendData,
2833 pLCHSGeometry);
2834
2835 /* Cache new geometry values in any case. */
2836 int rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pvBackendData,
2837 &pDisk->LCHSGeometry);
2838 if (RT_FAILURE(rc2))
2839 {
2840 pDisk->LCHSGeometry.cCylinders = 0;
2841 pDisk->LCHSGeometry.cHeads = 0;
2842 pDisk->LCHSGeometry.cSectors = 0;
2843 }
2844 else
2845 {
2846 /* Make sure the CHS geometry is properly clipped. */
2847 pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
2848 pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
2849 }
2850 }
2851 }
2852 else
2853 {
2854 PDMMEDIAGEOMETRY LCHS;
2855 rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pvBackendData,
2856 &LCHS);
2857 if ( RT_FAILURE(rc)
2858 || pLCHSGeometry->cCylinders != LCHS.cCylinders
2859 || pLCHSGeometry->cHeads != LCHS.cHeads
2860 || pLCHSGeometry->cSectors != LCHS.cSectors)
2861 {
2862 /* Only update geometry if it is changed. Avoids similar checks
2863 * in every backend. Most of the time the new geometry is set
2864 * to the previous values, so no need to go through the hassle
2865 * of updating an image which could be opened in read-only mode
2866 * right now. */
2867 rc = pImage->Backend->pfnSetLCHSGeometry(pImage->pvBackendData,
2868 pLCHSGeometry);
2869 }
2870 }
2871 } while (0);
2872
2873 LogFlowFunc(("returns %Rrc\n", rc));
2874 return rc;
2875}
2876
2877/**
2878 * Get version of image in HDD container.
2879 *
2880 * @returns VBox status code.
2881 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
2882 * @param pDisk Pointer to HDD container.
2883 * @param nImage Image number, counts from 0. 0 is always base image of container.
2884 * @param puVersion Where to store the image version.
2885 */
2886VBOXDDU_DECL(int) VDGetVersion(PVBOXHDD pDisk, unsigned nImage,
2887 unsigned *puVersion)
2888{
2889 int rc = VINF_SUCCESS;
2890
2891 LogFlowFunc(("pDisk=%#p nImage=%u puVersion=%#p\n",
2892 pDisk, nImage, puVersion));
2893 do
2894 {
2895 /* sanity check */
2896 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2897 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2898
2899 /* Check arguments. */
2900 AssertMsgBreakStmt(VALID_PTR(puVersion),
2901 ("puVersion=%#p\n", puVersion),
2902 rc = VERR_INVALID_PARAMETER);
2903
2904 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
2905 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
2906
2907 *puVersion = pImage->Backend->pfnGetVersion(pImage->pvBackendData);
2908 } while (0);
2909
2910 LogFlowFunc(("returns %Rrc uVersion=%#x\n", rc, *puVersion));
2911 return rc;
2912}
2913
2914/**
2915 * List the capabilities of image backend in HDD container.
2916 *
2917 * @returns VBox status code.
2918 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
2919 * @param pDisk Pointer to the HDD container.
2920 * @param nImage Image number, counts from 0. 0 is always base image of container.
2921 * @param pbackendInfo Where to store the backend information.
2922 */
2923VBOXDDU_DECL(int) VDBackendInfoSingle(PVBOXHDD pDisk, unsigned nImage,
2924 PVDBACKENDINFO pBackendInfo)
2925{
2926 int rc = VINF_SUCCESS;
2927
2928 LogFlowFunc(("pDisk=%#p nImage=%u pBackendInfo=%#p\n",
2929 pDisk, nImage, pBackendInfo));
2930 do
2931 {
2932 /* sanity check */
2933 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2934 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2935
2936 /* Check arguments. */
2937 AssertMsgBreakStmt(VALID_PTR(pBackendInfo),
2938 ("pBackendInfo=%#p\n", pBackendInfo),
2939 rc = VERR_INVALID_PARAMETER);
2940
2941 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
2942 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
2943
2944 pBackendInfo->pszBackend = RTStrDup(pImage->Backend->pszBackendName);
2945 pBackendInfo->uBackendCaps = pImage->Backend->uBackendCaps;
2946 pBackendInfo->papszFileExtensions = pImage->Backend->papszFileExtensions;
2947 pBackendInfo->paConfigInfo = pImage->Backend->paConfigInfo;
2948 } while (0);
2949
2950 LogFlowFunc(("returns %Rrc\n", rc));
2951 return rc;
2952}
2953
2954/**
2955 * Get flags of image in HDD container.
2956 *
2957 * @returns VBox status code.
2958 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
2959 * @param pDisk Pointer to HDD container.
2960 * @param nImage Image number, counts from 0. 0 is always base image of container.
2961 * @param puImageFlags Where to store the image flags.
2962 */
2963VBOXDDU_DECL(int) VDGetImageFlags(PVBOXHDD pDisk, unsigned nImage,
2964 unsigned *puImageFlags)
2965{
2966 int rc = VINF_SUCCESS;
2967
2968 LogFlowFunc(("pDisk=%#p nImage=%u puImageFlags=%#p\n",
2969 pDisk, nImage, puImageFlags));
2970 do
2971 {
2972 /* sanity check */
2973 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
2974 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
2975
2976 /* Check arguments. */
2977 AssertMsgBreakStmt(VALID_PTR(puImageFlags),
2978 ("puImageFlags=%#p\n", puImageFlags),
2979 rc = VERR_INVALID_PARAMETER);
2980
2981 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
2982 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
2983
2984 *puImageFlags = pImage->Backend->pfnGetImageFlags(pImage->pvBackendData);
2985 } while (0);
2986
2987 LogFlowFunc(("returns %Rrc uImageFlags=%#x\n", rc, *puImageFlags));
2988 return rc;
2989}
2990
2991/**
2992 * Get open flags of image in HDD container.
2993 *
2994 * @returns VBox status code.
2995 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
2996 * @param pDisk Pointer to HDD container.
2997 * @param nImage Image number, counts from 0. 0 is always base image of container.
2998 * @param puOpenFlags Where to store the image open flags.
2999 */
3000VBOXDDU_DECL(int) VDGetOpenFlags(PVBOXHDD pDisk, unsigned nImage,
3001 unsigned *puOpenFlags)
3002{
3003 int rc = VINF_SUCCESS;
3004
3005 LogFlowFunc(("pDisk=%#p nImage=%u puOpenFlags=%#p\n",
3006 pDisk, nImage, puOpenFlags));
3007 do
3008 {
3009 /* sanity check */
3010 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3011 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3012
3013 /* Check arguments. */
3014 AssertMsgBreakStmt(VALID_PTR(puOpenFlags),
3015 ("puOpenFlags=%#p\n", puOpenFlags),
3016 rc = VERR_INVALID_PARAMETER);
3017
3018 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3019 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3020
3021 *puOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pvBackendData);
3022 } while (0);
3023
3024 LogFlowFunc(("returns %Rrc uOpenFlags=%#x\n", rc, *puOpenFlags));
3025 return rc;
3026}
3027
3028/**
3029 * Set open flags of image in HDD container.
3030 * This operation may cause file locking changes and/or files being reopened.
3031 * Note that in case of unrecoverable error all images in HDD container will be closed.
3032 *
3033 * @returns VBox status code.
3034 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
3035 * @param pDisk Pointer to HDD container.
3036 * @param nImage Image number, counts from 0. 0 is always base image of container.
3037 * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
3038 */
3039VBOXDDU_DECL(int) VDSetOpenFlags(PVBOXHDD pDisk, unsigned nImage,
3040 unsigned uOpenFlags)
3041{
3042 int rc;
3043
3044 LogFlowFunc(("pDisk=%#p uOpenFlags=%#u\n", pDisk, uOpenFlags));
3045 do
3046 {
3047 /* sanity check */
3048 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3049 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3050
3051 /* Check arguments. */
3052 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
3053 ("uOpenFlags=%#x\n", uOpenFlags),
3054 rc = VERR_INVALID_PARAMETER);
3055
3056 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3057 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3058
3059 rc = pImage->Backend->pfnSetOpenFlags(pImage->pvBackendData,
3060 uOpenFlags);
3061 } while (0);
3062
3063 LogFlowFunc(("returns %Rrc\n", rc));
3064 return rc;
3065}
3066
3067/**
3068 * Get base filename of image in HDD container. Some image formats use
3069 * other filenames as well, so don't use this for anything but informational
3070 * purposes.
3071 *
3072 * @returns VBox status code.
3073 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
3074 * @returns VERR_BUFFER_OVERFLOW if pszFilename buffer too small to hold filename.
3075 * @param pDisk Pointer to HDD container.
3076 * @param nImage Image number, counts from 0. 0 is always base image of container.
3077 * @param pszFilename Where to store the image file name.
3078 * @param cbFilename Size of buffer pszFilename points to.
3079 */
3080VBOXDDU_DECL(int) VDGetFilename(PVBOXHDD pDisk, unsigned nImage,
3081 char *pszFilename, unsigned cbFilename)
3082{
3083 int rc;
3084
3085 LogFlowFunc(("pDisk=%#p nImage=%u pszFilename=%#p cbFilename=%u\n",
3086 pDisk, nImage, pszFilename, cbFilename));
3087 do
3088 {
3089 /* sanity check */
3090 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3091 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3092
3093 /* Check arguments. */
3094 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
3095 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
3096 rc = VERR_INVALID_PARAMETER);
3097 AssertMsgBreakStmt(cbFilename,
3098 ("cbFilename=%u\n", cbFilename),
3099 rc = VERR_INVALID_PARAMETER);
3100
3101 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3102 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3103
3104 size_t cb = strlen(pImage->pszFilename);
3105 if (cb <= cbFilename)
3106 {
3107 strcpy(pszFilename, pImage->pszFilename);
3108 rc = VINF_SUCCESS;
3109 }
3110 else
3111 {
3112 strncpy(pszFilename, pImage->pszFilename, cbFilename - 1);
3113 pszFilename[cbFilename - 1] = '\0';
3114 rc = VERR_BUFFER_OVERFLOW;
3115 }
3116 } while (0);
3117
3118 LogFlowFunc(("returns %Rrc, pszFilename=\"%s\"\n", rc, pszFilename));
3119 return rc;
3120}
3121
3122/**
3123 * Get the comment line of image in HDD container.
3124 *
3125 * @returns VBox status code.
3126 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
3127 * @returns VERR_BUFFER_OVERFLOW if pszComment buffer too small to hold comment text.
3128 * @param pDisk Pointer to HDD container.
3129 * @param nImage Image number, counts from 0. 0 is always base image of container.
3130 * @param pszComment Where to store the comment string of image. NULL is ok.
3131 * @param cbComment The size of pszComment buffer. 0 is ok.
3132 */
3133VBOXDDU_DECL(int) VDGetComment(PVBOXHDD pDisk, unsigned nImage,
3134 char *pszComment, unsigned cbComment)
3135{
3136 int rc;
3137
3138 LogFlowFunc(("pDisk=%#p nImage=%u pszComment=%#p cbComment=%u\n",
3139 pDisk, nImage, pszComment, cbComment));
3140 do
3141 {
3142 /* sanity check */
3143 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3144 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3145
3146 /* Check arguments. */
3147 AssertMsgBreakStmt(VALID_PTR(pszComment),
3148 ("pszComment=%#p \"%s\"\n", pszComment, pszComment),
3149 rc = VERR_INVALID_PARAMETER);
3150 AssertMsgBreakStmt(cbComment,
3151 ("cbComment=%u\n", cbComment),
3152 rc = VERR_INVALID_PARAMETER);
3153
3154 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3155 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3156
3157 rc = pImage->Backend->pfnGetComment(pImage->pvBackendData, pszComment,
3158 cbComment);
3159 } while (0);
3160
3161 LogFlowFunc(("returns %Rrc, pszComment=\"%s\"\n", rc, pszComment));
3162 return rc;
3163}
3164
3165/**
3166 * Changes the comment line of image in HDD container.
3167 *
3168 * @returns VBox status code.
3169 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
3170 * @param pDisk Pointer to HDD container.
3171 * @param nImage Image number, counts from 0. 0 is always base image of container.
3172 * @param pszComment New comment string (UTF-8). NULL is allowed to reset the comment.
3173 */
3174VBOXDDU_DECL(int) VDSetComment(PVBOXHDD pDisk, unsigned nImage,
3175 const char *pszComment)
3176{
3177 int rc;
3178
3179 LogFlowFunc(("pDisk=%#p nImage=%u pszComment=%#p \"%s\"\n",
3180 pDisk, nImage, pszComment, pszComment));
3181 do
3182 {
3183 /* sanity check */
3184 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3185 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3186
3187 /* Check arguments. */
3188 AssertMsgBreakStmt(VALID_PTR(pszComment) || pszComment == NULL,
3189 ("pszComment=%#p \"%s\"\n", pszComment, pszComment),
3190 rc = VERR_INVALID_PARAMETER);
3191
3192 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3193 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3194
3195 rc = pImage->Backend->pfnSetComment(pImage->pvBackendData, pszComment);
3196 } while (0);
3197
3198 LogFlowFunc(("returns %Rrc\n", rc));
3199 return rc;
3200}
3201
3202
3203/**
3204 * Get UUID of image in HDD container.
3205 *
3206 * @returns VBox status code.
3207 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
3208 * @param pDisk Pointer to HDD container.
3209 * @param nImage Image number, counts from 0. 0 is always base image of container.
3210 * @param pUuid Where to store the image creation UUID.
3211 */
3212VBOXDDU_DECL(int) VDGetUuid(PVBOXHDD pDisk, unsigned nImage, PRTUUID pUuid)
3213{
3214 int rc;
3215
3216 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
3217 do
3218 {
3219 /* sanity check */
3220 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3221 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3222
3223 /* Check arguments. */
3224 AssertMsgBreakStmt(VALID_PTR(pUuid),
3225 ("pUuid=%#p\n", pUuid),
3226 rc = VERR_INVALID_PARAMETER);
3227
3228 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3229 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3230
3231 rc = pImage->Backend->pfnGetUuid(pImage->pvBackendData, pUuid);
3232 } while (0);
3233
3234 LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
3235 return rc;
3236}
3237
3238/**
3239 * Set the image's UUID. Should not be used by normal applications.
3240 *
3241 * @returns VBox status code.
3242 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
3243 * @param pDisk Pointer to HDD container.
3244 * @param nImage Image number, counts from 0. 0 is always base image of container.
3245 * @param pUuid New UUID of the image. If NULL, a new UUID is created.
3246 */
3247VBOXDDU_DECL(int) VDSetUuid(PVBOXHDD pDisk, unsigned nImage, PCRTUUID pUuid)
3248{
3249 int rc;
3250
3251 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
3252 pDisk, nImage, pUuid, pUuid));
3253 do
3254 {
3255 /* sanity check */
3256 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3257 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3258
3259 AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
3260 ("pUuid=%#p\n", pUuid),
3261 rc = VERR_INVALID_PARAMETER);
3262
3263 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3264 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3265
3266 RTUUID Uuid;
3267 if (!pUuid)
3268 {
3269 RTUuidCreate(&Uuid);
3270 pUuid = &Uuid;
3271 }
3272 rc = pImage->Backend->pfnSetUuid(pImage->pvBackendData, pUuid);
3273 } while (0);
3274
3275 LogFlowFunc(("returns %Rrc\n", rc));
3276 return rc;
3277}
3278
3279/**
3280 * Get last modification UUID of image in HDD container.
3281 *
3282 * @returns VBox status code.
3283 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
3284 * @param pDisk Pointer to HDD container.
3285 * @param nImage Image number, counts from 0. 0 is always base image of container.
3286 * @param pUuid Where to store the image modification UUID.
3287 */
3288VBOXDDU_DECL(int) VDGetModificationUuid(PVBOXHDD pDisk, unsigned nImage, PRTUUID pUuid)
3289{
3290 int rc = VINF_SUCCESS;
3291
3292 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
3293 do
3294 {
3295 /* sanity check */
3296 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3297 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3298
3299 /* Check arguments. */
3300 AssertMsgBreakStmt(VALID_PTR(pUuid),
3301 ("pUuid=%#p\n", pUuid),
3302 rc = VERR_INVALID_PARAMETER);
3303
3304 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3305 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3306
3307 rc = pImage->Backend->pfnGetModificationUuid(pImage->pvBackendData,
3308 pUuid);
3309 } while (0);
3310
3311 LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
3312 return rc;
3313}
3314
3315/**
3316 * Set the image's last modification UUID. Should not be used by normal applications.
3317 *
3318 * @returns VBox status code.
3319 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
3320 * @param pDisk Pointer to HDD container.
3321 * @param nImage Image number, counts from 0. 0 is always base image of container.
3322 * @param pUuid New modification UUID of the image. If NULL, a new UUID is created.
3323 */
3324VBOXDDU_DECL(int) VDSetModificationUuid(PVBOXHDD pDisk, unsigned nImage, PCRTUUID pUuid)
3325{
3326 int rc;
3327
3328 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
3329 pDisk, nImage, pUuid, pUuid));
3330 do
3331 {
3332 /* sanity check */
3333 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3334 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3335
3336 /* Check arguments. */
3337 AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
3338 ("pUuid=%#p\n", pUuid),
3339 rc = VERR_INVALID_PARAMETER);
3340
3341 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3342 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3343
3344 RTUUID Uuid;
3345 if (!pUuid)
3346 {
3347 RTUuidCreate(&Uuid);
3348 pUuid = &Uuid;
3349 }
3350 rc = pImage->Backend->pfnSetModificationUuid(pImage->pvBackendData,
3351 pUuid);
3352 } while (0);
3353
3354 LogFlowFunc(("returns %Rrc\n", rc));
3355 return rc;
3356}
3357
3358/**
3359 * Get parent UUID of image in HDD container.
3360 *
3361 * @returns VBox status code.
3362 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
3363 * @param pDisk Pointer to HDD container.
3364 * @param nImage Image number, counts from 0. 0 is always base image of container.
3365 * @param pUuid Where to store the parent image UUID.
3366 */
3367VBOXDDU_DECL(int) VDGetParentUuid(PVBOXHDD pDisk, unsigned nImage,
3368 PRTUUID pUuid)
3369{
3370 int rc = VINF_SUCCESS;
3371
3372 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
3373 do
3374 {
3375 /* sanity check */
3376 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3377 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3378
3379 /* Check arguments. */
3380 AssertMsgBreakStmt(VALID_PTR(pUuid),
3381 ("pUuid=%#p\n", pUuid),
3382 rc = VERR_INVALID_PARAMETER);
3383
3384 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3385 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3386
3387 rc = pImage->Backend->pfnGetParentUuid(pImage->pvBackendData, pUuid);
3388 } while (0);
3389
3390 LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
3391 return rc;
3392}
3393
3394/**
3395 * Set the image's parent UUID. Should not be used by normal applications.
3396 *
3397 * @returns VBox status code.
3398 * @param pDisk Pointer to HDD container.
3399 * @param nImage Image number, counts from 0. 0 is always base image of container.
3400 * @param pUuid New parent UUID of the image. If NULL, a new UUID is created.
3401 */
3402VBOXDDU_DECL(int) VDSetParentUuid(PVBOXHDD pDisk, unsigned nImage,
3403 PCRTUUID pUuid)
3404{
3405 int rc;
3406
3407 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
3408 pDisk, nImage, pUuid, pUuid));
3409 do
3410 {
3411 /* sanity check */
3412 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3413 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3414
3415 /* Check arguments. */
3416 AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
3417 ("pUuid=%#p\n", pUuid),
3418 rc = VERR_INVALID_PARAMETER);
3419
3420 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3421 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3422
3423 RTUUID Uuid;
3424 if (!pUuid)
3425 {
3426 RTUuidCreate(&Uuid);
3427 pUuid = &Uuid;
3428 }
3429 rc = pImage->Backend->pfnSetParentUuid(pImage->pvBackendData, pUuid);
3430 } while (0);
3431
3432 LogFlowFunc(("returns %Rrc\n", rc));
3433 return rc;
3434}
3435
3436
3437/**
3438 * Debug helper - dumps all opened images in HDD container into the log file.
3439 *
3440 * @param pDisk Pointer to HDD container.
3441 */
3442VBOXDDU_DECL(void) VDDumpImages(PVBOXHDD pDisk)
3443{
3444 do
3445 {
3446 /* sanity check */
3447 AssertPtrBreak(pDisk);
3448 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3449
3450 RTLogPrintf("--- Dumping VD Disk, Images=%u\n", pDisk->cImages);
3451 for (PVDIMAGE pImage = pDisk->pBase; pImage; pImage = pImage->pNext)
3452 {
3453 RTLogPrintf("Dumping VD image \"%s\" (Backend=%s)\n",
3454 pImage->pszFilename, pImage->Backend->pszBackendName);
3455 pImage->Backend->pfnDump(pImage->pvBackendData);
3456 }
3457 } while (0);
3458}
3459
3460/**
3461 * Query if asynchronous operations are supported for this disk.
3462 *
3463 * @returns VBox status code.
3464 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
3465 * @param pDisk Pointer to the HDD container.
3466 * @param nImage Image number, counts from 0. 0 is always base image of container.
3467 * @param pfAIOSupported Where to store if async IO is supported.
3468 */
3469VBOXDDU_DECL(int) VDImageIsAsyncIOSupported(PVBOXHDD pDisk, unsigned nImage, bool *pfAIOSupported)
3470{
3471 int rc = VINF_SUCCESS;
3472
3473 LogFlowFunc(("pDisk=%#p nImage=%u pfAIOSupported=%#p\n", pDisk, nImage, pfAIOSupported));
3474 do
3475 {
3476 /* sanity check */
3477 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3478 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3479
3480 /* Check arguments. */
3481 AssertMsgBreakStmt(VALID_PTR(pfAIOSupported),
3482 ("pfAIOSupported=%#p\n", pfAIOSupported),
3483 rc = VERR_INVALID_PARAMETER);
3484
3485 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
3486 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
3487
3488 if (pImage->Backend->uBackendCaps & VD_CAP_ASYNC)
3489 *pfAIOSupported = pImage->Backend->pfnIsAsyncIOSupported(pImage->pvBackendData);
3490 else
3491 *pfAIOSupported = false;
3492 } while (0);
3493
3494 LogFlowFunc(("returns %Rrc, fAIOSupported=%u\n", rc, *pfAIOSupported));
3495 return rc;
3496}
3497
3498/**
3499 * Start a asynchronous read request.
3500 *
3501 * @returns VBox status code.
3502 * @param pDisk Pointer to the HDD container.
3503 * @param uOffset The offset of the virtual disk to read from.
3504 * @param cbRead How many bytes to read.
3505 * @param paSeg Pointer to an array of segments.
3506 * @param cSeg Number of segments in the array.
3507 * @param pvUser User data which is passed on completion
3508 */
3509VBOXDDU_DECL(int) VDAsyncRead(PVBOXHDD pDisk, uint64_t uOffset, size_t cbRead,
3510 PPDMDATASEG paSeg, unsigned cSeg,
3511 void *pvUser)
3512{
3513 int rc = VERR_VD_BLOCK_FREE;
3514
3515 LogFlowFunc(("pDisk=%#p uOffset=%llu paSeg=%p cSeg=%u cbRead=%zu\n",
3516 pDisk, uOffset, paSeg, cSeg, cbRead));
3517 do
3518 {
3519 /* sanity check */
3520 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3521 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3522
3523 /* Check arguments. */
3524 AssertMsgBreakStmt(cbRead,
3525 ("cbRead=%zu\n", cbRead),
3526 rc = VERR_INVALID_PARAMETER);
3527 AssertMsgBreakStmt(uOffset + cbRead <= pDisk->cbSize,
3528 ("uOffset=%llu cbRead=%zu pDisk->cbSize=%llu\n",
3529 uOffset, cbRead, pDisk->cbSize),
3530 rc = VERR_INVALID_PARAMETER);
3531 AssertMsgBreakStmt(VALID_PTR(paSeg),
3532 ("paSeg=%#p\n", paSeg),
3533 rc = VERR_INVALID_PARAMETER);
3534 AssertMsgBreakStmt(cSeg,
3535 ("cSeg=%zu\n", cSeg),
3536 rc = VERR_INVALID_PARAMETER);
3537
3538
3539 PVDIMAGE pImage = pDisk->pLast;
3540 AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
3541
3542 /* @todo: This does not work for images which do not have all meta data in memory. */
3543 for (PVDIMAGE pCurrImage = pImage;
3544 pCurrImage != NULL && rc == VERR_VD_BLOCK_FREE;
3545 pCurrImage = pCurrImage->pPrev)
3546 {
3547 rc = pCurrImage->Backend->pfnAsyncRead(pCurrImage->pvBackendData,
3548 uOffset, cbRead, paSeg, cSeg,
3549 pvUser);
3550 }
3551
3552 /* No image in the chain contains the data for the block. */
3553 if (rc == VERR_VD_BLOCK_FREE)
3554 {
3555 for (unsigned i = 0; i < cSeg && (cbRead > 0); i++)
3556 {
3557 memset(paSeg[i].pvSeg, '\0', paSeg[i].cbSeg);
3558 cbRead -= paSeg[i].cbSeg;
3559 }
3560 /* Request finished without the need to enqueue a async I/O request. Tell caller. */
3561 rc = VINF_VD_ASYNC_IO_FINISHED;
3562 }
3563
3564 } while (0);
3565
3566 LogFlowFunc(("returns %Rrc\n", rc));
3567 return rc;
3568}
3569
3570
3571/**
3572 * Start a asynchronous write request.
3573 *
3574 * @returns VBox status code.
3575 * @param pDisk Pointer to the HDD container.
3576 * @param uOffset The offset of the virtual disk to write to.
3577 * @param cbWrtie How many bytes to write.
3578 * @param paSeg Pointer to an array of segments.
3579 * @param cSeg Number of segments in the array.
3580 * @param pvUser User data which is passed on completion.
3581 */
3582VBOXDDU_DECL(int) VDAsyncWrite(PVBOXHDD pDisk, uint64_t uOffset, size_t cbWrite,
3583 PPDMDATASEG paSeg, unsigned cSeg,
3584 void *pvUser)
3585{
3586 int rc;
3587
3588 LogFlowFunc(("pDisk=%#p uOffset=%llu paSeg=%p cSeg=%u cbWrite=%zu\n",
3589 pDisk, uOffset, paSeg, cSeg, cbWrite));
3590 do
3591 {
3592 /* sanity check */
3593 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
3594 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3595
3596 /* Check arguments. */
3597 AssertMsgBreakStmt(cbWrite,
3598 ("cbWrite=%zu\n", cbWrite),
3599 rc = VERR_INVALID_PARAMETER);
3600 AssertMsgBreakStmt(uOffset + cbWrite <= pDisk->cbSize,
3601 ("uOffset=%llu cbWrite=%zu pDisk->cbSize=%llu\n",
3602 uOffset, cbWrite, pDisk->cbSize),
3603 rc = VERR_INVALID_PARAMETER);
3604 AssertMsgBreakStmt(VALID_PTR(paSeg),
3605 ("paSeg=%#p\n", paSeg),
3606 rc = VERR_INVALID_PARAMETER);
3607 AssertMsgBreakStmt(cSeg,
3608 ("cSeg=%zu\n", cSeg),
3609 rc = VERR_INVALID_PARAMETER);
3610
3611
3612 PVDIMAGE pImage = pDisk->pLast;
3613 AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
3614
3615 vdSetModifiedFlag(pDisk);
3616 rc = pImage->Backend->pfnAsyncWrite(pImage->pvBackendData,
3617 uOffset, cbWrite,
3618 paSeg, cSeg, pvUser);
3619 } while (0);
3620
3621 LogFlowFunc(("returns %Rrc\n", rc));
3622 return rc;
3623
3624}
3625
3626#if 0
3627/** @copydoc VBOXHDDBACKEND::pfnComposeLocation */
3628int genericFileComposeLocation(PVDINTERFACE pConfig, char **pszLocation)
3629{
3630 return NULL;
3631}
3632
3633
3634/** @copydoc VBOXHDDBACKEND::pfnComposeName */
3635int genericFileComposeName(PVDINTERFACE pConfig, char **pszName)
3636{
3637 return NULL;
3638}
3639#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