VirtualBox

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

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

Storage/VBoxHDD: drop 1024 cylinder LCHS paranoia. SCSI controllers have larger values, and this caused unnecessary trouble when converting images. The clipping is already done in DevPcBios.

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