VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/VBoxHDD-new.cpp@ 15577

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

Storage/VBoxHDD-new: fix incorrect error check when copying to an already existing target image.

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