VirtualBox

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

Last change on this file since 63785 was 63781, checked in by vboxsync, 8 years ago

Storage/VD: Rename VBOXHDDBACKEND to VDIMAGEBACKEND to follow the naming scheme used for the other backend types (cache and filter)

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

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