1 | /* $Id: VDIfVfs2.cpp 62482 2016-07-22 18:30:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Virtual Disk Image (VDI), I/O interface to IPRT VFS I/O stream glue.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2016 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #include <iprt/types.h>
|
---|
23 | #include <iprt/assert.h>
|
---|
24 | #include <iprt/mem.h>
|
---|
25 | #include <iprt/err.h>
|
---|
26 | #include <iprt/asm.h>
|
---|
27 | #include <iprt/string.h>
|
---|
28 | #include <iprt/file.h>
|
---|
29 | #include <iprt/sg.h>
|
---|
30 | #include <iprt/vfslowlevel.h>
|
---|
31 | #include <iprt/poll.h>
|
---|
32 | #include <VBox/vd.h>
|
---|
33 | #include <VBox/vd-ifs-internal.h>
|
---|
34 |
|
---|
35 | #include <VBox/log.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Structures and Typedefs *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | /**
|
---|
42 | * Extended VD I/O interface structure that vdIfFromVfs_xxx uses.
|
---|
43 | *
|
---|
44 | * It's passed as pvUser to each call.
|
---|
45 | */
|
---|
46 | typedef struct VDIFFROMVFS
|
---|
47 | {
|
---|
48 | VDINTERFACEIO CoreIo;
|
---|
49 |
|
---|
50 | /** Magic. */
|
---|
51 | uint32_t u32Magic;
|
---|
52 | /** The stream access mode (RTFILE_O_ACCESS_MASK), possibly others. */
|
---|
53 | uint32_t fAccessMode;
|
---|
54 | /** The I/O stream. This is NIL after it's been closed. */
|
---|
55 | RTVFSIOSTREAM hVfsIos;
|
---|
56 | /** Completion callback. */
|
---|
57 | PFNVDCOMPLETED pfnCompleted;
|
---|
58 | /** User parameter for the completion callback. */
|
---|
59 | void *pvCompletedUser;
|
---|
60 | /** Set if hVfsIos has been opened. */
|
---|
61 | bool fOpened;
|
---|
62 | } VDIFFROMVFS;
|
---|
63 | /** Magic value for VDIFFROMVFS::u32Magic. */
|
---|
64 | #define VDIFFROMVFS_MAGIC UINT32_C(0x11223344)
|
---|
65 |
|
---|
66 | /** Pointer to the instance data for the vdIfFromVfs_ methods. */
|
---|
67 | typedef struct VDIFFROMVFS *PVDIFFROMVFS;
|
---|
68 |
|
---|
69 |
|
---|
70 | typedef struct FILESTORAGEINTERNAL
|
---|
71 | {
|
---|
72 | /** File handle. */
|
---|
73 | RTFILE file;
|
---|
74 | } FILESTORAGEINTERNAL, *PFILESTORAGEINTERNAL;
|
---|
75 |
|
---|
76 |
|
---|
77 | /*********************************************************************************************************************************
|
---|
78 | * Defined Constants And Macros *
|
---|
79 | *********************************************************************************************************************************/
|
---|
80 |
|
---|
81 | #define STATUS_WAIT UINT32_C(0)
|
---|
82 | #define STATUS_WRITE UINT32_C(1)
|
---|
83 | #define STATUS_WRITING UINT32_C(2)
|
---|
84 | #define STATUS_READ UINT32_C(3)
|
---|
85 | #define STATUS_READING UINT32_C(4)
|
---|
86 | #define STATUS_END UINT32_C(5)
|
---|
87 |
|
---|
88 | /* Enable for getting some flow history. */
|
---|
89 | #if 0
|
---|
90 | # define DEBUG_PRINT_FLOW() RTPrintf("%s\n", __FUNCTION__)
|
---|
91 | #else
|
---|
92 | # define DEBUG_PRINT_FLOW() do {} while (0)
|
---|
93 | #endif
|
---|
94 |
|
---|
95 |
|
---|
96 | /*********************************************************************************************************************************
|
---|
97 | * Internal Functions *
|
---|
98 | *********************************************************************************************************************************/
|
---|
99 |
|
---|
100 |
|
---|
101 | /** @name VDINTERFACEIO stubs returning not-implemented.
|
---|
102 | * @{
|
---|
103 | */
|
---|
104 |
|
---|
105 | /** @interface_method_impl{VDINTERFACEIO,pfnDelete} */
|
---|
106 | static DECLCALLBACK(int) notImpl_Delete(void *pvUser, const char *pcszFilename)
|
---|
107 | {
|
---|
108 | NOREF(pvUser); NOREF(pcszFilename);
|
---|
109 | Log(("%s\n", __FUNCTION__));
|
---|
110 | return VERR_NOT_IMPLEMENTED;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /** @interface_method_impl{VDINTERFACEIO,pfnMove} */
|
---|
114 | static DECLCALLBACK(int) notImpl_Move(void *pvUser, const char *pcszSrc, const char *pcszDst, unsigned fMove)
|
---|
115 | {
|
---|
116 | NOREF(pvUser); NOREF(pcszSrc); NOREF(pcszDst); NOREF(fMove);
|
---|
117 | Log(("%s\n", __FUNCTION__));
|
---|
118 | return VERR_NOT_IMPLEMENTED;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /** @interface_method_impl{VDINTERFACEIO,pfnGetFreeSpace} */
|
---|
122 | static DECLCALLBACK(int) notImpl_GetFreeSpace(void *pvUser, const char *pcszFilename, int64_t *pcbFreeSpace)
|
---|
123 | {
|
---|
124 | NOREF(pvUser); NOREF(pcszFilename); NOREF(pcbFreeSpace);
|
---|
125 | Log(("%s\n", __FUNCTION__));
|
---|
126 | return VERR_NOT_IMPLEMENTED;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /** @interface_method_impl{VDINTERFACEIO,pfnGetModificationTime} */
|
---|
130 | static DECLCALLBACK(int) notImpl_GetModificationTime(void *pvUser, const char *pcszFilename, PRTTIMESPEC pModificationTime)
|
---|
131 | {
|
---|
132 | NOREF(pvUser); NOREF(pcszFilename); NOREF(pModificationTime);
|
---|
133 | Log(("%s\n", __FUNCTION__));
|
---|
134 | return VERR_NOT_IMPLEMENTED;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /** @interface_method_impl{VDINTERFACEIO,pfnSetSize} */
|
---|
138 | static DECLCALLBACK(int) notImpl_SetSize(void *pvUser, void *pvStorage, uint64_t cb)
|
---|
139 | {
|
---|
140 | NOREF(pvUser); NOREF(pvStorage); NOREF(cb);
|
---|
141 | Log(("%s\n", __FUNCTION__));
|
---|
142 | return VERR_NOT_IMPLEMENTED;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /** @interface_method_impl{VDINTERFACEIO,pfnWriteSync} */
|
---|
146 | static DECLCALLBACK(int) notImpl_WriteSync(void *pvUser, void *pvStorage, uint64_t off, const void *pvBuf,
|
---|
147 | size_t cbWrite, size_t *pcbWritten)
|
---|
148 | {
|
---|
149 | NOREF(pvUser); NOREF(pvStorage); NOREF(off); NOREF(pvBuf); NOREF(cbWrite); NOREF(pcbWritten);
|
---|
150 | Log(("%s\n", __FUNCTION__));
|
---|
151 | return VERR_NOT_IMPLEMENTED;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /** @interface_method_impl{VDINTERFACEIO,pfnFlushSync} */
|
---|
155 | static DECLCALLBACK(int) notImpl_FlushSync(void *pvUser, void *pvStorage)
|
---|
156 | {
|
---|
157 | NOREF(pvUser); NOREF(pvStorage);
|
---|
158 | Log(("%s\n", __FUNCTION__));
|
---|
159 | return VERR_NOT_IMPLEMENTED;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /** @} */
|
---|
163 |
|
---|
164 |
|
---|
165 | /** @interface_method_impl{VDINTERFACEIO,pfnOpen} */
|
---|
166 | static DECLCALLBACK(int) vdIfFromVfs_Open(void *pvUser, const char *pszLocation, uint32_t fOpen,
|
---|
167 | PFNVDCOMPLETED pfnCompleted, void **ppInt)
|
---|
168 | {
|
---|
169 | PVDIFFROMVFS pThis = (PVDIFFROMVFS)pvUser;
|
---|
170 |
|
---|
171 | /*
|
---|
172 | * Validate input.
|
---|
173 | */
|
---|
174 | AssertPtrReturn(ppInt, VERR_INVALID_POINTER);
|
---|
175 | AssertPtrNullReturn(pfnCompleted, VERR_INVALID_PARAMETER);
|
---|
176 |
|
---|
177 | /*
|
---|
178 | * We ignore the name, assuming the caller is opening the stream/file we're .
|
---|
179 | * serving. Thus, after close, all open calls fail.
|
---|
180 | */
|
---|
181 | AssertReturn(!pThis->fOpened, VERR_FILE_NOT_FOUND);
|
---|
182 | AssertReturn(pThis->hVfsIos != NIL_RTVFSIOSTREAM, VERR_FILE_NOT_FOUND); /* paranoia */
|
---|
183 | AssertMsgReturn((pThis->fAccessMode & fOpen & RTFILE_O_ACCESS_MASK) == (fOpen & RTFILE_O_ACCESS_MASK),
|
---|
184 | ("fAccessMode=%#x fOpen=%#x\n", pThis->fAccessMode, fOpen), VERR_ACCESS_DENIED);
|
---|
185 |
|
---|
186 | pThis->fAccessMode = fOpen & RTFILE_O_ACCESS_MASK;
|
---|
187 | pThis->fOpened = true;
|
---|
188 | pThis->pfnCompleted = pfnCompleted;
|
---|
189 | pThis->pvCompletedUser = pvUser;
|
---|
190 |
|
---|
191 | *ppInt = pThis->hVfsIos;
|
---|
192 | return VINF_SUCCESS;
|
---|
193 | }
|
---|
194 |
|
---|
195 | /** @interface_method_impl{VDINTERFACEIO,pfnClose} */
|
---|
196 | static DECLCALLBACK(int) vdIfFromVfs_Close(void *pvUser, void *pvStorage)
|
---|
197 | {
|
---|
198 | PVDIFFROMVFS pThis = (PVDIFFROMVFS)pvUser;
|
---|
199 | AssertPtrReturn(pThis, VERR_INVALID_POINTER);
|
---|
200 | AssertReturn(pThis->hVfsIos == (RTVFSIOSTREAM)pvStorage, VERR_INVALID_HANDLE);
|
---|
201 | AssertReturn(pThis->fOpened, VERR_INVALID_HANDLE);
|
---|
202 |
|
---|
203 | uint32_t cRefs = RTVfsIoStrmRelease(pThis->hVfsIos);
|
---|
204 | pThis->hVfsIos = NIL_RTVFSIOSTREAM;
|
---|
205 |
|
---|
206 | return VINF_SUCCESS;
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | /** @interface_method_impl{VDINTERFACEIO,pfnGetSize} */
|
---|
211 | static DECLCALLBACK(int) vdIfFromVfs_GetSize(void *pvUser, void *pvStorage, uint64_t *pcb)
|
---|
212 | {
|
---|
213 | PVDIFFROMVFS pThis = (PVDIFFROMVFS)pvUser;
|
---|
214 | AssertPtrReturn(pThis, VERR_INVALID_POINTER);
|
---|
215 | AssertReturn(pThis->hVfsIos == (RTVFSIOSTREAM)pvStorage, VERR_INVALID_HANDLE);
|
---|
216 | AssertReturn(pThis->fOpened, VERR_INVALID_HANDLE);
|
---|
217 |
|
---|
218 | RTFSOBJINFO ObjInfo;
|
---|
219 | int rc = RTVfsIoStrmQueryInfo(pThis->hVfsIos, &ObjInfo, RTFSOBJATTRADD_NOTHING);
|
---|
220 | if (RT_SUCCESS(rc))
|
---|
221 | *pcb = ObjInfo.cbObject;
|
---|
222 | return rc;
|
---|
223 | }
|
---|
224 |
|
---|
225 | /** @interface_method_impl{VDINTERFACEIO,pfnReadSync} */
|
---|
226 | static DECLCALLBACK(int) vdIfFromVfs_ReadSync(void *pvUser, void *pvStorage, uint64_t off, void *pvBuf,
|
---|
227 | size_t cbToRead, size_t *pcbRead)
|
---|
228 | {
|
---|
229 | PVDIFFROMVFS pThis = (PVDIFFROMVFS)pvUser;
|
---|
230 | AssertPtrReturn(pThis, VERR_INVALID_POINTER);
|
---|
231 | AssertReturn(pThis->hVfsIos == (RTVFSIOSTREAM)pvStorage, VERR_INVALID_HANDLE);
|
---|
232 | AssertReturn(pThis->fOpened, VERR_INVALID_HANDLE);
|
---|
233 | AssertPtrNullReturn(pcbRead, VERR_INVALID_POINTER);
|
---|
234 | AssertReturn(pThis->fAccessMode & RTFILE_O_READ, VERR_ACCESS_DENIED);
|
---|
235 |
|
---|
236 | return RTVfsIoStrmReadAt(pThis->hVfsIos, off, pvBuf, cbToRead, true /*fBlocking*/, pcbRead);
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | /** @interface_method_impl{VDINTERFACEIO,pfnWriteSync} */
|
---|
241 | static DECLCALLBACK(int) vdIfFromVfs_WriteSync(void *pvUser, void *pvStorage, uint64_t off, void const *pvBuf,
|
---|
242 | size_t cbToWrite, size_t *pcbWritten)
|
---|
243 | {
|
---|
244 | PVDIFFROMVFS pThis = (PVDIFFROMVFS)pvUser;
|
---|
245 | AssertPtrReturn(pThis, VERR_INVALID_POINTER);
|
---|
246 | AssertReturn(pThis->hVfsIos == (RTVFSIOSTREAM)pvStorage, VERR_INVALID_HANDLE);
|
---|
247 | AssertReturn(pThis->fOpened, VERR_INVALID_HANDLE);
|
---|
248 | AssertPtrNullReturn(pcbWritten, VERR_INVALID_POINTER);
|
---|
249 | AssertReturn(pThis->fAccessMode & RTFILE_O_WRITE, VERR_ACCESS_DENIED);
|
---|
250 |
|
---|
251 | return RTVfsIoStrmWriteAt(pThis->hVfsIos, off, pvBuf, cbToWrite, true /*fBlocking*/, pcbWritten);
|
---|
252 | }
|
---|
253 |
|
---|
254 |
|
---|
255 | VBOXDDU_DECL(int) VDIfCreateFromVfsStream(RTVFSIOSTREAM hVfsIos, uint32_t fAccessMode, PVDINTERFACEIO *ppIoIf)
|
---|
256 | {
|
---|
257 | /*
|
---|
258 | * Validate input.
|
---|
259 | */
|
---|
260 | AssertPtrReturn(ppIoIf, VERR_INVALID_POINTER);
|
---|
261 | *ppIoIf = NULL;
|
---|
262 | AssertReturn(hVfsIos != NIL_RTVFSIOSTREAM, VERR_INVALID_HANDLE);
|
---|
263 | AssertReturn(fAccessMode & RTFILE_O_ACCESS_MASK, VERR_INVALID_FLAGS);
|
---|
264 |
|
---|
265 | uint32_t cRefs = RTVfsIoStrmRetain(hVfsIos);
|
---|
266 | AssertReturn(cRefs != UINT32_MAX, VERR_INVALID_HANDLE);
|
---|
267 |
|
---|
268 | /*
|
---|
269 | * Allocate and init a callback + instance data structure.
|
---|
270 | */
|
---|
271 | int rc;
|
---|
272 | PVDIFFROMVFS pThis = (PVDIFFROMVFS)RTMemAllocZ(sizeof(*pThis));
|
---|
273 | if (pThis)
|
---|
274 | {
|
---|
275 | pThis->CoreIo.pfnOpen = vdIfFromVfs_Open;
|
---|
276 | pThis->CoreIo.pfnClose = vdIfFromVfs_Close;
|
---|
277 | pThis->CoreIo.pfnDelete = notImpl_Delete;
|
---|
278 | pThis->CoreIo.pfnMove = notImpl_Move;
|
---|
279 | pThis->CoreIo.pfnGetFreeSpace = notImpl_GetFreeSpace;
|
---|
280 | pThis->CoreIo.pfnGetModificationTime = notImpl_GetModificationTime;
|
---|
281 | pThis->CoreIo.pfnGetSize = vdIfFromVfs_GetSize;
|
---|
282 | pThis->CoreIo.pfnSetSize = notImpl_SetSize;
|
---|
283 | pThis->CoreIo.pfnReadSync = vdIfFromVfs_ReadSync;
|
---|
284 | pThis->CoreIo.pfnWriteSync = vdIfFromVfs_WriteSync;
|
---|
285 | pThis->CoreIo.pfnFlushSync = notImpl_FlushSync;
|
---|
286 |
|
---|
287 | pThis->hVfsIos = hVfsIos;
|
---|
288 | pThis->fAccessMode = fAccessMode;
|
---|
289 | pThis->fOpened = false;
|
---|
290 | pThis->u32Magic = VDIFFROMVFS_MAGIC;
|
---|
291 |
|
---|
292 | PVDINTERFACE pFakeList = NULL;
|
---|
293 | rc = VDInterfaceAdd(&pThis->CoreIo.Core, "FromVfsStream", VDINTERFACETYPE_IO, pThis, sizeof(pThis->CoreIo), &pFakeList);
|
---|
294 | if (RT_SUCCESS(rc))
|
---|
295 | {
|
---|
296 | *ppIoIf = &pThis->CoreIo;
|
---|
297 | return VINF_SUCCESS;
|
---|
298 | }
|
---|
299 |
|
---|
300 | RTMemFree(pThis);
|
---|
301 | }
|
---|
302 | else
|
---|
303 | rc = VERR_NO_MEMORY;
|
---|
304 | RTVfsIoStrmRelease(hVfsIos);
|
---|
305 | return rc;
|
---|
306 | }
|
---|
307 |
|
---|
308 |
|
---|
309 | VBOXDDU_DECL(int) VDIfDestroyFromVfsStream(PVDINTERFACEIO pIoIf)
|
---|
310 | {
|
---|
311 | if (pIoIf)
|
---|
312 | {
|
---|
313 | PVDIFFROMVFS pThis = (PVDIFFROMVFS)pIoIf;
|
---|
314 | AssertPtrReturn(pThis, VERR_INVALID_POINTER);
|
---|
315 | AssertReturn(pThis->u32Magic == VDIFFROMVFS_MAGIC, VERR_INVALID_MAGIC);
|
---|
316 |
|
---|
317 | if (pThis->hVfsIos != NIL_RTVFSIOSTREAM)
|
---|
318 | {
|
---|
319 | RTVfsIoStrmRelease(pThis->hVfsIos);
|
---|
320 | pThis->hVfsIos = NIL_RTVFSIOSTREAM;
|
---|
321 | }
|
---|
322 | pThis->u32Magic = ~VDIFFROMVFS_MAGIC;
|
---|
323 | }
|
---|
324 | return VINF_SUCCESS;
|
---|
325 | }
|
---|
326 |
|
---|