summaryrefslogtreecommitdiffstats
path: root/src/VBox/Storage/VDIfVfs2.cpp
blob: 27c905dd70a46c62fae447e6a487264c47e2379f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* $Id: VDIfVfs2.cpp $ */
/** @file
 * Virtual Disk Image (VDI), I/O interface to IPRT VFS I/O stream glue.
 */

/*
 * Copyright (C) 2012-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include <iprt/types.h>
#include <iprt/assert.h>
#include <iprt/mem.h>
#include <iprt/err.h>
#include <iprt/asm.h>
#include <iprt/string.h>
#include <iprt/file.h>
#include <iprt/sg.h>
#include <iprt/vfslowlevel.h>
#include <iprt/poll.h>
#include <VBox/vd.h>
#include <VBox/vd-ifs-internal.h>

#include <VBox/log.h>


/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/
/**
 * Extended VD I/O interface structure that vdIfFromVfs_xxx uses.
 *
 * It's passed as pvUser to each call.
 */
typedef struct VDIFFROMVFS
{
    VDINTERFACEIO   CoreIo;

    /** Magic.  */
    uint32_t        u32Magic;
    /** The stream access mode (RTFILE_O_ACCESS_MASK), possibly others. */
    uint32_t        fAccessMode;
    /** The I/O stream.  This is NIL after it's been closed. */
    RTVFSIOSTREAM   hVfsIos;
    /** Completion callback. */
    PFNVDCOMPLETED  pfnCompleted;
    /** User parameter for the completion callback. */
    void           *pvCompletedUser;
    /** Set if hVfsIos has been opened. */
    bool            fOpened;
} VDIFFROMVFS;
/** Magic value for VDIFFROMVFS::u32Magic. */
#define VDIFFROMVFS_MAGIC   UINT32_C(0x11223344)

/** Pointer to the instance data for the vdIfFromVfs_ methods. */
typedef struct VDIFFROMVFS *PVDIFFROMVFS;


typedef struct FILESTORAGEINTERNAL
{
    /** File handle. */
    RTFILE         file;
} FILESTORAGEINTERNAL, *PFILESTORAGEINTERNAL;


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/

#define STATUS_WAIT    UINT32_C(0)
#define STATUS_WRITE   UINT32_C(1)
#define STATUS_WRITING UINT32_C(2)
#define STATUS_READ    UINT32_C(3)
#define STATUS_READING UINT32_C(4)
#define STATUS_END     UINT32_C(5)

/* Enable for getting some flow history. */
#if 0
# define DEBUG_PRINT_FLOW() RTPrintf("%s\n", __FUNCTION__)
#else
# define DEBUG_PRINT_FLOW() do {} while (0)
#endif


/*********************************************************************************************************************************
*   Internal Functions                                                                                                           *
*********************************************************************************************************************************/


/** @name VDINTERFACEIO stubs returning not-implemented.
 * @{
 */

/** @interface_method_impl{VDINTERFACEIO,pfnDelete}  */
static DECLCALLBACK(int) notImpl_Delete(void *pvUser, const char *pcszFilename)
{
    NOREF(pvUser); NOREF(pcszFilename);
    Log(("%s\n",  __FUNCTION__));
    AssertFailed();
    return VERR_NOT_IMPLEMENTED;
}

/** @interface_method_impl{VDINTERFACEIO,pfnMove}  */
static DECLCALLBACK(int) notImpl_Move(void *pvUser, const char *pcszSrc, const char *pcszDst, unsigned fMove)
{
    NOREF(pvUser); NOREF(pcszSrc); NOREF(pcszDst); NOREF(fMove);
    Log(("%s\n",  __FUNCTION__));
    AssertFailed();
    return VERR_NOT_IMPLEMENTED;
}

/** @interface_method_impl{VDINTERFACEIO,pfnGetFreeSpace}  */
static DECLCALLBACK(int) notImpl_GetFreeSpace(void *pvUser, const char *pcszFilename, int64_t *pcbFreeSpace)
{
    NOREF(pvUser); NOREF(pcszFilename); NOREF(pcbFreeSpace);
    Log(("%s\n",  __FUNCTION__));
    AssertFailed();
    return VERR_NOT_IMPLEMENTED;
}

/** @interface_method_impl{VDINTERFACEIO,pfnGetModificationTime}  */
static DECLCALLBACK(int) notImpl_GetModificationTime(void *pvUser, const char *pcszFilename, PRTTIMESPEC pModificationTime)
{
    NOREF(pvUser); NOREF(pcszFilename); NOREF(pModificationTime);
    Log(("%s\n",  __FUNCTION__));
    AssertFailed();
    return VERR_NOT_IMPLEMENTED;
}

/** @interface_method_impl{VDINTERFACEIO,pfnSetSize}  */
static DECLCALLBACK(int) notImpl_SetSize(void *pvUser, void *pvStorage, uint64_t cb)
{
    NOREF(pvUser); NOREF(pvStorage); NOREF(cb);
    Log(("%s\n",  __FUNCTION__));
    AssertFailed();
    return VERR_NOT_IMPLEMENTED;
}

