VirtualBox

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

Last change on this file since 26515 was 26163, checked in by vboxsync, 15 years ago

PDM: s/pUsbReg/pReg/g (2nd try, backed out r57176)

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