1 | /* $Id: VDInternal.h 75349 2018-11-09 10:36:54Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VD - Virtual Disk container implementation, internal header file.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*********************************************************************************************************************************
|
---|
19 | * Header Files *
|
---|
20 | *********************************************************************************************************************************/
|
---|
21 | #ifndef ___VDInternal_h
|
---|
22 | #define ___VDInternal_h
|
---|
23 | #include <VBox/vd.h>
|
---|
24 | #include <VBox/vd-plugin.h>
|
---|
25 |
|
---|
26 | #include <iprt/avl.h>
|
---|
27 | #include <iprt/list.h>
|
---|
28 | #include <iprt/memcache.h>
|
---|
29 |
|
---|
30 | /** Disable dynamic backends on non x86 architectures. This feature
|
---|
31 | * requires the SUPR3 library which is not available there.
|
---|
32 | */
|
---|
33 | #if !defined(VBOX_HDD_NO_DYNAMIC_BACKENDS) && !defined(RT_ARCH_X86) && !defined(RT_ARCH_AMD64)
|
---|
34 | # define VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
35 | #endif
|
---|
36 |
|
---|
37 | /** Magic number contained in the VDISK instance data, used for checking that the passed
|
---|
38 | * pointer contains a valid instance in debug builds. */
|
---|
39 | #define VDISK_SIGNATURE 0x6f0e2a7d
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Structure containing everything I/O related
|
---|
43 | * for the image and cache descriptors.
|
---|
44 | */
|
---|
45 | typedef struct VDIO
|
---|
46 | {
|
---|
47 | /** I/O interface to the upper layer. */
|
---|
48 | PVDINTERFACEIO pInterfaceIo;
|
---|
49 |
|
---|
50 | /** Per image internal I/O interface. */
|
---|
51 | VDINTERFACEIOINT VDIfIoInt;
|
---|
52 |
|
---|
53 | /** Fallback I/O interface, only used if the caller doesn't provide it. */
|
---|
54 | VDINTERFACEIO VDIfIo;
|
---|
55 |
|
---|
56 | /** Opaque backend data. */
|
---|
57 | void *pBackendData;
|
---|
58 | /** Disk this image is part of */
|
---|
59 | PVDISK pDisk;
|
---|
60 | /** Flag whether to ignore flush requests. */
|
---|
61 | bool fIgnoreFlush;
|
---|
62 | } VDIO, *PVDIO;
|
---|
63 |
|
---|
64 | /** Forward declaration of an I/O task */
|
---|
65 | typedef struct VDIOTASK *PVDIOTASK;
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Virtual disk container image descriptor.
|
---|
69 | */
|
---|
70 | typedef struct VDIMAGE
|
---|
71 | {
|
---|
72 | /** Link to parent image descriptor, if any. */
|
---|
73 | struct VDIMAGE *pPrev;
|
---|
74 | /** Link to child image descriptor, if any. */
|
---|
75 | struct VDIMAGE *pNext;
|
---|
76 | /** Cached image size. */
|
---|
77 | uint64_t cbImage;
|
---|
78 | /** Container base filename. (UTF-8) */
|
---|
79 | char *pszFilename;
|
---|
80 | /** Data managed by the backend which keeps the actual info. */
|
---|
81 | void *pBackendData;
|
---|
82 | /** Cached sanitized image flags. */
|
---|
83 | unsigned uImageFlags;
|
---|
84 | /** Image open flags (only those handled generically in this code and which
|
---|
85 | * the backends will never ever see). */
|
---|
86 | unsigned uOpenFlags;
|
---|
87 |
|
---|
88 | /** Function pointers for the various backend methods. */
|
---|
89 | PCVDIMAGEBACKEND Backend;
|
---|
90 | /** Pointer to list of VD interfaces, per-image. */
|
---|
91 | PVDINTERFACE pVDIfsImage;
|
---|
92 | /** I/O related things. */
|
---|
93 | VDIO VDIo;
|
---|
94 | } VDIMAGE, *PVDIMAGE;
|
---|
95 |
|
---|
96 | /** The special uninitialized size value for he image. */
|
---|
97 | #define VD_IMAGE_SIZE_UNINITIALIZED UINT64_C(0)
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Virtual disk cache image descriptor.
|
---|
101 | */
|
---|
102 | typedef struct VDCACHE
|
---|
103 | {
|
---|
104 | /** Cache base filename. (UTF-8) */
|
---|
105 | char *pszFilename;
|
---|
106 | /** Data managed by the backend which keeps the actual info. */
|
---|
107 | void *pBackendData;
|
---|
108 | /** Cached sanitized image flags. */
|
---|
109 | unsigned uImageFlags;
|
---|
110 | /** Image open flags (only those handled generically in this code and which
|
---|
111 | * the backends will never ever see). */
|
---|
112 | unsigned uOpenFlags;
|
---|
113 |
|
---|
114 | /** Function pointers for the various backend methods. */
|
---|
115 | PCVDCACHEBACKEND Backend;
|
---|
116 |
|
---|
117 | /** Pointer to list of VD interfaces, per-cache. */
|
---|
118 | PVDINTERFACE pVDIfsCache;
|
---|
119 | /** I/O related things. */
|
---|
120 | VDIO VDIo;
|
---|
121 | } VDCACHE, *PVDCACHE;
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * A block waiting for a discard.
|
---|
125 | */
|
---|
126 | typedef struct VDDISCARDBLOCK
|
---|
127 | {
|
---|
128 | /** AVL core. */
|
---|
129 | AVLRU64NODECORE Core;
|
---|
130 | /** LRU list node. */
|
---|
131 | RTLISTNODE NodeLru;
|
---|
132 | /** Number of bytes to discard. */
|
---|
133 | size_t cbDiscard;
|
---|
134 | /** Bitmap of allocated sectors. */
|
---|
135 | void *pbmAllocated;
|
---|
136 | } VDDISCARDBLOCK, *PVDDISCARDBLOCK;
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * VD discard state.
|
---|
140 | */
|
---|
141 | typedef struct VDDISCARDSTATE
|
---|
142 | {
|
---|
143 | /** Number of bytes waiting for a discard. */
|
---|
144 | size_t cbDiscarding;
|
---|
145 | /** AVL tree with blocks waiting for a discard.
|
---|
146 | * The uOffset + cbDiscard range is the search key. */
|
---|
147 | PAVLRU64TREE pTreeBlocks;
|
---|
148 | /** LRU list of the least frequently discarded blocks.
|
---|
149 | * If there are to many blocks waiting the least frequently used
|
---|
150 | * will be removed and the range will be set to 0.
|
---|
151 | */
|
---|
152 | RTLISTNODE ListLru;
|
---|
153 | } VDDISCARDSTATE, *PVDDISCARDSTATE;
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * VD filter instance.
|
---|
157 | */
|
---|
158 | typedef struct VDFILTER
|
---|
159 | {
|
---|
160 | /** List node for the read filter chain. */
|
---|
161 | RTLISTNODE ListNodeChainRead;
|
---|
162 | /** List node for the write filter chain. */
|
---|
163 | RTLISTNODE ListNodeChainWrite;
|
---|
164 | /** Number of references to this filter. */
|
---|
165 | uint32_t cRefs;
|
---|
166 | /** Opaque VD filter backend instance data. */
|
---|
167 | void *pvBackendData;
|
---|
168 | /** Pointer to the filter backend interface. */
|
---|
169 | PCVDFILTERBACKEND pBackend;
|
---|
170 | /** Pointer to list of VD interfaces, per-filter. */
|
---|
171 | PVDINTERFACE pVDIfsFilter;
|
---|
172 | /** I/O related things. */
|
---|
173 | VDIO VDIo;
|
---|
174 | } VDFILTER;
|
---|
175 | /** Pointer to a VD filter instance. */
|
---|
176 | typedef VDFILTER *PVDFILTER;
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Virtual disk container main structure, private part.
|
---|
180 | */
|
---|
181 | struct VDISK
|
---|
182 | {
|
---|
183 | /** Structure signature (VDISK_SIGNATURE). */
|
---|
184 | uint32_t u32Signature;
|
---|
185 |
|
---|
186 | /** Image type. */
|
---|
187 | VDTYPE enmType;
|
---|
188 |
|
---|
189 | /** Number of opened images. */
|
---|
190 | unsigned cImages;
|
---|
191 |
|
---|
192 | /** Base image. */
|
---|
193 | PVDIMAGE pBase;
|
---|
194 |
|
---|
195 | /** Last opened image in the chain.
|
---|
196 | * The same as pBase if only one image is used. */
|
---|
197 | PVDIMAGE pLast;
|
---|
198 |
|
---|
199 | /** If a merge to one of the parents is running this may be non-NULL
|
---|
200 | * to indicate to what image the writes should be additionally relayed. */
|
---|
201 | PVDIMAGE pImageRelay;
|
---|
202 |
|
---|
203 | /** Flags representing the modification state. */
|
---|
204 | unsigned uModified;
|
---|
205 |
|
---|
206 | /** Cached size of this disk. */
|
---|
207 | uint64_t cbSize;
|
---|
208 | /** Cached PCHS geometry for this disk. */
|
---|
209 | VDGEOMETRY PCHSGeometry;
|
---|
210 | /** Cached LCHS geometry for this disk. */
|
---|
211 | VDGEOMETRY LCHSGeometry;
|
---|
212 |
|
---|
213 | /** Pointer to list of VD interfaces, per-disk. */
|
---|
214 | PVDINTERFACE pVDIfsDisk;
|
---|
215 | /** Pointer to the common interface structure for error reporting. */
|
---|
216 | PVDINTERFACEERROR pInterfaceError;
|
---|
217 | /** Pointer to the optional thread synchronization callbacks. */
|
---|
218 | PVDINTERFACETHREADSYNC pInterfaceThreadSync;
|
---|
219 |
|
---|
220 | /** Memory cache for I/O contexts */
|
---|
221 | RTMEMCACHE hMemCacheIoCtx;
|
---|
222 | /** Memory cache for I/O tasks. */
|
---|
223 | RTMEMCACHE hMemCacheIoTask;
|
---|
224 | /** An I/O context is currently using the disk structures
|
---|
225 | * Every I/O context must be placed on one of the lists below. */
|
---|
226 | volatile bool fLocked;
|
---|
227 | /** Head of pending I/O tasks waiting for completion - LIFO order. */
|
---|
228 | volatile PVDIOTASK pIoTasksPendingHead;
|
---|
229 | /** Head of newly queued I/O contexts - LIFO order. */
|
---|
230 | volatile PVDIOCTX pIoCtxHead;
|
---|
231 | /** Head of halted I/O contexts which are given back to generic
|
---|
232 | * disk framework by the backend. - LIFO order. */
|
---|
233 | volatile PVDIOCTX pIoCtxHaltedHead;
|
---|
234 |
|
---|
235 | /** Head of blocked I/O contexts, processed only
|
---|
236 | * after pIoCtxLockOwner was freed - LIFO order. */
|
---|
237 | volatile PVDIOCTX pIoCtxBlockedHead;
|
---|
238 | /** I/O context which locked the disk for a growing write or flush request.
|
---|
239 | * Other flush or growing write requests need to wait until
|
---|
240 | * the current one completes. - NIL_VDIOCTX if unlocked. */
|
---|
241 | volatile PVDIOCTX pIoCtxLockOwner;
|
---|
242 | /** If the disk was locked by a growing write, flush or discard request this
|
---|
243 | * contains the start offset to check for interfering I/O while it is in progress. */
|
---|
244 | uint64_t uOffsetStartLocked;
|
---|
245 | /** If the disk was locked by a growing write, flush or discard request this contains
|
---|
246 | * the first non affected offset to check for interfering I/O while it is in progress. */
|
---|
247 | uint64_t uOffsetEndLocked;
|
---|
248 |
|
---|
249 | /** Pointer to the L2 disk cache if any. */
|
---|
250 | PVDCACHE pCache;
|
---|
251 | /** Pointer to the discard state if any. */
|
---|
252 | PVDDISCARDSTATE pDiscard;
|
---|
253 |
|
---|
254 | /** Read filter chain - PVDFILTER. */
|
---|
255 | RTLISTANCHOR ListFilterChainRead;
|
---|
256 | /** Write filter chain - PVDFILTER. */
|
---|
257 | RTLISTANCHOR ListFilterChainWrite;
|
---|
258 | };
|
---|
259 |
|
---|
260 |
|
---|
261 | DECLHIDDEN(int) vdPluginInit(void);
|
---|
262 | DECLHIDDEN(int) vdPluginTerm(void);
|
---|
263 | DECLHIDDEN(bool) vdPluginIsInitialized(void);
|
---|
264 | DECLHIDDEN(int) vdPluginUnloadFromPath(const char *pszPath);
|
---|
265 | DECLHIDDEN(int) vdPluginUnloadFromFilename(const char *pszFilename);
|
---|
266 | DECLHIDDEN(int) vdPluginLoadFromPath(const char *pszPath);
|
---|
267 | DECLHIDDEN(int) vdPluginLoadFromFilename(const char *pszFilename);
|
---|
268 |
|
---|
269 | DECLHIDDEN(uint32_t) vdGetImageBackendCount(void);
|
---|
270 | DECLHIDDEN(int) vdQueryImageBackend(uint32_t idx, PCVDIMAGEBACKEND *ppBackend);
|
---|
271 | DECLHIDDEN(int) vdFindImageBackend(const char *pszBackend, PCVDIMAGEBACKEND *ppBackend);
|
---|
272 | DECLHIDDEN(uint32_t) vdGetCacheBackendCount(void);
|
---|
273 | DECLHIDDEN(int) vdQueryCacheBackend(uint32_t idx, PCVDCACHEBACKEND *ppBackend);
|
---|
274 | DECLHIDDEN(int) vdFindCacheBackend(const char *pszBackend, PCVDCACHEBACKEND *ppBackend);
|
---|
275 | DECLHIDDEN(uint32_t) vdGetFilterBackendCount(void);
|
---|
276 | DECLHIDDEN(int) vdQueryFilterBackend(uint32_t idx, PCVDFILTERBACKEND *ppBackend);
|
---|
277 | DECLHIDDEN(int) vdFindFilterBackend(const char *pszFilter, PCVDFILTERBACKEND *ppBackend);
|
---|
278 |
|
---|
279 | DECLHIDDEN(int) vdIoIterQueryStartNext(VDIOITER hVdIoIter, uint64_t *pu64Start);
|
---|
280 | DECLHIDDEN(int) vdIoIterQuerySegSizeByStart(VDIOITER hVdIoIter, uint64_t u64Start, size_t *pcRegSize);
|
---|
281 | DECLHIDDEN(int) vdIoIterAdvance(VDIOITER hVdIoIter, uint64_t cBlocksOrBytes);
|
---|
282 |
|
---|
283 | #endif /* !___VDInternal_h */
|
---|
284 |
|
---|