VirtualBox

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

Last change on this file since 24323 was 23973, checked in by vboxsync, 15 years ago

*,RTFileOpen: Fixing RTFileOpen flag misdesign: The deny, access and action flags are mandatory now.

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