#if 0  /* unused */
/** @interface_method_impl{VDINTERFACEIO,pfnWriteSync}  */
static DECLCALLBACK(int) notImpl_WriteSync(void *pvUser, void *pvStorage, uint64_t off, const void *pvBuf,
                                           size_t cbWrite, size_t *pcbWritten)
{
    RT_NOREF6(pvUser, pvStorage, off, pvBuf, cbWrite, pcbWritten)
    Log(("%s\n",  __FUNCTION__));
    return VERR_NOT_IMPLEMENTED;
}
#endif

/** @interface_method_impl{VDINTERFACEIO,pfnFlushSync}  */
static DECLCALLBACK(int) notImpl_FlushSync(void *pvUser, void *pvStorage)
{
    NOREF(pvUser); NOREF(pvStorage);
    Log(("%s\n",  __FUNCTION__));
    AssertFailed();
    return VERR_NOT_IMPLEMENTED;
}

/** @} */


/** @interface_method_impl{VDINTERFACEIO,pfnOpen}  */
static DECLCALLBACK(int) vdIfFromVfs_Open(void *pvUser, const char *pszLocation, uint32_t fOpen,
                                          PFNVDCOMPLETED pfnCompleted, void **ppvStorage)
{
    RT_NOREF1(pszLocation);
    PVDIFFROMVFS pThis = (PVDIFFROMVFS)pvUser;

    /*
     * Validate input.
     */
    AssertPtrReturn(ppvStorage, VERR_INVALID_POINTER);
    AssertPtrNullReturn(pfnCompleted, VERR_INVALID_PARAMETER);

    /*
     * We ignore the name, assuming the caller is opening the stream/file we're        .
     * serving.  Thus, after close, all open calls fail.
     */
    AssertReturn(!pThis->fOpened, VERR_FILE_NOT_FOUND);
    AssertReturn(pThis->hVfsIos != NIL_RTVFSIOSTREAM, VERR_FILE_NOT_FOUND); /* paranoia */
    AssertMsgReturn((pThis->fAccessMode & fOpen & RTFILE_O_ACCESS_MASK) == (fOpen & RTFILE_O_ACCESS_MASK),
                    ("fAccessMode=%#x fOpen=%#x\n", pThis->fAccessMode, fOpen), VERR_ACCESS_DENIED);

    pThis->fAccessMode      = fOpen & RTFILE_O_ACCESS_MASK;
    pThis->fOpened          = true;
    pThis->pfnCompleted     = pfnCompleted;
    pThis->pvCompletedUser  = pvUser;

    *ppvStorage = pThis->hVfsIos;
    return VINF_SUCCESS;
}

/** @interface_method_impl{VDINTERFACEIO,pfnClose}  */
static DECLCALLBACK(int) vdIfFromVfs_Close(void *pvUser, void *pvStorage)
{
    PVDIFFROMVFS pThis = (PVDIFFROMVFS)pvUser;
    AssertPtrReturn(pThis, VERR_INVALID_POINTER);
    AssertReturn(pThis->hVfsIos == (RTVFSIOSTREAM)pvStorage, VERR_INVALID_HANDLE);
    AssertReturn(pThis->fOpened, VERR_INVALID_HANDLE);

    RTVfsIoStrmRelease(pThis->hVfsIos);
    pThis->hVfsIos = NIL_RTVFSIOSTREAM;

    return VINF_SUCCESS;
}


/** @interface_method_impl{VDINTERFACEIO,pfnGetSize}  */
static DECLCALLBACK(int) vdIfFromVfs_GetSize(void *pvUser, void *pvStorage, uint64_t *pcb)
{
    PVDIFFROMVFS            pThis = (PVDIFFROMVFS)pvUser;
    AssertPtrReturn(pThis, VERR_INVALID_POINTER);
    AssertReturn(pThis->hVfsIos == (RTVFSIOSTREAM)pvStorage, VERR_INVALID_HANDLE);
    AssertReturn(pThis->fOpened, VERR_INVALID_HANDLE);

    RTFSOBJINFO ObjInfo;
    int rc = RTVfsIoStrmQueryInfo(pThis->hVfsIos, &ObjInfo, RTFSOBJATTRADD_NOTHING);
    if (RT_SUCCESS(rc))
        *pcb = ObjInfo.cbObject;
    return rc;
}

/** @interface_method_impl{VDINTERFACEIO,pfnReadSync}  */
static DECLCALLBACK(int) vdIfFromVfs_ReadSync(void *pvUser, void *pvStorage, uint64_t off, void *pvBuf,
                                              size_t cbToRead, size_t *pcbRead)
{
    PVDIFFROMVFS            pThis = (PVDIFFROMVFS)pvUser;
    AssertPtrReturn(pThis, VERR_INVALID_POINTER);
    AssertReturn(pThis->hVfsIos == (RTVFSIOSTREAM)pvStorage, VERR_INVALID_HANDLE);
    AssertReturn(pThis->fOpened, VERR_INVALID_HANDLE);
    AssertPtrNullReturn(pcbRead, VERR_INVALID_POINTER);
    AssertReturn(pThis->fAccessMode & RTFILE_O_READ, VERR_ACCESS_DENIED);

    return RTVfsIoStrmReadAt(pThis->hVfsIos, off, pvBuf, cbToRead, true /*fBlocking*/, pcbRead);
}


