VirtualBox

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

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

Storage/VBoxHDD-new: introduce parent UUID parameter for VDCreateDiff. Makes creation of iSCSI snapshots easier.

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