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