VirtualBox

source: vbox/trunk/src/VBox/Storage/VD.cpp@ 37329

Last change on this file since 37329 was 37329, checked in by vboxsync, 13 years ago

VD: Remove left over defines for logging and assertions

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 295.0 KB
Line 
1/* $Id: VD.cpp 37329 2011-06-06 15:43:39Z vboxsync $ */
2/** @file
3 * VBoxHDD - VBox HDD Container implementation.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Oracle Corporation
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
18/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21#define LOG_GROUP LOG_GROUP_VD
22#include <VBox/vd.h>
23#include <VBox/err.h>
24#include <VBox/sup.h>
25#include <VBox/log.h>
26
27#include <iprt/alloc.h>
28#include <iprt/assert.h>
29#include <iprt/uuid.h>
30#include <iprt/file.h>
31#include <iprt/string.h>
32#include <iprt/asm.h>
33#include <iprt/ldr.h>
34#include <iprt/dir.h>
35#include <iprt/path.h>
36#include <iprt/param.h>
37#include <iprt/memcache.h>
38#include <iprt/sg.h>
39#include <iprt/critsect.h>
40#include <iprt/list.h>
41#include <iprt/avl.h>
42
43#include <VBox/vd-plugin.h>
44#include <VBox/vd-cache-plugin.h>
45
46#define VBOXHDDDISK_SIGNATURE 0x6f0e2a7d
47
48/** Buffer size used for merging images. */
49#define VD_MERGE_BUFFER_SIZE (16 * _1M)
50
51/** Maximum number of segments in one I/O task. */
52#define VD_IO_TASK_SEGMENTS_MAX 64
53
54/**
55 * VD async I/O interface storage descriptor.
56 */
57typedef struct VDIIOFALLBACKSTORAGE
58{
59 /** File handle. */
60 RTFILE File;
61 /** Completion callback. */
62 PFNVDCOMPLETED pfnCompleted;
63 /** Thread for async access. */
64 RTTHREAD ThreadAsync;
65} VDIIOFALLBACKSTORAGE, *PVDIIOFALLBACKSTORAGE;
66
67/**
68 * Structure containing everything I/O related
69 * for the image and cache descriptors.
70 */
71typedef struct VDIO
72{
73 /** I/O interface to the upper layer. */
74 PVDINTERFACE pInterfaceIO;
75 /** I/O interface callback table. */
76 PVDINTERFACEIO pInterfaceIOCallbacks;
77
78 /** Per image internal I/O interface. */
79 VDINTERFACE VDIIOInt;
80
81 /** Fallback I/O interface, only used if the caller doesn't provide it. */
82 VDINTERFACE VDIIO;
83
84 /** Opaque backend data. */
85 void *pBackendData;
86 /** Disk this image is part of */
87 PVBOXHDD pDisk;
88} VDIO, *PVDIO;
89
90/**
91 * VBox HDD Container image descriptor.
92 */
93typedef struct VDIMAGE
94{
95 /** Link to parent image descriptor, if any. */
96 struct VDIMAGE *pPrev;
97 /** Link to child image descriptor, if any. */
98 struct VDIMAGE *pNext;
99 /** Container base filename. (UTF-8) */
100 char *pszFilename;
101 /** Data managed by the backend which keeps the actual info. */
102 void *pBackendData;
103 /** Cached sanitized image flags. */
104 unsigned uImageFlags;
105 /** Image open flags (only those handled generically in this code and which
106 * the backends will never ever see). */
107 unsigned uOpenFlags;
108
109 /** Function pointers for the various backend methods. */
110 PCVBOXHDDBACKEND Backend;
111 /** Pointer to list of VD interfaces, per-image. */
112 PVDINTERFACE pVDIfsImage;
113 /** I/O related things. */
114 VDIO VDIo;
115} VDIMAGE, *PVDIMAGE;
116
117/**
118 * uModified bit flags.
119 */
120#define VD_IMAGE_MODIFIED_FLAG RT_BIT(0)
121#define VD_IMAGE_MODIFIED_FIRST RT_BIT(1)
122#define VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE RT_BIT(2)
123
124
125/**
126 * VBox HDD Cache image descriptor.
127 */
128typedef struct VDCACHE
129{
130 /** Cache base filename. (UTF-8) */
131 char *pszFilename;
132 /** Data managed by the backend which keeps the actual info. */
133 void *pBackendData;
134 /** Cached sanitized image flags. */
135 unsigned uImageFlags;
136 /** Image open flags (only those handled generically in this code and which
137 * the backends will never ever see). */
138 unsigned uOpenFlags;
139
140 /** Function pointers for the various backend methods. */
141 PCVDCACHEBACKEND Backend;
142
143 /** Pointer to list of VD interfaces, per-cache. */
144 PVDINTERFACE pVDIfsCache;
145 /** I/O related things. */
146 VDIO VDIo;
147} VDCACHE, *PVDCACHE;
148
149/**
150 * VBox HDD Container main structure, private part.
151 */
152struct VBOXHDD
153{
154 /** Structure signature (VBOXHDDDISK_SIGNATURE). */
155 uint32_t u32Signature;
156
157 /** Image type. */
158 VDTYPE enmType;
159
160 /** Number of opened images. */
161 unsigned cImages;
162
163 /** Base image. */
164 PVDIMAGE pBase;
165
166 /** Last opened image in the chain.
167 * The same as pBase if only one image is used. */
168 PVDIMAGE pLast;
169
170 /** If a merge to one of the parents is running this may be non-NULL
171 * to indicate to what image the writes should be additionally relayed. */
172 PVDIMAGE pImageRelay;
173
174 /** Flags representing the modification state. */
175 unsigned uModified;
176
177 /** Cached size of this disk. */
178 uint64_t cbSize;
179 /** Cached PCHS geometry for this disk. */
180 VDGEOMETRY PCHSGeometry;
181 /** Cached LCHS geometry for this disk. */
182 VDGEOMETRY LCHSGeometry;
183
184 /** Pointer to list of VD interfaces, per-disk. */
185 PVDINTERFACE pVDIfsDisk;
186 /** Pointer to the common interface structure for error reporting. */
187 PVDINTERFACE pInterfaceError;
188 /** Pointer to the error interface callbacks we use if available. */
189 PVDINTERFACEERROR pInterfaceErrorCallbacks;
190
191 /** Pointer to the optional thread synchronization interface. */
192 PVDINTERFACE pInterfaceThreadSync;
193 /** Pointer to the optional thread synchronization callbacks. */
194 PVDINTERFACETHREADSYNC pInterfaceThreadSyncCallbacks;
195
196 /** Internal I/O interface callback table for the images. */
197 VDINTERFACEIOINT VDIIOIntCallbacks;
198
199 /** Callback table for the fallback I/O interface. */
200 VDINTERFACEIO VDIIOCallbacks;
201
202 /** Memory cache for I/O contexts */
203 RTMEMCACHE hMemCacheIoCtx;
204 /** Memory cache for I/O tasks. */
205 RTMEMCACHE hMemCacheIoTask;
206 /** Critical section protecting the disk against concurrent access. */
207 RTCRITSECT CritSect;
208 /** Flag whether the disk is currently locked by growing write or a flush
209 * request. Other flush or growing write requests need to wait until
210 * the current one completes.
211 */
212 volatile bool fLocked;
213 /** List of waiting requests. - Protected by the critical section. */
214 RTLISTNODE ListWriteLocked;
215 /** I/O context which locked the disk. */
216 PVDIOCTX pIoCtxLockOwner;
217
218 /** Pointer to the L2 disk cache if any. */
219 PVDCACHE pCache;
220};
221
222# define VD_THREAD_IS_CRITSECT_OWNER(Disk) \
223 do \
224 { \
225 AssertMsg(RTCritSectIsOwner(&Disk->CritSect), \
226 ("Thread does not own critical section\n"));\
227 } while(0)
228
229/**
230 * VBox parent read descriptor, used internally for compaction.
231 */
232typedef struct VDPARENTSTATEDESC
233{
234 /** Pointer to disk descriptor. */
235 PVBOXHDD pDisk;
236 /** Pointer to image descriptor. */
237 PVDIMAGE pImage;
238} VDPARENTSTATEDESC, *PVDPARENTSTATEDESC;
239
240/**
241 * Transfer direction.
242 */
243typedef enum VDIOCTXTXDIR
244{
245 /** Read */
246 VDIOCTXTXDIR_READ = 0,
247 /** Write */
248 VDIOCTXTXDIR_WRITE,
249 /** Flush */
250 VDIOCTXTXDIR_FLUSH,
251 /** 32bit hack */
252 VDIOCTXTXDIR_32BIT_HACK = 0x7fffffff
253} VDIOCTXTXDIR, *PVDIOCTXTXDIR;
254
255/** Transfer function */
256typedef DECLCALLBACK(int) FNVDIOCTXTRANSFER (PVDIOCTX pIoCtx);
257/** Pointer to a transfer function. */
258typedef FNVDIOCTXTRANSFER *PFNVDIOCTXTRANSFER;
259
260/**
261 * I/O context
262 */
263typedef struct VDIOCTX
264{
265 /** Disk this is request is for. */
266 PVBOXHDD pDisk;
267 /** Return code. */
268 int rcReq;
269 /** Transfer direction */
270 VDIOCTXTXDIR enmTxDir;
271 /** Number of bytes left until this context completes. */
272 volatile uint32_t cbTransferLeft;
273 /** Current offset */
274 volatile uint64_t uOffset;
275 /** Number of bytes to transfer */
276 volatile size_t cbTransfer;
277 /** Current image in the chain. */
278 PVDIMAGE pImageCur;
279 /** Start image to read from. pImageCur is reset to this
280 * value after it reached the first image in the chain. */
281 PVDIMAGE pImageStart;
282 /** S/G buffer */
283 RTSGBUF SgBuf;
284 /** Flag whether the I/O context is blocked because it is in the growing list. */
285 bool fBlocked;
286 /** Number of data transfers currently pending. */
287 volatile uint32_t cDataTransfersPending;
288 /** How many meta data transfers are pending. */
289 volatile uint32_t cMetaTransfersPending;
290 /** Flag whether the request finished */
291 volatile bool fComplete;
292 /** Temporary allocated memory which is freed
293 * when the context completes. */
294 void *pvAllocation;
295 /** Transfer function. */
296 PFNVDIOCTXTRANSFER pfnIoCtxTransfer;
297 /** Next transfer part after the current one completed. */
298 PFNVDIOCTXTRANSFER pfnIoCtxTransferNext;
299 /** Parent I/O context if any. Sets the type of the context (root/child) */
300 PVDIOCTX pIoCtxParent;
301 /** Type dependent data (root/child) */
302 union
303 {
304 /** Root data */
305 struct
306 {
307 /** Completion callback */
308 PFNVDASYNCTRANSFERCOMPLETE pfnComplete;
309 /** User argument 1 passed on completion. */
310 void *pvUser1;
311 /** User argument 1 passed on completion. */
312 void *pvUser2;
313 } Root;
314 /** Child data */
315 struct
316 {
317 /** Saved start offset */
318 uint64_t uOffsetSaved;
319 /** Saved transfer size */
320 size_t cbTransferLeftSaved;
321 /** Number of bytes transferred from the parent if this context completes. */
322 size_t cbTransferParent;
323 /** Number of bytes to pre read */
324 size_t cbPreRead;
325 /** Number of bytes to post read. */
326 size_t cbPostRead;
327 /** Number of bytes to write left in the parent. */
328 size_t cbWriteParent;
329 /** Write type dependent data. */
330 union
331 {
332 /** Optimized */
333 struct
334 {
335 /** Bytes to fill to satisfy the block size. Not part of the virtual disk. */
336 size_t cbFill;
337 /** Bytes to copy instead of reading from the parent */
338 size_t cbWriteCopy;
339 /** Bytes to read from the image. */
340 size_t cbReadImage;
341 } Optimized;
342 } Write;
343 } Child;
344 } Type;
345} VDIOCTX;
346
347typedef struct VDIOCTXDEFERRED
348{
349 /** Node in the list of deferred requests.
350 * A request can be deferred if the image is growing
351 * and the request accesses the same range or if
352 * the backend needs to read or write metadata from the disk
353 * before it can continue. */
354 RTLISTNODE NodeDeferred;
355 /** I/O context this entry points to. */
356 PVDIOCTX pIoCtx;
357} VDIOCTXDEFERRED, *PVDIOCTXDEFERRED;
358
359/**
360 * I/O task.
361 */
362typedef struct VDIOTASK
363{
364 /** Storage this task belongs to. */
365 PVDIOSTORAGE pIoStorage;
366 /** Optional completion callback. */
367 PFNVDXFERCOMPLETED pfnComplete;
368 /** Opaque user data. */
369 void *pvUser;
370 /** Flag whether this is a meta data transfer. */
371 bool fMeta;
372 /** Type dependent data. */
373 union
374 {
375 /** User data transfer. */
376 struct
377 {
378 /** Number of bytes this task transferred. */
379 uint32_t cbTransfer;
380 /** Pointer to the I/O context the task belongs. */
381 PVDIOCTX pIoCtx;
382 } User;
383 /** Meta data transfer. */
384 struct
385 {
386 /** Meta transfer this task is for. */
387 PVDMETAXFER pMetaXfer;
388 } Meta;
389 } Type;
390} VDIOTASK, *PVDIOTASK;
391
392/**
393 * Storage handle.
394 */
395typedef struct VDIOSTORAGE
396{
397 /** Image I/O state this storage handle belongs to. */
398 PVDIO pVDIo;
399 /** AVL tree for pending async metadata transfers. */
400 PAVLRFOFFTREE pTreeMetaXfers;
401 /** Storage handle */
402 void *pStorage;
403} VDIOSTORAGE;
404
405/**
406 * Metadata transfer.
407 *
408 * @note This entry can't be freed if either the list is not empty or
409 * the reference counter is not 0.
410 * The assumption is that the backends don't need to read huge amounts of
411 * metadata to complete a transfer so the additional memory overhead should
412 * be relatively small.
413 */
414typedef struct VDMETAXFER
415{
416 /** AVL core for fast search (the file offset is the key) */
417 AVLRFOFFNODECORE Core;
418 /** I/O storage for this transfer. */
419 PVDIOSTORAGE pIoStorage;
420 /** Flags. */
421 uint32_t fFlags;
422 /** List of I/O contexts waiting for this metadata transfer to complete. */
423 RTLISTNODE ListIoCtxWaiting;
424 /** Number of references to this entry. */
425 unsigned cRefs;
426 /** Size of the data stored with this entry. */
427 size_t cbMeta;
428 /** Data stored - variable size. */
429 uint8_t abData[1];
430} VDMETAXFER;
431
432/**
433 * The transfer direction for the metadata.
434 */
435#define VDMETAXFER_TXDIR_MASK 0x3
436#define VDMETAXFER_TXDIR_NONE 0x0
437#define VDMETAXFER_TXDIR_WRITE 0x1
438#define VDMETAXFER_TXDIR_READ 0x2
439#define VDMETAXFER_TXDIR_FLUSH 0x3
440#define VDMETAXFER_TXDIR_GET(flags) ((flags) & VDMETAXFER_TXDIR_MASK)
441#define VDMETAXFER_TXDIR_SET(flags, dir) ((flags) = (flags & ~VDMETAXFER_TXDIR_MASK) | (dir))
442
443extern VBOXHDDBACKEND g_RawBackend;
444extern VBOXHDDBACKEND g_VmdkBackend;
445extern VBOXHDDBACKEND g_VDIBackend;
446extern VBOXHDDBACKEND g_VhdBackend;
447extern VBOXHDDBACKEND g_ParallelsBackend;
448extern VBOXHDDBACKEND g_DmgBackend;
449extern VBOXHDDBACKEND g_ISCSIBackend;
450
451static unsigned g_cBackends = 0;
452static PVBOXHDDBACKEND *g_apBackends = NULL;
453static PVBOXHDDBACKEND aStaticBackends[] =
454{
455 &g_VmdkBackend,
456 &g_VDIBackend,
457 &g_VhdBackend,
458 &g_ParallelsBackend,
459 &g_DmgBackend,
460 &g_RawBackend,
461 &g_ISCSIBackend
462};
463
464/**
465 * Supported backends for the disk cache.
466 */
467extern VDCACHEBACKEND g_VciCacheBackend;
468
469static unsigned g_cCacheBackends = 0;
470static PVDCACHEBACKEND *g_apCacheBackends = NULL;
471static PVDCACHEBACKEND aStaticCacheBackends[] =
472{
473 &g_VciCacheBackend
474};
475
476/**
477 * internal: add several backends.
478 */
479static int vdAddBackends(PVBOXHDDBACKEND *ppBackends, unsigned cBackends)
480{
481 PVBOXHDDBACKEND *pTmp = (PVBOXHDDBACKEND*)RTMemRealloc(g_apBackends,
482 (g_cBackends + cBackends) * sizeof(PVBOXHDDBACKEND));
483 if (RT_UNLIKELY(!pTmp))
484 return VERR_NO_MEMORY;
485 g_apBackends = pTmp;
486 memcpy(&g_apBackends[g_cBackends], ppBackends, cBackends * sizeof(PVBOXHDDBACKEND));
487 g_cBackends += cBackends;
488 return VINF_SUCCESS;
489}
490
491/**
492 * internal: add single backend.
493 */
494DECLINLINE(int) vdAddBackend(PVBOXHDDBACKEND pBackend)
495{
496 return vdAddBackends(&pBackend, 1);
497}
498
499/**
500 * internal: add several cache backends.
501 */
502static int vdAddCacheBackends(PVDCACHEBACKEND *ppBackends, unsigned cBackends)
503{
504 PVDCACHEBACKEND *pTmp = (PVDCACHEBACKEND*)RTMemRealloc(g_apCacheBackends,
505 (g_cCacheBackends + cBackends) * sizeof(PVDCACHEBACKEND));
506 if (RT_UNLIKELY(!pTmp))
507 return VERR_NO_MEMORY;
508 g_apCacheBackends = pTmp;
509 memcpy(&g_apCacheBackends[g_cCacheBackends], ppBackends, cBackends * sizeof(PVDCACHEBACKEND));
510 g_cCacheBackends += cBackends;
511 return VINF_SUCCESS;
512}
513
514/**
515 * internal: add single cache backend.
516 */
517DECLINLINE(int) vdAddCacheBackend(PVDCACHEBACKEND pBackend)
518{
519 return vdAddCacheBackends(&pBackend, 1);
520}
521
522/**
523 * internal: issue error message.
524 */
525static int vdError(PVBOXHDD pDisk, int rc, RT_SRC_POS_DECL,
526 const char *pszFormat, ...)
527{
528 va_list va;
529 va_start(va, pszFormat);
530 if (pDisk->pInterfaceErrorCallbacks)
531 pDisk->pInterfaceErrorCallbacks->pfnError(pDisk->pInterfaceError->pvUser, rc, RT_SRC_POS_ARGS, pszFormat, va);
532 va_end(va);
533 return rc;
534}
535
536/**
537 * internal: thread synchronization, start read.
538 */
539DECLINLINE(int) vdThreadStartRead(PVBOXHDD pDisk)
540{
541 int rc = VINF_SUCCESS;
542 if (RT_UNLIKELY(pDisk->pInterfaceThreadSyncCallbacks))
543 rc = pDisk->pInterfaceThreadSyncCallbacks->pfnStartRead(pDisk->pInterfaceThreadSync->pvUser);
544 return rc;
545}
546
547/**
548 * internal: thread synchronization, finish read.
549 */
550DECLINLINE(int) vdThreadFinishRead(PVBOXHDD pDisk)
551{
552 int rc = VINF_SUCCESS;
553 if (RT_UNLIKELY(pDisk->pInterfaceThreadSyncCallbacks))
554 rc = pDisk->pInterfaceThreadSyncCallbacks->pfnFinishRead(pDisk->pInterfaceThreadSync->pvUser);
555 return rc;
556}
557
558/**
559 * internal: thread synchronization, start write.
560 */
561DECLINLINE(int) vdThreadStartWrite(PVBOXHDD pDisk)
562{
563 int rc = VINF_SUCCESS;
564 if (RT_UNLIKELY(pDisk->pInterfaceThreadSyncCallbacks))
565 rc = pDisk->pInterfaceThreadSyncCallbacks->pfnStartWrite(pDisk->pInterfaceThreadSync->pvUser);
566 return rc;
567}
568
569/**
570 * internal: thread synchronization, finish write.
571 */
572DECLINLINE(int) vdThreadFinishWrite(PVBOXHDD pDisk)
573{
574 int rc = VINF_SUCCESS;
575 if (RT_UNLIKELY(pDisk->pInterfaceThreadSyncCallbacks))
576 rc = pDisk->pInterfaceThreadSyncCallbacks->pfnFinishWrite(pDisk->pInterfaceThreadSync->pvUser);
577 return rc;
578}
579
580/**
581 * internal: find image format backend.
582 */
583static int vdFindBackend(const char *pszBackend, PCVBOXHDDBACKEND *ppBackend)
584{
585 int rc = VINF_SUCCESS;
586 PCVBOXHDDBACKEND pBackend = NULL;
587
588 if (!g_apBackends)
589 VDInit();
590
591 for (unsigned i = 0; i < g_cBackends; i++)
592 {
593 if (!RTStrICmp(pszBackend, g_apBackends[i]->pszBackendName))
594 {
595 pBackend = g_apBackends[i];
596 break;
597 }
598 }
599 *ppBackend = pBackend;
600 return rc;
601}
602
603/**
604 * internal: find cache format backend.
605 */
606static int vdFindCacheBackend(const char *pszBackend, PCVDCACHEBACKEND *ppBackend)
607{
608 int rc = VINF_SUCCESS;
609 PCVDCACHEBACKEND pBackend = NULL;
610
611 if (!g_apCacheBackends)
612 VDInit();
613
614 for (unsigned i = 0; i < g_cCacheBackends; i++)
615 {
616 if (!RTStrICmp(pszBackend, g_apCacheBackends[i]->pszBackendName))
617 {
618 pBackend = g_apCacheBackends[i];
619 break;
620 }
621 }
622 *ppBackend = pBackend;
623 return rc;
624}
625
626/**
627 * internal: add image structure to the end of images list.
628 */
629static void vdAddImageToList(PVBOXHDD pDisk, PVDIMAGE pImage)
630{
631 pImage->pPrev = NULL;
632 pImage->pNext = NULL;
633
634 if (pDisk->pBase)
635 {
636 Assert(pDisk->cImages > 0);
637 pImage->pPrev = pDisk->pLast;
638 pDisk->pLast->pNext = pImage;
639 pDisk->pLast = pImage;
640 }
641 else
642 {
643 Assert(pDisk->cImages == 0);
644 pDisk->pBase = pImage;
645 pDisk->pLast = pImage;
646 }
647
648 pDisk->cImages++;
649}
650
651/**
652 * internal: remove image structure from the images list.
653 */
654static void vdRemoveImageFromList(PVBOXHDD pDisk, PVDIMAGE pImage)
655{
656 Assert(pDisk->cImages > 0);
657
658 if (pImage->pPrev)
659 pImage->pPrev->pNext = pImage->pNext;
660 else
661 pDisk->pBase = pImage->pNext;
662
663 if (pImage->pNext)
664 pImage->pNext->pPrev = pImage->pPrev;
665 else
666 pDisk->pLast = pImage->pPrev;
667
668 pImage->pPrev = NULL;
669 pImage->pNext = NULL;
670
671 pDisk->cImages--;
672}
673
674/**
675 * internal: find image by index into the images list.
676 */
677static PVDIMAGE vdGetImageByNumber(PVBOXHDD pDisk, unsigned nImage)
678{
679 PVDIMAGE pImage = pDisk->pBase;
680 if (nImage == VD_LAST_IMAGE)
681 return pDisk->pLast;
682 while (pImage && nImage)
683 {
684 pImage = pImage->pNext;
685 nImage--;
686 }
687 return pImage;
688}
689
690/**
691 * Internal: Tries to read the desired range from the given cache.
692 *
693 * @returns VBox status code.
694 * @retval VERR_VD_BLOCK_FREE if the block is not in the cache.
695 * pcbRead will be set to the number of bytes not in the cache.
696 * Everything thereafter might be in the cache.
697 * @param pCache The cache to read from.
698 * @param uOffset Offset of the virtual disk to read.
699 * @param pvBuf Where to store the read data.
700 * @param cbRead How much to read.
701 * @param pcbRead Where to store the number of bytes actually read.
702 * On success this indicates the number of bytes read from the cache.
703 * If VERR_VD_BLOCK_FREE is returned this gives the number of bytes
704 * which are not in the cache.
705 * In both cases everything beyond this value
706 * might or might not be in the cache.
707 */
708static int vdCacheReadHelper(PVDCACHE pCache, uint64_t uOffset,
709 void *pvBuf, size_t cbRead, size_t *pcbRead)
710{
711 int rc = VINF_SUCCESS;
712
713 LogFlowFunc(("pCache=%#p uOffset=%llu pvBuf=%#p cbRead=%zu pcbRead=%#p\n",
714 pCache, uOffset, pvBuf, cbRead, pcbRead));
715
716 AssertPtr(pCache);
717 AssertPtr(pcbRead);
718
719 rc = pCache->Backend->pfnRead(pCache->pBackendData, uOffset, pvBuf,
720 cbRead, pcbRead);
721
722 LogFlowFunc(("returns rc=%Rrc pcbRead=%zu\n", rc, *pcbRead));
723 return rc;
724}
725
726/**
727 * Internal: Writes data for the given block into the cache.
728 *
729 * @returns VBox status code.
730 * @param pCache The cache to write to.
731 * @param uOffset Offset of the virtual disk to write to teh cache.
732 * @param pcvBuf The data to write.
733 * @param cbWrite How much to write.
734 * @param pcbWritten How much data could be written, optional.
735 */
736static int vdCacheWriteHelper(PVDCACHE pCache, uint64_t uOffset, const void *pcvBuf,
737 size_t cbWrite, size_t *pcbWritten)
738{
739 int rc = VINF_SUCCESS;
740
741 LogFlowFunc(("pCache=%#p uOffset=%llu pvBuf=%#p cbWrite=%zu pcbWritten=%#p\n",
742 pCache, uOffset, pcvBuf, cbWrite, pcbWritten));
743
744 AssertPtr(pCache);
745 AssertPtr(pcvBuf);
746 Assert(cbWrite > 0);
747
748 if (pcbWritten)
749 rc = pCache->Backend->pfnWrite(pCache->pBackendData, uOffset, pcvBuf,
750 cbWrite, pcbWritten);
751 else
752 {
753 size_t cbWritten = 0;
754
755 do
756 {
757 rc = pCache->Backend->pfnWrite(pCache->pBackendData, uOffset, pcvBuf,
758 cbWrite, &cbWritten);
759 uOffset += cbWritten;
760 pcvBuf = (char *)pcvBuf + cbWritten;
761 cbWrite -= cbWritten;
762 } while ( cbWrite
763 && RT_SUCCESS(rc));
764 }
765
766 LogFlowFunc(("returns rc=%Rrc pcbWritten=%zu\n",
767 rc, pcbWritten ? *pcbWritten : cbWrite));
768 return rc;
769}
770
771/**
772 * Internal: Reads a given amount of data from the image chain of the disk.
773 **/
774static int vdDiskReadHelper(PVBOXHDD pDisk, PVDIMAGE pImage, PVDIMAGE pImageParentOverride,
775 uint64_t uOffset, void *pvBuf, size_t cbRead, size_t *pcbThisRead)
776{
777 int rc = VINF_SUCCESS;
778 size_t cbThisRead = cbRead;
779
780 AssertPtr(pcbThisRead);
781
782 *pcbThisRead = 0;
783
784 /*
785 * Try to read from the given image.
786 * If the block is not allocated read from override chain if present.
787 */
788 rc = pImage->Backend->pfnRead(pImage->pBackendData,
789 uOffset, pvBuf, cbThisRead,
790 &cbThisRead);
791
792 if (rc == VERR_VD_BLOCK_FREE)
793 {
794 for (PVDIMAGE pCurrImage = pImageParentOverride ? pImageParentOverride : pImage->pPrev;
795 pCurrImage != NULL && rc == VERR_VD_BLOCK_FREE;
796 pCurrImage = pCurrImage->pPrev)
797 {
798 rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
799 uOffset, pvBuf, cbThisRead,
800 &cbThisRead);
801 }
802 }
803
804 if (RT_SUCCESS(rc) || rc == VERR_VD_BLOCK_FREE)
805 *pcbThisRead = cbThisRead;
806
807 return rc;
808}
809
810/**
811 * internal: read the specified amount of data in whatever blocks the backend
812 * will give us.
813 */
814static int vdReadHelper(PVBOXHDD pDisk, PVDIMAGE pImage, PVDIMAGE pImageParentOverride,
815 uint64_t uOffset, void *pvBuf, size_t cbRead,
816 bool fZeroFreeBlocks, bool fUpdateCache)
817{
818 int rc = VINF_SUCCESS;
819 size_t cbThisRead;
820 bool fAllFree = true;
821 size_t cbBufClear = 0;
822
823 /* Loop until all read. */
824 do
825 {
826 /* Search for image with allocated block. Do not attempt to read more
827 * than the previous reads marked as valid. Otherwise this would return
828 * stale data when different block sizes are used for the images. */
829 cbThisRead = cbRead;
830
831 if ( pDisk->pCache
832 && !pImageParentOverride)
833 {
834 rc = vdCacheReadHelper(pDisk->pCache, uOffset, pvBuf,
835 cbThisRead, &cbThisRead);
836
837 if (rc == VERR_VD_BLOCK_FREE)
838 {
839 rc = vdDiskReadHelper(pDisk, pImage, pImageParentOverride,
840 uOffset, pvBuf, cbThisRead, &cbThisRead);
841
842 /* If the read was successful, write the data back into the cache. */
843 if ( RT_SUCCESS(rc)
844 && fUpdateCache)
845 {
846 rc = vdCacheWriteHelper(pDisk->pCache, uOffset, pvBuf,
847 cbThisRead, NULL);
848 }
849 }
850 }
851 else
852 {
853 /** @todo can be be replaced by vdDiskReadHelper if it proves to be reliable,
854 * don't want to be responsible for data corruption...
855 */
856 /*
857 * Try to read from the given image.
858 * If the block is not allocated read from override chain if present.
859 */
860 rc = pImage->Backend->pfnRead(pImage->pBackendData,
861 uOffset, pvBuf, cbThisRead,
862 &cbThisRead);
863
864 if (rc == VERR_VD_BLOCK_FREE)
865 {
866 for (PVDIMAGE pCurrImage = pImageParentOverride ? pImageParentOverride : pImage->pPrev;
867 pCurrImage != NULL && rc == VERR_VD_BLOCK_FREE;
868 pCurrImage = pCurrImage->pPrev)
869 {
870 rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
871 uOffset, pvBuf, cbThisRead,
872 &cbThisRead);
873 }
874 }
875 }
876
877 /* No image in the chain contains the data for the block. */
878 if (rc == VERR_VD_BLOCK_FREE)
879 {
880 /* Fill the free space with 0 if we are told to do so
881 * or a previous read returned valid data. */
882 if (fZeroFreeBlocks || !fAllFree)
883 memset(pvBuf, '\0', cbThisRead);
884 else
885 cbBufClear += cbThisRead;
886
887 rc = VINF_SUCCESS;
888 }
889 else if (RT_SUCCESS(rc))
890 {
891 /* First not free block, fill the space before with 0. */
892 if (!fZeroFreeBlocks)
893 {
894 memset((char *)pvBuf - cbBufClear, '\0', cbBufClear);
895 cbBufClear = 0;
896 fAllFree = false;
897 }
898 }
899
900 cbRead -= cbThisRead;
901 uOffset += cbThisRead;
902 pvBuf = (char *)pvBuf + cbThisRead;
903 } while (cbRead != 0 && RT_SUCCESS(rc));
904
905 return (!fZeroFreeBlocks && fAllFree) ? VERR_VD_BLOCK_FREE : rc;
906}
907
908DECLINLINE(PVDIOCTX) vdIoCtxAlloc(PVBOXHDD pDisk, VDIOCTXTXDIR enmTxDir,
909 uint64_t uOffset, size_t cbTransfer,
910 PVDIMAGE pImageStart,
911 PCRTSGBUF pcSgBuf, void *pvAllocation,
912 PFNVDIOCTXTRANSFER pfnIoCtxTransfer)
913{
914 PVDIOCTX pIoCtx = NULL;
915
916 pIoCtx = (PVDIOCTX)RTMemCacheAlloc(pDisk->hMemCacheIoCtx);
917 if (RT_LIKELY(pIoCtx))
918 {
919 pIoCtx->pDisk = pDisk;
920 pIoCtx->enmTxDir = enmTxDir;
921 pIoCtx->cbTransferLeft = cbTransfer;
922 pIoCtx->uOffset = uOffset;
923 pIoCtx->cbTransfer = cbTransfer;
924 pIoCtx->pImageStart = pImageStart;
925 pIoCtx->pImageCur = pImageStart;
926 pIoCtx->cDataTransfersPending = 0;
927 pIoCtx->cMetaTransfersPending = 0;
928 pIoCtx->fComplete = false;
929 pIoCtx->fBlocked = false;
930 pIoCtx->pvAllocation = pvAllocation;
931 pIoCtx->pfnIoCtxTransfer = pfnIoCtxTransfer;
932 pIoCtx->pfnIoCtxTransferNext = NULL;
933 pIoCtx->rcReq = VINF_SUCCESS;
934
935 /* There is no S/G list for a flush request. */
936 if (enmTxDir != VDIOCTXTXDIR_FLUSH)
937 RTSgBufClone(&pIoCtx->SgBuf, pcSgBuf);
938 else
939 memset(&pIoCtx->SgBuf, 0, sizeof(RTSGBUF));
940 }
941
942 return pIoCtx;
943}
944
945DECLINLINE(PVDIOCTX) vdIoCtxRootAlloc(PVBOXHDD pDisk, VDIOCTXTXDIR enmTxDir,
946 uint64_t uOffset, size_t cbTransfer,
947 PVDIMAGE pImageStart, PCRTSGBUF pcSgBuf,
948 PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
949 void *pvUser1, void *pvUser2,
950 void *pvAllocation,
951 PFNVDIOCTXTRANSFER pfnIoCtxTransfer)
952{
953 PVDIOCTX pIoCtx = vdIoCtxAlloc(pDisk, enmTxDir, uOffset, cbTransfer, pImageStart,
954 pcSgBuf, pvAllocation, pfnIoCtxTransfer);
955
956 if (RT_LIKELY(pIoCtx))
957 {
958 pIoCtx->pIoCtxParent = NULL;
959 pIoCtx->Type.Root.pfnComplete = pfnComplete;
960 pIoCtx->Type.Root.pvUser1 = pvUser1;
961 pIoCtx->Type.Root.pvUser2 = pvUser2;
962 }
963
964 LogFlow(("Allocated root I/O context %#p\n", pIoCtx));
965 return pIoCtx;
966}
967
968DECLINLINE(PVDIOCTX) vdIoCtxChildAlloc(PVBOXHDD pDisk, VDIOCTXTXDIR enmTxDir,
969 uint64_t uOffset, size_t cbTransfer,
970 PVDIMAGE pImageStart, PCRTSGBUF pcSgBuf,
971 PVDIOCTX pIoCtxParent, size_t cbTransferParent,
972 size_t cbWriteParent, void *pvAllocation,
973 PFNVDIOCTXTRANSFER pfnIoCtxTransfer)
974{
975 PVDIOCTX pIoCtx = vdIoCtxAlloc(pDisk, enmTxDir, uOffset, cbTransfer, pImageStart,
976 pcSgBuf, pvAllocation, pfnIoCtxTransfer);
977
978 AssertPtr(pIoCtxParent);
979 Assert(!pIoCtxParent->pIoCtxParent);
980
981 if (RT_LIKELY(pIoCtx))
982 {
983 pIoCtx->pIoCtxParent = pIoCtxParent;
984 pIoCtx->Type.Child.uOffsetSaved = uOffset;
985 pIoCtx->Type.Child.cbTransferLeftSaved = cbTransfer;
986 pIoCtx->Type.Child.cbTransferParent = cbTransferParent;
987 pIoCtx->Type.Child.cbWriteParent = cbWriteParent;
988 }
989
990 LogFlow(("Allocated child I/O context %#p\n", pIoCtx));
991 return pIoCtx;
992}
993
994DECLINLINE(PVDIOTASK) vdIoTaskUserAlloc(PVDIOSTORAGE pIoStorage, PFNVDXFERCOMPLETED pfnComplete, void *pvUser, PVDIOCTX pIoCtx, uint32_t cbTransfer)
995{
996 PVDIOTASK pIoTask = NULL;
997
998 pIoTask = (PVDIOTASK)RTMemCacheAlloc(pIoStorage->pVDIo->pDisk->hMemCacheIoTask);
999 if (pIoTask)
1000 {
1001 pIoTask->pIoStorage = pIoStorage;
1002 pIoTask->pfnComplete = pfnComplete;
1003 pIoTask->pvUser = pvUser;
1004 pIoTask->fMeta = false;
1005 pIoTask->Type.User.cbTransfer = cbTransfer;
1006 pIoTask->Type.User.pIoCtx = pIoCtx;
1007 }
1008
1009 return pIoTask;
1010}
1011
1012DECLINLINE(PVDIOTASK) vdIoTaskMetaAlloc(PVDIOSTORAGE pIoStorage, PFNVDXFERCOMPLETED pfnComplete, void *pvUser, PVDMETAXFER pMetaXfer)
1013{
1014 PVDIOTASK pIoTask = NULL;
1015
1016 pIoTask = (PVDIOTASK)RTMemCacheAlloc(pIoStorage->pVDIo->pDisk->hMemCacheIoTask);
1017 if (pIoTask)
1018 {
1019 pIoTask->pIoStorage = pIoStorage;
1020 pIoTask->pfnComplete = pfnComplete;
1021 pIoTask->pvUser = pvUser;
1022 pIoTask->fMeta = true;
1023 pIoTask->Type.Meta.pMetaXfer = pMetaXfer;
1024 }
1025
1026 return pIoTask;
1027}
1028
1029DECLINLINE(void) vdIoCtxFree(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
1030{
1031 LogFlow(("Freeing I/O context %#p\n", pIoCtx));
1032 if (pIoCtx->pvAllocation)
1033 RTMemFree(pIoCtx->pvAllocation);
1034#ifdef DEBUG
1035 memset(pIoCtx, 0xff, sizeof(VDIOCTX));
1036#endif
1037 RTMemCacheFree(pDisk->hMemCacheIoCtx, pIoCtx);
1038}
1039
1040DECLINLINE(void) vdIoTaskFree(PVBOXHDD pDisk, PVDIOTASK pIoTask)
1041{
1042 RTMemCacheFree(pDisk->hMemCacheIoTask, pIoTask);
1043}
1044
1045DECLINLINE(void) vdIoCtxChildReset(PVDIOCTX pIoCtx)
1046{
1047 AssertPtr(pIoCtx->pIoCtxParent);
1048
1049 RTSgBufReset(&pIoCtx->SgBuf);
1050 pIoCtx->uOffset = pIoCtx->Type.Child.uOffsetSaved;
1051 pIoCtx->cbTransferLeft = pIoCtx->Type.Child.cbTransferLeftSaved;
1052}
1053
1054DECLINLINE(PVDMETAXFER) vdMetaXferAlloc(PVDIOSTORAGE pIoStorage, uint64_t uOffset, size_t cb)
1055{
1056 PVDMETAXFER pMetaXfer = (PVDMETAXFER)RTMemAlloc(RT_OFFSETOF(VDMETAXFER, abData[cb]));
1057
1058 if (RT_LIKELY(pMetaXfer))
1059 {
1060 pMetaXfer->Core.Key = uOffset;
1061 pMetaXfer->Core.KeyLast = uOffset + cb - 1;
1062 pMetaXfer->fFlags = VDMETAXFER_TXDIR_NONE;
1063 pMetaXfer->cbMeta = cb;
1064 pMetaXfer->pIoStorage = pIoStorage;
1065 pMetaXfer->cRefs = 0;
1066 RTListInit(&pMetaXfer->ListIoCtxWaiting);
1067 }
1068 return pMetaXfer;
1069}
1070
1071DECLINLINE(int) vdIoCtxDefer(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
1072{
1073 PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
1074
1075 if (!pDeferred)
1076 return VERR_NO_MEMORY;
1077
1078 LogFlowFunc(("Deferring write pIoCtx=%#p\n", pIoCtx));
1079
1080 Assert(!pIoCtx->pIoCtxParent && !pIoCtx->fBlocked);
1081
1082 RTListInit(&pDeferred->NodeDeferred);
1083 pDeferred->pIoCtx = pIoCtx;
1084 RTListAppend(&pDisk->ListWriteLocked, &pDeferred->NodeDeferred);
1085 pIoCtx->fBlocked = true;
1086 return VINF_SUCCESS;
1087}
1088
1089static size_t vdIoCtxCopy(PVDIOCTX pIoCtxDst, PVDIOCTX pIoCtxSrc, size_t cbData)
1090{
1091 return RTSgBufCopy(&pIoCtxDst->SgBuf, &pIoCtxSrc->SgBuf, cbData);
1092}
1093
1094static int vdIoCtxCmp(PVDIOCTX pIoCtx1, PVDIOCTX pIoCtx2, size_t cbData)
1095{
1096 return RTSgBufCmp(&pIoCtx1->SgBuf, &pIoCtx2->SgBuf, cbData);
1097}
1098
1099static size_t vdIoCtxCopyTo(PVDIOCTX pIoCtx, uint8_t *pbData, size_t cbData)
1100{
1101 return RTSgBufCopyToBuf(&pIoCtx->SgBuf, pbData, cbData);
1102}
1103
1104
1105static size_t vdIoCtxCopyFrom(PVDIOCTX pIoCtx, uint8_t *pbData, size_t cbData)
1106{
1107 return RTSgBufCopyFromBuf(&pIoCtx->SgBuf, pbData, cbData);
1108}
1109
1110static size_t vdIoCtxSet(PVDIOCTX pIoCtx, uint8_t ch, size_t cbData)
1111{
1112 return RTSgBufSet(&pIoCtx->SgBuf, ch, cbData);
1113}
1114
1115static int vdIoCtxProcess(PVDIOCTX pIoCtx)
1116{
1117 int rc = VINF_SUCCESS;
1118 PVBOXHDD pDisk = pIoCtx->pDisk;
1119
1120 LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
1121
1122 RTCritSectEnter(&pDisk->CritSect);
1123
1124 if ( !pIoCtx->cbTransferLeft
1125 && !pIoCtx->cMetaTransfersPending
1126 && !pIoCtx->cDataTransfersPending
1127 && !pIoCtx->pfnIoCtxTransfer)
1128 {
1129 rc = VINF_VD_ASYNC_IO_FINISHED;
1130 goto out;
1131 }
1132
1133 /*
1134 * We complete the I/O context in case of an error
1135 * if there is no I/O task pending.
1136 */
1137 if ( RT_FAILURE(pIoCtx->rcReq)
1138 && !pIoCtx->cMetaTransfersPending
1139 && !pIoCtx->cDataTransfersPending)
1140 {
1141 rc = VINF_VD_ASYNC_IO_FINISHED;
1142 goto out;
1143 }
1144
1145 /* Don't change anything if there is a metadata transfer pending or we are blocked. */
1146 if ( pIoCtx->cMetaTransfersPending
1147 || pIoCtx->fBlocked)
1148 {
1149 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
1150 goto out;
1151 }
1152
1153 if (pIoCtx->pfnIoCtxTransfer)
1154 {
1155 /* Call the transfer function advancing to the next while there is no error. */
1156 while ( pIoCtx->pfnIoCtxTransfer
1157 && RT_SUCCESS(rc))
1158 {
1159 LogFlowFunc(("calling transfer function %#p\n", pIoCtx->pfnIoCtxTransfer));
1160 rc = pIoCtx->pfnIoCtxTransfer(pIoCtx);
1161
1162 /* Advance to the next part of the transfer if the current one succeeded. */
1163 if (RT_SUCCESS(rc))
1164 {
1165 pIoCtx->pfnIoCtxTransfer = pIoCtx->pfnIoCtxTransferNext;
1166 pIoCtx->pfnIoCtxTransferNext = NULL;
1167 }
1168 }
1169 }
1170
1171 if ( RT_SUCCESS(rc)
1172 && !pIoCtx->cbTransferLeft
1173 && !pIoCtx->cMetaTransfersPending
1174 && !pIoCtx->cDataTransfersPending)
1175 rc = VINF_VD_ASYNC_IO_FINISHED;
1176 else if ( RT_SUCCESS(rc)
1177 || rc == VERR_VD_NOT_ENOUGH_METADATA
1178 || rc == VERR_VD_IOCTX_HALT)
1179 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
1180 else if (RT_FAILURE(rc) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
1181 {
1182 ASMAtomicCmpXchgS32(&pIoCtx->rcReq, rc, VINF_SUCCESS);
1183 /*
1184 * The I/O context completed if we have an error and there is no data
1185 * or meta data transfer pending.
1186 */
1187 if ( !pIoCtx->cMetaTransfersPending
1188 && !pIoCtx->cDataTransfersPending)
1189 rc = VINF_VD_ASYNC_IO_FINISHED;
1190 else
1191 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
1192 }
1193
1194out:
1195 RTCritSectLeave(&pDisk->CritSect);
1196
1197 LogFlowFunc(("pIoCtx=%#p rc=%Rrc cbTransferLeft=%u cMetaTransfersPending=%u fComplete=%RTbool\n",
1198 pIoCtx, rc, pIoCtx->cbTransferLeft, pIoCtx->cMetaTransfersPending,
1199 pIoCtx->fComplete));
1200
1201 return rc;
1202}
1203
1204DECLINLINE(bool) vdIoCtxIsDiskLockOwner(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
1205{
1206 return pDisk->fLocked
1207 && pDisk->pIoCtxLockOwner == pIoCtx;
1208}
1209
1210static int vdIoCtxLockDisk(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
1211{
1212 int rc = VINF_SUCCESS;
1213
1214 LogFlowFunc(("pDisk=%#p pIoCtx=%#p\n", pDisk, pIoCtx));
1215
1216 if (!ASMAtomicCmpXchgBool(&pDisk->fLocked, true, false))
1217 {
1218 Assert(pDisk->pIoCtxLockOwner != pIoCtx); /* No nesting allowed. */
1219
1220 rc = vdIoCtxDefer(pDisk, pIoCtx);
1221 if (RT_SUCCESS(rc))
1222 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
1223 }
1224 else
1225 {
1226 Assert(!pDisk->pIoCtxLockOwner);
1227 pDisk->pIoCtxLockOwner = pIoCtx;
1228 }
1229
1230 LogFlowFunc(("returns -> %Rrc\n", rc));
1231 return rc;
1232}
1233
1234static void vdIoCtxUnlockDisk(PVBOXHDD pDisk, PVDIOCTX pIoCtx, bool fProcessDeferredReqs)
1235{
1236 LogFlowFunc(("pDisk=%#p pIoCtx=%#p fProcessDeferredReqs=%RTbool\n",
1237 pDisk, pIoCtx, fProcessDeferredReqs));
1238
1239 LogFlow(("Unlocking disk lock owner is %#p\n", pDisk->pIoCtxLockOwner));
1240 Assert(pDisk->fLocked);
1241 Assert(pDisk->pIoCtxLockOwner == pIoCtx);
1242 pDisk->pIoCtxLockOwner = NULL;
1243 ASMAtomicXchgBool(&pDisk->fLocked, false);
1244
1245 if (fProcessDeferredReqs)
1246 {
1247 /* Process any pending writes if the current request didn't caused another growing. */
1248 RTCritSectEnter(&pDisk->CritSect);
1249
1250 if (!RTListIsEmpty(&pDisk->ListWriteLocked))
1251 {
1252 RTLISTNODE ListTmp;
1253
1254 RTListMove(&ListTmp, &pDisk->ListWriteLocked);
1255 RTCritSectLeave(&pDisk->CritSect);
1256
1257 /* Process the list. */
1258 do
1259 {
1260 int rc;
1261 PVDIOCTXDEFERRED pDeferred = RTListGetFirst(&ListTmp, VDIOCTXDEFERRED, NodeDeferred);
1262 PVDIOCTX pIoCtxWait = pDeferred->pIoCtx;
1263
1264 AssertPtr(pIoCtxWait);
1265
1266 RTListNodeRemove(&pDeferred->NodeDeferred);
1267 RTMemFree(pDeferred);
1268
1269 Assert(!pIoCtxWait->pIoCtxParent);
1270
1271 pIoCtxWait->fBlocked = false;
1272 LogFlowFunc(("Processing waiting I/O context pIoCtxWait=%#p\n", pIoCtxWait));
1273
1274 rc = vdIoCtxProcess(pIoCtxWait);
1275 if ( rc == VINF_VD_ASYNC_IO_FINISHED
1276 && ASMAtomicCmpXchgBool(&pIoCtxWait->fComplete, true, false))
1277 {
1278 LogFlowFunc(("Waiting I/O context completed pIoCtxWait=%#p\n", pIoCtxWait));
1279 vdThreadFinishWrite(pDisk);
1280 pIoCtxWait->Type.Root.pfnComplete(pIoCtxWait->Type.Root.pvUser1,
1281 pIoCtxWait->Type.Root.pvUser2,
1282 pIoCtxWait->rcReq);
1283 vdIoCtxFree(pDisk, pIoCtxWait);
1284 }
1285 } while (!RTListIsEmpty(&ListTmp));
1286 }
1287 else
1288 RTCritSectLeave(&pDisk->CritSect);
1289 }
1290
1291 LogFlowFunc(("returns\n"));
1292}
1293
1294/**
1295 * internal: read the specified amount of data in whatever blocks the backend
1296 * will give us - async version.
1297 */
1298static int vdReadHelperAsync(PVDIOCTX pIoCtx)
1299{
1300 int rc;
1301 size_t cbToRead = pIoCtx->cbTransfer;
1302 uint64_t uOffset = pIoCtx->uOffset;
1303 PVDIMAGE pCurrImage = NULL;
1304 size_t cbThisRead;
1305
1306 /* Loop until all reads started or we have a backend which needs to read metadata. */
1307 do
1308 {
1309 pCurrImage = pIoCtx->pImageCur;
1310
1311 /* Search for image with allocated block. Do not attempt to read more
1312 * than the previous reads marked as valid. Otherwise this would return
1313 * stale data when different block sizes are used for the images. */
1314 cbThisRead = cbToRead;
1315
1316 /*
1317 * Try to read from the given image.
1318 * If the block is not allocated read from override chain if present.
1319 */
1320 rc = pCurrImage->Backend->pfnAsyncRead(pCurrImage->pBackendData,
1321 uOffset, cbThisRead,
1322 pIoCtx, &cbThisRead);
1323
1324 if (rc == VERR_VD_BLOCK_FREE)
1325 {
1326 while ( pCurrImage->pPrev != NULL
1327 && rc == VERR_VD_BLOCK_FREE)
1328 {
1329 pCurrImage = pCurrImage->pPrev;
1330 rc = pCurrImage->Backend->pfnAsyncRead(pCurrImage->pBackendData,
1331 uOffset, cbThisRead,
1332 pIoCtx, &cbThisRead);
1333 }
1334 }
1335
1336 /* The task state will be updated on success already, don't do it here!. */
1337 if (rc == VERR_VD_BLOCK_FREE)
1338 {
1339 /* No image in the chain contains the data for the block. */
1340 vdIoCtxSet(pIoCtx, '\0', cbThisRead);
1341 ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbThisRead);
1342 rc = VINF_SUCCESS;
1343 }
1344 else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1345 rc = VINF_SUCCESS;
1346 else if (rc == VERR_VD_IOCTX_HALT)
1347 {
1348 uOffset += cbThisRead;
1349 cbToRead -= cbThisRead;
1350 pIoCtx->fBlocked = true;
1351 }
1352
1353 if (RT_FAILURE(rc))
1354 break;
1355
1356 cbToRead -= cbThisRead;
1357 uOffset += cbThisRead;
1358 } while (cbToRead != 0 && RT_SUCCESS(rc));
1359
1360 if ( rc == VERR_VD_NOT_ENOUGH_METADATA
1361 || rc == VERR_VD_IOCTX_HALT)
1362 {
1363 /* Save the current state. */
1364 pIoCtx->uOffset = uOffset;
1365 pIoCtx->cbTransfer = cbToRead;
1366 pIoCtx->pImageCur = pCurrImage ? pCurrImage : pIoCtx->pImageStart;
1367 }
1368
1369 return rc;
1370}
1371
1372/**
1373 * internal: parent image read wrapper for compacting.
1374 */
1375static int vdParentRead(void *pvUser, uint64_t uOffset, void *pvBuf,
1376 size_t cbRead)
1377{
1378 PVDPARENTSTATEDESC pParentState = (PVDPARENTSTATEDESC)pvUser;
1379 return vdReadHelper(pParentState->pDisk, pParentState->pImage, NULL, uOffset,
1380 pvBuf, cbRead, true /* fZeroFreeBlocks */,
1381 false /* fUpdateCache */);
1382}
1383
1384/**
1385 * internal: mark the disk as not modified.
1386 */
1387static void vdResetModifiedFlag(PVBOXHDD pDisk)
1388{
1389 if (pDisk->uModified & VD_IMAGE_MODIFIED_FLAG)
1390 {
1391 /* generate new last-modified uuid */
1392 if (!(pDisk->uModified & VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
1393 {
1394 RTUUID Uuid;
1395
1396 RTUuidCreate(&Uuid);
1397 pDisk->pLast->Backend->pfnSetModificationUuid(pDisk->pLast->pBackendData,
1398 &Uuid);
1399
1400 if (pDisk->pCache)
1401 pDisk->pCache->Backend->pfnSetModificationUuid(pDisk->pCache->pBackendData,
1402 &Uuid);
1403 }
1404
1405 pDisk->uModified &= ~VD_IMAGE_MODIFIED_FLAG;
1406 }
1407}
1408
1409/**
1410 * internal: mark the disk as modified.
1411 */
1412static void vdSetModifiedFlag(PVBOXHDD pDisk)
1413{
1414 pDisk->uModified |= VD_IMAGE_MODIFIED_FLAG;
1415 if (pDisk->uModified & VD_IMAGE_MODIFIED_FIRST)
1416 {
1417 pDisk->uModified &= ~VD_IMAGE_MODIFIED_FIRST;
1418
1419 /* First modify, so create a UUID and ensure it's written to disk. */
1420 vdResetModifiedFlag(pDisk);
1421
1422 if (!(pDisk->uModified & VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
1423 pDisk->pLast->Backend->pfnFlush(pDisk->pLast->pBackendData);
1424 }
1425}
1426
1427/**
1428 * internal: write a complete block (only used for diff images), taking the
1429 * remaining data from parent images. This implementation does not optimize
1430 * anything (except that it tries to read only that portions from parent
1431 * images that are really needed).
1432 */
1433static int vdWriteHelperStandard(PVBOXHDD pDisk, PVDIMAGE pImage,
1434 PVDIMAGE pImageParentOverride,
1435 uint64_t uOffset, size_t cbWrite,
1436 size_t cbThisWrite, size_t cbPreRead,
1437 size_t cbPostRead, const void *pvBuf,
1438 void *pvTmp)
1439{
1440 int rc = VINF_SUCCESS;
1441
1442 /* Read the data that goes before the write to fill the block. */
1443 if (cbPreRead)
1444 {
1445 /*
1446 * Updating the cache doesn't make sense here because
1447 * this will be done after the complete block was written.
1448 */
1449 rc = vdReadHelper(pDisk, pImage, pImageParentOverride,
1450 uOffset - cbPreRead, pvTmp, cbPreRead,
1451 true /* fZeroFreeBlocks*/,
1452 false /* fUpdateCache */);
1453 if (RT_FAILURE(rc))
1454 return rc;
1455 }
1456
1457 /* Copy the data to the right place in the buffer. */
1458 memcpy((char *)pvTmp + cbPreRead, pvBuf, cbThisWrite);
1459
1460 /* Read the data that goes after the write to fill the block. */
1461 if (cbPostRead)
1462 {
1463 /* If we have data to be written, use that instead of reading
1464 * data from the image. */
1465 size_t cbWriteCopy;
1466 if (cbWrite > cbThisWrite)
1467 cbWriteCopy = RT_MIN(cbWrite - cbThisWrite, cbPostRead);
1468 else
1469 cbWriteCopy = 0;
1470 /* Figure out how much we cannot read from the image, because
1471 * the last block to write might exceed the nominal size of the
1472 * image for technical reasons. */
1473 size_t cbFill;
1474 if (uOffset + cbThisWrite + cbPostRead > pDisk->cbSize)
1475 cbFill = uOffset + cbThisWrite + cbPostRead - pDisk->cbSize;
1476 else
1477 cbFill = 0;
1478 /* The rest must be read from the image. */
1479 size_t cbReadImage = cbPostRead - cbWriteCopy - cbFill;
1480
1481 /* Now assemble the remaining data. */
1482 if (cbWriteCopy)
1483 memcpy((char *)pvTmp + cbPreRead + cbThisWrite,
1484 (char *)pvBuf + cbThisWrite, cbWriteCopy);
1485 if (cbReadImage)
1486 rc = vdReadHelper(pDisk, pImage, pImageParentOverride,
1487 uOffset + cbThisWrite + cbWriteCopy,
1488 (char *)pvTmp + cbPreRead + cbThisWrite + cbWriteCopy,
1489 cbReadImage, true /* fZeroFreeBlocks */,
1490 false /* fUpdateCache */);
1491 if (RT_FAILURE(rc))
1492 return rc;
1493 /* Zero out the remainder of this block. Will never be visible, as this
1494 * is beyond the limit of the image. */
1495 if (cbFill)
1496 memset((char *)pvTmp + cbPreRead + cbThisWrite + cbWriteCopy + cbReadImage,
1497 '\0', cbFill);
1498 }
1499
1500 /* Write the full block to the virtual disk. */
1501 rc = pImage->Backend->pfnWrite(pImage->pBackendData,
1502 uOffset - cbPreRead, pvTmp,
1503 cbPreRead + cbThisWrite + cbPostRead,
1504 NULL, &cbPreRead, &cbPostRead, 0);
1505 Assert(rc != VERR_VD_BLOCK_FREE);
1506 Assert(cbPreRead == 0);
1507 Assert(cbPostRead == 0);
1508
1509 return rc;
1510}
1511
1512/**
1513 * internal: write a complete block (only used for diff images), taking the
1514 * remaining data from parent images. This implementation optimizes out writes
1515 * that do not change the data relative to the state as of the parent images.
1516 * All backends which support differential/growing images support this.
1517 */
1518static int vdWriteHelperOptimized(PVBOXHDD pDisk, PVDIMAGE pImage,
1519 PVDIMAGE pImageParentOverride,
1520 uint64_t uOffset, size_t cbWrite,
1521 size_t cbThisWrite, size_t cbPreRead,
1522 size_t cbPostRead, const void *pvBuf,
1523 void *pvTmp)
1524{
1525 size_t cbFill = 0;
1526 size_t cbWriteCopy = 0;
1527 size_t cbReadImage = 0;
1528 int rc;
1529
1530 if (cbPostRead)
1531 {
1532 /* Figure out how much we cannot read from the image, because
1533 * the last block to write might exceed the nominal size of the
1534 * image for technical reasons. */
1535 if (uOffset + cbThisWrite + cbPostRead > pDisk->cbSize)
1536 cbFill = uOffset + cbThisWrite + cbPostRead - pDisk->cbSize;
1537
1538 /* If we have data to be written, use that instead of reading
1539 * data from the image. */
1540 if (cbWrite > cbThisWrite)
1541 cbWriteCopy = RT_MIN(cbWrite - cbThisWrite, cbPostRead);
1542
1543 /* The rest must be read from the image. */
1544 cbReadImage = cbPostRead - cbWriteCopy - cbFill;
1545 }
1546
1547 /* Read the entire data of the block so that we can compare whether it will
1548 * be modified by the write or not. */
1549 rc = vdReadHelper(pDisk, pImage, pImageParentOverride, uOffset - cbPreRead, pvTmp,
1550 cbPreRead + cbThisWrite + cbPostRead - cbFill,
1551 true /* fZeroFreeBlocks */,
1552 false /* fUpdateCache */);
1553 if (RT_FAILURE(rc))
1554 return rc;
1555
1556 /* Check if the write would modify anything in this block. */
1557 if ( !memcmp((char *)pvTmp + cbPreRead, pvBuf, cbThisWrite)
1558 && (!cbWriteCopy || !memcmp((char *)pvTmp + cbPreRead + cbThisWrite,
1559 (char *)pvBuf + cbThisWrite, cbWriteCopy)))
1560 {
1561 /* Block is completely unchanged, so no need to write anything. */
1562 return VINF_SUCCESS;
1563 }
1564
1565 /* Copy the data to the right place in the buffer. */
1566 memcpy((char *)pvTmp + cbPreRead, pvBuf, cbThisWrite);
1567
1568 /* Handle the data that goes after the write to fill the block. */
1569 if (cbPostRead)
1570 {
1571 /* Now assemble the remaining data. */
1572 if (cbWriteCopy)
1573 memcpy((char *)pvTmp + cbPreRead + cbThisWrite,
1574 (char *)pvBuf + cbThisWrite, cbWriteCopy);
1575 /* Zero out the remainder of this block. Will never be visible, as this
1576 * is beyond the limit of the image. */
1577 if (cbFill)
1578 memset((char *)pvTmp + cbPreRead + cbThisWrite + cbWriteCopy + cbReadImage,
1579 '\0', cbFill);
1580 }
1581
1582 /* Write the full block to the virtual disk. */
1583 rc = pImage->Backend->pfnWrite(pImage->pBackendData,
1584 uOffset - cbPreRead, pvTmp,
1585 cbPreRead + cbThisWrite + cbPostRead,
1586 NULL, &cbPreRead, &cbPostRead, 0);
1587 Assert(rc != VERR_VD_BLOCK_FREE);
1588 Assert(cbPreRead == 0);
1589 Assert(cbPostRead == 0);
1590
1591 return rc;
1592}
1593
1594/**
1595 * internal: write buffer to the image, taking care of block boundaries and
1596 * write optimizations.
1597 */
1598static int vdWriteHelper(PVBOXHDD pDisk, PVDIMAGE pImage,
1599 PVDIMAGE pImageParentOverride, uint64_t uOffset,
1600 const void *pvBuf, size_t cbWrite,
1601 bool fUpdateCache)
1602{
1603 int rc;
1604 unsigned fWrite;
1605 size_t cbThisWrite;
1606 size_t cbPreRead, cbPostRead;
1607 uint64_t uOffsetCur = uOffset;
1608 size_t cbWriteCur = cbWrite;
1609 const void *pcvBufCur = pvBuf;
1610
1611 /* Loop until all written. */
1612 do
1613 {
1614 /* Try to write the possibly partial block to the last opened image.
1615 * This works when the block is already allocated in this image or
1616 * if it is a full-block write (and allocation isn't suppressed below).
1617 * For image formats which don't support zero blocks, it's beneficial
1618 * to avoid unnecessarily allocating unchanged blocks. This prevents
1619 * unwanted expanding of images. VMDK is an example. */
1620 cbThisWrite = cbWriteCur;
1621 fWrite = (pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME)
1622 ? 0 : VD_WRITE_NO_ALLOC;
1623 rc = pImage->Backend->pfnWrite(pImage->pBackendData, uOffsetCur, pcvBufCur,
1624 cbThisWrite, &cbThisWrite, &cbPreRead,
1625 &cbPostRead, fWrite);
1626 if (rc == VERR_VD_BLOCK_FREE)
1627 {
1628 void *pvTmp = RTMemTmpAlloc(cbPreRead + cbThisWrite + cbPostRead);
1629 AssertBreakStmt(VALID_PTR(pvTmp), rc = VERR_NO_MEMORY);
1630
1631 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME))
1632 {
1633 /* Optimized write, suppress writing to a so far unallocated
1634 * block if the data is in fact not changed. */
1635 rc = vdWriteHelperOptimized(pDisk, pImage, pImageParentOverride,
1636 uOffsetCur, cbWriteCur,
1637 cbThisWrite, cbPreRead, cbPostRead,
1638 pcvBufCur, pvTmp);
1639 }
1640 else
1641 {
1642 /* Normal write, not optimized in any way. The block will
1643 * be written no matter what. This will usually (unless the
1644 * backend has some further optimization enabled) cause the
1645 * block to be allocated. */
1646 rc = vdWriteHelperStandard(pDisk, pImage, pImageParentOverride,
1647 uOffsetCur, cbWriteCur,
1648 cbThisWrite, cbPreRead, cbPostRead,
1649 pcvBufCur, pvTmp);
1650 }
1651 RTMemTmpFree(pvTmp);
1652 if (RT_FAILURE(rc))
1653 break;
1654 }
1655
1656 cbWriteCur -= cbThisWrite;
1657 uOffsetCur += cbThisWrite;
1658 pcvBufCur = (char *)pcvBufCur + cbThisWrite;
1659 } while (cbWriteCur != 0 && RT_SUCCESS(rc));
1660
1661 /* Update the cache on success */
1662 if ( RT_SUCCESS(rc)
1663 && pDisk->pCache
1664 && fUpdateCache)
1665 rc = vdCacheWriteHelper(pDisk->pCache, uOffset, pvBuf, cbWrite, NULL);
1666
1667 return rc;
1668}
1669
1670/**
1671 * Flush helper async version.
1672 */
1673static int vdSetModifiedHelperAsync(PVDIOCTX pIoCtx)
1674{
1675 int rc = VINF_SUCCESS;
1676 PVBOXHDD pDisk = pIoCtx->pDisk;
1677 PVDIMAGE pImage = pIoCtx->pImageCur;
1678
1679 rc = pImage->Backend->pfnAsyncFlush(pImage->pBackendData, pIoCtx);
1680 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1681 rc = VINF_SUCCESS;
1682
1683 return rc;
1684}
1685
1686/**
1687 * internal: mark the disk as modified - async version.
1688 */
1689static int vdSetModifiedFlagAsync(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
1690{
1691 int rc = VINF_SUCCESS;
1692
1693 pDisk->uModified |= VD_IMAGE_MODIFIED_FLAG;
1694 if (pDisk->uModified & VD_IMAGE_MODIFIED_FIRST)
1695 {
1696 rc = vdIoCtxLockDisk(pDisk, pIoCtx);
1697 if (RT_SUCCESS(rc))
1698 {
1699 pDisk->uModified &= ~VD_IMAGE_MODIFIED_FIRST;
1700
1701 /* First modify, so create a UUID and ensure it's written to disk. */
1702 vdResetModifiedFlag(pDisk);
1703
1704 if (!(pDisk->uModified & VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
1705 {
1706 PVDIOCTX pIoCtxFlush = vdIoCtxChildAlloc(pDisk, VDIOCTXTXDIR_FLUSH,
1707 0, 0, pDisk->pLast,
1708 NULL, pIoCtx, 0, 0, NULL,
1709 vdSetModifiedHelperAsync);
1710
1711 if (pIoCtxFlush)
1712 {
1713 rc = vdIoCtxProcess(pIoCtxFlush);
1714 if (rc == VINF_VD_ASYNC_IO_FINISHED)
1715 {
1716 vdIoCtxUnlockDisk(pDisk, pIoCtx, false /* fProcessDeferredReqs */);
1717 vdIoCtxFree(pDisk, pIoCtxFlush);
1718 }
1719 else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1720 {
1721 pIoCtx->fBlocked = true;
1722 }
1723 else /* Another error */
1724 vdIoCtxFree(pDisk, pIoCtxFlush);
1725 }
1726 else
1727 rc = VERR_NO_MEMORY;
1728 }
1729 }
1730 }
1731
1732 return rc;
1733}
1734
1735/**
1736 * internal: write a complete block (only used for diff images), taking the
1737 * remaining data from parent images. This implementation does not optimize
1738 * anything (except that it tries to read only that portions from parent
1739 * images that are really needed) - async version.
1740 */
1741static int vdWriteHelperStandardAsync(PVDIOCTX pIoCtx)
1742{
1743 int rc = VINF_SUCCESS;
1744
1745#if 0
1746
1747 /* Read the data that goes before the write to fill the block. */
1748 if (cbPreRead)
1749 {
1750 rc = vdReadHelperAsync(pIoCtxDst);
1751 if (RT_FAILURE(rc))
1752 return rc;
1753 }
1754
1755 /* Copy the data to the right place in the buffer. */
1756 vdIoCtxCopy(pIoCtxDst, pIoCtxSrc, cbThisWrite);
1757
1758 /* Read the data that goes after the write to fill the block. */
1759 if (cbPostRead)
1760 {
1761 /* If we have data to be written, use that instead of reading
1762 * data from the image. */
1763 size_t cbWriteCopy;
1764 if (cbWrite > cbThisWrite)
1765 cbWriteCopy = RT_MIN(cbWrite - cbThisWrite, cbPostRead);
1766 else
1767 cbWriteCopy = 0;
1768 /* Figure out how much we cannot read from the image, because
1769 * the last block to write might exceed the nominal size of the
1770 * image for technical reasons. */
1771 size_t cbFill;
1772 if (uOffset + cbThisWrite + cbPostRead > pDisk->cbSize)
1773 cbFill = uOffset + cbThisWrite + cbPostRead - pDisk->cbSize;
1774 else
1775 cbFill = 0;
1776 /* The rest must be read from the image. */
1777 size_t cbReadImage = cbPostRead - cbWriteCopy - cbFill;
1778
1779 /* Now assemble the remaining data. */
1780 if (cbWriteCopy)
1781 {
1782 vdIoCtxCopy(pIoCtxDst, pIoCtxSrc, cbWriteCopy);
1783 ASMAtomicSubU32(&pIoCtxDst->cbTransferLeft, cbWriteCopy);
1784 }
1785
1786 if (cbReadImage)
1787 rc = vdReadHelperAsync(pDisk, pImage, pImageParentOverride, pIoCtxDst,
1788 uOffset + cbThisWrite + cbWriteCopy,
1789 cbReadImage);
1790 if (RT_FAILURE(rc))
1791 return rc;
1792 /* Zero out the remainder of this block. Will never be visible, as this
1793 * is beyond the limit of the image. */
1794 if (cbFill)
1795 {
1796 vdIoCtxSet(pIoCtxDst, '\0', cbFill);
1797 ASMAtomicSubU32(&pIoCtxDst->cbTransferLeft, cbFill);
1798 }
1799 }
1800
1801 if ( !pIoCtxDst->cbTransferLeft
1802 && !pIoCtxDst->cMetaTransfersPending
1803 && ASMAtomicCmpXchgBool(&pIoCtxDst->fComplete, true, false))
1804 {
1805 /* Write the full block to the virtual disk. */
1806 vdIoCtxChildReset(pIoCtxDst);
1807 rc = pImage->Backend->pfnAsyncWrite(pImage->pBackendData,
1808 uOffset - cbPreRead,
1809 cbPreRead + cbThisWrite + cbPostRead,
1810 pIoCtxDst,
1811 NULL, &cbPreRead, &cbPostRead, 0);
1812 Assert(rc != VERR_VD_BLOCK_FREE);
1813 Assert(cbPreRead == 0);
1814 Assert(cbPostRead == 0);
1815 }
1816 else
1817 {
1818 LogFlow(("cbTransferLeft=%u cMetaTransfersPending=%u fComplete=%RTbool\n",
1819 pIoCtxDst->cbTransferLeft, pIoCtxDst->cMetaTransfersPending,
1820 pIoCtxDst->fComplete));
1821 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
1822 }
1823
1824 return rc;
1825#endif
1826 return VERR_NOT_IMPLEMENTED;
1827}
1828
1829static int vdWriteHelperOptimizedCommitAsync(PVDIOCTX pIoCtx)
1830{
1831 int rc = VINF_SUCCESS;
1832 PVDIMAGE pImage = pIoCtx->pImageStart;
1833 size_t cbPreRead = pIoCtx->Type.Child.cbPreRead;
1834 size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
1835 size_t cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
1836
1837 LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
1838 rc = pImage->Backend->pfnAsyncWrite(pImage->pBackendData,
1839 pIoCtx->uOffset - cbPreRead,
1840 cbPreRead + cbThisWrite + cbPostRead,
1841 pIoCtx, NULL, &cbPreRead, &cbPostRead, 0);
1842 Assert(rc != VERR_VD_BLOCK_FREE);
1843 Assert(rc == VERR_VD_NOT_ENOUGH_METADATA || cbPreRead == 0);
1844 Assert(rc == VERR_VD_NOT_ENOUGH_METADATA || cbPostRead == 0);
1845 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1846 rc = VINF_SUCCESS;
1847 else if (rc == VERR_VD_IOCTX_HALT)
1848 {
1849 pIoCtx->fBlocked = true;
1850 rc = VINF_SUCCESS;
1851 }
1852
1853 LogFlowFunc(("returns rc=%Rrc\n", rc));
1854 return rc;
1855}
1856
1857static int vdWriteHelperOptimizedCmpAndWriteAsync(PVDIOCTX pIoCtx)
1858{
1859 int rc = VINF_SUCCESS;
1860 PVDIMAGE pImage = pIoCtx->pImageCur;
1861 size_t cbThisWrite = 0;
1862 size_t cbPreRead = pIoCtx->Type.Child.cbPreRead;
1863 size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
1864 size_t cbWriteCopy = pIoCtx->Type.Child.Write.Optimized.cbWriteCopy;
1865 size_t cbFill = pIoCtx->Type.Child.Write.Optimized.cbFill;
1866 size_t cbReadImage = pIoCtx->Type.Child.Write.Optimized.cbReadImage;
1867 PVDIOCTX pIoCtxParent = pIoCtx->pIoCtxParent;
1868
1869 LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
1870
1871 AssertPtr(pIoCtxParent);
1872 Assert(!pIoCtxParent->pIoCtxParent);
1873 Assert(!pIoCtx->cbTransferLeft && !pIoCtx->cMetaTransfersPending);
1874
1875 vdIoCtxChildReset(pIoCtx);
1876 cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
1877 RTSgBufAdvance(&pIoCtx->SgBuf, cbPreRead);
1878
1879 /* Check if the write would modify anything in this block. */
1880 if (!RTSgBufCmp(&pIoCtx->SgBuf, &pIoCtxParent->SgBuf, cbThisWrite))
1881 {
1882 RTSGBUF SgBufSrcTmp;
1883
1884 RTSgBufClone(&SgBufSrcTmp, &pIoCtxParent->SgBuf);
1885 RTSgBufAdvance(&SgBufSrcTmp, cbThisWrite);
1886 RTSgBufAdvance(&pIoCtx->SgBuf, cbThisWrite);
1887
1888 if (!cbWriteCopy || !RTSgBufCmp(&pIoCtx->SgBuf, &SgBufSrcTmp, cbWriteCopy))
1889 {
1890 /* Block is completely unchanged, so no need to write anything. */
1891 LogFlowFunc(("Block didn't changed\n"));
1892 ASMAtomicWriteU32(&pIoCtx->cbTransferLeft, 0);
1893 RTSgBufAdvance(&pIoCtxParent->SgBuf, cbThisWrite);
1894 return VINF_VD_ASYNC_IO_FINISHED;
1895 }
1896 }
1897
1898 /* Copy the data to the right place in the buffer. */
1899 RTSgBufReset(&pIoCtx->SgBuf);
1900 RTSgBufAdvance(&pIoCtx->SgBuf, cbPreRead);
1901 vdIoCtxCopy(pIoCtx, pIoCtxParent, cbThisWrite);
1902
1903 /* Handle the data that goes after the write to fill the block. */
1904 if (cbPostRead)
1905 {
1906 /* Now assemble the remaining data. */
1907 if (cbWriteCopy)
1908 {
1909 /*
1910 * The S/G buffer of the parent needs to be cloned because
1911 * it is not allowed to modify the state.
1912 */
1913 RTSGBUF SgBufParentTmp;
1914
1915 RTSgBufClone(&SgBufParentTmp, &pIoCtxParent->SgBuf);
1916 RTSgBufCopy(&pIoCtx->SgBuf, &SgBufParentTmp, cbWriteCopy);
1917 }
1918
1919 /* Zero out the remainder of this block. Will never be visible, as this
1920 * is beyond the limit of the image. */
1921 if (cbFill)
1922 {
1923 RTSgBufAdvance(&pIoCtx->SgBuf, cbReadImage);
1924 vdIoCtxSet(pIoCtx, '\0', cbFill);
1925 }
1926 }
1927
1928 /* Write the full block to the virtual disk. */
1929 RTSgBufReset(&pIoCtx->SgBuf);
1930 pIoCtx->pfnIoCtxTransferNext = vdWriteHelperOptimizedCommitAsync;
1931
1932 return rc;
1933}
1934
1935static int vdWriteHelperOptimizedPreReadAsync(PVDIOCTX pIoCtx)
1936{
1937 int rc = VINF_SUCCESS;
1938
1939 LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
1940
1941 if (pIoCtx->cbTransferLeft)
1942 rc = vdReadHelperAsync(pIoCtx);
1943
1944 if ( RT_SUCCESS(rc)
1945 && ( pIoCtx->cbTransferLeft
1946 || pIoCtx->cMetaTransfersPending))
1947 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
1948 else
1949 pIoCtx->pfnIoCtxTransferNext = vdWriteHelperOptimizedCmpAndWriteAsync;
1950
1951 return rc;
1952}
1953
1954/**
1955 * internal: write a complete block (only used for diff images), taking the
1956 * remaining data from parent images. This implementation optimizes out writes
1957 * that do not change the data relative to the state as of the parent images.
1958 * All backends which support differential/growing images support this - async version.
1959 */
1960static int vdWriteHelperOptimizedAsync(PVDIOCTX pIoCtx)
1961{
1962 PVBOXHDD pDisk = pIoCtx->pDisk;
1963 uint64_t uOffset = pIoCtx->Type.Child.uOffsetSaved;
1964 size_t cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
1965 size_t cbPreRead = pIoCtx->Type.Child.cbPreRead;
1966 size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
1967 size_t cbWrite = pIoCtx->Type.Child.cbWriteParent;
1968 size_t cbFill = 0;
1969 size_t cbWriteCopy = 0;
1970 size_t cbReadImage = 0;
1971
1972 LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
1973
1974 AssertPtr(pIoCtx->pIoCtxParent);
1975 Assert(!pIoCtx->pIoCtxParent->pIoCtxParent);
1976
1977 if (cbPostRead)
1978 {
1979 /* Figure out how much we cannot read from the image, because
1980 * the last block to write might exceed the nominal size of the
1981 * image for technical reasons. */
1982 if (uOffset + cbThisWrite + cbPostRead > pDisk->cbSize)
1983 cbFill = uOffset + cbThisWrite + cbPostRead - pDisk->cbSize;
1984
1985 /* If we have data to be written, use that instead of reading
1986 * data from the image. */
1987 if (cbWrite > cbThisWrite)
1988 cbWriteCopy = RT_MIN(cbWrite - cbThisWrite, cbPostRead);
1989
1990 /* The rest must be read from the image. */
1991 cbReadImage = cbPostRead - cbWriteCopy - cbFill;
1992 }
1993
1994 pIoCtx->Type.Child.Write.Optimized.cbFill = cbFill;
1995 pIoCtx->Type.Child.Write.Optimized.cbWriteCopy = cbWriteCopy;
1996 pIoCtx->Type.Child.Write.Optimized.cbReadImage = cbReadImage;
1997
1998 /* Read the entire data of the block so that we can compare whether it will
1999 * be modified by the write or not. */
2000 pIoCtx->cbTransferLeft = cbPreRead + cbThisWrite + cbPostRead - cbFill;
2001 pIoCtx->cbTransfer = pIoCtx->cbTransferLeft;
2002 pIoCtx->uOffset -= cbPreRead;
2003
2004 /* Next step */
2005 pIoCtx->pfnIoCtxTransferNext = vdWriteHelperOptimizedPreReadAsync;
2006 return VINF_SUCCESS;
2007}
2008
2009/**
2010 * internal: write buffer to the image, taking care of block boundaries and
2011 * write optimizations - async version.
2012 */
2013static int vdWriteHelperAsync(PVDIOCTX pIoCtx)
2014{
2015 int rc;
2016 size_t cbWrite = pIoCtx->cbTransfer;
2017 uint64_t uOffset = pIoCtx->uOffset;
2018 PVDIMAGE pImage = pIoCtx->pImageCur;
2019 PVBOXHDD pDisk = pIoCtx->pDisk;
2020 unsigned fWrite;
2021 size_t cbThisWrite;
2022 size_t cbPreRead, cbPostRead;
2023
2024 rc = vdSetModifiedFlagAsync(pDisk, pIoCtx);
2025 if (RT_FAILURE(rc)) /* Includes I/O in progress. */
2026 return rc;
2027
2028 /* Loop until all written. */
2029 do
2030 {
2031 /* Try to write the possibly partial block to the last opened image.
2032 * This works when the block is already allocated in this image or
2033 * if it is a full-block write (and allocation isn't suppressed below).
2034 * For image formats which don't support zero blocks, it's beneficial
2035 * to avoid unnecessarily allocating unchanged blocks. This prevents
2036 * unwanted expanding of images. VMDK is an example. */
2037 cbThisWrite = cbWrite;
2038 fWrite = (pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME)
2039 ? 0 : VD_WRITE_NO_ALLOC;
2040 rc = pImage->Backend->pfnAsyncWrite(pImage->pBackendData, uOffset,
2041 cbThisWrite, pIoCtx,
2042 &cbThisWrite, &cbPreRead,
2043 &cbPostRead, fWrite);
2044 if (rc == VERR_VD_BLOCK_FREE)
2045 {
2046 /* Lock the disk .*/
2047 rc = vdIoCtxLockDisk(pDisk, pIoCtx);
2048 if (RT_SUCCESS(rc))
2049 {
2050 /*
2051 * Allocate segment and buffer in one go.
2052 * A bit hackish but avoids the need to allocate memory twice.
2053 */
2054 PRTSGBUF pTmp = (PRTSGBUF)RTMemAlloc(cbPreRead + cbThisWrite + cbPostRead + sizeof(RTSGSEG) + sizeof(RTSGBUF));
2055 AssertBreakStmt(VALID_PTR(pTmp), rc = VERR_NO_MEMORY);
2056 PRTSGSEG pSeg = (PRTSGSEG)(pTmp + 1);
2057
2058 pSeg->pvSeg = pSeg + 1;
2059 pSeg->cbSeg = cbPreRead + cbThisWrite + cbPostRead;
2060 RTSgBufInit(pTmp, pSeg, 1);
2061
2062 PVDIOCTX pIoCtxWrite = vdIoCtxChildAlloc(pDisk, VDIOCTXTXDIR_WRITE,
2063 uOffset, pSeg->cbSeg, pImage,
2064 pTmp,
2065 pIoCtx, cbThisWrite,
2066 cbWrite,
2067 pTmp,
2068 (pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME)
2069 ? vdWriteHelperStandardAsync
2070 : vdWriteHelperOptimizedAsync);
2071 if (!VALID_PTR(pIoCtxWrite))
2072 {
2073 RTMemTmpFree(pTmp);
2074 rc = VERR_NO_MEMORY;
2075 break;
2076 }
2077
2078 LogFlowFunc(("Disk is growing because of pIoCtx=%#p pIoCtxWrite=%#p\n",
2079 pIoCtx, pIoCtxWrite));
2080
2081 pIoCtxWrite->Type.Child.cbPreRead = cbPreRead;
2082 pIoCtxWrite->Type.Child.cbPostRead = cbPostRead;
2083
2084 /* Process the write request */
2085 rc = vdIoCtxProcess(pIoCtxWrite);
2086
2087 if (RT_FAILURE(rc) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
2088 {
2089 vdIoCtxFree(pDisk, pIoCtxWrite);
2090 break;
2091 }
2092 else if ( rc == VINF_VD_ASYNC_IO_FINISHED
2093 && ASMAtomicCmpXchgBool(&pIoCtxWrite->fComplete, true, false))
2094 {
2095 LogFlow(("Child write request completed\n"));
2096 Assert(pIoCtx->cbTransferLeft >= cbThisWrite);
2097 ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbThisWrite);
2098 vdIoCtxUnlockDisk(pDisk, pIoCtx, false /* fProcessDeferredReqs*/ );
2099 vdIoCtxFree(pDisk, pIoCtxWrite);
2100
2101 rc = VINF_SUCCESS;
2102 }
2103 else
2104 {
2105 LogFlow(("Child write pending\n"));
2106 pIoCtx->fBlocked = true;
2107 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
2108 cbWrite -= cbThisWrite;
2109 uOffset += cbThisWrite;
2110 break;
2111 }
2112 }
2113 else
2114 {
2115 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
2116 break;
2117 }
2118 }
2119
2120 if (rc == VERR_VD_IOCTX_HALT)
2121 {
2122 cbWrite -= cbThisWrite;
2123 uOffset += cbThisWrite;
2124 pIoCtx->fBlocked = true;
2125 break;
2126 }
2127 else if (rc == VERR_VD_NOT_ENOUGH_METADATA)
2128 break;
2129
2130 cbWrite -= cbThisWrite;
2131 uOffset += cbThisWrite;
2132 } while (cbWrite != 0 && (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS));
2133
2134 if ( rc == VERR_VD_ASYNC_IO_IN_PROGRESS
2135 || rc == VERR_VD_NOT_ENOUGH_METADATA
2136 || rc == VERR_VD_IOCTX_HALT)
2137 {
2138 /*
2139 * Tell the caller that we don't need to go back here because all
2140 * writes are initiated.
2141 */
2142 if (!cbWrite)
2143 rc = VINF_SUCCESS;
2144
2145 pIoCtx->uOffset = uOffset;
2146 pIoCtx->cbTransfer = cbWrite;
2147 }
2148
2149 return rc;
2150}
2151
2152/**
2153 * Flush helper async version.
2154 */
2155static int vdFlushHelperAsync(PVDIOCTX pIoCtx)
2156{
2157 int rc = VINF_SUCCESS;
2158 PVBOXHDD pDisk = pIoCtx->pDisk;
2159 PVDIMAGE pImage = pIoCtx->pImageCur;
2160
2161 rc = vdIoCtxLockDisk(pDisk, pIoCtx);
2162 if (RT_SUCCESS(rc))
2163 {
2164 vdResetModifiedFlag(pDisk);
2165 rc = pImage->Backend->pfnAsyncFlush(pImage->pBackendData, pIoCtx);
2166 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
2167 rc = VINF_SUCCESS;
2168 else if (rc == VINF_VD_ASYNC_IO_FINISHED)
2169 vdIoCtxUnlockDisk(pDisk, pIoCtx, true /* fProcessDeferredReqs */);
2170 }
2171
2172 return rc;
2173}
2174
2175/**
2176 * internal: scans plugin directory and loads the backends have been found.
2177 */
2178static int vdLoadDynamicBackends()
2179{
2180#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
2181 int rc = VINF_SUCCESS;
2182 PRTDIR pPluginDir = NULL;
2183
2184 /* Enumerate plugin backends. */
2185 char szPath[RTPATH_MAX];
2186 rc = RTPathAppPrivateArch(szPath, sizeof(szPath));
2187 if (RT_FAILURE(rc))
2188 return rc;
2189
2190 /* To get all entries with VBoxHDD as prefix. */
2191 char *pszPluginFilter = RTPathJoinA(szPath, VBOX_HDDFORMAT_PLUGIN_PREFIX "*");
2192 if (!pszPluginFilter)
2193 return VERR_NO_STR_MEMORY;
2194
2195 PRTDIRENTRYEX pPluginDirEntry = NULL;
2196 size_t cbPluginDirEntry = sizeof(RTDIRENTRYEX);
2197 /* The plugins are in the same directory as the other shared libs. */
2198 rc = RTDirOpenFiltered(&pPluginDir, pszPluginFilter, RTDIRFILTER_WINNT);
2199 if (RT_FAILURE(rc))
2200 {
2201 /* On Windows the above immediately signals that there are no
2202 * files matching, while on other platforms enumerating the
2203 * files below fails. Either way: no plugins. */
2204 goto out;
2205 }
2206
2207 pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(sizeof(RTDIRENTRYEX));
2208 if (!pPluginDirEntry)
2209 {
2210 rc = VERR_NO_MEMORY;
2211 goto out;
2212 }
2213
2214 while ((rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK)) != VERR_NO_MORE_FILES)
2215 {
2216 RTLDRMOD hPlugin = NIL_RTLDRMOD;
2217 PFNVBOXHDDFORMATLOAD pfnHDDFormatLoad = NULL;
2218 PVBOXHDDBACKEND pBackend = NULL;
2219 char *pszPluginPath = NULL;
2220
2221 if (rc == VERR_BUFFER_OVERFLOW)
2222 {
2223 /* allocate new buffer. */
2224 RTMemFree(pPluginDirEntry);
2225 pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(cbPluginDirEntry);
2226 if (!pPluginDirEntry)
2227 {
2228 rc = VERR_NO_MEMORY;
2229 break;
2230 }
2231 /* Retry. */
2232 rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
2233 if (RT_FAILURE(rc))
2234 break;
2235 }
2236 else if (RT_FAILURE(rc))
2237 break;
2238
2239 /* We got the new entry. */
2240 if (!RTFS_IS_FILE(pPluginDirEntry->Info.Attr.fMode))
2241 continue;
2242
2243 /* Prepend the path to the libraries. */
2244 pszPluginPath = RTPathJoinA(szPath, pPluginDirEntry->szName);
2245 if (!pszPluginPath)
2246 {
2247 rc = VERR_NO_STR_MEMORY;
2248 break;
2249 }
2250
2251 rc = SUPR3HardenedLdrLoadPlugIn(pszPluginPath, &hPlugin, NULL);
2252 if (RT_SUCCESS(rc))
2253 {
2254 rc = RTLdrGetSymbol(hPlugin, VBOX_HDDFORMAT_LOAD_NAME, (void**)&pfnHDDFormatLoad);
2255 if (RT_FAILURE(rc) || !pfnHDDFormatLoad)
2256 {
2257 LogFunc(("error resolving the entry point %s in plugin %s, rc=%Rrc, pfnHDDFormat=%#p\n", VBOX_HDDFORMAT_LOAD_NAME, pPluginDirEntry->szName, rc, pfnHDDFormatLoad));
2258 if (RT_SUCCESS(rc))
2259 rc = VERR_SYMBOL_NOT_FOUND;
2260 }
2261
2262 if (RT_SUCCESS(rc))
2263 {
2264 /* Get the function table. */
2265 rc = pfnHDDFormatLoad(&pBackend);
2266 if (RT_SUCCESS(rc) && pBackend->cbSize == sizeof(VBOXHDDBACKEND))
2267 {
2268 pBackend->hPlugin = hPlugin;
2269 vdAddBackend(pBackend);
2270 }
2271 else
2272 LogFunc(("ignored plugin '%s': pBackend->cbSize=%d rc=%Rrc\n", pszPluginPath, pBackend->cbSize, rc));
2273 }
2274 else
2275 LogFunc(("ignored plugin '%s': rc=%Rrc\n", pszPluginPath, rc));
2276
2277 if (RT_FAILURE(rc))
2278 RTLdrClose(hPlugin);
2279 }
2280 RTStrFree(pszPluginPath);
2281 }
2282out:
2283 if (rc == VERR_NO_MORE_FILES)
2284 rc = VINF_SUCCESS;
2285 RTStrFree(pszPluginFilter);
2286 if (pPluginDirEntry)
2287 RTMemFree(pPluginDirEntry);
2288 if (pPluginDir)
2289 RTDirClose(pPluginDir);
2290 return rc;
2291#else
2292 return VINF_SUCCESS;
2293#endif
2294}
2295
2296/**
2297 * internal: scans plugin directory and loads the cache backends have been found.
2298 */
2299static int vdLoadDynamicCacheBackends()
2300{
2301#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
2302 int rc = VINF_SUCCESS;
2303 PRTDIR pPluginDir = NULL;
2304
2305 /* Enumerate plugin backends. */
2306 char szPath[RTPATH_MAX];
2307 rc = RTPathAppPrivateArch(szPath, sizeof(szPath));
2308 if (RT_FAILURE(rc))
2309 return rc;
2310
2311 /* To get all entries with VBoxHDD as prefix. */
2312 char *pszPluginFilter = RTPathJoinA(szPath, VD_CACHEFORMAT_PLUGIN_PREFIX "*");
2313 if (!pszPluginFilter)
2314 {
2315 rc = VERR_NO_STR_MEMORY;
2316 return rc;
2317 }
2318
2319 PRTDIRENTRYEX pPluginDirEntry = NULL;
2320 size_t cbPluginDirEntry = sizeof(RTDIRENTRYEX);
2321 /* The plugins are in the same directory as the other shared libs. */
2322 rc = RTDirOpenFiltered(&pPluginDir, pszPluginFilter, RTDIRFILTER_WINNT);
2323 if (RT_FAILURE(rc))
2324 {
2325 /* On Windows the above immediately signals that there are no
2326 * files matching, while on other platforms enumerating the
2327 * files below fails. Either way: no plugins. */
2328 goto out;
2329 }
2330
2331 pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(sizeof(RTDIRENTRYEX));
2332 if (!pPluginDirEntry)
2333 {
2334 rc = VERR_NO_MEMORY;
2335 goto out;
2336 }
2337
2338 while ((rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK)) != VERR_NO_MORE_FILES)
2339 {
2340 RTLDRMOD hPlugin = NIL_RTLDRMOD;
2341 PFNVDCACHEFORMATLOAD pfnVDCacheLoad = NULL;
2342 PVDCACHEBACKEND pBackend = NULL;
2343 char *pszPluginPath = NULL;
2344
2345 if (rc == VERR_BUFFER_OVERFLOW)
2346 {
2347 /* allocate new buffer. */
2348 RTMemFree(pPluginDirEntry);
2349 pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(cbPluginDirEntry);
2350 if (!pPluginDirEntry)
2351 {
2352 rc = VERR_NO_MEMORY;
2353 break;
2354 }
2355 /* Retry. */
2356 rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
2357 if (RT_FAILURE(rc))
2358 break;
2359 }
2360 else if (RT_FAILURE(rc))
2361 break;
2362
2363 /* We got the new entry. */
2364 if (!RTFS_IS_FILE(pPluginDirEntry->Info.Attr.fMode))
2365 continue;
2366
2367 /* Prepend the path to the libraries. */
2368 pszPluginPath = RTPathJoinA(szPath, pPluginDirEntry->szName);
2369 if (!pszPluginPath)
2370 {
2371 rc = VERR_NO_STR_MEMORY;
2372 break;
2373 }
2374
2375 rc = SUPR3HardenedLdrLoadPlugIn(pszPluginPath, &hPlugin, NULL);
2376 if (RT_SUCCESS(rc))
2377 {
2378 rc = RTLdrGetSymbol(hPlugin, VD_CACHEFORMAT_LOAD_NAME, (void**)&pfnVDCacheLoad);
2379 if (RT_FAILURE(rc) || !pfnVDCacheLoad)
2380 {
2381 LogFunc(("error resolving the entry point %s in plugin %s, rc=%Rrc, pfnVDCacheLoad=%#p\n",
2382 VD_CACHEFORMAT_LOAD_NAME, pPluginDirEntry->szName, rc, pfnVDCacheLoad));
2383 if (RT_SUCCESS(rc))
2384 rc = VERR_SYMBOL_NOT_FOUND;
2385 }
2386
2387 if (RT_SUCCESS(rc))
2388 {
2389 /* Get the function table. */
2390 rc = pfnVDCacheLoad(&pBackend);
2391 if (RT_SUCCESS(rc) && pBackend->cbSize == sizeof(VDCACHEBACKEND))
2392 {
2393 pBackend->hPlugin = hPlugin;
2394 vdAddCacheBackend(pBackend);
2395 }
2396 else
2397 LogFunc(("ignored plugin '%s': pBackend->cbSize=%d rc=%Rrc\n", pszPluginPath, pBackend->cbSize, rc));
2398 }
2399 else
2400 LogFunc(("ignored plugin '%s': rc=%Rrc\n", pszPluginPath, rc));
2401
2402 if (RT_FAILURE(rc))
2403 RTLdrClose(hPlugin);
2404 }
2405 RTStrFree(pszPluginPath);
2406 }
2407out:
2408 if (rc == VERR_NO_MORE_FILES)
2409 rc = VINF_SUCCESS;
2410 RTStrFree(pszPluginFilter);
2411 if (pPluginDirEntry)
2412 RTMemFree(pPluginDirEntry);
2413 if (pPluginDir)
2414 RTDirClose(pPluginDir);
2415 return rc;
2416#else
2417 return VINF_SUCCESS;
2418#endif
2419}
2420
2421/**
2422 * VD async I/O interface open callback.
2423 */
2424static int vdIOOpenFallback(void *pvUser, const char *pszLocation,
2425 uint32_t fOpen, PFNVDCOMPLETED pfnCompleted,
2426 void **ppStorage)
2427{
2428 PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)RTMemAllocZ(sizeof(VDIIOFALLBACKSTORAGE));
2429
2430 if (!pStorage)
2431 return VERR_NO_MEMORY;
2432
2433 pStorage->pfnCompleted = pfnCompleted;
2434
2435 /* Open the file. */
2436 int rc = RTFileOpen(&pStorage->File, pszLocation, fOpen);
2437 if (RT_SUCCESS(rc))
2438 {
2439 *ppStorage = pStorage;
2440 return VINF_SUCCESS;
2441 }
2442
2443 RTMemFree(pStorage);
2444 return rc;
2445}
2446
2447/**
2448 * VD async I/O interface close callback.
2449 */
2450static int vdIOCloseFallback(void *pvUser, void *pvStorage)
2451{
2452 PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
2453
2454 RTFileClose(pStorage->File);
2455 RTMemFree(pStorage);
2456 return VINF_SUCCESS;
2457}
2458
2459static int vdIODeleteFallback(void *pvUser, const char *pcszFilename)
2460{
2461 return RTFileDelete(pcszFilename);
2462}
2463
2464static int vdIOMoveFallback(void *pvUser, const char *pcszSrc, const char *pcszDst, unsigned fMove)
2465{
2466 return RTFileMove(pcszSrc, pcszDst, fMove);
2467}
2468
2469static int vdIOGetFreeSpaceFallback(void *pvUser, const char *pcszFilename, int64_t *pcbFreeSpace)
2470{
2471 return RTFsQuerySizes(pcszFilename, NULL, pcbFreeSpace, NULL, NULL);
2472}
2473
2474static int vdIOGetModificationTimeFallback(void *pvUser, const char *pcszFilename, PRTTIMESPEC pModificationTime)
2475{
2476 RTFSOBJINFO info;
2477 int rc = RTPathQueryInfo(pcszFilename, &info, RTFSOBJATTRADD_NOTHING);
2478 if (RT_SUCCESS(rc))
2479 *pModificationTime = info.ModificationTime;
2480 return rc;
2481}
2482
2483/**
2484 * VD async I/O interface callback for retrieving the file size.
2485 */
2486static int vdIOGetSizeFallback(void *pvUser, void *pvStorage, uint64_t *pcbSize)
2487{
2488 PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
2489
2490 return RTFileGetSize(pStorage->File, pcbSize);
2491}
2492
2493/**
2494 * VD async I/O interface callback for setting the file size.
2495 */
2496static int vdIOSetSizeFallback(void *pvUser, void *pvStorage, uint64_t cbSize)
2497{
2498 PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
2499
2500 return RTFileSetSize(pStorage->File, cbSize);
2501}
2502
2503/**
2504 * VD async I/O interface callback for a synchronous write to the file.
2505 */
2506static int vdIOWriteSyncFallback(void *pvUser, void *pvStorage, uint64_t uOffset,
2507 const void *pvBuf, size_t cbWrite, size_t *pcbWritten)
2508{
2509 PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
2510
2511 return RTFileWriteAt(pStorage->File, uOffset, pvBuf, cbWrite, pcbWritten);
2512}
2513
2514/**
2515 * VD async I/O interface callback for a synchronous read from the file.
2516 */
2517static int vdIOReadSyncFallback(void *pvUser, void *pvStorage, uint64_t uOffset,
2518 void *pvBuf, size_t cbRead, size_t *pcbRead)
2519{
2520 PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
2521
2522 return RTFileReadAt(pStorage->File, uOffset, pvBuf, cbRead, pcbRead);
2523}
2524
2525/**
2526 * VD async I/O interface callback for a synchronous flush of the file data.
2527 */
2528static int vdIOFlushSyncFallback(void *pvUser, void *pvStorage)
2529{
2530 PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
2531
2532 return RTFileFlush(pStorage->File);
2533}
2534
2535/**
2536 * VD async I/O interface callback for a asynchronous read from the file.
2537 */
2538static int vdIOReadAsyncFallback(void *pvUser, void *pStorage, uint64_t uOffset,
2539 PCRTSGSEG paSegments, size_t cSegments,
2540 size_t cbRead, void *pvCompletion,
2541 void **ppTask)
2542{
2543 return VERR_NOT_IMPLEMENTED;
2544}
2545
2546/**
2547 * VD async I/O interface callback for a asynchronous write to the file.
2548 */
2549static int vdIOWriteAsyncFallback(void *pvUser, void *pStorage, uint64_t uOffset,
2550 PCRTSGSEG paSegments, size_t cSegments,
2551 size_t cbWrite, void *pvCompletion,
2552 void **ppTask)
2553{
2554 return VERR_NOT_IMPLEMENTED;
2555}
2556
2557/**
2558 * VD async I/O interface callback for a asynchronous flush of the file data.
2559 */
2560static int vdIOFlushAsyncFallback(void *pvUser, void *pStorage,
2561 void *pvCompletion, void **ppTask)
2562{
2563 return VERR_NOT_IMPLEMENTED;
2564}
2565
2566/**
2567 * Internal - Continues an I/O context after
2568 * it was halted because of an active transfer.
2569 */
2570static int vdIoCtxContinue(PVDIOCTX pIoCtx, int rcReq)
2571{
2572 PVBOXHDD pDisk = pIoCtx->pDisk;
2573 int rc = VINF_SUCCESS;
2574
2575 if (RT_FAILURE(rcReq))
2576 ASMAtomicCmpXchgS32(&pIoCtx->rcReq, rcReq, VINF_SUCCESS);
2577
2578 if (!pIoCtx->fBlocked)
2579 {
2580 /* Continue the transfer */
2581 rc = vdIoCtxProcess(pIoCtx);
2582
2583 if ( rc == VINF_VD_ASYNC_IO_FINISHED
2584 && ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
2585 {
2586 LogFlowFunc(("I/O context completed pIoCtx=%#p\n", pIoCtx));
2587 if (pIoCtx->pIoCtxParent)
2588 {
2589 PVDIOCTX pIoCtxParent = pIoCtx->pIoCtxParent;
2590
2591 Assert(!pIoCtxParent->pIoCtxParent);
2592 if (RT_FAILURE(pIoCtx->rcReq))
2593 ASMAtomicCmpXchgS32(&pIoCtxParent->rcReq, pIoCtx->rcReq, VINF_SUCCESS);
2594
2595 if (pIoCtx->enmTxDir == VDIOCTXTXDIR_WRITE)
2596 {
2597 LogFlowFunc(("I/O context transferred %u bytes for the parent pIoCtxParent=%p\n",
2598 pIoCtx->Type.Child.cbTransferParent, pIoCtxParent));
2599
2600 /* Update the parent state. */
2601 Assert(pIoCtxParent->cbTransferLeft >= pIoCtx->Type.Child.cbTransferParent);
2602 ASMAtomicSubU32(&pIoCtxParent->cbTransferLeft, pIoCtx->Type.Child.cbTransferParent);
2603 }
2604 else
2605 Assert(pIoCtx->enmTxDir == VDIOCTXTXDIR_FLUSH);
2606
2607 /*
2608 * A completed child write means that we finished growing the image.
2609 * We have to process any pending writes now.
2610 */
2611 vdIoCtxUnlockDisk(pDisk, pIoCtxParent, false /* fProcessDeferredReqs */);
2612
2613 /* Unblock the parent */
2614 pIoCtxParent->fBlocked = false;
2615
2616 rc = vdIoCtxProcess(pIoCtxParent);
2617
2618 if ( rc == VINF_VD_ASYNC_IO_FINISHED
2619 && ASMAtomicCmpXchgBool(&pIoCtxParent->fComplete, true, false))
2620 {
2621 RTCritSectLeave(&pDisk->CritSect);
2622 LogFlowFunc(("Parent I/O context completed pIoCtxParent=%#p rcReq=%Rrc\n", pIoCtxParent, pIoCtxParent->rcReq));
2623 pIoCtxParent->Type.Root.pfnComplete(pIoCtxParent->Type.Root.pvUser1,
2624 pIoCtxParent->Type.Root.pvUser2,
2625 pIoCtxParent->rcReq);
2626 vdThreadFinishWrite(pDisk);
2627 vdIoCtxFree(pDisk, pIoCtxParent);
2628 RTCritSectEnter(&pDisk->CritSect);
2629 }
2630
2631 /* Process any pending writes if the current request didn't caused another growing. */
2632 RTCritSectEnter(&pDisk->CritSect);
2633
2634 if ( !RTListIsEmpty(&pDisk->ListWriteLocked)
2635 && !vdIoCtxIsDiskLockOwner(pDisk, pIoCtx))
2636 {
2637 RTLISTNODE ListTmp;
2638
2639 LogFlowFunc(("Before: pNext=%#p pPrev=%#p\n", pDisk->ListWriteLocked.pNext,
2640 pDisk->ListWriteLocked.pPrev));
2641
2642 RTListMove(&ListTmp, &pDisk->ListWriteLocked);
2643
2644 LogFlowFunc(("After: pNext=%#p pPrev=%#p\n", pDisk->ListWriteLocked.pNext,
2645 pDisk->ListWriteLocked.pPrev));
2646
2647 RTCritSectLeave(&pDisk->CritSect);
2648
2649 /* Process the list. */
2650 do
2651 {
2652 PVDIOCTXDEFERRED pDeferred = RTListGetFirst(&ListTmp, VDIOCTXDEFERRED, NodeDeferred);
2653 PVDIOCTX pIoCtxWait = pDeferred->pIoCtx;
2654
2655 AssertPtr(pIoCtxWait);
2656
2657 RTListNodeRemove(&pDeferred->NodeDeferred);
2658 RTMemFree(pDeferred);
2659
2660 Assert(!pIoCtxWait->pIoCtxParent);
2661
2662 pIoCtxWait->fBlocked = false;
2663 LogFlowFunc(("Processing waiting I/O context pIoCtxWait=%#p\n", pIoCtxWait));
2664
2665 rc = vdIoCtxProcess(pIoCtxWait);
2666 if ( rc == VINF_VD_ASYNC_IO_FINISHED
2667 && ASMAtomicCmpXchgBool(&pIoCtxWait->fComplete, true, false))
2668 {
2669 RTCritSectLeave(&pDisk->CritSect);
2670 LogFlowFunc(("Waiting I/O context completed pIoCtxWait=%#p\n", pIoCtxWait));
2671 vdThreadFinishWrite(pDisk);
2672 pIoCtxWait->Type.Root.pfnComplete(pIoCtxWait->Type.Root.pvUser1,
2673 pIoCtxWait->Type.Root.pvUser2,
2674 pIoCtxWait->rcReq);
2675 vdIoCtxFree(pDisk, pIoCtxWait);
2676 RTCritSectEnter(&pDisk->CritSect);
2677 }
2678 } while (!RTListIsEmpty(&ListTmp));
2679 }
2680 else
2681 RTCritSectLeave(&pDisk->CritSect);
2682 }
2683 else
2684 {
2685 if (pIoCtx->enmTxDir == VDIOCTXTXDIR_FLUSH)
2686 {
2687 vdIoCtxUnlockDisk(pDisk, pIoCtx, true /* fProcessDerredReqs */);
2688 vdThreadFinishWrite(pDisk);
2689 }
2690 else if (pIoCtx->enmTxDir == VDIOCTXTXDIR_WRITE)
2691 vdThreadFinishWrite(pDisk);
2692 else
2693 {
2694 Assert(pIoCtx->enmTxDir == VDIOCTXTXDIR_READ);
2695 vdThreadFinishRead(pDisk);
2696 }
2697
2698 LogFlowFunc(("I/O context completed pIoCtx=%#p rcReq=%Rrc\n", pIoCtx, pIoCtx->rcReq));
2699 RTCritSectLeave(&pDisk->CritSect);
2700 pIoCtx->Type.Root.pfnComplete(pIoCtx->Type.Root.pvUser1,
2701 pIoCtx->Type.Root.pvUser2,
2702 pIoCtx->rcReq);
2703 RTCritSectEnter(&pDisk->CritSect);
2704 }
2705
2706 vdIoCtxFree(pDisk, pIoCtx);
2707 }
2708 }
2709
2710 return VINF_SUCCESS;
2711}
2712
2713/**
2714 * Internal - Called when user transfer completed.
2715 */
2716static int vdUserXferCompleted(PVDIOSTORAGE pIoStorage, PVDIOCTX pIoCtx,
2717 PFNVDXFERCOMPLETED pfnComplete, void *pvUser,
2718 size_t cbTransfer, int rcReq)
2719{
2720 int rc = VINF_SUCCESS;
2721 bool fIoCtxContinue = true;
2722 PVBOXHDD pDisk = pIoCtx->pDisk;
2723
2724 LogFlowFunc(("pIoStorage=%#p pIoCtx=%#p pfnComplete=%#p pvUser=%#p cbTransfer=%zu rcReq=%Rrc\n",
2725 pIoStorage, pIoCtx, pfnComplete, pvUser, cbTransfer, rcReq));
2726
2727 RTCritSectEnter(&pDisk->CritSect);
2728 Assert(pIoCtx->cbTransferLeft >= cbTransfer);
2729 ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbTransfer);
2730 ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
2731
2732 if (pfnComplete)
2733 rc = pfnComplete(pIoStorage->pVDIo->pBackendData, pIoCtx, pvUser, rcReq);
2734
2735 if (RT_SUCCESS(rc))
2736 rc = vdIoCtxContinue(pIoCtx, rcReq);
2737 else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
2738 rc = VINF_SUCCESS;
2739
2740 RTCritSectLeave(&pDisk->CritSect);
2741
2742 return rc;
2743}
2744
2745/**
2746 * Internal - Called when a meta transfer completed.
2747 */
2748static int vdMetaXferCompleted(PVDIOSTORAGE pIoStorage, PFNVDXFERCOMPLETED pfnComplete, void *pvUser,
2749 PVDMETAXFER pMetaXfer, int rcReq)
2750{
2751 PVBOXHDD pDisk = pIoStorage->pVDIo->pDisk;
2752 RTLISTNODE ListIoCtxWaiting;
2753 bool fFlush;
2754
2755 LogFlowFunc(("pIoStorage=%#p pfnComplete=%#p pvUser=%#p pMetaXfer=%#p rcReq=%Rrc\n",
2756 pIoStorage, pfnComplete, pvUser, pMetaXfer, rcReq));
2757
2758 RTCritSectEnter(&pDisk->CritSect);
2759 fFlush = VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_FLUSH;
2760 VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
2761
2762 if (!fFlush)
2763 {
2764 RTListMove(&ListIoCtxWaiting, &pMetaXfer->ListIoCtxWaiting);
2765
2766 if (RT_FAILURE(rcReq))
2767 {
2768 /* Remove from the AVL tree. */
2769 LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
2770 bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
2771 Assert(fRemoved);
2772 RTMemFree(pMetaXfer);
2773 }
2774 else
2775 {
2776 /* Increase the reference counter to make sure it doesn't go away before the last context is processed. */
2777 pMetaXfer->cRefs++;
2778 }
2779 }
2780 else
2781 RTListMove(&ListIoCtxWaiting, &pMetaXfer->ListIoCtxWaiting);
2782
2783 /* Go through the waiting list and continue the I/O contexts. */
2784 while (!RTListIsEmpty(&ListIoCtxWaiting))
2785 {
2786 int rc = VINF_SUCCESS;
2787 bool fContinue = true;
2788 PVDIOCTXDEFERRED pDeferred = RTListGetFirst(&ListIoCtxWaiting, VDIOCTXDEFERRED, NodeDeferred);
2789 PVDIOCTX pIoCtx = pDeferred->pIoCtx;
2790 RTListNodeRemove(&pDeferred->NodeDeferred);
2791
2792 RTMemFree(pDeferred);
2793 ASMAtomicDecU32(&pIoCtx->cMetaTransfersPending);
2794
2795 if (pfnComplete)
2796 rc = pfnComplete(pIoStorage->pVDIo->pBackendData, pIoCtx, pvUser, rcReq);
2797
2798 LogFlow(("Completion callback for I/O context %#p returned %Rrc\n", pIoCtx, rc));
2799
2800 if (RT_SUCCESS(rc))
2801 {
2802 rc = vdIoCtxContinue(pIoCtx, rcReq);
2803 AssertRC(rc);
2804 }
2805 else
2806 Assert(rc == VERR_VD_ASYNC_IO_IN_PROGRESS);
2807 }
2808
2809 /* Remove if not used anymore. */
2810 if (RT_SUCCESS(rcReq) && !fFlush)
2811 {
2812 pMetaXfer->cRefs--;
2813 if (!pMetaXfer->cRefs && RTListIsEmpty(&pMetaXfer->ListIoCtxWaiting))
2814 {
2815 /* Remove from the AVL tree. */
2816 LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
2817 bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
2818 Assert(fRemoved);
2819 RTMemFree(pMetaXfer);
2820 }
2821 }
2822 else if (fFlush)
2823 RTMemFree(pMetaXfer);
2824
2825 RTCritSectLeave(&pDisk->CritSect);
2826
2827 return VINF_SUCCESS;
2828}
2829
2830static int vdIOIntReqCompleted(void *pvUser, int rcReq)
2831{
2832 int rc = VINF_SUCCESS;
2833 PVDIOTASK pIoTask = (PVDIOTASK)pvUser;
2834 PVDIOSTORAGE pIoStorage = pIoTask->pIoStorage;
2835
2836 LogFlowFunc(("Task completed pIoTask=%#p\n", pIoTask));
2837
2838 if (!pIoTask->fMeta)
2839 rc = vdUserXferCompleted(pIoStorage, pIoTask->Type.User.pIoCtx,
2840 pIoTask->pfnComplete, pIoTask->pvUser,
2841 pIoTask->Type.User.cbTransfer, rcReq);
2842 else
2843 rc = vdMetaXferCompleted(pIoStorage, pIoTask->pfnComplete, pIoTask->pvUser,
2844 pIoTask->Type.Meta.pMetaXfer, rcReq);
2845
2846 vdIoTaskFree(pIoStorage->pVDIo->pDisk, pIoTask);
2847
2848 return rc;
2849}
2850
2851/**
2852 * VD I/O interface callback for opening a file.
2853 */
2854static int vdIOIntOpen(void *pvUser, const char *pszLocation,
2855 unsigned uOpenFlags, PPVDIOSTORAGE ppIoStorage)
2856{
2857 int rc = VINF_SUCCESS;
2858 PVDIO pVDIo = (PVDIO)pvUser;
2859 PVDIOSTORAGE pIoStorage = (PVDIOSTORAGE)RTMemAllocZ(sizeof(VDIOSTORAGE));
2860
2861 if (!pIoStorage)
2862 return VERR_NO_MEMORY;
2863
2864 /* Create the AVl tree. */
2865 pIoStorage->pTreeMetaXfers = (PAVLRFOFFTREE)RTMemAllocZ(sizeof(AVLRFOFFTREE));
2866 if (pIoStorage->pTreeMetaXfers)
2867 {
2868 rc = pVDIo->pInterfaceIOCallbacks->pfnOpen(pVDIo->pInterfaceIO->pvUser,
2869 pszLocation, uOpenFlags,
2870 vdIOIntReqCompleted,
2871 &pIoStorage->pStorage);
2872 if (RT_SUCCESS(rc))
2873 {
2874 pIoStorage->pVDIo = pVDIo;
2875 *ppIoStorage = pIoStorage;
2876 return VINF_SUCCESS;
2877 }
2878
2879 RTMemFree(pIoStorage->pTreeMetaXfers);
2880 }
2881 else
2882 rc = VERR_NO_MEMORY;
2883
2884 RTMemFree(pIoStorage);
2885 return rc;
2886}
2887
2888static int vdIOIntTreeMetaXferDestroy(PAVLRFOFFNODECORE pNode, void *pvUser)
2889{
2890 AssertMsgFailed(("Tree should be empty at this point!\n"));
2891 return VINF_SUCCESS;
2892}
2893
2894static int vdIOIntClose(void *pvUser, PVDIOSTORAGE pIoStorage)
2895{
2896 PVDIO pVDIo = (PVDIO)pvUser;
2897
2898 int rc = pVDIo->pInterfaceIOCallbacks->pfnClose(pVDIo->pInterfaceIO->pvUser,
2899 pIoStorage->pStorage);
2900 AssertRC(rc);
2901
2902 RTAvlrFileOffsetDestroy(pIoStorage->pTreeMetaXfers, vdIOIntTreeMetaXferDestroy, NULL);
2903 RTMemFree(pIoStorage->pTreeMetaXfers);
2904 RTMemFree(pIoStorage);
2905 return VINF_SUCCESS;
2906}
2907
2908static int vdIOIntDelete(void *pvUser, const char *pcszFilename)
2909{
2910 PVDIO pVDIo = (PVDIO)pvUser;
2911 return pVDIo->pInterfaceIOCallbacks->pfnDelete(pVDIo->pInterfaceIO->pvUser,
2912 pcszFilename);
2913}
2914
2915static int vdIOIntMove(void *pvUser, const char *pcszSrc, const char *pcszDst,
2916 unsigned fMove)
2917{
2918 PVDIO pVDIo = (PVDIO)pvUser;
2919 return pVDIo->pInterfaceIOCallbacks->pfnMove(pVDIo->pInterfaceIO->pvUser,
2920 pcszSrc, pcszDst, fMove);
2921}
2922
2923static int vdIOIntGetFreeSpace(void *pvUser, const char *pcszFilename,
2924 int64_t *pcbFreeSpace)
2925{
2926 PVDIO pVDIo = (PVDIO)pvUser;
2927 return pVDIo->pInterfaceIOCallbacks->pfnGetFreeSpace(pVDIo->pInterfaceIO->pvUser,
2928 pcszFilename,
2929 pcbFreeSpace);
2930}
2931
2932static int vdIOIntGetModificationTime(void *pvUser, const char *pcszFilename,
2933 PRTTIMESPEC pModificationTime)
2934{
2935 PVDIO pVDIo = (PVDIO)pvUser;
2936 return pVDIo->pInterfaceIOCallbacks->pfnGetModificationTime(pVDIo->pInterfaceIO->pvUser,
2937 pcszFilename,
2938 pModificationTime);
2939}
2940
2941static int vdIOIntGetSize(void *pvUser, PVDIOSTORAGE pIoStorage,
2942 uint64_t *pcbSize)
2943{
2944 PVDIO pVDIo = (PVDIO)pvUser;
2945 return pVDIo->pInterfaceIOCallbacks->pfnGetSize(pVDIo->pInterfaceIO->pvUser,
2946 pIoStorage->pStorage,
2947 pcbSize);
2948}
2949
2950static int vdIOIntSetSize(void *pvUser, PVDIOSTORAGE pIoStorage,
2951 uint64_t cbSize)
2952{
2953 PVDIO pVDIo = (PVDIO)pvUser;
2954
2955 return pVDIo->pInterfaceIOCallbacks->pfnSetSize(pVDIo->pInterfaceIO->pvUser,
2956 pIoStorage->pStorage,
2957 cbSize);
2958}
2959
2960static int vdIOIntWriteSync(void *pvUser, PVDIOSTORAGE pIoStorage,
2961 uint64_t uOffset, const void *pvBuf,
2962 size_t cbWrite, size_t *pcbWritten)
2963{
2964 PVDIO pVDIo = (PVDIO)pvUser;
2965
2966 return pVDIo->pInterfaceIOCallbacks->pfnWriteSync(pVDIo->pInterfaceIO->pvUser,
2967 pIoStorage->pStorage,
2968 uOffset, pvBuf, cbWrite,
2969 pcbWritten);
2970}
2971
2972static int vdIOIntReadSync(void *pvUser, PVDIOSTORAGE pIoStorage,
2973 uint64_t uOffset, void *pvBuf, size_t cbRead,
2974 size_t *pcbRead)
2975{
2976 PVDIO pVDIo = (PVDIO)pvUser;
2977 return pVDIo->pInterfaceIOCallbacks->pfnReadSync(pVDIo->pInterfaceIO->pvUser,
2978 pIoStorage->pStorage,
2979 uOffset, pvBuf, cbRead,
2980 pcbRead);
2981}
2982
2983static int vdIOIntFlushSync(void *pvUser, PVDIOSTORAGE pIoStorage)
2984{
2985 PVDIO pVDIo = (PVDIO)pvUser;
2986 return pVDIo->pInterfaceIOCallbacks->pfnFlushSync(pVDIo->pInterfaceIO->pvUser,
2987 pIoStorage->pStorage);
2988}
2989
2990static int vdIOIntReadUserAsync(void *pvUser, PVDIOSTORAGE pIoStorage,
2991 uint64_t uOffset, PVDIOCTX pIoCtx,
2992 size_t cbRead)
2993{
2994 int rc = VINF_SUCCESS;
2995 PVDIO pVDIo = (PVDIO)pvUser;
2996 PVBOXHDD pDisk = pVDIo->pDisk;
2997
2998 LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pIoCtx=%#p cbRead=%u\n",
2999 pvUser, pIoStorage, uOffset, pIoCtx, cbRead));
3000
3001 VD_THREAD_IS_CRITSECT_OWNER(pDisk);
3002
3003 Assert(cbRead > 0);
3004
3005 /* Build the S/G array and spawn a new I/O task */
3006 while (cbRead)
3007 {
3008 RTSGSEG aSeg[VD_IO_TASK_SEGMENTS_MAX];
3009 unsigned cSegments = VD_IO_TASK_SEGMENTS_MAX;
3010 size_t cbTaskRead = 0;
3011
3012 cbTaskRead = RTSgBufSegArrayCreate(&pIoCtx->SgBuf, aSeg, &cSegments, cbRead);
3013
3014 Assert(cSegments > 0);
3015 Assert(cbTaskRead > 0);
3016 AssertMsg(cbTaskRead <= cbRead, ("Invalid number of bytes to read\n"));
3017
3018 LogFlow(("Reading %u bytes into %u segments\n", cbTaskRead, cSegments));
3019
3020#ifdef RT_STRICT
3021 for (unsigned i = 0; i < cSegments; i++)
3022 AssertMsg(aSeg[i].pvSeg && !(aSeg[i].cbSeg % 512),
3023 ("Segment %u is invalid\n", i));
3024#endif
3025
3026 PVDIOTASK pIoTask = vdIoTaskUserAlloc(pIoStorage, NULL, NULL, pIoCtx, cbTaskRead);
3027
3028 if (!pIoTask)
3029 return VERR_NO_MEMORY;
3030
3031 ASMAtomicIncU32(&pIoCtx->cDataTransfersPending);
3032
3033 void *pvTask;
3034 rc = pVDIo->pInterfaceIOCallbacks->pfnReadAsync(pVDIo->pInterfaceIO->pvUser,
3035 pIoStorage->pStorage,
3036 uOffset, aSeg, cSegments,
3037 cbTaskRead, pIoTask,
3038 &pvTask);
3039 if (RT_SUCCESS(rc))
3040 {
3041 AssertMsg(cbTaskRead <= pIoCtx->cbTransferLeft, ("Impossible!\n"));
3042 ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbTaskRead);
3043 ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
3044 vdIoTaskFree(pDisk, pIoTask);
3045 }
3046 else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
3047 {
3048 ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
3049 vdIoTaskFree(pDisk, pIoTask);
3050 break;
3051 }
3052
3053 uOffset += cbTaskRead;
3054 cbRead -= cbTaskRead;
3055 }
3056
3057 LogFlowFunc(("returns rc=%Rrc\n", rc));
3058 return rc;
3059}
3060
3061static int vdIOIntWriteUserAsync(void *pvUser, PVDIOSTORAGE pIoStorage,
3062 uint64_t uOffset, PVDIOCTX pIoCtx,
3063 size_t cbWrite,
3064 PFNVDXFERCOMPLETED pfnComplete,
3065 void *pvCompleteUser)
3066{
3067 int rc = VINF_SUCCESS;
3068 PVDIO pVDIo = (PVDIO)pvUser;
3069 PVBOXHDD pDisk = pVDIo->pDisk;
3070
3071 LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pIoCtx=%#p cbWrite=%u\n",
3072 pvUser, pIoStorage, uOffset, pIoCtx, cbWrite));
3073
3074 VD_THREAD_IS_CRITSECT_OWNER(pDisk);
3075
3076 Assert(cbWrite > 0);
3077
3078 /* Build the S/G array and spawn a new I/O task */
3079 while (cbWrite)
3080 {
3081 RTSGSEG aSeg[VD_IO_TASK_SEGMENTS_MAX];
3082 unsigned cSegments = VD_IO_TASK_SEGMENTS_MAX;
3083 size_t cbTaskWrite = 0;
3084
3085 cbTaskWrite = RTSgBufSegArrayCreate(&pIoCtx->SgBuf, aSeg, &cSegments, cbWrite);
3086
3087 Assert(cSegments > 0);
3088 Assert(cbTaskWrite > 0);
3089 AssertMsg(cbTaskWrite <= cbWrite, ("Invalid number of bytes to write\n"));
3090
3091 LogFlow(("Writing %u bytes from %u segments\n", cbTaskWrite, cSegments));
3092
3093#ifdef DEBUG
3094 for (unsigned i = 0; i < cSegments; i++)
3095 AssertMsg(aSeg[i].pvSeg && !(aSeg[i].cbSeg % 512),
3096 ("Segment %u is invalid\n", i));
3097#endif
3098
3099 PVDIOTASK pIoTask = vdIoTaskUserAlloc(pIoStorage, pfnComplete, pvCompleteUser, pIoCtx, cbTaskWrite);
3100
3101 if (!pIoTask)
3102 return VERR_NO_MEMORY;
3103
3104 ASMAtomicIncU32(&pIoCtx->cDataTransfersPending);
3105
3106 void *pvTask;
3107 rc = pVDIo->pInterfaceIOCallbacks->pfnWriteAsync(pVDIo->pInterfaceIO->pvUser,
3108 pIoStorage->pStorage,
3109 uOffset, aSeg, cSegments,
3110 cbTaskWrite, pIoTask,
3111 &pvTask);
3112 if (RT_SUCCESS(rc))
3113 {
3114 AssertMsg(cbTaskWrite <= pIoCtx->cbTransferLeft, ("Impossible!\n"));
3115 ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbTaskWrite);
3116 ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
3117 vdIoTaskFree(pDisk, pIoTask);
3118 }
3119 else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
3120 {
3121 ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
3122 vdIoTaskFree(pDisk, pIoTask);
3123 break;
3124 }
3125
3126 uOffset += cbTaskWrite;
3127 cbWrite -= cbTaskWrite;
3128 }
3129
3130 return rc;
3131}
3132
3133static int vdIOIntReadMetaAsync(void *pvUser, PVDIOSTORAGE pIoStorage,
3134 uint64_t uOffset, void *pvBuf,
3135 size_t cbRead, PVDIOCTX pIoCtx,
3136 PPVDMETAXFER ppMetaXfer,
3137 PFNVDXFERCOMPLETED pfnComplete,
3138 void *pvCompleteUser)
3139{
3140 PVDIO pVDIo = (PVDIO)pvUser;
3141 PVBOXHDD pDisk = pVDIo->pDisk;
3142 int rc = VINF_SUCCESS;
3143 RTSGSEG Seg;
3144 PVDIOTASK pIoTask;
3145 PVDMETAXFER pMetaXfer = NULL;
3146 void *pvTask = NULL;
3147
3148 LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pvBuf=%#p cbRead=%u\n",
3149 pvUser, pIoStorage, uOffset, pvBuf, cbRead));
3150
3151 VD_THREAD_IS_CRITSECT_OWNER(pDisk);
3152
3153 pMetaXfer = (PVDMETAXFER)RTAvlrFileOffsetGet(pIoStorage->pTreeMetaXfers, uOffset);
3154 if (!pMetaXfer)
3155 {
3156#ifdef RT_STRICT
3157 pMetaXfer = (PVDMETAXFER)RTAvlrFileOffsetGetBestFit(pIoStorage->pTreeMetaXfers, uOffset, false /* fAbove */);
3158 AssertMsg(!pMetaXfer || (pMetaXfer->Core.Key + (RTFOFF)pMetaXfer->cbMeta <= (RTFOFF)uOffset),
3159 ("Overlapping meta transfers!\n"));
3160#endif
3161
3162 /* Allocate a new meta transfer. */
3163 pMetaXfer = vdMetaXferAlloc(pIoStorage, uOffset, cbRead);
3164 if (!pMetaXfer)
3165 return VERR_NO_MEMORY;
3166
3167 pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvCompleteUser, pMetaXfer);
3168 if (!pIoTask)
3169 {
3170 RTMemFree(pMetaXfer);
3171 return VERR_NO_MEMORY;
3172 }
3173
3174 Seg.cbSeg = cbRead;
3175 Seg.pvSeg = pMetaXfer->abData;
3176
3177 VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_READ);
3178 rc = pVDIo->pInterfaceIOCallbacks->pfnReadAsync(pVDIo->pInterfaceIO->pvUser,
3179 pIoStorage->pStorage,
3180 uOffset, &Seg, 1,
3181 cbRead, pIoTask,
3182 &pvTask);
3183
3184 if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
3185 {
3186 bool fInserted = RTAvlrFileOffsetInsert(pIoStorage->pTreeMetaXfers, &pMetaXfer->Core);
3187 Assert(fInserted);
3188 }
3189 else
3190 RTMemFree(pMetaXfer);
3191
3192 if (RT_SUCCESS(rc))
3193 {
3194 VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
3195 vdIoTaskFree(pDisk, pIoTask);
3196 }
3197 else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS && !pfnComplete)
3198 rc = VERR_VD_NOT_ENOUGH_METADATA;
3199 }
3200
3201 Assert(VALID_PTR(pMetaXfer) || RT_FAILURE(rc));
3202
3203 if (RT_SUCCESS(rc) || rc == VERR_VD_NOT_ENOUGH_METADATA || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
3204 {
3205 /* If it is pending add the request to the list. */
3206 if (VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_READ)
3207 {
3208 PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
3209 AssertPtr(pDeferred);
3210
3211 RTListInit(&pDeferred->NodeDeferred);
3212 pDeferred->pIoCtx = pIoCtx;
3213
3214 ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
3215 RTListAppend(&pMetaXfer->ListIoCtxWaiting, &pDeferred->NodeDeferred);
3216 rc = VERR_VD_NOT_ENOUGH_METADATA;
3217 }
3218 else
3219 {
3220 /* Transfer the data. */
3221 pMetaXfer->cRefs++;
3222 Assert(pMetaXfer->cbMeta >= cbRead);
3223 Assert(pMetaXfer->Core.Key == (RTFOFF)uOffset);
3224 memcpy(pvBuf, pMetaXfer->abData, cbRead);
3225 *ppMetaXfer = pMetaXfer;
3226 }
3227 }
3228
3229 return rc;
3230}
3231
3232static int vdIOIntWriteMetaAsync(void *pvUser, PVDIOSTORAGE pIoStorage,
3233 uint64_t uOffset, void *pvBuf,
3234 size_t cbWrite, PVDIOCTX pIoCtx,
3235 PFNVDXFERCOMPLETED pfnComplete,
3236 void *pvCompleteUser)
3237{
3238 PVDIO pVDIo = (PVDIO)pvUser;
3239 PVBOXHDD pDisk = pVDIo->pDisk;
3240 int rc = VINF_SUCCESS;
3241 RTSGSEG Seg;
3242 PVDIOTASK pIoTask;
3243 PVDMETAXFER pMetaXfer = NULL;
3244 bool fInTree = false;
3245 void *pvTask = NULL;
3246
3247 LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pvBuf=%#p cbWrite=%u\n",
3248 pvUser, pIoStorage, uOffset, pvBuf, cbWrite));
3249
3250 VD_THREAD_IS_CRITSECT_OWNER(pDisk);
3251
3252 pMetaXfer = (PVDMETAXFER)RTAvlrFileOffsetGet(pIoStorage->pTreeMetaXfers, uOffset);
3253 if (!pMetaXfer)
3254 {
3255 /* Allocate a new meta transfer. */
3256 pMetaXfer = vdMetaXferAlloc(pIoStorage, uOffset, cbWrite);
3257 if (!pMetaXfer)
3258 return VERR_NO_MEMORY;
3259 }
3260 else
3261 {
3262 Assert(pMetaXfer->cbMeta >= cbWrite);
3263 Assert(pMetaXfer->Core.Key == (RTFOFF)uOffset);
3264 fInTree = true;
3265 }
3266
3267 Assert(VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE);
3268
3269 pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvCompleteUser, pMetaXfer);
3270 if (!pIoTask)
3271 {
3272 RTMemFree(pMetaXfer);
3273 return VERR_NO_MEMORY;
3274 }
3275
3276 memcpy(pMetaXfer->abData, pvBuf, cbWrite);
3277 Seg.cbSeg = cbWrite;
3278 Seg.pvSeg = pMetaXfer->abData;
3279
3280 ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
3281
3282 VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_WRITE);
3283 rc = pVDIo->pInterfaceIOCallbacks->pfnWriteAsync(pVDIo->pInterfaceIO->pvUser,
3284 pIoStorage->pStorage,
3285 uOffset, &Seg, 1,
3286 cbWrite, pIoTask,
3287 &pvTask);
3288 if (RT_SUCCESS(rc))
3289 {
3290 VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
3291 ASMAtomicDecU32(&pIoCtx->cMetaTransfersPending);
3292 vdIoTaskFree(pDisk, pIoTask);
3293 if (fInTree && !pMetaXfer->cRefs)
3294 {
3295 LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
3296 bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
3297 AssertMsg(fRemoved, ("Metadata transfer wasn't removed\n"));
3298 RTMemFree(pMetaXfer);
3299 pMetaXfer = NULL;
3300 }
3301 }
3302 else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
3303 {
3304 PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
3305 AssertPtr(pDeferred);
3306
3307 RTListInit(&pDeferred->NodeDeferred);
3308 pDeferred->pIoCtx = pIoCtx;
3309
3310 if (!fInTree)
3311 {
3312 bool fInserted = RTAvlrFileOffsetInsert(pIoStorage->pTreeMetaXfers, &pMetaXfer->Core);
3313 Assert(fInserted);
3314 }
3315
3316 RTListAppend(&pMetaXfer->ListIoCtxWaiting, &pDeferred->NodeDeferred);
3317 }
3318 else
3319 {
3320 RTMemFree(pMetaXfer);
3321 pMetaXfer = NULL;
3322 }
3323
3324 return rc;
3325}
3326
3327static void vdIOIntMetaXferRelease(void *pvUser, PVDMETAXFER pMetaXfer)
3328{
3329 PVDIO pVDIo = (PVDIO)pvUser;
3330 PVBOXHDD pDisk = pVDIo->pDisk;
3331 PVDIOSTORAGE pIoStorage = pMetaXfer->pIoStorage;
3332
3333 VD_THREAD_IS_CRITSECT_OWNER(pDisk);
3334
3335 Assert( VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE
3336 || VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_WRITE);
3337 Assert(pMetaXfer->cRefs > 0);
3338
3339 pMetaXfer->cRefs--;
3340 if ( !pMetaXfer->cRefs
3341 && RTListIsEmpty(&pMetaXfer->ListIoCtxWaiting)
3342 && VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE)
3343 {
3344 /* Free the meta data entry. */
3345 LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
3346 bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
3347 AssertMsg(fRemoved, ("Metadata transfer wasn't removed\n"));
3348
3349 RTMemFree(pMetaXfer);
3350 }
3351}
3352
3353static int vdIOIntFlushAsync(void *pvUser, PVDIOSTORAGE pIoStorage,
3354 PVDIOCTX pIoCtx, PFNVDXFERCOMPLETED pfnComplete,
3355 void *pvCompleteUser)
3356{
3357 PVDIO pVDIo = (PVDIO)pvUser;
3358 PVBOXHDD pDisk = pVDIo->pDisk;
3359 int rc = VINF_SUCCESS;
3360 PVDIOTASK pIoTask;
3361 PVDMETAXFER pMetaXfer = NULL;
3362 void *pvTask = NULL;
3363
3364 VD_THREAD_IS_CRITSECT_OWNER(pDisk);
3365
3366 LogFlowFunc(("pvUser=%#p pIoStorage=%#p pIoCtx=%#p\n",
3367 pvUser, pIoStorage, pIoCtx));
3368
3369 /* Allocate a new meta transfer. */
3370 pMetaXfer = vdMetaXferAlloc(pIoStorage, 0, 0);
3371 if (!pMetaXfer)
3372 return VERR_NO_MEMORY;
3373
3374 pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvUser, pMetaXfer);
3375 if (!pIoTask)
3376 {
3377 RTMemFree(pMetaXfer);
3378 return VERR_NO_MEMORY;
3379 }
3380
3381 ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
3382
3383 PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
3384 AssertPtr(pDeferred);
3385
3386 RTListInit(&pDeferred->NodeDeferred);
3387 pDeferred->pIoCtx = pIoCtx;
3388
3389 RTListAppend(&pMetaXfer->ListIoCtxWaiting, &pDeferred->NodeDeferred);
3390 VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_FLUSH);
3391 rc = pVDIo->pInterfaceIOCallbacks->pfnFlushAsync(pVDIo->pInterfaceIO->pvUser,
3392 pIoStorage->pStorage,
3393 pIoTask, &pvTask);
3394 if (RT_SUCCESS(rc))
3395 {
3396 VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
3397 ASMAtomicDecU32(&pIoCtx->cMetaTransfersPending);
3398 vdIoTaskFree(pDisk, pIoTask);
3399 RTMemFree(pDeferred);
3400 RTMemFree(pMetaXfer);
3401 }
3402 else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
3403 RTMemFree(pMetaXfer);
3404
3405 return rc;
3406}
3407
3408static size_t vdIOIntIoCtxCopyTo(void *pvUser, PVDIOCTX pIoCtx,
3409 void *pvBuf, size_t cbBuf)
3410{
3411 PVDIO pVDIo = (PVDIO)pvUser;
3412 PVBOXHDD pDisk = pVDIo->pDisk;
3413 size_t cbCopied = 0;
3414
3415 VD_THREAD_IS_CRITSECT_OWNER(pDisk);
3416
3417 cbCopied = vdIoCtxCopyTo(pIoCtx, (uint8_t *)pvBuf, cbBuf);
3418 Assert(cbCopied == cbBuf);
3419
3420 ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbCopied);
3421
3422 return cbCopied;
3423}
3424
3425static size_t vdIOIntIoCtxCopyFrom(void *pvUser, PVDIOCTX pIoCtx,
3426 void *pvBuf, size_t cbBuf)
3427{
3428 PVDIO pVDIo = (PVDIO)pvUser;
3429 PVBOXHDD pDisk = pVDIo->pDisk;
3430 size_t cbCopied = 0;
3431
3432 VD_THREAD_IS_CRITSECT_OWNER(pDisk);
3433
3434 cbCopied = vdIoCtxCopyFrom(pIoCtx, (uint8_t *)pvBuf, cbBuf);
3435 Assert(cbCopied == cbBuf);
3436
3437 ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbCopied);
3438
3439 return cbCopied;
3440}
3441
3442static size_t vdIOIntIoCtxSet(void *pvUser, PVDIOCTX pIoCtx, int ch, size_t cb)
3443{
3444 PVDIO pVDIo = (PVDIO)pvUser;
3445 PVBOXHDD pDisk = pVDIo->pDisk;
3446 size_t cbSet = 0;
3447
3448 VD_THREAD_IS_CRITSECT_OWNER(pDisk);
3449
3450 cbSet = vdIoCtxSet(pIoCtx, ch, cb);
3451 Assert(cbSet == cb);
3452
3453 ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbSet);
3454
3455 return cbSet;
3456}
3457
3458static size_t vdIOIntIoCtxSegArrayCreate(void *pvUser, PVDIOCTX pIoCtx,
3459 PRTSGSEG paSeg, unsigned *pcSeg,
3460 size_t cbData)
3461{
3462 PVDIO pVDIo = (PVDIO)pvUser;
3463 PVBOXHDD pDisk = pVDIo->pDisk;
3464 size_t cbCreated = 0;
3465
3466 VD_THREAD_IS_CRITSECT_OWNER(pDisk);
3467
3468 cbCreated = RTSgBufSegArrayCreate(&pIoCtx->SgBuf, paSeg, pcSeg, cbData);
3469 Assert(!paSeg || cbData == cbCreated);
3470
3471 return cbCreated;
3472}
3473
3474static void vdIOIntIoCtxCompleted(void *pvUser, PVDIOCTX pIoCtx, int rcReq,
3475 size_t cbCompleted)
3476{
3477 PVDIO pVDIo = (PVDIO)pvUser;
3478 PVBOXHDD pDisk = pVDIo->pDisk;
3479
3480 /*
3481 * Grab the disk critical section to avoid races with other threads which
3482 * might still modify the I/O context.
3483 * Example is that iSCSI is doing an asynchronous write but calls us already
3484 * while the other thread is still hanging in vdWriteHelperAsync and couldn't update
3485 * the fBlocked state yet.
3486 * It can overwrite the state to true before we call vdIoCtxContinue and the
3487 * the request would hang indefinite.
3488 */
3489 int rc = RTCritSectEnter(&pDisk->CritSect);
3490 AssertRC(rc);
3491
3492 /* Continue */
3493 pIoCtx->fBlocked = false;
3494 ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbCompleted);
3495
3496 /* Clear the pointer to next transfer function in case we have nothing to transfer anymore.
3497 * @todo: Find a better way to prevent vdIoCtxContinue from calling the read/write helper again. */
3498 if (!pIoCtx->cbTransferLeft)
3499 pIoCtx->pfnIoCtxTransfer = NULL;
3500
3501 vdIoCtxContinue(pIoCtx, rcReq);
3502
3503 rc = RTCritSectLeave(&pDisk->CritSect);
3504 AssertRC(rc);
3505}
3506
3507/**
3508 * VD I/O interface callback for opening a file (limited version for VDGetFormat).
3509 */
3510static int vdIOIntOpenLimited(void *pvUser, const char *pszLocation,
3511 uint32_t fOpen, PPVDIOSTORAGE ppIoStorage)
3512{
3513 int rc = VINF_SUCCESS;
3514 PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
3515 PVDIOSTORAGE pIoStorage = (PVDIOSTORAGE)RTMemAllocZ(sizeof(VDIOSTORAGE));
3516
3517 if (!pIoStorage)
3518 return VERR_NO_MEMORY;
3519
3520 rc = pInterfaceIOCallbacks->pfnOpen(NULL, pszLocation, fOpen,
3521 NULL, &pIoStorage->pStorage);
3522 if (RT_SUCCESS(rc))
3523 *ppIoStorage = pIoStorage;
3524 else
3525 RTMemFree(pIoStorage);
3526
3527 return rc;
3528}
3529
3530static int vdIOIntCloseLimited(void *pvUser, PVDIOSTORAGE pIoStorage)
3531{
3532 PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
3533 int rc = pInterfaceIOCallbacks->pfnClose(NULL, pIoStorage->pStorage);
3534 AssertRC(rc);
3535
3536 RTMemFree(pIoStorage);
3537 return VINF_SUCCESS;
3538}
3539
3540static int vdIOIntDeleteLimited(void *pvUser, const char *pcszFilename)
3541{
3542 PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
3543 return pInterfaceIOCallbacks->pfnDelete(NULL, pcszFilename);
3544}
3545
3546static int vdIOIntMoveLimited(void *pvUser, const char *pcszSrc,
3547 const char *pcszDst, unsigned fMove)
3548{
3549 PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
3550 return pInterfaceIOCallbacks->pfnMove(NULL, pcszSrc, pcszDst, fMove);
3551}
3552
3553static int vdIOIntGetFreeSpaceLimited(void *pvUser, const char *pcszFilename,
3554 int64_t *pcbFreeSpace)
3555{
3556 PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
3557 return pInterfaceIOCallbacks->pfnGetFreeSpace(NULL, pcszFilename, pcbFreeSpace);
3558}
3559
3560static int vdIOIntGetModificationTimeLimited(void *pvUser,
3561 const char *pcszFilename,
3562 PRTTIMESPEC pModificationTime)
3563{
3564 PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
3565 return pInterfaceIOCallbacks->pfnGetModificationTime(NULL, pcszFilename, pModificationTime);
3566}
3567
3568static int vdIOIntGetSizeLimited(void *pvUser, PVDIOSTORAGE pIoStorage,
3569 uint64_t *pcbSize)
3570{
3571 PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
3572 return pInterfaceIOCallbacks->pfnGetSize(NULL, pIoStorage->pStorage, pcbSize);
3573}
3574
3575static int vdIOIntSetSizeLimited(void *pvUser, PVDIOSTORAGE pIoStorage,
3576 uint64_t cbSize)
3577{
3578 PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
3579 return pInterfaceIOCallbacks->pfnSetSize(NULL, pIoStorage->pStorage, cbSize);
3580}
3581
3582static int vdIOIntWriteSyncLimited(void *pvUser, PVDIOSTORAGE pIoStorage,
3583 uint64_t uOffset, const void *pvBuf,
3584 size_t cbWrite, size_t *pcbWritten)
3585{
3586 PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
3587 return pInterfaceIOCallbacks->pfnWriteSync(NULL, pIoStorage->pStorage, uOffset, pvBuf, cbWrite, pcbWritten);
3588}
3589
3590static int vdIOIntReadSyncLimited(void *pvUser, PVDIOSTORAGE pIoStorage,
3591 uint64_t uOffset, void *pvBuf, size_t cbRead,
3592 size_t *pcbRead)
3593{
3594 PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
3595 return pInterfaceIOCallbacks->pfnReadSync(NULL, pIoStorage->pStorage, uOffset, pvBuf, cbRead, pcbRead);
3596}
3597
3598static int vdIOIntFlushSyncLimited(void *pvUser, PVDIOSTORAGE pIoStorage)
3599{
3600 PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
3601 return pInterfaceIOCallbacks->pfnFlushSync(NULL, pIoStorage->pStorage);
3602}
3603
3604/**
3605 * internal: send output to the log (unconditionally).
3606 */
3607int vdLogMessage(void *pvUser, const char *pszFormat, va_list args)
3608{
3609 NOREF(pvUser);
3610 RTLogPrintfV(pszFormat, args);
3611 return VINF_SUCCESS;
3612}
3613
3614DECLINLINE(int) vdMessageWrapper(PVBOXHDD pDisk, const char *pszFormat, ...)
3615{
3616 va_list va;
3617 va_start(va, pszFormat);
3618 int rc = pDisk->pInterfaceErrorCallbacks->pfnMessage(pDisk->pInterfaceError->pvUser,
3619 pszFormat, va);
3620 va_end(va);
3621 return rc;
3622}
3623
3624
3625/**
3626 * internal: adjust PCHS geometry
3627 */
3628static void vdFixupPCHSGeometry(PVDGEOMETRY pPCHS, uint64_t cbSize)
3629{
3630 /* Fix broken PCHS geometry. Can happen for two reasons: either the backend
3631 * mixes up PCHS and LCHS, or the application used to create the source
3632 * image has put garbage in it. Additionally, if the PCHS geometry covers
3633 * more than the image size, set it back to the default. */
3634 if ( pPCHS->cHeads > 16
3635 || pPCHS->cSectors > 63
3636 || pPCHS->cCylinders == 0
3637 || (uint64_t)pPCHS->cHeads * pPCHS->cSectors * pPCHS->cCylinders * 512 > cbSize)
3638 {
3639 Assert(!(RT_MIN(cbSize / 512 / 16 / 63, 16383) - (uint32_t)RT_MIN(cbSize / 512 / 16 / 63, 16383)));
3640 pPCHS->cCylinders = (uint32_t)RT_MIN(cbSize / 512 / 16 / 63, 16383);
3641 pPCHS->cHeads = 16;
3642 pPCHS->cSectors = 63;
3643 }
3644}
3645
3646/**
3647 * internal: adjust PCHS geometry
3648 */
3649static void vdFixupLCHSGeometry(PVDGEOMETRY pLCHS, uint64_t cbSize)
3650{
3651 /* Fix broken LCHS geometry. Can happen for two reasons: either the backend
3652 * mixes up PCHS and LCHS, or the application used to create the source
3653 * image has put garbage in it. The fix in this case is to clear the LCHS
3654 * geometry to trigger autodetection when it is used next. If the geometry
3655 * already says "please autodetect" (cylinders=0) keep it. */
3656 if ( ( pLCHS->cHeads > 255
3657 || pLCHS->cHeads == 0
3658 || pLCHS->cSectors > 63
3659 || pLCHS->cSectors == 0)
3660 && pLCHS->cCylinders != 0)
3661 {
3662 pLCHS->cCylinders = 0;
3663 pLCHS->cHeads = 0;
3664 pLCHS->cSectors = 0;
3665 }
3666 /* Always recompute the number of cylinders stored in the LCHS
3667 * geometry if it isn't set to "autotedetect" at the moment.
3668 * This is very useful if the destination image size is
3669 * larger or smaller than the source image size. Do not modify
3670 * the number of heads and sectors. Windows guests hate it. */
3671 if ( pLCHS->cCylinders != 0
3672 && pLCHS->cHeads != 0 /* paranoia */
3673 && pLCHS->cSectors != 0 /* paranoia */)
3674 {
3675 Assert(!(RT_MIN(cbSize / 512 / pLCHS->cHeads / pLCHS->cSectors, 1024) - (uint32_t)RT_MIN(cbSize / 512 / pLCHS->cHeads / pLCHS->cSectors, 1024)));
3676 pLCHS->cCylinders = (uint32_t)RT_MIN(cbSize / 512 / pLCHS->cHeads / pLCHS->cSectors, 1024);
3677 }
3678}
3679
3680/**
3681 * Initializes HDD backends.
3682 *
3683 * @returns VBox status code.
3684 */
3685VBOXDDU_DECL(int) VDInit(void)
3686{
3687 int rc = vdAddBackends(aStaticBackends, RT_ELEMENTS(aStaticBackends));
3688 if (RT_SUCCESS(rc))
3689 {
3690 rc = vdAddCacheBackends(aStaticCacheBackends, RT_ELEMENTS(aStaticCacheBackends));
3691 if (RT_SUCCESS(rc))
3692 {
3693 rc = vdLoadDynamicBackends();
3694 if (RT_SUCCESS(rc))
3695 rc = vdLoadDynamicCacheBackends();
3696 }
3697 }
3698 LogRel(("VDInit finished\n"));
3699 return rc;
3700}
3701
3702/**
3703 * Destroys loaded HDD backends.
3704 *
3705 * @returns VBox status code.
3706 */
3707VBOXDDU_DECL(int) VDShutdown(void)
3708{
3709 PVBOXHDDBACKEND *pBackends = g_apBackends;
3710 PVDCACHEBACKEND *pCacheBackends = g_apCacheBackends;
3711 unsigned cBackends = g_cBackends;
3712
3713 if (!pBackends)
3714 return VERR_INTERNAL_ERROR;
3715
3716 g_cBackends = 0;
3717 g_apBackends = NULL;
3718
3719#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
3720 for (unsigned i = 0; i < cBackends; i++)
3721 if (pBackends[i]->hPlugin != NIL_RTLDRMOD)
3722 RTLdrClose(pBackends[i]->hPlugin);
3723#endif
3724
3725 /* Clear the supported cache backends. */
3726 cBackends = g_cCacheBackends;
3727 g_cCacheBackends = 0;
3728 g_apCacheBackends = NULL;
3729
3730#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
3731 for (unsigned i = 0; i < cBackends; i++)
3732 if (pCacheBackends[i]->hPlugin != NIL_RTLDRMOD)
3733 RTLdrClose(pCacheBackends[i]->hPlugin);
3734#endif
3735
3736 if (pCacheBackends)
3737 RTMemFree(pCacheBackends);
3738 RTMemFree(pBackends);
3739 return VINF_SUCCESS;
3740}
3741
3742
3743/**
3744 * Lists all HDD backends and their capabilities in a caller-provided buffer.
3745 *
3746 * @returns VBox status code.
3747 * VERR_BUFFER_OVERFLOW if not enough space is passed.
3748 * @param cEntriesAlloc Number of list entries available.
3749 * @param pEntries Pointer to array for the entries.
3750 * @param pcEntriesUsed Number of entries returned.
3751 */
3752VBOXDDU_DECL(int) VDBackendInfo(unsigned cEntriesAlloc, PVDBACKENDINFO pEntries,
3753 unsigned *pcEntriesUsed)
3754{
3755 int rc = VINF_SUCCESS;
3756 PRTDIR pPluginDir = NULL;
3757 unsigned cEntries = 0;
3758
3759 LogFlowFunc(("cEntriesAlloc=%u pEntries=%#p pcEntriesUsed=%#p\n", cEntriesAlloc, pEntries, pcEntriesUsed));
3760 /* Check arguments. */
3761 AssertMsgReturn(cEntriesAlloc,
3762 ("cEntriesAlloc=%u\n", cEntriesAlloc),
3763 VERR_INVALID_PARAMETER);
3764 AssertMsgReturn(VALID_PTR(pEntries),
3765 ("pEntries=%#p\n", pEntries),
3766 VERR_INVALID_PARAMETER);
3767 AssertMsgReturn(VALID_PTR(pcEntriesUsed),
3768 ("pcEntriesUsed=%#p\n", pcEntriesUsed),
3769 VERR_INVALID_PARAMETER);
3770 if (!g_apBackends)
3771 VDInit();
3772
3773 if (cEntriesAlloc < g_cBackends)
3774 {
3775 *pcEntriesUsed = g_cBackends;
3776 return VERR_BUFFER_OVERFLOW;
3777 }
3778
3779 for (unsigned i = 0; i < g_cBackends; i++)
3780 {
3781 pEntries[i].pszBackend = g_apBackends[i]->pszBackendName;
3782 pEntries[i].uBackendCaps = g_apBackends[i]->uBackendCaps;
3783 pEntries[i].paFileExtensions = g_apBackends[i]->paFileExtensions;
3784 pEntries[i].paConfigInfo = g_apBackends[i]->paConfigInfo;
3785 pEntries[i].pfnComposeLocation = g_apBackends[i]->pfnComposeLocation;
3786 pEntries[i].pfnComposeName = g_apBackends[i]->pfnComposeName;
3787 }
3788
3789 LogFlowFunc(("returns %Rrc *pcEntriesUsed=%u\n", rc, cEntries));
3790 *pcEntriesUsed = g_cBackends;
3791 return rc;
3792}
3793
3794/**
3795 * Lists the capabilities of a backend identified by its name.
3796 *
3797 * @returns VBox status code.
3798 * @param pszBackend The backend name.
3799 * @param pEntries Pointer to an entry.
3800 */
3801VBOXDDU_DECL(int) VDBackendInfoOne(const char *pszBackend, PVDBACKENDINFO pEntry)
3802{
3803 LogFlowFunc(("pszBackend=%#p pEntry=%#p\n", pszBackend, pEntry));
3804 /* Check arguments. */
3805 AssertMsgReturn(VALID_PTR(pszBackend),
3806 ("pszBackend=%#p\n", pszBackend),
3807 VERR_INVALID_PARAMETER);
3808 AssertMsgReturn(VALID_PTR(pEntry),
3809 ("pEntry=%#p\n", pEntry),
3810 VERR_INVALID_PARAMETER);
3811 if (!g_apBackends)
3812 VDInit();
3813
3814 /* Go through loaded backends. */
3815 for (unsigned i = 0; i < g_cBackends; i++)
3816 {
3817 if (!RTStrICmp(pszBackend, g_apBackends[i]->pszBackendName))
3818 {
3819 pEntry->pszBackend = g_apBackends[i]->pszBackendName;
3820 pEntry->uBackendCaps = g_apBackends[i]->uBackendCaps;
3821 pEntry->paFileExtensions = g_apBackends[i]->paFileExtensions;
3822 pEntry->paConfigInfo = g_apBackends[i]->paConfigInfo;
3823 return VINF_SUCCESS;
3824 }
3825 }
3826
3827 return VERR_NOT_FOUND;
3828}
3829
3830/**
3831 * Allocates and initializes an empty HDD container.
3832 * No image files are opened.
3833 *
3834 * @returns VBox status code.
3835 * @param pVDIfsDisk Pointer to the per-disk VD interface list.
3836 * @param enmType Type of the image container.
3837 * @param ppDisk Where to store the reference to HDD container.
3838 */
3839VBOXDDU_DECL(int) VDCreate(PVDINTERFACE pVDIfsDisk, VDTYPE enmType, PVBOXHDD *ppDisk)
3840{
3841 int rc = VINF_SUCCESS;
3842 PVBOXHDD pDisk = NULL;
3843
3844 LogFlowFunc(("pVDIfsDisk=%#p\n", pVDIfsDisk));
3845 do
3846 {
3847 /* Check arguments. */
3848 AssertMsgBreakStmt(VALID_PTR(ppDisk),
3849 ("ppDisk=%#p\n", ppDisk),
3850 rc = VERR_INVALID_PARAMETER);
3851
3852 pDisk = (PVBOXHDD)RTMemAllocZ(sizeof(VBOXHDD));
3853 if (pDisk)
3854 {
3855 pDisk->u32Signature = VBOXHDDDISK_SIGNATURE;
3856 pDisk->enmType = enmType;
3857 pDisk->cImages = 0;
3858 pDisk->pBase = NULL;
3859 pDisk->pLast = NULL;
3860 pDisk->cbSize = 0;
3861 pDisk->PCHSGeometry.cCylinders = 0;
3862 pDisk->PCHSGeometry.cHeads = 0;
3863 pDisk->PCHSGeometry.cSectors = 0;
3864 pDisk->LCHSGeometry.cCylinders = 0;
3865 pDisk->LCHSGeometry.cHeads = 0;
3866 pDisk->LCHSGeometry.cSectors = 0;
3867 pDisk->pVDIfsDisk = pVDIfsDisk;
3868 pDisk->pInterfaceError = NULL;
3869 pDisk->pInterfaceErrorCallbacks = NULL;
3870 pDisk->pInterfaceThreadSync = NULL;
3871 pDisk->pInterfaceThreadSyncCallbacks = NULL;
3872 pDisk->fLocked = false;
3873 pDisk->pIoCtxLockOwner = NULL;
3874 RTListInit(&pDisk->ListWriteLocked);
3875
3876 /* Create the I/O ctx cache */
3877 rc = RTMemCacheCreate(&pDisk->hMemCacheIoCtx, sizeof(VDIOCTX), 0, UINT32_MAX,
3878 NULL, NULL, NULL, 0);
3879 if (RT_FAILURE(rc))
3880 {
3881 RTMemFree(pDisk);
3882 break;
3883 }
3884
3885 /* Create the I/O task cache */
3886 rc = RTMemCacheCreate(&pDisk->hMemCacheIoTask, sizeof(VDIOTASK), 0, UINT32_MAX,
3887 NULL, NULL, NULL, 0);
3888 if (RT_FAILURE(rc))
3889 {
3890 RTMemCacheDestroy(pDisk->hMemCacheIoCtx);
3891 RTMemFree(pDisk);
3892 break;
3893 }
3894
3895 /* Create critical section. */
3896 rc = RTCritSectInit(&pDisk->CritSect);
3897 if (RT_FAILURE(rc))
3898 {
3899 RTMemCacheDestroy(pDisk->hMemCacheIoCtx);
3900 RTMemCacheDestroy(pDisk->hMemCacheIoTask);
3901 RTMemFree(pDisk);
3902 break;
3903 }
3904
3905 pDisk->pInterfaceError = VDInterfaceGet(pVDIfsDisk, VDINTERFACETYPE_ERROR);
3906 if (pDisk->pInterfaceError)
3907 pDisk->pInterfaceErrorCallbacks = VDGetInterfaceError(pDisk->pInterfaceError);
3908
3909 pDisk->pInterfaceThreadSync = VDInterfaceGet(pVDIfsDisk, VDINTERFACETYPE_THREADSYNC);
3910 if (pDisk->pInterfaceThreadSync)
3911 pDisk->pInterfaceThreadSyncCallbacks = VDGetInterfaceThreadSync(pDisk->pInterfaceThreadSync);
3912
3913 /* Create fallback I/O callback table */
3914 pDisk->VDIIOCallbacks.cbSize = sizeof(VDINTERFACEIO);
3915 pDisk->VDIIOCallbacks.enmInterface = VDINTERFACETYPE_IO;
3916 pDisk->VDIIOCallbacks.pfnOpen = vdIOOpenFallback;
3917 pDisk->VDIIOCallbacks.pfnClose = vdIOCloseFallback;
3918 pDisk->VDIIOCallbacks.pfnDelete = vdIODeleteFallback;
3919 pDisk->VDIIOCallbacks.pfnMove = vdIOMoveFallback;
3920 pDisk->VDIIOCallbacks.pfnGetFreeSpace = vdIOGetFreeSpaceFallback;
3921 pDisk->VDIIOCallbacks.pfnGetModificationTime = vdIOGetModificationTimeFallback;
3922 pDisk->VDIIOCallbacks.pfnGetSize = vdIOGetSizeFallback;
3923 pDisk->VDIIOCallbacks.pfnSetSize = vdIOSetSizeFallback;
3924 pDisk->VDIIOCallbacks.pfnReadSync = vdIOReadSyncFallback;
3925 pDisk->VDIIOCallbacks.pfnWriteSync = vdIOWriteSyncFallback;
3926 pDisk->VDIIOCallbacks.pfnFlushSync = vdIOFlushSyncFallback;
3927 pDisk->VDIIOCallbacks.pfnReadAsync = vdIOReadAsyncFallback;
3928 pDisk->VDIIOCallbacks.pfnWriteAsync = vdIOWriteAsyncFallback;
3929 pDisk->VDIIOCallbacks.pfnFlushAsync = vdIOFlushAsyncFallback;
3930
3931 /*
3932 * Create the internal I/O callback table.
3933 * The interface is per-image but no need to duplicate the
3934 * callback table every time.
3935 */
3936 pDisk->VDIIOIntCallbacks.cbSize = sizeof(VDINTERFACEIOINT);
3937 pDisk->VDIIOIntCallbacks.enmInterface = VDINTERFACETYPE_IOINT;
3938 pDisk->VDIIOIntCallbacks.pfnOpen = vdIOIntOpen;
3939 pDisk->VDIIOIntCallbacks.pfnClose = vdIOIntClose;
3940 pDisk->VDIIOIntCallbacks.pfnDelete = vdIOIntDelete;
3941 pDisk->VDIIOIntCallbacks.pfnMove = vdIOIntMove;
3942 pDisk->VDIIOIntCallbacks.pfnGetFreeSpace = vdIOIntGetFreeSpace;
3943 pDisk->VDIIOIntCallbacks.pfnGetModificationTime = vdIOIntGetModificationTime;
3944 pDisk->VDIIOIntCallbacks.pfnGetSize = vdIOIntGetSize;
3945 pDisk->VDIIOIntCallbacks.pfnSetSize = vdIOIntSetSize;
3946 pDisk->VDIIOIntCallbacks.pfnReadSync = vdIOIntReadSync;
3947 pDisk->VDIIOIntCallbacks.pfnWriteSync = vdIOIntWriteSync;
3948 pDisk->VDIIOIntCallbacks.pfnFlushSync = vdIOIntFlushSync;
3949 pDisk->VDIIOIntCallbacks.pfnReadUserAsync = vdIOIntReadUserAsync;
3950 pDisk->VDIIOIntCallbacks.pfnWriteUserAsync = vdIOIntWriteUserAsync;
3951 pDisk->VDIIOIntCallbacks.pfnReadMetaAsync = vdIOIntReadMetaAsync;
3952 pDisk->VDIIOIntCallbacks.pfnWriteMetaAsync = vdIOIntWriteMetaAsync;
3953 pDisk->VDIIOIntCallbacks.pfnMetaXferRelease = vdIOIntMetaXferRelease;
3954 pDisk->VDIIOIntCallbacks.pfnFlushAsync = vdIOIntFlushAsync;
3955 pDisk->VDIIOIntCallbacks.pfnIoCtxCopyFrom = vdIOIntIoCtxCopyFrom;
3956 pDisk->VDIIOIntCallbacks.pfnIoCtxCopyTo = vdIOIntIoCtxCopyTo;
3957 pDisk->VDIIOIntCallbacks.pfnIoCtxSet = vdIOIntIoCtxSet;
3958 pDisk->VDIIOIntCallbacks.pfnIoCtxSegArrayCreate = vdIOIntIoCtxSegArrayCreate;
3959 pDisk->VDIIOIntCallbacks.pfnIoCtxCompleted = vdIOIntIoCtxCompleted;
3960
3961 *ppDisk = pDisk;
3962 }
3963 else
3964 {
3965 rc = VERR_NO_MEMORY;
3966 break;
3967 }
3968 } while (0);
3969
3970 LogFlowFunc(("returns %Rrc (pDisk=%#p)\n", rc, pDisk));
3971 return rc;
3972}
3973
3974/**
3975 * Destroys HDD container.
3976 * If container has opened image files they will be closed.
3977 *
3978 * @param pDisk Pointer to HDD container.
3979 */
3980VBOXDDU_DECL(void) VDDestroy(PVBOXHDD pDisk)
3981{
3982 LogFlowFunc(("pDisk=%#p\n", pDisk));
3983 do
3984 {
3985 /* sanity check */
3986 AssertPtrBreak(pDisk);
3987 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3988 VDCloseAll(pDisk);
3989 RTCritSectDelete(&pDisk->CritSect);
3990 RTMemCacheDestroy(pDisk->hMemCacheIoCtx);
3991 RTMemCacheDestroy(pDisk->hMemCacheIoTask);
3992 RTMemFree(pDisk);
3993 } while (0);
3994 LogFlowFunc(("returns\n"));
3995}
3996
3997/**
3998 * Try to get the backend name which can use this image.
3999 *
4000 * @returns VBox status code.
4001 * VINF_SUCCESS if a plugin was found.
4002 * ppszFormat contains the string which can be used as backend name.
4003 * VERR_NOT_SUPPORTED if no backend was found.
4004 * @param pVDIfsDisk Pointer to the per-disk VD interface list.
4005 * @param pVDIfsImage Pointer to the per-image VD interface list.
4006 * @param pszFilename Name of the image file for which the backend is queried.
4007 * @param ppszFormat Receives pointer of the UTF-8 string which contains the format name.
4008 * The returned pointer must be freed using RTStrFree().
4009 */
4010VBOXDDU_DECL(int) VDGetFormat(PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
4011 const char *pszFilename, char **ppszFormat, VDTYPE *penmType)
4012{
4013 int rc = VERR_NOT_SUPPORTED;
4014 VDINTERFACEIOINT VDIIOIntCallbacks;
4015 VDINTERFACE VDIIOInt;
4016 VDINTERFACEIO VDIIOCallbacksFallback;
4017 PVDINTERFACE pInterfaceIO;
4018 PVDINTERFACEIO pInterfaceIOCallbacks;
4019
4020 LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
4021 /* Check arguments. */
4022 AssertMsgReturn(VALID_PTR(pszFilename) && *pszFilename,
4023 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
4024 VERR_INVALID_PARAMETER);
4025 AssertMsgReturn(VALID_PTR(ppszFormat),
4026 ("ppszFormat=%#p\n", ppszFormat),
4027 VERR_INVALID_PARAMETER);
4028 AssertMsgReturn(VALID_PTR(ppszFormat),
4029 ("penmType=%#p\n", penmType),
4030 VERR_INVALID_PARAMETER);
4031
4032 if (!g_apBackends)
4033 VDInit();
4034
4035 pInterfaceIO = VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IO);
4036 if (!pInterfaceIO)
4037 {
4038 /*
4039 * Caller doesn't provide an I/O interface, create our own using the
4040 * native file API.
4041 */
4042 VDIIOCallbacksFallback.cbSize = sizeof(VDINTERFACEIO);
4043 VDIIOCallbacksFallback.enmInterface = VDINTERFACETYPE_IO;
4044 VDIIOCallbacksFallback.pfnOpen = vdIOOpenFallback;
4045 VDIIOCallbacksFallback.pfnClose = vdIOCloseFallback;
4046 VDIIOCallbacksFallback.pfnDelete = vdIODeleteFallback;
4047 VDIIOCallbacksFallback.pfnMove = vdIOMoveFallback;
4048 VDIIOCallbacksFallback.pfnGetFreeSpace = vdIOGetFreeSpaceFallback;
4049 VDIIOCallbacksFallback.pfnGetModificationTime = vdIOGetModificationTimeFallback;
4050 VDIIOCallbacksFallback.pfnGetSize = vdIOGetSizeFallback;
4051 VDIIOCallbacksFallback.pfnSetSize = vdIOSetSizeFallback;
4052 VDIIOCallbacksFallback.pfnReadSync = vdIOReadSyncFallback;
4053 VDIIOCallbacksFallback.pfnWriteSync = vdIOWriteSyncFallback;
4054 VDIIOCallbacksFallback.pfnFlushSync = vdIOFlushSyncFallback;
4055 pInterfaceIOCallbacks = &VDIIOCallbacksFallback;
4056 }
4057 else
4058 pInterfaceIOCallbacks = VDGetInterfaceIO(pInterfaceIO);
4059
4060 /* Set up the internal I/O interface. */
4061 AssertReturn(!VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IOINT),
4062 VERR_INVALID_PARAMETER);
4063 VDIIOIntCallbacks.cbSize = sizeof(VDINTERFACEIOINT);
4064 VDIIOIntCallbacks.enmInterface = VDINTERFACETYPE_IOINT;
4065 VDIIOIntCallbacks.pfnOpen = vdIOIntOpenLimited;
4066 VDIIOIntCallbacks.pfnClose = vdIOIntCloseLimited;
4067 VDIIOIntCallbacks.pfnDelete = vdIOIntDeleteLimited;
4068 VDIIOIntCallbacks.pfnMove = vdIOIntMoveLimited;
4069 VDIIOIntCallbacks.pfnGetFreeSpace = vdIOIntGetFreeSpaceLimited;
4070 VDIIOIntCallbacks.pfnGetModificationTime = vdIOIntGetModificationTimeLimited;
4071 VDIIOIntCallbacks.pfnGetSize = vdIOIntGetSizeLimited;
4072 VDIIOIntCallbacks.pfnSetSize = vdIOIntSetSizeLimited;
4073 VDIIOIntCallbacks.pfnReadSync = vdIOIntReadSyncLimited;
4074 VDIIOIntCallbacks.pfnWriteSync = vdIOIntWriteSyncLimited;
4075 VDIIOIntCallbacks.pfnFlushSync = vdIOIntFlushSyncLimited;
4076 VDIIOIntCallbacks.pfnReadUserAsync = NULL;
4077 VDIIOIntCallbacks.pfnWriteUserAsync = NULL;
4078 VDIIOIntCallbacks.pfnReadMetaAsync = NULL;
4079 VDIIOIntCallbacks.pfnWriteMetaAsync = NULL;
4080 VDIIOIntCallbacks.pfnFlushAsync = NULL;
4081 rc = VDInterfaceAdd(&VDIIOInt, "VD_IOINT", VDINTERFACETYPE_IOINT,
4082 &VDIIOIntCallbacks, pInterfaceIOCallbacks, &pVDIfsImage);
4083 AssertRC(rc);
4084
4085 /* Find the backend supporting this file format. */
4086 for (unsigned i = 0; i < g_cBackends; i++)
4087 {
4088 if (g_apBackends[i]->pfnCheckIfValid)
4089 {
4090 rc = g_apBackends[i]->pfnCheckIfValid(pszFilename, pVDIfsDisk,
4091 pVDIfsImage, penmType);
4092 if ( RT_SUCCESS(rc)
4093 /* The correct backend has been found, but there is a small
4094 * incompatibility so that the file cannot be used. Stop here
4095 * and signal success - the actual open will of course fail,
4096 * but that will create a really sensible error message. */
4097 || ( rc != VERR_VD_GEN_INVALID_HEADER
4098 && rc != VERR_VD_VDI_INVALID_HEADER
4099 && rc != VERR_VD_VMDK_INVALID_HEADER
4100 && rc != VERR_VD_ISCSI_INVALID_HEADER
4101 && rc != VERR_VD_VHD_INVALID_HEADER
4102 && rc != VERR_VD_RAW_INVALID_HEADER
4103 && rc != VERR_VD_PARALLELS_INVALID_HEADER
4104 && rc != VERR_VD_DMG_INVALID_HEADER))
4105 {
4106 /* Copy the name into the new string. */
4107 char *pszFormat = RTStrDup(g_apBackends[i]->pszBackendName);
4108 if (!pszFormat)
4109 {
4110 rc = VERR_NO_MEMORY;
4111 break;
4112 }
4113 *ppszFormat = pszFormat;
4114 rc = VINF_SUCCESS;
4115 break;
4116 }
4117 rc = VERR_NOT_SUPPORTED;
4118 }
4119 }
4120
4121 /* Try the cache backends. */
4122 if (rc == VERR_NOT_SUPPORTED)
4123 {
4124 for (unsigned i = 0; i < g_cCacheBackends; i++)
4125 {
4126 if (g_apCacheBackends[i]->pfnProbe)
4127 {
4128 rc = g_apCacheBackends[i]->pfnProbe(pszFilename, pVDIfsDisk,
4129 pVDIfsImage);
4130 if ( RT_SUCCESS(rc)
4131 || (rc != VERR_VD_GEN_INVALID_HEADER))
4132 {
4133 /* Copy the name into the new string. */
4134 char *pszFormat = RTStrDup(g_apBackends[i]->pszBackendName);
4135 if (!pszFormat)
4136 {
4137 rc = VERR_NO_MEMORY;
4138 break;
4139 }
4140 *ppszFormat = pszFormat;
4141 rc = VINF_SUCCESS;
4142 break;
4143 }
4144 rc = VERR_NOT_SUPPORTED;
4145 }
4146 }
4147 }
4148
4149 LogFlowFunc(("returns %Rrc *ppszFormat=\"%s\"\n", rc, *ppszFormat));
4150 return rc;
4151}
4152
4153/**
4154 * Opens an image file.
4155 *
4156 * The first opened image file in HDD container must have a base image type,
4157 * others (next opened images) must be a differencing or undo images.
4158 * Linkage is checked for differencing image to be in consistence with the previously opened image.
4159 * When another differencing image is opened and the last image was opened in read/write access
4160 * mode, then the last image is reopened in read-only with deny write sharing mode. This allows
4161 * other processes to use images in read-only mode too.
4162 *
4163 * Note that the image is opened in read-only mode if a read/write open is not possible.
4164 * Use VDIsReadOnly to check open mode.
4165 *
4166 * @returns VBox status code.
4167 * @param pDisk Pointer to HDD container.
4168 * @param pszBackend Name of the image file backend to use.
4169 * @param pszFilename Name of the image file to open.
4170 * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
4171 * @param pVDIfsImage Pointer to the per-image VD interface list.
4172 */
4173VBOXDDU_DECL(int) VDOpen(PVBOXHDD pDisk, const char *pszBackend,
4174 const char *pszFilename, unsigned uOpenFlags,
4175 PVDINTERFACE pVDIfsImage)
4176{
4177 int rc = VINF_SUCCESS;
4178 int rc2;
4179 bool fLockWrite = false;
4180 PVDIMAGE pImage = NULL;
4181
4182 LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uOpenFlags=%#x, pVDIfsImage=%#p\n",
4183 pDisk, pszBackend, pszFilename, uOpenFlags, pVDIfsImage));
4184
4185 do
4186 {
4187 /* sanity check */
4188 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
4189 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
4190
4191 /* Check arguments. */
4192 AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
4193 ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
4194 rc = VERR_INVALID_PARAMETER);
4195 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
4196 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
4197 rc = VERR_INVALID_PARAMETER);
4198 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
4199 ("uOpenFlags=%#x\n", uOpenFlags),
4200 rc = VERR_INVALID_PARAMETER);
4201
4202 /* Set up image descriptor. */
4203 pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
4204 if (!pImage)
4205 {
4206 rc = VERR_NO_MEMORY;
4207 break;
4208 }
4209 pImage->pszFilename = RTStrDup(pszFilename);
4210 if (!pImage->pszFilename)
4211 {
4212 rc = VERR_NO_MEMORY;
4213 break;
4214 }
4215
4216 pImage->VDIo.pDisk = pDisk;
4217 pImage->pVDIfsImage = pVDIfsImage;
4218
4219 rc = vdFindBackend(pszBackend, &pImage->Backend);
4220 if (RT_FAILURE(rc))
4221 break;
4222 if (!pImage->Backend)
4223 {
4224 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
4225 N_("VD: unknown backend name '%s'"), pszBackend);
4226 break;
4227 }
4228
4229 /*
4230 * Fail if the the backend can't do async I/O but the
4231 * flag is set.
4232 */
4233 if ( !(pImage->Backend->uBackendCaps & VD_CAP_ASYNC)
4234 && (uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO))
4235 {
4236 rc = vdError(pDisk, VERR_NOT_SUPPORTED, RT_SRC_POS,
4237 N_("VD: Backend '%s' does not support async I/O"), pszBackend);
4238 break;
4239 }
4240
4241 /* Set up the I/O interface. */
4242 pImage->VDIo.pInterfaceIO = VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IO);
4243 if (pImage->VDIo.pInterfaceIO)
4244 pImage->VDIo.pInterfaceIOCallbacks = VDGetInterfaceIO(pImage->VDIo.pInterfaceIO);
4245 else
4246 {
4247 rc = VDInterfaceAdd(&pImage->VDIo.VDIIO, "VD_IO", VDINTERFACETYPE_IO,
4248 &pDisk->VDIIOCallbacks, pDisk, &pVDIfsImage);
4249 pImage->VDIo.pInterfaceIO = &pImage->VDIo.VDIIO;
4250 pImage->VDIo.pInterfaceIOCallbacks = &pDisk->VDIIOCallbacks;
4251 }
4252
4253 /* Set up the internal I/O interface. */
4254 AssertBreakStmt(!VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IOINT),
4255 rc = VERR_INVALID_PARAMETER);
4256 rc = VDInterfaceAdd(&pImage->VDIo.VDIIOInt, "VD_IOINT", VDINTERFACETYPE_IOINT,
4257 &pDisk->VDIIOIntCallbacks, &pImage->VDIo, &pImage->pVDIfsImage);
4258 AssertRC(rc);
4259
4260 pImage->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
4261 rc = pImage->Backend->pfnOpen(pImage->pszFilename,
4262 uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
4263 pDisk->pVDIfsDisk,
4264 pImage->pVDIfsImage,
4265 pDisk->enmType,
4266 &pImage->pBackendData);
4267 /* If the open in read-write mode failed, retry in read-only mode. */
4268 if (RT_FAILURE(rc))
4269 {
4270 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY)
4271 && ( rc == VERR_ACCESS_DENIED
4272 || rc == VERR_PERMISSION_DENIED
4273 || rc == VERR_WRITE_PROTECT
4274 || rc == VERR_SHARING_VIOLATION
4275 || rc == VERR_FILE_LOCK_FAILED))
4276 rc = pImage->Backend->pfnOpen(pImage->pszFilename,
4277 (uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME)
4278 | VD_OPEN_FLAGS_READONLY,
4279 pDisk->pVDIfsDisk,
4280 pImage->pVDIfsImage,
4281 pDisk->enmType,
4282 &pImage->pBackendData);
4283 if (RT_FAILURE(rc))
4284 {
4285 rc = vdError(pDisk, rc, RT_SRC_POS,
4286 N_("VD: error %Rrc opening image file '%s'"), rc, pszFilename);
4287 break;
4288 }
4289 }
4290
4291 /* Lock disk for writing, as we modify pDisk information below. */
4292 rc2 = vdThreadStartWrite(pDisk);
4293 AssertRC(rc2);
4294 fLockWrite = true;
4295
4296 pImage->VDIo.pBackendData = pImage->pBackendData;
4297
4298 /* Check image type. As the image itself has only partial knowledge
4299 * whether it's a base image or not, this info is derived here. The
4300 * base image can be fixed or normal, all others must be normal or
4301 * diff images. Some image formats don't distinguish between normal
4302 * and diff images, so this must be corrected here. */
4303 unsigned uImageFlags;
4304 uImageFlags = pImage->Backend->pfnGetImageFlags(pImage->pBackendData);
4305 if (RT_FAILURE(rc))
4306 uImageFlags = VD_IMAGE_FLAGS_NONE;
4307 if ( RT_SUCCESS(rc)
4308 && !(uOpenFlags & VD_OPEN_FLAGS_INFO))
4309 {
4310 if ( pDisk->cImages == 0
4311 && (uImageFlags & VD_IMAGE_FLAGS_DIFF))
4312 {
4313 rc = VERR_VD_INVALID_TYPE;
4314 break;
4315 }
4316 else if (pDisk->cImages != 0)
4317 {
4318 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
4319 {
4320 rc = VERR_VD_INVALID_TYPE;
4321 break;
4322 }
4323 else
4324 uImageFlags |= VD_IMAGE_FLAGS_DIFF;
4325 }
4326 }
4327
4328 /* Ensure we always get correct diff information, even if the backend
4329 * doesn't actually have a stored flag for this. It must not return
4330 * bogus information for the parent UUID if it is not a diff image. */
4331 RTUUID parentUuid;
4332 RTUuidClear(&parentUuid);
4333 rc2 = pImage->Backend->pfnGetParentUuid(pImage->pBackendData, &parentUuid);
4334 if (RT_SUCCESS(rc2) && !RTUuidIsNull(&parentUuid))
4335 uImageFlags |= VD_IMAGE_FLAGS_DIFF;
4336
4337 pImage->uImageFlags = uImageFlags;
4338
4339 /* Force sane optimization settings. It's not worth avoiding writes
4340 * to fixed size images. The overhead would have almost no payback. */
4341 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
4342 pImage->uOpenFlags |= VD_OPEN_FLAGS_HONOR_SAME;
4343
4344 /** @todo optionally check UUIDs */
4345
4346 /* Cache disk information. */
4347 pDisk->cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
4348
4349 /* Cache PCHS geometry. */
4350 rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
4351 &pDisk->PCHSGeometry);
4352 if (RT_FAILURE(rc2))
4353 {
4354 pDisk->PCHSGeometry.cCylinders = 0;
4355 pDisk->PCHSGeometry.cHeads = 0;
4356 pDisk->PCHSGeometry.cSectors = 0;
4357 }
4358 else
4359 {
4360 /* Make sure the PCHS geometry is properly clipped. */
4361 pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
4362 pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
4363 pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
4364 }
4365
4366 /* Cache LCHS geometry. */
4367 rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
4368 &pDisk->LCHSGeometry);
4369 if (RT_FAILURE(rc2))
4370 {
4371 pDisk->LCHSGeometry.cCylinders = 0;
4372 pDisk->LCHSGeometry.cHeads = 0;
4373 pDisk->LCHSGeometry.cSectors = 0;
4374 }
4375 else
4376 {
4377 /* Make sure the LCHS geometry is properly clipped. */
4378 pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
4379 pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
4380 }
4381
4382 if (pDisk->cImages != 0)
4383 {
4384 /* Switch previous image to read-only mode. */
4385 unsigned uOpenFlagsPrevImg;
4386 uOpenFlagsPrevImg = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pBackendData);
4387 if (!(uOpenFlagsPrevImg & VD_OPEN_FLAGS_READONLY))
4388 {
4389 uOpenFlagsPrevImg |= VD_OPEN_FLAGS_READONLY;
4390 rc = pDisk->pLast->Backend->pfnSetOpenFlags(pDisk->pLast->pBackendData, uOpenFlagsPrevImg);
4391 }
4392 }
4393
4394 if (RT_SUCCESS(rc))
4395 {
4396 /* Image successfully opened, make it the last image. */
4397 vdAddImageToList(pDisk, pImage);
4398 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
4399 pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
4400 }
4401 else
4402 {
4403 /* Error detected, but image opened. Close image. */
4404 rc2 = pImage->Backend->pfnClose(pImage->pBackendData, false);
4405 AssertRC(rc2);
4406 pImage->pBackendData = NULL;
4407 }
4408 } while (0);
4409
4410 if (RT_UNLIKELY(fLockWrite))
4411 {
4412 rc2 = vdThreadFinishWrite(pDisk);
4413 AssertRC(rc2);
4414 }
4415
4416 if (RT_FAILURE(rc))
4417 {
4418 if (pImage)
4419 {
4420 if (pImage->pszFilename)
4421 RTStrFree(pImage->pszFilename);
4422 RTMemFree(pImage);
4423 }
4424 }
4425
4426 LogFlowFunc(("returns %Rrc\n", rc));
4427 return rc;
4428}
4429
4430/**
4431 * Opens a cache image.
4432 *
4433 * @return VBox status code.
4434 * @param pDisk Pointer to the HDD container which should use the cache image.
4435 * @param pszBackend Name of the cache file backend to use (case insensitive).
4436 * @param pszFilename Name of the cache image to open.
4437 * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
4438 * @param pVDIfsCache Pointer to the per-cache VD interface list.
4439 */
4440VBOXDDU_DECL(int) VDCacheOpen(PVBOXHDD pDisk, const char *pszBackend,
4441 const char *pszFilename, unsigned uOpenFlags,
4442 PVDINTERFACE pVDIfsCache)
4443{
4444 int rc = VINF_SUCCESS;
4445 int rc2;
4446 bool fLockWrite = false;
4447 PVDCACHE pCache = NULL;
4448
4449 LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uOpenFlags=%#x, pVDIfsCache=%#p\n",
4450 pDisk, pszBackend, pszFilename, uOpenFlags, pVDIfsCache));
4451
4452 do
4453 {
4454 /* sanity check */
4455 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
4456 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
4457
4458 /* Check arguments. */
4459 AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
4460 ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
4461 rc = VERR_INVALID_PARAMETER);
4462 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
4463 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
4464 rc = VERR_INVALID_PARAMETER);
4465 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
4466 ("uOpenFlags=%#x\n", uOpenFlags),
4467 rc = VERR_INVALID_PARAMETER);
4468
4469 /* Set up image descriptor. */
4470 pCache = (PVDCACHE)RTMemAllocZ(sizeof(VDCACHE));
4471 if (!pCache)
4472 {
4473 rc = VERR_NO_MEMORY;
4474 break;
4475 }
4476 pCache->pszFilename = RTStrDup(pszFilename);
4477 if (!pCache->pszFilename)
4478 {
4479 rc = VERR_NO_MEMORY;
4480 break;
4481 }
4482
4483 pCache->VDIo.pDisk = pDisk;
4484 pCache->pVDIfsCache = pVDIfsCache;
4485
4486 rc = vdFindCacheBackend(pszBackend, &pCache->Backend);
4487 if (RT_FAILURE(rc))
4488 break;
4489 if (!pCache->Backend)
4490 {
4491 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
4492 N_("VD: unknown backend name '%s'"), pszBackend);
4493 break;
4494 }
4495
4496 /* Set up the I/O interface. */
4497 pCache->VDIo.pInterfaceIO = VDInterfaceGet(pVDIfsCache, VDINTERFACETYPE_IO);
4498 if (pCache->VDIo.pInterfaceIO)
4499 pCache->VDIo.pInterfaceIOCallbacks = VDGetInterfaceIO(pCache->VDIo.pInterfaceIO);
4500 else
4501 {
4502 rc = VDInterfaceAdd(&pCache->VDIo.VDIIO, "VD_IO", VDINTERFACETYPE_IO,
4503 &pDisk->VDIIOCallbacks, pDisk, &pVDIfsCache);
4504 pCache->VDIo.pInterfaceIO = &pCache->VDIo.VDIIO;
4505 pCache->VDIo.pInterfaceIOCallbacks = &pDisk->VDIIOCallbacks;
4506 }
4507
4508 /* Set up the internal I/O interface. */
4509 AssertBreakStmt(!VDInterfaceGet(pVDIfsCache, VDINTERFACETYPE_IOINT),
4510 rc = VERR_INVALID_PARAMETER);
4511 rc = VDInterfaceAdd(&pCache->VDIo.VDIIOInt, "VD_IOINT", VDINTERFACETYPE_IOINT,
4512 &pDisk->VDIIOIntCallbacks, &pCache->VDIo, &pCache->pVDIfsCache);
4513 AssertRC(rc);
4514
4515 pCache->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
4516 rc = pCache->Backend->pfnOpen(pCache->pszFilename,
4517 uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
4518 pDisk->pVDIfsDisk,
4519 pCache->pVDIfsCache,
4520 &pCache->pBackendData);
4521 /* If the open in read-write mode failed, retry in read-only mode. */
4522 if (RT_FAILURE(rc))
4523 {
4524 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY)
4525 && ( rc == VERR_ACCESS_DENIED
4526 || rc == VERR_PERMISSION_DENIED
4527 || rc == VERR_WRITE_PROTECT
4528 || rc == VERR_SHARING_VIOLATION
4529 || rc == VERR_FILE_LOCK_FAILED))
4530 rc = pCache->Backend->pfnOpen(pCache->pszFilename,
4531 (uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME)
4532 | VD_OPEN_FLAGS_READONLY,
4533 pDisk->pVDIfsDisk,
4534 pCache->pVDIfsCache,
4535 &pCache->pBackendData);
4536 if (RT_FAILURE(rc))
4537 {
4538 rc = vdError(pDisk, rc, RT_SRC_POS,
4539 N_("VD: error %Rrc opening image file '%s'"), rc, pszFilename);
4540 break;
4541 }
4542 }
4543
4544 /* Lock disk for writing, as we modify pDisk information below. */
4545 rc2 = vdThreadStartWrite(pDisk);
4546 AssertRC(rc2);
4547 fLockWrite = true;
4548
4549 /*
4550 * Check that the modification UUID of the cache and last image
4551 * match. If not the image was modified in-between without the cache.
4552 * The cache might contain stale data.
4553 */
4554 RTUUID UuidImage, UuidCache;
4555
4556 rc = pCache->Backend->pfnGetModificationUuid(pCache->pBackendData,
4557 &UuidCache);
4558 if (RT_SUCCESS(rc))
4559 {
4560 rc = pDisk->pLast->Backend->pfnGetModificationUuid(pDisk->pLast->pBackendData,
4561 &UuidImage);
4562 if (RT_SUCCESS(rc))
4563 {
4564 if (RTUuidCompare(&UuidImage, &UuidCache))
4565 rc = VERR_VD_CACHE_NOT_UP_TO_DATE;
4566 }
4567 }
4568
4569 /*
4570 * We assume that the user knows what he is doing if one of the images
4571 * doesn't support the modification uuid.
4572 */
4573 if (rc == VERR_NOT_SUPPORTED)
4574 rc = VINF_SUCCESS;
4575
4576 if (RT_SUCCESS(rc))
4577 {
4578 /* Cache successfully opened, make it the current one. */
4579 if (!pDisk->pCache)
4580 pDisk->pCache = pCache;
4581 else
4582 rc = VERR_VD_CACHE_ALREADY_EXISTS;
4583 }
4584
4585 if (RT_FAILURE(rc))
4586 {
4587 /* Error detected, but image opened. Close image. */
4588 rc2 = pCache->Backend->pfnClose(pCache->pBackendData, false);
4589 AssertRC(rc2);
4590 pCache->pBackendData = NULL;
4591 }
4592 } while (0);
4593
4594 if (RT_UNLIKELY(fLockWrite))
4595 {
4596 rc2 = vdThreadFinishWrite(pDisk);
4597 AssertRC(rc2);
4598 }
4599
4600 if (RT_FAILURE(rc))
4601 {
4602 if (pCache)
4603 {
4604 if (pCache->pszFilename)
4605 RTStrFree(pCache->pszFilename);
4606 RTMemFree(pCache);
4607 }
4608 }
4609
4610 LogFlowFunc(("returns %Rrc\n", rc));
4611 return rc;
4612}
4613
4614/**
4615 * Creates and opens a new base image file.
4616 *
4617 * @returns VBox status code.
4618 * @param pDisk Pointer to HDD container.
4619 * @param pszBackend Name of the image file backend to use.
4620 * @param pszFilename Name of the image file to create.
4621 * @param cbSize Image size in bytes.
4622 * @param uImageFlags Flags specifying special image features.
4623 * @param pszComment Pointer to image comment. NULL is ok.
4624 * @param pPCHSGeometry Pointer to physical disk geometry <= (16383,16,63). Not NULL.
4625 * @param pLCHSGeometry Pointer to logical disk geometry <= (x,255,63). Not NULL.
4626 * @param pUuid New UUID of the image. If NULL, a new UUID is created.
4627 * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
4628 * @param pVDIfsImage Pointer to the per-image VD interface list.
4629 * @param pVDIfsOperation Pointer to the per-operation VD interface list.
4630 */
4631VBOXDDU_DECL(int) VDCreateBase(PVBOXHDD pDisk, const char *pszBackend,
4632 const char *pszFilename, uint64_t cbSize,
4633 unsigned uImageFlags, const char *pszComment,
4634 PCVDGEOMETRY pPCHSGeometry,
4635 PCVDGEOMETRY pLCHSGeometry,
4636 PCRTUUID pUuid, unsigned uOpenFlags,
4637 PVDINTERFACE pVDIfsImage,
4638 PVDINTERFACE pVDIfsOperation)
4639{
4640 int rc = VINF_SUCCESS;
4641 int rc2;
4642 bool fLockWrite = false, fLockRead = false;
4643 PVDIMAGE pImage = NULL;
4644 RTUUID uuid;
4645
4646 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",
4647 pDisk, pszBackend, pszFilename, cbSize, uImageFlags, pszComment,
4648 pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
4649 pPCHSGeometry->cSectors, pLCHSGeometry->cCylinders,
4650 pLCHSGeometry->cHeads, pLCHSGeometry->cSectors, pUuid,
4651 uOpenFlags, pVDIfsImage, pVDIfsOperation));
4652
4653 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
4654 VDINTERFACETYPE_PROGRESS);
4655 PVDINTERFACEPROGRESS pCbProgress = NULL;
4656 if (pIfProgress)
4657 pCbProgress = VDGetInterfaceProgress(pIfProgress);
4658
4659 do
4660 {
4661 /* sanity check */
4662 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
4663 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
4664
4665 /* Check arguments. */
4666 AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
4667 ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
4668 rc = VERR_INVALID_PARAMETER);
4669 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
4670 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
4671 rc = VERR_INVALID_PARAMETER);
4672 AssertMsgBreakStmt(cbSize,
4673 ("cbSize=%llu\n", cbSize),
4674 rc = VERR_INVALID_PARAMETER);
4675 AssertMsgBreakStmt( ((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0)
4676 || ((uImageFlags & (VD_IMAGE_FLAGS_FIXED | VD_IMAGE_FLAGS_DIFF)) != VD_IMAGE_FLAGS_FIXED),
4677 ("uImageFlags=%#x\n", uImageFlags),
4678 rc = VERR_INVALID_PARAMETER);
4679 /* The PCHS geometry fields may be 0 to leave it for later. */
4680 AssertMsgBreakStmt( VALID_PTR(pPCHSGeometry)
4681 && pPCHSGeometry->cHeads <= 16
4682 && pPCHSGeometry->cSectors <= 63,
4683 ("pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pPCHSGeometry,
4684 pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
4685 pPCHSGeometry->cSectors),
4686 rc = VERR_INVALID_PARAMETER);
4687 /* The LCHS geometry fields may be 0 to leave it to later autodetection. */
4688 AssertMsgBreakStmt( VALID_PTR(pLCHSGeometry)
4689 && pLCHSGeometry->cHeads <= 255
4690 && pLCHSGeometry->cSectors <= 63,
4691 ("pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pLCHSGeometry,
4692 pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads,
4693 pLCHSGeometry->cSectors),
4694 rc = VERR_INVALID_PARAMETER);
4695 /* The UUID may be NULL. */
4696 AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
4697 ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
4698 rc = VERR_INVALID_PARAMETER);
4699 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
4700 ("uOpenFlags=%#x\n", uOpenFlags),
4701 rc = VERR_INVALID_PARAMETER);
4702
4703 /* Check state. Needs a temporary read lock. Holding the write lock
4704 * all the time would be blocking other activities for too long. */
4705 rc2 = vdThreadStartRead(pDisk);
4706 AssertRC(rc2);
4707 fLockRead = true;
4708 AssertMsgBreakStmt(pDisk->cImages == 0,
4709 ("Create base image cannot be done with other images open\n"),
4710 rc = VERR_VD_INVALID_STATE);
4711 rc2 = vdThreadFinishRead(pDisk);
4712 AssertRC(rc2);
4713 fLockRead = false;
4714
4715 /* Set up image descriptor. */
4716 pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
4717 if (!pImage)
4718 {
4719 rc = VERR_NO_MEMORY;
4720 break;
4721 }
4722 pImage->pszFilename = RTStrDup(pszFilename);
4723 if (!pImage->pszFilename)
4724 {
4725 rc = VERR_NO_MEMORY;
4726 break;
4727 }
4728 pImage->VDIo.pDisk = pDisk;
4729 pImage->pVDIfsImage = pVDIfsImage;
4730
4731 /* Set up the I/O interface. */
4732 pImage->VDIo.pInterfaceIO = VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IO);
4733 if (pImage->VDIo.pInterfaceIO)
4734 pImage->VDIo.pInterfaceIOCallbacks = VDGetInterfaceIO(pImage->VDIo.pInterfaceIO);
4735 else
4736 {
4737 rc = VDInterfaceAdd(&pImage->VDIo.VDIIO, "VD_IO", VDINTERFACETYPE_IO,
4738 &pDisk->VDIIOCallbacks, pDisk, &pVDIfsImage);
4739 pImage->VDIo.pInterfaceIO = &pImage->VDIo.VDIIO;
4740 pImage->VDIo.pInterfaceIOCallbacks = &pDisk->VDIIOCallbacks;
4741 }
4742
4743 /* Set up the internal I/O interface. */
4744 AssertBreakStmt(!VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IOINT),
4745 rc = VERR_INVALID_PARAMETER);
4746 rc = VDInterfaceAdd(&pImage->VDIo.VDIIOInt, "VD_IOINT", VDINTERFACETYPE_IOINT,
4747 &pDisk->VDIIOIntCallbacks, &pImage->VDIo, &pImage->pVDIfsImage);
4748 AssertRC(rc);
4749
4750 rc = vdFindBackend(pszBackend, &pImage->Backend);
4751 if (RT_FAILURE(rc))
4752 break;
4753 if (!pImage->Backend)
4754 {
4755 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
4756 N_("VD: unknown backend name '%s'"), pszBackend);
4757 break;
4758 }
4759 if (!(pImage->Backend->uBackendCaps & ( VD_CAP_CREATE_FIXED
4760 | VD_CAP_CREATE_DYNAMIC)))
4761 {
4762 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
4763 N_("VD: backend '%s' cannot create base images"), pszBackend);
4764 break;
4765 }
4766
4767 /* Create UUID if the caller didn't specify one. */
4768 if (!pUuid)
4769 {
4770 rc = RTUuidCreate(&uuid);
4771 if (RT_FAILURE(rc))
4772 {
4773 rc = vdError(pDisk, rc, RT_SRC_POS,
4774 N_("VD: cannot generate UUID for image '%s'"),
4775 pszFilename);
4776 break;
4777 }
4778 pUuid = &uuid;
4779 }
4780
4781 pImage->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
4782 uImageFlags &= ~VD_IMAGE_FLAGS_DIFF;
4783 rc = pImage->Backend->pfnCreate(pImage->pszFilename, cbSize,
4784 uImageFlags, pszComment, pPCHSGeometry,
4785 pLCHSGeometry, pUuid,
4786 uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
4787 0, 99,
4788 pDisk->pVDIfsDisk,
4789 pImage->pVDIfsImage,
4790 pVDIfsOperation,
4791 &pImage->pBackendData);
4792
4793 if (RT_SUCCESS(rc))
4794 {
4795 pImage->VDIo.pBackendData = pImage->pBackendData;
4796 pImage->uImageFlags = uImageFlags;
4797
4798 /* Force sane optimization settings. It's not worth avoiding writes
4799 * to fixed size images. The overhead would have almost no payback. */
4800 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
4801 pImage->uOpenFlags |= VD_OPEN_FLAGS_HONOR_SAME;
4802
4803 /* Lock disk for writing, as we modify pDisk information below. */
4804 rc2 = vdThreadStartWrite(pDisk);
4805 AssertRC(rc2);
4806 fLockWrite = true;
4807
4808 /** @todo optionally check UUIDs */
4809
4810 /* Re-check state, as the lock wasn't held and another image
4811 * creation call could have been done by another thread. */
4812 AssertMsgStmt(pDisk->cImages == 0,
4813 ("Create base image cannot be done with other images open\n"),
4814 rc = VERR_VD_INVALID_STATE);
4815 }
4816
4817 if (RT_SUCCESS(rc))
4818 {
4819 /* Cache disk information. */
4820 pDisk->cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
4821
4822 /* Cache PCHS geometry. */
4823 rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
4824 &pDisk->PCHSGeometry);
4825 if (RT_FAILURE(rc2))
4826 {
4827 pDisk->PCHSGeometry.cCylinders = 0;
4828 pDisk->PCHSGeometry.cHeads = 0;
4829 pDisk->PCHSGeometry.cSectors = 0;
4830 }
4831 else
4832 {
4833 /* Make sure the CHS geometry is properly clipped. */
4834 pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
4835 pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
4836 pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
4837 }
4838
4839 /* Cache LCHS geometry. */
4840 rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
4841 &pDisk->LCHSGeometry);
4842 if (RT_FAILURE(rc2))
4843 {
4844 pDisk->LCHSGeometry.cCylinders = 0;
4845 pDisk->LCHSGeometry.cHeads = 0;
4846 pDisk->LCHSGeometry.cSectors = 0;
4847 }
4848 else
4849 {
4850 /* Make sure the CHS geometry is properly clipped. */
4851 pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
4852 pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
4853 }
4854
4855 /* Image successfully opened, make it the last image. */
4856 vdAddImageToList(pDisk, pImage);
4857 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
4858 pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
4859 }
4860 else
4861 {
4862 /* Error detected, image may or may not be opened. Close and delete
4863 * image if it was opened. */
4864 if (pImage->pBackendData)
4865 {
4866 rc2 = pImage->Backend->pfnClose(pImage->pBackendData, true);
4867 AssertRC(rc2);
4868 pImage->pBackendData = NULL;
4869 }
4870 }
4871 } while (0);
4872
4873 if (RT_UNLIKELY(fLockWrite))
4874 {
4875 rc2 = vdThreadFinishWrite(pDisk);
4876 AssertRC(rc2);
4877 }
4878 else if (RT_UNLIKELY(fLockRead))
4879 {
4880 rc2 = vdThreadFinishRead(pDisk);
4881 AssertRC(rc2);
4882 }
4883
4884 if (RT_FAILURE(rc))
4885 {
4886 if (pImage)
4887 {
4888 if (pImage->pszFilename)
4889 RTStrFree(pImage->pszFilename);
4890 RTMemFree(pImage);
4891 }
4892 }
4893
4894 if (RT_SUCCESS(rc) && pCbProgress && pCbProgress->pfnProgress)
4895 pCbProgress->pfnProgress(pIfProgress->pvUser, 100);
4896
4897 LogFlowFunc(("returns %Rrc\n", rc));
4898 return rc;
4899}
4900
4901/**
4902 * Creates and opens a new differencing image file in HDD container.
4903 * See comments for VDOpen function about differencing images.
4904 *
4905 * @returns VBox status code.
4906 * @param pDisk Pointer to HDD container.
4907 * @param pszBackend Name of the image file backend to use.
4908 * @param pszFilename Name of the differencing image file to create.
4909 * @param uImageFlags Flags specifying special image features.
4910 * @param pszComment Pointer to image comment. NULL is ok.
4911 * @param pUuid New UUID of the image. If NULL, a new UUID is created.
4912 * @param pParentUuid New parent UUID of the image. If NULL, the UUID is queried automatically.
4913 * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
4914 * @param pVDIfsImage Pointer to the per-image VD interface list.
4915 * @param pVDIfsOperation Pointer to the per-operation VD interface list.
4916 */
4917VBOXDDU_DECL(int) VDCreateDiff(PVBOXHDD pDisk, const char *pszBackend,
4918 const char *pszFilename, unsigned uImageFlags,
4919 const char *pszComment, PCRTUUID pUuid,
4920 PCRTUUID pParentUuid, unsigned uOpenFlags,
4921 PVDINTERFACE pVDIfsImage,
4922 PVDINTERFACE pVDIfsOperation)
4923{
4924 int rc = VINF_SUCCESS;
4925 int rc2;
4926 bool fLockWrite = false, fLockRead = false;
4927 PVDIMAGE pImage = NULL;
4928 RTUUID uuid;
4929
4930 LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uImageFlags=%#x pszComment=\"%s\" Uuid=%RTuuid uOpenFlags=%#x pVDIfsImage=%#p pVDIfsOperation=%#p\n",
4931 pDisk, pszBackend, pszFilename, uImageFlags, pszComment, pUuid, uOpenFlags, pVDIfsImage, pVDIfsOperation));
4932
4933 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
4934 VDINTERFACETYPE_PROGRESS);
4935 PVDINTERFACEPROGRESS pCbProgress = NULL;
4936 if (pIfProgress)
4937 pCbProgress = VDGetInterfaceProgress(pIfProgress);
4938
4939 do
4940 {
4941 /* sanity check */
4942 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
4943 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
4944
4945 /* Check arguments. */
4946 AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
4947 ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
4948 rc = VERR_INVALID_PARAMETER);
4949 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
4950 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
4951 rc = VERR_INVALID_PARAMETER);
4952 AssertMsgBreakStmt((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0,
4953 ("uImageFlags=%#x\n", uImageFlags),
4954 rc = VERR_INVALID_PARAMETER);
4955 /* The UUID may be NULL. */
4956 AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
4957 ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
4958 rc = VERR_INVALID_PARAMETER);
4959 /* The parent UUID may be NULL. */
4960 AssertMsgBreakStmt(pParentUuid == NULL || VALID_PTR(pParentUuid),
4961 ("pParentUuid=%#p ParentUUID=%RTuuid\n", pParentUuid, pParentUuid),
4962 rc = VERR_INVALID_PARAMETER);
4963 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
4964 ("uOpenFlags=%#x\n", uOpenFlags),
4965 rc = VERR_INVALID_PARAMETER);
4966
4967 /* Check state. Needs a temporary read lock. Holding the write lock
4968 * all the time would be blocking other activities for too long. */
4969 rc2 = vdThreadStartRead(pDisk);
4970 AssertRC(rc2);
4971 fLockRead = true;
4972 AssertMsgBreakStmt(pDisk->cImages != 0,
4973 ("Create diff image cannot be done without other images open\n"),
4974 rc = VERR_VD_INVALID_STATE);
4975 rc2 = vdThreadFinishRead(pDisk);
4976 AssertRC(rc2);
4977 fLockRead = false;
4978
4979 /* Set up image descriptor. */
4980 pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
4981 if (!pImage)
4982 {
4983 rc = VERR_NO_MEMORY;
4984 break;
4985 }
4986 pImage->pszFilename = RTStrDup(pszFilename);
4987 if (!pImage->pszFilename)
4988 {
4989 rc = VERR_NO_MEMORY;
4990 break;
4991 }
4992
4993 rc = vdFindBackend(pszBackend, &pImage->Backend);
4994 if (RT_FAILURE(rc))
4995 break;
4996 if (!pImage->Backend)
4997 {
4998 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
4999 N_("VD: unknown backend name '%s'"), pszBackend);
5000 break;
5001 }
5002 if ( !(pImage->Backend->uBackendCaps & VD_CAP_DIFF)
5003 || !(pImage->Backend->uBackendCaps & ( VD_CAP_CREATE_FIXED
5004 | VD_CAP_CREATE_DYNAMIC)))
5005 {
5006 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
5007 N_("VD: backend '%s' cannot create diff images"), pszBackend);
5008 break;
5009 }
5010
5011 pImage->VDIo.pDisk = pDisk;
5012 pImage->pVDIfsImage = pVDIfsImage;
5013
5014 /* Set up the I/O interface. */
5015 pImage->VDIo.pInterfaceIO = VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IO);
5016 if (pImage->VDIo.pInterfaceIO)
5017 pImage->VDIo.pInterfaceIOCallbacks = VDGetInterfaceIO(pImage->VDIo.pInterfaceIO);
5018 else
5019 {
5020 rc = VDInterfaceAdd(&pImage->VDIo.VDIIO, "VD_IO", VDINTERFACETYPE_IO,
5021 &pDisk->VDIIOCallbacks, pDisk, &pVDIfsImage);
5022 pImage->VDIo.pInterfaceIO = &pImage->VDIo.VDIIO;
5023 pImage->VDIo.pInterfaceIOCallbacks = &pDisk->VDIIOCallbacks;
5024 }
5025
5026 /* Set up the internal I/O interface. */
5027 AssertBreakStmt(!VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IOINT),
5028 rc = VERR_INVALID_PARAMETER);
5029 rc = VDInterfaceAdd(&pImage->VDIo.VDIIOInt, "VD_IOINT", VDINTERFACETYPE_IOINT,
5030 &pDisk->VDIIOIntCallbacks, &pImage->VDIo, &pImage->pVDIfsImage);
5031 AssertRC(rc);
5032
5033 /* Create UUID if the caller didn't specify one. */
5034 if (!pUuid)
5035 {
5036 rc = RTUuidCreate(&uuid);
5037 if (RT_FAILURE(rc))
5038 {
5039 rc = vdError(pDisk, rc, RT_SRC_POS,
5040 N_("VD: cannot generate UUID for image '%s'"),
5041 pszFilename);
5042 break;
5043 }
5044 pUuid = &uuid;
5045 }
5046
5047 pImage->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
5048 uImageFlags |= VD_IMAGE_FLAGS_DIFF;
5049 rc = pImage->Backend->pfnCreate(pImage->pszFilename, pDisk->cbSize,
5050 uImageFlags | VD_IMAGE_FLAGS_DIFF,
5051 pszComment, &pDisk->PCHSGeometry,
5052 &pDisk->LCHSGeometry, pUuid,
5053 uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
5054 0, 99,
5055 pDisk->pVDIfsDisk,
5056 pImage->pVDIfsImage,
5057 pVDIfsOperation,
5058 &pImage->pBackendData);
5059
5060 if (RT_SUCCESS(rc))
5061 {
5062 pImage->VDIo.pBackendData = pImage->pBackendData;
5063 pImage->uImageFlags = uImageFlags;
5064
5065 /* Lock disk for writing, as we modify pDisk information below. */
5066 rc2 = vdThreadStartWrite(pDisk);
5067 AssertRC(rc2);
5068 fLockWrite = true;
5069
5070 /* Switch previous image to read-only mode. */
5071 unsigned uOpenFlagsPrevImg;
5072 uOpenFlagsPrevImg = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pBackendData);
5073 if (!(uOpenFlagsPrevImg & VD_OPEN_FLAGS_READONLY))
5074 {
5075 uOpenFlagsPrevImg |= VD_OPEN_FLAGS_READONLY;
5076 rc = pDisk->pLast->Backend->pfnSetOpenFlags(pDisk->pLast->pBackendData, uOpenFlagsPrevImg);
5077 }
5078
5079 /** @todo optionally check UUIDs */
5080
5081 /* Re-check state, as the lock wasn't held and another image
5082 * creation call could have been done by another thread. */
5083 AssertMsgStmt(pDisk->cImages != 0,
5084 ("Create diff image cannot be done without other images open\n"),
5085 rc = VERR_VD_INVALID_STATE);
5086 }
5087
5088 if (RT_SUCCESS(rc))
5089 {
5090 RTUUID Uuid;
5091 RTTIMESPEC ts;
5092
5093 if (pParentUuid && !RTUuidIsNull(pParentUuid))
5094 {
5095 Uuid = *pParentUuid;
5096 pImage->Backend->pfnSetParentUuid(pImage->pBackendData, &Uuid);
5097 }
5098 else
5099 {
5100 rc2 = pDisk->pLast->Backend->pfnGetUuid(pDisk->pLast->pBackendData,
5101 &Uuid);
5102 if (RT_SUCCESS(rc2))
5103 pImage->Backend->pfnSetParentUuid(pImage->pBackendData, &Uuid);
5104 }
5105 rc2 = pDisk->pLast->Backend->pfnGetModificationUuid(pDisk->pLast->pBackendData,
5106 &Uuid);
5107 if (RT_SUCCESS(rc2))
5108 pImage->Backend->pfnSetParentModificationUuid(pImage->pBackendData,
5109 &Uuid);
5110 if (pDisk->pLast->Backend->pfnGetTimeStamp)
5111 rc2 = pDisk->pLast->Backend->pfnGetTimeStamp(pDisk->pLast->pBackendData,
5112 &ts);
5113 else
5114 rc2 = VERR_NOT_IMPLEMENTED;
5115 if (RT_SUCCESS(rc2) && pImage->Backend->pfnSetParentTimeStamp)
5116 pImage->Backend->pfnSetParentTimeStamp(pImage->pBackendData, &ts);
5117
5118 if (pImage->Backend->pfnSetParentFilename)
5119 rc2 = pImage->Backend->pfnSetParentFilename(pImage->pBackendData, pDisk->pLast->pszFilename);
5120 }
5121
5122 if (RT_SUCCESS(rc))
5123 {
5124 /* Image successfully opened, make it the last image. */
5125 vdAddImageToList(pDisk, pImage);
5126 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
5127 pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
5128 }
5129 else
5130 {
5131 /* Error detected, but image opened. Close and delete image. */
5132 rc2 = pImage->Backend->pfnClose(pImage->pBackendData, true);
5133 AssertRC(rc2);
5134 pImage->pBackendData = NULL;
5135 }
5136 } while (0);
5137
5138 if (RT_UNLIKELY(fLockWrite))
5139 {
5140 rc2 = vdThreadFinishWrite(pDisk);
5141 AssertRC(rc2);
5142 }
5143 else if (RT_UNLIKELY(fLockRead))
5144 {
5145 rc2 = vdThreadFinishRead(pDisk);
5146 AssertRC(rc2);
5147 }
5148
5149 if (RT_FAILURE(rc))
5150 {
5151 if (pImage)
5152 {
5153 if (pImage->pszFilename)
5154 RTStrFree(pImage->pszFilename);
5155 RTMemFree(pImage);
5156 }
5157 }
5158
5159 if (RT_SUCCESS(rc) && pCbProgress && pCbProgress->pfnProgress)
5160 pCbProgress->pfnProgress(pIfProgress->pvUser, 100);
5161
5162 LogFlowFunc(("returns %Rrc\n", rc));
5163 return rc;
5164}
5165
5166
5167/**
5168 * Creates and opens new cache image file in HDD container.
5169 *
5170 * @return VBox status code.
5171 * @param pDisk Name of the cache file backend to use (case insensitive).
5172 * @param pszFilename Name of the differencing cache file to create.
5173 * @param cbSize Maximum size of the cache.
5174 * @param uImageFlags Flags specifying special cache features.
5175 * @param pszComment Pointer to image comment. NULL is ok.
5176 * @param pUuid New UUID of the image. If NULL, a new UUID is created.
5177 * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
5178 * @param pVDIfsCache Pointer to the per-cache VD interface list.
5179 * @param pVDIfsOperation Pointer to the per-operation VD interface list.
5180 */
5181VBOXDDU_DECL(int) VDCreateCache(PVBOXHDD pDisk, const char *pszBackend,
5182 const char *pszFilename, uint64_t cbSize,
5183 unsigned uImageFlags, const char *pszComment,
5184 PCRTUUID pUuid, unsigned uOpenFlags,
5185 PVDINTERFACE pVDIfsCache, PVDINTERFACE pVDIfsOperation)
5186{
5187 int rc = VINF_SUCCESS;
5188 int rc2;
5189 bool fLockWrite = false, fLockRead = false;
5190 PVDCACHE pCache = NULL;
5191 RTUUID uuid;
5192
5193 LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" Uuid=%RTuuid uOpenFlags=%#x pVDIfsImage=%#p pVDIfsOperation=%#p\n",
5194 pDisk, pszBackend, pszFilename, cbSize, uImageFlags, pszComment, pUuid, uOpenFlags, pVDIfsCache, pVDIfsOperation));
5195
5196 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
5197 VDINTERFACETYPE_PROGRESS);
5198 PVDINTERFACEPROGRESS pCbProgress = NULL;
5199 if (pIfProgress)
5200 pCbProgress = VDGetInterfaceProgress(pIfProgress);
5201
5202 do
5203 {
5204 /* sanity check */
5205 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
5206 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
5207
5208 /* Check arguments. */
5209 AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
5210 ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
5211 rc = VERR_INVALID_PARAMETER);
5212 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
5213 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
5214 rc = VERR_INVALID_PARAMETER);
5215 AssertMsgBreakStmt(cbSize,
5216 ("cbSize=%llu\n", cbSize),
5217 rc = VERR_INVALID_PARAMETER);
5218 AssertMsgBreakStmt((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0,
5219 ("uImageFlags=%#x\n", uImageFlags),
5220 rc = VERR_INVALID_PARAMETER);
5221 /* The UUID may be NULL. */
5222 AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
5223 ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
5224 rc = VERR_INVALID_PARAMETER);
5225 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
5226 ("uOpenFlags=%#x\n", uOpenFlags),
5227 rc = VERR_INVALID_PARAMETER);
5228
5229 /* Check state. Needs a temporary read lock. Holding the write lock
5230 * all the time would be blocking other activities for too long. */
5231 rc2 = vdThreadStartRead(pDisk);
5232 AssertRC(rc2);
5233 fLockRead = true;
5234 AssertMsgBreakStmt(!pDisk->pCache,
5235 ("Create cache image cannot be done with a cache already attached\n"),
5236 rc = VERR_VD_CACHE_ALREADY_EXISTS);
5237 rc2 = vdThreadFinishRead(pDisk);
5238 AssertRC(rc2);
5239 fLockRead = false;
5240
5241 /* Set up image descriptor. */
5242 pCache = (PVDCACHE)RTMemAllocZ(sizeof(VDCACHE));
5243 if (!pCache)
5244 {
5245 rc = VERR_NO_MEMORY;
5246 break;
5247 }
5248 pCache->pszFilename = RTStrDup(pszFilename);
5249 if (!pCache->pszFilename)
5250 {
5251 rc = VERR_NO_MEMORY;
5252 break;
5253 }
5254
5255 rc = vdFindCacheBackend(pszBackend, &pCache->Backend);
5256 if (RT_FAILURE(rc))
5257 break;
5258 if (!pCache->Backend)
5259 {
5260 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
5261 N_("VD: unknown backend name '%s'"), pszBackend);
5262 break;
5263 }
5264
5265 pCache->VDIo.pDisk = pDisk;
5266 pCache->pVDIfsCache = pVDIfsCache;
5267
5268 /* Set up the I/O interface. */
5269 pCache->VDIo.pInterfaceIO = VDInterfaceGet(pVDIfsCache, VDINTERFACETYPE_IO);
5270 if (pCache->VDIo.pInterfaceIO)
5271 pCache->VDIo.pInterfaceIOCallbacks = VDGetInterfaceIO(pCache->VDIo.pInterfaceIO);
5272 else
5273 {
5274 rc = VDInterfaceAdd(&pCache->VDIo.VDIIO, "VD_IO", VDINTERFACETYPE_IO,
5275 &pDisk->VDIIOCallbacks, pDisk, &pVDIfsCache);
5276 pCache->VDIo.pInterfaceIO = &pCache->VDIo.VDIIO;
5277 pCache->VDIo.pInterfaceIOCallbacks = &pDisk->VDIIOCallbacks;
5278 }
5279
5280 /* Set up the internal I/O interface. */
5281 AssertBreakStmt(!VDInterfaceGet(pVDIfsCache, VDINTERFACETYPE_IOINT),
5282 rc = VERR_INVALID_PARAMETER);
5283 rc = VDInterfaceAdd(&pCache->VDIo.VDIIOInt, "VD_IOINT", VDINTERFACETYPE_IOINT,
5284 &pDisk->VDIIOIntCallbacks, &pCache->VDIo, &pCache->pVDIfsCache);
5285 AssertRC(rc);
5286
5287 /* Create UUID if the caller didn't specify one. */
5288 if (!pUuid)
5289 {
5290 rc = RTUuidCreate(&uuid);
5291 if (RT_FAILURE(rc))
5292 {
5293 rc = vdError(pDisk, rc, RT_SRC_POS,
5294 N_("VD: cannot generate UUID for image '%s'"),
5295 pszFilename);
5296 break;
5297 }
5298 pUuid = &uuid;
5299 }
5300
5301 pCache->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
5302 rc = pCache->Backend->pfnCreate(pCache->pszFilename, cbSize,
5303 uImageFlags,
5304 pszComment, pUuid,
5305 uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
5306 0, 99,
5307 pDisk->pVDIfsDisk,
5308 pCache->pVDIfsCache,
5309 pVDIfsOperation,
5310 &pCache->pBackendData);
5311
5312 if (RT_SUCCESS(rc))
5313 {
5314 /* Lock disk for writing, as we modify pDisk information below. */
5315 rc2 = vdThreadStartWrite(pDisk);
5316 AssertRC(rc2);
5317 fLockWrite = true;
5318
5319 pCache->VDIo.pBackendData = pCache->pBackendData;
5320
5321 /* Re-check state, as the lock wasn't held and another image
5322 * creation call could have been done by another thread. */
5323 AssertMsgStmt(!pDisk->pCache,
5324 ("Create cache image cannot be done with another cache open\n"),
5325 rc = VERR_VD_CACHE_ALREADY_EXISTS);
5326 }
5327
5328 if ( RT_SUCCESS(rc)
5329 && pDisk->pLast)
5330 {
5331 RTUUID UuidModification;
5332
5333 /* Set same modification Uuid as the last image. */
5334 rc = pDisk->pLast->Backend->pfnGetModificationUuid(pDisk->pLast->pBackendData,
5335 &UuidModification);
5336 if (RT_SUCCESS(rc))
5337 {
5338 rc = pCache->Backend->pfnSetModificationUuid(pCache->pBackendData,
5339 &UuidModification);
5340 }
5341
5342 if (rc == VERR_NOT_SUPPORTED)
5343 rc = VINF_SUCCESS;
5344 }
5345
5346 if (RT_SUCCESS(rc))
5347 {
5348 /* Cache successfully created. */
5349 pDisk->pCache = pCache;
5350 }
5351 else
5352 {
5353 /* Error detected, but image opened. Close and delete image. */
5354 rc2 = pCache->Backend->pfnClose(pCache->pBackendData, true);
5355 AssertRC(rc2);
5356 pCache->pBackendData = NULL;
5357 }
5358 } while (0);
5359
5360 if (RT_UNLIKELY(fLockWrite))
5361 {
5362 rc2 = vdThreadFinishWrite(pDisk);
5363 AssertRC(rc2);
5364 }
5365 else if (RT_UNLIKELY(fLockRead))
5366 {
5367 rc2 = vdThreadFinishRead(pDisk);
5368 AssertRC(rc2);
5369 }
5370
5371 if (RT_FAILURE(rc))
5372 {
5373 if (pCache)
5374 {
5375 if (pCache->pszFilename)
5376 RTStrFree(pCache->pszFilename);
5377 RTMemFree(pCache);
5378 }
5379 }
5380
5381 if (RT_SUCCESS(rc) && pCbProgress && pCbProgress->pfnProgress)
5382 pCbProgress->pfnProgress(pIfProgress->pvUser, 100);
5383
5384 LogFlowFunc(("returns %Rrc\n", rc));
5385 return rc;
5386}
5387
5388/**
5389 * Merges two images (not necessarily with direct parent/child relationship).
5390 * As a side effect the source image and potentially the other images which
5391 * are also merged to the destination are deleted from both the disk and the
5392 * images in the HDD container.
5393 *
5394 * @returns VBox status code.
5395 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
5396 * @param pDisk Pointer to HDD container.
5397 * @param nImageFrom Name of the image file to merge from.
5398 * @param nImageTo Name of the image file to merge to.
5399 * @param pVDIfsOperation Pointer to the per-operation VD interface list.
5400 */
5401VBOXDDU_DECL(int) VDMerge(PVBOXHDD pDisk, unsigned nImageFrom,
5402 unsigned nImageTo, PVDINTERFACE pVDIfsOperation)
5403{
5404 int rc = VINF_SUCCESS;
5405 int rc2;
5406 bool fLockWrite = false, fLockRead = false;
5407 void *pvBuf = NULL;
5408
5409 LogFlowFunc(("pDisk=%#p nImageFrom=%u nImageTo=%u pVDIfsOperation=%#p\n",
5410 pDisk, nImageFrom, nImageTo, pVDIfsOperation));
5411
5412 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
5413 VDINTERFACETYPE_PROGRESS);
5414 PVDINTERFACEPROGRESS pCbProgress = NULL;
5415 if (pIfProgress)
5416 pCbProgress = VDGetInterfaceProgress(pIfProgress);
5417
5418 do
5419 {
5420 /* sanity check */
5421 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
5422 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
5423
5424 /* For simplicity reasons lock for writing as the image reopen below
5425 * might need it. After all the reopen is usually needed. */
5426 rc2 = vdThreadStartWrite(pDisk);
5427 AssertRC(rc2);
5428 fLockWrite = true;
5429 PVDIMAGE pImageFrom = vdGetImageByNumber(pDisk, nImageFrom);
5430 PVDIMAGE pImageTo = vdGetImageByNumber(pDisk, nImageTo);
5431 if (!pImageFrom || !pImageTo)
5432 {
5433 rc = VERR_VD_IMAGE_NOT_FOUND;
5434 break;
5435 }
5436 AssertBreakStmt(pImageFrom != pImageTo, rc = VERR_INVALID_PARAMETER);
5437
5438 /* Make sure destination image is writable. */
5439 unsigned uOpenFlags = pImageTo->Backend->pfnGetOpenFlags(pImageTo->pBackendData);
5440 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
5441 {
5442 uOpenFlags &= ~VD_OPEN_FLAGS_READONLY;
5443 rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pBackendData,
5444 uOpenFlags);
5445 if (RT_FAILURE(rc))
5446 break;
5447 }
5448
5449 /* Get size of destination image. */
5450 uint64_t cbSize = pImageTo->Backend->pfnGetSize(pImageTo->pBackendData);
5451 rc2 = vdThreadFinishWrite(pDisk);
5452 AssertRC(rc2);
5453 fLockWrite = false;
5454
5455 /* Allocate tmp buffer. */
5456 pvBuf = RTMemTmpAlloc(VD_MERGE_BUFFER_SIZE);
5457 if (!pvBuf)
5458 {
5459 rc = VERR_NO_MEMORY;
5460 break;
5461 }
5462
5463 /* Merging is done directly on the images itself. This potentially
5464 * causes trouble if the disk is full in the middle of operation. */
5465 if (nImageFrom < nImageTo)
5466 {
5467 /* Merge parent state into child. This means writing all not
5468 * allocated blocks in the destination image which are allocated in
5469 * the images to be merged. */
5470 uint64_t uOffset = 0;
5471 uint64_t cbRemaining = cbSize;
5472 do
5473 {
5474 size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
5475
5476 /* Need to hold the write lock during a read-write operation. */
5477 rc2 = vdThreadStartWrite(pDisk);
5478 AssertRC(rc2);
5479 fLockWrite = true;
5480
5481 rc = pImageTo->Backend->pfnRead(pImageTo->pBackendData,
5482 uOffset, pvBuf, cbThisRead,
5483 &cbThisRead);
5484 if (rc == VERR_VD_BLOCK_FREE)
5485 {
5486 /* Search for image with allocated block. Do not attempt to
5487 * read more than the previous reads marked as valid.
5488 * Otherwise this would return stale data when different
5489 * block sizes are used for the images. */
5490 for (PVDIMAGE pCurrImage = pImageTo->pPrev;
5491 pCurrImage != NULL && pCurrImage != pImageFrom->pPrev && rc == VERR_VD_BLOCK_FREE;
5492 pCurrImage = pCurrImage->pPrev)
5493 {
5494 rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
5495 uOffset, pvBuf,
5496 cbThisRead,
5497 &cbThisRead);
5498 }
5499
5500 if (rc != VERR_VD_BLOCK_FREE)
5501 {
5502 if (RT_FAILURE(rc))
5503 break;
5504 /* Updating the cache is required because this might be a live merge. */
5505 rc = vdWriteHelper(pDisk, pImageTo, pImageFrom->pPrev,
5506 uOffset, pvBuf, cbThisRead,
5507 true /* fUpdateCache */);
5508 if (RT_FAILURE(rc))
5509 break;
5510 }
5511 else
5512 rc = VINF_SUCCESS;
5513 }
5514 else if (RT_FAILURE(rc))
5515 break;
5516
5517 rc2 = vdThreadFinishWrite(pDisk);
5518 AssertRC(rc2);
5519 fLockWrite = false;
5520
5521 uOffset += cbThisRead;
5522 cbRemaining -= cbThisRead;
5523
5524 if (pCbProgress && pCbProgress->pfnProgress)
5525 {
5526 /** @todo r=klaus: this can update the progress to the same
5527 * percentage over and over again if the image format makes
5528 * relatively small increments. */
5529 rc = pCbProgress->pfnProgress(pIfProgress->pvUser,
5530 uOffset * 99 / cbSize);
5531 if (RT_FAILURE(rc))
5532 break;
5533 }
5534 } while (uOffset < cbSize);
5535 }
5536 else
5537 {
5538 /*
5539 * We may need to update the parent uuid of the child coming after
5540 * the last image to be merged. We have to reopen it read/write.
5541 *
5542 * This is done before we do the actual merge to prevent an
5543 * inconsistent chain if the mode change fails for some reason.
5544 */
5545 if (pImageFrom->pNext)
5546 {
5547 PVDIMAGE pImageChild = pImageFrom->pNext;
5548
5549 /* Take the write lock. */
5550 rc2 = vdThreadStartWrite(pDisk);
5551 AssertRC(rc2);
5552 fLockWrite = true;
5553
5554 /* We need to open the image in read/write mode. */
5555 uOpenFlags = pImageChild->Backend->pfnGetOpenFlags(pImageChild->pBackendData);
5556
5557 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
5558 {
5559 uOpenFlags &= ~VD_OPEN_FLAGS_READONLY;
5560 rc = pImageChild->Backend->pfnSetOpenFlags(pImageChild->pBackendData,
5561 uOpenFlags);
5562 if (RT_FAILURE(rc))
5563 break;
5564 }
5565
5566 rc2 = vdThreadFinishWrite(pDisk);
5567 AssertRC(rc2);
5568 fLockWrite = false;
5569 }
5570
5571 /* If the merge is from the last image we have to relay all writes
5572 * to the merge destination as well, so that concurrent writes
5573 * (in case of a live merge) are handled correctly. */
5574 if (!pImageFrom->pNext)
5575 {
5576 /* Take the write lock. */
5577 rc2 = vdThreadStartWrite(pDisk);
5578 AssertRC(rc2);
5579 fLockWrite = true;
5580
5581 pDisk->pImageRelay = pImageTo;
5582
5583 rc2 = vdThreadFinishWrite(pDisk);
5584 AssertRC(rc2);
5585 fLockWrite = false;
5586 }
5587
5588 /* Merge child state into parent. This means writing all blocks
5589 * which are allocated in the image up to the source image to the
5590 * destination image. */
5591 uint64_t uOffset = 0;
5592 uint64_t cbRemaining = cbSize;
5593 do
5594 {
5595 size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
5596 rc = VERR_VD_BLOCK_FREE;
5597
5598 /* Need to hold the write lock during a read-write operation. */
5599 rc2 = vdThreadStartWrite(pDisk);
5600 AssertRC(rc2);
5601 fLockWrite = true;
5602
5603 /* Search for image with allocated block. Do not attempt to
5604 * read more than the previous reads marked as valid. Otherwise
5605 * this would return stale data when different block sizes are
5606 * used for the images. */
5607 for (PVDIMAGE pCurrImage = pImageFrom;
5608 pCurrImage != NULL && pCurrImage != pImageTo && rc == VERR_VD_BLOCK_FREE;
5609 pCurrImage = pCurrImage->pPrev)
5610 {
5611 rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
5612 uOffset, pvBuf,
5613 cbThisRead, &cbThisRead);
5614 }
5615
5616 if (rc != VERR_VD_BLOCK_FREE)
5617 {
5618 if (RT_FAILURE(rc))
5619 break;
5620 rc = vdWriteHelper(pDisk, pImageTo, NULL, uOffset, pvBuf,
5621 cbThisRead, true /* fUpdateCache */);
5622 if (RT_FAILURE(rc))
5623 break;
5624 }
5625 else
5626 rc = VINF_SUCCESS;
5627
5628 rc2 = vdThreadFinishWrite(pDisk);
5629 AssertRC(rc2);
5630 fLockWrite = false;
5631
5632 uOffset += cbThisRead;
5633 cbRemaining -= cbThisRead;
5634
5635 if (pCbProgress && pCbProgress->pfnProgress)
5636 {
5637 /** @todo r=klaus: this can update the progress to the same
5638 * percentage over and over again if the image format makes
5639 * relatively small increments. */
5640 rc = pCbProgress->pfnProgress(pIfProgress->pvUser,
5641 uOffset * 99 / cbSize);
5642 if (RT_FAILURE(rc))
5643 break;
5644 }
5645 } while (uOffset < cbSize);
5646
5647 /* In case we set up a "write proxy" image above we must clear
5648 * this again now to prevent stray writes. Failure or not. */
5649 if (!pImageFrom->pNext)
5650 {
5651 /* Take the write lock. */
5652 rc2 = vdThreadStartWrite(pDisk);
5653 AssertRC(rc2);
5654 fLockWrite = true;
5655
5656 pDisk->pImageRelay = NULL;
5657
5658 rc2 = vdThreadFinishWrite(pDisk);
5659 AssertRC(rc2);
5660 fLockWrite = false;
5661 }
5662 }
5663
5664 /*
5665 * Leave in case of an error to avoid corrupted data in the image chain
5666 * (includes cancelling the operation by the user).
5667 */
5668 if (RT_FAILURE(rc))
5669 break;
5670
5671 /* Need to hold the write lock while finishing the merge. */
5672 rc2 = vdThreadStartWrite(pDisk);
5673 AssertRC(rc2);
5674 fLockWrite = true;
5675
5676 /* Update parent UUID so that image chain is consistent. */
5677 RTUUID Uuid;
5678 PVDIMAGE pImageChild = NULL;
5679 if (nImageFrom < nImageTo)
5680 {
5681 if (pImageFrom->pPrev)
5682 {
5683 rc = pImageFrom->pPrev->Backend->pfnGetUuid(pImageFrom->pPrev->pBackendData,
5684 &Uuid);
5685 AssertRC(rc);
5686 }
5687 else
5688 RTUuidClear(&Uuid);
5689 rc = pImageTo->Backend->pfnSetParentUuid(pImageTo->pBackendData,
5690 &Uuid);
5691 AssertRC(rc);
5692 }
5693 else
5694 {
5695 /* Update the parent uuid of the child of the last merged image. */
5696 if (pImageFrom->pNext)
5697 {
5698 rc = pImageTo->Backend->pfnGetUuid(pImageTo->pBackendData,
5699 &Uuid);
5700 AssertRC(rc);
5701
5702 rc = pImageFrom->Backend->pfnSetParentUuid(pImageFrom->pNext->pBackendData,
5703 &Uuid);
5704 AssertRC(rc);
5705
5706 pImageChild = pImageFrom->pNext;
5707 }
5708 }
5709
5710 /* Delete the no longer needed images. */
5711 PVDIMAGE pImg = pImageFrom, pTmp;
5712 while (pImg != pImageTo)
5713 {
5714 if (nImageFrom < nImageTo)
5715 pTmp = pImg->pNext;
5716 else
5717 pTmp = pImg->pPrev;
5718 vdRemoveImageFromList(pDisk, pImg);
5719 pImg->Backend->pfnClose(pImg->pBackendData, true);
5720 RTMemFree(pImg->pszFilename);
5721 RTMemFree(pImg);
5722 pImg = pTmp;
5723 }
5724
5725 /* Make sure destination image is back to read only if necessary. */
5726 if (pImageTo != pDisk->pLast)
5727 {
5728 uOpenFlags = pImageTo->Backend->pfnGetOpenFlags(pImageTo->pBackendData);
5729 uOpenFlags |= VD_OPEN_FLAGS_READONLY;
5730 rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pBackendData,
5731 uOpenFlags);
5732 if (RT_FAILURE(rc))
5733 break;
5734 }
5735
5736 /*
5737 * Make sure the child is readonly
5738 * for the child -> parent merge direction
5739 * if necessary.
5740 */
5741 if ( nImageFrom > nImageTo
5742 && pImageChild
5743 && pImageChild != pDisk->pLast)
5744 {
5745 uOpenFlags = pImageChild->Backend->pfnGetOpenFlags(pImageChild->pBackendData);
5746 uOpenFlags |= VD_OPEN_FLAGS_READONLY;
5747 rc = pImageChild->Backend->pfnSetOpenFlags(pImageChild->pBackendData,
5748 uOpenFlags);
5749 if (RT_FAILURE(rc))
5750 break;
5751 }
5752 } while (0);
5753
5754 if (RT_UNLIKELY(fLockWrite))
5755 {
5756 rc2 = vdThreadFinishWrite(pDisk);
5757 AssertRC(rc2);
5758 }
5759 else if (RT_UNLIKELY(fLockRead))
5760 {
5761 rc2 = vdThreadFinishRead(pDisk);
5762 AssertRC(rc2);
5763 }
5764
5765 if (pvBuf)
5766 RTMemTmpFree(pvBuf);
5767
5768 if (RT_SUCCESS(rc) && pCbProgress && pCbProgress->pfnProgress)
5769 pCbProgress->pfnProgress(pIfProgress->pvUser, 100);
5770
5771 LogFlowFunc(("returns %Rrc\n", rc));
5772 return rc;
5773}
5774
5775/**
5776 * Copies an image from one HDD container to another.
5777 * The copy is opened in the target HDD container.
5778 * It is possible to convert between different image formats, because the
5779 * backend for the destination may be different from the source.
5780 * If both the source and destination reference the same HDD container,
5781 * then the image is moved (by copying/deleting or renaming) to the new location.
5782 * The source container is unchanged if the move operation fails, otherwise
5783 * the image at the new location is opened in the same way as the old one was.
5784 *
5785 * @returns VBox status code.
5786 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
5787 * @param pDiskFrom Pointer to source HDD container.
5788 * @param nImage Image number, counts from 0. 0 is always base image of container.
5789 * @param pDiskTo Pointer to destination HDD container.
5790 * @param pszBackend Name of the image file backend to use.
5791 * @param pszFilename New name of the image (may be NULL if pDiskFrom == pDiskTo).
5792 * @param fMoveByRename If true, attempt to perform a move by renaming (if successful the new size is ignored).
5793 * @param cbSize New image size (0 means leave unchanged).
5794 * @param uImageFlags Flags specifying special destination image features.
5795 * @param pDstUuid New UUID of the destination image. If NULL, a new UUID is created.
5796 * This parameter is used if and only if a true copy is created.
5797 * In all rename/move cases the UUIDs are copied over.
5798 * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
5799 * Only used if the destination image is created.
5800 * @param pVDIfsOperation Pointer to the per-operation VD interface list.
5801 * @param pDstVDIfsImage Pointer to the per-image VD interface list, for the
5802 * destination image.
5803 * @param pDstVDIfsOperation Pointer to the per-image VD interface list,
5804 * for the destination image.
5805 */
5806VBOXDDU_DECL(int) VDCopy(PVBOXHDD pDiskFrom, unsigned nImage, PVBOXHDD pDiskTo,
5807 const char *pszBackend, const char *pszFilename,
5808 bool fMoveByRename, uint64_t cbSize,
5809 unsigned uImageFlags, PCRTUUID pDstUuid,
5810 unsigned uOpenFlags, PVDINTERFACE pVDIfsOperation,
5811 PVDINTERFACE pDstVDIfsImage,
5812 PVDINTERFACE pDstVDIfsOperation)
5813{
5814 int rc = VINF_SUCCESS;
5815 int rc2;
5816 bool fLockReadFrom = false, fLockWriteFrom = false, fLockWriteTo = false;
5817 void *pvBuf = NULL;
5818 PVDIMAGE pImageTo = NULL;
5819
5820 LogFlowFunc(("pDiskFrom=%#p nImage=%u pDiskTo=%#p pszBackend=\"%s\" pszFilename=\"%s\" fMoveByRename=%d cbSize=%llu uImageFlags=%#x pDstUuid=%#p uOpenFlags=%#x pVDIfsOperation=%#p pDstVDIfsImage=%#p pDstVDIfsOperation=%#p\n",
5821 pDiskFrom, nImage, pDiskTo, pszBackend, pszFilename, fMoveByRename, cbSize, uImageFlags, pDstUuid, uOpenFlags, pVDIfsOperation, pDstVDIfsImage, pDstVDIfsOperation));
5822
5823 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
5824 VDINTERFACETYPE_PROGRESS);
5825 PVDINTERFACEPROGRESS pCbProgress = NULL;
5826 if (pIfProgress)
5827 pCbProgress = VDGetInterfaceProgress(pIfProgress);
5828
5829 PVDINTERFACE pDstIfProgress = VDInterfaceGet(pDstVDIfsOperation,
5830 VDINTERFACETYPE_PROGRESS);
5831 PVDINTERFACEPROGRESS pDstCbProgress = NULL;
5832 if (pDstIfProgress)
5833 pDstCbProgress = VDGetInterfaceProgress(pDstIfProgress);
5834
5835 do {
5836 /* Check arguments. */
5837 AssertMsgBreakStmt(VALID_PTR(pDiskFrom), ("pDiskFrom=%#p\n", pDiskFrom),
5838 rc = VERR_INVALID_PARAMETER);
5839 AssertMsg(pDiskFrom->u32Signature == VBOXHDDDISK_SIGNATURE,
5840 ("u32Signature=%08x\n", pDiskFrom->u32Signature));
5841
5842 rc2 = vdThreadStartRead(pDiskFrom);
5843 AssertRC(rc2);
5844 fLockReadFrom = true;
5845 PVDIMAGE pImageFrom = vdGetImageByNumber(pDiskFrom, nImage);
5846 AssertPtrBreakStmt(pImageFrom, rc = VERR_VD_IMAGE_NOT_FOUND);
5847 AssertMsgBreakStmt(VALID_PTR(pDiskTo), ("pDiskTo=%#p\n", pDiskTo),
5848 rc = VERR_INVALID_PARAMETER);
5849 AssertMsg(pDiskTo->u32Signature == VBOXHDDDISK_SIGNATURE,
5850 ("u32Signature=%08x\n", pDiskTo->u32Signature));
5851
5852 /* Move the image. */
5853 if (pDiskFrom == pDiskTo)
5854 {
5855 /* Rename only works when backends are the same, are file based
5856 * and the rename method is implemented. */
5857 if ( fMoveByRename
5858 && !RTStrICmp(pszBackend, pImageFrom->Backend->pszBackendName)
5859 && pImageFrom->Backend->uBackendCaps & VD_CAP_FILE
5860 && pImageFrom->Backend->pfnRename)
5861 {
5862 rc2 = vdThreadFinishRead(pDiskFrom);
5863 AssertRC(rc2);
5864 fLockReadFrom = false;
5865
5866 rc2 = vdThreadStartWrite(pDiskFrom);
5867 AssertRC(rc2);
5868 fLockWriteFrom = true;
5869 rc = pImageFrom->Backend->pfnRename(pImageFrom->pBackendData, pszFilename ? pszFilename : pImageFrom->pszFilename);
5870 break;
5871 }
5872
5873 /** @todo Moving (including shrinking/growing) of the image is
5874 * requested, but the rename attempt failed or it wasn't possible.
5875 * Must now copy image to temp location. */
5876 AssertReleaseMsgFailed(("VDCopy: moving by copy/delete not implemented\n"));
5877 }
5878
5879 /* pszFilename is allowed to be NULL, as this indicates copy to the existing image. */
5880 AssertMsgBreakStmt(pszFilename == NULL || (VALID_PTR(pszFilename) && *pszFilename),
5881 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
5882 rc = VERR_INVALID_PARAMETER);
5883
5884 uint64_t cbSizeFrom;
5885 cbSizeFrom = pImageFrom->Backend->pfnGetSize(pImageFrom->pBackendData);
5886 if (cbSizeFrom == 0)
5887 {
5888 rc = VERR_VD_VALUE_NOT_FOUND;
5889 break;
5890 }
5891
5892 VDGEOMETRY PCHSGeometryFrom = {0, 0, 0};
5893 VDGEOMETRY LCHSGeometryFrom = {0, 0, 0};
5894 pImageFrom->Backend->pfnGetPCHSGeometry(pImageFrom->pBackendData, &PCHSGeometryFrom);
5895 pImageFrom->Backend->pfnGetLCHSGeometry(pImageFrom->pBackendData, &LCHSGeometryFrom);
5896
5897 RTUUID ImageUuid, ImageModificationUuid;
5898 if (pDiskFrom != pDiskTo)
5899 {
5900 if (pDstUuid)
5901 ImageUuid = *pDstUuid;
5902 else
5903 RTUuidCreate(&ImageUuid);
5904 }
5905 else
5906 {
5907 rc = pImageFrom->Backend->pfnGetUuid(pImageFrom->pBackendData, &ImageUuid);
5908 if (RT_FAILURE(rc))
5909 RTUuidCreate(&ImageUuid);
5910 }
5911 rc = pImageFrom->Backend->pfnGetModificationUuid(pImageFrom->pBackendData, &ImageModificationUuid);
5912 if (RT_FAILURE(rc))
5913 RTUuidClear(&ImageModificationUuid);
5914
5915 char szComment[1024];
5916 rc = pImageFrom->Backend->pfnGetComment(pImageFrom->pBackendData, szComment, sizeof(szComment));
5917 if (RT_FAILURE(rc))
5918 szComment[0] = '\0';
5919 else
5920 szComment[sizeof(szComment) - 1] = '\0';
5921
5922 rc2 = vdThreadFinishRead(pDiskFrom);
5923 AssertRC(rc2);
5924 fLockReadFrom = false;
5925
5926 rc2 = vdThreadStartRead(pDiskTo);
5927 AssertRC(rc2);
5928 unsigned cImagesTo = pDiskTo->cImages;
5929 rc2 = vdThreadFinishRead(pDiskTo);
5930 AssertRC(rc2);
5931
5932 if (pszFilename)
5933 {
5934 if (cbSize == 0)
5935 cbSize = cbSizeFrom;
5936
5937 /* Create destination image with the properties of source image. */
5938 /** @todo replace the VDCreateDiff/VDCreateBase calls by direct
5939 * calls to the backend. Unifies the code and reduces the API
5940 * dependencies. Would also make the synchronization explicit. */
5941 if (cImagesTo > 0)
5942 {
5943 rc = VDCreateDiff(pDiskTo, pszBackend, pszFilename,
5944 uImageFlags, szComment, &ImageUuid,
5945 NULL /* pParentUuid */,
5946 uOpenFlags & ~VD_OPEN_FLAGS_READONLY,
5947 pDstVDIfsImage, NULL);
5948
5949 rc2 = vdThreadStartWrite(pDiskTo);
5950 AssertRC(rc2);
5951 fLockWriteTo = true;
5952 } else {
5953 /** @todo hack to force creation of a fixed image for
5954 * the RAW backend, which can't handle anything else. */
5955 if (!RTStrICmp(pszBackend, "RAW"))
5956 uImageFlags |= VD_IMAGE_FLAGS_FIXED;
5957
5958 vdFixupPCHSGeometry(&PCHSGeometryFrom, cbSize);
5959 vdFixupLCHSGeometry(&LCHSGeometryFrom, cbSize);
5960
5961 rc = VDCreateBase(pDiskTo, pszBackend, pszFilename, cbSize,
5962 uImageFlags, szComment,
5963 &PCHSGeometryFrom, &LCHSGeometryFrom,
5964 NULL, uOpenFlags & ~VD_OPEN_FLAGS_READONLY,
5965 pDstVDIfsImage, NULL);
5966
5967 rc2 = vdThreadStartWrite(pDiskTo);
5968 AssertRC(rc2);
5969 fLockWriteTo = true;
5970
5971 if (RT_SUCCESS(rc) && !RTUuidIsNull(&ImageUuid))
5972 pDiskTo->pLast->Backend->pfnSetUuid(pDiskTo->pLast->pBackendData, &ImageUuid);
5973 }
5974 if (RT_FAILURE(rc))
5975 break;
5976
5977 pImageTo = pDiskTo->pLast;
5978 AssertPtrBreakStmt(pImageTo, rc = VERR_VD_IMAGE_NOT_FOUND);
5979
5980 cbSize = RT_MIN(cbSize, cbSizeFrom);
5981 }
5982 else
5983 {
5984 pImageTo = pDiskTo->pLast;
5985 AssertPtrBreakStmt(pImageTo, rc = VERR_VD_IMAGE_NOT_FOUND);
5986
5987 uint64_t cbSizeTo;
5988 cbSizeTo = pImageTo->Backend->pfnGetSize(pImageTo->pBackendData);
5989 if (cbSizeTo == 0)
5990 {
5991 rc = VERR_VD_VALUE_NOT_FOUND;
5992 break;
5993 }
5994
5995 if (cbSize == 0)
5996 cbSize = RT_MIN(cbSizeFrom, cbSizeTo);
5997
5998 vdFixupPCHSGeometry(&PCHSGeometryFrom, cbSize);
5999 vdFixupLCHSGeometry(&LCHSGeometryFrom, cbSize);
6000
6001 /* Update the geometry in the destination image. */
6002 pImageTo->Backend->pfnSetPCHSGeometry(pImageTo->pBackendData, &PCHSGeometryFrom);
6003 pImageTo->Backend->pfnSetLCHSGeometry(pImageTo->pBackendData, &LCHSGeometryFrom);
6004 }
6005
6006 rc2 = vdThreadFinishWrite(pDiskTo);
6007 AssertRC(rc2);
6008 fLockWriteTo = false;
6009
6010 /* Allocate tmp buffer. */
6011 pvBuf = RTMemTmpAlloc(VD_MERGE_BUFFER_SIZE);
6012 if (!pvBuf)
6013 {
6014 rc = VERR_NO_MEMORY;
6015 break;
6016 }
6017
6018 /* Whether we can take the optimized copy path (false) or not.
6019 * Don't optimize if the image existed or if it is a child image. */
6020 bool fRegularRead = (pszFilename == NULL) || (cImagesTo > 0);
6021
6022 /* Copy the data. */
6023 uint64_t uOffset = 0;
6024 uint64_t cbRemaining = cbSize;
6025
6026 do
6027 {
6028 size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
6029
6030 /* Note that we don't attempt to synchronize cross-disk accesses.
6031 * It wouldn't be very difficult to do, just the lock order would
6032 * need to be defined somehow to prevent deadlocks. Postpone such
6033 * magic as there is no use case for this. */
6034
6035 rc2 = vdThreadStartRead(pDiskFrom);
6036 AssertRC(rc2);
6037 fLockReadFrom = true;
6038
6039 /*
6040 * Updating the cache doesn't make any sense
6041 * as we are looping once through the image.
6042 */
6043 rc = vdReadHelper(pDiskFrom, pImageFrom, NULL, uOffset, pvBuf,
6044 cbThisRead, fRegularRead,
6045 false /* fUpdateCache */);
6046 if (RT_FAILURE(rc) && rc != VERR_VD_BLOCK_FREE)
6047 break;
6048
6049 rc2 = vdThreadFinishRead(pDiskFrom);
6050 AssertRC(rc2);
6051 fLockReadFrom = false;
6052
6053 if (rc != VERR_VD_BLOCK_FREE)
6054 {
6055 rc2 = vdThreadStartWrite(pDiskTo);
6056 AssertRC(rc2);
6057 fLockWriteTo = true;
6058
6059 rc = vdWriteHelper(pDiskTo, pImageTo, NULL, uOffset, pvBuf,
6060 cbThisRead, false /* fUpdateCache */);
6061 if (RT_FAILURE(rc))
6062 break;
6063
6064 rc2 = vdThreadFinishWrite(pDiskTo);
6065 AssertRC(rc2);
6066 fLockWriteTo = false;
6067 }
6068 else /* Don't propagate the error to the outside */
6069 rc = VINF_SUCCESS;
6070
6071 uOffset += cbThisRead;
6072 cbRemaining -= cbThisRead;
6073
6074 if (pCbProgress && pCbProgress->pfnProgress)
6075 {
6076 /** @todo r=klaus: this can update the progress to the same
6077 * percentage over and over again if the image format makes
6078 * relatively small increments. */
6079 rc = pCbProgress->pfnProgress(pIfProgress->pvUser,
6080 uOffset * 99 / cbSize);
6081 if (RT_FAILURE(rc))
6082 break;
6083 }
6084 if (pDstCbProgress && pDstCbProgress->pfnProgress)
6085 {
6086 /** @todo r=klaus: this can update the progress to the same
6087 * percentage over and over again if the image format makes
6088 * relatively small increments. */
6089 rc = pDstCbProgress->pfnProgress(pDstIfProgress->pvUser,
6090 uOffset * 99 / cbSize);
6091 if (RT_FAILURE(rc))
6092 break;
6093 }
6094 } while (uOffset < cbSize);
6095
6096 if (RT_SUCCESS(rc))
6097 {
6098 rc2 = vdThreadStartWrite(pDiskTo);
6099 AssertRC(rc2);
6100 fLockWriteTo = true;
6101
6102 /* Only set modification UUID if it is non-null, since the source
6103 * backend might not provide a valid modification UUID. */
6104 if (!RTUuidIsNull(&ImageModificationUuid))
6105 pImageTo->Backend->pfnSetModificationUuid(pImageTo->pBackendData, &ImageModificationUuid);
6106
6107 /* Set the requested open flags if they differ from the value
6108 * required for creating the image and copying the contents. */
6109 if ( pImageTo && pszFilename
6110 && uOpenFlags != (uOpenFlags & ~VD_OPEN_FLAGS_READONLY))
6111 rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pBackendData,
6112 uOpenFlags);
6113 }
6114 } while (0);
6115
6116 if (RT_FAILURE(rc) && pImageTo && pszFilename)
6117 {
6118 /* Take the write lock only if it is not taken. Not worth making the
6119 * above code even more complicated. */
6120 if (RT_UNLIKELY(!fLockWriteTo))
6121 {
6122 rc2 = vdThreadStartWrite(pDiskTo);
6123 AssertRC(rc2);
6124 fLockWriteTo = true;
6125 }
6126 /* Error detected, but new image created. Remove image from list. */
6127 vdRemoveImageFromList(pDiskTo, pImageTo);
6128
6129 /* Close and delete image. */
6130 rc2 = pImageTo->Backend->pfnClose(pImageTo->pBackendData, true);
6131 AssertRC(rc2);
6132 pImageTo->pBackendData = NULL;
6133
6134 /* Free remaining resources. */
6135 if (pImageTo->pszFilename)
6136 RTStrFree(pImageTo->pszFilename);
6137
6138 RTMemFree(pImageTo);
6139 }
6140
6141 if (RT_UNLIKELY(fLockWriteTo))
6142 {
6143 rc2 = vdThreadFinishWrite(pDiskTo);
6144 AssertRC(rc2);
6145 }
6146 if (RT_UNLIKELY(fLockWriteFrom))
6147 {
6148 rc2 = vdThreadFinishWrite(pDiskFrom);
6149 AssertRC(rc2);
6150 }
6151 else if (RT_UNLIKELY(fLockReadFrom))
6152 {
6153 rc2 = vdThreadFinishRead(pDiskFrom);
6154 AssertRC(rc2);
6155 }
6156
6157 if (pvBuf)
6158 RTMemTmpFree(pvBuf);
6159
6160 if (RT_SUCCESS(rc))
6161 {
6162 if (pCbProgress && pCbProgress->pfnProgress)
6163 pCbProgress->pfnProgress(pIfProgress->pvUser, 100);
6164 if (pDstCbProgress && pDstCbProgress->pfnProgress)
6165 pDstCbProgress->pfnProgress(pDstIfProgress->pvUser, 100);
6166 }
6167
6168 LogFlowFunc(("returns %Rrc\n", rc));
6169 return rc;
6170}
6171
6172/**
6173 * Optimizes the storage consumption of an image. Typically the unused blocks
6174 * have to be wiped with zeroes to achieve a substantial reduced storage use.
6175 * Another optimization done is reordering the image blocks, which can provide
6176 * a significant performance boost, as reads and writes tend to use less random
6177 * file offsets.
6178 *
6179 * @return VBox status code.
6180 * @return VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
6181 * @return VERR_VD_IMAGE_READ_ONLY if image is not writable.
6182 * @return VERR_NOT_SUPPORTED if this kind of image can be compacted, but
6183 * the code for this isn't implemented yet.
6184 * @param pDisk Pointer to HDD container.
6185 * @param nImage Image number, counts from 0. 0 is always base image of container.
6186 * @param pVDIfsOperation Pointer to the per-operation VD interface list.
6187 */
6188VBOXDDU_DECL(int) VDCompact(PVBOXHDD pDisk, unsigned nImage,
6189 PVDINTERFACE pVDIfsOperation)
6190{
6191 int rc = VINF_SUCCESS;
6192 int rc2;
6193 bool fLockRead = false, fLockWrite = false;
6194 void *pvBuf = NULL;
6195 void *pvTmp = NULL;
6196
6197 LogFlowFunc(("pDisk=%#p nImage=%u pVDIfsOperation=%#p\n",
6198 pDisk, nImage, pVDIfsOperation));
6199
6200 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
6201 VDINTERFACETYPE_PROGRESS);
6202 PVDINTERFACEPROGRESS pCbProgress = NULL;
6203 if (pIfProgress)
6204 pCbProgress = VDGetInterfaceProgress(pIfProgress);
6205
6206 do {
6207 /* Check arguments. */
6208 AssertMsgBreakStmt(VALID_PTR(pDisk), ("pDisk=%#p\n", pDisk),
6209 rc = VERR_INVALID_PARAMETER);
6210 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE,
6211 ("u32Signature=%08x\n", pDisk->u32Signature));
6212
6213 rc2 = vdThreadStartRead(pDisk);
6214 AssertRC(rc2);
6215 fLockRead = true;
6216
6217 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
6218 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
6219
6220 /* If there is no compact callback for not file based backends then
6221 * the backend doesn't need compaction. No need to make much fuss about
6222 * this. For file based ones signal this as not yet supported. */
6223 if (!pImage->Backend->pfnCompact)
6224 {
6225 if (pImage->Backend->uBackendCaps & VD_CAP_FILE)
6226 rc = VERR_NOT_SUPPORTED;
6227 else
6228 rc = VINF_SUCCESS;
6229 break;
6230 }
6231
6232 /* Insert interface for reading parent state into per-operation list,
6233 * if there is a parent image. */
6234 VDINTERFACE IfOpParent;
6235 VDINTERFACEPARENTSTATE ParentCb;
6236 VDPARENTSTATEDESC ParentUser;
6237 if (pImage->pPrev)
6238 {
6239 ParentCb.cbSize = sizeof(ParentCb);
6240 ParentCb.enmInterface = VDINTERFACETYPE_PARENTSTATE;
6241 ParentCb.pfnParentRead = vdParentRead;
6242 ParentUser.pDisk = pDisk;
6243 ParentUser.pImage = pImage->pPrev;
6244 rc = VDInterfaceAdd(&IfOpParent, "VDCompact_ParentState", VDINTERFACETYPE_PARENTSTATE,
6245 &ParentCb, &ParentUser, &pVDIfsOperation);
6246 AssertRC(rc);
6247 }
6248
6249 rc2 = vdThreadFinishRead(pDisk);
6250 AssertRC(rc2);
6251 fLockRead = false;
6252
6253 rc2 = vdThreadStartWrite(pDisk);
6254 AssertRC(rc2);
6255 fLockWrite = true;
6256
6257 rc = pImage->Backend->pfnCompact(pImage->pBackendData,
6258 0, 99,
6259 pDisk->pVDIfsDisk,
6260 pImage->pVDIfsImage,
6261 pVDIfsOperation);
6262 } while (0);
6263
6264 if (RT_UNLIKELY(fLockWrite))
6265 {
6266 rc2 = vdThreadFinishWrite(pDisk);
6267 AssertRC(rc2);
6268 }
6269 else if (RT_UNLIKELY(fLockRead))
6270 {
6271 rc2 = vdThreadFinishRead(pDisk);
6272 AssertRC(rc2);
6273 }
6274
6275 if (pvBuf)
6276 RTMemTmpFree(pvBuf);
6277 if (pvTmp)
6278 RTMemTmpFree(pvTmp);
6279
6280 if (RT_SUCCESS(rc))
6281 {
6282 if (pCbProgress && pCbProgress->pfnProgress)
6283 pCbProgress->pfnProgress(pIfProgress->pvUser, 100);
6284 }
6285
6286 LogFlowFunc(("returns %Rrc\n", rc));
6287 return rc;
6288}
6289
6290/**
6291 * Resizes the the given disk image to the given size.
6292 *
6293 * @return VBox status
6294 * @return VERR_VD_IMAGE_READ_ONLY if image is not writable.
6295 * @return VERR_NOT_SUPPORTED if this kind of image can be compacted, but
6296 *
6297 * @param pDisk Pointer to the HDD container.
6298 * @param cbSize New size of the image.
6299 * @param pPCHSGeometry Pointer to the new physical disk geometry <= (16383,16,63). Not NULL.
6300 * @param pLCHSGeometry Pointer to the new logical disk geometry <= (x,255,63). Not NULL.
6301 * @param pVDIfsOperation Pointer to the per-operation VD interface list.
6302 */
6303VBOXDDU_DECL(int) VDResize(PVBOXHDD pDisk, uint64_t cbSize,
6304 PCVDGEOMETRY pPCHSGeometry,
6305 PCVDGEOMETRY pLCHSGeometry,
6306 PVDINTERFACE pVDIfsOperation)
6307{
6308 /** @todo r=klaus resizing was designed to be part of VDCopy, so having a separate function is not desirable. */
6309 int rc = VINF_SUCCESS;
6310 int rc2;
6311 bool fLockRead = false, fLockWrite = false;
6312
6313 LogFlowFunc(("pDisk=%#p cbSize=%llu pVDIfsOperation=%#p\n",
6314 pDisk, cbSize, pVDIfsOperation));
6315
6316 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
6317 VDINTERFACETYPE_PROGRESS);
6318 PVDINTERFACEPROGRESS pCbProgress = NULL;
6319 if (pIfProgress)
6320 pCbProgress = VDGetInterfaceProgress(pIfProgress);
6321
6322 do {
6323 /* Check arguments. */
6324 AssertMsgBreakStmt(VALID_PTR(pDisk), ("pDisk=%#p\n", pDisk),
6325 rc = VERR_INVALID_PARAMETER);
6326 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE,
6327 ("u32Signature=%08x\n", pDisk->u32Signature));
6328
6329 rc2 = vdThreadStartRead(pDisk);
6330 AssertRC(rc2);
6331 fLockRead = true;
6332
6333 /* Not supported if the disk has child images attached. */
6334 AssertMsgBreakStmt(pDisk->cImages == 1, ("cImages=%u\n", pDisk->cImages),
6335 rc = VERR_NOT_SUPPORTED);
6336
6337 PVDIMAGE pImage = pDisk->pBase;
6338
6339 /* If there is no compact callback for not file based backends then
6340 * the backend doesn't need compaction. No need to make much fuss about
6341 * this. For file based ones signal this as not yet supported. */
6342 if (!pImage->Backend->pfnResize)
6343 {
6344 if (pImage->Backend->uBackendCaps & VD_CAP_FILE)
6345 rc = VERR_NOT_SUPPORTED;
6346 else
6347 rc = VINF_SUCCESS;
6348 break;
6349 }
6350
6351 rc2 = vdThreadFinishRead(pDisk);
6352 AssertRC(rc2);
6353 fLockRead = false;
6354
6355 rc2 = vdThreadStartWrite(pDisk);
6356 AssertRC(rc2);
6357 fLockWrite = true;
6358
6359 VDGEOMETRY PCHSGeometryOld;
6360 VDGEOMETRY LCHSGeometryOld;
6361 PCVDGEOMETRY pPCHSGeometryNew;
6362 PCVDGEOMETRY pLCHSGeometryNew;
6363
6364 if (pPCHSGeometry->cCylinders == 0)
6365 {
6366 /* Auto-detect marker, calculate new value ourself. */
6367 rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData, &PCHSGeometryOld);
6368 if (RT_SUCCESS(rc) && (PCHSGeometryOld.cCylinders != 0))
6369 PCHSGeometryOld.cCylinders = RT_MIN(cbSize / 512 / PCHSGeometryOld.cHeads / PCHSGeometryOld.cSectors, 16383);
6370 else if (rc == VERR_VD_GEOMETRY_NOT_SET)
6371 rc = VINF_SUCCESS;
6372
6373 pPCHSGeometryNew = &PCHSGeometryOld;
6374 }
6375 else
6376 pPCHSGeometryNew = pPCHSGeometry;
6377
6378 if (pLCHSGeometry->cCylinders == 0)
6379 {
6380 /* Auto-detect marker, calculate new value ourself. */
6381 rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData, &LCHSGeometryOld);
6382 if (RT_SUCCESS(rc) && (LCHSGeometryOld.cCylinders != 0))
6383 LCHSGeometryOld.cCylinders = cbSize / 512 / LCHSGeometryOld.cHeads / LCHSGeometryOld.cSectors;
6384 else if (rc == VERR_VD_GEOMETRY_NOT_SET)
6385 rc = VINF_SUCCESS;
6386
6387 pLCHSGeometryNew = &LCHSGeometryOld;
6388 }
6389 else
6390 pLCHSGeometryNew = pLCHSGeometry;
6391
6392 if (RT_SUCCESS(rc))
6393 rc = pImage->Backend->pfnResize(pImage->pBackendData,
6394 cbSize,
6395 pPCHSGeometryNew,
6396 pLCHSGeometryNew,
6397 0, 99,
6398 pDisk->pVDIfsDisk,
6399 pImage->pVDIfsImage,
6400 pVDIfsOperation);
6401 } while (0);
6402
6403 if (RT_UNLIKELY(fLockWrite))
6404 {
6405 rc2 = vdThreadFinishWrite(pDisk);
6406 AssertRC(rc2);
6407 }
6408 else if (RT_UNLIKELY(fLockRead))
6409 {
6410 rc2 = vdThreadFinishRead(pDisk);
6411 AssertRC(rc2);
6412 }
6413
6414 if (RT_SUCCESS(rc))
6415 {
6416 if (pCbProgress && pCbProgress->pfnProgress)
6417 pCbProgress->pfnProgress(pIfProgress->pvUser, 100);
6418 }
6419
6420 LogFlowFunc(("returns %Rrc\n", rc));
6421 return rc;
6422}
6423
6424/**
6425 * Closes the last opened image file in HDD container.
6426 * If previous image file was opened in read-only mode (the normal case) and
6427 * the last opened image is in read-write mode then the previous image will be
6428 * reopened in read/write mode.
6429 *
6430 * @returns VBox status code.
6431 * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
6432 * @param pDisk Pointer to HDD container.
6433 * @param fDelete If true, delete the image from the host disk.
6434 */
6435VBOXDDU_DECL(int) VDClose(PVBOXHDD pDisk, bool fDelete)
6436{
6437 int rc = VINF_SUCCESS;
6438 int rc2;
6439 bool fLockWrite = false;
6440
6441 LogFlowFunc(("pDisk=%#p fDelete=%d\n", pDisk, fDelete));
6442 do
6443 {
6444 /* sanity check */
6445 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
6446 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6447
6448 /* Not worth splitting this up into a read lock phase and write
6449 * lock phase, as closing an image is a relatively fast operation
6450 * dominated by the part which needs the write lock. */
6451 rc2 = vdThreadStartWrite(pDisk);
6452 AssertRC(rc2);
6453 fLockWrite = true;
6454
6455 PVDIMAGE pImage = pDisk->pLast;
6456 if (!pImage)
6457 {
6458 rc = VERR_VD_NOT_OPENED;
6459 break;
6460 }
6461 unsigned uOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
6462 /* Remove image from list of opened images. */
6463 vdRemoveImageFromList(pDisk, pImage);
6464 /* Close (and optionally delete) image. */
6465 rc = pImage->Backend->pfnClose(pImage->pBackendData, fDelete);
6466 /* Free remaining resources related to the image. */
6467 RTStrFree(pImage->pszFilename);
6468 RTMemFree(pImage);
6469
6470 pImage = pDisk->pLast;
6471 if (!pImage)
6472 break;
6473
6474 /* If disk was previously in read/write mode, make sure it will stay
6475 * like this (if possible) after closing this image. Set the open flags
6476 * accordingly. */
6477 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
6478 {
6479 uOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
6480 uOpenFlags &= ~ VD_OPEN_FLAGS_READONLY;
6481 rc = pImage->Backend->pfnSetOpenFlags(pImage->pBackendData, uOpenFlags);
6482 }
6483
6484 /* Cache disk information. */
6485 pDisk->cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
6486
6487 /* Cache PCHS geometry. */
6488 rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
6489 &pDisk->PCHSGeometry);
6490 if (RT_FAILURE(rc2))
6491 {
6492 pDisk->PCHSGeometry.cCylinders = 0;
6493 pDisk->PCHSGeometry.cHeads = 0;
6494 pDisk->PCHSGeometry.cSectors = 0;
6495 }
6496 else
6497 {
6498 /* Make sure the PCHS geometry is properly clipped. */
6499 pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
6500 pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
6501 pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
6502 }
6503
6504 /* Cache LCHS geometry. */
6505 rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
6506 &pDisk->LCHSGeometry);
6507 if (RT_FAILURE(rc2))
6508 {
6509 pDisk->LCHSGeometry.cCylinders = 0;
6510 pDisk->LCHSGeometry.cHeads = 0;
6511 pDisk->LCHSGeometry.cSectors = 0;
6512 }
6513 else
6514 {
6515 /* Make sure the LCHS geometry is properly clipped. */
6516 pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
6517 pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
6518 }
6519 } while (0);
6520
6521 if (RT_UNLIKELY(fLockWrite))
6522 {
6523 rc2 = vdThreadFinishWrite(pDisk);
6524 AssertRC(rc2);
6525 }
6526
6527 LogFlowFunc(("returns %Rrc\n", rc));
6528 return rc;
6529}
6530
6531/**
6532 * Closes the currently opened cache image file in HDD container.
6533 *
6534 * @return VBox status code.
6535 * @return VERR_VD_NOT_OPENED if no cache is opened in HDD container.
6536 * @param pDisk Pointer to HDD container.
6537 * @param fDelete If true, delete the image from the host disk.
6538 */
6539VBOXDDU_DECL(int) VDCacheClose(PVBOXHDD pDisk, bool fDelete)
6540{
6541 int rc = VINF_SUCCESS;
6542 int rc2;
6543 bool fLockWrite = false;
6544 PVDCACHE pCache = NULL;
6545
6546 LogFlowFunc(("pDisk=%#p fDelete=%d\n", pDisk, fDelete));
6547
6548 do
6549 {
6550 /* sanity check */
6551 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
6552 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6553
6554 rc2 = vdThreadStartWrite(pDisk);
6555 AssertRC(rc2);
6556 fLockWrite = true;
6557
6558 AssertPtrBreakStmt(pDisk->pCache, rc = VERR_VD_CACHE_NOT_FOUND);
6559
6560 pCache = pDisk->pCache;
6561 pDisk->pCache = NULL;
6562
6563 pCache->Backend->pfnClose(pCache->pBackendData, fDelete);
6564 if (pCache->pszFilename)
6565 RTStrFree(pCache->pszFilename);
6566 RTMemFree(pCache);
6567 } while (0);
6568
6569 if (RT_LIKELY(fLockWrite))
6570 {
6571 rc2 = vdThreadFinishWrite(pDisk);
6572 AssertRC(rc2);
6573 }
6574
6575 LogFlowFunc(("returns %Rrc\n", rc));
6576 return rc;
6577}
6578
6579/**
6580 * Closes all opened image files in HDD container.
6581 *
6582 * @returns VBox status code.
6583 * @param pDisk Pointer to HDD container.
6584 */
6585VBOXDDU_DECL(int) VDCloseAll(PVBOXHDD pDisk)
6586{
6587 int rc = VINF_SUCCESS;
6588 int rc2;
6589 bool fLockWrite = false;
6590
6591 LogFlowFunc(("pDisk=%#p\n", pDisk));
6592 do
6593 {
6594 /* sanity check */
6595 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
6596 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6597
6598 /* Lock the entire operation. */
6599 rc2 = vdThreadStartWrite(pDisk);
6600 AssertRC(rc2);
6601 fLockWrite = true;
6602
6603 PVDCACHE pCache = pDisk->pCache;
6604 if (pCache)
6605 {
6606 rc2 = pCache->Backend->pfnClose(pCache->pBackendData, false);
6607 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
6608 rc = rc2;
6609
6610 if (pCache->pszFilename)
6611 RTStrFree(pCache->pszFilename);
6612 RTMemFree(pCache);
6613 }
6614
6615 PVDIMAGE pImage = pDisk->pLast;
6616 while (VALID_PTR(pImage))
6617 {
6618 PVDIMAGE pPrev = pImage->pPrev;
6619 /* Remove image from list of opened images. */
6620 vdRemoveImageFromList(pDisk, pImage);
6621 /* Close image. */
6622 rc2 = pImage->Backend->pfnClose(pImage->pBackendData, false);
6623 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
6624 rc = rc2;
6625 /* Free remaining resources related to the image. */
6626 RTStrFree(pImage->pszFilename);
6627 RTMemFree(pImage);
6628 pImage = pPrev;
6629 }
6630 Assert(!VALID_PTR(pDisk->pLast));
6631 } while (0);
6632
6633 if (RT_UNLIKELY(fLockWrite))
6634 {
6635 rc2 = vdThreadFinishWrite(pDisk);
6636 AssertRC(rc2);
6637 }
6638
6639 LogFlowFunc(("returns %Rrc\n", rc));
6640 return rc;
6641}
6642
6643/**
6644 * Read data from virtual HDD.
6645 *
6646 * @returns VBox status code.
6647 * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
6648 * @param pDisk Pointer to HDD container.
6649 * @param uOffset Offset of first reading byte from start of disk.
6650 * @param pvBuf Pointer to buffer for reading data.
6651 * @param cbRead Number of bytes to read.
6652 */
6653VBOXDDU_DECL(int) VDRead(PVBOXHDD pDisk, uint64_t uOffset, void *pvBuf,
6654 size_t cbRead)
6655{
6656 int rc = VINF_SUCCESS;
6657 int rc2;
6658 bool fLockRead = false;
6659
6660 LogFlowFunc(("pDisk=%#p uOffset=%llu pvBuf=%p cbRead=%zu\n",
6661 pDisk, uOffset, pvBuf, cbRead));
6662 do
6663 {
6664 /* sanity check */
6665 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
6666 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6667
6668 /* Check arguments. */
6669 AssertMsgBreakStmt(VALID_PTR(pvBuf),
6670 ("pvBuf=%#p\n", pvBuf),
6671 rc = VERR_INVALID_PARAMETER);
6672 AssertMsgBreakStmt(cbRead,
6673 ("cbRead=%zu\n", cbRead),
6674 rc = VERR_INVALID_PARAMETER);
6675
6676 rc2 = vdThreadStartRead(pDisk);
6677 AssertRC(rc2);
6678 fLockRead = true;
6679
6680 AssertMsgBreakStmt(uOffset + cbRead <= pDisk->cbSize,
6681 ("uOffset=%llu cbRead=%zu pDisk->cbSize=%llu\n",
6682 uOffset, cbRead, pDisk->cbSize),
6683 rc = VERR_INVALID_PARAMETER);
6684
6685 PVDIMAGE pImage = pDisk->pLast;
6686 AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
6687
6688 rc = vdReadHelper(pDisk, pImage, NULL, uOffset, pvBuf, cbRead,
6689 true /* fZeroFreeBlocks */,
6690 true /* fUpdateCache */);
6691 } while (0);
6692
6693 if (RT_UNLIKELY(fLockRead))
6694 {
6695 rc2 = vdThreadFinishRead(pDisk);
6696 AssertRC(rc2);
6697 }
6698
6699 LogFlowFunc(("returns %Rrc\n", rc));
6700 return rc;
6701}
6702
6703/**
6704 * Write data to virtual HDD.
6705 *
6706 * @returns VBox status code.
6707 * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
6708 * @param pDisk Pointer to HDD container.
6709 * @param uOffset Offset of the first byte being
6710 * written from start of disk.
6711 * @param pvBuf Pointer to buffer for writing data.
6712 * @param cbWrite Number of bytes to write.
6713 */
6714VBOXDDU_DECL(int) VDWrite(PVBOXHDD pDisk, uint64_t uOffset, const void *pvBuf,
6715 size_t cbWrite)
6716{
6717 int rc = VINF_SUCCESS;
6718 int rc2;
6719 bool fLockWrite = false;
6720
6721 LogFlowFunc(("pDisk=%#p uOffset=%llu pvBuf=%p cbWrite=%zu\n",
6722 pDisk, uOffset, pvBuf, cbWrite));
6723 do
6724 {
6725 /* sanity check */
6726 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
6727 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6728
6729 /* Check arguments. */
6730 AssertMsgBreakStmt(VALID_PTR(pvBuf),
6731 ("pvBuf=%#p\n", pvBuf),
6732 rc = VERR_INVALID_PARAMETER);
6733 AssertMsgBreakStmt(cbWrite,
6734 ("cbWrite=%zu\n", cbWrite),
6735 rc = VERR_INVALID_PARAMETER);
6736
6737 rc2 = vdThreadStartWrite(pDisk);
6738 AssertRC(rc2);
6739 fLockWrite = true;
6740
6741 AssertMsgBreakStmt(uOffset + cbWrite <= pDisk->cbSize,
6742 ("uOffset=%llu cbWrite=%zu pDisk->cbSize=%llu\n",
6743 uOffset, cbWrite, pDisk->cbSize),
6744 rc = VERR_INVALID_PARAMETER);
6745
6746 PVDIMAGE pImage = pDisk->pLast;
6747 AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
6748
6749 vdSetModifiedFlag(pDisk);
6750 rc = vdWriteHelper(pDisk, pImage, NULL, uOffset, pvBuf, cbWrite,
6751 true /* fUpdateCache */);
6752 if (RT_FAILURE(rc))
6753 break;
6754
6755 /* If there is a merge (in the direction towards a parent) running
6756 * concurrently then we have to also "relay" the write to this parent,
6757 * as the merge position might be already past the position where
6758 * this write is going. The "context" of the write can come from the
6759 * natural chain, since merging either already did or will take care
6760 * of the "other" content which is might be needed to fill the block
6761 * to a full allocation size. The cache doesn't need to be touched
6762 * as this write is covered by the previous one. */
6763 if (RT_UNLIKELY(pDisk->pImageRelay))
6764 rc = vdWriteHelper(pDisk, pDisk->pImageRelay, NULL, uOffset,
6765 pvBuf, cbWrite, false /* fUpdateCache */);
6766 } while (0);
6767
6768 if (RT_UNLIKELY(fLockWrite))
6769 {
6770 rc2 = vdThreadFinishWrite(pDisk);
6771 AssertRC(rc2);
6772 }
6773
6774 LogFlowFunc(("returns %Rrc\n", rc));
6775 return rc;
6776}
6777
6778/**
6779 * Make sure the on disk representation of a virtual HDD is up to date.
6780 *
6781 * @returns VBox status code.
6782 * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
6783 * @param pDisk Pointer to HDD container.
6784 */
6785VBOXDDU_DECL(int) VDFlush(PVBOXHDD pDisk)
6786{
6787 int rc = VINF_SUCCESS;
6788 int rc2;
6789 bool fLockWrite = false;
6790
6791 LogFlowFunc(("pDisk=%#p\n", pDisk));
6792 do
6793 {
6794 /* sanity check */
6795 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
6796 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6797
6798 rc2 = vdThreadStartWrite(pDisk);
6799 AssertRC(rc2);
6800 fLockWrite = true;
6801
6802 PVDIMAGE pImage = pDisk->pLast;
6803 AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
6804
6805 vdResetModifiedFlag(pDisk);
6806 rc = pImage->Backend->pfnFlush(pImage->pBackendData);
6807
6808 if ( RT_SUCCESS(rc)
6809 && pDisk->pCache)
6810 rc = pDisk->pCache->Backend->pfnFlush(pDisk->pCache->pBackendData);
6811 } while (0);
6812
6813 if (RT_UNLIKELY(fLockWrite))
6814 {
6815 rc2 = vdThreadFinishWrite(pDisk);
6816 AssertRC(rc2);
6817 }
6818
6819 LogFlowFunc(("returns %Rrc\n", rc));
6820 return rc;
6821}
6822
6823/**
6824 * Get number of opened images in HDD container.
6825 *
6826 * @returns Number of opened images for HDD container. 0 if no images have been opened.
6827 * @param pDisk Pointer to HDD container.
6828 */
6829VBOXDDU_DECL(unsigned) VDGetCount(PVBOXHDD pDisk)
6830{
6831 unsigned cImages;
6832 int rc2;
6833 bool fLockRead = false;
6834
6835 LogFlowFunc(("pDisk=%#p\n", pDisk));
6836 do
6837 {
6838 /* sanity check */
6839 AssertPtrBreakStmt(pDisk, cImages = 0);
6840 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6841
6842 rc2 = vdThreadStartRead(pDisk);
6843 AssertRC(rc2);
6844 fLockRead = true;
6845
6846 cImages = pDisk->cImages;
6847 } while (0);
6848
6849 if (RT_UNLIKELY(fLockRead))
6850 {
6851 rc2 = vdThreadFinishRead(pDisk);
6852 AssertRC(rc2);
6853 }
6854
6855 LogFlowFunc(("returns %u\n", cImages));
6856 return cImages;
6857}
6858
6859/**
6860 * Get read/write mode of HDD container.
6861 *
6862 * @returns Virtual disk ReadOnly status.
6863 * @returns true if no image is opened in HDD container.
6864 * @param pDisk Pointer to HDD container.
6865 */
6866VBOXDDU_DECL(bool) VDIsReadOnly(PVBOXHDD pDisk)
6867{
6868 bool fReadOnly;
6869 int rc2;
6870 bool fLockRead = false;
6871
6872 LogFlowFunc(("pDisk=%#p\n", pDisk));
6873 do
6874 {
6875 /* sanity check */
6876 AssertPtrBreakStmt(pDisk, fReadOnly = false);
6877 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6878
6879 rc2 = vdThreadStartRead(pDisk);
6880 AssertRC(rc2);
6881 fLockRead = true;
6882
6883 PVDIMAGE pImage = pDisk->pLast;
6884 AssertPtrBreakStmt(pImage, fReadOnly = true);
6885
6886 unsigned uOpenFlags;
6887 uOpenFlags = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pBackendData);
6888 fReadOnly = !!(uOpenFlags & VD_OPEN_FLAGS_READONLY);
6889 } while (0);
6890
6891 if (RT_UNLIKELY(fLockRead))
6892 {
6893 rc2 = vdThreadFinishRead(pDisk);
6894 AssertRC(rc2);
6895 }
6896
6897 LogFlowFunc(("returns %d\n", fReadOnly));
6898 return fReadOnly;
6899}
6900
6901/**
6902 * Get total capacity of an image in HDD container.
6903 *
6904 * @returns Virtual disk size in bytes.
6905 * @returns 0 if no image with specified number was not opened.
6906 * @param pDisk Pointer to HDD container.
6907 * @param nImage Image number, counts from 0. 0 is always base image of container.
6908 */
6909VBOXDDU_DECL(uint64_t) VDGetSize(PVBOXHDD pDisk, unsigned nImage)
6910{
6911 uint64_t cbSize;
6912 int rc2;
6913 bool fLockRead = false;
6914
6915 LogFlowFunc(("pDisk=%#p nImage=%u\n", pDisk, nImage));
6916 do
6917 {
6918 /* sanity check */
6919 AssertPtrBreakStmt(pDisk, cbSize = 0);
6920 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6921
6922 rc2 = vdThreadStartRead(pDisk);
6923 AssertRC(rc2);
6924 fLockRead = true;
6925
6926 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
6927 AssertPtrBreakStmt(pImage, cbSize = 0);
6928 cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
6929 } while (0);
6930
6931 if (RT_UNLIKELY(fLockRead))
6932 {
6933 rc2 = vdThreadFinishRead(pDisk);
6934 AssertRC(rc2);
6935 }
6936
6937 LogFlowFunc(("returns %llu\n", cbSize));
6938 return cbSize;
6939}
6940
6941/**
6942 * Get total file size of an image in HDD container.
6943 *
6944 * @returns Virtual disk size in bytes.
6945 * @returns 0 if no image is opened in HDD container.
6946 * @param pDisk Pointer to HDD container.
6947 * @param nImage Image number, counts from 0. 0 is always base image of container.
6948 */
6949VBOXDDU_DECL(uint64_t) VDGetFileSize(PVBOXHDD pDisk, unsigned nImage)
6950{
6951 uint64_t cbSize;
6952 int rc2;
6953 bool fLockRead = false;
6954
6955 LogFlowFunc(("pDisk=%#p nImage=%u\n", pDisk, nImage));
6956 do
6957 {
6958 /* sanity check */
6959 AssertPtrBreakStmt(pDisk, cbSize = 0);
6960 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6961
6962 rc2 = vdThreadStartRead(pDisk);
6963 AssertRC(rc2);
6964 fLockRead = true;
6965
6966 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
6967 AssertPtrBreakStmt(pImage, cbSize = 0);
6968 cbSize = pImage->Backend->pfnGetFileSize(pImage->pBackendData);
6969 } while (0);
6970
6971 if (RT_UNLIKELY(fLockRead))
6972 {
6973 rc2 = vdThreadFinishRead(pDisk);
6974 AssertRC(rc2);
6975 }
6976
6977 LogFlowFunc(("returns %llu\n", cbSize));
6978 return cbSize;
6979}
6980
6981/**
6982 * Get virtual disk PCHS geometry stored in HDD container.
6983 *
6984 * @returns VBox status code.
6985 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
6986 * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
6987 * @param pDisk Pointer to HDD container.
6988 * @param nImage Image number, counts from 0. 0 is always base image of container.
6989 * @param pPCHSGeometry Where to store PCHS geometry. Not NULL.
6990 */
6991VBOXDDU_DECL(int) VDGetPCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
6992 PVDGEOMETRY pPCHSGeometry)
6993{
6994 int rc = VINF_SUCCESS;
6995 int rc2;
6996 bool fLockRead = false;
6997
6998 LogFlowFunc(("pDisk=%#p nImage=%u pPCHSGeometry=%#p\n",
6999 pDisk, nImage, pPCHSGeometry));
7000 do
7001 {
7002 /* sanity check */
7003 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7004 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7005
7006 /* Check arguments. */
7007 AssertMsgBreakStmt(VALID_PTR(pPCHSGeometry),
7008 ("pPCHSGeometry=%#p\n", pPCHSGeometry),
7009 rc = VERR_INVALID_PARAMETER);
7010
7011 rc2 = vdThreadStartRead(pDisk);
7012 AssertRC(rc2);
7013 fLockRead = true;
7014
7015 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7016 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7017
7018 if (pImage == pDisk->pLast)
7019 {
7020 /* Use cached information if possible. */
7021 if (pDisk->PCHSGeometry.cCylinders != 0)
7022 *pPCHSGeometry = pDisk->PCHSGeometry;
7023 else
7024 rc = VERR_VD_GEOMETRY_NOT_SET;
7025 }
7026 else
7027 rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
7028 pPCHSGeometry);
7029 } while (0);
7030
7031 if (RT_UNLIKELY(fLockRead))
7032 {
7033 rc2 = vdThreadFinishRead(pDisk);
7034 AssertRC(rc2);
7035 }
7036
7037 LogFlowFunc(("%Rrc (PCHS=%u/%u/%u)\n", rc,
7038 pDisk->PCHSGeometry.cCylinders, pDisk->PCHSGeometry.cHeads,
7039 pDisk->PCHSGeometry.cSectors));
7040 return rc;
7041}
7042
7043/**
7044 * Store virtual disk PCHS geometry in HDD container.
7045 *
7046 * Note that in case of unrecoverable error all images in HDD container will be closed.
7047 *
7048 * @returns VBox status code.
7049 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7050 * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
7051 * @param pDisk Pointer to HDD container.
7052 * @param nImage Image number, counts from 0. 0 is always base image of container.
7053 * @param pPCHSGeometry Where to load PCHS geometry from. Not NULL.
7054 */
7055VBOXDDU_DECL(int) VDSetPCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
7056 PCVDGEOMETRY pPCHSGeometry)
7057{
7058 int rc = VINF_SUCCESS;
7059 int rc2;
7060 bool fLockWrite = false;
7061
7062 LogFlowFunc(("pDisk=%#p nImage=%u pPCHSGeometry=%#p PCHS=%u/%u/%u\n",
7063 pDisk, nImage, pPCHSGeometry, pPCHSGeometry->cCylinders,
7064 pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
7065 do
7066 {
7067 /* sanity check */
7068 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7069 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7070
7071 /* Check arguments. */
7072 AssertMsgBreakStmt( VALID_PTR(pPCHSGeometry)
7073 && pPCHSGeometry->cHeads <= 16
7074 && pPCHSGeometry->cSectors <= 63,
7075 ("pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pPCHSGeometry,
7076 pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
7077 pPCHSGeometry->cSectors),
7078 rc = VERR_INVALID_PARAMETER);
7079
7080 rc2 = vdThreadStartWrite(pDisk);
7081 AssertRC(rc2);
7082 fLockWrite = true;
7083
7084 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7085 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7086
7087 if (pImage == pDisk->pLast)
7088 {
7089 if ( pPCHSGeometry->cCylinders != pDisk->PCHSGeometry.cCylinders
7090 || pPCHSGeometry->cHeads != pDisk->PCHSGeometry.cHeads
7091 || pPCHSGeometry->cSectors != pDisk->PCHSGeometry.cSectors)
7092 {
7093 /* Only update geometry if it is changed. Avoids similar checks
7094 * in every backend. Most of the time the new geometry is set
7095 * to the previous values, so no need to go through the hassle
7096 * of updating an image which could be opened in read-only mode
7097 * right now. */
7098 rc = pImage->Backend->pfnSetPCHSGeometry(pImage->pBackendData,
7099 pPCHSGeometry);
7100
7101 /* Cache new geometry values in any case. */
7102 rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
7103 &pDisk->PCHSGeometry);
7104 if (RT_FAILURE(rc2))
7105 {
7106 pDisk->PCHSGeometry.cCylinders = 0;
7107 pDisk->PCHSGeometry.cHeads = 0;
7108 pDisk->PCHSGeometry.cSectors = 0;
7109 }
7110 else
7111 {
7112 /* Make sure the CHS geometry is properly clipped. */
7113 pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 255);
7114 pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
7115 }
7116 }
7117 }
7118 else
7119 {
7120 VDGEOMETRY PCHS;
7121 rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
7122 &PCHS);
7123 if ( RT_FAILURE(rc)
7124 || pPCHSGeometry->cCylinders != PCHS.cCylinders
7125 || pPCHSGeometry->cHeads != PCHS.cHeads
7126 || pPCHSGeometry->cSectors != PCHS.cSectors)
7127 {
7128 /* Only update geometry if it is changed. Avoids similar checks
7129 * in every backend. Most of the time the new geometry is set
7130 * to the previous values, so no need to go through the hassle
7131 * of updating an image which could be opened in read-only mode
7132 * right now. */
7133 rc = pImage->Backend->pfnSetPCHSGeometry(pImage->pBackendData,
7134 pPCHSGeometry);
7135 }
7136 }
7137 } while (0);
7138
7139 if (RT_UNLIKELY(fLockWrite))
7140 {
7141 rc2 = vdThreadFinishWrite(pDisk);
7142 AssertRC(rc2);
7143 }
7144
7145 LogFlowFunc(("returns %Rrc\n", rc));
7146 return rc;
7147}
7148
7149/**
7150 * Get virtual disk LCHS geometry stored in HDD container.
7151 *
7152 * @returns VBox status code.
7153 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7154 * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
7155 * @param pDisk Pointer to HDD container.
7156 * @param nImage Image number, counts from 0. 0 is always base image of container.
7157 * @param pLCHSGeometry Where to store LCHS geometry. Not NULL.
7158 */
7159VBOXDDU_DECL(int) VDGetLCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
7160 PVDGEOMETRY pLCHSGeometry)
7161{
7162 int rc = VINF_SUCCESS;
7163 int rc2;
7164 bool fLockRead = false;
7165
7166 LogFlowFunc(("pDisk=%#p nImage=%u pLCHSGeometry=%#p\n",
7167 pDisk, nImage, pLCHSGeometry));
7168 do
7169 {
7170 /* sanity check */
7171 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7172 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7173
7174 /* Check arguments. */
7175 AssertMsgBreakStmt(VALID_PTR(pLCHSGeometry),
7176 ("pLCHSGeometry=%#p\n", pLCHSGeometry),
7177 rc = VERR_INVALID_PARAMETER);
7178
7179 rc2 = vdThreadStartRead(pDisk);
7180 AssertRC(rc2);
7181 fLockRead = true;
7182
7183 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7184 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7185
7186 if (pImage == pDisk->pLast)
7187 {
7188 /* Use cached information if possible. */
7189 if (pDisk->LCHSGeometry.cCylinders != 0)
7190 *pLCHSGeometry = pDisk->LCHSGeometry;
7191 else
7192 rc = VERR_VD_GEOMETRY_NOT_SET;
7193 }
7194 else
7195 rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
7196 pLCHSGeometry);
7197 } while (0);
7198
7199 if (RT_UNLIKELY(fLockRead))
7200 {
7201 rc2 = vdThreadFinishRead(pDisk);
7202 AssertRC(rc2);
7203 }
7204
7205 LogFlowFunc((": %Rrc (LCHS=%u/%u/%u)\n", rc,
7206 pDisk->LCHSGeometry.cCylinders, pDisk->LCHSGeometry.cHeads,
7207 pDisk->LCHSGeometry.cSectors));
7208 return rc;
7209}
7210
7211/**
7212 * Store virtual disk LCHS geometry in HDD container.
7213 *
7214 * Note that in case of unrecoverable error all images in HDD container will be closed.
7215 *
7216 * @returns VBox status code.
7217 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7218 * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
7219 * @param pDisk Pointer to HDD container.
7220 * @param nImage Image number, counts from 0. 0 is always base image of container.
7221 * @param pLCHSGeometry Where to load LCHS geometry from. Not NULL.
7222 */
7223VBOXDDU_DECL(int) VDSetLCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
7224 PCVDGEOMETRY pLCHSGeometry)
7225{
7226 int rc = VINF_SUCCESS;
7227 int rc2;
7228 bool fLockWrite = false;
7229
7230 LogFlowFunc(("pDisk=%#p nImage=%u pLCHSGeometry=%#p LCHS=%u/%u/%u\n",
7231 pDisk, nImage, pLCHSGeometry, pLCHSGeometry->cCylinders,
7232 pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
7233 do
7234 {
7235 /* sanity check */
7236 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7237 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7238
7239 /* Check arguments. */
7240 AssertMsgBreakStmt( VALID_PTR(pLCHSGeometry)
7241 && pLCHSGeometry->cHeads <= 255
7242 && pLCHSGeometry->cSectors <= 63,
7243 ("pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pLCHSGeometry,
7244 pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads,
7245 pLCHSGeometry->cSectors),
7246 rc = VERR_INVALID_PARAMETER);
7247
7248 rc2 = vdThreadStartWrite(pDisk);
7249 AssertRC(rc2);
7250 fLockWrite = true;
7251
7252 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7253 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7254
7255 if (pImage == pDisk->pLast)
7256 {
7257 if ( pLCHSGeometry->cCylinders != pDisk->LCHSGeometry.cCylinders
7258 || pLCHSGeometry->cHeads != pDisk->LCHSGeometry.cHeads
7259 || pLCHSGeometry->cSectors != pDisk->LCHSGeometry.cSectors)
7260 {
7261 /* Only update geometry if it is changed. Avoids similar checks
7262 * in every backend. Most of the time the new geometry is set
7263 * to the previous values, so no need to go through the hassle
7264 * of updating an image which could be opened in read-only mode
7265 * right now. */
7266 rc = pImage->Backend->pfnSetLCHSGeometry(pImage->pBackendData,
7267 pLCHSGeometry);
7268
7269 /* Cache new geometry values in any case. */
7270 rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
7271 &pDisk->LCHSGeometry);
7272 if (RT_FAILURE(rc2))
7273 {
7274 pDisk->LCHSGeometry.cCylinders = 0;
7275 pDisk->LCHSGeometry.cHeads = 0;
7276 pDisk->LCHSGeometry.cSectors = 0;
7277 }
7278 else
7279 {
7280 /* Make sure the CHS geometry is properly clipped. */
7281 pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
7282 pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
7283 }
7284 }
7285 }
7286 else
7287 {
7288 VDGEOMETRY LCHS;
7289 rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
7290 &LCHS);
7291 if ( RT_FAILURE(rc)
7292 || pLCHSGeometry->cCylinders != LCHS.cCylinders
7293 || pLCHSGeometry->cHeads != LCHS.cHeads
7294 || pLCHSGeometry->cSectors != LCHS.cSectors)
7295 {
7296 /* Only update geometry if it is changed. Avoids similar checks
7297 * in every backend. Most of the time the new geometry is set
7298 * to the previous values, so no need to go through the hassle
7299 * of updating an image which could be opened in read-only mode
7300 * right now. */
7301 rc = pImage->Backend->pfnSetLCHSGeometry(pImage->pBackendData,
7302 pLCHSGeometry);
7303 }
7304 }
7305 } while (0);
7306
7307 if (RT_UNLIKELY(fLockWrite))
7308 {
7309 rc2 = vdThreadFinishWrite(pDisk);
7310 AssertRC(rc2);
7311 }
7312
7313 LogFlowFunc(("returns %Rrc\n", rc));
7314 return rc;
7315}
7316
7317/**
7318 * Get version of image in HDD container.
7319 *
7320 * @returns VBox status code.
7321 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7322 * @param pDisk Pointer to HDD container.
7323 * @param nImage Image number, counts from 0. 0 is always base image of container.
7324 * @param puVersion Where to store the image version.
7325 */
7326VBOXDDU_DECL(int) VDGetVersion(PVBOXHDD pDisk, unsigned nImage,
7327 unsigned *puVersion)
7328{
7329 int rc = VINF_SUCCESS;
7330 int rc2;
7331 bool fLockRead = false;
7332
7333 LogFlowFunc(("pDisk=%#p nImage=%u puVersion=%#p\n",
7334 pDisk, nImage, puVersion));
7335 do
7336 {
7337 /* sanity check */
7338 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7339 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7340
7341 /* Check arguments. */
7342 AssertMsgBreakStmt(VALID_PTR(puVersion),
7343 ("puVersion=%#p\n", puVersion),
7344 rc = VERR_INVALID_PARAMETER);
7345
7346 rc2 = vdThreadStartRead(pDisk);
7347 AssertRC(rc2);
7348 fLockRead = true;
7349
7350 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7351 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7352
7353 *puVersion = pImage->Backend->pfnGetVersion(pImage->pBackendData);
7354 } while (0);
7355
7356 if (RT_UNLIKELY(fLockRead))
7357 {
7358 rc2 = vdThreadFinishRead(pDisk);
7359 AssertRC(rc2);
7360 }
7361
7362 LogFlowFunc(("returns %Rrc uVersion=%#x\n", rc, *puVersion));
7363 return rc;
7364}
7365
7366/**
7367 * List the capabilities of image backend in HDD container.
7368 *
7369 * @returns VBox status code.
7370 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7371 * @param pDisk Pointer to the HDD container.
7372 * @param nImage Image number, counts from 0. 0 is always base image of container.
7373 * @param pbackendInfo Where to store the backend information.
7374 */
7375VBOXDDU_DECL(int) VDBackendInfoSingle(PVBOXHDD pDisk, unsigned nImage,
7376 PVDBACKENDINFO pBackendInfo)
7377{
7378 int rc = VINF_SUCCESS;
7379 int rc2;
7380 bool fLockRead = false;
7381
7382 LogFlowFunc(("pDisk=%#p nImage=%u pBackendInfo=%#p\n",
7383 pDisk, nImage, pBackendInfo));
7384 do
7385 {
7386 /* sanity check */
7387 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7388 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7389
7390 /* Check arguments. */
7391 AssertMsgBreakStmt(VALID_PTR(pBackendInfo),
7392 ("pBackendInfo=%#p\n", pBackendInfo),
7393 rc = VERR_INVALID_PARAMETER);
7394
7395 rc2 = vdThreadStartRead(pDisk);
7396 AssertRC(rc2);
7397 fLockRead = true;
7398
7399 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7400 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7401
7402 pBackendInfo->pszBackend = pImage->Backend->pszBackendName;
7403 pBackendInfo->uBackendCaps = pImage->Backend->uBackendCaps;
7404 pBackendInfo->paFileExtensions = pImage->Backend->paFileExtensions;
7405 pBackendInfo->paConfigInfo = pImage->Backend->paConfigInfo;
7406 } while (0);
7407
7408 if (RT_UNLIKELY(fLockRead))
7409 {
7410 rc2 = vdThreadFinishRead(pDisk);
7411 AssertRC(rc2);
7412 }
7413
7414 LogFlowFunc(("returns %Rrc\n", rc));
7415 return rc;
7416}
7417
7418/**
7419 * Get flags of image in HDD container.
7420 *
7421 * @returns VBox status code.
7422 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7423 * @param pDisk Pointer to HDD container.
7424 * @param nImage Image number, counts from 0. 0 is always base image of container.
7425 * @param puImageFlags Where to store the image flags.
7426 */
7427VBOXDDU_DECL(int) VDGetImageFlags(PVBOXHDD pDisk, unsigned nImage,
7428 unsigned *puImageFlags)
7429{
7430 int rc = VINF_SUCCESS;
7431 int rc2;
7432 bool fLockRead = false;
7433
7434 LogFlowFunc(("pDisk=%#p nImage=%u puImageFlags=%#p\n",
7435 pDisk, nImage, puImageFlags));
7436 do
7437 {
7438 /* sanity check */
7439 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7440 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7441
7442 /* Check arguments. */
7443 AssertMsgBreakStmt(VALID_PTR(puImageFlags),
7444 ("puImageFlags=%#p\n", puImageFlags),
7445 rc = VERR_INVALID_PARAMETER);
7446
7447 rc2 = vdThreadStartRead(pDisk);
7448 AssertRC(rc2);
7449 fLockRead = true;
7450
7451 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7452 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7453
7454 *puImageFlags = pImage->uImageFlags;
7455 } while (0);
7456
7457 if (RT_UNLIKELY(fLockRead))
7458 {
7459 rc2 = vdThreadFinishRead(pDisk);
7460 AssertRC(rc2);
7461 }
7462
7463 LogFlowFunc(("returns %Rrc uImageFlags=%#x\n", rc, *puImageFlags));
7464 return rc;
7465}
7466
7467/**
7468 * Get open flags of image in HDD container.
7469 *
7470 * @returns VBox status code.
7471 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7472 * @param pDisk Pointer to HDD container.
7473 * @param nImage Image number, counts from 0. 0 is always base image of container.
7474 * @param puOpenFlags Where to store the image open flags.
7475 */
7476VBOXDDU_DECL(int) VDGetOpenFlags(PVBOXHDD pDisk, unsigned nImage,
7477 unsigned *puOpenFlags)
7478{
7479 int rc = VINF_SUCCESS;
7480 int rc2;
7481 bool fLockRead = false;
7482
7483 LogFlowFunc(("pDisk=%#p nImage=%u puOpenFlags=%#p\n",
7484 pDisk, nImage, puOpenFlags));
7485 do
7486 {
7487 /* sanity check */
7488 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7489 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7490
7491 /* Check arguments. */
7492 AssertMsgBreakStmt(VALID_PTR(puOpenFlags),
7493 ("puOpenFlags=%#p\n", puOpenFlags),
7494 rc = VERR_INVALID_PARAMETER);
7495
7496 rc2 = vdThreadStartRead(pDisk);
7497 AssertRC(rc2);
7498 fLockRead = true;
7499
7500 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7501 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7502
7503 *puOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
7504 } while (0);
7505
7506 if (RT_UNLIKELY(fLockRead))
7507 {
7508 rc2 = vdThreadFinishRead(pDisk);
7509 AssertRC(rc2);
7510 }
7511
7512 LogFlowFunc(("returns %Rrc uOpenFlags=%#x\n", rc, *puOpenFlags));
7513 return rc;
7514}
7515
7516/**
7517 * Set open flags of image in HDD container.
7518 * This operation may cause file locking changes and/or files being reopened.
7519 * Note that in case of unrecoverable error all images in HDD container will be closed.
7520 *
7521 * @returns VBox status code.
7522 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7523 * @param pDisk Pointer to HDD container.
7524 * @param nImage Image number, counts from 0. 0 is always base image of container.
7525 * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
7526 */
7527VBOXDDU_DECL(int) VDSetOpenFlags(PVBOXHDD pDisk, unsigned nImage,
7528 unsigned uOpenFlags)
7529{
7530 int rc;
7531 int rc2;
7532 bool fLockWrite = false;
7533
7534 LogFlowFunc(("pDisk=%#p uOpenFlags=%#u\n", pDisk, uOpenFlags));
7535 do
7536 {
7537 /* sanity check */
7538 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7539 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7540
7541 /* Check arguments. */
7542 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
7543 ("uOpenFlags=%#x\n", uOpenFlags),
7544 rc = VERR_INVALID_PARAMETER);
7545
7546 rc2 = vdThreadStartWrite(pDisk);
7547 AssertRC(rc2);
7548 fLockWrite = true;
7549
7550 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7551 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7552
7553 rc = pImage->Backend->pfnSetOpenFlags(pImage->pBackendData,
7554 uOpenFlags);
7555 } while (0);
7556
7557 if (RT_UNLIKELY(fLockWrite))
7558 {
7559 rc2 = vdThreadFinishWrite(pDisk);
7560 AssertRC(rc2);
7561 }
7562
7563 LogFlowFunc(("returns %Rrc\n", rc));
7564 return rc;
7565}
7566
7567/**
7568 * Get base filename of image in HDD container. Some image formats use
7569 * other filenames as well, so don't use this for anything but informational
7570 * purposes.
7571 *
7572 * @returns VBox status code.
7573 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7574 * @returns VERR_BUFFER_OVERFLOW if pszFilename buffer too small to hold filename.
7575 * @param pDisk Pointer to HDD container.
7576 * @param nImage Image number, counts from 0. 0 is always base image of container.
7577 * @param pszFilename Where to store the image file name.
7578 * @param cbFilename Size of buffer pszFilename points to.
7579 */
7580VBOXDDU_DECL(int) VDGetFilename(PVBOXHDD pDisk, unsigned nImage,
7581 char *pszFilename, unsigned cbFilename)
7582{
7583 int rc;
7584 int rc2;
7585 bool fLockRead = false;
7586
7587 LogFlowFunc(("pDisk=%#p nImage=%u pszFilename=%#p cbFilename=%u\n",
7588 pDisk, nImage, pszFilename, cbFilename));
7589 do
7590 {
7591 /* sanity check */
7592 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7593 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7594
7595 /* Check arguments. */
7596 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
7597 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
7598 rc = VERR_INVALID_PARAMETER);
7599 AssertMsgBreakStmt(cbFilename,
7600 ("cbFilename=%u\n", cbFilename),
7601 rc = VERR_INVALID_PARAMETER);
7602
7603 rc2 = vdThreadStartRead(pDisk);
7604 AssertRC(rc2);
7605 fLockRead = true;
7606
7607 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7608 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7609
7610 size_t cb = strlen(pImage->pszFilename);
7611 if (cb <= cbFilename)
7612 {
7613 strcpy(pszFilename, pImage->pszFilename);
7614 rc = VINF_SUCCESS;
7615 }
7616 else
7617 {
7618 strncpy(pszFilename, pImage->pszFilename, cbFilename - 1);
7619 pszFilename[cbFilename - 1] = '\0';
7620 rc = VERR_BUFFER_OVERFLOW;
7621 }
7622 } while (0);
7623
7624 if (RT_UNLIKELY(fLockRead))
7625 {
7626 rc2 = vdThreadFinishRead(pDisk);
7627 AssertRC(rc2);
7628 }
7629
7630 LogFlowFunc(("returns %Rrc, pszFilename=\"%s\"\n", rc, pszFilename));
7631 return rc;
7632}
7633
7634/**
7635 * Get the comment line of image in HDD container.
7636 *
7637 * @returns VBox status code.
7638 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7639 * @returns VERR_BUFFER_OVERFLOW if pszComment buffer too small to hold comment text.
7640 * @param pDisk Pointer to HDD container.
7641 * @param nImage Image number, counts from 0. 0 is always base image of container.
7642 * @param pszComment Where to store the comment string of image. NULL is ok.
7643 * @param cbComment The size of pszComment buffer. 0 is ok.
7644 */
7645VBOXDDU_DECL(int) VDGetComment(PVBOXHDD pDisk, unsigned nImage,
7646 char *pszComment, unsigned cbComment)
7647{
7648 int rc;
7649 int rc2;
7650 bool fLockRead = false;
7651
7652 LogFlowFunc(("pDisk=%#p nImage=%u pszComment=%#p cbComment=%u\n",
7653 pDisk, nImage, pszComment, cbComment));
7654 do
7655 {
7656 /* sanity check */
7657 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7658 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7659
7660 /* Check arguments. */
7661 AssertMsgBreakStmt(VALID_PTR(pszComment),
7662 ("pszComment=%#p \"%s\"\n", pszComment, pszComment),
7663 rc = VERR_INVALID_PARAMETER);
7664 AssertMsgBreakStmt(cbComment,
7665 ("cbComment=%u\n", cbComment),
7666 rc = VERR_INVALID_PARAMETER);
7667
7668 rc2 = vdThreadStartRead(pDisk);
7669 AssertRC(rc2);
7670 fLockRead = true;
7671
7672 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7673 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7674
7675 rc = pImage->Backend->pfnGetComment(pImage->pBackendData, pszComment,
7676 cbComment);
7677 } while (0);
7678
7679 if (RT_UNLIKELY(fLockRead))
7680 {
7681 rc2 = vdThreadFinishRead(pDisk);
7682 AssertRC(rc2);
7683 }
7684
7685 LogFlowFunc(("returns %Rrc, pszComment=\"%s\"\n", rc, pszComment));
7686 return rc;
7687}
7688
7689/**
7690 * Changes the comment line of image in HDD container.
7691 *
7692 * @returns VBox status code.
7693 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7694 * @param pDisk Pointer to HDD container.
7695 * @param nImage Image number, counts from 0. 0 is always base image of container.
7696 * @param pszComment New comment string (UTF-8). NULL is allowed to reset the comment.
7697 */
7698VBOXDDU_DECL(int) VDSetComment(PVBOXHDD pDisk, unsigned nImage,
7699 const char *pszComment)
7700{
7701 int rc;
7702 int rc2;
7703 bool fLockWrite = false;
7704
7705 LogFlowFunc(("pDisk=%#p nImage=%u pszComment=%#p \"%s\"\n",
7706 pDisk, nImage, pszComment, pszComment));
7707 do
7708 {
7709 /* sanity check */
7710 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7711 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7712
7713 /* Check arguments. */
7714 AssertMsgBreakStmt(VALID_PTR(pszComment) || pszComment == NULL,
7715 ("pszComment=%#p \"%s\"\n", pszComment, pszComment),
7716 rc = VERR_INVALID_PARAMETER);
7717
7718 rc2 = vdThreadStartWrite(pDisk);
7719 AssertRC(rc2);
7720 fLockWrite = true;
7721
7722 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7723 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7724
7725 rc = pImage->Backend->pfnSetComment(pImage->pBackendData, pszComment);
7726 } while (0);
7727
7728 if (RT_UNLIKELY(fLockWrite))
7729 {
7730 rc2 = vdThreadFinishWrite(pDisk);
7731 AssertRC(rc2);
7732 }
7733
7734 LogFlowFunc(("returns %Rrc\n", rc));
7735 return rc;
7736}
7737
7738
7739/**
7740 * Get UUID of image in HDD container.
7741 *
7742 * @returns VBox status code.
7743 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7744 * @param pDisk Pointer to HDD container.
7745 * @param nImage Image number, counts from 0. 0 is always base image of container.
7746 * @param pUuid Where to store the image creation UUID.
7747 */
7748VBOXDDU_DECL(int) VDGetUuid(PVBOXHDD pDisk, unsigned nImage, PRTUUID pUuid)
7749{
7750 int rc;
7751 int rc2;
7752 bool fLockRead = false;
7753
7754 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
7755 do
7756 {
7757 /* sanity check */
7758 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7759 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7760
7761 /* Check arguments. */
7762 AssertMsgBreakStmt(VALID_PTR(pUuid),
7763 ("pUuid=%#p\n", pUuid),
7764 rc = VERR_INVALID_PARAMETER);
7765
7766 rc2 = vdThreadStartRead(pDisk);
7767 AssertRC(rc2);
7768 fLockRead = true;
7769
7770 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7771 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7772
7773 rc = pImage->Backend->pfnGetUuid(pImage->pBackendData, pUuid);
7774 } while (0);
7775
7776 if (RT_UNLIKELY(fLockRead))
7777 {
7778 rc2 = vdThreadFinishRead(pDisk);
7779 AssertRC(rc2);
7780 }
7781
7782 LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
7783 return rc;
7784}
7785
7786/**
7787 * Set the image's UUID. Should not be used by normal applications.
7788 *
7789 * @returns VBox status code.
7790 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7791 * @param pDisk Pointer to HDD container.
7792 * @param nImage Image number, counts from 0. 0 is always base image of container.
7793 * @param pUuid New UUID of the image. If NULL, a new UUID is created.
7794 */
7795VBOXDDU_DECL(int) VDSetUuid(PVBOXHDD pDisk, unsigned nImage, PCRTUUID pUuid)
7796{
7797 int rc;
7798 int rc2;
7799 bool fLockWrite = false;
7800
7801 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
7802 pDisk, nImage, pUuid, pUuid));
7803 do
7804 {
7805 /* sanity check */
7806 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7807 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7808
7809 AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
7810 ("pUuid=%#p\n", pUuid),
7811 rc = VERR_INVALID_PARAMETER);
7812
7813 rc2 = vdThreadStartWrite(pDisk);
7814 AssertRC(rc2);
7815 fLockWrite = true;
7816
7817 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7818 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7819
7820 RTUUID Uuid;
7821 if (!pUuid)
7822 {
7823 RTUuidCreate(&Uuid);
7824 pUuid = &Uuid;
7825 }
7826 rc = pImage->Backend->pfnSetUuid(pImage->pBackendData, pUuid);
7827 } while (0);
7828
7829 if (RT_UNLIKELY(fLockWrite))
7830 {
7831 rc2 = vdThreadFinishWrite(pDisk);
7832 AssertRC(rc2);
7833 }
7834
7835 LogFlowFunc(("returns %Rrc\n", rc));
7836 return rc;
7837}
7838
7839/**
7840 * Get last modification UUID of image in HDD container.
7841 *
7842 * @returns VBox status code.
7843 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7844 * @param pDisk Pointer to HDD container.
7845 * @param nImage Image number, counts from 0. 0 is always base image of container.
7846 * @param pUuid Where to store the image modification UUID.
7847 */
7848VBOXDDU_DECL(int) VDGetModificationUuid(PVBOXHDD pDisk, unsigned nImage, PRTUUID pUuid)
7849{
7850 int rc = VINF_SUCCESS;
7851 int rc2;
7852 bool fLockRead = false;
7853
7854 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
7855 do
7856 {
7857 /* sanity check */
7858 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7859 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7860
7861 /* Check arguments. */
7862 AssertMsgBreakStmt(VALID_PTR(pUuid),
7863 ("pUuid=%#p\n", pUuid),
7864 rc = VERR_INVALID_PARAMETER);
7865
7866 rc2 = vdThreadStartRead(pDisk);
7867 AssertRC(rc2);
7868 fLockRead = true;
7869
7870 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7871 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7872
7873 rc = pImage->Backend->pfnGetModificationUuid(pImage->pBackendData,
7874 pUuid);
7875 } while (0);
7876
7877 if (RT_UNLIKELY(fLockRead))
7878 {
7879 rc2 = vdThreadFinishRead(pDisk);
7880 AssertRC(rc2);
7881 }
7882
7883 LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
7884 return rc;
7885}
7886
7887/**
7888 * Set the image's last modification UUID. Should not be used by normal applications.
7889 *
7890 * @returns VBox status code.
7891 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7892 * @param pDisk Pointer to HDD container.
7893 * @param nImage Image number, counts from 0. 0 is always base image of container.
7894 * @param pUuid New modification UUID of the image. If NULL, a new UUID is created.
7895 */
7896VBOXDDU_DECL(int) VDSetModificationUuid(PVBOXHDD pDisk, unsigned nImage, PCRTUUID pUuid)
7897{
7898 int rc;
7899 int rc2;
7900 bool fLockWrite = false;
7901
7902 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
7903 pDisk, nImage, pUuid, pUuid));
7904 do
7905 {
7906 /* sanity check */
7907 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7908 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7909
7910 /* Check arguments. */
7911 AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
7912 ("pUuid=%#p\n", pUuid),
7913 rc = VERR_INVALID_PARAMETER);
7914
7915 rc2 = vdThreadStartWrite(pDisk);
7916 AssertRC(rc2);
7917 fLockWrite = true;
7918
7919 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7920 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7921
7922 RTUUID Uuid;
7923 if (!pUuid)
7924 {
7925 RTUuidCreate(&Uuid);
7926 pUuid = &Uuid;
7927 }
7928 rc = pImage->Backend->pfnSetModificationUuid(pImage->pBackendData,
7929 pUuid);
7930 } while (0);
7931
7932 if (RT_UNLIKELY(fLockWrite))
7933 {
7934 rc2 = vdThreadFinishWrite(pDisk);
7935 AssertRC(rc2);
7936 }
7937
7938 LogFlowFunc(("returns %Rrc\n", rc));
7939 return rc;
7940}
7941
7942/**
7943 * Get parent UUID of image in HDD container.
7944 *
7945 * @returns VBox status code.
7946 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7947 * @param pDisk Pointer to HDD container.
7948 * @param nImage Image number, counts from 0. 0 is always base image of container.
7949 * @param pUuid Where to store the parent image UUID.
7950 */
7951VBOXDDU_DECL(int) VDGetParentUuid(PVBOXHDD pDisk, unsigned nImage,
7952 PRTUUID pUuid)
7953{
7954 int rc = VINF_SUCCESS;
7955 int rc2;
7956 bool fLockRead = false;
7957
7958 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
7959 do
7960 {
7961 /* sanity check */
7962 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7963 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7964
7965 /* Check arguments. */
7966 AssertMsgBreakStmt(VALID_PTR(pUuid),
7967 ("pUuid=%#p\n", pUuid),
7968 rc = VERR_INVALID_PARAMETER);
7969
7970 rc2 = vdThreadStartRead(pDisk);
7971 AssertRC(rc2);
7972 fLockRead = true;
7973
7974 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7975 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7976
7977 rc = pImage->Backend->pfnGetParentUuid(pImage->pBackendData, pUuid);
7978 } while (0);
7979
7980 if (RT_UNLIKELY(fLockRead))
7981 {
7982 rc2 = vdThreadFinishRead(pDisk);
7983 AssertRC(rc2);
7984 }
7985
7986 LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
7987 return rc;
7988}
7989
7990/**
7991 * Set the image's parent UUID. Should not be used by normal applications.
7992 *
7993 * @returns VBox status code.
7994 * @param pDisk Pointer to HDD container.
7995 * @param nImage Image number, counts from 0. 0 is always base image of container.
7996 * @param pUuid New parent UUID of the image. If NULL, a new UUID is created.
7997 */
7998VBOXDDU_DECL(int) VDSetParentUuid(PVBOXHDD pDisk, unsigned nImage,
7999 PCRTUUID pUuid)
8000{
8001 int rc;
8002 int rc2;
8003 bool fLockWrite = false;
8004
8005 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
8006 pDisk, nImage, pUuid, pUuid));
8007 do
8008 {
8009 /* sanity check */
8010 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
8011 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8012
8013 /* Check arguments. */
8014 AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
8015 ("pUuid=%#p\n", pUuid),
8016 rc = VERR_INVALID_PARAMETER);
8017
8018 rc2 = vdThreadStartWrite(pDisk);
8019 AssertRC(rc2);
8020 fLockWrite = true;
8021
8022 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
8023 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
8024
8025 RTUUID Uuid;
8026 if (!pUuid)
8027 {
8028 RTUuidCreate(&Uuid);
8029 pUuid = &Uuid;
8030 }
8031 rc = pImage->Backend->pfnSetParentUuid(pImage->pBackendData, pUuid);
8032 } while (0);
8033
8034 if (RT_UNLIKELY(fLockWrite))
8035 {
8036 rc2 = vdThreadFinishWrite(pDisk);
8037 AssertRC(rc2);
8038 }
8039
8040 LogFlowFunc(("returns %Rrc\n", rc));
8041 return rc;
8042}
8043
8044
8045/**
8046 * Debug helper - dumps all opened images in HDD container into the log file.
8047 *
8048 * @param pDisk Pointer to HDD container.
8049 */
8050VBOXDDU_DECL(void) VDDumpImages(PVBOXHDD pDisk)
8051{
8052 int rc2;
8053 bool fLockRead = false;
8054
8055 do
8056 {
8057 /* sanity check */
8058 AssertPtrBreak(pDisk);
8059 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8060
8061 if (!pDisk->pInterfaceErrorCallbacks || !VALID_PTR(pDisk->pInterfaceErrorCallbacks->pfnMessage))
8062 pDisk->pInterfaceErrorCallbacks->pfnMessage = vdLogMessage;
8063
8064 rc2 = vdThreadStartRead(pDisk);
8065 AssertRC(rc2);
8066 fLockRead = true;
8067
8068 vdMessageWrapper(pDisk, "--- Dumping VD Disk, Images=%u\n", pDisk->cImages);
8069 for (PVDIMAGE pImage = pDisk->pBase; pImage; pImage = pImage->pNext)
8070 {
8071 vdMessageWrapper(pDisk, "Dumping VD image \"%s\" (Backend=%s)\n",
8072 pImage->pszFilename, pImage->Backend->pszBackendName);
8073 pImage->Backend->pfnDump(pImage->pBackendData);
8074 }
8075 } while (0);
8076
8077 if (RT_UNLIKELY(fLockRead))
8078 {
8079 rc2 = vdThreadFinishRead(pDisk);
8080 AssertRC(rc2);
8081 }
8082}
8083
8084
8085VBOXDDU_DECL(int) VDAsyncRead(PVBOXHDD pDisk, uint64_t uOffset, size_t cbRead,
8086 PCRTSGBUF pcSgBuf,
8087 PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
8088 void *pvUser1, void *pvUser2)
8089{
8090 int rc = VERR_VD_BLOCK_FREE;
8091 int rc2;
8092 bool fLockRead = false;
8093 PVDIOCTX pIoCtx = NULL;
8094
8095 LogFlowFunc(("pDisk=%#p uOffset=%llu pcSgBuf=%#p cbRead=%zu pvUser1=%#p pvUser2=%#p\n",
8096 pDisk, uOffset, pcSgBuf, cbRead, pvUser1, pvUser2));
8097
8098 do
8099 {
8100 /* sanity check */
8101 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
8102 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8103
8104 /* Check arguments. */
8105 AssertMsgBreakStmt(cbRead,
8106 ("cbRead=%zu\n", cbRead),
8107 rc = VERR_INVALID_PARAMETER);
8108 AssertMsgBreakStmt(VALID_PTR(pcSgBuf),
8109 ("pcSgBuf=%#p\n", pcSgBuf),
8110 rc = VERR_INVALID_PARAMETER);
8111
8112 rc2 = vdThreadStartRead(pDisk);
8113 AssertRC(rc2);
8114 fLockRead = true;
8115
8116 AssertMsgBreakStmt(uOffset + cbRead <= pDisk->cbSize,
8117 ("uOffset=%llu cbRead=%zu pDisk->cbSize=%llu\n",
8118 uOffset, cbRead, pDisk->cbSize),
8119 rc = VERR_INVALID_PARAMETER);
8120 AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
8121
8122 pIoCtx = vdIoCtxRootAlloc(pDisk, VDIOCTXTXDIR_READ, uOffset,
8123 cbRead, pDisk->pLast, pcSgBuf,
8124 pfnComplete, pvUser1, pvUser2,
8125 NULL, vdReadHelperAsync);
8126 if (!pIoCtx)
8127 {
8128 rc = VERR_NO_MEMORY;
8129 break;
8130 }
8131
8132 rc = vdIoCtxProcess(pIoCtx);
8133 if (rc == VINF_VD_ASYNC_IO_FINISHED)
8134 {
8135 if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
8136 vdIoCtxFree(pDisk, pIoCtx);
8137 else
8138 rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
8139 }
8140 else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
8141 vdIoCtxFree(pDisk, pIoCtx);
8142
8143 } while (0);
8144
8145 if (RT_UNLIKELY(fLockRead) && ( rc == VINF_VD_ASYNC_IO_FINISHED
8146 || rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
8147 {
8148 rc2 = vdThreadFinishRead(pDisk);
8149 AssertRC(rc2);
8150 }
8151
8152 LogFlowFunc(("returns %Rrc\n", rc));
8153 return rc;
8154}
8155
8156
8157VBOXDDU_DECL(int) VDAsyncWrite(PVBOXHDD pDisk, uint64_t uOffset, size_t cbWrite,
8158 PCRTSGBUF pcSgBuf,
8159 PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
8160 void *pvUser1, void *pvUser2)
8161{
8162 int rc;
8163 int rc2;
8164 bool fLockWrite = false;
8165 PVDIOCTX pIoCtx = NULL;
8166
8167 LogFlowFunc(("pDisk=%#p uOffset=%llu cSgBuf=%#p cbWrite=%zu pvUser1=%#p pvUser2=%#p\n",
8168 pDisk, uOffset, pcSgBuf, cbWrite, pvUser1, pvUser2));
8169 do
8170 {
8171 /* sanity check */
8172 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
8173 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8174
8175 /* Check arguments. */
8176 AssertMsgBreakStmt(cbWrite,
8177 ("cbWrite=%zu\n", cbWrite),
8178 rc = VERR_INVALID_PARAMETER);
8179 AssertMsgBreakStmt(VALID_PTR(pcSgBuf),
8180 ("pcSgBuf=%#p\n", pcSgBuf),
8181 rc = VERR_INVALID_PARAMETER);
8182
8183 rc2 = vdThreadStartWrite(pDisk);
8184 AssertRC(rc2);
8185 fLockWrite = true;
8186
8187 AssertMsgBreakStmt(uOffset + cbWrite <= pDisk->cbSize,
8188 ("uOffset=%llu cbWrite=%zu pDisk->cbSize=%llu\n",
8189 uOffset, cbWrite, pDisk->cbSize),
8190 rc = VERR_INVALID_PARAMETER);
8191 AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
8192
8193 pIoCtx = vdIoCtxRootAlloc(pDisk, VDIOCTXTXDIR_WRITE, uOffset,
8194 cbWrite, pDisk->pLast, pcSgBuf,
8195 pfnComplete, pvUser1, pvUser2,
8196 NULL, vdWriteHelperAsync);
8197 if (!pIoCtx)
8198 {
8199 rc = VERR_NO_MEMORY;
8200 break;
8201 }
8202
8203 rc = vdIoCtxProcess(pIoCtx);
8204 if (rc == VINF_VD_ASYNC_IO_FINISHED)
8205 {
8206 if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
8207 vdIoCtxFree(pDisk, pIoCtx);
8208 else
8209 rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
8210 }
8211 else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
8212 vdIoCtxFree(pDisk, pIoCtx);
8213 } while (0);
8214
8215 if (RT_UNLIKELY(fLockWrite) && ( rc == VINF_VD_ASYNC_IO_FINISHED
8216 || rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
8217 {
8218 rc2 = vdThreadFinishWrite(pDisk);
8219 AssertRC(rc2);
8220 }
8221
8222 LogFlowFunc(("returns %Rrc\n", rc));
8223 return rc;
8224}
8225
8226
8227VBOXDDU_DECL(int) VDAsyncFlush(PVBOXHDD pDisk, PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
8228 void *pvUser1, void *pvUser2)
8229{
8230 int rc;
8231 int rc2;
8232 bool fLockWrite = false;
8233 PVDIOCTX pIoCtx = NULL;
8234
8235 LogFlowFunc(("pDisk=%#p\n", pDisk));
8236
8237 do
8238 {
8239 /* sanity check */
8240 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
8241 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8242
8243 rc2 = vdThreadStartWrite(pDisk);
8244 AssertRC(rc2);
8245 fLockWrite = true;
8246
8247 AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
8248
8249 pIoCtx = vdIoCtxRootAlloc(pDisk, VDIOCTXTXDIR_FLUSH, 0,
8250 0, pDisk->pLast, NULL,
8251 pfnComplete, pvUser1, pvUser2,
8252 NULL, vdFlushHelperAsync);
8253 if (!pIoCtx)
8254 {
8255 rc = VERR_NO_MEMORY;
8256 break;
8257 }
8258
8259 rc = vdIoCtxProcess(pIoCtx);
8260 if (rc == VINF_VD_ASYNC_IO_FINISHED)
8261 {
8262 if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
8263 vdIoCtxFree(pDisk, pIoCtx);
8264 else
8265 rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
8266 }
8267 else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
8268 vdIoCtxFree(pDisk, pIoCtx);
8269 } while (0);
8270
8271 if (RT_UNLIKELY(fLockWrite) && ( rc == VINF_VD_ASYNC_IO_FINISHED
8272 || rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
8273 {
8274 rc2 = vdThreadFinishWrite(pDisk);
8275 AssertRC(rc2);
8276 }
8277
8278 LogFlowFunc(("returns %Rrc\n", rc));
8279 return rc;
8280}
8281
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette