VirtualBox

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

Last change on this file since 36240 was 36221, checked in by vboxsync, 14 years ago

Storage: Fix forgotten unlock of the disk

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 295.4 KB
Line 
1/* $Id: VD.cpp 36221 2011-03-09 11:01:08Z 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, pIoCtxFlush, 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 LogFlowFunc(("Parent I/O context completed pIoCtxParent=%#p rcReq=%Rrc\n", pIoCtxParent, pIoCtxParent->rcReq));
2622 pIoCtxParent->Type.Root.pfnComplete(pIoCtxParent->Type.Root.pvUser1,
2623 pIoCtxParent->Type.Root.pvUser2,
2624 pIoCtxParent->rcReq);
2625 vdThreadFinishWrite(pDisk);
2626 vdIoCtxFree(pDisk, pIoCtxParent);
2627 }
2628
2629 /* Process any pending writes if the current request didn't caused another growing. */
2630 RTCritSectEnter(&pDisk->CritSect);
2631
2632 if ( !RTListIsEmpty(&pDisk->ListWriteLocked)
2633 && !vdIoCtxIsDiskLockOwner(pDisk, pIoCtx))
2634 {
2635 RTLISTNODE ListTmp;
2636
2637 LogFlowFunc(("Before: pNext=%#p pPrev=%#p\n", pDisk->ListWriteLocked.pNext,
2638 pDisk->ListWriteLocked.pPrev));
2639
2640 RTListMove(&ListTmp, &pDisk->ListWriteLocked);
2641
2642 LogFlowFunc(("After: pNext=%#p pPrev=%#p\n", pDisk->ListWriteLocked.pNext,
2643 pDisk->ListWriteLocked.pPrev));
2644
2645 RTCritSectLeave(&pDisk->CritSect);
2646
2647 /* Process the list. */
2648 do
2649 {
2650 PVDIOCTXDEFERRED pDeferred = RTListGetFirst(&ListTmp, VDIOCTXDEFERRED, NodeDeferred);
2651 PVDIOCTX pIoCtxWait = pDeferred->pIoCtx;
2652
2653 AssertPtr(pIoCtxWait);
2654
2655 RTListNodeRemove(&pDeferred->NodeDeferred);
2656 RTMemFree(pDeferred);
2657
2658 Assert(!pIoCtxWait->pIoCtxParent);
2659
2660 pIoCtxWait->fBlocked = false;
2661 LogFlowFunc(("Processing waiting I/O context pIoCtxWait=%#p\n", pIoCtxWait));
2662
2663 rc = vdIoCtxProcess(pIoCtxWait);
2664 if ( rc == VINF_VD_ASYNC_IO_FINISHED
2665 && ASMAtomicCmpXchgBool(&pIoCtxWait->fComplete, true, false))
2666 {
2667 LogFlowFunc(("Waiting I/O context completed pIoCtxWait=%#p\n", pIoCtxWait));
2668 vdThreadFinishWrite(pDisk);
2669 pIoCtxWait->Type.Root.pfnComplete(pIoCtxWait->Type.Root.pvUser1,
2670 pIoCtxWait->Type.Root.pvUser2,
2671 pIoCtxWait->rcReq);
2672 vdIoCtxFree(pDisk, pIoCtxWait);
2673 }
2674 } while (!RTListIsEmpty(&ListTmp));
2675 }
2676 else
2677 RTCritSectLeave(&pDisk->CritSect);
2678 }
2679 else
2680 {
2681 if (pIoCtx->enmTxDir == VDIOCTXTXDIR_FLUSH)
2682 {
2683 vdIoCtxUnlockDisk(pDisk, pIoCtx, true /* fProcessDerredReqs */);
2684 vdThreadFinishWrite(pDisk);
2685 }
2686 else if (pIoCtx->enmTxDir == VDIOCTXTXDIR_WRITE)
2687 vdThreadFinishWrite(pDisk);
2688 else
2689 {
2690 Assert(pIoCtx->enmTxDir == VDIOCTXTXDIR_READ);
2691 vdThreadFinishRead(pDisk);
2692 }
2693
2694 LogFlowFunc(("I/O context completed pIoCtx=%#p rcReq=%Rrc\n", pIoCtx, pIoCtx->rcReq));
2695 pIoCtx->Type.Root.pfnComplete(pIoCtx->Type.Root.pvUser1,
2696 pIoCtx->Type.Root.pvUser2,
2697 pIoCtx->rcReq);
2698 }
2699
2700 vdIoCtxFree(pDisk, pIoCtx);
2701 }
2702 }
2703
2704 return VINF_SUCCESS;
2705}
2706
2707/**
2708 * Internal - Called when user transfer completed.
2709 */
2710static int vdUserXferCompleted(PVDIOSTORAGE pIoStorage, PVDIOCTX pIoCtx,
2711 PFNVDXFERCOMPLETED pfnComplete, void *pvUser,
2712 size_t cbTransfer, int rcReq)
2713{
2714 int rc = VINF_SUCCESS;
2715 bool fIoCtxContinue = true;
2716 PVBOXHDD pDisk = pIoCtx->pDisk;
2717
2718 LogFlowFunc(("pIoStorage=%#p pIoCtx=%#p pfnComplete=%#p pvUser=%#p cbTransfer=%zu rcReq=%Rrc\n",
2719 pIoStorage, pIoCtx, pfnComplete, pvUser, cbTransfer, rcReq));
2720
2721 Assert(pIoCtx->cbTransferLeft >= cbTransfer);
2722 ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbTransfer);
2723 ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
2724
2725 if (pfnComplete)
2726 {
2727 RTCritSectEnter(&pDisk->CritSect);
2728 rc = pfnComplete(pIoStorage->pVDIo->pBackendData, pIoCtx, pvUser, rcReq);
2729 RTCritSectLeave(&pDisk->CritSect);
2730 }
2731
2732 if (RT_SUCCESS(rc))
2733 rc = vdIoCtxContinue(pIoCtx, rcReq);
2734 else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
2735 rc = VINF_SUCCESS;
2736
2737 return rc;
2738}
2739
2740/**
2741 * Internal - Called when a meta transfer completed.
2742 */
2743static int vdMetaXferCompleted(PVDIOSTORAGE pIoStorage, PFNVDXFERCOMPLETED pfnComplete, void *pvUser,
2744 PVDMETAXFER pMetaXfer, int rcReq)
2745{
2746 PVBOXHDD pDisk = pIoStorage->pVDIo->pDisk;
2747 RTLISTNODE ListIoCtxWaiting;
2748 bool fFlush;
2749
2750 LogFlowFunc(("pIoStorage=%#p pfnComplete=%#p pvUser=%#p pMetaXfer=%#p rcReq=%Rrc\n",
2751 pIoStorage, pfnComplete, pvUser, pMetaXfer, rcReq));
2752
2753 RTCritSectEnter(&pDisk->CritSect);
2754 fFlush = VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_FLUSH;
2755 VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
2756
2757 if (!fFlush)
2758 {
2759 RTListMove(&ListIoCtxWaiting, &pMetaXfer->ListIoCtxWaiting);
2760
2761 if (RT_FAILURE(rcReq))
2762 {
2763 /* Remove from the AVL tree. */
2764 LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
2765 bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
2766 Assert(fRemoved);
2767 RTMemFree(pMetaXfer);
2768 }
2769 else
2770 {
2771 /* Increase the reference counter to make sure it doesn't go away before the last context is processed. */
2772 pMetaXfer->cRefs++;
2773 }
2774 }
2775 else
2776 RTListMove(&ListIoCtxWaiting, &pMetaXfer->ListIoCtxWaiting);
2777 RTCritSectLeave(&pDisk->CritSect);
2778
2779 /* Go through the waiting list and continue the I/O contexts. */
2780 while (!RTListIsEmpty(&ListIoCtxWaiting))
2781 {
2782 int rc = VINF_SUCCESS;
2783 bool fContinue = true;
2784 PVDIOCTXDEFERRED pDeferred = RTListGetFirst(&ListIoCtxWaiting, VDIOCTXDEFERRED, NodeDeferred);
2785 PVDIOCTX pIoCtx = pDeferred->pIoCtx;
2786 RTListNodeRemove(&pDeferred->NodeDeferred);
2787
2788 RTMemFree(pDeferred);
2789 ASMAtomicDecU32(&pIoCtx->cMetaTransfersPending);
2790
2791 if (pfnComplete)
2792 {
2793 RTCritSectEnter(&pDisk->CritSect);
2794 rc = pfnComplete(pIoStorage->pVDIo->pBackendData, pIoCtx, pvUser, rcReq);
2795 RTCritSectLeave(&pDisk->CritSect);
2796 }
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 RTCritSectEnter(&pDisk->CritSect);
2813 pMetaXfer->cRefs--;
2814 if (!pMetaXfer->cRefs && RTListIsEmpty(&pMetaXfer->ListIoCtxWaiting))
2815 {
2816 /* Remove from the AVL tree. */
2817 LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
2818 bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
2819 Assert(fRemoved);
2820 RTMemFree(pMetaXfer);
2821 }
2822 RTCritSectLeave(&pDisk->CritSect);
2823 }
2824 else if (fFlush)
2825 RTMemFree(pMetaXfer);
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 break;
3050 }
3051
3052 uOffset += cbTaskRead;
3053 cbRead -= cbTaskRead;
3054 }
3055
3056 LogFlowFunc(("returns rc=%Rrc\n", rc));
3057 return rc;
3058}
3059
3060static int vdIOIntWriteUserAsync(void *pvUser, PVDIOSTORAGE pIoStorage,
3061 uint64_t uOffset, PVDIOCTX pIoCtx,
3062 size_t cbWrite,
3063 PFNVDXFERCOMPLETED pfnComplete,
3064 void *pvCompleteUser)
3065{
3066 int rc = VINF_SUCCESS;
3067 PVDIO pVDIo = (PVDIO)pvUser;
3068 PVBOXHDD pDisk = pVDIo->pDisk;
3069
3070 LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pIoCtx=%#p cbWrite=%u\n",
3071 pvUser, pIoStorage, uOffset, pIoCtx, cbWrite));
3072
3073 VD_THREAD_IS_CRITSECT_OWNER(pDisk);
3074
3075 Assert(cbWrite > 0);
3076
3077 /* Build the S/G array and spawn a new I/O task */
3078 while (cbWrite)
3079 {
3080 RTSGSEG aSeg[VD_IO_TASK_SEGMENTS_MAX];
3081 unsigned cSegments = VD_IO_TASK_SEGMENTS_MAX;
3082 size_t cbTaskWrite = 0;
3083
3084 cbTaskWrite = RTSgBufSegArrayCreate(&pIoCtx->SgBuf, aSeg, &cSegments, cbWrite);
3085
3086 Assert(cSegments > 0);
3087 Assert(cbTaskWrite > 0);
3088 AssertMsg(cbTaskWrite <= cbWrite, ("Invalid number of bytes to write\n"));
3089
3090 LogFlow(("Writing %u bytes from %u segments\n", cbTaskWrite, cSegments));
3091
3092#ifdef DEBUG
3093 for (unsigned i = 0; i < cSegments; i++)
3094 AssertMsg(aSeg[i].pvSeg && !(aSeg[i].cbSeg % 512),
3095 ("Segment %u is invalid\n", i));
3096#endif
3097
3098 PVDIOTASK pIoTask = vdIoTaskUserAlloc(pIoStorage, pfnComplete, pvCompleteUser, pIoCtx, cbTaskWrite);
3099
3100 if (!pIoTask)
3101 return VERR_NO_MEMORY;
3102
3103 ASMAtomicIncU32(&pIoCtx->cDataTransfersPending);
3104
3105 void *pvTask;
3106 rc = pVDIo->pInterfaceIOCallbacks->pfnWriteAsync(pVDIo->pInterfaceIO->pvUser,
3107 pIoStorage->pStorage,
3108 uOffset, aSeg, cSegments,
3109 cbTaskWrite, pIoTask,
3110 &pvTask);
3111 if (RT_SUCCESS(rc))
3112 {
3113 AssertMsg(cbTaskWrite <= pIoCtx->cbTransferLeft, ("Impossible!\n"));
3114 ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbTaskWrite);
3115 ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
3116 vdIoTaskFree(pDisk, pIoTask);
3117 }
3118 else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
3119 {
3120 ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
3121 break;
3122 }
3123
3124 uOffset += cbTaskWrite;
3125 cbWrite -= cbTaskWrite;
3126 }
3127
3128 return rc;
3129}
3130
3131static int vdIOIntReadMetaAsync(void *pvUser, PVDIOSTORAGE pIoStorage,
3132 uint64_t uOffset, void *pvBuf,
3133 size_t cbRead, PVDIOCTX pIoCtx,
3134 PPVDMETAXFER ppMetaXfer,
3135 PFNVDXFERCOMPLETED pfnComplete,
3136 void *pvCompleteUser)
3137{
3138 PVDIO pVDIo = (PVDIO)pvUser;
3139 PVBOXHDD pDisk = pVDIo->pDisk;
3140 int rc = VINF_SUCCESS;
3141 RTSGSEG Seg;
3142 PVDIOTASK pIoTask;
3143 PVDMETAXFER pMetaXfer = NULL;
3144 void *pvTask = NULL;
3145
3146 LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pvBuf=%#p cbRead=%u\n",
3147 pvUser, pIoStorage, uOffset, pvBuf, cbRead));
3148
3149 VD_THREAD_IS_CRITSECT_OWNER(pDisk);
3150
3151 pMetaXfer = (PVDMETAXFER)RTAvlrFileOffsetGet(pIoStorage->pTreeMetaXfers, uOffset);
3152 if (!pMetaXfer)
3153 {
3154#ifdef RT_STRICT
3155 pMetaXfer = (PVDMETAXFER)RTAvlrFileOffsetGetBestFit(pIoStorage->pTreeMetaXfers, uOffset, false /* fAbove */);
3156 AssertMsg(!pMetaXfer || (pMetaXfer->Core.Key + (RTFOFF)pMetaXfer->cbMeta <= (RTFOFF)uOffset),
3157 ("Overlapping meta transfers!\n"));
3158#endif
3159
3160 /* Allocate a new meta transfer. */
3161 pMetaXfer = vdMetaXferAlloc(pIoStorage, uOffset, cbRead);
3162 if (!pMetaXfer)
3163 return VERR_NO_MEMORY;
3164
3165 pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvCompleteUser, pMetaXfer);
3166 if (!pIoTask)
3167 {
3168 RTMemFree(pMetaXfer);
3169 return VERR_NO_MEMORY;
3170 }
3171
3172 Seg.cbSeg = cbRead;
3173 Seg.pvSeg = pMetaXfer->abData;
3174
3175 VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_READ);
3176 rc = pVDIo->pInterfaceIOCallbacks->pfnReadAsync(pVDIo->pInterfaceIO->pvUser,
3177 pIoStorage->pStorage,
3178 uOffset, &Seg, 1,
3179 cbRead, pIoTask,
3180 &pvTask);
3181
3182 if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
3183 {
3184 bool fInserted = RTAvlrFileOffsetInsert(pIoStorage->pTreeMetaXfers, &pMetaXfer->Core);
3185 Assert(fInserted);
3186 }
3187 else
3188 RTMemFree(pMetaXfer);
3189
3190 if (RT_SUCCESS(rc))
3191 {
3192 VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
3193 vdIoTaskFree(pDisk, pIoTask);
3194 }
3195 else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS && !pfnComplete)
3196 rc = VERR_VD_NOT_ENOUGH_METADATA;
3197 }
3198
3199 Assert(VALID_PTR(pMetaXfer) || RT_FAILURE(rc));
3200
3201 if (RT_SUCCESS(rc) || rc == VERR_VD_NOT_ENOUGH_METADATA || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
3202 {
3203 /* If it is pending add the request to the list. */
3204 if (VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_READ)
3205 {
3206 PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
3207 AssertPtr(pDeferred);
3208
3209 RTListInit(&pDeferred->NodeDeferred);
3210 pDeferred->pIoCtx = pIoCtx;
3211
3212 ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
3213 RTListAppend(&pMetaXfer->ListIoCtxWaiting, &pDeferred->NodeDeferred);
3214 rc = VERR_VD_NOT_ENOUGH_METADATA;
3215 }
3216 else
3217 {
3218 /* Transfer the data. */
3219 pMetaXfer->cRefs++;
3220 Assert(pMetaXfer->cbMeta >= cbRead);
3221 Assert(pMetaXfer->Core.Key == (RTFOFF)uOffset);
3222 memcpy(pvBuf, pMetaXfer->abData, cbRead);
3223 *ppMetaXfer = pMetaXfer;
3224 }
3225 }
3226
3227 return rc;
3228}
3229
3230static int vdIOIntWriteMetaAsync(void *pvUser, PVDIOSTORAGE pIoStorage,
3231 uint64_t uOffset, void *pvBuf,
3232 size_t cbWrite, PVDIOCTX pIoCtx,
3233 PFNVDXFERCOMPLETED pfnComplete,
3234 void *pvCompleteUser)
3235{
3236 PVDIO pVDIo = (PVDIO)pvUser;
3237 PVBOXHDD pDisk = pVDIo->pDisk;
3238 int rc = VINF_SUCCESS;
3239 RTSGSEG Seg;
3240 PVDIOTASK pIoTask;
3241 PVDMETAXFER pMetaXfer = NULL;
3242 bool fInTree = false;
3243 void *pvTask = NULL;
3244
3245 LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pvBuf=%#p cbWrite=%u\n",
3246 pvUser, pIoStorage, uOffset, pvBuf, cbWrite));
3247
3248 VD_THREAD_IS_CRITSECT_OWNER(pDisk);
3249
3250 pMetaXfer = (PVDMETAXFER)RTAvlrFileOffsetGet(pIoStorage->pTreeMetaXfers, uOffset);
3251 if (!pMetaXfer)
3252 {
3253 /* Allocate a new meta transfer. */
3254 pMetaXfer = vdMetaXferAlloc(pIoStorage, uOffset, cbWrite);
3255 if (!pMetaXfer)
3256 return VERR_NO_MEMORY;
3257 }
3258 else
3259 {
3260 Assert(pMetaXfer->cbMeta >= cbWrite);
3261 Assert(pMetaXfer->Core.Key == (RTFOFF)uOffset);
3262 fInTree = true;
3263 }
3264
3265 Assert(VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE);
3266
3267 pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvCompleteUser, pMetaXfer);
3268 if (!pIoTask)
3269 {
3270 RTMemFree(pMetaXfer);
3271 return VERR_NO_MEMORY;
3272 }
3273
3274 memcpy(pMetaXfer->abData, pvBuf, cbWrite);
3275 Seg.cbSeg = cbWrite;
3276 Seg.pvSeg = pMetaXfer->abData;
3277
3278 ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
3279
3280 VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_WRITE);
3281 rc = pVDIo->pInterfaceIOCallbacks->pfnWriteAsync(pVDIo->pInterfaceIO->pvUser,
3282 pIoStorage->pStorage,
3283 uOffset, &Seg, 1,
3284 cbWrite, pIoTask,
3285 &pvTask);
3286 if (RT_SUCCESS(rc))
3287 {
3288 VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
3289 ASMAtomicDecU32(&pIoCtx->cMetaTransfersPending);
3290 vdIoTaskFree(pDisk, pIoTask);
3291 if (fInTree && !pMetaXfer->cRefs)
3292 {
3293 LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
3294 bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
3295 AssertMsg(fRemoved, ("Metadata transfer wasn't removed\n"));
3296 RTMemFree(pMetaXfer);
3297 pMetaXfer = NULL;
3298 }
3299 }
3300 else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
3301 {
3302 PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
3303 AssertPtr(pDeferred);
3304
3305 RTListInit(&pDeferred->NodeDeferred);
3306 pDeferred->pIoCtx = pIoCtx;
3307
3308 if (!fInTree)
3309 {
3310 bool fInserted = RTAvlrFileOffsetInsert(pIoStorage->pTreeMetaXfers, &pMetaXfer->Core);
3311 Assert(fInserted);
3312 }
3313
3314 RTListAppend(&pMetaXfer->ListIoCtxWaiting, &pDeferred->NodeDeferred);
3315 }
3316 else
3317 {
3318 RTMemFree(pMetaXfer);
3319 pMetaXfer = NULL;
3320 }
3321
3322 return rc;
3323}
3324
3325static void vdIOIntMetaXferRelease(void *pvUser, PVDMETAXFER pMetaXfer)
3326{
3327 PVDIO pVDIo = (PVDIO)pvUser;
3328 PVBOXHDD pDisk = pVDIo->pDisk;
3329 PVDIOSTORAGE pIoStorage = pMetaXfer->pIoStorage;
3330
3331 VD_THREAD_IS_CRITSECT_OWNER(pDisk);
3332
3333 Assert( VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE
3334 || VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_WRITE);
3335 Assert(pMetaXfer->cRefs > 0);
3336
3337 pMetaXfer->cRefs--;
3338 if ( !pMetaXfer->cRefs
3339 && RTListIsEmpty(&pMetaXfer->ListIoCtxWaiting)
3340 && VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE)
3341 {
3342 /* Free the meta data entry. */
3343 LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
3344 bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
3345 AssertMsg(fRemoved, ("Metadata transfer wasn't removed\n"));
3346
3347 RTMemFree(pMetaXfer);
3348 }
3349}
3350
3351static int vdIOIntFlushAsync(void *pvUser, PVDIOSTORAGE pIoStorage,
3352 PVDIOCTX pIoCtx, PFNVDXFERCOMPLETED pfnComplete,
3353 void *pvCompleteUser)
3354{
3355 PVDIO pVDIo = (PVDIO)pvUser;
3356 PVBOXHDD pDisk = pVDIo->pDisk;
3357 int rc = VINF_SUCCESS;
3358 PVDIOTASK pIoTask;
3359 PVDMETAXFER pMetaXfer = NULL;
3360 void *pvTask = NULL;
3361
3362 VD_THREAD_IS_CRITSECT_OWNER(pDisk);
3363
3364 LogFlowFunc(("pvUser=%#p pIoStorage=%#p pIoCtx=%#p\n",
3365 pvUser, pIoStorage, pIoCtx));
3366
3367 /* Allocate a new meta transfer. */
3368 pMetaXfer = vdMetaXferAlloc(pIoStorage, 0, 0);
3369 if (!pMetaXfer)
3370 return VERR_NO_MEMORY;
3371
3372 pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvUser, pMetaXfer);
3373 if (!pIoTask)
3374 {
3375 RTMemFree(pMetaXfer);
3376 return VERR_NO_MEMORY;
3377 }
3378
3379 ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
3380
3381 PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
3382 AssertPtr(pDeferred);
3383
3384 RTListInit(&pDeferred->NodeDeferred);
3385 pDeferred->pIoCtx = pIoCtx;
3386
3387 RTListAppend(&pMetaXfer->ListIoCtxWaiting, &pDeferred->NodeDeferred);
3388 VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_FLUSH);
3389 rc = pVDIo->pInterfaceIOCallbacks->pfnFlushAsync(pVDIo->pInterfaceIO->pvUser,
3390 pIoStorage->pStorage,
3391 pIoTask, &pvTask);
3392 if (RT_SUCCESS(rc))
3393 {
3394 VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
3395 ASMAtomicDecU32(&pIoCtx->cMetaTransfersPending);
3396 vdIoTaskFree(pDisk, pIoTask);
3397 RTMemFree(pDeferred);
3398 RTMemFree(pMetaXfer);
3399 }
3400 else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
3401 RTMemFree(pMetaXfer);
3402
3403 return rc;
3404}
3405
3406static size_t vdIOIntIoCtxCopyTo(void *pvUser, PVDIOCTX pIoCtx,
3407 void *pvBuf, size_t cbBuf)
3408{
3409 PVDIO pVDIo = (PVDIO)pvUser;
3410 PVBOXHDD pDisk = pVDIo->pDisk;
3411 size_t cbCopied = 0;
3412
3413 VD_THREAD_IS_CRITSECT_OWNER(pDisk);
3414
3415 cbCopied = vdIoCtxCopyTo(pIoCtx, (uint8_t *)pvBuf, cbBuf);
3416 Assert(cbCopied == cbBuf);
3417
3418 ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbCopied);
3419
3420 return cbCopied;
3421}
3422
3423static size_t vdIOIntIoCtxCopyFrom(void *pvUser, PVDIOCTX pIoCtx,
3424 void *pvBuf, size_t cbBuf)
3425{
3426 PVDIO pVDIo = (PVDIO)pvUser;
3427 PVBOXHDD pDisk = pVDIo->pDisk;
3428 size_t cbCopied = 0;
3429
3430 VD_THREAD_IS_CRITSECT_OWNER(pDisk);
3431
3432 cbCopied = vdIoCtxCopyFrom(pIoCtx, (uint8_t *)pvBuf, cbBuf);
3433 Assert(cbCopied == cbBuf);
3434
3435 ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbCopied);
3436
3437 return cbCopied;
3438}
3439
3440static size_t vdIOIntIoCtxSet(void *pvUser, PVDIOCTX pIoCtx, int ch, size_t cb)
3441{
3442 PVDIO pVDIo = (PVDIO)pvUser;
3443 PVBOXHDD pDisk = pVDIo->pDisk;
3444 size_t cbSet = 0;
3445
3446 VD_THREAD_IS_CRITSECT_OWNER(pDisk);
3447
3448 cbSet = vdIoCtxSet(pIoCtx, ch, cb);
3449 Assert(cbSet == cb);
3450
3451 ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbSet);
3452
3453 return cbSet;
3454}
3455
3456static size_t vdIOIntIoCtxSegArrayCreate(void *pvUser, PVDIOCTX pIoCtx,
3457 PRTSGSEG paSeg, unsigned *pcSeg,
3458 size_t cbData)
3459{
3460 PVDIO pVDIo = (PVDIO)pvUser;
3461 PVBOXHDD pDisk = pVDIo->pDisk;
3462 size_t cbCreated = 0;
3463
3464 VD_THREAD_IS_CRITSECT_OWNER(pDisk);
3465
3466 cbCreated = RTSgBufSegArrayCreate(&pIoCtx->SgBuf, paSeg, pcSeg, cbData);
3467 Assert(!paSeg || cbData == cbCreated);
3468
3469 return cbCreated;
3470}
3471
3472static void vdIOIntIoCtxCompleted(void *pvUser, PVDIOCTX pIoCtx, int rcReq,
3473 size_t cbCompleted)
3474{
3475 /* Continue */
3476 pIoCtx->fBlocked = false;
3477 ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbCompleted);
3478
3479 /* Clear the pointer to next transfer function in case we have nothing to transfer anymore.
3480 * @todo: Find a better way to prevent vdIoCtxContinue from calling the read/write helper again. */
3481 if (!pIoCtx->cbTransferLeft)
3482 pIoCtx->pfnIoCtxTransfer = NULL;
3483
3484 vdIoCtxContinue(pIoCtx, rcReq);
3485}
3486
3487/**
3488 * VD I/O interface callback for opening a file (limited version for VDGetFormat).
3489 */
3490static int vdIOIntOpenLimited(void *pvUser, const char *pszLocation,
3491 uint32_t fOpen, PPVDIOSTORAGE ppIoStorage)
3492{
3493 int rc = VINF_SUCCESS;
3494 PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
3495 PVDIOSTORAGE pIoStorage = (PVDIOSTORAGE)RTMemAllocZ(sizeof(VDIOSTORAGE));
3496
3497 if (!pIoStorage)
3498 return VERR_NO_MEMORY;
3499
3500 rc = pInterfaceIOCallbacks->pfnOpen(NULL, pszLocation, fOpen,
3501 NULL, &pIoStorage->pStorage);
3502 if (RT_SUCCESS(rc))
3503 *ppIoStorage = pIoStorage;
3504 else
3505 RTMemFree(pIoStorage);
3506
3507 return rc;
3508}
3509
3510static int vdIOIntCloseLimited(void *pvUser, PVDIOSTORAGE pIoStorage)
3511{
3512 PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
3513 int rc = pInterfaceIOCallbacks->pfnClose(NULL, pIoStorage->pStorage);
3514 AssertRC(rc);
3515
3516 RTMemFree(pIoStorage);
3517 return VINF_SUCCESS;
3518}
3519
3520static int vdIOIntDeleteLimited(void *pvUser, const char *pcszFilename)
3521{
3522 PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
3523 return pInterfaceIOCallbacks->pfnDelete(NULL, pcszFilename);
3524}
3525
3526static int vdIOIntMoveLimited(void *pvUser, const char *pcszSrc,
3527 const char *pcszDst, unsigned fMove)
3528{
3529 PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
3530 return pInterfaceIOCallbacks->pfnMove(NULL, pcszSrc, pcszDst, fMove);
3531}
3532
3533static int vdIOIntGetFreeSpaceLimited(void *pvUser, const char *pcszFilename,
3534 int64_t *pcbFreeSpace)
3535{
3536 PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
3537 return pInterfaceIOCallbacks->pfnGetFreeSpace(NULL, pcszFilename, pcbFreeSpace);
3538}
3539
3540static int vdIOIntGetModificationTimeLimited(void *pvUser,
3541 const char *pcszFilename,
3542 PRTTIMESPEC pModificationTime)
3543{
3544 PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
3545 return pInterfaceIOCallbacks->pfnGetModificationTime(NULL, pcszFilename, pModificationTime);
3546}
3547
3548static int vdIOIntGetSizeLimited(void *pvUser, PVDIOSTORAGE pIoStorage,
3549 uint64_t *pcbSize)
3550{
3551 PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
3552 return pInterfaceIOCallbacks->pfnGetSize(NULL, pIoStorage->pStorage, pcbSize);
3553}
3554
3555static int vdIOIntSetSizeLimited(void *pvUser, PVDIOSTORAGE pIoStorage,
3556 uint64_t cbSize)
3557{
3558 PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
3559 return pInterfaceIOCallbacks->pfnSetSize(NULL, pIoStorage->pStorage, cbSize);
3560}
3561
3562static int vdIOIntWriteSyncLimited(void *pvUser, PVDIOSTORAGE pIoStorage,
3563 uint64_t uOffset, const void *pvBuf,
3564 size_t cbWrite, size_t *pcbWritten)
3565{
3566 PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
3567 return pInterfaceIOCallbacks->pfnWriteSync(NULL, pIoStorage->pStorage, uOffset, pvBuf, cbWrite, pcbWritten);
3568}
3569
3570static int vdIOIntReadSyncLimited(void *pvUser, PVDIOSTORAGE pIoStorage,
3571 uint64_t uOffset, void *pvBuf, size_t cbRead,
3572 size_t *pcbRead)
3573{
3574 PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
3575 return pInterfaceIOCallbacks->pfnReadSync(NULL, pIoStorage->pStorage, uOffset, pvBuf, cbRead, pcbRead);
3576}
3577
3578static int vdIOIntFlushSyncLimited(void *pvUser, PVDIOSTORAGE pIoStorage)
3579{
3580 PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
3581 return pInterfaceIOCallbacks->pfnFlushSync(NULL, pIoStorage->pStorage);
3582}
3583
3584/**
3585 * internal: send output to the log (unconditionally).
3586 */
3587int vdLogMessage(void *pvUser, const char *pszFormat, va_list args)
3588{
3589 NOREF(pvUser);
3590 RTLogPrintfV(pszFormat, args);
3591 return VINF_SUCCESS;
3592}
3593
3594DECLINLINE(int) vdMessageWrapper(PVBOXHDD pDisk, const char *pszFormat, ...)
3595{
3596 va_list va;
3597 va_start(va, pszFormat);
3598 int rc = pDisk->pInterfaceErrorCallbacks->pfnMessage(pDisk->pInterfaceError->pvUser,
3599 pszFormat, va);
3600 va_end(va);
3601 return rc;
3602}
3603
3604
3605/**
3606 * internal: adjust PCHS geometry
3607 */
3608static void vdFixupPCHSGeometry(PVDGEOMETRY pPCHS, uint64_t cbSize)
3609{
3610 /* Fix broken PCHS geometry. Can happen for two reasons: either the backend
3611 * mixes up PCHS and LCHS, or the application used to create the source
3612 * image has put garbage in it. Additionally, if the PCHS geometry covers
3613 * more than the image size, set it back to the default. */
3614 if ( pPCHS->cHeads > 16
3615 || pPCHS->cSectors > 63
3616 || pPCHS->cCylinders == 0
3617 || (uint64_t)pPCHS->cHeads * pPCHS->cSectors * pPCHS->cCylinders * 512 > cbSize)
3618 {
3619 Assert(!(RT_MIN(cbSize / 512 / 16 / 63, 16383) - (uint32_t)RT_MIN(cbSize / 512 / 16 / 63, 16383)));
3620 pPCHS->cCylinders = (uint32_t)RT_MIN(cbSize / 512 / 16 / 63, 16383);
3621 pPCHS->cHeads = 16;
3622 pPCHS->cSectors = 63;
3623 }
3624}
3625
3626/**
3627 * internal: adjust PCHS geometry
3628 */
3629static void vdFixupLCHSGeometry(PVDGEOMETRY pLCHS, uint64_t cbSize)
3630{
3631 /* Fix broken LCHS geometry. Can happen for two reasons: either the backend
3632 * mixes up PCHS and LCHS, or the application used to create the source
3633 * image has put garbage in it. The fix in this case is to clear the LCHS
3634 * geometry to trigger autodetection when it is used next. If the geometry
3635 * already says "please autodetect" (cylinders=0) keep it. */
3636 if ( ( pLCHS->cHeads > 255
3637 || pLCHS->cHeads == 0
3638 || pLCHS->cSectors > 63
3639 || pLCHS->cSectors == 0)
3640 && pLCHS->cCylinders != 0)
3641 {
3642 pLCHS->cCylinders = 0;
3643 pLCHS->cHeads = 0;
3644 pLCHS->cSectors = 0;
3645 }
3646 /* Always recompute the number of cylinders stored in the LCHS
3647 * geometry if it isn't set to "autotedetect" at the moment.
3648 * This is very useful if the destination image size is
3649 * larger or smaller than the source image size. Do not modify
3650 * the number of heads and sectors. Windows guests hate it. */
3651 if ( pLCHS->cCylinders != 0
3652 && pLCHS->cHeads != 0 /* paranoia */
3653 && pLCHS->cSectors != 0 /* paranoia */)
3654 {
3655 Assert(!(RT_MIN(cbSize / 512 / pLCHS->cHeads / pLCHS->cSectors, 1024) - (uint32_t)RT_MIN(cbSize / 512 / pLCHS->cHeads / pLCHS->cSectors, 1024)));
3656 pLCHS->cCylinders = (uint32_t)RT_MIN(cbSize / 512 / pLCHS->cHeads / pLCHS->cSectors, 1024);
3657 }
3658}
3659
3660/**
3661 * Initializes HDD backends.
3662 *
3663 * @returns VBox status code.
3664 */
3665VBOXDDU_DECL(int) VDInit(void)
3666{
3667 int rc = vdAddBackends(aStaticBackends, RT_ELEMENTS(aStaticBackends));
3668 if (RT_SUCCESS(rc))
3669 {
3670 rc = vdAddCacheBackends(aStaticCacheBackends, RT_ELEMENTS(aStaticCacheBackends));
3671 if (RT_SUCCESS(rc))
3672 {
3673 rc = vdLoadDynamicBackends();
3674 if (RT_SUCCESS(rc))
3675 rc = vdLoadDynamicCacheBackends();
3676 }
3677 }
3678 LogRel(("VDInit finished\n"));
3679 return rc;
3680}
3681
3682/**
3683 * Destroys loaded HDD backends.
3684 *
3685 * @returns VBox status code.
3686 */
3687VBOXDDU_DECL(int) VDShutdown(void)
3688{
3689 PVBOXHDDBACKEND *pBackends = g_apBackends;
3690 PVDCACHEBACKEND *pCacheBackends = g_apCacheBackends;
3691 unsigned cBackends = g_cBackends;
3692
3693 if (!pBackends)
3694 return VERR_INTERNAL_ERROR;
3695
3696 g_cBackends = 0;
3697 g_apBackends = NULL;
3698
3699#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
3700 for (unsigned i = 0; i < cBackends; i++)
3701 if (pBackends[i]->hPlugin != NIL_RTLDRMOD)
3702 RTLdrClose(pBackends[i]->hPlugin);
3703#endif
3704
3705 /* Clear the supported cache backends. */
3706 cBackends = g_cCacheBackends;
3707 g_cCacheBackends = 0;
3708 g_apCacheBackends = NULL;
3709
3710#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
3711 for (unsigned i = 0; i < cBackends; i++)
3712 if (pCacheBackends[i]->hPlugin != NIL_RTLDRMOD)
3713 RTLdrClose(pCacheBackends[i]->hPlugin);
3714#endif
3715
3716 if (pCacheBackends)
3717 RTMemFree(pCacheBackends);
3718 RTMemFree(pBackends);
3719 return VINF_SUCCESS;
3720}
3721
3722
3723/**
3724 * Lists all HDD backends and their capabilities in a caller-provided buffer.
3725 *
3726 * @returns VBox status code.
3727 * VERR_BUFFER_OVERFLOW if not enough space is passed.
3728 * @param cEntriesAlloc Number of list entries available.
3729 * @param pEntries Pointer to array for the entries.
3730 * @param pcEntriesUsed Number of entries returned.
3731 */
3732VBOXDDU_DECL(int) VDBackendInfo(unsigned cEntriesAlloc, PVDBACKENDINFO pEntries,
3733 unsigned *pcEntriesUsed)
3734{
3735 int rc = VINF_SUCCESS;
3736 PRTDIR pPluginDir = NULL;
3737 unsigned cEntries = 0;
3738
3739 LogFlowFunc(("cEntriesAlloc=%u pEntries=%#p pcEntriesUsed=%#p\n", cEntriesAlloc, pEntries, pcEntriesUsed));
3740 /* Check arguments. */
3741 AssertMsgReturn(cEntriesAlloc,
3742 ("cEntriesAlloc=%u\n", cEntriesAlloc),
3743 VERR_INVALID_PARAMETER);
3744 AssertMsgReturn(VALID_PTR(pEntries),
3745 ("pEntries=%#p\n", pEntries),
3746 VERR_INVALID_PARAMETER);
3747 AssertMsgReturn(VALID_PTR(pcEntriesUsed),
3748 ("pcEntriesUsed=%#p\n", pcEntriesUsed),
3749 VERR_INVALID_PARAMETER);
3750 if (!g_apBackends)
3751 VDInit();
3752
3753 if (cEntriesAlloc < g_cBackends)
3754 {
3755 *pcEntriesUsed = g_cBackends;
3756 return VERR_BUFFER_OVERFLOW;
3757 }
3758
3759 for (unsigned i = 0; i < g_cBackends; i++)
3760 {
3761 pEntries[i].pszBackend = g_apBackends[i]->pszBackendName;
3762 pEntries[i].uBackendCaps = g_apBackends[i]->uBackendCaps;
3763 pEntries[i].paFileExtensions = g_apBackends[i]->paFileExtensions;
3764 pEntries[i].paConfigInfo = g_apBackends[i]->paConfigInfo;
3765 pEntries[i].pfnComposeLocation = g_apBackends[i]->pfnComposeLocation;
3766 pEntries[i].pfnComposeName = g_apBackends[i]->pfnComposeName;
3767 }
3768
3769 LogFlowFunc(("returns %Rrc *pcEntriesUsed=%u\n", rc, cEntries));
3770 *pcEntriesUsed = g_cBackends;
3771 return rc;
3772}
3773
3774/**
3775 * Lists the capabilities of a backend identified by its name.
3776 *
3777 * @returns VBox status code.
3778 * @param pszBackend The backend name.
3779 * @param pEntries Pointer to an entry.
3780 */
3781VBOXDDU_DECL(int) VDBackendInfoOne(const char *pszBackend, PVDBACKENDINFO pEntry)
3782{
3783 LogFlowFunc(("pszBackend=%#p pEntry=%#p\n", pszBackend, pEntry));
3784 /* Check arguments. */
3785 AssertMsgReturn(VALID_PTR(pszBackend),
3786 ("pszBackend=%#p\n", pszBackend),
3787 VERR_INVALID_PARAMETER);
3788 AssertMsgReturn(VALID_PTR(pEntry),
3789 ("pEntry=%#p\n", pEntry),
3790 VERR_INVALID_PARAMETER);
3791 if (!g_apBackends)
3792 VDInit();
3793
3794 /* Go through loaded backends. */
3795 for (unsigned i = 0; i < g_cBackends; i++)
3796 {
3797 if (!RTStrICmp(pszBackend, g_apBackends[i]->pszBackendName))
3798 {
3799 pEntry->pszBackend = g_apBackends[i]->pszBackendName;
3800 pEntry->uBackendCaps = g_apBackends[i]->uBackendCaps;
3801 pEntry->paFileExtensions = g_apBackends[i]->paFileExtensions;
3802 pEntry->paConfigInfo = g_apBackends[i]->paConfigInfo;
3803 return VINF_SUCCESS;
3804 }
3805 }
3806
3807 return VERR_NOT_FOUND;
3808}
3809
3810/**
3811 * Allocates and initializes an empty HDD container.
3812 * No image files are opened.
3813 *
3814 * @returns VBox status code.
3815 * @param pVDIfsDisk Pointer to the per-disk VD interface list.
3816 * @param enmType Type of the image container.
3817 * @param ppDisk Where to store the reference to HDD container.
3818 */
3819VBOXDDU_DECL(int) VDCreate(PVDINTERFACE pVDIfsDisk, VDTYPE enmType, PVBOXHDD *ppDisk)
3820{
3821 int rc = VINF_SUCCESS;
3822 PVBOXHDD pDisk = NULL;
3823
3824 LogFlowFunc(("pVDIfsDisk=%#p\n", pVDIfsDisk));
3825 do
3826 {
3827 /* Check arguments. */
3828 AssertMsgBreakStmt(VALID_PTR(ppDisk),
3829 ("ppDisk=%#p\n", ppDisk),
3830 rc = VERR_INVALID_PARAMETER);
3831
3832 pDisk = (PVBOXHDD)RTMemAllocZ(sizeof(VBOXHDD));
3833 if (pDisk)
3834 {
3835 pDisk->u32Signature = VBOXHDDDISK_SIGNATURE;
3836 pDisk->enmType = enmType;
3837 pDisk->cImages = 0;
3838 pDisk->pBase = NULL;
3839 pDisk->pLast = NULL;
3840 pDisk->cbSize = 0;
3841 pDisk->PCHSGeometry.cCylinders = 0;
3842 pDisk->PCHSGeometry.cHeads = 0;
3843 pDisk->PCHSGeometry.cSectors = 0;
3844 pDisk->LCHSGeometry.cCylinders = 0;
3845 pDisk->LCHSGeometry.cHeads = 0;
3846 pDisk->LCHSGeometry.cSectors = 0;
3847 pDisk->pVDIfsDisk = pVDIfsDisk;
3848 pDisk->pInterfaceError = NULL;
3849 pDisk->pInterfaceErrorCallbacks = NULL;
3850 pDisk->pInterfaceThreadSync = NULL;
3851 pDisk->pInterfaceThreadSyncCallbacks = NULL;
3852 pDisk->fLocked = false;
3853 pDisk->pIoCtxLockOwner = NULL;
3854 RTListInit(&pDisk->ListWriteLocked);
3855
3856 /* Create the I/O ctx cache */
3857 rc = RTMemCacheCreate(&pDisk->hMemCacheIoCtx, sizeof(VDIOCTX), 0, UINT32_MAX,
3858 NULL, NULL, NULL, 0);
3859 if (RT_FAILURE(rc))
3860 {
3861 RTMemFree(pDisk);
3862 break;
3863 }
3864
3865 /* Create the I/O task cache */
3866 rc = RTMemCacheCreate(&pDisk->hMemCacheIoTask, sizeof(VDIOTASK), 0, UINT32_MAX,
3867 NULL, NULL, NULL, 0);
3868 if (RT_FAILURE(rc))
3869 {
3870 RTMemCacheDestroy(pDisk->hMemCacheIoCtx);
3871 RTMemFree(pDisk);
3872 break;
3873 }
3874
3875 /* Create critical section. */
3876 rc = RTCritSectInit(&pDisk->CritSect);
3877 if (RT_FAILURE(rc))
3878 {
3879 RTMemCacheDestroy(pDisk->hMemCacheIoCtx);
3880 RTMemCacheDestroy(pDisk->hMemCacheIoTask);
3881 RTMemFree(pDisk);
3882 break;
3883 }
3884
3885 pDisk->pInterfaceError = VDInterfaceGet(pVDIfsDisk, VDINTERFACETYPE_ERROR);
3886 if (pDisk->pInterfaceError)
3887 pDisk->pInterfaceErrorCallbacks = VDGetInterfaceError(pDisk->pInterfaceError);
3888
3889 pDisk->pInterfaceThreadSync = VDInterfaceGet(pVDIfsDisk, VDINTERFACETYPE_THREADSYNC);
3890 if (pDisk->pInterfaceThreadSync)
3891 pDisk->pInterfaceThreadSyncCallbacks = VDGetInterfaceThreadSync(pDisk->pInterfaceThreadSync);
3892
3893 /* Create fallback I/O callback table */
3894 pDisk->VDIIOCallbacks.cbSize = sizeof(VDINTERFACEIO);
3895 pDisk->VDIIOCallbacks.enmInterface = VDINTERFACETYPE_IO;
3896 pDisk->VDIIOCallbacks.pfnOpen = vdIOOpenFallback;
3897 pDisk->VDIIOCallbacks.pfnClose = vdIOCloseFallback;
3898 pDisk->VDIIOCallbacks.pfnDelete = vdIODeleteFallback;
3899 pDisk->VDIIOCallbacks.pfnMove = vdIOMoveFallback;
3900 pDisk->VDIIOCallbacks.pfnGetFreeSpace = vdIOGetFreeSpaceFallback;
3901 pDisk->VDIIOCallbacks.pfnGetModificationTime = vdIOGetModificationTimeFallback;
3902 pDisk->VDIIOCallbacks.pfnGetSize = vdIOGetSizeFallback;
3903 pDisk->VDIIOCallbacks.pfnSetSize = vdIOSetSizeFallback;
3904 pDisk->VDIIOCallbacks.pfnReadSync = vdIOReadSyncFallback;
3905 pDisk->VDIIOCallbacks.pfnWriteSync = vdIOWriteSyncFallback;
3906 pDisk->VDIIOCallbacks.pfnFlushSync = vdIOFlushSyncFallback;
3907 pDisk->VDIIOCallbacks.pfnReadAsync = vdIOReadAsyncFallback;
3908 pDisk->VDIIOCallbacks.pfnWriteAsync = vdIOWriteAsyncFallback;
3909 pDisk->VDIIOCallbacks.pfnFlushAsync = vdIOFlushAsyncFallback;
3910
3911 /*
3912 * Create the internal I/O callback table.
3913 * The interface is per-image but no need to duplicate the
3914 * callback table every time.
3915 */
3916 pDisk->VDIIOIntCallbacks.cbSize = sizeof(VDINTERFACEIOINT);
3917 pDisk->VDIIOIntCallbacks.enmInterface = VDINTERFACETYPE_IOINT;
3918 pDisk->VDIIOIntCallbacks.pfnOpen = vdIOIntOpen;
3919 pDisk->VDIIOIntCallbacks.pfnClose = vdIOIntClose;
3920 pDisk->VDIIOIntCallbacks.pfnDelete = vdIOIntDelete;
3921 pDisk->VDIIOIntCallbacks.pfnMove = vdIOIntMove;
3922 pDisk->VDIIOIntCallbacks.pfnGetFreeSpace = vdIOIntGetFreeSpace;
3923 pDisk->VDIIOIntCallbacks.pfnGetModificationTime = vdIOIntGetModificationTime;
3924 pDisk->VDIIOIntCallbacks.pfnGetSize = vdIOIntGetSize;
3925 pDisk->VDIIOIntCallbacks.pfnSetSize = vdIOIntSetSize;
3926 pDisk->VDIIOIntCallbacks.pfnReadSync = vdIOIntReadSync;
3927 pDisk->VDIIOIntCallbacks.pfnWriteSync = vdIOIntWriteSync;
3928 pDisk->VDIIOIntCallbacks.pfnFlushSync = vdIOIntFlushSync;
3929 pDisk->VDIIOIntCallbacks.pfnReadUserAsync = vdIOIntReadUserAsync;
3930 pDisk->VDIIOIntCallbacks.pfnWriteUserAsync = vdIOIntWriteUserAsync;
3931 pDisk->VDIIOIntCallbacks.pfnReadMetaAsync = vdIOIntReadMetaAsync;
3932 pDisk->VDIIOIntCallbacks.pfnWriteMetaAsync = vdIOIntWriteMetaAsync;
3933 pDisk->VDIIOIntCallbacks.pfnMetaXferRelease = vdIOIntMetaXferRelease;
3934 pDisk->VDIIOIntCallbacks.pfnFlushAsync = vdIOIntFlushAsync;
3935 pDisk->VDIIOIntCallbacks.pfnIoCtxCopyFrom = vdIOIntIoCtxCopyFrom;
3936 pDisk->VDIIOIntCallbacks.pfnIoCtxCopyTo = vdIOIntIoCtxCopyTo;
3937 pDisk->VDIIOIntCallbacks.pfnIoCtxSet = vdIOIntIoCtxSet;
3938 pDisk->VDIIOIntCallbacks.pfnIoCtxSegArrayCreate = vdIOIntIoCtxSegArrayCreate;
3939 pDisk->VDIIOIntCallbacks.pfnIoCtxCompleted = vdIOIntIoCtxCompleted;
3940
3941 *ppDisk = pDisk;
3942 }
3943 else
3944 {
3945 rc = VERR_NO_MEMORY;
3946 break;
3947 }
3948 } while (0);
3949
3950 LogFlowFunc(("returns %Rrc (pDisk=%#p)\n", rc, pDisk));
3951 return rc;
3952}
3953
3954/**
3955 * Destroys HDD container.
3956 * If container has opened image files they will be closed.
3957 *
3958 * @param pDisk Pointer to HDD container.
3959 */
3960VBOXDDU_DECL(void) VDDestroy(PVBOXHDD pDisk)
3961{
3962 LogFlowFunc(("pDisk=%#p\n", pDisk));
3963 do
3964 {
3965 /* sanity check */
3966 AssertPtrBreak(pDisk);
3967 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
3968 VDCloseAll(pDisk);
3969 RTCritSectDelete(&pDisk->CritSect);
3970 RTMemCacheDestroy(pDisk->hMemCacheIoCtx);
3971 RTMemCacheDestroy(pDisk->hMemCacheIoTask);
3972 RTMemFree(pDisk);
3973 } while (0);
3974 LogFlowFunc(("returns\n"));
3975}
3976
3977/**
3978 * Try to get the backend name which can use this image.
3979 *
3980 * @returns VBox status code.
3981 * VINF_SUCCESS if a plugin was found.
3982 * ppszFormat contains the string which can be used as backend name.
3983 * VERR_NOT_SUPPORTED if no backend was found.
3984 * @param pVDIfsDisk Pointer to the per-disk VD interface list.
3985 * @param pVDIfsImage Pointer to the per-image VD interface list.
3986 * @param pszFilename Name of the image file for which the backend is queried.
3987 * @param ppszFormat Receives pointer of the UTF-8 string which contains the format name.
3988 * The returned pointer must be freed using RTStrFree().
3989 */
3990VBOXDDU_DECL(int) VDGetFormat(PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
3991 const char *pszFilename, char **ppszFormat, VDTYPE *penmType)
3992{
3993 int rc = VERR_NOT_SUPPORTED;
3994 VDINTERFACEIOINT VDIIOIntCallbacks;
3995 VDINTERFACE VDIIOInt;
3996 VDINTERFACEIO VDIIOCallbacksFallback;
3997 PVDINTERFACE pInterfaceIO;
3998 PVDINTERFACEIO pInterfaceIOCallbacks;
3999
4000 LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
4001 /* Check arguments. */
4002 AssertMsgReturn(VALID_PTR(pszFilename) && *pszFilename,
4003 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
4004 VERR_INVALID_PARAMETER);
4005 AssertMsgReturn(VALID_PTR(ppszFormat),
4006 ("ppszFormat=%#p\n", ppszFormat),
4007 VERR_INVALID_PARAMETER);
4008 AssertMsgReturn(VALID_PTR(ppszFormat),
4009 ("penmType=%#p\n", penmType),
4010 VERR_INVALID_PARAMETER);
4011
4012 if (!g_apBackends)
4013 VDInit();
4014
4015 pInterfaceIO = VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IO);
4016 if (!pInterfaceIO)
4017 {
4018 /*
4019 * Caller doesn't provide an I/O interface, create our own using the
4020 * native file API.
4021 */
4022 VDIIOCallbacksFallback.cbSize = sizeof(VDINTERFACEIO);
4023 VDIIOCallbacksFallback.enmInterface = VDINTERFACETYPE_IO;
4024 VDIIOCallbacksFallback.pfnOpen = vdIOOpenFallback;
4025 VDIIOCallbacksFallback.pfnClose = vdIOCloseFallback;
4026 VDIIOCallbacksFallback.pfnDelete = vdIODeleteFallback;
4027 VDIIOCallbacksFallback.pfnMove = vdIOMoveFallback;
4028 VDIIOCallbacksFallback.pfnGetFreeSpace = vdIOGetFreeSpaceFallback;
4029 VDIIOCallbacksFallback.pfnGetModificationTime = vdIOGetModificationTimeFallback;
4030 VDIIOCallbacksFallback.pfnGetSize = vdIOGetSizeFallback;
4031 VDIIOCallbacksFallback.pfnSetSize = vdIOSetSizeFallback;
4032 VDIIOCallbacksFallback.pfnReadSync = vdIOReadSyncFallback;
4033 VDIIOCallbacksFallback.pfnWriteSync = vdIOWriteSyncFallback;
4034 VDIIOCallbacksFallback.pfnFlushSync = vdIOFlushSyncFallback;
4035 pInterfaceIOCallbacks = &VDIIOCallbacksFallback;
4036 }
4037 else
4038 pInterfaceIOCallbacks = VDGetInterfaceIO(pInterfaceIO);
4039
4040 /* Set up the internal I/O interface. */
4041 AssertReturn(!VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IOINT),
4042 VERR_INVALID_PARAMETER);
4043 VDIIOIntCallbacks.cbSize = sizeof(VDINTERFACEIOINT);
4044 VDIIOIntCallbacks.enmInterface = VDINTERFACETYPE_IOINT;
4045 VDIIOIntCallbacks.pfnOpen = vdIOIntOpenLimited;
4046 VDIIOIntCallbacks.pfnClose = vdIOIntCloseLimited;
4047 VDIIOIntCallbacks.pfnDelete = vdIOIntDeleteLimited;
4048 VDIIOIntCallbacks.pfnMove = vdIOIntMoveLimited;
4049 VDIIOIntCallbacks.pfnGetFreeSpace = vdIOIntGetFreeSpaceLimited;
4050 VDIIOIntCallbacks.pfnGetModificationTime = vdIOIntGetModificationTimeLimited;
4051 VDIIOIntCallbacks.pfnGetSize = vdIOIntGetSizeLimited;
4052 VDIIOIntCallbacks.pfnSetSize = vdIOIntSetSizeLimited;
4053 VDIIOIntCallbacks.pfnReadSync = vdIOIntReadSyncLimited;
4054 VDIIOIntCallbacks.pfnWriteSync = vdIOIntWriteSyncLimited;
4055 VDIIOIntCallbacks.pfnFlushSync = vdIOIntFlushSyncLimited;
4056 VDIIOIntCallbacks.pfnReadUserAsync = NULL;
4057 VDIIOIntCallbacks.pfnWriteUserAsync = NULL;
4058 VDIIOIntCallbacks.pfnReadMetaAsync = NULL;
4059 VDIIOIntCallbacks.pfnWriteMetaAsync = NULL;
4060 VDIIOIntCallbacks.pfnFlushAsync = NULL;
4061 rc = VDInterfaceAdd(&VDIIOInt, "VD_IOINT", VDINTERFACETYPE_IOINT,
4062 &VDIIOIntCallbacks, pInterfaceIOCallbacks, &pVDIfsImage);
4063 AssertRC(rc);
4064
4065 /* Find the backend supporting this file format. */
4066 for (unsigned i = 0; i < g_cBackends; i++)
4067 {
4068 if (g_apBackends[i]->pfnCheckIfValid)
4069 {
4070 rc = g_apBackends[i]->pfnCheckIfValid(pszFilename, pVDIfsDisk,
4071 pVDIfsImage, penmType);
4072 if ( RT_SUCCESS(rc)
4073 /* The correct backend has been found, but there is a small
4074 * incompatibility so that the file cannot be used. Stop here
4075 * and signal success - the actual open will of course fail,
4076 * but that will create a really sensible error message. */
4077 || ( rc != VERR_VD_GEN_INVALID_HEADER
4078 && rc != VERR_VD_VDI_INVALID_HEADER
4079 && rc != VERR_VD_VMDK_INVALID_HEADER
4080 && rc != VERR_VD_ISCSI_INVALID_HEADER
4081 && rc != VERR_VD_VHD_INVALID_HEADER
4082 && rc != VERR_VD_RAW_INVALID_HEADER
4083 && rc != VERR_VD_PARALLELS_INVALID_HEADER
4084 && rc != VERR_VD_DMG_INVALID_HEADER))
4085 {
4086 /* Copy the name into the new string. */
4087 char *pszFormat = RTStrDup(g_apBackends[i]->pszBackendName);
4088 if (!pszFormat)
4089 {
4090 rc = VERR_NO_MEMORY;
4091 break;
4092 }
4093 *ppszFormat = pszFormat;
4094 rc = VINF_SUCCESS;
4095 break;
4096 }
4097 rc = VERR_NOT_SUPPORTED;
4098 }
4099 }
4100
4101 /* Try the cache backends. */
4102 if (rc == VERR_NOT_SUPPORTED)
4103 {
4104 for (unsigned i = 0; i < g_cCacheBackends; i++)
4105 {
4106 if (g_apCacheBackends[i]->pfnProbe)
4107 {
4108 rc = g_apCacheBackends[i]->pfnProbe(pszFilename, pVDIfsDisk,
4109 pVDIfsImage);
4110 if ( RT_SUCCESS(rc)
4111 || (rc != VERR_VD_GEN_INVALID_HEADER))
4112 {
4113 /* Copy the name into the new string. */
4114 char *pszFormat = RTStrDup(g_apBackends[i]->pszBackendName);
4115 if (!pszFormat)
4116 {
4117 rc = VERR_NO_MEMORY;
4118 break;
4119 }
4120 *ppszFormat = pszFormat;
4121 rc = VINF_SUCCESS;
4122 break;
4123 }
4124 rc = VERR_NOT_SUPPORTED;
4125 }
4126 }
4127 }
4128
4129 LogFlowFunc(("returns %Rrc *ppszFormat=\"%s\"\n", rc, *ppszFormat));
4130 return rc;
4131}
4132
4133/**
4134 * Opens an image file.
4135 *
4136 * The first opened image file in HDD container must have a base image type,
4137 * others (next opened images) must be a differencing or undo images.
4138 * Linkage is checked for differencing image to be in consistence with the previously opened image.
4139 * When another differencing image is opened and the last image was opened in read/write access
4140 * mode, then the last image is reopened in read-only with deny write sharing mode. This allows
4141 * other processes to use images in read-only mode too.
4142 *
4143 * Note that the image is opened in read-only mode if a read/write open is not possible.
4144 * Use VDIsReadOnly to check open mode.
4145 *
4146 * @returns VBox status code.
4147 * @param pDisk Pointer to HDD container.
4148 * @param pszBackend Name of the image file backend to use.
4149 * @param pszFilename Name of the image file to open.
4150 * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
4151 * @param pVDIfsImage Pointer to the per-image VD interface list.
4152 */
4153VBOXDDU_DECL(int) VDOpen(PVBOXHDD pDisk, const char *pszBackend,
4154 const char *pszFilename, unsigned uOpenFlags,
4155 PVDINTERFACE pVDIfsImage)
4156{
4157 int rc = VINF_SUCCESS;
4158 int rc2;
4159 bool fLockWrite = false;
4160 PVDIMAGE pImage = NULL;
4161
4162 LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uOpenFlags=%#x, pVDIfsImage=%#p\n",
4163 pDisk, pszBackend, pszFilename, uOpenFlags, pVDIfsImage));
4164
4165 do
4166 {
4167 /* sanity check */
4168 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
4169 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
4170
4171 /* Check arguments. */
4172 AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
4173 ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
4174 rc = VERR_INVALID_PARAMETER);
4175 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
4176 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
4177 rc = VERR_INVALID_PARAMETER);
4178 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
4179 ("uOpenFlags=%#x\n", uOpenFlags),
4180 rc = VERR_INVALID_PARAMETER);
4181
4182 /* Set up image descriptor. */
4183 pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
4184 if (!pImage)
4185 {
4186 rc = VERR_NO_MEMORY;
4187 break;
4188 }
4189 pImage->pszFilename = RTStrDup(pszFilename);
4190 if (!pImage->pszFilename)
4191 {
4192 rc = VERR_NO_MEMORY;
4193 break;
4194 }
4195
4196 pImage->VDIo.pDisk = pDisk;
4197 pImage->pVDIfsImage = pVDIfsImage;
4198
4199 rc = vdFindBackend(pszBackend, &pImage->Backend);
4200 if (RT_FAILURE(rc))
4201 break;
4202 if (!pImage->Backend)
4203 {
4204 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
4205 N_("VD: unknown backend name '%s'"), pszBackend);
4206 break;
4207 }
4208
4209 /* Set up the I/O interface. */
4210 pImage->VDIo.pInterfaceIO = VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IO);
4211 if (pImage->VDIo.pInterfaceIO)
4212 pImage->VDIo.pInterfaceIOCallbacks = VDGetInterfaceIO(pImage->VDIo.pInterfaceIO);
4213 else
4214 {
4215 rc = VDInterfaceAdd(&pImage->VDIo.VDIIO, "VD_IO", VDINTERFACETYPE_IO,
4216 &pDisk->VDIIOCallbacks, pDisk, &pVDIfsImage);
4217 pImage->VDIo.pInterfaceIO = &pImage->VDIo.VDIIO;
4218 pImage->VDIo.pInterfaceIOCallbacks = &pDisk->VDIIOCallbacks;
4219 }
4220
4221 /* Set up the internal I/O interface. */
4222 AssertBreakStmt(!VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IOINT),
4223 rc = VERR_INVALID_PARAMETER);
4224 rc = VDInterfaceAdd(&pImage->VDIo.VDIIOInt, "VD_IOINT", VDINTERFACETYPE_IOINT,
4225 &pDisk->VDIIOIntCallbacks, &pImage->VDIo, &pImage->pVDIfsImage);
4226 AssertRC(rc);
4227
4228 pImage->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
4229 rc = pImage->Backend->pfnOpen(pImage->pszFilename,
4230 uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
4231 pDisk->pVDIfsDisk,
4232 pImage->pVDIfsImage,
4233 pDisk->enmType,
4234 &pImage->pBackendData);
4235 /* If the open in read-write mode failed, retry in read-only mode. */
4236 if (RT_FAILURE(rc))
4237 {
4238 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY)
4239 && ( rc == VERR_ACCESS_DENIED
4240 || rc == VERR_PERMISSION_DENIED
4241 || rc == VERR_WRITE_PROTECT
4242 || rc == VERR_SHARING_VIOLATION
4243 || rc == VERR_FILE_LOCK_FAILED))
4244 rc = pImage->Backend->pfnOpen(pImage->pszFilename,
4245 (uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME)
4246 | VD_OPEN_FLAGS_READONLY,
4247 pDisk->pVDIfsDisk,
4248 pImage->pVDIfsImage,
4249 pDisk->enmType,
4250 &pImage->pBackendData);
4251 if (RT_FAILURE(rc))
4252 {
4253 rc = vdError(pDisk, rc, RT_SRC_POS,
4254 N_("VD: error %Rrc opening image file '%s'"), rc, pszFilename);
4255 break;
4256 }
4257 }
4258
4259 /* Lock disk for writing, as we modify pDisk information below. */
4260 rc2 = vdThreadStartWrite(pDisk);
4261 AssertRC(rc2);
4262 fLockWrite = true;
4263
4264 pImage->VDIo.pBackendData = pImage->pBackendData;
4265
4266 /* Check image type. As the image itself has only partial knowledge
4267 * whether it's a base image or not, this info is derived here. The
4268 * base image can be fixed or normal, all others must be normal or
4269 * diff images. Some image formats don't distinguish between normal
4270 * and diff images, so this must be corrected here. */
4271 unsigned uImageFlags;
4272 uImageFlags = pImage->Backend->pfnGetImageFlags(pImage->pBackendData);
4273 if (RT_FAILURE(rc))
4274 uImageFlags = VD_IMAGE_FLAGS_NONE;
4275 if ( RT_SUCCESS(rc)
4276 && !(uOpenFlags & VD_OPEN_FLAGS_INFO))
4277 {
4278 if ( pDisk->cImages == 0
4279 && (uImageFlags & VD_IMAGE_FLAGS_DIFF))
4280 {
4281 rc = VERR_VD_INVALID_TYPE;
4282 break;
4283 }
4284 else if (pDisk->cImages != 0)
4285 {
4286 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
4287 {
4288 rc = VERR_VD_INVALID_TYPE;
4289 break;
4290 }
4291 else
4292 uImageFlags |= VD_IMAGE_FLAGS_DIFF;
4293 }
4294 }
4295
4296 /* Ensure we always get correct diff information, even if the backend
4297 * doesn't actually have a stored flag for this. It must not return
4298 * bogus information for the parent UUID if it is not a diff image. */
4299 RTUUID parentUuid;
4300 RTUuidClear(&parentUuid);
4301 rc2 = pImage->Backend->pfnGetParentUuid(pImage->pBackendData, &parentUuid);
4302 if (RT_SUCCESS(rc2) && !RTUuidIsNull(&parentUuid))
4303 uImageFlags |= VD_IMAGE_FLAGS_DIFF;
4304
4305 pImage->uImageFlags = uImageFlags;
4306
4307 /* Force sane optimization settings. It's not worth avoiding writes
4308 * to fixed size images. The overhead would have almost no payback. */
4309 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
4310 pImage->uOpenFlags |= VD_OPEN_FLAGS_HONOR_SAME;
4311
4312 /** @todo optionally check UUIDs */
4313
4314 /* Cache disk information. */
4315 pDisk->cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
4316
4317 /* Cache PCHS geometry. */
4318 rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
4319 &pDisk->PCHSGeometry);
4320 if (RT_FAILURE(rc2))
4321 {
4322 pDisk->PCHSGeometry.cCylinders = 0;
4323 pDisk->PCHSGeometry.cHeads = 0;
4324 pDisk->PCHSGeometry.cSectors = 0;
4325 }
4326 else
4327 {
4328 /* Make sure the PCHS geometry is properly clipped. */
4329 pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
4330 pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
4331 pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
4332 }
4333
4334 /* Cache LCHS geometry. */
4335 rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
4336 &pDisk->LCHSGeometry);
4337 if (RT_FAILURE(rc2))
4338 {
4339 pDisk->LCHSGeometry.cCylinders = 0;
4340 pDisk->LCHSGeometry.cHeads = 0;
4341 pDisk->LCHSGeometry.cSectors = 0;
4342 }
4343 else
4344 {
4345 /* Make sure the LCHS geometry is properly clipped. */
4346 pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
4347 pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
4348 }
4349
4350 if (pDisk->cImages != 0)
4351 {
4352 /* Switch previous image to read-only mode. */
4353 unsigned uOpenFlagsPrevImg;
4354 uOpenFlagsPrevImg = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pBackendData);
4355 if (!(uOpenFlagsPrevImg & VD_OPEN_FLAGS_READONLY))
4356 {
4357 uOpenFlagsPrevImg |= VD_OPEN_FLAGS_READONLY;
4358 rc = pDisk->pLast->Backend->pfnSetOpenFlags(pDisk->pLast->pBackendData, uOpenFlagsPrevImg);
4359 }
4360 }
4361
4362 if (RT_SUCCESS(rc))
4363 {
4364 /* Image successfully opened, make it the last image. */
4365 vdAddImageToList(pDisk, pImage);
4366 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
4367 pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
4368 }
4369 else
4370 {
4371 /* Error detected, but image opened. Close image. */
4372 rc2 = pImage->Backend->pfnClose(pImage->pBackendData, false);
4373 AssertRC(rc2);
4374 pImage->pBackendData = NULL;
4375 }
4376 } while (0);
4377
4378 if (RT_UNLIKELY(fLockWrite))
4379 {
4380 rc2 = vdThreadFinishWrite(pDisk);
4381 AssertRC(rc2);
4382 }
4383
4384 if (RT_FAILURE(rc))
4385 {
4386 if (pImage)
4387 {
4388 if (pImage->pszFilename)
4389 RTStrFree(pImage->pszFilename);
4390 RTMemFree(pImage);
4391 }
4392 }
4393
4394 LogFlowFunc(("returns %Rrc\n", rc));
4395 return rc;
4396}
4397
4398/**
4399 * Opens a cache image.
4400 *
4401 * @return VBox status code.
4402 * @param pDisk Pointer to the HDD container which should use the cache image.
4403 * @param pszBackend Name of the cache file backend to use (case insensitive).
4404 * @param pszFilename Name of the cache image to open.
4405 * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
4406 * @param pVDIfsCache Pointer to the per-cache VD interface list.
4407 */
4408VBOXDDU_DECL(int) VDCacheOpen(PVBOXHDD pDisk, const char *pszBackend,
4409 const char *pszFilename, unsigned uOpenFlags,
4410 PVDINTERFACE pVDIfsCache)
4411{
4412 int rc = VINF_SUCCESS;
4413 int rc2;
4414 bool fLockWrite = false;
4415 PVDCACHE pCache = NULL;
4416
4417 LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uOpenFlags=%#x, pVDIfsCache=%#p\n",
4418 pDisk, pszBackend, pszFilename, uOpenFlags, pVDIfsCache));
4419
4420 do
4421 {
4422 /* sanity check */
4423 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
4424 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
4425
4426 /* Check arguments. */
4427 AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
4428 ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
4429 rc = VERR_INVALID_PARAMETER);
4430 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
4431 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
4432 rc = VERR_INVALID_PARAMETER);
4433 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
4434 ("uOpenFlags=%#x\n", uOpenFlags),
4435 rc = VERR_INVALID_PARAMETER);
4436
4437 /* Set up image descriptor. */
4438 pCache = (PVDCACHE)RTMemAllocZ(sizeof(VDCACHE));
4439 if (!pCache)
4440 {
4441 rc = VERR_NO_MEMORY;
4442 break;
4443 }
4444 pCache->pszFilename = RTStrDup(pszFilename);
4445 if (!pCache->pszFilename)
4446 {
4447 rc = VERR_NO_MEMORY;
4448 break;
4449 }
4450
4451 pCache->VDIo.pDisk = pDisk;
4452 pCache->pVDIfsCache = pVDIfsCache;
4453
4454 rc = vdFindCacheBackend(pszBackend, &pCache->Backend);
4455 if (RT_FAILURE(rc))
4456 break;
4457 if (!pCache->Backend)
4458 {
4459 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
4460 N_("VD: unknown backend name '%s'"), pszBackend);
4461 break;
4462 }
4463
4464 /* Set up the I/O interface. */
4465 pCache->VDIo.pInterfaceIO = VDInterfaceGet(pVDIfsCache, VDINTERFACETYPE_IO);
4466 if (pCache->VDIo.pInterfaceIO)
4467 pCache->VDIo.pInterfaceIOCallbacks = VDGetInterfaceIO(pCache->VDIo.pInterfaceIO);
4468 else
4469 {
4470 rc = VDInterfaceAdd(&pCache->VDIo.VDIIO, "VD_IO", VDINTERFACETYPE_IO,
4471 &pDisk->VDIIOCallbacks, pDisk, &pVDIfsCache);
4472 pCache->VDIo.pInterfaceIO = &pCache->VDIo.VDIIO;
4473 pCache->VDIo.pInterfaceIOCallbacks = &pDisk->VDIIOCallbacks;
4474 }
4475
4476 /* Set up the internal I/O interface. */
4477 AssertBreakStmt(!VDInterfaceGet(pVDIfsCache, VDINTERFACETYPE_IOINT),
4478 rc = VERR_INVALID_PARAMETER);
4479 rc = VDInterfaceAdd(&pCache->VDIo.VDIIOInt, "VD_IOINT", VDINTERFACETYPE_IOINT,
4480 &pDisk->VDIIOIntCallbacks, &pCache->VDIo, &pCache->pVDIfsCache);
4481 AssertRC(rc);
4482
4483 pCache->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
4484 rc = pCache->Backend->pfnOpen(pCache->pszFilename,
4485 uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
4486 pDisk->pVDIfsDisk,
4487 pCache->pVDIfsCache,
4488 &pCache->pBackendData);
4489 /* If the open in read-write mode failed, retry in read-only mode. */
4490 if (RT_FAILURE(rc))
4491 {
4492 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY)
4493 && ( rc == VERR_ACCESS_DENIED
4494 || rc == VERR_PERMISSION_DENIED
4495 || rc == VERR_WRITE_PROTECT
4496 || rc == VERR_SHARING_VIOLATION
4497 || rc == VERR_FILE_LOCK_FAILED))
4498 rc = pCache->Backend->pfnOpen(pCache->pszFilename,
4499 (uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME)
4500 | VD_OPEN_FLAGS_READONLY,
4501 pDisk->pVDIfsDisk,
4502 pCache->pVDIfsCache,
4503 &pCache->pBackendData);
4504 if (RT_FAILURE(rc))
4505 {
4506 rc = vdError(pDisk, rc, RT_SRC_POS,
4507 N_("VD: error %Rrc opening image file '%s'"), rc, pszFilename);
4508 break;
4509 }
4510 }
4511
4512 /* Lock disk for writing, as we modify pDisk information below. */
4513 rc2 = vdThreadStartWrite(pDisk);
4514 AssertRC(rc2);
4515 fLockWrite = true;
4516
4517 /*
4518 * Check that the modification UUID of the cache and last image
4519 * match. If not the image was modified in-between without the cache.
4520 * The cache might contain stale data.
4521 */
4522 RTUUID UuidImage, UuidCache;
4523
4524 rc = pCache->Backend->pfnGetModificationUuid(pCache->pBackendData,
4525 &UuidCache);
4526 if (RT_SUCCESS(rc))
4527 {
4528 rc = pDisk->pLast->Backend->pfnGetModificationUuid(pDisk->pLast->pBackendData,
4529 &UuidImage);
4530 if (RT_SUCCESS(rc))
4531 {
4532 if (RTUuidCompare(&UuidImage, &UuidCache))
4533 rc = VERR_VD_CACHE_NOT_UP_TO_DATE;
4534 }
4535 }
4536
4537 /*
4538 * We assume that the user knows what he is doing if one of the images
4539 * doesn't support the modification uuid.
4540 */
4541 if (rc == VERR_NOT_SUPPORTED)
4542 rc = VINF_SUCCESS;
4543
4544 if (RT_SUCCESS(rc))
4545 {
4546 /* Cache successfully opened, make it the current one. */
4547 if (!pDisk->pCache)
4548 pDisk->pCache = pCache;
4549 else
4550 rc = VERR_VD_CACHE_ALREADY_EXISTS;
4551 }
4552
4553 if (RT_FAILURE(rc))
4554 {
4555 /* Error detected, but image opened. Close image. */
4556 rc2 = pCache->Backend->pfnClose(pCache->pBackendData, false);
4557 AssertRC(rc2);
4558 pCache->pBackendData = NULL;
4559 }
4560 } while (0);
4561
4562 if (RT_UNLIKELY(fLockWrite))
4563 {
4564 rc2 = vdThreadFinishWrite(pDisk);
4565 AssertRC(rc2);
4566 }
4567
4568 if (RT_FAILURE(rc))
4569 {
4570 if (pCache)
4571 {
4572 if (pCache->pszFilename)
4573 RTStrFree(pCache->pszFilename);
4574 RTMemFree(pCache);
4575 }
4576 }
4577
4578 LogFlowFunc(("returns %Rrc\n", rc));
4579 return rc;
4580}
4581
4582/**
4583 * Creates and opens a new base image file.
4584 *
4585 * @returns VBox status code.
4586 * @param pDisk Pointer to HDD container.
4587 * @param pszBackend Name of the image file backend to use.
4588 * @param pszFilename Name of the image file to create.
4589 * @param cbSize Image size in bytes.
4590 * @param uImageFlags Flags specifying special image features.
4591 * @param pszComment Pointer to image comment. NULL is ok.
4592 * @param pPCHSGeometry Pointer to physical disk geometry <= (16383,16,63). Not NULL.
4593 * @param pLCHSGeometry Pointer to logical disk geometry <= (x,255,63). Not NULL.
4594 * @param pUuid New UUID of the image. If NULL, a new UUID is created.
4595 * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
4596 * @param pVDIfsImage Pointer to the per-image VD interface list.
4597 * @param pVDIfsOperation Pointer to the per-operation VD interface list.
4598 */
4599VBOXDDU_DECL(int) VDCreateBase(PVBOXHDD pDisk, const char *pszBackend,
4600 const char *pszFilename, uint64_t cbSize,
4601 unsigned uImageFlags, const char *pszComment,
4602 PCVDGEOMETRY pPCHSGeometry,
4603 PCVDGEOMETRY pLCHSGeometry,
4604 PCRTUUID pUuid, unsigned uOpenFlags,
4605 PVDINTERFACE pVDIfsImage,
4606 PVDINTERFACE pVDIfsOperation)
4607{
4608 int rc = VINF_SUCCESS;
4609 int rc2;
4610 bool fLockWrite = false, fLockRead = false;
4611 PVDIMAGE pImage = NULL;
4612 RTUUID uuid;
4613
4614 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",
4615 pDisk, pszBackend, pszFilename, cbSize, uImageFlags, pszComment,
4616 pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
4617 pPCHSGeometry->cSectors, pLCHSGeometry->cCylinders,
4618 pLCHSGeometry->cHeads, pLCHSGeometry->cSectors, pUuid,
4619 uOpenFlags, pVDIfsImage, pVDIfsOperation));
4620
4621 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
4622 VDINTERFACETYPE_PROGRESS);
4623 PVDINTERFACEPROGRESS pCbProgress = NULL;
4624 if (pIfProgress)
4625 pCbProgress = VDGetInterfaceProgress(pIfProgress);
4626
4627 do
4628 {
4629 /* sanity check */
4630 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
4631 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
4632
4633 /* Check arguments. */
4634 AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
4635 ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
4636 rc = VERR_INVALID_PARAMETER);
4637 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
4638 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
4639 rc = VERR_INVALID_PARAMETER);
4640 AssertMsgBreakStmt(cbSize,
4641 ("cbSize=%llu\n", cbSize),
4642 rc = VERR_INVALID_PARAMETER);
4643 AssertMsgBreakStmt( ((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0)
4644 || ((uImageFlags & (VD_IMAGE_FLAGS_FIXED | VD_IMAGE_FLAGS_DIFF)) != VD_IMAGE_FLAGS_FIXED),
4645 ("uImageFlags=%#x\n", uImageFlags),
4646 rc = VERR_INVALID_PARAMETER);
4647 /* The PCHS geometry fields may be 0 to leave it for later. */
4648 AssertMsgBreakStmt( VALID_PTR(pPCHSGeometry)
4649 && pPCHSGeometry->cHeads <= 16
4650 && pPCHSGeometry->cSectors <= 63,
4651 ("pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pPCHSGeometry,
4652 pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
4653 pPCHSGeometry->cSectors),
4654 rc = VERR_INVALID_PARAMETER);
4655 /* The LCHS geometry fields may be 0 to leave it to later autodetection. */
4656 AssertMsgBreakStmt( VALID_PTR(pLCHSGeometry)
4657 && pLCHSGeometry->cHeads <= 255
4658 && pLCHSGeometry->cSectors <= 63,
4659 ("pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pLCHSGeometry,
4660 pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads,
4661 pLCHSGeometry->cSectors),
4662 rc = VERR_INVALID_PARAMETER);
4663 /* The UUID may be NULL. */
4664 AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
4665 ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
4666 rc = VERR_INVALID_PARAMETER);
4667 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
4668 ("uOpenFlags=%#x\n", uOpenFlags),
4669 rc = VERR_INVALID_PARAMETER);
4670
4671 /* Check state. Needs a temporary read lock. Holding the write lock
4672 * all the time would be blocking other activities for too long. */
4673 rc2 = vdThreadStartRead(pDisk);
4674 AssertRC(rc2);
4675 fLockRead = true;
4676 AssertMsgBreakStmt(pDisk->cImages == 0,
4677 ("Create base image cannot be done with other images open\n"),
4678 rc = VERR_VD_INVALID_STATE);
4679 rc2 = vdThreadFinishRead(pDisk);
4680 AssertRC(rc2);
4681 fLockRead = false;
4682
4683 /* Set up image descriptor. */
4684 pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
4685 if (!pImage)
4686 {
4687 rc = VERR_NO_MEMORY;
4688 break;
4689 }
4690 pImage->pszFilename = RTStrDup(pszFilename);
4691 if (!pImage->pszFilename)
4692 {
4693 rc = VERR_NO_MEMORY;
4694 break;
4695 }
4696 pImage->VDIo.pDisk = pDisk;
4697 pImage->pVDIfsImage = pVDIfsImage;
4698
4699 /* Set up the I/O interface. */
4700 pImage->VDIo.pInterfaceIO = VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IO);
4701 if (pImage->VDIo.pInterfaceIO)
4702 pImage->VDIo.pInterfaceIOCallbacks = VDGetInterfaceIO(pImage->VDIo.pInterfaceIO);
4703 else
4704 {
4705 rc = VDInterfaceAdd(&pImage->VDIo.VDIIO, "VD_IO", VDINTERFACETYPE_IO,
4706 &pDisk->VDIIOCallbacks, pDisk, &pVDIfsImage);
4707 pImage->VDIo.pInterfaceIO = &pImage->VDIo.VDIIO;
4708 pImage->VDIo.pInterfaceIOCallbacks = &pDisk->VDIIOCallbacks;
4709 }
4710
4711 /* Set up the internal I/O interface. */
4712 AssertBreakStmt(!VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IOINT),
4713 rc = VERR_INVALID_PARAMETER);
4714 rc = VDInterfaceAdd(&pImage->VDIo.VDIIOInt, "VD_IOINT", VDINTERFACETYPE_IOINT,
4715 &pDisk->VDIIOIntCallbacks, &pImage->VDIo, &pImage->pVDIfsImage);
4716 AssertRC(rc);
4717
4718 rc = vdFindBackend(pszBackend, &pImage->Backend);
4719 if (RT_FAILURE(rc))
4720 break;
4721 if (!pImage->Backend)
4722 {
4723 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
4724 N_("VD: unknown backend name '%s'"), pszBackend);
4725 break;
4726 }
4727 if (!(pImage->Backend->uBackendCaps & ( VD_CAP_CREATE_FIXED
4728 | VD_CAP_CREATE_DYNAMIC)))
4729 {
4730 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
4731 N_("VD: backend '%s' cannot create base images"), pszBackend);
4732 break;
4733 }
4734
4735 /* Create UUID if the caller didn't specify one. */
4736 if (!pUuid)
4737 {
4738 rc = RTUuidCreate(&uuid);
4739 if (RT_FAILURE(rc))
4740 {
4741 rc = vdError(pDisk, rc, RT_SRC_POS,
4742 N_("VD: cannot generate UUID for image '%s'"),
4743 pszFilename);
4744 break;
4745 }
4746 pUuid = &uuid;
4747 }
4748
4749 pImage->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
4750 uImageFlags &= ~VD_IMAGE_FLAGS_DIFF;
4751 rc = pImage->Backend->pfnCreate(pImage->pszFilename, cbSize,
4752 uImageFlags, pszComment, pPCHSGeometry,
4753 pLCHSGeometry, pUuid,
4754 uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
4755 0, 99,
4756 pDisk->pVDIfsDisk,
4757 pImage->pVDIfsImage,
4758 pVDIfsOperation,
4759 &pImage->pBackendData);
4760
4761 if (RT_SUCCESS(rc))
4762 {
4763 pImage->VDIo.pBackendData = pImage->pBackendData;
4764 pImage->uImageFlags = uImageFlags;
4765
4766 /* Force sane optimization settings. It's not worth avoiding writes
4767 * to fixed size images. The overhead would have almost no payback. */
4768 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
4769 pImage->uOpenFlags |= VD_OPEN_FLAGS_HONOR_SAME;
4770
4771 /* Lock disk for writing, as we modify pDisk information below. */
4772 rc2 = vdThreadStartWrite(pDisk);
4773 AssertRC(rc2);
4774 fLockWrite = true;
4775
4776 /** @todo optionally check UUIDs */
4777
4778 /* Re-check state, as the lock wasn't held and another image
4779 * creation call could have been done by another thread. */
4780 AssertMsgStmt(pDisk->cImages == 0,
4781 ("Create base image cannot be done with other images open\n"),
4782 rc = VERR_VD_INVALID_STATE);
4783 }
4784
4785 if (RT_SUCCESS(rc))
4786 {
4787 /* Cache disk information. */
4788 pDisk->cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
4789
4790 /* Cache PCHS geometry. */
4791 rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
4792 &pDisk->PCHSGeometry);
4793 if (RT_FAILURE(rc2))
4794 {
4795 pDisk->PCHSGeometry.cCylinders = 0;
4796 pDisk->PCHSGeometry.cHeads = 0;
4797 pDisk->PCHSGeometry.cSectors = 0;
4798 }
4799 else
4800 {
4801 /* Make sure the CHS geometry is properly clipped. */
4802 pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
4803 pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
4804 pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
4805 }
4806
4807 /* Cache LCHS geometry. */
4808 rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
4809 &pDisk->LCHSGeometry);
4810 if (RT_FAILURE(rc2))
4811 {
4812 pDisk->LCHSGeometry.cCylinders = 0;
4813 pDisk->LCHSGeometry.cHeads = 0;
4814 pDisk->LCHSGeometry.cSectors = 0;
4815 }
4816 else
4817 {
4818 /* Make sure the CHS geometry is properly clipped. */
4819 pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
4820 pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
4821 }
4822
4823 /* Image successfully opened, make it the last image. */
4824 vdAddImageToList(pDisk, pImage);
4825 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
4826 pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
4827 }
4828 else
4829 {
4830 /* Error detected, image may or may not be opened. Close and delete
4831 * image if it was opened. */
4832 if (pImage->pBackendData)
4833 {
4834 rc2 = pImage->Backend->pfnClose(pImage->pBackendData, true);
4835 AssertRC(rc2);
4836 pImage->pBackendData = NULL;
4837 }
4838 }
4839 } while (0);
4840
4841 if (RT_UNLIKELY(fLockWrite))
4842 {
4843 rc2 = vdThreadFinishWrite(pDisk);
4844 AssertRC(rc2);
4845 }
4846 else if (RT_UNLIKELY(fLockRead))
4847 {
4848 rc2 = vdThreadFinishRead(pDisk);
4849 AssertRC(rc2);
4850 }
4851
4852 if (RT_FAILURE(rc))
4853 {
4854 if (pImage)
4855 {
4856 if (pImage->pszFilename)
4857 RTStrFree(pImage->pszFilename);
4858 RTMemFree(pImage);
4859 }
4860 }
4861
4862 if (RT_SUCCESS(rc) && pCbProgress && pCbProgress->pfnProgress)
4863 pCbProgress->pfnProgress(pIfProgress->pvUser, 100);
4864
4865 LogFlowFunc(("returns %Rrc\n", rc));
4866 return rc;
4867}
4868
4869/**
4870 * Creates and opens a new differencing image file in HDD container.
4871 * See comments for VDOpen function about differencing images.
4872 *
4873 * @returns VBox status code.
4874 * @param pDisk Pointer to HDD container.
4875 * @param pszBackend Name of the image file backend to use.
4876 * @param pszFilename Name of the differencing image file to create.
4877 * @param uImageFlags Flags specifying special image features.
4878 * @param pszComment Pointer to image comment. NULL is ok.
4879 * @param pUuid New UUID of the image. If NULL, a new UUID is created.
4880 * @param pParentUuid New parent UUID of the image. If NULL, the UUID is queried automatically.
4881 * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
4882 * @param pVDIfsImage Pointer to the per-image VD interface list.
4883 * @param pVDIfsOperation Pointer to the per-operation VD interface list.
4884 */
4885VBOXDDU_DECL(int) VDCreateDiff(PVBOXHDD pDisk, const char *pszBackend,
4886 const char *pszFilename, unsigned uImageFlags,
4887 const char *pszComment, PCRTUUID pUuid,
4888 PCRTUUID pParentUuid, unsigned uOpenFlags,
4889 PVDINTERFACE pVDIfsImage,
4890 PVDINTERFACE pVDIfsOperation)
4891{
4892 int rc = VINF_SUCCESS;
4893 int rc2;
4894 bool fLockWrite = false, fLockRead = false;
4895 PVDIMAGE pImage = NULL;
4896 RTUUID uuid;
4897
4898 LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uImageFlags=%#x pszComment=\"%s\" Uuid=%RTuuid uOpenFlags=%#x pVDIfsImage=%#p pVDIfsOperation=%#p\n",
4899 pDisk, pszBackend, pszFilename, uImageFlags, pszComment, pUuid, uOpenFlags, pVDIfsImage, pVDIfsOperation));
4900
4901 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
4902 VDINTERFACETYPE_PROGRESS);
4903 PVDINTERFACEPROGRESS pCbProgress = NULL;
4904 if (pIfProgress)
4905 pCbProgress = VDGetInterfaceProgress(pIfProgress);
4906
4907 do
4908 {
4909 /* sanity check */
4910 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
4911 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
4912
4913 /* Check arguments. */
4914 AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
4915 ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
4916 rc = VERR_INVALID_PARAMETER);
4917 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
4918 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
4919 rc = VERR_INVALID_PARAMETER);
4920 AssertMsgBreakStmt((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0,
4921 ("uImageFlags=%#x\n", uImageFlags),
4922 rc = VERR_INVALID_PARAMETER);
4923 /* The UUID may be NULL. */
4924 AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
4925 ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
4926 rc = VERR_INVALID_PARAMETER);
4927 /* The parent UUID may be NULL. */
4928 AssertMsgBreakStmt(pParentUuid == NULL || VALID_PTR(pParentUuid),
4929 ("pParentUuid=%#p ParentUUID=%RTuuid\n", pParentUuid, pParentUuid),
4930 rc = VERR_INVALID_PARAMETER);
4931 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
4932 ("uOpenFlags=%#x\n", uOpenFlags),
4933 rc = VERR_INVALID_PARAMETER);
4934
4935 /* Check state. Needs a temporary read lock. Holding the write lock
4936 * all the time would be blocking other activities for too long. */
4937 rc2 = vdThreadStartRead(pDisk);
4938 AssertRC(rc2);
4939 fLockRead = true;
4940 AssertMsgBreakStmt(pDisk->cImages != 0,
4941 ("Create diff image cannot be done without other images open\n"),
4942 rc = VERR_VD_INVALID_STATE);
4943 rc2 = vdThreadFinishRead(pDisk);
4944 AssertRC(rc2);
4945 fLockRead = false;
4946
4947 /* Set up image descriptor. */
4948 pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
4949 if (!pImage)
4950 {
4951 rc = VERR_NO_MEMORY;
4952 break;
4953 }
4954 pImage->pszFilename = RTStrDup(pszFilename);
4955 if (!pImage->pszFilename)
4956 {
4957 rc = VERR_NO_MEMORY;
4958 break;
4959 }
4960
4961 rc = vdFindBackend(pszBackend, &pImage->Backend);
4962 if (RT_FAILURE(rc))
4963 break;
4964 if (!pImage->Backend)
4965 {
4966 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
4967 N_("VD: unknown backend name '%s'"), pszBackend);
4968 break;
4969 }
4970 if ( !(pImage->Backend->uBackendCaps & VD_CAP_DIFF)
4971 || !(pImage->Backend->uBackendCaps & ( VD_CAP_CREATE_FIXED
4972 | VD_CAP_CREATE_DYNAMIC)))
4973 {
4974 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
4975 N_("VD: backend '%s' cannot create diff images"), pszBackend);
4976 break;
4977 }
4978
4979 pImage->VDIo.pDisk = pDisk;
4980 pImage->pVDIfsImage = pVDIfsImage;
4981
4982 /* Set up the I/O interface. */
4983 pImage->VDIo.pInterfaceIO = VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IO);
4984 if (pImage->VDIo.pInterfaceIO)
4985 pImage->VDIo.pInterfaceIOCallbacks = VDGetInterfaceIO(pImage->VDIo.pInterfaceIO);
4986 else
4987 {
4988 rc = VDInterfaceAdd(&pImage->VDIo.VDIIO, "VD_IO", VDINTERFACETYPE_IO,
4989 &pDisk->VDIIOCallbacks, pDisk, &pVDIfsImage);
4990 pImage->VDIo.pInterfaceIO = &pImage->VDIo.VDIIO;
4991 pImage->VDIo.pInterfaceIOCallbacks = &pDisk->VDIIOCallbacks;
4992 }
4993
4994 /* Set up the internal I/O interface. */
4995 AssertBreakStmt(!VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IOINT),
4996 rc = VERR_INVALID_PARAMETER);
4997 rc = VDInterfaceAdd(&pImage->VDIo.VDIIOInt, "VD_IOINT", VDINTERFACETYPE_IOINT,
4998 &pDisk->VDIIOIntCallbacks, &pImage->VDIo, &pImage->pVDIfsImage);
4999 AssertRC(rc);
5000
5001 /* Create UUID if the caller didn't specify one. */
5002 if (!pUuid)
5003 {
5004 rc = RTUuidCreate(&uuid);
5005 if (RT_FAILURE(rc))
5006 {
5007 rc = vdError(pDisk, rc, RT_SRC_POS,
5008 N_("VD: cannot generate UUID for image '%s'"),
5009 pszFilename);
5010 break;
5011 }
5012 pUuid = &uuid;
5013 }
5014
5015 pImage->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
5016 uImageFlags |= VD_IMAGE_FLAGS_DIFF;
5017 rc = pImage->Backend->pfnCreate(pImage->pszFilename, pDisk->cbSize,
5018 uImageFlags | VD_IMAGE_FLAGS_DIFF,
5019 pszComment, &pDisk->PCHSGeometry,
5020 &pDisk->LCHSGeometry, pUuid,
5021 uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
5022 0, 99,
5023 pDisk->pVDIfsDisk,
5024 pImage->pVDIfsImage,
5025 pVDIfsOperation,
5026 &pImage->pBackendData);
5027
5028 if (RT_SUCCESS(rc))
5029 {
5030 pImage->VDIo.pBackendData = pImage->pBackendData;
5031 pImage->uImageFlags = uImageFlags;
5032
5033 /* Lock disk for writing, as we modify pDisk information below. */
5034 rc2 = vdThreadStartWrite(pDisk);
5035 AssertRC(rc2);
5036 fLockWrite = true;
5037
5038 /* Switch previous image to read-only mode. */
5039 unsigned uOpenFlagsPrevImg;
5040 uOpenFlagsPrevImg = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pBackendData);
5041 if (!(uOpenFlagsPrevImg & VD_OPEN_FLAGS_READONLY))
5042 {
5043 uOpenFlagsPrevImg |= VD_OPEN_FLAGS_READONLY;
5044 rc = pDisk->pLast->Backend->pfnSetOpenFlags(pDisk->pLast->pBackendData, uOpenFlagsPrevImg);
5045 }
5046
5047 /** @todo optionally check UUIDs */
5048
5049 /* Re-check state, as the lock wasn't held and another image
5050 * creation call could have been done by another thread. */
5051 AssertMsgStmt(pDisk->cImages != 0,
5052 ("Create diff image cannot be done without other images open\n"),
5053 rc = VERR_VD_INVALID_STATE);
5054 }
5055
5056 if (RT_SUCCESS(rc))
5057 {
5058 RTUUID Uuid;
5059 RTTIMESPEC ts;
5060
5061 if (pParentUuid && !RTUuidIsNull(pParentUuid))
5062 {
5063 Uuid = *pParentUuid;
5064 pImage->Backend->pfnSetParentUuid(pImage->pBackendData, &Uuid);
5065 }
5066 else
5067 {
5068 rc2 = pDisk->pLast->Backend->pfnGetUuid(pDisk->pLast->pBackendData,
5069 &Uuid);
5070 if (RT_SUCCESS(rc2))
5071 pImage->Backend->pfnSetParentUuid(pImage->pBackendData, &Uuid);
5072 }
5073 rc2 = pDisk->pLast->Backend->pfnGetModificationUuid(pDisk->pLast->pBackendData,
5074 &Uuid);
5075 if (RT_SUCCESS(rc2))
5076 pImage->Backend->pfnSetParentModificationUuid(pImage->pBackendData,
5077 &Uuid);
5078 if (pDisk->pLast->Backend->pfnGetTimeStamp)
5079 rc2 = pDisk->pLast->Backend->pfnGetTimeStamp(pDisk->pLast->pBackendData,
5080 &ts);
5081 else
5082 rc2 = VERR_NOT_IMPLEMENTED;
5083 if (RT_SUCCESS(rc2) && pImage->Backend->pfnSetParentTimeStamp)
5084 pImage->Backend->pfnSetParentTimeStamp(pImage->pBackendData, &ts);
5085
5086 if (pImage->Backend->pfnSetParentFilename)
5087 rc2 = pImage->Backend->pfnSetParentFilename(pImage->pBackendData, pDisk->pLast->pszFilename);
5088 }
5089
5090 if (RT_SUCCESS(rc))
5091 {
5092 /* Image successfully opened, make it the last image. */
5093 vdAddImageToList(pDisk, pImage);
5094 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
5095 pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
5096 }
5097 else
5098 {
5099 /* Error detected, but image opened. Close and delete image. */
5100 rc2 = pImage->Backend->pfnClose(pImage->pBackendData, true);
5101 AssertRC(rc2);
5102 pImage->pBackendData = NULL;
5103 }
5104 } while (0);
5105
5106 if (RT_UNLIKELY(fLockWrite))
5107 {
5108 rc2 = vdThreadFinishWrite(pDisk);
5109 AssertRC(rc2);
5110 }
5111 else if (RT_UNLIKELY(fLockRead))
5112 {
5113 rc2 = vdThreadFinishRead(pDisk);
5114 AssertRC(rc2);
5115 }
5116
5117 if (RT_FAILURE(rc))
5118 {
5119 if (pImage)
5120 {
5121 if (pImage->pszFilename)
5122 RTStrFree(pImage->pszFilename);
5123 RTMemFree(pImage);
5124 }
5125 }
5126
5127 if (RT_SUCCESS(rc) && pCbProgress && pCbProgress->pfnProgress)
5128 pCbProgress->pfnProgress(pIfProgress->pvUser, 100);
5129
5130 LogFlowFunc(("returns %Rrc\n", rc));
5131 return rc;
5132}
5133
5134
5135/**
5136 * Creates and opens new cache image file in HDD container.
5137 *
5138 * @return VBox status code.
5139 * @param pDisk Name of the cache file backend to use (case insensitive).
5140 * @param pszFilename Name of the differencing cache file to create.
5141 * @param cbSize Maximum size of the cache.
5142 * @param uImageFlags Flags specifying special cache features.
5143 * @param pszComment Pointer to image comment. NULL is ok.
5144 * @param pUuid New UUID of the image. If NULL, a new UUID is created.
5145 * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
5146 * @param pVDIfsCache Pointer to the per-cache VD interface list.
5147 * @param pVDIfsOperation Pointer to the per-operation VD interface list.
5148 */
5149VBOXDDU_DECL(int) VDCreateCache(PVBOXHDD pDisk, const char *pszBackend,
5150 const char *pszFilename, uint64_t cbSize,
5151 unsigned uImageFlags, const char *pszComment,
5152 PCRTUUID pUuid, unsigned uOpenFlags,
5153 PVDINTERFACE pVDIfsCache, PVDINTERFACE pVDIfsOperation)
5154{
5155 int rc = VINF_SUCCESS;
5156 int rc2;
5157 bool fLockWrite = false, fLockRead = false;
5158 PVDCACHE pCache = NULL;
5159 RTUUID uuid;
5160
5161 LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" Uuid=%RTuuid uOpenFlags=%#x pVDIfsImage=%#p pVDIfsOperation=%#p\n",
5162 pDisk, pszBackend, pszFilename, cbSize, uImageFlags, pszComment, pUuid, uOpenFlags, pVDIfsCache, pVDIfsOperation));
5163
5164 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
5165 VDINTERFACETYPE_PROGRESS);
5166 PVDINTERFACEPROGRESS pCbProgress = NULL;
5167 if (pIfProgress)
5168 pCbProgress = VDGetInterfaceProgress(pIfProgress);
5169
5170 do
5171 {
5172 /* sanity check */
5173 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
5174 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
5175
5176 /* Check arguments. */
5177 AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
5178 ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
5179 rc = VERR_INVALID_PARAMETER);
5180 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
5181 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
5182 rc = VERR_INVALID_PARAMETER);
5183 AssertMsgBreakStmt(cbSize,
5184 ("cbSize=%llu\n", cbSize),
5185 rc = VERR_INVALID_PARAMETER);
5186 AssertMsgBreakStmt((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0,
5187 ("uImageFlags=%#x\n", uImageFlags),
5188 rc = VERR_INVALID_PARAMETER);
5189 /* The UUID may be NULL. */
5190 AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
5191 ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
5192 rc = VERR_INVALID_PARAMETER);
5193 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
5194 ("uOpenFlags=%#x\n", uOpenFlags),
5195 rc = VERR_INVALID_PARAMETER);
5196
5197 /* Check state. Needs a temporary read lock. Holding the write lock
5198 * all the time would be blocking other activities for too long. */
5199 rc2 = vdThreadStartRead(pDisk);
5200 AssertRC(rc2);
5201 fLockRead = true;
5202 AssertMsgBreakStmt(!pDisk->pCache,
5203 ("Create cache image cannot be done with a cache already attached\n"),
5204 rc = VERR_VD_CACHE_ALREADY_EXISTS);
5205 rc2 = vdThreadFinishRead(pDisk);
5206 AssertRC(rc2);
5207 fLockRead = false;
5208
5209 /* Set up image descriptor. */
5210 pCache = (PVDCACHE)RTMemAllocZ(sizeof(VDCACHE));
5211 if (!pCache)
5212 {
5213 rc = VERR_NO_MEMORY;
5214 break;
5215 }
5216 pCache->pszFilename = RTStrDup(pszFilename);
5217 if (!pCache->pszFilename)
5218 {
5219 rc = VERR_NO_MEMORY;
5220 break;
5221 }
5222
5223 rc = vdFindCacheBackend(pszBackend, &pCache->Backend);
5224 if (RT_FAILURE(rc))
5225 break;
5226 if (!pCache->Backend)
5227 {
5228 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
5229 N_("VD: unknown backend name '%s'"), pszBackend);
5230 break;
5231 }
5232
5233 pCache->VDIo.pDisk = pDisk;
5234 pCache->pVDIfsCache = pVDIfsCache;
5235
5236 /* Set up the I/O interface. */
5237 pCache->VDIo.pInterfaceIO = VDInterfaceGet(pVDIfsCache, VDINTERFACETYPE_IO);
5238 if (pCache->VDIo.pInterfaceIO)
5239 pCache->VDIo.pInterfaceIOCallbacks = VDGetInterfaceIO(pCache->VDIo.pInterfaceIO);
5240 else
5241 {
5242 rc = VDInterfaceAdd(&pCache->VDIo.VDIIO, "VD_IO", VDINTERFACETYPE_IO,
5243 &pDisk->VDIIOCallbacks, pDisk, &pVDIfsCache);
5244 pCache->VDIo.pInterfaceIO = &pCache->VDIo.VDIIO;
5245 pCache->VDIo.pInterfaceIOCallbacks = &pDisk->VDIIOCallbacks;
5246 }
5247
5248 /* Set up the internal I/O interface. */
5249 AssertBreakStmt(!VDInterfaceGet(pVDIfsCache, VDINTERFACETYPE_IOINT),
5250 rc = VERR_INVALID_PARAMETER);
5251 rc = VDInterfaceAdd(&pCache->VDIo.VDIIOInt, "VD_IOINT", VDINTERFACETYPE_IOINT,
5252 &pDisk->VDIIOIntCallbacks, &pCache->VDIo, &pCache->pVDIfsCache);
5253 AssertRC(rc);
5254
5255 /* Create UUID if the caller didn't specify one. */
5256 if (!pUuid)
5257 {
5258 rc = RTUuidCreate(&uuid);
5259 if (RT_FAILURE(rc))
5260 {
5261 rc = vdError(pDisk, rc, RT_SRC_POS,
5262 N_("VD: cannot generate UUID for image '%s'"),
5263 pszFilename);
5264 break;
5265 }
5266 pUuid = &uuid;
5267 }
5268
5269 pCache->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
5270 rc = pCache->Backend->pfnCreate(pCache->pszFilename, cbSize,
5271 uImageFlags,
5272 pszComment, pUuid,
5273 uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
5274 0, 99,
5275 pDisk->pVDIfsDisk,
5276 pCache->pVDIfsCache,
5277 pVDIfsOperation,
5278 &pCache->pBackendData);
5279
5280 if (RT_SUCCESS(rc))
5281 {
5282 /* Lock disk for writing, as we modify pDisk information below. */
5283 rc2 = vdThreadStartWrite(pDisk);
5284 AssertRC(rc2);
5285 fLockWrite = true;
5286
5287 pCache->VDIo.pBackendData = pCache->pBackendData;
5288
5289 /* Re-check state, as the lock wasn't held and another image
5290 * creation call could have been done by another thread. */
5291 AssertMsgStmt(!pDisk->pCache,
5292 ("Create cache image cannot be done with another cache open\n"),
5293 rc = VERR_VD_CACHE_ALREADY_EXISTS);
5294 }
5295
5296 if ( RT_SUCCESS(rc)
5297 && pDisk->pLast)
5298 {
5299 RTUUID UuidModification;
5300
5301 /* Set same modification Uuid as the last image. */
5302 rc = pDisk->pLast->Backend->pfnGetModificationUuid(pDisk->pLast->pBackendData,
5303 &UuidModification);
5304 if (RT_SUCCESS(rc))
5305 {
5306 rc = pCache->Backend->pfnSetModificationUuid(pCache->pBackendData,
5307 &UuidModification);
5308 }
5309
5310 if (rc == VERR_NOT_SUPPORTED)
5311 rc = VINF_SUCCESS;
5312 }
5313
5314 if (RT_SUCCESS(rc))
5315 {
5316 /* Cache successfully created. */
5317 pDisk->pCache = pCache;
5318 }
5319 else
5320 {
5321 /* Error detected, but image opened. Close and delete image. */
5322 rc2 = pCache->Backend->pfnClose(pCache->pBackendData, true);
5323 AssertRC(rc2);
5324 pCache->pBackendData = NULL;
5325 }
5326 } while (0);
5327
5328 if (RT_UNLIKELY(fLockWrite))
5329 {
5330 rc2 = vdThreadFinishWrite(pDisk);
5331 AssertRC(rc2);
5332 }
5333 else if (RT_UNLIKELY(fLockRead))
5334 {
5335 rc2 = vdThreadFinishRead(pDisk);
5336 AssertRC(rc2);
5337 }
5338
5339 if (RT_FAILURE(rc))
5340 {
5341 if (pCache)
5342 {
5343 if (pCache->pszFilename)
5344 RTStrFree(pCache->pszFilename);
5345 RTMemFree(pCache);
5346 }
5347 }
5348
5349 if (RT_SUCCESS(rc) && pCbProgress && pCbProgress->pfnProgress)
5350 pCbProgress->pfnProgress(pIfProgress->pvUser, 100);
5351
5352 LogFlowFunc(("returns %Rrc\n", rc));
5353 return rc;
5354}
5355
5356/**
5357 * Merges two images (not necessarily with direct parent/child relationship).
5358 * As a side effect the source image and potentially the other images which
5359 * are also merged to the destination are deleted from both the disk and the
5360 * images in the HDD container.
5361 *
5362 * @returns VBox status code.
5363 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
5364 * @param pDisk Pointer to HDD container.
5365 * @param nImageFrom Name of the image file to merge from.
5366 * @param nImageTo Name of the image file to merge to.
5367 * @param pVDIfsOperation Pointer to the per-operation VD interface list.
5368 */
5369VBOXDDU_DECL(int) VDMerge(PVBOXHDD pDisk, unsigned nImageFrom,
5370 unsigned nImageTo, PVDINTERFACE pVDIfsOperation)
5371{
5372 int rc = VINF_SUCCESS;
5373 int rc2;
5374 bool fLockWrite = false, fLockRead = false;
5375 void *pvBuf = NULL;
5376
5377 LogFlowFunc(("pDisk=%#p nImageFrom=%u nImageTo=%u pVDIfsOperation=%#p\n",
5378 pDisk, nImageFrom, nImageTo, pVDIfsOperation));
5379
5380 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
5381 VDINTERFACETYPE_PROGRESS);
5382 PVDINTERFACEPROGRESS pCbProgress = NULL;
5383 if (pIfProgress)
5384 pCbProgress = VDGetInterfaceProgress(pIfProgress);
5385
5386 do
5387 {
5388 /* sanity check */
5389 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
5390 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
5391
5392 /* For simplicity reasons lock for writing as the image reopen below
5393 * might need it. After all the reopen is usually needed. */
5394 rc2 = vdThreadStartWrite(pDisk);
5395 AssertRC(rc2);
5396 fLockWrite = true;
5397 PVDIMAGE pImageFrom = vdGetImageByNumber(pDisk, nImageFrom);
5398 PVDIMAGE pImageTo = vdGetImageByNumber(pDisk, nImageTo);
5399 if (!pImageFrom || !pImageTo)
5400 {
5401 rc = VERR_VD_IMAGE_NOT_FOUND;
5402 break;
5403 }
5404 AssertBreakStmt(pImageFrom != pImageTo, rc = VERR_INVALID_PARAMETER);
5405
5406 /* Make sure destination image is writable. */
5407 unsigned uOpenFlags = pImageTo->Backend->pfnGetOpenFlags(pImageTo->pBackendData);
5408 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
5409 {
5410 uOpenFlags &= ~VD_OPEN_FLAGS_READONLY;
5411 rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pBackendData,
5412 uOpenFlags);
5413 if (RT_FAILURE(rc))
5414 break;
5415 }
5416
5417 /* Get size of destination image. */
5418 uint64_t cbSize = pImageTo->Backend->pfnGetSize(pImageTo->pBackendData);
5419 rc2 = vdThreadFinishWrite(pDisk);
5420 AssertRC(rc2);
5421 fLockWrite = false;
5422
5423 /* Allocate tmp buffer. */
5424 pvBuf = RTMemTmpAlloc(VD_MERGE_BUFFER_SIZE);
5425 if (!pvBuf)
5426 {
5427 rc = VERR_NO_MEMORY;
5428 break;
5429 }
5430
5431 /* Merging is done directly on the images itself. This potentially
5432 * causes trouble if the disk is full in the middle of operation. */
5433 if (nImageFrom < nImageTo)
5434 {
5435 /* Merge parent state into child. This means writing all not
5436 * allocated blocks in the destination image which are allocated in
5437 * the images to be merged. */
5438 uint64_t uOffset = 0;
5439 uint64_t cbRemaining = cbSize;
5440 do
5441 {
5442 size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
5443
5444 /* Need to hold the write lock during a read-write operation. */
5445 rc2 = vdThreadStartWrite(pDisk);
5446 AssertRC(rc2);
5447 fLockWrite = true;
5448
5449 rc = pImageTo->Backend->pfnRead(pImageTo->pBackendData,
5450 uOffset, pvBuf, cbThisRead,
5451 &cbThisRead);
5452 if (rc == VERR_VD_BLOCK_FREE)
5453 {
5454 /* Search for image with allocated block. Do not attempt to
5455 * read more than the previous reads marked as valid.
5456 * Otherwise this would return stale data when different
5457 * block sizes are used for the images. */
5458 for (PVDIMAGE pCurrImage = pImageTo->pPrev;
5459 pCurrImage != NULL && pCurrImage != pImageFrom->pPrev && rc == VERR_VD_BLOCK_FREE;
5460 pCurrImage = pCurrImage->pPrev)
5461 {
5462 rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
5463 uOffset, pvBuf,
5464 cbThisRead,
5465 &cbThisRead);
5466 }
5467
5468 if (rc != VERR_VD_BLOCK_FREE)
5469 {
5470 if (RT_FAILURE(rc))
5471 break;
5472 /* Updating the cache is required because this might be a live merge. */
5473 rc = vdWriteHelper(pDisk, pImageTo, pImageFrom->pPrev,
5474 uOffset, pvBuf, cbThisRead,
5475 true /* fUpdateCache */);
5476 if (RT_FAILURE(rc))
5477 break;
5478 }
5479 else
5480 rc = VINF_SUCCESS;
5481 }
5482 else if (RT_FAILURE(rc))
5483 break;
5484
5485 rc2 = vdThreadFinishWrite(pDisk);
5486 AssertRC(rc2);
5487 fLockWrite = false;
5488
5489 uOffset += cbThisRead;
5490 cbRemaining -= cbThisRead;
5491
5492 if (pCbProgress && pCbProgress->pfnProgress)
5493 {
5494 /** @todo r=klaus: this can update the progress to the same
5495 * percentage over and over again if the image format makes
5496 * relatively small increments. */
5497 rc = pCbProgress->pfnProgress(pIfProgress->pvUser,
5498 uOffset * 99 / cbSize);
5499 if (RT_FAILURE(rc))
5500 break;
5501 }
5502 } while (uOffset < cbSize);
5503 }
5504 else
5505 {
5506 /*
5507 * We may need to update the parent uuid of the child coming after
5508 * the last image to be merged. We have to reopen it read/write.
5509 *
5510 * This is done before we do the actual merge to prevent an
5511 * inconsistent chain if the mode change fails for some reason.
5512 */
5513 if (pImageFrom->pNext)
5514 {
5515 PVDIMAGE pImageChild = pImageFrom->pNext;
5516
5517 /* Take the write lock. */
5518 rc2 = vdThreadStartWrite(pDisk);
5519 AssertRC(rc2);
5520 fLockWrite = true;
5521
5522 /* We need to open the image in read/write mode. */
5523 uOpenFlags = pImageChild->Backend->pfnGetOpenFlags(pImageChild->pBackendData);
5524
5525 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
5526 {
5527 uOpenFlags &= ~VD_OPEN_FLAGS_READONLY;
5528 rc = pImageChild->Backend->pfnSetOpenFlags(pImageChild->pBackendData,
5529 uOpenFlags);
5530 if (RT_FAILURE(rc))
5531 break;
5532 }
5533
5534 rc2 = vdThreadFinishWrite(pDisk);
5535 AssertRC(rc2);
5536 fLockWrite = false;
5537 }
5538
5539 /* If the merge is from the last image we have to relay all writes
5540 * to the merge destination as well, so that concurrent writes
5541 * (in case of a live merge) are handled correctly. */
5542 if (!pImageFrom->pNext)
5543 {
5544 /* Take the write lock. */
5545 rc2 = vdThreadStartWrite(pDisk);
5546 AssertRC(rc2);
5547 fLockWrite = true;
5548
5549 pDisk->pImageRelay = pImageTo;
5550
5551 rc2 = vdThreadFinishWrite(pDisk);
5552 AssertRC(rc2);
5553 fLockWrite = false;
5554 }
5555
5556 /* Merge child state into parent. This means writing all blocks
5557 * which are allocated in the image up to the source image to the
5558 * destination image. */
5559 uint64_t uOffset = 0;
5560 uint64_t cbRemaining = cbSize;
5561 do
5562 {
5563 size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
5564 rc = VERR_VD_BLOCK_FREE;
5565
5566 /* Need to hold the write lock during a read-write operation. */
5567 rc2 = vdThreadStartWrite(pDisk);
5568 AssertRC(rc2);
5569 fLockWrite = true;
5570
5571 /* Search for image with allocated block. Do not attempt to
5572 * read more than the previous reads marked as valid. Otherwise
5573 * this would return stale data when different block sizes are
5574 * used for the images. */
5575 for (PVDIMAGE pCurrImage = pImageFrom;
5576 pCurrImage != NULL && pCurrImage != pImageTo && rc == VERR_VD_BLOCK_FREE;
5577 pCurrImage = pCurrImage->pPrev)
5578 {
5579 rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
5580 uOffset, pvBuf,
5581 cbThisRead, &cbThisRead);
5582 }
5583
5584 if (rc != VERR_VD_BLOCK_FREE)
5585 {
5586 if (RT_FAILURE(rc))
5587 break;
5588 rc = vdWriteHelper(pDisk, pImageTo, NULL, uOffset, pvBuf,
5589 cbThisRead, true /* fUpdateCache */);
5590 if (RT_FAILURE(rc))
5591 break;
5592 }
5593 else
5594 rc = VINF_SUCCESS;
5595
5596 rc2 = vdThreadFinishWrite(pDisk);
5597 AssertRC(rc2);
5598 fLockWrite = false;
5599
5600 uOffset += cbThisRead;
5601 cbRemaining -= cbThisRead;
5602
5603 if (pCbProgress && pCbProgress->pfnProgress)
5604 {
5605 /** @todo r=klaus: this can update the progress to the same
5606 * percentage over and over again if the image format makes
5607 * relatively small increments. */
5608 rc = pCbProgress->pfnProgress(pIfProgress->pvUser,
5609 uOffset * 99 / cbSize);
5610 if (RT_FAILURE(rc))
5611 break;
5612 }
5613 } while (uOffset < cbSize);
5614
5615 /* In case we set up a "write proxy" image above we must clear
5616 * this again now to prevent stray writes. Failure or not. */
5617 if (!pImageFrom->pNext)
5618 {
5619 /* Take the write lock. */
5620 rc2 = vdThreadStartWrite(pDisk);
5621 AssertRC(rc2);
5622 fLockWrite = true;
5623
5624 pDisk->pImageRelay = NULL;
5625
5626 rc2 = vdThreadFinishWrite(pDisk);
5627 AssertRC(rc2);
5628 fLockWrite = false;
5629 }
5630 }
5631
5632 /*
5633 * Leave in case of an error to avoid corrupted data in the image chain
5634 * (includes cancelling the operation by the user).
5635 */
5636 if (RT_FAILURE(rc))
5637 break;
5638
5639 /* Need to hold the write lock while finishing the merge. */
5640 rc2 = vdThreadStartWrite(pDisk);
5641 AssertRC(rc2);
5642 fLockWrite = true;
5643
5644 /* Update parent UUID so that image chain is consistent. */
5645 RTUUID Uuid;
5646 PVDIMAGE pImageChild = NULL;
5647 if (nImageFrom < nImageTo)
5648 {
5649 if (pImageFrom->pPrev)
5650 {
5651 rc = pImageFrom->pPrev->Backend->pfnGetUuid(pImageFrom->pPrev->pBackendData,
5652 &Uuid);
5653 AssertRC(rc);
5654 }
5655 else
5656 RTUuidClear(&Uuid);
5657 rc = pImageTo->Backend->pfnSetParentUuid(pImageTo->pBackendData,
5658 &Uuid);
5659 AssertRC(rc);
5660 }
5661 else
5662 {
5663 /* Update the parent uuid of the child of the last merged image. */
5664 if (pImageFrom->pNext)
5665 {
5666 rc = pImageTo->Backend->pfnGetUuid(pImageTo->pBackendData,
5667 &Uuid);
5668 AssertRC(rc);
5669
5670 rc = pImageFrom->Backend->pfnSetParentUuid(pImageFrom->pNext->pBackendData,
5671 &Uuid);
5672 AssertRC(rc);
5673
5674 pImageChild = pImageFrom->pNext;
5675 }
5676 }
5677
5678 /* Delete the no longer needed images. */
5679 PVDIMAGE pImg = pImageFrom, pTmp;
5680 while (pImg != pImageTo)
5681 {
5682 if (nImageFrom < nImageTo)
5683 pTmp = pImg->pNext;
5684 else
5685 pTmp = pImg->pPrev;
5686 vdRemoveImageFromList(pDisk, pImg);
5687 pImg->Backend->pfnClose(pImg->pBackendData, true);
5688 RTMemFree(pImg->pszFilename);
5689 RTMemFree(pImg);
5690 pImg = pTmp;
5691 }
5692
5693 /* Make sure destination image is back to read only if necessary. */
5694 if (pImageTo != pDisk->pLast)
5695 {
5696 uOpenFlags = pImageTo->Backend->pfnGetOpenFlags(pImageTo->pBackendData);
5697 uOpenFlags |= VD_OPEN_FLAGS_READONLY;
5698 rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pBackendData,
5699 uOpenFlags);
5700 if (RT_FAILURE(rc))
5701 break;
5702 }
5703
5704 /*
5705 * Make sure the child is readonly
5706 * for the child -> parent merge direction
5707 * if necessary.
5708 */
5709 if ( nImageFrom > nImageTo
5710 && pImageChild
5711 && pImageChild != pDisk->pLast)
5712 {
5713 uOpenFlags = pImageChild->Backend->pfnGetOpenFlags(pImageChild->pBackendData);
5714 uOpenFlags |= VD_OPEN_FLAGS_READONLY;
5715 rc = pImageChild->Backend->pfnSetOpenFlags(pImageChild->pBackendData,
5716 uOpenFlags);
5717 if (RT_FAILURE(rc))
5718 break;
5719 }
5720 } while (0);
5721
5722 if (RT_UNLIKELY(fLockWrite))
5723 {
5724 rc2 = vdThreadFinishWrite(pDisk);
5725 AssertRC(rc2);
5726 }
5727 else if (RT_UNLIKELY(fLockRead))
5728 {
5729 rc2 = vdThreadFinishRead(pDisk);
5730 AssertRC(rc2);
5731 }
5732
5733 if (pvBuf)
5734 RTMemTmpFree(pvBuf);
5735
5736 if (RT_SUCCESS(rc) && pCbProgress && pCbProgress->pfnProgress)
5737 pCbProgress->pfnProgress(pIfProgress->pvUser, 100);
5738
5739 LogFlowFunc(("returns %Rrc\n", rc));
5740 return rc;
5741}
5742
5743/**
5744 * Copies an image from one HDD container to another.
5745 * The copy is opened in the target HDD container.
5746 * It is possible to convert between different image formats, because the
5747 * backend for the destination may be different from the source.
5748 * If both the source and destination reference the same HDD container,
5749 * then the image is moved (by copying/deleting or renaming) to the new location.
5750 * The source container is unchanged if the move operation fails, otherwise
5751 * the image at the new location is opened in the same way as the old one was.
5752 *
5753 * @returns VBox status code.
5754 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
5755 * @param pDiskFrom Pointer to source HDD container.
5756 * @param nImage Image number, counts from 0. 0 is always base image of container.
5757 * @param pDiskTo Pointer to destination HDD container.
5758 * @param pszBackend Name of the image file backend to use.
5759 * @param pszFilename New name of the image (may be NULL if pDiskFrom == pDiskTo).
5760 * @param fMoveByRename If true, attempt to perform a move by renaming (if successful the new size is ignored).
5761 * @param cbSize New image size (0 means leave unchanged).
5762 * @param uImageFlags Flags specifying special destination image features.
5763 * @param pDstUuid New UUID of the destination image. If NULL, a new UUID is created.
5764 * This parameter is used if and only if a true copy is created.
5765 * In all rename/move cases the UUIDs are copied over.
5766 * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
5767 * Only used if the destination image is created.
5768 * @param pVDIfsOperation Pointer to the per-operation VD interface list.
5769 * @param pDstVDIfsImage Pointer to the per-image VD interface list, for the
5770 * destination image.
5771 * @param pDstVDIfsOperation Pointer to the per-image VD interface list,
5772 * for the destination image.
5773 */
5774VBOXDDU_DECL(int) VDCopy(PVBOXHDD pDiskFrom, unsigned nImage, PVBOXHDD pDiskTo,
5775 const char *pszBackend, const char *pszFilename,
5776 bool fMoveByRename, uint64_t cbSize,
5777 unsigned uImageFlags, PCRTUUID pDstUuid,
5778 unsigned uOpenFlags, PVDINTERFACE pVDIfsOperation,
5779 PVDINTERFACE pDstVDIfsImage,
5780 PVDINTERFACE pDstVDIfsOperation)
5781{
5782 int rc = VINF_SUCCESS;
5783 int rc2;
5784 bool fLockReadFrom = false, fLockWriteFrom = false, fLockWriteTo = false;
5785 void *pvBuf = NULL;
5786 PVDIMAGE pImageTo = NULL;
5787
5788 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",
5789 pDiskFrom, nImage, pDiskTo, pszBackend, pszFilename, fMoveByRename, cbSize, uImageFlags, pDstUuid, uOpenFlags, pVDIfsOperation, pDstVDIfsImage, pDstVDIfsOperation));
5790
5791 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
5792 VDINTERFACETYPE_PROGRESS);
5793 PVDINTERFACEPROGRESS pCbProgress = NULL;
5794 if (pIfProgress)
5795 pCbProgress = VDGetInterfaceProgress(pIfProgress);
5796
5797 PVDINTERFACE pDstIfProgress = VDInterfaceGet(pDstVDIfsOperation,
5798 VDINTERFACETYPE_PROGRESS);
5799 PVDINTERFACEPROGRESS pDstCbProgress = NULL;
5800 if (pDstIfProgress)
5801 pDstCbProgress = VDGetInterfaceProgress(pDstIfProgress);
5802
5803 do {
5804 /* Check arguments. */
5805 AssertMsgBreakStmt(VALID_PTR(pDiskFrom), ("pDiskFrom=%#p\n", pDiskFrom),
5806 rc = VERR_INVALID_PARAMETER);
5807 AssertMsg(pDiskFrom->u32Signature == VBOXHDDDISK_SIGNATURE,
5808 ("u32Signature=%08x\n", pDiskFrom->u32Signature));
5809
5810 rc2 = vdThreadStartRead(pDiskFrom);
5811 AssertRC(rc2);
5812 fLockReadFrom = true;
5813 PVDIMAGE pImageFrom = vdGetImageByNumber(pDiskFrom, nImage);
5814 AssertPtrBreakStmt(pImageFrom, rc = VERR_VD_IMAGE_NOT_FOUND);
5815 AssertMsgBreakStmt(VALID_PTR(pDiskTo), ("pDiskTo=%#p\n", pDiskTo),
5816 rc = VERR_INVALID_PARAMETER);
5817 AssertMsg(pDiskTo->u32Signature == VBOXHDDDISK_SIGNATURE,
5818 ("u32Signature=%08x\n", pDiskTo->u32Signature));
5819
5820 /* Move the image. */
5821 if (pDiskFrom == pDiskTo)
5822 {
5823 /* Rename only works when backends are the same, are file based
5824 * and the rename method is implemented. */
5825 if ( fMoveByRename
5826 && !RTStrICmp(pszBackend, pImageFrom->Backend->pszBackendName)
5827 && pImageFrom->Backend->uBackendCaps & VD_CAP_FILE
5828 && pImageFrom->Backend->pfnRename)
5829 {
5830 rc2 = vdThreadFinishRead(pDiskFrom);
5831 AssertRC(rc2);
5832 fLockReadFrom = false;
5833
5834 rc2 = vdThreadStartWrite(pDiskFrom);
5835 AssertRC(rc2);
5836 fLockWriteFrom = true;
5837 rc = pImageFrom->Backend->pfnRename(pImageFrom->pBackendData, pszFilename ? pszFilename : pImageFrom->pszFilename);
5838 break;
5839 }
5840
5841 /** @todo Moving (including shrinking/growing) of the image is
5842 * requested, but the rename attempt failed or it wasn't possible.
5843 * Must now copy image to temp location. */
5844 AssertReleaseMsgFailed(("VDCopy: moving by copy/delete not implemented\n"));
5845 }
5846
5847 /* pszFilename is allowed to be NULL, as this indicates copy to the existing image. */
5848 AssertMsgBreakStmt(pszFilename == NULL || (VALID_PTR(pszFilename) && *pszFilename),
5849 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
5850 rc = VERR_INVALID_PARAMETER);
5851
5852 uint64_t cbSizeFrom;
5853 cbSizeFrom = pImageFrom->Backend->pfnGetSize(pImageFrom->pBackendData);
5854 if (cbSizeFrom == 0)
5855 {
5856 rc = VERR_VD_VALUE_NOT_FOUND;
5857 break;
5858 }
5859
5860 VDGEOMETRY PCHSGeometryFrom = {0, 0, 0};
5861 VDGEOMETRY LCHSGeometryFrom = {0, 0, 0};
5862 pImageFrom->Backend->pfnGetPCHSGeometry(pImageFrom->pBackendData, &PCHSGeometryFrom);
5863 pImageFrom->Backend->pfnGetLCHSGeometry(pImageFrom->pBackendData, &LCHSGeometryFrom);
5864
5865 RTUUID ImageUuid, ImageModificationUuid;
5866 if (pDiskFrom != pDiskTo)
5867 {
5868 if (pDstUuid)
5869 ImageUuid = *pDstUuid;
5870 else
5871 RTUuidCreate(&ImageUuid);
5872 }
5873 else
5874 {
5875 rc = pImageFrom->Backend->pfnGetUuid(pImageFrom->pBackendData, &ImageUuid);
5876 if (RT_FAILURE(rc))
5877 RTUuidCreate(&ImageUuid);
5878 }
5879 rc = pImageFrom->Backend->pfnGetModificationUuid(pImageFrom->pBackendData, &ImageModificationUuid);
5880 if (RT_FAILURE(rc))
5881 RTUuidClear(&ImageModificationUuid);
5882
5883 char szComment[1024];
5884 rc = pImageFrom->Backend->pfnGetComment(pImageFrom->pBackendData, szComment, sizeof(szComment));
5885 if (RT_FAILURE(rc))
5886 szComment[0] = '\0';
5887 else
5888 szComment[sizeof(szComment) - 1] = '\0';
5889
5890 rc2 = vdThreadFinishRead(pDiskFrom);
5891 AssertRC(rc2);
5892 fLockReadFrom = false;
5893
5894 rc2 = vdThreadStartRead(pDiskTo);
5895 AssertRC(rc2);
5896 unsigned cImagesTo = pDiskTo->cImages;
5897 rc2 = vdThreadFinishRead(pDiskTo);
5898 AssertRC(rc2);
5899
5900 if (pszFilename)
5901 {
5902 if (cbSize == 0)
5903 cbSize = cbSizeFrom;
5904
5905 /* Create destination image with the properties of source image. */
5906 /** @todo replace the VDCreateDiff/VDCreateBase calls by direct
5907 * calls to the backend. Unifies the code and reduces the API
5908 * dependencies. Would also make the synchronization explicit. */
5909 if (cImagesTo > 0)
5910 {
5911 rc = VDCreateDiff(pDiskTo, pszBackend, pszFilename,
5912 uImageFlags, szComment, &ImageUuid,
5913 NULL /* pParentUuid */,
5914 uOpenFlags & ~VD_OPEN_FLAGS_READONLY,
5915 pDstVDIfsImage, NULL);
5916
5917 rc2 = vdThreadStartWrite(pDiskTo);
5918 AssertRC(rc2);
5919 fLockWriteTo = true;
5920 } else {
5921 /** @todo hack to force creation of a fixed image for
5922 * the RAW backend, which can't handle anything else. */
5923 if (!RTStrICmp(pszBackend, "RAW"))
5924 uImageFlags |= VD_IMAGE_FLAGS_FIXED;
5925
5926 vdFixupPCHSGeometry(&PCHSGeometryFrom, cbSize);
5927 vdFixupLCHSGeometry(&LCHSGeometryFrom, cbSize);
5928
5929 rc = VDCreateBase(pDiskTo, pszBackend, pszFilename, cbSize,
5930 uImageFlags, szComment,
5931 &PCHSGeometryFrom, &LCHSGeometryFrom,
5932 NULL, uOpenFlags & ~VD_OPEN_FLAGS_READONLY,
5933 pDstVDIfsImage, NULL);
5934
5935 rc2 = vdThreadStartWrite(pDiskTo);
5936 AssertRC(rc2);
5937 fLockWriteTo = true;
5938
5939 if (RT_SUCCESS(rc) && !RTUuidIsNull(&ImageUuid))
5940 pDiskTo->pLast->Backend->pfnSetUuid(pDiskTo->pLast->pBackendData, &ImageUuid);
5941 }
5942 if (RT_FAILURE(rc))
5943 break;
5944
5945 pImageTo = pDiskTo->pLast;
5946 AssertPtrBreakStmt(pImageTo, rc = VERR_VD_IMAGE_NOT_FOUND);
5947
5948 cbSize = RT_MIN(cbSize, cbSizeFrom);
5949 }
5950 else
5951 {
5952 pImageTo = pDiskTo->pLast;
5953 AssertPtrBreakStmt(pImageTo, rc = VERR_VD_IMAGE_NOT_FOUND);
5954
5955 uint64_t cbSizeTo;
5956 cbSizeTo = pImageTo->Backend->pfnGetSize(pImageTo->pBackendData);
5957 if (cbSizeTo == 0)
5958 {
5959 rc = VERR_VD_VALUE_NOT_FOUND;
5960 break;
5961 }
5962
5963 if (cbSize == 0)
5964 cbSize = RT_MIN(cbSizeFrom, cbSizeTo);
5965
5966 vdFixupPCHSGeometry(&PCHSGeometryFrom, cbSize);
5967 vdFixupLCHSGeometry(&LCHSGeometryFrom, cbSize);
5968
5969 /* Update the geometry in the destination image. */
5970 pImageTo->Backend->pfnSetPCHSGeometry(pImageTo->pBackendData, &PCHSGeometryFrom);
5971 pImageTo->Backend->pfnSetLCHSGeometry(pImageTo->pBackendData, &LCHSGeometryFrom);
5972 }
5973
5974 rc2 = vdThreadFinishWrite(pDiskTo);
5975 AssertRC(rc2);
5976 fLockWriteTo = false;
5977
5978 /* Allocate tmp buffer. */
5979 pvBuf = RTMemTmpAlloc(VD_MERGE_BUFFER_SIZE);
5980 if (!pvBuf)
5981 {
5982 rc = VERR_NO_MEMORY;
5983 break;
5984 }
5985
5986 /* Whether we can take the optimized copy path (false) or not.
5987 * Don't optimize if the image existed or if it is a child image. */
5988 bool fRegularRead = (pszFilename == NULL) || (cImagesTo > 0);
5989
5990 /* Copy the data. */
5991 uint64_t uOffset = 0;
5992 uint64_t cbRemaining = cbSize;
5993
5994 do
5995 {
5996 size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
5997
5998 /* Note that we don't attempt to synchronize cross-disk accesses.
5999 * It wouldn't be very difficult to do, just the lock order would
6000 * need to be defined somehow to prevent deadlocks. Postpone such
6001 * magic as there is no use case for this. */
6002
6003 rc2 = vdThreadStartRead(pDiskFrom);
6004 AssertRC(rc2);
6005 fLockReadFrom = true;
6006
6007 /*
6008 * Updating the cache doesn't make any sense
6009 * as we are looping once through the image.
6010 */
6011 rc = vdReadHelper(pDiskFrom, pImageFrom, NULL, uOffset, pvBuf,
6012 cbThisRead, fRegularRead,
6013 false /* fUpdateCache */);
6014 if (RT_FAILURE(rc) && rc != VERR_VD_BLOCK_FREE)
6015 break;
6016
6017 rc2 = vdThreadFinishRead(pDiskFrom);
6018 AssertRC(rc2);
6019 fLockReadFrom = false;
6020
6021 if (rc != VERR_VD_BLOCK_FREE)
6022 {
6023 rc2 = vdThreadStartWrite(pDiskTo);
6024 AssertRC(rc2);
6025 fLockWriteTo = true;
6026
6027 rc = vdWriteHelper(pDiskTo, pImageTo, NULL, uOffset, pvBuf,
6028 cbThisRead, false /* fUpdateCache */);
6029 if (RT_FAILURE(rc))
6030 break;
6031
6032 rc2 = vdThreadFinishWrite(pDiskTo);
6033 AssertRC(rc2);
6034 fLockWriteTo = false;
6035 }
6036 else /* Don't propagate the error to the outside */
6037 rc = VINF_SUCCESS;
6038
6039 uOffset += cbThisRead;
6040 cbRemaining -= cbThisRead;
6041
6042 if (pCbProgress && pCbProgress->pfnProgress)
6043 {
6044 /** @todo r=klaus: this can update the progress to the same
6045 * percentage over and over again if the image format makes
6046 * relatively small increments. */
6047 rc = pCbProgress->pfnProgress(pIfProgress->pvUser,
6048 uOffset * 99 / cbSize);
6049 if (RT_FAILURE(rc))
6050 break;
6051 }
6052 if (pDstCbProgress && pDstCbProgress->pfnProgress)
6053 {
6054 /** @todo r=klaus: this can update the progress to the same
6055 * percentage over and over again if the image format makes
6056 * relatively small increments. */
6057 rc = pDstCbProgress->pfnProgress(pDstIfProgress->pvUser,
6058 uOffset * 99 / cbSize);
6059 if (RT_FAILURE(rc))
6060 break;
6061 }
6062 } while (uOffset < cbSize);
6063
6064 if (RT_SUCCESS(rc))
6065 {
6066 rc2 = vdThreadStartWrite(pDiskTo);
6067 AssertRC(rc2);
6068 fLockWriteTo = true;
6069
6070 /* Only set modification UUID if it is non-null, since the source
6071 * backend might not provide a valid modification UUID. */
6072 if (!RTUuidIsNull(&ImageModificationUuid))
6073 pImageTo->Backend->pfnSetModificationUuid(pImageTo->pBackendData, &ImageModificationUuid);
6074
6075 /* Set the requested open flags if they differ from the value
6076 * required for creating the image and copying the contents. */
6077 if ( pImageTo && pszFilename
6078 && uOpenFlags != (uOpenFlags & ~VD_OPEN_FLAGS_READONLY))
6079 rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pBackendData,
6080 uOpenFlags);
6081 }
6082 } while (0);
6083
6084 if (RT_FAILURE(rc) && pImageTo && pszFilename)
6085 {
6086 /* Take the write lock only if it is not taken. Not worth making the
6087 * above code even more complicated. */
6088 if (RT_UNLIKELY(!fLockWriteTo))
6089 {
6090 rc2 = vdThreadStartWrite(pDiskTo);
6091 AssertRC(rc2);
6092 fLockWriteTo = true;
6093 }
6094 /* Error detected, but new image created. Remove image from list. */
6095 vdRemoveImageFromList(pDiskTo, pImageTo);
6096
6097 /* Close and delete image. */
6098 rc2 = pImageTo->Backend->pfnClose(pImageTo->pBackendData, true);
6099 AssertRC(rc2);
6100 pImageTo->pBackendData = NULL;
6101
6102 /* Free remaining resources. */
6103 if (pImageTo->pszFilename)
6104 RTStrFree(pImageTo->pszFilename);
6105
6106 RTMemFree(pImageTo);
6107 }
6108
6109 if (RT_UNLIKELY(fLockWriteTo))
6110 {
6111 rc2 = vdThreadFinishWrite(pDiskTo);
6112 AssertRC(rc2);
6113 }
6114 if (RT_UNLIKELY(fLockWriteFrom))
6115 {
6116 rc2 = vdThreadFinishWrite(pDiskFrom);
6117 AssertRC(rc2);
6118 }
6119 else if (RT_UNLIKELY(fLockReadFrom))
6120 {
6121 rc2 = vdThreadFinishRead(pDiskFrom);
6122 AssertRC(rc2);
6123 }
6124
6125 if (pvBuf)
6126 RTMemTmpFree(pvBuf);
6127
6128 if (RT_SUCCESS(rc))
6129 {
6130 if (pCbProgress && pCbProgress->pfnProgress)
6131 pCbProgress->pfnProgress(pIfProgress->pvUser, 100);
6132 if (pDstCbProgress && pDstCbProgress->pfnProgress)
6133 pDstCbProgress->pfnProgress(pDstIfProgress->pvUser, 100);
6134 }
6135
6136 LogFlowFunc(("returns %Rrc\n", rc));
6137 return rc;
6138}
6139
6140/**
6141 * Optimizes the storage consumption of an image. Typically the unused blocks
6142 * have to be wiped with zeroes to achieve a substantial reduced storage use.
6143 * Another optimization done is reordering the image blocks, which can provide
6144 * a significant performance boost, as reads and writes tend to use less random
6145 * file offsets.
6146 *
6147 * @return VBox status code.
6148 * @return VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
6149 * @return VERR_VD_IMAGE_READ_ONLY if image is not writable.
6150 * @return VERR_NOT_SUPPORTED if this kind of image can be compacted, but
6151 * the code for this isn't implemented yet.
6152 * @param pDisk Pointer to HDD container.
6153 * @param nImage Image number, counts from 0. 0 is always base image of container.
6154 * @param pVDIfsOperation Pointer to the per-operation VD interface list.
6155 */
6156VBOXDDU_DECL(int) VDCompact(PVBOXHDD pDisk, unsigned nImage,
6157 PVDINTERFACE pVDIfsOperation)
6158{
6159 int rc = VINF_SUCCESS;
6160 int rc2;
6161 bool fLockRead = false, fLockWrite = false;
6162 void *pvBuf = NULL;
6163 void *pvTmp = NULL;
6164
6165 LogFlowFunc(("pDisk=%#p nImage=%u pVDIfsOperation=%#p\n",
6166 pDisk, nImage, pVDIfsOperation));
6167
6168 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
6169 VDINTERFACETYPE_PROGRESS);
6170 PVDINTERFACEPROGRESS pCbProgress = NULL;
6171 if (pIfProgress)
6172 pCbProgress = VDGetInterfaceProgress(pIfProgress);
6173
6174 do {
6175 /* Check arguments. */
6176 AssertMsgBreakStmt(VALID_PTR(pDisk), ("pDisk=%#p\n", pDisk),
6177 rc = VERR_INVALID_PARAMETER);
6178 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE,
6179 ("u32Signature=%08x\n", pDisk->u32Signature));
6180
6181 rc2 = vdThreadStartRead(pDisk);
6182 AssertRC(rc2);
6183 fLockRead = true;
6184
6185 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
6186 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
6187
6188 /* If there is no compact callback for not file based backends then
6189 * the backend doesn't need compaction. No need to make much fuss about
6190 * this. For file based ones signal this as not yet supported. */
6191 if (!pImage->Backend->pfnCompact)
6192 {
6193 if (pImage->Backend->uBackendCaps & VD_CAP_FILE)
6194 rc = VERR_NOT_SUPPORTED;
6195 else
6196 rc = VINF_SUCCESS;
6197 break;
6198 }
6199
6200 /* Insert interface for reading parent state into per-operation list,
6201 * if there is a parent image. */
6202 VDINTERFACE IfOpParent;
6203 VDINTERFACEPARENTSTATE ParentCb;
6204 VDPARENTSTATEDESC ParentUser;
6205 if (pImage->pPrev)
6206 {
6207 ParentCb.cbSize = sizeof(ParentCb);
6208 ParentCb.enmInterface = VDINTERFACETYPE_PARENTSTATE;
6209 ParentCb.pfnParentRead = vdParentRead;
6210 ParentUser.pDisk = pDisk;
6211 ParentUser.pImage = pImage->pPrev;
6212 rc = VDInterfaceAdd(&IfOpParent, "VDCompact_ParentState", VDINTERFACETYPE_PARENTSTATE,
6213 &ParentCb, &ParentUser, &pVDIfsOperation);
6214 AssertRC(rc);
6215 }
6216
6217 rc2 = vdThreadFinishRead(pDisk);
6218 AssertRC(rc2);
6219 fLockRead = false;
6220
6221 rc2 = vdThreadStartWrite(pDisk);
6222 AssertRC(rc2);
6223 fLockWrite = true;
6224
6225 rc = pImage->Backend->pfnCompact(pImage->pBackendData,
6226 0, 99,
6227 pDisk->pVDIfsDisk,
6228 pImage->pVDIfsImage,
6229 pVDIfsOperation);
6230 } while (0);
6231
6232 if (RT_UNLIKELY(fLockWrite))
6233 {
6234 rc2 = vdThreadFinishWrite(pDisk);
6235 AssertRC(rc2);
6236 }
6237 else if (RT_UNLIKELY(fLockRead))
6238 {
6239 rc2 = vdThreadFinishRead(pDisk);
6240 AssertRC(rc2);
6241 }
6242
6243 if (pvBuf)
6244 RTMemTmpFree(pvBuf);
6245 if (pvTmp)
6246 RTMemTmpFree(pvTmp);
6247
6248 if (RT_SUCCESS(rc))
6249 {
6250 if (pCbProgress && pCbProgress->pfnProgress)
6251 pCbProgress->pfnProgress(pIfProgress->pvUser, 100);
6252 }
6253
6254 LogFlowFunc(("returns %Rrc\n", rc));
6255 return rc;
6256}
6257
6258/**
6259 * Resizes the the given disk image to the given size.
6260 *
6261 * @return VBox status
6262 * @return VERR_VD_IMAGE_READ_ONLY if image is not writable.
6263 * @return VERR_NOT_SUPPORTED if this kind of image can be compacted, but
6264 *
6265 * @param pDisk Pointer to the HDD container.
6266 * @param cbSize New size of the image.
6267 * @param pPCHSGeometry Pointer to the new physical disk geometry <= (16383,16,63). Not NULL.
6268 * @param pLCHSGeometry Pointer to the new logical disk geometry <= (x,255,63). Not NULL.
6269 * @param pVDIfsOperation Pointer to the per-operation VD interface list.
6270 */
6271VBOXDDU_DECL(int) VDResize(PVBOXHDD pDisk, uint64_t cbSize,
6272 PCVDGEOMETRY pPCHSGeometry,
6273 PCVDGEOMETRY pLCHSGeometry,
6274 PVDINTERFACE pVDIfsOperation)
6275{
6276 /** @todo r=klaus resizing was designed to be part of VDCopy, so having a separate function is not desirable. */
6277 int rc = VINF_SUCCESS;
6278 int rc2;
6279 bool fLockRead = false, fLockWrite = false;
6280
6281 LogFlowFunc(("pDisk=%#p cbSize=%llu pVDIfsOperation=%#p\n",
6282 pDisk, cbSize, pVDIfsOperation));
6283
6284 PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
6285 VDINTERFACETYPE_PROGRESS);
6286 PVDINTERFACEPROGRESS pCbProgress = NULL;
6287 if (pIfProgress)
6288 pCbProgress = VDGetInterfaceProgress(pIfProgress);
6289
6290 do {
6291 /* Check arguments. */
6292 AssertMsgBreakStmt(VALID_PTR(pDisk), ("pDisk=%#p\n", pDisk),
6293 rc = VERR_INVALID_PARAMETER);
6294 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE,
6295 ("u32Signature=%08x\n", pDisk->u32Signature));
6296
6297 rc2 = vdThreadStartRead(pDisk);
6298 AssertRC(rc2);
6299 fLockRead = true;
6300
6301 /* Not supported if the disk has child images attached. */
6302 AssertMsgBreakStmt(pDisk->cImages == 1, ("cImages=%u\n", pDisk->cImages),
6303 rc = VERR_NOT_SUPPORTED);
6304
6305 PVDIMAGE pImage = pDisk->pBase;
6306
6307 /* If there is no compact callback for not file based backends then
6308 * the backend doesn't need compaction. No need to make much fuss about
6309 * this. For file based ones signal this as not yet supported. */
6310 if (!pImage->Backend->pfnResize)
6311 {
6312 if (pImage->Backend->uBackendCaps & VD_CAP_FILE)
6313 rc = VERR_NOT_SUPPORTED;
6314 else
6315 rc = VINF_SUCCESS;
6316 break;
6317 }
6318
6319 rc2 = vdThreadFinishRead(pDisk);
6320 AssertRC(rc2);
6321 fLockRead = false;
6322
6323 rc2 = vdThreadStartWrite(pDisk);
6324 AssertRC(rc2);
6325 fLockWrite = true;
6326
6327 VDGEOMETRY PCHSGeometryOld;
6328 VDGEOMETRY LCHSGeometryOld;
6329 PCVDGEOMETRY pPCHSGeometryNew;
6330 PCVDGEOMETRY pLCHSGeometryNew;
6331
6332 if (pPCHSGeometry->cCylinders == 0)
6333 {
6334 /* Auto-detect marker, calculate new value ourself. */
6335 rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData, &PCHSGeometryOld);
6336 if (RT_SUCCESS(rc) && (PCHSGeometryOld.cCylinders != 0))
6337 PCHSGeometryOld.cCylinders = RT_MIN(cbSize / 512 / PCHSGeometryOld.cHeads / PCHSGeometryOld.cSectors, 16383);
6338 else if (rc == VERR_VD_GEOMETRY_NOT_SET)
6339 rc = VINF_SUCCESS;
6340
6341 pPCHSGeometryNew = &PCHSGeometryOld;
6342 }
6343 else
6344 pPCHSGeometryNew = pPCHSGeometry;
6345
6346 if (pLCHSGeometry->cCylinders == 0)
6347 {
6348 /* Auto-detect marker, calculate new value ourself. */
6349 rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData, &LCHSGeometryOld);
6350 if (RT_SUCCESS(rc) && (LCHSGeometryOld.cCylinders != 0))
6351 LCHSGeometryOld.cCylinders = cbSize / 512 / LCHSGeometryOld.cHeads / LCHSGeometryOld.cSectors;
6352 else if (rc == VERR_VD_GEOMETRY_NOT_SET)
6353 rc = VINF_SUCCESS;
6354
6355 pLCHSGeometryNew = &LCHSGeometryOld;
6356 }
6357 else
6358 pLCHSGeometryNew = pLCHSGeometry;
6359
6360 if (RT_SUCCESS(rc))
6361 rc = pImage->Backend->pfnResize(pImage->pBackendData,
6362 cbSize,
6363 pPCHSGeometryNew,
6364 pLCHSGeometryNew,
6365 0, 99,
6366 pDisk->pVDIfsDisk,
6367 pImage->pVDIfsImage,
6368 pVDIfsOperation);
6369 } while (0);
6370
6371 if (RT_UNLIKELY(fLockWrite))
6372 {
6373 rc2 = vdThreadFinishWrite(pDisk);
6374 AssertRC(rc2);
6375 }
6376 else if (RT_UNLIKELY(fLockRead))
6377 {
6378 rc2 = vdThreadFinishRead(pDisk);
6379 AssertRC(rc2);
6380 }
6381
6382 if (RT_SUCCESS(rc))
6383 {
6384 if (pCbProgress && pCbProgress->pfnProgress)
6385 pCbProgress->pfnProgress(pIfProgress->pvUser, 100);
6386 }
6387
6388 LogFlowFunc(("returns %Rrc\n", rc));
6389 return rc;
6390}
6391
6392/**
6393 * Closes the last opened image file in HDD container.
6394 * If previous image file was opened in read-only mode (the normal case) and
6395 * the last opened image is in read-write mode then the previous image will be
6396 * reopened in read/write mode.
6397 *
6398 * @returns VBox status code.
6399 * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
6400 * @param pDisk Pointer to HDD container.
6401 * @param fDelete If true, delete the image from the host disk.
6402 */
6403VBOXDDU_DECL(int) VDClose(PVBOXHDD pDisk, bool fDelete)
6404{
6405 int rc = VINF_SUCCESS;
6406 int rc2;
6407 bool fLockWrite = false;
6408
6409 LogFlowFunc(("pDisk=%#p fDelete=%d\n", pDisk, fDelete));
6410 do
6411 {
6412 /* sanity check */
6413 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
6414 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6415
6416 /* Not worth splitting this up into a read lock phase and write
6417 * lock phase, as closing an image is a relatively fast operation
6418 * dominated by the part which needs the write lock. */
6419 rc2 = vdThreadStartWrite(pDisk);
6420 AssertRC(rc2);
6421 fLockWrite = true;
6422
6423 PVDIMAGE pImage = pDisk->pLast;
6424 if (!pImage)
6425 {
6426 rc = VERR_VD_NOT_OPENED;
6427 break;
6428 }
6429 unsigned uOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
6430 /* Remove image from list of opened images. */
6431 vdRemoveImageFromList(pDisk, pImage);
6432 /* Close (and optionally delete) image. */
6433 rc = pImage->Backend->pfnClose(pImage->pBackendData, fDelete);
6434 /* Free remaining resources related to the image. */
6435 RTStrFree(pImage->pszFilename);
6436 RTMemFree(pImage);
6437
6438 pImage = pDisk->pLast;
6439 if (!pImage)
6440 break;
6441
6442 /* If disk was previously in read/write mode, make sure it will stay
6443 * like this (if possible) after closing this image. Set the open flags
6444 * accordingly. */
6445 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
6446 {
6447 uOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
6448 uOpenFlags &= ~ VD_OPEN_FLAGS_READONLY;
6449 rc = pImage->Backend->pfnSetOpenFlags(pImage->pBackendData, uOpenFlags);
6450 }
6451
6452 /* Cache disk information. */
6453 pDisk->cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
6454
6455 /* Cache PCHS geometry. */
6456 rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
6457 &pDisk->PCHSGeometry);
6458 if (RT_FAILURE(rc2))
6459 {
6460 pDisk->PCHSGeometry.cCylinders = 0;
6461 pDisk->PCHSGeometry.cHeads = 0;
6462 pDisk->PCHSGeometry.cSectors = 0;
6463 }
6464 else
6465 {
6466 /* Make sure the PCHS geometry is properly clipped. */
6467 pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
6468 pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
6469 pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
6470 }
6471
6472 /* Cache LCHS geometry. */
6473 rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
6474 &pDisk->LCHSGeometry);
6475 if (RT_FAILURE(rc2))
6476 {
6477 pDisk->LCHSGeometry.cCylinders = 0;
6478 pDisk->LCHSGeometry.cHeads = 0;
6479 pDisk->LCHSGeometry.cSectors = 0;
6480 }
6481 else
6482 {
6483 /* Make sure the LCHS geometry is properly clipped. */
6484 pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
6485 pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
6486 }
6487 } while (0);
6488
6489 if (RT_UNLIKELY(fLockWrite))
6490 {
6491 rc2 = vdThreadFinishWrite(pDisk);
6492 AssertRC(rc2);
6493 }
6494
6495 LogFlowFunc(("returns %Rrc\n", rc));
6496 return rc;
6497}
6498
6499/**
6500 * Closes the currently opened cache image file in HDD container.
6501 *
6502 * @return VBox status code.
6503 * @return VERR_VD_NOT_OPENED if no cache is opened in HDD container.
6504 * @param pDisk Pointer to HDD container.
6505 * @param fDelete If true, delete the image from the host disk.
6506 */
6507VBOXDDU_DECL(int) VDCacheClose(PVBOXHDD pDisk, bool fDelete)
6508{
6509 int rc = VINF_SUCCESS;
6510 int rc2;
6511 bool fLockWrite = false;
6512 PVDCACHE pCache = NULL;
6513
6514 LogFlowFunc(("pDisk=%#p fDelete=%d\n", pDisk, fDelete));
6515
6516 do
6517 {
6518 /* sanity check */
6519 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
6520 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6521
6522 rc2 = vdThreadStartWrite(pDisk);
6523 AssertRC(rc2);
6524 fLockWrite = true;
6525
6526 AssertPtrBreakStmt(pDisk->pCache, rc = VERR_VD_CACHE_NOT_FOUND);
6527
6528 pCache = pDisk->pCache;
6529 pDisk->pCache = NULL;
6530
6531 pCache->Backend->pfnClose(pCache->pBackendData, fDelete);
6532 if (pCache->pszFilename)
6533 RTStrFree(pCache->pszFilename);
6534 RTMemFree(pCache);
6535 } while (0);
6536
6537 if (RT_LIKELY(fLockWrite))
6538 {
6539 rc2 = vdThreadFinishWrite(pDisk);
6540 AssertRC(rc2);
6541 }
6542
6543 LogFlowFunc(("returns %Rrc\n", rc));
6544 return rc;
6545}
6546
6547/**
6548 * Closes all opened image files in HDD container.
6549 *
6550 * @returns VBox status code.
6551 * @param pDisk Pointer to HDD container.
6552 */
6553VBOXDDU_DECL(int) VDCloseAll(PVBOXHDD pDisk)
6554{
6555 int rc = VINF_SUCCESS;
6556 int rc2;
6557 bool fLockWrite = false;
6558
6559 LogFlowFunc(("pDisk=%#p\n", pDisk));
6560 do
6561 {
6562 /* sanity check */
6563 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
6564 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6565
6566 /* Lock the entire operation. */
6567 rc2 = vdThreadStartWrite(pDisk);
6568 AssertRC(rc2);
6569 fLockWrite = true;
6570
6571 PVDCACHE pCache = pDisk->pCache;
6572 if (pCache)
6573 {
6574 rc2 = pCache->Backend->pfnClose(pCache->pBackendData, false);
6575 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
6576 rc = rc2;
6577
6578 if (pCache->pszFilename)
6579 RTStrFree(pCache->pszFilename);
6580 RTMemFree(pCache);
6581 }
6582
6583 PVDIMAGE pImage = pDisk->pLast;
6584 while (VALID_PTR(pImage))
6585 {
6586 PVDIMAGE pPrev = pImage->pPrev;
6587 /* Remove image from list of opened images. */
6588 vdRemoveImageFromList(pDisk, pImage);
6589 /* Close image. */
6590 rc2 = pImage->Backend->pfnClose(pImage->pBackendData, false);
6591 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
6592 rc = rc2;
6593 /* Free remaining resources related to the image. */
6594 RTStrFree(pImage->pszFilename);
6595 RTMemFree(pImage);
6596 pImage = pPrev;
6597 }
6598 Assert(!VALID_PTR(pDisk->pLast));
6599 } while (0);
6600
6601 if (RT_UNLIKELY(fLockWrite))
6602 {
6603 rc2 = vdThreadFinishWrite(pDisk);
6604 AssertRC(rc2);
6605 }
6606
6607 LogFlowFunc(("returns %Rrc\n", rc));
6608 return rc;
6609}
6610
6611/**
6612 * Read data from virtual HDD.
6613 *
6614 * @returns VBox status code.
6615 * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
6616 * @param pDisk Pointer to HDD container.
6617 * @param uOffset Offset of first reading byte from start of disk.
6618 * @param pvBuf Pointer to buffer for reading data.
6619 * @param cbRead Number of bytes to read.
6620 */
6621VBOXDDU_DECL(int) VDRead(PVBOXHDD pDisk, uint64_t uOffset, void *pvBuf,
6622 size_t cbRead)
6623{
6624 int rc = VINF_SUCCESS;
6625 int rc2;
6626 bool fLockRead = false;
6627
6628 LogFlowFunc(("pDisk=%#p uOffset=%llu pvBuf=%p cbRead=%zu\n",
6629 pDisk, uOffset, pvBuf, cbRead));
6630 do
6631 {
6632 /* sanity check */
6633 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
6634 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6635
6636 /* Check arguments. */
6637 AssertMsgBreakStmt(VALID_PTR(pvBuf),
6638 ("pvBuf=%#p\n", pvBuf),
6639 rc = VERR_INVALID_PARAMETER);
6640 AssertMsgBreakStmt(cbRead,
6641 ("cbRead=%zu\n", cbRead),
6642 rc = VERR_INVALID_PARAMETER);
6643
6644 rc2 = vdThreadStartRead(pDisk);
6645 AssertRC(rc2);
6646 fLockRead = true;
6647
6648 AssertMsgBreakStmt(uOffset + cbRead <= pDisk->cbSize,
6649 ("uOffset=%llu cbRead=%zu pDisk->cbSize=%llu\n",
6650 uOffset, cbRead, pDisk->cbSize),
6651 rc = VERR_INVALID_PARAMETER);
6652
6653 PVDIMAGE pImage = pDisk->pLast;
6654 AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
6655
6656 rc = vdReadHelper(pDisk, pImage, NULL, uOffset, pvBuf, cbRead,
6657 true /* fZeroFreeBlocks */,
6658 true /* fUpdateCache */);
6659 } while (0);
6660
6661 if (RT_UNLIKELY(fLockRead))
6662 {
6663 rc2 = vdThreadFinishRead(pDisk);
6664 AssertRC(rc2);
6665 }
6666
6667 LogFlowFunc(("returns %Rrc\n", rc));
6668 return rc;
6669}
6670
6671/**
6672 * Write data to virtual HDD.
6673 *
6674 * @returns VBox status code.
6675 * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
6676 * @param pDisk Pointer to HDD container.
6677 * @param uOffset Offset of the first byte being
6678 * written from start of disk.
6679 * @param pvBuf Pointer to buffer for writing data.
6680 * @param cbWrite Number of bytes to write.
6681 */
6682VBOXDDU_DECL(int) VDWrite(PVBOXHDD pDisk, uint64_t uOffset, const void *pvBuf,
6683 size_t cbWrite)
6684{
6685 int rc = VINF_SUCCESS;
6686 int rc2;
6687 bool fLockWrite = false;
6688
6689 LogFlowFunc(("pDisk=%#p uOffset=%llu pvBuf=%p cbWrite=%zu\n",
6690 pDisk, uOffset, pvBuf, cbWrite));
6691 do
6692 {
6693 /* sanity check */
6694 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
6695 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6696
6697 /* Check arguments. */
6698 AssertMsgBreakStmt(VALID_PTR(pvBuf),
6699 ("pvBuf=%#p\n", pvBuf),
6700 rc = VERR_INVALID_PARAMETER);
6701 AssertMsgBreakStmt(cbWrite,
6702 ("cbWrite=%zu\n", cbWrite),
6703 rc = VERR_INVALID_PARAMETER);
6704
6705 rc2 = vdThreadStartWrite(pDisk);
6706 AssertRC(rc2);
6707 fLockWrite = true;
6708
6709 AssertMsgBreakStmt(uOffset + cbWrite <= pDisk->cbSize,
6710 ("uOffset=%llu cbWrite=%zu pDisk->cbSize=%llu\n",
6711 uOffset, cbWrite, pDisk->cbSize),
6712 rc = VERR_INVALID_PARAMETER);
6713
6714 PVDIMAGE pImage = pDisk->pLast;
6715 AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
6716
6717 vdSetModifiedFlag(pDisk);
6718 rc = vdWriteHelper(pDisk, pImage, NULL, uOffset, pvBuf, cbWrite,
6719 true /* fUpdateCache */);
6720 if (RT_FAILURE(rc))
6721 break;
6722
6723 /* If there is a merge (in the direction towards a parent) running
6724 * concurrently then we have to also "relay" the write to this parent,
6725 * as the merge position might be already past the position where
6726 * this write is going. The "context" of the write can come from the
6727 * natural chain, since merging either already did or will take care
6728 * of the "other" content which is might be needed to fill the block
6729 * to a full allocation size. The cache doesn't need to be touched
6730 * as this write is covered by the previous one. */
6731 if (RT_UNLIKELY(pDisk->pImageRelay))
6732 rc = vdWriteHelper(pDisk, pDisk->pImageRelay, NULL, uOffset,
6733 pvBuf, cbWrite, false /* fUpdateCache */);
6734 } while (0);
6735
6736 if (RT_UNLIKELY(fLockWrite))
6737 {
6738 rc2 = vdThreadFinishWrite(pDisk);
6739 AssertRC(rc2);
6740 }
6741
6742 LogFlowFunc(("returns %Rrc\n", rc));
6743 return rc;
6744}
6745
6746/**
6747 * Make sure the on disk representation of a virtual HDD is up to date.
6748 *
6749 * @returns VBox status code.
6750 * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
6751 * @param pDisk Pointer to HDD container.
6752 */
6753VBOXDDU_DECL(int) VDFlush(PVBOXHDD pDisk)
6754{
6755 int rc = VINF_SUCCESS;
6756 int rc2;
6757 bool fLockWrite = false;
6758
6759 LogFlowFunc(("pDisk=%#p\n", pDisk));
6760 do
6761 {
6762 /* sanity check */
6763 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
6764 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6765
6766 rc2 = vdThreadStartWrite(pDisk);
6767 AssertRC(rc2);
6768 fLockWrite = true;
6769
6770 PVDIMAGE pImage = pDisk->pLast;
6771 AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
6772
6773 vdResetModifiedFlag(pDisk);
6774 rc = pImage->Backend->pfnFlush(pImage->pBackendData);
6775
6776 if ( RT_SUCCESS(rc)
6777 && pDisk->pCache)
6778 rc = pDisk->pCache->Backend->pfnFlush(pDisk->pCache->pBackendData);
6779 } while (0);
6780
6781 if (RT_UNLIKELY(fLockWrite))
6782 {
6783 rc2 = vdThreadFinishWrite(pDisk);
6784 AssertRC(rc2);
6785 }
6786
6787 LogFlowFunc(("returns %Rrc\n", rc));
6788 return rc;
6789}
6790
6791/**
6792 * Get number of opened images in HDD container.
6793 *
6794 * @returns Number of opened images for HDD container. 0 if no images have been opened.
6795 * @param pDisk Pointer to HDD container.
6796 */
6797VBOXDDU_DECL(unsigned) VDGetCount(PVBOXHDD pDisk)
6798{
6799 unsigned cImages;
6800 int rc2;
6801 bool fLockRead = false;
6802
6803 LogFlowFunc(("pDisk=%#p\n", pDisk));
6804 do
6805 {
6806 /* sanity check */
6807 AssertPtrBreakStmt(pDisk, cImages = 0);
6808 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6809
6810 rc2 = vdThreadStartRead(pDisk);
6811 AssertRC(rc2);
6812 fLockRead = true;
6813
6814 cImages = pDisk->cImages;
6815 } while (0);
6816
6817 if (RT_UNLIKELY(fLockRead))
6818 {
6819 rc2 = vdThreadFinishRead(pDisk);
6820 AssertRC(rc2);
6821 }
6822
6823 LogFlowFunc(("returns %u\n", cImages));
6824 return cImages;
6825}
6826
6827/**
6828 * Get read/write mode of HDD container.
6829 *
6830 * @returns Virtual disk ReadOnly status.
6831 * @returns true if no image is opened in HDD container.
6832 * @param pDisk Pointer to HDD container.
6833 */
6834VBOXDDU_DECL(bool) VDIsReadOnly(PVBOXHDD pDisk)
6835{
6836 bool fReadOnly;
6837 int rc2;
6838 bool fLockRead = false;
6839
6840 LogFlowFunc(("pDisk=%#p\n", pDisk));
6841 do
6842 {
6843 /* sanity check */
6844 AssertPtrBreakStmt(pDisk, fReadOnly = false);
6845 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6846
6847 rc2 = vdThreadStartRead(pDisk);
6848 AssertRC(rc2);
6849 fLockRead = true;
6850
6851 PVDIMAGE pImage = pDisk->pLast;
6852 AssertPtrBreakStmt(pImage, fReadOnly = true);
6853
6854 unsigned uOpenFlags;
6855 uOpenFlags = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pBackendData);
6856 fReadOnly = !!(uOpenFlags & VD_OPEN_FLAGS_READONLY);
6857 } while (0);
6858
6859 if (RT_UNLIKELY(fLockRead))
6860 {
6861 rc2 = vdThreadFinishRead(pDisk);
6862 AssertRC(rc2);
6863 }
6864
6865 LogFlowFunc(("returns %d\n", fReadOnly));
6866 return fReadOnly;
6867}
6868
6869/**
6870 * Get total capacity of an image in HDD container.
6871 *
6872 * @returns Virtual disk size in bytes.
6873 * @returns 0 if no image with specified number was not opened.
6874 * @param pDisk Pointer to HDD container.
6875 * @param nImage Image number, counts from 0. 0 is always base image of container.
6876 */
6877VBOXDDU_DECL(uint64_t) VDGetSize(PVBOXHDD pDisk, unsigned nImage)
6878{
6879 uint64_t cbSize;
6880 int rc2;
6881 bool fLockRead = false;
6882
6883 LogFlowFunc(("pDisk=%#p nImage=%u\n", pDisk, nImage));
6884 do
6885 {
6886 /* sanity check */
6887 AssertPtrBreakStmt(pDisk, cbSize = 0);
6888 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6889
6890 rc2 = vdThreadStartRead(pDisk);
6891 AssertRC(rc2);
6892 fLockRead = true;
6893
6894 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
6895 AssertPtrBreakStmt(pImage, cbSize = 0);
6896 cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
6897 } while (0);
6898
6899 if (RT_UNLIKELY(fLockRead))
6900 {
6901 rc2 = vdThreadFinishRead(pDisk);
6902 AssertRC(rc2);
6903 }
6904
6905 LogFlowFunc(("returns %llu\n", cbSize));
6906 return cbSize;
6907}
6908
6909/**
6910 * Get total file size of an image in HDD container.
6911 *
6912 * @returns Virtual disk size in bytes.
6913 * @returns 0 if no image is opened in HDD container.
6914 * @param pDisk Pointer to HDD container.
6915 * @param nImage Image number, counts from 0. 0 is always base image of container.
6916 */
6917VBOXDDU_DECL(uint64_t) VDGetFileSize(PVBOXHDD pDisk, unsigned nImage)
6918{
6919 uint64_t cbSize;
6920 int rc2;
6921 bool fLockRead = false;
6922
6923 LogFlowFunc(("pDisk=%#p nImage=%u\n", pDisk, nImage));
6924 do
6925 {
6926 /* sanity check */
6927 AssertPtrBreakStmt(pDisk, cbSize = 0);
6928 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6929
6930 rc2 = vdThreadStartRead(pDisk);
6931 AssertRC(rc2);
6932 fLockRead = true;
6933
6934 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
6935 AssertPtrBreakStmt(pImage, cbSize = 0);
6936 cbSize = pImage->Backend->pfnGetFileSize(pImage->pBackendData);
6937 } while (0);
6938
6939 if (RT_UNLIKELY(fLockRead))
6940 {
6941 rc2 = vdThreadFinishRead(pDisk);
6942 AssertRC(rc2);
6943 }
6944
6945 LogFlowFunc(("returns %llu\n", cbSize));
6946 return cbSize;
6947}
6948
6949/**
6950 * Get virtual disk PCHS geometry stored in HDD container.
6951 *
6952 * @returns VBox status code.
6953 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
6954 * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
6955 * @param pDisk Pointer to HDD container.
6956 * @param nImage Image number, counts from 0. 0 is always base image of container.
6957 * @param pPCHSGeometry Where to store PCHS geometry. Not NULL.
6958 */
6959VBOXDDU_DECL(int) VDGetPCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
6960 PVDGEOMETRY pPCHSGeometry)
6961{
6962 int rc = VINF_SUCCESS;
6963 int rc2;
6964 bool fLockRead = false;
6965
6966 LogFlowFunc(("pDisk=%#p nImage=%u pPCHSGeometry=%#p\n",
6967 pDisk, nImage, pPCHSGeometry));
6968 do
6969 {
6970 /* sanity check */
6971 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
6972 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6973
6974 /* Check arguments. */
6975 AssertMsgBreakStmt(VALID_PTR(pPCHSGeometry),
6976 ("pPCHSGeometry=%#p\n", pPCHSGeometry),
6977 rc = VERR_INVALID_PARAMETER);
6978
6979 rc2 = vdThreadStartRead(pDisk);
6980 AssertRC(rc2);
6981 fLockRead = true;
6982
6983 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
6984 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
6985
6986 if (pImage == pDisk->pLast)
6987 {
6988 /* Use cached information if possible. */
6989 if (pDisk->PCHSGeometry.cCylinders != 0)
6990 *pPCHSGeometry = pDisk->PCHSGeometry;
6991 else
6992 rc = VERR_VD_GEOMETRY_NOT_SET;
6993 }
6994 else
6995 rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
6996 pPCHSGeometry);
6997 } while (0);
6998
6999 if (RT_UNLIKELY(fLockRead))
7000 {
7001 rc2 = vdThreadFinishRead(pDisk);
7002 AssertRC(rc2);
7003 }
7004
7005 LogFlowFunc(("%Rrc (PCHS=%u/%u/%u)\n", rc,
7006 pDisk->PCHSGeometry.cCylinders, pDisk->PCHSGeometry.cHeads,
7007 pDisk->PCHSGeometry.cSectors));
7008 return rc;
7009}
7010
7011/**
7012 * Store virtual disk PCHS geometry in HDD container.
7013 *
7014 * Note that in case of unrecoverable error all images in HDD container will be closed.
7015 *
7016 * @returns VBox status code.
7017 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7018 * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
7019 * @param pDisk Pointer to HDD container.
7020 * @param nImage Image number, counts from 0. 0 is always base image of container.
7021 * @param pPCHSGeometry Where to load PCHS geometry from. Not NULL.
7022 */
7023VBOXDDU_DECL(int) VDSetPCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
7024 PCVDGEOMETRY pPCHSGeometry)
7025{
7026 int rc = VINF_SUCCESS;
7027 int rc2;
7028 bool fLockWrite = false;
7029
7030 LogFlowFunc(("pDisk=%#p nImage=%u pPCHSGeometry=%#p PCHS=%u/%u/%u\n",
7031 pDisk, nImage, pPCHSGeometry, pPCHSGeometry->cCylinders,
7032 pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
7033 do
7034 {
7035 /* sanity check */
7036 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7037 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7038
7039 /* Check arguments. */
7040 AssertMsgBreakStmt( VALID_PTR(pPCHSGeometry)
7041 && pPCHSGeometry->cHeads <= 16
7042 && pPCHSGeometry->cSectors <= 63,
7043 ("pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pPCHSGeometry,
7044 pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
7045 pPCHSGeometry->cSectors),
7046 rc = VERR_INVALID_PARAMETER);
7047
7048 rc2 = vdThreadStartWrite(pDisk);
7049 AssertRC(rc2);
7050 fLockWrite = true;
7051
7052 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7053 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7054
7055 if (pImage == pDisk->pLast)
7056 {
7057 if ( pPCHSGeometry->cCylinders != pDisk->PCHSGeometry.cCylinders
7058 || pPCHSGeometry->cHeads != pDisk->PCHSGeometry.cHeads
7059 || pPCHSGeometry->cSectors != pDisk->PCHSGeometry.cSectors)
7060 {
7061 /* Only update geometry if it is changed. Avoids similar checks
7062 * in every backend. Most of the time the new geometry is set
7063 * to the previous values, so no need to go through the hassle
7064 * of updating an image which could be opened in read-only mode
7065 * right now. */
7066 rc = pImage->Backend->pfnSetPCHSGeometry(pImage->pBackendData,
7067 pPCHSGeometry);
7068
7069 /* Cache new geometry values in any case. */
7070 rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
7071 &pDisk->PCHSGeometry);
7072 if (RT_FAILURE(rc2))
7073 {
7074 pDisk->PCHSGeometry.cCylinders = 0;
7075 pDisk->PCHSGeometry.cHeads = 0;
7076 pDisk->PCHSGeometry.cSectors = 0;
7077 }
7078 else
7079 {
7080 /* Make sure the CHS geometry is properly clipped. */
7081 pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 255);
7082 pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
7083 }
7084 }
7085 }
7086 else
7087 {
7088 VDGEOMETRY PCHS;
7089 rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
7090 &PCHS);
7091 if ( RT_FAILURE(rc)
7092 || pPCHSGeometry->cCylinders != PCHS.cCylinders
7093 || pPCHSGeometry->cHeads != PCHS.cHeads
7094 || pPCHSGeometry->cSectors != PCHS.cSectors)
7095 {
7096 /* Only update geometry if it is changed. Avoids similar checks
7097 * in every backend. Most of the time the new geometry is set
7098 * to the previous values, so no need to go through the hassle
7099 * of updating an image which could be opened in read-only mode
7100 * right now. */
7101 rc = pImage->Backend->pfnSetPCHSGeometry(pImage->pBackendData,
7102 pPCHSGeometry);
7103 }
7104 }
7105 } while (0);
7106
7107 if (RT_UNLIKELY(fLockWrite))
7108 {
7109 rc2 = vdThreadFinishWrite(pDisk);
7110 AssertRC(rc2);
7111 }
7112
7113 LogFlowFunc(("returns %Rrc\n", rc));
7114 return rc;
7115}
7116
7117/**
7118 * Get virtual disk LCHS geometry stored in HDD container.
7119 *
7120 * @returns VBox status code.
7121 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7122 * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
7123 * @param pDisk Pointer to HDD container.
7124 * @param nImage Image number, counts from 0. 0 is always base image of container.
7125 * @param pLCHSGeometry Where to store LCHS geometry. Not NULL.
7126 */
7127VBOXDDU_DECL(int) VDGetLCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
7128 PVDGEOMETRY pLCHSGeometry)
7129{
7130 int rc = VINF_SUCCESS;
7131 int rc2;
7132 bool fLockRead = false;
7133
7134 LogFlowFunc(("pDisk=%#p nImage=%u pLCHSGeometry=%#p\n",
7135 pDisk, nImage, pLCHSGeometry));
7136 do
7137 {
7138 /* sanity check */
7139 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7140 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7141
7142 /* Check arguments. */
7143 AssertMsgBreakStmt(VALID_PTR(pLCHSGeometry),
7144 ("pLCHSGeometry=%#p\n", pLCHSGeometry),
7145 rc = VERR_INVALID_PARAMETER);
7146
7147 rc2 = vdThreadStartRead(pDisk);
7148 AssertRC(rc2);
7149 fLockRead = true;
7150
7151 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7152 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7153
7154 if (pImage == pDisk->pLast)
7155 {
7156 /* Use cached information if possible. */
7157 if (pDisk->LCHSGeometry.cCylinders != 0)
7158 *pLCHSGeometry = pDisk->LCHSGeometry;
7159 else
7160 rc = VERR_VD_GEOMETRY_NOT_SET;
7161 }
7162 else
7163 rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
7164 pLCHSGeometry);
7165 } while (0);
7166
7167 if (RT_UNLIKELY(fLockRead))
7168 {
7169 rc2 = vdThreadFinishRead(pDisk);
7170 AssertRC(rc2);
7171 }
7172
7173 LogFlowFunc((": %Rrc (LCHS=%u/%u/%u)\n", rc,
7174 pDisk->LCHSGeometry.cCylinders, pDisk->LCHSGeometry.cHeads,
7175 pDisk->LCHSGeometry.cSectors));
7176 return rc;
7177}
7178
7179/**
7180 * Store virtual disk LCHS geometry in HDD container.
7181 *
7182 * Note that in case of unrecoverable error all images in HDD container will be closed.
7183 *
7184 * @returns VBox status code.
7185 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7186 * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
7187 * @param pDisk Pointer to HDD container.
7188 * @param nImage Image number, counts from 0. 0 is always base image of container.
7189 * @param pLCHSGeometry Where to load LCHS geometry from. Not NULL.
7190 */
7191VBOXDDU_DECL(int) VDSetLCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
7192 PCVDGEOMETRY pLCHSGeometry)
7193{
7194 int rc = VINF_SUCCESS;
7195 int rc2;
7196 bool fLockWrite = false;
7197
7198 LogFlowFunc(("pDisk=%#p nImage=%u pLCHSGeometry=%#p LCHS=%u/%u/%u\n",
7199 pDisk, nImage, pLCHSGeometry, pLCHSGeometry->cCylinders,
7200 pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
7201 do
7202 {
7203 /* sanity check */
7204 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7205 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7206
7207 /* Check arguments. */
7208 AssertMsgBreakStmt( VALID_PTR(pLCHSGeometry)
7209 && pLCHSGeometry->cHeads <= 255
7210 && pLCHSGeometry->cSectors <= 63,
7211 ("pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pLCHSGeometry,
7212 pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads,
7213 pLCHSGeometry->cSectors),
7214 rc = VERR_INVALID_PARAMETER);
7215
7216 rc2 = vdThreadStartWrite(pDisk);
7217 AssertRC(rc2);
7218 fLockWrite = true;
7219
7220 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7221 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7222
7223 if (pImage == pDisk->pLast)
7224 {
7225 if ( pLCHSGeometry->cCylinders != pDisk->LCHSGeometry.cCylinders
7226 || pLCHSGeometry->cHeads != pDisk->LCHSGeometry.cHeads
7227 || pLCHSGeometry->cSectors != pDisk->LCHSGeometry.cSectors)
7228 {
7229 /* Only update geometry if it is changed. Avoids similar checks
7230 * in every backend. Most of the time the new geometry is set
7231 * to the previous values, so no need to go through the hassle
7232 * of updating an image which could be opened in read-only mode
7233 * right now. */
7234 rc = pImage->Backend->pfnSetLCHSGeometry(pImage->pBackendData,
7235 pLCHSGeometry);
7236
7237 /* Cache new geometry values in any case. */
7238 rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
7239 &pDisk->LCHSGeometry);
7240 if (RT_FAILURE(rc2))
7241 {
7242 pDisk->LCHSGeometry.cCylinders = 0;
7243 pDisk->LCHSGeometry.cHeads = 0;
7244 pDisk->LCHSGeometry.cSectors = 0;
7245 }
7246 else
7247 {
7248 /* Make sure the CHS geometry is properly clipped. */
7249 pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
7250 pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
7251 }
7252 }
7253 }
7254 else
7255 {
7256 VDGEOMETRY LCHS;
7257 rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
7258 &LCHS);
7259 if ( RT_FAILURE(rc)
7260 || pLCHSGeometry->cCylinders != LCHS.cCylinders
7261 || pLCHSGeometry->cHeads != LCHS.cHeads
7262 || pLCHSGeometry->cSectors != LCHS.cSectors)
7263 {
7264 /* Only update geometry if it is changed. Avoids similar checks
7265 * in every backend. Most of the time the new geometry is set
7266 * to the previous values, so no need to go through the hassle
7267 * of updating an image which could be opened in read-only mode
7268 * right now. */
7269 rc = pImage->Backend->pfnSetLCHSGeometry(pImage->pBackendData,
7270 pLCHSGeometry);
7271 }
7272 }
7273 } while (0);
7274
7275 if (RT_UNLIKELY(fLockWrite))
7276 {
7277 rc2 = vdThreadFinishWrite(pDisk);
7278 AssertRC(rc2);
7279 }
7280
7281 LogFlowFunc(("returns %Rrc\n", rc));
7282 return rc;
7283}
7284
7285/**
7286 * Get version of image in HDD container.
7287 *
7288 * @returns VBox status code.
7289 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7290 * @param pDisk Pointer to HDD container.
7291 * @param nImage Image number, counts from 0. 0 is always base image of container.
7292 * @param puVersion Where to store the image version.
7293 */
7294VBOXDDU_DECL(int) VDGetVersion(PVBOXHDD pDisk, unsigned nImage,
7295 unsigned *puVersion)
7296{
7297 int rc = VINF_SUCCESS;
7298 int rc2;
7299 bool fLockRead = false;
7300
7301 LogFlowFunc(("pDisk=%#p nImage=%u puVersion=%#p\n",
7302 pDisk, nImage, puVersion));
7303 do
7304 {
7305 /* sanity check */
7306 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7307 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7308
7309 /* Check arguments. */
7310 AssertMsgBreakStmt(VALID_PTR(puVersion),
7311 ("puVersion=%#p\n", puVersion),
7312 rc = VERR_INVALID_PARAMETER);
7313
7314 rc2 = vdThreadStartRead(pDisk);
7315 AssertRC(rc2);
7316 fLockRead = true;
7317
7318 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7319 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7320
7321 *puVersion = pImage->Backend->pfnGetVersion(pImage->pBackendData);
7322 } while (0);
7323
7324 if (RT_UNLIKELY(fLockRead))
7325 {
7326 rc2 = vdThreadFinishRead(pDisk);
7327 AssertRC(rc2);
7328 }
7329
7330 LogFlowFunc(("returns %Rrc uVersion=%#x\n", rc, *puVersion));
7331 return rc;
7332}
7333
7334/**
7335 * List the capabilities of image backend in HDD container.
7336 *
7337 * @returns VBox status code.
7338 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7339 * @param pDisk Pointer to the HDD container.
7340 * @param nImage Image number, counts from 0. 0 is always base image of container.
7341 * @param pbackendInfo Where to store the backend information.
7342 */
7343VBOXDDU_DECL(int) VDBackendInfoSingle(PVBOXHDD pDisk, unsigned nImage,
7344 PVDBACKENDINFO pBackendInfo)
7345{
7346 int rc = VINF_SUCCESS;
7347 int rc2;
7348 bool fLockRead = false;
7349
7350 LogFlowFunc(("pDisk=%#p nImage=%u pBackendInfo=%#p\n",
7351 pDisk, nImage, pBackendInfo));
7352 do
7353 {
7354 /* sanity check */
7355 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7356 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7357
7358 /* Check arguments. */
7359 AssertMsgBreakStmt(VALID_PTR(pBackendInfo),
7360 ("pBackendInfo=%#p\n", pBackendInfo),
7361 rc = VERR_INVALID_PARAMETER);
7362
7363 rc2 = vdThreadStartRead(pDisk);
7364 AssertRC(rc2);
7365 fLockRead = true;
7366
7367 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7368 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7369
7370 pBackendInfo->pszBackend = pImage->Backend->pszBackendName;
7371 pBackendInfo->uBackendCaps = pImage->Backend->uBackendCaps;
7372 pBackendInfo->paFileExtensions = pImage->Backend->paFileExtensions;
7373 pBackendInfo->paConfigInfo = pImage->Backend->paConfigInfo;
7374 } while (0);
7375
7376 if (RT_UNLIKELY(fLockRead))
7377 {
7378 rc2 = vdThreadFinishRead(pDisk);
7379 AssertRC(rc2);
7380 }
7381
7382 LogFlowFunc(("returns %Rrc\n", rc));
7383 return rc;
7384}
7385
7386/**
7387 * Get flags of image in HDD container.
7388 *
7389 * @returns VBox status code.
7390 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7391 * @param pDisk Pointer to HDD container.
7392 * @param nImage Image number, counts from 0. 0 is always base image of container.
7393 * @param puImageFlags Where to store the image flags.
7394 */
7395VBOXDDU_DECL(int) VDGetImageFlags(PVBOXHDD pDisk, unsigned nImage,
7396 unsigned *puImageFlags)
7397{
7398 int rc = VINF_SUCCESS;
7399 int rc2;
7400 bool fLockRead = false;
7401
7402 LogFlowFunc(("pDisk=%#p nImage=%u puImageFlags=%#p\n",
7403 pDisk, nImage, puImageFlags));
7404 do
7405 {
7406 /* sanity check */
7407 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7408 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7409
7410 /* Check arguments. */
7411 AssertMsgBreakStmt(VALID_PTR(puImageFlags),
7412 ("puImageFlags=%#p\n", puImageFlags),
7413 rc = VERR_INVALID_PARAMETER);
7414
7415 rc2 = vdThreadStartRead(pDisk);
7416 AssertRC(rc2);
7417 fLockRead = true;
7418
7419 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7420 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7421
7422 *puImageFlags = pImage->uImageFlags;
7423 } while (0);
7424
7425 if (RT_UNLIKELY(fLockRead))
7426 {
7427 rc2 = vdThreadFinishRead(pDisk);
7428 AssertRC(rc2);
7429 }
7430
7431 LogFlowFunc(("returns %Rrc uImageFlags=%#x\n", rc, *puImageFlags));
7432 return rc;
7433}
7434
7435/**
7436 * Get open flags of image in HDD container.
7437 *
7438 * @returns VBox status code.
7439 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7440 * @param pDisk Pointer to HDD container.
7441 * @param nImage Image number, counts from 0. 0 is always base image of container.
7442 * @param puOpenFlags Where to store the image open flags.
7443 */
7444VBOXDDU_DECL(int) VDGetOpenFlags(PVBOXHDD pDisk, unsigned nImage,
7445 unsigned *puOpenFlags)
7446{
7447 int rc = VINF_SUCCESS;
7448 int rc2;
7449 bool fLockRead = false;
7450
7451 LogFlowFunc(("pDisk=%#p nImage=%u puOpenFlags=%#p\n",
7452 pDisk, nImage, puOpenFlags));
7453 do
7454 {
7455 /* sanity check */
7456 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7457 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7458
7459 /* Check arguments. */
7460 AssertMsgBreakStmt(VALID_PTR(puOpenFlags),
7461 ("puOpenFlags=%#p\n", puOpenFlags),
7462 rc = VERR_INVALID_PARAMETER);
7463
7464 rc2 = vdThreadStartRead(pDisk);
7465 AssertRC(rc2);
7466 fLockRead = true;
7467
7468 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7469 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7470
7471 *puOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
7472 } while (0);
7473
7474 if (RT_UNLIKELY(fLockRead))
7475 {
7476 rc2 = vdThreadFinishRead(pDisk);
7477 AssertRC(rc2);
7478 }
7479
7480 LogFlowFunc(("returns %Rrc uOpenFlags=%#x\n", rc, *puOpenFlags));
7481 return rc;
7482}
7483
7484/**
7485 * Set open flags of image in HDD container.
7486 * This operation may cause file locking changes and/or files being reopened.
7487 * Note that in case of unrecoverable error all images in HDD container will be closed.
7488 *
7489 * @returns VBox status code.
7490 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7491 * @param pDisk Pointer to HDD container.
7492 * @param nImage Image number, counts from 0. 0 is always base image of container.
7493 * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
7494 */
7495VBOXDDU_DECL(int) VDSetOpenFlags(PVBOXHDD pDisk, unsigned nImage,
7496 unsigned uOpenFlags)
7497{
7498 int rc;
7499 int rc2;
7500 bool fLockWrite = false;
7501
7502 LogFlowFunc(("pDisk=%#p uOpenFlags=%#u\n", pDisk, uOpenFlags));
7503 do
7504 {
7505 /* sanity check */
7506 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7507 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7508
7509 /* Check arguments. */
7510 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
7511 ("uOpenFlags=%#x\n", uOpenFlags),
7512 rc = VERR_INVALID_PARAMETER);
7513
7514 rc2 = vdThreadStartWrite(pDisk);
7515 AssertRC(rc2);
7516 fLockWrite = true;
7517
7518 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7519 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7520
7521 rc = pImage->Backend->pfnSetOpenFlags(pImage->pBackendData,
7522 uOpenFlags);
7523 } while (0);
7524
7525 if (RT_UNLIKELY(fLockWrite))
7526 {
7527 rc2 = vdThreadFinishWrite(pDisk);
7528 AssertRC(rc2);
7529 }
7530
7531 LogFlowFunc(("returns %Rrc\n", rc));
7532 return rc;
7533}
7534
7535/**
7536 * Get base filename of image in HDD container. Some image formats use
7537 * other filenames as well, so don't use this for anything but informational
7538 * purposes.
7539 *
7540 * @returns VBox status code.
7541 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7542 * @returns VERR_BUFFER_OVERFLOW if pszFilename buffer too small to hold filename.
7543 * @param pDisk Pointer to HDD container.
7544 * @param nImage Image number, counts from 0. 0 is always base image of container.
7545 * @param pszFilename Where to store the image file name.
7546 * @param cbFilename Size of buffer pszFilename points to.
7547 */
7548VBOXDDU_DECL(int) VDGetFilename(PVBOXHDD pDisk, unsigned nImage,
7549 char *pszFilename, unsigned cbFilename)
7550{
7551 int rc;
7552 int rc2;
7553 bool fLockRead = false;
7554
7555 LogFlowFunc(("pDisk=%#p nImage=%u pszFilename=%#p cbFilename=%u\n",
7556 pDisk, nImage, pszFilename, cbFilename));
7557 do
7558 {
7559 /* sanity check */
7560 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7561 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7562
7563 /* Check arguments. */
7564 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
7565 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
7566 rc = VERR_INVALID_PARAMETER);
7567 AssertMsgBreakStmt(cbFilename,
7568 ("cbFilename=%u\n", cbFilename),
7569 rc = VERR_INVALID_PARAMETER);
7570
7571 rc2 = vdThreadStartRead(pDisk);
7572 AssertRC(rc2);
7573 fLockRead = true;
7574
7575 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7576 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7577
7578 size_t cb = strlen(pImage->pszFilename);
7579 if (cb <= cbFilename)
7580 {
7581 strcpy(pszFilename, pImage->pszFilename);
7582 rc = VINF_SUCCESS;
7583 }
7584 else
7585 {
7586 strncpy(pszFilename, pImage->pszFilename, cbFilename - 1);
7587 pszFilename[cbFilename - 1] = '\0';
7588 rc = VERR_BUFFER_OVERFLOW;
7589 }
7590 } while (0);
7591
7592 if (RT_UNLIKELY(fLockRead))
7593 {
7594 rc2 = vdThreadFinishRead(pDisk);
7595 AssertRC(rc2);
7596 }
7597
7598 LogFlowFunc(("returns %Rrc, pszFilename=\"%s\"\n", rc, pszFilename));
7599 return rc;
7600}
7601
7602/**
7603 * Get the comment line of image in HDD container.
7604 *
7605 * @returns VBox status code.
7606 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7607 * @returns VERR_BUFFER_OVERFLOW if pszComment buffer too small to hold comment text.
7608 * @param pDisk Pointer to HDD container.
7609 * @param nImage Image number, counts from 0. 0 is always base image of container.
7610 * @param pszComment Where to store the comment string of image. NULL is ok.
7611 * @param cbComment The size of pszComment buffer. 0 is ok.
7612 */
7613VBOXDDU_DECL(int) VDGetComment(PVBOXHDD pDisk, unsigned nImage,
7614 char *pszComment, unsigned cbComment)
7615{
7616 int rc;
7617 int rc2;
7618 bool fLockRead = false;
7619
7620 LogFlowFunc(("pDisk=%#p nImage=%u pszComment=%#p cbComment=%u\n",
7621 pDisk, nImage, pszComment, cbComment));
7622 do
7623 {
7624 /* sanity check */
7625 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7626 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7627
7628 /* Check arguments. */
7629 AssertMsgBreakStmt(VALID_PTR(pszComment),
7630 ("pszComment=%#p \"%s\"\n", pszComment, pszComment),
7631 rc = VERR_INVALID_PARAMETER);
7632 AssertMsgBreakStmt(cbComment,
7633 ("cbComment=%u\n", cbComment),
7634 rc = VERR_INVALID_PARAMETER);
7635
7636 rc2 = vdThreadStartRead(pDisk);
7637 AssertRC(rc2);
7638 fLockRead = true;
7639
7640 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7641 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7642
7643 rc = pImage->Backend->pfnGetComment(pImage->pBackendData, pszComment,
7644 cbComment);
7645 } while (0);
7646
7647 if (RT_UNLIKELY(fLockRead))
7648 {
7649 rc2 = vdThreadFinishRead(pDisk);
7650 AssertRC(rc2);
7651 }
7652
7653 LogFlowFunc(("returns %Rrc, pszComment=\"%s\"\n", rc, pszComment));
7654 return rc;
7655}
7656
7657/**
7658 * Changes the comment line of image in HDD container.
7659 *
7660 * @returns VBox status code.
7661 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7662 * @param pDisk Pointer to HDD container.
7663 * @param nImage Image number, counts from 0. 0 is always base image of container.
7664 * @param pszComment New comment string (UTF-8). NULL is allowed to reset the comment.
7665 */
7666VBOXDDU_DECL(int) VDSetComment(PVBOXHDD pDisk, unsigned nImage,
7667 const char *pszComment)
7668{
7669 int rc;
7670 int rc2;
7671 bool fLockWrite = false;
7672
7673 LogFlowFunc(("pDisk=%#p nImage=%u pszComment=%#p \"%s\"\n",
7674 pDisk, nImage, pszComment, pszComment));
7675 do
7676 {
7677 /* sanity check */
7678 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7679 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7680
7681 /* Check arguments. */
7682 AssertMsgBreakStmt(VALID_PTR(pszComment) || pszComment == NULL,
7683 ("pszComment=%#p \"%s\"\n", pszComment, pszComment),
7684 rc = VERR_INVALID_PARAMETER);
7685
7686 rc2 = vdThreadStartWrite(pDisk);
7687 AssertRC(rc2);
7688 fLockWrite = true;
7689
7690 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7691 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7692
7693 rc = pImage->Backend->pfnSetComment(pImage->pBackendData, pszComment);
7694 } while (0);
7695
7696 if (RT_UNLIKELY(fLockWrite))
7697 {
7698 rc2 = vdThreadFinishWrite(pDisk);
7699 AssertRC(rc2);
7700 }
7701
7702 LogFlowFunc(("returns %Rrc\n", rc));
7703 return rc;
7704}
7705
7706
7707/**
7708 * Get UUID of image in HDD container.
7709 *
7710 * @returns VBox status code.
7711 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7712 * @param pDisk Pointer to HDD container.
7713 * @param nImage Image number, counts from 0. 0 is always base image of container.
7714 * @param pUuid Where to store the image creation UUID.
7715 */
7716VBOXDDU_DECL(int) VDGetUuid(PVBOXHDD pDisk, unsigned nImage, PRTUUID pUuid)
7717{
7718 int rc;
7719 int rc2;
7720 bool fLockRead = false;
7721
7722 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
7723 do
7724 {
7725 /* sanity check */
7726 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7727 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7728
7729 /* Check arguments. */
7730 AssertMsgBreakStmt(VALID_PTR(pUuid),
7731 ("pUuid=%#p\n", pUuid),
7732 rc = VERR_INVALID_PARAMETER);
7733
7734 rc2 = vdThreadStartRead(pDisk);
7735 AssertRC(rc2);
7736 fLockRead = true;
7737
7738 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7739 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7740
7741 rc = pImage->Backend->pfnGetUuid(pImage->pBackendData, pUuid);
7742 } while (0);
7743
7744 if (RT_UNLIKELY(fLockRead))
7745 {
7746 rc2 = vdThreadFinishRead(pDisk);
7747 AssertRC(rc2);
7748 }
7749
7750 LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
7751 return rc;
7752}
7753
7754/**
7755 * Set the image's UUID. Should not be used by normal applications.
7756 *
7757 * @returns VBox status code.
7758 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7759 * @param pDisk Pointer to HDD container.
7760 * @param nImage Image number, counts from 0. 0 is always base image of container.
7761 * @param pUuid New UUID of the image. If NULL, a new UUID is created.
7762 */
7763VBOXDDU_DECL(int) VDSetUuid(PVBOXHDD pDisk, unsigned nImage, PCRTUUID pUuid)
7764{
7765 int rc;
7766 int rc2;
7767 bool fLockWrite = false;
7768
7769 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
7770 pDisk, nImage, pUuid, pUuid));
7771 do
7772 {
7773 /* sanity check */
7774 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7775 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7776
7777 AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
7778 ("pUuid=%#p\n", pUuid),
7779 rc = VERR_INVALID_PARAMETER);
7780
7781 rc2 = vdThreadStartWrite(pDisk);
7782 AssertRC(rc2);
7783 fLockWrite = true;
7784
7785 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7786 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7787
7788 RTUUID Uuid;
7789 if (!pUuid)
7790 {
7791 RTUuidCreate(&Uuid);
7792 pUuid = &Uuid;
7793 }
7794 rc = pImage->Backend->pfnSetUuid(pImage->pBackendData, pUuid);
7795 } while (0);
7796
7797 if (RT_UNLIKELY(fLockWrite))
7798 {
7799 rc2 = vdThreadFinishWrite(pDisk);
7800 AssertRC(rc2);
7801 }
7802
7803 LogFlowFunc(("returns %Rrc\n", rc));
7804 return rc;
7805}
7806
7807/**
7808 * Get last modification UUID of image in HDD container.
7809 *
7810 * @returns VBox status code.
7811 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7812 * @param pDisk Pointer to HDD container.
7813 * @param nImage Image number, counts from 0. 0 is always base image of container.
7814 * @param pUuid Where to store the image modification UUID.
7815 */
7816VBOXDDU_DECL(int) VDGetModificationUuid(PVBOXHDD pDisk, unsigned nImage, PRTUUID pUuid)
7817{
7818 int rc = VINF_SUCCESS;
7819 int rc2;
7820 bool fLockRead = false;
7821
7822 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
7823 do
7824 {
7825 /* sanity check */
7826 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7827 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7828
7829 /* Check arguments. */
7830 AssertMsgBreakStmt(VALID_PTR(pUuid),
7831 ("pUuid=%#p\n", pUuid),
7832 rc = VERR_INVALID_PARAMETER);
7833
7834 rc2 = vdThreadStartRead(pDisk);
7835 AssertRC(rc2);
7836 fLockRead = true;
7837
7838 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7839 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7840
7841 rc = pImage->Backend->pfnGetModificationUuid(pImage->pBackendData,
7842 pUuid);
7843 } while (0);
7844
7845 if (RT_UNLIKELY(fLockRead))
7846 {
7847 rc2 = vdThreadFinishRead(pDisk);
7848 AssertRC(rc2);
7849 }
7850
7851 LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
7852 return rc;
7853}
7854
7855/**
7856 * Set the image's last modification UUID. Should not be used by normal applications.
7857 *
7858 * @returns VBox status code.
7859 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7860 * @param pDisk Pointer to HDD container.
7861 * @param nImage Image number, counts from 0. 0 is always base image of container.
7862 * @param pUuid New modification UUID of the image. If NULL, a new UUID is created.
7863 */
7864VBOXDDU_DECL(int) VDSetModificationUuid(PVBOXHDD pDisk, unsigned nImage, PCRTUUID pUuid)
7865{
7866 int rc;
7867 int rc2;
7868 bool fLockWrite = false;
7869
7870 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
7871 pDisk, nImage, pUuid, pUuid));
7872 do
7873 {
7874 /* sanity check */
7875 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7876 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7877
7878 /* Check arguments. */
7879 AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
7880 ("pUuid=%#p\n", pUuid),
7881 rc = VERR_INVALID_PARAMETER);
7882
7883 rc2 = vdThreadStartWrite(pDisk);
7884 AssertRC(rc2);
7885 fLockWrite = true;
7886
7887 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7888 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7889
7890 RTUUID Uuid;
7891 if (!pUuid)
7892 {
7893 RTUuidCreate(&Uuid);
7894 pUuid = &Uuid;
7895 }
7896 rc = pImage->Backend->pfnSetModificationUuid(pImage->pBackendData,
7897 pUuid);
7898 } while (0);
7899
7900 if (RT_UNLIKELY(fLockWrite))
7901 {
7902 rc2 = vdThreadFinishWrite(pDisk);
7903 AssertRC(rc2);
7904 }
7905
7906 LogFlowFunc(("returns %Rrc\n", rc));
7907 return rc;
7908}
7909
7910/**
7911 * Get parent UUID of image in HDD container.
7912 *
7913 * @returns VBox status code.
7914 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
7915 * @param pDisk Pointer to HDD container.
7916 * @param nImage Image number, counts from 0. 0 is always base image of container.
7917 * @param pUuid Where to store the parent image UUID.
7918 */
7919VBOXDDU_DECL(int) VDGetParentUuid(PVBOXHDD pDisk, unsigned nImage,
7920 PRTUUID pUuid)
7921{
7922 int rc = VINF_SUCCESS;
7923 int rc2;
7924 bool fLockRead = false;
7925
7926 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
7927 do
7928 {
7929 /* sanity check */
7930 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7931 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7932
7933 /* Check arguments. */
7934 AssertMsgBreakStmt(VALID_PTR(pUuid),
7935 ("pUuid=%#p\n", pUuid),
7936 rc = VERR_INVALID_PARAMETER);
7937
7938 rc2 = vdThreadStartRead(pDisk);
7939 AssertRC(rc2);
7940 fLockRead = true;
7941
7942 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7943 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7944
7945 rc = pImage->Backend->pfnGetParentUuid(pImage->pBackendData, pUuid);
7946 } while (0);
7947
7948 if (RT_UNLIKELY(fLockRead))
7949 {
7950 rc2 = vdThreadFinishRead(pDisk);
7951 AssertRC(rc2);
7952 }
7953
7954 LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
7955 return rc;
7956}
7957
7958/**
7959 * Set the image's parent UUID. Should not be used by normal applications.
7960 *
7961 * @returns VBox status code.
7962 * @param pDisk Pointer to HDD container.
7963 * @param nImage Image number, counts from 0. 0 is always base image of container.
7964 * @param pUuid New parent UUID of the image. If NULL, a new UUID is created.
7965 */
7966VBOXDDU_DECL(int) VDSetParentUuid(PVBOXHDD pDisk, unsigned nImage,
7967 PCRTUUID pUuid)
7968{
7969 int rc;
7970 int rc2;
7971 bool fLockWrite = false;
7972
7973 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
7974 pDisk, nImage, pUuid, pUuid));
7975 do
7976 {
7977 /* sanity check */
7978 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
7979 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
7980
7981 /* Check arguments. */
7982 AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
7983 ("pUuid=%#p\n", pUuid),
7984 rc = VERR_INVALID_PARAMETER);
7985
7986 rc2 = vdThreadStartWrite(pDisk);
7987 AssertRC(rc2);
7988 fLockWrite = true;
7989
7990 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7991 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7992
7993 RTUUID Uuid;
7994 if (!pUuid)
7995 {
7996 RTUuidCreate(&Uuid);
7997 pUuid = &Uuid;
7998 }
7999 rc = pImage->Backend->pfnSetParentUuid(pImage->pBackendData, pUuid);
8000 } while (0);
8001
8002 if (RT_UNLIKELY(fLockWrite))
8003 {
8004 rc2 = vdThreadFinishWrite(pDisk);
8005 AssertRC(rc2);
8006 }
8007
8008 LogFlowFunc(("returns %Rrc\n", rc));
8009 return rc;
8010}
8011
8012
8013/**
8014 * Debug helper - dumps all opened images in HDD container into the log file.
8015 *
8016 * @param pDisk Pointer to HDD container.
8017 */
8018VBOXDDU_DECL(void) VDDumpImages(PVBOXHDD pDisk)
8019{
8020 int rc2;
8021 bool fLockRead = false;
8022
8023 do
8024 {
8025 /* sanity check */
8026 AssertPtrBreak(pDisk);
8027 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8028
8029 if (!pDisk->pInterfaceErrorCallbacks || !VALID_PTR(pDisk->pInterfaceErrorCallbacks->pfnMessage))
8030 pDisk->pInterfaceErrorCallbacks->pfnMessage = vdLogMessage;
8031
8032 rc2 = vdThreadStartRead(pDisk);
8033 AssertRC(rc2);
8034 fLockRead = true;
8035
8036 vdMessageWrapper(pDisk, "--- Dumping VD Disk, Images=%u\n", pDisk->cImages);
8037 for (PVDIMAGE pImage = pDisk->pBase; pImage; pImage = pImage->pNext)
8038 {
8039 vdMessageWrapper(pDisk, "Dumping VD image \"%s\" (Backend=%s)\n",
8040 pImage->pszFilename, pImage->Backend->pszBackendName);
8041 pImage->Backend->pfnDump(pImage->pBackendData);
8042 }
8043 } while (0);
8044
8045 if (RT_UNLIKELY(fLockRead))
8046 {
8047 rc2 = vdThreadFinishRead(pDisk);
8048 AssertRC(rc2);
8049 }
8050}
8051
8052/**
8053 * Query if asynchronous operations are supported for this disk.
8054 *
8055 * @returns VBox status code.
8056 * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
8057 * @param pDisk Pointer to the HDD container.
8058 * @param nImage Image number, counts from 0. 0 is always base image of container.
8059 * @param pfAIOSupported Where to store if async IO is supported.
8060 */
8061VBOXDDU_DECL(int) VDImageIsAsyncIOSupported(PVBOXHDD pDisk, unsigned nImage, bool *pfAIOSupported)
8062{
8063 int rc = VINF_SUCCESS;
8064 int rc2;
8065 bool fLockRead = false;
8066
8067 LogFlowFunc(("pDisk=%#p nImage=%u pfAIOSupported=%#p\n", pDisk, nImage, pfAIOSupported));
8068 do
8069 {
8070 /* sanity check */
8071 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
8072 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8073
8074 /* Check arguments. */
8075 AssertMsgBreakStmt(VALID_PTR(pfAIOSupported),
8076 ("pfAIOSupported=%#p\n", pfAIOSupported),
8077 rc = VERR_INVALID_PARAMETER);
8078
8079 rc2 = vdThreadStartRead(pDisk);
8080 AssertRC(rc2);
8081 fLockRead = true;
8082
8083 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
8084 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
8085
8086 if (pImage->Backend->uBackendCaps & VD_CAP_ASYNC)
8087 *pfAIOSupported = pImage->Backend->pfnIsAsyncIOSupported(pImage->pBackendData);
8088 else
8089 *pfAIOSupported = false;
8090 } while (0);
8091
8092 if (RT_UNLIKELY(fLockRead))
8093 {
8094 rc2 = vdThreadFinishRead(pDisk);
8095 AssertRC(rc2);
8096 }
8097
8098 LogFlowFunc(("returns %Rrc, fAIOSupported=%u\n", rc, *pfAIOSupported));
8099 return rc;
8100}
8101
8102
8103VBOXDDU_DECL(int) VDAsyncRead(PVBOXHDD pDisk, uint64_t uOffset, size_t cbRead,
8104 PCRTSGBUF pcSgBuf,
8105 PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
8106 void *pvUser1, void *pvUser2)
8107{
8108 int rc = VERR_VD_BLOCK_FREE;
8109 int rc2;
8110 bool fLockRead = false;
8111 PVDIOCTX pIoCtx = NULL;
8112
8113 LogFlowFunc(("pDisk=%#p uOffset=%llu pcSgBuf=%#p cbRead=%zu pvUser1=%#p pvUser2=%#p\n",
8114 pDisk, uOffset, pcSgBuf, cbRead, pvUser1, pvUser2));
8115
8116 do
8117 {
8118 /* sanity check */
8119 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
8120 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8121
8122 /* Check arguments. */
8123 AssertMsgBreakStmt(cbRead,
8124 ("cbRead=%zu\n", cbRead),
8125 rc = VERR_INVALID_PARAMETER);
8126 AssertMsgBreakStmt(VALID_PTR(pcSgBuf),
8127 ("pcSgBuf=%#p\n", pcSgBuf),
8128 rc = VERR_INVALID_PARAMETER);
8129
8130 rc2 = vdThreadStartRead(pDisk);
8131 AssertRC(rc2);
8132 fLockRead = true;
8133
8134 AssertMsgBreakStmt(uOffset + cbRead <= pDisk->cbSize,
8135 ("uOffset=%llu cbRead=%zu pDisk->cbSize=%llu\n",
8136 uOffset, cbRead, pDisk->cbSize),
8137 rc = VERR_INVALID_PARAMETER);
8138 AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
8139
8140 pIoCtx = vdIoCtxRootAlloc(pDisk, VDIOCTXTXDIR_READ, uOffset,
8141 cbRead, pDisk->pLast, pcSgBuf,
8142 pfnComplete, pvUser1, pvUser2,
8143 NULL, vdReadHelperAsync);
8144 if (!pIoCtx)
8145 {
8146 rc = VERR_NO_MEMORY;
8147 break;
8148 }
8149
8150 rc = vdIoCtxProcess(pIoCtx);
8151 if (rc == VINF_VD_ASYNC_IO_FINISHED)
8152 {
8153 if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
8154 vdIoCtxFree(pDisk, pIoCtx);
8155 else
8156 rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
8157 }
8158 else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
8159 vdIoCtxFree(pDisk, pIoCtx);
8160
8161 } while (0);
8162
8163 if (RT_UNLIKELY(fLockRead) && ( rc == VINF_VD_ASYNC_IO_FINISHED
8164 || rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
8165 {
8166 rc2 = vdThreadFinishRead(pDisk);
8167 AssertRC(rc2);
8168 }
8169
8170 LogFlowFunc(("returns %Rrc\n", rc));
8171 return rc;
8172}
8173
8174
8175VBOXDDU_DECL(int) VDAsyncWrite(PVBOXHDD pDisk, uint64_t uOffset, size_t cbWrite,
8176 PCRTSGBUF pcSgBuf,
8177 PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
8178 void *pvUser1, void *pvUser2)
8179{
8180 int rc;
8181 int rc2;
8182 bool fLockWrite = false;
8183 PVDIOCTX pIoCtx = NULL;
8184
8185 LogFlowFunc(("pDisk=%#p uOffset=%llu cSgBuf=%#p cbWrite=%zu pvUser1=%#p pvUser2=%#p\n",
8186 pDisk, uOffset, pcSgBuf, cbWrite, pvUser1, pvUser2));
8187 do
8188 {
8189 /* sanity check */
8190 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
8191 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8192
8193 /* Check arguments. */
8194 AssertMsgBreakStmt(cbWrite,
8195 ("cbWrite=%zu\n", cbWrite),
8196 rc = VERR_INVALID_PARAMETER);
8197 AssertMsgBreakStmt(VALID_PTR(pcSgBuf),
8198 ("pcSgBuf=%#p\n", pcSgBuf),
8199 rc = VERR_INVALID_PARAMETER);
8200
8201 rc2 = vdThreadStartWrite(pDisk);
8202 AssertRC(rc2);
8203 fLockWrite = true;
8204
8205 AssertMsgBreakStmt(uOffset + cbWrite <= pDisk->cbSize,
8206 ("uOffset=%llu cbWrite=%zu pDisk->cbSize=%llu\n",
8207 uOffset, cbWrite, pDisk->cbSize),
8208 rc = VERR_INVALID_PARAMETER);
8209 AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
8210
8211 pIoCtx = vdIoCtxRootAlloc(pDisk, VDIOCTXTXDIR_WRITE, uOffset,
8212 cbWrite, pDisk->pLast, pcSgBuf,
8213 pfnComplete, pvUser1, pvUser2,
8214 NULL, vdWriteHelperAsync);
8215 if (!pIoCtx)
8216 {
8217 rc = VERR_NO_MEMORY;
8218 break;
8219 }
8220
8221 rc = vdIoCtxProcess(pIoCtx);
8222 if (rc == VINF_VD_ASYNC_IO_FINISHED)
8223 {
8224 if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
8225 vdIoCtxFree(pDisk, pIoCtx);
8226 else
8227 rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
8228 }
8229 else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
8230 vdIoCtxFree(pDisk, pIoCtx);
8231 } while (0);
8232
8233 if (RT_UNLIKELY(fLockWrite) && ( rc == VINF_VD_ASYNC_IO_FINISHED
8234 || rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
8235 {
8236 rc2 = vdThreadFinishWrite(pDisk);
8237 AssertRC(rc2);
8238 }
8239
8240 LogFlowFunc(("returns %Rrc\n", rc));
8241 return rc;
8242}
8243
8244
8245VBOXDDU_DECL(int) VDAsyncFlush(PVBOXHDD pDisk, PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
8246 void *pvUser1, void *pvUser2)
8247{
8248 int rc;
8249 int rc2;
8250 bool fLockWrite = false;
8251 PVDIOCTX pIoCtx = NULL;
8252
8253 LogFlowFunc(("pDisk=%#p\n", pDisk));
8254
8255 do
8256 {
8257 /* sanity check */
8258 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
8259 AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8260
8261 rc2 = vdThreadStartWrite(pDisk);
8262 AssertRC(rc2);
8263 fLockWrite = true;
8264
8265 AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
8266
8267 pIoCtx = vdIoCtxRootAlloc(pDisk, VDIOCTXTXDIR_FLUSH, 0,
8268 0, pDisk->pLast, NULL,
8269 pfnComplete, pvUser1, pvUser2,
8270 NULL, vdFlushHelperAsync);
8271 if (!pIoCtx)
8272 {
8273 rc = VERR_NO_MEMORY;
8274 break;
8275 }
8276
8277 rc = vdIoCtxProcess(pIoCtx);
8278 if (rc == VINF_VD_ASYNC_IO_FINISHED)
8279 {
8280 if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
8281 vdIoCtxFree(pDisk, pIoCtx);
8282 else
8283 rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
8284 }
8285 else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
8286 vdIoCtxFree(pDisk, pIoCtx);
8287 } while (0);
8288
8289 if (RT_UNLIKELY(fLockWrite) && ( rc == VINF_VD_ASYNC_IO_FINISHED
8290 || rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
8291 {
8292 rc2 = vdThreadFinishWrite(pDisk);
8293 AssertRC(rc2);
8294 }
8295
8296 LogFlowFunc(("returns %Rrc\n", rc));
8297 return rc;
8298}
8299
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