VirtualBox

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

Last change on this file since 90778 was 88528, checked in by vboxsync, 3 years ago

Storage/VD: Improved validation of the accessed range in VDRead/VDWrite/VDAsyncRead/VDAsyncWrite, bugref:9902

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 353.2 KB
Line 
1/* $Id: VD.cpp 88528 2021-04-15 11:56:09Z vboxsync $ */
2/** @file
3 * VD - Virtual disk container implementation.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_VD
23#include <VBox/vd.h>
24#include <VBox/err.h>
25#include <VBox/sup.h>
26#include <VBox/log.h>
27
28#include <iprt/alloc.h>
29#include <iprt/assert.h>
30#include <iprt/uuid.h>
31#include <iprt/file.h>
32#include <iprt/string.h>
33#include <iprt/asm.h>
34#include <iprt/param.h>
35#include <iprt/path.h>
36#include <iprt/sg.h>
37#include <iprt/semaphore.h>
38#include <iprt/vector.h>
39
40#include "VDInternal.h"
41
42/** Buffer size used for merging images. */
43#define VD_MERGE_BUFFER_SIZE (16 * _1M)
44
45/** Maximum number of segments in one I/O task. */
46#define VD_IO_TASK_SEGMENTS_MAX 64
47
48/** Threshold after not recently used blocks are removed from the list. */
49#define VD_DISCARD_REMOVE_THRESHOLD (10 * _1M) /** @todo experiment */
50
51/**
52 * VD async I/O interface storage descriptor.
53 */
54typedef struct VDIIOFALLBACKSTORAGE
55{
56 /** File handle. */
57 RTFILE File;
58 /** Completion callback. */
59 PFNVDCOMPLETED pfnCompleted;
60 /** Thread for async access. */
61 RTTHREAD ThreadAsync;
62} VDIIOFALLBACKSTORAGE, *PVDIIOFALLBACKSTORAGE;
63
64/**
65 * uModified bit flags.
66 */
67#define VD_IMAGE_MODIFIED_FLAG RT_BIT(0)
68#define VD_IMAGE_MODIFIED_FIRST RT_BIT(1)
69#define VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE RT_BIT(2)
70
71
72# define VD_IS_LOCKED(a_pDisk) \
73 do \
74 { \
75 NOREF(a_pDisk); \
76 AssertMsg((a_pDisk)->fLocked, \
77 ("Lock not held\n"));\
78 } while(0)
79
80/**
81 * VBox parent read descriptor, used internally for compaction.
82 */
83typedef struct VDPARENTSTATEDESC
84{
85 /** Pointer to disk descriptor. */
86 PVDISK pDisk;
87 /** Pointer to image descriptor. */
88 PVDIMAGE pImage;
89} VDPARENTSTATEDESC, *PVDPARENTSTATEDESC;
90
91/**
92 * Transfer direction.
93 */
94typedef enum VDIOCTXTXDIR
95{
96 /** Read */
97 VDIOCTXTXDIR_READ = 0,
98 /** Write */
99 VDIOCTXTXDIR_WRITE,
100 /** Flush */
101 VDIOCTXTXDIR_FLUSH,
102 /** Discard */
103 VDIOCTXTXDIR_DISCARD,
104 /** 32bit hack */
105 VDIOCTXTXDIR_32BIT_HACK = 0x7fffffff
106} VDIOCTXTXDIR, *PVDIOCTXTXDIR;
107
108/** Transfer function */
109typedef DECLCALLBACKTYPE(int, FNVDIOCTXTRANSFER ,(PVDIOCTX pIoCtx));
110/** Pointer to a transfer function. */
111typedef FNVDIOCTXTRANSFER *PFNVDIOCTXTRANSFER;
112
113/**
114 * I/O context
115 */
116typedef struct VDIOCTX
117{
118 /** Pointer to the next I/O context. */
119 struct VDIOCTX * volatile pIoCtxNext;
120 /** Disk this is request is for. */
121 PVDISK pDisk;
122 /** Return code. */
123 int rcReq;
124 /** Various flags for the I/O context. */
125 uint32_t fFlags;
126 /** Number of data transfers currently pending. */
127 volatile uint32_t cDataTransfersPending;
128 /** How many meta data transfers are pending. */
129 volatile uint32_t cMetaTransfersPending;
130 /** Flag whether the request finished */
131 volatile bool fComplete;
132 /** Temporary allocated memory which is freed
133 * when the context completes. */
134 void *pvAllocation;
135 /** Transfer function. */
136 PFNVDIOCTXTRANSFER pfnIoCtxTransfer;
137 /** Next transfer part after the current one completed. */
138 PFNVDIOCTXTRANSFER pfnIoCtxTransferNext;
139 /** Transfer direction */
140 VDIOCTXTXDIR enmTxDir;
141 /** Request type dependent data. */
142 union
143 {
144 /** I/O request (read/write). */
145 struct
146 {
147 /** Number of bytes left until this context completes. */
148 volatile uint32_t cbTransferLeft;
149 /** Current offset */
150 volatile uint64_t uOffset;
151 /** Number of bytes to transfer */
152 volatile size_t cbTransfer;
153 /** Current image in the chain. */
154 PVDIMAGE pImageCur;
155 /** Start image to read from. pImageCur is reset to this
156 * value after it reached the first image in the chain. */
157 PVDIMAGE pImageStart;
158 /** S/G buffer */
159 RTSGBUF SgBuf;
160 /** Number of bytes to clear in the buffer before the current read. */
161 size_t cbBufClear;
162 /** Number of images to read. */
163 unsigned cImagesRead;
164 /** Override for the parent image to start reading from. */
165 PVDIMAGE pImageParentOverride;
166 /** Original offset of the transfer - required for filtering read requests. */
167 uint64_t uOffsetXferOrig;
168 /** Original size of the transfer - required for fitlering read requests. */
169 size_t cbXferOrig;
170 } Io;
171 /** Discard requests. */
172 struct
173 {
174 /** Pointer to the range descriptor array. */
175 PCRTRANGE paRanges;
176 /** Number of ranges in the array. */
177 unsigned cRanges;
178 /** Range descriptor index which is processed. */
179 unsigned idxRange;
180 /** Start offset to discard currently. */
181 uint64_t offCur;
182 /** How many bytes left to discard in the current range. */
183 size_t cbDiscardLeft;
184 /** How many bytes to discard in the current block (<= cbDiscardLeft). */
185 size_t cbThisDiscard;
186 /** Discard block handled currently. */
187 PVDDISCARDBLOCK pBlock;
188 } Discard;
189 } Req;
190 /** Parent I/O context if any. Sets the type of the context (root/child) */
191 PVDIOCTX pIoCtxParent;
192 /** Type dependent data (root/child) */
193 union
194 {
195 /** Root data */
196 struct
197 {
198 /** Completion callback */
199 PFNVDASYNCTRANSFERCOMPLETE pfnComplete;
200 /** User argument 1 passed on completion. */
201 void *pvUser1;
202 /** User argument 2 passed on completion. */
203 void *pvUser2;
204 } Root;
205 /** Child data */
206 struct
207 {
208 /** Saved start offset */
209 uint64_t uOffsetSaved;
210 /** Saved transfer size */
211 size_t cbTransferLeftSaved;
212 /** Number of bytes transferred from the parent if this context completes. */
213 size_t cbTransferParent;
214 /** Number of bytes to pre read */
215 size_t cbPreRead;
216 /** Number of bytes to post read. */
217 size_t cbPostRead;
218 /** Number of bytes to write left in the parent. */
219 size_t cbWriteParent;
220 /** Write type dependent data. */
221 union
222 {
223 /** Optimized */
224 struct
225 {
226 /** Bytes to fill to satisfy the block size. Not part of the virtual disk. */
227 size_t cbFill;
228 /** Bytes to copy instead of reading from the parent */
229 size_t cbWriteCopy;
230 /** Bytes to read from the image. */
231 size_t cbReadImage;
232 } Optimized;
233 } Write;
234 } Child;
235 } Type;
236} VDIOCTX;
237
238/** Default flags for an I/O context, i.e. unblocked and async. */
239#define VDIOCTX_FLAGS_DEFAULT (0)
240/** Flag whether the context is blocked. */
241#define VDIOCTX_FLAGS_BLOCKED RT_BIT_32(0)
242/** Flag whether the I/O context is using synchronous I/O. */
243#define VDIOCTX_FLAGS_SYNC RT_BIT_32(1)
244/** Flag whether the read should update the cache. */
245#define VDIOCTX_FLAGS_READ_UPDATE_CACHE RT_BIT_32(2)
246/** Flag whether free blocks should be zeroed.
247 * If false and no image has data for sepcified
248 * range VERR_VD_BLOCK_FREE is returned for the I/O context.
249 * Note that unallocated blocks are still zeroed
250 * if at least one image has valid data for a part
251 * of the range.
252 */
253#define VDIOCTX_FLAGS_ZERO_FREE_BLOCKS RT_BIT_32(3)
254/** Don't free the I/O context when complete because
255 * it was alloacted elsewhere (stack, ...). */
256#define VDIOCTX_FLAGS_DONT_FREE RT_BIT_32(4)
257/** Don't set the modified flag for this I/O context when writing. */
258#define VDIOCTX_FLAGS_DONT_SET_MODIFIED_FLAG RT_BIT_32(5)
259/** The write filter was applied already and shouldn't be applied a second time.
260 * Used at the beginning of vdWriteHelperAsync() because it might be called
261 * multiple times.
262 */
263#define VDIOCTX_FLAGS_WRITE_FILTER_APPLIED RT_BIT_32(6)
264
265/** NIL I/O context pointer value. */
266#define NIL_VDIOCTX ((PVDIOCTX)0)
267
268/**
269 * List node for deferred I/O contexts.
270 */
271typedef struct VDIOCTXDEFERRED
272{
273 /** Node in the list of deferred requests.
274 * A request can be deferred if the image is growing
275 * and the request accesses the same range or if
276 * the backend needs to read or write metadata from the disk
277 * before it can continue. */
278 RTLISTNODE NodeDeferred;
279 /** I/O context this entry points to. */
280 PVDIOCTX pIoCtx;
281} VDIOCTXDEFERRED, *PVDIOCTXDEFERRED;
282
283/**
284 * I/O task.
285 */
286typedef struct VDIOTASK
287{
288 /** Next I/O task waiting in the list. */
289 struct VDIOTASK * volatile pNext;
290 /** Storage this task belongs to. */
291 PVDIOSTORAGE pIoStorage;
292 /** Optional completion callback. */
293 PFNVDXFERCOMPLETED pfnComplete;
294 /** Opaque user data. */
295 void *pvUser;
296 /** Completion status code for the task. */
297 int rcReq;
298 /** Flag whether this is a meta data transfer. */
299 bool fMeta;
300 /** Type dependent data. */
301 union
302 {
303 /** User data transfer. */
304 struct
305 {
306 /** Number of bytes this task transferred. */
307 uint32_t cbTransfer;
308 /** Pointer to the I/O context the task belongs. */
309 PVDIOCTX pIoCtx;
310 } User;
311 /** Meta data transfer. */
312 struct
313 {
314 /** Meta transfer this task is for. */
315 PVDMETAXFER pMetaXfer;
316 } Meta;
317 } Type;
318} VDIOTASK;
319
320/**
321 * Storage handle.
322 */
323typedef struct VDIOSTORAGE
324{
325 /** Image I/O state this storage handle belongs to. */
326 PVDIO pVDIo;
327 /** AVL tree for pending async metadata transfers. */
328 PAVLRFOFFTREE pTreeMetaXfers;
329 /** Storage handle */
330 void *pStorage;
331} VDIOSTORAGE;
332
333/**
334 * Metadata transfer.
335 *
336 * @note This entry can't be freed if either the list is not empty or
337 * the reference counter is not 0.
338 * The assumption is that the backends don't need to read huge amounts of
339 * metadata to complete a transfer so the additional memory overhead should
340 * be relatively small.
341 */
342typedef struct VDMETAXFER
343{
344 /** AVL core for fast search (the file offset is the key) */
345 AVLRFOFFNODECORE Core;
346 /** I/O storage for this transfer. */
347 PVDIOSTORAGE pIoStorage;
348 /** Flags. */
349 uint32_t fFlags;
350 /** List of I/O contexts waiting for this metadata transfer to complete. */
351 RTLISTNODE ListIoCtxWaiting;
352 /** Number of references to this entry. */
353 unsigned cRefs;
354 /** Size of the data stored with this entry. */
355 size_t cbMeta;
356 /** Shadow buffer which is used in case a write is still active and other
357 * writes update the shadow buffer. */
358 uint8_t *pbDataShw;
359 /** List of I/O contexts updating the shadow buffer while there is a write
360 * in progress. */
361 RTLISTNODE ListIoCtxShwWrites;
362 /** Data stored - variable size. */
363 uint8_t abData[1];
364} VDMETAXFER;
365
366/**
367 * The transfer direction for the metadata.
368 */
369#define VDMETAXFER_TXDIR_MASK 0x3
370#define VDMETAXFER_TXDIR_NONE 0x0
371#define VDMETAXFER_TXDIR_WRITE 0x1
372#define VDMETAXFER_TXDIR_READ 0x2
373#define VDMETAXFER_TXDIR_FLUSH 0x3
374#define VDMETAXFER_TXDIR_GET(flags) ((flags) & VDMETAXFER_TXDIR_MASK)
375#define VDMETAXFER_TXDIR_SET(flags, dir) ((flags) = (flags & ~VDMETAXFER_TXDIR_MASK) | (dir))
376
377/** Forward declaration of the async discard helper. */
378static DECLCALLBACK(int) vdDiscardHelperAsync(PVDIOCTX pIoCtx);
379static DECLCALLBACK(int) vdWriteHelperAsync(PVDIOCTX pIoCtx);
380static void vdDiskProcessBlockedIoCtx(PVDISK pDisk);
381static int vdDiskUnlock(PVDISK pDisk, PVDIOCTX pIoCtxRc);
382static DECLCALLBACK(void) vdIoCtxSyncComplete(void *pvUser1, void *pvUser2, int rcReq);
383
384/**
385 * internal: issue error message.
386 */
387static int vdError(PVDISK pDisk, int rc, RT_SRC_POS_DECL,
388 const char *pszFormat, ...)
389{
390 va_list va;
391 va_start(va, pszFormat);
392 if (pDisk->pInterfaceError)
393 pDisk->pInterfaceError->pfnError(pDisk->pInterfaceError->Core.pvUser, rc, RT_SRC_POS_ARGS, pszFormat, va);
394 va_end(va);
395 return rc;
396}
397
398/**
399 * internal: thread synchronization, start read.
400 */
401DECLINLINE(int) vdThreadStartRead(PVDISK pDisk)
402{
403 int rc = VINF_SUCCESS;
404 if (RT_UNLIKELY(pDisk->pInterfaceThreadSync))
405 rc = pDisk->pInterfaceThreadSync->pfnStartRead(pDisk->pInterfaceThreadSync->Core.pvUser);
406 return rc;
407}
408
409/**
410 * internal: thread synchronization, finish read.
411 */
412DECLINLINE(int) vdThreadFinishRead(PVDISK pDisk)
413{
414 int rc = VINF_SUCCESS;
415 if (RT_UNLIKELY(pDisk->pInterfaceThreadSync))
416 rc = pDisk->pInterfaceThreadSync->pfnFinishRead(pDisk->pInterfaceThreadSync->Core.pvUser);
417 return rc;
418}
419
420/**
421 * internal: thread synchronization, start write.
422 */
423DECLINLINE(int) vdThreadStartWrite(PVDISK pDisk)
424{
425 int rc = VINF_SUCCESS;
426 if (RT_UNLIKELY(pDisk->pInterfaceThreadSync))
427 rc = pDisk->pInterfaceThreadSync->pfnStartWrite(pDisk->pInterfaceThreadSync->Core.pvUser);
428 return rc;
429}
430
431/**
432 * internal: thread synchronization, finish write.
433 */
434DECLINLINE(int) vdThreadFinishWrite(PVDISK pDisk)
435{
436 int rc = VINF_SUCCESS;
437 if (RT_UNLIKELY(pDisk->pInterfaceThreadSync))
438 rc = pDisk->pInterfaceThreadSync->pfnFinishWrite(pDisk->pInterfaceThreadSync->Core.pvUser);
439 return rc;
440}
441
442/**
443 * internal: add image structure to the end of images list.
444 */
445static void vdAddImageToList(PVDISK pDisk, PVDIMAGE pImage)
446{
447 pImage->pPrev = NULL;
448 pImage->pNext = NULL;
449
450 if (pDisk->pBase)
451 {
452 Assert(pDisk->cImages > 0);
453 pImage->pPrev = pDisk->pLast;
454 pDisk->pLast->pNext = pImage;
455 pDisk->pLast = pImage;
456 }
457 else
458 {
459 Assert(pDisk->cImages == 0);
460 pDisk->pBase = pImage;
461 pDisk->pLast = pImage;
462 }
463
464 pDisk->cImages++;
465}
466
467/**
468 * internal: remove image structure from the images list.
469 */
470static void vdRemoveImageFromList(PVDISK pDisk, PVDIMAGE pImage)
471{
472 Assert(pDisk->cImages > 0);
473
474 if (pImage->pPrev)
475 pImage->pPrev->pNext = pImage->pNext;
476 else
477 pDisk->pBase = pImage->pNext;
478
479 if (pImage->pNext)
480 pImage->pNext->pPrev = pImage->pPrev;
481 else
482 pDisk->pLast = pImage->pPrev;
483
484 pImage->pPrev = NULL;
485 pImage->pNext = NULL;
486
487 pDisk->cImages--;
488}
489
490/**
491 * Release a referene to the filter decrementing the counter and destroying the filter
492 * when the counter reaches zero.
493 *
494 * @returns The new reference count.
495 * @param pFilter The filter to release.
496 */
497static uint32_t vdFilterRelease(PVDFILTER pFilter)
498{
499 uint32_t cRefs = ASMAtomicDecU32(&pFilter->cRefs);
500 if (!cRefs)
501 {
502 pFilter->pBackend->pfnDestroy(pFilter->pvBackendData);
503 RTMemFree(pFilter);
504 }
505
506 return cRefs;
507}
508
509/**
510 * Increments the reference counter of the given filter.
511 *
512 * @return The new reference count.
513 * @param pFilter The filter.
514 */
515static uint32_t vdFilterRetain(PVDFILTER pFilter)
516{
517 return ASMAtomicIncU32(&pFilter->cRefs);
518}
519
520/**
521 * internal: find image by index into the images list.
522 */
523static PVDIMAGE vdGetImageByNumber(PVDISK pDisk, unsigned nImage)
524{
525 PVDIMAGE pImage = pDisk->pBase;
526 if (nImage == VD_LAST_IMAGE)
527 return pDisk->pLast;
528 while (pImage && nImage)
529 {
530 pImage = pImage->pNext;
531 nImage--;
532 }
533 return pImage;
534}
535
536/**
537 * Creates a new region list from the given one converting to match the flags if necessary.
538 *
539 * @returns VBox status code.
540 * @param pRegionList The region list to convert from.
541 * @param fFlags The flags for the new region list.
542 * @param ppRegionList Where to store the new region list on success.
543 */
544static int vdRegionListConv(PCVDREGIONLIST pRegionList, uint32_t fFlags, PPVDREGIONLIST ppRegionList)
545{
546 int rc = VINF_SUCCESS;
547 PVDREGIONLIST pRegionListNew = (PVDREGIONLIST)RTMemDup(pRegionList,
548 RT_UOFFSETOF_DYN(VDREGIONLIST, aRegions[pRegionList->cRegions]));
549 if (RT_LIKELY(pRegionListNew))
550 {
551 /* Do we have to convert anything? */
552 if (pRegionList->fFlags != fFlags)
553 {
554 uint64_t offRegionNext = 0;
555
556 pRegionListNew->fFlags = fFlags;
557 for (unsigned i = 0; i < pRegionListNew->cRegions; i++)
558 {
559 PVDREGIONDESC pRegion = &pRegionListNew->aRegions[i];
560
561 if ( (fFlags & VD_REGION_LIST_F_LOC_SIZE_BLOCKS)
562 && !(pRegionList->fFlags & VD_REGION_LIST_F_LOC_SIZE_BLOCKS))
563 {
564 Assert(!(pRegion->cRegionBlocksOrBytes % pRegion->cbBlock));
565
566 /* Convert from bytes to logical blocks. */
567 pRegion->offRegion = offRegionNext;
568 pRegion->cRegionBlocksOrBytes = pRegion->cRegionBlocksOrBytes / pRegion->cbBlock;
569 offRegionNext += pRegion->cRegionBlocksOrBytes;
570 }
571 else
572 {
573 /* Convert from logical blocks to bytes. */
574 pRegion->offRegion = offRegionNext;
575 pRegion->cRegionBlocksOrBytes = pRegion->cRegionBlocksOrBytes * pRegion->cbBlock;
576 offRegionNext += pRegion->cRegionBlocksOrBytes;
577 }
578 }
579 }
580
581 *ppRegionList = pRegionListNew;
582 }
583 else
584 rc = VERR_NO_MEMORY;
585
586 return rc;
587}
588
589/**
590 * Returns the virtual size of the image in bytes.
591 *
592 * @returns Size of the given image in bytes.
593 * @param pImage The image to get the size from.
594 */
595static uint64_t vdImageGetSize(PVDIMAGE pImage)
596{
597 uint64_t cbImage = 0;
598
599 if (pImage->cbImage == VD_IMAGE_SIZE_UNINITIALIZED)
600 {
601 PCVDREGIONLIST pRegionList = NULL;
602 int rc = pImage->Backend->pfnQueryRegions(pImage->pBackendData, &pRegionList);
603 if (RT_SUCCESS(rc))
604 {
605 if (pRegionList->fFlags & VD_REGION_LIST_F_LOC_SIZE_BLOCKS)
606 {
607 PVDREGIONLIST pRegionListConv = NULL;
608 rc = vdRegionListConv(pRegionList, 0, &pRegionListConv);
609 if (RT_SUCCESS(rc))
610 {
611 for (uint32_t i = 0; i < pRegionListConv->cRegions; i++)
612 cbImage += pRegionListConv->aRegions[i].cRegionBlocksOrBytes;
613
614 VDRegionListFree(pRegionListConv);
615 }
616 }
617 else
618 for (uint32_t i = 0; i < pRegionList->cRegions; i++)
619 cbImage += pRegionList->aRegions[i].cRegionBlocksOrBytes;
620
621 AssertPtr(pImage->Backend->pfnRegionListRelease);
622 pImage->Backend->pfnRegionListRelease(pImage->pBackendData, pRegionList);
623 pImage->cbImage = cbImage; /* Cache the value. */
624 }
625 }
626 else
627 cbImage = pImage->cbImage;
628
629 return cbImage;
630}
631
632/**
633 * Applies the filter chain to the given write request.
634 *
635 * @returns VBox status code.
636 * @param pDisk The HDD container.
637 * @param uOffset The start offset of the write.
638 * @param cbWrite Number of bytes to write.
639 * @param pIoCtx The I/O context associated with the request.
640 */
641static int vdFilterChainApplyWrite(PVDISK pDisk, uint64_t uOffset, size_t cbWrite,
642 PVDIOCTX pIoCtx)
643{
644 int rc = VINF_SUCCESS;
645
646 VD_IS_LOCKED(pDisk);
647
648 PVDFILTER pFilter;
649 RTListForEach(&pDisk->ListFilterChainWrite, pFilter, VDFILTER, ListNodeChainWrite)
650 {
651 rc = pFilter->pBackend->pfnFilterWrite(pFilter->pvBackendData, uOffset, cbWrite, pIoCtx);
652 if (RT_FAILURE(rc))
653 break;
654 /* Reset S/G buffer for the next filter. */
655 RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
656 }
657
658 return rc;
659}
660
661/**
662 * Applies the filter chain to the given read request.
663 *
664 * @returns VBox status code.
665 * @param pDisk The HDD container.
666 * @param uOffset The start offset of the read.
667 * @param cbRead Number of bytes read.
668 * @param pIoCtx The I/O context associated with the request.
669 */
670static int vdFilterChainApplyRead(PVDISK pDisk, uint64_t uOffset, size_t cbRead,
671 PVDIOCTX pIoCtx)
672{
673 int rc = VINF_SUCCESS;
674
675 VD_IS_LOCKED(pDisk);
676
677 /* Reset buffer before starting. */
678 RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
679
680 PVDFILTER pFilter;
681 RTListForEach(&pDisk->ListFilterChainRead, pFilter, VDFILTER, ListNodeChainRead)
682 {
683 rc = pFilter->pBackend->pfnFilterRead(pFilter->pvBackendData, uOffset, cbRead, pIoCtx);
684 if (RT_FAILURE(rc))
685 break;
686 /* Reset S/G buffer for the next filter. */
687 RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
688 }
689
690 return rc;
691}
692
693DECLINLINE(void) vdIoCtxRootComplete(PVDISK pDisk, PVDIOCTX pIoCtx)
694{
695 if ( RT_SUCCESS(pIoCtx->rcReq)
696 && pIoCtx->enmTxDir == VDIOCTXTXDIR_READ)
697 pIoCtx->rcReq = vdFilterChainApplyRead(pDisk, pIoCtx->Req.Io.uOffsetXferOrig,
698 pIoCtx->Req.Io.cbXferOrig, pIoCtx);
699
700 pIoCtx->Type.Root.pfnComplete(pIoCtx->Type.Root.pvUser1,
701 pIoCtx->Type.Root.pvUser2,
702 pIoCtx->rcReq);
703}
704
705/**
706 * Initialize the structure members of a given I/O context.
707 */
708DECLINLINE(void) vdIoCtxInit(PVDIOCTX pIoCtx, PVDISK pDisk, VDIOCTXTXDIR enmTxDir,
709 uint64_t uOffset, size_t cbTransfer, PVDIMAGE pImageStart,
710 PCRTSGBUF pcSgBuf, void *pvAllocation,
711 PFNVDIOCTXTRANSFER pfnIoCtxTransfer, uint32_t fFlags)
712{
713 pIoCtx->pDisk = pDisk;
714 pIoCtx->enmTxDir = enmTxDir;
715 pIoCtx->Req.Io.cbTransferLeft = (uint32_t)cbTransfer; Assert((uint32_t)cbTransfer == cbTransfer);
716 pIoCtx->Req.Io.uOffset = uOffset;
717 pIoCtx->Req.Io.cbTransfer = cbTransfer;
718 pIoCtx->Req.Io.pImageStart = pImageStart;
719 pIoCtx->Req.Io.pImageCur = pImageStart;
720 pIoCtx->Req.Io.cbBufClear = 0;
721 pIoCtx->Req.Io.pImageParentOverride = NULL;
722 pIoCtx->Req.Io.uOffsetXferOrig = uOffset;
723 pIoCtx->Req.Io.cbXferOrig = cbTransfer;
724 pIoCtx->cDataTransfersPending = 0;
725 pIoCtx->cMetaTransfersPending = 0;
726 pIoCtx->fComplete = false;
727 pIoCtx->fFlags = fFlags;
728 pIoCtx->pvAllocation = pvAllocation;
729 pIoCtx->pfnIoCtxTransfer = pfnIoCtxTransfer;
730 pIoCtx->pfnIoCtxTransferNext = NULL;
731 pIoCtx->rcReq = VINF_SUCCESS;
732 pIoCtx->pIoCtxParent = NULL;
733
734 /* There is no S/G list for a flush request. */
735 if ( enmTxDir != VDIOCTXTXDIR_FLUSH
736 && enmTxDir != VDIOCTXTXDIR_DISCARD)
737 RTSgBufClone(&pIoCtx->Req.Io.SgBuf, pcSgBuf);
738 else
739 memset(&pIoCtx->Req.Io.SgBuf, 0, sizeof(RTSGBUF));
740}
741
742/**
743 * Internal: Tries to read the desired range from the given cache.
744 *
745 * @returns VBox status code.
746 * @retval VERR_VD_BLOCK_FREE if the block is not in the cache.
747 * pcbRead will be set to the number of bytes not in the cache.
748 * Everything thereafter might be in the cache.
749 * @param pCache The cache to read from.
750 * @param uOffset Offset of the virtual disk to read.
751 * @param cbRead How much to read.
752 * @param pIoCtx The I/O context to read into.
753 * @param pcbRead Where to store the number of bytes actually read.
754 * On success this indicates the number of bytes read from the cache.
755 * If VERR_VD_BLOCK_FREE is returned this gives the number of bytes
756 * which are not in the cache.
757 * In both cases everything beyond this value
758 * might or might not be in the cache.
759 */
760static int vdCacheReadHelper(PVDCACHE pCache, uint64_t uOffset,
761 size_t cbRead, PVDIOCTX pIoCtx, size_t *pcbRead)
762{
763 int rc = VINF_SUCCESS;
764
765 LogFlowFunc(("pCache=%#p uOffset=%llu pIoCtx=%p cbRead=%zu pcbRead=%#p\n",
766 pCache, uOffset, pIoCtx, cbRead, pcbRead));
767
768 AssertPtr(pCache);
769 AssertPtr(pcbRead);
770
771 rc = pCache->Backend->pfnRead(pCache->pBackendData, uOffset, cbRead,
772 pIoCtx, pcbRead);
773
774 LogFlowFunc(("returns rc=%Rrc pcbRead=%zu\n", rc, *pcbRead));
775 return rc;
776}
777
778/**
779 * Internal: Writes data for the given block into the cache.
780 *
781 * @returns VBox status code.
782 * @param pCache The cache to write to.
783 * @param uOffset Offset of the virtual disk to write to the cache.
784 * @param cbWrite How much to write.
785 * @param pIoCtx The I/O context to write from.
786 * @param pcbWritten How much data could be written, optional.
787 */
788static int vdCacheWriteHelper(PVDCACHE pCache, uint64_t uOffset, size_t cbWrite,
789 PVDIOCTX pIoCtx, size_t *pcbWritten)
790{
791 int rc = VINF_SUCCESS;
792
793 LogFlowFunc(("pCache=%#p uOffset=%llu pIoCtx=%p cbWrite=%zu pcbWritten=%#p\n",
794 pCache, uOffset, pIoCtx, cbWrite, pcbWritten));
795
796 AssertPtr(pCache);
797 AssertPtr(pIoCtx);
798 Assert(cbWrite > 0);
799
800 if (pcbWritten)
801 rc = pCache->Backend->pfnWrite(pCache->pBackendData, uOffset, cbWrite,
802 pIoCtx, pcbWritten);
803 else
804 {
805 size_t cbWritten = 0;
806
807 do
808 {
809 rc = pCache->Backend->pfnWrite(pCache->pBackendData, uOffset, cbWrite,
810 pIoCtx, &cbWritten);
811 uOffset += cbWritten;
812 cbWrite -= cbWritten;
813 } while ( cbWrite
814 && ( RT_SUCCESS(rc)
815 || rc == VERR_VD_ASYNC_IO_IN_PROGRESS));
816 }
817
818 LogFlowFunc(("returns rc=%Rrc pcbWritten=%zu\n",
819 rc, pcbWritten ? *pcbWritten : cbWrite));
820 return rc;
821}
822
823/**
824 * Creates a new empty discard state.
825 *
826 * @returns Pointer to the new discard state or NULL if out of memory.
827 */
828static PVDDISCARDSTATE vdDiscardStateCreate(void)
829{
830 PVDDISCARDSTATE pDiscard = (PVDDISCARDSTATE)RTMemAllocZ(sizeof(VDDISCARDSTATE));
831
832 if (pDiscard)
833 {
834 RTListInit(&pDiscard->ListLru);
835 pDiscard->pTreeBlocks = (PAVLRU64TREE)RTMemAllocZ(sizeof(AVLRU64TREE));
836 if (!pDiscard->pTreeBlocks)
837 {
838 RTMemFree(pDiscard);
839 pDiscard = NULL;
840 }
841 }
842
843 return pDiscard;
844}
845
846/**
847 * Removes the least recently used blocks from the waiting list until
848 * the new value is reached.
849 *
850 * @returns VBox status code.
851 * @param pDisk VD disk container.
852 * @param pDiscard The discard state.
853 * @param cbDiscardingNew How many bytes should be waiting on success.
854 * The number of bytes waiting can be less.
855 */
856static int vdDiscardRemoveBlocks(PVDISK pDisk, PVDDISCARDSTATE pDiscard, size_t cbDiscardingNew)
857{
858 int rc = VINF_SUCCESS;
859
860 LogFlowFunc(("pDisk=%#p pDiscard=%#p cbDiscardingNew=%zu\n",
861 pDisk, pDiscard, cbDiscardingNew));
862
863 while (pDiscard->cbDiscarding > cbDiscardingNew)
864 {
865 PVDDISCARDBLOCK pBlock = RTListGetLast(&pDiscard->ListLru, VDDISCARDBLOCK, NodeLru);
866
867 Assert(!RTListIsEmpty(&pDiscard->ListLru));
868
869 /* Go over the allocation bitmap and mark all discarded sectors as unused. */
870 uint64_t offStart = pBlock->Core.Key;
871 uint32_t idxStart = 0;
872 size_t cbLeft = pBlock->cbDiscard;
873 bool fAllocated = ASMBitTest(pBlock->pbmAllocated, idxStart);
874 uint32_t cSectors = (uint32_t)(pBlock->cbDiscard / 512);
875
876 while (cbLeft > 0)
877 {
878 int32_t idxEnd;
879 size_t cbThis = cbLeft;
880
881 if (fAllocated)
882 {
883 /* Check for the first unallocated bit. */
884 idxEnd = ASMBitNextClear(pBlock->pbmAllocated, cSectors, idxStart);
885 if (idxEnd != -1)
886 {
887 cbThis = (idxEnd - idxStart) * 512;
888 fAllocated = false;
889 }
890 }
891 else
892 {
893 /* Mark as unused and check for the first set bit. */
894 idxEnd = ASMBitNextSet(pBlock->pbmAllocated, cSectors, idxStart);
895 if (idxEnd != -1)
896 cbThis = (idxEnd - idxStart) * 512;
897
898
899 VDIOCTX IoCtx;
900 vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_DISCARD, 0, 0, NULL,
901 NULL, NULL, NULL, VDIOCTX_FLAGS_SYNC);
902 rc = pDisk->pLast->Backend->pfnDiscard(pDisk->pLast->pBackendData,
903 &IoCtx, offStart, cbThis, NULL,
904 NULL, &cbThis, NULL,
905 VD_DISCARD_MARK_UNUSED);
906 if (RT_FAILURE(rc))
907 break;
908
909 fAllocated = true;
910 }
911
912 idxStart = idxEnd;
913 offStart += cbThis;
914 cbLeft -= cbThis;
915 }
916
917 if (RT_FAILURE(rc))
918 break;
919
920 PVDDISCARDBLOCK pBlockRemove = (PVDDISCARDBLOCK)RTAvlrU64RangeRemove(pDiscard->pTreeBlocks, pBlock->Core.Key);
921 Assert(pBlockRemove == pBlock); NOREF(pBlockRemove);
922 RTListNodeRemove(&pBlock->NodeLru);
923
924 pDiscard->cbDiscarding -= pBlock->cbDiscard;
925 RTMemFree(pBlock->pbmAllocated);
926 RTMemFree(pBlock);
927 }
928
929 Assert(RT_FAILURE(rc) || pDiscard->cbDiscarding <= cbDiscardingNew);
930
931 LogFlowFunc(("returns rc=%Rrc\n", rc));
932 return rc;
933}
934
935/**
936 * Destroys the current discard state, writing any waiting blocks to the image.
937 *
938 * @returns VBox status code.
939 * @param pDisk VD disk container.
940 */
941static int vdDiscardStateDestroy(PVDISK pDisk)
942{
943 int rc = VINF_SUCCESS;
944
945 if (pDisk->pDiscard)
946 {
947 rc = vdDiscardRemoveBlocks(pDisk, pDisk->pDiscard, 0 /* Remove all blocks. */);
948 AssertRC(rc);
949 RTMemFree(pDisk->pDiscard->pTreeBlocks);
950 RTMemFree(pDisk->pDiscard);
951 pDisk->pDiscard = NULL;
952 }
953
954 return rc;
955}
956
957/**
958 * Marks the given range as allocated in the image.
959 * Required if there are discards in progress and a write to a block which can get discarded
960 * is written to.
961 *
962 * @returns VBox status code.
963 * @param pDisk VD container data.
964 * @param uOffset First byte to mark as allocated.
965 * @param cbRange Number of bytes to mark as allocated.
966 */
967static int vdDiscardSetRangeAllocated(PVDISK pDisk, uint64_t uOffset, size_t cbRange)
968{
969 PVDDISCARDSTATE pDiscard = pDisk->pDiscard;
970 int rc = VINF_SUCCESS;
971
972 if (pDiscard)
973 {
974 do
975 {
976 size_t cbThisRange = cbRange;
977 PVDDISCARDBLOCK pBlock = (PVDDISCARDBLOCK)RTAvlrU64RangeGet(pDiscard->pTreeBlocks, uOffset);
978
979 if (pBlock)
980 {
981 int32_t idxStart, idxEnd;
982
983 Assert(!(cbThisRange % 512));
984 Assert(!((uOffset - pBlock->Core.Key) % 512));
985
986 cbThisRange = RT_MIN(cbThisRange, pBlock->Core.KeyLast - uOffset + 1);
987
988 idxStart = (uOffset - pBlock->Core.Key) / 512;
989 idxEnd = idxStart + (int32_t)(cbThisRange / 512);
990 ASMBitSetRange(pBlock->pbmAllocated, idxStart, idxEnd);
991 }
992 else
993 {
994 pBlock = (PVDDISCARDBLOCK)RTAvlrU64GetBestFit(pDiscard->pTreeBlocks, uOffset, true);
995 if (pBlock)
996 cbThisRange = RT_MIN(cbThisRange, pBlock->Core.Key - uOffset);
997 }
998
999 Assert(cbRange >= cbThisRange);
1000
1001 uOffset += cbThisRange;
1002 cbRange -= cbThisRange;
1003 } while (cbRange != 0);
1004 }
1005
1006 return rc;
1007}
1008
1009DECLINLINE(PVDIOCTX) vdIoCtxAlloc(PVDISK pDisk, VDIOCTXTXDIR enmTxDir,
1010 uint64_t uOffset, size_t cbTransfer,
1011 PVDIMAGE pImageStart,PCRTSGBUF pcSgBuf,
1012 void *pvAllocation, PFNVDIOCTXTRANSFER pfnIoCtxTransfer,
1013 uint32_t fFlags)
1014{
1015 PVDIOCTX pIoCtx = NULL;
1016
1017 pIoCtx = (PVDIOCTX)RTMemCacheAlloc(pDisk->hMemCacheIoCtx);
1018 if (RT_LIKELY(pIoCtx))
1019 {
1020 vdIoCtxInit(pIoCtx, pDisk, enmTxDir, uOffset, cbTransfer, pImageStart,
1021 pcSgBuf, pvAllocation, pfnIoCtxTransfer, fFlags);
1022 }
1023
1024 return pIoCtx;
1025}
1026
1027DECLINLINE(PVDIOCTX) vdIoCtxRootAlloc(PVDISK pDisk, VDIOCTXTXDIR enmTxDir,
1028 uint64_t uOffset, size_t cbTransfer,
1029 PVDIMAGE pImageStart, PCRTSGBUF pcSgBuf,
1030 PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
1031 void *pvUser1, void *pvUser2,
1032 void *pvAllocation,
1033 PFNVDIOCTXTRANSFER pfnIoCtxTransfer,
1034 uint32_t fFlags)
1035{
1036 PVDIOCTX pIoCtx = vdIoCtxAlloc(pDisk, enmTxDir, uOffset, cbTransfer, pImageStart,
1037 pcSgBuf, pvAllocation, pfnIoCtxTransfer, fFlags);
1038
1039 if (RT_LIKELY(pIoCtx))
1040 {
1041 pIoCtx->pIoCtxParent = NULL;
1042 pIoCtx->Type.Root.pfnComplete = pfnComplete;
1043 pIoCtx->Type.Root.pvUser1 = pvUser1;
1044 pIoCtx->Type.Root.pvUser2 = pvUser2;
1045 }
1046
1047 LogFlow(("Allocated root I/O context %#p\n", pIoCtx));
1048 return pIoCtx;
1049}
1050
1051DECLINLINE(void) vdIoCtxDiscardInit(PVDIOCTX pIoCtx, PVDISK pDisk, PCRTRANGE paRanges,
1052 unsigned cRanges, PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
1053 void *pvUser1, void *pvUser2, void *pvAllocation,
1054 PFNVDIOCTXTRANSFER pfnIoCtxTransfer, uint32_t fFlags)
1055{
1056 pIoCtx->pIoCtxNext = NULL;
1057 pIoCtx->pDisk = pDisk;
1058 pIoCtx->enmTxDir = VDIOCTXTXDIR_DISCARD;
1059 pIoCtx->cDataTransfersPending = 0;
1060 pIoCtx->cMetaTransfersPending = 0;
1061 pIoCtx->fComplete = false;
1062 pIoCtx->fFlags = fFlags;
1063 pIoCtx->pvAllocation = pvAllocation;
1064 pIoCtx->pfnIoCtxTransfer = pfnIoCtxTransfer;
1065 pIoCtx->pfnIoCtxTransferNext = NULL;
1066 pIoCtx->rcReq = VINF_SUCCESS;
1067 pIoCtx->Req.Discard.paRanges = paRanges;
1068 pIoCtx->Req.Discard.cRanges = cRanges;
1069 pIoCtx->Req.Discard.idxRange = 0;
1070 pIoCtx->Req.Discard.cbDiscardLeft = 0;
1071 pIoCtx->Req.Discard.offCur = 0;
1072 pIoCtx->Req.Discard.cbThisDiscard = 0;
1073
1074 pIoCtx->pIoCtxParent = NULL;
1075 pIoCtx->Type.Root.pfnComplete = pfnComplete;
1076 pIoCtx->Type.Root.pvUser1 = pvUser1;
1077 pIoCtx->Type.Root.pvUser2 = pvUser2;
1078}
1079
1080DECLINLINE(PVDIOCTX) vdIoCtxDiscardAlloc(PVDISK pDisk, PCRTRANGE paRanges,
1081 unsigned cRanges,
1082 PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
1083 void *pvUser1, void *pvUser2,
1084 void *pvAllocation,
1085 PFNVDIOCTXTRANSFER pfnIoCtxTransfer,
1086 uint32_t fFlags)
1087{
1088 PVDIOCTX pIoCtx = NULL;
1089
1090 pIoCtx = (PVDIOCTX)RTMemCacheAlloc(pDisk->hMemCacheIoCtx);
1091 if (RT_LIKELY(pIoCtx))
1092 {
1093 vdIoCtxDiscardInit(pIoCtx, pDisk, paRanges, cRanges, pfnComplete, pvUser1,
1094 pvUser2, pvAllocation, pfnIoCtxTransfer, fFlags);
1095 }
1096
1097 LogFlow(("Allocated discard I/O context %#p\n", pIoCtx));
1098 return pIoCtx;
1099}
1100
1101DECLINLINE(PVDIOCTX) vdIoCtxChildAlloc(PVDISK pDisk, VDIOCTXTXDIR enmTxDir,
1102 uint64_t uOffset, size_t cbTransfer,
1103 PVDIMAGE pImageStart, PCRTSGBUF pcSgBuf,
1104 PVDIOCTX pIoCtxParent, size_t cbTransferParent,
1105 size_t cbWriteParent, void *pvAllocation,
1106 PFNVDIOCTXTRANSFER pfnIoCtxTransfer)
1107{
1108 PVDIOCTX pIoCtx = vdIoCtxAlloc(pDisk, enmTxDir, uOffset, cbTransfer, pImageStart,
1109 pcSgBuf, pvAllocation, pfnIoCtxTransfer, pIoCtxParent->fFlags & ~VDIOCTX_FLAGS_DONT_FREE);
1110
1111 AssertPtr(pIoCtxParent);
1112 Assert(!pIoCtxParent->pIoCtxParent);
1113
1114 if (RT_LIKELY(pIoCtx))
1115 {
1116 pIoCtx->pIoCtxParent = pIoCtxParent;
1117 pIoCtx->Type.Child.uOffsetSaved = uOffset;
1118 pIoCtx->Type.Child.cbTransferLeftSaved = cbTransfer;
1119 pIoCtx->Type.Child.cbTransferParent = cbTransferParent;
1120 pIoCtx->Type.Child.cbWriteParent = cbWriteParent;
1121 }
1122
1123 LogFlow(("Allocated child I/O context %#p\n", pIoCtx));
1124 return pIoCtx;
1125}
1126
1127DECLINLINE(PVDIOTASK) vdIoTaskUserAlloc(PVDIOSTORAGE pIoStorage, PFNVDXFERCOMPLETED pfnComplete, void *pvUser, PVDIOCTX pIoCtx, uint32_t cbTransfer)
1128{
1129 PVDIOTASK pIoTask = NULL;
1130
1131 pIoTask = (PVDIOTASK)RTMemCacheAlloc(pIoStorage->pVDIo->pDisk->hMemCacheIoTask);
1132 if (pIoTask)
1133 {
1134 pIoTask->pIoStorage = pIoStorage;
1135 pIoTask->pfnComplete = pfnComplete;
1136 pIoTask->pvUser = pvUser;
1137 pIoTask->fMeta = false;
1138 pIoTask->Type.User.cbTransfer = cbTransfer;
1139 pIoTask->Type.User.pIoCtx = pIoCtx;
1140 }
1141
1142 return pIoTask;
1143}
1144
1145DECLINLINE(PVDIOTASK) vdIoTaskMetaAlloc(PVDIOSTORAGE pIoStorage, PFNVDXFERCOMPLETED pfnComplete, void *pvUser, PVDMETAXFER pMetaXfer)
1146{
1147 PVDIOTASK pIoTask = NULL;
1148
1149 pIoTask = (PVDIOTASK)RTMemCacheAlloc(pIoStorage->pVDIo->pDisk->hMemCacheIoTask);
1150 if (pIoTask)
1151 {
1152 pIoTask->pIoStorage = pIoStorage;
1153 pIoTask->pfnComplete = pfnComplete;
1154 pIoTask->pvUser = pvUser;
1155 pIoTask->fMeta = true;
1156 pIoTask->Type.Meta.pMetaXfer = pMetaXfer;
1157 }
1158
1159 return pIoTask;
1160}
1161
1162DECLINLINE(void) vdIoCtxFree(PVDISK pDisk, PVDIOCTX pIoCtx)
1163{
1164 Log(("Freeing I/O context %#p\n", pIoCtx));
1165
1166 if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_DONT_FREE))
1167 {
1168 if (pIoCtx->pvAllocation)
1169 RTMemFree(pIoCtx->pvAllocation);
1170#ifdef DEBUG
1171 memset(&pIoCtx->pDisk, 0xff, sizeof(void *));
1172#endif
1173 RTMemCacheFree(pDisk->hMemCacheIoCtx, pIoCtx);
1174 }
1175}
1176
1177DECLINLINE(void) vdIoTaskFree(PVDISK pDisk, PVDIOTASK pIoTask)
1178{
1179#ifdef DEBUG
1180 memset(pIoTask, 0xff, sizeof(VDIOTASK));
1181#endif
1182 RTMemCacheFree(pDisk->hMemCacheIoTask, pIoTask);
1183}
1184
1185DECLINLINE(void) vdIoCtxChildReset(PVDIOCTX pIoCtx)
1186{
1187 AssertPtr(pIoCtx->pIoCtxParent);
1188
1189 RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
1190 pIoCtx->Req.Io.uOffset = pIoCtx->Type.Child.uOffsetSaved;
1191 pIoCtx->Req.Io.cbTransferLeft = (uint32_t)pIoCtx->Type.Child.cbTransferLeftSaved;
1192 Assert((uint32_t)pIoCtx->Type.Child.cbTransferLeftSaved == pIoCtx->Type.Child.cbTransferLeftSaved);
1193}
1194
1195DECLINLINE(PVDMETAXFER) vdMetaXferAlloc(PVDIOSTORAGE pIoStorage, uint64_t uOffset, size_t cb)
1196{
1197 PVDMETAXFER pMetaXfer = (PVDMETAXFER)RTMemAlloc(RT_UOFFSETOF_DYN(VDMETAXFER, abData[cb]));
1198
1199 if (RT_LIKELY(pMetaXfer))
1200 {
1201 pMetaXfer->Core.Key = uOffset;
1202 pMetaXfer->Core.KeyLast = uOffset + cb - 1;
1203 pMetaXfer->fFlags = VDMETAXFER_TXDIR_NONE;
1204 pMetaXfer->cbMeta = cb;
1205 pMetaXfer->pIoStorage = pIoStorage;
1206 pMetaXfer->cRefs = 0;
1207 pMetaXfer->pbDataShw = NULL;
1208 RTListInit(&pMetaXfer->ListIoCtxWaiting);
1209 RTListInit(&pMetaXfer->ListIoCtxShwWrites);
1210 }
1211 return pMetaXfer;
1212}
1213
1214DECLINLINE(void) vdIoCtxAddToWaitingList(volatile PVDIOCTX *ppList, PVDIOCTX pIoCtx)
1215{
1216 /* Put it on the waiting list. */
1217 PVDIOCTX pNext = ASMAtomicUoReadPtrT(ppList, PVDIOCTX);
1218 PVDIOCTX pHeadOld;
1219 pIoCtx->pIoCtxNext = pNext;
1220 while (!ASMAtomicCmpXchgExPtr(ppList, pIoCtx, pNext, &pHeadOld))
1221 {
1222 pNext = pHeadOld;
1223 Assert(pNext != pIoCtx);
1224 pIoCtx->pIoCtxNext = pNext;
1225 ASMNopPause();
1226 }
1227}
1228
1229DECLINLINE(void) vdIoCtxDefer(PVDISK pDisk, PVDIOCTX pIoCtx)
1230{
1231 LogFlowFunc(("Deferring I/O context pIoCtx=%#p\n", pIoCtx));
1232
1233 Assert(!pIoCtx->pIoCtxParent && !(pIoCtx->fFlags & VDIOCTX_FLAGS_BLOCKED));
1234 pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
1235 vdIoCtxAddToWaitingList(&pDisk->pIoCtxBlockedHead, pIoCtx);
1236}
1237
1238static size_t vdIoCtxCopy(PVDIOCTX pIoCtxDst, PVDIOCTX pIoCtxSrc, size_t cbData)
1239{
1240 return RTSgBufCopy(&pIoCtxDst->Req.Io.SgBuf, &pIoCtxSrc->Req.Io.SgBuf, cbData);
1241}
1242
1243#if 0 /* unused */
1244static int vdIoCtxCmp(PVDIOCTX pIoCtx1, PVDIOCTX pIoCtx2, size_t cbData)
1245{
1246 return RTSgBufCmp(&pIoCtx1->Req.Io.SgBuf, &pIoCtx2->Req.Io.SgBuf, cbData);
1247}
1248#endif
1249
1250static size_t vdIoCtxCopyTo(PVDIOCTX pIoCtx, const uint8_t *pbData, size_t cbData)
1251{
1252 return RTSgBufCopyFromBuf(&pIoCtx->Req.Io.SgBuf, pbData, cbData);
1253}
1254
1255static size_t vdIoCtxCopyFrom(PVDIOCTX pIoCtx, uint8_t *pbData, size_t cbData)
1256{
1257 return RTSgBufCopyToBuf(&pIoCtx->Req.Io.SgBuf, pbData, cbData);
1258}
1259
1260static size_t vdIoCtxSet(PVDIOCTX pIoCtx, uint8_t ch, size_t cbData)
1261{
1262 return RTSgBufSet(&pIoCtx->Req.Io.SgBuf, ch, cbData);
1263}
1264
1265/**
1266 * Returns whether the given I/O context has completed.
1267 *
1268 * @returns Flag whether the I/O context is complete.
1269 * @param pIoCtx The I/O context to check.
1270 */
1271DECLINLINE(bool) vdIoCtxIsComplete(PVDIOCTX pIoCtx)
1272{
1273 if ( !pIoCtx->cMetaTransfersPending
1274 && !pIoCtx->cDataTransfersPending
1275 && !pIoCtx->pfnIoCtxTransfer)
1276 return true;
1277
1278 /*
1279 * We complete the I/O context in case of an error
1280 * if there is no I/O task pending.
1281 */
1282 if ( RT_FAILURE(pIoCtx->rcReq)
1283 && !pIoCtx->cMetaTransfersPending
1284 && !pIoCtx->cDataTransfersPending)
1285 return true;
1286
1287 return false;
1288}
1289
1290/**
1291 * Returns whether the given I/O context is blocked due to a metadata transfer
1292 * or because the backend blocked it.
1293 *
1294 * @returns Flag whether the I/O context is blocked.
1295 * @param pIoCtx The I/O context to check.
1296 */
1297DECLINLINE(bool) vdIoCtxIsBlocked(PVDIOCTX pIoCtx)
1298{
1299 /* Don't change anything if there is a metadata transfer pending or we are blocked. */
1300 if ( pIoCtx->cMetaTransfersPending
1301 || (pIoCtx->fFlags & VDIOCTX_FLAGS_BLOCKED))
1302 return true;
1303
1304 return false;
1305}
1306
1307/**
1308 * Process the I/O context, core method which assumes that the I/O context
1309 * acquired the lock.
1310 *
1311 * @returns VBox status code.
1312 * @param pIoCtx I/O context to process.
1313 */
1314static int vdIoCtxProcessLocked(PVDIOCTX pIoCtx)
1315{
1316 int rc = VINF_SUCCESS;
1317
1318 VD_IS_LOCKED(pIoCtx->pDisk);
1319
1320 LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
1321
1322 if (!vdIoCtxIsComplete(pIoCtx))
1323 {
1324 if (!vdIoCtxIsBlocked(pIoCtx))
1325 {
1326 if (pIoCtx->pfnIoCtxTransfer)
1327 {
1328 /* Call the transfer function advancing to the next while there is no error. */
1329 while ( pIoCtx->pfnIoCtxTransfer
1330 && !pIoCtx->cMetaTransfersPending
1331 && RT_SUCCESS(rc))
1332 {
1333 LogFlowFunc(("calling transfer function %#p\n", pIoCtx->pfnIoCtxTransfer));
1334 rc = pIoCtx->pfnIoCtxTransfer(pIoCtx);
1335
1336 /* Advance to the next part of the transfer if the current one succeeded. */
1337 if (RT_SUCCESS(rc))
1338 {
1339 pIoCtx->pfnIoCtxTransfer = pIoCtx->pfnIoCtxTransferNext;
1340 pIoCtx->pfnIoCtxTransferNext = NULL;
1341 }
1342 }
1343 }
1344
1345 if ( RT_SUCCESS(rc)
1346 && !pIoCtx->cMetaTransfersPending
1347 && !pIoCtx->cDataTransfersPending
1348 && !(pIoCtx->fFlags & VDIOCTX_FLAGS_BLOCKED))
1349 rc = VINF_VD_ASYNC_IO_FINISHED;
1350 else if ( RT_SUCCESS(rc)
1351 || rc == VERR_VD_NOT_ENOUGH_METADATA
1352 || rc == VERR_VD_IOCTX_HALT)
1353 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
1354 else if ( RT_FAILURE(rc)
1355 && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
1356 {
1357 ASMAtomicCmpXchgS32(&pIoCtx->rcReq, rc, VINF_SUCCESS);
1358
1359 /*
1360 * The I/O context completed if we have an error and there is no data
1361 * or meta data transfer pending.
1362 */
1363 if ( !pIoCtx->cMetaTransfersPending
1364 && !pIoCtx->cDataTransfersPending)
1365 rc = VINF_VD_ASYNC_IO_FINISHED;
1366 else
1367 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
1368 }
1369 }
1370 else
1371 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
1372 }
1373 else
1374 rc = VINF_VD_ASYNC_IO_FINISHED;
1375
1376 LogFlowFunc(("pIoCtx=%#p rc=%Rrc cDataTransfersPending=%u cMetaTransfersPending=%u fComplete=%RTbool\n",
1377 pIoCtx, rc, pIoCtx->cDataTransfersPending, pIoCtx->cMetaTransfersPending,
1378 pIoCtx->fComplete));
1379
1380 return rc;
1381}
1382
1383/**
1384 * Processes the list of waiting I/O contexts.
1385 *
1386 * @returns VBox status code, only valid if pIoCtxRc is not NULL, treat as void
1387 * function otherwise.
1388 * @param pDisk The disk structure.
1389 * @param pIoCtxRc An I/O context handle which waits on the list. When processed
1390 * The status code is returned. NULL if there is no I/O context
1391 * to return the status code for.
1392 */
1393static int vdDiskProcessWaitingIoCtx(PVDISK pDisk, PVDIOCTX pIoCtxRc)
1394{
1395 int rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
1396
1397 LogFlowFunc(("pDisk=%#p pIoCtxRc=%#p\n", pDisk, pIoCtxRc));
1398
1399 VD_IS_LOCKED(pDisk);
1400
1401 /* Get the waiting list and process it in FIFO order. */
1402 PVDIOCTX pIoCtxHead = ASMAtomicXchgPtrT(&pDisk->pIoCtxHead, NULL, PVDIOCTX);
1403
1404 /* Reverse it. */
1405 PVDIOCTX pCur = pIoCtxHead;
1406 pIoCtxHead = NULL;
1407 while (pCur)
1408 {
1409 PVDIOCTX pInsert = pCur;
1410 pCur = pCur->pIoCtxNext;
1411 pInsert->pIoCtxNext = pIoCtxHead;
1412 pIoCtxHead = pInsert;
1413 }
1414
1415 /* Process now. */
1416 pCur = pIoCtxHead;
1417 while (pCur)
1418 {
1419 int rcTmp;
1420 PVDIOCTX pTmp = pCur;
1421
1422 pCur = pCur->pIoCtxNext;
1423 pTmp->pIoCtxNext = NULL;
1424
1425 /*
1426 * Need to clear the sync flag here if there is a new I/O context
1427 * with it set and the context is not given in pIoCtxRc.
1428 * This happens most likely on a different thread and that one shouldn't
1429 * process the context synchronously.
1430 *
1431 * The thread who issued the context will wait on the event semaphore
1432 * anyway which is signalled when the completion handler is called.
1433 */
1434 if ( pTmp->fFlags & VDIOCTX_FLAGS_SYNC
1435 && pTmp != pIoCtxRc)
1436 pTmp->fFlags &= ~VDIOCTX_FLAGS_SYNC;
1437
1438 rcTmp = vdIoCtxProcessLocked(pTmp);
1439 if (pTmp == pIoCtxRc)
1440 {
1441 if ( rcTmp == VINF_VD_ASYNC_IO_FINISHED
1442 && RT_SUCCESS(pTmp->rcReq)
1443 && pTmp->enmTxDir == VDIOCTXTXDIR_READ)
1444 {
1445 int rc2 = vdFilterChainApplyRead(pDisk, pTmp->Req.Io.uOffsetXferOrig,
1446 pTmp->Req.Io.cbXferOrig, pTmp);
1447 if (RT_FAILURE(rc2))
1448 rcTmp = rc2;
1449 }
1450
1451 /* The given I/O context was processed, pass the return code to the caller. */
1452 if ( rcTmp == VINF_VD_ASYNC_IO_FINISHED
1453 && (pTmp->fFlags & VDIOCTX_FLAGS_SYNC))
1454 rc = pTmp->rcReq;
1455 else
1456 rc = rcTmp;
1457 }
1458 else if ( rcTmp == VINF_VD_ASYNC_IO_FINISHED
1459 && ASMAtomicCmpXchgBool(&pTmp->fComplete, true, false))
1460 {
1461 LogFlowFunc(("Waiting I/O context completed pTmp=%#p\n", pTmp));
1462 vdThreadFinishWrite(pDisk);
1463
1464 bool fFreeCtx = RT_BOOL(!(pTmp->fFlags & VDIOCTX_FLAGS_DONT_FREE));
1465 vdIoCtxRootComplete(pDisk, pTmp);
1466
1467 if (fFreeCtx)
1468 vdIoCtxFree(pDisk, pTmp);
1469 }
1470 }
1471
1472 LogFlowFunc(("returns rc=%Rrc\n", rc));
1473 return rc;
1474}
1475
1476/**
1477 * Processes the list of blocked I/O contexts.
1478 *
1479 * @returns nothing.
1480 * @param pDisk The disk structure.
1481 */
1482static void vdDiskProcessBlockedIoCtx(PVDISK pDisk)
1483{
1484 LogFlowFunc(("pDisk=%#p\n", pDisk));
1485
1486 VD_IS_LOCKED(pDisk);
1487
1488 /* Get the waiting list and process it in FIFO order. */
1489 PVDIOCTX pIoCtxHead = ASMAtomicXchgPtrT(&pDisk->pIoCtxBlockedHead, NULL, PVDIOCTX);
1490
1491 /* Reverse it. */
1492 PVDIOCTX pCur = pIoCtxHead;
1493 pIoCtxHead = NULL;
1494 while (pCur)
1495 {
1496 PVDIOCTX pInsert = pCur;
1497 pCur = pCur->pIoCtxNext;
1498 pInsert->pIoCtxNext = pIoCtxHead;
1499 pIoCtxHead = pInsert;
1500 }
1501
1502 /* Process now. */
1503 pCur = pIoCtxHead;
1504 while (pCur)
1505 {
1506 int rc;
1507 PVDIOCTX pTmp = pCur;
1508
1509 pCur = pCur->pIoCtxNext;
1510 pTmp->pIoCtxNext = NULL;
1511
1512 Assert(!pTmp->pIoCtxParent);
1513 Assert(pTmp->fFlags & VDIOCTX_FLAGS_BLOCKED);
1514 pTmp->fFlags &= ~VDIOCTX_FLAGS_BLOCKED;
1515
1516 rc = vdIoCtxProcessLocked(pTmp);
1517 if ( rc == VINF_VD_ASYNC_IO_FINISHED
1518 && ASMAtomicCmpXchgBool(&pTmp->fComplete, true, false))
1519 {
1520 LogFlowFunc(("Waiting I/O context completed pTmp=%#p\n", pTmp));
1521 vdThreadFinishWrite(pDisk);
1522
1523 bool fFreeCtx = RT_BOOL(!(pTmp->fFlags & VDIOCTX_FLAGS_DONT_FREE));
1524 vdIoCtxRootComplete(pDisk, pTmp);
1525 if (fFreeCtx)
1526 vdIoCtxFree(pDisk, pTmp);
1527 }
1528 }
1529
1530 LogFlowFunc(("returns\n"));
1531}
1532
1533/**
1534 * Processes the I/O context trying to lock the criticial section.
1535 * The context is deferred if the critical section is busy.
1536 *
1537 * @returns VBox status code.
1538 * @param pIoCtx The I/O context to process.
1539 */
1540static int vdIoCtxProcessTryLockDefer(PVDIOCTX pIoCtx)
1541{
1542 int rc = VINF_SUCCESS;
1543 PVDISK pDisk = pIoCtx->pDisk;
1544
1545 Log(("Defer pIoCtx=%#p\n", pIoCtx));
1546
1547 /* Put it on the waiting list first. */
1548 vdIoCtxAddToWaitingList(&pDisk->pIoCtxHead, pIoCtx);
1549
1550 if (ASMAtomicCmpXchgBool(&pDisk->fLocked, true, false))
1551 {
1552 /* Leave it again, the context will be processed just before leaving the lock. */
1553 LogFlowFunc(("Successfully acquired the lock\n"));
1554 rc = vdDiskUnlock(pDisk, pIoCtx);
1555 }
1556 else
1557 {
1558 LogFlowFunc(("Lock is held\n"));
1559 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
1560 }
1561
1562 return rc;
1563}
1564
1565/**
1566 * Process the I/O context in a synchronous manner, waiting
1567 * for it to complete.
1568 *
1569 * @returns VBox status code of the completed request.
1570 * @param pIoCtx The sync I/O context.
1571 * @param hEventComplete Event sempahore to wait on for completion.
1572 */
1573static int vdIoCtxProcessSync(PVDIOCTX pIoCtx, RTSEMEVENT hEventComplete)
1574{
1575 int rc = VINF_SUCCESS;
1576 PVDISK pDisk = pIoCtx->pDisk;
1577
1578 LogFlowFunc(("pIoCtx=%p\n", pIoCtx));
1579
1580 AssertMsg(pIoCtx->fFlags & (VDIOCTX_FLAGS_SYNC | VDIOCTX_FLAGS_DONT_FREE),
1581 ("I/O context is not marked as synchronous\n"));
1582
1583 rc = vdIoCtxProcessTryLockDefer(pIoCtx);
1584 if (rc == VINF_VD_ASYNC_IO_FINISHED)
1585 rc = VINF_SUCCESS;
1586
1587 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1588 {
1589 rc = RTSemEventWait(hEventComplete, RT_INDEFINITE_WAIT);
1590 AssertRC(rc);
1591 }
1592
1593 rc = pIoCtx->rcReq;
1594 vdIoCtxFree(pDisk, pIoCtx);
1595
1596 return rc;
1597}
1598
1599DECLINLINE(bool) vdIoCtxIsDiskLockOwner(PVDISK pDisk, PVDIOCTX pIoCtx)
1600{
1601 return pDisk->pIoCtxLockOwner == pIoCtx;
1602}
1603
1604static int vdIoCtxLockDisk(PVDISK pDisk, PVDIOCTX pIoCtx)
1605{
1606 int rc = VINF_SUCCESS;
1607
1608 VD_IS_LOCKED(pDisk);
1609
1610 LogFlowFunc(("pDisk=%#p pIoCtx=%#p\n", pDisk, pIoCtx));
1611
1612 if (!ASMAtomicCmpXchgPtr(&pDisk->pIoCtxLockOwner, pIoCtx, NIL_VDIOCTX))
1613 {
1614 Assert(pDisk->pIoCtxLockOwner != pIoCtx); /* No nesting allowed. */
1615 vdIoCtxDefer(pDisk, pIoCtx);
1616 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
1617 }
1618
1619 LogFlowFunc(("returns -> %Rrc\n", rc));
1620 return rc;
1621}
1622
1623static void vdIoCtxUnlockDisk(PVDISK pDisk, PVDIOCTX pIoCtx, bool fProcessBlockedReqs)
1624{
1625 RT_NOREF1(pIoCtx);
1626 LogFlowFunc(("pDisk=%#p pIoCtx=%#p fProcessBlockedReqs=%RTbool\n",
1627 pDisk, pIoCtx, fProcessBlockedReqs));
1628
1629 VD_IS_LOCKED(pDisk);
1630
1631 LogFlow(("Unlocking disk lock owner is %#p\n", pDisk->pIoCtxLockOwner));
1632 Assert(pDisk->pIoCtxLockOwner == pIoCtx);
1633 ASMAtomicXchgPtrT(&pDisk->pIoCtxLockOwner, NIL_VDIOCTX, PVDIOCTX);
1634
1635 if (fProcessBlockedReqs)
1636 {
1637 /* Process any blocked writes if the current request didn't caused another growing. */
1638 vdDiskProcessBlockedIoCtx(pDisk);
1639 }
1640
1641 LogFlowFunc(("returns\n"));
1642}
1643
1644/**
1645 * Internal: Reads a given amount of data from the image chain of the disk.
1646 **/
1647static int vdDiskReadHelper(PVDISK pDisk, PVDIMAGE pImage, PVDIMAGE pImageParentOverride,
1648 uint64_t uOffset, size_t cbRead, PVDIOCTX pIoCtx, size_t *pcbThisRead)
1649{
1650 RT_NOREF1(pDisk);
1651 int rc = VINF_SUCCESS;
1652 size_t cbThisRead = cbRead;
1653
1654 AssertPtr(pcbThisRead);
1655
1656 *pcbThisRead = 0;
1657
1658 /*
1659 * Try to read from the given image.
1660 * If the block is not allocated read from override chain if present.
1661 */
1662 rc = pImage->Backend->pfnRead(pImage->pBackendData,
1663 uOffset, cbThisRead, pIoCtx,
1664 &cbThisRead);
1665
1666 if (rc == VERR_VD_BLOCK_FREE)
1667 {
1668 for (PVDIMAGE pCurrImage = pImageParentOverride ? pImageParentOverride : pImage->pPrev;
1669 pCurrImage != NULL && rc == VERR_VD_BLOCK_FREE;
1670 pCurrImage = pCurrImage->pPrev)
1671 {
1672 rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
1673 uOffset, cbThisRead, pIoCtx,
1674 &cbThisRead);
1675 }
1676 }
1677
1678 if (RT_SUCCESS(rc) || rc == VERR_VD_BLOCK_FREE)
1679 *pcbThisRead = cbThisRead;
1680
1681 return rc;
1682}
1683
1684/**
1685 * internal: read the specified amount of data in whatever blocks the backend
1686 * will give us - async version.
1687 */
1688static DECLCALLBACK(int) vdReadHelperAsync(PVDIOCTX pIoCtx)
1689{
1690 int rc;
1691 PVDISK pDisk = pIoCtx->pDisk;
1692 size_t cbToRead = pIoCtx->Req.Io.cbTransfer;
1693 uint64_t uOffset = pIoCtx->Req.Io.uOffset;
1694 PVDIMAGE pCurrImage = pIoCtx->Req.Io.pImageCur;
1695 PVDIMAGE pImageParentOverride = pIoCtx->Req.Io.pImageParentOverride;
1696 unsigned cImagesRead = pIoCtx->Req.Io.cImagesRead;
1697 size_t cbThisRead;
1698
1699 /*
1700 * Check whether there is a full block write in progress which was not allocated.
1701 * Defer I/O if the range interferes but only if it does not belong to the
1702 * write doing the allocation.
1703 */
1704 if ( pDisk->pIoCtxLockOwner != NIL_VDIOCTX
1705 && uOffset >= pDisk->uOffsetStartLocked
1706 && uOffset < pDisk->uOffsetEndLocked
1707 && ( !pIoCtx->pIoCtxParent
1708 || pIoCtx->pIoCtxParent != pDisk->pIoCtxLockOwner))
1709 {
1710 Log(("Interferring read while allocating a new block => deferring read\n"));
1711 vdIoCtxDefer(pDisk, pIoCtx);
1712 return VERR_VD_ASYNC_IO_IN_PROGRESS;
1713 }
1714
1715 /* Loop until all reads started or we have a backend which needs to read metadata. */
1716 do
1717 {
1718 /* Search for image with allocated block. Do not attempt to read more
1719 * than the previous reads marked as valid. Otherwise this would return
1720 * stale data when different block sizes are used for the images. */
1721 cbThisRead = cbToRead;
1722
1723 if ( pDisk->pCache
1724 && !pImageParentOverride)
1725 {
1726 rc = vdCacheReadHelper(pDisk->pCache, uOffset, cbThisRead,
1727 pIoCtx, &cbThisRead);
1728 if (rc == VERR_VD_BLOCK_FREE)
1729 {
1730 rc = vdDiskReadHelper(pDisk, pCurrImage, NULL, uOffset, cbThisRead,
1731 pIoCtx, &cbThisRead);
1732
1733 /* If the read was successful, write the data back into the cache. */
1734 if ( RT_SUCCESS(rc)
1735 && pIoCtx->fFlags & VDIOCTX_FLAGS_READ_UPDATE_CACHE)
1736 {
1737 rc = vdCacheWriteHelper(pDisk->pCache, uOffset, cbThisRead,
1738 pIoCtx, NULL);
1739 }
1740 }
1741 }
1742 else
1743 {
1744 /*
1745 * Try to read from the given image.
1746 * If the block is not allocated read from override chain if present.
1747 */
1748 rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
1749 uOffset, cbThisRead, pIoCtx,
1750 &cbThisRead);
1751
1752 if ( rc == VERR_VD_BLOCK_FREE
1753 && cImagesRead != 1)
1754 {
1755 unsigned cImagesToProcess = cImagesRead;
1756
1757 pCurrImage = pImageParentOverride ? pImageParentOverride : pCurrImage->pPrev;
1758 pIoCtx->Req.Io.pImageParentOverride = NULL;
1759
1760 while (pCurrImage && rc == VERR_VD_BLOCK_FREE)
1761 {
1762 rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
1763 uOffset, cbThisRead,
1764 pIoCtx, &cbThisRead);
1765 if (cImagesToProcess == 1)
1766 break;
1767 else if (cImagesToProcess > 0)
1768 cImagesToProcess--;
1769
1770 if (rc == VERR_VD_BLOCK_FREE)
1771 pCurrImage = pCurrImage->pPrev;
1772 }
1773 }
1774 }
1775
1776 /* The task state will be updated on success already, don't do it here!. */
1777 if (rc == VERR_VD_BLOCK_FREE)
1778 {
1779 /* No image in the chain contains the data for the block. */
1780 ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbThisRead); Assert(cbThisRead == (uint32_t)cbThisRead);
1781
1782 /* Fill the free space with 0 if we are told to do so
1783 * or a previous read returned valid data. */
1784 if (pIoCtx->fFlags & VDIOCTX_FLAGS_ZERO_FREE_BLOCKS)
1785 vdIoCtxSet(pIoCtx, '\0', cbThisRead);
1786 else
1787 pIoCtx->Req.Io.cbBufClear += cbThisRead;
1788
1789 if (pIoCtx->Req.Io.pImageCur->uOpenFlags & VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS)
1790 rc = VINF_VD_NEW_ZEROED_BLOCK;
1791 else
1792 rc = VINF_SUCCESS;
1793 }
1794 else if (rc == VERR_VD_IOCTX_HALT)
1795 {
1796 uOffset += cbThisRead;
1797 cbToRead -= cbThisRead;
1798 pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
1799 }
1800 else if ( RT_SUCCESS(rc)
1801 || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1802 {
1803 /* First not free block, fill the space before with 0. */
1804 if ( pIoCtx->Req.Io.cbBufClear
1805 && !(pIoCtx->fFlags & VDIOCTX_FLAGS_ZERO_FREE_BLOCKS))
1806 {
1807 RTSGBUF SgBuf;
1808 RTSgBufClone(&SgBuf, &pIoCtx->Req.Io.SgBuf);
1809 RTSgBufReset(&SgBuf);
1810 RTSgBufSet(&SgBuf, 0, pIoCtx->Req.Io.cbBufClear);
1811 pIoCtx->Req.Io.cbBufClear = 0;
1812 pIoCtx->fFlags |= VDIOCTX_FLAGS_ZERO_FREE_BLOCKS;
1813 }
1814 rc = VINF_SUCCESS;
1815 }
1816
1817 if (RT_FAILURE(rc))
1818 break;
1819
1820 cbToRead -= cbThisRead;
1821 uOffset += cbThisRead;
1822 pCurrImage = pIoCtx->Req.Io.pImageStart; /* Start with the highest image in the chain. */
1823 } while (cbToRead != 0 && RT_SUCCESS(rc));
1824
1825 if ( rc == VERR_VD_NOT_ENOUGH_METADATA
1826 || rc == VERR_VD_IOCTX_HALT)
1827 {
1828 /* Save the current state. */
1829 pIoCtx->Req.Io.uOffset = uOffset;
1830 pIoCtx->Req.Io.cbTransfer = cbToRead;
1831 pIoCtx->Req.Io.pImageCur = pCurrImage ? pCurrImage : pIoCtx->Req.Io.pImageStart;
1832 }
1833
1834 return (!(pIoCtx->fFlags & VDIOCTX_FLAGS_ZERO_FREE_BLOCKS))
1835 ? VERR_VD_BLOCK_FREE
1836 : rc;
1837}
1838
1839/**
1840 * internal: parent image read wrapper for compacting.
1841 */
1842static DECLCALLBACK(int) vdParentRead(void *pvUser, uint64_t uOffset, void *pvBuf,
1843 size_t cbRead)
1844{
1845 PVDPARENTSTATEDESC pParentState = (PVDPARENTSTATEDESC)pvUser;
1846
1847 /** @todo
1848 * Only used for compaction so far which is not possible to mix with async I/O.
1849 * Needs to be changed if we want to support online compaction of images.
1850 */
1851 bool fLocked = ASMAtomicXchgBool(&pParentState->pDisk->fLocked, true);
1852 AssertMsgReturn(!fLocked,
1853 ("Calling synchronous parent read while another thread holds the disk lock\n"),
1854 VERR_VD_INVALID_STATE);
1855
1856 /* Fake an I/O context. */
1857 RTSGSEG Segment;
1858 RTSGBUF SgBuf;
1859 VDIOCTX IoCtx;
1860
1861 Segment.pvSeg = pvBuf;
1862 Segment.cbSeg = cbRead;
1863 RTSgBufInit(&SgBuf, &Segment, 1);
1864 vdIoCtxInit(&IoCtx, pParentState->pDisk, VDIOCTXTXDIR_READ, uOffset, cbRead, pParentState->pImage,
1865 &SgBuf, NULL, NULL, VDIOCTX_FLAGS_SYNC | VDIOCTX_FLAGS_ZERO_FREE_BLOCKS);
1866 int rc = vdReadHelperAsync(&IoCtx);
1867 ASMAtomicXchgBool(&pParentState->pDisk->fLocked, false);
1868 return rc;
1869}
1870
1871/**
1872 * Extended version of vdReadHelper(), implementing certain optimizations
1873 * for image cloning.
1874 *
1875 * @returns VBox status code.
1876 * @param pDisk The disk to read from.
1877 * @param pImage The image to start reading from.
1878 * @param pImageParentOverride The parent image to read from
1879 * if the starting image returns a free block.
1880 * If NULL is passed the real parent of the image
1881 * in the chain is used.
1882 * @param uOffset Offset in the disk to start reading from.
1883 * @param pvBuf Where to store the read data.
1884 * @param cbRead How much to read.
1885 * @param fZeroFreeBlocks Flag whether free blocks should be zeroed.
1886 * If false and no image has data for sepcified
1887 * range VERR_VD_BLOCK_FREE is returned.
1888 * Note that unallocated blocks are still zeroed
1889 * if at least one image has valid data for a part
1890 * of the range.
1891 * @param fUpdateCache Flag whether to update the attached cache if
1892 * available.
1893 * @param cImagesRead Number of images in the chain to read until
1894 * the read is cut off. A value of 0 disables the cut off.
1895 */
1896static int vdReadHelperEx(PVDISK pDisk, PVDIMAGE pImage, PVDIMAGE pImageParentOverride,
1897 uint64_t uOffset, void *pvBuf, size_t cbRead,
1898 bool fZeroFreeBlocks, bool fUpdateCache, unsigned cImagesRead)
1899{
1900 int rc = VINF_SUCCESS;
1901 uint32_t fFlags = VDIOCTX_FLAGS_SYNC | VDIOCTX_FLAGS_DONT_FREE;
1902 RTSGSEG Segment;
1903 RTSGBUF SgBuf;
1904 VDIOCTX IoCtx;
1905 RTSEMEVENT hEventComplete = NIL_RTSEMEVENT;
1906
1907 rc = RTSemEventCreate(&hEventComplete);
1908 if (RT_FAILURE(rc))
1909 return rc;
1910
1911 if (fZeroFreeBlocks)
1912 fFlags |= VDIOCTX_FLAGS_ZERO_FREE_BLOCKS;
1913 if (fUpdateCache)
1914 fFlags |= VDIOCTX_FLAGS_READ_UPDATE_CACHE;
1915
1916 Segment.pvSeg = pvBuf;
1917 Segment.cbSeg = cbRead;
1918 RTSgBufInit(&SgBuf, &Segment, 1);
1919 vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_READ, uOffset, cbRead, pImage, &SgBuf,
1920 NULL, vdReadHelperAsync, fFlags);
1921
1922 IoCtx.Req.Io.pImageParentOverride = pImageParentOverride;
1923 IoCtx.Req.Io.cImagesRead = cImagesRead;
1924 IoCtx.Type.Root.pfnComplete = vdIoCtxSyncComplete;
1925 IoCtx.Type.Root.pvUser1 = pDisk;
1926 IoCtx.Type.Root.pvUser2 = hEventComplete;
1927 rc = vdIoCtxProcessSync(&IoCtx, hEventComplete);
1928 RTSemEventDestroy(hEventComplete);
1929 return rc;
1930}
1931
1932/**
1933 * internal: read the specified amount of data in whatever blocks the backend
1934 * will give us.
1935 */
1936static int vdReadHelper(PVDISK pDisk, PVDIMAGE pImage, uint64_t uOffset,
1937 void *pvBuf, size_t cbRead, bool fUpdateCache)
1938{
1939 return vdReadHelperEx(pDisk, pImage, NULL, uOffset, pvBuf, cbRead,
1940 true /* fZeroFreeBlocks */, fUpdateCache, 0);
1941}
1942
1943/**
1944 * internal: mark the disk as not modified.
1945 */
1946static void vdResetModifiedFlag(PVDISK pDisk)
1947{
1948 if (pDisk->uModified & VD_IMAGE_MODIFIED_FLAG)
1949 {
1950 /* generate new last-modified uuid */
1951 if (!(pDisk->uModified & VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
1952 {
1953 RTUUID Uuid;
1954
1955 RTUuidCreate(&Uuid);
1956 pDisk->pLast->Backend->pfnSetModificationUuid(pDisk->pLast->pBackendData,
1957 &Uuid);
1958
1959 if (pDisk->pCache)
1960 pDisk->pCache->Backend->pfnSetModificationUuid(pDisk->pCache->pBackendData,
1961 &Uuid);
1962 }
1963
1964 pDisk->uModified &= ~VD_IMAGE_MODIFIED_FLAG;
1965 }
1966}
1967
1968/**
1969 * internal: mark the disk as modified.
1970 */
1971static void vdSetModifiedFlag(PVDISK pDisk)
1972{
1973 pDisk->uModified |= VD_IMAGE_MODIFIED_FLAG;
1974 if (pDisk->uModified & VD_IMAGE_MODIFIED_FIRST)
1975 {
1976 pDisk->uModified &= ~VD_IMAGE_MODIFIED_FIRST;
1977
1978 /* First modify, so create a UUID and ensure it's written to disk. */
1979 vdResetModifiedFlag(pDisk);
1980
1981 if (!(pDisk->uModified & VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
1982 {
1983 VDIOCTX IoCtx;
1984 vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_FLUSH, 0, 0, NULL,
1985 NULL, NULL, NULL, VDIOCTX_FLAGS_SYNC);
1986 pDisk->pLast->Backend->pfnFlush(pDisk->pLast->pBackendData, &IoCtx);
1987 }
1988 }
1989}
1990
1991/**
1992 * internal: write buffer to the image, taking care of block boundaries and
1993 * write optimizations.
1994 */
1995static int vdWriteHelperEx(PVDISK pDisk, PVDIMAGE pImage,
1996 PVDIMAGE pImageParentOverride, uint64_t uOffset,
1997 const void *pvBuf, size_t cbWrite,
1998 uint32_t fFlags, unsigned cImagesRead)
1999{
2000 int rc = VINF_SUCCESS;
2001 RTSGSEG Segment;
2002 RTSGBUF SgBuf;
2003 VDIOCTX IoCtx;
2004 RTSEMEVENT hEventComplete = NIL_RTSEMEVENT;
2005
2006 rc = RTSemEventCreate(&hEventComplete);
2007 if (RT_FAILURE(rc))
2008 return rc;
2009
2010 fFlags |= VDIOCTX_FLAGS_SYNC | VDIOCTX_FLAGS_DONT_FREE;
2011
2012 Segment.pvSeg = (void *)pvBuf;
2013 Segment.cbSeg = cbWrite;
2014 RTSgBufInit(&SgBuf, &Segment, 1);
2015 vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_WRITE, uOffset, cbWrite, pImage, &SgBuf,
2016 NULL, vdWriteHelperAsync, fFlags);
2017
2018 IoCtx.Req.Io.pImageParentOverride = pImageParentOverride;
2019 IoCtx.Req.Io.cImagesRead = cImagesRead;
2020 IoCtx.pIoCtxParent = NULL;
2021 IoCtx.Type.Root.pfnComplete = vdIoCtxSyncComplete;
2022 IoCtx.Type.Root.pvUser1 = pDisk;
2023 IoCtx.Type.Root.pvUser2 = hEventComplete;
2024 if (RT_SUCCESS(rc))
2025 rc = vdIoCtxProcessSync(&IoCtx, hEventComplete);
2026
2027 RTSemEventDestroy(hEventComplete);
2028 return rc;
2029}
2030
2031/**
2032 * internal: write buffer to the image, taking care of block boundaries and
2033 * write optimizations.
2034 */
2035static int vdWriteHelper(PVDISK pDisk, PVDIMAGE pImage, uint64_t uOffset,
2036 const void *pvBuf, size_t cbWrite, uint32_t fFlags)
2037{
2038 return vdWriteHelperEx(pDisk, pImage, NULL, uOffset, pvBuf, cbWrite,
2039 fFlags, 0);
2040}
2041
2042/**
2043 * Internal: Copies the content of one disk to another one applying optimizations
2044 * to speed up the copy process if possible.
2045 */
2046static int vdCopyHelper(PVDISK pDiskFrom, PVDIMAGE pImageFrom, PVDISK pDiskTo,
2047 uint64_t cbSize, unsigned cImagesFromRead, unsigned cImagesToRead,
2048 bool fSuppressRedundantIo, PVDINTERFACEPROGRESS pIfProgress,
2049 PVDINTERFACEPROGRESS pDstIfProgress)
2050{
2051 int rc = VINF_SUCCESS;
2052 int rc2;
2053 uint64_t uOffset = 0;
2054 uint64_t cbRemaining = cbSize;
2055 void *pvBuf = NULL;
2056 bool fLockReadFrom = false;
2057 bool fLockWriteTo = false;
2058 bool fBlockwiseCopy = false;
2059 unsigned uProgressOld = 0;
2060
2061 LogFlowFunc(("pDiskFrom=%#p pImageFrom=%#p pDiskTo=%#p cbSize=%llu cImagesFromRead=%u cImagesToRead=%u fSuppressRedundantIo=%RTbool pIfProgress=%#p pDstIfProgress=%#p\n",
2062 pDiskFrom, pImageFrom, pDiskTo, cbSize, cImagesFromRead, cImagesToRead, fSuppressRedundantIo, pDstIfProgress, pDstIfProgress));
2063
2064 if ( (fSuppressRedundantIo || (cImagesFromRead > 0))
2065 && RTListIsEmpty(&pDiskFrom->ListFilterChainRead))
2066 fBlockwiseCopy = true;
2067
2068 /* Allocate tmp buffer. */
2069 pvBuf = RTMemTmpAlloc(VD_MERGE_BUFFER_SIZE);
2070 if (!pvBuf)
2071 return rc;
2072
2073 do
2074 {
2075 size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
2076
2077 /* Note that we don't attempt to synchronize cross-disk accesses.
2078 * It wouldn't be very difficult to do, just the lock order would
2079 * need to be defined somehow to prevent deadlocks. Postpone such
2080 * magic as there is no use case for this. */
2081
2082 rc2 = vdThreadStartRead(pDiskFrom);
2083 AssertRC(rc2);
2084 fLockReadFrom = true;
2085
2086 if (fBlockwiseCopy)
2087 {
2088 RTSGSEG SegmentBuf;
2089 RTSGBUF SgBuf;
2090 VDIOCTX IoCtx;
2091
2092 SegmentBuf.pvSeg = pvBuf;
2093 SegmentBuf.cbSeg = VD_MERGE_BUFFER_SIZE;
2094 RTSgBufInit(&SgBuf, &SegmentBuf, 1);
2095 vdIoCtxInit(&IoCtx, pDiskFrom, VDIOCTXTXDIR_READ, 0, 0, NULL,
2096 &SgBuf, NULL, NULL, VDIOCTX_FLAGS_SYNC);
2097
2098 /* Read the source data. */
2099 rc = pImageFrom->Backend->pfnRead(pImageFrom->pBackendData,
2100 uOffset, cbThisRead, &IoCtx,
2101 &cbThisRead);
2102
2103 if ( rc == VERR_VD_BLOCK_FREE
2104 && cImagesFromRead != 1)
2105 {
2106 unsigned cImagesToProcess = cImagesFromRead;
2107
2108 for (PVDIMAGE pCurrImage = pImageFrom->pPrev;
2109 pCurrImage != NULL && rc == VERR_VD_BLOCK_FREE;
2110 pCurrImage = pCurrImage->pPrev)
2111 {
2112 rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
2113 uOffset, cbThisRead,
2114 &IoCtx, &cbThisRead);
2115 if (cImagesToProcess == 1)
2116 break;
2117 else if (cImagesToProcess > 0)
2118 cImagesToProcess--;
2119 }
2120 }
2121 }
2122 else
2123 rc = vdReadHelper(pDiskFrom, pImageFrom, uOffset, pvBuf, cbThisRead,
2124 false /* fUpdateCache */);
2125
2126 if (RT_FAILURE(rc) && rc != VERR_VD_BLOCK_FREE)
2127 break;
2128
2129 rc2 = vdThreadFinishRead(pDiskFrom);
2130 AssertRC(rc2);
2131 fLockReadFrom = false;
2132
2133 if (rc != VERR_VD_BLOCK_FREE)
2134 {
2135 rc2 = vdThreadStartWrite(pDiskTo);
2136 AssertRC(rc2);
2137 fLockWriteTo = true;
2138
2139 /* Only do collapsed I/O if we are copying the data blockwise. */
2140 rc = vdWriteHelperEx(pDiskTo, pDiskTo->pLast, NULL, uOffset, pvBuf,
2141 cbThisRead, VDIOCTX_FLAGS_DONT_SET_MODIFIED_FLAG /* fFlags */,
2142 fBlockwiseCopy ? cImagesToRead : 0);
2143 if (RT_FAILURE(rc))
2144 break;
2145
2146 rc2 = vdThreadFinishWrite(pDiskTo);
2147 AssertRC(rc2);
2148 fLockWriteTo = false;
2149 }
2150 else /* Don't propagate the error to the outside */
2151 rc = VINF_SUCCESS;
2152
2153 uOffset += cbThisRead;
2154 cbRemaining -= cbThisRead;
2155
2156 unsigned uProgressNew = uOffset * 99 / cbSize;
2157 if (uProgressNew != uProgressOld)
2158 {
2159 uProgressOld = uProgressNew;
2160
2161 if (pIfProgress && pIfProgress->pfnProgress)
2162 {
2163 rc = pIfProgress->pfnProgress(pIfProgress->Core.pvUser,
2164 uProgressOld);
2165 if (RT_FAILURE(rc))
2166 break;
2167 }
2168 if (pDstIfProgress && pDstIfProgress->pfnProgress)
2169 {
2170 rc = pDstIfProgress->pfnProgress(pDstIfProgress->Core.pvUser,
2171 uProgressOld);
2172 if (RT_FAILURE(rc))
2173 break;
2174 }
2175 }
2176 } while (uOffset < cbSize);
2177
2178 RTMemFree(pvBuf);
2179
2180 if (fLockReadFrom)
2181 {
2182 rc2 = vdThreadFinishRead(pDiskFrom);
2183 AssertRC(rc2);
2184 }
2185
2186 if (fLockWriteTo)
2187 {
2188 rc2 = vdThreadFinishWrite(pDiskTo);
2189 AssertRC(rc2);
2190 }
2191
2192 LogFlowFunc(("returns rc=%Rrc\n", rc));
2193 return rc;
2194}
2195
2196/**
2197 * Flush helper async version.
2198 */
2199static DECLCALLBACK(int) vdSetModifiedHelperAsync(PVDIOCTX pIoCtx)
2200{
2201 int rc = VINF_SUCCESS;
2202 PVDIMAGE pImage = pIoCtx->Req.Io.pImageCur;
2203
2204 rc = pImage->Backend->pfnFlush(pImage->pBackendData, pIoCtx);
2205 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
2206 rc = VINF_SUCCESS;
2207
2208 return rc;
2209}
2210
2211/**
2212 * internal: mark the disk as modified - async version.
2213 */
2214static int vdSetModifiedFlagAsync(PVDISK pDisk, PVDIOCTX pIoCtx)
2215{
2216 int rc = VINF_SUCCESS;
2217
2218 VD_IS_LOCKED(pDisk);
2219
2220 pDisk->uModified |= VD_IMAGE_MODIFIED_FLAG;
2221 if (pDisk->uModified & VD_IMAGE_MODIFIED_FIRST)
2222 {
2223 rc = vdIoCtxLockDisk(pDisk, pIoCtx);
2224 if (RT_SUCCESS(rc))
2225 {
2226 pDisk->uModified &= ~VD_IMAGE_MODIFIED_FIRST;
2227
2228 /* First modify, so create a UUID and ensure it's written to disk. */
2229 vdResetModifiedFlag(pDisk);
2230
2231 if (!(pDisk->uModified & VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
2232 {
2233 PVDIOCTX pIoCtxFlush = vdIoCtxChildAlloc(pDisk, VDIOCTXTXDIR_FLUSH,
2234 0, 0, pDisk->pLast,
2235 NULL, pIoCtx, 0, 0, NULL,
2236 vdSetModifiedHelperAsync);
2237
2238 if (pIoCtxFlush)
2239 {
2240 rc = vdIoCtxProcessLocked(pIoCtxFlush);
2241 if (rc == VINF_VD_ASYNC_IO_FINISHED)
2242 {
2243 vdIoCtxUnlockDisk(pDisk, pIoCtx, false /* fProcessDeferredReqs */);
2244 vdIoCtxFree(pDisk, pIoCtxFlush);
2245 }
2246 else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
2247 {
2248 ASMAtomicIncU32(&pIoCtx->cDataTransfersPending);
2249 pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
2250 }
2251 else /* Another error */
2252 vdIoCtxFree(pDisk, pIoCtxFlush);
2253 }
2254 else
2255 rc = VERR_NO_MEMORY;
2256 }
2257 }
2258 }
2259
2260 return rc;
2261}
2262
2263static DECLCALLBACK(int) vdWriteHelperCommitAsync(PVDIOCTX pIoCtx)
2264{
2265 int rc = VINF_SUCCESS;
2266 PVDIMAGE pImage = pIoCtx->Req.Io.pImageStart;
2267 size_t cbPreRead = pIoCtx->Type.Child.cbPreRead;
2268 size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
2269 size_t cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
2270
2271 LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
2272 rc = pImage->Backend->pfnWrite(pImage->pBackendData,
2273 pIoCtx->Req.Io.uOffset - cbPreRead,
2274 cbPreRead + cbThisWrite + cbPostRead,
2275 pIoCtx, NULL, &cbPreRead, &cbPostRead, 0);
2276 Assert(rc != VERR_VD_BLOCK_FREE);
2277 Assert(rc == VERR_VD_NOT_ENOUGH_METADATA || cbPreRead == 0);
2278 Assert(rc == VERR_VD_NOT_ENOUGH_METADATA || cbPostRead == 0);
2279 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
2280 rc = VINF_SUCCESS;
2281 else if (rc == VERR_VD_IOCTX_HALT)
2282 {
2283 pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
2284 rc = VINF_SUCCESS;
2285 }
2286
2287 LogFlowFunc(("returns rc=%Rrc\n", rc));
2288 return rc;
2289}
2290
2291static DECLCALLBACK(int) vdWriteHelperOptimizedCmpAndWriteAsync(PVDIOCTX pIoCtx)
2292{
2293 int rc = VINF_SUCCESS;
2294 size_t cbThisWrite = 0;
2295 size_t cbPreRead = pIoCtx->Type.Child.cbPreRead;
2296 size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
2297 size_t cbWriteCopy = pIoCtx->Type.Child.Write.Optimized.cbWriteCopy;
2298 size_t cbFill = pIoCtx->Type.Child.Write.Optimized.cbFill;
2299 size_t cbReadImage = pIoCtx->Type.Child.Write.Optimized.cbReadImage;
2300 PVDIOCTX pIoCtxParent = pIoCtx->pIoCtxParent;
2301
2302 LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
2303
2304 AssertPtr(pIoCtxParent);
2305 Assert(!pIoCtxParent->pIoCtxParent);
2306 Assert(!pIoCtx->Req.Io.cbTransferLeft && !pIoCtx->cMetaTransfersPending);
2307
2308 vdIoCtxChildReset(pIoCtx);
2309 cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
2310 RTSgBufAdvance(&pIoCtx->Req.Io.SgBuf, cbPreRead);
2311
2312 /* Check if the write would modify anything in this block. */
2313 if (!RTSgBufCmp(&pIoCtx->Req.Io.SgBuf, &pIoCtxParent->Req.Io.SgBuf, cbThisWrite))
2314 {
2315 RTSGBUF SgBufSrcTmp;
2316
2317 RTSgBufClone(&SgBufSrcTmp, &pIoCtxParent->Req.Io.SgBuf);
2318 RTSgBufAdvance(&SgBufSrcTmp, cbThisWrite);
2319 RTSgBufAdvance(&pIoCtx->Req.Io.SgBuf, cbThisWrite);
2320
2321 if (!cbWriteCopy || !RTSgBufCmp(&pIoCtx->Req.Io.SgBuf, &SgBufSrcTmp, cbWriteCopy))
2322 {
2323 /* Block is completely unchanged, so no need to write anything. */
2324 LogFlowFunc(("Block didn't changed\n"));
2325 ASMAtomicWriteU32(&pIoCtx->Req.Io.cbTransferLeft, 0);
2326 RTSgBufAdvance(&pIoCtxParent->Req.Io.SgBuf, cbThisWrite);
2327 return VINF_VD_ASYNC_IO_FINISHED;
2328 }
2329 }
2330
2331 /* Copy the data to the right place in the buffer. */
2332 RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
2333 RTSgBufAdvance(&pIoCtx->Req.Io.SgBuf, cbPreRead);
2334 vdIoCtxCopy(pIoCtx, pIoCtxParent, cbThisWrite);
2335
2336 /* Handle the data that goes after the write to fill the block. */
2337 if (cbPostRead)
2338 {
2339 /* Now assemble the remaining data. */
2340 if (cbWriteCopy)
2341 {
2342 /*
2343 * The S/G buffer of the parent needs to be cloned because
2344 * it is not allowed to modify the state.
2345 */
2346 RTSGBUF SgBufParentTmp;
2347
2348 RTSgBufClone(&SgBufParentTmp, &pIoCtxParent->Req.Io.SgBuf);
2349 RTSgBufCopy(&pIoCtx->Req.Io.SgBuf, &SgBufParentTmp, cbWriteCopy);
2350 }
2351
2352 /* Zero out the remainder of this block. Will never be visible, as this
2353 * is beyond the limit of the image. */
2354 if (cbFill)
2355 {
2356 RTSgBufAdvance(&pIoCtx->Req.Io.SgBuf, cbReadImage);
2357 vdIoCtxSet(pIoCtx, '\0', cbFill);
2358 }
2359 }
2360
2361 /* Write the full block to the virtual disk. */
2362 RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
2363 pIoCtx->pfnIoCtxTransferNext = vdWriteHelperCommitAsync;
2364
2365 return rc;
2366}
2367
2368static DECLCALLBACK(int) vdWriteHelperOptimizedPreReadAsync(PVDIOCTX pIoCtx)
2369{
2370 int rc = VINF_SUCCESS;
2371
2372 LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
2373
2374 pIoCtx->fFlags |= VDIOCTX_FLAGS_ZERO_FREE_BLOCKS;
2375
2376 if ( pIoCtx->Req.Io.cbTransferLeft
2377 && !pIoCtx->cDataTransfersPending)
2378 rc = vdReadHelperAsync(pIoCtx);
2379
2380 if ( ( RT_SUCCESS(rc)
2381 || (rc == VERR_VD_ASYNC_IO_IN_PROGRESS))
2382 && ( pIoCtx->Req.Io.cbTransferLeft
2383 || pIoCtx->cMetaTransfersPending))
2384 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
2385 else
2386 pIoCtx->pfnIoCtxTransferNext = vdWriteHelperOptimizedCmpAndWriteAsync;
2387
2388 return rc;
2389}
2390
2391/**
2392 * internal: write a complete block (only used for diff images), taking the
2393 * remaining data from parent images. This implementation optimizes out writes
2394 * that do not change the data relative to the state as of the parent images.
2395 * All backends which support differential/growing images support this - async version.
2396 */
2397static DECLCALLBACK(int) vdWriteHelperOptimizedAsync(PVDIOCTX pIoCtx)
2398{
2399 PVDISK pDisk = pIoCtx->pDisk;
2400 uint64_t uOffset = pIoCtx->Type.Child.uOffsetSaved;
2401 size_t cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
2402 size_t cbPreRead = pIoCtx->Type.Child.cbPreRead;
2403 size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
2404 size_t cbWrite = pIoCtx->Type.Child.cbWriteParent;
2405 size_t cbFill = 0;
2406 size_t cbWriteCopy = 0;
2407 size_t cbReadImage = 0;
2408
2409 LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
2410
2411 AssertPtr(pIoCtx->pIoCtxParent);
2412 Assert(!pIoCtx->pIoCtxParent->pIoCtxParent);
2413
2414 if (cbPostRead)
2415 {
2416 /* Figure out how much we cannot read from the image, because
2417 * the last block to write might exceed the nominal size of the
2418 * image for technical reasons. */
2419 if (uOffset + cbThisWrite + cbPostRead > pDisk->cbSize)
2420 cbFill = uOffset + cbThisWrite + cbPostRead - pDisk->cbSize;
2421
2422 /* If we have data to be written, use that instead of reading
2423 * data from the image. */
2424 if (cbWrite > cbThisWrite)
2425 cbWriteCopy = RT_MIN(cbWrite - cbThisWrite, cbPostRead);
2426
2427 /* The rest must be read from the image. */
2428 cbReadImage = cbPostRead - cbWriteCopy - cbFill;
2429 }
2430
2431 pIoCtx->Type.Child.Write.Optimized.cbFill = cbFill;
2432 pIoCtx->Type.Child.Write.Optimized.cbWriteCopy = cbWriteCopy;
2433 pIoCtx->Type.Child.Write.Optimized.cbReadImage = cbReadImage;
2434
2435 /* Read the entire data of the block so that we can compare whether it will
2436 * be modified by the write or not. */
2437 size_t cbTmp = cbPreRead + cbThisWrite + cbPostRead - cbFill; Assert(cbTmp == (uint32_t)cbTmp);
2438 pIoCtx->Req.Io.cbTransferLeft = (uint32_t)cbTmp;
2439 pIoCtx->Req.Io.cbTransfer = pIoCtx->Req.Io.cbTransferLeft;
2440 pIoCtx->Req.Io.uOffset -= cbPreRead;
2441
2442 /* Next step */
2443 pIoCtx->pfnIoCtxTransferNext = vdWriteHelperOptimizedPreReadAsync;
2444 return VINF_SUCCESS;
2445}
2446
2447static DECLCALLBACK(int) vdWriteHelperStandardReadImageAsync(PVDIOCTX pIoCtx)
2448{
2449 int rc = VINF_SUCCESS;
2450
2451 LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
2452
2453 pIoCtx->fFlags |= VDIOCTX_FLAGS_ZERO_FREE_BLOCKS;
2454
2455 if ( pIoCtx->Req.Io.cbTransferLeft
2456 && !pIoCtx->cDataTransfersPending)
2457 rc = vdReadHelperAsync(pIoCtx);
2458
2459 if ( RT_SUCCESS(rc)
2460 && ( pIoCtx->Req.Io.cbTransferLeft
2461 || pIoCtx->cMetaTransfersPending))
2462 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
2463 else
2464 {
2465 size_t cbFill = pIoCtx->Type.Child.Write.Optimized.cbFill;
2466
2467 /* Zero out the remainder of this block. Will never be visible, as this
2468 * is beyond the limit of the image. */
2469 if (cbFill)
2470 vdIoCtxSet(pIoCtx, '\0', cbFill);
2471
2472 /* Write the full block to the virtual disk. */
2473 RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
2474
2475 vdIoCtxChildReset(pIoCtx);
2476 pIoCtx->pfnIoCtxTransferNext = vdWriteHelperCommitAsync;
2477 }
2478
2479 return rc;
2480}
2481
2482static DECLCALLBACK(int) vdWriteHelperStandardAssemble(PVDIOCTX pIoCtx)
2483{
2484 int rc = VINF_SUCCESS;
2485 size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
2486 size_t cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
2487 PVDIOCTX pIoCtxParent = pIoCtx->pIoCtxParent;
2488
2489 LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
2490
2491 vdIoCtxCopy(pIoCtx, pIoCtxParent, cbThisWrite);
2492 if (cbPostRead)
2493 {
2494 size_t cbFill = pIoCtx->Type.Child.Write.Optimized.cbFill;
2495 size_t cbWriteCopy = pIoCtx->Type.Child.Write.Optimized.cbWriteCopy;
2496 size_t cbReadImage = pIoCtx->Type.Child.Write.Optimized.cbReadImage;
2497
2498 /* Now assemble the remaining data. */
2499 if (cbWriteCopy)
2500 {
2501 /*
2502 * The S/G buffer of the parent needs to be cloned because
2503 * it is not allowed to modify the state.
2504 */
2505 RTSGBUF SgBufParentTmp;
2506
2507 RTSgBufClone(&SgBufParentTmp, &pIoCtxParent->Req.Io.SgBuf);
2508 RTSgBufCopy(&pIoCtx->Req.Io.SgBuf, &SgBufParentTmp, cbWriteCopy);
2509 }
2510
2511 if (cbReadImage)
2512 {
2513 /* Read remaining data. */
2514 pIoCtx->pfnIoCtxTransferNext = vdWriteHelperStandardReadImageAsync;
2515
2516 /* Read the data that goes before the write to fill the block. */
2517 pIoCtx->Req.Io.cbTransferLeft = (uint32_t)cbReadImage; Assert(cbReadImage == (uint32_t)cbReadImage);
2518 pIoCtx->Req.Io.cbTransfer = pIoCtx->Req.Io.cbTransferLeft;
2519 pIoCtx->Req.Io.uOffset += cbWriteCopy;
2520 }
2521 else
2522 {
2523 /* Zero out the remainder of this block. Will never be visible, as this
2524 * is beyond the limit of the image. */
2525 if (cbFill)
2526 vdIoCtxSet(pIoCtx, '\0', cbFill);
2527
2528 /* Write the full block to the virtual disk. */
2529 RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
2530 vdIoCtxChildReset(pIoCtx);
2531 pIoCtx->pfnIoCtxTransferNext = vdWriteHelperCommitAsync;
2532 }
2533 }
2534 else
2535 {
2536 /* Write the full block to the virtual disk. */
2537 RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
2538 vdIoCtxChildReset(pIoCtx);
2539 pIoCtx->pfnIoCtxTransferNext = vdWriteHelperCommitAsync;
2540 }
2541
2542 return rc;
2543}
2544
2545static DECLCALLBACK(int) vdWriteHelperStandardPreReadAsync(PVDIOCTX pIoCtx)
2546{
2547 int rc = VINF_SUCCESS;
2548
2549 LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
2550
2551 pIoCtx->fFlags |= VDIOCTX_FLAGS_ZERO_FREE_BLOCKS;
2552
2553 if ( pIoCtx->Req.Io.cbTransferLeft
2554 && !pIoCtx->cDataTransfersPending)
2555 rc = vdReadHelperAsync(pIoCtx);
2556
2557 if ( RT_SUCCESS(rc)
2558 && ( pIoCtx->Req.Io.cbTransferLeft
2559 || pIoCtx->cMetaTransfersPending))
2560 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
2561 else
2562 pIoCtx->pfnIoCtxTransferNext = vdWriteHelperStandardAssemble;
2563
2564 return rc;
2565}
2566
2567static DECLCALLBACK(int) vdWriteHelperStandardAsync(PVDIOCTX pIoCtx)
2568{
2569 PVDISK pDisk = pIoCtx->pDisk;
2570 uint64_t uOffset = pIoCtx->Type.Child.uOffsetSaved;
2571 size_t cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
2572 size_t cbPreRead = pIoCtx->Type.Child.cbPreRead;
2573 size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
2574 size_t cbWrite = pIoCtx->Type.Child.cbWriteParent;
2575 size_t cbFill = 0;
2576 size_t cbWriteCopy = 0;
2577 size_t cbReadImage = 0;
2578
2579 LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
2580
2581 AssertPtr(pIoCtx->pIoCtxParent);
2582 Assert(!pIoCtx->pIoCtxParent->pIoCtxParent);
2583
2584 /* Calculate the amount of data to read that goes after the write to fill the block. */
2585 if (cbPostRead)
2586 {
2587 /* If we have data to be written, use that instead of reading
2588 * data from the image. */
2589 if (cbWrite > cbThisWrite)
2590 cbWriteCopy = RT_MIN(cbWrite - cbThisWrite, cbPostRead);
2591 else
2592 cbWriteCopy = 0;
2593
2594 /* Figure out how much we cannot read from the image, because
2595 * the last block to write might exceed the nominal size of the
2596 * image for technical reasons. */
2597 if (uOffset + cbThisWrite + cbPostRead > pDisk->cbSize)
2598 cbFill = uOffset + cbThisWrite + cbPostRead - pDisk->cbSize;
2599
2600 /* The rest must be read from the image. */
2601 cbReadImage = cbPostRead - cbWriteCopy - cbFill;
2602 }
2603
2604 pIoCtx->Type.Child.Write.Optimized.cbFill = cbFill;
2605 pIoCtx->Type.Child.Write.Optimized.cbWriteCopy = cbWriteCopy;
2606 pIoCtx->Type.Child.Write.Optimized.cbReadImage = cbReadImage;
2607
2608 /* Next step */
2609 if (cbPreRead)
2610 {
2611 pIoCtx->pfnIoCtxTransferNext = vdWriteHelperStandardPreReadAsync;
2612
2613 /* Read the data that goes before the write to fill the block. */
2614 pIoCtx->Req.Io.cbTransferLeft = (uint32_t)cbPreRead; Assert(cbPreRead == (uint32_t)cbPreRead);
2615 pIoCtx->Req.Io.cbTransfer = pIoCtx->Req.Io.cbTransferLeft;
2616 pIoCtx->Req.Io.uOffset -= cbPreRead;
2617 }
2618 else
2619 pIoCtx->pfnIoCtxTransferNext = vdWriteHelperStandardAssemble;
2620
2621 return VINF_SUCCESS;
2622}
2623
2624/**
2625 * internal: write buffer to the image, taking care of block boundaries and
2626 * write optimizations - async version.
2627 */
2628static DECLCALLBACK(int) vdWriteHelperAsync(PVDIOCTX pIoCtx)
2629{
2630 int rc;
2631 size_t cbWrite = pIoCtx->Req.Io.cbTransfer;
2632 uint64_t uOffset = pIoCtx->Req.Io.uOffset;
2633 PVDIMAGE pImage = pIoCtx->Req.Io.pImageCur;
2634 PVDISK pDisk = pIoCtx->pDisk;
2635 unsigned fWrite;
2636 size_t cbThisWrite;
2637 size_t cbPreRead, cbPostRead;
2638
2639 /* Apply write filter chain here if it was not done already. */
2640 if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_WRITE_FILTER_APPLIED))
2641 {
2642 rc = vdFilterChainApplyWrite(pDisk, uOffset, cbWrite, pIoCtx);
2643 if (RT_FAILURE(rc))
2644 return rc;
2645 pIoCtx->fFlags |= VDIOCTX_FLAGS_WRITE_FILTER_APPLIED;
2646 }
2647
2648 if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_DONT_SET_MODIFIED_FLAG))
2649 {
2650 rc = vdSetModifiedFlagAsync(pDisk, pIoCtx);
2651 if (RT_FAILURE(rc)) /* Includes I/O in progress. */
2652 return rc;
2653 }
2654
2655 rc = vdDiscardSetRangeAllocated(pDisk, uOffset, cbWrite);
2656 if (RT_FAILURE(rc))
2657 return rc;
2658
2659 /* Loop until all written. */
2660 do
2661 {
2662 /* Try to write the possibly partial block to the last opened image.
2663 * This works when the block is already allocated in this image or
2664 * if it is a full-block write (and allocation isn't suppressed below).
2665 * For image formats which don't support zero blocks, it's beneficial
2666 * to avoid unnecessarily allocating unchanged blocks. This prevents
2667 * unwanted expanding of images. VMDK is an example. */
2668 cbThisWrite = cbWrite;
2669
2670 /*
2671 * Check whether there is a full block write in progress which was not allocated.
2672 * Defer I/O if the range interferes.
2673 */
2674 if ( pDisk->pIoCtxLockOwner != NIL_VDIOCTX
2675 && uOffset >= pDisk->uOffsetStartLocked
2676 && uOffset < pDisk->uOffsetEndLocked)
2677 {
2678 Log(("Interferring write while allocating a new block => deferring write\n"));
2679 vdIoCtxDefer(pDisk, pIoCtx);
2680 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
2681 break;
2682 }
2683
2684 fWrite = (pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME)
2685 ? 0 : VD_WRITE_NO_ALLOC;
2686 rc = pImage->Backend->pfnWrite(pImage->pBackendData, uOffset, cbThisWrite,
2687 pIoCtx, &cbThisWrite, &cbPreRead, &cbPostRead,
2688 fWrite);
2689 if (rc == VERR_VD_BLOCK_FREE)
2690 {
2691 /* Lock the disk .*/
2692 rc = vdIoCtxLockDisk(pDisk, pIoCtx);
2693 if (RT_SUCCESS(rc))
2694 {
2695 /*
2696 * Allocate segment and buffer in one go.
2697 * A bit hackish but avoids the need to allocate memory twice.
2698 */
2699 PRTSGBUF pTmp = (PRTSGBUF)RTMemAlloc(cbPreRead + cbThisWrite + cbPostRead + sizeof(RTSGSEG) + sizeof(RTSGBUF));
2700 AssertBreakStmt(pTmp, rc = VERR_NO_MEMORY);
2701 PRTSGSEG pSeg = (PRTSGSEG)(pTmp + 1);
2702
2703 pSeg->pvSeg = pSeg + 1;
2704 pSeg->cbSeg = cbPreRead + cbThisWrite + cbPostRead;
2705 RTSgBufInit(pTmp, pSeg, 1);
2706
2707 PVDIOCTX pIoCtxWrite = vdIoCtxChildAlloc(pDisk, VDIOCTXTXDIR_WRITE,
2708 uOffset, pSeg->cbSeg, pImage,
2709 pTmp,
2710 pIoCtx, cbThisWrite,
2711 cbWrite,
2712 pTmp,
2713 (pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME)
2714 ? vdWriteHelperStandardAsync
2715 : vdWriteHelperOptimizedAsync);
2716 if (!VALID_PTR(pIoCtxWrite))
2717 {
2718 RTMemTmpFree(pTmp);
2719 rc = VERR_NO_MEMORY;
2720 break;
2721 }
2722
2723 LogFlowFunc(("Disk is growing because of pIoCtx=%#p pIoCtxWrite=%#p\n",
2724 pIoCtx, pIoCtxWrite));
2725
2726 /* Save the current range for the growing operation to check for intersecting requests later. */
2727 pDisk->uOffsetStartLocked = uOffset - cbPreRead;
2728 pDisk->uOffsetEndLocked = uOffset + cbThisWrite + cbPostRead;
2729
2730 pIoCtxWrite->Type.Child.cbPreRead = cbPreRead;
2731 pIoCtxWrite->Type.Child.cbPostRead = cbPostRead;
2732 pIoCtxWrite->Req.Io.pImageParentOverride = pIoCtx->Req.Io.pImageParentOverride;
2733
2734 /* Process the write request */
2735 rc = vdIoCtxProcessLocked(pIoCtxWrite);
2736
2737 if (RT_FAILURE(rc) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
2738 {
2739 vdIoCtxUnlockDisk(pDisk, pIoCtx, false /* fProcessDeferredReqs*/ );
2740 vdIoCtxFree(pDisk, pIoCtxWrite);
2741 break;
2742 }
2743 else if ( rc == VINF_VD_ASYNC_IO_FINISHED
2744 && ASMAtomicCmpXchgBool(&pIoCtxWrite->fComplete, true, false))
2745 {
2746 LogFlow(("Child write request completed\n"));
2747 Assert(pIoCtx->Req.Io.cbTransferLeft >= cbThisWrite);
2748 Assert(cbThisWrite == (uint32_t)cbThisWrite);
2749 rc = pIoCtxWrite->rcReq;
2750 ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbThisWrite);
2751 vdIoCtxUnlockDisk(pDisk, pIoCtx, false /* fProcessDeferredReqs*/ );
2752 vdIoCtxFree(pDisk, pIoCtxWrite);
2753 }
2754 else
2755 {
2756 LogFlow(("Child write pending\n"));
2757 ASMAtomicIncU32(&pIoCtx->cDataTransfersPending);
2758 pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
2759 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
2760 cbWrite -= cbThisWrite;
2761 uOffset += cbThisWrite;
2762 break;
2763 }
2764 }
2765 else
2766 {
2767 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
2768 break;
2769 }
2770 }
2771
2772 if (rc == VERR_VD_IOCTX_HALT)
2773 {
2774 cbWrite -= cbThisWrite;
2775 uOffset += cbThisWrite;
2776 pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
2777 break;
2778 }
2779 else if (rc == VERR_VD_NOT_ENOUGH_METADATA)
2780 break;
2781
2782 cbWrite -= cbThisWrite;
2783 uOffset += cbThisWrite;
2784 } while (cbWrite != 0 && (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS));
2785
2786 if ( rc == VERR_VD_ASYNC_IO_IN_PROGRESS
2787 || rc == VERR_VD_NOT_ENOUGH_METADATA
2788 || rc == VERR_VD_IOCTX_HALT)
2789 {
2790 /*
2791 * Tell the caller that we don't need to go back here because all
2792 * writes are initiated.
2793 */
2794 if ( !cbWrite
2795 && rc != VERR_VD_IOCTX_HALT)
2796 rc = VINF_SUCCESS;
2797
2798 pIoCtx->Req.Io.uOffset = uOffset;
2799 pIoCtx->Req.Io.cbTransfer = cbWrite;
2800 }
2801
2802 return rc;
2803}
2804
2805/**
2806 * Flush helper async version.
2807 */
2808static DECLCALLBACK(int) vdFlushHelperAsync(PVDIOCTX pIoCtx)
2809{
2810 int rc = VINF_SUCCESS;
2811 PVDISK pDisk = pIoCtx->pDisk;
2812 PVDIMAGE pImage = pIoCtx->Req.Io.pImageCur;
2813
2814 rc = vdIoCtxLockDisk(pDisk, pIoCtx);
2815 if (RT_SUCCESS(rc))
2816 {
2817 /* Mark the whole disk as locked. */
2818 pDisk->uOffsetStartLocked = 0;
2819 pDisk->uOffsetEndLocked = UINT64_C(0xffffffffffffffff);
2820
2821 vdResetModifiedFlag(pDisk);
2822 rc = pImage->Backend->pfnFlush(pImage->pBackendData, pIoCtx);
2823 if ( ( RT_SUCCESS(rc)
2824 || rc == VERR_VD_ASYNC_IO_IN_PROGRESS
2825 || rc == VERR_VD_IOCTX_HALT)
2826 && pDisk->pCache)
2827 {
2828 rc = pDisk->pCache->Backend->pfnFlush(pDisk->pCache->pBackendData, pIoCtx);
2829 if ( RT_SUCCESS(rc)
2830 || ( rc != VERR_VD_ASYNC_IO_IN_PROGRESS
2831 && rc != VERR_VD_IOCTX_HALT))
2832 vdIoCtxUnlockDisk(pDisk, pIoCtx, true /* fProcessBlockedReqs */);
2833 else if (rc != VERR_VD_IOCTX_HALT)
2834 rc = VINF_SUCCESS;
2835 }
2836 else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
2837 rc = VINF_SUCCESS;
2838 else if (rc != VERR_VD_IOCTX_HALT)/* Some other error. */
2839 vdIoCtxUnlockDisk(pDisk, pIoCtx, true /* fProcessBlockedReqs */);
2840 }
2841
2842 return rc;
2843}
2844
2845/**
2846 * Async discard helper - discards a whole block which is recorded in the block
2847 * tree.
2848 *
2849 * @returns VBox status code.
2850 * @param pIoCtx The I/O context to operate on.
2851 */
2852static DECLCALLBACK(int) vdDiscardWholeBlockAsync(PVDIOCTX pIoCtx)
2853{
2854 int rc = VINF_SUCCESS;
2855 PVDISK pDisk = pIoCtx->pDisk;
2856 PVDDISCARDSTATE pDiscard = pDisk->pDiscard;
2857 PVDDISCARDBLOCK pBlock = pIoCtx->Req.Discard.pBlock;
2858 size_t cbPreAllocated, cbPostAllocated, cbActuallyDiscarded;
2859
2860 LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
2861
2862 AssertPtr(pBlock);
2863
2864 rc = pDisk->pLast->Backend->pfnDiscard(pDisk->pLast->pBackendData, pIoCtx,
2865 pBlock->Core.Key, pBlock->cbDiscard,
2866 &cbPreAllocated, &cbPostAllocated,
2867 &cbActuallyDiscarded, NULL, 0);
2868 Assert(rc != VERR_VD_DISCARD_ALIGNMENT_NOT_MET);
2869 Assert(!cbPreAllocated);
2870 Assert(!cbPostAllocated);
2871 Assert(cbActuallyDiscarded == pBlock->cbDiscard || RT_FAILURE(rc));
2872
2873 /* Remove the block on success. */
2874 if ( RT_SUCCESS(rc)
2875 || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
2876 {
2877 PVDDISCARDBLOCK pBlockRemove = (PVDDISCARDBLOCK)RTAvlrU64RangeRemove(pDiscard->pTreeBlocks, pBlock->Core.Key);
2878 Assert(pBlockRemove == pBlock); RT_NOREF1(pBlockRemove);
2879
2880 pDiscard->cbDiscarding -= pBlock->cbDiscard;
2881 RTListNodeRemove(&pBlock->NodeLru);
2882 RTMemFree(pBlock->pbmAllocated);
2883 RTMemFree(pBlock);
2884 pIoCtx->Req.Discard.pBlock = NULL;/* Safety precaution. */
2885 pIoCtx->pfnIoCtxTransferNext = vdDiscardHelperAsync; /* Next part. */
2886 rc = VINF_SUCCESS;
2887 }
2888
2889 LogFlowFunc(("returns rc=%Rrc\n", rc));
2890 return rc;
2891}
2892
2893/**
2894 * Removes the least recently used blocks from the waiting list until
2895 * the new value is reached - version for async I/O.
2896 *
2897 * @returns VBox status code.
2898 * @param pDisk VD disk container.
2899 * @param pIoCtx The I/O context associated with this discard operation.
2900 * @param cbDiscardingNew How many bytes should be waiting on success.
2901 * The number of bytes waiting can be less.
2902 */
2903static int vdDiscardRemoveBlocksAsync(PVDISK pDisk, PVDIOCTX pIoCtx, size_t cbDiscardingNew)
2904{
2905 int rc = VINF_SUCCESS;
2906 PVDDISCARDSTATE pDiscard = pDisk->pDiscard;
2907
2908 LogFlowFunc(("pDisk=%#p pDiscard=%#p cbDiscardingNew=%zu\n",
2909 pDisk, pDiscard, cbDiscardingNew));
2910
2911 while (pDiscard->cbDiscarding > cbDiscardingNew)
2912 {
2913 PVDDISCARDBLOCK pBlock = RTListGetLast(&pDiscard->ListLru, VDDISCARDBLOCK, NodeLru);
2914
2915 Assert(!RTListIsEmpty(&pDiscard->ListLru));
2916
2917 /* Go over the allocation bitmap and mark all discarded sectors as unused. */
2918 uint64_t offStart = pBlock->Core.Key;
2919 uint32_t idxStart = 0;
2920 size_t cbLeft = pBlock->cbDiscard;
2921 bool fAllocated = ASMBitTest(pBlock->pbmAllocated, idxStart);
2922 uint32_t cSectors = (uint32_t)(pBlock->cbDiscard / 512);
2923
2924 while (cbLeft > 0)
2925 {
2926 int32_t idxEnd;
2927 size_t cbThis = cbLeft;
2928
2929 if (fAllocated)
2930 {
2931 /* Check for the first unallocated bit. */
2932 idxEnd = ASMBitNextClear(pBlock->pbmAllocated, cSectors, idxStart);
2933 if (idxEnd != -1)
2934 {
2935 cbThis = (idxEnd - idxStart) * 512;
2936 fAllocated = false;
2937 }
2938 }
2939 else
2940 {
2941 /* Mark as unused and check for the first set bit. */
2942 idxEnd = ASMBitNextSet(pBlock->pbmAllocated, cSectors, idxStart);
2943 if (idxEnd != -1)
2944 cbThis = (idxEnd - idxStart) * 512;
2945
2946 rc = pDisk->pLast->Backend->pfnDiscard(pDisk->pLast->pBackendData, pIoCtx,
2947 offStart, cbThis, NULL, NULL, &cbThis,
2948 NULL, VD_DISCARD_MARK_UNUSED);
2949 if ( RT_FAILURE(rc)
2950 && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
2951 break;
2952
2953 fAllocated = true;
2954 }
2955
2956 idxStart = idxEnd;
2957 offStart += cbThis;
2958 cbLeft -= cbThis;
2959 }
2960
2961 if ( RT_FAILURE(rc)
2962 && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
2963 break;
2964
2965 PVDDISCARDBLOCK pBlockRemove = (PVDDISCARDBLOCK)RTAvlrU64RangeRemove(pDiscard->pTreeBlocks, pBlock->Core.Key);
2966 Assert(pBlockRemove == pBlock); NOREF(pBlockRemove);
2967 RTListNodeRemove(&pBlock->NodeLru);
2968
2969 pDiscard->cbDiscarding -= pBlock->cbDiscard;
2970 RTMemFree(pBlock->pbmAllocated);
2971 RTMemFree(pBlock);
2972 }
2973
2974 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
2975 rc = VINF_SUCCESS;
2976
2977 Assert(RT_FAILURE(rc) || pDiscard->cbDiscarding <= cbDiscardingNew);
2978
2979 LogFlowFunc(("returns rc=%Rrc\n", rc));
2980 return rc;
2981}
2982
2983/**
2984 * Async discard helper - discards the current range if there is no matching
2985 * block in the tree.
2986 *
2987 * @returns VBox status code.
2988 * @param pIoCtx The I/O context to operate on.
2989 */
2990static DECLCALLBACK(int) vdDiscardCurrentRangeAsync(PVDIOCTX pIoCtx)
2991{
2992 PVDISK pDisk = pIoCtx->pDisk;
2993 PVDDISCARDSTATE pDiscard = pDisk->pDiscard;
2994 uint64_t offStart = pIoCtx->Req.Discard.offCur;
2995 size_t cbThisDiscard = pIoCtx->Req.Discard.cbThisDiscard;
2996 void *pbmAllocated = NULL;
2997 size_t cbPreAllocated, cbPostAllocated;
2998 int rc = VINF_SUCCESS;
2999
3000 LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
3001
3002 /* No block found, try to discard using the backend first. */
3003 rc = pDisk->pLast->Backend->pfnDiscard(pDisk->pLast->pBackendData, pIoCtx,
3004 offStart, cbThisDiscard, &cbPreAllocated,
3005 &cbPostAllocated, &cbThisDiscard,
3006 &pbmAllocated, 0);
3007 if (rc == VERR_VD_DISCARD_ALIGNMENT_NOT_MET)
3008 {
3009 /* Create new discard block. */
3010 PVDDISCARDBLOCK pBlock = (PVDDISCARDBLOCK)RTMemAllocZ(sizeof(VDDISCARDBLOCK));
3011 if (pBlock)
3012 {
3013 pBlock->Core.Key = offStart - cbPreAllocated;
3014 pBlock->Core.KeyLast = offStart + cbThisDiscard + cbPostAllocated - 1;
3015 pBlock->cbDiscard = cbPreAllocated + cbThisDiscard + cbPostAllocated;
3016 pBlock->pbmAllocated = pbmAllocated;
3017 bool fInserted = RTAvlrU64Insert(pDiscard->pTreeBlocks, &pBlock->Core);
3018 Assert(fInserted); NOREF(fInserted);
3019
3020 RTListPrepend(&pDiscard->ListLru, &pBlock->NodeLru);
3021 pDiscard->cbDiscarding += pBlock->cbDiscard;
3022
3023 Assert(pIoCtx->Req.Discard.cbDiscardLeft >= cbThisDiscard);
3024 pIoCtx->Req.Discard.cbDiscardLeft -= cbThisDiscard;
3025 pIoCtx->Req.Discard.offCur += cbThisDiscard;
3026 pIoCtx->Req.Discard.cbThisDiscard = cbThisDiscard;
3027
3028 if (pDiscard->cbDiscarding > VD_DISCARD_REMOVE_THRESHOLD)
3029 rc = vdDiscardRemoveBlocksAsync(pDisk, pIoCtx, VD_DISCARD_REMOVE_THRESHOLD);
3030 else
3031 rc = VINF_SUCCESS;
3032
3033 if (RT_SUCCESS(rc))
3034 pIoCtx->pfnIoCtxTransferNext = vdDiscardHelperAsync; /* Next part. */
3035 }
3036 else
3037 {
3038 RTMemFree(pbmAllocated);
3039 rc = VERR_NO_MEMORY;
3040 }
3041 }
3042 else if ( RT_SUCCESS(rc)
3043 || rc == VERR_VD_ASYNC_IO_IN_PROGRESS) /* Save state and andvance to next range. */
3044 {
3045 Assert(pIoCtx->Req.Discard.cbDiscardLeft >= cbThisDiscard);
3046 pIoCtx->Req.Discard.cbDiscardLeft -= cbThisDiscard;
3047 pIoCtx->Req.Discard.offCur += cbThisDiscard;
3048 pIoCtx->Req.Discard.cbThisDiscard = cbThisDiscard;
3049 pIoCtx->pfnIoCtxTransferNext = vdDiscardHelperAsync;
3050 rc = VINF_SUCCESS;
3051 }
3052
3053 LogFlowFunc(("returns rc=%Rrc\n", rc));
3054 return rc;
3055}
3056
3057/**
3058 * Async discard helper - entry point.
3059 *
3060 * @returns VBox status code.
3061 * @param pIoCtx The I/O context to operate on.
3062 */
3063static DECLCALLBACK(int) vdDiscardHelperAsync(PVDIOCTX pIoCtx)
3064{
3065 int rc = VINF_SUCCESS;
3066 PVDISK pDisk = pIoCtx->pDisk;
3067 PCRTRANGE paRanges = pIoCtx->Req.Discard.paRanges;
3068 unsigned cRanges = pIoCtx->Req.Discard.cRanges;
3069 PVDDISCARDSTATE pDiscard = pDisk->pDiscard;
3070
3071 LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
3072
3073 /* Check if the I/O context processed all ranges. */
3074 if ( pIoCtx->Req.Discard.idxRange == cRanges
3075 && !pIoCtx->Req.Discard.cbDiscardLeft)
3076 {
3077 LogFlowFunc(("All ranges discarded, completing\n"));
3078 vdIoCtxUnlockDisk(pDisk, pIoCtx, true /* fProcessDeferredReqs*/);
3079 return VINF_SUCCESS;
3080 }
3081
3082 if (pDisk->pIoCtxLockOwner != pIoCtx)
3083 rc = vdIoCtxLockDisk(pDisk, pIoCtx);
3084
3085 if (RT_SUCCESS(rc))
3086 {
3087 uint64_t offStart = pIoCtx->Req.Discard.offCur;
3088 size_t cbDiscardLeft = pIoCtx->Req.Discard.cbDiscardLeft;
3089 size_t cbThisDiscard;
3090
3091 pDisk->uOffsetStartLocked = offStart;
3092 pDisk->uOffsetEndLocked = offStart + cbDiscardLeft;
3093
3094 if (RT_UNLIKELY(!pDiscard))
3095 {
3096 pDiscard = vdDiscardStateCreate();
3097 if (!pDiscard)
3098 return VERR_NO_MEMORY;
3099
3100 pDisk->pDiscard = pDiscard;
3101 }
3102
3103 if (!pIoCtx->Req.Discard.cbDiscardLeft)
3104 {
3105 offStart = paRanges[pIoCtx->Req.Discard.idxRange].offStart;
3106 cbDiscardLeft = paRanges[pIoCtx->Req.Discard.idxRange].cbRange;
3107 LogFlowFunc(("New range descriptor loaded (%u) offStart=%llu cbDiscard=%zu\n",
3108 pIoCtx->Req.Discard.idxRange, offStart, cbDiscardLeft));
3109 pIoCtx->Req.Discard.idxRange++;
3110 }
3111
3112 /* Look for a matching block in the AVL tree first. */
3113 PVDDISCARDBLOCK pBlock = (PVDDISCARDBLOCK)RTAvlrU64GetBestFit(pDiscard->pTreeBlocks, offStart, false);
3114 if (!pBlock || pBlock->Core.KeyLast < offStart)
3115 {
3116 PVDDISCARDBLOCK pBlockAbove = (PVDDISCARDBLOCK)RTAvlrU64GetBestFit(pDiscard->pTreeBlocks, offStart, true);
3117
3118 /* Clip range to remain in the current block. */
3119 if (pBlockAbove)
3120 cbThisDiscard = RT_MIN(cbDiscardLeft, pBlockAbove->Core.KeyLast - offStart + 1);
3121 else
3122 cbThisDiscard = cbDiscardLeft;
3123
3124 Assert(!(cbThisDiscard % 512));
3125 pIoCtx->Req.Discard.pBlock = NULL;
3126 pIoCtx->pfnIoCtxTransferNext = vdDiscardCurrentRangeAsync;
3127 }
3128 else
3129 {
3130 /* Range lies partly in the block, update allocation bitmap. */
3131 int32_t idxStart, idxEnd;
3132
3133 cbThisDiscard = RT_MIN(cbDiscardLeft, pBlock->Core.KeyLast - offStart + 1);
3134
3135 AssertPtr(pBlock);
3136
3137 Assert(!(cbThisDiscard % 512));
3138 Assert(!((offStart - pBlock->Core.Key) % 512));
3139
3140 idxStart = (offStart - pBlock->Core.Key) / 512;
3141 idxEnd = idxStart + (int32_t)(cbThisDiscard / 512);
3142
3143 ASMBitClearRange(pBlock->pbmAllocated, idxStart, idxEnd);
3144
3145 cbDiscardLeft -= cbThisDiscard;
3146 offStart += cbThisDiscard;
3147
3148 /* Call the backend to discard the block if it is completely unallocated now. */
3149 if (ASMBitFirstSet((volatile void *)pBlock->pbmAllocated, (uint32_t)(pBlock->cbDiscard / 512)) == -1)
3150 {
3151 pIoCtx->Req.Discard.pBlock = pBlock;
3152 pIoCtx->pfnIoCtxTransferNext = vdDiscardWholeBlockAsync;
3153 rc = VINF_SUCCESS;
3154 }
3155 else
3156 {
3157 RTListNodeRemove(&pBlock->NodeLru);
3158 RTListPrepend(&pDiscard->ListLru, &pBlock->NodeLru);
3159
3160 /* Start with next range. */
3161 pIoCtx->pfnIoCtxTransferNext = vdDiscardHelperAsync;
3162 rc = VINF_SUCCESS;
3163 }
3164 }
3165
3166 /* Save state in the context. */
3167 pIoCtx->Req.Discard.offCur = offStart;
3168 pIoCtx->Req.Discard.cbDiscardLeft = cbDiscardLeft;
3169 pIoCtx->Req.Discard.cbThisDiscard = cbThisDiscard;
3170 }
3171
3172 LogFlowFunc(("returns rc=%Rrc\n", rc));
3173 return rc;
3174}
3175
3176/**
3177 * VD async I/O interface open callback.
3178 */
3179static DECLCALLBACK(int) vdIOOpenFallback(void *pvUser, const char *pszLocation,
3180 uint32_t fOpen, PFNVDCOMPLETED pfnCompleted,
3181 void **ppStorage)
3182{
3183 RT_NOREF1(pvUser);
3184 PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)RTMemAllocZ(sizeof(VDIIOFALLBACKSTORAGE));
3185
3186 if (!pStorage)
3187 return VERR_NO_MEMORY;
3188
3189 pStorage->pfnCompleted = pfnCompleted;
3190
3191 /* Open the file. */
3192 int rc = RTFileOpen(&pStorage->File, pszLocation, fOpen);
3193 if (RT_SUCCESS(rc))
3194 {
3195 *ppStorage = pStorage;
3196 return VINF_SUCCESS;
3197 }
3198
3199 RTMemFree(pStorage);
3200 return rc;
3201}
3202
3203/**
3204 * VD async I/O interface close callback.
3205 */
3206static DECLCALLBACK(int) vdIOCloseFallback(void *pvUser, void *pvStorage)
3207{
3208 RT_NOREF1(pvUser);
3209 PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
3210
3211 RTFileClose(pStorage->File);
3212 RTMemFree(pStorage);
3213 return VINF_SUCCESS;
3214}
3215
3216static DECLCALLBACK(int) vdIODeleteFallback(void *pvUser, const char *pcszFilename)
3217{
3218 RT_NOREF1(pvUser);
3219 return RTFileDelete(pcszFilename);
3220}
3221
3222static DECLCALLBACK(int) vdIOMoveFallback(void *pvUser, const char *pcszSrc, const char *pcszDst, unsigned fMove)
3223{
3224 RT_NOREF1(pvUser);
3225 return RTFileMove(pcszSrc, pcszDst, fMove);
3226}
3227
3228static DECLCALLBACK(int) vdIOGetFreeSpaceFallback(void *pvUser, const char *pcszFilename, int64_t *pcbFreeSpace)
3229{
3230 RT_NOREF1(pvUser);
3231 return RTFsQuerySizes(pcszFilename, NULL, pcbFreeSpace, NULL, NULL);
3232}
3233
3234static DECLCALLBACK(int) vdIOGetModificationTimeFallback(void *pvUser, const char *pcszFilename, PRTTIMESPEC pModificationTime)
3235{
3236 RT_NOREF1(pvUser);
3237 RTFSOBJINFO info;
3238 int rc = RTPathQueryInfo(pcszFilename, &info, RTFSOBJATTRADD_NOTHING);
3239 if (RT_SUCCESS(rc))
3240 *pModificationTime = info.ModificationTime;
3241 return rc;
3242}
3243
3244/**
3245 * VD async I/O interface callback for retrieving the file size.
3246 */
3247static DECLCALLBACK(int) vdIOGetSizeFallback(void *pvUser, void *pvStorage, uint64_t *pcbSize)
3248{
3249 RT_NOREF1(pvUser);
3250 PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
3251
3252 return RTFileQuerySize(pStorage->File, pcbSize);
3253}
3254
3255/**
3256 * VD async I/O interface callback for setting the file size.
3257 */
3258static DECLCALLBACK(int) vdIOSetSizeFallback(void *pvUser, void *pvStorage, uint64_t cbSize)
3259{
3260 RT_NOREF1(pvUser);
3261 PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
3262
3263 return RTFileSetSize(pStorage->File, cbSize);
3264}
3265
3266/**
3267 * VD async I/O interface callback for setting the file allocation size.
3268 */
3269static DECLCALLBACK(int) vdIOSetAllocationSizeFallback(void *pvUser, void *pvStorage, uint64_t cbSize,
3270 uint32_t fFlags)
3271{
3272 RT_NOREF2(pvUser, fFlags);
3273 PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
3274
3275 return RTFileSetAllocationSize(pStorage->File, cbSize, RTFILE_ALLOC_SIZE_F_DEFAULT);
3276}
3277
3278/**
3279 * VD async I/O interface callback for a synchronous write to the file.
3280 */
3281static DECLCALLBACK(int) vdIOWriteSyncFallback(void *pvUser, void *pvStorage, uint64_t uOffset,
3282 const void *pvBuf, size_t cbWrite, size_t *pcbWritten)
3283{
3284 RT_NOREF1(pvUser);
3285 PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
3286
3287 return RTFileWriteAt(pStorage->File, uOffset, pvBuf, cbWrite, pcbWritten);
3288}
3289
3290/**
3291 * VD async I/O interface callback for a synchronous read from the file.
3292 */
3293static DECLCALLBACK(int) vdIOReadSyncFallback(void *pvUser, void *pvStorage, uint64_t uOffset,
3294 void *pvBuf, size_t cbRead, size_t *pcbRead)
3295{
3296 RT_NOREF1(pvUser);
3297 PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
3298
3299 return RTFileReadAt(pStorage->File, uOffset, pvBuf, cbRead, pcbRead);
3300}
3301
3302/**
3303 * VD async I/O interface callback for a synchronous flush of the file data.
3304 */
3305static DECLCALLBACK(int) vdIOFlushSyncFallback(void *pvUser, void *pvStorage)
3306{
3307 RT_NOREF1(pvUser);
3308 PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
3309
3310 return RTFileFlush(pStorage->File);
3311}
3312
3313/**
3314 * VD async I/O interface callback for a asynchronous read from the file.
3315 */
3316static DECLCALLBACK(int) vdIOReadAsyncFallback(void *pvUser, void *pStorage, uint64_t uOffset,
3317 PCRTSGSEG paSegments, size_t cSegments,
3318 size_t cbRead, void *pvCompletion,
3319 void **ppTask)
3320{
3321 RT_NOREF8(pvUser, pStorage, uOffset, paSegments, cSegments, cbRead, pvCompletion, ppTask);
3322 AssertFailed();
3323 return VERR_NOT_IMPLEMENTED;
3324}
3325
3326/**
3327 * VD async I/O interface callback for a asynchronous write to the file.
3328 */
3329static DECLCALLBACK(int) vdIOWriteAsyncFallback(void *pvUser, void *pStorage, uint64_t uOffset,
3330 PCRTSGSEG paSegments, size_t cSegments,
3331 size_t cbWrite, void *pvCompletion,
3332 void **ppTask)
3333{
3334 RT_NOREF8(pvUser, pStorage, uOffset, paSegments, cSegments, cbWrite, pvCompletion, ppTask);
3335 AssertFailed();
3336 return VERR_NOT_IMPLEMENTED;
3337}
3338
3339/**
3340 * VD async I/O interface callback for a asynchronous flush of the file data.
3341 */
3342static DECLCALLBACK(int) vdIOFlushAsyncFallback(void *pvUser, void *pStorage,
3343 void *pvCompletion, void **ppTask)
3344{
3345 RT_NOREF4(pvUser, pStorage, pvCompletion, ppTask);
3346 AssertFailed();
3347 return VERR_NOT_IMPLEMENTED;
3348}
3349
3350/**
3351 * Internal - Continues an I/O context after
3352 * it was halted because of an active transfer.
3353 */
3354static int vdIoCtxContinue(PVDIOCTX pIoCtx, int rcReq)
3355{
3356 PVDISK pDisk = pIoCtx->pDisk;
3357 int rc = VINF_SUCCESS;
3358
3359 VD_IS_LOCKED(pDisk);
3360
3361 if (RT_FAILURE(rcReq))
3362 ASMAtomicCmpXchgS32(&pIoCtx->rcReq, rcReq, VINF_SUCCESS);
3363
3364 if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_BLOCKED))
3365 {
3366 /* Continue the transfer */
3367 rc = vdIoCtxProcessLocked(pIoCtx);
3368
3369 if ( rc == VINF_VD_ASYNC_IO_FINISHED
3370 && ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
3371 {
3372 LogFlowFunc(("I/O context completed pIoCtx=%#p\n", pIoCtx));
3373 bool fFreeCtx = RT_BOOL(!(pIoCtx->fFlags & VDIOCTX_FLAGS_DONT_FREE));
3374 if (pIoCtx->pIoCtxParent)
3375 {
3376 PVDIOCTX pIoCtxParent = pIoCtx->pIoCtxParent;
3377
3378 Assert(!pIoCtxParent->pIoCtxParent);
3379 if (RT_FAILURE(pIoCtx->rcReq))
3380 ASMAtomicCmpXchgS32(&pIoCtxParent->rcReq, pIoCtx->rcReq, VINF_SUCCESS);
3381
3382 ASMAtomicDecU32(&pIoCtxParent->cDataTransfersPending);
3383
3384 if (pIoCtx->enmTxDir == VDIOCTXTXDIR_WRITE)
3385 {
3386 LogFlowFunc(("I/O context transferred %u bytes for the parent pIoCtxParent=%p\n",
3387 pIoCtx->Type.Child.cbTransferParent, pIoCtxParent));
3388
3389 /* Update the parent state. */
3390 Assert(pIoCtxParent->Req.Io.cbTransferLeft >= pIoCtx->Type.Child.cbTransferParent);
3391 ASMAtomicSubU32(&pIoCtxParent->Req.Io.cbTransferLeft, (uint32_t)pIoCtx->Type.Child.cbTransferParent);
3392 }
3393 else
3394 Assert(pIoCtx->enmTxDir == VDIOCTXTXDIR_FLUSH);
3395
3396 /*
3397 * A completed child write means that we finished growing the image.
3398 * We have to process any pending writes now.
3399 */
3400 vdIoCtxUnlockDisk(pDisk, pIoCtxParent, false /* fProcessDeferredReqs */);
3401
3402 /* Unblock the parent */
3403 pIoCtxParent->fFlags &= ~VDIOCTX_FLAGS_BLOCKED;
3404
3405 rc = vdIoCtxProcessLocked(pIoCtxParent);
3406
3407 if ( rc == VINF_VD_ASYNC_IO_FINISHED
3408 && ASMAtomicCmpXchgBool(&pIoCtxParent->fComplete, true, false))
3409 {
3410 LogFlowFunc(("Parent I/O context completed pIoCtxParent=%#p rcReq=%Rrc\n", pIoCtxParent, pIoCtxParent->rcReq));
3411 bool fFreeParentCtx = RT_BOOL(!(pIoCtxParent->fFlags & VDIOCTX_FLAGS_DONT_FREE));
3412 vdIoCtxRootComplete(pDisk, pIoCtxParent);
3413 vdThreadFinishWrite(pDisk);
3414
3415 if (fFreeParentCtx)
3416 vdIoCtxFree(pDisk, pIoCtxParent);
3417 vdDiskProcessBlockedIoCtx(pDisk);
3418 }
3419 else if (!vdIoCtxIsDiskLockOwner(pDisk, pIoCtx))
3420 {
3421 /* Process any pending writes if the current request didn't caused another growing. */
3422 vdDiskProcessBlockedIoCtx(pDisk);
3423 }
3424 }
3425 else
3426 {
3427 if (pIoCtx->enmTxDir == VDIOCTXTXDIR_FLUSH)
3428 {
3429 vdIoCtxUnlockDisk(pDisk, pIoCtx, true /* fProcessDerredReqs */);
3430 vdThreadFinishWrite(pDisk);
3431 }
3432 else if ( pIoCtx->enmTxDir == VDIOCTXTXDIR_WRITE
3433 || pIoCtx->enmTxDir == VDIOCTXTXDIR_DISCARD)
3434 vdThreadFinishWrite(pDisk);
3435 else
3436 {
3437 Assert(pIoCtx->enmTxDir == VDIOCTXTXDIR_READ);
3438 vdThreadFinishRead(pDisk);
3439 }
3440
3441 LogFlowFunc(("I/O context completed pIoCtx=%#p rcReq=%Rrc\n", pIoCtx, pIoCtx->rcReq));
3442 vdIoCtxRootComplete(pDisk, pIoCtx);
3443 }
3444
3445 if (fFreeCtx)
3446 vdIoCtxFree(pDisk, pIoCtx);
3447 }
3448 }
3449
3450 return VINF_SUCCESS;
3451}
3452
3453/**
3454 * Internal - Called when user transfer completed.
3455 */
3456static int vdUserXferCompleted(PVDIOSTORAGE pIoStorage, PVDIOCTX pIoCtx,
3457 PFNVDXFERCOMPLETED pfnComplete, void *pvUser,
3458 size_t cbTransfer, int rcReq)
3459{
3460 int rc = VINF_SUCCESS;
3461 PVDISK pDisk = pIoCtx->pDisk;
3462
3463 LogFlowFunc(("pIoStorage=%#p pIoCtx=%#p pfnComplete=%#p pvUser=%#p cbTransfer=%zu rcReq=%Rrc\n",
3464 pIoStorage, pIoCtx, pfnComplete, pvUser, cbTransfer, rcReq));
3465
3466 VD_IS_LOCKED(pDisk);
3467
3468 Assert(pIoCtx->Req.Io.cbTransferLeft >= cbTransfer);
3469 ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbTransfer); Assert(cbTransfer == (uint32_t)cbTransfer);
3470 ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
3471
3472 if (pfnComplete)
3473 rc = pfnComplete(pIoStorage->pVDIo->pBackendData, pIoCtx, pvUser, rcReq);
3474
3475 if (RT_SUCCESS(rc))
3476 rc = vdIoCtxContinue(pIoCtx, rcReq);
3477 else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
3478 rc = VINF_SUCCESS;
3479
3480 return rc;
3481}
3482
3483static void vdIoCtxContinueDeferredList(PVDIOSTORAGE pIoStorage, PRTLISTANCHOR pListWaiting,
3484 PFNVDXFERCOMPLETED pfnComplete, void *pvUser, int rcReq)
3485{
3486 LogFlowFunc(("pIoStorage=%#p pListWaiting=%#p pfnComplete=%#p pvUser=%#p rcReq=%Rrc\n",
3487 pIoStorage, pListWaiting, pfnComplete, pvUser, rcReq));
3488
3489 /* Go through the waiting list and continue the I/O contexts. */
3490 while (!RTListIsEmpty(pListWaiting))
3491 {
3492 int rc = VINF_SUCCESS;
3493 PVDIOCTXDEFERRED pDeferred = RTListGetFirst(pListWaiting, VDIOCTXDEFERRED, NodeDeferred);
3494 PVDIOCTX pIoCtx = pDeferred->pIoCtx;
3495 RTListNodeRemove(&pDeferred->NodeDeferred);
3496
3497 RTMemFree(pDeferred);
3498 ASMAtomicDecU32(&pIoCtx->cMetaTransfersPending);
3499
3500 if (pfnComplete)
3501 rc = pfnComplete(pIoStorage->pVDIo->pBackendData, pIoCtx, pvUser, rcReq);
3502
3503 LogFlow(("Completion callback for I/O context %#p returned %Rrc\n", pIoCtx, rc));
3504
3505 if (RT_SUCCESS(rc))
3506 {
3507 rc = vdIoCtxContinue(pIoCtx, rcReq);
3508 AssertRC(rc);
3509 }
3510 else
3511 Assert(rc == VERR_VD_ASYNC_IO_IN_PROGRESS);
3512 }
3513}
3514
3515/**
3516 * Internal - Called when a meta transfer completed.
3517 */
3518static int vdMetaXferCompleted(PVDIOSTORAGE pIoStorage, PFNVDXFERCOMPLETED pfnComplete, void *pvUser,
3519 PVDMETAXFER pMetaXfer, int rcReq)
3520{
3521 PVDISK pDisk = pIoStorage->pVDIo->pDisk;
3522 RTLISTANCHOR ListIoCtxWaiting;
3523 bool fFlush;
3524
3525 LogFlowFunc(("pIoStorage=%#p pfnComplete=%#p pvUser=%#p pMetaXfer=%#p rcReq=%Rrc\n",
3526 pIoStorage, pfnComplete, pvUser, pMetaXfer, rcReq));
3527
3528 VD_IS_LOCKED(pDisk);
3529
3530 fFlush = VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_FLUSH;
3531
3532 if (!fFlush)
3533 {
3534 RTListMove(&ListIoCtxWaiting, &pMetaXfer->ListIoCtxWaiting);
3535
3536 if (RT_FAILURE(rcReq))
3537 {
3538 /* Remove from the AVL tree. */
3539 LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
3540 bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
3541 Assert(fRemoved); NOREF(fRemoved);
3542 /* If this was a write check if there is a shadow buffer with updated data. */
3543 if (pMetaXfer->pbDataShw)
3544 {
3545 Assert(VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_WRITE);
3546 Assert(!RTListIsEmpty(&pMetaXfer->ListIoCtxShwWrites));
3547 RTListConcatenate(&ListIoCtxWaiting, &pMetaXfer->ListIoCtxShwWrites);
3548 RTMemFree(pMetaXfer->pbDataShw);
3549 pMetaXfer->pbDataShw = NULL;
3550 }
3551 RTMemFree(pMetaXfer);
3552 }
3553 else
3554 {
3555 /* Increase the reference counter to make sure it doesn't go away before the last context is processed. */
3556 pMetaXfer->cRefs++;
3557 }
3558 }
3559 else
3560 RTListMove(&ListIoCtxWaiting, &pMetaXfer->ListIoCtxWaiting);
3561
3562 VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
3563 vdIoCtxContinueDeferredList(pIoStorage, &ListIoCtxWaiting, pfnComplete, pvUser, rcReq);
3564
3565 /*
3566 * If there is a shadow buffer and the previous write was successful update with the
3567 * new data and trigger a new write.
3568 */
3569 if ( pMetaXfer->pbDataShw
3570 && RT_SUCCESS(rcReq)
3571 && VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE)
3572 {
3573 LogFlowFunc(("pMetaXfer=%#p Updating from shadow buffer and triggering new write\n", pMetaXfer));
3574 memcpy(pMetaXfer->abData, pMetaXfer->pbDataShw, pMetaXfer->cbMeta);
3575 RTMemFree(pMetaXfer->pbDataShw);
3576 pMetaXfer->pbDataShw = NULL;
3577 Assert(!RTListIsEmpty(&pMetaXfer->ListIoCtxShwWrites));
3578
3579 /* Setup a new I/O write. */
3580 PVDIOTASK pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvUser, pMetaXfer);
3581 if (RT_LIKELY(pIoTask))
3582 {
3583 void *pvTask = NULL;
3584 RTSGSEG Seg;
3585
3586 Seg.cbSeg = pMetaXfer->cbMeta;
3587 Seg.pvSeg = pMetaXfer->abData;
3588
3589 VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_WRITE);
3590 rcReq = pIoStorage->pVDIo->pInterfaceIo->pfnWriteAsync(pIoStorage->pVDIo->pInterfaceIo->Core.pvUser,
3591 pIoStorage->pStorage,
3592 pMetaXfer->Core.Key, &Seg, 1,
3593 pMetaXfer->cbMeta, pIoTask,
3594 &pvTask);
3595 if ( RT_SUCCESS(rcReq)
3596 || rcReq != VERR_VD_ASYNC_IO_IN_PROGRESS)
3597 {
3598 VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
3599 vdIoTaskFree(pDisk, pIoTask);
3600 }
3601 else
3602 RTListMove(&pMetaXfer->ListIoCtxWaiting, &pMetaXfer->ListIoCtxShwWrites);
3603 }
3604 else
3605 rcReq = VERR_NO_MEMORY;
3606
3607 /* Cleanup if there was an error or the request completed already. */
3608 if (rcReq != VERR_VD_ASYNC_IO_IN_PROGRESS)
3609 vdIoCtxContinueDeferredList(pIoStorage, &pMetaXfer->ListIoCtxShwWrites, pfnComplete, pvUser, rcReq);
3610 }
3611
3612 /* Remove if not used anymore. */
3613 if (!fFlush)
3614 {
3615 pMetaXfer->cRefs--;
3616 if (!pMetaXfer->cRefs && RTListIsEmpty(&pMetaXfer->ListIoCtxWaiting))
3617 {
3618 /* Remove from the AVL tree. */
3619 LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
3620 bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
3621 Assert(fRemoved); NOREF(fRemoved);
3622 RTMemFree(pMetaXfer);
3623 }
3624 }
3625 else if (fFlush)
3626 RTMemFree(pMetaXfer);
3627
3628 return VINF_SUCCESS;
3629}
3630
3631/**
3632 * Processes a list of waiting I/O tasks. The disk lock must be held by caller.
3633 *
3634 * @returns nothing.
3635 * @param pDisk The disk to process the list for.
3636 */
3637static void vdIoTaskProcessWaitingList(PVDISK pDisk)
3638{
3639 LogFlowFunc(("pDisk=%#p\n", pDisk));
3640
3641 VD_IS_LOCKED(pDisk);
3642
3643 PVDIOTASK pHead = ASMAtomicXchgPtrT(&pDisk->pIoTasksPendingHead, NULL, PVDIOTASK);
3644
3645 Log(("I/O task list cleared\n"));
3646
3647 /* Reverse order. */
3648 PVDIOTASK pCur = pHead;
3649 pHead = NULL;
3650 while (pCur)
3651 {
3652 PVDIOTASK pInsert = pCur;
3653 pCur = pCur->pNext;
3654 pInsert->pNext = pHead;
3655 pHead = pInsert;
3656 }
3657
3658 while (pHead)
3659 {
3660 PVDIOSTORAGE pIoStorage = pHead->pIoStorage;
3661
3662 if (!pHead->fMeta)
3663 vdUserXferCompleted(pIoStorage, pHead->Type.User.pIoCtx,
3664 pHead->pfnComplete, pHead->pvUser,
3665 pHead->Type.User.cbTransfer, pHead->rcReq);
3666 else
3667 vdMetaXferCompleted(pIoStorage, pHead->pfnComplete, pHead->pvUser,
3668 pHead->Type.Meta.pMetaXfer, pHead->rcReq);
3669
3670 pCur = pHead;
3671 pHead = pHead->pNext;
3672 vdIoTaskFree(pDisk, pCur);
3673 }
3674}
3675
3676/**
3677 * Process any I/O context on the halted list.
3678 *
3679 * @returns nothing.
3680 * @param pDisk The disk.
3681 */
3682static void vdIoCtxProcessHaltedList(PVDISK pDisk)
3683{
3684 LogFlowFunc(("pDisk=%#p\n", pDisk));
3685
3686 VD_IS_LOCKED(pDisk);
3687
3688 /* Get the waiting list and process it in FIFO order. */
3689 PVDIOCTX pIoCtxHead = ASMAtomicXchgPtrT(&pDisk->pIoCtxHaltedHead, NULL, PVDIOCTX);
3690
3691 /* Reverse it. */
3692 PVDIOCTX pCur = pIoCtxHead;
3693 pIoCtxHead = NULL;
3694 while (pCur)
3695 {
3696 PVDIOCTX pInsert = pCur;
3697 pCur = pCur->pIoCtxNext;
3698 pInsert->pIoCtxNext = pIoCtxHead;
3699 pIoCtxHead = pInsert;
3700 }
3701
3702 /* Process now. */
3703 pCur = pIoCtxHead;
3704 while (pCur)
3705 {
3706 PVDIOCTX pTmp = pCur;
3707
3708 pCur = pCur->pIoCtxNext;
3709 pTmp->pIoCtxNext = NULL;
3710
3711 /* Continue */
3712 pTmp->fFlags &= ~VDIOCTX_FLAGS_BLOCKED;
3713 vdIoCtxContinue(pTmp, pTmp->rcReq);
3714 }
3715}
3716
3717/**
3718 * Unlock the disk and process pending tasks.
3719 *
3720 * @returns VBox status code.
3721 * @param pDisk The disk to unlock.
3722 * @param pIoCtxRc The I/O context to get the status code from, optional.
3723 */
3724static int vdDiskUnlock(PVDISK pDisk, PVDIOCTX pIoCtxRc)
3725{
3726 int rc = VINF_SUCCESS;
3727
3728 VD_IS_LOCKED(pDisk);
3729
3730 /*
3731 * Process the list of waiting I/O tasks first
3732 * because they might complete I/O contexts.
3733 * Same for the list of halted I/O contexts.
3734 * Afterwards comes the list of new I/O contexts.
3735 */
3736 vdIoTaskProcessWaitingList(pDisk);
3737 vdIoCtxProcessHaltedList(pDisk);
3738 rc = vdDiskProcessWaitingIoCtx(pDisk, pIoCtxRc);
3739 ASMAtomicXchgBool(&pDisk->fLocked, false);
3740
3741 /*
3742 * Need to check for new I/O tasks and waiting I/O contexts now
3743 * again as other threads might added them while we processed
3744 * previous lists.
3745 */
3746 while ( ASMAtomicUoReadPtrT(&pDisk->pIoCtxHead, PVDIOCTX) != NULL
3747 || ASMAtomicUoReadPtrT(&pDisk->pIoTasksPendingHead, PVDIOTASK) != NULL
3748 || ASMAtomicUoReadPtrT(&pDisk->pIoCtxHaltedHead, PVDIOCTX) != NULL)
3749 {
3750 /* Try lock disk again. */
3751 if (ASMAtomicCmpXchgBool(&pDisk->fLocked, true, false))
3752 {
3753 vdIoTaskProcessWaitingList(pDisk);
3754 vdIoCtxProcessHaltedList(pDisk);
3755 vdDiskProcessWaitingIoCtx(pDisk, NULL);
3756 ASMAtomicXchgBool(&pDisk->fLocked, false);
3757 }
3758 else /* Let the other thread everything when he unlocks the disk. */
3759 break;
3760 }
3761
3762 return rc;
3763}
3764
3765/**
3766 * Try to lock the disk to complete pressing of the I/O task.
3767 * The completion is deferred if the disk is locked already.
3768 *
3769 * @returns nothing.
3770 * @param pIoTask The I/O task to complete.
3771 */
3772static void vdXferTryLockDiskDeferIoTask(PVDIOTASK pIoTask)
3773{
3774 PVDIOSTORAGE pIoStorage = pIoTask->pIoStorage;
3775 PVDISK pDisk = pIoStorage->pVDIo->pDisk;
3776
3777 Log(("Deferring I/O task pIoTask=%p\n", pIoTask));
3778
3779 /* Put it on the waiting list. */
3780 PVDIOTASK pNext = ASMAtomicUoReadPtrT(&pDisk->pIoTasksPendingHead, PVDIOTASK);
3781 PVDIOTASK pHeadOld;
3782 pIoTask->pNext = pNext;
3783 while (!ASMAtomicCmpXchgExPtr(&pDisk->pIoTasksPendingHead, pIoTask, pNext, &pHeadOld))
3784 {
3785 pNext = pHeadOld;
3786 Assert(pNext != pIoTask);
3787 pIoTask->pNext = pNext;
3788 ASMNopPause();
3789 }
3790
3791 if (ASMAtomicCmpXchgBool(&pDisk->fLocked, true, false))
3792 {
3793 /* Release disk lock, it will take care of processing all lists. */
3794 vdDiskUnlock(pDisk, NULL);
3795 }
3796}
3797
3798static DECLCALLBACK(int) vdIOIntReqCompleted(void *pvUser, int rcReq)
3799{
3800 PVDIOTASK pIoTask = (PVDIOTASK)pvUser;
3801
3802 LogFlowFunc(("Task completed pIoTask=%#p\n", pIoTask));
3803
3804 pIoTask->rcReq = rcReq;
3805 vdXferTryLockDiskDeferIoTask(pIoTask);
3806 return VINF_SUCCESS;
3807}
3808
3809/**
3810 * VD I/O interface callback for opening a file.
3811 */
3812static DECLCALLBACK(int) vdIOIntOpen(void *pvUser, const char *pszLocation,
3813 unsigned uOpenFlags, PPVDIOSTORAGE ppIoStorage)
3814{
3815 int rc = VINF_SUCCESS;
3816 PVDIO pVDIo = (PVDIO)pvUser;
3817 PVDIOSTORAGE pIoStorage = (PVDIOSTORAGE)RTMemAllocZ(sizeof(VDIOSTORAGE));
3818
3819 if (!pIoStorage)
3820 return VERR_NO_MEMORY;
3821
3822 /* Create the AVl tree. */
3823 pIoStorage->pTreeMetaXfers = (PAVLRFOFFTREE)RTMemAllocZ(sizeof(AVLRFOFFTREE));
3824 if (pIoStorage->pTreeMetaXfers)
3825 {
3826 rc = pVDIo->pInterfaceIo->pfnOpen(pVDIo->pInterfaceIo->Core.pvUser,
3827 pszLocation, uOpenFlags,
3828 vdIOIntReqCompleted,
3829 &pIoStorage->pStorage);
3830 if (RT_SUCCESS(rc))
3831 {
3832 pIoStorage->pVDIo = pVDIo;
3833 *ppIoStorage = pIoStorage;
3834 return VINF_SUCCESS;
3835 }
3836
3837 RTMemFree(pIoStorage->pTreeMetaXfers);
3838 }
3839 else
3840 rc = VERR_NO_MEMORY;
3841
3842 RTMemFree(pIoStorage);
3843 return rc;
3844}
3845
3846static DECLCALLBACK(int) vdIOIntTreeMetaXferDestroy(PAVLRFOFFNODECORE pNode, void *pvUser)
3847{
3848 RT_NOREF2(pNode, pvUser);
3849 AssertMsgFailed(("Tree should be empty at this point!\n"));
3850 return VINF_SUCCESS;
3851}
3852
3853static DECLCALLBACK(int) vdIOIntClose(void *pvUser, PVDIOSTORAGE pIoStorage)
3854{
3855 int rc = VINF_SUCCESS;
3856 PVDIO pVDIo = (PVDIO)pvUser;
3857
3858 /* We free everything here, even if closing the file failed for some reason. */
3859 rc = pVDIo->pInterfaceIo->pfnClose(pVDIo->pInterfaceIo->Core.pvUser, pIoStorage->pStorage);
3860 RTAvlrFileOffsetDestroy(pIoStorage->pTreeMetaXfers, vdIOIntTreeMetaXferDestroy, NULL);
3861 RTMemFree(pIoStorage->pTreeMetaXfers);
3862 RTMemFree(pIoStorage);
3863 return rc;
3864}
3865
3866static DECLCALLBACK(int) vdIOIntDelete(void *pvUser, const char *pcszFilename)
3867{
3868 PVDIO pVDIo = (PVDIO)pvUser;
3869 return pVDIo->pInterfaceIo->pfnDelete(pVDIo->pInterfaceIo->Core.pvUser,
3870 pcszFilename);
3871}
3872
3873static DECLCALLBACK(int) vdIOIntMove(void *pvUser, const char *pcszSrc, const char *pcszDst,
3874 unsigned fMove)
3875{
3876 PVDIO pVDIo = (PVDIO)pvUser;
3877 return pVDIo->pInterfaceIo->pfnMove(pVDIo->pInterfaceIo->Core.pvUser,
3878 pcszSrc, pcszDst, fMove);
3879}
3880
3881static DECLCALLBACK(int) vdIOIntGetFreeSpace(void *pvUser, const char *pcszFilename,
3882 int64_t *pcbFreeSpace)
3883{
3884 PVDIO pVDIo = (PVDIO)pvUser;
3885 return pVDIo->pInterfaceIo->pfnGetFreeSpace(pVDIo->pInterfaceIo->Core.pvUser,
3886 pcszFilename, pcbFreeSpace);
3887}
3888
3889static DECLCALLBACK(int) vdIOIntGetModificationTime(void *pvUser, const char *pcszFilename,
3890 PRTTIMESPEC pModificationTime)
3891{
3892 PVDIO pVDIo = (PVDIO)pvUser;
3893 return pVDIo->pInterfaceIo->pfnGetModificationTime(pVDIo->pInterfaceIo->Core.pvUser,
3894 pcszFilename, pModificationTime);
3895}
3896
3897static DECLCALLBACK(int) vdIOIntGetSize(void *pvUser, PVDIOSTORAGE pIoStorage,
3898 uint64_t *pcbSize)
3899{
3900 PVDIO pVDIo = (PVDIO)pvUser;
3901 return pVDIo->pInterfaceIo->pfnGetSize(pVDIo->pInterfaceIo->Core.pvUser,
3902 pIoStorage->pStorage, pcbSize);
3903}
3904
3905static DECLCALLBACK(int) vdIOIntSetSize(void *pvUser, PVDIOSTORAGE pIoStorage,
3906 uint64_t cbSize)
3907{
3908 PVDIO pVDIo = (PVDIO)pvUser;
3909 return pVDIo->pInterfaceIo->pfnSetSize(pVDIo->pInterfaceIo->Core.pvUser,
3910 pIoStorage->pStorage, cbSize);
3911}
3912
3913static DECLCALLBACK(int) vdIOIntSetAllocationSize(void *pvUser, PVDIOSTORAGE pIoStorage,
3914 uint64_t cbSize, uint32_t fFlags,
3915 PVDINTERFACEPROGRESS pIfProgress,
3916 unsigned uPercentStart, unsigned uPercentSpan)
3917{
3918 PVDIO pVDIo = (PVDIO)pvUser;
3919 int rc = pVDIo->pInterfaceIo->pfnSetAllocationSize(pVDIo->pInterfaceIo->Core.pvUser,
3920 pIoStorage->pStorage, cbSize, fFlags);
3921 if (rc == VERR_NOT_SUPPORTED)
3922 {
3923 /* Fallback if the underlying medium does not support optimized storage allocation. */
3924 uint64_t cbSizeCur = 0;
3925 rc = pVDIo->pInterfaceIo->pfnGetSize(pVDIo->pInterfaceIo->Core.pvUser,
3926 pIoStorage->pStorage, &cbSizeCur);
3927 if (RT_SUCCESS(rc))
3928 {
3929 if (cbSizeCur < cbSize)
3930 {
3931 const size_t cbBuf = 128 * _1K;
3932 void *pvBuf = RTMemTmpAllocZ(cbBuf);
3933 if (RT_LIKELY(pvBuf))
3934 {
3935 uint64_t cbFill = cbSize - cbSizeCur;
3936 uint64_t uOff = 0;
3937
3938 /* Write data to all blocks. */
3939 while ( uOff < cbFill
3940 && RT_SUCCESS(rc))
3941 {
3942 size_t cbChunk = (size_t)RT_MIN(cbFill - uOff, cbBuf);
3943
3944 rc = pVDIo->pInterfaceIo->pfnWriteSync(pVDIo->pInterfaceIo->Core.pvUser,
3945 pIoStorage->pStorage, cbSizeCur + uOff,
3946 pvBuf, cbChunk, NULL);
3947 if (RT_SUCCESS(rc))
3948 {
3949 uOff += cbChunk;
3950
3951 rc = vdIfProgress(pIfProgress, uPercentStart + uOff * uPercentSpan / cbFill);
3952 }
3953 }
3954
3955 RTMemTmpFree(pvBuf);
3956 }
3957 else
3958 rc = VERR_NO_MEMORY;
3959 }
3960 else if (cbSizeCur > cbSize)
3961 rc = pVDIo->pInterfaceIo->pfnSetSize(pVDIo->pInterfaceIo->Core.pvUser,
3962 pIoStorage->pStorage, cbSize);
3963 }
3964 }
3965
3966 if (RT_SUCCESS(rc))
3967 rc = vdIfProgress(pIfProgress, uPercentStart + uPercentSpan);
3968
3969 return rc;
3970}
3971
3972static DECLCALLBACK(int) vdIOIntReadUser(void *pvUser, PVDIOSTORAGE pIoStorage, uint64_t uOffset,
3973 PVDIOCTX pIoCtx, size_t cbRead)
3974{
3975 int rc = VINF_SUCCESS;
3976 PVDIO pVDIo = (PVDIO)pvUser;
3977 PVDISK pDisk = pVDIo->pDisk;
3978
3979 LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pIoCtx=%#p cbRead=%u\n",
3980 pvUser, pIoStorage, uOffset, pIoCtx, cbRead));
3981
3982 /** @todo Enable check for sync I/O later. */
3983 if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
3984 VD_IS_LOCKED(pDisk);
3985
3986 Assert(cbRead > 0);
3987
3988 if (pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC)
3989 {
3990 RTSGSEG Seg;
3991 unsigned cSegments = 1;
3992 size_t cbTaskRead = 0;
3993
3994 /* Synchronous I/O contexts only have one buffer segment. */
3995 AssertMsgReturn(pIoCtx->Req.Io.SgBuf.cSegs == 1,
3996 ("Invalid number of buffer segments for synchronous I/O context"),
3997 VERR_INVALID_PARAMETER);
3998
3999 cbTaskRead = RTSgBufSegArrayCreate(&pIoCtx->Req.Io.SgBuf, &Seg, &cSegments, cbRead);
4000 Assert(cbRead == cbTaskRead);
4001 Assert(cSegments == 1);
4002 rc = pVDIo->pInterfaceIo->pfnReadSync(pVDIo->pInterfaceIo->Core.pvUser,
4003 pIoStorage->pStorage, uOffset,
4004 Seg.pvSeg, cbRead, NULL);
4005 if (RT_SUCCESS(rc))
4006 {
4007 Assert(cbRead == (uint32_t)cbRead);
4008 ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbRead);
4009 }
4010 }
4011 else
4012 {
4013 /* Build the S/G array and spawn a new I/O task */
4014 while (cbRead)
4015 {
4016 RTSGSEG aSeg[VD_IO_TASK_SEGMENTS_MAX];
4017 unsigned cSegments = VD_IO_TASK_SEGMENTS_MAX;
4018 size_t cbTaskRead = RTSgBufSegArrayCreate(&pIoCtx->Req.Io.SgBuf, aSeg, &cSegments, cbRead);
4019
4020 Assert(cSegments > 0);
4021 Assert(cbTaskRead > 0);
4022 AssertMsg(cbTaskRead <= cbRead, ("Invalid number of bytes to read\n"));
4023
4024 LogFlow(("Reading %u bytes into %u segments\n", cbTaskRead, cSegments));
4025
4026#ifdef RT_STRICT
4027 for (unsigned i = 0; i < cSegments; i++)
4028 AssertMsg(aSeg[i].pvSeg && !(aSeg[i].cbSeg % 512),
4029 ("Segment %u is invalid\n", i));
4030#endif
4031
4032 Assert(cbTaskRead == (uint32_t)cbTaskRead);
4033 PVDIOTASK pIoTask = vdIoTaskUserAlloc(pIoStorage, NULL, NULL, pIoCtx, (uint32_t)cbTaskRead);
4034
4035 if (!pIoTask)
4036 return VERR_NO_MEMORY;
4037
4038 ASMAtomicIncU32(&pIoCtx->cDataTransfersPending);
4039
4040 void *pvTask;
4041 Log(("Spawning pIoTask=%p pIoCtx=%p\n", pIoTask, pIoCtx));
4042 rc = pVDIo->pInterfaceIo->pfnReadAsync(pVDIo->pInterfaceIo->Core.pvUser,
4043 pIoStorage->pStorage, uOffset,
4044 aSeg, cSegments, cbTaskRead, pIoTask,
4045 &pvTask);
4046 if (RT_SUCCESS(rc))
4047 {
4048 AssertMsg(cbTaskRead <= pIoCtx->Req.Io.cbTransferLeft, ("Impossible!\n"));
4049 ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbTaskRead);
4050 ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
4051 vdIoTaskFree(pDisk, pIoTask);
4052 }
4053 else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
4054 {
4055 ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
4056 vdIoTaskFree(pDisk, pIoTask);
4057 break;
4058 }
4059
4060 uOffset += cbTaskRead;
4061 cbRead -= cbTaskRead;
4062 }
4063 }
4064
4065 LogFlowFunc(("returns rc=%Rrc\n", rc));
4066 return rc;
4067}
4068
4069static DECLCALLBACK(int) vdIOIntWriteUser(void *pvUser, PVDIOSTORAGE pIoStorage, uint64_t uOffset,
4070 PVDIOCTX pIoCtx, size_t cbWrite, PFNVDXFERCOMPLETED pfnComplete,
4071 void *pvCompleteUser)
4072{
4073 int rc = VINF_SUCCESS;
4074 PVDIO pVDIo = (PVDIO)pvUser;
4075 PVDISK pDisk = pVDIo->pDisk;
4076
4077 LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pIoCtx=%#p cbWrite=%u\n",
4078 pvUser, pIoStorage, uOffset, pIoCtx, cbWrite));
4079
4080 /** @todo Enable check for sync I/O later. */
4081 if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
4082 VD_IS_LOCKED(pDisk);
4083
4084 Assert(cbWrite > 0);
4085
4086 if (pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC)
4087 {
4088 RTSGSEG Seg;
4089 unsigned cSegments = 1;
4090 size_t cbTaskWrite = 0;
4091
4092 /* Synchronous I/O contexts only have one buffer segment. */
4093 AssertMsgReturn(pIoCtx->Req.Io.SgBuf.cSegs == 1,
4094 ("Invalid number of buffer segments for synchronous I/O context"),
4095 VERR_INVALID_PARAMETER);
4096
4097 cbTaskWrite = RTSgBufSegArrayCreate(&pIoCtx->Req.Io.SgBuf, &Seg, &cSegments, cbWrite);
4098 Assert(cbWrite == cbTaskWrite);
4099 Assert(cSegments == 1);
4100 rc = pVDIo->pInterfaceIo->pfnWriteSync(pVDIo->pInterfaceIo->Core.pvUser,
4101 pIoStorage->pStorage, uOffset,
4102 Seg.pvSeg, cbWrite, NULL);
4103 if (RT_SUCCESS(rc))
4104 {
4105 Assert(pIoCtx->Req.Io.cbTransferLeft >= cbWrite);
4106 ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbWrite);
4107 }
4108 }
4109 else
4110 {
4111 /* Build the S/G array and spawn a new I/O task */
4112 while (cbWrite)
4113 {
4114 RTSGSEG aSeg[VD_IO_TASK_SEGMENTS_MAX];
4115 unsigned cSegments = VD_IO_TASK_SEGMENTS_MAX;
4116 size_t cbTaskWrite = 0;
4117
4118 cbTaskWrite = RTSgBufSegArrayCreate(&pIoCtx->Req.Io.SgBuf, aSeg, &cSegments, cbWrite);
4119
4120 Assert(cSegments > 0);
4121 Assert(cbTaskWrite > 0);
4122 AssertMsg(cbTaskWrite <= cbWrite, ("Invalid number of bytes to write\n"));
4123
4124 LogFlow(("Writing %u bytes from %u segments\n", cbTaskWrite, cSegments));
4125
4126#ifdef DEBUG
4127 for (unsigned i = 0; i < cSegments; i++)
4128 AssertMsg(aSeg[i].pvSeg && !(aSeg[i].cbSeg % 512),
4129 ("Segment %u is invalid\n", i));
4130#endif
4131
4132 Assert(cbTaskWrite == (uint32_t)cbTaskWrite);
4133 PVDIOTASK pIoTask = vdIoTaskUserAlloc(pIoStorage, pfnComplete, pvCompleteUser, pIoCtx, (uint32_t)cbTaskWrite);
4134
4135 if (!pIoTask)
4136 return VERR_NO_MEMORY;
4137
4138 ASMAtomicIncU32(&pIoCtx->cDataTransfersPending);
4139
4140 void *pvTask;
4141 Log(("Spawning pIoTask=%p pIoCtx=%p\n", pIoTask, pIoCtx));
4142 rc = pVDIo->pInterfaceIo->pfnWriteAsync(pVDIo->pInterfaceIo->Core.pvUser,
4143 pIoStorage->pStorage,
4144 uOffset, aSeg, cSegments,
4145 cbTaskWrite, pIoTask, &pvTask);
4146 if (RT_SUCCESS(rc))
4147 {
4148 AssertMsg(cbTaskWrite <= pIoCtx->Req.Io.cbTransferLeft, ("Impossible!\n"));
4149 ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbTaskWrite);
4150 ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
4151 vdIoTaskFree(pDisk, pIoTask);
4152 }
4153 else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
4154 {
4155 ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
4156 vdIoTaskFree(pDisk, pIoTask);
4157 break;
4158 }
4159
4160 uOffset += cbTaskWrite;
4161 cbWrite -= cbTaskWrite;
4162 }
4163 }
4164
4165 LogFlowFunc(("returns rc=%Rrc\n", rc));
4166 return rc;
4167}
4168
4169static DECLCALLBACK(int) vdIOIntReadMeta(void *pvUser, PVDIOSTORAGE pIoStorage, uint64_t uOffset,
4170 void *pvBuf, size_t cbRead, PVDIOCTX pIoCtx,
4171 PPVDMETAXFER ppMetaXfer, PFNVDXFERCOMPLETED pfnComplete,
4172 void *pvCompleteUser)
4173{
4174 PVDIO pVDIo = (PVDIO)pvUser;
4175 PVDISK pDisk = pVDIo->pDisk;
4176 int rc = VINF_SUCCESS;
4177 RTSGSEG Seg;
4178 PVDIOTASK pIoTask;
4179 PVDMETAXFER pMetaXfer = NULL;
4180 void *pvTask = NULL;
4181
4182 LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pvBuf=%#p cbRead=%u\n",
4183 pvUser, pIoStorage, uOffset, pvBuf, cbRead));
4184
4185 AssertMsgReturn( pIoCtx
4186 || (!ppMetaXfer && !pfnComplete && !pvCompleteUser),
4187 ("A synchronous metadata read is requested but the parameters are wrong\n"),
4188 VERR_INVALID_POINTER);
4189
4190 /** @todo Enable check for sync I/O later. */
4191 if ( pIoCtx
4192 && !(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
4193 VD_IS_LOCKED(pDisk);
4194
4195 if ( !pIoCtx
4196 || pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC)
4197 {
4198 /* Handle synchronous metadata I/O. */
4199 /** @todo Integrate with metadata transfers below. */
4200 rc = pVDIo->pInterfaceIo->pfnReadSync(pVDIo->pInterfaceIo->Core.pvUser,
4201 pIoStorage->pStorage, uOffset,
4202 pvBuf, cbRead, NULL);
4203 if (ppMetaXfer)
4204 *ppMetaXfer = NULL;
4205 }
4206 else
4207 {
4208 pMetaXfer = (PVDMETAXFER)RTAvlrFileOffsetGet(pIoStorage->pTreeMetaXfers, uOffset);
4209 if (!pMetaXfer)
4210 {
4211#ifdef RT_STRICT
4212 pMetaXfer = (PVDMETAXFER)RTAvlrFileOffsetGetBestFit(pIoStorage->pTreeMetaXfers, uOffset, false /* fAbove */);
4213 AssertMsg(!pMetaXfer || (pMetaXfer->Core.Key + (RTFOFF)pMetaXfer->cbMeta <= (RTFOFF)uOffset),
4214 ("Overlapping meta transfers!\n"));
4215#endif
4216
4217 /* Allocate a new meta transfer. */
4218 pMetaXfer = vdMetaXferAlloc(pIoStorage, uOffset, cbRead);
4219 if (!pMetaXfer)
4220 return VERR_NO_MEMORY;
4221
4222 pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvCompleteUser, pMetaXfer);
4223 if (!pIoTask)
4224 {
4225 RTMemFree(pMetaXfer);
4226 return VERR_NO_MEMORY;
4227 }
4228
4229 Seg.cbSeg = cbRead;
4230 Seg.pvSeg = pMetaXfer->abData;
4231
4232 VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_READ);
4233 rc = pVDIo->pInterfaceIo->pfnReadAsync(pVDIo->pInterfaceIo->Core.pvUser,
4234 pIoStorage->pStorage,
4235 uOffset, &Seg, 1,
4236 cbRead, pIoTask, &pvTask);
4237
4238 if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
4239 {
4240 bool fInserted = RTAvlrFileOffsetInsert(pIoStorage->pTreeMetaXfers, &pMetaXfer->Core);
4241 Assert(fInserted); NOREF(fInserted);
4242 }
4243 else
4244 RTMemFree(pMetaXfer);
4245
4246 if (RT_SUCCESS(rc))
4247 {
4248 VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
4249 vdIoTaskFree(pDisk, pIoTask);
4250 }
4251 else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS && !pfnComplete)
4252 rc = VERR_VD_NOT_ENOUGH_METADATA;
4253 }
4254
4255 Assert(VALID_PTR(pMetaXfer) || RT_FAILURE(rc));
4256
4257 if (RT_SUCCESS(rc) || rc == VERR_VD_NOT_ENOUGH_METADATA || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
4258 {
4259 /* If it is pending add the request to the list. */
4260 if (VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_READ)
4261 {
4262 PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
4263 AssertPtr(pDeferred);
4264
4265 RTListInit(&pDeferred->NodeDeferred);
4266 pDeferred->pIoCtx = pIoCtx;
4267
4268 ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
4269 RTListAppend(&pMetaXfer->ListIoCtxWaiting, &pDeferred->NodeDeferred);
4270 rc = VERR_VD_NOT_ENOUGH_METADATA;
4271 }
4272 else
4273 {
4274 /* Transfer the data. */
4275 pMetaXfer->cRefs++;
4276 Assert(pMetaXfer->cbMeta >= cbRead);
4277 Assert(pMetaXfer->Core.Key == (RTFOFF)uOffset);
4278 if (pMetaXfer->pbDataShw)
4279 memcpy(pvBuf, pMetaXfer->pbDataShw, cbRead);
4280 else
4281 memcpy(pvBuf, pMetaXfer->abData, cbRead);
4282 *ppMetaXfer = pMetaXfer;
4283 }
4284 }
4285 }
4286
4287 LogFlowFunc(("returns rc=%Rrc\n", rc));
4288 return rc;
4289}
4290
4291static DECLCALLBACK(int) vdIOIntWriteMeta(void *pvUser, PVDIOSTORAGE pIoStorage, uint64_t uOffset,
4292 const void *pvBuf, size_t cbWrite, PVDIOCTX pIoCtx,
4293 PFNVDXFERCOMPLETED pfnComplete, void *pvCompleteUser)
4294{
4295 PVDIO pVDIo = (PVDIO)pvUser;
4296 PVDISK pDisk = pVDIo->pDisk;
4297 int rc = VINF_SUCCESS;
4298 RTSGSEG Seg;
4299 PVDIOTASK pIoTask;
4300 PVDMETAXFER pMetaXfer = NULL;
4301 bool fInTree = false;
4302 void *pvTask = NULL;
4303
4304 LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pvBuf=%#p cbWrite=%u\n",
4305 pvUser, pIoStorage, uOffset, pvBuf, cbWrite));
4306
4307 AssertMsgReturn( pIoCtx
4308 || (!pfnComplete && !pvCompleteUser),
4309 ("A synchronous metadata write is requested but the parameters are wrong\n"),
4310 VERR_INVALID_POINTER);
4311
4312 /** @todo Enable check for sync I/O later. */
4313 if ( pIoCtx
4314 && !(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
4315 VD_IS_LOCKED(pDisk);
4316
4317 if ( !pIoCtx
4318 || pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC)
4319 {
4320 /* Handle synchronous metadata I/O. */
4321 /** @todo Integrate with metadata transfers below. */
4322 rc = pVDIo->pInterfaceIo->pfnWriteSync(pVDIo->pInterfaceIo->Core.pvUser,
4323 pIoStorage->pStorage, uOffset,
4324 pvBuf, cbWrite, NULL);
4325 }
4326 else
4327 {
4328 pMetaXfer = (PVDMETAXFER)RTAvlrFileOffsetGet(pIoStorage->pTreeMetaXfers, uOffset);
4329 if (!pMetaXfer)
4330 {
4331 /* Allocate a new meta transfer. */
4332 pMetaXfer = vdMetaXferAlloc(pIoStorage, uOffset, cbWrite);
4333 if (!pMetaXfer)
4334 return VERR_NO_MEMORY;
4335 }
4336 else
4337 {
4338 Assert(pMetaXfer->cbMeta >= cbWrite);
4339 Assert(pMetaXfer->Core.Key == (RTFOFF)uOffset);
4340 fInTree = true;
4341 }
4342
4343 if (VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE)
4344 {
4345 pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvCompleteUser, pMetaXfer);
4346 if (!pIoTask)
4347 {
4348 RTMemFree(pMetaXfer);
4349 return VERR_NO_MEMORY;
4350 }
4351
4352 memcpy(pMetaXfer->abData, pvBuf, cbWrite);
4353 Seg.cbSeg = cbWrite;
4354 Seg.pvSeg = pMetaXfer->abData;
4355
4356 ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
4357
4358 VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_WRITE);
4359 rc = pVDIo->pInterfaceIo->pfnWriteAsync(pVDIo->pInterfaceIo->Core.pvUser,
4360 pIoStorage->pStorage,
4361 uOffset, &Seg, 1, cbWrite, pIoTask,
4362 &pvTask);
4363 if (RT_SUCCESS(rc))
4364 {
4365 VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
4366 ASMAtomicDecU32(&pIoCtx->cMetaTransfersPending);
4367 vdIoTaskFree(pDisk, pIoTask);
4368 if (fInTree && !pMetaXfer->cRefs)
4369 {
4370 LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
4371 bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
4372 AssertMsg(fRemoved, ("Metadata transfer wasn't removed\n")); NOREF(fRemoved);
4373 RTMemFree(pMetaXfer);
4374 pMetaXfer = NULL;
4375 }
4376 }
4377 else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
4378 {
4379 PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
4380 AssertPtr(pDeferred);
4381
4382 RTListInit(&pDeferred->NodeDeferred);
4383 pDeferred->pIoCtx = pIoCtx;
4384
4385 if (!fInTree)
4386 {
4387 bool fInserted = RTAvlrFileOffsetInsert(pIoStorage->pTreeMetaXfers, &pMetaXfer->Core);
4388 Assert(fInserted); NOREF(fInserted);
4389 }
4390
4391 RTListAppend(&pMetaXfer->ListIoCtxWaiting, &pDeferred->NodeDeferred);
4392 }
4393 else
4394 {
4395 RTMemFree(pMetaXfer);
4396 pMetaXfer = NULL;
4397 }
4398 }
4399 else
4400 {
4401 /* I/O is in progress, update shadow buffer and add to waiting list. */
4402 Assert(VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_WRITE);
4403 if (!pMetaXfer->pbDataShw)
4404 {
4405 /* Allocate shadow buffer and set initial state. */
4406 LogFlowFunc(("pMetaXfer=%#p Creating shadow buffer\n", pMetaXfer));
4407 pMetaXfer->pbDataShw = (uint8_t *)RTMemAlloc(pMetaXfer->cbMeta);
4408 if (RT_LIKELY(pMetaXfer->pbDataShw))
4409 memcpy(pMetaXfer->pbDataShw, pMetaXfer->abData, pMetaXfer->cbMeta);
4410 else
4411 rc = VERR_NO_MEMORY;
4412 }
4413
4414 if (RT_SUCCESS(rc))
4415 {
4416 /* Update with written data and append to waiting list. */
4417 PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
4418 if (pDeferred)
4419 {
4420 LogFlowFunc(("pMetaXfer=%#p Updating shadow buffer\n", pMetaXfer));
4421
4422 RTListInit(&pDeferred->NodeDeferred);
4423 pDeferred->pIoCtx = pIoCtx;
4424 ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
4425 memcpy(pMetaXfer->pbDataShw, pvBuf, cbWrite);
4426 RTListAppend(&pMetaXfer->ListIoCtxShwWrites, &pDeferred->NodeDeferred);
4427 }
4428 else
4429 {
4430 /*
4431 * Free shadow buffer if there is no one depending on it, i.e.
4432 * we just allocated it.
4433 */
4434 if (RTListIsEmpty(&pMetaXfer->ListIoCtxShwWrites))
4435 {
4436 RTMemFree(pMetaXfer->pbDataShw);
4437 pMetaXfer->pbDataShw = NULL;
4438 }
4439 rc = VERR_NO_MEMORY;
4440 }
4441 }
4442 }
4443 }
4444
4445 LogFlowFunc(("returns rc=%Rrc\n", rc));
4446 return rc;
4447}
4448
4449static DECLCALLBACK(void) vdIOIntMetaXferRelease(void *pvUser, PVDMETAXFER pMetaXfer)
4450{
4451 PVDIO pVDIo = (PVDIO)pvUser;
4452 PVDISK pDisk = pVDIo->pDisk;
4453 PVDIOSTORAGE pIoStorage;
4454
4455 /*
4456 * It is possible that we get called with a NULL metadata xfer handle
4457 * for synchronous I/O. Just exit.
4458 */
4459 if (!pMetaXfer)
4460 return;
4461
4462 pIoStorage = pMetaXfer->pIoStorage;
4463
4464 VD_IS_LOCKED(pDisk);
4465
4466 Assert( VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE
4467 || VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_WRITE);
4468 Assert(pMetaXfer->cRefs > 0);
4469
4470 pMetaXfer->cRefs--;
4471 if ( !pMetaXfer->cRefs
4472 && RTListIsEmpty(&pMetaXfer->ListIoCtxWaiting)
4473 && VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE)
4474 {
4475 /* Free the meta data entry. */
4476 LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
4477 bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
4478 AssertMsg(fRemoved, ("Metadata transfer wasn't removed\n")); NOREF(fRemoved);
4479
4480 RTMemFree(pMetaXfer);
4481 }
4482}
4483
4484static DECLCALLBACK(int) vdIOIntFlush(void *pvUser, PVDIOSTORAGE pIoStorage, PVDIOCTX pIoCtx,
4485 PFNVDXFERCOMPLETED pfnComplete, void *pvCompleteUser)
4486{
4487 PVDIO pVDIo = (PVDIO)pvUser;
4488 PVDISK pDisk = pVDIo->pDisk;
4489 int rc = VINF_SUCCESS;
4490 PVDIOTASK pIoTask;
4491 PVDMETAXFER pMetaXfer = NULL;
4492 void *pvTask = NULL;
4493
4494 LogFlowFunc(("pvUser=%#p pIoStorage=%#p pIoCtx=%#p\n",
4495 pvUser, pIoStorage, pIoCtx));
4496
4497 AssertMsgReturn( pIoCtx
4498 || (!pfnComplete && !pvCompleteUser),
4499 ("A synchronous metadata write is requested but the parameters are wrong\n"),
4500 VERR_INVALID_POINTER);
4501
4502 /** @todo Enable check for sync I/O later. */
4503 if ( pIoCtx
4504 && !(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
4505 VD_IS_LOCKED(pDisk);
4506
4507 if (pVDIo->fIgnoreFlush)
4508 return VINF_SUCCESS;
4509
4510 if ( !pIoCtx
4511 || pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC)
4512 {
4513 /* Handle synchronous flushes. */
4514 /** @todo Integrate with metadata transfers below. */
4515 rc = pVDIo->pInterfaceIo->pfnFlushSync(pVDIo->pInterfaceIo->Core.pvUser,
4516 pIoStorage->pStorage);
4517 }
4518 else
4519 {
4520 /* Allocate a new meta transfer. */
4521 pMetaXfer = vdMetaXferAlloc(pIoStorage, 0, 0);
4522 if (!pMetaXfer)
4523 return VERR_NO_MEMORY;
4524
4525 pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvUser, pMetaXfer);
4526 if (!pIoTask)
4527 {
4528 RTMemFree(pMetaXfer);
4529 return VERR_NO_MEMORY;
4530 }
4531
4532 ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
4533
4534 PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
4535 AssertPtr(pDeferred);
4536
4537 RTListInit(&pDeferred->NodeDeferred);
4538 pDeferred->pIoCtx = pIoCtx;
4539
4540 RTListAppend(&pMetaXfer->ListIoCtxWaiting, &pDeferred->NodeDeferred);
4541 VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_FLUSH);
4542 rc = pVDIo->pInterfaceIo->pfnFlushAsync(pVDIo->pInterfaceIo->Core.pvUser,
4543 pIoStorage->pStorage,
4544 pIoTask, &pvTask);
4545 if (RT_SUCCESS(rc))
4546 {
4547 VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
4548 ASMAtomicDecU32(&pIoCtx->cMetaTransfersPending);
4549 vdIoTaskFree(pDisk, pIoTask);
4550 RTMemFree(pDeferred);
4551 RTMemFree(pMetaXfer);
4552 }
4553 else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
4554 RTMemFree(pMetaXfer);
4555 }
4556
4557 LogFlowFunc(("returns rc=%Rrc\n", rc));
4558 return rc;
4559}
4560
4561static DECLCALLBACK(size_t) vdIOIntIoCtxCopyTo(void *pvUser, PVDIOCTX pIoCtx,
4562 const void *pvBuf, size_t cbBuf)
4563{
4564 PVDIO pVDIo = (PVDIO)pvUser;
4565 PVDISK pDisk = pVDIo->pDisk;
4566 size_t cbCopied = 0;
4567
4568 /** @todo Enable check for sync I/O later. */
4569 if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
4570 VD_IS_LOCKED(pDisk);
4571
4572 cbCopied = vdIoCtxCopyTo(pIoCtx, (uint8_t *)pvBuf, cbBuf);
4573 Assert(cbCopied == cbBuf);
4574
4575 /// @todo Assert(pIoCtx->Req.Io.cbTransferLeft >= cbCopied); - triggers with vdCopyHelper/dmgRead.
4576 ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbCopied);
4577
4578 return cbCopied;
4579}
4580
4581static DECLCALLBACK(size_t) vdIOIntIoCtxCopyFrom(void *pvUser, PVDIOCTX pIoCtx,
4582 void *pvBuf, size_t cbBuf)
4583{
4584 PVDIO pVDIo = (PVDIO)pvUser;
4585 PVDISK pDisk = pVDIo->pDisk;
4586 size_t cbCopied = 0;
4587
4588 /** @todo Enable check for sync I/O later. */
4589 if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
4590 VD_IS_LOCKED(pDisk);
4591
4592 cbCopied = vdIoCtxCopyFrom(pIoCtx, (uint8_t *)pvBuf, cbBuf);
4593 Assert(cbCopied == cbBuf);
4594
4595 /// @todo Assert(pIoCtx->Req.Io.cbTransferLeft > cbCopied); - triggers with vdCopyHelper/dmgRead.
4596 ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbCopied);
4597
4598 return cbCopied;
4599}
4600
4601static DECLCALLBACK(size_t) vdIOIntIoCtxSet(void *pvUser, PVDIOCTX pIoCtx, int ch, size_t cb)
4602{
4603 PVDIO pVDIo = (PVDIO)pvUser;
4604 PVDISK pDisk = pVDIo->pDisk;
4605 size_t cbSet = 0;
4606
4607 /** @todo Enable check for sync I/O later. */
4608 if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
4609 VD_IS_LOCKED(pDisk);
4610
4611 cbSet = vdIoCtxSet(pIoCtx, ch, cb);
4612 Assert(cbSet == cb);
4613
4614 /// @todo Assert(pIoCtx->Req.Io.cbTransferLeft >= cbSet); - triggers with vdCopyHelper/dmgRead.
4615 ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbSet);
4616
4617 return cbSet;
4618}
4619
4620static DECLCALLBACK(size_t) vdIOIntIoCtxSegArrayCreate(void *pvUser, PVDIOCTX pIoCtx,
4621 PRTSGSEG paSeg, unsigned *pcSeg,
4622 size_t cbData)
4623{
4624 PVDIO pVDIo = (PVDIO)pvUser;
4625 PVDISK pDisk = pVDIo->pDisk;
4626 size_t cbCreated = 0;
4627
4628 /** @todo It is possible that this gets called from a filter plugin
4629 * outside of the disk lock. Refine assertion or remove completely. */
4630#if 0
4631 /** @todo Enable check for sync I/O later. */
4632 if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
4633 VD_IS_LOCKED(pDisk);
4634#else
4635 NOREF(pDisk);
4636#endif
4637
4638 cbCreated = RTSgBufSegArrayCreate(&pIoCtx->Req.Io.SgBuf, paSeg, pcSeg, cbData);
4639 Assert(!paSeg || cbData == cbCreated);
4640
4641 return cbCreated;
4642}
4643
4644static DECLCALLBACK(void) vdIOIntIoCtxCompleted(void *pvUser, PVDIOCTX pIoCtx, int rcReq,
4645 size_t cbCompleted)
4646{
4647 PVDIO pVDIo = (PVDIO)pvUser;
4648 PVDISK pDisk = pVDIo->pDisk;
4649
4650 LogFlowFunc(("pvUser=%#p pIoCtx=%#p rcReq=%Rrc cbCompleted=%zu\n",
4651 pvUser, pIoCtx, rcReq, cbCompleted));
4652
4653 /*
4654 * Grab the disk critical section to avoid races with other threads which
4655 * might still modify the I/O context.
4656 * Example is that iSCSI is doing an asynchronous write but calls us already
4657 * while the other thread is still hanging in vdWriteHelperAsync and couldn't update
4658 * the blocked state yet.
4659 * It can overwrite the state to true before we call vdIoCtxContinue and the
4660 * the request would hang indefinite.
4661 */
4662 ASMAtomicCmpXchgS32(&pIoCtx->rcReq, rcReq, VINF_SUCCESS);
4663 Assert(pIoCtx->Req.Io.cbTransferLeft >= cbCompleted);
4664 ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbCompleted);
4665
4666 /* Set next transfer function if the current one finished.
4667 * @todo: Find a better way to prevent vdIoCtxContinue from calling the current helper again. */
4668 if (!pIoCtx->Req.Io.cbTransferLeft)
4669 {
4670 pIoCtx->pfnIoCtxTransfer = pIoCtx->pfnIoCtxTransferNext;
4671 pIoCtx->pfnIoCtxTransferNext = NULL;
4672 }
4673
4674 vdIoCtxAddToWaitingList(&pDisk->pIoCtxHaltedHead, pIoCtx);
4675 if (ASMAtomicCmpXchgBool(&pDisk->fLocked, true, false))
4676 {
4677 /* Immediately drop the lock again, it will take care of processing the list. */
4678 vdDiskUnlock(pDisk, NULL);
4679 }
4680}
4681
4682static DECLCALLBACK(bool) vdIOIntIoCtxIsSynchronous(void *pvUser, PVDIOCTX pIoCtx)
4683{
4684 NOREF(pvUser);
4685 return !!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC);
4686}
4687
4688static DECLCALLBACK(bool) vdIOIntIoCtxIsZero(void *pvUser, PVDIOCTX pIoCtx, size_t cbCheck,
4689 bool fAdvance)
4690{
4691 NOREF(pvUser);
4692
4693 bool fIsZero = RTSgBufIsZero(&pIoCtx->Req.Io.SgBuf, cbCheck);
4694 if (fIsZero && fAdvance)
4695 RTSgBufAdvance(&pIoCtx->Req.Io.SgBuf, cbCheck);
4696
4697 return fIsZero;
4698}
4699
4700static DECLCALLBACK(size_t) vdIOIntIoCtxGetDataUnitSize(void *pvUser, PVDIOCTX pIoCtx)
4701{
4702 RT_NOREF1(pIoCtx);
4703 PVDIO pVDIo = (PVDIO)pvUser;
4704 PVDISK pDisk = pVDIo->pDisk;
4705 size_t cbSector = 0;
4706
4707 PVDIMAGE pImage = vdGetImageByNumber(pDisk, VD_LAST_IMAGE);
4708 AssertPtrReturn(pImage, 0);
4709
4710 PCVDREGIONLIST pRegionList = NULL;
4711 int rc = pImage->Backend->pfnQueryRegions(pImage->pBackendData, &pRegionList);
4712 if (RT_SUCCESS(rc))
4713 {
4714 cbSector = pRegionList->aRegions[0].cbBlock;
4715
4716 AssertPtr(pImage->Backend->pfnRegionListRelease);
4717 pImage->Backend->pfnRegionListRelease(pImage->pBackendData, pRegionList);
4718 }
4719
4720 return cbSector;
4721}
4722
4723/**
4724 * VD I/O interface callback for opening a file (limited version for VDGetFormat).
4725 */
4726static DECLCALLBACK(int) vdIOIntOpenLimited(void *pvUser, const char *pszLocation,
4727 uint32_t fOpen, PPVDIOSTORAGE ppIoStorage)
4728{
4729 int rc = VINF_SUCCESS;
4730 PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
4731 PVDIOSTORAGE pIoStorage = (PVDIOSTORAGE)RTMemAllocZ(sizeof(VDIOSTORAGE));
4732
4733 if (!pIoStorage)
4734 return VERR_NO_MEMORY;
4735
4736 rc = pInterfaceIo->pfnOpen(NULL, pszLocation, fOpen, NULL, &pIoStorage->pStorage);
4737 if (RT_SUCCESS(rc))
4738 *ppIoStorage = pIoStorage;
4739 else
4740 RTMemFree(pIoStorage);
4741
4742 return rc;
4743}
4744
4745static DECLCALLBACK(int) vdIOIntCloseLimited(void *pvUser, PVDIOSTORAGE pIoStorage)
4746{
4747 PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
4748 int rc = pInterfaceIo->pfnClose(NULL, pIoStorage->pStorage);
4749
4750 RTMemFree(pIoStorage);
4751 return rc;
4752}
4753
4754static DECLCALLBACK(int) vdIOIntDeleteLimited(void *pvUser, const char *pcszFilename)
4755{
4756 PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
4757 return pInterfaceIo->pfnDelete(NULL, pcszFilename);
4758}
4759
4760static DECLCALLBACK(int) vdIOIntMoveLimited(void *pvUser, const char *pcszSrc,
4761 const char *pcszDst, unsigned fMove)
4762{
4763 PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
4764 return pInterfaceIo->pfnMove(NULL, pcszSrc, pcszDst, fMove);
4765}
4766
4767static DECLCALLBACK(int) vdIOIntGetFreeSpaceLimited(void *pvUser, const char *pcszFilename,
4768 int64_t *pcbFreeSpace)
4769{
4770 PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
4771 return pInterfaceIo->pfnGetFreeSpace(NULL, pcszFilename, pcbFreeSpace);
4772}
4773
4774static DECLCALLBACK(int) vdIOIntGetModificationTimeLimited(void *pvUser,
4775 const char *pcszFilename,
4776 PRTTIMESPEC pModificationTime)
4777{
4778 PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
4779 return pInterfaceIo->pfnGetModificationTime(NULL, pcszFilename, pModificationTime);
4780}
4781
4782static DECLCALLBACK(int) vdIOIntGetSizeLimited(void *pvUser, PVDIOSTORAGE pIoStorage,
4783 uint64_t *pcbSize)
4784{
4785 PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
4786 return pInterfaceIo->pfnGetSize(NULL, pIoStorage->pStorage, pcbSize);
4787}
4788
4789static DECLCALLBACK(int) vdIOIntSetSizeLimited(void *pvUser, PVDIOSTORAGE pIoStorage,
4790 uint64_t cbSize)
4791{
4792 PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
4793 return pInterfaceIo->pfnSetSize(NULL, pIoStorage->pStorage, cbSize);
4794}
4795
4796static DECLCALLBACK(int) vdIOIntWriteUserLimited(void *pvUser, PVDIOSTORAGE pStorage,
4797 uint64_t uOffset, PVDIOCTX pIoCtx,
4798 size_t cbWrite,
4799 PFNVDXFERCOMPLETED pfnComplete,
4800 void *pvCompleteUser)
4801{
4802 NOREF(pvUser);
4803 NOREF(pStorage);
4804 NOREF(uOffset);
4805 NOREF(pIoCtx);
4806 NOREF(cbWrite);
4807 NOREF(pfnComplete);
4808 NOREF(pvCompleteUser);
4809 AssertMsgFailedReturn(("This needs to be implemented when called\n"), VERR_NOT_IMPLEMENTED);
4810}
4811
4812static DECLCALLBACK(int) vdIOIntReadUserLimited(void *pvUser, PVDIOSTORAGE pStorage,
4813 uint64_t uOffset, PVDIOCTX pIoCtx,
4814 size_t cbRead)
4815{
4816 NOREF(pvUser);
4817 NOREF(pStorage);
4818 NOREF(uOffset);
4819 NOREF(pIoCtx);
4820 NOREF(cbRead);
4821 AssertMsgFailedReturn(("This needs to be implemented when called\n"), VERR_NOT_IMPLEMENTED);
4822}
4823
4824static DECLCALLBACK(int) vdIOIntWriteMetaLimited(void *pvUser, PVDIOSTORAGE pStorage,
4825 uint64_t uOffset, const void *pvBuffer,
4826 size_t cbBuffer, PVDIOCTX pIoCtx,
4827 PFNVDXFERCOMPLETED pfnComplete,
4828 void *pvCompleteUser)
4829{
4830 PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
4831
4832 AssertMsgReturn(!pIoCtx && !pfnComplete && !pvCompleteUser,
4833 ("Async I/O not implemented for the limited interface"),
4834 VERR_NOT_SUPPORTED);
4835
4836 return pInterfaceIo->pfnWriteSync(NULL, pStorage->pStorage, uOffset, pvBuffer, cbBuffer, NULL);
4837}
4838
4839static DECLCALLBACK(int) vdIOIntReadMetaLimited(void *pvUser, PVDIOSTORAGE pStorage,
4840 uint64_t uOffset, void *pvBuffer,
4841 size_t cbBuffer, PVDIOCTX pIoCtx,
4842 PPVDMETAXFER ppMetaXfer,
4843 PFNVDXFERCOMPLETED pfnComplete,
4844 void *pvCompleteUser)
4845{
4846 PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
4847
4848 AssertMsgReturn(!pIoCtx && !ppMetaXfer && !pfnComplete && !pvCompleteUser,
4849 ("Async I/O not implemented for the limited interface"),
4850 VERR_NOT_SUPPORTED);
4851
4852 return pInterfaceIo->pfnReadSync(NULL, pStorage->pStorage, uOffset, pvBuffer, cbBuffer, NULL);
4853}
4854
4855#if 0 /* unsed */
4856static int vdIOIntMetaXferReleaseLimited(void *pvUser, PVDMETAXFER pMetaXfer)
4857{
4858 /* This is a NOP in this case. */
4859 NOREF(pvUser);
4860 NOREF(pMetaXfer);
4861 return VINF_SUCCESS;
4862}
4863#endif
4864
4865static DECLCALLBACK(int) vdIOIntFlushLimited(void *pvUser, PVDIOSTORAGE pStorage,
4866 PVDIOCTX pIoCtx,
4867 PFNVDXFERCOMPLETED pfnComplete,
4868 void *pvCompleteUser)
4869{
4870 PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
4871
4872 AssertMsgReturn(!pIoCtx && !pfnComplete && !pvCompleteUser,
4873 ("Async I/O not implemented for the limited interface"),
4874 VERR_NOT_SUPPORTED);
4875
4876 return pInterfaceIo->pfnFlushSync(NULL, pStorage->pStorage);
4877}
4878
4879/**
4880 * internal: send output to the log (unconditionally).
4881 */
4882static DECLCALLBACK(int) vdLogMessage(void *pvUser, const char *pszFormat, va_list args)
4883{
4884 NOREF(pvUser);
4885 RTLogPrintfV(pszFormat, args);
4886 return VINF_SUCCESS;
4887}
4888
4889DECLINLINE(int) vdMessageWrapper(PVDISK pDisk, const char *pszFormat, ...)
4890{
4891 va_list va;
4892 va_start(va, pszFormat);
4893 int rc = pDisk->pInterfaceError->pfnMessage(pDisk->pInterfaceError->Core.pvUser,
4894 pszFormat, va);
4895 va_end(va);
4896 return rc;
4897}
4898
4899
4900/**
4901 * internal: adjust PCHS geometry
4902 */
4903static void vdFixupPCHSGeometry(PVDGEOMETRY pPCHS, uint64_t cbSize)
4904{
4905 /* Fix broken PCHS geometry. Can happen for two reasons: either the backend
4906 * mixes up PCHS and LCHS, or the application used to create the source
4907 * image has put garbage in it. Additionally, if the PCHS geometry covers
4908 * more than the image size, set it back to the default. */
4909 if ( pPCHS->cHeads > 16
4910 || pPCHS->cSectors > 63
4911 || pPCHS->cCylinders == 0
4912 || (uint64_t)pPCHS->cHeads * pPCHS->cSectors * pPCHS->cCylinders * 512 > cbSize)
4913 {
4914 Assert(!(RT_MIN(cbSize / 512 / 16 / 63, 16383) - (uint32_t)RT_MIN(cbSize / 512 / 16 / 63, 16383)));
4915 pPCHS->cCylinders = (uint32_t)RT_MIN(cbSize / 512 / 16 / 63, 16383);
4916 pPCHS->cHeads = 16;
4917 pPCHS->cSectors = 63;
4918 }
4919}
4920
4921/**
4922 * internal: adjust LCHS geometry
4923 */
4924static void vdFixupLCHSGeometry(PVDGEOMETRY pLCHS, uint64_t cbSize)
4925{
4926 /* Fix broken LCHS geometry. Can happen for two reasons: either the backend
4927 * mixes up PCHS and LCHS, or the application used to create the source
4928 * image has put garbage in it. The fix in this case is to clear the LCHS
4929 * geometry to trigger autodetection when it is used next. If the geometry
4930 * already says "please autodetect" (cylinders=0) keep it. */
4931 if ( ( pLCHS->cHeads > 255
4932 || pLCHS->cHeads == 0
4933 || pLCHS->cSectors > 63
4934 || pLCHS->cSectors == 0)
4935 && pLCHS->cCylinders != 0)
4936 {
4937 pLCHS->cCylinders = 0;
4938 pLCHS->cHeads = 0;
4939 pLCHS->cSectors = 0;
4940 }
4941 /* Always recompute the number of cylinders stored in the LCHS
4942 * geometry if it isn't set to "autotedetect" at the moment.
4943 * This is very useful if the destination image size is
4944 * larger or smaller than the source image size. Do not modify
4945 * the number of heads and sectors. Windows guests hate it. */
4946 if ( pLCHS->cCylinders != 0
4947 && pLCHS->cHeads != 0 /* paranoia */
4948 && pLCHS->cSectors != 0 /* paranoia */)
4949 {
4950 Assert(!(RT_MIN(cbSize / 512 / pLCHS->cHeads / pLCHS->cSectors, 1024) - (uint32_t)RT_MIN(cbSize / 512 / pLCHS->cHeads / pLCHS->cSectors, 1024)));
4951 pLCHS->cCylinders = (uint32_t)RT_MIN(cbSize / 512 / pLCHS->cHeads / pLCHS->cSectors, 1024);
4952 }
4953}
4954
4955/**
4956 * Sets the I/O callbacks of the given interface to the fallback methods
4957 *
4958 * @returns nothing.
4959 * @param pIfIo The I/O interface to setup.
4960 */
4961static void vdIfIoFallbackCallbacksSetup(PVDINTERFACEIO pIfIo)
4962{
4963 pIfIo->pfnOpen = vdIOOpenFallback;
4964 pIfIo->pfnClose = vdIOCloseFallback;
4965 pIfIo->pfnDelete = vdIODeleteFallback;
4966 pIfIo->pfnMove = vdIOMoveFallback;
4967 pIfIo->pfnGetFreeSpace = vdIOGetFreeSpaceFallback;
4968 pIfIo->pfnGetModificationTime = vdIOGetModificationTimeFallback;
4969 pIfIo->pfnGetSize = vdIOGetSizeFallback;
4970 pIfIo->pfnSetSize = vdIOSetSizeFallback;
4971 pIfIo->pfnSetAllocationSize = vdIOSetAllocationSizeFallback;
4972 pIfIo->pfnReadSync = vdIOReadSyncFallback;
4973 pIfIo->pfnWriteSync = vdIOWriteSyncFallback;
4974 pIfIo->pfnFlushSync = vdIOFlushSyncFallback;
4975 pIfIo->pfnReadAsync = vdIOReadAsyncFallback;
4976 pIfIo->pfnWriteAsync = vdIOWriteAsyncFallback;
4977 pIfIo->pfnFlushAsync = vdIOFlushAsyncFallback;
4978}
4979
4980/**
4981 * Sets the internal I/O callbacks of the given interface.
4982 *
4983 * @returns nothing.
4984 * @param pIfIoInt The internal I/O interface to setup.
4985 */
4986static void vdIfIoIntCallbacksSetup(PVDINTERFACEIOINT pIfIoInt)
4987{
4988 pIfIoInt->pfnOpen = vdIOIntOpen;
4989 pIfIoInt->pfnClose = vdIOIntClose;
4990 pIfIoInt->pfnDelete = vdIOIntDelete;
4991 pIfIoInt->pfnMove = vdIOIntMove;
4992 pIfIoInt->pfnGetFreeSpace = vdIOIntGetFreeSpace;
4993 pIfIoInt->pfnGetModificationTime = vdIOIntGetModificationTime;
4994 pIfIoInt->pfnGetSize = vdIOIntGetSize;
4995 pIfIoInt->pfnSetSize = vdIOIntSetSize;
4996 pIfIoInt->pfnSetAllocationSize = vdIOIntSetAllocationSize;
4997 pIfIoInt->pfnReadUser = vdIOIntReadUser;
4998 pIfIoInt->pfnWriteUser = vdIOIntWriteUser;
4999 pIfIoInt->pfnReadMeta = vdIOIntReadMeta;
5000 pIfIoInt->pfnWriteMeta = vdIOIntWriteMeta;
5001 pIfIoInt->pfnMetaXferRelease = vdIOIntMetaXferRelease;
5002 pIfIoInt->pfnFlush = vdIOIntFlush;
5003 pIfIoInt->pfnIoCtxCopyFrom = vdIOIntIoCtxCopyFrom;
5004 pIfIoInt->pfnIoCtxCopyTo = vdIOIntIoCtxCopyTo;
5005 pIfIoInt->pfnIoCtxSet = vdIOIntIoCtxSet;
5006 pIfIoInt->pfnIoCtxSegArrayCreate = vdIOIntIoCtxSegArrayCreate;
5007 pIfIoInt->pfnIoCtxCompleted = vdIOIntIoCtxCompleted;
5008 pIfIoInt->pfnIoCtxIsSynchronous = vdIOIntIoCtxIsSynchronous;
5009 pIfIoInt->pfnIoCtxIsZero = vdIOIntIoCtxIsZero;
5010 pIfIoInt->pfnIoCtxGetDataUnitSize = vdIOIntIoCtxGetDataUnitSize;
5011}
5012
5013/**
5014 * Internally used completion handler for synchronous I/O contexts.
5015 */
5016static DECLCALLBACK(void) vdIoCtxSyncComplete(void *pvUser1, void *pvUser2, int rcReq)
5017{
5018 RT_NOREF2(pvUser1, rcReq);
5019 RTSEMEVENT hEvent = (RTSEMEVENT)pvUser2;
5020
5021 RTSemEventSignal(hEvent);
5022}
5023
5024
5025VBOXDDU_DECL(int) VDInit(void)
5026{
5027 int rc = vdPluginInit();
5028 LogRel(("VD: VDInit finished with %Rrc\n", rc));
5029 return rc;
5030}
5031
5032
5033VBOXDDU_DECL(int) VDShutdown(void)
5034{
5035 return vdPluginTerm();
5036}
5037
5038
5039VBOXDDU_DECL(int) VDPluginLoadFromFilename(const char *pszFilename)
5040{
5041 if (!vdPluginIsInitialized())
5042 {
5043 int rc = VDInit();
5044 if (RT_FAILURE(rc))
5045 return rc;
5046 }
5047
5048 return vdPluginLoadFromFilename(pszFilename);
5049}
5050
5051/**
5052 * Load all plugins from a given path.
5053 *
5054 * @returns VBox statuse code.
5055 * @param pszPath The path to load plugins from.
5056 */
5057VBOXDDU_DECL(int) VDPluginLoadFromPath(const char *pszPath)
5058{
5059 if (!vdPluginIsInitialized())
5060 {
5061 int rc = VDInit();
5062 if (RT_FAILURE(rc))
5063 return rc;
5064 }
5065
5066 return vdPluginLoadFromPath(pszPath);
5067}
5068
5069
5070VBOXDDU_DECL(int) VDPluginUnloadFromFilename(const char *pszFilename)
5071{
5072 if (!vdPluginIsInitialized())
5073 {
5074 int rc = VDInit();
5075 if (RT_FAILURE(rc))
5076 return rc;
5077 }
5078
5079 return vdPluginUnloadFromFilename(pszFilename);
5080}
5081
5082
5083VBOXDDU_DECL(int) VDPluginUnloadFromPath(const char *pszPath)
5084{
5085 if (!vdPluginIsInitialized())
5086 {
5087 int rc = VDInit();
5088 if (RT_FAILURE(rc))
5089 return rc;
5090 }
5091
5092 return vdPluginUnloadFromPath(pszPath);
5093}
5094
5095
5096VBOXDDU_DECL(int) VDBackendInfo(unsigned cEntriesAlloc, PVDBACKENDINFO pEntries,
5097 unsigned *pcEntriesUsed)
5098{
5099 int rc = VINF_SUCCESS;
5100
5101 LogFlowFunc(("cEntriesAlloc=%u pEntries=%#p pcEntriesUsed=%#p\n", cEntriesAlloc, pEntries, pcEntriesUsed));
5102 /* Check arguments. */
5103 AssertMsgReturn(cEntriesAlloc,
5104 ("cEntriesAlloc=%u\n", cEntriesAlloc),
5105 VERR_INVALID_PARAMETER);
5106 AssertMsgReturn(VALID_PTR(pEntries),
5107 ("pEntries=%#p\n", pEntries),
5108 VERR_INVALID_PARAMETER);
5109 AssertMsgReturn(VALID_PTR(pcEntriesUsed),
5110 ("pcEntriesUsed=%#p\n", pcEntriesUsed),
5111 VERR_INVALID_PARAMETER);
5112 if (!vdPluginIsInitialized())
5113 VDInit();
5114
5115 uint32_t cBackends = vdGetImageBackendCount();
5116 if (cEntriesAlloc < cBackends)
5117 {
5118 *pcEntriesUsed = cBackends;
5119 return VERR_BUFFER_OVERFLOW;
5120 }
5121
5122 for (unsigned i = 0; i < cBackends; i++)
5123 {
5124 PCVDIMAGEBACKEND pBackend;
5125 rc = vdQueryImageBackend(i, &pBackend);
5126 AssertRC(rc);
5127
5128 pEntries[i].pszBackend = pBackend->pszBackendName;
5129 pEntries[i].uBackendCaps = pBackend->uBackendCaps;
5130 pEntries[i].paFileExtensions = pBackend->paFileExtensions;
5131 pEntries[i].paConfigInfo = pBackend->paConfigInfo;
5132 pEntries[i].pfnComposeLocation = pBackend->pfnComposeLocation;
5133 pEntries[i].pfnComposeName = pBackend->pfnComposeName;
5134 }
5135
5136 LogFlowFunc(("returns %Rrc *pcEntriesUsed=%u\n", rc, cBackends));
5137 *pcEntriesUsed = cBackends;
5138 return rc;
5139}
5140
5141
5142VBOXDDU_DECL(int) VDBackendInfoOne(const char *pszBackend, PVDBACKENDINFO pEntry)
5143{
5144 LogFlowFunc(("pszBackend=%#p pEntry=%#p\n", pszBackend, pEntry));
5145 /* Check arguments. */
5146 AssertMsgReturn(VALID_PTR(pszBackend),
5147 ("pszBackend=%#p\n", pszBackend),
5148 VERR_INVALID_PARAMETER);
5149 AssertMsgReturn(VALID_PTR(pEntry),
5150 ("pEntry=%#p\n", pEntry),
5151 VERR_INVALID_PARAMETER);
5152 if (!vdPluginIsInitialized())
5153 VDInit();
5154
5155 PCVDIMAGEBACKEND pBackend;
5156 int rc = vdFindImageBackend(pszBackend, &pBackend);
5157 if (RT_SUCCESS(rc))
5158 {
5159 pEntry->pszBackend = pBackend->pszBackendName;
5160 pEntry->uBackendCaps = pBackend->uBackendCaps;
5161 pEntry->paFileExtensions = pBackend->paFileExtensions;
5162 pEntry->paConfigInfo = pBackend->paConfigInfo;
5163 }
5164
5165 return rc;
5166}
5167
5168
5169VBOXDDU_DECL(int) VDFilterInfo(unsigned cEntriesAlloc, PVDFILTERINFO pEntries,
5170 unsigned *pcEntriesUsed)
5171{
5172 int rc = VINF_SUCCESS;
5173
5174 LogFlowFunc(("cEntriesAlloc=%u pEntries=%#p pcEntriesUsed=%#p\n", cEntriesAlloc, pEntries, pcEntriesUsed));
5175 /* Check arguments. */
5176 AssertMsgReturn(cEntriesAlloc,
5177 ("cEntriesAlloc=%u\n", cEntriesAlloc),
5178 VERR_INVALID_PARAMETER);
5179 AssertMsgReturn(VALID_PTR(pEntries),
5180 ("pEntries=%#p\n", pEntries),
5181 VERR_INVALID_PARAMETER);
5182 AssertMsgReturn(VALID_PTR(pcEntriesUsed),
5183 ("pcEntriesUsed=%#p\n", pcEntriesUsed),
5184 VERR_INVALID_PARAMETER);
5185 if (!vdPluginIsInitialized())
5186 VDInit();
5187
5188 uint32_t cBackends = vdGetFilterBackendCount();
5189 if (cEntriesAlloc < cBackends)
5190 {
5191 *pcEntriesUsed = cBackends;
5192 return VERR_BUFFER_OVERFLOW;
5193 }
5194
5195 for (unsigned i = 0; i < cBackends; i++)
5196 {
5197 PCVDFILTERBACKEND pBackend;
5198 rc = vdQueryFilterBackend(i, &pBackend);
5199 pEntries[i].pszFilter = pBackend->pszBackendName;
5200 pEntries[i].paConfigInfo = pBackend->paConfigInfo;
5201 }
5202
5203 LogFlowFunc(("returns %Rrc *pcEntriesUsed=%u\n", rc, cBackends));
5204 *pcEntriesUsed = cBackends;
5205 return rc;
5206}
5207
5208
5209VBOXDDU_DECL(int) VDFilterInfoOne(const char *pszFilter, PVDFILTERINFO pEntry)
5210{
5211 LogFlowFunc(("pszFilter=%#p pEntry=%#p\n", pszFilter, pEntry));
5212 /* Check arguments. */
5213 AssertMsgReturn(VALID_PTR(pszFilter),
5214 ("pszFilter=%#p\n", pszFilter),
5215 VERR_INVALID_PARAMETER);
5216 AssertMsgReturn(VALID_PTR(pEntry),
5217 ("pEntry=%#p\n", pEntry),
5218 VERR_INVALID_PARAMETER);
5219 if (!vdPluginIsInitialized())
5220 VDInit();
5221
5222 PCVDFILTERBACKEND pBackend;
5223 int rc = vdFindFilterBackend(pszFilter, &pBackend);
5224 if (RT_SUCCESS(rc))
5225 {
5226 pEntry->pszFilter = pBackend->pszBackendName;
5227 pEntry->paConfigInfo = pBackend->paConfigInfo;
5228 }
5229
5230 return rc;
5231}
5232
5233
5234VBOXDDU_DECL(int) VDCreate(PVDINTERFACE pVDIfsDisk, VDTYPE enmType, PVDISK *ppDisk)
5235{
5236 int rc = VINF_SUCCESS;
5237 PVDISK pDisk = NULL;
5238
5239 LogFlowFunc(("pVDIfsDisk=%#p\n", pVDIfsDisk));
5240 do
5241 {
5242 /* Check arguments. */
5243 AssertMsgBreakStmt(VALID_PTR(ppDisk),
5244 ("ppDisk=%#p\n", ppDisk),
5245 rc = VERR_INVALID_PARAMETER);
5246
5247 pDisk = (PVDISK)RTMemAllocZ(sizeof(VDISK));
5248 if (pDisk)
5249 {
5250 pDisk->u32Signature = VDISK_SIGNATURE;
5251 pDisk->enmType = enmType;
5252 pDisk->cImages = 0;
5253 pDisk->pBase = NULL;
5254 pDisk->pLast = NULL;
5255 pDisk->cbSize = 0;
5256 pDisk->PCHSGeometry.cCylinders = 0;
5257 pDisk->PCHSGeometry.cHeads = 0;
5258 pDisk->PCHSGeometry.cSectors = 0;
5259 pDisk->LCHSGeometry.cCylinders = 0;
5260 pDisk->LCHSGeometry.cHeads = 0;
5261 pDisk->LCHSGeometry.cSectors = 0;
5262 pDisk->pVDIfsDisk = pVDIfsDisk;
5263 pDisk->pInterfaceError = NULL;
5264 pDisk->pInterfaceThreadSync = NULL;
5265 pDisk->pIoCtxLockOwner = NULL;
5266 pDisk->pIoCtxHead = NULL;
5267 pDisk->fLocked = false;
5268 pDisk->hMemCacheIoCtx = NIL_RTMEMCACHE;
5269 pDisk->hMemCacheIoTask = NIL_RTMEMCACHE;
5270 RTListInit(&pDisk->ListFilterChainWrite);
5271 RTListInit(&pDisk->ListFilterChainRead);
5272
5273 /* Create the I/O ctx cache */
5274 rc = RTMemCacheCreate(&pDisk->hMemCacheIoCtx, sizeof(VDIOCTX), 0, UINT32_MAX,
5275 NULL, NULL, NULL, 0);
5276 if (RT_FAILURE(rc))
5277 break;
5278
5279 /* Create the I/O task cache */
5280 rc = RTMemCacheCreate(&pDisk->hMemCacheIoTask, sizeof(VDIOTASK), 0, UINT32_MAX,
5281 NULL, NULL, NULL, 0);
5282 if (RT_FAILURE(rc))
5283 break;
5284
5285 pDisk->pInterfaceError = VDIfErrorGet(pVDIfsDisk);
5286 pDisk->pInterfaceThreadSync = VDIfThreadSyncGet(pVDIfsDisk);
5287
5288 *ppDisk = pDisk;
5289 }
5290 else
5291 {
5292 rc = VERR_NO_MEMORY;
5293 break;
5294 }
5295 } while (0);
5296
5297 if ( RT_FAILURE(rc)
5298 && pDisk)
5299 {
5300 if (pDisk->hMemCacheIoCtx != NIL_RTMEMCACHE)
5301 RTMemCacheDestroy(pDisk->hMemCacheIoCtx);
5302 if (pDisk->hMemCacheIoTask != NIL_RTMEMCACHE)
5303 RTMemCacheDestroy(pDisk->hMemCacheIoTask);
5304 }
5305
5306 LogFlowFunc(("returns %Rrc (pDisk=%#p)\n", rc, pDisk));
5307 return rc;
5308}
5309
5310
5311VBOXDDU_DECL(int) VDDestroy(PVDISK pDisk)
5312{
5313 int rc = VINF_SUCCESS;
5314 LogFlowFunc(("pDisk=%#p\n", pDisk));
5315 do
5316 {
5317 /* sanity check */
5318 AssertPtrBreak(pDisk);
5319 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
5320 Assert(!pDisk->fLocked);
5321
5322 rc = VDCloseAll(pDisk);
5323 int rc2 = VDFilterRemoveAll(pDisk);
5324 if (RT_SUCCESS(rc))
5325 rc = rc2;
5326
5327 RTMemCacheDestroy(pDisk->hMemCacheIoCtx);
5328 RTMemCacheDestroy(pDisk->hMemCacheIoTask);
5329 RTMemFree(pDisk);
5330 } while (0);
5331 LogFlowFunc(("returns %Rrc\n", rc));
5332 return rc;
5333}
5334
5335
5336VBOXDDU_DECL(int) VDGetFormat(PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
5337 const char *pszFilename, VDTYPE enmDesiredType,
5338 char **ppszFormat, VDTYPE *penmType)
5339{
5340 int rc = VERR_NOT_SUPPORTED;
5341 VDINTERFACEIOINT VDIfIoInt;
5342 VDINTERFACEIO VDIfIoFallback;
5343 PVDINTERFACEIO pInterfaceIo;
5344
5345 LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
5346 /* Check arguments. */
5347 AssertMsgReturn(VALID_PTR(pszFilename) && *pszFilename,
5348 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
5349 VERR_INVALID_PARAMETER);
5350 AssertMsgReturn(VALID_PTR(ppszFormat),
5351 ("ppszFormat=%#p\n", ppszFormat),
5352 VERR_INVALID_PARAMETER);
5353 AssertMsgReturn(VALID_PTR(penmType),
5354 ("penmType=%#p\n", penmType),
5355 VERR_INVALID_PARAMETER);
5356 AssertReturn(enmDesiredType >= VDTYPE_INVALID && enmDesiredType <= VDTYPE_FLOPPY, VERR_INVALID_PARAMETER);
5357
5358 if (!vdPluginIsInitialized())
5359 VDInit();
5360
5361 pInterfaceIo = VDIfIoGet(pVDIfsImage);
5362 if (!pInterfaceIo)
5363 {
5364 /*
5365 * Caller doesn't provide an I/O interface, create our own using the
5366 * native file API.
5367 */
5368 vdIfIoFallbackCallbacksSetup(&VDIfIoFallback);
5369 pInterfaceIo = &VDIfIoFallback;
5370 }
5371
5372 /* Set up the internal I/O interface. */
5373 AssertReturn(!VDIfIoIntGet(pVDIfsImage), VERR_INVALID_PARAMETER);
5374 VDIfIoInt.pfnOpen = vdIOIntOpenLimited;
5375 VDIfIoInt.pfnClose = vdIOIntCloseLimited;
5376 VDIfIoInt.pfnDelete = vdIOIntDeleteLimited;
5377 VDIfIoInt.pfnMove = vdIOIntMoveLimited;
5378 VDIfIoInt.pfnGetFreeSpace = vdIOIntGetFreeSpaceLimited;
5379 VDIfIoInt.pfnGetModificationTime = vdIOIntGetModificationTimeLimited;
5380 VDIfIoInt.pfnGetSize = vdIOIntGetSizeLimited;
5381 VDIfIoInt.pfnSetSize = vdIOIntSetSizeLimited;
5382 VDIfIoInt.pfnReadUser = vdIOIntReadUserLimited;
5383 VDIfIoInt.pfnWriteUser = vdIOIntWriteUserLimited;
5384 VDIfIoInt.pfnReadMeta = vdIOIntReadMetaLimited;
5385 VDIfIoInt.pfnWriteMeta = vdIOIntWriteMetaLimited;
5386 VDIfIoInt.pfnFlush = vdIOIntFlushLimited;
5387 rc = VDInterfaceAdd(&VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
5388 pInterfaceIo, sizeof(VDINTERFACEIOINT), &pVDIfsImage);
5389 AssertRC(rc);
5390
5391 /** @todo r=bird: Would be better to do a scoring approach here, where the
5392 * backend that scores the highest is choosen. That way we don't have to depend
5393 * on registration order and filename suffixes to figure out what RAW should
5394 * handle and not. Besides, the registration order won't cut it for plug-ins
5395 * anyway, as they end up after the builtin ones.
5396 */
5397
5398 /* Find the backend supporting this file format. */
5399 for (unsigned i = 0; i < vdGetImageBackendCount(); i++)
5400 {
5401 PCVDIMAGEBACKEND pBackend;
5402 rc = vdQueryImageBackend(i, &pBackend);
5403 AssertRC(rc);
5404
5405 if (pBackend->pfnProbe)
5406 {
5407 rc = pBackend->pfnProbe(pszFilename, pVDIfsDisk, pVDIfsImage, enmDesiredType, penmType);
5408 if ( RT_SUCCESS(rc)
5409 /* The correct backend has been found, but there is a small
5410 * incompatibility so that the file cannot be used. Stop here
5411 * and signal success - the actual open will of course fail,
5412 * but that will create a really sensible error message. */
5413
5414 /** @todo r=bird: this bit of code is _certifiably_ _insane_ as it allows
5415 * simple stuff like VERR_EOF to pass thru. I've just amended it with
5416 * disallowing VERR_EOF too, but someone needs to pick up the courage to
5417 * fix this stuff properly or at least update the docs!
5418 * (Parallels returns VERR_EOF, btw.) */
5419
5420 || ( rc != VERR_VD_GEN_INVALID_HEADER
5421 && rc != VERR_VD_VDI_INVALID_HEADER
5422 && rc != VERR_VD_VMDK_INVALID_HEADER
5423 && rc != VERR_VD_ISCSI_INVALID_HEADER
5424 && rc != VERR_VD_VHD_INVALID_HEADER
5425 && rc != VERR_VD_RAW_INVALID_HEADER
5426 && rc != VERR_VD_RAW_SIZE_MODULO_512
5427 && rc != VERR_VD_RAW_SIZE_MODULO_2048
5428 && rc != VERR_VD_RAW_SIZE_OPTICAL_TOO_SMALL
5429 && rc != VERR_VD_RAW_SIZE_FLOPPY_TOO_BIG
5430 && rc != VERR_VD_PARALLELS_INVALID_HEADER
5431 && rc != VERR_VD_DMG_INVALID_HEADER
5432 && rc != VERR_EOF /* bird for viso */
5433 ))
5434 {
5435 /* Copy the name into the new string. */
5436 char *pszFormat = RTStrDup(pBackend->pszBackendName);
5437 if (!pszFormat)
5438 {
5439 rc = VERR_NO_MEMORY;
5440 break;
5441 }
5442 *ppszFormat = pszFormat;
5443 /* Do not consider the typical file access errors as success,
5444 * which allows the caller to deal with such issues. */
5445 if ( rc != VERR_ACCESS_DENIED
5446 && rc != VERR_PATH_NOT_FOUND
5447 && rc != VERR_FILE_NOT_FOUND)
5448 rc = VINF_SUCCESS;
5449 break;
5450 }
5451 rc = VERR_NOT_SUPPORTED;
5452 }
5453 }
5454
5455 /* Try the cache backends. */
5456 if (rc == VERR_NOT_SUPPORTED)
5457 {
5458 for (unsigned i = 0; i < vdGetCacheBackendCount(); i++)
5459 {
5460 PCVDCACHEBACKEND pBackend;
5461 rc = vdQueryCacheBackend(i, &pBackend);
5462 AssertRC(rc);
5463
5464 if (pBackend->pfnProbe)
5465 {
5466 rc = pBackend->pfnProbe(pszFilename, pVDIfsDisk, pVDIfsImage);
5467 if ( RT_SUCCESS(rc)
5468 || (rc != VERR_VD_GEN_INVALID_HEADER))
5469 {
5470 /* Copy the name into the new string. */
5471 char *pszFormat = RTStrDup(pBackend->pszBackendName);
5472 if (!pszFormat)
5473 {
5474 rc = VERR_NO_MEMORY;
5475 break;
5476 }
5477 *ppszFormat = pszFormat;
5478 rc = VINF_SUCCESS;
5479 break;
5480 }
5481 rc = VERR_NOT_SUPPORTED;
5482 }
5483 }
5484 }
5485
5486 LogFlowFunc(("returns %Rrc *ppszFormat=\"%s\"\n", rc, *ppszFormat));
5487 return rc;
5488}
5489
5490
5491VBOXDDU_DECL(int) VDOpen(PVDISK pDisk, const char *pszBackend,
5492 const char *pszFilename, unsigned uOpenFlags,
5493 PVDINTERFACE pVDIfsImage)
5494{
5495 int rc = VINF_SUCCESS;
5496 int rc2;
5497 bool fLockWrite = false;
5498 PVDIMAGE pImage = NULL;
5499
5500 LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uOpenFlags=%#x, pVDIfsImage=%#p\n",
5501 pDisk, pszBackend, pszFilename, uOpenFlags, pVDIfsImage));
5502
5503 do
5504 {
5505 /* sanity check */
5506 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
5507 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
5508
5509 /* Check arguments. */
5510 AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
5511 ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
5512 rc = VERR_INVALID_PARAMETER);
5513 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
5514 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
5515 rc = VERR_INVALID_PARAMETER);
5516 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
5517 ("uOpenFlags=%#x\n", uOpenFlags),
5518 rc = VERR_INVALID_PARAMETER);
5519 AssertMsgBreakStmt( !(uOpenFlags & VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)
5520 || (uOpenFlags & VD_OPEN_FLAGS_READONLY),
5521 ("uOpenFlags=%#x\n", uOpenFlags),
5522 rc = VERR_INVALID_PARAMETER);
5523
5524 /*
5525 * Destroy the current discard state first which might still have pending blocks
5526 * for the currently opened image which will be switched to readonly mode.
5527 */
5528 /* Lock disk for writing, as we modify pDisk information below. */
5529 rc2 = vdThreadStartWrite(pDisk);
5530 AssertRC(rc2);
5531 fLockWrite = true;
5532 rc = vdDiscardStateDestroy(pDisk);
5533 if (RT_FAILURE(rc))
5534 break;
5535 rc2 = vdThreadFinishWrite(pDisk);
5536 AssertRC(rc2);
5537 fLockWrite = false;
5538
5539 /* Set up image descriptor. */
5540 pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
5541 if (!pImage)
5542 {
5543 rc = VERR_NO_MEMORY;
5544 break;
5545 }
5546 pImage->pszFilename = RTStrDup(pszFilename);
5547 if (!pImage->pszFilename)
5548 {
5549 rc = VERR_NO_MEMORY;
5550 break;
5551 }
5552
5553 pImage->cbImage = VD_IMAGE_SIZE_UNINITIALIZED;
5554 pImage->VDIo.pDisk = pDisk;
5555 pImage->pVDIfsImage = pVDIfsImage;
5556
5557 rc = vdFindImageBackend(pszBackend, &pImage->Backend);
5558 if (RT_FAILURE(rc))
5559 break;
5560 if (!pImage->Backend)
5561 {
5562 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
5563 N_("VD: unknown backend name '%s'"), pszBackend);
5564 break;
5565 }
5566
5567 /*
5568 * Fail if the backend can't do async I/O but the
5569 * flag is set.
5570 */
5571 if ( !(pImage->Backend->uBackendCaps & VD_CAP_ASYNC)
5572 && (uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO))
5573 {
5574 rc = vdError(pDisk, VERR_NOT_SUPPORTED, RT_SRC_POS,
5575 N_("VD: Backend '%s' does not support async I/O"), pszBackend);
5576 break;
5577 }
5578
5579 /*
5580 * Fail if the backend doesn't support the discard operation but the
5581 * flag is set.
5582 */
5583 if ( !(pImage->Backend->uBackendCaps & VD_CAP_DISCARD)
5584 && (uOpenFlags & VD_OPEN_FLAGS_DISCARD))
5585 {
5586 rc = vdError(pDisk, VERR_VD_DISCARD_NOT_SUPPORTED, RT_SRC_POS,
5587 N_("VD: Backend '%s' does not support discard"), pszBackend);
5588 break;
5589 }
5590
5591 /* Set up the I/O interface. */
5592 pImage->VDIo.pInterfaceIo = VDIfIoGet(pVDIfsImage);
5593 if (!pImage->VDIo.pInterfaceIo)
5594 {
5595 vdIfIoFallbackCallbacksSetup(&pImage->VDIo.VDIfIo);
5596 rc = VDInterfaceAdd(&pImage->VDIo.VDIfIo.Core, "VD_IO", VDINTERFACETYPE_IO,
5597 pDisk, sizeof(VDINTERFACEIO), &pVDIfsImage);
5598 pImage->VDIo.pInterfaceIo = &pImage->VDIo.VDIfIo;
5599 }
5600
5601 /* Set up the internal I/O interface. */
5602 AssertBreakStmt(!VDIfIoIntGet(pVDIfsImage), rc = VERR_INVALID_PARAMETER);
5603 vdIfIoIntCallbacksSetup(&pImage->VDIo.VDIfIoInt);
5604 rc = VDInterfaceAdd(&pImage->VDIo.VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
5605 &pImage->VDIo, sizeof(VDINTERFACEIOINT), &pImage->pVDIfsImage);
5606 AssertRC(rc);
5607
5608 pImage->uOpenFlags = uOpenFlags & (VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_DISCARD | VD_OPEN_FLAGS_IGNORE_FLUSH | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS);
5609 pImage->VDIo.fIgnoreFlush = (uOpenFlags & VD_OPEN_FLAGS_IGNORE_FLUSH) != 0;
5610 rc = pImage->Backend->pfnOpen(pImage->pszFilename,
5611 uOpenFlags & ~(VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_IGNORE_FLUSH | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS),
5612 pDisk->pVDIfsDisk,
5613 pImage->pVDIfsImage,
5614 pDisk->enmType,
5615 &pImage->pBackendData);
5616 /*
5617 * If the image is corrupted and there is a repair method try to repair it
5618 * first if it was openend in read-write mode and open again afterwards.
5619 */
5620 if ( RT_UNLIKELY(rc == VERR_VD_IMAGE_CORRUPTED)
5621 && !(uOpenFlags & VD_OPEN_FLAGS_READONLY)
5622 && pImage->Backend->pfnRepair)
5623 {
5624 rc = pImage->Backend->pfnRepair(pszFilename, pDisk->pVDIfsDisk, pImage->pVDIfsImage, 0 /* fFlags */);
5625 if (RT_SUCCESS(rc))
5626 rc = pImage->Backend->pfnOpen(pImage->pszFilename,
5627 uOpenFlags & ~(VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_IGNORE_FLUSH | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS),
5628 pDisk->pVDIfsDisk,
5629 pImage->pVDIfsImage,
5630 pDisk->enmType,
5631 &pImage->pBackendData);
5632 else
5633 {
5634 rc = vdError(pDisk, rc, RT_SRC_POS,
5635 N_("VD: error %Rrc repairing corrupted image file '%s'"), rc, pszFilename);
5636 break;
5637 }
5638 }
5639 else if (RT_UNLIKELY(rc == VERR_VD_IMAGE_CORRUPTED))
5640 {
5641 rc = vdError(pDisk, rc, RT_SRC_POS,
5642 N_("VD: Image file '%s' is corrupted and can't be opened"), pszFilename);
5643 break;
5644 }
5645
5646 /* If the open in read-write mode failed, retry in read-only mode. */
5647 if (RT_FAILURE(rc))
5648 {
5649 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY)
5650 && ( rc == VERR_ACCESS_DENIED
5651 || rc == VERR_PERMISSION_DENIED
5652 || rc == VERR_WRITE_PROTECT
5653 || rc == VERR_SHARING_VIOLATION
5654 || rc == VERR_FILE_LOCK_FAILED))
5655 rc = pImage->Backend->pfnOpen(pImage->pszFilename,
5656 (uOpenFlags & ~(VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS))
5657 | VD_OPEN_FLAGS_READONLY,
5658 pDisk->pVDIfsDisk,
5659 pImage->pVDIfsImage,
5660 pDisk->enmType,
5661 &pImage->pBackendData);
5662 if (RT_FAILURE(rc))
5663 {
5664 rc = vdError(pDisk, rc, RT_SRC_POS,
5665 N_("VD: error %Rrc opening image file '%s'"), rc, pszFilename);
5666 break;
5667 }
5668 }
5669
5670 /* Lock disk for writing, as we modify pDisk information below. */
5671 rc2 = vdThreadStartWrite(pDisk);
5672 AssertRC(rc2);
5673 fLockWrite = true;
5674
5675 pImage->VDIo.pBackendData = pImage->pBackendData;
5676
5677 /* Check image type. As the image itself has only partial knowledge
5678 * whether it's a base image or not, this info is derived here. The
5679 * base image can be fixed or normal, all others must be normal or
5680 * diff images. Some image formats don't distinguish between normal
5681 * and diff images, so this must be corrected here. */
5682 unsigned uImageFlags;
5683 uImageFlags = pImage->Backend->pfnGetImageFlags(pImage->pBackendData);
5684 if (RT_FAILURE(rc))
5685 uImageFlags = VD_IMAGE_FLAGS_NONE;
5686 if ( RT_SUCCESS(rc)
5687 && !(uOpenFlags & VD_OPEN_FLAGS_INFO))
5688 {
5689 if ( pDisk->cImages == 0
5690 && (uImageFlags & VD_IMAGE_FLAGS_DIFF))
5691 {
5692 rc = VERR_VD_INVALID_TYPE;
5693 break;
5694 }
5695 else if (pDisk->cImages != 0)
5696 {
5697 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
5698 {
5699 rc = VERR_VD_INVALID_TYPE;
5700 break;
5701 }
5702 else
5703 uImageFlags |= VD_IMAGE_FLAGS_DIFF;
5704 }
5705 }
5706
5707 /* Ensure we always get correct diff information, even if the backend
5708 * doesn't actually have a stored flag for this. It must not return
5709 * bogus information for the parent UUID if it is not a diff image. */
5710 RTUUID parentUuid;
5711 RTUuidClear(&parentUuid);
5712 rc2 = pImage->Backend->pfnGetParentUuid(pImage->pBackendData, &parentUuid);
5713 if (RT_SUCCESS(rc2) && !RTUuidIsNull(&parentUuid))
5714 uImageFlags |= VD_IMAGE_FLAGS_DIFF;
5715
5716 pImage->uImageFlags = uImageFlags;
5717
5718 /* Force sane optimization settings. It's not worth avoiding writes
5719 * to fixed size images. The overhead would have almost no payback. */
5720 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
5721 pImage->uOpenFlags |= VD_OPEN_FLAGS_HONOR_SAME;
5722
5723 /** @todo optionally check UUIDs */
5724
5725 /* Cache disk information. */
5726 pDisk->cbSize = vdImageGetSize(pImage);
5727
5728 /* Cache PCHS geometry. */
5729 rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
5730 &pDisk->PCHSGeometry);
5731 if (RT_FAILURE(rc2))
5732 {
5733 pDisk->PCHSGeometry.cCylinders = 0;
5734 pDisk->PCHSGeometry.cHeads = 0;
5735 pDisk->PCHSGeometry.cSectors = 0;
5736 }
5737 else
5738 {
5739 /* Make sure the PCHS geometry is properly clipped. */
5740 pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
5741 pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
5742 pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
5743 }
5744
5745 /* Cache LCHS geometry. */
5746 rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
5747 &pDisk->LCHSGeometry);
5748 if (RT_FAILURE(rc2))
5749 {
5750 pDisk->LCHSGeometry.cCylinders = 0;
5751 pDisk->LCHSGeometry.cHeads = 0;
5752 pDisk->LCHSGeometry.cSectors = 0;
5753 }
5754 else
5755 {
5756 /* Make sure the LCHS geometry is properly clipped. */
5757 pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
5758 pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
5759 }
5760
5761 if (pDisk->cImages != 0)
5762 {
5763 /* Switch previous image to read-only mode. */
5764 unsigned uOpenFlagsPrevImg;
5765 uOpenFlagsPrevImg = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pBackendData);
5766 if (!(uOpenFlagsPrevImg & VD_OPEN_FLAGS_READONLY))
5767 {
5768 uOpenFlagsPrevImg |= VD_OPEN_FLAGS_READONLY;
5769 rc = pDisk->pLast->Backend->pfnSetOpenFlags(pDisk->pLast->pBackendData, uOpenFlagsPrevImg);
5770 }
5771 }
5772
5773 if (RT_SUCCESS(rc))
5774 {
5775 /* Image successfully opened, make it the last image. */
5776 vdAddImageToList(pDisk, pImage);
5777 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
5778 pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
5779 }
5780 else
5781 {
5782 /* Error detected, but image opened. Close image. */
5783 rc2 = pImage->Backend->pfnClose(pImage->pBackendData, false);
5784 AssertRC(rc2);
5785 pImage->pBackendData = NULL;
5786 }
5787 } while (0);
5788
5789 if (RT_UNLIKELY(fLockWrite))
5790 {
5791 rc2 = vdThreadFinishWrite(pDisk);
5792 AssertRC(rc2);
5793 }
5794
5795 if (RT_FAILURE(rc))
5796 {
5797 if (pImage)
5798 {
5799 if (pImage->pszFilename)
5800 RTStrFree(pImage->pszFilename);
5801 RTMemFree(pImage);
5802 }
5803 }
5804
5805 LogFlowFunc(("returns %Rrc\n", rc));
5806 return rc;
5807}
5808
5809
5810VBOXDDU_DECL(int) VDCacheOpen(PVDISK pDisk, const char *pszBackend,
5811 const char *pszFilename, unsigned uOpenFlags,
5812 PVDINTERFACE pVDIfsCache)
5813{
5814 int rc = VINF_SUCCESS;
5815 int rc2;
5816 bool fLockWrite = false;
5817 PVDCACHE pCache = NULL;
5818
5819 LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uOpenFlags=%#x, pVDIfsCache=%#p\n",
5820 pDisk, pszBackend, pszFilename, uOpenFlags, pVDIfsCache));
5821
5822 do
5823 {
5824 /* sanity check */
5825 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
5826 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
5827
5828 /* Check arguments. */
5829 AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
5830 ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
5831 rc = VERR_INVALID_PARAMETER);
5832 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
5833 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
5834 rc = VERR_INVALID_PARAMETER);
5835 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
5836 ("uOpenFlags=%#x\n", uOpenFlags),
5837 rc = VERR_INVALID_PARAMETER);
5838
5839 /* Set up image descriptor. */
5840 pCache = (PVDCACHE)RTMemAllocZ(sizeof(VDCACHE));
5841 if (!pCache)
5842 {
5843 rc = VERR_NO_MEMORY;
5844 break;
5845 }
5846 pCache->pszFilename = RTStrDup(pszFilename);
5847 if (!pCache->pszFilename)
5848 {
5849 rc = VERR_NO_MEMORY;
5850 break;
5851 }
5852
5853 pCache->VDIo.pDisk = pDisk;
5854 pCache->pVDIfsCache = pVDIfsCache;
5855
5856 rc = vdFindCacheBackend(pszBackend, &pCache->Backend);
5857 if (RT_FAILURE(rc))
5858 break;
5859 if (!pCache->Backend)
5860 {
5861 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
5862 N_("VD: unknown backend name '%s'"), pszBackend);
5863 break;
5864 }
5865
5866 /* Set up the I/O interface. */
5867 pCache->VDIo.pInterfaceIo = VDIfIoGet(pVDIfsCache);
5868 if (!pCache->VDIo.pInterfaceIo)
5869 {
5870 vdIfIoFallbackCallbacksSetup(&pCache->VDIo.VDIfIo);
5871 rc = VDInterfaceAdd(&pCache->VDIo.VDIfIo.Core, "VD_IO", VDINTERFACETYPE_IO,
5872 pDisk, sizeof(VDINTERFACEIO), &pVDIfsCache);
5873 pCache->VDIo.pInterfaceIo = &pCache->VDIo.VDIfIo;
5874 }
5875
5876 /* Set up the internal I/O interface. */
5877 AssertBreakStmt(!VDIfIoIntGet(pVDIfsCache), rc = VERR_INVALID_PARAMETER);
5878 vdIfIoIntCallbacksSetup(&pCache->VDIo.VDIfIoInt);
5879 rc = VDInterfaceAdd(&pCache->VDIo.VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
5880 &pCache->VDIo, sizeof(VDINTERFACEIOINT), &pCache->pVDIfsCache);
5881 AssertRC(rc);
5882
5883 pCache->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
5884 rc = pCache->Backend->pfnOpen(pCache->pszFilename,
5885 uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
5886 pDisk->pVDIfsDisk,
5887 pCache->pVDIfsCache,
5888 &pCache->pBackendData);
5889 /* If the open in read-write mode failed, retry in read-only mode. */
5890 if (RT_FAILURE(rc))
5891 {
5892 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY)
5893 && ( rc == VERR_ACCESS_DENIED
5894 || rc == VERR_PERMISSION_DENIED
5895 || rc == VERR_WRITE_PROTECT
5896 || rc == VERR_SHARING_VIOLATION
5897 || rc == VERR_FILE_LOCK_FAILED))
5898 rc = pCache->Backend->pfnOpen(pCache->pszFilename,
5899 (uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME)
5900 | VD_OPEN_FLAGS_READONLY,
5901 pDisk->pVDIfsDisk,
5902 pCache->pVDIfsCache,
5903 &pCache->pBackendData);
5904 if (RT_FAILURE(rc))
5905 {
5906 rc = vdError(pDisk, rc, RT_SRC_POS,
5907 N_("VD: error %Rrc opening image file '%s'"), rc, pszFilename);
5908 break;
5909 }
5910 }
5911
5912 /* Lock disk for writing, as we modify pDisk information below. */
5913 rc2 = vdThreadStartWrite(pDisk);
5914 AssertRC(rc2);
5915 fLockWrite = true;
5916
5917 /*
5918 * Check that the modification UUID of the cache and last image
5919 * match. If not the image was modified in-between without the cache.
5920 * The cache might contain stale data.
5921 */
5922 RTUUID UuidImage, UuidCache;
5923
5924 rc = pCache->Backend->pfnGetModificationUuid(pCache->pBackendData,
5925 &UuidCache);
5926 if (RT_SUCCESS(rc))
5927 {
5928 rc = pDisk->pLast->Backend->pfnGetModificationUuid(pDisk->pLast->pBackendData,
5929 &UuidImage);
5930 if (RT_SUCCESS(rc))
5931 {
5932 if (RTUuidCompare(&UuidImage, &UuidCache))
5933 rc = VERR_VD_CACHE_NOT_UP_TO_DATE;
5934 }
5935 }
5936
5937 /*
5938 * We assume that the user knows what he is doing if one of the images
5939 * doesn't support the modification uuid.
5940 */
5941 if (rc == VERR_NOT_SUPPORTED)
5942 rc = VINF_SUCCESS;
5943
5944 if (RT_SUCCESS(rc))
5945 {
5946 /* Cache successfully opened, make it the current one. */
5947 if (!pDisk->pCache)
5948 pDisk->pCache = pCache;
5949 else
5950 rc = VERR_VD_CACHE_ALREADY_EXISTS;
5951 }
5952
5953 if (RT_FAILURE(rc))
5954 {
5955 /* Error detected, but image opened. Close image. */
5956 rc2 = pCache->Backend->pfnClose(pCache->pBackendData, false);
5957 AssertRC(rc2);
5958 pCache->pBackendData = NULL;
5959 }
5960 } while (0);
5961
5962 if (RT_UNLIKELY(fLockWrite))
5963 {
5964 rc2 = vdThreadFinishWrite(pDisk);
5965 AssertRC(rc2);
5966 }
5967
5968 if (RT_FAILURE(rc))
5969 {
5970 if (pCache)
5971 {
5972 if (pCache->pszFilename)
5973 RTStrFree(pCache->pszFilename);
5974 RTMemFree(pCache);
5975 }
5976 }
5977
5978 LogFlowFunc(("returns %Rrc\n", rc));
5979 return rc;
5980}
5981
5982
5983VBOXDDU_DECL(int) VDFilterAdd(PVDISK pDisk, const char *pszFilter, uint32_t fFlags,
5984 PVDINTERFACE pVDIfsFilter)
5985{
5986 int rc = VINF_SUCCESS;
5987 int rc2;
5988 bool fLockWrite = false;
5989 PVDFILTER pFilter = NULL;
5990
5991 LogFlowFunc(("pDisk=%#p pszFilter=\"%s\" pVDIfsFilter=%#p\n",
5992 pDisk, pszFilter, pVDIfsFilter));
5993
5994 do
5995 {
5996 /* sanity check */
5997 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
5998 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
5999
6000 /* Check arguments. */
6001 AssertMsgBreakStmt(VALID_PTR(pszFilter) && *pszFilter,
6002 ("pszFilter=%#p \"%s\"\n", pszFilter, pszFilter),
6003 rc = VERR_INVALID_PARAMETER);
6004
6005 AssertMsgBreakStmt(!(fFlags & ~VD_FILTER_FLAGS_MASK),
6006 ("Invalid flags set (fFlags=%#x)\n", fFlags),
6007 rc = VERR_INVALID_PARAMETER);
6008
6009 /* Set up image descriptor. */
6010 pFilter = (PVDFILTER)RTMemAllocZ(sizeof(VDFILTER));
6011 if (!pFilter)
6012 {
6013 rc = VERR_NO_MEMORY;
6014 break;
6015 }
6016
6017 rc = vdFindFilterBackend(pszFilter, &pFilter->pBackend);
6018 if (RT_FAILURE(rc))
6019 break;
6020 if (!pFilter->pBackend)
6021 {
6022 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
6023 N_("VD: unknown filter backend name '%s'"), pszFilter);
6024 break;
6025 }
6026
6027 pFilter->VDIo.pDisk = pDisk;
6028 pFilter->pVDIfsFilter = pVDIfsFilter;
6029
6030 /* Set up the internal I/O interface. */
6031 AssertBreakStmt(!VDIfIoIntGet(pVDIfsFilter), rc = VERR_INVALID_PARAMETER);
6032 vdIfIoIntCallbacksSetup(&pFilter->VDIo.VDIfIoInt);
6033 rc = VDInterfaceAdd(&pFilter->VDIo.VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
6034 &pFilter->VDIo, sizeof(VDINTERFACEIOINT), &pFilter->pVDIfsFilter);
6035 AssertRC(rc);
6036
6037 rc = pFilter->pBackend->pfnCreate(pDisk->pVDIfsDisk, fFlags & VD_FILTER_FLAGS_INFO,
6038 pFilter->pVDIfsFilter, &pFilter->pvBackendData);
6039 if (RT_FAILURE(rc))
6040 break;
6041
6042 /* Lock disk for writing, as we modify pDisk information below. */
6043 rc2 = vdThreadStartWrite(pDisk);
6044 AssertRC(rc2);
6045 fLockWrite = true;
6046
6047 /* Add filter to chains. */
6048 if (fFlags & VD_FILTER_FLAGS_WRITE)
6049 {
6050 RTListAppend(&pDisk->ListFilterChainWrite, &pFilter->ListNodeChainWrite);
6051 vdFilterRetain(pFilter);
6052 }
6053
6054 if (fFlags & VD_FILTER_FLAGS_READ)
6055 {
6056 RTListAppend(&pDisk->ListFilterChainRead, &pFilter->ListNodeChainRead);
6057 vdFilterRetain(pFilter);
6058 }
6059 } while (0);
6060
6061 if (RT_UNLIKELY(fLockWrite))
6062 {
6063 rc2 = vdThreadFinishWrite(pDisk);
6064 AssertRC(rc2);
6065 }
6066
6067 if (RT_FAILURE(rc))
6068 {
6069 if (pFilter)
6070 RTMemFree(pFilter);
6071 }
6072
6073 LogFlowFunc(("returns %Rrc\n", rc));
6074 return rc;
6075}
6076
6077
6078VBOXDDU_DECL(int) VDCreateBase(PVDISK pDisk, const char *pszBackend,
6079 const char *pszFilename, uint64_t cbSize,
6080 unsigned uImageFlags, const char *pszComment,
6081 PCVDGEOMETRY pPCHSGeometry,
6082 PCVDGEOMETRY pLCHSGeometry,
6083 PCRTUUID pUuid, unsigned uOpenFlags,
6084 PVDINTERFACE pVDIfsImage,
6085 PVDINTERFACE pVDIfsOperation)
6086{
6087 int rc = VINF_SUCCESS;
6088 int rc2;
6089 bool fLockWrite = false, fLockRead = false;
6090 PVDIMAGE pImage = NULL;
6091 RTUUID uuid;
6092
6093 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",
6094 pDisk, pszBackend, pszFilename, cbSize, uImageFlags, pszComment,
6095 pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
6096 pPCHSGeometry->cSectors, pLCHSGeometry->cCylinders,
6097 pLCHSGeometry->cHeads, pLCHSGeometry->cSectors, pUuid,
6098 uOpenFlags, pVDIfsImage, pVDIfsOperation));
6099
6100 AssertPtrNullReturn(pVDIfsOperation, VERR_INVALID_PARAMETER);
6101 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
6102
6103 do
6104 {
6105 /** @todo r=bird: there is no particular reason why this validation has to be
6106 * done inside the do-break-while-goto-is-false loop. (pIfProgress is
6107 * only called on success.) Could just AssertMsgReturn, rather than
6108 * complicated AssertMsgBreakStmt. */
6109 /* sanity check */
6110 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
6111 AssertMsgBreakStmt(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature),
6112 rc = VERR_INVALID_MAGIC);
6113
6114 /* Check arguments. */
6115 AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
6116 ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
6117 rc = VERR_INVALID_PARAMETER);
6118 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
6119 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
6120 rc = VERR_INVALID_PARAMETER);
6121 AssertMsgBreakStmt(cbSize || (uImageFlags & VD_VMDK_IMAGE_FLAGS_RAWDISK),
6122 ("cbSize=%llu\n", cbSize),
6123 rc = VERR_INVALID_PARAMETER);
6124 if (cbSize % 512 && !(uImageFlags & VD_VMDK_IMAGE_FLAGS_RAWDISK))
6125 {
6126 rc = vdError(pDisk, VERR_VD_INVALID_SIZE, RT_SRC_POS,
6127 N_("VD: The given disk size %llu is not aligned on a sector boundary (512 bytes)"), cbSize);
6128 break;
6129 }
6130 AssertMsgBreakStmt( ((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0)
6131 || ((uImageFlags & (VD_IMAGE_FLAGS_FIXED | VD_IMAGE_FLAGS_DIFF)) != VD_IMAGE_FLAGS_FIXED),
6132 ("uImageFlags=%#x\n", uImageFlags),
6133 rc = VERR_INVALID_PARAMETER);
6134 AssertMsgBreakStmt( !(uImageFlags & VD_VMDK_IMAGE_FLAGS_RAWDISK)
6135 || !(uImageFlags & ~(VD_VMDK_IMAGE_FLAGS_RAWDISK | VD_IMAGE_FLAGS_FIXED)),
6136 ("uImageFlags=%#x\n", uImageFlags),
6137 rc = VERR_INVALID_PARAMETER);
6138 /* The PCHS geometry fields may be 0 to leave it for later. */
6139 AssertPtrBreakStmt(pPCHSGeometry, rc = VERR_INVALID_PARAMETER);
6140 AssertMsgBreakStmt( pPCHSGeometry->cHeads <= 16
6141 && pPCHSGeometry->cSectors <= 63,
6142 ("PCHS=%u/%u/%u\n", pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors),
6143 rc = VERR_INVALID_PARAMETER);
6144 /* The LCHS geometry fields may be 0 to leave it to later autodetection. */
6145 AssertPtrBreakStmt(pLCHSGeometry, rc = VERR_INVALID_PARAMETER);
6146 AssertMsgBreakStmt( pLCHSGeometry->cHeads <= 255
6147 && pLCHSGeometry->cSectors <= 63,
6148 ("LCHS=%u/%u/%u\n", pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors),
6149 rc = VERR_INVALID_PARAMETER);
6150 /* The UUID may be NULL. */
6151 AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
6152 ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
6153 rc = VERR_INVALID_PARAMETER);
6154 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
6155 ("uOpenFlags=%#x\n", uOpenFlags),
6156 rc = VERR_INVALID_PARAMETER);
6157
6158 /* Check state. Needs a temporary read lock. Holding the write lock
6159 * all the time would be blocking other activities for too long. */
6160 rc2 = vdThreadStartRead(pDisk);
6161 AssertRC(rc2);
6162 fLockRead = true;
6163 AssertMsgBreakStmt(pDisk->cImages == 0,
6164 ("Create base image cannot be done with other images open\n"),
6165 rc = VERR_VD_INVALID_STATE);
6166 rc2 = vdThreadFinishRead(pDisk);
6167 AssertRC(rc2);
6168 fLockRead = false;
6169
6170 /* Set up image descriptor. */
6171 pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
6172 if (!pImage)
6173 {
6174 rc = VERR_NO_MEMORY;
6175 break;
6176 }
6177 pImage->pszFilename = RTStrDup(pszFilename);
6178 if (!pImage->pszFilename)
6179 {
6180 rc = VERR_NO_MEMORY;
6181 break;
6182 }
6183 pImage->cbImage = VD_IMAGE_SIZE_UNINITIALIZED;
6184 pImage->VDIo.pDisk = pDisk;
6185 pImage->pVDIfsImage = pVDIfsImage;
6186
6187 /* Set up the I/O interface. */
6188 pImage->VDIo.pInterfaceIo = VDIfIoGet(pVDIfsImage);
6189 if (!pImage->VDIo.pInterfaceIo)
6190 {
6191 vdIfIoFallbackCallbacksSetup(&pImage->VDIo.VDIfIo);
6192 rc = VDInterfaceAdd(&pImage->VDIo.VDIfIo.Core, "VD_IO", VDINTERFACETYPE_IO,
6193 pDisk, sizeof(VDINTERFACEIO), &pVDIfsImage);
6194 pImage->VDIo.pInterfaceIo = &pImage->VDIo.VDIfIo;
6195 }
6196
6197 /* Set up the internal I/O interface. */
6198 AssertBreakStmt(!VDIfIoIntGet(pVDIfsImage), rc = VERR_INVALID_PARAMETER);
6199 vdIfIoIntCallbacksSetup(&pImage->VDIo.VDIfIoInt);
6200 rc = VDInterfaceAdd(&pImage->VDIo.VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
6201 &pImage->VDIo, sizeof(VDINTERFACEIOINT), &pImage->pVDIfsImage);
6202 AssertRC(rc);
6203
6204 rc = vdFindImageBackend(pszBackend, &pImage->Backend);
6205 if (RT_FAILURE(rc))
6206 break;
6207 if (!pImage->Backend)
6208 {
6209 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
6210 N_("VD: unknown backend name '%s'"), pszBackend);
6211 break;
6212 }
6213 if (!(pImage->Backend->uBackendCaps & ( VD_CAP_CREATE_FIXED
6214 | VD_CAP_CREATE_DYNAMIC)))
6215 {
6216 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
6217 N_("VD: backend '%s' cannot create base images"), pszBackend);
6218 break;
6219 }
6220 if ( ( (uImageFlags & VD_VMDK_IMAGE_FLAGS_SPLIT_2G)
6221 && !(pImage->Backend->uBackendCaps & VD_CAP_CREATE_SPLIT_2G))
6222 || ( (uImageFlags & ( VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED
6223 | VD_VMDK_IMAGE_FLAGS_RAWDISK))
6224 && RTStrICmp(pszBackend, "VMDK")))
6225 {
6226 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
6227 N_("VD: backend '%s' does not support the selected image variant"), pszBackend);
6228 break;
6229 }
6230
6231 /* Create UUID if the caller didn't specify one. */
6232 if (!pUuid)
6233 {
6234 rc = RTUuidCreate(&uuid);
6235 if (RT_FAILURE(rc))
6236 {
6237 rc = vdError(pDisk, rc, RT_SRC_POS,
6238 N_("VD: cannot generate UUID for image '%s'"),
6239 pszFilename);
6240 break;
6241 }
6242 pUuid = &uuid;
6243 }
6244
6245 pImage->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
6246 uImageFlags &= ~VD_IMAGE_FLAGS_DIFF;
6247 pImage->VDIo.fIgnoreFlush = (uOpenFlags & VD_OPEN_FLAGS_IGNORE_FLUSH) != 0;
6248 rc = pImage->Backend->pfnCreate(pImage->pszFilename, cbSize,
6249 uImageFlags, pszComment, pPCHSGeometry,
6250 pLCHSGeometry, pUuid,
6251 uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
6252 0, 99,
6253 pDisk->pVDIfsDisk,
6254 pImage->pVDIfsImage,
6255 pVDIfsOperation,
6256 pDisk->enmType,
6257 &pImage->pBackendData);
6258
6259 if (RT_SUCCESS(rc))
6260 {
6261 pImage->VDIo.pBackendData = pImage->pBackendData;
6262 pImage->uImageFlags = uImageFlags;
6263
6264 /* Force sane optimization settings. It's not worth avoiding writes
6265 * to fixed size images. The overhead would have almost no payback. */
6266 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
6267 pImage->uOpenFlags |= VD_OPEN_FLAGS_HONOR_SAME;
6268
6269 /* Lock disk for writing, as we modify pDisk information below. */
6270 rc2 = vdThreadStartWrite(pDisk);
6271 AssertRC(rc2);
6272 fLockWrite = true;
6273
6274 /** @todo optionally check UUIDs */
6275
6276 /* Re-check state, as the lock wasn't held and another image
6277 * creation call could have been done by another thread. */
6278 AssertMsgStmt(pDisk->cImages == 0,
6279 ("Create base image cannot be done with other images open\n"),
6280 rc = VERR_VD_INVALID_STATE);
6281 }
6282
6283 if (RT_SUCCESS(rc))
6284 {
6285 /* Cache disk information. */
6286 pDisk->cbSize = vdImageGetSize(pImage);
6287
6288 /* Cache PCHS geometry. */
6289 rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
6290 &pDisk->PCHSGeometry);
6291 if (RT_FAILURE(rc2))
6292 {
6293 pDisk->PCHSGeometry.cCylinders = 0;
6294 pDisk->PCHSGeometry.cHeads = 0;
6295 pDisk->PCHSGeometry.cSectors = 0;
6296 }
6297 else
6298 {
6299 /* Make sure the CHS geometry is properly clipped. */
6300 pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
6301 pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
6302 pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
6303 }
6304
6305 /* Cache LCHS geometry. */
6306 rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
6307 &pDisk->LCHSGeometry);
6308 if (RT_FAILURE(rc2))
6309 {
6310 pDisk->LCHSGeometry.cCylinders = 0;
6311 pDisk->LCHSGeometry.cHeads = 0;
6312 pDisk->LCHSGeometry.cSectors = 0;
6313 }
6314 else
6315 {
6316 /* Make sure the CHS geometry is properly clipped. */
6317 pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
6318 pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
6319 }
6320
6321 /* Image successfully opened, make it the last image. */
6322 vdAddImageToList(pDisk, pImage);
6323 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
6324 pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
6325 }
6326 else
6327 {
6328 /* Error detected, image may or may not be opened. Close and delete
6329 * image if it was opened. */
6330 if (pImage->pBackendData)
6331 {
6332 rc2 = pImage->Backend->pfnClose(pImage->pBackendData, true);
6333 AssertRC(rc2);
6334 pImage->pBackendData = NULL;
6335 }
6336 }
6337 } while (0);
6338
6339 if (RT_UNLIKELY(fLockWrite))
6340 {
6341 rc2 = vdThreadFinishWrite(pDisk);
6342 AssertRC(rc2);
6343 }
6344 else if (RT_UNLIKELY(fLockRead))
6345 {
6346 rc2 = vdThreadFinishRead(pDisk);
6347 AssertRC(rc2);
6348 }
6349
6350 if (RT_FAILURE(rc))
6351 {
6352 if (pImage)
6353 {
6354 if (pImage->pszFilename)
6355 RTStrFree(pImage->pszFilename);
6356 RTMemFree(pImage);
6357 }
6358 }
6359
6360 if (RT_SUCCESS(rc) && pIfProgress && pIfProgress->pfnProgress)
6361 pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
6362
6363 LogFlowFunc(("returns %Rrc\n", rc));
6364 return rc;
6365}
6366
6367
6368VBOXDDU_DECL(int) VDCreateDiff(PVDISK pDisk, const char *pszBackend,
6369 const char *pszFilename, unsigned uImageFlags,
6370 const char *pszComment, PCRTUUID pUuid,
6371 PCRTUUID pParentUuid, unsigned uOpenFlags,
6372 PVDINTERFACE pVDIfsImage,
6373 PVDINTERFACE pVDIfsOperation)
6374{
6375 int rc = VINF_SUCCESS;
6376 int rc2;
6377 bool fLockWrite = false, fLockRead = false;
6378 PVDIMAGE pImage = NULL;
6379 RTUUID uuid;
6380
6381 LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uImageFlags=%#x pszComment=\"%s\" Uuid=%RTuuid uOpenFlags=%#x pVDIfsImage=%#p pVDIfsOperation=%#p\n",
6382 pDisk, pszBackend, pszFilename, uImageFlags, pszComment, pUuid, uOpenFlags, pVDIfsImage, pVDIfsOperation));
6383
6384 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
6385
6386 do
6387 {
6388 /* sanity check */
6389 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
6390 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6391
6392 /* Check arguments. */
6393 AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
6394 ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
6395 rc = VERR_INVALID_PARAMETER);
6396 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
6397 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
6398 rc = VERR_INVALID_PARAMETER);
6399 AssertMsgBreakStmt((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0,
6400 ("uImageFlags=%#x\n", uImageFlags),
6401 rc = VERR_INVALID_PARAMETER);
6402 /* The UUID may be NULL. */
6403 AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
6404 ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
6405 rc = VERR_INVALID_PARAMETER);
6406 /* The parent UUID may be NULL. */
6407 AssertMsgBreakStmt(pParentUuid == NULL || VALID_PTR(pParentUuid),
6408 ("pParentUuid=%#p ParentUUID=%RTuuid\n", pParentUuid, pParentUuid),
6409 rc = VERR_INVALID_PARAMETER);
6410 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
6411 ("uOpenFlags=%#x\n", uOpenFlags),
6412 rc = VERR_INVALID_PARAMETER);
6413
6414 /* Check state. Needs a temporary read lock. Holding the write lock
6415 * all the time would be blocking other activities for too long. */
6416 rc2 = vdThreadStartRead(pDisk);
6417 AssertRC(rc2);
6418 fLockRead = true;
6419 AssertMsgBreakStmt(pDisk->cImages != 0,
6420 ("Create diff image cannot be done without other images open\n"),
6421 rc = VERR_VD_INVALID_STATE);
6422 rc2 = vdThreadFinishRead(pDisk);
6423 AssertRC(rc2);
6424 fLockRead = false;
6425
6426 /*
6427 * Destroy the current discard state first which might still have pending blocks
6428 * for the currently opened image which will be switched to readonly mode.
6429 */
6430 /* Lock disk for writing, as we modify pDisk information below. */
6431 rc2 = vdThreadStartWrite(pDisk);
6432 AssertRC(rc2);
6433 fLockWrite = true;
6434 rc = vdDiscardStateDestroy(pDisk);
6435 if (RT_FAILURE(rc))
6436 break;
6437 rc2 = vdThreadFinishWrite(pDisk);
6438 AssertRC(rc2);
6439 fLockWrite = false;
6440
6441 /* Set up image descriptor. */
6442 pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
6443 if (!pImage)
6444 {
6445 rc = VERR_NO_MEMORY;
6446 break;
6447 }
6448 pImage->pszFilename = RTStrDup(pszFilename);
6449 if (!pImage->pszFilename)
6450 {
6451 rc = VERR_NO_MEMORY;
6452 break;
6453 }
6454
6455 rc = vdFindImageBackend(pszBackend, &pImage->Backend);
6456 if (RT_FAILURE(rc))
6457 break;
6458 if (!pImage->Backend)
6459 {
6460 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
6461 N_("VD: unknown backend name '%s'"), pszBackend);
6462 break;
6463 }
6464 if ( !(pImage->Backend->uBackendCaps & VD_CAP_DIFF)
6465 || !(pImage->Backend->uBackendCaps & ( VD_CAP_CREATE_FIXED
6466 | VD_CAP_CREATE_DYNAMIC)))
6467 {
6468 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
6469 N_("VD: backend '%s' cannot create diff images"), pszBackend);
6470 break;
6471 }
6472
6473 pImage->cbImage = VD_IMAGE_SIZE_UNINITIALIZED;
6474 pImage->VDIo.pDisk = pDisk;
6475 pImage->pVDIfsImage = pVDIfsImage;
6476
6477 /* Set up the I/O interface. */
6478 pImage->VDIo.pInterfaceIo = VDIfIoGet(pVDIfsImage);
6479 if (!pImage->VDIo.pInterfaceIo)
6480 {
6481 vdIfIoFallbackCallbacksSetup(&pImage->VDIo.VDIfIo);
6482 rc = VDInterfaceAdd(&pImage->VDIo.VDIfIo.Core, "VD_IO", VDINTERFACETYPE_IO,
6483 pDisk, sizeof(VDINTERFACEIO), &pVDIfsImage);
6484 pImage->VDIo.pInterfaceIo = &pImage->VDIo.VDIfIo;
6485 }
6486
6487 /* Set up the internal I/O interface. */
6488 AssertBreakStmt(!VDIfIoIntGet(pVDIfsImage), rc = VERR_INVALID_PARAMETER);
6489 vdIfIoIntCallbacksSetup(&pImage->VDIo.VDIfIoInt);
6490 rc = VDInterfaceAdd(&pImage->VDIo.VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
6491 &pImage->VDIo, sizeof(VDINTERFACEIOINT), &pImage->pVDIfsImage);
6492 AssertRC(rc);
6493
6494 /* Create UUID if the caller didn't specify one. */
6495 if (!pUuid)
6496 {
6497 rc = RTUuidCreate(&uuid);
6498 if (RT_FAILURE(rc))
6499 {
6500 rc = vdError(pDisk, rc, RT_SRC_POS,
6501 N_("VD: cannot generate UUID for image '%s'"),
6502 pszFilename);
6503 break;
6504 }
6505 pUuid = &uuid;
6506 }
6507
6508 pImage->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
6509 pImage->VDIo.fIgnoreFlush = (uOpenFlags & VD_OPEN_FLAGS_IGNORE_FLUSH) != 0;
6510 uImageFlags |= VD_IMAGE_FLAGS_DIFF;
6511 rc = pImage->Backend->pfnCreate(pImage->pszFilename, pDisk->cbSize,
6512 uImageFlags | VD_IMAGE_FLAGS_DIFF,
6513 pszComment, &pDisk->PCHSGeometry,
6514 &pDisk->LCHSGeometry, pUuid,
6515 uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
6516 0, 99,
6517 pDisk->pVDIfsDisk,
6518 pImage->pVDIfsImage,
6519 pVDIfsOperation,
6520 pDisk->enmType,
6521 &pImage->pBackendData);
6522
6523 if (RT_SUCCESS(rc))
6524 {
6525 pImage->VDIo.pBackendData = pImage->pBackendData;
6526 pImage->uImageFlags = uImageFlags;
6527
6528 /* Lock disk for writing, as we modify pDisk information below. */
6529 rc2 = vdThreadStartWrite(pDisk);
6530 AssertRC(rc2);
6531 fLockWrite = true;
6532
6533 /* Switch previous image to read-only mode. */
6534 unsigned uOpenFlagsPrevImg;
6535 uOpenFlagsPrevImg = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pBackendData);
6536 if (!(uOpenFlagsPrevImg & VD_OPEN_FLAGS_READONLY))
6537 {
6538 uOpenFlagsPrevImg |= VD_OPEN_FLAGS_READONLY;
6539 rc = pDisk->pLast->Backend->pfnSetOpenFlags(pDisk->pLast->pBackendData, uOpenFlagsPrevImg);
6540 }
6541
6542 /** @todo optionally check UUIDs */
6543
6544 /* Re-check state, as the lock wasn't held and another image
6545 * creation call could have been done by another thread. */
6546 AssertMsgStmt(pDisk->cImages != 0,
6547 ("Create diff image cannot be done without other images open\n"),
6548 rc = VERR_VD_INVALID_STATE);
6549 }
6550
6551 if (RT_SUCCESS(rc))
6552 {
6553 RTUUID Uuid;
6554 RTTIMESPEC ts;
6555
6556 if (pParentUuid && !RTUuidIsNull(pParentUuid))
6557 {
6558 Uuid = *pParentUuid;
6559 pImage->Backend->pfnSetParentUuid(pImage->pBackendData, &Uuid);
6560 }
6561 else
6562 {
6563 rc2 = pDisk->pLast->Backend->pfnGetUuid(pDisk->pLast->pBackendData,
6564 &Uuid);
6565 if (RT_SUCCESS(rc2))
6566 pImage->Backend->pfnSetParentUuid(pImage->pBackendData, &Uuid);
6567 }
6568 rc2 = pDisk->pLast->Backend->pfnGetModificationUuid(pDisk->pLast->pBackendData,
6569 &Uuid);
6570 if (RT_SUCCESS(rc2))
6571 pImage->Backend->pfnSetParentModificationUuid(pImage->pBackendData,
6572 &Uuid);
6573 if (pDisk->pLast->Backend->pfnGetTimestamp)
6574 rc2 = pDisk->pLast->Backend->pfnGetTimestamp(pDisk->pLast->pBackendData,
6575 &ts);
6576 else
6577 rc2 = VERR_NOT_IMPLEMENTED;
6578 if (RT_SUCCESS(rc2) && pImage->Backend->pfnSetParentTimestamp)
6579 pImage->Backend->pfnSetParentTimestamp(pImage->pBackendData, &ts);
6580
6581 if (pImage->Backend->pfnSetParentFilename)
6582 rc2 = pImage->Backend->pfnSetParentFilename(pImage->pBackendData, pDisk->pLast->pszFilename);
6583 }
6584
6585 if (RT_SUCCESS(rc))
6586 {
6587 /* Image successfully opened, make it the last image. */
6588 vdAddImageToList(pDisk, pImage);
6589 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
6590 pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
6591 }
6592 else
6593 {
6594 /* Error detected, but image opened. Close and delete image. */
6595 rc2 = pImage->Backend->pfnClose(pImage->pBackendData, true);
6596 AssertRC(rc2);
6597 pImage->pBackendData = NULL;
6598 }
6599 } while (0);
6600
6601 if (RT_UNLIKELY(fLockWrite))
6602 {
6603 rc2 = vdThreadFinishWrite(pDisk);
6604 AssertRC(rc2);
6605 }
6606 else if (RT_UNLIKELY(fLockRead))
6607 {
6608 rc2 = vdThreadFinishRead(pDisk);
6609 AssertRC(rc2);
6610 }
6611
6612 if (RT_FAILURE(rc))
6613 {
6614 if (pImage)
6615 {
6616 if (pImage->pszFilename)
6617 RTStrFree(pImage->pszFilename);
6618 RTMemFree(pImage);
6619 }
6620 }
6621
6622 if (RT_SUCCESS(rc) && pIfProgress && pIfProgress->pfnProgress)
6623 pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
6624
6625 LogFlowFunc(("returns %Rrc\n", rc));
6626 return rc;
6627}
6628
6629
6630VBOXDDU_DECL(int) VDCreateCache(PVDISK pDisk, const char *pszBackend,
6631 const char *pszFilename, uint64_t cbSize,
6632 unsigned uImageFlags, const char *pszComment,
6633 PCRTUUID pUuid, unsigned uOpenFlags,
6634 PVDINTERFACE pVDIfsCache, PVDINTERFACE pVDIfsOperation)
6635{
6636 int rc = VINF_SUCCESS;
6637 int rc2;
6638 bool fLockWrite = false, fLockRead = false;
6639 PVDCACHE pCache = NULL;
6640 RTUUID uuid;
6641
6642 LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" Uuid=%RTuuid uOpenFlags=%#x pVDIfsImage=%#p pVDIfsOperation=%#p\n",
6643 pDisk, pszBackend, pszFilename, cbSize, uImageFlags, pszComment, pUuid, uOpenFlags, pVDIfsCache, pVDIfsOperation));
6644
6645 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
6646
6647 do
6648 {
6649 /* sanity check */
6650 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
6651 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6652
6653 /* Check arguments. */
6654 AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
6655 ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
6656 rc = VERR_INVALID_PARAMETER);
6657 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
6658 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
6659 rc = VERR_INVALID_PARAMETER);
6660 AssertMsgBreakStmt(cbSize,
6661 ("cbSize=%llu\n", cbSize),
6662 rc = VERR_INVALID_PARAMETER);
6663 AssertMsgBreakStmt((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0,
6664 ("uImageFlags=%#x\n", uImageFlags),
6665 rc = VERR_INVALID_PARAMETER);
6666 /* The UUID may be NULL. */
6667 AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
6668 ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
6669 rc = VERR_INVALID_PARAMETER);
6670 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
6671 ("uOpenFlags=%#x\n", uOpenFlags),
6672 rc = VERR_INVALID_PARAMETER);
6673
6674 /* Check state. Needs a temporary read lock. Holding the write lock
6675 * all the time would be blocking other activities for too long. */
6676 rc2 = vdThreadStartRead(pDisk);
6677 AssertRC(rc2);
6678 fLockRead = true;
6679 AssertMsgBreakStmt(!pDisk->pCache,
6680 ("Create cache image cannot be done with a cache already attached\n"),
6681 rc = VERR_VD_CACHE_ALREADY_EXISTS);
6682 rc2 = vdThreadFinishRead(pDisk);
6683 AssertRC(rc2);
6684 fLockRead = false;
6685
6686 /* Set up image descriptor. */
6687 pCache = (PVDCACHE)RTMemAllocZ(sizeof(VDCACHE));
6688 if (!pCache)
6689 {
6690 rc = VERR_NO_MEMORY;
6691 break;
6692 }
6693 pCache->pszFilename = RTStrDup(pszFilename);
6694 if (!pCache->pszFilename)
6695 {
6696 rc = VERR_NO_MEMORY;
6697 break;
6698 }
6699
6700 rc = vdFindCacheBackend(pszBackend, &pCache->Backend);
6701 if (RT_FAILURE(rc))
6702 break;
6703 if (!pCache->Backend)
6704 {
6705 rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
6706 N_("VD: unknown backend name '%s'"), pszBackend);
6707 break;
6708 }
6709
6710 pCache->VDIo.pDisk = pDisk;
6711 pCache->pVDIfsCache = pVDIfsCache;
6712
6713 /* Set up the I/O interface. */
6714 pCache->VDIo.pInterfaceIo = VDIfIoGet(pVDIfsCache);
6715 if (!pCache->VDIo.pInterfaceIo)
6716 {
6717 vdIfIoFallbackCallbacksSetup(&pCache->VDIo.VDIfIo);
6718 rc = VDInterfaceAdd(&pCache->VDIo.VDIfIo.Core, "VD_IO", VDINTERFACETYPE_IO,
6719 pDisk, sizeof(VDINTERFACEIO), &pVDIfsCache);
6720 pCache->VDIo.pInterfaceIo = &pCache->VDIo.VDIfIo;
6721 }
6722
6723 /* Set up the internal I/O interface. */
6724 AssertBreakStmt(!VDIfIoIntGet(pVDIfsCache), rc = VERR_INVALID_PARAMETER);
6725 vdIfIoIntCallbacksSetup(&pCache->VDIo.VDIfIoInt);
6726 rc = VDInterfaceAdd(&pCache->VDIo.VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
6727 &pCache->VDIo, sizeof(VDINTERFACEIOINT), &pCache->pVDIfsCache);
6728 AssertRC(rc);
6729
6730 /* Create UUID if the caller didn't specify one. */
6731 if (!pUuid)
6732 {
6733 rc = RTUuidCreate(&uuid);
6734 if (RT_FAILURE(rc))
6735 {
6736 rc = vdError(pDisk, rc, RT_SRC_POS,
6737 N_("VD: cannot generate UUID for image '%s'"),
6738 pszFilename);
6739 break;
6740 }
6741 pUuid = &uuid;
6742 }
6743
6744 pCache->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
6745 pCache->VDIo.fIgnoreFlush = (uOpenFlags & VD_OPEN_FLAGS_IGNORE_FLUSH) != 0;
6746 rc = pCache->Backend->pfnCreate(pCache->pszFilename, cbSize,
6747 uImageFlags,
6748 pszComment, pUuid,
6749 uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
6750 0, 99,
6751 pDisk->pVDIfsDisk,
6752 pCache->pVDIfsCache,
6753 pVDIfsOperation,
6754 &pCache->pBackendData);
6755
6756 if (RT_SUCCESS(rc))
6757 {
6758 /* Lock disk for writing, as we modify pDisk information below. */
6759 rc2 = vdThreadStartWrite(pDisk);
6760 AssertRC(rc2);
6761 fLockWrite = true;
6762
6763 pCache->VDIo.pBackendData = pCache->pBackendData;
6764
6765 /* Re-check state, as the lock wasn't held and another image
6766 * creation call could have been done by another thread. */
6767 AssertMsgStmt(!pDisk->pCache,
6768 ("Create cache image cannot be done with another cache open\n"),
6769 rc = VERR_VD_CACHE_ALREADY_EXISTS);
6770 }
6771
6772 if ( RT_SUCCESS(rc)
6773 && pDisk->pLast)
6774 {
6775 RTUUID UuidModification;
6776
6777 /* Set same modification Uuid as the last image. */
6778 rc = pDisk->pLast->Backend->pfnGetModificationUuid(pDisk->pLast->pBackendData,
6779 &UuidModification);
6780 if (RT_SUCCESS(rc))
6781 {
6782 rc = pCache->Backend->pfnSetModificationUuid(pCache->pBackendData,
6783 &UuidModification);
6784 }
6785
6786 if (rc == VERR_NOT_SUPPORTED)
6787 rc = VINF_SUCCESS;
6788 }
6789
6790 if (RT_SUCCESS(rc))
6791 {
6792 /* Cache successfully created. */
6793 pDisk->pCache = pCache;
6794 }
6795 else
6796 {
6797 /* Error detected, but image opened. Close and delete image. */
6798 rc2 = pCache->Backend->pfnClose(pCache->pBackendData, true);
6799 AssertRC(rc2);
6800 pCache->pBackendData = NULL;
6801 }
6802 } while (0);
6803
6804 if (RT_UNLIKELY(fLockWrite))
6805 {
6806 rc2 = vdThreadFinishWrite(pDisk);
6807 AssertRC(rc2);
6808 }
6809 else if (RT_UNLIKELY(fLockRead))
6810 {
6811 rc2 = vdThreadFinishRead(pDisk);
6812 AssertRC(rc2);
6813 }
6814
6815 if (RT_FAILURE(rc))
6816 {
6817 if (pCache)
6818 {
6819 if (pCache->pszFilename)
6820 RTStrFree(pCache->pszFilename);
6821 RTMemFree(pCache);
6822 }
6823 }
6824
6825 if (RT_SUCCESS(rc) && pIfProgress && pIfProgress->pfnProgress)
6826 pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
6827
6828 LogFlowFunc(("returns %Rrc\n", rc));
6829 return rc;
6830}
6831
6832
6833VBOXDDU_DECL(int) VDMerge(PVDISK pDisk, unsigned nImageFrom,
6834 unsigned nImageTo, PVDINTERFACE pVDIfsOperation)
6835{
6836 int rc = VINF_SUCCESS;
6837 int rc2;
6838 bool fLockWrite = false, fLockRead = false;
6839 void *pvBuf = NULL;
6840
6841 LogFlowFunc(("pDisk=%#p nImageFrom=%u nImageTo=%u pVDIfsOperation=%#p\n",
6842 pDisk, nImageFrom, nImageTo, pVDIfsOperation));
6843
6844 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
6845
6846 do
6847 {
6848 /* sanity check */
6849 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
6850 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
6851
6852 /* For simplicity reasons lock for writing as the image reopen below
6853 * might need it. After all the reopen is usually needed. */
6854 rc2 = vdThreadStartWrite(pDisk);
6855 AssertRC(rc2);
6856 fLockWrite = true;
6857 PVDIMAGE pImageFrom = vdGetImageByNumber(pDisk, nImageFrom);
6858 PVDIMAGE pImageTo = vdGetImageByNumber(pDisk, nImageTo);
6859 if (!pImageFrom || !pImageTo)
6860 {
6861 rc = VERR_VD_IMAGE_NOT_FOUND;
6862 break;
6863 }
6864 AssertBreakStmt(pImageFrom != pImageTo, rc = VERR_INVALID_PARAMETER);
6865
6866 /* Make sure destination image is writable. */
6867 unsigned uOpenFlags = pImageTo->Backend->pfnGetOpenFlags(pImageTo->pBackendData);
6868 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
6869 {
6870 /*
6871 * Clear skip consistency checks because the image is made writable now and
6872 * skipping consistency checks is only possible for readonly images.
6873 */
6874 uOpenFlags &= ~(VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS);
6875 rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pBackendData,
6876 uOpenFlags);
6877 if (RT_FAILURE(rc))
6878 break;
6879 }
6880
6881 /* Get size of destination image. */
6882 uint64_t cbSize = vdImageGetSize(pImageTo);
6883 rc2 = vdThreadFinishWrite(pDisk);
6884 AssertRC(rc2);
6885 fLockWrite = false;
6886
6887 /* Allocate tmp buffer. */
6888 pvBuf = RTMemTmpAlloc(VD_MERGE_BUFFER_SIZE);
6889 if (!pvBuf)
6890 {
6891 rc = VERR_NO_MEMORY;
6892 break;
6893 }
6894
6895 /* Merging is done directly on the images itself. This potentially
6896 * causes trouble if the disk is full in the middle of operation. */
6897 if (nImageFrom < nImageTo)
6898 {
6899 /* Merge parent state into child. This means writing all not
6900 * allocated blocks in the destination image which are allocated in
6901 * the images to be merged. */
6902 uint64_t uOffset = 0;
6903 uint64_t cbRemaining = cbSize;
6904
6905 do
6906 {
6907 size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
6908 RTSGSEG SegmentBuf;
6909 RTSGBUF SgBuf;
6910 VDIOCTX IoCtx;
6911
6912 SegmentBuf.pvSeg = pvBuf;
6913 SegmentBuf.cbSeg = VD_MERGE_BUFFER_SIZE;
6914 RTSgBufInit(&SgBuf, &SegmentBuf, 1);
6915 vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_READ, 0, 0, NULL,
6916 &SgBuf, NULL, NULL, VDIOCTX_FLAGS_SYNC);
6917
6918 /* Need to hold the write lock during a read-write operation. */
6919 rc2 = vdThreadStartWrite(pDisk);
6920 AssertRC(rc2);
6921 fLockWrite = true;
6922
6923 rc = pImageTo->Backend->pfnRead(pImageTo->pBackendData,
6924 uOffset, cbThisRead,
6925 &IoCtx, &cbThisRead);
6926 if (rc == VERR_VD_BLOCK_FREE)
6927 {
6928 /* Search for image with allocated block. Do not attempt to
6929 * read more than the previous reads marked as valid.
6930 * Otherwise this would return stale data when different
6931 * block sizes are used for the images. */
6932 for (PVDIMAGE pCurrImage = pImageTo->pPrev;
6933 pCurrImage != NULL && pCurrImage != pImageFrom->pPrev && rc == VERR_VD_BLOCK_FREE;
6934 pCurrImage = pCurrImage->pPrev)
6935 {
6936 /*
6937 * Skip reading when offset exceeds image size which can happen when the target is
6938 * bigger than the source.
6939 */
6940 uint64_t cbImage = vdImageGetSize(pCurrImage);
6941 if (uOffset < cbImage)
6942 {
6943 cbThisRead = RT_MIN(cbThisRead, cbImage - uOffset);
6944 rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
6945 uOffset, cbThisRead,
6946 &IoCtx, &cbThisRead);
6947 }
6948 else
6949 rc = VERR_VD_BLOCK_FREE;
6950 }
6951
6952 if (rc != VERR_VD_BLOCK_FREE)
6953 {
6954 if (RT_FAILURE(rc))
6955 break;
6956 /* Updating the cache is required because this might be a live merge. */
6957 rc = vdWriteHelperEx(pDisk, pImageTo, pImageFrom->pPrev,
6958 uOffset, pvBuf, cbThisRead,
6959 VDIOCTX_FLAGS_READ_UPDATE_CACHE, 0);
6960 if (RT_FAILURE(rc))
6961 break;
6962 }
6963 else
6964 rc = VINF_SUCCESS;
6965 }
6966 else if (RT_FAILURE(rc))
6967 break;
6968
6969 rc2 = vdThreadFinishWrite(pDisk);
6970 AssertRC(rc2);
6971 fLockWrite = false;
6972
6973 uOffset += cbThisRead;
6974 cbRemaining -= cbThisRead;
6975
6976 if (pIfProgress && pIfProgress->pfnProgress)
6977 {
6978 /** @todo r=klaus: this can update the progress to the same
6979 * percentage over and over again if the image format makes
6980 * relatively small increments. */
6981 rc = pIfProgress->pfnProgress(pIfProgress->Core.pvUser,
6982 uOffset * 99 / cbSize);
6983 if (RT_FAILURE(rc))
6984 break;
6985 }
6986 } while (uOffset < cbSize);
6987 }
6988 else
6989 {
6990 /*
6991 * We may need to update the parent uuid of the child coming after
6992 * the last image to be merged. We have to reopen it read/write.
6993 *
6994 * This is done before we do the actual merge to prevent an
6995 * inconsistent chain if the mode change fails for some reason.
6996 */
6997 if (pImageFrom->pNext)
6998 {
6999 PVDIMAGE pImageChild = pImageFrom->pNext;
7000
7001 /* Take the write lock. */
7002 rc2 = vdThreadStartWrite(pDisk);
7003 AssertRC(rc2);
7004 fLockWrite = true;
7005
7006 /* We need to open the image in read/write mode. */
7007 uOpenFlags = pImageChild->Backend->pfnGetOpenFlags(pImageChild->pBackendData);
7008
7009 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
7010 {
7011 uOpenFlags &= ~VD_OPEN_FLAGS_READONLY;
7012 rc = pImageChild->Backend->pfnSetOpenFlags(pImageChild->pBackendData,
7013 uOpenFlags);
7014 if (RT_FAILURE(rc))
7015 break;
7016 }
7017
7018 rc2 = vdThreadFinishWrite(pDisk);
7019 AssertRC(rc2);
7020 fLockWrite = false;
7021 }
7022
7023 /* If the merge is from the last image we have to relay all writes
7024 * to the merge destination as well, so that concurrent writes
7025 * (in case of a live merge) are handled correctly. */
7026 if (!pImageFrom->pNext)
7027 {
7028 /* Take the write lock. */
7029 rc2 = vdThreadStartWrite(pDisk);
7030 AssertRC(rc2);
7031 fLockWrite = true;
7032
7033 pDisk->pImageRelay = pImageTo;
7034
7035 rc2 = vdThreadFinishWrite(pDisk);
7036 AssertRC(rc2);
7037 fLockWrite = false;
7038 }
7039
7040 /* Merge child state into parent. This means writing all blocks
7041 * which are allocated in the image up to the source image to the
7042 * destination image. */
7043 unsigned uProgressOld = 0;
7044 uint64_t uOffset = 0;
7045 uint64_t cbRemaining = cbSize;
7046 do
7047 {
7048 size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
7049 RTSGSEG SegmentBuf;
7050 RTSGBUF SgBuf;
7051 VDIOCTX IoCtx;
7052
7053 rc = VERR_VD_BLOCK_FREE;
7054
7055 SegmentBuf.pvSeg = pvBuf;
7056 SegmentBuf.cbSeg = VD_MERGE_BUFFER_SIZE;
7057 RTSgBufInit(&SgBuf, &SegmentBuf, 1);
7058 vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_READ, 0, 0, NULL,
7059 &SgBuf, NULL, NULL, VDIOCTX_FLAGS_SYNC);
7060
7061 /* Need to hold the write lock during a read-write operation. */
7062 rc2 = vdThreadStartWrite(pDisk);
7063 AssertRC(rc2);
7064 fLockWrite = true;
7065
7066 /* Search for image with allocated block. Do not attempt to
7067 * read more than the previous reads marked as valid. Otherwise
7068 * this would return stale data when different block sizes are
7069 * used for the images. */
7070 for (PVDIMAGE pCurrImage = pImageFrom;
7071 pCurrImage != NULL && pCurrImage != pImageTo && rc == VERR_VD_BLOCK_FREE;
7072 pCurrImage = pCurrImage->pPrev)
7073 {
7074 /*
7075 * Skip reading when offset exceeds image size which can happen when the target is
7076 * bigger than the source.
7077 */
7078 uint64_t cbImage = vdImageGetSize(pCurrImage);
7079 if (uOffset < cbImage)
7080 {
7081 cbThisRead = RT_MIN(cbThisRead, cbImage - uOffset);
7082 rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
7083 uOffset, cbThisRead,
7084 &IoCtx, &cbThisRead);
7085 }
7086 else
7087 rc = VERR_VD_BLOCK_FREE;
7088 }
7089
7090 if (rc != VERR_VD_BLOCK_FREE)
7091 {
7092 if (RT_FAILURE(rc))
7093 break;
7094 rc = vdWriteHelper(pDisk, pImageTo, uOffset, pvBuf,
7095 cbThisRead, VDIOCTX_FLAGS_READ_UPDATE_CACHE);
7096 if (RT_FAILURE(rc))
7097 break;
7098 }
7099 else
7100 rc = VINF_SUCCESS;
7101
7102 rc2 = vdThreadFinishWrite(pDisk);
7103 AssertRC(rc2);
7104 fLockWrite = false;
7105
7106 uOffset += cbThisRead;
7107 cbRemaining -= cbThisRead;
7108
7109 unsigned uProgressNew = uOffset * 99 / cbSize;
7110 if (uProgressNew != uProgressOld)
7111 {
7112 uProgressOld = uProgressNew;
7113
7114 if (pIfProgress && pIfProgress->pfnProgress)
7115 {
7116 rc = pIfProgress->pfnProgress(pIfProgress->Core.pvUser,
7117 uProgressOld);
7118 if (RT_FAILURE(rc))
7119 break;
7120 }
7121 }
7122
7123 } while (uOffset < cbSize);
7124
7125 /* In case we set up a "write proxy" image above we must clear
7126 * this again now to prevent stray writes. Failure or not. */
7127 if (!pImageFrom->pNext)
7128 {
7129 /* Take the write lock. */
7130 rc2 = vdThreadStartWrite(pDisk);
7131 AssertRC(rc2);
7132 fLockWrite = true;
7133
7134 pDisk->pImageRelay = NULL;
7135
7136 rc2 = vdThreadFinishWrite(pDisk);
7137 AssertRC(rc2);
7138 fLockWrite = false;
7139 }
7140 }
7141
7142 /*
7143 * Leave in case of an error to avoid corrupted data in the image chain
7144 * (includes cancelling the operation by the user).
7145 */
7146 if (RT_FAILURE(rc))
7147 break;
7148
7149 /* Need to hold the write lock while finishing the merge. */
7150 rc2 = vdThreadStartWrite(pDisk);
7151 AssertRC(rc2);
7152 fLockWrite = true;
7153
7154 /* Update parent UUID so that image chain is consistent.
7155 * The two attempts work around the problem that some backends
7156 * (e.g. iSCSI) do not support UUIDs, so we exploit the fact that
7157 * so far there can only be one such image in the chain. */
7158 /** @todo needs a better long-term solution, passing the UUID
7159 * knowledge from the caller or some such */
7160 RTUUID Uuid;
7161 PVDIMAGE pImageChild = NULL;
7162 if (nImageFrom < nImageTo)
7163 {
7164 if (pImageFrom->pPrev)
7165 {
7166 /* plan A: ask the parent itself for its UUID */
7167 rc = pImageFrom->pPrev->Backend->pfnGetUuid(pImageFrom->pPrev->pBackendData,
7168 &Uuid);
7169 if (RT_FAILURE(rc))
7170 {
7171 /* plan B: ask the child of the parent for parent UUID */
7172 rc = pImageFrom->Backend->pfnGetParentUuid(pImageFrom->pBackendData,
7173 &Uuid);
7174 }
7175 AssertRC(rc);
7176 }
7177 else
7178 RTUuidClear(&Uuid);
7179 rc = pImageTo->Backend->pfnSetParentUuid(pImageTo->pBackendData,
7180 &Uuid);
7181 AssertRC(rc);
7182 }
7183 else
7184 {
7185 /* Update the parent uuid of the child of the last merged image. */
7186 if (pImageFrom->pNext)
7187 {
7188 /* plan A: ask the parent itself for its UUID */
7189 rc = pImageTo->Backend->pfnGetUuid(pImageTo->pBackendData,
7190 &Uuid);
7191 if (RT_FAILURE(rc))
7192 {
7193 /* plan B: ask the child of the parent for parent UUID */
7194 rc = pImageTo->pNext->Backend->pfnGetParentUuid(pImageTo->pNext->pBackendData,
7195 &Uuid);
7196 }
7197 AssertRC(rc);
7198
7199 rc = pImageFrom->Backend->pfnSetParentUuid(pImageFrom->pNext->pBackendData,
7200 &Uuid);
7201 AssertRC(rc);
7202
7203 pImageChild = pImageFrom->pNext;
7204 }
7205 }
7206
7207 /* Delete the no longer needed images. */
7208 PVDIMAGE pImg = pImageFrom, pTmp;
7209 while (pImg != pImageTo)
7210 {
7211 if (nImageFrom < nImageTo)
7212 pTmp = pImg->pNext;
7213 else
7214 pTmp = pImg->pPrev;
7215 vdRemoveImageFromList(pDisk, pImg);
7216 pImg->Backend->pfnClose(pImg->pBackendData, true);
7217 RTStrFree(pImg->pszFilename);
7218 RTMemFree(pImg);
7219 pImg = pTmp;
7220 }
7221
7222 /* Make sure destination image is back to read only if necessary. */
7223 if (pImageTo != pDisk->pLast)
7224 {
7225 uOpenFlags = pImageTo->Backend->pfnGetOpenFlags(pImageTo->pBackendData);
7226 uOpenFlags |= VD_OPEN_FLAGS_READONLY;
7227 rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pBackendData,
7228 uOpenFlags);
7229 if (RT_FAILURE(rc))
7230 break;
7231 }
7232
7233 /*
7234 * Make sure the child is readonly
7235 * for the child -> parent merge direction
7236 * if necessary.
7237 */
7238 if ( nImageFrom > nImageTo
7239 && pImageChild
7240 && pImageChild != pDisk->pLast)
7241 {
7242 uOpenFlags = pImageChild->Backend->pfnGetOpenFlags(pImageChild->pBackendData);
7243 uOpenFlags |= VD_OPEN_FLAGS_READONLY;
7244 rc = pImageChild->Backend->pfnSetOpenFlags(pImageChild->pBackendData,
7245 uOpenFlags);
7246 if (RT_FAILURE(rc))
7247 break;
7248 }
7249 } while (0);
7250
7251 if (RT_UNLIKELY(fLockWrite))
7252 {
7253 rc2 = vdThreadFinishWrite(pDisk);
7254 AssertRC(rc2);
7255 }
7256 else if (RT_UNLIKELY(fLockRead))
7257 {
7258 rc2 = vdThreadFinishRead(pDisk);
7259 AssertRC(rc2);
7260 }
7261
7262 if (pvBuf)
7263 RTMemTmpFree(pvBuf);
7264
7265 if (RT_SUCCESS(rc) && pIfProgress && pIfProgress->pfnProgress)
7266 pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
7267
7268 LogFlowFunc(("returns %Rrc\n", rc));
7269 return rc;
7270}
7271
7272
7273VBOXDDU_DECL(int) VDCopyEx(PVDISK pDiskFrom, unsigned nImage, PVDISK pDiskTo,
7274 const char *pszBackend, const char *pszFilename,
7275 bool fMoveByRename, uint64_t cbSize,
7276 unsigned nImageFromSame, unsigned nImageToSame,
7277 unsigned uImageFlags, PCRTUUID pDstUuid,
7278 unsigned uOpenFlags, PVDINTERFACE pVDIfsOperation,
7279 PVDINTERFACE pDstVDIfsImage,
7280 PVDINTERFACE pDstVDIfsOperation)
7281{
7282 int rc = VINF_SUCCESS;
7283 int rc2;
7284 bool fLockReadFrom = false, fLockWriteFrom = false, fLockWriteTo = false;
7285 PVDIMAGE pImageTo = NULL;
7286
7287 LogFlowFunc(("pDiskFrom=%#p nImage=%u pDiskTo=%#p pszBackend=\"%s\" pszFilename=\"%s\" fMoveByRename=%d cbSize=%llu nImageFromSame=%u nImageToSame=%u uImageFlags=%#x pDstUuid=%#p uOpenFlags=%#x pVDIfsOperation=%#p pDstVDIfsImage=%#p pDstVDIfsOperation=%#p\n",
7288 pDiskFrom, nImage, pDiskTo, pszBackend, pszFilename, fMoveByRename, cbSize, nImageFromSame, nImageToSame, uImageFlags, pDstUuid, uOpenFlags, pVDIfsOperation, pDstVDIfsImage, pDstVDIfsOperation));
7289
7290 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
7291 PVDINTERFACEPROGRESS pDstIfProgress = VDIfProgressGet(pDstVDIfsOperation);
7292
7293 do {
7294 /* Check arguments. */
7295 AssertMsgBreakStmt(VALID_PTR(pDiskFrom), ("pDiskFrom=%#p\n", pDiskFrom),
7296 rc = VERR_INVALID_PARAMETER);
7297 AssertMsg(pDiskFrom->u32Signature == VDISK_SIGNATURE,
7298 ("u32Signature=%08x\n", pDiskFrom->u32Signature));
7299
7300 rc2 = vdThreadStartRead(pDiskFrom);
7301 AssertRC(rc2);
7302 fLockReadFrom = true;
7303 PVDIMAGE pImageFrom = vdGetImageByNumber(pDiskFrom, nImage);
7304 AssertPtrBreakStmt(pImageFrom, rc = VERR_VD_IMAGE_NOT_FOUND);
7305 AssertMsgBreakStmt(VALID_PTR(pDiskTo), ("pDiskTo=%#p\n", pDiskTo),
7306 rc = VERR_INVALID_PARAMETER);
7307 AssertMsg(pDiskTo->u32Signature == VDISK_SIGNATURE,
7308 ("u32Signature=%08x\n", pDiskTo->u32Signature));
7309 AssertMsgBreakStmt( (nImageFromSame < nImage || nImageFromSame == VD_IMAGE_CONTENT_UNKNOWN)
7310 && (nImageToSame < pDiskTo->cImages || nImageToSame == VD_IMAGE_CONTENT_UNKNOWN)
7311 && ( (nImageFromSame == VD_IMAGE_CONTENT_UNKNOWN && nImageToSame == VD_IMAGE_CONTENT_UNKNOWN)
7312 || (nImageFromSame != VD_IMAGE_CONTENT_UNKNOWN && nImageToSame != VD_IMAGE_CONTENT_UNKNOWN)),
7313 ("nImageFromSame=%u nImageToSame=%u\n", nImageFromSame, nImageToSame),
7314 rc = VERR_INVALID_PARAMETER);
7315
7316 /* Move the image. */
7317 if (pDiskFrom == pDiskTo)
7318 {
7319 /* Rename only works when backends are the same, are file based
7320 * and the rename method is implemented. */
7321 if ( fMoveByRename
7322 && !RTStrICmp(pszBackend, pImageFrom->Backend->pszBackendName)
7323 && pImageFrom->Backend->uBackendCaps & VD_CAP_FILE
7324 && pImageFrom->Backend->pfnRename)
7325 {
7326 rc2 = vdThreadFinishRead(pDiskFrom);
7327 AssertRC(rc2);
7328 fLockReadFrom = false;
7329
7330 rc2 = vdThreadStartWrite(pDiskFrom);
7331 AssertRC(rc2);
7332 fLockWriteFrom = true;
7333 rc = pImageFrom->Backend->pfnRename(pImageFrom->pBackendData, pszFilename ? pszFilename : pImageFrom->pszFilename);
7334 break;
7335 }
7336
7337 /** @todo Moving (including shrinking/growing) of the image is
7338 * requested, but the rename attempt failed or it wasn't possible.
7339 * Must now copy image to temp location. */
7340 AssertReleaseMsgFailed(("VDCopy: moving by copy/delete not implemented\n"));
7341 }
7342
7343 /* pszFilename is allowed to be NULL, as this indicates copy to the existing image. */
7344 AssertMsgBreakStmt(pszFilename == NULL || (VALID_PTR(pszFilename) && *pszFilename),
7345 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
7346 rc = VERR_INVALID_PARAMETER);
7347
7348 uint64_t cbSizeFrom;
7349 cbSizeFrom = vdImageGetSize(pImageFrom);
7350 if (cbSizeFrom == 0)
7351 {
7352 rc = VERR_VD_VALUE_NOT_FOUND;
7353 break;
7354 }
7355
7356 VDGEOMETRY PCHSGeometryFrom = {0, 0, 0};
7357 VDGEOMETRY LCHSGeometryFrom = {0, 0, 0};
7358 pImageFrom->Backend->pfnGetPCHSGeometry(pImageFrom->pBackendData, &PCHSGeometryFrom);
7359 pImageFrom->Backend->pfnGetLCHSGeometry(pImageFrom->pBackendData, &LCHSGeometryFrom);
7360
7361 RTUUID ImageUuid, ImageModificationUuid;
7362 if (pDiskFrom != pDiskTo)
7363 {
7364 if (pDstUuid)
7365 ImageUuid = *pDstUuid;
7366 else
7367 RTUuidCreate(&ImageUuid);
7368 }
7369 else
7370 {
7371 rc = pImageFrom->Backend->pfnGetUuid(pImageFrom->pBackendData, &ImageUuid);
7372 if (RT_FAILURE(rc))
7373 RTUuidCreate(&ImageUuid);
7374 }
7375 rc = pImageFrom->Backend->pfnGetModificationUuid(pImageFrom->pBackendData, &ImageModificationUuid);
7376 if (RT_FAILURE(rc))
7377 RTUuidClear(&ImageModificationUuid);
7378
7379 char szComment[1024];
7380 rc = pImageFrom->Backend->pfnGetComment(pImageFrom->pBackendData, szComment, sizeof(szComment));
7381 if (RT_FAILURE(rc))
7382 szComment[0] = '\0';
7383 else
7384 szComment[sizeof(szComment) - 1] = '\0';
7385
7386 rc2 = vdThreadFinishRead(pDiskFrom);
7387 AssertRC(rc2);
7388 fLockReadFrom = false;
7389
7390 rc2 = vdThreadStartRead(pDiskTo);
7391 AssertRC(rc2);
7392 unsigned cImagesTo = pDiskTo->cImages;
7393 rc2 = vdThreadFinishRead(pDiskTo);
7394 AssertRC(rc2);
7395
7396 if (pszFilename)
7397 {
7398 if (cbSize == 0)
7399 cbSize = cbSizeFrom;
7400
7401 /* Create destination image with the properties of source image. */
7402 /** @todo replace the VDCreateDiff/VDCreateBase calls by direct
7403 * calls to the backend. Unifies the code and reduces the API
7404 * dependencies. Would also make the synchronization explicit. */
7405 if (cImagesTo > 0)
7406 {
7407 rc = VDCreateDiff(pDiskTo, pszBackend, pszFilename,
7408 uImageFlags, szComment, &ImageUuid,
7409 NULL /* pParentUuid */,
7410 uOpenFlags & ~VD_OPEN_FLAGS_READONLY,
7411 pDstVDIfsImage, NULL);
7412
7413 rc2 = vdThreadStartWrite(pDiskTo);
7414 AssertRC(rc2);
7415 fLockWriteTo = true;
7416 } else {
7417 /** @todo hack to force creation of a fixed image for
7418 * the RAW backend, which can't handle anything else. */
7419 if (!RTStrICmp(pszBackend, "RAW"))
7420 uImageFlags |= VD_IMAGE_FLAGS_FIXED;
7421
7422 vdFixupPCHSGeometry(&PCHSGeometryFrom, cbSize);
7423 vdFixupLCHSGeometry(&LCHSGeometryFrom, cbSize);
7424
7425 rc = VDCreateBase(pDiskTo, pszBackend, pszFilename, cbSize,
7426 uImageFlags, szComment,
7427 &PCHSGeometryFrom, &LCHSGeometryFrom,
7428 NULL, uOpenFlags & ~VD_OPEN_FLAGS_READONLY,
7429 pDstVDIfsImage, NULL);
7430
7431 rc2 = vdThreadStartWrite(pDiskTo);
7432 AssertRC(rc2);
7433 fLockWriteTo = true;
7434
7435 if (RT_SUCCESS(rc) && !RTUuidIsNull(&ImageUuid))
7436 pDiskTo->pLast->Backend->pfnSetUuid(pDiskTo->pLast->pBackendData, &ImageUuid);
7437 }
7438 if (RT_FAILURE(rc))
7439 break;
7440
7441 pImageTo = pDiskTo->pLast;
7442 AssertPtrBreakStmt(pImageTo, rc = VERR_VD_IMAGE_NOT_FOUND);
7443
7444 cbSize = RT_MIN(cbSize, cbSizeFrom);
7445 }
7446 else
7447 {
7448 pImageTo = pDiskTo->pLast;
7449 AssertPtrBreakStmt(pImageTo, rc = VERR_VD_IMAGE_NOT_FOUND);
7450
7451 uint64_t cbSizeTo;
7452 cbSizeTo = vdImageGetSize(pImageTo);
7453 if (cbSizeTo == 0)
7454 {
7455 rc = VERR_VD_VALUE_NOT_FOUND;
7456 break;
7457 }
7458
7459 if (cbSize == 0)
7460 cbSize = RT_MIN(cbSizeFrom, cbSizeTo);
7461
7462 vdFixupPCHSGeometry(&PCHSGeometryFrom, cbSize);
7463 vdFixupLCHSGeometry(&LCHSGeometryFrom, cbSize);
7464
7465 /* Update the geometry in the destination image. */
7466 pImageTo->Backend->pfnSetPCHSGeometry(pImageTo->pBackendData, &PCHSGeometryFrom);
7467 pImageTo->Backend->pfnSetLCHSGeometry(pImageTo->pBackendData, &LCHSGeometryFrom);
7468 }
7469
7470 rc2 = vdThreadFinishWrite(pDiskTo);
7471 AssertRC(rc2);
7472 fLockWriteTo = false;
7473
7474 /* Whether we can take the optimized copy path (false) or not.
7475 * Don't optimize if the image existed or if it is a child image. */
7476 bool fSuppressRedundantIo = ( !(pszFilename == NULL || cImagesTo > 0)
7477 || (nImageToSame != VD_IMAGE_CONTENT_UNKNOWN));
7478 unsigned cImagesFromReadBack, cImagesToReadBack;
7479
7480 if (nImageFromSame == VD_IMAGE_CONTENT_UNKNOWN)
7481 cImagesFromReadBack = 0;
7482 else
7483 {
7484 if (nImage == VD_LAST_IMAGE)
7485 cImagesFromReadBack = pDiskFrom->cImages - nImageFromSame - 1;
7486 else
7487 cImagesFromReadBack = nImage - nImageFromSame;
7488 }
7489
7490 if (nImageToSame == VD_IMAGE_CONTENT_UNKNOWN)
7491 cImagesToReadBack = 0;
7492 else
7493 cImagesToReadBack = pDiskTo->cImages - nImageToSame - 1;
7494
7495 /* Copy the data. */
7496 rc = vdCopyHelper(pDiskFrom, pImageFrom, pDiskTo, cbSize,
7497 cImagesFromReadBack, cImagesToReadBack,
7498 fSuppressRedundantIo, pIfProgress, pDstIfProgress);
7499
7500 if (RT_SUCCESS(rc))
7501 {
7502 rc2 = vdThreadStartWrite(pDiskTo);
7503 AssertRC(rc2);
7504 fLockWriteTo = true;
7505
7506 /* Only set modification UUID if it is non-null, since the source
7507 * backend might not provide a valid modification UUID. */
7508 if (!RTUuidIsNull(&ImageModificationUuid))
7509 pImageTo->Backend->pfnSetModificationUuid(pImageTo->pBackendData, &ImageModificationUuid);
7510
7511 /* Set the requested open flags if they differ from the value
7512 * required for creating the image and copying the contents. */
7513 if ( pImageTo && pszFilename
7514 && uOpenFlags != (uOpenFlags & ~VD_OPEN_FLAGS_READONLY))
7515 rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pBackendData,
7516 uOpenFlags);
7517 }
7518 } while (0);
7519
7520 if (RT_FAILURE(rc) && pImageTo && pszFilename)
7521 {
7522 /* Take the write lock only if it is not taken. Not worth making the
7523 * above code even more complicated. */
7524 if (RT_UNLIKELY(!fLockWriteTo))
7525 {
7526 rc2 = vdThreadStartWrite(pDiskTo);
7527 AssertRC(rc2);
7528 fLockWriteTo = true;
7529 }
7530 /* Error detected, but new image created. Remove image from list. */
7531 vdRemoveImageFromList(pDiskTo, pImageTo);
7532
7533 /* Close and delete image. */
7534 rc2 = pImageTo->Backend->pfnClose(pImageTo->pBackendData, true);
7535 AssertRC(rc2);
7536 pImageTo->pBackendData = NULL;
7537
7538 /* Free remaining resources. */
7539 if (pImageTo->pszFilename)
7540 RTStrFree(pImageTo->pszFilename);
7541
7542 RTMemFree(pImageTo);
7543 }
7544
7545 if (RT_UNLIKELY(fLockWriteTo))
7546 {
7547 rc2 = vdThreadFinishWrite(pDiskTo);
7548 AssertRC(rc2);
7549 }
7550 if (RT_UNLIKELY(fLockWriteFrom))
7551 {
7552 rc2 = vdThreadFinishWrite(pDiskFrom);
7553 AssertRC(rc2);
7554 }
7555 else if (RT_UNLIKELY(fLockReadFrom))
7556 {
7557 rc2 = vdThreadFinishRead(pDiskFrom);
7558 AssertRC(rc2);
7559 }
7560
7561 if (RT_SUCCESS(rc))
7562 {
7563 if (pIfProgress && pIfProgress->pfnProgress)
7564 pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
7565 if (pDstIfProgress && pDstIfProgress->pfnProgress)
7566 pDstIfProgress->pfnProgress(pDstIfProgress->Core.pvUser, 100);
7567 }
7568
7569 LogFlowFunc(("returns %Rrc\n", rc));
7570 return rc;
7571}
7572
7573
7574VBOXDDU_DECL(int) VDCopy(PVDISK pDiskFrom, unsigned nImage, PVDISK pDiskTo,
7575 const char *pszBackend, const char *pszFilename,
7576 bool fMoveByRename, uint64_t cbSize,
7577 unsigned uImageFlags, PCRTUUID pDstUuid,
7578 unsigned uOpenFlags, PVDINTERFACE pVDIfsOperation,
7579 PVDINTERFACE pDstVDIfsImage,
7580 PVDINTERFACE pDstVDIfsOperation)
7581{
7582 return VDCopyEx(pDiskFrom, nImage, pDiskTo, pszBackend, pszFilename, fMoveByRename,
7583 cbSize, VD_IMAGE_CONTENT_UNKNOWN, VD_IMAGE_CONTENT_UNKNOWN,
7584 uImageFlags, pDstUuid, uOpenFlags, pVDIfsOperation,
7585 pDstVDIfsImage, pDstVDIfsOperation);
7586}
7587
7588
7589VBOXDDU_DECL(int) VDCompact(PVDISK pDisk, unsigned nImage,
7590 PVDINTERFACE pVDIfsOperation)
7591{
7592 int rc = VINF_SUCCESS;
7593 int rc2;
7594 bool fLockRead = false, fLockWrite = false;
7595 void *pvBuf = NULL;
7596 void *pvTmp = NULL;
7597
7598 LogFlowFunc(("pDisk=%#p nImage=%u pVDIfsOperation=%#p\n",
7599 pDisk, nImage, pVDIfsOperation));
7600
7601 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
7602
7603 do {
7604 /* Check arguments. */
7605 AssertMsgBreakStmt(VALID_PTR(pDisk), ("pDisk=%#p\n", pDisk),
7606 rc = VERR_INVALID_PARAMETER);
7607 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE,
7608 ("u32Signature=%08x\n", pDisk->u32Signature));
7609
7610 rc2 = vdThreadStartRead(pDisk);
7611 AssertRC(rc2);
7612 fLockRead = true;
7613
7614 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
7615 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
7616
7617 /* If there is no compact callback for not file based backends then
7618 * the backend doesn't need compaction. No need to make much fuss about
7619 * this. For file based ones signal this as not yet supported. */
7620 if (!pImage->Backend->pfnCompact)
7621 {
7622 if (pImage->Backend->uBackendCaps & VD_CAP_FILE)
7623 rc = VERR_NOT_SUPPORTED;
7624 else
7625 rc = VINF_SUCCESS;
7626 break;
7627 }
7628
7629 /* Insert interface for reading parent state into per-operation list,
7630 * if there is a parent image. */
7631 VDINTERFACEPARENTSTATE VDIfParent;
7632 VDPARENTSTATEDESC ParentUser;
7633 if (pImage->pPrev)
7634 {
7635 VDIfParent.pfnParentRead = vdParentRead;
7636 ParentUser.pDisk = pDisk;
7637 ParentUser.pImage = pImage->pPrev;
7638 rc = VDInterfaceAdd(&VDIfParent.Core, "VDCompact_ParentState", VDINTERFACETYPE_PARENTSTATE,
7639 &ParentUser, sizeof(VDINTERFACEPARENTSTATE), &pVDIfsOperation);
7640 AssertRC(rc);
7641 }
7642
7643 rc2 = vdThreadFinishRead(pDisk);
7644 AssertRC(rc2);
7645 fLockRead = false;
7646
7647 rc2 = vdThreadStartWrite(pDisk);
7648 AssertRC(rc2);
7649 fLockWrite = true;
7650
7651 rc = pImage->Backend->pfnCompact(pImage->pBackendData,
7652 0, 99,
7653 pDisk->pVDIfsDisk,
7654 pImage->pVDIfsImage,
7655 pVDIfsOperation);
7656 } while (0);
7657
7658 if (RT_UNLIKELY(fLockWrite))
7659 {
7660 rc2 = vdThreadFinishWrite(pDisk);
7661 AssertRC(rc2);
7662 }
7663 else if (RT_UNLIKELY(fLockRead))
7664 {
7665 rc2 = vdThreadFinishRead(pDisk);
7666 AssertRC(rc2);
7667 }
7668
7669 if (pvBuf)
7670 RTMemTmpFree(pvBuf);
7671 if (pvTmp)
7672 RTMemTmpFree(pvTmp);
7673
7674 if (RT_SUCCESS(rc))
7675 {
7676 if (pIfProgress && pIfProgress->pfnProgress)
7677 pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
7678 }
7679
7680 LogFlowFunc(("returns %Rrc\n", rc));
7681 return rc;
7682}
7683
7684
7685VBOXDDU_DECL(int) VDResize(PVDISK pDisk, uint64_t cbSize,
7686 PCVDGEOMETRY pPCHSGeometry,
7687 PCVDGEOMETRY pLCHSGeometry,
7688 PVDINTERFACE pVDIfsOperation)
7689{
7690 /** @todo r=klaus resizing was designed to be part of VDCopy, so having a separate function is not desirable. */
7691 int rc = VINF_SUCCESS;
7692 int rc2;
7693 bool fLockRead = false, fLockWrite = false;
7694
7695 LogFlowFunc(("pDisk=%#p cbSize=%llu pVDIfsOperation=%#p\n",
7696 pDisk, cbSize, pVDIfsOperation));
7697
7698 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
7699
7700 do {
7701 /* Check arguments. */
7702 AssertMsgBreakStmt(VALID_PTR(pDisk), ("pDisk=%#p\n", pDisk),
7703 rc = VERR_INVALID_PARAMETER);
7704 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE,
7705 ("u32Signature=%08x\n", pDisk->u32Signature));
7706
7707 rc2 = vdThreadStartRead(pDisk);
7708 AssertRC(rc2);
7709 fLockRead = true;
7710
7711 /* Must have at least one image in the chain, will resize last. */
7712 AssertMsgBreakStmt(pDisk->cImages >= 1, ("cImages=%u\n", pDisk->cImages),
7713 rc = VERR_NOT_SUPPORTED);
7714
7715 PVDIMAGE pImage = pDisk->pLast;
7716
7717 /* If there is no compact callback for not file based backends then
7718 * the backend doesn't need compaction. No need to make much fuss about
7719 * this. For file based ones signal this as not yet supported. */
7720 if (!pImage->Backend->pfnResize)
7721 {
7722 if (pImage->Backend->uBackendCaps & VD_CAP_FILE)
7723 rc = VERR_NOT_SUPPORTED;
7724 else
7725 rc = VINF_SUCCESS;
7726 break;
7727 }
7728
7729 rc2 = vdThreadFinishRead(pDisk);
7730 AssertRC(rc2);
7731 fLockRead = false;
7732
7733 rc2 = vdThreadStartWrite(pDisk);
7734 AssertRC(rc2);
7735 fLockWrite = true;
7736
7737 VDGEOMETRY PCHSGeometryOld;
7738 VDGEOMETRY LCHSGeometryOld;
7739 PCVDGEOMETRY pPCHSGeometryNew;
7740 PCVDGEOMETRY pLCHSGeometryNew;
7741
7742 if (pPCHSGeometry->cCylinders == 0)
7743 {
7744 /* Auto-detect marker, calculate new value ourself. */
7745 rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData, &PCHSGeometryOld);
7746 if (RT_SUCCESS(rc) && (PCHSGeometryOld.cCylinders != 0))
7747 PCHSGeometryOld.cCylinders = RT_MIN(cbSize / 512 / PCHSGeometryOld.cHeads / PCHSGeometryOld.cSectors, 16383);
7748 else if (rc == VERR_VD_GEOMETRY_NOT_SET)
7749 rc = VINF_SUCCESS;
7750
7751 pPCHSGeometryNew = &PCHSGeometryOld;
7752 }
7753 else
7754 pPCHSGeometryNew = pPCHSGeometry;
7755
7756 if (pLCHSGeometry->cCylinders == 0)
7757 {
7758 /* Auto-detect marker, calculate new value ourself. */
7759 rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData, &LCHSGeometryOld);
7760 if (RT_SUCCESS(rc) && (LCHSGeometryOld.cCylinders != 0))
7761 LCHSGeometryOld.cCylinders = cbSize / 512 / LCHSGeometryOld.cHeads / LCHSGeometryOld.cSectors;
7762 else if (rc == VERR_VD_GEOMETRY_NOT_SET)
7763 rc = VINF_SUCCESS;
7764
7765 pLCHSGeometryNew = &LCHSGeometryOld;
7766 }
7767 else
7768 pLCHSGeometryNew = pLCHSGeometry;
7769
7770 if (RT_SUCCESS(rc))
7771 rc = pImage->Backend->pfnResize(pImage->pBackendData,
7772 cbSize,
7773 pPCHSGeometryNew,
7774 pLCHSGeometryNew,
7775 0, 99,
7776 pDisk->pVDIfsDisk,
7777 pImage->pVDIfsImage,
7778 pVDIfsOperation);
7779 /* Mark the image size as uninitialized so it gets recalculated the next time. */
7780 if (RT_SUCCESS(rc))
7781 pImage->cbImage = VD_IMAGE_SIZE_UNINITIALIZED;
7782 } while (0);
7783
7784 if (RT_UNLIKELY(fLockWrite))
7785 {
7786 rc2 = vdThreadFinishWrite(pDisk);
7787 AssertRC(rc2);
7788 }
7789 else if (RT_UNLIKELY(fLockRead))
7790 {
7791 rc2 = vdThreadFinishRead(pDisk);
7792 AssertRC(rc2);
7793 }
7794
7795 if (RT_SUCCESS(rc))
7796 {
7797 if (pIfProgress && pIfProgress->pfnProgress)
7798 pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
7799
7800 pDisk->cbSize = cbSize;
7801 }
7802
7803 LogFlowFunc(("returns %Rrc\n", rc));
7804 return rc;
7805}
7806
7807VBOXDDU_DECL(int) VDPrepareWithFilters(PVDISK pDisk, PVDINTERFACE pVDIfsOperation)
7808{
7809 int rc = VINF_SUCCESS;
7810 int rc2;
7811 bool fLockRead = false, fLockWrite = false;
7812
7813 LogFlowFunc(("pDisk=%#p pVDIfsOperation=%#p\n", pDisk, pVDIfsOperation));
7814
7815 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
7816
7817 do {
7818 /* Check arguments. */
7819 AssertMsgBreakStmt(VALID_PTR(pDisk), ("pDisk=%#p\n", pDisk),
7820 rc = VERR_INVALID_PARAMETER);
7821 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE,
7822 ("u32Signature=%08x\n", pDisk->u32Signature));
7823
7824 rc2 = vdThreadStartRead(pDisk);
7825 AssertRC(rc2);
7826 fLockRead = true;
7827
7828 /* Must have at least one image in the chain. */
7829 AssertMsgBreakStmt(pDisk->cImages >= 1, ("cImages=%u\n", pDisk->cImages),
7830 rc = VERR_VD_NOT_OPENED);
7831
7832 unsigned uOpenFlags = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pBackendData);
7833 AssertMsgBreakStmt(!(uOpenFlags & VD_OPEN_FLAGS_READONLY),
7834 ("Last image should be read write"),
7835 rc = VERR_VD_IMAGE_READ_ONLY);
7836
7837 rc2 = vdThreadFinishRead(pDisk);
7838 AssertRC(rc2);
7839 fLockRead = false;
7840
7841 rc2 = vdThreadStartWrite(pDisk);
7842 AssertRC(rc2);
7843 fLockWrite = true;
7844
7845 /*
7846 * Open all images in the chain in read write mode first to avoid running
7847 * into an error in the middle of the process.
7848 */
7849 PVDIMAGE pImage = pDisk->pBase;
7850
7851 while (pImage)
7852 {
7853 uOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
7854 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
7855 {
7856 /*
7857 * Clear skip consistency checks because the image is made writable now and
7858 * skipping consistency checks is only possible for readonly images.
7859 */
7860 uOpenFlags &= ~(VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS);
7861 rc = pImage->Backend->pfnSetOpenFlags(pImage->pBackendData, uOpenFlags);
7862 if (RT_FAILURE(rc))
7863 break;
7864 }
7865 pImage = pImage->pNext;
7866 }
7867
7868 if (RT_SUCCESS(rc))
7869 {
7870 unsigned cImgCur = 0;
7871 unsigned uPercentStart = 0;
7872 unsigned uPercentSpan = 100 / pDisk->cImages - 1;
7873
7874 /* Allocate tmp buffer. */
7875 void *pvBuf = RTMemTmpAlloc(VD_MERGE_BUFFER_SIZE);
7876 if (!pvBuf)
7877 {
7878 rc = VERR_NO_MEMORY;
7879 break;
7880 }
7881
7882 pImage = pDisk->pBase;
7883 pDisk->fLocked = true;
7884
7885 while ( pImage
7886 && RT_SUCCESS(rc))
7887 {
7888 /* Get size of image. */
7889 uint64_t cbSize = vdImageGetSize(pImage);
7890 uint64_t cbSizeFile = pImage->Backend->pfnGetFileSize(pImage->pBackendData);
7891 uint64_t cbFileWritten = 0;
7892 uint64_t uOffset = 0;
7893 uint64_t cbRemaining = cbSize;
7894
7895 do
7896 {
7897 size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
7898 RTSGSEG SegmentBuf;
7899 RTSGBUF SgBuf;
7900 VDIOCTX IoCtx;
7901
7902 SegmentBuf.pvSeg = pvBuf;
7903 SegmentBuf.cbSeg = VD_MERGE_BUFFER_SIZE;
7904 RTSgBufInit(&SgBuf, &SegmentBuf, 1);
7905 vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_READ, 0, 0, NULL,
7906 &SgBuf, NULL, NULL, VDIOCTX_FLAGS_SYNC);
7907
7908 rc = pImage->Backend->pfnRead(pImage->pBackendData, uOffset,
7909 cbThisRead, &IoCtx, &cbThisRead);
7910 if (rc != VERR_VD_BLOCK_FREE)
7911 {
7912 if (RT_FAILURE(rc))
7913 break;
7914
7915 /* Apply filter chains. */
7916 rc = vdFilterChainApplyRead(pDisk, uOffset, cbThisRead, &IoCtx);
7917 if (RT_FAILURE(rc))
7918 break;
7919
7920 rc = vdFilterChainApplyWrite(pDisk, uOffset, cbThisRead, &IoCtx);
7921 if (RT_FAILURE(rc))
7922 break;
7923
7924 RTSgBufReset(&SgBuf);
7925 size_t cbThisWrite = 0;
7926 size_t cbPreRead = 0;
7927 size_t cbPostRead = 0;
7928 rc = pImage->Backend->pfnWrite(pImage->pBackendData, uOffset,
7929 cbThisRead, &IoCtx, &cbThisWrite,
7930 &cbPreRead, &cbPostRead, 0);
7931 if (RT_FAILURE(rc))
7932 break;
7933 Assert(cbThisWrite == cbThisRead);
7934 cbFileWritten += cbThisWrite;
7935 }
7936 else
7937 rc = VINF_SUCCESS;
7938
7939 uOffset += cbThisRead;
7940 cbRemaining -= cbThisRead;
7941
7942 if (pIfProgress && pIfProgress->pfnProgress)
7943 {
7944 rc2 = pIfProgress->pfnProgress(pIfProgress->Core.pvUser,
7945 uPercentStart + cbFileWritten * uPercentSpan / cbSizeFile);
7946 AssertRC(rc2); /* Cancelling this operation without leaving an inconsistent state is not possible. */
7947 }
7948 } while (uOffset < cbSize);
7949
7950 pImage = pImage->pNext;
7951 cImgCur++;
7952 uPercentStart += uPercentSpan;
7953 }
7954
7955 pDisk->fLocked = false;
7956 if (pvBuf)
7957 RTMemTmpFree(pvBuf);
7958 }
7959
7960 /* Change images except last one back to readonly. */
7961 pImage = pDisk->pBase;
7962 while ( pImage != pDisk->pLast
7963 && pImage)
7964 {
7965 uOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
7966 uOpenFlags |= VD_OPEN_FLAGS_READONLY;
7967 rc2 = pImage->Backend->pfnSetOpenFlags(pImage->pBackendData, uOpenFlags);
7968 if (RT_FAILURE(rc2))
7969 {
7970 if (RT_SUCCESS(rc))
7971 rc = rc2;
7972 break;
7973 }
7974 pImage = pImage->pNext;
7975 }
7976 } while (0);
7977
7978 if (RT_UNLIKELY(fLockWrite))
7979 {
7980 rc2 = vdThreadFinishWrite(pDisk);
7981 AssertRC(rc2);
7982 }
7983 else if (RT_UNLIKELY(fLockRead))
7984 {
7985 rc2 = vdThreadFinishRead(pDisk);
7986 AssertRC(rc2);
7987 }
7988
7989 if ( RT_SUCCESS(rc)
7990 && pIfProgress
7991 && pIfProgress->pfnProgress)
7992 pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
7993
7994 LogFlowFunc(("returns %Rrc\n", rc));
7995 return rc;
7996}
7997
7998
7999VBOXDDU_DECL(int) VDClose(PVDISK pDisk, bool fDelete)
8000{
8001 int rc = VINF_SUCCESS;
8002 int rc2;
8003 bool fLockWrite = false;
8004
8005 LogFlowFunc(("pDisk=%#p fDelete=%d\n", pDisk, fDelete));
8006 do
8007 {
8008 /* sanity check */
8009 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
8010 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8011
8012 /* Not worth splitting this up into a read lock phase and write
8013 * lock phase, as closing an image is a relatively fast operation
8014 * dominated by the part which needs the write lock. */
8015 rc2 = vdThreadStartWrite(pDisk);
8016 AssertRC(rc2);
8017 fLockWrite = true;
8018
8019 PVDIMAGE pImage = pDisk->pLast;
8020 if (!pImage)
8021 {
8022 rc = VERR_VD_NOT_OPENED;
8023 break;
8024 }
8025
8026 /* Destroy the current discard state first which might still have pending blocks. */
8027 rc = vdDiscardStateDestroy(pDisk);
8028 if (RT_FAILURE(rc))
8029 break;
8030
8031 unsigned uOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
8032 /* Remove image from list of opened images. */
8033 vdRemoveImageFromList(pDisk, pImage);
8034 /* Close (and optionally delete) image. */
8035 rc = pImage->Backend->pfnClose(pImage->pBackendData, fDelete);
8036 /* Free remaining resources related to the image. */
8037 RTStrFree(pImage->pszFilename);
8038 RTMemFree(pImage);
8039
8040 pImage = pDisk->pLast;
8041 if (!pImage)
8042 break;
8043
8044 /* If disk was previously in read/write mode, make sure it will stay
8045 * like this (if possible) after closing this image. Set the open flags
8046 * accordingly. */
8047 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
8048 {
8049 uOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
8050 uOpenFlags &= ~ VD_OPEN_FLAGS_READONLY;
8051 rc = pImage->Backend->pfnSetOpenFlags(pImage->pBackendData, uOpenFlags);
8052 }
8053
8054 /* Cache disk information. */
8055 pDisk->cbSize = vdImageGetSize(pImage);
8056
8057 /* Cache PCHS geometry. */
8058 rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
8059 &pDisk->PCHSGeometry);
8060 if (RT_FAILURE(rc2))
8061 {
8062 pDisk->PCHSGeometry.cCylinders = 0;
8063 pDisk->PCHSGeometry.cHeads = 0;
8064 pDisk->PCHSGeometry.cSectors = 0;
8065 }
8066 else
8067 {
8068 /* Make sure the PCHS geometry is properly clipped. */
8069 pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
8070 pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
8071 pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
8072 }
8073
8074 /* Cache LCHS geometry. */
8075 rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
8076 &pDisk->LCHSGeometry);
8077 if (RT_FAILURE(rc2))
8078 {
8079 pDisk->LCHSGeometry.cCylinders = 0;
8080 pDisk->LCHSGeometry.cHeads = 0;
8081 pDisk->LCHSGeometry.cSectors = 0;
8082 }
8083 else
8084 {
8085 /* Make sure the LCHS geometry is properly clipped. */
8086 pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
8087 pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
8088 }
8089 } while (0);
8090
8091 if (RT_UNLIKELY(fLockWrite))
8092 {
8093 rc2 = vdThreadFinishWrite(pDisk);
8094 AssertRC(rc2);
8095 }
8096
8097 LogFlowFunc(("returns %Rrc\n", rc));
8098 return rc;
8099}
8100
8101
8102VBOXDDU_DECL(int) VDCacheClose(PVDISK pDisk, bool fDelete)
8103{
8104 int rc = VINF_SUCCESS;
8105 int rc2;
8106 bool fLockWrite = false;
8107 PVDCACHE pCache = NULL;
8108
8109 LogFlowFunc(("pDisk=%#p fDelete=%d\n", pDisk, fDelete));
8110
8111 do
8112 {
8113 /* sanity check */
8114 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
8115 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8116
8117 rc2 = vdThreadStartWrite(pDisk);
8118 AssertRC(rc2);
8119 fLockWrite = true;
8120
8121 AssertPtrBreakStmt(pDisk->pCache, rc = VERR_VD_CACHE_NOT_FOUND);
8122
8123 pCache = pDisk->pCache;
8124 pDisk->pCache = NULL;
8125
8126 pCache->Backend->pfnClose(pCache->pBackendData, fDelete);
8127 if (pCache->pszFilename)
8128 RTStrFree(pCache->pszFilename);
8129 RTMemFree(pCache);
8130 } while (0);
8131
8132 if (RT_LIKELY(fLockWrite))
8133 {
8134 rc2 = vdThreadFinishWrite(pDisk);
8135 AssertRC(rc2);
8136 }
8137
8138 LogFlowFunc(("returns %Rrc\n", rc));
8139 return rc;
8140}
8141
8142VBOXDDU_DECL(int) VDFilterRemove(PVDISK pDisk, uint32_t fFlags)
8143{
8144 int rc = VINF_SUCCESS;
8145 int rc2;
8146 bool fLockWrite = false;
8147 PVDFILTER pFilter = NULL;
8148
8149 LogFlowFunc(("pDisk=%#p\n", pDisk));
8150
8151 do
8152 {
8153 /* sanity check */
8154 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
8155 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8156
8157 AssertMsgBreakStmt(!(fFlags & ~VD_FILTER_FLAGS_MASK),
8158 ("Invalid flags set (fFlags=%#x)\n", fFlags),
8159 rc = VERR_INVALID_PARAMETER);
8160
8161 rc2 = vdThreadStartWrite(pDisk);
8162 AssertRC(rc2);
8163 fLockWrite = true;
8164
8165 if (fFlags & VD_FILTER_FLAGS_WRITE)
8166 {
8167 AssertBreakStmt(!RTListIsEmpty(&pDisk->ListFilterChainWrite), rc = VERR_VD_NOT_OPENED);
8168 pFilter = RTListGetLast(&pDisk->ListFilterChainWrite, VDFILTER, ListNodeChainWrite);
8169 AssertPtr(pFilter);
8170 RTListNodeRemove(&pFilter->ListNodeChainWrite);
8171 vdFilterRelease(pFilter);
8172 }
8173
8174 if (fFlags & VD_FILTER_FLAGS_READ)
8175 {
8176 AssertBreakStmt(!RTListIsEmpty(&pDisk->ListFilterChainRead), rc = VERR_VD_NOT_OPENED);
8177 pFilter = RTListGetLast(&pDisk->ListFilterChainRead, VDFILTER, ListNodeChainRead);
8178 AssertPtr(pFilter);
8179 RTListNodeRemove(&pFilter->ListNodeChainRead);
8180 vdFilterRelease(pFilter);
8181 }
8182 } while (0);
8183
8184 if (RT_LIKELY(fLockWrite))
8185 {
8186 rc2 = vdThreadFinishWrite(pDisk);
8187 AssertRC(rc2);
8188 }
8189
8190 LogFlowFunc(("returns %Rrc\n", rc));
8191 return rc;
8192}
8193
8194
8195VBOXDDU_DECL(int) VDCloseAll(PVDISK pDisk)
8196{
8197 int rc = VINF_SUCCESS;
8198 int rc2;
8199 bool fLockWrite = false;
8200
8201 LogFlowFunc(("pDisk=%#p\n", pDisk));
8202 do
8203 {
8204 /* sanity check */
8205 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
8206 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8207
8208 /* Lock the entire operation. */
8209 rc2 = vdThreadStartWrite(pDisk);
8210 AssertRC(rc2);
8211 fLockWrite = true;
8212
8213 PVDCACHE pCache = pDisk->pCache;
8214 if (pCache)
8215 {
8216 rc2 = pCache->Backend->pfnClose(pCache->pBackendData, false);
8217 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
8218 rc = rc2;
8219
8220 if (pCache->pszFilename)
8221 RTStrFree(pCache->pszFilename);
8222 RTMemFree(pCache);
8223 }
8224
8225 PVDIMAGE pImage = pDisk->pLast;
8226 while (VALID_PTR(pImage))
8227 {
8228 PVDIMAGE pPrev = pImage->pPrev;
8229 /* Remove image from list of opened images. */
8230 vdRemoveImageFromList(pDisk, pImage);
8231 /* Close image. */
8232 rc2 = pImage->Backend->pfnClose(pImage->pBackendData, false);
8233 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
8234 rc = rc2;
8235 /* Free remaining resources related to the image. */
8236 RTStrFree(pImage->pszFilename);
8237 RTMemFree(pImage);
8238 pImage = pPrev;
8239 }
8240 Assert(!VALID_PTR(pDisk->pLast));
8241 } while (0);
8242
8243 if (RT_UNLIKELY(fLockWrite))
8244 {
8245 rc2 = vdThreadFinishWrite(pDisk);
8246 AssertRC(rc2);
8247 }
8248
8249 LogFlowFunc(("returns %Rrc\n", rc));
8250 return rc;
8251}
8252
8253
8254VBOXDDU_DECL(int) VDFilterRemoveAll(PVDISK pDisk)
8255{
8256 int rc = VINF_SUCCESS;
8257 int rc2;
8258 bool fLockWrite = false;
8259
8260 LogFlowFunc(("pDisk=%#p\n", pDisk));
8261 do
8262 {
8263 /* sanity check */
8264 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
8265 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8266
8267 /* Lock the entire operation. */
8268 rc2 = vdThreadStartWrite(pDisk);
8269 AssertRC(rc2);
8270 fLockWrite = true;
8271
8272 PVDFILTER pFilter, pFilterNext;
8273 RTListForEachSafe(&pDisk->ListFilterChainWrite, pFilter, pFilterNext, VDFILTER, ListNodeChainWrite)
8274 {
8275 RTListNodeRemove(&pFilter->ListNodeChainWrite);
8276 vdFilterRelease(pFilter);
8277 }
8278
8279 RTListForEachSafe(&pDisk->ListFilterChainRead, pFilter, pFilterNext, VDFILTER, ListNodeChainRead)
8280 {
8281 RTListNodeRemove(&pFilter->ListNodeChainRead);
8282 vdFilterRelease(pFilter);
8283 }
8284 Assert(RTListIsEmpty(&pDisk->ListFilterChainRead));
8285 Assert(RTListIsEmpty(&pDisk->ListFilterChainWrite));
8286 } while (0);
8287
8288 if (RT_UNLIKELY(fLockWrite))
8289 {
8290 rc2 = vdThreadFinishWrite(pDisk);
8291 AssertRC(rc2);
8292 }
8293
8294 LogFlowFunc(("returns %Rrc\n", rc));
8295 return rc;
8296}
8297
8298
8299VBOXDDU_DECL(int) VDRead(PVDISK pDisk, uint64_t uOffset, void *pvBuf,
8300 size_t cbRead)
8301{
8302 int rc = VINF_SUCCESS;
8303 int rc2;
8304 bool fLockRead = false;
8305
8306 LogFlowFunc(("pDisk=%#p uOffset=%llu pvBuf=%p cbRead=%zu\n",
8307 pDisk, uOffset, pvBuf, cbRead));
8308 do
8309 {
8310 /* sanity check */
8311 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
8312 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8313
8314 /* Check arguments. */
8315 AssertMsgBreakStmt(VALID_PTR(pvBuf),
8316 ("pvBuf=%#p\n", pvBuf),
8317 rc = VERR_INVALID_PARAMETER);
8318 AssertMsgBreakStmt(cbRead,
8319 ("cbRead=%zu\n", cbRead),
8320 rc = VERR_INVALID_PARAMETER);
8321
8322 rc2 = vdThreadStartRead(pDisk);
8323 AssertRC(rc2);
8324 fLockRead = true;
8325
8326 AssertMsgBreakStmt( uOffset < pDisk->cbSize
8327 && cbRead <= pDisk->cbSize - uOffset,
8328 ("uOffset=%llu cbRead=%zu pDisk->cbSize=%llu\n",
8329 uOffset, cbRead, pDisk->cbSize),
8330 rc = VERR_INVALID_PARAMETER);
8331
8332 PVDIMAGE pImage = pDisk->pLast;
8333 AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
8334
8335 if (uOffset + cbRead > pDisk->cbSize)
8336 {
8337 /* Floppy images might be smaller than the standard expected by
8338 the floppy controller code. So, we won't fail here. */
8339 AssertMsgBreakStmt(pDisk->enmType == VDTYPE_FLOPPY,
8340 ("uOffset=%llu cbRead=%zu pDisk->cbSize=%llu\n",
8341 uOffset, cbRead, pDisk->cbSize),
8342 rc = VERR_EOF);
8343 memset(pvBuf, 0xf6, cbRead); /* f6h = format.com filler byte */
8344 if (uOffset >= pDisk->cbSize)
8345 break;
8346 cbRead = pDisk->cbSize - uOffset;
8347 }
8348
8349 rc = vdReadHelper(pDisk, pImage, uOffset, pvBuf, cbRead,
8350 true /* fUpdateCache */);
8351 } while (0);
8352
8353 if (RT_UNLIKELY(fLockRead))
8354 {
8355 rc2 = vdThreadFinishRead(pDisk);
8356 AssertRC(rc2);
8357 }
8358
8359 LogFlowFunc(("returns %Rrc\n", rc));
8360 return rc;
8361}
8362
8363
8364VBOXDDU_DECL(int) VDWrite(PVDISK pDisk, uint64_t uOffset, const void *pvBuf,
8365 size_t cbWrite)
8366{
8367 int rc = VINF_SUCCESS;
8368 int rc2;
8369 bool fLockWrite = false;
8370
8371 LogFlowFunc(("pDisk=%#p uOffset=%llu pvBuf=%p cbWrite=%zu\n",
8372 pDisk, uOffset, pvBuf, cbWrite));
8373 do
8374 {
8375 /* sanity check */
8376 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
8377 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8378
8379 /* Check arguments. */
8380 AssertMsgBreakStmt(VALID_PTR(pvBuf),
8381 ("pvBuf=%#p\n", pvBuf),
8382 rc = VERR_INVALID_PARAMETER);
8383 AssertMsgBreakStmt(cbWrite,
8384 ("cbWrite=%zu\n", cbWrite),
8385 rc = VERR_INVALID_PARAMETER);
8386
8387 rc2 = vdThreadStartWrite(pDisk);
8388 AssertRC(rc2);
8389 fLockWrite = true;
8390
8391 AssertMsgBreakStmt( uOffset < pDisk->cbSize
8392 && cbWrite <= pDisk->cbSize - uOffset,
8393 ("uOffset=%llu cbWrite=%zu pDisk->cbSize=%llu\n",
8394 uOffset, cbWrite, pDisk->cbSize),
8395 rc = VERR_INVALID_PARAMETER);
8396
8397 PVDIMAGE pImage = pDisk->pLast;
8398 AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
8399
8400 vdSetModifiedFlag(pDisk);
8401 rc = vdWriteHelper(pDisk, pImage, uOffset, pvBuf, cbWrite,
8402 VDIOCTX_FLAGS_READ_UPDATE_CACHE);
8403 if (RT_FAILURE(rc))
8404 break;
8405
8406 /* If there is a merge (in the direction towards a parent) running
8407 * concurrently then we have to also "relay" the write to this parent,
8408 * as the merge position might be already past the position where
8409 * this write is going. The "context" of the write can come from the
8410 * natural chain, since merging either already did or will take care
8411 * of the "other" content which is might be needed to fill the block
8412 * to a full allocation size. The cache doesn't need to be touched
8413 * as this write is covered by the previous one. */
8414 if (RT_UNLIKELY(pDisk->pImageRelay))
8415 rc = vdWriteHelper(pDisk, pDisk->pImageRelay, uOffset,
8416 pvBuf, cbWrite, VDIOCTX_FLAGS_DEFAULT);
8417 } while (0);
8418
8419 if (RT_UNLIKELY(fLockWrite))
8420 {
8421 rc2 = vdThreadFinishWrite(pDisk);
8422 AssertRC(rc2);
8423 }
8424
8425 LogFlowFunc(("returns %Rrc\n", rc));
8426 return rc;
8427}
8428
8429
8430VBOXDDU_DECL(int) VDFlush(PVDISK pDisk)
8431{
8432 int rc = VINF_SUCCESS;
8433 int rc2;
8434 bool fLockWrite = false;
8435
8436 LogFlowFunc(("pDisk=%#p\n", pDisk));
8437 do
8438 {
8439 /* sanity check */
8440 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
8441 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8442
8443 rc2 = vdThreadStartWrite(pDisk);
8444 AssertRC(rc2);
8445 fLockWrite = true;
8446
8447 PVDIMAGE pImage = pDisk->pLast;
8448 AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
8449
8450 VDIOCTX IoCtx;
8451 RTSEMEVENT hEventComplete = NIL_RTSEMEVENT;
8452
8453 rc = RTSemEventCreate(&hEventComplete);
8454 if (RT_FAILURE(rc))
8455 break;
8456
8457 vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_FLUSH, 0, 0, pImage, NULL,
8458 NULL, vdFlushHelperAsync, VDIOCTX_FLAGS_SYNC | VDIOCTX_FLAGS_DONT_FREE);
8459
8460 IoCtx.Type.Root.pfnComplete = vdIoCtxSyncComplete;
8461 IoCtx.Type.Root.pvUser1 = pDisk;
8462 IoCtx.Type.Root.pvUser2 = hEventComplete;
8463 rc = vdIoCtxProcessSync(&IoCtx, hEventComplete);
8464
8465 RTSemEventDestroy(hEventComplete);
8466 } while (0);
8467
8468 if (RT_UNLIKELY(fLockWrite))
8469 {
8470 rc2 = vdThreadFinishWrite(pDisk);
8471 AssertRC(rc2);
8472 }
8473
8474 LogFlowFunc(("returns %Rrc\n", rc));
8475 return rc;
8476}
8477
8478
8479VBOXDDU_DECL(unsigned) VDGetCount(PVDISK pDisk)
8480{
8481 unsigned cImages;
8482 int rc2;
8483 bool fLockRead = false;
8484
8485 LogFlowFunc(("pDisk=%#p\n", pDisk));
8486 do
8487 {
8488 /* sanity check */
8489 AssertPtrBreakStmt(pDisk, cImages = 0);
8490 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8491
8492 rc2 = vdThreadStartRead(pDisk);
8493 AssertRC(rc2);
8494 fLockRead = true;
8495
8496 cImages = pDisk->cImages;
8497 } while (0);
8498
8499 if (RT_UNLIKELY(fLockRead))
8500 {
8501 rc2 = vdThreadFinishRead(pDisk);
8502 AssertRC(rc2);
8503 }
8504
8505 LogFlowFunc(("returns %u\n", cImages));
8506 return cImages;
8507}
8508
8509
8510VBOXDDU_DECL(bool) VDIsReadOnly(PVDISK pDisk)
8511{
8512 bool fReadOnly;
8513 int rc2;
8514 bool fLockRead = false;
8515
8516 LogFlowFunc(("pDisk=%#p\n", pDisk));
8517 do
8518 {
8519 /* sanity check */
8520 AssertPtrBreakStmt(pDisk, fReadOnly = false);
8521 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8522
8523 rc2 = vdThreadStartRead(pDisk);
8524 AssertRC(rc2);
8525 fLockRead = true;
8526
8527 PVDIMAGE pImage = pDisk->pLast;
8528 AssertPtrBreakStmt(pImage, fReadOnly = true);
8529
8530 unsigned uOpenFlags;
8531 uOpenFlags = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pBackendData);
8532 fReadOnly = !!(uOpenFlags & VD_OPEN_FLAGS_READONLY);
8533 } while (0);
8534
8535 if (RT_UNLIKELY(fLockRead))
8536 {
8537 rc2 = vdThreadFinishRead(pDisk);
8538 AssertRC(rc2);
8539 }
8540
8541 LogFlowFunc(("returns %d\n", fReadOnly));
8542 return fReadOnly;
8543}
8544
8545
8546VBOXDDU_DECL(uint32_t) VDGetSectorSize(PVDISK pDisk, unsigned nImage)
8547{
8548 uint64_t cbSector;
8549 int rc2;
8550 bool fLockRead = false;
8551
8552 LogFlowFunc(("pDisk=%#p nImage=%u\n", pDisk, nImage));
8553 do
8554 {
8555 /* sanity check */
8556 AssertPtrBreakStmt(pDisk, cbSector = 0);
8557 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8558
8559 rc2 = vdThreadStartRead(pDisk);
8560 AssertRC(rc2);
8561 fLockRead = true;
8562
8563 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
8564 AssertPtrBreakStmt(pImage, cbSector = 0);
8565
8566 PCVDREGIONLIST pRegionList = NULL;
8567 int rc = pImage->Backend->pfnQueryRegions(pImage->pBackendData, &pRegionList);
8568 if (RT_SUCCESS(rc))
8569 {
8570 AssertBreakStmt(pRegionList->cRegions == 1, cbSector = 0);
8571 cbSector = pRegionList->aRegions[0].cbBlock;
8572
8573 AssertPtr(pImage->Backend->pfnRegionListRelease);
8574 pImage->Backend->pfnRegionListRelease(pImage->pBackendData, pRegionList);
8575 }
8576 else
8577 cbSector = 0;
8578 } while (0);
8579
8580 if (RT_UNLIKELY(fLockRead))
8581 {
8582 rc2 = vdThreadFinishRead(pDisk);
8583 AssertRC(rc2);
8584 }
8585
8586 LogFlowFunc(("returns %u\n", cbSector));
8587 return cbSector;
8588}
8589
8590
8591VBOXDDU_DECL(uint64_t) VDGetSize(PVDISK pDisk, unsigned nImage)
8592{
8593 uint64_t cbSize;
8594 int rc2;
8595 bool fLockRead = false;
8596
8597 LogFlowFunc(("pDisk=%#p nImage=%u\n", pDisk, nImage));
8598 do
8599 {
8600 /* sanity check */
8601 AssertPtrBreakStmt(pDisk, cbSize = 0);
8602 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8603
8604 rc2 = vdThreadStartRead(pDisk);
8605 AssertRC(rc2);
8606 fLockRead = true;
8607
8608 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
8609 AssertPtrBreakStmt(pImage, cbSize = 0);
8610
8611 cbSize = vdImageGetSize(pImage);
8612 } while (0);
8613
8614 if (RT_UNLIKELY(fLockRead))
8615 {
8616 rc2 = vdThreadFinishRead(pDisk);
8617 AssertRC(rc2);
8618 }
8619
8620 LogFlowFunc(("returns %llu\n", cbSize));
8621 return cbSize;
8622}
8623
8624
8625VBOXDDU_DECL(uint64_t) VDGetFileSize(PVDISK pDisk, unsigned nImage)
8626{
8627 uint64_t cbSize;
8628 int rc2;
8629 bool fLockRead = false;
8630
8631 LogFlowFunc(("pDisk=%#p nImage=%u\n", pDisk, nImage));
8632 do
8633 {
8634 /* sanity check */
8635 AssertPtrBreakStmt(pDisk, cbSize = 0);
8636 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8637
8638 rc2 = vdThreadStartRead(pDisk);
8639 AssertRC(rc2);
8640 fLockRead = true;
8641
8642 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
8643 AssertPtrBreakStmt(pImage, cbSize = 0);
8644 cbSize = pImage->Backend->pfnGetFileSize(pImage->pBackendData);
8645 } while (0);
8646
8647 if (RT_UNLIKELY(fLockRead))
8648 {
8649 rc2 = vdThreadFinishRead(pDisk);
8650 AssertRC(rc2);
8651 }
8652
8653 LogFlowFunc(("returns %llu\n", cbSize));
8654 return cbSize;
8655}
8656
8657
8658VBOXDDU_DECL(int) VDGetPCHSGeometry(PVDISK pDisk, unsigned nImage,
8659 PVDGEOMETRY pPCHSGeometry)
8660{
8661 int rc = VINF_SUCCESS;
8662 int rc2;
8663 bool fLockRead = false;
8664
8665 LogFlowFunc(("pDisk=%#p nImage=%u pPCHSGeometry=%#p\n",
8666 pDisk, nImage, pPCHSGeometry));
8667 do
8668 {
8669 /* sanity check */
8670 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
8671 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8672
8673 /* Check arguments. */
8674 AssertMsgBreakStmt(VALID_PTR(pPCHSGeometry),
8675 ("pPCHSGeometry=%#p\n", pPCHSGeometry),
8676 rc = VERR_INVALID_PARAMETER);
8677
8678 rc2 = vdThreadStartRead(pDisk);
8679 AssertRC(rc2);
8680 fLockRead = true;
8681
8682 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
8683 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
8684
8685 if (pImage == pDisk->pLast)
8686 {
8687 /* Use cached information if possible. */
8688 if (pDisk->PCHSGeometry.cCylinders != 0)
8689 *pPCHSGeometry = pDisk->PCHSGeometry;
8690 else
8691 rc = VERR_VD_GEOMETRY_NOT_SET;
8692 }
8693 else
8694 rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
8695 pPCHSGeometry);
8696 } while (0);
8697
8698 if (RT_UNLIKELY(fLockRead))
8699 {
8700 rc2 = vdThreadFinishRead(pDisk);
8701 AssertRC(rc2);
8702 }
8703
8704 LogFlowFunc(("%Rrc (PCHS=%u/%u/%u)\n", rc,
8705 pDisk->PCHSGeometry.cCylinders, pDisk->PCHSGeometry.cHeads,
8706 pDisk->PCHSGeometry.cSectors));
8707 return rc;
8708}
8709
8710
8711VBOXDDU_DECL(int) VDSetPCHSGeometry(PVDISK pDisk, unsigned nImage,
8712 PCVDGEOMETRY pPCHSGeometry)
8713{
8714 int rc = VINF_SUCCESS;
8715 int rc2;
8716 bool fLockWrite = false;
8717
8718 LogFlowFunc(("pDisk=%#p nImage=%u pPCHSGeometry=%#p PCHS=%u/%u/%u\n",
8719 pDisk, nImage, pPCHSGeometry, pPCHSGeometry->cCylinders,
8720 pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
8721 do
8722 {
8723 /* sanity check */
8724 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
8725 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8726
8727 /* Check arguments. */
8728 AssertMsgBreakStmt( VALID_PTR(pPCHSGeometry)
8729 && pPCHSGeometry->cHeads <= 16
8730 && pPCHSGeometry->cSectors <= 63,
8731 ("pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pPCHSGeometry,
8732 pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
8733 pPCHSGeometry->cSectors),
8734 rc = VERR_INVALID_PARAMETER);
8735
8736 rc2 = vdThreadStartWrite(pDisk);
8737 AssertRC(rc2);
8738 fLockWrite = true;
8739
8740 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
8741 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
8742
8743 if (pImage == pDisk->pLast)
8744 {
8745 if ( pPCHSGeometry->cCylinders != pDisk->PCHSGeometry.cCylinders
8746 || pPCHSGeometry->cHeads != pDisk->PCHSGeometry.cHeads
8747 || pPCHSGeometry->cSectors != pDisk->PCHSGeometry.cSectors)
8748 {
8749 /* Only update geometry if it is changed. Avoids similar checks
8750 * in every backend. Most of the time the new geometry is set
8751 * to the previous values, so no need to go through the hassle
8752 * of updating an image which could be opened in read-only mode
8753 * right now. */
8754 rc = pImage->Backend->pfnSetPCHSGeometry(pImage->pBackendData,
8755 pPCHSGeometry);
8756
8757 /* Cache new geometry values in any case. */
8758 rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
8759 &pDisk->PCHSGeometry);
8760 if (RT_FAILURE(rc2))
8761 {
8762 pDisk->PCHSGeometry.cCylinders = 0;
8763 pDisk->PCHSGeometry.cHeads = 0;
8764 pDisk->PCHSGeometry.cSectors = 0;
8765 }
8766 else
8767 {
8768 /* Make sure the CHS geometry is properly clipped. */
8769 pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 255);
8770 pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
8771 }
8772 }
8773 }
8774 else
8775 {
8776 VDGEOMETRY PCHS;
8777 rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
8778 &PCHS);
8779 if ( RT_FAILURE(rc)
8780 || pPCHSGeometry->cCylinders != PCHS.cCylinders
8781 || pPCHSGeometry->cHeads != PCHS.cHeads
8782 || pPCHSGeometry->cSectors != PCHS.cSectors)
8783 {
8784 /* Only update geometry if it is changed. Avoids similar checks
8785 * in every backend. Most of the time the new geometry is set
8786 * to the previous values, so no need to go through the hassle
8787 * of updating an image which could be opened in read-only mode
8788 * right now. */
8789 rc = pImage->Backend->pfnSetPCHSGeometry(pImage->pBackendData,
8790 pPCHSGeometry);
8791 }
8792 }
8793 } while (0);
8794
8795 if (RT_UNLIKELY(fLockWrite))
8796 {
8797 rc2 = vdThreadFinishWrite(pDisk);
8798 AssertRC(rc2);
8799 }
8800
8801 LogFlowFunc(("returns %Rrc\n", rc));
8802 return rc;
8803}
8804
8805
8806VBOXDDU_DECL(int) VDGetLCHSGeometry(PVDISK pDisk, unsigned nImage,
8807 PVDGEOMETRY pLCHSGeometry)
8808{
8809 int rc = VINF_SUCCESS;
8810 int rc2;
8811 bool fLockRead = false;
8812
8813 LogFlowFunc(("pDisk=%#p nImage=%u pLCHSGeometry=%#p\n",
8814 pDisk, nImage, pLCHSGeometry));
8815 do
8816 {
8817 /* sanity check */
8818 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
8819 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8820
8821 /* Check arguments. */
8822 AssertMsgBreakStmt(VALID_PTR(pLCHSGeometry),
8823 ("pLCHSGeometry=%#p\n", pLCHSGeometry),
8824 rc = VERR_INVALID_PARAMETER);
8825
8826 rc2 = vdThreadStartRead(pDisk);
8827 AssertRC(rc2);
8828 fLockRead = true;
8829
8830 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
8831 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
8832
8833 if (pImage == pDisk->pLast)
8834 {
8835 /* Use cached information if possible. */
8836 if (pDisk->LCHSGeometry.cCylinders != 0)
8837 *pLCHSGeometry = pDisk->LCHSGeometry;
8838 else
8839 rc = VERR_VD_GEOMETRY_NOT_SET;
8840 }
8841 else
8842 rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
8843 pLCHSGeometry);
8844 } while (0);
8845
8846 if (RT_UNLIKELY(fLockRead))
8847 {
8848 rc2 = vdThreadFinishRead(pDisk);
8849 AssertRC(rc2);
8850 }
8851
8852 LogFlowFunc((": %Rrc (LCHS=%u/%u/%u)\n", rc,
8853 pDisk->LCHSGeometry.cCylinders, pDisk->LCHSGeometry.cHeads,
8854 pDisk->LCHSGeometry.cSectors));
8855 return rc;
8856}
8857
8858
8859VBOXDDU_DECL(int) VDSetLCHSGeometry(PVDISK pDisk, unsigned nImage,
8860 PCVDGEOMETRY pLCHSGeometry)
8861{
8862 int rc = VINF_SUCCESS;
8863 int rc2;
8864 bool fLockWrite = false;
8865
8866 LogFlowFunc(("pDisk=%#p nImage=%u pLCHSGeometry=%#p LCHS=%u/%u/%u\n",
8867 pDisk, nImage, pLCHSGeometry, pLCHSGeometry->cCylinders,
8868 pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
8869 do
8870 {
8871 /* sanity check */
8872 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
8873 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8874
8875 /* Check arguments. */
8876 AssertMsgBreakStmt( VALID_PTR(pLCHSGeometry)
8877 && pLCHSGeometry->cHeads <= 255
8878 && pLCHSGeometry->cSectors <= 63,
8879 ("pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pLCHSGeometry,
8880 pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads,
8881 pLCHSGeometry->cSectors),
8882 rc = VERR_INVALID_PARAMETER);
8883
8884 rc2 = vdThreadStartWrite(pDisk);
8885 AssertRC(rc2);
8886 fLockWrite = true;
8887
8888 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
8889 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
8890
8891 if (pImage == pDisk->pLast)
8892 {
8893 if ( pLCHSGeometry->cCylinders != pDisk->LCHSGeometry.cCylinders
8894 || pLCHSGeometry->cHeads != pDisk->LCHSGeometry.cHeads
8895 || pLCHSGeometry->cSectors != pDisk->LCHSGeometry.cSectors)
8896 {
8897 /* Only update geometry if it is changed. Avoids similar checks
8898 * in every backend. Most of the time the new geometry is set
8899 * to the previous values, so no need to go through the hassle
8900 * of updating an image which could be opened in read-only mode
8901 * right now. */
8902 rc = pImage->Backend->pfnSetLCHSGeometry(pImage->pBackendData,
8903 pLCHSGeometry);
8904
8905 /* Cache new geometry values in any case. */
8906 rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
8907 &pDisk->LCHSGeometry);
8908 if (RT_FAILURE(rc2))
8909 {
8910 pDisk->LCHSGeometry.cCylinders = 0;
8911 pDisk->LCHSGeometry.cHeads = 0;
8912 pDisk->LCHSGeometry.cSectors = 0;
8913 }
8914 else
8915 {
8916 /* Make sure the CHS geometry is properly clipped. */
8917 pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
8918 pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
8919 }
8920 }
8921 }
8922 else
8923 {
8924 VDGEOMETRY LCHS;
8925 rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
8926 &LCHS);
8927 if ( RT_FAILURE(rc)
8928 || pLCHSGeometry->cCylinders != LCHS.cCylinders
8929 || pLCHSGeometry->cHeads != LCHS.cHeads
8930 || pLCHSGeometry->cSectors != LCHS.cSectors)
8931 {
8932 /* Only update geometry if it is changed. Avoids similar checks
8933 * in every backend. Most of the time the new geometry is set
8934 * to the previous values, so no need to go through the hassle
8935 * of updating an image which could be opened in read-only mode
8936 * right now. */
8937 rc = pImage->Backend->pfnSetLCHSGeometry(pImage->pBackendData,
8938 pLCHSGeometry);
8939 }
8940 }
8941 } while (0);
8942
8943 if (RT_UNLIKELY(fLockWrite))
8944 {
8945 rc2 = vdThreadFinishWrite(pDisk);
8946 AssertRC(rc2);
8947 }
8948
8949 LogFlowFunc(("returns %Rrc\n", rc));
8950 return rc;
8951}
8952
8953
8954VBOXDDU_DECL(int) VDQueryRegions(PVDISK pDisk, unsigned nImage, uint32_t fFlags,
8955 PPVDREGIONLIST ppRegionList)
8956{
8957 int rc = VINF_SUCCESS;
8958 int rc2;
8959 bool fLockRead = false;
8960
8961 LogFlowFunc(("pDisk=%#p nImage=%u fFlags=%#x ppRegionList=%#p\n",
8962 pDisk, nImage, fFlags, ppRegionList));
8963 do
8964 {
8965 /* sanity check */
8966 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
8967 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
8968
8969 /* Check arguments. */
8970 AssertMsgBreakStmt(VALID_PTR(ppRegionList),
8971 ("ppRegionList=%#p\n", ppRegionList),
8972 rc = VERR_INVALID_PARAMETER);
8973
8974 rc2 = vdThreadStartRead(pDisk);
8975 AssertRC(rc2);
8976 fLockRead = true;
8977
8978 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
8979 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
8980
8981 PCVDREGIONLIST pRegionList = NULL;
8982 rc = pImage->Backend->pfnQueryRegions(pImage->pBackendData, &pRegionList);
8983 if (RT_SUCCESS(rc))
8984 {
8985 rc = vdRegionListConv(pRegionList, fFlags, ppRegionList);
8986
8987 AssertPtr(pImage->Backend->pfnRegionListRelease);
8988 pImage->Backend->pfnRegionListRelease(pImage->pBackendData, pRegionList);
8989 }
8990 } while (0);
8991
8992 if (RT_UNLIKELY(fLockRead))
8993 {
8994 rc2 = vdThreadFinishRead(pDisk);
8995 AssertRC(rc2);
8996 }
8997
8998 LogFlowFunc((": %Rrc\n", rc));
8999 return rc;
9000}
9001
9002
9003VBOXDDU_DECL(void) VDRegionListFree(PVDREGIONLIST pRegionList)
9004{
9005 RTMemFree(pRegionList);
9006}
9007
9008
9009VBOXDDU_DECL(int) VDGetVersion(PVDISK pDisk, unsigned nImage,
9010 unsigned *puVersion)
9011{
9012 int rc = VINF_SUCCESS;
9013 int rc2;
9014 bool fLockRead = false;
9015
9016 LogFlowFunc(("pDisk=%#p nImage=%u puVersion=%#p\n",
9017 pDisk, nImage, puVersion));
9018 do
9019 {
9020 /* sanity check */
9021 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
9022 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
9023
9024 /* Check arguments. */
9025 AssertMsgBreakStmt(VALID_PTR(puVersion),
9026 ("puVersion=%#p\n", puVersion),
9027 rc = VERR_INVALID_PARAMETER);
9028
9029 rc2 = vdThreadStartRead(pDisk);
9030 AssertRC(rc2);
9031 fLockRead = true;
9032
9033 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
9034 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
9035
9036 *puVersion = pImage->Backend->pfnGetVersion(pImage->pBackendData);
9037 } while (0);
9038
9039 if (RT_UNLIKELY(fLockRead))
9040 {
9041 rc2 = vdThreadFinishRead(pDisk);
9042 AssertRC(rc2);
9043 }
9044
9045 LogFlowFunc(("returns %Rrc uVersion=%#x\n", rc, *puVersion));
9046 return rc;
9047}
9048
9049
9050VBOXDDU_DECL(int) VDBackendInfoSingle(PVDISK pDisk, unsigned nImage,
9051 PVDBACKENDINFO pBackendInfo)
9052{
9053 int rc = VINF_SUCCESS;
9054 int rc2;
9055 bool fLockRead = false;
9056
9057 LogFlowFunc(("pDisk=%#p nImage=%u pBackendInfo=%#p\n",
9058 pDisk, nImage, pBackendInfo));
9059 do
9060 {
9061 /* sanity check */
9062 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
9063 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
9064
9065 /* Check arguments. */
9066 AssertMsgBreakStmt(VALID_PTR(pBackendInfo),
9067 ("pBackendInfo=%#p\n", pBackendInfo),
9068 rc = VERR_INVALID_PARAMETER);
9069
9070 rc2 = vdThreadStartRead(pDisk);
9071 AssertRC(rc2);
9072 fLockRead = true;
9073
9074 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
9075 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
9076
9077 pBackendInfo->pszBackend = pImage->Backend->pszBackendName;
9078 pBackendInfo->uBackendCaps = pImage->Backend->uBackendCaps;
9079 pBackendInfo->paFileExtensions = pImage->Backend->paFileExtensions;
9080 pBackendInfo->paConfigInfo = pImage->Backend->paConfigInfo;
9081 } while (0);
9082
9083 if (RT_UNLIKELY(fLockRead))
9084 {
9085 rc2 = vdThreadFinishRead(pDisk);
9086 AssertRC(rc2);
9087 }
9088
9089 LogFlowFunc(("returns %Rrc\n", rc));
9090 return rc;
9091}
9092
9093
9094VBOXDDU_DECL(int) VDGetImageFlags(PVDISK pDisk, unsigned nImage,
9095 unsigned *puImageFlags)
9096{
9097 int rc = VINF_SUCCESS;
9098 int rc2;
9099 bool fLockRead = false;
9100
9101 LogFlowFunc(("pDisk=%#p nImage=%u puImageFlags=%#p\n",
9102 pDisk, nImage, puImageFlags));
9103 do
9104 {
9105 /* sanity check */
9106 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
9107 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
9108
9109 /* Check arguments. */
9110 AssertMsgBreakStmt(VALID_PTR(puImageFlags),
9111 ("puImageFlags=%#p\n", puImageFlags),
9112 rc = VERR_INVALID_PARAMETER);
9113
9114 rc2 = vdThreadStartRead(pDisk);
9115 AssertRC(rc2);
9116 fLockRead = true;
9117
9118 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
9119 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
9120
9121 *puImageFlags = pImage->uImageFlags;
9122 } while (0);
9123
9124 if (RT_UNLIKELY(fLockRead))
9125 {
9126 rc2 = vdThreadFinishRead(pDisk);
9127 AssertRC(rc2);
9128 }
9129
9130 LogFlowFunc(("returns %Rrc uImageFlags=%#x\n", rc, *puImageFlags));
9131 return rc;
9132}
9133
9134
9135VBOXDDU_DECL(int) VDGetOpenFlags(PVDISK pDisk, unsigned nImage,
9136 unsigned *puOpenFlags)
9137{
9138 int rc = VINF_SUCCESS;
9139 int rc2;
9140 bool fLockRead = false;
9141
9142 LogFlowFunc(("pDisk=%#p nImage=%u puOpenFlags=%#p\n",
9143 pDisk, nImage, puOpenFlags));
9144 do
9145 {
9146 /* sanity check */
9147 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
9148 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
9149
9150 /* Check arguments. */
9151 AssertMsgBreakStmt(VALID_PTR(puOpenFlags),
9152 ("puOpenFlags=%#p\n", puOpenFlags),
9153 rc = VERR_INVALID_PARAMETER);
9154
9155 rc2 = vdThreadStartRead(pDisk);
9156 AssertRC(rc2);
9157 fLockRead = true;
9158
9159 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
9160 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
9161
9162 *puOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
9163 } while (0);
9164
9165 if (RT_UNLIKELY(fLockRead))
9166 {
9167 rc2 = vdThreadFinishRead(pDisk);
9168 AssertRC(rc2);
9169 }
9170
9171 LogFlowFunc(("returns %Rrc uOpenFlags=%#x\n", rc, *puOpenFlags));
9172 return rc;
9173}
9174
9175
9176VBOXDDU_DECL(int) VDSetOpenFlags(PVDISK pDisk, unsigned nImage,
9177 unsigned uOpenFlags)
9178{
9179 int rc;
9180 int rc2;
9181 bool fLockWrite = false;
9182
9183 LogFlowFunc(("pDisk=%#p uOpenFlags=%#u\n", pDisk, uOpenFlags));
9184 do
9185 {
9186 /* sanity check */
9187 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
9188 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
9189
9190 /* Check arguments. */
9191 AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
9192 ("uOpenFlags=%#x\n", uOpenFlags),
9193 rc = VERR_INVALID_PARAMETER);
9194
9195 rc2 = vdThreadStartWrite(pDisk);
9196 AssertRC(rc2);
9197 fLockWrite = true;
9198
9199 /* Destroy any discard state because the image might be changed to readonly mode. */
9200 rc = vdDiscardStateDestroy(pDisk);
9201 if (RT_FAILURE(rc))
9202 break;
9203
9204 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
9205 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
9206
9207 rc = pImage->Backend->pfnSetOpenFlags(pImage->pBackendData,
9208 uOpenFlags & ~(VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_IGNORE_FLUSH | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS));
9209 if (RT_SUCCESS(rc))
9210 pImage->uOpenFlags = uOpenFlags & (VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_DISCARD | VD_OPEN_FLAGS_IGNORE_FLUSH | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS);
9211 } while (0);
9212
9213 if (RT_UNLIKELY(fLockWrite))
9214 {
9215 rc2 = vdThreadFinishWrite(pDisk);
9216 AssertRC(rc2);
9217 }
9218
9219 LogFlowFunc(("returns %Rrc\n", rc));
9220 return rc;
9221}
9222
9223
9224VBOXDDU_DECL(int) VDGetFilename(PVDISK pDisk, unsigned nImage,
9225 char *pszFilename, unsigned cbFilename)
9226{
9227 int rc;
9228 int rc2;
9229 bool fLockRead = false;
9230
9231 LogFlowFunc(("pDisk=%#p nImage=%u pszFilename=%#p cbFilename=%u\n",
9232 pDisk, nImage, pszFilename, cbFilename));
9233 do
9234 {
9235 /* sanity check */
9236 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
9237 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
9238
9239 /* Check arguments. */
9240 AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
9241 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
9242 rc = VERR_INVALID_PARAMETER);
9243 AssertMsgBreakStmt(cbFilename,
9244 ("cbFilename=%u\n", cbFilename),
9245 rc = VERR_INVALID_PARAMETER);
9246
9247 rc2 = vdThreadStartRead(pDisk);
9248 AssertRC(rc2);
9249 fLockRead = true;
9250
9251 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
9252 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
9253
9254 size_t cb = strlen(pImage->pszFilename);
9255 if (cb <= cbFilename)
9256 {
9257 strcpy(pszFilename, pImage->pszFilename);
9258 rc = VINF_SUCCESS;
9259 }
9260 else
9261 {
9262 strncpy(pszFilename, pImage->pszFilename, cbFilename - 1);
9263 pszFilename[cbFilename - 1] = '\0';
9264 rc = VERR_BUFFER_OVERFLOW;
9265 }
9266 } while (0);
9267
9268 if (RT_UNLIKELY(fLockRead))
9269 {
9270 rc2 = vdThreadFinishRead(pDisk);
9271 AssertRC(rc2);
9272 }
9273
9274 LogFlowFunc(("returns %Rrc, pszFilename=\"%s\"\n", rc, pszFilename));
9275 return rc;
9276}
9277
9278
9279VBOXDDU_DECL(int) VDGetComment(PVDISK pDisk, unsigned nImage,
9280 char *pszComment, unsigned cbComment)
9281{
9282 int rc;
9283 int rc2;
9284 bool fLockRead = false;
9285
9286 LogFlowFunc(("pDisk=%#p nImage=%u pszComment=%#p cbComment=%u\n",
9287 pDisk, nImage, pszComment, cbComment));
9288 do
9289 {
9290 /* sanity check */
9291 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
9292 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
9293
9294 /* Check arguments. */
9295 AssertMsgBreakStmt(VALID_PTR(pszComment),
9296 ("pszComment=%#p \"%s\"\n", pszComment, pszComment),
9297 rc = VERR_INVALID_PARAMETER);
9298 AssertMsgBreakStmt(cbComment,
9299 ("cbComment=%u\n", cbComment),
9300 rc = VERR_INVALID_PARAMETER);
9301
9302 rc2 = vdThreadStartRead(pDisk);
9303 AssertRC(rc2);
9304 fLockRead = true;
9305
9306 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
9307 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
9308
9309 rc = pImage->Backend->pfnGetComment(pImage->pBackendData, pszComment,
9310 cbComment);
9311 } while (0);
9312
9313 if (RT_UNLIKELY(fLockRead))
9314 {
9315 rc2 = vdThreadFinishRead(pDisk);
9316 AssertRC(rc2);
9317 }
9318
9319 LogFlowFunc(("returns %Rrc, pszComment=\"%s\"\n", rc, pszComment));
9320 return rc;
9321}
9322
9323
9324VBOXDDU_DECL(int) VDSetComment(PVDISK pDisk, unsigned nImage,
9325 const char *pszComment)
9326{
9327 int rc;
9328 int rc2;
9329 bool fLockWrite = false;
9330
9331 LogFlowFunc(("pDisk=%#p nImage=%u pszComment=%#p \"%s\"\n",
9332 pDisk, nImage, pszComment, pszComment));
9333 do
9334 {
9335 /* sanity check */
9336 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
9337 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
9338
9339 /* Check arguments. */
9340 AssertMsgBreakStmt(VALID_PTR(pszComment) || pszComment == NULL,
9341 ("pszComment=%#p \"%s\"\n", pszComment, pszComment),
9342 rc = VERR_INVALID_PARAMETER);
9343
9344 rc2 = vdThreadStartWrite(pDisk);
9345 AssertRC(rc2);
9346 fLockWrite = true;
9347
9348 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
9349 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
9350
9351 rc = pImage->Backend->pfnSetComment(pImage->pBackendData, pszComment);
9352 } while (0);
9353
9354 if (RT_UNLIKELY(fLockWrite))
9355 {
9356 rc2 = vdThreadFinishWrite(pDisk);
9357 AssertRC(rc2);
9358 }
9359
9360 LogFlowFunc(("returns %Rrc\n", rc));
9361 return rc;
9362}
9363
9364
9365VBOXDDU_DECL(int) VDGetUuid(PVDISK pDisk, unsigned nImage, PRTUUID pUuid)
9366{
9367 int rc;
9368 int rc2;
9369 bool fLockRead = false;
9370
9371 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
9372 do
9373 {
9374 /* sanity check */
9375 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
9376 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
9377
9378 /* Check arguments. */
9379 AssertMsgBreakStmt(VALID_PTR(pUuid),
9380 ("pUuid=%#p\n", pUuid),
9381 rc = VERR_INVALID_PARAMETER);
9382
9383 rc2 = vdThreadStartRead(pDisk);
9384 AssertRC(rc2);
9385 fLockRead = true;
9386
9387 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
9388 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
9389
9390 rc = pImage->Backend->pfnGetUuid(pImage->pBackendData, pUuid);
9391 } while (0);
9392
9393 if (RT_UNLIKELY(fLockRead))
9394 {
9395 rc2 = vdThreadFinishRead(pDisk);
9396 AssertRC(rc2);
9397 }
9398
9399 LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
9400 return rc;
9401}
9402
9403
9404VBOXDDU_DECL(int) VDSetUuid(PVDISK pDisk, unsigned nImage, PCRTUUID pUuid)
9405{
9406 int rc;
9407 int rc2;
9408 bool fLockWrite = false;
9409
9410 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
9411 pDisk, nImage, pUuid, pUuid));
9412 do
9413 {
9414 /* sanity check */
9415 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
9416 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
9417
9418 AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
9419 ("pUuid=%#p\n", pUuid),
9420 rc = VERR_INVALID_PARAMETER);
9421
9422 rc2 = vdThreadStartWrite(pDisk);
9423 AssertRC(rc2);
9424 fLockWrite = true;
9425
9426 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
9427 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
9428
9429 RTUUID Uuid;
9430 if (!pUuid)
9431 {
9432 RTUuidCreate(&Uuid);
9433 pUuid = &Uuid;
9434 }
9435 rc = pImage->Backend->pfnSetUuid(pImage->pBackendData, pUuid);
9436 } while (0);
9437
9438 if (RT_UNLIKELY(fLockWrite))
9439 {
9440 rc2 = vdThreadFinishWrite(pDisk);
9441 AssertRC(rc2);
9442 }
9443
9444 LogFlowFunc(("returns %Rrc\n", rc));
9445 return rc;
9446}
9447
9448
9449VBOXDDU_DECL(int) VDGetModificationUuid(PVDISK pDisk, unsigned nImage, PRTUUID pUuid)
9450{
9451 int rc = VINF_SUCCESS;
9452 int rc2;
9453 bool fLockRead = false;
9454
9455 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
9456 do
9457 {
9458 /* sanity check */
9459 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
9460 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
9461
9462 /* Check arguments. */
9463 AssertMsgBreakStmt(VALID_PTR(pUuid),
9464 ("pUuid=%#p\n", pUuid),
9465 rc = VERR_INVALID_PARAMETER);
9466
9467 rc2 = vdThreadStartRead(pDisk);
9468 AssertRC(rc2);
9469 fLockRead = true;
9470
9471 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
9472 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
9473
9474 rc = pImage->Backend->pfnGetModificationUuid(pImage->pBackendData,
9475 pUuid);
9476 } while (0);
9477
9478 if (RT_UNLIKELY(fLockRead))
9479 {
9480 rc2 = vdThreadFinishRead(pDisk);
9481 AssertRC(rc2);
9482 }
9483
9484 LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
9485 return rc;
9486}
9487
9488
9489VBOXDDU_DECL(int) VDSetModificationUuid(PVDISK pDisk, unsigned nImage, PCRTUUID pUuid)
9490{
9491 int rc;
9492 int rc2;
9493 bool fLockWrite = false;
9494
9495 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
9496 pDisk, nImage, pUuid, pUuid));
9497 do
9498 {
9499 /* sanity check */
9500 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
9501 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
9502
9503 /* Check arguments. */
9504 AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
9505 ("pUuid=%#p\n", pUuid),
9506 rc = VERR_INVALID_PARAMETER);
9507
9508 rc2 = vdThreadStartWrite(pDisk);
9509 AssertRC(rc2);
9510 fLockWrite = true;
9511
9512 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
9513 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
9514
9515 RTUUID Uuid;
9516 if (!pUuid)
9517 {
9518 RTUuidCreate(&Uuid);
9519 pUuid = &Uuid;
9520 }
9521 rc = pImage->Backend->pfnSetModificationUuid(pImage->pBackendData,
9522 pUuid);
9523 } while (0);
9524
9525 if (RT_UNLIKELY(fLockWrite))
9526 {
9527 rc2 = vdThreadFinishWrite(pDisk);
9528 AssertRC(rc2);
9529 }
9530
9531 LogFlowFunc(("returns %Rrc\n", rc));
9532 return rc;
9533}
9534
9535
9536VBOXDDU_DECL(int) VDGetParentUuid(PVDISK pDisk, unsigned nImage,
9537 PRTUUID pUuid)
9538{
9539 int rc = VINF_SUCCESS;
9540 int rc2;
9541 bool fLockRead = false;
9542
9543 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
9544 do
9545 {
9546 /* sanity check */
9547 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
9548 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
9549
9550 /* Check arguments. */
9551 AssertMsgBreakStmt(VALID_PTR(pUuid),
9552 ("pUuid=%#p\n", pUuid),
9553 rc = VERR_INVALID_PARAMETER);
9554
9555 rc2 = vdThreadStartRead(pDisk);
9556 AssertRC(rc2);
9557 fLockRead = true;
9558
9559 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
9560 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
9561
9562 rc = pImage->Backend->pfnGetParentUuid(pImage->pBackendData, pUuid);
9563 } while (0);
9564
9565 if (RT_UNLIKELY(fLockRead))
9566 {
9567 rc2 = vdThreadFinishRead(pDisk);
9568 AssertRC(rc2);
9569 }
9570
9571 LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
9572 return rc;
9573}
9574
9575
9576VBOXDDU_DECL(int) VDSetParentUuid(PVDISK pDisk, unsigned nImage,
9577 PCRTUUID pUuid)
9578{
9579 int rc;
9580 int rc2;
9581 bool fLockWrite = false;
9582
9583 LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
9584 pDisk, nImage, pUuid, pUuid));
9585 do
9586 {
9587 /* sanity check */
9588 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
9589 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
9590
9591 /* Check arguments. */
9592 AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
9593 ("pUuid=%#p\n", pUuid),
9594 rc = VERR_INVALID_PARAMETER);
9595
9596 rc2 = vdThreadStartWrite(pDisk);
9597 AssertRC(rc2);
9598 fLockWrite = true;
9599
9600 PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
9601 AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
9602
9603 RTUUID Uuid;
9604 if (!pUuid)
9605 {
9606 RTUuidCreate(&Uuid);
9607 pUuid = &Uuid;
9608 }
9609 rc = pImage->Backend->pfnSetParentUuid(pImage->pBackendData, pUuid);
9610 } while (0);
9611
9612 if (RT_UNLIKELY(fLockWrite))
9613 {
9614 rc2 = vdThreadFinishWrite(pDisk);
9615 AssertRC(rc2);
9616 }
9617
9618 LogFlowFunc(("returns %Rrc\n", rc));
9619 return rc;
9620}
9621
9622
9623VBOXDDU_DECL(void) VDDumpImages(PVDISK pDisk)
9624{
9625 int rc2;
9626 bool fLockRead = false;
9627
9628 do
9629 {
9630 /* sanity check */
9631 AssertPtrBreak(pDisk);
9632 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
9633
9634 if (!pDisk->pInterfaceError || !VALID_PTR(pDisk->pInterfaceError->pfnMessage))
9635 pDisk->pInterfaceError->pfnMessage = vdLogMessage;
9636
9637 rc2 = vdThreadStartRead(pDisk);
9638 AssertRC(rc2);
9639 fLockRead = true;
9640
9641 vdMessageWrapper(pDisk, "--- Dumping VD Disk, Images=%u\n", pDisk->cImages);
9642 for (PVDIMAGE pImage = pDisk->pBase; pImage; pImage = pImage->pNext)
9643 {
9644 vdMessageWrapper(pDisk, "Dumping VD image \"%s\" (Backend=%s)\n",
9645 pImage->pszFilename, pImage->Backend->pszBackendName);
9646 pImage->Backend->pfnDump(pImage->pBackendData);
9647 }
9648 } while (0);
9649
9650 if (RT_UNLIKELY(fLockRead))
9651 {
9652 rc2 = vdThreadFinishRead(pDisk);
9653 AssertRC(rc2);
9654 }
9655}
9656
9657
9658VBOXDDU_DECL(int) VDDiscardRanges(PVDISK pDisk, PCRTRANGE paRanges, unsigned cRanges)
9659{
9660 int rc;
9661 int rc2;
9662 bool fLockWrite = false;
9663
9664 LogFlowFunc(("pDisk=%#p paRanges=%#p cRanges=%u\n",
9665 pDisk, paRanges, cRanges));
9666 do
9667 {
9668 /* sanity check */
9669 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
9670 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
9671
9672 /* Check arguments. */
9673 AssertMsgBreakStmt(cRanges,
9674 ("cRanges=%u\n", cRanges),
9675 rc = VERR_INVALID_PARAMETER);
9676 AssertMsgBreakStmt(VALID_PTR(paRanges),
9677 ("paRanges=%#p\n", paRanges),
9678 rc = VERR_INVALID_PARAMETER);
9679
9680 rc2 = vdThreadStartWrite(pDisk);
9681 AssertRC(rc2);
9682 fLockWrite = true;
9683
9684 AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
9685
9686 AssertMsgBreakStmt(pDisk->pLast->uOpenFlags & VD_OPEN_FLAGS_DISCARD,
9687 ("Discarding not supported\n"),
9688 rc = VERR_NOT_SUPPORTED);
9689
9690 VDIOCTX IoCtx;
9691 RTSEMEVENT hEventComplete = NIL_RTSEMEVENT;
9692
9693 rc = RTSemEventCreate(&hEventComplete);
9694 if (RT_FAILURE(rc))
9695 break;
9696
9697 vdIoCtxDiscardInit(&IoCtx, pDisk, paRanges, cRanges,
9698 vdIoCtxSyncComplete, pDisk, hEventComplete, NULL,
9699 vdDiscardHelperAsync, VDIOCTX_FLAGS_SYNC | VDIOCTX_FLAGS_DONT_FREE);
9700 rc = vdIoCtxProcessSync(&IoCtx, hEventComplete);
9701
9702 RTSemEventDestroy(hEventComplete);
9703 } while (0);
9704
9705 if (RT_UNLIKELY(fLockWrite))
9706 {
9707 rc2 = vdThreadFinishWrite(pDisk);
9708 AssertRC(rc2);
9709 }
9710
9711 LogFlowFunc(("returns %Rrc\n", rc));
9712 return rc;
9713}
9714
9715
9716VBOXDDU_DECL(int) VDAsyncRead(PVDISK pDisk, uint64_t uOffset, size_t cbRead,
9717 PCRTSGBUF pcSgBuf,
9718 PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
9719 void *pvUser1, void *pvUser2)
9720{
9721 int rc = VERR_VD_BLOCK_FREE;
9722 int rc2;
9723 bool fLockRead = false;
9724 PVDIOCTX pIoCtx = NULL;
9725
9726 LogFlowFunc(("pDisk=%#p uOffset=%llu pcSgBuf=%#p cbRead=%zu pvUser1=%#p pvUser2=%#p\n",
9727 pDisk, uOffset, pcSgBuf, cbRead, pvUser1, pvUser2));
9728
9729 do
9730 {
9731 /* sanity check */
9732 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
9733 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
9734
9735 /* Check arguments. */
9736 AssertMsgBreakStmt(cbRead,
9737 ("cbRead=%zu\n", cbRead),
9738 rc = VERR_INVALID_PARAMETER);
9739 AssertMsgBreakStmt(VALID_PTR(pcSgBuf),
9740 ("pcSgBuf=%#p\n", pcSgBuf),
9741 rc = VERR_INVALID_PARAMETER);
9742
9743 rc2 = vdThreadStartRead(pDisk);
9744 AssertRC(rc2);
9745 fLockRead = true;
9746
9747 AssertMsgBreakStmt( uOffset < pDisk->cbSize
9748 && cbRead <= pDisk->cbSize - uOffset,
9749 ("uOffset=%llu cbRead=%zu pDisk->cbSize=%llu\n",
9750 uOffset, cbRead, pDisk->cbSize),
9751 rc = VERR_INVALID_PARAMETER);
9752 AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
9753
9754 pIoCtx = vdIoCtxRootAlloc(pDisk, VDIOCTXTXDIR_READ, uOffset,
9755 cbRead, pDisk->pLast, pcSgBuf,
9756 pfnComplete, pvUser1, pvUser2,
9757 NULL, vdReadHelperAsync,
9758 VDIOCTX_FLAGS_ZERO_FREE_BLOCKS);
9759 if (!pIoCtx)
9760 {
9761 rc = VERR_NO_MEMORY;
9762 break;
9763 }
9764
9765 rc = vdIoCtxProcessTryLockDefer(pIoCtx);
9766 if (rc == VINF_VD_ASYNC_IO_FINISHED)
9767 {
9768 if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
9769 vdIoCtxFree(pDisk, pIoCtx);
9770 else
9771 rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
9772 }
9773 else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
9774 vdIoCtxFree(pDisk, pIoCtx);
9775
9776 } while (0);
9777
9778 if (RT_UNLIKELY(fLockRead) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
9779 {
9780 rc2 = vdThreadFinishRead(pDisk);
9781 AssertRC(rc2);
9782 }
9783
9784 LogFlowFunc(("returns %Rrc\n", rc));
9785 return rc;
9786}
9787
9788
9789VBOXDDU_DECL(int) VDAsyncWrite(PVDISK pDisk, uint64_t uOffset, size_t cbWrite,
9790 PCRTSGBUF pcSgBuf,
9791 PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
9792 void *pvUser1, void *pvUser2)
9793{
9794 int rc;
9795 int rc2;
9796 bool fLockWrite = false;
9797 PVDIOCTX pIoCtx = NULL;
9798
9799 LogFlowFunc(("pDisk=%#p uOffset=%llu cSgBuf=%#p cbWrite=%zu pvUser1=%#p pvUser2=%#p\n",
9800 pDisk, uOffset, pcSgBuf, cbWrite, pvUser1, pvUser2));
9801 do
9802 {
9803 /* sanity check */
9804 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
9805 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
9806
9807 /* Check arguments. */
9808 AssertMsgBreakStmt(cbWrite,
9809 ("cbWrite=%zu\n", cbWrite),
9810 rc = VERR_INVALID_PARAMETER);
9811 AssertMsgBreakStmt(VALID_PTR(pcSgBuf),
9812 ("pcSgBuf=%#p\n", pcSgBuf),
9813 rc = VERR_INVALID_PARAMETER);
9814
9815 rc2 = vdThreadStartWrite(pDisk);
9816 AssertRC(rc2);
9817 fLockWrite = true;
9818
9819 AssertMsgBreakStmt( uOffset < pDisk->cbSize
9820 && cbWrite <= pDisk->cbSize - uOffset,
9821 ("uOffset=%llu cbWrite=%zu pDisk->cbSize=%llu\n",
9822 uOffset, cbWrite, pDisk->cbSize),
9823 rc = VERR_INVALID_PARAMETER);
9824 AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
9825
9826 pIoCtx = vdIoCtxRootAlloc(pDisk, VDIOCTXTXDIR_WRITE, uOffset,
9827 cbWrite, pDisk->pLast, pcSgBuf,
9828 pfnComplete, pvUser1, pvUser2,
9829 NULL, vdWriteHelperAsync,
9830 VDIOCTX_FLAGS_DEFAULT);
9831 if (!pIoCtx)
9832 {
9833 rc = VERR_NO_MEMORY;
9834 break;
9835 }
9836
9837 rc = vdIoCtxProcessTryLockDefer(pIoCtx);
9838 if (rc == VINF_VD_ASYNC_IO_FINISHED)
9839 {
9840 if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
9841 vdIoCtxFree(pDisk, pIoCtx);
9842 else
9843 rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
9844 }
9845 else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
9846 vdIoCtxFree(pDisk, pIoCtx);
9847 } while (0);
9848
9849 if (RT_UNLIKELY(fLockWrite) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
9850 {
9851 rc2 = vdThreadFinishWrite(pDisk);
9852 AssertRC(rc2);
9853 }
9854
9855 LogFlowFunc(("returns %Rrc\n", rc));
9856 return rc;
9857}
9858
9859
9860VBOXDDU_DECL(int) VDAsyncFlush(PVDISK pDisk, PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
9861 void *pvUser1, void *pvUser2)
9862{
9863 int rc;
9864 int rc2;
9865 bool fLockWrite = false;
9866 PVDIOCTX pIoCtx = NULL;
9867
9868 LogFlowFunc(("pDisk=%#p\n", pDisk));
9869
9870 do
9871 {
9872 /* sanity check */
9873 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
9874 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
9875
9876 rc2 = vdThreadStartWrite(pDisk);
9877 AssertRC(rc2);
9878 fLockWrite = true;
9879
9880 AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
9881
9882 pIoCtx = vdIoCtxRootAlloc(pDisk, VDIOCTXTXDIR_FLUSH, 0,
9883 0, pDisk->pLast, NULL,
9884 pfnComplete, pvUser1, pvUser2,
9885 NULL, vdFlushHelperAsync,
9886 VDIOCTX_FLAGS_DEFAULT);
9887 if (!pIoCtx)
9888 {
9889 rc = VERR_NO_MEMORY;
9890 break;
9891 }
9892
9893 rc = vdIoCtxProcessTryLockDefer(pIoCtx);
9894 if (rc == VINF_VD_ASYNC_IO_FINISHED)
9895 {
9896 if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
9897 vdIoCtxFree(pDisk, pIoCtx);
9898 else
9899 rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
9900 }
9901 else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
9902 vdIoCtxFree(pDisk, pIoCtx);
9903 } while (0);
9904
9905 if (RT_UNLIKELY(fLockWrite) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
9906 {
9907 rc2 = vdThreadFinishWrite(pDisk);
9908 AssertRC(rc2);
9909 }
9910
9911 LogFlowFunc(("returns %Rrc\n", rc));
9912 return rc;
9913}
9914
9915VBOXDDU_DECL(int) VDAsyncDiscardRanges(PVDISK pDisk, PCRTRANGE paRanges, unsigned cRanges,
9916 PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
9917 void *pvUser1, void *pvUser2)
9918{
9919 int rc;
9920 int rc2;
9921 bool fLockWrite = false;
9922 PVDIOCTX pIoCtx = NULL;
9923
9924 LogFlowFunc(("pDisk=%#p\n", pDisk));
9925
9926 do
9927 {
9928 /* sanity check */
9929 AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
9930 AssertMsg(pDisk->u32Signature == VDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
9931
9932 rc2 = vdThreadStartWrite(pDisk);
9933 AssertRC(rc2);
9934 fLockWrite = true;
9935
9936 AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
9937
9938 pIoCtx = vdIoCtxDiscardAlloc(pDisk, paRanges, cRanges,
9939 pfnComplete, pvUser1, pvUser2, NULL,
9940 vdDiscardHelperAsync,
9941 VDIOCTX_FLAGS_DEFAULT);
9942 if (!pIoCtx)
9943 {
9944 rc = VERR_NO_MEMORY;
9945 break;
9946 }
9947
9948 rc = vdIoCtxProcessTryLockDefer(pIoCtx);
9949 if (rc == VINF_VD_ASYNC_IO_FINISHED)
9950 {
9951 if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
9952 vdIoCtxFree(pDisk, pIoCtx);
9953 else
9954 rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
9955 }
9956 else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
9957 vdIoCtxFree(pDisk, pIoCtx);
9958 } while (0);
9959
9960 if (RT_UNLIKELY(fLockWrite) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
9961 {
9962 rc2 = vdThreadFinishWrite(pDisk);
9963 AssertRC(rc2);
9964 }
9965
9966 LogFlowFunc(("returns %Rrc\n", rc));
9967 return rc;
9968}
9969
9970VBOXDDU_DECL(int) VDRepair(PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
9971 const char *pszFilename, const char *pszBackend,
9972 uint32_t fFlags)
9973{
9974 int rc = VERR_NOT_SUPPORTED;
9975 PCVDIMAGEBACKEND pBackend = NULL;
9976 VDINTERFACEIOINT VDIfIoInt;
9977 VDINTERFACEIO VDIfIoFallback;
9978 PVDINTERFACEIO pInterfaceIo;
9979
9980 LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
9981 /* Check arguments. */
9982 AssertMsgReturn(VALID_PTR(pszFilename) && *pszFilename,
9983 ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
9984 VERR_INVALID_PARAMETER);
9985 AssertMsgReturn(VALID_PTR(pszBackend),
9986 ("pszBackend=%#p\n", pszBackend),
9987 VERR_INVALID_PARAMETER);
9988 AssertMsgReturn((fFlags & ~VD_REPAIR_FLAGS_MASK) == 0,
9989 ("fFlags=%#x\n", fFlags),
9990 VERR_INVALID_PARAMETER);
9991
9992 pInterfaceIo = VDIfIoGet(pVDIfsImage);
9993 if (!pInterfaceIo)
9994 {
9995 /*
9996 * Caller doesn't provide an I/O interface, create our own using the
9997 * native file API.
9998 */
9999 vdIfIoFallbackCallbacksSetup(&VDIfIoFallback);
10000 pInterfaceIo = &VDIfIoFallback;
10001 }
10002
10003 /* Set up the internal I/O interface. */
10004 AssertReturn(!VDIfIoIntGet(pVDIfsImage), VERR_INVALID_PARAMETER);
10005 VDIfIoInt.pfnOpen = vdIOIntOpenLimited;
10006 VDIfIoInt.pfnClose = vdIOIntCloseLimited;
10007 VDIfIoInt.pfnDelete = vdIOIntDeleteLimited;
10008 VDIfIoInt.pfnMove = vdIOIntMoveLimited;
10009 VDIfIoInt.pfnGetFreeSpace = vdIOIntGetFreeSpaceLimited;
10010 VDIfIoInt.pfnGetModificationTime = vdIOIntGetModificationTimeLimited;
10011 VDIfIoInt.pfnGetSize = vdIOIntGetSizeLimited;
10012 VDIfIoInt.pfnSetSize = vdIOIntSetSizeLimited;
10013 VDIfIoInt.pfnReadUser = vdIOIntReadUserLimited;
10014 VDIfIoInt.pfnWriteUser = vdIOIntWriteUserLimited;
10015 VDIfIoInt.pfnReadMeta = vdIOIntReadMetaLimited;
10016 VDIfIoInt.pfnWriteMeta = vdIOIntWriteMetaLimited;
10017 VDIfIoInt.pfnFlush = vdIOIntFlushLimited;
10018 rc = VDInterfaceAdd(&VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
10019 pInterfaceIo, sizeof(VDINTERFACEIOINT), &pVDIfsImage);
10020 AssertRC(rc);
10021
10022 rc = vdFindImageBackend(pszBackend, &pBackend);
10023 if (RT_SUCCESS(rc))
10024 {
10025 if (pBackend->pfnRepair)
10026 rc = pBackend->pfnRepair(pszFilename, pVDIfsDisk, pVDIfsImage, fFlags);
10027 else
10028 rc = VERR_VD_IMAGE_REPAIR_NOT_SUPPORTED;
10029 }
10030
10031 LogFlowFunc(("returns %Rrc\n", rc));
10032 return rc;
10033}
10034
10035
10036/*
10037 * generic plugin functions
10038 */
10039
10040/**
10041 * @interface_method_impl{VDIMAGEBACKEND,pfnComposeLocation}
10042 */
10043DECLCALLBACK(int) genericFileComposeLocation(PVDINTERFACE pConfig, char **pszLocation)
10044{
10045 RT_NOREF1(pConfig);
10046 *pszLocation = NULL;
10047 return VINF_SUCCESS;
10048}
10049
10050/**
10051 * @interface_method_impl{VDIMAGEBACKEND,pfnComposeName}
10052 */
10053DECLCALLBACK(int) genericFileComposeName(PVDINTERFACE pConfig, char **pszName)
10054{
10055 RT_NOREF1(pConfig);
10056 *pszName = NULL;
10057 return VINF_SUCCESS;
10058}
10059
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use