VirtualBox

source: vbox/trunk/include/VBox/vd-ifs.h@ 59455

Last change on this file since 59455 was 59455, checked in by vboxsync, 9 years ago

Storage/VD: Remove the custom code in each backend which allocates all blocks in a fixed size image by writing zeros to it. Preparations to make use of more optimized methods to allocate large files on recent hosts (fallocate() and friends)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 60.3 KB
Line 
1/** @file
2 * VD Container API - interfaces.
3 */
4
5/*
6 * Copyright (C) 2011-2015 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.215389.xyz. 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
39RT_C_DECLS_BEGIN
40
41/** Interface header magic. */
42#define VDINTERFACE_MAGIC UINT32_C(0x19701015)
43
44/**
45 * Supported interface types.
46 */
47typedef 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 /** Interface for the metadata traverse callback. Per-operation. */
71 VDINTERFACETYPE_TRAVERSEMETADATA,
72 /** Interface for crypto operations. Per-filter. */
73 VDINTERFACETYPE_CRYPTO,
74 /** invalid interface. */
75 VDINTERFACETYPE_INVALID
76} VDINTERFACETYPE;
77
78/**
79 * Common structure for all interfaces and at the beginning of all types.
80 */
81typedef struct VDINTERFACE
82{
83 uint32_t u32Magic;
84 /** Human readable interface name. */
85 const char *pszInterfaceName;
86 /** Pointer to the next common interface structure. */
87 struct VDINTERFACE *pNext;
88 /** Interface type. */
89 VDINTERFACETYPE enmInterface;
90 /** Size of the interface. */
91 size_t cbSize;
92 /** Opaque user data which is passed on every call. */
93 void *pvUser;
94} VDINTERFACE;
95/** Pointer to a VDINTERFACE. */
96typedef VDINTERFACE *PVDINTERFACE;
97/** Pointer to a const VDINTERFACE. */
98typedef const VDINTERFACE *PCVDINTERFACE;
99
100/**
101 * Helper functions to handle interface lists.
102 *
103 * @note These interface lists are used consistently to pass per-disk,
104 * per-image and/or per-operation callbacks. Those three purposes are strictly
105 * separate. See the individual interface declarations for what context they
106 * apply to. The caller is responsible for ensuring that the lifetime of the
107 * interface descriptors is appropriate for the category of interface.
108 */
109
110/**
111 * Get a specific interface from a list of interfaces specified by the type.
112 *
113 * @return Pointer to the matching interface or NULL if none was found.
114 * @param pVDIfs Pointer to the VD interface list.
115 * @param enmInterface Interface to search for.
116 */
117DECLINLINE(PVDINTERFACE) VDInterfaceGet(PVDINTERFACE pVDIfs, VDINTERFACETYPE enmInterface)
118{
119 AssertMsgReturn( enmInterface >= VDINTERFACETYPE_FIRST
120 && enmInterface < VDINTERFACETYPE_INVALID,
121 ("enmInterface=%u", enmInterface), NULL);
122
123 while (pVDIfs)
124 {
125 AssertMsgBreak(pVDIfs->u32Magic == VDINTERFACE_MAGIC,
126 ("u32Magic=%#x\n", pVDIfs->u32Magic));
127
128 if (pVDIfs->enmInterface == enmInterface)
129 return pVDIfs;
130 pVDIfs = pVDIfs->pNext;
131 }
132
133 /* No matching interface was found. */
134 return NULL;
135}
136
137/**
138 * Add an interface to a list of interfaces.
139 *
140 * @return VBox status code.
141 * @param pInterface Pointer to an unitialized common interface structure.
142 * @param pszName Name of the interface.
143 * @param enmInterface Type of the interface.
144 * @param pvUser Opaque user data passed on every function call.
145 * @param cbInterface The interface size.
146 * @param ppVDIfs Pointer to the VD interface list.
147 */
148DECLINLINE(int) VDInterfaceAdd(PVDINTERFACE pInterface, const char *pszName, VDINTERFACETYPE enmInterface, void *pvUser,
149 size_t cbInterface, PVDINTERFACE *ppVDIfs)
150{
151 /* Argument checks. */
152 AssertMsgReturn( enmInterface >= VDINTERFACETYPE_FIRST
153 && enmInterface < VDINTERFACETYPE_INVALID,
154 ("enmInterface=%u", enmInterface), VERR_INVALID_PARAMETER);
155
156 AssertMsgReturn(VALID_PTR(ppVDIfs),
157 ("pInterfaceList=%#p", ppVDIfs),
158 VERR_INVALID_PARAMETER);
159
160 /* Fill out interface descriptor. */
161 pInterface->u32Magic = VDINTERFACE_MAGIC;
162 pInterface->cbSize = cbInterface;
163 pInterface->pszInterfaceName = pszName;
164 pInterface->enmInterface = enmInterface;
165 pInterface->pvUser = pvUser;
166 pInterface->pNext = *ppVDIfs;
167
168 /* Remember the new start of the list. */
169 *ppVDIfs = pInterface;
170
171 return VINF_SUCCESS;
172}
173
174/**
175 * Removes an interface from a list of interfaces.
176 *
177 * @return VBox status code
178 * @param pInterface Pointer to an initialized common interface structure to remove.
179 * @param ppVDIfs Pointer to the VD interface list to remove from.
180 */
181DECLINLINE(int) VDInterfaceRemove(PVDINTERFACE pInterface, PVDINTERFACE *ppVDIfs)
182{
183 int rc = VERR_NOT_FOUND;
184
185 /* Argument checks. */
186 AssertMsgReturn(VALID_PTR(pInterface),
187 ("pInterface=%#p", pInterface),
188 VERR_INVALID_PARAMETER);
189
190 AssertMsgReturn(VALID_PTR(ppVDIfs),
191 ("pInterfaceList=%#p", ppVDIfs),
192 VERR_INVALID_PARAMETER);
193
194 if (*ppVDIfs)
195 {
196 PVDINTERFACE pPrev = NULL;
197 PVDINTERFACE pCurr = *ppVDIfs;
198
199 while ( pCurr
200 && (pCurr != pInterface))
201 {
202 pPrev = pCurr;
203 pCurr = pCurr->pNext;
204 }
205
206 /* First interface */
207 if (!pPrev)
208 {
209 *ppVDIfs = pCurr->pNext;
210 rc = VINF_SUCCESS;
211 }
212 else if (pCurr)
213 {
214 pPrev = pCurr->pNext;
215 rc = VINF_SUCCESS;
216 }
217 }
218
219 return rc;
220}
221
222/**
223 * Interface to deliver error messages (and also informational messages)
224 * to upper layers.
225 *
226 * Per-disk interface. Optional, but think twice if you want to miss the
227 * opportunity of reporting better human-readable error messages.
228 */
229typedef struct VDINTERFACEERROR
230{
231 /**
232 * Common interface header.
233 */
234 VDINTERFACE Core;
235
236 /**
237 * Error message callback. Must be able to accept special IPRT format
238 * strings.
239 *
240 * @param pvUser The opaque data passed on container creation.
241 * @param rc The VBox error code.
242 * @param SRC_POS Use RT_SRC_POS.
243 * @param pszFormat Error message format string.
244 * @param va Error message arguments.
245 */
246 DECLR3CALLBACKMEMBER(void, pfnError, (void *pvUser, int rc, RT_SRC_POS_DECL,
247 const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(6, 0));
248
249 /**
250 * Informational message callback. May be NULL. Used e.g. in
251 * VDDumpImages(). Must be able to accept special IPRT format strings.
252 *
253 * @return VBox status code.
254 * @param pvUser The opaque data passed on container creation.
255 * @param pszFormat Message format string.
256 * @param va Message arguments.
257 */
258 DECLR3CALLBACKMEMBER(int, pfnMessage, (void *pvUser, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(2, 0));
259
260} VDINTERFACEERROR, *PVDINTERFACEERROR;
261
262/**
263 * Get error interface from interface list.
264 *
265 * @return Pointer to the first error interface in the list.
266 * @param pVDIfs Pointer to the interface list.
267 */
268DECLINLINE(PVDINTERFACEERROR) VDIfErrorGet(PVDINTERFACE pVDIfs)
269{
270 PVDINTERFACE pIf = VDInterfaceGet(pVDIfs, VDINTERFACETYPE_ERROR);
271
272 /* Check that the interface descriptor is a progress interface. */
273 AssertMsgReturn( !pIf
274 || ( (pIf->enmInterface == VDINTERFACETYPE_ERROR)
275 && (pIf->cbSize == sizeof(VDINTERFACEERROR))),
276 ("Not an error interface\n"), NULL);
277
278 return (PVDINTERFACEERROR)pIf;
279}
280
281/**
282 * Signal an error to the frontend.
283 *
284 * @returns VBox status code.
285 * @param pIfError The error interface.
286 * @param rc The status code.
287 * @param SRC_POS The position in the source code.
288 * @param pszFormat The format string to pass.
289 * @param ... Arguments to the format string.
290 */
291DECLINLINE(int) RT_IPRT_FORMAT_ATTR(6, 7) vdIfError(PVDINTERFACEERROR pIfError, int rc, RT_SRC_POS_DECL,
292 const char *pszFormat, ...)
293{
294 va_list va;
295 va_start(va, pszFormat);
296 if (pIfError)
297 pIfError->pfnError(pIfError->Core.pvUser, rc, RT_SRC_POS_ARGS, pszFormat, va);
298 va_end(va);
299 return rc;
300}
301
302/**
303 * Signal an informational message to the frontend.
304 *
305 * @returns VBox status code.
306 * @param pIfError The error interface.
307 * @param pszFormat The format string to pass.
308 * @param ... Arguments to the format string.
309 */
310DECLINLINE(int) RT_IPRT_FORMAT_ATTR(2, 3) vdIfErrorMessage(PVDINTERFACEERROR pIfError, const char *pszFormat, ...)
311{
312 int rc = VINF_SUCCESS;
313 va_list va;
314 va_start(va, pszFormat);
315 if (pIfError && pIfError->pfnMessage)
316 rc = pIfError->pfnMessage(pIfError->Core.pvUser, pszFormat, va);
317 va_end(va);
318 return rc;
319}
320
321/**
322 * Completion callback which is called by the interface owner
323 * to inform the backend that a task finished.
324 *
325 * @return VBox status code.
326 * @param pvUser Opaque user data which is passed on request submission.
327 * @param rcReq Status code of the completed request.
328 */
329typedef DECLCALLBACK(int) FNVDCOMPLETED(void *pvUser, int rcReq);
330/** Pointer to FNVDCOMPLETED() */
331typedef FNVDCOMPLETED *PFNVDCOMPLETED;
332
333/**
334 * Support interface for I/O
335 *
336 * Per-image. Optional as input.
337 */
338typedef struct VDINTERFACEIO
339{
340 /**
341 * Common interface header.
342 */
343 VDINTERFACE Core;
344
345 /**
346 * Open callback
347 *
348 * @return VBox status code.
349 * @param pvUser The opaque data passed on container creation.
350 * @param pszLocation Name of the location to open.
351 * @param fOpen Flags for opening the backend.
352 * See RTFILE_O_* \#defines, inventing another set
353 * of open flags is not worth the mapping effort.
354 * @param pfnCompleted The callback which is called whenever a task
355 * completed. The backend has to pass the user data
356 * of the request initiator (ie the one who calls
357 * VDAsyncRead or VDAsyncWrite) in pvCompletion
358 * if this is NULL.
359 * @param ppStorage Where to store the opaque storage handle.
360 */
361 DECLR3CALLBACKMEMBER(int, pfnOpen, (void *pvUser, const char *pszLocation,
362 uint32_t fOpen,
363 PFNVDCOMPLETED pfnCompleted,
364 void **ppStorage));
365
366 /**
367 * Close callback.
368 *
369 * @return VBox status code.
370 * @param pvUser The opaque data passed on container creation.
371 * @param pStorage The opaque storage handle to close.
372 */
373 DECLR3CALLBACKMEMBER(int, pfnClose, (void *pvUser, void *pStorage));
374
375 /**
376 * Delete callback.
377 *
378 * @return VBox status code.
379 * @param pvUser The opaque data passed on container creation.
380 * @param pcszFilename Name of the file to delete.
381 */
382 DECLR3CALLBACKMEMBER(int, pfnDelete, (void *pvUser, const char *pcszFilename));
383
384 /**
385 * Move callback.
386 *
387 * @return VBox status code.
388 * @param pvUser The opaque data passed on container creation.
389 * @param pcszSrc The path to the source file.
390 * @param pcszDst The path to the destination file.
391 * This file will be created.
392 * @param fMove A combination of the RTFILEMOVE_* flags.
393 */
394 DECLR3CALLBACKMEMBER(int, pfnMove, (void *pvUser, const char *pcszSrc, const char *pcszDst, unsigned fMove));
395
396 /**
397 * Returns the free space on a disk.
398 *
399 * @return VBox status code.
400 * @param pvUser The opaque data passed on container creation.
401 * @param pcszFilename Name of a file to identify the disk.
402 * @param pcbFreeSpace Where to store the free space of the disk.
403 */
404 DECLR3CALLBACKMEMBER(int, pfnGetFreeSpace, (void *pvUser, const char *pcszFilename, int64_t *pcbFreeSpace));
405
406 /**
407 * Returns the last modification timestamp of a file.
408 *
409 * @return VBox status code.
410 * @param pvUser The opaque data passed on container creation.
411 * @param pcszFilename Name of a file to identify the disk.
412 * @param pModificationTime Where to store the timestamp of the file.
413 */
414 DECLR3CALLBACKMEMBER(int, pfnGetModificationTime, (void *pvUser, const char *pcszFilename, PRTTIMESPEC pModificationTime));
415
416 /**
417 * Returns the size of the opened storage backend.
418 *
419 * @return VBox status code.
420 * @param pvUser The opaque data passed on container creation.
421 * @param pStorage The opaque storage handle to close.
422 * @param pcbSize Where to store the size of the storage backend.
423 */
424 DECLR3CALLBACKMEMBER(int, pfnGetSize, (void *pvUser, void *pStorage, uint64_t *pcbSize));
425
426 /**
427 * Sets the size of the opened storage backend if possible.
428 *
429 * @return VBox status code.
430 * @retval VERR_NOT_SUPPORTED if the backend does not support this operation.
431 * @param pvUser The opaque data passed on container creation.
432 * @param pStorage The opaque storage handle to close.
433 * @param cbSize The new size of the image.
434 *
435 * @note Depending on the host the underlying storage (backing file, etc.)
436 * might not have all required storage allocated (sparse file) which
437 * can delay writes or fail with a not enough free space error if there
438 * is not enough space on the storage medium when writing to the range for
439 * the first time.
440 * Use VDINTERFACEIO::pfnSetAllocationSize to make sure the storage is
441 * really alloacted.
442 */
443 DECLR3CALLBACKMEMBER(int, pfnSetSize, (void *pvUser, void *pStorage, uint64_t cbSize));
444
445 /**
446 * Sets the size of the opened storage backend making sure the given size
447 * is really allocated.
448 *
449 * @return VBox status code.
450 * @retval VERR_NOT_SUPPORTED if the implementer of the interface doesn't support
451 * this method.
452 * @param pvUser The opaque data passed on container creation.
453 * @param pStorage The storage handle.
454 * @param cbSize The new size of the image.
455 * @param fFlags Flags for controlling the allocation strategy.
456 * Reserved for future use, MBZ.
457 */
458 DECLR3CALLBACKMEMBER(int, pfnSetAllocationSize, (void *pvUser, void *pStorage,
459 uint64_t cbSize, uint32_t fFlags));
460
461 /**
462 * Synchronous write callback.
463 *
464 * @return VBox status code.
465 * @param pvUser The opaque data passed on container creation.
466 * @param pStorage The storage handle to use.
467 * @param uOffset The offset to start from.
468 * @param pvBuffer Pointer to the bits need to be written.
469 * @param cbBuffer How many bytes to write.
470 * @param pcbWritten Where to store how many bytes were actually written.
471 */
472 DECLR3CALLBACKMEMBER(int, pfnWriteSync, (void *pvUser, void *pStorage, uint64_t uOffset,
473 const void *pvBuffer, size_t cbBuffer, size_t *pcbWritten));
474
475 /**
476 * Synchronous read callback.
477 *
478 * @return VBox status code.
479 * @param pvUser The opaque data passed on container creation.
480 * @param pStorage The storage handle to use.
481 * @param uOffset The offset to start from.
482 * @param pvBuffer Where to store the read bits.
483 * @param cbBuffer How many bytes to read.
484 * @param pcbRead Where to store how many bytes were actually read.
485 */
486 DECLR3CALLBACKMEMBER(int, pfnReadSync, (void *pvUser, void *pStorage, uint64_t uOffset,
487 void *pvBuffer, size_t cbBuffer, size_t *pcbRead));
488
489 /**
490 * Flush data to the storage backend.
491 *
492 * @return VBox status code.
493 * @param pvUser The opaque data passed on container creation.
494 * @param pStorage The storage handle to flush.
495 */
496 DECLR3CALLBACKMEMBER(int, pfnFlushSync, (void *pvUser, void *pStorage));
497
498 /**
499 * Initiate an asynchronous read request.
500 *
501 * @return VBox status code.
502 * @param pvUser The opaque user data passed on container creation.
503 * @param pStorage The storage handle.
504 * @param uOffset The offset to start reading from.
505 * @param paSegments Scatter gather list to store the data in.
506 * @param cSegments Number of segments in the list.
507 * @param cbRead How many bytes to read.
508 * @param pvCompletion The opaque user data which is returned upon completion.
509 * @param ppTask Where to store the opaque task handle.
510 */
511 DECLR3CALLBACKMEMBER(int, pfnReadAsync, (void *pvUser, void *pStorage, uint64_t uOffset,
512 PCRTSGSEG paSegments, size_t cSegments,
513 size_t cbRead, void *pvCompletion,
514 void **ppTask));
515
516 /**
517 * Initiate an asynchronous write request.
518 *
519 * @return VBox status code.
520 * @param pvUser The opaque user data passed on conatiner creation.
521 * @param pStorage The storage handle.
522 * @param uOffset The offset to start writing to.
523 * @param paSegments Scatter gather list of the data to write
524 * @param cSegments Number of segments in the list.
525 * @param cbWrite How many bytes to write.
526 * @param pvCompletion The opaque user data which is returned upon completion.
527 * @param ppTask Where to store the opaque task handle.
528 */
529 DECLR3CALLBACKMEMBER(int, pfnWriteAsync, (void *pvUser, void *pStorage, uint64_t uOffset,
530 PCRTSGSEG paSegments, size_t cSegments,
531 size_t cbWrite, void *pvCompletion,
532 void **ppTask));
533
534 /**
535 * Initiates an async flush request.
536 *
537 * @return VBox status code.
538 * @param pvUser The opaque data passed on container creation.
539 * @param pStorage The storage handle to flush.
540 * @param pvCompletion The opaque user data which is returned upon completion.
541 * @param ppTask Where to store the opaque task handle.
542 */
543 DECLR3CALLBACKMEMBER(int, pfnFlushAsync, (void *pvUser, void *pStorage,
544 void *pvCompletion, void **ppTask));
545
546} VDINTERFACEIO, *PVDINTERFACEIO;
547
548/**
549 * Get I/O interface from interface list.
550 *
551 * @return Pointer to the first I/O interface in the list.
552 * @param pVDIfs Pointer to the interface list.
553 */
554DECLINLINE(PVDINTERFACEIO) VDIfIoGet(PVDINTERFACE pVDIfs)
555{
556 PVDINTERFACE pIf = VDInterfaceGet(pVDIfs, VDINTERFACETYPE_IO);
557
558 /* Check that the interface descriptor is a progress interface. */
559 AssertMsgReturn( !pIf
560 || ( (pIf->enmInterface == VDINTERFACETYPE_IO)
561 && (pIf->cbSize == sizeof(VDINTERFACEIO))),
562 ("Not a I/O interface"), NULL);
563
564 return (PVDINTERFACEIO)pIf;
565}
566
567DECLINLINE(int) vdIfIoFileOpen(PVDINTERFACEIO pIfIo, const char *pszFilename,
568 uint32_t fOpen, PFNVDCOMPLETED pfnCompleted,
569 void **ppStorage)
570{
571 return pIfIo->pfnOpen(pIfIo->Core.pvUser, pszFilename, fOpen, pfnCompleted, ppStorage);
572}
573
574DECLINLINE(int) vdIfIoFileClose(PVDINTERFACEIO pIfIo, void *pStorage)
575{
576 return pIfIo->pfnClose(pIfIo->Core.pvUser, pStorage);
577}
578
579DECLINLINE(int) vdIfIoFileDelete(PVDINTERFACEIO pIfIo, const char *pszFilename)
580{
581 return pIfIo->pfnDelete(pIfIo->Core.pvUser, pszFilename);
582}
583
584DECLINLINE(int) vdIfIoFileMove(PVDINTERFACEIO pIfIo, const char *pszSrc,
585 const char *pszDst, unsigned fMove)
586{
587 return pIfIo->pfnMove(pIfIo->Core.pvUser, pszSrc, pszDst, fMove);
588}
589
590DECLINLINE(int) vdIfIoFileGetFreeSpace(PVDINTERFACEIO pIfIo, const char *pszFilename,
591 int64_t *pcbFree)
592{
593 return pIfIo->pfnGetFreeSpace(pIfIo->Core.pvUser, pszFilename, pcbFree);
594}
595
596DECLINLINE(int) vdIfIoFileGetModificationTime(PVDINTERFACEIO pIfIo, const char *pcszFilename,
597 PRTTIMESPEC pModificationTime)
598{
599 return pIfIo->pfnGetModificationTime(pIfIo->Core.pvUser, pcszFilename,
600 pModificationTime);
601}
602
603DECLINLINE(int) vdIfIoFileGetSize(PVDINTERFACEIO pIfIo, void *pStorage,
604 uint64_t *pcbSize)
605{
606 return pIfIo->pfnGetSize(pIfIo->Core.pvUser, pStorage, pcbSize);
607}
608
609DECLINLINE(int) vdIfIoFileSetSize(PVDINTERFACEIO pIfIo, void *pStorage,
610 uint64_t cbSize)
611{
612 return pIfIo->pfnSetSize(pIfIo->Core.pvUser, pStorage, cbSize);
613}
614
615DECLINLINE(int) vdIfIoFileWriteSync(PVDINTERFACEIO pIfIo, void *pStorage,
616 uint64_t uOffset, const void *pvBuffer, size_t cbBuffer,
617 size_t *pcbWritten)
618{
619 return pIfIo->pfnWriteSync(pIfIo->Core.pvUser, pStorage, uOffset,
620 pvBuffer, cbBuffer, pcbWritten);
621}
622
623DECLINLINE(int) vdIfIoFileReadSync(PVDINTERFACEIO pIfIo, void *pStorage,
624 uint64_t uOffset, void *pvBuffer, size_t cbBuffer,
625 size_t *pcbRead)
626{
627 return pIfIo->pfnReadSync(pIfIo->Core.pvUser, pStorage, uOffset,
628 pvBuffer, cbBuffer, pcbRead);
629}
630
631DECLINLINE(int) vdIfIoFileFlushSync(PVDINTERFACEIO pIfIo, void *pStorage)
632{
633 return pIfIo->pfnFlushSync(pIfIo->Core.pvUser, pStorage);
634}
635
636/**
637 * Create a VFS stream handle around a VD I/O interface.
638 *
639 * The I/O interface will not be closed or free by the stream, the caller will
640 * do so after it is done with the stream and has released the instances of the
641 * I/O stream object returned by this API.
642 *
643 * @return VBox status code.
644 * @param pVDIfsIo Pointer to the VD I/O interface.
645 * @param pvStorage The storage argument to pass to the interface
646 * methods.
647 * @param fFlags RTFILE_O_XXX, access mask requied.
648 * @param phVfsIos Where to return the VFS I/O stream handle on
649 * success.
650 */
651VBOXDDU_DECL(int) VDIfCreateVfsStream(PVDINTERFACEIO pVDIfsIo, void *pvStorage, uint32_t fFlags, PRTVFSIOSTREAM phVfsIos);
652
653/**
654 * Create a VFS file handle around a VD I/O interface.
655 *
656 * The I/O interface will not be closed or free by the VFS file, the caller will
657 * do so after it is done with the VFS file and has released the instances of
658 * the VFS object returned by this API.
659 *
660 * @return VBox status code.
661 * @param pVDIfs Pointer to the VD I/O interface. If NULL, then @a
662 * pVDIfsInt must be specified.
663 * @param pVDIfsInt Pointer to the internal VD I/O interface. If NULL,
664 * then @ pVDIfs must be specified.
665 * @param pvStorage The storage argument to pass to the interface
666 * methods.
667 * @param fFlags RTFILE_O_XXX, access mask requied.
668 * @param phVfsFile Where to return the VFS file handle on success.
669 */
670VBOXDDU_DECL(int) VDIfCreateVfsFile(PVDINTERFACEIO pVDIfs, struct VDINTERFACEIOINT *pVDIfsInt, void *pvStorage, uint32_t fFlags, PRTVFSFILE phVfsFile);
671
672
673/**
674 * Callback which provides progress information about a currently running
675 * lengthy operation.
676 *
677 * @return VBox status code.
678 * @param pvUser The opaque user data associated with this interface.
679 * @param uPercent Completion percentage.
680 */
681typedef DECLCALLBACK(int) FNVDPROGRESS(void *pvUser, unsigned uPercentage);
682/** Pointer to FNVDPROGRESS() */
683typedef FNVDPROGRESS *PFNVDPROGRESS;
684
685/**
686 * Progress notification interface
687 *
688 * Per-operation. Optional.
689 */
690typedef struct VDINTERFACEPROGRESS
691{
692 /**
693 * Common interface header.
694 */
695 VDINTERFACE Core;
696
697 /**
698 * Progress notification callbacks.
699 */
700 PFNVDPROGRESS pfnProgress;
701
702} VDINTERFACEPROGRESS, *PVDINTERFACEPROGRESS;
703
704/**
705 * Get progress interface from interface list.
706 *
707 * @return Pointer to the first progress interface in the list.
708 * @param pVDIfs Pointer to the interface list.
709 */
710DECLINLINE(PVDINTERFACEPROGRESS) VDIfProgressGet(PVDINTERFACE pVDIfs)
711{
712 PVDINTERFACE pIf = VDInterfaceGet(pVDIfs, VDINTERFACETYPE_PROGRESS);
713
714 /* Check that the interface descriptor is a progress interface. */
715 AssertMsgReturn( !pIf
716 || ( (pIf->enmInterface == VDINTERFACETYPE_PROGRESS)
717 && (pIf->cbSize == sizeof(VDINTERFACEPROGRESS))),
718 ("Not a progress interface"), NULL);
719
720 return (PVDINTERFACEPROGRESS)pIf;
721}
722
723
724/**
725 * Configuration information interface
726 *
727 * Per-image. Optional for most backends, but mandatory for images which do
728 * not operate on files (including standard block or character devices).
729 */
730typedef struct VDINTERFACECONFIG
731{
732 /**
733 * Common interface header.
734 */
735 VDINTERFACE Core;
736
737 /**
738 * Validates that the keys are within a set of valid names.
739 *
740 * @return true if all key names are found in pszzAllowed.
741 * @return false if not.
742 * @param pvUser The opaque user data associated with this interface.
743 * @param pszzValid List of valid key names separated by '\\0' and ending with
744 * a double '\\0'.
745 */
746 DECLR3CALLBACKMEMBER(bool, pfnAreKeysValid, (void *pvUser, const char *pszzValid));
747
748 /**
749 * Retrieves the length of the string value associated with a key (including
750 * the terminator, for compatibility with CFGMR3QuerySize).
751 *
752 * @return VBox status code.
753 * VERR_CFGM_VALUE_NOT_FOUND means that the key is not known.
754 * @param pvUser The opaque user data associated with this interface.
755 * @param pszName Name of the key to query.
756 * @param pcbValue Where to store the value length. Non-NULL.
757 */
758 DECLR3CALLBACKMEMBER(int, pfnQuerySize, (void *pvUser, const char *pszName, size_t *pcbValue));
759
760 /**
761 * Query the string value associated with a key.
762 *
763 * @return VBox status code.
764 * VERR_CFGM_VALUE_NOT_FOUND means that the key is not known.
765 * VERR_CFGM_NOT_ENOUGH_SPACE means that the buffer is not big enough.
766 * @param pvUser The opaque user data associated with this interface.
767 * @param pszName Name of the key to query.
768 * @param pszValue Pointer to buffer where to store value.
769 * @param cchValue Length of value buffer.
770 */
771 DECLR3CALLBACKMEMBER(int, pfnQuery, (void *pvUser, const char *pszName, char *pszValue, size_t cchValue));
772
773 /**
774 * Query the bytes value associated with a key.
775 *
776 * @return VBox status code.
777 * VERR_CFGM_VALUE_NOT_FOUND means that the key is not known.
778 * VERR_CFGM_NOT_ENOUGH_SPACE means that the buffer is not big enough.
779 * @param pvUser The opaque user data associated with this interface.
780 * @param pszName Name of the key to query.
781 * @param ppvData Pointer to buffer where to store the data.
782 * @param cbData Length of data buffer.
783 */
784 DECLR3CALLBACKMEMBER(int, pfnQueryBytes, (void *pvUser, const char *pszName, void *ppvData, size_t cbData));
785
786} VDINTERFACECONFIG, *PVDINTERFACECONFIG;
787
788/**
789 * Get configuration information interface from interface list.
790 *
791 * @return Pointer to the first configuration information interface in the list.
792 * @param pVDIfs Pointer to the interface list.
793 */
794DECLINLINE(PVDINTERFACECONFIG) VDIfConfigGet(PVDINTERFACE pVDIfs)
795{
796 PVDINTERFACE pIf = VDInterfaceGet(pVDIfs, VDINTERFACETYPE_CONFIG);
797
798 /* Check that the interface descriptor is a progress interface. */
799 AssertMsgReturn( !pIf
800 || ( (pIf->enmInterface == VDINTERFACETYPE_CONFIG)
801 && (pIf->cbSize == sizeof(VDINTERFACECONFIG))),
802 ("Not a config interface"), NULL);
803
804 return (PVDINTERFACECONFIG)pIf;
805}
806
807/**
808 * Query configuration, validates that the keys are within a set of valid names.
809 *
810 * @return true if all key names are found in pszzAllowed.
811 * @return false if not.
812 * @param pCfgIf Pointer to configuration callback table.
813 * @param pszzValid List of valid names separated by '\\0' and ending with
814 * a double '\\0'.
815 */
816DECLINLINE(bool) VDCFGAreKeysValid(PVDINTERFACECONFIG pCfgIf, const char *pszzValid)
817{
818 return pCfgIf->pfnAreKeysValid(pCfgIf->Core.pvUser, pszzValid);
819}
820
821/**
822 * Checks whether a given key is existing.
823 *
824 * @return true if the key exists.
825 * @return false if the key does not exist.
826 * @param pCfgIf Pointer to configuration callback table.
827 * @param pszName Name of the key.
828 */
829DECLINLINE(bool) VDCFGIsKeyExisting(PVDINTERFACECONFIG pCfgIf, const char *pszName)
830{
831 size_t cb = 0;
832 int rc = pCfgIf->pfnQuerySize(pCfgIf->Core.pvUser, pszName, &cb);
833 return rc == VERR_CFGM_VALUE_NOT_FOUND ? false : true;
834}
835
836/**
837 * Query configuration, unsigned 64-bit integer value with default.
838 *
839 * @return VBox status code.
840 * @param pCfgIf Pointer to configuration callback table.
841 * @param pszName Name of an integer value
842 * @param pu64 Where to store the value. Set to default on failure.
843 * @param u64Def The default value.
844 */
845DECLINLINE(int) VDCFGQueryU64Def(PVDINTERFACECONFIG pCfgIf,
846 const char *pszName, uint64_t *pu64,
847 uint64_t u64Def)
848{
849 char aszBuf[32];
850 int rc = pCfgIf->pfnQuery(pCfgIf->Core.pvUser, pszName, aszBuf, sizeof(aszBuf));
851 if (RT_SUCCESS(rc))
852 {
853 rc = RTStrToUInt64Full(aszBuf, 0, pu64);
854 }
855 else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
856 {
857 rc = VINF_SUCCESS;
858 *pu64 = u64Def;
859 }
860 return rc;
861}
862
863/**
864 * Query configuration, unsigned 64-bit integer value.
865 *
866 * @return VBox status code.
867 * @param pCfgIf Pointer to configuration callback table.
868 * @param pszName Name of an integer value
869 * @param pu64 Where to store the value.
870 */
871DECLINLINE(int) VDCFGQueryU64(PVDINTERFACECONFIG pCfgIf, const char *pszName,
872 uint64_t *pu64)
873{
874 char aszBuf[32];
875 int rc = pCfgIf->pfnQuery(pCfgIf->Core.pvUser, pszName, aszBuf, sizeof(aszBuf));
876 if (RT_SUCCESS(rc))
877 {
878 rc = RTStrToUInt64Full(aszBuf, 0, pu64);
879 }
880
881 return rc;
882}
883
884/**
885 * Query configuration, unsigned 32-bit integer value with default.
886 *
887 * @return VBox status code.
888 * @param pCfgIf Pointer to configuration callback table.
889 * @param pszName Name of an integer value
890 * @param pu32 Where to store the value. Set to default on failure.
891 * @param u32Def The default value.
892 */
893DECLINLINE(int) VDCFGQueryU32Def(PVDINTERFACECONFIG pCfgIf,
894 const char *pszName, uint32_t *pu32,
895 uint32_t u32Def)
896{
897 uint64_t u64;
898 int rc = VDCFGQueryU64Def(pCfgIf, pszName, &u64, u32Def);
899 if (RT_SUCCESS(rc))
900 {
901 if (!(u64 & UINT64_C(0xffffffff00000000)))
902 *pu32 = (uint32_t)u64;
903 else
904 rc = VERR_CFGM_INTEGER_TOO_BIG;
905 }
906 return rc;
907}
908
909/**
910 * Query configuration, bool value with default.
911 *
912 * @return VBox status code.
913 * @param pCfgIf Pointer to configuration callback table.
914 * @param pszName Name of an integer value
915 * @param pf Where to store the value. Set to default on failure.
916 * @param fDef The default value.
917 */
918DECLINLINE(int) VDCFGQueryBoolDef(PVDINTERFACECONFIG pCfgIf,
919 const char *pszName, bool *pf,
920 bool fDef)
921{
922 uint64_t u64;
923 int rc = VDCFGQueryU64Def(pCfgIf, pszName, &u64, fDef);
924 if (RT_SUCCESS(rc))
925 *pf = u64 ? true : false;
926 return rc;
927}
928
929/**
930 * Query configuration, bool value.
931 *
932 * @return VBox status code.
933 * @param pCfgIf Pointer to configuration callback table.
934 * @param pszName Name of an integer value
935 * @param pf Where to store the value.
936 */
937DECLINLINE(int) VDCFGQueryBool(PVDINTERFACECONFIG pCfgIf, const char *pszName,
938 bool *pf)
939{
940 uint64_t u64;
941 int rc = VDCFGQueryU64(pCfgIf, pszName, &u64);
942 if (RT_SUCCESS(rc))
943 *pf = u64 ? true : false;
944 return rc;
945}
946
947/**
948 * Query configuration, dynamically allocated (RTMemAlloc) zero terminated
949 * character value.
950 *
951 * @return VBox status code.
952 * @param pCfgIf Pointer to configuration callback table.
953 * @param pszName Name of an zero terminated character value
954 * @param ppszString Where to store the string pointer. Not set on failure.
955 * Free this using RTMemFree().
956 */
957DECLINLINE(int) VDCFGQueryStringAlloc(PVDINTERFACECONFIG pCfgIf,
958 const char *pszName, char **ppszString)
959{
960 size_t cb;
961 int rc = pCfgIf->pfnQuerySize(pCfgIf->Core.pvUser, pszName, &cb);
962 if (RT_SUCCESS(rc))
963 {
964 char *pszString = (char *)RTMemAlloc(cb);
965 if (pszString)
966 {
967 rc = pCfgIf->pfnQuery(pCfgIf->Core.pvUser, pszName, pszString, cb);
968 if (RT_SUCCESS(rc))
969 *ppszString = pszString;
970 else
971 RTMemFree(pszString);
972 }
973 else
974 rc = VERR_NO_MEMORY;
975 }
976 return rc;
977}
978
979/**
980 * Query configuration, dynamically allocated (RTMemAlloc) zero terminated
981 * character value with default.
982 *
983 * @return VBox status code.
984 * @param pCfgIf Pointer to configuration callback table.
985 * @param pszName Name of an zero terminated character value
986 * @param ppszString Where to store the string pointer. Not set on failure.
987 * Free this using RTMemFree().
988 * @param pszDef The default value.
989 */
990DECLINLINE(int) VDCFGQueryStringAllocDef(PVDINTERFACECONFIG pCfgIf,
991 const char *pszName,
992 char **ppszString,
993 const char *pszDef)
994{
995 size_t cb;
996 int rc = pCfgIf->pfnQuerySize(pCfgIf->Core.pvUser, pszName, &cb);
997 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
998 {
999 cb = strlen(pszDef) + 1;
1000 rc = VINF_SUCCESS;
1001 }
1002 if (RT_SUCCESS(rc))
1003 {
1004 char *pszString = (char *)RTMemAlloc(cb);
1005 if (pszString)
1006 {
1007 rc = pCfgIf->pfnQuery(pCfgIf->Core.pvUser, pszName, pszString, cb);
1008 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
1009 {
1010 memcpy(pszString, pszDef, cb);
1011 rc = VINF_SUCCESS;
1012 }
1013 if (RT_SUCCESS(rc))
1014 *ppszString = pszString;
1015 else
1016 RTMemFree(pszString);
1017 }
1018 else
1019 rc = VERR_NO_MEMORY;
1020 }
1021 return rc;
1022}
1023
1024/**
1025 * Query configuration, dynamically allocated (RTMemAlloc) byte string value.
1026 *
1027 * @return VBox status code.
1028 * @param pCfgIf Pointer to configuration callback table.
1029 * @param pszName Name of an zero terminated character value
1030 * @param ppvData Where to store the byte string pointer. Not set on failure.
1031 * Free this using RTMemFree().
1032 * @param pcbData Where to store the byte string length.
1033 */
1034DECLINLINE(int) VDCFGQueryBytesAlloc(PVDINTERFACECONFIG pCfgIf,
1035 const char *pszName, void **ppvData, size_t *pcbData)
1036{
1037 size_t cb;
1038 int rc = pCfgIf->pfnQuerySize(pCfgIf->Core.pvUser, pszName, &cb);
1039 if (RT_SUCCESS(rc))
1040 {
1041 char *pbData;
1042 Assert(cb);
1043
1044 pbData = (char *)RTMemAlloc(cb);
1045 if (pbData)
1046 {
1047 if(pCfgIf->pfnQueryBytes)
1048 rc = pCfgIf->pfnQueryBytes(pCfgIf->Core.pvUser, pszName, pbData, cb);
1049 else
1050 rc = pCfgIf->pfnQuery(pCfgIf->Core.pvUser, pszName, pbData, cb);
1051
1052 if (RT_SUCCESS(rc))
1053 {
1054 *ppvData = pbData;
1055 /* Exclude terminator if the byte data was obtained using the string query callback. */
1056 *pcbData = cb;
1057 if (!pCfgIf->pfnQueryBytes)
1058 (*pcbData)--;
1059 }
1060 else
1061 RTMemFree(pbData);
1062 }
1063 else
1064 rc = VERR_NO_MEMORY;
1065 }
1066 return rc;
1067}
1068
1069/** Forward declaration of a VD socket. */
1070typedef struct VDSOCKETINT *VDSOCKET;
1071/** Pointer to a VD socket. */
1072typedef VDSOCKET *PVDSOCKET;
1073/** Nil socket handle. */
1074#define NIL_VDSOCKET ((VDSOCKET)0)
1075
1076/** Connect flag to indicate that the backend wants to use the extended
1077 * socket I/O multiplexing call. This might not be supported on all configurations
1078 * (internal networking and iSCSI)
1079 * and the backend needs to take appropriate action.
1080 */
1081#define VD_INTERFACETCPNET_CONNECT_EXTENDED_SELECT RT_BIT_32(0)
1082
1083/** @name Select events
1084 * @{ */
1085/** Readable without blocking. */
1086#define VD_INTERFACETCPNET_EVT_READ RT_BIT_32(0)
1087/** Writable without blocking. */
1088#define VD_INTERFACETCPNET_EVT_WRITE RT_BIT_32(1)
1089/** Error condition, hangup, exception or similar. */
1090#define VD_INTERFACETCPNET_EVT_ERROR RT_BIT_32(2)
1091/** Hint for the select that getting interrupted while waiting is more likely.
1092 * The interface implementation can optimize the waiting strategy based on this.
1093 * It is assumed that it is more likely to get one of the above socket events
1094 * instead of being interrupted if the flag is not set. */
1095#define VD_INTERFACETCPNET_HINT_INTERRUPT RT_BIT_32(3)
1096/** Mask of the valid bits. */
1097#define VD_INTERFACETCPNET_EVT_VALID_MASK UINT32_C(0x0000000f)
1098/** @} */
1099
1100/**
1101 * TCP network stack interface
1102 *
1103 * Per-image. Mandatory for backends which have the VD_CAP_TCPNET bit set.
1104 */
1105typedef struct VDINTERFACETCPNET
1106{
1107 /**
1108 * Common interface header.
1109 */
1110 VDINTERFACE Core;
1111
1112 /**
1113 * Creates a socket. The socket is not connected if this succeeds.
1114 *
1115 * @return iprt status code.
1116 * @retval VERR_NOT_SUPPORTED if the combination of flags is not supported.
1117 * @param fFlags Combination of the VD_INTERFACETCPNET_CONNECT_* \#defines.
1118 * @param pSock Where to store the handle.
1119 */
1120 DECLR3CALLBACKMEMBER(int, pfnSocketCreate, (uint32_t fFlags, PVDSOCKET pSock));
1121
1122 /**
1123 * Destroys the socket.
1124 *
1125 * @return iprt status code.
1126 * @param Sock Socket descriptor.
1127 */
1128 DECLR3CALLBACKMEMBER(int, pfnSocketDestroy, (VDSOCKET Sock));
1129
1130 /**
1131 * Connect as a client to a TCP port.
1132 *
1133 * @return iprt status code.
1134 * @param Sock Socket descriptor.
1135 * @param pszAddress The address to connect to.
1136 * @param uPort The port to connect to.
1137 * @param cMillies Number of milliseconds to wait for the connect attempt to complete.
1138 * Use RT_INDEFINITE_WAIT to wait for ever.
1139 * Use RT_SOCKETCONNECT_DEFAULT_WAIT to wait for the default time
1140 * configured on the running system.
1141 */
1142 DECLR3CALLBACKMEMBER(int, pfnClientConnect, (VDSOCKET Sock, const char *pszAddress, uint32_t uPort,
1143 RTMSINTERVAL cMillies));
1144
1145 /**
1146 * Close a TCP connection.
1147 *
1148 * @return iprt status code.
1149 * @param Sock Socket descriptor.
1150 */
1151 DECLR3CALLBACKMEMBER(int, pfnClientClose, (VDSOCKET Sock));
1152
1153 /**
1154 * Returns whether the socket is currently connected to the client.
1155 *
1156 * @returns true if the socket is connected.
1157 * false otherwise.
1158 * @param Sock Socket descriptor.
1159 */
1160 DECLR3CALLBACKMEMBER(bool, pfnIsClientConnected, (VDSOCKET Sock));
1161
1162 /**
1163 * Socket I/O multiplexing.
1164 * Checks if the socket is ready for reading.
1165 *
1166 * @return iprt status code.
1167 * @param Sock Socket descriptor.
1168 * @param cMillies Number of milliseconds to wait for the socket.
1169 * Use RT_INDEFINITE_WAIT to wait for ever.
1170 */
1171 DECLR3CALLBACKMEMBER(int, pfnSelectOne, (VDSOCKET Sock, RTMSINTERVAL cMillies));
1172
1173 /**
1174 * Receive data from a socket.
1175 *
1176 * @return iprt status code.
1177 * @param Sock Socket descriptor.
1178 * @param pvBuffer Where to put the data we read.
1179 * @param cbBuffer Read buffer size.
1180 * @param pcbRead Number of bytes read.
1181 * If NULL the entire buffer will be filled upon successful return.
1182 * If not NULL a partial read can be done successfully.
1183 */
1184 DECLR3CALLBACKMEMBER(int, pfnRead, (VDSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead));
1185
1186 /**
1187 * Send data to a socket.
1188 *
1189 * @return iprt status code.
1190 * @param Sock Socket descriptor.
1191 * @param pvBuffer Buffer to write data to socket.
1192 * @param cbBuffer How much to write.
1193 */
1194 DECLR3CALLBACKMEMBER(int, pfnWrite, (VDSOCKET Sock, const void *pvBuffer, size_t cbBuffer));
1195
1196 /**
1197 * Send data from scatter/gather buffer to a socket.
1198 *
1199 * @return iprt status code.
1200 * @param Sock Socket descriptor.
1201 * @param pSgBuffer Scatter/gather buffer to write data to socket.
1202 */
1203 DECLR3CALLBACKMEMBER(int, pfnSgWrite, (VDSOCKET Sock, PCRTSGBUF pSgBuffer));
1204
1205 /**
1206 * Receive data from a socket - not blocking.
1207 *
1208 * @return iprt status code.
1209 * @param Sock Socket descriptor.
1210 * @param pvBuffer Where to put the data we read.
1211 * @param cbBuffer Read buffer size.
1212 * @param pcbRead Number of bytes read.
1213 */
1214 DECLR3CALLBACKMEMBER(int, pfnReadNB, (VDSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead));
1215
1216 /**
1217 * Send data to a socket - not blocking.
1218 *
1219 * @return iprt status code.
1220 * @param Sock Socket descriptor.
1221 * @param pvBuffer Buffer to write data to socket.
1222 * @param cbBuffer How much to write.
1223 * @param pcbWritten Number of bytes written.
1224 */
1225 DECLR3CALLBACKMEMBER(int, pfnWriteNB, (VDSOCKET Sock, const void *pvBuffer, size_t cbBuffer, size_t *pcbWritten));
1226
1227 /**
1228 * Send data from scatter/gather buffer to a socket - not blocking.
1229 *
1230 * @return iprt status code.
1231 * @param Sock Socket descriptor.
1232 * @param pSgBuffer Scatter/gather buffer to write data to socket.
1233 * @param pcbWritten Number of bytes written.
1234 */
1235 DECLR3CALLBACKMEMBER(int, pfnSgWriteNB, (VDSOCKET Sock, PRTSGBUF pSgBuffer, size_t *pcbWritten));
1236
1237 /**
1238 * Flush socket write buffers.
1239 *
1240 * @return iprt status code.
1241 * @param Sock Socket descriptor.
1242 */
1243 DECLR3CALLBACKMEMBER(int, pfnFlush, (VDSOCKET Sock));
1244
1245 /**
1246 * Enables or disables delaying sends to coalesce packets.
1247 *
1248 * @return iprt status code.
1249 * @param Sock Socket descriptor.
1250 * @param fEnable When set to true enables coalescing.
1251 */
1252 DECLR3CALLBACKMEMBER(int, pfnSetSendCoalescing, (VDSOCKET Sock, bool fEnable));
1253
1254 /**
1255 * Gets the address of the local side.
1256 *
1257 * @return iprt status code.
1258 * @param Sock Socket descriptor.
1259 * @param pAddr Where to store the local address on success.
1260 */
1261 DECLR3CALLBACKMEMBER(int, pfnGetLocalAddress, (VDSOCKET Sock, PRTNETADDR pAddr));
1262
1263 /**
1264 * Gets the address of the other party.
1265 *
1266 * @return iprt status code.
1267 * @param Sock Socket descriptor.
1268 * @param pAddr Where to store the peer address on success.
1269 */
1270 DECLR3CALLBACKMEMBER(int, pfnGetPeerAddress, (VDSOCKET Sock, PRTNETADDR pAddr));
1271
1272 /**
1273 * Socket I/O multiplexing - extended version which can be woken up.
1274 * Checks if the socket is ready for reading or writing.
1275 *
1276 * @return iprt status code.
1277 * @retval VERR_INTERRUPTED if the thread was woken up by a pfnPoke call.
1278 * @param Sock Socket descriptor.
1279 * @param fEvents Mask of events to wait for.
1280 * @param pfEvents Where to store the received events.
1281 * @param cMillies Number of milliseconds to wait for the socket.
1282 * Use RT_INDEFINITE_WAIT to wait for ever.
1283 */
1284 DECLR3CALLBACKMEMBER(int, pfnSelectOneEx, (VDSOCKET Sock, uint32_t fEvents,
1285 uint32_t *pfEvents, RTMSINTERVAL cMillies));
1286
1287 /**
1288 * Wakes up the thread waiting in pfnSelectOneEx.
1289 *
1290 * @return iprt status code.
1291 * @param Sock Socket descriptor.
1292 */
1293 DECLR3CALLBACKMEMBER(int, pfnPoke, (VDSOCKET Sock));
1294
1295} VDINTERFACETCPNET, *PVDINTERFACETCPNET;
1296
1297/**
1298 * Get TCP network stack interface from interface list.
1299 *
1300 * @return Pointer to the first TCP network stack interface in the list.
1301 * @param pVDIfs Pointer to the interface list.
1302 */
1303DECLINLINE(PVDINTERFACETCPNET) VDIfTcpNetGet(PVDINTERFACE pVDIfs)
1304{
1305 PVDINTERFACE pIf = VDInterfaceGet(pVDIfs, VDINTERFACETYPE_TCPNET);
1306
1307 /* Check that the interface descriptor is a progress interface. */
1308 AssertMsgReturn( !pIf
1309 || ( (pIf->enmInterface == VDINTERFACETYPE_TCPNET)
1310 && (pIf->cbSize == sizeof(VDINTERFACETCPNET))),
1311 ("Not a TCP net interface"), NULL);
1312
1313 return (PVDINTERFACETCPNET)pIf;
1314}
1315
1316
1317/**
1318 * Interface to synchronize concurrent accesses by several threads.
1319 *
1320 * @note The scope of this interface is to manage concurrent accesses after
1321 * the HDD container has been created, and they must stop before destroying the
1322 * container. Opening or closing images is covered by the synchronization, but
1323 * that does not mean it is safe to close images while a thread executes
1324 * #VDMerge or #VDCopy operating on these images. Making them safe would require
1325 * the lock to be held during the entire operation, which prevents other
1326 * concurrent acitivities.
1327 *
1328 * @note Right now this is kept as simple as possible, and does not even
1329 * attempt to provide enough information to allow e.g. concurrent write
1330 * accesses to different areas of the disk. The reason is that it is very
1331 * difficult to predict which area of a disk is affected by a write,
1332 * especially when different image formats are mixed. Maybe later a more
1333 * sophisticated interface will be provided which has the necessary information
1334 * about worst case affected areas.
1335 *
1336 * Per-disk interface. Optional, needed if the disk is accessed concurrently
1337 * by several threads, e.g. when merging diff images while a VM is running.
1338 */
1339typedef struct VDINTERFACETHREADSYNC
1340{
1341 /**
1342 * Common interface header.
1343 */
1344 VDINTERFACE Core;
1345
1346 /**
1347 * Start a read operation.
1348 */
1349 DECLR3CALLBACKMEMBER(int, pfnStartRead, (void *pvUser));
1350
1351 /**
1352 * Finish a read operation.
1353 */
1354 DECLR3CALLBACKMEMBER(int, pfnFinishRead, (void *pvUser));
1355
1356 /**
1357 * Start a write operation.
1358 */
1359 DECLR3CALLBACKMEMBER(int, pfnStartWrite, (void *pvUser));
1360
1361 /**
1362 * Finish a write operation.
1363 */
1364 DECLR3CALLBACKMEMBER(int, pfnFinishWrite, (void *pvUser));
1365
1366} VDINTERFACETHREADSYNC, *PVDINTERFACETHREADSYNC;
1367
1368/**
1369 * Get thread synchronization interface from interface list.
1370 *
1371 * @return Pointer to the first thread synchronization interface in the list.
1372 * @param pVDIfs Pointer to the interface list.
1373 */
1374DECLINLINE(PVDINTERFACETHREADSYNC) VDIfThreadSyncGet(PVDINTERFACE pVDIfs)
1375{
1376 PVDINTERFACE pIf = VDInterfaceGet(pVDIfs, VDINTERFACETYPE_THREADSYNC);
1377
1378 /* Check that the interface descriptor is a progress interface. */
1379 AssertMsgReturn( !pIf
1380 || ( (pIf->enmInterface == VDINTERFACETYPE_THREADSYNC)
1381 && (pIf->cbSize == sizeof(VDINTERFACETHREADSYNC))),
1382 ("Not a thread synchronization interface"), NULL);
1383
1384 return (PVDINTERFACETHREADSYNC)pIf;
1385}
1386
1387/**
1388 * Interface to query usage of disk ranges.
1389 *
1390 * Per-operation interface. Optional.
1391 */
1392typedef struct VDINTERFACEQUERYRANGEUSE
1393{
1394 /**
1395 * Common interface header.
1396 */
1397 VDINTERFACE Core;
1398
1399 /**
1400 * Query use of a disk range.
1401 */
1402 DECLR3CALLBACKMEMBER(int, pfnQueryRangeUse, (void *pvUser, uint64_t off, uint64_t cb,
1403 bool *pfUsed));
1404
1405} VDINTERFACEQUERYRANGEUSE, *PVDINTERFACEQUERYRANGEUSE;
1406
1407/**
1408 * Get query range use interface from interface list.
1409 *
1410 * @return Pointer to the first thread synchronization interface in the list.
1411 * @param pVDIfs Pointer to the interface list.
1412 */
1413DECLINLINE(PVDINTERFACEQUERYRANGEUSE) VDIfQueryRangeUseGet(PVDINTERFACE pVDIfs)
1414{
1415 PVDINTERFACE pIf = VDInterfaceGet(pVDIfs, VDINTERFACETYPE_QUERYRANGEUSE);
1416
1417 /* Check that the interface descriptor is a progress interface. */
1418 AssertMsgReturn( !pIf
1419 || ( (pIf->enmInterface == VDINTERFACETYPE_QUERYRANGEUSE)
1420 && (pIf->cbSize == sizeof(VDINTERFACEQUERYRANGEUSE))),
1421 ("Not a query range use interface"), NULL);
1422
1423 return (PVDINTERFACEQUERYRANGEUSE)pIf;
1424}
1425
1426DECLINLINE(int) vdIfQueryRangeUse(PVDINTERFACEQUERYRANGEUSE pIfQueryRangeUse, uint64_t off, uint64_t cb,
1427 bool *pfUsed)
1428{
1429 return pIfQueryRangeUse->pfnQueryRangeUse(pIfQueryRangeUse->Core.pvUser, off, cb, pfUsed);
1430}
1431
1432
1433/**
1434 * Interface used to retrieve keys for cryptographic operations.
1435 *
1436 * Per-module interface. Optional but cryptographic modules might fail and
1437 * return an error if this is not present.
1438 */
1439typedef struct VDINTERFACECRYPTO
1440{
1441 /**
1442 * Common interface header.
1443 */
1444 VDINTERFACE Core;
1445
1446 /**
1447 * Retains a key identified by the ID. The caller will only hold a reference
1448 * to the key and must not modify the key buffer in any way.
1449 *
1450 * @returns VBox status code.
1451 * @param pvUser The opaque user data associated with this interface.
1452 * @param pszId The alias/id for the key to retrieve.
1453 * @param ppbKey Where to store the pointer to the key buffer on success.
1454 * @param pcbKey Where to store the size of the key in bytes on success.
1455 */
1456 DECLR3CALLBACKMEMBER(int, pfnKeyRetain, (void *pvUser, const char *pszId, const uint8_t **ppbKey, size_t *pcbKey));
1457
1458 /**
1459 * Releases one reference of the key identified by the given identifier.
1460 * The caller must not access the key buffer after calling this operation.
1461 *
1462 * @returns VBox status code.
1463 * @param pvUser The opaque user data associated with this interface.
1464 * @param pszId The alias/id for the key to release.
1465 *
1466 * @note It is advised to release the key whenever it is not used anymore so
1467 * the entity storing the key can do anything to make retrieving the key
1468 * from memory more difficult like scrambling the memory buffer for
1469 * instance.
1470 */
1471 DECLR3CALLBACKMEMBER(int, pfnKeyRelease, (void *pvUser, const char *pszId));
1472
1473 /**
1474 * Gets a reference to the password identified by the given ID to open a key store supplied through the config interface.
1475 *
1476 * @returns VBox status code.
1477 * @param pvUser The opaque user data associated with this interface.
1478 * @param pszId The alias/id for the password to retain.
1479 * @param ppszPassword Where to store the password to unlock the key store on success.
1480 */
1481 DECLR3CALLBACKMEMBER(int, pfnKeyStorePasswordRetain, (void *pvUser, const char *pszId, const char **ppszPassword));
1482
1483 /**
1484 * Releases a reference of the password previously acquired with VDINTERFACECRYPTO::pfnKeyStorePasswordRetain()
1485 * identified by the given ID.
1486 *
1487 * @returns VBox status code.
1488 * @param pvUser The opaque user data associated with this interface.
1489 * @param pszId The alias/id for the password to release.
1490 */
1491 DECLR3CALLBACKMEMBER(int, pfnKeyStorePasswordRelease, (void *pvUser, const char *pszId));
1492
1493 /**
1494 * Saves a key store.
1495 *
1496 * @returns VBox status code.
1497 * @param pvUser The opaque user data associated with this interface.
1498 * @param pvKeyStore The key store to save.
1499 * @param cbKeyStore Size of the key store in bytes.
1500 *
1501 * @note The format is filter specific and should be treated as binary data.
1502 */
1503 DECLR3CALLBACKMEMBER(int, pfnKeyStoreSave, (void *pvUser, const void *pvKeyStore, size_t cbKeyStore));
1504
1505 /**
1506 * Returns the parameters after the key store was loaded successfully.
1507 *
1508 * @returns VBox status code.
1509 * @param pvUser The opaque user data associated with this interface.
1510 * @param pszCipher The cipher identifier the DEK is used for.
1511 * @param pbDek The raw DEK which was contained in the key store loaded by
1512 * VDINTERFACECRYPTO::pfnKeyStoreLoad().
1513 * @param cbDek The size of the DEK.
1514 *
1515 * @note The provided pointer to the DEK is only valid until this call returns.
1516 * The content might change afterwards with out notice (when scrambling the key
1517 * for further protection for example) or might be even freed.
1518 *
1519 * @note This method is optional and can be NULL if the caller does not require the
1520 * parameters.
1521 */
1522 DECLR3CALLBACKMEMBER(int, pfnKeyStoreReturnParameters, (void *pvUser, const char *pszCipher,
1523 const uint8_t *pbDek, size_t cbDek));
1524
1525} VDINTERFACECRYPTO, *PVDINTERFACECRYPTO;
1526
1527
1528/**
1529 * Get error interface from interface list.
1530 *
1531 * @return Pointer to the first error interface in the list.
1532 * @param pVDIfs Pointer to the interface list.
1533 */
1534DECLINLINE(PVDINTERFACECRYPTO) VDIfCryptoGet(PVDINTERFACE pVDIfs)
1535{
1536 PVDINTERFACE pIf = VDInterfaceGet(pVDIfs, VDINTERFACETYPE_CRYPTO);
1537
1538 /* Check that the interface descriptor is a crypto interface. */
1539 AssertMsgReturn( !pIf
1540 || ( (pIf->enmInterface == VDINTERFACETYPE_CRYPTO)
1541 && (pIf->cbSize == sizeof(VDINTERFACECRYPTO))),
1542 ("Not an crypto interface\n"), NULL);
1543
1544 return (PVDINTERFACECRYPTO)pIf;
1545}
1546
1547/**
1548 * Retains a key identified by the ID. The caller will only hold a reference
1549 * to the key and must not modify the key buffer in any way.
1550 *
1551 * @returns VBox status code.
1552 * @param pIfCrypto Pointer to the crypto interface.
1553 * @param pszId The alias/id for the key to retrieve.
1554 * @param ppbKey Where to store the pointer to the key buffer on success.
1555 * @param pcbKey Where to store the size of the key in bytes on success.
1556 */
1557DECLINLINE(int) vdIfCryptoKeyRetain(PVDINTERFACECRYPTO pIfCrypto, const char *pszId, const uint8_t **ppbKey, size_t *pcbKey)
1558{
1559 return pIfCrypto->pfnKeyRetain(pIfCrypto->Core.pvUser, pszId, ppbKey, pcbKey);
1560}
1561
1562/**
1563 * Releases one reference of the key identified by the given identifier.
1564 * The caller must not access the key buffer after calling this operation.
1565 *
1566 * @returns VBox status code.
1567 * @param pIfCrypto Pointer to the crypto interface.
1568 * @param pszId The alias/id for the key to release.
1569 *
1570 * @note It is advised to release the key whenever it is not used anymore so
1571 * the entity storing the key can do anything to make retrieving the key
1572 * from memory more difficult like scrambling the memory buffer for
1573 * instance.
1574 */
1575DECLINLINE(int) vdIfCryptoKeyRelease(PVDINTERFACECRYPTO pIfCrypto, const char *pszId)
1576{
1577 return pIfCrypto->pfnKeyRelease(pIfCrypto->Core.pvUser, pszId);
1578}
1579
1580/**
1581 * Gets a reference to the password identified by the given ID to open a key store supplied through the config interface.
1582 *
1583 * @returns VBox status code.
1584 * @param pIfCrypto Pointer to the crypto interface.
1585 * @param pszId The alias/id for the password to retain.
1586 * @param ppszPassword Where to store the password to unlock the key store on success.
1587 */
1588DECLINLINE(int) vdIfCryptoKeyStorePasswordRetain(PVDINTERFACECRYPTO pIfCrypto, const char *pszId, const char **ppszPassword)
1589{
1590 return pIfCrypto->pfnKeyStorePasswordRetain(pIfCrypto->Core.pvUser, pszId, ppszPassword);
1591}
1592
1593/**
1594 * Releases a reference of the password previously acquired with VDINTERFACECRYPTO::pfnKeyStorePasswordRetain()
1595 * identified by the given ID.
1596 *
1597 * @returns VBox status code.
1598 * @param pIfCrypto Pointer to the crypto interface.
1599 * @param pszId The alias/id for the password to release.
1600 */
1601DECLINLINE(int) vdIfCryptoKeyStorePasswordRelease(PVDINTERFACECRYPTO pIfCrypto, const char *pszId)
1602{
1603 return pIfCrypto->pfnKeyStorePasswordRelease(pIfCrypto->Core.pvUser, pszId);
1604}
1605
1606/**
1607 * Saves a key store.
1608 *
1609 * @returns VBox status code.
1610 * @param pIfCrypto Pointer to the crypto interface.
1611 * @param pvKeyStore The key store to save.
1612 * @param cbKeyStore Size of the key store in bytes.
1613 *
1614 * @note The format is filter specific and should be treated as binary data.
1615 */
1616DECLINLINE(int) vdIfCryptoKeyStoreSave(PVDINTERFACECRYPTO pIfCrypto, const void *pvKeyStore, size_t cbKeyStore)
1617{
1618 return pIfCrypto->pfnKeyStoreSave(pIfCrypto->Core.pvUser, pvKeyStore, cbKeyStore);
1619}
1620
1621/**
1622 * Returns the parameters after the key store was loaded successfully.
1623 *
1624 * @returns VBox status code.
1625 * @param pIfCrypto Pointer to the crypto interface.
1626 * @param pszCipher The cipher identifier the DEK is used for.
1627 * @param pbDek The raw DEK which was contained in the key store loaded by
1628 * VDINTERFACECRYPTO::pfnKeyStoreLoad().
1629 * @param cbDek The size of the DEK.
1630 *
1631 * @note The provided pointer to the DEK is only valid until this call returns.
1632 * The content might change afterwards with out notice (when scrambling the key
1633 * for further protection for example) or might be even freed.
1634 *
1635 * @note This method is optional and can be NULL if the caller does not require the
1636 * parameters.
1637 */
1638DECLINLINE(int) vdIfCryptoKeyStoreReturnParameters(PVDINTERFACECRYPTO pIfCrypto, const char *pszCipher,
1639 const uint8_t *pbDek, size_t cbDek)
1640{
1641 if (pIfCrypto->pfnKeyStoreReturnParameters)
1642 return pIfCrypto->pfnKeyStoreReturnParameters(pIfCrypto->Core.pvUser, pszCipher, pbDek, cbDek);
1643
1644 return VINF_SUCCESS;
1645}
1646
1647
1648RT_C_DECLS_END
1649
1650/** @} */
1651
1652#endif
Note: See TracBrowser for help on using the repository browser.

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