/** @interface_method_impl{VDINTERFACEIO,pfnWriteSync}  */
static DECLCALLBACK(int) vdIfFromVfs_WriteSync(void *pvUser, void *pvStorage, uint64_t off, void const *pvBuf,
                                               size_t cbToWrite, size_t *pcbWritten)
{
    PVDIFFROMVFS            pThis = (PVDIFFROMVFS)pvUser;
    AssertPtrReturn(pThis, VERR_INVALID_POINTER);
    AssertReturn(pThis->hVfsIos == (RTVFSIOSTREAM)pvStorage, VERR_INVALID_HANDLE);
    AssertReturn(pThis->fOpened, VERR_INVALID_HANDLE);
    AssertPtrNullReturn(pcbWritten, VERR_INVALID_POINTER);
    AssertReturn(pThis->fAccessMode & RTFILE_O_WRITE, VERR_ACCESS_DENIED);

    return RTVfsIoStrmWriteAt(pThis->hVfsIos, off, pvBuf, cbToWrite, true /*fBlocking*/, pcbWritten);
}


VBOXDDU_DECL(int) VDIfCreateFromVfsStream(RTVFSIOSTREAM hVfsIos, uint32_t fAccessMode, PVDINTERFACEIO *ppIoIf)
{
    /*
     * Validate input.
     */
    AssertPtrReturn(ppIoIf, VERR_INVALID_POINTER);
    *ppIoIf = NULL;
    AssertReturn(hVfsIos != NIL_RTVFSIOSTREAM, VERR_INVALID_HANDLE);
    AssertReturn(fAccessMode & RTFILE_O_ACCESS_MASK, VERR_INVALID_FLAGS);

    uint32_t cRefs = RTVfsIoStrmRetain(hVfsIos);
    AssertReturn(cRefs != UINT32_MAX, VERR_INVALID_HANDLE);

    /*
     * Allocate and init a callback + instance data structure.
     */
    int rc;
    PVDIFFROMVFS pThis = (PVDIFFROMVFS)RTMemAllocZ(sizeof(*pThis));
    if (pThis)
    {
        pThis->CoreIo.pfnOpen                = vdIfFromVfs_Open;
        pThis->CoreIo.pfnClose               = vdIfFromVfs_Close;
        pThis->CoreIo.pfnDelete              = notImpl_Delete;
        pThis->CoreIo.pfnMove                = notImpl_Move;
        pThis->CoreIo.pfnGetFreeSpace        = notImpl_GetFreeSpace;
        pThis->CoreIo.pfnGetModificationTime = notImpl_GetModificationTime;
        pThis->CoreIo.pfnGetSize             = vdIfFromVfs_GetSize;
        pThis->CoreIo.pfnSetSize             = notImpl_SetSize;
        pThis->CoreIo.pfnReadSync            = vdIfFromVfs_ReadSync;
        pThis->CoreIo.pfnWriteSync           = vdIfFromVfs_WriteSync;
        pThis->CoreIo.pfnFlushSync           = notImpl_FlushSync;

        pThis->hVfsIos     = hVfsIos;
        pThis->fAccessMode = fAccessMode;
        pThis->fOpened     = false;
        pThis->u32Magic    = VDIFFROMVFS_MAGIC;

        PVDINTERFACE pFakeList = NULL;
        rc = VDInterfaceAdd(&pThis->CoreIo.Core, "FromVfsStream", VDINTERFACETYPE_IO, pThis, sizeof(pThis->CoreIo), &pFakeList);
        if (RT_SUCCESS(rc))
        {
            *ppIoIf = &pThis->CoreIo;
            return VINF_SUCCESS;
        }

        RTMemFree(pThis);
    }
    else
        rc = VERR_NO_MEMORY;
    RTVfsIoStrmRelease(hVfsIos);
    return rc;
}


VBOXDDU_DECL(int) VDIfDestroyFromVfsStream(PVDINTERFACEIO pIoIf)
{
    if (pIoIf)
    {
        PVDIFFROMVFS            pThis = (PVDIFFROMVFS)pIoIf;
        AssertPtrReturn(pThis, VERR_INVALID_POINTER);
        AssertReturn(pThis->u32Magic == VDIFFROMVFS_MAGIC, VERR_INVALID_MAGIC);

        if (pThis->hVfsIos != NIL_RTVFSIOSTREAM)
        {
            RTVfsIoStrmRelease(pThis->hVfsIos);
            pThis->hVfsIos = NIL_RTVFSIOSTREAM;
        }
        pThis->u32Magic = ~VDIFFROMVFS_MAGIC;
        RTMemFree(pThis);
    }
    return VINF_SUCCESS;
}