1 | /** @file
|
---|
2 | * VD Container API - interfaces.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2011 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef ___VBox_VD_Interfaces_h
|
---|
27 | #define ___VBox_VD_Interfaces_h
|
---|
28 |
|
---|
29 | #include <iprt/assert.h>
|
---|
30 | #include <iprt/string.h>
|
---|
31 | #include <iprt/mem.h>
|
---|
32 | #include <iprt/file.h>
|
---|
33 | #include <iprt/net.h>
|
---|
34 | #include <iprt/sg.h>
|
---|
35 | #include <VBox/cdefs.h>
|
---|
36 | #include <VBox/types.h>
|
---|
37 | #include <VBox/err.h>
|
---|
38 |
|
---|
39 | RT_C_DECLS_BEGIN
|
---|
40 |
|
---|
41 | /** Interface header magic. */
|
---|
42 | #define VDINTERFACE_MAGIC UINT32_C(0x19701015)
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Supported interface types.
|
---|
46 | */
|
---|
47 | typedef enum VDINTERFACETYPE
|
---|
48 | {
|
---|
49 | /** First valid interface. */
|
---|
50 | VDINTERFACETYPE_FIRST = 0,
|
---|
51 | /** Interface to pass error message to upper layers. Per-disk. */
|
---|
52 | VDINTERFACETYPE_ERROR = VDINTERFACETYPE_FIRST,
|
---|
53 | /** Interface for I/O operations. Per-image. */
|
---|
54 | VDINTERFACETYPE_IO,
|
---|
55 | /** Interface for progress notification. Per-operation. */
|
---|
56 | VDINTERFACETYPE_PROGRESS,
|
---|
57 | /** Interface for configuration information. Per-image. */
|
---|
58 | VDINTERFACETYPE_CONFIG,
|
---|
59 | /** Interface for TCP network stack. Per-image. */
|
---|
60 | VDINTERFACETYPE_TCPNET,
|
---|
61 | /** Interface for getting parent image state. Per-operation. */
|
---|
62 | VDINTERFACETYPE_PARENTSTATE,
|
---|
63 | /** Interface for synchronizing accesses from several threads. Per-disk. */
|
---|
64 | VDINTERFACETYPE_THREADSYNC,
|
---|
65 | /** Interface for I/O between the generic VBoxHDD code and the backend. Per-image (internal).
|
---|
66 | * This interface is completely internal and must not be used elsewhere. */
|
---|
67 | VDINTERFACETYPE_IOINT,
|
---|
68 | /** Interface to query the use of block ranges on the disk. Per-operation. */
|
---|
69 | VDINTERFACETYPE_QUERYRANGEUSE,
|
---|
70 | /** invalid interface. */
|
---|
71 | VDINTERFACETYPE_INVALID
|
---|
72 | } VDINTERFACETYPE;
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Common structure for all interfaces and at the beginning of all types.
|
---|
76 | */
|
---|
77 | typedef struct VDINTERFACE
|
---|
78 | {
|
---|
79 | uint32_t u32Magic;
|
---|
80 | /** Human readable interface name. */
|
---|
81 | const char *pszInterfaceName;
|
---|
82 | /** Pointer to the next common interface structure. */
|
---|
83 | struct VDINTERFACE *pNext;
|
---|
84 | /** Interface type. */
|
---|
85 | VDINTERFACETYPE enmInterface;
|
---|
86 | /** Size of the interface. */
|
---|
87 | size_t cbSize;
|
---|
88 | /** Opaque user data which is passed on every call. */
|
---|
89 | void *pvUser;
|
---|
90 | } VDINTERFACE;
|
---|
91 | /** Pointer to a VDINTERFACE. */
|
---|
92 | typedef VDINTERFACE *PVDINTERFACE;
|
---|
93 | /** Pointer to a const VDINTERFACE. */
|
---|
94 | typedef const VDINTERFACE *PCVDINTERFACE;
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Helper functions to handle interface lists.
|
---|
98 | *
|
---|
99 | * @note These interface lists are used consistently to pass per-disk,
|
---|
100 | * per-image and/or per-operation callbacks. Those three purposes are strictly
|
---|
101 | * separate. See the individual interface declarations for what context they
|
---|
102 | * apply to. The caller is responsible for ensuring that the lifetime of the
|
---|
103 | * interface descriptors is appropriate for the category of interface.
|
---|
104 | */
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Get a specific interface from a list of interfaces specified by the type.
|
---|
108 | *
|
---|
109 | * @return Pointer to the matching interface or NULL if none was found.
|
---|
110 | * @param pVDIfs Pointer to the VD interface list.
|
---|
111 | * @param enmInterface Interface to search for.
|
---|
112 | */
|
---|
113 | DECLINLINE(PVDINTERFACE) VDInterfaceGet(PVDINTERFACE pVDIfs, VDINTERFACETYPE enmInterface)
|
---|
114 | {
|
---|
115 | AssertMsgReturn( enmInterface >= VDINTERFACETYPE_FIRST
|
---|
116 | && enmInterface < VDINTERFACETYPE_INVALID,
|
---|
117 | ("enmInterface=%u", enmInterface), NULL);
|
---|
118 |
|
---|
119 | while (pVDIfs)
|
---|
120 | {
|
---|
121 | AssertMsgBreak(pVDIfs->u32Magic == VDINTERFACE_MAGIC,
|
---|
122 | ("u32Magic=%#x\n", pVDIfs->u32Magic));
|
---|
123 |
|
---|
124 | if (pVDIfs->enmInterface == enmInterface)
|
---|
125 | return pVDIfs;
|
---|
126 | pVDIfs = pVDIfs->pNext;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /* No matching interface was found. */
|
---|
130 | return NULL;
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Add an interface to a list of interfaces.
|
---|
135 | *
|
---|
136 | * @return VBox status code.
|
---|
137 | * @param pInterface Pointer to an unitialized common interface structure.
|
---|
138 | * @param pszName Name of the interface.
|
---|
139 | * @param enmInterface Type of the interface.
|
---|
140 | * @param pvUser Opaque user data passed on every function call.
|
---|
141 | * @param ppVDIfs Pointer to the VD interface list.
|
---|
142 | */
|
---|
143 | DECLINLINE(int) VDInterfaceAdd(PVDINTERFACE pInterface, const char *pszName,
|
---|
144 | VDINTERFACETYPE enmInterface, void *pvUser,
|
---|
145 | size_t cbInterface, PVDINTERFACE *ppVDIfs)
|
---|
146 | {
|
---|
147 | /* Argument checks. */
|
---|
148 | AssertMsgReturn( enmInterface >= VDINTERFACETYPE_FIRST
|
---|
149 | && enmInterface < VDINTERFACETYPE_INVALID,
|
---|
150 | ("enmInterface=%u", enmInterface), VERR_INVALID_PARAMETER);
|
---|
151 |
|
---|
152 | AssertMsgReturn(VALID_PTR(ppVDIfs),
|
---|
153 | ("pInterfaceList=%#p", ppVDIfs),
|
---|
154 | VERR_INVALID_PARAMETER);
|
---|
155 |
|
---|
156 | /* Fill out interface descriptor. */
|
---|
157 | pInterface->u32Magic = VDINTERFACE_MAGIC;
|
---|
158 | pInterface->cbSize = cbInterface;
|
---|
159 | pInterface->pszInterfaceName = pszName;
|
---|
160 | pInterface->enmInterface = enmInterface;
|
---|
161 | pInterface->pvUser = pvUser;
|
---|
162 | pInterface->pNext = *ppVDIfs;
|
---|
163 |
|
---|
164 | /* Remember the new start of the list. */
|
---|
165 | *ppVDIfs = pInterface;
|
---|
166 |
|
---|
167 | return VINF_SUCCESS;
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Removes an interface from a list of interfaces.
|
---|
172 | *
|
---|
173 | * @return VBox status code
|
---|
174 | * @param pInterface Pointer to an initialized common interface structure to remove.
|
---|
175 | * @param ppVDIfs Pointer to the VD interface list to remove from.
|
---|
176 | */
|
---|
177 | DECLINLINE(int) VDInterfaceRemove(PVDINTERFACE pInterface, PVDINTERFACE *ppVDIfs)
|
---|
178 | {
|
---|
179 | int rc = VERR_NOT_FOUND;
|
---|
180 |
|
---|
181 | /* Argument checks. */
|
---|
182 | AssertMsgReturn(VALID_PTR(pInterface),
|
---|
183 | ("pInterface=%#p", pInterface),
|
---|
184 | VERR_INVALID_PARAMETER);
|
---|
185 |
|
---|
186 | AssertMsgReturn(VALID_PTR(ppVDIfs),
|
---|
187 | ("pInterfaceList=%#p", ppVDIfs),
|
---|
188 | VERR_INVALID_PARAMETER);
|
---|
189 |
|
---|
190 | if (*ppVDIfs)
|
---|
191 | {
|
---|
192 | PVDINTERFACE pPrev = NULL;
|
---|
193 | PVDINTERFACE pCurr = *ppVDIfs;
|
---|
194 |
|
---|
195 | while ( pCurr
|
---|
196 | && (pCurr != pInterface))
|
---|
197 | {
|
---|
198 | pPrev = pCurr;
|
---|
199 | pCurr = pCurr->pNext;
|
---|
200 | }
|
---|
201 |
|
---|
202 | /* First interface */
|
---|
203 | if (!pPrev)
|
---|
204 | {
|
---|
205 | *ppVDIfs = pCurr->pNext;
|
---|
206 | rc = VINF_SUCCESS;
|
---|
207 | }
|
---|
208 | else if (pCurr)
|
---|
209 | {
|
---|
210 | pPrev = pCurr->pNext;
|
---|
211 | rc = VINF_SUCCESS;
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | return rc;
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Interface to deliver error messages (and also informational messages)
|
---|
220 | * to upper layers.
|
---|
221 | *
|
---|
222 | * Per-disk interface. Optional, but think twice if you want to miss the
|
---|
223 | * opportunity of reporting better human-readable error messages.
|
---|
224 | */
|
---|
225 | typedef struct VDINTERFACEERROR
|
---|
226 | {
|
---|
227 | /**
|
---|
228 | * Common interface header.
|
---|
229 | */
|
---|
230 | VDINTERFACE Core;
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Error message callback. Must be able to accept special IPRT format
|
---|
234 | * strings.
|
---|
235 | *
|
---|
236 | * @param pvUser The opaque data passed on container creation.
|
---|
237 | * @param rc The VBox error code.
|
---|
238 | * @param RT_SRC_POS_DECL Use RT_SRC_POS.
|
---|
239 | * @param pszFormat Error message format string.
|
---|
240 | * @param va Error message arguments.
|
---|
241 | */
|
---|
242 | DECLR3CALLBACKMEMBER(void, pfnError, (void *pvUser, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va));
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Informational message callback. May be NULL. Used e.g. in
|
---|
246 | * VDDumpImages(). Must be able to accept special IPRT format strings.
|
---|
247 | *
|
---|
248 | * @return VBox status code.
|
---|
249 | * @param pvUser The opaque data passed on container creation.
|
---|
250 | * @param pszFormat Message format string.
|
---|
251 | * @param va Message arguments.
|
---|
252 | */
|
---|
253 | DECLR3CALLBACKMEMBER(int, pfnMessage, (void *pvUser, const char *pszFormat, va_list va));
|
---|
254 |
|
---|
255 | } VDINTERFACEERROR, *PVDINTERFACEERROR;
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * Get error interface from interface list.
|
---|
259 | *
|
---|
260 | * @return Pointer to the first error interface in the list.
|
---|
261 | * @param pVDIfs Pointer to the interface list.
|
---|
262 | */
|
---|
263 | DECLINLINE(PVDINTERFACEERROR) VDIfErrorGet(PVDINTERFACE pVDIfs)
|
---|
264 | {
|
---|
265 | PVDINTERFACE pIf = VDInterfaceGet(pVDIfs, VDINTERFACETYPE_ERROR);
|
---|
266 |
|
---|
267 | /* Check that the interface descriptor is a progress interface. */
|
---|
268 | AssertMsgReturn( !pIf
|
---|
269 | || ( (pIf->enmInterface == VDINTERFACETYPE_ERROR)
|
---|
270 | && (pIf->cbSize == sizeof(VDINTERFACEERROR))),
|
---|
271 | ("Not an error interface\n"), NULL);
|
---|
272 |
|
---|
273 | return (PVDINTERFACEERROR)pIf;
|
---|
274 | }
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Signal an error to the frontend.
|
---|
278 | *
|
---|
279 | * @returns VBox status code.
|
---|
280 | * @param pIfError The error interface.
|
---|
281 | * @param rc The status code.
|
---|
282 | * @param RT_SRC_POS_DECL The position in the source code.
|
---|
283 | * @param pszFormat The format string to pass.
|
---|
284 | * @param ... Arguments to the format string.
|
---|
285 | */
|
---|
286 | DECLINLINE(int) vdIfError(PVDINTERFACEERROR pIfError, int rc, RT_SRC_POS_DECL,
|
---|
287 | const char *pszFormat, ...)
|
---|
288 | {
|
---|
289 | va_list va;
|
---|
290 | va_start(va, pszFormat);
|
---|
291 | if (pIfError)
|
---|
292 | pIfError->pfnError(pIfError->Core.pvUser, rc, RT_SRC_POS_ARGS, pszFormat, va);
|
---|
293 | va_end(va);
|
---|
294 | return rc;
|
---|
295 | }
|
---|
296 |
|
---|
297 | /**
|
---|
298 | * Signal an informational message to the frontend.
|
---|
299 | *
|
---|
300 | * @returns VBox status code.
|
---|
301 | * @param pIfError The error interface.
|
---|
302 | * @param pszFormat The format string to pass.
|
---|
303 | * @param ... Arguments to the format string.
|
---|
304 | */
|
---|
305 | DECLINLINE(int) vdIfErrorMessage(PVDINTERFACEERROR pIfError, const char *pszFormat, ...)
|
---|
306 | {
|
---|
307 | int rc = VINF_SUCCESS;
|
---|
308 | va_list va;
|
---|
309 | va_start(va, pszFormat);
|
---|
310 | if (pIfError && pIfError->pfnMessage)
|
---|
311 | rc = pIfError->pfnMessage(pIfError->Core.pvUser, pszFormat, va);
|
---|
312 | va_end(va);
|
---|
313 | return rc;
|
---|
314 | }
|
---|
315 |
|
---|
316 | /**
|
---|
317 | * Completion callback which is called by the interface owner
|
---|
318 | * to inform the backend that a task finished.
|
---|
319 | *
|
---|
320 | * @return VBox status code.
|
---|
321 | * @param pvUser Opaque user data which is passed on request submission.
|
---|
322 | * @param rcReq Status code of the completed request.
|
---|
323 | */
|
---|
324 | typedef DECLCALLBACK(int) FNVDCOMPLETED(void *pvUser, int rcReq);
|
---|
325 | /** Pointer to FNVDCOMPLETED() */
|
---|
326 | typedef FNVDCOMPLETED *PFNVDCOMPLETED;
|
---|
327 |
|
---|
328 | /**
|
---|
329 | * Support interface for I/O
|
---|
330 | *
|
---|
331 | * Per-image. Optional as input.
|
---|
332 | */
|
---|
333 | typedef struct VDINTERFACEIO
|
---|
334 | {
|
---|
335 | /**
|
---|
336 | * Common interface header.
|
---|
337 | */
|
---|
338 | VDINTERFACE Core;
|
---|
339 |
|
---|
340 | /**
|
---|
341 | * Open callback
|
---|
342 | *
|
---|
343 | * @return VBox status code.
|
---|
344 | * @param pvUser The opaque data passed on container creation.
|
---|
345 | * @param pszLocation Name of the location to open.
|
---|
346 | * @param fOpen Flags for opening the backend.
|
---|
347 | * See RTFILE_O_* #defines, inventing another set
|
---|
348 | * of open flags is not worth the mapping effort.
|
---|
349 | * @param pfnCompleted The callback which is called whenever a task
|
---|
350 | * completed. The backend has to pass the user data
|
---|
351 | * of the request initiator (ie the one who calls
|
---|
352 | * VDAsyncRead or VDAsyncWrite) in pvCompletion
|
---|
353 | * if this is NULL.
|
---|
354 | * @param ppStorage Where to store the opaque storage handle.
|
---|
355 | */
|
---|
356 | DECLR3CALLBACKMEMBER(int, pfnOpen, (void *pvUser, const char *pszLocation,
|
---|
357 | uint32_t fOpen,
|
---|
358 | PFNVDCOMPLETED pfnCompleted,
|
---|
359 | void **ppStorage));
|
---|
360 |
|
---|
361 | /**
|
---|
362 | * Close callback.
|
---|
363 | *
|
---|
364 | * @return VBox status code.
|
---|
365 | * @param pvUser The opaque data passed on container creation.
|
---|
366 | * @param pStorage The opaque storage handle to close.
|
---|
367 | */
|
---|
368 | DECLR3CALLBACKMEMBER(int, pfnClose, (void *pvUser, void *pStorage));
|
---|
369 |
|
---|
370 | /**
|
---|
371 | * Delete callback.
|
---|
372 | *
|
---|
373 | * @return VBox status code.
|
---|
374 | * @param pvUser The opaque data passed on container creation.
|
---|
375 | * @param pcszFilename Name of the file to delete.
|
---|
376 | */
|
---|
377 | DECLR3CALLBACKMEMBER(int, pfnDelete, (void *pvUser, const char *pcszFilename));
|
---|
378 |
|
---|
379 | /**
|
---|
380 | * Move callback.
|
---|
381 | *
|
---|
382 | * @return VBox status code.
|
---|
383 | * @param pvUser The opaque data passed on container creation.
|
---|
384 | * @param pcszSrc The path to the source file.
|
---|
385 | * @param pcszDst The path to the destination file.
|
---|
386 | * This file will be created.
|
---|
387 | * @param fMove A combination of the RTFILEMOVE_* flags.
|
---|
388 | */
|
---|
389 | DECLR3CALLBACKMEMBER(int, pfnMove, (void *pvUser, const char *pcszSrc, const char *pcszDst, unsigned fMove));
|
---|
390 |
|
---|
391 | /**
|
---|
392 | * Returns the free space on a disk.
|
---|
393 | *
|
---|
394 | * @return VBox status code.
|
---|
395 | * @param pvUser The opaque data passed on container creation.
|
---|
396 | * @param pcszFilename Name of a file to identify the disk.
|
---|
397 | * @param pcbFreeSpace Where to store the free space of the disk.
|
---|
398 | */
|
---|
399 | DECLR3CALLBACKMEMBER(int, pfnGetFreeSpace, (void *pvUser, const char *pcszFilename, int64_t *pcbFreeSpace));
|
---|
400 |
|
---|
401 | /**
|
---|
402 | * Returns the last modification timestamp of a file.
|
---|
403 | *
|
---|
404 | * @return VBox status code.
|
---|
405 | * @param pvUser The opaque data passed on container creation.
|
---|
406 | * @param pcszFilename Name of a file to identify the disk.
|
---|
407 | * @param pModificationTime Where to store the timestamp of the file.
|
---|
408 | */
|
---|
409 | DECLR3CALLBACKMEMBER(int, pfnGetModificationTime, (void *pvUser, const char *pcszFilename, PRTTIMESPEC pModificationTime));
|
---|
410 |
|
---|
411 | /**
|
---|
412 | * Returns the size of the opened storage backend.
|
---|
413 | *
|
---|
414 | * @return VBox status code.
|
---|
415 | * @param pvUser The opaque data passed on container creation.
|
---|
416 | * @param pStorage The opaque storage handle to close.
|
---|
417 | * @param pcbSize Where to store the size of the storage backend.
|
---|
418 | */
|
---|
419 | DECLR3CALLBACKMEMBER(int, pfnGetSize, (void *pvUser, void *pStorage, uint64_t *pcbSize));
|
---|
420 |
|
---|
421 | /**
|
---|
422 | * Sets the size of the opened storage backend if possible.
|
---|
423 | *
|
---|
424 | * @return VBox status code.
|
---|
425 | * @retval VERR_NOT_SUPPORTED if the backend does not support this operation.
|
---|
426 | * @param pvUser The opaque data passed on container creation.
|
---|
427 | * @param pStorage The opaque storage handle to close.
|
---|
428 | * @param cbSize The new size of the image.
|
---|
429 | */
|
---|
430 | DECLR3CALLBACKMEMBER(int, pfnSetSize, (void *pvUser, void *pStorage, uint64_t cbSize));
|
---|
431 |
|
---|
432 | /**
|
---|
433 | * Synchronous write callback.
|
---|
434 | *
|
---|
435 | * @return VBox status code.
|
---|
436 | * @param pvUser The opaque data passed on container creation.
|
---|
437 | * @param pStorage The storage handle to use.
|
---|
438 | * @param uOffset The offset to start from.
|
---|
439 | * @param pvBuffer Pointer to the bits need to be written.
|
---|
440 | * @param cbBuffer How many bytes to write.
|
---|
441 | * @param pcbWritten Where to store how many bytes were actually written.
|
---|
442 | */
|
---|
443 | DECLR3CALLBACKMEMBER(int, pfnWriteSync, (void *pvUser, void *pStorage, uint64_t uOffset,
|
---|
444 | const void *pvBuffer, size_t cbBuffer, size_t *pcbWritten));
|
---|
445 |
|
---|
446 | /**
|
---|
447 | * Synchronous read callback.
|
---|
448 | *
|
---|
449 | * @return VBox status code.
|
---|
450 | * @param pvUser The opaque data passed on container creation.
|
---|
451 | * @param pStorage The storage handle to use.
|
---|
452 | * @param uOffset The offset to start from.
|
---|
453 | * @param pvBuffer Where to store the read bits.
|
---|
454 | * @param cbBuffer How many bytes to read.
|
---|
455 | * @param pcbRead Where to store how many bytes were actually read.
|
---|
456 | */
|
---|
457 | DECLR3CALLBACKMEMBER(int, pfnReadSync, (void *pvUser, void *pStorage, uint64_t uOffset,
|
---|
458 | void *pvBuffer, size_t cbBuffer, size_t *pcbRead));
|
---|
459 |
|
---|
460 | /**
|
---|
461 | * Flush data to the storage backend.
|
---|
462 | *
|
---|
463 | * @return VBox status code.
|
---|
464 | * @param pvUser The opaque data passed on container creation.
|
---|
465 | * @param pStorage The storage handle to flush.
|
---|
466 | */
|
---|
467 | DECLR3CALLBACKMEMBER(int, pfnFlushSync, (void *pvUser, void *pStorage));
|
---|
468 |
|
---|
469 | /**
|
---|
470 | * Initiate an asynchronous read request.
|
---|
471 | *
|
---|
472 | * @return VBox status code.
|
---|
473 | * @param pvUser The opaque user data passed on container creation.
|
---|
474 | * @param pStorage The storage handle.
|
---|
475 | * @param uOffset The offset to start reading from.
|
---|
476 | * @param paSegments Scatter gather list to store the data in.
|
---|
477 | * @param cSegments Number of segments in the list.
|
---|
478 | * @param cbRead How many bytes to read.
|
---|
479 | * @param pvCompletion The opaque user data which is returned upon completion.
|
---|
480 | * @param ppTask Where to store the opaque task handle.
|
---|
481 | */
|
---|
482 | DECLR3CALLBACKMEMBER(int, pfnReadAsync, (void *pvUser, void *pStorage, uint64_t uOffset,
|
---|
483 | PCRTSGSEG paSegments, size_t cSegments,
|
---|
484 | size_t cbRead, void *pvCompletion,
|
---|
485 | void **ppTask));
|
---|
486 |
|
---|
487 | /**
|
---|
488 | * Initiate an asynchronous write request.
|
---|
489 | *
|
---|
490 | * @return VBox status code.
|
---|
491 | * @param pvUser The opaque user data passed on conatiner creation.
|
---|
492 | * @param pStorage The storage handle.
|
---|
493 | * @param uOffset The offset to start writing to.
|
---|
494 | * @param paSegments Scatter gather list of the data to write
|
---|
495 | * @param cSegments Number of segments in the list.
|
---|
496 | * @param cbWrite How many bytes to write.
|
---|
497 | * @param pvCompletion The opaque user data which is returned upon completion.
|
---|
498 | * @param ppTask Where to store the opaque task handle.
|
---|
499 | */
|
---|
500 | DECLR3CALLBACKMEMBER(int, pfnWriteAsync, (void *pvUser, void *pStorage, uint64_t uOffset,
|
---|
501 | PCRTSGSEG paSegments, size_t cSegments,
|
---|
502 | size_t cbWrite, void *pvCompletion,
|
---|
503 | void **ppTask));
|
---|
504 |
|
---|
505 | /**
|
---|
506 | * Initiates an async flush request.
|
---|
507 | *
|
---|
508 | * @return VBox status code.
|
---|
509 | * @param pvUser The opaque data passed on container creation.
|
---|
510 | * @param pStorage The storage handle to flush.
|
---|
511 | * @param pvCompletion The opaque user data which is returned upon completion.
|
---|
512 | * @param ppTask Where to store the opaque task handle.
|
---|
513 | */
|
---|
514 | DECLR3CALLBACKMEMBER(int, pfnFlushAsync, (void *pvUser, void *pStorage,
|
---|
515 | void *pvCompletion, void **ppTask));
|
---|
516 |
|
---|
517 | } VDINTERFACEIO, *PVDINTERFACEIO;
|
---|
518 |
|
---|
519 | /**
|
---|
520 | * Get I/O interface from interface list.
|
---|
521 | *
|
---|
522 | * @return Pointer to the first I/O interface in the list.
|
---|
523 | * @param pVDIfs Pointer to the interface list.
|
---|
524 | */
|
---|
525 | DECLINLINE(PVDINTERFACEIO) VDIfIoGet(PVDINTERFACE pVDIfs)
|
---|
526 | {
|
---|
527 | PVDINTERFACE pIf = VDInterfaceGet(pVDIfs, VDINTERFACETYPE_IO);
|
---|
528 |
|
---|
529 | /* Check that the interface descriptor is a progress interface. */
|
---|
530 | AssertMsgReturn( !pIf
|
---|
531 | || ( (pIf->enmInterface == VDINTERFACETYPE_IO)
|
---|
532 | && (pIf->cbSize == sizeof(VDINTERFACEIO))),
|
---|
533 | ("Not a I/O interface"), NULL);
|
---|
534 |
|
---|
535 | return (PVDINTERFACEIO)pIf;
|
---|
536 | }
|
---|
537 |
|
---|
538 | DECLINLINE(int) vdIfIoFileOpen(PVDINTERFACEIO pIfIo, const char *pszFilename,
|
---|
539 | uint32_t fOpen, PFNVDCOMPLETED pfnCompleted,
|
---|
540 | void **ppStorage)
|
---|
541 | {
|
---|
542 | return pIfIo->pfnOpen(pIfIo->Core.pvUser, pszFilename, fOpen, pfnCompleted, ppStorage);
|
---|
543 | }
|
---|
544 |
|
---|
545 | DECLINLINE(int) vdIfIoFileClose(PVDINTERFACEIO pIfIo, void *pStorage)
|
---|
546 | {
|
---|
547 | return pIfIo->pfnClose(pIfIo->Core.pvUser, pStorage);
|
---|
548 | }
|
---|
549 |
|
---|
550 | DECLINLINE(int) vdIfIoFileDelete(PVDINTERFACEIO pIfIo, const char *pszFilename)
|
---|
551 | {
|
---|
552 | return pIfIo->pfnDelete(pIfIo->Core.pvUser, pszFilename);
|
---|
553 | }
|
---|
554 |
|
---|
555 | DECLINLINE(int) vdIfIoFileMove(PVDINTERFACEIO pIfIo, const char *pszSrc,
|
---|
556 | const char *pszDst, unsigned fMove)
|
---|
557 | {
|
---|
558 | return pIfIo->pfnMove(pIfIo->Core.pvUser, pszSrc, pszDst, fMove);
|
---|
559 | }
|
---|
560 |
|
---|
561 | DECLINLINE(int) vdIfIoFileGetFreeSpace(PVDINTERFACEIO pIfIo, const char *pszFilename,
|
---|
562 | int64_t *pcbFree)
|
---|
563 | {
|
---|
564 | return pIfIo->pfnGetFreeSpace(pIfIo->Core.pvUser, pszFilename, pcbFree);
|
---|
565 | }
|
---|
566 |
|
---|
567 | DECLINLINE(int) vdIfIoFileGetModificationTime(PVDINTERFACEIO pIfIo, const char *pcszFilename,
|
---|
568 | PRTTIMESPEC pModificationTime)
|
---|
569 | {
|
---|
570 | return pIfIo->pfnGetModificationTime(pIfIo->Core.pvUser, pcszFilename,
|
---|
571 | pModificationTime);
|
---|
572 | }
|
---|
573 |
|
---|
574 | DECLINLINE(int) vdIfIoFileGetSize(PVDINTERFACEIO pIfIo, void *pStorage,
|
---|
575 | uint64_t *pcbSize)
|
---|
576 | {
|
---|
577 | return pIfIo->pfnGetSize(pIfIo->Core.pvUser, pStorage, pcbSize);
|
---|
578 | }
|
---|
579 |
|
---|
580 | DECLINLINE(int) vdIfIoFileSetSize(PVDINTERFACEIO pIfIo, void *pStorage,
|
---|
581 | uint64_t cbSize)
|
---|
582 | {
|
---|
583 | return pIfIo->pfnSetSize(pIfIo->Core.pvUser, pStorage, cbSize);
|
---|
584 | }
|
---|
585 |
|
---|
586 | DECLINLINE(int) vdIfIoFileWriteSync(PVDINTERFACEIO pIfIo, void *pStorage,
|
---|
587 | uint64_t uOffset, const void *pvBuffer, size_t cbBuffer,
|
---|
588 | size_t *pcbWritten)
|
---|
589 | {
|
---|
590 | return pIfIo->pfnWriteSync(pIfIo->Core.pvUser, pStorage, uOffset,
|
---|
591 | pvBuffer, cbBuffer, pcbWritten);
|
---|
592 | }
|
---|
593 |
|
---|
594 | DECLINLINE(int) vdIfIoFileReadSync(PVDINTERFACEIO pIfIo, void *pStorage,
|
---|
595 | uint64_t uOffset, void *pvBuffer, size_t cbBuffer,
|
---|
596 | size_t *pcbRead)
|
---|
597 | {
|
---|
598 | return pIfIo->pfnReadSync(pIfIo->Core.pvUser, pStorage, uOffset,
|
---|
599 | pvBuffer, cbBuffer, pcbRead);
|
---|
600 | }
|
---|
601 |
|
---|
602 | DECLINLINE(int) vdIfIoFileFlushSync(PVDINTERFACEIO pIfIo, void *pStorage)
|
---|
603 | {
|
---|
604 | return pIfIo->pfnFlushSync(pIfIo->Core.pvUser, pStorage);
|
---|
605 | }
|
---|
606 |
|
---|
607 | /**
|
---|
608 | * Callback which provides progress information about a currently running
|
---|
609 | * lengthy operation.
|
---|
610 | *
|
---|
611 | * @return VBox status code.
|
---|
612 | * @param pvUser The opaque user data associated with this interface.
|
---|
613 | * @param uPercent Completion percentage.
|
---|
614 | */
|
---|
615 | typedef DECLCALLBACK(int) FNVDPROGRESS(void *pvUser, unsigned uPercentage);
|
---|
616 | /** Pointer to FNVDPROGRESS() */
|
---|
617 | typedef FNVDPROGRESS *PFNVDPROGRESS;
|
---|
618 |
|
---|
619 | /**
|
---|
620 | * Progress notification interface
|
---|
621 | *
|
---|
622 | * Per-operation. Optional.
|
---|
623 | */
|
---|
624 | typedef struct VDINTERFACEPROGRESS
|
---|
625 | {
|
---|
626 | /**
|
---|
627 | * Common interface header.
|
---|
628 | */
|
---|
629 | VDINTERFACE Core;
|
---|
630 |
|
---|
631 | /**
|
---|
632 | * Progress notification callbacks.
|
---|
633 | */
|
---|
634 | PFNVDPROGRESS pfnProgress;
|
---|
635 |
|
---|
636 | } VDINTERFACEPROGRESS, *PVDINTERFACEPROGRESS;
|
---|
637 |
|
---|
638 | /**
|
---|
639 | * Get progress interface from interface list.
|
---|
640 | *
|
---|
641 | * @return Pointer to the first progress interface in the list.
|
---|
642 | * @param pVDIfs Pointer to the interface list.
|
---|
643 | */
|
---|
644 | DECLINLINE(PVDINTERFACEPROGRESS) VDIfProgressGet(PVDINTERFACE pVDIfs)
|
---|
645 | {
|
---|
646 | PVDINTERFACE pIf = VDInterfaceGet(pVDIfs, VDINTERFACETYPE_PROGRESS);
|
---|
647 |
|
---|
648 | /* Check that the interface descriptor is a progress interface. */
|
---|
649 | AssertMsgReturn( !pIf
|
---|
650 | || ( (pIf->enmInterface == VDINTERFACETYPE_PROGRESS)
|
---|
651 | && (pIf->cbSize == sizeof(VDINTERFACEPROGRESS))),
|
---|
652 | ("Not a progress interface"), NULL);
|
---|
653 |
|
---|
654 | return (PVDINTERFACEPROGRESS)pIf;
|
---|
655 | }
|
---|
656 |
|
---|
657 |
|
---|
658 | /**
|
---|
659 | * Configuration information interface
|
---|
660 | *
|
---|
661 | * Per-image. Optional for most backends, but mandatory for images which do
|
---|
662 | * not operate on files (including standard block or character devices).
|
---|
663 | */
|
---|
664 | typedef struct VDINTERFACECONFIG
|
---|
665 | {
|
---|
666 | /**
|
---|
667 | * Common interface header.
|
---|
668 | */
|
---|
669 | VDINTERFACE Core;
|
---|
670 |
|
---|
671 | /**
|
---|
672 | * Validates that the keys are within a set of valid names.
|
---|
673 | *
|
---|
674 | * @return true if all key names are found in pszzAllowed.
|
---|
675 | * @return false if not.
|
---|
676 | * @param pvUser The opaque user data associated with this interface.
|
---|
677 | * @param pszzValid List of valid key names separated by '\\0' and ending with
|
---|
678 | * a double '\\0'.
|
---|
679 | */
|
---|
680 | DECLR3CALLBACKMEMBER(bool, pfnAreKeysValid, (void *pvUser, const char *pszzValid));
|
---|
681 |
|
---|
682 | /**
|
---|
683 | * Retrieves the length of the string value associated with a key (including
|
---|
684 | * the terminator, for compatibility with CFGMR3QuerySize).
|
---|
685 | *
|
---|
686 | * @return VBox status code.
|
---|
687 | * VERR_CFGM_VALUE_NOT_FOUND means that the key is not known.
|
---|
688 | * @param pvUser The opaque user data associated with this interface.
|
---|
689 | * @param pszName Name of the key to query.
|
---|
690 | * @param pcbValue Where to store the value length. Non-NULL.
|
---|
691 | */
|
---|
692 | DECLR3CALLBACKMEMBER(int, pfnQuerySize, (void *pvUser, const char *pszName, size_t *pcbValue));
|
---|
693 |
|
---|
694 | /**
|
---|
695 | * Query the string value associated with a key.
|
---|
696 | *
|
---|
697 | * @return VBox status code.
|
---|
698 | * VERR_CFGM_VALUE_NOT_FOUND means that the key is not known.
|
---|
699 | * VERR_CFGM_NOT_ENOUGH_SPACE means that the buffer is not big enough.
|
---|
700 | * @param pvUser The opaque user data associated with this interface.
|
---|
701 | * @param pszName Name of the key to query.
|
---|
702 | * @param pszValue Pointer to buffer where to store value.
|
---|
703 | * @param cchValue Length of value buffer.
|
---|
704 | */
|
---|
705 | DECLR3CALLBACKMEMBER(int, pfnQuery, (void *pvUser, const char *pszName, char *pszValue, size_t cchValue));
|
---|
706 |
|
---|
707 | } VDINTERFACECONFIG, *PVDINTERFACECONFIG;
|
---|
708 |
|
---|
709 | /**
|
---|
710 | * Get configuration information interface from interface list.
|
---|
711 | *
|
---|
712 | * @return Pointer to the first configuration information interface in the list.
|
---|
713 | * @param pVDIfs Pointer to the interface list.
|
---|
714 | */
|
---|
715 | DECLINLINE(PVDINTERFACECONFIG) VDIfConfigGet(PVDINTERFACE pVDIfs)
|
---|
716 | {
|
---|
717 | PVDINTERFACE pIf = VDInterfaceGet(pVDIfs, VDINTERFACETYPE_CONFIG);
|
---|
718 |
|
---|
719 | /* Check that the interface descriptor is a progress interface. */
|
---|
720 | AssertMsgReturn( !pIf
|
---|
721 | || ( (pIf->enmInterface == VDINTERFACETYPE_CONFIG)
|
---|
722 | && (pIf->cbSize == sizeof(VDINTERFACECONFIG))),
|
---|
723 | ("Not a config interface"), NULL);
|
---|
724 |
|
---|
725 | return (PVDINTERFACECONFIG)pIf;
|
---|
726 | }
|
---|
727 |
|
---|
728 | /**
|
---|
729 | * Query configuration, validates that the keys are within a set of valid names.
|
---|
730 | *
|
---|
731 | * @return true if all key names are found in pszzAllowed.
|
---|
732 | * @return false if not.
|
---|
733 | * @param pCfgIf Pointer to configuration callback table.
|
---|
734 | * @param pszzValid List of valid names separated by '\\0' and ending with
|
---|
735 | * a double '\\0'.
|
---|
736 | */
|
---|
737 | DECLINLINE(bool) VDCFGAreKeysValid(PVDINTERFACECONFIG pCfgIf, const char *pszzValid)
|
---|
738 | {
|
---|
739 | return pCfgIf->pfnAreKeysValid(pCfgIf->Core.pvUser, pszzValid);
|
---|
740 | }
|
---|
741 |
|
---|
742 | /**
|
---|
743 | * Query configuration, unsigned 64-bit integer value with default.
|
---|
744 | *
|
---|
745 | * @return VBox status code.
|
---|
746 | * @param pCfgIf Pointer to configuration callback table.
|
---|
747 | * @param pszName Name of an integer value
|
---|
748 | * @param pu64 Where to store the value. Set to default on failure.
|
---|
749 | * @param u64Def The default value.
|
---|
750 | */
|
---|
751 | DECLINLINE(int) VDCFGQueryU64Def(PVDINTERFACECONFIG pCfgIf,
|
---|
752 | const char *pszName, uint64_t *pu64,
|
---|
753 | uint64_t u64Def)
|
---|
754 | {
|
---|
755 | char aszBuf[32];
|
---|
756 | int rc = pCfgIf->pfnQuery(pCfgIf->Core.pvUser, pszName, aszBuf, sizeof(aszBuf));
|
---|
757 | if (RT_SUCCESS(rc))
|
---|
758 | {
|
---|
759 | rc = RTStrToUInt64Full(aszBuf, 0, pu64);
|
---|
760 | }
|
---|
761 | else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
762 | {
|
---|
763 | rc = VINF_SUCCESS;
|
---|
764 | *pu64 = u64Def;
|
---|
765 | }
|
---|
766 | return rc;
|
---|
767 | }
|
---|
768 |
|
---|
769 | /**
|
---|
770 | * Query configuration, unsigned 32-bit integer value with default.
|
---|
771 | *
|
---|
772 | * @return VBox status code.
|
---|
773 | * @param pCfgIf Pointer to configuration callback table.
|
---|
774 | * @param pszName Name of an integer value
|
---|
775 | * @param pu32 Where to store the value. Set to default on failure.
|
---|
776 | * @param u32Def The default value.
|
---|
777 | */
|
---|
778 | DECLINLINE(int) VDCFGQueryU32Def(PVDINTERFACECONFIG pCfgIf,
|
---|
779 | const char *pszName, uint32_t *pu32,
|
---|
780 | uint32_t u32Def)
|
---|
781 | {
|
---|
782 | uint64_t u64;
|
---|
783 | int rc = VDCFGQueryU64Def(pCfgIf, pszName, &u64, u32Def);
|
---|
784 | if (RT_SUCCESS(rc))
|
---|
785 | {
|
---|
786 | if (!(u64 & UINT64_C(0xffffffff00000000)))
|
---|
787 | *pu32 = (uint32_t)u64;
|
---|
788 | else
|
---|
789 | rc = VERR_CFGM_INTEGER_TOO_BIG;
|
---|
790 | }
|
---|
791 | return rc;
|
---|
792 | }
|
---|
793 |
|
---|
794 | /**
|
---|
795 | * Query configuration, bool value with default.
|
---|
796 | *
|
---|
797 | * @return VBox status code.
|
---|
798 | * @param pCfgIf Pointer to configuration callback table.
|
---|
799 | * @param pszName Name of an integer value
|
---|
800 | * @param pf Where to store the value. Set to default on failure.
|
---|
801 | * @param fDef The default value.
|
---|
802 | */
|
---|
803 | DECLINLINE(int) VDCFGQueryBoolDef(PVDINTERFACECONFIG pCfgIf,
|
---|
804 | const char *pszName, bool *pf,
|
---|
805 | bool fDef)
|
---|
806 | {
|
---|
807 | uint64_t u64;
|
---|
808 | int rc = VDCFGQueryU64Def(pCfgIf, pszName, &u64, fDef);
|
---|
809 | if (RT_SUCCESS(rc))
|
---|
810 | *pf = u64 ? true : false;
|
---|
811 | return rc;
|
---|
812 | }
|
---|
813 |
|
---|
814 | /**
|
---|
815 | * Query configuration, dynamically allocated (RTMemAlloc) zero terminated
|
---|
816 | * character value.
|
---|
817 | *
|
---|
818 | * @return VBox status code.
|
---|
819 | * @param pCfgIf Pointer to configuration callback table.
|
---|
820 | * @param pszName Name of an zero terminated character value
|
---|
821 | * @param ppszString Where to store the string pointer. Not set on failure.
|
---|
822 | * Free this using RTMemFree().
|
---|
823 | */
|
---|
824 | DECLINLINE(int) VDCFGQueryStringAlloc(PVDINTERFACECONFIG pCfgIf,
|
---|
825 | const char *pszName, char **ppszString)
|
---|
826 | {
|
---|
827 | size_t cb;
|
---|
828 | int rc = pCfgIf->pfnQuerySize(pCfgIf->Core.pvUser, pszName, &cb);
|
---|
829 | if (RT_SUCCESS(rc))
|
---|
830 | {
|
---|
831 | char *pszString = (char *)RTMemAlloc(cb);
|
---|
832 | if (pszString)
|
---|
833 | {
|
---|
834 | rc = pCfgIf->pfnQuery(pCfgIf->Core.pvUser, pszName, pszString, cb);
|
---|
835 | if (RT_SUCCESS(rc))
|
---|
836 | *ppszString = pszString;
|
---|
837 | else
|
---|
838 | RTMemFree(pszString);
|
---|
839 | }
|
---|
840 | else
|
---|
841 | rc = VERR_NO_MEMORY;
|
---|
842 | }
|
---|
843 | return rc;
|
---|
844 | }
|
---|
845 |
|
---|
846 | /**
|
---|
847 | * Query configuration, dynamically allocated (RTMemAlloc) zero terminated
|
---|
848 | * character value with default.
|
---|
849 | *
|
---|
850 | * @return VBox status code.
|
---|
851 | * @param pCfgIf Pointer to configuration callback table.
|
---|
852 | * @param pszName Name of an zero terminated character value
|
---|
853 | * @param ppszString Where to store the string pointer. Not set on failure.
|
---|
854 | * Free this using RTMemFree().
|
---|
855 | * @param pszDef The default value.
|
---|
856 | */
|
---|
857 | DECLINLINE(int) VDCFGQueryStringAllocDef(PVDINTERFACECONFIG pCfgIf,
|
---|
858 | const char *pszName,
|
---|
859 | char **ppszString,
|
---|
860 | const char *pszDef)
|
---|
861 | {
|
---|
862 | size_t cb;
|
---|
863 | int rc = pCfgIf->pfnQuerySize(pCfgIf->Core.pvUser, pszName, &cb);
|
---|
864 | if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
|
---|
865 | {
|
---|
866 | cb = strlen(pszDef) + 1;
|
---|
867 | rc = VINF_SUCCESS;
|
---|
868 | }
|
---|
869 | if (RT_SUCCESS(rc))
|
---|
870 | {
|
---|
871 | char *pszString = (char *)RTMemAlloc(cb);
|
---|
872 | if (pszString)
|
---|
873 | {
|
---|
874 | rc = pCfgIf->pfnQuery(pCfgIf->Core.pvUser, pszName, pszString, cb);
|
---|
875 | if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
|
---|
876 | {
|
---|
877 | memcpy(pszString, pszDef, cb);
|
---|
878 | rc = VINF_SUCCESS;
|
---|
879 | }
|
---|
880 | if (RT_SUCCESS(rc))
|
---|
881 | *ppszString = pszString;
|
---|
882 | else
|
---|
883 | RTMemFree(pszString);
|
---|
884 | }
|
---|
885 | else
|
---|
886 | rc = VERR_NO_MEMORY;
|
---|
887 | }
|
---|
888 | return rc;
|
---|
889 | }
|
---|
890 |
|
---|
891 | /**
|
---|
892 | * Query configuration, dynamically allocated (RTMemAlloc) byte string value.
|
---|
893 | *
|
---|
894 | * @return VBox status code.
|
---|
895 | * @param pCfgIf Pointer to configuration callback table.
|
---|
896 | * @param pszName Name of an zero terminated character value
|
---|
897 | * @param ppvData Where to store the byte string pointer. Not set on failure.
|
---|
898 | * Free this using RTMemFree().
|
---|
899 | * @param pcbData Where to store the byte string length.
|
---|
900 | */
|
---|
901 | DECLINLINE(int) VDCFGQueryBytesAlloc(PVDINTERFACECONFIG pCfgIf,
|
---|
902 | const char *pszName, void **ppvData, size_t *pcbData)
|
---|
903 | {
|
---|
904 | size_t cb;
|
---|
905 | int rc = pCfgIf->pfnQuerySize(pCfgIf->Core.pvUser, pszName, &cb);
|
---|
906 | if (RT_SUCCESS(rc))
|
---|
907 | {
|
---|
908 | char *pbData;
|
---|
909 | Assert(cb);
|
---|
910 |
|
---|
911 | pbData = (char *)RTMemAlloc(cb);
|
---|
912 | if (pbData)
|
---|
913 | {
|
---|
914 | rc = pCfgIf->pfnQuery(pCfgIf->Core.pvUser, pszName, pbData, cb);
|
---|
915 | if (RT_SUCCESS(rc))
|
---|
916 | {
|
---|
917 | *ppvData = pbData;
|
---|
918 | *pcbData = cb - 1; /* Exclude terminator of the queried string. */
|
---|
919 | }
|
---|
920 | else
|
---|
921 | RTMemFree(pbData);
|
---|
922 | }
|
---|
923 | else
|
---|
924 | rc = VERR_NO_MEMORY;
|
---|
925 | }
|
---|
926 | return rc;
|
---|
927 | }
|
---|
928 |
|
---|
929 | /** Forward declaration of a VD socket. */
|
---|
930 | typedef struct VDSOCKETINT *VDSOCKET;
|
---|
931 | /** Pointer to a VD socket. */
|
---|
932 | typedef VDSOCKET *PVDSOCKET;
|
---|
933 | /** Nil socket handle. */
|
---|
934 | #define NIL_VDSOCKET ((VDSOCKET)0)
|
---|
935 |
|
---|
936 | /** Connect flag to indicate that the backend wants to use the extended
|
---|
937 | * socket I/O multiplexing call. This might not be supported on all configurations
|
---|
938 | * (internal networking and iSCSI)
|
---|
939 | * and the backend needs to take appropriate action.
|
---|
940 | */
|
---|
941 | #define VD_INTERFACETCPNET_CONNECT_EXTENDED_SELECT RT_BIT_32(0)
|
---|
942 |
|
---|
943 | /** @name Select events
|
---|
944 | * @{ */
|
---|
945 | /** Readable without blocking. */
|
---|
946 | #define VD_INTERFACETCPNET_EVT_READ RT_BIT_32(0)
|
---|
947 | /** Writable without blocking. */
|
---|
948 | #define VD_INTERFACETCPNET_EVT_WRITE RT_BIT_32(1)
|
---|
949 | /** Error condition, hangup, exception or similar. */
|
---|
950 | #define VD_INTERFACETCPNET_EVT_ERROR RT_BIT_32(2)
|
---|
951 | /** Hint for the select that getting interrupted while waiting is more likely.
|
---|
952 | * The interface implementation can optimize the waiting strategy based on this.
|
---|
953 | * It is assumed that it is more likely to get one of the above socket events
|
---|
954 | * instead of being interrupted if the flag is not set. */
|
---|
955 | #define VD_INTERFACETCPNET_HINT_INTERRUPT RT_BIT_32(3)
|
---|
956 | /** Mask of the valid bits. */
|
---|
957 | #define VD_INTERFACETCPNET_EVT_VALID_MASK UINT32_C(0x0000000f)
|
---|
958 | /** @} */
|
---|
959 |
|
---|
960 | /**
|
---|
961 | * TCP network stack interface
|
---|
962 | *
|
---|
963 | * Per-image. Mandatory for backends which have the VD_CAP_TCPNET bit set.
|
---|
964 | */
|
---|
965 | typedef struct VDINTERFACETCPNET
|
---|
966 | {
|
---|
967 | /**
|
---|
968 | * Common interface header.
|
---|
969 | */
|
---|
970 | VDINTERFACE Core;
|
---|
971 |
|
---|
972 | /**
|
---|
973 | * Creates a socket. The socket is not connected if this succeeds.
|
---|
974 | *
|
---|
975 | * @return iprt status code.
|
---|
976 | * @retval VERR_NOT_SUPPORTED if the combination of flags is not supported.
|
---|
977 | * @param fFlags Combination of the VD_INTERFACETCPNET_CONNECT_* #defines.
|
---|
978 | * @param pSock Where to store the handle.
|
---|
979 | */
|
---|
980 | DECLR3CALLBACKMEMBER(int, pfnSocketCreate, (uint32_t fFlags, PVDSOCKET pSock));
|
---|
981 |
|
---|
982 | /**
|
---|
983 | * Destroys the socket.
|
---|
984 | *
|
---|
985 | * @return iprt status code.
|
---|
986 | * @param Sock Socket descriptor.
|
---|
987 | */
|
---|
988 | DECLR3CALLBACKMEMBER(int, pfnSocketDestroy, (VDSOCKET Sock));
|
---|
989 |
|
---|
990 | /**
|
---|
991 | * Connect as a client to a TCP port.
|
---|
992 | *
|
---|
993 | * @return iprt status code.
|
---|
994 | * @param Sock Socket descriptor.
|
---|
995 | * @param pszAddress The address to connect to.
|
---|
996 | * @param uPort The port to connect to.
|
---|
997 | */
|
---|
998 | DECLR3CALLBACKMEMBER(int, pfnClientConnect, (VDSOCKET Sock, const char *pszAddress, uint32_t uPort));
|
---|
999 |
|
---|
1000 | /**
|
---|
1001 | * Close a TCP connection.
|
---|
1002 | *
|
---|
1003 | * @return iprt status code.
|
---|
1004 | * @param Sock Socket descriptor.
|
---|
1005 | */
|
---|
1006 | DECLR3CALLBACKMEMBER(int, pfnClientClose, (VDSOCKET Sock));
|
---|
1007 |
|
---|
1008 | /**
|
---|
1009 | * Returns whether the socket is currently connected to the client.
|
---|
1010 | *
|
---|
1011 | * @returns true if the socket is connected.
|
---|
1012 | * false otherwise.
|
---|
1013 | * @param Sock Socket descriptor.
|
---|
1014 | */
|
---|
1015 | DECLR3CALLBACKMEMBER(bool, pfnIsClientConnected, (VDSOCKET Sock));
|
---|
1016 |
|
---|
1017 | /**
|
---|
1018 | * Socket I/O multiplexing.
|
---|
1019 | * Checks if the socket is ready for reading.
|
---|
1020 | *
|
---|
1021 | * @return iprt status code.
|
---|
1022 | * @param Sock Socket descriptor.
|
---|
1023 | * @param cMillies Number of milliseconds to wait for the socket.
|
---|
1024 | * Use RT_INDEFINITE_WAIT to wait for ever.
|
---|
1025 | */
|
---|
1026 | DECLR3CALLBACKMEMBER(int, pfnSelectOne, (VDSOCKET Sock, RTMSINTERVAL cMillies));
|
---|
1027 |
|
---|
1028 | /**
|
---|
1029 | * Receive data from a socket.
|
---|
1030 | *
|
---|
1031 | * @return iprt status code.
|
---|
1032 | * @param Sock Socket descriptor.
|
---|
1033 | * @param pvBuffer Where to put the data we read.
|
---|
1034 | * @param cbBuffer Read buffer size.
|
---|
1035 | * @param pcbRead Number of bytes read.
|
---|
1036 | * If NULL the entire buffer will be filled upon successful return.
|
---|
1037 | * If not NULL a partial read can be done successfully.
|
---|
1038 | */
|
---|
1039 | DECLR3CALLBACKMEMBER(int, pfnRead, (VDSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead));
|
---|
1040 |
|
---|
1041 | /**
|
---|
1042 | * Send data to a socket.
|
---|
1043 | *
|
---|
1044 | * @return iprt status code.
|
---|
1045 | * @param Sock Socket descriptor.
|
---|
1046 | * @param pvBuffer Buffer to write data to socket.
|
---|
1047 | * @param cbBuffer How much to write.
|
---|
1048 | */
|
---|
1049 | DECLR3CALLBACKMEMBER(int, pfnWrite, (VDSOCKET Sock, const void *pvBuffer, size_t cbBuffer));
|
---|
1050 |
|
---|
1051 | /**
|
---|
1052 | * Send data from scatter/gather buffer to a socket.
|
---|
1053 | *
|
---|
1054 | * @return iprt status code.
|
---|
1055 | * @param Sock Socket descriptor.
|
---|
1056 | * @param pSgBuffer Scatter/gather buffer to write data to socket.
|
---|
1057 | */
|
---|
1058 | DECLR3CALLBACKMEMBER(int, pfnSgWrite, (VDSOCKET Sock, PCRTSGBUF pSgBuffer));
|
---|
1059 |
|
---|
1060 | /**
|
---|
1061 | * Receive data from a socket - not blocking.
|
---|
1062 | *
|
---|
1063 | * @return iprt status code.
|
---|
1064 | * @param Sock Socket descriptor.
|
---|
1065 | * @param pvBuffer Where to put the data we read.
|
---|
1066 | * @param cbBuffer Read buffer size.
|
---|
1067 | * @param pcbRead Number of bytes read.
|
---|
1068 | */
|
---|
1069 | DECLR3CALLBACKMEMBER(int, pfnReadNB, (VDSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead));
|
---|
1070 |
|
---|
1071 | /**
|
---|
1072 | * Send data to a socket - not blocking.
|
---|
1073 | *
|
---|
1074 | * @return iprt status code.
|
---|
1075 | * @param Sock Socket descriptor.
|
---|
1076 | * @param pvBuffer Buffer to write data to socket.
|
---|
1077 | * @param cbBuffer How much to write.
|
---|
1078 | * @param pcbWritten Number of bytes written.
|
---|
1079 | */
|
---|
1080 | DECLR3CALLBACKMEMBER(int, pfnWriteNB, (VDSOCKET Sock, const void *pvBuffer, size_t cbBuffer, size_t *pcbWritten));
|
---|
1081 |
|
---|
1082 | /**
|
---|
1083 | * Send data from scatter/gather buffer to a socket - not blocking.
|
---|
1084 | *
|
---|
1085 | * @return iprt status code.
|
---|
1086 | * @param Sock Socket descriptor.
|
---|
1087 | * @param pSgBuffer Scatter/gather buffer to write data to socket.
|
---|
1088 | * @param pcbWritten Number of bytes written.
|
---|
1089 | */
|
---|
1090 | DECLR3CALLBACKMEMBER(int, pfnSgWriteNB, (VDSOCKET Sock, PRTSGBUF pSgBuffer, size_t *pcbWritten));
|
---|
1091 |
|
---|
1092 | /**
|
---|
1093 | * Flush socket write buffers.
|
---|
1094 | *
|
---|
1095 | * @return iprt status code.
|
---|
1096 | * @param Sock Socket descriptor.
|
---|
1097 | */
|
---|
1098 | DECLR3CALLBACKMEMBER(int, pfnFlush, (VDSOCKET Sock));
|
---|
1099 |
|
---|
1100 | /**
|
---|
1101 | * Enables or disables delaying sends to coalesce packets.
|
---|
1102 | *
|
---|
1103 | * @return iprt status code.
|
---|
1104 | * @param Sock Socket descriptor.
|
---|
1105 | * @param fEnable When set to true enables coalescing.
|
---|
1106 | */
|
---|
1107 | DECLR3CALLBACKMEMBER(int, pfnSetSendCoalescing, (VDSOCKET Sock, bool fEnable));
|
---|
1108 |
|
---|
1109 | /**
|
---|
1110 | * Gets the address of the local side.
|
---|
1111 | *
|
---|
1112 | * @return iprt status code.
|
---|
1113 | * @param Sock Socket descriptor.
|
---|
1114 | * @param pAddr Where to store the local address on success.
|
---|
1115 | */
|
---|
1116 | DECLR3CALLBACKMEMBER(int, pfnGetLocalAddress, (VDSOCKET Sock, PRTNETADDR pAddr));
|
---|
1117 |
|
---|
1118 | /**
|
---|
1119 | * Gets the address of the other party.
|
---|
1120 | *
|
---|
1121 | * @return iprt status code.
|
---|
1122 | * @param Sock Socket descriptor.
|
---|
1123 | * @param pAddr Where to store the peer address on success.
|
---|
1124 | */
|
---|
1125 | DECLR3CALLBACKMEMBER(int, pfnGetPeerAddress, (VDSOCKET Sock, PRTNETADDR pAddr));
|
---|
1126 |
|
---|
1127 | /**
|
---|
1128 | * Socket I/O multiplexing - extended version which can be woken up.
|
---|
1129 | * Checks if the socket is ready for reading or writing.
|
---|
1130 | *
|
---|
1131 | * @return iprt status code.
|
---|
1132 | * @retval VERR_INTERRUPTED if the thread was woken up by a pfnPoke call.
|
---|
1133 | * @param Sock Socket descriptor.
|
---|
1134 | * @param fEvents Mask of events to wait for.
|
---|
1135 | * @param pfEvents Where to store the received events.
|
---|
1136 | * @param cMillies Number of milliseconds to wait for the socket.
|
---|
1137 | * Use RT_INDEFINITE_WAIT to wait for ever.
|
---|
1138 | */
|
---|
1139 | DECLR3CALLBACKMEMBER(int, pfnSelectOneEx, (VDSOCKET Sock, uint32_t fEvents,
|
---|
1140 | uint32_t *pfEvents, RTMSINTERVAL cMillies));
|
---|
1141 |
|
---|
1142 | /**
|
---|
1143 | * Wakes up the thread waiting in pfnSelectOneEx.
|
---|
1144 | *
|
---|
1145 | * @return iprt status code.
|
---|
1146 | * @param Sock Socket descriptor.
|
---|
1147 | */
|
---|
1148 | DECLR3CALLBACKMEMBER(int, pfnPoke, (VDSOCKET Sock));
|
---|
1149 |
|
---|
1150 | } VDINTERFACETCPNET, *PVDINTERFACETCPNET;
|
---|
1151 |
|
---|
1152 | /**
|
---|
1153 | * Get TCP network stack interface from interface list.
|
---|
1154 | *
|
---|
1155 | * @return Pointer to the first TCP network stack interface in the list.
|
---|
1156 | * @param pVDIfs Pointer to the interface list.
|
---|
1157 | */
|
---|
1158 | DECLINLINE(PVDINTERFACETCPNET) VDIfTcpNetGet(PVDINTERFACE pVDIfs)
|
---|
1159 | {
|
---|
1160 | PVDINTERFACE pIf = VDInterfaceGet(pVDIfs, VDINTERFACETYPE_TCPNET);
|
---|
1161 |
|
---|
1162 | /* Check that the interface descriptor is a progress interface. */
|
---|
1163 | AssertMsgReturn( !pIf
|
---|
1164 | || ( (pIf->enmInterface == VDINTERFACETYPE_TCPNET)
|
---|
1165 | && (pIf->cbSize == sizeof(VDINTERFACETCPNET))),
|
---|
1166 | ("Not a TCP net interface"), NULL);
|
---|
1167 |
|
---|
1168 | return (PVDINTERFACETCPNET)pIf;
|
---|
1169 | }
|
---|
1170 |
|
---|
1171 |
|
---|
1172 | /**
|
---|
1173 | * Interface to synchronize concurrent accesses by several threads.
|
---|
1174 | *
|
---|
1175 | * @note The scope of this interface is to manage concurrent accesses after
|
---|
1176 | * the HDD container has been created, and they must stop before destroying the
|
---|
1177 | * container. Opening or closing images is covered by the synchronization, but
|
---|
1178 | * that does not mean it is safe to close images while a thread executes
|
---|
1179 | * <link to="VDMerge"/> or <link to="VDCopy"/> operating on these images.
|
---|
1180 | * Making them safe would require the lock to be held during the entire
|
---|
1181 | * operation, which prevents other concurrent acitivities.
|
---|
1182 | *
|
---|
1183 | * @note Right now this is kept as simple as possible, and does not even
|
---|
1184 | * attempt to provide enough information to allow e.g. concurrent write
|
---|
1185 | * accesses to different areas of the disk. The reason is that it is very
|
---|
1186 | * difficult to predict which area of a disk is affected by a write,
|
---|
1187 | * especially when different image formats are mixed. Maybe later a more
|
---|
1188 | * sophisticated interface will be provided which has the necessary information
|
---|
1189 | * about worst case affected areas.
|
---|
1190 | *
|
---|
1191 | * Per-disk interface. Optional, needed if the disk is accessed concurrently
|
---|
1192 | * by several threads, e.g. when merging diff images while a VM is running.
|
---|
1193 | */
|
---|
1194 | typedef struct VDINTERFACETHREADSYNC
|
---|
1195 | {
|
---|
1196 | /**
|
---|
1197 | * Common interface header.
|
---|
1198 | */
|
---|
1199 | VDINTERFACE Core;
|
---|
1200 |
|
---|
1201 | /**
|
---|
1202 | * Start a read operation.
|
---|
1203 | */
|
---|
1204 | DECLR3CALLBACKMEMBER(int, pfnStartRead, (void *pvUser));
|
---|
1205 |
|
---|
1206 | /**
|
---|
1207 | * Finish a read operation.
|
---|
1208 | */
|
---|
1209 | DECLR3CALLBACKMEMBER(int, pfnFinishRead, (void *pvUser));
|
---|
1210 |
|
---|
1211 | /**
|
---|
1212 | * Start a write operation.
|
---|
1213 | */
|
---|
1214 | DECLR3CALLBACKMEMBER(int, pfnStartWrite, (void *pvUser));
|
---|
1215 |
|
---|
1216 | /**
|
---|
1217 | * Finish a write operation.
|
---|
1218 | */
|
---|
1219 | DECLR3CALLBACKMEMBER(int, pfnFinishWrite, (void *pvUser));
|
---|
1220 |
|
---|
1221 | } VDINTERFACETHREADSYNC, *PVDINTERFACETHREADSYNC;
|
---|
1222 |
|
---|
1223 | /**
|
---|
1224 | * Get thread synchronization interface from interface list.
|
---|
1225 | *
|
---|
1226 | * @return Pointer to the first thread synchronization interface in the list.
|
---|
1227 | * @param pVDIfs Pointer to the interface list.
|
---|
1228 | */
|
---|
1229 | DECLINLINE(PVDINTERFACETHREADSYNC) VDIfThreadSyncGet(PVDINTERFACE pVDIfs)
|
---|
1230 | {
|
---|
1231 | PVDINTERFACE pIf = VDInterfaceGet(pVDIfs, VDINTERFACETYPE_THREADSYNC);
|
---|
1232 |
|
---|
1233 | /* Check that the interface descriptor is a progress interface. */
|
---|
1234 | AssertMsgReturn( !pIf
|
---|
1235 | || ( (pIf->enmInterface == VDINTERFACETYPE_THREADSYNC)
|
---|
1236 | && (pIf->cbSize == sizeof(VDINTERFACETHREADSYNC))),
|
---|
1237 | ("Not a thread synchronization interface"), NULL);
|
---|
1238 |
|
---|
1239 | return (PVDINTERFACETHREADSYNC)pIf;
|
---|
1240 | }
|
---|
1241 |
|
---|
1242 | /**
|
---|
1243 | * Interface to query usage of disk ranges.
|
---|
1244 | *
|
---|
1245 | * Per-operation interface. Optional.
|
---|
1246 | */
|
---|
1247 | typedef struct VDINTERFACEQUERYRANGEUSE
|
---|
1248 | {
|
---|
1249 | /**
|
---|
1250 | * Common interface header.
|
---|
1251 | */
|
---|
1252 | VDINTERFACE Core;
|
---|
1253 |
|
---|
1254 | /**
|
---|
1255 | * Query use of a disk range.
|
---|
1256 | */
|
---|
1257 | DECLR3CALLBACKMEMBER(int, pfnQueryRangeUse, (void *pvUser, uint64_t off, uint64_t cb,
|
---|
1258 | bool *pfUsed));
|
---|
1259 |
|
---|
1260 | } VDINTERFACEQUERYRANGEUSE, *PVDINTERFACEQUERYRANGEUSE;
|
---|
1261 |
|
---|
1262 | /**
|
---|
1263 | * Get query range use interface from interface list.
|
---|
1264 | *
|
---|
1265 | * @return Pointer to the first thread synchronization interface in the list.
|
---|
1266 | * @param pVDIfs Pointer to the interface list.
|
---|
1267 | */
|
---|
1268 | DECLINLINE(PVDINTERFACEQUERYRANGEUSE) VDIfQueryRangeUseGet(PVDINTERFACE pVDIfs)
|
---|
1269 | {
|
---|
1270 | PVDINTERFACE pIf = VDInterfaceGet(pVDIfs, VDINTERFACETYPE_QUERYRANGEUSE);
|
---|
1271 |
|
---|
1272 | /* Check that the interface descriptor is a progress interface. */
|
---|
1273 | AssertMsgReturn( !pIf
|
---|
1274 | || ( (pIf->enmInterface == VDINTERFACETYPE_QUERYRANGEUSE)
|
---|
1275 | && (pIf->cbSize == sizeof(VDINTERFACEQUERYRANGEUSE))),
|
---|
1276 | ("Not a query range use interface"), NULL);
|
---|
1277 |
|
---|
1278 | return (PVDINTERFACEQUERYRANGEUSE)pIf;
|
---|
1279 | }
|
---|
1280 |
|
---|
1281 | DECLINLINE(int) vdIfQueryRangeUse(PVDINTERFACEQUERYRANGEUSE pIfQueryRangeUse, uint64_t off, uint64_t cb,
|
---|
1282 | bool *pfUsed)
|
---|
1283 | {
|
---|
1284 | return pIfQueryRangeUse->pfnQueryRangeUse(pIfQueryRangeUse->Core.pvUser, off, cb, pfUsed);
|
---|
1285 | }
|
---|
1286 |
|
---|
1287 | RT_C_DECLS_END
|
---|
1288 |
|
---|
1289 | /** @} */
|
---|
1290 |
|
---|
1291 | #endif
|
---|