1 | /* $Id: VD.cpp 37121 2011-05-17 11:04:30Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxHDD - VBox HDD Container implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*******************************************************************************
|
---|
19 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 | #define LOG_GROUP LOG_GROUP_VD
|
---|
22 | #define RT_STRICT
|
---|
23 | #define LOG_ENABLED
|
---|
24 | #include <VBox/vd.h>
|
---|
25 | #include <VBox/err.h>
|
---|
26 | #include <VBox/sup.h>
|
---|
27 | #include <VBox/log.h>
|
---|
28 |
|
---|
29 | #include <iprt/alloc.h>
|
---|
30 | #include <iprt/assert.h>
|
---|
31 | #include <iprt/uuid.h>
|
---|
32 | #include <iprt/file.h>
|
---|
33 | #include <iprt/string.h>
|
---|
34 | #include <iprt/asm.h>
|
---|
35 | #include <iprt/ldr.h>
|
---|
36 | #include <iprt/dir.h>
|
---|
37 | #include <iprt/path.h>
|
---|
38 | #include <iprt/param.h>
|
---|
39 | #include <iprt/memcache.h>
|
---|
40 | #include <iprt/sg.h>
|
---|
41 | #include <iprt/critsect.h>
|
---|
42 | #include <iprt/list.h>
|
---|
43 | #include <iprt/avl.h>
|
---|
44 |
|
---|
45 | #include <VBox/vd-plugin.h>
|
---|
46 | #include <VBox/vd-cache-plugin.h>
|
---|
47 |
|
---|
48 | #define VBOXHDDDISK_SIGNATURE 0x6f0e2a7d
|
---|
49 |
|
---|
50 | /** Buffer size used for merging images. */
|
---|
51 | #define VD_MERGE_BUFFER_SIZE (16 * _1M)
|
---|
52 |
|
---|
53 | /** Maximum number of segments in one I/O task. */
|
---|
54 | #define VD_IO_TASK_SEGMENTS_MAX 64
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * VD async I/O interface storage descriptor.
|
---|
58 | */
|
---|
59 | typedef struct VDIIOFALLBACKSTORAGE
|
---|
60 | {
|
---|
61 | /** File handle. */
|
---|
62 | RTFILE File;
|
---|
63 | /** Completion callback. */
|
---|
64 | PFNVDCOMPLETED pfnCompleted;
|
---|
65 | /** Thread for async access. */
|
---|
66 | RTTHREAD ThreadAsync;
|
---|
67 | } VDIIOFALLBACKSTORAGE, *PVDIIOFALLBACKSTORAGE;
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Structure containing everything I/O related
|
---|
71 | * for the image and cache descriptors.
|
---|
72 | */
|
---|
73 | typedef struct VDIO
|
---|
74 | {
|
---|
75 | /** I/O interface to the upper layer. */
|
---|
76 | PVDINTERFACE pInterfaceIO;
|
---|
77 | /** I/O interface callback table. */
|
---|
78 | PVDINTERFACEIO pInterfaceIOCallbacks;
|
---|
79 |
|
---|
80 | /** Per image internal I/O interface. */
|
---|
81 | VDINTERFACE VDIIOInt;
|
---|
82 |
|
---|
83 | /** Fallback I/O interface, only used if the caller doesn't provide it. */
|
---|
84 | VDINTERFACE VDIIO;
|
---|
85 |
|
---|
86 | /** Opaque backend data. */
|
---|
87 | void *pBackendData;
|
---|
88 | /** Disk this image is part of */
|
---|
89 | PVBOXHDD pDisk;
|
---|
90 | } VDIO, *PVDIO;
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * VBox HDD Container image descriptor.
|
---|
94 | */
|
---|
95 | typedef struct VDIMAGE
|
---|
96 | {
|
---|
97 | /** Link to parent image descriptor, if any. */
|
---|
98 | struct VDIMAGE *pPrev;
|
---|
99 | /** Link to child image descriptor, if any. */
|
---|
100 | struct VDIMAGE *pNext;
|
---|
101 | /** Container base filename. (UTF-8) */
|
---|
102 | char *pszFilename;
|
---|
103 | /** Data managed by the backend which keeps the actual info. */
|
---|
104 | void *pBackendData;
|
---|
105 | /** Cached sanitized image flags. */
|
---|
106 | unsigned uImageFlags;
|
---|
107 | /** Image open flags (only those handled generically in this code and which
|
---|
108 | * the backends will never ever see). */
|
---|
109 | unsigned uOpenFlags;
|
---|
110 |
|
---|
111 | /** Function pointers for the various backend methods. */
|
---|
112 | PCVBOXHDDBACKEND Backend;
|
---|
113 | /** Pointer to list of VD interfaces, per-image. */
|
---|
114 | PVDINTERFACE pVDIfsImage;
|
---|
115 | /** I/O related things. */
|
---|
116 | VDIO VDIo;
|
---|
117 | } VDIMAGE, *PVDIMAGE;
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * uModified bit flags.
|
---|
121 | */
|
---|
122 | #define VD_IMAGE_MODIFIED_FLAG RT_BIT(0)
|
---|
123 | #define VD_IMAGE_MODIFIED_FIRST RT_BIT(1)
|
---|
124 | #define VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE RT_BIT(2)
|
---|
125 |
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * VBox HDD Cache image descriptor.
|
---|
129 | */
|
---|
130 | typedef struct VDCACHE
|
---|
131 | {
|
---|
132 | /** Cache base filename. (UTF-8) */
|
---|
133 | char *pszFilename;
|
---|
134 | /** Data managed by the backend which keeps the actual info. */
|
---|
135 | void *pBackendData;
|
---|
136 | /** Cached sanitized image flags. */
|
---|
137 | unsigned uImageFlags;
|
---|
138 | /** Image open flags (only those handled generically in this code and which
|
---|
139 | * the backends will never ever see). */
|
---|
140 | unsigned uOpenFlags;
|
---|
141 |
|
---|
142 | /** Function pointers for the various backend methods. */
|
---|
143 | PCVDCACHEBACKEND Backend;
|
---|
144 |
|
---|
145 | /** Pointer to list of VD interfaces, per-cache. */
|
---|
146 | PVDINTERFACE pVDIfsCache;
|
---|
147 | /** I/O related things. */
|
---|
148 | VDIO VDIo;
|
---|
149 | } VDCACHE, *PVDCACHE;
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * VBox HDD Container main structure, private part.
|
---|
153 | */
|
---|
154 | struct VBOXHDD
|
---|
155 | {
|
---|
156 | /** Structure signature (VBOXHDDDISK_SIGNATURE). */
|
---|
157 | uint32_t u32Signature;
|
---|
158 |
|
---|
159 | /** Image type. */
|
---|
160 | VDTYPE enmType;
|
---|
161 |
|
---|
162 | /** Number of opened images. */
|
---|
163 | unsigned cImages;
|
---|
164 |
|
---|
165 | /** Base image. */
|
---|
166 | PVDIMAGE pBase;
|
---|
167 |
|
---|
168 | /** Last opened image in the chain.
|
---|
169 | * The same as pBase if only one image is used. */
|
---|
170 | PVDIMAGE pLast;
|
---|
171 |
|
---|
172 | /** If a merge to one of the parents is running this may be non-NULL
|
---|
173 | * to indicate to what image the writes should be additionally relayed. */
|
---|
174 | PVDIMAGE pImageRelay;
|
---|
175 |
|
---|
176 | /** Flags representing the modification state. */
|
---|
177 | unsigned uModified;
|
---|
178 |
|
---|
179 | /** Cached size of this disk. */
|
---|
180 | uint64_t cbSize;
|
---|
181 | /** Cached PCHS geometry for this disk. */
|
---|
182 | VDGEOMETRY PCHSGeometry;
|
---|
183 | /** Cached LCHS geometry for this disk. */
|
---|
184 | VDGEOMETRY LCHSGeometry;
|
---|
185 |
|
---|
186 | /** Pointer to list of VD interfaces, per-disk. */
|
---|
187 | PVDINTERFACE pVDIfsDisk;
|
---|
188 | /** Pointer to the common interface structure for error reporting. */
|
---|
189 | PVDINTERFACE pInterfaceError;
|
---|
190 | /** Pointer to the error interface callbacks we use if available. */
|
---|
191 | PVDINTERFACEERROR pInterfaceErrorCallbacks;
|
---|
192 |
|
---|
193 | /** Pointer to the optional thread synchronization interface. */
|
---|
194 | PVDINTERFACE pInterfaceThreadSync;
|
---|
195 | /** Pointer to the optional thread synchronization callbacks. */
|
---|
196 | PVDINTERFACETHREADSYNC pInterfaceThreadSyncCallbacks;
|
---|
197 |
|
---|
198 | /** Internal I/O interface callback table for the images. */
|
---|
199 | VDINTERFACEIOINT VDIIOIntCallbacks;
|
---|
200 |
|
---|
201 | /** Callback table for the fallback I/O interface. */
|
---|
202 | VDINTERFACEIO VDIIOCallbacks;
|
---|
203 |
|
---|
204 | /** Memory cache for I/O contexts */
|
---|
205 | RTMEMCACHE hMemCacheIoCtx;
|
---|
206 | /** Memory cache for I/O tasks. */
|
---|
207 | RTMEMCACHE hMemCacheIoTask;
|
---|
208 | /** Critical section protecting the disk against concurrent access. */
|
---|
209 | RTCRITSECT CritSect;
|
---|
210 | /** Flag whether the disk is currently locked by growing write or a flush
|
---|
211 | * request. Other flush or growing write requests need to wait until
|
---|
212 | * the current one completes.
|
---|
213 | */
|
---|
214 | volatile bool fLocked;
|
---|
215 | /** List of waiting requests. - Protected by the critical section. */
|
---|
216 | RTLISTNODE ListWriteLocked;
|
---|
217 | /** I/O context which locked the disk. */
|
---|
218 | PVDIOCTX pIoCtxLockOwner;
|
---|
219 |
|
---|
220 | /** Pointer to the L2 disk cache if any. */
|
---|
221 | PVDCACHE pCache;
|
---|
222 | };
|
---|
223 |
|
---|
224 | # define VD_THREAD_IS_CRITSECT_OWNER(Disk) \
|
---|
225 | do \
|
---|
226 | { \
|
---|
227 | AssertMsg(RTCritSectIsOwner(&Disk->CritSect), \
|
---|
228 | ("Thread does not own critical section\n"));\
|
---|
229 | } while(0)
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * VBox parent read descriptor, used internally for compaction.
|
---|
233 | */
|
---|
234 | typedef struct VDPARENTSTATEDESC
|
---|
235 | {
|
---|
236 | /** Pointer to disk descriptor. */
|
---|
237 | PVBOXHDD pDisk;
|
---|
238 | /** Pointer to image descriptor. */
|
---|
239 | PVDIMAGE pImage;
|
---|
240 | } VDPARENTSTATEDESC, *PVDPARENTSTATEDESC;
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * Transfer direction.
|
---|
244 | */
|
---|
245 | typedef enum VDIOCTXTXDIR
|
---|
246 | {
|
---|
247 | /** Read */
|
---|
248 | VDIOCTXTXDIR_READ = 0,
|
---|
249 | /** Write */
|
---|
250 | VDIOCTXTXDIR_WRITE,
|
---|
251 | /** Flush */
|
---|
252 | VDIOCTXTXDIR_FLUSH,
|
---|
253 | /** 32bit hack */
|
---|
254 | VDIOCTXTXDIR_32BIT_HACK = 0x7fffffff
|
---|
255 | } VDIOCTXTXDIR, *PVDIOCTXTXDIR;
|
---|
256 |
|
---|
257 | /** Transfer function */
|
---|
258 | typedef DECLCALLBACK(int) FNVDIOCTXTRANSFER (PVDIOCTX pIoCtx);
|
---|
259 | /** Pointer to a transfer function. */
|
---|
260 | typedef FNVDIOCTXTRANSFER *PFNVDIOCTXTRANSFER;
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * I/O context
|
---|
264 | */
|
---|
265 | typedef struct VDIOCTX
|
---|
266 | {
|
---|
267 | /** Disk this is request is for. */
|
---|
268 | PVBOXHDD pDisk;
|
---|
269 | /** Return code. */
|
---|
270 | int rcReq;
|
---|
271 | /** Transfer direction */
|
---|
272 | VDIOCTXTXDIR enmTxDir;
|
---|
273 | /** Number of bytes left until this context completes. */
|
---|
274 | volatile uint32_t cbTransferLeft;
|
---|
275 | /** Current offset */
|
---|
276 | volatile uint64_t uOffset;
|
---|
277 | /** Number of bytes to transfer */
|
---|
278 | volatile size_t cbTransfer;
|
---|
279 | /** Current image in the chain. */
|
---|
280 | PVDIMAGE pImageCur;
|
---|
281 | /** Start image to read from. pImageCur is reset to this
|
---|
282 | * value after it reached the first image in the chain. */
|
---|
283 | PVDIMAGE pImageStart;
|
---|
284 | /** S/G buffer */
|
---|
285 | RTSGBUF SgBuf;
|
---|
286 | /** Flag whether the I/O context is blocked because it is in the growing list. */
|
---|
287 | bool fBlocked;
|
---|
288 | /** Number of data transfers currently pending. */
|
---|
289 | volatile uint32_t cDataTransfersPending;
|
---|
290 | /** How many meta data transfers are pending. */
|
---|
291 | volatile uint32_t cMetaTransfersPending;
|
---|
292 | /** Flag whether the request finished */
|
---|
293 | volatile bool fComplete;
|
---|
294 | /** Temporary allocated memory which is freed
|
---|
295 | * when the context completes. */
|
---|
296 | void *pvAllocation;
|
---|
297 | /** Transfer function. */
|
---|
298 | PFNVDIOCTXTRANSFER pfnIoCtxTransfer;
|
---|
299 | /** Next transfer part after the current one completed. */
|
---|
300 | PFNVDIOCTXTRANSFER pfnIoCtxTransferNext;
|
---|
301 | /** Parent I/O context if any. Sets the type of the context (root/child) */
|
---|
302 | PVDIOCTX pIoCtxParent;
|
---|
303 | /** Type dependent data (root/child) */
|
---|
304 | union
|
---|
305 | {
|
---|
306 | /** Root data */
|
---|
307 | struct
|
---|
308 | {
|
---|
309 | /** Completion callback */
|
---|
310 | PFNVDASYNCTRANSFERCOMPLETE pfnComplete;
|
---|
311 | /** User argument 1 passed on completion. */
|
---|
312 | void *pvUser1;
|
---|
313 | /** User argument 1 passed on completion. */
|
---|
314 | void *pvUser2;
|
---|
315 | } Root;
|
---|
316 | /** Child data */
|
---|
317 | struct
|
---|
318 | {
|
---|
319 | /** Saved start offset */
|
---|
320 | uint64_t uOffsetSaved;
|
---|
321 | /** Saved transfer size */
|
---|
322 | size_t cbTransferLeftSaved;
|
---|
323 | /** Number of bytes transferred from the parent if this context completes. */
|
---|
324 | size_t cbTransferParent;
|
---|
325 | /** Number of bytes to pre read */
|
---|
326 | size_t cbPreRead;
|
---|
327 | /** Number of bytes to post read. */
|
---|
328 | size_t cbPostRead;
|
---|
329 | /** Number of bytes to write left in the parent. */
|
---|
330 | size_t cbWriteParent;
|
---|
331 | /** Write type dependent data. */
|
---|
332 | union
|
---|
333 | {
|
---|
334 | /** Optimized */
|
---|
335 | struct
|
---|
336 | {
|
---|
337 | /** Bytes to fill to satisfy the block size. Not part of the virtual disk. */
|
---|
338 | size_t cbFill;
|
---|
339 | /** Bytes to copy instead of reading from the parent */
|
---|
340 | size_t cbWriteCopy;
|
---|
341 | /** Bytes to read from the image. */
|
---|
342 | size_t cbReadImage;
|
---|
343 | } Optimized;
|
---|
344 | } Write;
|
---|
345 | } Child;
|
---|
346 | } Type;
|
---|
347 | } VDIOCTX;
|
---|
348 |
|
---|
349 | typedef struct VDIOCTXDEFERRED
|
---|
350 | {
|
---|
351 | /** Node in the list of deferred requests.
|
---|
352 | * A request can be deferred if the image is growing
|
---|
353 | * and the request accesses the same range or if
|
---|
354 | * the backend needs to read or write metadata from the disk
|
---|
355 | * before it can continue. */
|
---|
356 | RTLISTNODE NodeDeferred;
|
---|
357 | /** I/O context this entry points to. */
|
---|
358 | PVDIOCTX pIoCtx;
|
---|
359 | } VDIOCTXDEFERRED, *PVDIOCTXDEFERRED;
|
---|
360 |
|
---|
361 | /**
|
---|
362 | * I/O task.
|
---|
363 | */
|
---|
364 | typedef struct VDIOTASK
|
---|
365 | {
|
---|
366 | /** Storage this task belongs to. */
|
---|
367 | PVDIOSTORAGE pIoStorage;
|
---|
368 | /** Optional completion callback. */
|
---|
369 | PFNVDXFERCOMPLETED pfnComplete;
|
---|
370 | /** Opaque user data. */
|
---|
371 | void *pvUser;
|
---|
372 | /** Flag whether this is a meta data transfer. */
|
---|
373 | bool fMeta;
|
---|
374 | /** Type dependent data. */
|
---|
375 | union
|
---|
376 | {
|
---|
377 | /** User data transfer. */
|
---|
378 | struct
|
---|
379 | {
|
---|
380 | /** Number of bytes this task transferred. */
|
---|
381 | uint32_t cbTransfer;
|
---|
382 | /** Pointer to the I/O context the task belongs. */
|
---|
383 | PVDIOCTX pIoCtx;
|
---|
384 | } User;
|
---|
385 | /** Meta data transfer. */
|
---|
386 | struct
|
---|
387 | {
|
---|
388 | /** Meta transfer this task is for. */
|
---|
389 | PVDMETAXFER pMetaXfer;
|
---|
390 | } Meta;
|
---|
391 | } Type;
|
---|
392 | } VDIOTASK, *PVDIOTASK;
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Storage handle.
|
---|
396 | */
|
---|
397 | typedef struct VDIOSTORAGE
|
---|
398 | {
|
---|
399 | /** Image I/O state this storage handle belongs to. */
|
---|
400 | PVDIO pVDIo;
|
---|
401 | /** AVL tree for pending async metadata transfers. */
|
---|
402 | PAVLRFOFFTREE pTreeMetaXfers;
|
---|
403 | /** Storage handle */
|
---|
404 | void *pStorage;
|
---|
405 | } VDIOSTORAGE;
|
---|
406 |
|
---|
407 | /**
|
---|
408 | * Metadata transfer.
|
---|
409 | *
|
---|
410 | * @note This entry can't be freed if either the list is not empty or
|
---|
411 | * the reference counter is not 0.
|
---|
412 | * The assumption is that the backends don't need to read huge amounts of
|
---|
413 | * metadata to complete a transfer so the additional memory overhead should
|
---|
414 | * be relatively small.
|
---|
415 | */
|
---|
416 | typedef struct VDMETAXFER
|
---|
417 | {
|
---|
418 | /** AVL core for fast search (the file offset is the key) */
|
---|
419 | AVLRFOFFNODECORE Core;
|
---|
420 | /** I/O storage for this transfer. */
|
---|
421 | PVDIOSTORAGE pIoStorage;
|
---|
422 | /** Flags. */
|
---|
423 | uint32_t fFlags;
|
---|
424 | /** List of I/O contexts waiting for this metadata transfer to complete. */
|
---|
425 | RTLISTNODE ListIoCtxWaiting;
|
---|
426 | /** Number of references to this entry. */
|
---|
427 | unsigned cRefs;
|
---|
428 | /** Size of the data stored with this entry. */
|
---|
429 | size_t cbMeta;
|
---|
430 | /** Data stored - variable size. */
|
---|
431 | uint8_t abData[1];
|
---|
432 | } VDMETAXFER;
|
---|
433 |
|
---|
434 | /**
|
---|
435 | * The transfer direction for the metadata.
|
---|
436 | */
|
---|
437 | #define VDMETAXFER_TXDIR_MASK 0x3
|
---|
438 | #define VDMETAXFER_TXDIR_NONE 0x0
|
---|
439 | #define VDMETAXFER_TXDIR_WRITE 0x1
|
---|
440 | #define VDMETAXFER_TXDIR_READ 0x2
|
---|
441 | #define VDMETAXFER_TXDIR_FLUSH 0x3
|
---|
442 | #define VDMETAXFER_TXDIR_GET(flags) ((flags) & VDMETAXFER_TXDIR_MASK)
|
---|
443 | #define VDMETAXFER_TXDIR_SET(flags, dir) ((flags) = (flags & ~VDMETAXFER_TXDIR_MASK) | (dir))
|
---|
444 |
|
---|
445 | extern VBOXHDDBACKEND g_RawBackend;
|
---|
446 | extern VBOXHDDBACKEND g_VmdkBackend;
|
---|
447 | extern VBOXHDDBACKEND g_VDIBackend;
|
---|
448 | extern VBOXHDDBACKEND g_VhdBackend;
|
---|
449 | extern VBOXHDDBACKEND g_ParallelsBackend;
|
---|
450 | extern VBOXHDDBACKEND g_DmgBackend;
|
---|
451 | extern VBOXHDDBACKEND g_ISCSIBackend;
|
---|
452 |
|
---|
453 | static unsigned g_cBackends = 0;
|
---|
454 | static PVBOXHDDBACKEND *g_apBackends = NULL;
|
---|
455 | static PVBOXHDDBACKEND aStaticBackends[] =
|
---|
456 | {
|
---|
457 | &g_VmdkBackend,
|
---|
458 | &g_VDIBackend,
|
---|
459 | &g_VhdBackend,
|
---|
460 | &g_ParallelsBackend,
|
---|
461 | &g_DmgBackend,
|
---|
462 | &g_RawBackend,
|
---|
463 | &g_ISCSIBackend
|
---|
464 | };
|
---|
465 |
|
---|
466 | /**
|
---|
467 | * Supported backends for the disk cache.
|
---|
468 | */
|
---|
469 | extern VDCACHEBACKEND g_VciCacheBackend;
|
---|
470 |
|
---|
471 | static unsigned g_cCacheBackends = 0;
|
---|
472 | static PVDCACHEBACKEND *g_apCacheBackends = NULL;
|
---|
473 | static PVDCACHEBACKEND aStaticCacheBackends[] =
|
---|
474 | {
|
---|
475 | &g_VciCacheBackend
|
---|
476 | };
|
---|
477 |
|
---|
478 | /**
|
---|
479 | * internal: add several backends.
|
---|
480 | */
|
---|
481 | static int vdAddBackends(PVBOXHDDBACKEND *ppBackends, unsigned cBackends)
|
---|
482 | {
|
---|
483 | PVBOXHDDBACKEND *pTmp = (PVBOXHDDBACKEND*)RTMemRealloc(g_apBackends,
|
---|
484 | (g_cBackends + cBackends) * sizeof(PVBOXHDDBACKEND));
|
---|
485 | if (RT_UNLIKELY(!pTmp))
|
---|
486 | return VERR_NO_MEMORY;
|
---|
487 | g_apBackends = pTmp;
|
---|
488 | memcpy(&g_apBackends[g_cBackends], ppBackends, cBackends * sizeof(PVBOXHDDBACKEND));
|
---|
489 | g_cBackends += cBackends;
|
---|
490 | return VINF_SUCCESS;
|
---|
491 | }
|
---|
492 |
|
---|
493 | /**
|
---|
494 | * internal: add single backend.
|
---|
495 | */
|
---|
496 | DECLINLINE(int) vdAddBackend(PVBOXHDDBACKEND pBackend)
|
---|
497 | {
|
---|
498 | return vdAddBackends(&pBackend, 1);
|
---|
499 | }
|
---|
500 |
|
---|
501 | /**
|
---|
502 | * internal: add several cache backends.
|
---|
503 | */
|
---|
504 | static int vdAddCacheBackends(PVDCACHEBACKEND *ppBackends, unsigned cBackends)
|
---|
505 | {
|
---|
506 | PVDCACHEBACKEND *pTmp = (PVDCACHEBACKEND*)RTMemRealloc(g_apCacheBackends,
|
---|
507 | (g_cCacheBackends + cBackends) * sizeof(PVDCACHEBACKEND));
|
---|
508 | if (RT_UNLIKELY(!pTmp))
|
---|
509 | return VERR_NO_MEMORY;
|
---|
510 | g_apCacheBackends = pTmp;
|
---|
511 | memcpy(&g_apCacheBackends[g_cCacheBackends], ppBackends, cBackends * sizeof(PVDCACHEBACKEND));
|
---|
512 | g_cCacheBackends += cBackends;
|
---|
513 | return VINF_SUCCESS;
|
---|
514 | }
|
---|
515 |
|
---|
516 | /**
|
---|
517 | * internal: add single cache backend.
|
---|
518 | */
|
---|
519 | DECLINLINE(int) vdAddCacheBackend(PVDCACHEBACKEND pBackend)
|
---|
520 | {
|
---|
521 | return vdAddCacheBackends(&pBackend, 1);
|
---|
522 | }
|
---|
523 |
|
---|
524 | /**
|
---|
525 | * internal: issue error message.
|
---|
526 | */
|
---|
527 | static int vdError(PVBOXHDD pDisk, int rc, RT_SRC_POS_DECL,
|
---|
528 | const char *pszFormat, ...)
|
---|
529 | {
|
---|
530 | va_list va;
|
---|
531 | va_start(va, pszFormat);
|
---|
532 | if (pDisk->pInterfaceErrorCallbacks)
|
---|
533 | pDisk->pInterfaceErrorCallbacks->pfnError(pDisk->pInterfaceError->pvUser, rc, RT_SRC_POS_ARGS, pszFormat, va);
|
---|
534 | va_end(va);
|
---|
535 | return rc;
|
---|
536 | }
|
---|
537 |
|
---|
538 | /**
|
---|
539 | * internal: thread synchronization, start read.
|
---|
540 | */
|
---|
541 | DECLINLINE(int) vdThreadStartRead(PVBOXHDD pDisk)
|
---|
542 | {
|
---|
543 | int rc = VINF_SUCCESS;
|
---|
544 | if (RT_UNLIKELY(pDisk->pInterfaceThreadSyncCallbacks))
|
---|
545 | rc = pDisk->pInterfaceThreadSyncCallbacks->pfnStartRead(pDisk->pInterfaceThreadSync->pvUser);
|
---|
546 | return rc;
|
---|
547 | }
|
---|
548 |
|
---|
549 | /**
|
---|
550 | * internal: thread synchronization, finish read.
|
---|
551 | */
|
---|
552 | DECLINLINE(int) vdThreadFinishRead(PVBOXHDD pDisk)
|
---|
553 | {
|
---|
554 | int rc = VINF_SUCCESS;
|
---|
555 | if (RT_UNLIKELY(pDisk->pInterfaceThreadSyncCallbacks))
|
---|
556 | rc = pDisk->pInterfaceThreadSyncCallbacks->pfnFinishRead(pDisk->pInterfaceThreadSync->pvUser);
|
---|
557 | return rc;
|
---|
558 | }
|
---|
559 |
|
---|
560 | /**
|
---|
561 | * internal: thread synchronization, start write.
|
---|
562 | */
|
---|
563 | DECLINLINE(int) vdThreadStartWrite(PVBOXHDD pDisk)
|
---|
564 | {
|
---|
565 | int rc = VINF_SUCCESS;
|
---|
566 | if (RT_UNLIKELY(pDisk->pInterfaceThreadSyncCallbacks))
|
---|
567 | rc = pDisk->pInterfaceThreadSyncCallbacks->pfnStartWrite(pDisk->pInterfaceThreadSync->pvUser);
|
---|
568 | return rc;
|
---|
569 | }
|
---|
570 |
|
---|
571 | /**
|
---|
572 | * internal: thread synchronization, finish write.
|
---|
573 | */
|
---|
574 | DECLINLINE(int) vdThreadFinishWrite(PVBOXHDD pDisk)
|
---|
575 | {
|
---|
576 | int rc = VINF_SUCCESS;
|
---|
577 | if (RT_UNLIKELY(pDisk->pInterfaceThreadSyncCallbacks))
|
---|
578 | rc = pDisk->pInterfaceThreadSyncCallbacks->pfnFinishWrite(pDisk->pInterfaceThreadSync->pvUser);
|
---|
579 | return rc;
|
---|
580 | }
|
---|
581 |
|
---|
582 | /**
|
---|
583 | * internal: find image format backend.
|
---|
584 | */
|
---|
585 | static int vdFindBackend(const char *pszBackend, PCVBOXHDDBACKEND *ppBackend)
|
---|
586 | {
|
---|
587 | int rc = VINF_SUCCESS;
|
---|
588 | PCVBOXHDDBACKEND pBackend = NULL;
|
---|
589 |
|
---|
590 | if (!g_apBackends)
|
---|
591 | VDInit();
|
---|
592 |
|
---|
593 | for (unsigned i = 0; i < g_cBackends; i++)
|
---|
594 | {
|
---|
595 | if (!RTStrICmp(pszBackend, g_apBackends[i]->pszBackendName))
|
---|
596 | {
|
---|
597 | pBackend = g_apBackends[i];
|
---|
598 | break;
|
---|
599 | }
|
---|
600 | }
|
---|
601 | *ppBackend = pBackend;
|
---|
602 | return rc;
|
---|
603 | }
|
---|
604 |
|
---|
605 | /**
|
---|
606 | * internal: find cache format backend.
|
---|
607 | */
|
---|
608 | static int vdFindCacheBackend(const char *pszBackend, PCVDCACHEBACKEND *ppBackend)
|
---|
609 | {
|
---|
610 | int rc = VINF_SUCCESS;
|
---|
611 | PCVDCACHEBACKEND pBackend = NULL;
|
---|
612 |
|
---|
613 | if (!g_apCacheBackends)
|
---|
614 | VDInit();
|
---|
615 |
|
---|
616 | for (unsigned i = 0; i < g_cCacheBackends; i++)
|
---|
617 | {
|
---|
618 | if (!RTStrICmp(pszBackend, g_apCacheBackends[i]->pszBackendName))
|
---|
619 | {
|
---|
620 | pBackend = g_apCacheBackends[i];
|
---|
621 | break;
|
---|
622 | }
|
---|
623 | }
|
---|
624 | *ppBackend = pBackend;
|
---|
625 | return rc;
|
---|
626 | }
|
---|
627 |
|
---|
628 | /**
|
---|
629 | * internal: add image structure to the end of images list.
|
---|
630 | */
|
---|
631 | static void vdAddImageToList(PVBOXHDD pDisk, PVDIMAGE pImage)
|
---|
632 | {
|
---|
633 | pImage->pPrev = NULL;
|
---|
634 | pImage->pNext = NULL;
|
---|
635 |
|
---|
636 | if (pDisk->pBase)
|
---|
637 | {
|
---|
638 | Assert(pDisk->cImages > 0);
|
---|
639 | pImage->pPrev = pDisk->pLast;
|
---|
640 | pDisk->pLast->pNext = pImage;
|
---|
641 | pDisk->pLast = pImage;
|
---|
642 | }
|
---|
643 | else
|
---|
644 | {
|
---|
645 | Assert(pDisk->cImages == 0);
|
---|
646 | pDisk->pBase = pImage;
|
---|
647 | pDisk->pLast = pImage;
|
---|
648 | }
|
---|
649 |
|
---|
650 | pDisk->cImages++;
|
---|
651 | }
|
---|
652 |
|
---|
653 | /**
|
---|
654 | * internal: remove image structure from the images list.
|
---|
655 | */
|
---|
656 | static void vdRemoveImageFromList(PVBOXHDD pDisk, PVDIMAGE pImage)
|
---|
657 | {
|
---|
658 | Assert(pDisk->cImages > 0);
|
---|
659 |
|
---|
660 | if (pImage->pPrev)
|
---|
661 | pImage->pPrev->pNext = pImage->pNext;
|
---|
662 | else
|
---|
663 | pDisk->pBase = pImage->pNext;
|
---|
664 |
|
---|
665 | if (pImage->pNext)
|
---|
666 | pImage->pNext->pPrev = pImage->pPrev;
|
---|
667 | else
|
---|
668 | pDisk->pLast = pImage->pPrev;
|
---|
669 |
|
---|
670 | pImage->pPrev = NULL;
|
---|
671 | pImage->pNext = NULL;
|
---|
672 |
|
---|
673 | pDisk->cImages--;
|
---|
674 | }
|
---|
675 |
|
---|
676 | /**
|
---|
677 | * internal: find image by index into the images list.
|
---|
678 | */
|
---|
679 | static PVDIMAGE vdGetImageByNumber(PVBOXHDD pDisk, unsigned nImage)
|
---|
680 | {
|
---|
681 | PVDIMAGE pImage = pDisk->pBase;
|
---|
682 | if (nImage == VD_LAST_IMAGE)
|
---|
683 | return pDisk->pLast;
|
---|
684 | while (pImage && nImage)
|
---|
685 | {
|
---|
686 | pImage = pImage->pNext;
|
---|
687 | nImage--;
|
---|
688 | }
|
---|
689 | return pImage;
|
---|
690 | }
|
---|
691 |
|
---|
692 | /**
|
---|
693 | * Internal: Tries to read the desired range from the given cache.
|
---|
694 | *
|
---|
695 | * @returns VBox status code.
|
---|
696 | * @retval VERR_VD_BLOCK_FREE if the block is not in the cache.
|
---|
697 | * pcbRead will be set to the number of bytes not in the cache.
|
---|
698 | * Everything thereafter might be in the cache.
|
---|
699 | * @param pCache The cache to read from.
|
---|
700 | * @param uOffset Offset of the virtual disk to read.
|
---|
701 | * @param pvBuf Where to store the read data.
|
---|
702 | * @param cbRead How much to read.
|
---|
703 | * @param pcbRead Where to store the number of bytes actually read.
|
---|
704 | * On success this indicates the number of bytes read from the cache.
|
---|
705 | * If VERR_VD_BLOCK_FREE is returned this gives the number of bytes
|
---|
706 | * which are not in the cache.
|
---|
707 | * In both cases everything beyond this value
|
---|
708 | * might or might not be in the cache.
|
---|
709 | */
|
---|
710 | static int vdCacheReadHelper(PVDCACHE pCache, uint64_t uOffset,
|
---|
711 | void *pvBuf, size_t cbRead, size_t *pcbRead)
|
---|
712 | {
|
---|
713 | int rc = VINF_SUCCESS;
|
---|
714 |
|
---|
715 | LogFlowFunc(("pCache=%#p uOffset=%llu pvBuf=%#p cbRead=%zu pcbRead=%#p\n",
|
---|
716 | pCache, uOffset, pvBuf, cbRead, pcbRead));
|
---|
717 |
|
---|
718 | AssertPtr(pCache);
|
---|
719 | AssertPtr(pcbRead);
|
---|
720 |
|
---|
721 | rc = pCache->Backend->pfnRead(pCache->pBackendData, uOffset, pvBuf,
|
---|
722 | cbRead, pcbRead);
|
---|
723 |
|
---|
724 | LogFlowFunc(("returns rc=%Rrc pcbRead=%zu\n", rc, *pcbRead));
|
---|
725 | return rc;
|
---|
726 | }
|
---|
727 |
|
---|
728 | /**
|
---|
729 | * Internal: Writes data for the given block into the cache.
|
---|
730 | *
|
---|
731 | * @returns VBox status code.
|
---|
732 | * @param pCache The cache to write to.
|
---|
733 | * @param uOffset Offset of the virtual disk to write to teh cache.
|
---|
734 | * @param pcvBuf The data to write.
|
---|
735 | * @param cbWrite How much to write.
|
---|
736 | * @param pcbWritten How much data could be written, optional.
|
---|
737 | */
|
---|
738 | static int vdCacheWriteHelper(PVDCACHE pCache, uint64_t uOffset, const void *pcvBuf,
|
---|
739 | size_t cbWrite, size_t *pcbWritten)
|
---|
740 | {
|
---|
741 | int rc = VINF_SUCCESS;
|
---|
742 |
|
---|
743 | LogFlowFunc(("pCache=%#p uOffset=%llu pvBuf=%#p cbWrite=%zu pcbWritten=%#p\n",
|
---|
744 | pCache, uOffset, pcvBuf, cbWrite, pcbWritten));
|
---|
745 |
|
---|
746 | AssertPtr(pCache);
|
---|
747 | AssertPtr(pcvBuf);
|
---|
748 | Assert(cbWrite > 0);
|
---|
749 |
|
---|
750 | if (pcbWritten)
|
---|
751 | rc = pCache->Backend->pfnWrite(pCache->pBackendData, uOffset, pcvBuf,
|
---|
752 | cbWrite, pcbWritten);
|
---|
753 | else
|
---|
754 | {
|
---|
755 | size_t cbWritten = 0;
|
---|
756 |
|
---|
757 | do
|
---|
758 | {
|
---|
759 | rc = pCache->Backend->pfnWrite(pCache->pBackendData, uOffset, pcvBuf,
|
---|
760 | cbWrite, &cbWritten);
|
---|
761 | uOffset += cbWritten;
|
---|
762 | pcvBuf = (char *)pcvBuf + cbWritten;
|
---|
763 | cbWrite -= cbWritten;
|
---|
764 | } while ( cbWrite
|
---|
765 | && RT_SUCCESS(rc));
|
---|
766 | }
|
---|
767 |
|
---|
768 | LogFlowFunc(("returns rc=%Rrc pcbWritten=%zu\n",
|
---|
769 | rc, pcbWritten ? *pcbWritten : cbWrite));
|
---|
770 | return rc;
|
---|
771 | }
|
---|
772 |
|
---|
773 | /**
|
---|
774 | * Internal: Reads a given amount of data from the image chain of the disk.
|
---|
775 | **/
|
---|
776 | static int vdDiskReadHelper(PVBOXHDD pDisk, PVDIMAGE pImage, PVDIMAGE pImageParentOverride,
|
---|
777 | uint64_t uOffset, void *pvBuf, size_t cbRead, size_t *pcbThisRead)
|
---|
778 | {
|
---|
779 | int rc = VINF_SUCCESS;
|
---|
780 | size_t cbThisRead = cbRead;
|
---|
781 |
|
---|
782 | AssertPtr(pcbThisRead);
|
---|
783 |
|
---|
784 | *pcbThisRead = 0;
|
---|
785 |
|
---|
786 | /*
|
---|
787 | * Try to read from the given image.
|
---|
788 | * If the block is not allocated read from override chain if present.
|
---|
789 | */
|
---|
790 | rc = pImage->Backend->pfnRead(pImage->pBackendData,
|
---|
791 | uOffset, pvBuf, cbThisRead,
|
---|
792 | &cbThisRead);
|
---|
793 |
|
---|
794 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
795 | {
|
---|
796 | for (PVDIMAGE pCurrImage = pImageParentOverride ? pImageParentOverride : pImage->pPrev;
|
---|
797 | pCurrImage != NULL && rc == VERR_VD_BLOCK_FREE;
|
---|
798 | pCurrImage = pCurrImage->pPrev)
|
---|
799 | {
|
---|
800 | rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
|
---|
801 | uOffset, pvBuf, cbThisRead,
|
---|
802 | &cbThisRead);
|
---|
803 | }
|
---|
804 | }
|
---|
805 |
|
---|
806 | if (RT_SUCCESS(rc) || rc == VERR_VD_BLOCK_FREE)
|
---|
807 | *pcbThisRead = cbThisRead;
|
---|
808 |
|
---|
809 | return rc;
|
---|
810 | }
|
---|
811 |
|
---|
812 | /**
|
---|
813 | * internal: read the specified amount of data in whatever blocks the backend
|
---|
814 | * will give us.
|
---|
815 | */
|
---|
816 | static int vdReadHelper(PVBOXHDD pDisk, PVDIMAGE pImage, PVDIMAGE pImageParentOverride,
|
---|
817 | uint64_t uOffset, void *pvBuf, size_t cbRead,
|
---|
818 | bool fZeroFreeBlocks, bool fUpdateCache)
|
---|
819 | {
|
---|
820 | int rc = VINF_SUCCESS;
|
---|
821 | size_t cbThisRead;
|
---|
822 | bool fAllFree = true;
|
---|
823 | size_t cbBufClear = 0;
|
---|
824 |
|
---|
825 | /* Loop until all read. */
|
---|
826 | do
|
---|
827 | {
|
---|
828 | /* Search for image with allocated block. Do not attempt to read more
|
---|
829 | * than the previous reads marked as valid. Otherwise this would return
|
---|
830 | * stale data when different block sizes are used for the images. */
|
---|
831 | cbThisRead = cbRead;
|
---|
832 |
|
---|
833 | if ( pDisk->pCache
|
---|
834 | && !pImageParentOverride)
|
---|
835 | {
|
---|
836 | rc = vdCacheReadHelper(pDisk->pCache, uOffset, pvBuf,
|
---|
837 | cbThisRead, &cbThisRead);
|
---|
838 |
|
---|
839 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
840 | {
|
---|
841 | rc = vdDiskReadHelper(pDisk, pImage, pImageParentOverride,
|
---|
842 | uOffset, pvBuf, cbThisRead, &cbThisRead);
|
---|
843 |
|
---|
844 | /* If the read was successful, write the data back into the cache. */
|
---|
845 | if ( RT_SUCCESS(rc)
|
---|
846 | && fUpdateCache)
|
---|
847 | {
|
---|
848 | rc = vdCacheWriteHelper(pDisk->pCache, uOffset, pvBuf,
|
---|
849 | cbThisRead, NULL);
|
---|
850 | }
|
---|
851 | }
|
---|
852 | }
|
---|
853 | else
|
---|
854 | {
|
---|
855 | /** @todo can be be replaced by vdDiskReadHelper if it proves to be reliable,
|
---|
856 | * don't want to be responsible for data corruption...
|
---|
857 | */
|
---|
858 | /*
|
---|
859 | * Try to read from the given image.
|
---|
860 | * If the block is not allocated read from override chain if present.
|
---|
861 | */
|
---|
862 | rc = pImage->Backend->pfnRead(pImage->pBackendData,
|
---|
863 | uOffset, pvBuf, cbThisRead,
|
---|
864 | &cbThisRead);
|
---|
865 |
|
---|
866 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
867 | {
|
---|
868 | for (PVDIMAGE pCurrImage = pImageParentOverride ? pImageParentOverride : pImage->pPrev;
|
---|
869 | pCurrImage != NULL && rc == VERR_VD_BLOCK_FREE;
|
---|
870 | pCurrImage = pCurrImage->pPrev)
|
---|
871 | {
|
---|
872 | rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
|
---|
873 | uOffset, pvBuf, cbThisRead,
|
---|
874 | &cbThisRead);
|
---|
875 | }
|
---|
876 | }
|
---|
877 | }
|
---|
878 |
|
---|
879 | /* No image in the chain contains the data for the block. */
|
---|
880 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
881 | {
|
---|
882 | /* Fill the free space with 0 if we are told to do so
|
---|
883 | * or a previous read returned valid data. */
|
---|
884 | if (fZeroFreeBlocks || !fAllFree)
|
---|
885 | memset(pvBuf, '\0', cbThisRead);
|
---|
886 | else
|
---|
887 | cbBufClear += cbThisRead;
|
---|
888 |
|
---|
889 | rc = VINF_SUCCESS;
|
---|
890 | }
|
---|
891 | else if (RT_SUCCESS(rc))
|
---|
892 | {
|
---|
893 | /* First not free block, fill the space before with 0. */
|
---|
894 | if (!fZeroFreeBlocks)
|
---|
895 | {
|
---|
896 | memset((char *)pvBuf - cbBufClear, '\0', cbBufClear);
|
---|
897 | cbBufClear = 0;
|
---|
898 | fAllFree = false;
|
---|
899 | }
|
---|
900 | }
|
---|
901 |
|
---|
902 | cbRead -= cbThisRead;
|
---|
903 | uOffset += cbThisRead;
|
---|
904 | pvBuf = (char *)pvBuf + cbThisRead;
|
---|
905 | } while (cbRead != 0 && RT_SUCCESS(rc));
|
---|
906 |
|
---|
907 | return (!fZeroFreeBlocks && fAllFree) ? VERR_VD_BLOCK_FREE : rc;
|
---|
908 | }
|
---|
909 |
|
---|
910 | DECLINLINE(PVDIOCTX) vdIoCtxAlloc(PVBOXHDD pDisk, VDIOCTXTXDIR enmTxDir,
|
---|
911 | uint64_t uOffset, size_t cbTransfer,
|
---|
912 | PVDIMAGE pImageStart,
|
---|
913 | PCRTSGBUF pcSgBuf, void *pvAllocation,
|
---|
914 | PFNVDIOCTXTRANSFER pfnIoCtxTransfer)
|
---|
915 | {
|
---|
916 | PVDIOCTX pIoCtx = NULL;
|
---|
917 |
|
---|
918 | pIoCtx = (PVDIOCTX)RTMemCacheAlloc(pDisk->hMemCacheIoCtx);
|
---|
919 | if (RT_LIKELY(pIoCtx))
|
---|
920 | {
|
---|
921 | pIoCtx->pDisk = pDisk;
|
---|
922 | pIoCtx->enmTxDir = enmTxDir;
|
---|
923 | pIoCtx->cbTransferLeft = cbTransfer;
|
---|
924 | pIoCtx->uOffset = uOffset;
|
---|
925 | pIoCtx->cbTransfer = cbTransfer;
|
---|
926 | pIoCtx->pImageStart = pImageStart;
|
---|
927 | pIoCtx->pImageCur = pImageStart;
|
---|
928 | pIoCtx->cDataTransfersPending = 0;
|
---|
929 | pIoCtx->cMetaTransfersPending = 0;
|
---|
930 | pIoCtx->fComplete = false;
|
---|
931 | pIoCtx->fBlocked = false;
|
---|
932 | pIoCtx->pvAllocation = pvAllocation;
|
---|
933 | pIoCtx->pfnIoCtxTransfer = pfnIoCtxTransfer;
|
---|
934 | pIoCtx->pfnIoCtxTransferNext = NULL;
|
---|
935 | pIoCtx->rcReq = VINF_SUCCESS;
|
---|
936 |
|
---|
937 | /* There is no S/G list for a flush request. */
|
---|
938 | if (enmTxDir != VDIOCTXTXDIR_FLUSH)
|
---|
939 | RTSgBufClone(&pIoCtx->SgBuf, pcSgBuf);
|
---|
940 | else
|
---|
941 | memset(&pIoCtx->SgBuf, 0, sizeof(RTSGBUF));
|
---|
942 | }
|
---|
943 |
|
---|
944 | return pIoCtx;
|
---|
945 | }
|
---|
946 |
|
---|
947 | DECLINLINE(PVDIOCTX) vdIoCtxRootAlloc(PVBOXHDD pDisk, VDIOCTXTXDIR enmTxDir,
|
---|
948 | uint64_t uOffset, size_t cbTransfer,
|
---|
949 | PVDIMAGE pImageStart, PCRTSGBUF pcSgBuf,
|
---|
950 | PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
951 | void *pvUser1, void *pvUser2,
|
---|
952 | void *pvAllocation,
|
---|
953 | PFNVDIOCTXTRANSFER pfnIoCtxTransfer)
|
---|
954 | {
|
---|
955 | PVDIOCTX pIoCtx = vdIoCtxAlloc(pDisk, enmTxDir, uOffset, cbTransfer, pImageStart,
|
---|
956 | pcSgBuf, pvAllocation, pfnIoCtxTransfer);
|
---|
957 |
|
---|
958 | if (RT_LIKELY(pIoCtx))
|
---|
959 | {
|
---|
960 | pIoCtx->pIoCtxParent = NULL;
|
---|
961 | pIoCtx->Type.Root.pfnComplete = pfnComplete;
|
---|
962 | pIoCtx->Type.Root.pvUser1 = pvUser1;
|
---|
963 | pIoCtx->Type.Root.pvUser2 = pvUser2;
|
---|
964 | }
|
---|
965 |
|
---|
966 | LogFlow(("Allocated root I/O context %#p\n", pIoCtx));
|
---|
967 | return pIoCtx;
|
---|
968 | }
|
---|
969 |
|
---|
970 | DECLINLINE(PVDIOCTX) vdIoCtxChildAlloc(PVBOXHDD pDisk, VDIOCTXTXDIR enmTxDir,
|
---|
971 | uint64_t uOffset, size_t cbTransfer,
|
---|
972 | PVDIMAGE pImageStart, PCRTSGBUF pcSgBuf,
|
---|
973 | PVDIOCTX pIoCtxParent, size_t cbTransferParent,
|
---|
974 | size_t cbWriteParent, void *pvAllocation,
|
---|
975 | PFNVDIOCTXTRANSFER pfnIoCtxTransfer)
|
---|
976 | {
|
---|
977 | PVDIOCTX pIoCtx = vdIoCtxAlloc(pDisk, enmTxDir, uOffset, cbTransfer, pImageStart,
|
---|
978 | pcSgBuf, pvAllocation, pfnIoCtxTransfer);
|
---|
979 |
|
---|
980 | AssertPtr(pIoCtxParent);
|
---|
981 | Assert(!pIoCtxParent->pIoCtxParent);
|
---|
982 |
|
---|
983 | if (RT_LIKELY(pIoCtx))
|
---|
984 | {
|
---|
985 | pIoCtx->pIoCtxParent = pIoCtxParent;
|
---|
986 | pIoCtx->Type.Child.uOffsetSaved = uOffset;
|
---|
987 | pIoCtx->Type.Child.cbTransferLeftSaved = cbTransfer;
|
---|
988 | pIoCtx->Type.Child.cbTransferParent = cbTransferParent;
|
---|
989 | pIoCtx->Type.Child.cbWriteParent = cbWriteParent;
|
---|
990 | }
|
---|
991 |
|
---|
992 | LogFlow(("Allocated child I/O context %#p\n", pIoCtx));
|
---|
993 | return pIoCtx;
|
---|
994 | }
|
---|
995 |
|
---|
996 | DECLINLINE(PVDIOTASK) vdIoTaskUserAlloc(PVDIOSTORAGE pIoStorage, PFNVDXFERCOMPLETED pfnComplete, void *pvUser, PVDIOCTX pIoCtx, uint32_t cbTransfer)
|
---|
997 | {
|
---|
998 | PVDIOTASK pIoTask = NULL;
|
---|
999 |
|
---|
1000 | pIoTask = (PVDIOTASK)RTMemCacheAlloc(pIoStorage->pVDIo->pDisk->hMemCacheIoTask);
|
---|
1001 | if (pIoTask)
|
---|
1002 | {
|
---|
1003 | pIoTask->pIoStorage = pIoStorage;
|
---|
1004 | pIoTask->pfnComplete = pfnComplete;
|
---|
1005 | pIoTask->pvUser = pvUser;
|
---|
1006 | pIoTask->fMeta = false;
|
---|
1007 | pIoTask->Type.User.cbTransfer = cbTransfer;
|
---|
1008 | pIoTask->Type.User.pIoCtx = pIoCtx;
|
---|
1009 | }
|
---|
1010 |
|
---|
1011 | return pIoTask;
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | DECLINLINE(PVDIOTASK) vdIoTaskMetaAlloc(PVDIOSTORAGE pIoStorage, PFNVDXFERCOMPLETED pfnComplete, void *pvUser, PVDMETAXFER pMetaXfer)
|
---|
1015 | {
|
---|
1016 | PVDIOTASK pIoTask = NULL;
|
---|
1017 |
|
---|
1018 | pIoTask = (PVDIOTASK)RTMemCacheAlloc(pIoStorage->pVDIo->pDisk->hMemCacheIoTask);
|
---|
1019 | if (pIoTask)
|
---|
1020 | {
|
---|
1021 | pIoTask->pIoStorage = pIoStorage;
|
---|
1022 | pIoTask->pfnComplete = pfnComplete;
|
---|
1023 | pIoTask->pvUser = pvUser;
|
---|
1024 | pIoTask->fMeta = true;
|
---|
1025 | pIoTask->Type.Meta.pMetaXfer = pMetaXfer;
|
---|
1026 | }
|
---|
1027 |
|
---|
1028 | return pIoTask;
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 | DECLINLINE(void) vdIoCtxFree(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
|
---|
1032 | {
|
---|
1033 | LogFlow(("Freeing I/O context %#p\n", pIoCtx));
|
---|
1034 | if (pIoCtx->pvAllocation)
|
---|
1035 | RTMemFree(pIoCtx->pvAllocation);
|
---|
1036 | #ifdef DEBUG
|
---|
1037 | memset(pIoCtx, 0xff, sizeof(VDIOCTX));
|
---|
1038 | #endif
|
---|
1039 | RTMemCacheFree(pDisk->hMemCacheIoCtx, pIoCtx);
|
---|
1040 | }
|
---|
1041 |
|
---|
1042 | DECLINLINE(void) vdIoTaskFree(PVBOXHDD pDisk, PVDIOTASK pIoTask)
|
---|
1043 | {
|
---|
1044 | RTMemCacheFree(pDisk->hMemCacheIoTask, pIoTask);
|
---|
1045 | }
|
---|
1046 |
|
---|
1047 | DECLINLINE(void) vdIoCtxChildReset(PVDIOCTX pIoCtx)
|
---|
1048 | {
|
---|
1049 | AssertPtr(pIoCtx->pIoCtxParent);
|
---|
1050 |
|
---|
1051 | RTSgBufReset(&pIoCtx->SgBuf);
|
---|
1052 | pIoCtx->uOffset = pIoCtx->Type.Child.uOffsetSaved;
|
---|
1053 | pIoCtx->cbTransferLeft = pIoCtx->Type.Child.cbTransferLeftSaved;
|
---|
1054 | }
|
---|
1055 |
|
---|
1056 | DECLINLINE(PVDMETAXFER) vdMetaXferAlloc(PVDIOSTORAGE pIoStorage, uint64_t uOffset, size_t cb)
|
---|
1057 | {
|
---|
1058 | PVDMETAXFER pMetaXfer = (PVDMETAXFER)RTMemAlloc(RT_OFFSETOF(VDMETAXFER, abData[cb]));
|
---|
1059 |
|
---|
1060 | if (RT_LIKELY(pMetaXfer))
|
---|
1061 | {
|
---|
1062 | pMetaXfer->Core.Key = uOffset;
|
---|
1063 | pMetaXfer->Core.KeyLast = uOffset + cb - 1;
|
---|
1064 | pMetaXfer->fFlags = VDMETAXFER_TXDIR_NONE;
|
---|
1065 | pMetaXfer->cbMeta = cb;
|
---|
1066 | pMetaXfer->pIoStorage = pIoStorage;
|
---|
1067 | pMetaXfer->cRefs = 0;
|
---|
1068 | RTListInit(&pMetaXfer->ListIoCtxWaiting);
|
---|
1069 | }
|
---|
1070 | return pMetaXfer;
|
---|
1071 | }
|
---|
1072 |
|
---|
1073 | DECLINLINE(int) vdIoCtxDefer(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
|
---|
1074 | {
|
---|
1075 | PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
|
---|
1076 |
|
---|
1077 | if (!pDeferred)
|
---|
1078 | return VERR_NO_MEMORY;
|
---|
1079 |
|
---|
1080 | LogFlowFunc(("Deferring write pIoCtx=%#p\n", pIoCtx));
|
---|
1081 |
|
---|
1082 | Assert(!pIoCtx->pIoCtxParent && !pIoCtx->fBlocked);
|
---|
1083 |
|
---|
1084 | RTListInit(&pDeferred->NodeDeferred);
|
---|
1085 | pDeferred->pIoCtx = pIoCtx;
|
---|
1086 | RTListAppend(&pDisk->ListWriteLocked, &pDeferred->NodeDeferred);
|
---|
1087 | pIoCtx->fBlocked = true;
|
---|
1088 | return VINF_SUCCESS;
|
---|
1089 | }
|
---|
1090 |
|
---|
1091 | static size_t vdIoCtxCopy(PVDIOCTX pIoCtxDst, PVDIOCTX pIoCtxSrc, size_t cbData)
|
---|
1092 | {
|
---|
1093 | return RTSgBufCopy(&pIoCtxDst->SgBuf, &pIoCtxSrc->SgBuf, cbData);
|
---|
1094 | }
|
---|
1095 |
|
---|
1096 | static int vdIoCtxCmp(PVDIOCTX pIoCtx1, PVDIOCTX pIoCtx2, size_t cbData)
|
---|
1097 | {
|
---|
1098 | return RTSgBufCmp(&pIoCtx1->SgBuf, &pIoCtx2->SgBuf, cbData);
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | static size_t vdIoCtxCopyTo(PVDIOCTX pIoCtx, uint8_t *pbData, size_t cbData)
|
---|
1102 | {
|
---|
1103 | return RTSgBufCopyToBuf(&pIoCtx->SgBuf, pbData, cbData);
|
---|
1104 | }
|
---|
1105 |
|
---|
1106 |
|
---|
1107 | static size_t vdIoCtxCopyFrom(PVDIOCTX pIoCtx, uint8_t *pbData, size_t cbData)
|
---|
1108 | {
|
---|
1109 | return RTSgBufCopyFromBuf(&pIoCtx->SgBuf, pbData, cbData);
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 | static size_t vdIoCtxSet(PVDIOCTX pIoCtx, uint8_t ch, size_t cbData)
|
---|
1113 | {
|
---|
1114 | return RTSgBufSet(&pIoCtx->SgBuf, ch, cbData);
|
---|
1115 | }
|
---|
1116 |
|
---|
1117 | static int vdIoCtxProcess(PVDIOCTX pIoCtx)
|
---|
1118 | {
|
---|
1119 | int rc = VINF_SUCCESS;
|
---|
1120 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
1121 |
|
---|
1122 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
1123 |
|
---|
1124 | RTCritSectEnter(&pDisk->CritSect);
|
---|
1125 |
|
---|
1126 | if ( !pIoCtx->cbTransferLeft
|
---|
1127 | && !pIoCtx->cMetaTransfersPending
|
---|
1128 | && !pIoCtx->cDataTransfersPending
|
---|
1129 | && !pIoCtx->pfnIoCtxTransfer)
|
---|
1130 | {
|
---|
1131 | rc = VINF_VD_ASYNC_IO_FINISHED;
|
---|
1132 | goto out;
|
---|
1133 | }
|
---|
1134 |
|
---|
1135 | /*
|
---|
1136 | * We complete the I/O context in case of an error
|
---|
1137 | * if there is no I/O task pending.
|
---|
1138 | */
|
---|
1139 | if ( RT_FAILURE(pIoCtx->rcReq)
|
---|
1140 | && !pIoCtx->cMetaTransfersPending
|
---|
1141 | && !pIoCtx->cDataTransfersPending)
|
---|
1142 | {
|
---|
1143 | rc = VINF_VD_ASYNC_IO_FINISHED;
|
---|
1144 | goto out;
|
---|
1145 | }
|
---|
1146 |
|
---|
1147 | /* Don't change anything if there is a metadata transfer pending or we are blocked. */
|
---|
1148 | if ( pIoCtx->cMetaTransfersPending
|
---|
1149 | || pIoCtx->fBlocked)
|
---|
1150 | {
|
---|
1151 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1152 | goto out;
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 | if (pIoCtx->pfnIoCtxTransfer)
|
---|
1156 | {
|
---|
1157 | /* Call the transfer function advancing to the next while there is no error. */
|
---|
1158 | while ( pIoCtx->pfnIoCtxTransfer
|
---|
1159 | && RT_SUCCESS(rc))
|
---|
1160 | {
|
---|
1161 | LogFlowFunc(("calling transfer function %#p\n", pIoCtx->pfnIoCtxTransfer));
|
---|
1162 | rc = pIoCtx->pfnIoCtxTransfer(pIoCtx);
|
---|
1163 |
|
---|
1164 | /* Advance to the next part of the transfer if the current one succeeded. */
|
---|
1165 | if (RT_SUCCESS(rc))
|
---|
1166 | {
|
---|
1167 | pIoCtx->pfnIoCtxTransfer = pIoCtx->pfnIoCtxTransferNext;
|
---|
1168 | pIoCtx->pfnIoCtxTransferNext = NULL;
|
---|
1169 | }
|
---|
1170 | }
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 | if ( RT_SUCCESS(rc)
|
---|
1174 | && !pIoCtx->cbTransferLeft
|
---|
1175 | && !pIoCtx->cMetaTransfersPending
|
---|
1176 | && !pIoCtx->cDataTransfersPending)
|
---|
1177 | rc = VINF_VD_ASYNC_IO_FINISHED;
|
---|
1178 | else if ( RT_SUCCESS(rc)
|
---|
1179 | || rc == VERR_VD_NOT_ENOUGH_METADATA
|
---|
1180 | || rc == VERR_VD_IOCTX_HALT)
|
---|
1181 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1182 | else if (RT_FAILURE(rc) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
1183 | {
|
---|
1184 | ASMAtomicCmpXchgS32(&pIoCtx->rcReq, rc, VINF_SUCCESS);
|
---|
1185 | /*
|
---|
1186 | * The I/O context completed if we have an error and there is no data
|
---|
1187 | * or meta data transfer pending.
|
---|
1188 | */
|
---|
1189 | if ( !pIoCtx->cMetaTransfersPending
|
---|
1190 | && !pIoCtx->cDataTransfersPending)
|
---|
1191 | rc = VINF_VD_ASYNC_IO_FINISHED;
|
---|
1192 | else
|
---|
1193 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1194 | }
|
---|
1195 |
|
---|
1196 | out:
|
---|
1197 | RTCritSectLeave(&pDisk->CritSect);
|
---|
1198 |
|
---|
1199 | LogFlowFunc(("pIoCtx=%#p rc=%Rrc cbTransferLeft=%u cMetaTransfersPending=%u fComplete=%RTbool\n",
|
---|
1200 | pIoCtx, rc, pIoCtx->cbTransferLeft, pIoCtx->cMetaTransfersPending,
|
---|
1201 | pIoCtx->fComplete));
|
---|
1202 |
|
---|
1203 | return rc;
|
---|
1204 | }
|
---|
1205 |
|
---|
1206 | DECLINLINE(bool) vdIoCtxIsDiskLockOwner(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
|
---|
1207 | {
|
---|
1208 | return pDisk->fLocked
|
---|
1209 | && pDisk->pIoCtxLockOwner == pIoCtx;
|
---|
1210 | }
|
---|
1211 |
|
---|
1212 | static int vdIoCtxLockDisk(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
|
---|
1213 | {
|
---|
1214 | int rc = VINF_SUCCESS;
|
---|
1215 |
|
---|
1216 | LogFlowFunc(("pDisk=%#p pIoCtx=%#p\n", pDisk, pIoCtx));
|
---|
1217 |
|
---|
1218 | if (!ASMAtomicCmpXchgBool(&pDisk->fLocked, true, false))
|
---|
1219 | {
|
---|
1220 | Assert(pDisk->pIoCtxLockOwner != pIoCtx); /* No nesting allowed. */
|
---|
1221 |
|
---|
1222 | rc = vdIoCtxDefer(pDisk, pIoCtx);
|
---|
1223 | if (RT_SUCCESS(rc))
|
---|
1224 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1225 | }
|
---|
1226 | else
|
---|
1227 | {
|
---|
1228 | Assert(!pDisk->pIoCtxLockOwner);
|
---|
1229 | pDisk->pIoCtxLockOwner = pIoCtx;
|
---|
1230 | }
|
---|
1231 |
|
---|
1232 | LogFlowFunc(("returns -> %Rrc\n", rc));
|
---|
1233 | return rc;
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 | static void vdIoCtxUnlockDisk(PVBOXHDD pDisk, PVDIOCTX pIoCtx, bool fProcessDeferredReqs)
|
---|
1237 | {
|
---|
1238 | LogFlowFunc(("pDisk=%#p pIoCtx=%#p fProcessDeferredReqs=%RTbool\n",
|
---|
1239 | pDisk, pIoCtx, fProcessDeferredReqs));
|
---|
1240 |
|
---|
1241 | LogFlow(("Unlocking disk lock owner is %#p\n", pDisk->pIoCtxLockOwner));
|
---|
1242 | Assert(pDisk->fLocked);
|
---|
1243 | Assert(pDisk->pIoCtxLockOwner == pIoCtx);
|
---|
1244 | pDisk->pIoCtxLockOwner = NULL;
|
---|
1245 | ASMAtomicXchgBool(&pDisk->fLocked, false);
|
---|
1246 |
|
---|
1247 | if (fProcessDeferredReqs)
|
---|
1248 | {
|
---|
1249 | /* Process any pending writes if the current request didn't caused another growing. */
|
---|
1250 | RTCritSectEnter(&pDisk->CritSect);
|
---|
1251 |
|
---|
1252 | if (!RTListIsEmpty(&pDisk->ListWriteLocked))
|
---|
1253 | {
|
---|
1254 | RTLISTNODE ListTmp;
|
---|
1255 |
|
---|
1256 | RTListMove(&ListTmp, &pDisk->ListWriteLocked);
|
---|
1257 | RTCritSectLeave(&pDisk->CritSect);
|
---|
1258 |
|
---|
1259 | /* Process the list. */
|
---|
1260 | do
|
---|
1261 | {
|
---|
1262 | int rc;
|
---|
1263 | PVDIOCTXDEFERRED pDeferred = RTListGetFirst(&ListTmp, VDIOCTXDEFERRED, NodeDeferred);
|
---|
1264 | PVDIOCTX pIoCtxWait = pDeferred->pIoCtx;
|
---|
1265 |
|
---|
1266 | AssertPtr(pIoCtxWait);
|
---|
1267 |
|
---|
1268 | RTListNodeRemove(&pDeferred->NodeDeferred);
|
---|
1269 | RTMemFree(pDeferred);
|
---|
1270 |
|
---|
1271 | Assert(!pIoCtxWait->pIoCtxParent);
|
---|
1272 |
|
---|
1273 | pIoCtxWait->fBlocked = false;
|
---|
1274 | LogFlowFunc(("Processing waiting I/O context pIoCtxWait=%#p\n", pIoCtxWait));
|
---|
1275 |
|
---|
1276 | rc = vdIoCtxProcess(pIoCtxWait);
|
---|
1277 | if ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
1278 | && ASMAtomicCmpXchgBool(&pIoCtxWait->fComplete, true, false))
|
---|
1279 | {
|
---|
1280 | LogFlowFunc(("Waiting I/O context completed pIoCtxWait=%#p\n", pIoCtxWait));
|
---|
1281 | vdThreadFinishWrite(pDisk);
|
---|
1282 | pIoCtxWait->Type.Root.pfnComplete(pIoCtxWait->Type.Root.pvUser1,
|
---|
1283 | pIoCtxWait->Type.Root.pvUser2,
|
---|
1284 | pIoCtxWait->rcReq);
|
---|
1285 | vdIoCtxFree(pDisk, pIoCtxWait);
|
---|
1286 | }
|
---|
1287 | } while (!RTListIsEmpty(&ListTmp));
|
---|
1288 | }
|
---|
1289 | else
|
---|
1290 | RTCritSectLeave(&pDisk->CritSect);
|
---|
1291 | }
|
---|
1292 |
|
---|
1293 | LogFlowFunc(("returns\n"));
|
---|
1294 | }
|
---|
1295 |
|
---|
1296 | /**
|
---|
1297 | * internal: read the specified amount of data in whatever blocks the backend
|
---|
1298 | * will give us - async version.
|
---|
1299 | */
|
---|
1300 | static int vdReadHelperAsync(PVDIOCTX pIoCtx)
|
---|
1301 | {
|
---|
1302 | int rc;
|
---|
1303 | size_t cbToRead = pIoCtx->cbTransfer;
|
---|
1304 | uint64_t uOffset = pIoCtx->uOffset;
|
---|
1305 | PVDIMAGE pCurrImage = NULL;
|
---|
1306 | size_t cbThisRead;
|
---|
1307 |
|
---|
1308 | /* Loop until all reads started or we have a backend which needs to read metadata. */
|
---|
1309 | do
|
---|
1310 | {
|
---|
1311 | pCurrImage = pIoCtx->pImageCur;
|
---|
1312 |
|
---|
1313 | /* Search for image with allocated block. Do not attempt to read more
|
---|
1314 | * than the previous reads marked as valid. Otherwise this would return
|
---|
1315 | * stale data when different block sizes are used for the images. */
|
---|
1316 | cbThisRead = cbToRead;
|
---|
1317 |
|
---|
1318 | /*
|
---|
1319 | * Try to read from the given image.
|
---|
1320 | * If the block is not allocated read from override chain if present.
|
---|
1321 | */
|
---|
1322 | rc = pCurrImage->Backend->pfnAsyncRead(pCurrImage->pBackendData,
|
---|
1323 | uOffset, cbThisRead,
|
---|
1324 | pIoCtx, &cbThisRead);
|
---|
1325 |
|
---|
1326 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
1327 | {
|
---|
1328 | while ( pCurrImage->pPrev != NULL
|
---|
1329 | && rc == VERR_VD_BLOCK_FREE)
|
---|
1330 | {
|
---|
1331 | pCurrImage = pCurrImage->pPrev;
|
---|
1332 | rc = pCurrImage->Backend->pfnAsyncRead(pCurrImage->pBackendData,
|
---|
1333 | uOffset, cbThisRead,
|
---|
1334 | pIoCtx, &cbThisRead);
|
---|
1335 | }
|
---|
1336 | }
|
---|
1337 |
|
---|
1338 | /* The task state will be updated on success already, don't do it here!. */
|
---|
1339 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
1340 | {
|
---|
1341 | /* No image in the chain contains the data for the block. */
|
---|
1342 | vdIoCtxSet(pIoCtx, '\0', cbThisRead);
|
---|
1343 | ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbThisRead);
|
---|
1344 | rc = VINF_SUCCESS;
|
---|
1345 | }
|
---|
1346 | else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
1347 | rc = VINF_SUCCESS;
|
---|
1348 | else if (rc == VERR_VD_IOCTX_HALT)
|
---|
1349 | {
|
---|
1350 | uOffset += cbThisRead;
|
---|
1351 | cbToRead -= cbThisRead;
|
---|
1352 | pIoCtx->fBlocked = true;
|
---|
1353 | }
|
---|
1354 |
|
---|
1355 | if (RT_FAILURE(rc))
|
---|
1356 | break;
|
---|
1357 |
|
---|
1358 | cbToRead -= cbThisRead;
|
---|
1359 | uOffset += cbThisRead;
|
---|
1360 | } while (cbToRead != 0 && RT_SUCCESS(rc));
|
---|
1361 |
|
---|
1362 | if ( rc == VERR_VD_NOT_ENOUGH_METADATA
|
---|
1363 | || rc == VERR_VD_IOCTX_HALT)
|
---|
1364 | {
|
---|
1365 | /* Save the current state. */
|
---|
1366 | pIoCtx->uOffset = uOffset;
|
---|
1367 | pIoCtx->cbTransfer = cbToRead;
|
---|
1368 | pIoCtx->pImageCur = pCurrImage ? pCurrImage : pIoCtx->pImageStart;
|
---|
1369 | }
|
---|
1370 |
|
---|
1371 | return rc;
|
---|
1372 | }
|
---|
1373 |
|
---|
1374 | /**
|
---|
1375 | * internal: parent image read wrapper for compacting.
|
---|
1376 | */
|
---|
1377 | static int vdParentRead(void *pvUser, uint64_t uOffset, void *pvBuf,
|
---|
1378 | size_t cbRead)
|
---|
1379 | {
|
---|
1380 | PVDPARENTSTATEDESC pParentState = (PVDPARENTSTATEDESC)pvUser;
|
---|
1381 | return vdReadHelper(pParentState->pDisk, pParentState->pImage, NULL, uOffset,
|
---|
1382 | pvBuf, cbRead, true /* fZeroFreeBlocks */,
|
---|
1383 | false /* fUpdateCache */);
|
---|
1384 | }
|
---|
1385 |
|
---|
1386 | /**
|
---|
1387 | * internal: mark the disk as not modified.
|
---|
1388 | */
|
---|
1389 | static void vdResetModifiedFlag(PVBOXHDD pDisk)
|
---|
1390 | {
|
---|
1391 | if (pDisk->uModified & VD_IMAGE_MODIFIED_FLAG)
|
---|
1392 | {
|
---|
1393 | /* generate new last-modified uuid */
|
---|
1394 | if (!(pDisk->uModified & VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
|
---|
1395 | {
|
---|
1396 | RTUUID Uuid;
|
---|
1397 |
|
---|
1398 | RTUuidCreate(&Uuid);
|
---|
1399 | pDisk->pLast->Backend->pfnSetModificationUuid(pDisk->pLast->pBackendData,
|
---|
1400 | &Uuid);
|
---|
1401 |
|
---|
1402 | if (pDisk->pCache)
|
---|
1403 | pDisk->pCache->Backend->pfnSetModificationUuid(pDisk->pCache->pBackendData,
|
---|
1404 | &Uuid);
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 | pDisk->uModified &= ~VD_IMAGE_MODIFIED_FLAG;
|
---|
1408 | }
|
---|
1409 | }
|
---|
1410 |
|
---|
1411 | /**
|
---|
1412 | * internal: mark the disk as modified.
|
---|
1413 | */
|
---|
1414 | static void vdSetModifiedFlag(PVBOXHDD pDisk)
|
---|
1415 | {
|
---|
1416 | pDisk->uModified |= VD_IMAGE_MODIFIED_FLAG;
|
---|
1417 | if (pDisk->uModified & VD_IMAGE_MODIFIED_FIRST)
|
---|
1418 | {
|
---|
1419 | pDisk->uModified &= ~VD_IMAGE_MODIFIED_FIRST;
|
---|
1420 |
|
---|
1421 | /* First modify, so create a UUID and ensure it's written to disk. */
|
---|
1422 | vdResetModifiedFlag(pDisk);
|
---|
1423 |
|
---|
1424 | if (!(pDisk->uModified & VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
|
---|
1425 | pDisk->pLast->Backend->pfnFlush(pDisk->pLast->pBackendData);
|
---|
1426 | }
|
---|
1427 | }
|
---|
1428 |
|
---|
1429 | /**
|
---|
1430 | * internal: write a complete block (only used for diff images), taking the
|
---|
1431 | * remaining data from parent images. This implementation does not optimize
|
---|
1432 | * anything (except that it tries to read only that portions from parent
|
---|
1433 | * images that are really needed).
|
---|
1434 | */
|
---|
1435 | static int vdWriteHelperStandard(PVBOXHDD pDisk, PVDIMAGE pImage,
|
---|
1436 | PVDIMAGE pImageParentOverride,
|
---|
1437 | uint64_t uOffset, size_t cbWrite,
|
---|
1438 | size_t cbThisWrite, size_t cbPreRead,
|
---|
1439 | size_t cbPostRead, const void *pvBuf,
|
---|
1440 | void *pvTmp)
|
---|
1441 | {
|
---|
1442 | int rc = VINF_SUCCESS;
|
---|
1443 |
|
---|
1444 | /* Read the data that goes before the write to fill the block. */
|
---|
1445 | if (cbPreRead)
|
---|
1446 | {
|
---|
1447 | /*
|
---|
1448 | * Updating the cache doesn't make sense here because
|
---|
1449 | * this will be done after the complete block was written.
|
---|
1450 | */
|
---|
1451 | rc = vdReadHelper(pDisk, pImage, pImageParentOverride,
|
---|
1452 | uOffset - cbPreRead, pvTmp, cbPreRead,
|
---|
1453 | true /* fZeroFreeBlocks*/,
|
---|
1454 | false /* fUpdateCache */);
|
---|
1455 | if (RT_FAILURE(rc))
|
---|
1456 | return rc;
|
---|
1457 | }
|
---|
1458 |
|
---|
1459 | /* Copy the data to the right place in the buffer. */
|
---|
1460 | memcpy((char *)pvTmp + cbPreRead, pvBuf, cbThisWrite);
|
---|
1461 |
|
---|
1462 | /* Read the data that goes after the write to fill the block. */
|
---|
1463 | if (cbPostRead)
|
---|
1464 | {
|
---|
1465 | /* If we have data to be written, use that instead of reading
|
---|
1466 | * data from the image. */
|
---|
1467 | size_t cbWriteCopy;
|
---|
1468 | if (cbWrite > cbThisWrite)
|
---|
1469 | cbWriteCopy = RT_MIN(cbWrite - cbThisWrite, cbPostRead);
|
---|
1470 | else
|
---|
1471 | cbWriteCopy = 0;
|
---|
1472 | /* Figure out how much we cannot read from the image, because
|
---|
1473 | * the last block to write might exceed the nominal size of the
|
---|
1474 | * image for technical reasons. */
|
---|
1475 | size_t cbFill;
|
---|
1476 | if (uOffset + cbThisWrite + cbPostRead > pDisk->cbSize)
|
---|
1477 | cbFill = uOffset + cbThisWrite + cbPostRead - pDisk->cbSize;
|
---|
1478 | else
|
---|
1479 | cbFill = 0;
|
---|
1480 | /* The rest must be read from the image. */
|
---|
1481 | size_t cbReadImage = cbPostRead - cbWriteCopy - cbFill;
|
---|
1482 |
|
---|
1483 | /* Now assemble the remaining data. */
|
---|
1484 | if (cbWriteCopy)
|
---|
1485 | memcpy((char *)pvTmp + cbPreRead + cbThisWrite,
|
---|
1486 | (char *)pvBuf + cbThisWrite, cbWriteCopy);
|
---|
1487 | if (cbReadImage)
|
---|
1488 | rc = vdReadHelper(pDisk, pImage, pImageParentOverride,
|
---|
1489 | uOffset + cbThisWrite + cbWriteCopy,
|
---|
1490 | (char *)pvTmp + cbPreRead + cbThisWrite + cbWriteCopy,
|
---|
1491 | cbReadImage, true /* fZeroFreeBlocks */,
|
---|
1492 | false /* fUpdateCache */);
|
---|
1493 | if (RT_FAILURE(rc))
|
---|
1494 | return rc;
|
---|
1495 | /* Zero out the remainder of this block. Will never be visible, as this
|
---|
1496 | * is beyond the limit of the image. */
|
---|
1497 | if (cbFill)
|
---|
1498 | memset((char *)pvTmp + cbPreRead + cbThisWrite + cbWriteCopy + cbReadImage,
|
---|
1499 | '\0', cbFill);
|
---|
1500 | }
|
---|
1501 |
|
---|
1502 | /* Write the full block to the virtual disk. */
|
---|
1503 | rc = pImage->Backend->pfnWrite(pImage->pBackendData,
|
---|
1504 | uOffset - cbPreRead, pvTmp,
|
---|
1505 | cbPreRead + cbThisWrite + cbPostRead,
|
---|
1506 | NULL, &cbPreRead, &cbPostRead, 0);
|
---|
1507 | Assert(rc != VERR_VD_BLOCK_FREE);
|
---|
1508 | Assert(cbPreRead == 0);
|
---|
1509 | Assert(cbPostRead == 0);
|
---|
1510 |
|
---|
1511 | return rc;
|
---|
1512 | }
|
---|
1513 |
|
---|
1514 | /**
|
---|
1515 | * internal: write a complete block (only used for diff images), taking the
|
---|
1516 | * remaining data from parent images. This implementation optimizes out writes
|
---|
1517 | * that do not change the data relative to the state as of the parent images.
|
---|
1518 | * All backends which support differential/growing images support this.
|
---|
1519 | */
|
---|
1520 | static int vdWriteHelperOptimized(PVBOXHDD pDisk, PVDIMAGE pImage,
|
---|
1521 | PVDIMAGE pImageParentOverride,
|
---|
1522 | uint64_t uOffset, size_t cbWrite,
|
---|
1523 | size_t cbThisWrite, size_t cbPreRead,
|
---|
1524 | size_t cbPostRead, const void *pvBuf,
|
---|
1525 | void *pvTmp)
|
---|
1526 | {
|
---|
1527 | size_t cbFill = 0;
|
---|
1528 | size_t cbWriteCopy = 0;
|
---|
1529 | size_t cbReadImage = 0;
|
---|
1530 | int rc;
|
---|
1531 |
|
---|
1532 | if (cbPostRead)
|
---|
1533 | {
|
---|
1534 | /* Figure out how much we cannot read from the image, because
|
---|
1535 | * the last block to write might exceed the nominal size of the
|
---|
1536 | * image for technical reasons. */
|
---|
1537 | if (uOffset + cbThisWrite + cbPostRead > pDisk->cbSize)
|
---|
1538 | cbFill = uOffset + cbThisWrite + cbPostRead - pDisk->cbSize;
|
---|
1539 |
|
---|
1540 | /* If we have data to be written, use that instead of reading
|
---|
1541 | * data from the image. */
|
---|
1542 | if (cbWrite > cbThisWrite)
|
---|
1543 | cbWriteCopy = RT_MIN(cbWrite - cbThisWrite, cbPostRead);
|
---|
1544 |
|
---|
1545 | /* The rest must be read from the image. */
|
---|
1546 | cbReadImage = cbPostRead - cbWriteCopy - cbFill;
|
---|
1547 | }
|
---|
1548 |
|
---|
1549 | /* Read the entire data of the block so that we can compare whether it will
|
---|
1550 | * be modified by the write or not. */
|
---|
1551 | rc = vdReadHelper(pDisk, pImage, pImageParentOverride, uOffset - cbPreRead, pvTmp,
|
---|
1552 | cbPreRead + cbThisWrite + cbPostRead - cbFill,
|
---|
1553 | true /* fZeroFreeBlocks */,
|
---|
1554 | false /* fUpdateCache */);
|
---|
1555 | if (RT_FAILURE(rc))
|
---|
1556 | return rc;
|
---|
1557 |
|
---|
1558 | /* Check if the write would modify anything in this block. */
|
---|
1559 | if ( !memcmp((char *)pvTmp + cbPreRead, pvBuf, cbThisWrite)
|
---|
1560 | && (!cbWriteCopy || !memcmp((char *)pvTmp + cbPreRead + cbThisWrite,
|
---|
1561 | (char *)pvBuf + cbThisWrite, cbWriteCopy)))
|
---|
1562 | {
|
---|
1563 | /* Block is completely unchanged, so no need to write anything. */
|
---|
1564 | return VINF_SUCCESS;
|
---|
1565 | }
|
---|
1566 |
|
---|
1567 | /* Copy the data to the right place in the buffer. */
|
---|
1568 | memcpy((char *)pvTmp + cbPreRead, pvBuf, cbThisWrite);
|
---|
1569 |
|
---|
1570 | /* Handle the data that goes after the write to fill the block. */
|
---|
1571 | if (cbPostRead)
|
---|
1572 | {
|
---|
1573 | /* Now assemble the remaining data. */
|
---|
1574 | if (cbWriteCopy)
|
---|
1575 | memcpy((char *)pvTmp + cbPreRead + cbThisWrite,
|
---|
1576 | (char *)pvBuf + cbThisWrite, cbWriteCopy);
|
---|
1577 | /* Zero out the remainder of this block. Will never be visible, as this
|
---|
1578 | * is beyond the limit of the image. */
|
---|
1579 | if (cbFill)
|
---|
1580 | memset((char *)pvTmp + cbPreRead + cbThisWrite + cbWriteCopy + cbReadImage,
|
---|
1581 | '\0', cbFill);
|
---|
1582 | }
|
---|
1583 |
|
---|
1584 | /* Write the full block to the virtual disk. */
|
---|
1585 | rc = pImage->Backend->pfnWrite(pImage->pBackendData,
|
---|
1586 | uOffset - cbPreRead, pvTmp,
|
---|
1587 | cbPreRead + cbThisWrite + cbPostRead,
|
---|
1588 | NULL, &cbPreRead, &cbPostRead, 0);
|
---|
1589 | Assert(rc != VERR_VD_BLOCK_FREE);
|
---|
1590 | Assert(cbPreRead == 0);
|
---|
1591 | Assert(cbPostRead == 0);
|
---|
1592 |
|
---|
1593 | return rc;
|
---|
1594 | }
|
---|
1595 |
|
---|
1596 | /**
|
---|
1597 | * internal: write buffer to the image, taking care of block boundaries and
|
---|
1598 | * write optimizations.
|
---|
1599 | */
|
---|
1600 | static int vdWriteHelper(PVBOXHDD pDisk, PVDIMAGE pImage,
|
---|
1601 | PVDIMAGE pImageParentOverride, uint64_t uOffset,
|
---|
1602 | const void *pvBuf, size_t cbWrite,
|
---|
1603 | bool fUpdateCache)
|
---|
1604 | {
|
---|
1605 | int rc;
|
---|
1606 | unsigned fWrite;
|
---|
1607 | size_t cbThisWrite;
|
---|
1608 | size_t cbPreRead, cbPostRead;
|
---|
1609 | uint64_t uOffsetCur = uOffset;
|
---|
1610 | size_t cbWriteCur = cbWrite;
|
---|
1611 | const void *pcvBufCur = pvBuf;
|
---|
1612 |
|
---|
1613 | /* Loop until all written. */
|
---|
1614 | do
|
---|
1615 | {
|
---|
1616 | /* Try to write the possibly partial block to the last opened image.
|
---|
1617 | * This works when the block is already allocated in this image or
|
---|
1618 | * if it is a full-block write (and allocation isn't suppressed below).
|
---|
1619 | * For image formats which don't support zero blocks, it's beneficial
|
---|
1620 | * to avoid unnecessarily allocating unchanged blocks. This prevents
|
---|
1621 | * unwanted expanding of images. VMDK is an example. */
|
---|
1622 | cbThisWrite = cbWriteCur;
|
---|
1623 | fWrite = (pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME)
|
---|
1624 | ? 0 : VD_WRITE_NO_ALLOC;
|
---|
1625 | rc = pImage->Backend->pfnWrite(pImage->pBackendData, uOffsetCur, pcvBufCur,
|
---|
1626 | cbThisWrite, &cbThisWrite, &cbPreRead,
|
---|
1627 | &cbPostRead, fWrite);
|
---|
1628 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
1629 | {
|
---|
1630 | void *pvTmp = RTMemTmpAlloc(cbPreRead + cbThisWrite + cbPostRead);
|
---|
1631 | AssertBreakStmt(VALID_PTR(pvTmp), rc = VERR_NO_MEMORY);
|
---|
1632 |
|
---|
1633 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME))
|
---|
1634 | {
|
---|
1635 | /* Optimized write, suppress writing to a so far unallocated
|
---|
1636 | * block if the data is in fact not changed. */
|
---|
1637 | rc = vdWriteHelperOptimized(pDisk, pImage, pImageParentOverride,
|
---|
1638 | uOffsetCur, cbWriteCur,
|
---|
1639 | cbThisWrite, cbPreRead, cbPostRead,
|
---|
1640 | pcvBufCur, pvTmp);
|
---|
1641 | }
|
---|
1642 | else
|
---|
1643 | {
|
---|
1644 | /* Normal write, not optimized in any way. The block will
|
---|
1645 | * be written no matter what. This will usually (unless the
|
---|
1646 | * backend has some further optimization enabled) cause the
|
---|
1647 | * block to be allocated. */
|
---|
1648 | rc = vdWriteHelperStandard(pDisk, pImage, pImageParentOverride,
|
---|
1649 | uOffsetCur, cbWriteCur,
|
---|
1650 | cbThisWrite, cbPreRead, cbPostRead,
|
---|
1651 | pcvBufCur, pvTmp);
|
---|
1652 | }
|
---|
1653 | RTMemTmpFree(pvTmp);
|
---|
1654 | if (RT_FAILURE(rc))
|
---|
1655 | break;
|
---|
1656 | }
|
---|
1657 |
|
---|
1658 | cbWriteCur -= cbThisWrite;
|
---|
1659 | uOffsetCur += cbThisWrite;
|
---|
1660 | pcvBufCur = (char *)pcvBufCur + cbThisWrite;
|
---|
1661 | } while (cbWriteCur != 0 && RT_SUCCESS(rc));
|
---|
1662 |
|
---|
1663 | /* Update the cache on success */
|
---|
1664 | if ( RT_SUCCESS(rc)
|
---|
1665 | && pDisk->pCache
|
---|
1666 | && fUpdateCache)
|
---|
1667 | rc = vdCacheWriteHelper(pDisk->pCache, uOffset, pvBuf, cbWrite, NULL);
|
---|
1668 |
|
---|
1669 | return rc;
|
---|
1670 | }
|
---|
1671 |
|
---|
1672 | /**
|
---|
1673 | * Flush helper async version.
|
---|
1674 | */
|
---|
1675 | static int vdSetModifiedHelperAsync(PVDIOCTX pIoCtx)
|
---|
1676 | {
|
---|
1677 | int rc = VINF_SUCCESS;
|
---|
1678 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
1679 | PVDIMAGE pImage = pIoCtx->pImageCur;
|
---|
1680 |
|
---|
1681 | rc = pImage->Backend->pfnAsyncFlush(pImage->pBackendData, pIoCtx);
|
---|
1682 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
1683 | rc = VINF_SUCCESS;
|
---|
1684 |
|
---|
1685 | return rc;
|
---|
1686 | }
|
---|
1687 |
|
---|
1688 | /**
|
---|
1689 | * internal: mark the disk as modified - async version.
|
---|
1690 | */
|
---|
1691 | static int vdSetModifiedFlagAsync(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
|
---|
1692 | {
|
---|
1693 | int rc = VINF_SUCCESS;
|
---|
1694 |
|
---|
1695 | pDisk->uModified |= VD_IMAGE_MODIFIED_FLAG;
|
---|
1696 | if (pDisk->uModified & VD_IMAGE_MODIFIED_FIRST)
|
---|
1697 | {
|
---|
1698 | rc = vdIoCtxLockDisk(pDisk, pIoCtx);
|
---|
1699 | if (RT_SUCCESS(rc))
|
---|
1700 | {
|
---|
1701 | pDisk->uModified &= ~VD_IMAGE_MODIFIED_FIRST;
|
---|
1702 |
|
---|
1703 | /* First modify, so create a UUID and ensure it's written to disk. */
|
---|
1704 | vdResetModifiedFlag(pDisk);
|
---|
1705 |
|
---|
1706 | if (!(pDisk->uModified & VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
|
---|
1707 | {
|
---|
1708 | PVDIOCTX pIoCtxFlush = vdIoCtxChildAlloc(pDisk, VDIOCTXTXDIR_FLUSH,
|
---|
1709 | 0, 0, pDisk->pLast,
|
---|
1710 | NULL, pIoCtx, 0, 0, NULL,
|
---|
1711 | vdSetModifiedHelperAsync);
|
---|
1712 |
|
---|
1713 | if (pIoCtxFlush)
|
---|
1714 | {
|
---|
1715 | rc = vdIoCtxProcess(pIoCtxFlush);
|
---|
1716 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
1717 | {
|
---|
1718 | vdIoCtxUnlockDisk(pDisk, pIoCtx, false /* fProcessDeferredReqs */);
|
---|
1719 | vdIoCtxFree(pDisk, pIoCtxFlush);
|
---|
1720 | }
|
---|
1721 | else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
1722 | {
|
---|
1723 | pIoCtx->fBlocked = true;
|
---|
1724 | }
|
---|
1725 | else /* Another error */
|
---|
1726 | vdIoCtxFree(pDisk, pIoCtxFlush);
|
---|
1727 | }
|
---|
1728 | else
|
---|
1729 | rc = VERR_NO_MEMORY;
|
---|
1730 | }
|
---|
1731 | }
|
---|
1732 | }
|
---|
1733 |
|
---|
1734 | return rc;
|
---|
1735 | }
|
---|
1736 |
|
---|
1737 | /**
|
---|
1738 | * internal: write a complete block (only used for diff images), taking the
|
---|
1739 | * remaining data from parent images. This implementation does not optimize
|
---|
1740 | * anything (except that it tries to read only that portions from parent
|
---|
1741 | * images that are really needed) - async version.
|
---|
1742 | */
|
---|
1743 | static int vdWriteHelperStandardAsync(PVDIOCTX pIoCtx)
|
---|
1744 | {
|
---|
1745 | int rc = VINF_SUCCESS;
|
---|
1746 |
|
---|
1747 | #if 0
|
---|
1748 |
|
---|
1749 | /* Read the data that goes before the write to fill the block. */
|
---|
1750 | if (cbPreRead)
|
---|
1751 | {
|
---|
1752 | rc = vdReadHelperAsync(pIoCtxDst);
|
---|
1753 | if (RT_FAILURE(rc))
|
---|
1754 | return rc;
|
---|
1755 | }
|
---|
1756 |
|
---|
1757 | /* Copy the data to the right place in the buffer. */
|
---|
1758 | vdIoCtxCopy(pIoCtxDst, pIoCtxSrc, cbThisWrite);
|
---|
1759 |
|
---|
1760 | /* Read the data that goes after the write to fill the block. */
|
---|
1761 | if (cbPostRead)
|
---|
1762 | {
|
---|
1763 | /* If we have data to be written, use that instead of reading
|
---|
1764 | * data from the image. */
|
---|
1765 | size_t cbWriteCopy;
|
---|
1766 | if (cbWrite > cbThisWrite)
|
---|
1767 | cbWriteCopy = RT_MIN(cbWrite - cbThisWrite, cbPostRead);
|
---|
1768 | else
|
---|
1769 | cbWriteCopy = 0;
|
---|
1770 | /* Figure out how much we cannot read from the image, because
|
---|
1771 | * the last block to write might exceed the nominal size of the
|
---|
1772 | * image for technical reasons. */
|
---|
1773 | size_t cbFill;
|
---|
1774 | if (uOffset + cbThisWrite + cbPostRead > pDisk->cbSize)
|
---|
1775 | cbFill = uOffset + cbThisWrite + cbPostRead - pDisk->cbSize;
|
---|
1776 | else
|
---|
1777 | cbFill = 0;
|
---|
1778 | /* The rest must be read from the image. */
|
---|
1779 | size_t cbReadImage = cbPostRead - cbWriteCopy - cbFill;
|
---|
1780 |
|
---|
1781 | /* Now assemble the remaining data. */
|
---|
1782 | if (cbWriteCopy)
|
---|
1783 | {
|
---|
1784 | vdIoCtxCopy(pIoCtxDst, pIoCtxSrc, cbWriteCopy);
|
---|
1785 | ASMAtomicSubU32(&pIoCtxDst->cbTransferLeft, cbWriteCopy);
|
---|
1786 | }
|
---|
1787 |
|
---|
1788 | if (cbReadImage)
|
---|
1789 | rc = vdReadHelperAsync(pDisk, pImage, pImageParentOverride, pIoCtxDst,
|
---|
1790 | uOffset + cbThisWrite + cbWriteCopy,
|
---|
1791 | cbReadImage);
|
---|
1792 | if (RT_FAILURE(rc))
|
---|
1793 | return rc;
|
---|
1794 | /* Zero out the remainder of this block. Will never be visible, as this
|
---|
1795 | * is beyond the limit of the image. */
|
---|
1796 | if (cbFill)
|
---|
1797 | {
|
---|
1798 | vdIoCtxSet(pIoCtxDst, '\0', cbFill);
|
---|
1799 | ASMAtomicSubU32(&pIoCtxDst->cbTransferLeft, cbFill);
|
---|
1800 | }
|
---|
1801 | }
|
---|
1802 |
|
---|
1803 | if ( !pIoCtxDst->cbTransferLeft
|
---|
1804 | && !pIoCtxDst->cMetaTransfersPending
|
---|
1805 | && ASMAtomicCmpXchgBool(&pIoCtxDst->fComplete, true, false))
|
---|
1806 | {
|
---|
1807 | /* Write the full block to the virtual disk. */
|
---|
1808 | vdIoCtxChildReset(pIoCtxDst);
|
---|
1809 | rc = pImage->Backend->pfnAsyncWrite(pImage->pBackendData,
|
---|
1810 | uOffset - cbPreRead,
|
---|
1811 | cbPreRead + cbThisWrite + cbPostRead,
|
---|
1812 | pIoCtxDst,
|
---|
1813 | NULL, &cbPreRead, &cbPostRead, 0);
|
---|
1814 | Assert(rc != VERR_VD_BLOCK_FREE);
|
---|
1815 | Assert(cbPreRead == 0);
|
---|
1816 | Assert(cbPostRead == 0);
|
---|
1817 | }
|
---|
1818 | else
|
---|
1819 | {
|
---|
1820 | LogFlow(("cbTransferLeft=%u cMetaTransfersPending=%u fComplete=%RTbool\n",
|
---|
1821 | pIoCtxDst->cbTransferLeft, pIoCtxDst->cMetaTransfersPending,
|
---|
1822 | pIoCtxDst->fComplete));
|
---|
1823 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1824 | }
|
---|
1825 |
|
---|
1826 | return rc;
|
---|
1827 | #endif
|
---|
1828 | return VERR_NOT_IMPLEMENTED;
|
---|
1829 | }
|
---|
1830 |
|
---|
1831 | static int vdWriteHelperOptimizedCommitAsync(PVDIOCTX pIoCtx)
|
---|
1832 | {
|
---|
1833 | int rc = VINF_SUCCESS;
|
---|
1834 | PVDIMAGE pImage = pIoCtx->pImageStart;
|
---|
1835 | size_t cbPreRead = pIoCtx->Type.Child.cbPreRead;
|
---|
1836 | size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
|
---|
1837 | size_t cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
|
---|
1838 |
|
---|
1839 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
1840 | rc = pImage->Backend->pfnAsyncWrite(pImage->pBackendData,
|
---|
1841 | pIoCtx->uOffset - cbPreRead,
|
---|
1842 | cbPreRead + cbThisWrite + cbPostRead,
|
---|
1843 | pIoCtx, NULL, &cbPreRead, &cbPostRead, 0);
|
---|
1844 | Assert(rc != VERR_VD_BLOCK_FREE);
|
---|
1845 | Assert(rc == VERR_VD_NOT_ENOUGH_METADATA || cbPreRead == 0);
|
---|
1846 | Assert(rc == VERR_VD_NOT_ENOUGH_METADATA || cbPostRead == 0);
|
---|
1847 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
1848 | rc = VINF_SUCCESS;
|
---|
1849 | else if (rc == VERR_VD_IOCTX_HALT)
|
---|
1850 | {
|
---|
1851 | pIoCtx->fBlocked = true;
|
---|
1852 | rc = VINF_SUCCESS;
|
---|
1853 | }
|
---|
1854 |
|
---|
1855 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
1856 | return rc;
|
---|
1857 | }
|
---|
1858 |
|
---|
1859 | static int vdWriteHelperOptimizedCmpAndWriteAsync(PVDIOCTX pIoCtx)
|
---|
1860 | {
|
---|
1861 | int rc = VINF_SUCCESS;
|
---|
1862 | PVDIMAGE pImage = pIoCtx->pImageCur;
|
---|
1863 | size_t cbThisWrite = 0;
|
---|
1864 | size_t cbPreRead = pIoCtx->Type.Child.cbPreRead;
|
---|
1865 | size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
|
---|
1866 | size_t cbWriteCopy = pIoCtx->Type.Child.Write.Optimized.cbWriteCopy;
|
---|
1867 | size_t cbFill = pIoCtx->Type.Child.Write.Optimized.cbFill;
|
---|
1868 | size_t cbReadImage = pIoCtx->Type.Child.Write.Optimized.cbReadImage;
|
---|
1869 | PVDIOCTX pIoCtxParent = pIoCtx->pIoCtxParent;
|
---|
1870 |
|
---|
1871 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
1872 |
|
---|
1873 | AssertPtr(pIoCtxParent);
|
---|
1874 | Assert(!pIoCtxParent->pIoCtxParent);
|
---|
1875 | Assert(!pIoCtx->cbTransferLeft && !pIoCtx->cMetaTransfersPending);
|
---|
1876 |
|
---|
1877 | vdIoCtxChildReset(pIoCtx);
|
---|
1878 | cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
|
---|
1879 | RTSgBufAdvance(&pIoCtx->SgBuf, cbPreRead);
|
---|
1880 |
|
---|
1881 | /* Check if the write would modify anything in this block. */
|
---|
1882 | if (!RTSgBufCmp(&pIoCtx->SgBuf, &pIoCtxParent->SgBuf, cbThisWrite))
|
---|
1883 | {
|
---|
1884 | RTSGBUF SgBufSrcTmp;
|
---|
1885 |
|
---|
1886 | RTSgBufClone(&SgBufSrcTmp, &pIoCtxParent->SgBuf);
|
---|
1887 | RTSgBufAdvance(&SgBufSrcTmp, cbThisWrite);
|
---|
1888 | RTSgBufAdvance(&pIoCtx->SgBuf, cbThisWrite);
|
---|
1889 |
|
---|
1890 | if (!cbWriteCopy || !RTSgBufCmp(&pIoCtx->SgBuf, &SgBufSrcTmp, cbWriteCopy))
|
---|
1891 | {
|
---|
1892 | /* Block is completely unchanged, so no need to write anything. */
|
---|
1893 | LogFlowFunc(("Block didn't changed\n"));
|
---|
1894 | ASMAtomicWriteU32(&pIoCtx->cbTransferLeft, 0);
|
---|
1895 | RTSgBufAdvance(&pIoCtxParent->SgBuf, cbThisWrite);
|
---|
1896 | return VINF_VD_ASYNC_IO_FINISHED;
|
---|
1897 | }
|
---|
1898 | }
|
---|
1899 |
|
---|
1900 | /* Copy the data to the right place in the buffer. */
|
---|
1901 | RTSgBufReset(&pIoCtx->SgBuf);
|
---|
1902 | RTSgBufAdvance(&pIoCtx->SgBuf, cbPreRead);
|
---|
1903 | vdIoCtxCopy(pIoCtx, pIoCtxParent, cbThisWrite);
|
---|
1904 |
|
---|
1905 | /* Handle the data that goes after the write to fill the block. */
|
---|
1906 | if (cbPostRead)
|
---|
1907 | {
|
---|
1908 | /* Now assemble the remaining data. */
|
---|
1909 | if (cbWriteCopy)
|
---|
1910 | {
|
---|
1911 | /*
|
---|
1912 | * The S/G buffer of the parent needs to be cloned because
|
---|
1913 | * it is not allowed to modify the state.
|
---|
1914 | */
|
---|
1915 | RTSGBUF SgBufParentTmp;
|
---|
1916 |
|
---|
1917 | RTSgBufClone(&SgBufParentTmp, &pIoCtxParent->SgBuf);
|
---|
1918 | RTSgBufCopy(&pIoCtx->SgBuf, &SgBufParentTmp, cbWriteCopy);
|
---|
1919 | }
|
---|
1920 |
|
---|
1921 | /* Zero out the remainder of this block. Will never be visible, as this
|
---|
1922 | * is beyond the limit of the image. */
|
---|
1923 | if (cbFill)
|
---|
1924 | {
|
---|
1925 | RTSgBufAdvance(&pIoCtx->SgBuf, cbReadImage);
|
---|
1926 | vdIoCtxSet(pIoCtx, '\0', cbFill);
|
---|
1927 | }
|
---|
1928 | }
|
---|
1929 |
|
---|
1930 | /* Write the full block to the virtual disk. */
|
---|
1931 | RTSgBufReset(&pIoCtx->SgBuf);
|
---|
1932 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperOptimizedCommitAsync;
|
---|
1933 |
|
---|
1934 | return rc;
|
---|
1935 | }
|
---|
1936 |
|
---|
1937 | static int vdWriteHelperOptimizedPreReadAsync(PVDIOCTX pIoCtx)
|
---|
1938 | {
|
---|
1939 | int rc = VINF_SUCCESS;
|
---|
1940 |
|
---|
1941 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
1942 |
|
---|
1943 | if (pIoCtx->cbTransferLeft)
|
---|
1944 | rc = vdReadHelperAsync(pIoCtx);
|
---|
1945 |
|
---|
1946 | if ( RT_SUCCESS(rc)
|
---|
1947 | && ( pIoCtx->cbTransferLeft
|
---|
1948 | || pIoCtx->cMetaTransfersPending))
|
---|
1949 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1950 | else
|
---|
1951 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperOptimizedCmpAndWriteAsync;
|
---|
1952 |
|
---|
1953 | return rc;
|
---|
1954 | }
|
---|
1955 |
|
---|
1956 | /**
|
---|
1957 | * internal: write a complete block (only used for diff images), taking the
|
---|
1958 | * remaining data from parent images. This implementation optimizes out writes
|
---|
1959 | * that do not change the data relative to the state as of the parent images.
|
---|
1960 | * All backends which support differential/growing images support this - async version.
|
---|
1961 | */
|
---|
1962 | static int vdWriteHelperOptimizedAsync(PVDIOCTX pIoCtx)
|
---|
1963 | {
|
---|
1964 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
1965 | uint64_t uOffset = pIoCtx->Type.Child.uOffsetSaved;
|
---|
1966 | size_t cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
|
---|
1967 | size_t cbPreRead = pIoCtx->Type.Child.cbPreRead;
|
---|
1968 | size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
|
---|
1969 | size_t cbWrite = pIoCtx->Type.Child.cbWriteParent;
|
---|
1970 | size_t cbFill = 0;
|
---|
1971 | size_t cbWriteCopy = 0;
|
---|
1972 | size_t cbReadImage = 0;
|
---|
1973 |
|
---|
1974 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
1975 |
|
---|
1976 | AssertPtr(pIoCtx->pIoCtxParent);
|
---|
1977 | Assert(!pIoCtx->pIoCtxParent->pIoCtxParent);
|
---|
1978 |
|
---|
1979 | if (cbPostRead)
|
---|
1980 | {
|
---|
1981 | /* Figure out how much we cannot read from the image, because
|
---|
1982 | * the last block to write might exceed the nominal size of the
|
---|
1983 | * image for technical reasons. */
|
---|
1984 | if (uOffset + cbThisWrite + cbPostRead > pDisk->cbSize)
|
---|
1985 | cbFill = uOffset + cbThisWrite + cbPostRead - pDisk->cbSize;
|
---|
1986 |
|
---|
1987 | /* If we have data to be written, use that instead of reading
|
---|
1988 | * data from the image. */
|
---|
1989 | if (cbWrite > cbThisWrite)
|
---|
1990 | cbWriteCopy = RT_MIN(cbWrite - cbThisWrite, cbPostRead);
|
---|
1991 |
|
---|
1992 | /* The rest must be read from the image. */
|
---|
1993 | cbReadImage = cbPostRead - cbWriteCopy - cbFill;
|
---|
1994 | }
|
---|
1995 |
|
---|
1996 | pIoCtx->Type.Child.Write.Optimized.cbFill = cbFill;
|
---|
1997 | pIoCtx->Type.Child.Write.Optimized.cbWriteCopy = cbWriteCopy;
|
---|
1998 | pIoCtx->Type.Child.Write.Optimized.cbReadImage = cbReadImage;
|
---|
1999 |
|
---|
2000 | /* Read the entire data of the block so that we can compare whether it will
|
---|
2001 | * be modified by the write or not. */
|
---|
2002 | pIoCtx->cbTransferLeft = cbPreRead + cbThisWrite + cbPostRead - cbFill;
|
---|
2003 | pIoCtx->cbTransfer = pIoCtx->cbTransferLeft;
|
---|
2004 | pIoCtx->uOffset -= cbPreRead;
|
---|
2005 |
|
---|
2006 | /* Next step */
|
---|
2007 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperOptimizedPreReadAsync;
|
---|
2008 | return VINF_SUCCESS;
|
---|
2009 | }
|
---|
2010 |
|
---|
2011 | /**
|
---|
2012 | * internal: write buffer to the image, taking care of block boundaries and
|
---|
2013 | * write optimizations - async version.
|
---|
2014 | */
|
---|
2015 | static int vdWriteHelperAsync(PVDIOCTX pIoCtx)
|
---|
2016 | {
|
---|
2017 | int rc;
|
---|
2018 | size_t cbWrite = pIoCtx->cbTransfer;
|
---|
2019 | uint64_t uOffset = pIoCtx->uOffset;
|
---|
2020 | PVDIMAGE pImage = pIoCtx->pImageCur;
|
---|
2021 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
2022 | unsigned fWrite;
|
---|
2023 | size_t cbThisWrite;
|
---|
2024 | size_t cbPreRead, cbPostRead;
|
---|
2025 |
|
---|
2026 | rc = vdSetModifiedFlagAsync(pDisk, pIoCtx);
|
---|
2027 | if (RT_FAILURE(rc)) /* Includes I/O in progress. */
|
---|
2028 | return rc;
|
---|
2029 |
|
---|
2030 | /* Loop until all written. */
|
---|
2031 | do
|
---|
2032 | {
|
---|
2033 | /* Try to write the possibly partial block to the last opened image.
|
---|
2034 | * This works when the block is already allocated in this image or
|
---|
2035 | * if it is a full-block write (and allocation isn't suppressed below).
|
---|
2036 | * For image formats which don't support zero blocks, it's beneficial
|
---|
2037 | * to avoid unnecessarily allocating unchanged blocks. This prevents
|
---|
2038 | * unwanted expanding of images. VMDK is an example. */
|
---|
2039 | cbThisWrite = cbWrite;
|
---|
2040 | fWrite = (pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME)
|
---|
2041 | ? 0 : VD_WRITE_NO_ALLOC;
|
---|
2042 | rc = pImage->Backend->pfnAsyncWrite(pImage->pBackendData, uOffset,
|
---|
2043 | cbThisWrite, pIoCtx,
|
---|
2044 | &cbThisWrite, &cbPreRead,
|
---|
2045 | &cbPostRead, fWrite);
|
---|
2046 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
2047 | {
|
---|
2048 | /* Lock the disk .*/
|
---|
2049 | rc = vdIoCtxLockDisk(pDisk, pIoCtx);
|
---|
2050 | if (RT_SUCCESS(rc))
|
---|
2051 | {
|
---|
2052 | /*
|
---|
2053 | * Allocate segment and buffer in one go.
|
---|
2054 | * A bit hackish but avoids the need to allocate memory twice.
|
---|
2055 | */
|
---|
2056 | PRTSGBUF pTmp = (PRTSGBUF)RTMemAlloc(cbPreRead + cbThisWrite + cbPostRead + sizeof(RTSGSEG) + sizeof(RTSGBUF));
|
---|
2057 | AssertBreakStmt(VALID_PTR(pTmp), rc = VERR_NO_MEMORY);
|
---|
2058 | PRTSGSEG pSeg = (PRTSGSEG)(pTmp + 1);
|
---|
2059 |
|
---|
2060 | pSeg->pvSeg = pSeg + 1;
|
---|
2061 | pSeg->cbSeg = cbPreRead + cbThisWrite + cbPostRead;
|
---|
2062 | RTSgBufInit(pTmp, pSeg, 1);
|
---|
2063 |
|
---|
2064 | PVDIOCTX pIoCtxWrite = vdIoCtxChildAlloc(pDisk, VDIOCTXTXDIR_WRITE,
|
---|
2065 | uOffset, pSeg->cbSeg, pImage,
|
---|
2066 | pTmp,
|
---|
2067 | pIoCtx, cbThisWrite,
|
---|
2068 | cbWrite,
|
---|
2069 | pTmp,
|
---|
2070 | (pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME)
|
---|
2071 | ? vdWriteHelperStandardAsync
|
---|
2072 | : vdWriteHelperOptimizedAsync);
|
---|
2073 | if (!VALID_PTR(pIoCtxWrite))
|
---|
2074 | {
|
---|
2075 | RTMemTmpFree(pTmp);
|
---|
2076 | rc = VERR_NO_MEMORY;
|
---|
2077 | break;
|
---|
2078 | }
|
---|
2079 |
|
---|
2080 | LogFlowFunc(("Disk is growing because of pIoCtx=%#p pIoCtxWrite=%#p\n",
|
---|
2081 | pIoCtx, pIoCtxWrite));
|
---|
2082 |
|
---|
2083 | pIoCtxWrite->Type.Child.cbPreRead = cbPreRead;
|
---|
2084 | pIoCtxWrite->Type.Child.cbPostRead = cbPostRead;
|
---|
2085 |
|
---|
2086 | /* Process the write request */
|
---|
2087 | rc = vdIoCtxProcess(pIoCtxWrite);
|
---|
2088 |
|
---|
2089 | if (RT_FAILURE(rc) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
2090 | {
|
---|
2091 | vdIoCtxFree(pDisk, pIoCtxWrite);
|
---|
2092 | break;
|
---|
2093 | }
|
---|
2094 | else if ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
2095 | && ASMAtomicCmpXchgBool(&pIoCtxWrite->fComplete, true, false))
|
---|
2096 | {
|
---|
2097 | LogFlow(("Child write request completed\n"));
|
---|
2098 | Assert(pIoCtx->cbTransferLeft >= cbThisWrite);
|
---|
2099 | ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbThisWrite);
|
---|
2100 | vdIoCtxUnlockDisk(pDisk, pIoCtx, false /* fProcessDeferredReqs*/ );
|
---|
2101 | vdIoCtxFree(pDisk, pIoCtxWrite);
|
---|
2102 |
|
---|
2103 | rc = VINF_SUCCESS;
|
---|
2104 | }
|
---|
2105 | else
|
---|
2106 | {
|
---|
2107 | LogFlow(("Child write pending\n"));
|
---|
2108 | pIoCtx->fBlocked = true;
|
---|
2109 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
2110 | cbWrite -= cbThisWrite;
|
---|
2111 | uOffset += cbThisWrite;
|
---|
2112 | break;
|
---|
2113 | }
|
---|
2114 | }
|
---|
2115 | else
|
---|
2116 | {
|
---|
2117 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
2118 | break;
|
---|
2119 | }
|
---|
2120 | }
|
---|
2121 |
|
---|
2122 | if (rc == VERR_VD_IOCTX_HALT)
|
---|
2123 | {
|
---|
2124 | cbWrite -= cbThisWrite;
|
---|
2125 | uOffset += cbThisWrite;
|
---|
2126 | pIoCtx->fBlocked = true;
|
---|
2127 | break;
|
---|
2128 | }
|
---|
2129 | else if (rc == VERR_VD_NOT_ENOUGH_METADATA)
|
---|
2130 | break;
|
---|
2131 |
|
---|
2132 | cbWrite -= cbThisWrite;
|
---|
2133 | uOffset += cbThisWrite;
|
---|
2134 | } while (cbWrite != 0 && (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS));
|
---|
2135 |
|
---|
2136 | if ( rc == VERR_VD_ASYNC_IO_IN_PROGRESS
|
---|
2137 | || rc == VERR_VD_NOT_ENOUGH_METADATA
|
---|
2138 | || rc == VERR_VD_IOCTX_HALT)
|
---|
2139 | {
|
---|
2140 | /*
|
---|
2141 | * Tell the caller that we don't need to go back here because all
|
---|
2142 | * writes are initiated.
|
---|
2143 | */
|
---|
2144 | if (!cbWrite)
|
---|
2145 | rc = VINF_SUCCESS;
|
---|
2146 |
|
---|
2147 | pIoCtx->uOffset = uOffset;
|
---|
2148 | pIoCtx->cbTransfer = cbWrite;
|
---|
2149 | }
|
---|
2150 |
|
---|
2151 | return rc;
|
---|
2152 | }
|
---|
2153 |
|
---|
2154 | /**
|
---|
2155 | * Flush helper async version.
|
---|
2156 | */
|
---|
2157 | static int vdFlushHelperAsync(PVDIOCTX pIoCtx)
|
---|
2158 | {
|
---|
2159 | int rc = VINF_SUCCESS;
|
---|
2160 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
2161 | PVDIMAGE pImage = pIoCtx->pImageCur;
|
---|
2162 |
|
---|
2163 | rc = vdIoCtxLockDisk(pDisk, pIoCtx);
|
---|
2164 | if (RT_SUCCESS(rc))
|
---|
2165 | {
|
---|
2166 | vdResetModifiedFlag(pDisk);
|
---|
2167 | rc = pImage->Backend->pfnAsyncFlush(pImage->pBackendData, pIoCtx);
|
---|
2168 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
2169 | rc = VINF_SUCCESS;
|
---|
2170 | else if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
2171 | vdIoCtxUnlockDisk(pDisk, pIoCtx, true /* fProcessDeferredReqs */);
|
---|
2172 | }
|
---|
2173 |
|
---|
2174 | return rc;
|
---|
2175 | }
|
---|
2176 |
|
---|
2177 | /**
|
---|
2178 | * internal: scans plugin directory and loads the backends have been found.
|
---|
2179 | */
|
---|
2180 | static int vdLoadDynamicBackends()
|
---|
2181 | {
|
---|
2182 | #ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
2183 | int rc = VINF_SUCCESS;
|
---|
2184 | PRTDIR pPluginDir = NULL;
|
---|
2185 |
|
---|
2186 | /* Enumerate plugin backends. */
|
---|
2187 | char szPath[RTPATH_MAX];
|
---|
2188 | rc = RTPathAppPrivateArch(szPath, sizeof(szPath));
|
---|
2189 | if (RT_FAILURE(rc))
|
---|
2190 | return rc;
|
---|
2191 |
|
---|
2192 | /* To get all entries with VBoxHDD as prefix. */
|
---|
2193 | char *pszPluginFilter = RTPathJoinA(szPath, VBOX_HDDFORMAT_PLUGIN_PREFIX "*");
|
---|
2194 | if (!pszPluginFilter)
|
---|
2195 | return VERR_NO_STR_MEMORY;
|
---|
2196 |
|
---|
2197 | PRTDIRENTRYEX pPluginDirEntry = NULL;
|
---|
2198 | size_t cbPluginDirEntry = sizeof(RTDIRENTRYEX);
|
---|
2199 | /* The plugins are in the same directory as the other shared libs. */
|
---|
2200 | rc = RTDirOpenFiltered(&pPluginDir, pszPluginFilter, RTDIRFILTER_WINNT);
|
---|
2201 | if (RT_FAILURE(rc))
|
---|
2202 | {
|
---|
2203 | /* On Windows the above immediately signals that there are no
|
---|
2204 | * files matching, while on other platforms enumerating the
|
---|
2205 | * files below fails. Either way: no plugins. */
|
---|
2206 | goto out;
|
---|
2207 | }
|
---|
2208 |
|
---|
2209 | pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(sizeof(RTDIRENTRYEX));
|
---|
2210 | if (!pPluginDirEntry)
|
---|
2211 | {
|
---|
2212 | rc = VERR_NO_MEMORY;
|
---|
2213 | goto out;
|
---|
2214 | }
|
---|
2215 |
|
---|
2216 | while ((rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK)) != VERR_NO_MORE_FILES)
|
---|
2217 | {
|
---|
2218 | RTLDRMOD hPlugin = NIL_RTLDRMOD;
|
---|
2219 | PFNVBOXHDDFORMATLOAD pfnHDDFormatLoad = NULL;
|
---|
2220 | PVBOXHDDBACKEND pBackend = NULL;
|
---|
2221 | char *pszPluginPath = NULL;
|
---|
2222 |
|
---|
2223 | if (rc == VERR_BUFFER_OVERFLOW)
|
---|
2224 | {
|
---|
2225 | /* allocate new buffer. */
|
---|
2226 | RTMemFree(pPluginDirEntry);
|
---|
2227 | pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(cbPluginDirEntry);
|
---|
2228 | if (!pPluginDirEntry)
|
---|
2229 | {
|
---|
2230 | rc = VERR_NO_MEMORY;
|
---|
2231 | break;
|
---|
2232 | }
|
---|
2233 | /* Retry. */
|
---|
2234 | rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
|
---|
2235 | if (RT_FAILURE(rc))
|
---|
2236 | break;
|
---|
2237 | }
|
---|
2238 | else if (RT_FAILURE(rc))
|
---|
2239 | break;
|
---|
2240 |
|
---|
2241 | /* We got the new entry. */
|
---|
2242 | if (!RTFS_IS_FILE(pPluginDirEntry->Info.Attr.fMode))
|
---|
2243 | continue;
|
---|
2244 |
|
---|
2245 | /* Prepend the path to the libraries. */
|
---|
2246 | pszPluginPath = RTPathJoinA(szPath, pPluginDirEntry->szName);
|
---|
2247 | if (!pszPluginPath)
|
---|
2248 | {
|
---|
2249 | rc = VERR_NO_STR_MEMORY;
|
---|
2250 | break;
|
---|
2251 | }
|
---|
2252 |
|
---|
2253 | rc = SUPR3HardenedLdrLoadPlugIn(pszPluginPath, &hPlugin, NULL);
|
---|
2254 | if (RT_SUCCESS(rc))
|
---|
2255 | {
|
---|
2256 | rc = RTLdrGetSymbol(hPlugin, VBOX_HDDFORMAT_LOAD_NAME, (void**)&pfnHDDFormatLoad);
|
---|
2257 | if (RT_FAILURE(rc) || !pfnHDDFormatLoad)
|
---|
2258 | {
|
---|
2259 | LogFunc(("error resolving the entry point %s in plugin %s, rc=%Rrc, pfnHDDFormat=%#p\n", VBOX_HDDFORMAT_LOAD_NAME, pPluginDirEntry->szName, rc, pfnHDDFormatLoad));
|
---|
2260 | if (RT_SUCCESS(rc))
|
---|
2261 | rc = VERR_SYMBOL_NOT_FOUND;
|
---|
2262 | }
|
---|
2263 |
|
---|
2264 | if (RT_SUCCESS(rc))
|
---|
2265 | {
|
---|
2266 | /* Get the function table. */
|
---|
2267 | rc = pfnHDDFormatLoad(&pBackend);
|
---|
2268 | if (RT_SUCCESS(rc) && pBackend->cbSize == sizeof(VBOXHDDBACKEND))
|
---|
2269 | {
|
---|
2270 | pBackend->hPlugin = hPlugin;
|
---|
2271 | vdAddBackend(pBackend);
|
---|
2272 | }
|
---|
2273 | else
|
---|
2274 | LogFunc(("ignored plugin '%s': pBackend->cbSize=%d rc=%Rrc\n", pszPluginPath, pBackend->cbSize, rc));
|
---|
2275 | }
|
---|
2276 | else
|
---|
2277 | LogFunc(("ignored plugin '%s': rc=%Rrc\n", pszPluginPath, rc));
|
---|
2278 |
|
---|
2279 | if (RT_FAILURE(rc))
|
---|
2280 | RTLdrClose(hPlugin);
|
---|
2281 | }
|
---|
2282 | RTStrFree(pszPluginPath);
|
---|
2283 | }
|
---|
2284 | out:
|
---|
2285 | if (rc == VERR_NO_MORE_FILES)
|
---|
2286 | rc = VINF_SUCCESS;
|
---|
2287 | RTStrFree(pszPluginFilter);
|
---|
2288 | if (pPluginDirEntry)
|
---|
2289 | RTMemFree(pPluginDirEntry);
|
---|
2290 | if (pPluginDir)
|
---|
2291 | RTDirClose(pPluginDir);
|
---|
2292 | return rc;
|
---|
2293 | #else
|
---|
2294 | return VINF_SUCCESS;
|
---|
2295 | #endif
|
---|
2296 | }
|
---|
2297 |
|
---|
2298 | /**
|
---|
2299 | * internal: scans plugin directory and loads the cache backends have been found.
|
---|
2300 | */
|
---|
2301 | static int vdLoadDynamicCacheBackends()
|
---|
2302 | {
|
---|
2303 | #ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
2304 | int rc = VINF_SUCCESS;
|
---|
2305 | PRTDIR pPluginDir = NULL;
|
---|
2306 |
|
---|
2307 | /* Enumerate plugin backends. */
|
---|
2308 | char szPath[RTPATH_MAX];
|
---|
2309 | rc = RTPathAppPrivateArch(szPath, sizeof(szPath));
|
---|
2310 | if (RT_FAILURE(rc))
|
---|
2311 | return rc;
|
---|
2312 |
|
---|
2313 | /* To get all entries with VBoxHDD as prefix. */
|
---|
2314 | char *pszPluginFilter = RTPathJoinA(szPath, VD_CACHEFORMAT_PLUGIN_PREFIX "*");
|
---|
2315 | if (!pszPluginFilter)
|
---|
2316 | {
|
---|
2317 | rc = VERR_NO_STR_MEMORY;
|
---|
2318 | return rc;
|
---|
2319 | }
|
---|
2320 |
|
---|
2321 | PRTDIRENTRYEX pPluginDirEntry = NULL;
|
---|
2322 | size_t cbPluginDirEntry = sizeof(RTDIRENTRYEX);
|
---|
2323 | /* The plugins are in the same directory as the other shared libs. */
|
---|
2324 | rc = RTDirOpenFiltered(&pPluginDir, pszPluginFilter, RTDIRFILTER_WINNT);
|
---|
2325 | if (RT_FAILURE(rc))
|
---|
2326 | {
|
---|
2327 | /* On Windows the above immediately signals that there are no
|
---|
2328 | * files matching, while on other platforms enumerating the
|
---|
2329 | * files below fails. Either way: no plugins. */
|
---|
2330 | goto out;
|
---|
2331 | }
|
---|
2332 |
|
---|
2333 | pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(sizeof(RTDIRENTRYEX));
|
---|
2334 | if (!pPluginDirEntry)
|
---|
2335 | {
|
---|
2336 | rc = VERR_NO_MEMORY;
|
---|
2337 | goto out;
|
---|
2338 | }
|
---|
2339 |
|
---|
2340 | while ((rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK)) != VERR_NO_MORE_FILES)
|
---|
2341 | {
|
---|
2342 | RTLDRMOD hPlugin = NIL_RTLDRMOD;
|
---|
2343 | PFNVDCACHEFORMATLOAD pfnVDCacheLoad = NULL;
|
---|
2344 | PVDCACHEBACKEND pBackend = NULL;
|
---|
2345 | char *pszPluginPath = NULL;
|
---|
2346 |
|
---|
2347 | if (rc == VERR_BUFFER_OVERFLOW)
|
---|
2348 | {
|
---|
2349 | /* allocate new buffer. */
|
---|
2350 | RTMemFree(pPluginDirEntry);
|
---|
2351 | pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(cbPluginDirEntry);
|
---|
2352 | if (!pPluginDirEntry)
|
---|
2353 | {
|
---|
2354 | rc = VERR_NO_MEMORY;
|
---|
2355 | break;
|
---|
2356 | }
|
---|
2357 | /* Retry. */
|
---|
2358 | rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
|
---|
2359 | if (RT_FAILURE(rc))
|
---|
2360 | break;
|
---|
2361 | }
|
---|
2362 | else if (RT_FAILURE(rc))
|
---|
2363 | break;
|
---|
2364 |
|
---|
2365 | /* We got the new entry. */
|
---|
2366 | if (!RTFS_IS_FILE(pPluginDirEntry->Info.Attr.fMode))
|
---|
2367 | continue;
|
---|
2368 |
|
---|
2369 | /* Prepend the path to the libraries. */
|
---|
2370 | pszPluginPath = RTPathJoinA(szPath, pPluginDirEntry->szName);
|
---|
2371 | if (!pszPluginPath)
|
---|
2372 | {
|
---|
2373 | rc = VERR_NO_STR_MEMORY;
|
---|
2374 | break;
|
---|
2375 | }
|
---|
2376 |
|
---|
2377 | rc = SUPR3HardenedLdrLoadPlugIn(pszPluginPath, &hPlugin, NULL);
|
---|
2378 | if (RT_SUCCESS(rc))
|
---|
2379 | {
|
---|
2380 | rc = RTLdrGetSymbol(hPlugin, VD_CACHEFORMAT_LOAD_NAME, (void**)&pfnVDCacheLoad);
|
---|
2381 | if (RT_FAILURE(rc) || !pfnVDCacheLoad)
|
---|
2382 | {
|
---|
2383 | LogFunc(("error resolving the entry point %s in plugin %s, rc=%Rrc, pfnVDCacheLoad=%#p\n",
|
---|
2384 | VD_CACHEFORMAT_LOAD_NAME, pPluginDirEntry->szName, rc, pfnVDCacheLoad));
|
---|
2385 | if (RT_SUCCESS(rc))
|
---|
2386 | rc = VERR_SYMBOL_NOT_FOUND;
|
---|
2387 | }
|
---|
2388 |
|
---|
2389 | if (RT_SUCCESS(rc))
|
---|
2390 | {
|
---|
2391 | /* Get the function table. */
|
---|
2392 | rc = pfnVDCacheLoad(&pBackend);
|
---|
2393 | if (RT_SUCCESS(rc) && pBackend->cbSize == sizeof(VDCACHEBACKEND))
|
---|
2394 | {
|
---|
2395 | pBackend->hPlugin = hPlugin;
|
---|
2396 | vdAddCacheBackend(pBackend);
|
---|
2397 | }
|
---|
2398 | else
|
---|
2399 | LogFunc(("ignored plugin '%s': pBackend->cbSize=%d rc=%Rrc\n", pszPluginPath, pBackend->cbSize, rc));
|
---|
2400 | }
|
---|
2401 | else
|
---|
2402 | LogFunc(("ignored plugin '%s': rc=%Rrc\n", pszPluginPath, rc));
|
---|
2403 |
|
---|
2404 | if (RT_FAILURE(rc))
|
---|
2405 | RTLdrClose(hPlugin);
|
---|
2406 | }
|
---|
2407 | RTStrFree(pszPluginPath);
|
---|
2408 | }
|
---|
2409 | out:
|
---|
2410 | if (rc == VERR_NO_MORE_FILES)
|
---|
2411 | rc = VINF_SUCCESS;
|
---|
2412 | RTStrFree(pszPluginFilter);
|
---|
2413 | if (pPluginDirEntry)
|
---|
2414 | RTMemFree(pPluginDirEntry);
|
---|
2415 | if (pPluginDir)
|
---|
2416 | RTDirClose(pPluginDir);
|
---|
2417 | return rc;
|
---|
2418 | #else
|
---|
2419 | return VINF_SUCCESS;
|
---|
2420 | #endif
|
---|
2421 | }
|
---|
2422 |
|
---|
2423 | /**
|
---|
2424 | * VD async I/O interface open callback.
|
---|
2425 | */
|
---|
2426 | static int vdIOOpenFallback(void *pvUser, const char *pszLocation,
|
---|
2427 | uint32_t fOpen, PFNVDCOMPLETED pfnCompleted,
|
---|
2428 | void **ppStorage)
|
---|
2429 | {
|
---|
2430 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)RTMemAllocZ(sizeof(VDIIOFALLBACKSTORAGE));
|
---|
2431 |
|
---|
2432 | if (!pStorage)
|
---|
2433 | return VERR_NO_MEMORY;
|
---|
2434 |
|
---|
2435 | pStorage->pfnCompleted = pfnCompleted;
|
---|
2436 |
|
---|
2437 | /* Open the file. */
|
---|
2438 | int rc = RTFileOpen(&pStorage->File, pszLocation, fOpen);
|
---|
2439 | if (RT_SUCCESS(rc))
|
---|
2440 | {
|
---|
2441 | *ppStorage = pStorage;
|
---|
2442 | return VINF_SUCCESS;
|
---|
2443 | }
|
---|
2444 |
|
---|
2445 | RTMemFree(pStorage);
|
---|
2446 | return rc;
|
---|
2447 | }
|
---|
2448 |
|
---|
2449 | /**
|
---|
2450 | * VD async I/O interface close callback.
|
---|
2451 | */
|
---|
2452 | static int vdIOCloseFallback(void *pvUser, void *pvStorage)
|
---|
2453 | {
|
---|
2454 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
2455 |
|
---|
2456 | RTFileClose(pStorage->File);
|
---|
2457 | RTMemFree(pStorage);
|
---|
2458 | return VINF_SUCCESS;
|
---|
2459 | }
|
---|
2460 |
|
---|
2461 | static int vdIODeleteFallback(void *pvUser, const char *pcszFilename)
|
---|
2462 | {
|
---|
2463 | return RTFileDelete(pcszFilename);
|
---|
2464 | }
|
---|
2465 |
|
---|
2466 | static int vdIOMoveFallback(void *pvUser, const char *pcszSrc, const char *pcszDst, unsigned fMove)
|
---|
2467 | {
|
---|
2468 | return RTFileMove(pcszSrc, pcszDst, fMove);
|
---|
2469 | }
|
---|
2470 |
|
---|
2471 | static int vdIOGetFreeSpaceFallback(void *pvUser, const char *pcszFilename, int64_t *pcbFreeSpace)
|
---|
2472 | {
|
---|
2473 | return RTFsQuerySizes(pcszFilename, NULL, pcbFreeSpace, NULL, NULL);
|
---|
2474 | }
|
---|
2475 |
|
---|
2476 | static int vdIOGetModificationTimeFallback(void *pvUser, const char *pcszFilename, PRTTIMESPEC pModificationTime)
|
---|
2477 | {
|
---|
2478 | RTFSOBJINFO info;
|
---|
2479 | int rc = RTPathQueryInfo(pcszFilename, &info, RTFSOBJATTRADD_NOTHING);
|
---|
2480 | if (RT_SUCCESS(rc))
|
---|
2481 | *pModificationTime = info.ModificationTime;
|
---|
2482 | return rc;
|
---|
2483 | }
|
---|
2484 |
|
---|
2485 | /**
|
---|
2486 | * VD async I/O interface callback for retrieving the file size.
|
---|
2487 | */
|
---|
2488 | static int vdIOGetSizeFallback(void *pvUser, void *pvStorage, uint64_t *pcbSize)
|
---|
2489 | {
|
---|
2490 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
2491 |
|
---|
2492 | return RTFileGetSize(pStorage->File, pcbSize);
|
---|
2493 | }
|
---|
2494 |
|
---|
2495 | /**
|
---|
2496 | * VD async I/O interface callback for setting the file size.
|
---|
2497 | */
|
---|
2498 | static int vdIOSetSizeFallback(void *pvUser, void *pvStorage, uint64_t cbSize)
|
---|
2499 | {
|
---|
2500 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
2501 |
|
---|
2502 | return RTFileSetSize(pStorage->File, cbSize);
|
---|
2503 | }
|
---|
2504 |
|
---|
2505 | /**
|
---|
2506 | * VD async I/O interface callback for a synchronous write to the file.
|
---|
2507 | */
|
---|
2508 | static int vdIOWriteSyncFallback(void *pvUser, void *pvStorage, uint64_t uOffset,
|
---|
2509 | const void *pvBuf, size_t cbWrite, size_t *pcbWritten)
|
---|
2510 | {
|
---|
2511 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
2512 |
|
---|
2513 | return RTFileWriteAt(pStorage->File, uOffset, pvBuf, cbWrite, pcbWritten);
|
---|
2514 | }
|
---|
2515 |
|
---|
2516 | /**
|
---|
2517 | * VD async I/O interface callback for a synchronous read from the file.
|
---|
2518 | */
|
---|
2519 | static int vdIOReadSyncFallback(void *pvUser, void *pvStorage, uint64_t uOffset,
|
---|
2520 | void *pvBuf, size_t cbRead, size_t *pcbRead)
|
---|
2521 | {
|
---|
2522 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
2523 |
|
---|
2524 | return RTFileReadAt(pStorage->File, uOffset, pvBuf, cbRead, pcbRead);
|
---|
2525 | }
|
---|
2526 |
|
---|
2527 | /**
|
---|
2528 | * VD async I/O interface callback for a synchronous flush of the file data.
|
---|
2529 | */
|
---|
2530 | static int vdIOFlushSyncFallback(void *pvUser, void *pvStorage)
|
---|
2531 | {
|
---|
2532 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
2533 |
|
---|
2534 | return RTFileFlush(pStorage->File);
|
---|
2535 | }
|
---|
2536 |
|
---|
2537 | /**
|
---|
2538 | * VD async I/O interface callback for a asynchronous read from the file.
|
---|
2539 | */
|
---|
2540 | static int vdIOReadAsyncFallback(void *pvUser, void *pStorage, uint64_t uOffset,
|
---|
2541 | PCRTSGSEG paSegments, size_t cSegments,
|
---|
2542 | size_t cbRead, void *pvCompletion,
|
---|
2543 | void **ppTask)
|
---|
2544 | {
|
---|
2545 | return VERR_NOT_IMPLEMENTED;
|
---|
2546 | }
|
---|
2547 |
|
---|
2548 | /**
|
---|
2549 | * VD async I/O interface callback for a asynchronous write to the file.
|
---|
2550 | */
|
---|
2551 | static int vdIOWriteAsyncFallback(void *pvUser, void *pStorage, uint64_t uOffset,
|
---|
2552 | PCRTSGSEG paSegments, size_t cSegments,
|
---|
2553 | size_t cbWrite, void *pvCompletion,
|
---|
2554 | void **ppTask)
|
---|
2555 | {
|
---|
2556 | return VERR_NOT_IMPLEMENTED;
|
---|
2557 | }
|
---|
2558 |
|
---|
2559 | /**
|
---|
2560 | * VD async I/O interface callback for a asynchronous flush of the file data.
|
---|
2561 | */
|
---|
2562 | static int vdIOFlushAsyncFallback(void *pvUser, void *pStorage,
|
---|
2563 | void *pvCompletion, void **ppTask)
|
---|
2564 | {
|
---|
2565 | return VERR_NOT_IMPLEMENTED;
|
---|
2566 | }
|
---|
2567 |
|
---|
2568 | /**
|
---|
2569 | * Internal - Continues an I/O context after
|
---|
2570 | * it was halted because of an active transfer.
|
---|
2571 | */
|
---|
2572 | static int vdIoCtxContinue(PVDIOCTX pIoCtx, int rcReq)
|
---|
2573 | {
|
---|
2574 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
2575 | int rc = VINF_SUCCESS;
|
---|
2576 |
|
---|
2577 | if (RT_FAILURE(rcReq))
|
---|
2578 | ASMAtomicCmpXchgS32(&pIoCtx->rcReq, rcReq, VINF_SUCCESS);
|
---|
2579 |
|
---|
2580 | if (!pIoCtx->fBlocked)
|
---|
2581 | {
|
---|
2582 | /* Continue the transfer */
|
---|
2583 | rc = vdIoCtxProcess(pIoCtx);
|
---|
2584 |
|
---|
2585 | if ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
2586 | && ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
|
---|
2587 | {
|
---|
2588 | LogFlowFunc(("I/O context completed pIoCtx=%#p\n", pIoCtx));
|
---|
2589 | if (pIoCtx->pIoCtxParent)
|
---|
2590 | {
|
---|
2591 | PVDIOCTX pIoCtxParent = pIoCtx->pIoCtxParent;
|
---|
2592 |
|
---|
2593 | Assert(!pIoCtxParent->pIoCtxParent);
|
---|
2594 | if (RT_FAILURE(pIoCtx->rcReq))
|
---|
2595 | ASMAtomicCmpXchgS32(&pIoCtxParent->rcReq, pIoCtx->rcReq, VINF_SUCCESS);
|
---|
2596 |
|
---|
2597 | if (pIoCtx->enmTxDir == VDIOCTXTXDIR_WRITE)
|
---|
2598 | {
|
---|
2599 | LogFlowFunc(("I/O context transferred %u bytes for the parent pIoCtxParent=%p\n",
|
---|
2600 | pIoCtx->Type.Child.cbTransferParent, pIoCtxParent));
|
---|
2601 |
|
---|
2602 | /* Update the parent state. */
|
---|
2603 | Assert(pIoCtxParent->cbTransferLeft >= pIoCtx->Type.Child.cbTransferParent);
|
---|
2604 | ASMAtomicSubU32(&pIoCtxParent->cbTransferLeft, pIoCtx->Type.Child.cbTransferParent);
|
---|
2605 | }
|
---|
2606 | else
|
---|
2607 | Assert(pIoCtx->enmTxDir == VDIOCTXTXDIR_FLUSH);
|
---|
2608 |
|
---|
2609 | /*
|
---|
2610 | * A completed child write means that we finished growing the image.
|
---|
2611 | * We have to process any pending writes now.
|
---|
2612 | */
|
---|
2613 | vdIoCtxUnlockDisk(pDisk, pIoCtxParent, false /* fProcessDeferredReqs */);
|
---|
2614 |
|
---|
2615 | /* Unblock the parent */
|
---|
2616 | pIoCtxParent->fBlocked = false;
|
---|
2617 |
|
---|
2618 | rc = vdIoCtxProcess(pIoCtxParent);
|
---|
2619 |
|
---|
2620 | if ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
2621 | && ASMAtomicCmpXchgBool(&pIoCtxParent->fComplete, true, false))
|
---|
2622 | {
|
---|
2623 | RTCritSectLeave(&pDisk->CritSect);
|
---|
2624 | LogFlowFunc(("Parent I/O context completed pIoCtxParent=%#p rcReq=%Rrc\n", pIoCtxParent, pIoCtxParent->rcReq));
|
---|
2625 | pIoCtxParent->Type.Root.pfnComplete(pIoCtxParent->Type.Root.pvUser1,
|
---|
2626 | pIoCtxParent->Type.Root.pvUser2,
|
---|
2627 | pIoCtxParent->rcReq);
|
---|
2628 | vdThreadFinishWrite(pDisk);
|
---|
2629 | vdIoCtxFree(pDisk, pIoCtxParent);
|
---|
2630 | RTCritSectEnter(&pDisk->CritSect);
|
---|
2631 | }
|
---|
2632 |
|
---|
2633 | /* Process any pending writes if the current request didn't caused another growing. */
|
---|
2634 | RTCritSectEnter(&pDisk->CritSect);
|
---|
2635 |
|
---|
2636 | if ( !RTListIsEmpty(&pDisk->ListWriteLocked)
|
---|
2637 | && !vdIoCtxIsDiskLockOwner(pDisk, pIoCtx))
|
---|
2638 | {
|
---|
2639 | RTLISTNODE ListTmp;
|
---|
2640 |
|
---|
2641 | LogFlowFunc(("Before: pNext=%#p pPrev=%#p\n", pDisk->ListWriteLocked.pNext,
|
---|
2642 | pDisk->ListWriteLocked.pPrev));
|
---|
2643 |
|
---|
2644 | RTListMove(&ListTmp, &pDisk->ListWriteLocked);
|
---|
2645 |
|
---|
2646 | LogFlowFunc(("After: pNext=%#p pPrev=%#p\n", pDisk->ListWriteLocked.pNext,
|
---|
2647 | pDisk->ListWriteLocked.pPrev));
|
---|
2648 |
|
---|
2649 | RTCritSectLeave(&pDisk->CritSect);
|
---|
2650 |
|
---|
2651 | /* Process the list. */
|
---|
2652 | do
|
---|
2653 | {
|
---|
2654 | PVDIOCTXDEFERRED pDeferred = RTListGetFirst(&ListTmp, VDIOCTXDEFERRED, NodeDeferred);
|
---|
2655 | PVDIOCTX pIoCtxWait = pDeferred->pIoCtx;
|
---|
2656 |
|
---|
2657 | AssertPtr(pIoCtxWait);
|
---|
2658 |
|
---|
2659 | RTListNodeRemove(&pDeferred->NodeDeferred);
|
---|
2660 | RTMemFree(pDeferred);
|
---|
2661 |
|
---|
2662 | Assert(!pIoCtxWait->pIoCtxParent);
|
---|
2663 |
|
---|
2664 | pIoCtxWait->fBlocked = false;
|
---|
2665 | LogFlowFunc(("Processing waiting I/O context pIoCtxWait=%#p\n", pIoCtxWait));
|
---|
2666 |
|
---|
2667 | rc = vdIoCtxProcess(pIoCtxWait);
|
---|
2668 | if ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
2669 | && ASMAtomicCmpXchgBool(&pIoCtxWait->fComplete, true, false))
|
---|
2670 | {
|
---|
2671 | RTCritSectLeave(&pDisk->CritSect);
|
---|
2672 | LogFlowFunc(("Waiting I/O context completed pIoCtxWait=%#p\n", pIoCtxWait));
|
---|
2673 | vdThreadFinishWrite(pDisk);
|
---|
2674 | pIoCtxWait->Type.Root.pfnComplete(pIoCtxWait->Type.Root.pvUser1,
|
---|
2675 | pIoCtxWait->Type.Root.pvUser2,
|
---|
2676 | pIoCtxWait->rcReq);
|
---|
2677 | vdIoCtxFree(pDisk, pIoCtxWait);
|
---|
2678 | RTCritSectEnter(&pDisk->CritSect);
|
---|
2679 | }
|
---|
2680 | } while (!RTListIsEmpty(&ListTmp));
|
---|
2681 | }
|
---|
2682 | else
|
---|
2683 | RTCritSectLeave(&pDisk->CritSect);
|
---|
2684 | }
|
---|
2685 | else
|
---|
2686 | {
|
---|
2687 | if (pIoCtx->enmTxDir == VDIOCTXTXDIR_FLUSH)
|
---|
2688 | {
|
---|
2689 | vdIoCtxUnlockDisk(pDisk, pIoCtx, true /* fProcessDerredReqs */);
|
---|
2690 | vdThreadFinishWrite(pDisk);
|
---|
2691 | }
|
---|
2692 | else if (pIoCtx->enmTxDir == VDIOCTXTXDIR_WRITE)
|
---|
2693 | vdThreadFinishWrite(pDisk);
|
---|
2694 | else
|
---|
2695 | {
|
---|
2696 | Assert(pIoCtx->enmTxDir == VDIOCTXTXDIR_READ);
|
---|
2697 | vdThreadFinishRead(pDisk);
|
---|
2698 | }
|
---|
2699 |
|
---|
2700 | LogFlowFunc(("I/O context completed pIoCtx=%#p rcReq=%Rrc\n", pIoCtx, pIoCtx->rcReq));
|
---|
2701 | RTCritSectLeave(&pDisk->CritSect);
|
---|
2702 | pIoCtx->Type.Root.pfnComplete(pIoCtx->Type.Root.pvUser1,
|
---|
2703 | pIoCtx->Type.Root.pvUser2,
|
---|
2704 | pIoCtx->rcReq);
|
---|
2705 | RTCritSectEnter(&pDisk->CritSect);
|
---|
2706 | }
|
---|
2707 |
|
---|
2708 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
2709 | }
|
---|
2710 | }
|
---|
2711 |
|
---|
2712 | return VINF_SUCCESS;
|
---|
2713 | }
|
---|
2714 |
|
---|
2715 | /**
|
---|
2716 | * Internal - Called when user transfer completed.
|
---|
2717 | */
|
---|
2718 | static int vdUserXferCompleted(PVDIOSTORAGE pIoStorage, PVDIOCTX pIoCtx,
|
---|
2719 | PFNVDXFERCOMPLETED pfnComplete, void *pvUser,
|
---|
2720 | size_t cbTransfer, int rcReq)
|
---|
2721 | {
|
---|
2722 | int rc = VINF_SUCCESS;
|
---|
2723 | bool fIoCtxContinue = true;
|
---|
2724 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
2725 |
|
---|
2726 | LogFlowFunc(("pIoStorage=%#p pIoCtx=%#p pfnComplete=%#p pvUser=%#p cbTransfer=%zu rcReq=%Rrc\n",
|
---|
2727 | pIoStorage, pIoCtx, pfnComplete, pvUser, cbTransfer, rcReq));
|
---|
2728 |
|
---|
2729 | RTCritSectEnter(&pDisk->CritSect);
|
---|
2730 | Assert(pIoCtx->cbTransferLeft >= cbTransfer);
|
---|
2731 | ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbTransfer);
|
---|
2732 | ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
|
---|
2733 |
|
---|
2734 | if (pfnComplete)
|
---|
2735 | rc = pfnComplete(pIoStorage->pVDIo->pBackendData, pIoCtx, pvUser, rcReq);
|
---|
2736 |
|
---|
2737 | if (RT_SUCCESS(rc))
|
---|
2738 | rc = vdIoCtxContinue(pIoCtx, rcReq);
|
---|
2739 | else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
2740 | rc = VINF_SUCCESS;
|
---|
2741 |
|
---|
2742 | RTCritSectLeave(&pDisk->CritSect);
|
---|
2743 |
|
---|
2744 | return rc;
|
---|
2745 | }
|
---|
2746 |
|
---|
2747 | /**
|
---|
2748 | * Internal - Called when a meta transfer completed.
|
---|
2749 | */
|
---|
2750 | static int vdMetaXferCompleted(PVDIOSTORAGE pIoStorage, PFNVDXFERCOMPLETED pfnComplete, void *pvUser,
|
---|
2751 | PVDMETAXFER pMetaXfer, int rcReq)
|
---|
2752 | {
|
---|
2753 | PVBOXHDD pDisk = pIoStorage->pVDIo->pDisk;
|
---|
2754 | RTLISTNODE ListIoCtxWaiting;
|
---|
2755 | bool fFlush;
|
---|
2756 |
|
---|
2757 | LogFlowFunc(("pIoStorage=%#p pfnComplete=%#p pvUser=%#p pMetaXfer=%#p rcReq=%Rrc\n",
|
---|
2758 | pIoStorage, pfnComplete, pvUser, pMetaXfer, rcReq));
|
---|
2759 |
|
---|
2760 | RTCritSectEnter(&pDisk->CritSect);
|
---|
2761 | fFlush = VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_FLUSH;
|
---|
2762 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
|
---|
2763 |
|
---|
2764 | if (!fFlush)
|
---|
2765 | {
|
---|
2766 | RTListMove(&ListIoCtxWaiting, &pMetaXfer->ListIoCtxWaiting);
|
---|
2767 |
|
---|
2768 | if (RT_FAILURE(rcReq))
|
---|
2769 | {
|
---|
2770 | /* Remove from the AVL tree. */
|
---|
2771 | LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
|
---|
2772 | bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
|
---|
2773 | Assert(fRemoved);
|
---|
2774 | RTMemFree(pMetaXfer);
|
---|
2775 | }
|
---|
2776 | else
|
---|
2777 | {
|
---|
2778 | /* Increase the reference counter to make sure it doesn't go away before the last context is processed. */
|
---|
2779 | pMetaXfer->cRefs++;
|
---|
2780 | }
|
---|
2781 | }
|
---|
2782 | else
|
---|
2783 | RTListMove(&ListIoCtxWaiting, &pMetaXfer->ListIoCtxWaiting);
|
---|
2784 |
|
---|
2785 | /* Go through the waiting list and continue the I/O contexts. */
|
---|
2786 | while (!RTListIsEmpty(&ListIoCtxWaiting))
|
---|
2787 | {
|
---|
2788 | int rc = VINF_SUCCESS;
|
---|
2789 | bool fContinue = true;
|
---|
2790 | PVDIOCTXDEFERRED pDeferred = RTListGetFirst(&ListIoCtxWaiting, VDIOCTXDEFERRED, NodeDeferred);
|
---|
2791 | PVDIOCTX pIoCtx = pDeferred->pIoCtx;
|
---|
2792 | RTListNodeRemove(&pDeferred->NodeDeferred);
|
---|
2793 |
|
---|
2794 | RTMemFree(pDeferred);
|
---|
2795 | ASMAtomicDecU32(&pIoCtx->cMetaTransfersPending);
|
---|
2796 |
|
---|
2797 | if (pfnComplete)
|
---|
2798 | rc = pfnComplete(pIoStorage->pVDIo->pBackendData, pIoCtx, pvUser, rcReq);
|
---|
2799 |
|
---|
2800 | LogFlow(("Completion callback for I/O context %#p returned %Rrc\n", pIoCtx, rc));
|
---|
2801 |
|
---|
2802 | if (RT_SUCCESS(rc))
|
---|
2803 | {
|
---|
2804 | rc = vdIoCtxContinue(pIoCtx, rcReq);
|
---|
2805 | AssertRC(rc);
|
---|
2806 | }
|
---|
2807 | else
|
---|
2808 | Assert(rc == VERR_VD_ASYNC_IO_IN_PROGRESS);
|
---|
2809 | }
|
---|
2810 |
|
---|
2811 | /* Remove if not used anymore. */
|
---|
2812 | if (RT_SUCCESS(rcReq) && !fFlush)
|
---|
2813 | {
|
---|
2814 | pMetaXfer->cRefs--;
|
---|
2815 | if (!pMetaXfer->cRefs && RTListIsEmpty(&pMetaXfer->ListIoCtxWaiting))
|
---|
2816 | {
|
---|
2817 | /* Remove from the AVL tree. */
|
---|
2818 | LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
|
---|
2819 | bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
|
---|
2820 | Assert(fRemoved);
|
---|
2821 | RTMemFree(pMetaXfer);
|
---|
2822 | }
|
---|
2823 | }
|
---|
2824 | else if (fFlush)
|
---|
2825 | RTMemFree(pMetaXfer);
|
---|
2826 |
|
---|
2827 | RTCritSectLeave(&pDisk->CritSect);
|
---|
2828 |
|
---|
2829 | return VINF_SUCCESS;
|
---|
2830 | }
|
---|
2831 |
|
---|
2832 | static int vdIOIntReqCompleted(void *pvUser, int rcReq)
|
---|
2833 | {
|
---|
2834 | int rc = VINF_SUCCESS;
|
---|
2835 | PVDIOTASK pIoTask = (PVDIOTASK)pvUser;
|
---|
2836 | PVDIOSTORAGE pIoStorage = pIoTask->pIoStorage;
|
---|
2837 |
|
---|
2838 | LogFlowFunc(("Task completed pIoTask=%#p\n", pIoTask));
|
---|
2839 |
|
---|
2840 | if (!pIoTask->fMeta)
|
---|
2841 | rc = vdUserXferCompleted(pIoStorage, pIoTask->Type.User.pIoCtx,
|
---|
2842 | pIoTask->pfnComplete, pIoTask->pvUser,
|
---|
2843 | pIoTask->Type.User.cbTransfer, rcReq);
|
---|
2844 | else
|
---|
2845 | rc = vdMetaXferCompleted(pIoStorage, pIoTask->pfnComplete, pIoTask->pvUser,
|
---|
2846 | pIoTask->Type.Meta.pMetaXfer, rcReq);
|
---|
2847 |
|
---|
2848 | vdIoTaskFree(pIoStorage->pVDIo->pDisk, pIoTask);
|
---|
2849 |
|
---|
2850 | return rc;
|
---|
2851 | }
|
---|
2852 |
|
---|
2853 | /**
|
---|
2854 | * VD I/O interface callback for opening a file.
|
---|
2855 | */
|
---|
2856 | static int vdIOIntOpen(void *pvUser, const char *pszLocation,
|
---|
2857 | unsigned uOpenFlags, PPVDIOSTORAGE ppIoStorage)
|
---|
2858 | {
|
---|
2859 | int rc = VINF_SUCCESS;
|
---|
2860 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
2861 | PVDIOSTORAGE pIoStorage = (PVDIOSTORAGE)RTMemAllocZ(sizeof(VDIOSTORAGE));
|
---|
2862 |
|
---|
2863 | if (!pIoStorage)
|
---|
2864 | return VERR_NO_MEMORY;
|
---|
2865 |
|
---|
2866 | /* Create the AVl tree. */
|
---|
2867 | pIoStorage->pTreeMetaXfers = (PAVLRFOFFTREE)RTMemAllocZ(sizeof(AVLRFOFFTREE));
|
---|
2868 | if (pIoStorage->pTreeMetaXfers)
|
---|
2869 | {
|
---|
2870 | rc = pVDIo->pInterfaceIOCallbacks->pfnOpen(pVDIo->pInterfaceIO->pvUser,
|
---|
2871 | pszLocation, uOpenFlags,
|
---|
2872 | vdIOIntReqCompleted,
|
---|
2873 | &pIoStorage->pStorage);
|
---|
2874 | if (RT_SUCCESS(rc))
|
---|
2875 | {
|
---|
2876 | pIoStorage->pVDIo = pVDIo;
|
---|
2877 | *ppIoStorage = pIoStorage;
|
---|
2878 | return VINF_SUCCESS;
|
---|
2879 | }
|
---|
2880 |
|
---|
2881 | RTMemFree(pIoStorage->pTreeMetaXfers);
|
---|
2882 | }
|
---|
2883 | else
|
---|
2884 | rc = VERR_NO_MEMORY;
|
---|
2885 |
|
---|
2886 | RTMemFree(pIoStorage);
|
---|
2887 | return rc;
|
---|
2888 | }
|
---|
2889 |
|
---|
2890 | static int vdIOIntTreeMetaXferDestroy(PAVLRFOFFNODECORE pNode, void *pvUser)
|
---|
2891 | {
|
---|
2892 | AssertMsgFailed(("Tree should be empty at this point!\n"));
|
---|
2893 | return VINF_SUCCESS;
|
---|
2894 | }
|
---|
2895 |
|
---|
2896 | static int vdIOIntClose(void *pvUser, PVDIOSTORAGE pIoStorage)
|
---|
2897 | {
|
---|
2898 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
2899 |
|
---|
2900 | int rc = pVDIo->pInterfaceIOCallbacks->pfnClose(pVDIo->pInterfaceIO->pvUser,
|
---|
2901 | pIoStorage->pStorage);
|
---|
2902 | AssertRC(rc);
|
---|
2903 |
|
---|
2904 | RTAvlrFileOffsetDestroy(pIoStorage->pTreeMetaXfers, vdIOIntTreeMetaXferDestroy, NULL);
|
---|
2905 | RTMemFree(pIoStorage->pTreeMetaXfers);
|
---|
2906 | RTMemFree(pIoStorage);
|
---|
2907 | return VINF_SUCCESS;
|
---|
2908 | }
|
---|
2909 |
|
---|
2910 | static int vdIOIntDelete(void *pvUser, const char *pcszFilename)
|
---|
2911 | {
|
---|
2912 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
2913 | return pVDIo->pInterfaceIOCallbacks->pfnDelete(pVDIo->pInterfaceIO->pvUser,
|
---|
2914 | pcszFilename);
|
---|
2915 | }
|
---|
2916 |
|
---|
2917 | static int vdIOIntMove(void *pvUser, const char *pcszSrc, const char *pcszDst,
|
---|
2918 | unsigned fMove)
|
---|
2919 | {
|
---|
2920 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
2921 | return pVDIo->pInterfaceIOCallbacks->pfnMove(pVDIo->pInterfaceIO->pvUser,
|
---|
2922 | pcszSrc, pcszDst, fMove);
|
---|
2923 | }
|
---|
2924 |
|
---|
2925 | static int vdIOIntGetFreeSpace(void *pvUser, const char *pcszFilename,
|
---|
2926 | int64_t *pcbFreeSpace)
|
---|
2927 | {
|
---|
2928 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
2929 | return pVDIo->pInterfaceIOCallbacks->pfnGetFreeSpace(pVDIo->pInterfaceIO->pvUser,
|
---|
2930 | pcszFilename,
|
---|
2931 | pcbFreeSpace);
|
---|
2932 | }
|
---|
2933 |
|
---|
2934 | static int vdIOIntGetModificationTime(void *pvUser, const char *pcszFilename,
|
---|
2935 | PRTTIMESPEC pModificationTime)
|
---|
2936 | {
|
---|
2937 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
2938 | return pVDIo->pInterfaceIOCallbacks->pfnGetModificationTime(pVDIo->pInterfaceIO->pvUser,
|
---|
2939 | pcszFilename,
|
---|
2940 | pModificationTime);
|
---|
2941 | }
|
---|
2942 |
|
---|
2943 | static int vdIOIntGetSize(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
2944 | uint64_t *pcbSize)
|
---|
2945 | {
|
---|
2946 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
2947 | return pVDIo->pInterfaceIOCallbacks->pfnGetSize(pVDIo->pInterfaceIO->pvUser,
|
---|
2948 | pIoStorage->pStorage,
|
---|
2949 | pcbSize);
|
---|
2950 | }
|
---|
2951 |
|
---|
2952 | static int vdIOIntSetSize(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
2953 | uint64_t cbSize)
|
---|
2954 | {
|
---|
2955 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
2956 |
|
---|
2957 | return pVDIo->pInterfaceIOCallbacks->pfnSetSize(pVDIo->pInterfaceIO->pvUser,
|
---|
2958 | pIoStorage->pStorage,
|
---|
2959 | cbSize);
|
---|
2960 | }
|
---|
2961 |
|
---|
2962 | static int vdIOIntWriteSync(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
2963 | uint64_t uOffset, const void *pvBuf,
|
---|
2964 | size_t cbWrite, size_t *pcbWritten)
|
---|
2965 | {
|
---|
2966 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
2967 |
|
---|
2968 | return pVDIo->pInterfaceIOCallbacks->pfnWriteSync(pVDIo->pInterfaceIO->pvUser,
|
---|
2969 | pIoStorage->pStorage,
|
---|
2970 | uOffset, pvBuf, cbWrite,
|
---|
2971 | pcbWritten);
|
---|
2972 | }
|
---|
2973 |
|
---|
2974 | static int vdIOIntReadSync(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
2975 | uint64_t uOffset, void *pvBuf, size_t cbRead,
|
---|
2976 | size_t *pcbRead)
|
---|
2977 | {
|
---|
2978 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
2979 | return pVDIo->pInterfaceIOCallbacks->pfnReadSync(pVDIo->pInterfaceIO->pvUser,
|
---|
2980 | pIoStorage->pStorage,
|
---|
2981 | uOffset, pvBuf, cbRead,
|
---|
2982 | pcbRead);
|
---|
2983 | }
|
---|
2984 |
|
---|
2985 | static int vdIOIntFlushSync(void *pvUser, PVDIOSTORAGE pIoStorage)
|
---|
2986 | {
|
---|
2987 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
2988 | return pVDIo->pInterfaceIOCallbacks->pfnFlushSync(pVDIo->pInterfaceIO->pvUser,
|
---|
2989 | pIoStorage->pStorage);
|
---|
2990 | }
|
---|
2991 |
|
---|
2992 | static int vdIOIntReadUserAsync(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
2993 | uint64_t uOffset, PVDIOCTX pIoCtx,
|
---|
2994 | size_t cbRead)
|
---|
2995 | {
|
---|
2996 | int rc = VINF_SUCCESS;
|
---|
2997 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
2998 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
2999 |
|
---|
3000 | LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pIoCtx=%#p cbRead=%u\n",
|
---|
3001 | pvUser, pIoStorage, uOffset, pIoCtx, cbRead));
|
---|
3002 |
|
---|
3003 | VD_THREAD_IS_CRITSECT_OWNER(pDisk);
|
---|
3004 |
|
---|
3005 | Assert(cbRead > 0);
|
---|
3006 |
|
---|
3007 | /* Build the S/G array and spawn a new I/O task */
|
---|
3008 | while (cbRead)
|
---|
3009 | {
|
---|
3010 | RTSGSEG aSeg[VD_IO_TASK_SEGMENTS_MAX];
|
---|
3011 | unsigned cSegments = VD_IO_TASK_SEGMENTS_MAX;
|
---|
3012 | size_t cbTaskRead = 0;
|
---|
3013 |
|
---|
3014 | cbTaskRead = RTSgBufSegArrayCreate(&pIoCtx->SgBuf, aSeg, &cSegments, cbRead);
|
---|
3015 |
|
---|
3016 | Assert(cSegments > 0);
|
---|
3017 | Assert(cbTaskRead > 0);
|
---|
3018 | AssertMsg(cbTaskRead <= cbRead, ("Invalid number of bytes to read\n"));
|
---|
3019 |
|
---|
3020 | LogFlow(("Reading %u bytes into %u segments\n", cbTaskRead, cSegments));
|
---|
3021 |
|
---|
3022 | #ifdef RT_STRICT
|
---|
3023 | for (unsigned i = 0; i < cSegments; i++)
|
---|
3024 | AssertMsg(aSeg[i].pvSeg && !(aSeg[i].cbSeg % 512),
|
---|
3025 | ("Segment %u is invalid\n", i));
|
---|
3026 | #endif
|
---|
3027 |
|
---|
3028 | PVDIOTASK pIoTask = vdIoTaskUserAlloc(pIoStorage, NULL, NULL, pIoCtx, cbTaskRead);
|
---|
3029 |
|
---|
3030 | if (!pIoTask)
|
---|
3031 | return VERR_NO_MEMORY;
|
---|
3032 |
|
---|
3033 | ASMAtomicIncU32(&pIoCtx->cDataTransfersPending);
|
---|
3034 |
|
---|
3035 | void *pvTask;
|
---|
3036 | rc = pVDIo->pInterfaceIOCallbacks->pfnReadAsync(pVDIo->pInterfaceIO->pvUser,
|
---|
3037 | pIoStorage->pStorage,
|
---|
3038 | uOffset, aSeg, cSegments,
|
---|
3039 | cbTaskRead, pIoTask,
|
---|
3040 | &pvTask);
|
---|
3041 | if (RT_SUCCESS(rc))
|
---|
3042 | {
|
---|
3043 | AssertMsg(cbTaskRead <= pIoCtx->cbTransferLeft, ("Impossible!\n"));
|
---|
3044 | ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbTaskRead);
|
---|
3045 | ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
|
---|
3046 | vdIoTaskFree(pDisk, pIoTask);
|
---|
3047 | }
|
---|
3048 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
3049 | {
|
---|
3050 | ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
|
---|
3051 | vdIoTaskFree(pDisk, pIoTask);
|
---|
3052 | break;
|
---|
3053 | }
|
---|
3054 |
|
---|
3055 | uOffset += cbTaskRead;
|
---|
3056 | cbRead -= cbTaskRead;
|
---|
3057 | }
|
---|
3058 |
|
---|
3059 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
3060 | return rc;
|
---|
3061 | }
|
---|
3062 |
|
---|
3063 | static int vdIOIntWriteUserAsync(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
3064 | uint64_t uOffset, PVDIOCTX pIoCtx,
|
---|
3065 | size_t cbWrite,
|
---|
3066 | PFNVDXFERCOMPLETED pfnComplete,
|
---|
3067 | void *pvCompleteUser)
|
---|
3068 | {
|
---|
3069 | int rc = VINF_SUCCESS;
|
---|
3070 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
3071 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
3072 |
|
---|
3073 | LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pIoCtx=%#p cbWrite=%u\n",
|
---|
3074 | pvUser, pIoStorage, uOffset, pIoCtx, cbWrite));
|
---|
3075 |
|
---|
3076 | VD_THREAD_IS_CRITSECT_OWNER(pDisk);
|
---|
3077 |
|
---|
3078 | Assert(cbWrite > 0);
|
---|
3079 |
|
---|
3080 | /* Build the S/G array and spawn a new I/O task */
|
---|
3081 | while (cbWrite)
|
---|
3082 | {
|
---|
3083 | RTSGSEG aSeg[VD_IO_TASK_SEGMENTS_MAX];
|
---|
3084 | unsigned cSegments = VD_IO_TASK_SEGMENTS_MAX;
|
---|
3085 | size_t cbTaskWrite = 0;
|
---|
3086 |
|
---|
3087 | cbTaskWrite = RTSgBufSegArrayCreate(&pIoCtx->SgBuf, aSeg, &cSegments, cbWrite);
|
---|
3088 |
|
---|
3089 | Assert(cSegments > 0);
|
---|
3090 | Assert(cbTaskWrite > 0);
|
---|
3091 | AssertMsg(cbTaskWrite <= cbWrite, ("Invalid number of bytes to write\n"));
|
---|
3092 |
|
---|
3093 | LogFlow(("Writing %u bytes from %u segments\n", cbTaskWrite, cSegments));
|
---|
3094 |
|
---|
3095 | #ifdef DEBUG
|
---|
3096 | for (unsigned i = 0; i < cSegments; i++)
|
---|
3097 | AssertMsg(aSeg[i].pvSeg && !(aSeg[i].cbSeg % 512),
|
---|
3098 | ("Segment %u is invalid\n", i));
|
---|
3099 | #endif
|
---|
3100 |
|
---|
3101 | PVDIOTASK pIoTask = vdIoTaskUserAlloc(pIoStorage, pfnComplete, pvCompleteUser, pIoCtx, cbTaskWrite);
|
---|
3102 |
|
---|
3103 | if (!pIoTask)
|
---|
3104 | return VERR_NO_MEMORY;
|
---|
3105 |
|
---|
3106 | ASMAtomicIncU32(&pIoCtx->cDataTransfersPending);
|
---|
3107 |
|
---|
3108 | void *pvTask;
|
---|
3109 | rc = pVDIo->pInterfaceIOCallbacks->pfnWriteAsync(pVDIo->pInterfaceIO->pvUser,
|
---|
3110 | pIoStorage->pStorage,
|
---|
3111 | uOffset, aSeg, cSegments,
|
---|
3112 | cbTaskWrite, pIoTask,
|
---|
3113 | &pvTask);
|
---|
3114 | if (RT_SUCCESS(rc))
|
---|
3115 | {
|
---|
3116 | AssertMsg(cbTaskWrite <= pIoCtx->cbTransferLeft, ("Impossible!\n"));
|
---|
3117 | ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbTaskWrite);
|
---|
3118 | ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
|
---|
3119 | vdIoTaskFree(pDisk, pIoTask);
|
---|
3120 | }
|
---|
3121 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
3122 | {
|
---|
3123 | ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
|
---|
3124 | vdIoTaskFree(pDisk, pIoTask);
|
---|
3125 | break;
|
---|
3126 | }
|
---|
3127 |
|
---|
3128 | uOffset += cbTaskWrite;
|
---|
3129 | cbWrite -= cbTaskWrite;
|
---|
3130 | }
|
---|
3131 |
|
---|
3132 | return rc;
|
---|
3133 | }
|
---|
3134 |
|
---|
3135 | static int vdIOIntReadMetaAsync(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
3136 | uint64_t uOffset, void *pvBuf,
|
---|
3137 | size_t cbRead, PVDIOCTX pIoCtx,
|
---|
3138 | PPVDMETAXFER ppMetaXfer,
|
---|
3139 | PFNVDXFERCOMPLETED pfnComplete,
|
---|
3140 | void *pvCompleteUser)
|
---|
3141 | {
|
---|
3142 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
3143 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
3144 | int rc = VINF_SUCCESS;
|
---|
3145 | RTSGSEG Seg;
|
---|
3146 | PVDIOTASK pIoTask;
|
---|
3147 | PVDMETAXFER pMetaXfer = NULL;
|
---|
3148 | void *pvTask = NULL;
|
---|
3149 |
|
---|
3150 | LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pvBuf=%#p cbRead=%u\n",
|
---|
3151 | pvUser, pIoStorage, uOffset, pvBuf, cbRead));
|
---|
3152 |
|
---|
3153 | VD_THREAD_IS_CRITSECT_OWNER(pDisk);
|
---|
3154 |
|
---|
3155 | pMetaXfer = (PVDMETAXFER)RTAvlrFileOffsetGet(pIoStorage->pTreeMetaXfers, uOffset);
|
---|
3156 | if (!pMetaXfer)
|
---|
3157 | {
|
---|
3158 | #ifdef RT_STRICT
|
---|
3159 | pMetaXfer = (PVDMETAXFER)RTAvlrFileOffsetGetBestFit(pIoStorage->pTreeMetaXfers, uOffset, false /* fAbove */);
|
---|
3160 | AssertMsg(!pMetaXfer || (pMetaXfer->Core.Key + (RTFOFF)pMetaXfer->cbMeta <= (RTFOFF)uOffset),
|
---|
3161 | ("Overlapping meta transfers!\n"));
|
---|
3162 | #endif
|
---|
3163 |
|
---|
3164 | /* Allocate a new meta transfer. */
|
---|
3165 | pMetaXfer = vdMetaXferAlloc(pIoStorage, uOffset, cbRead);
|
---|
3166 | if (!pMetaXfer)
|
---|
3167 | return VERR_NO_MEMORY;
|
---|
3168 |
|
---|
3169 | pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvCompleteUser, pMetaXfer);
|
---|
3170 | if (!pIoTask)
|
---|
3171 | {
|
---|
3172 | RTMemFree(pMetaXfer);
|
---|
3173 | return VERR_NO_MEMORY;
|
---|
3174 | }
|
---|
3175 |
|
---|
3176 | Seg.cbSeg = cbRead;
|
---|
3177 | Seg.pvSeg = pMetaXfer->abData;
|
---|
3178 |
|
---|
3179 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_READ);
|
---|
3180 | rc = pVDIo->pInterfaceIOCallbacks->pfnReadAsync(pVDIo->pInterfaceIO->pvUser,
|
---|
3181 | pIoStorage->pStorage,
|
---|
3182 | uOffset, &Seg, 1,
|
---|
3183 | cbRead, pIoTask,
|
---|
3184 | &pvTask);
|
---|
3185 |
|
---|
3186 | if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
3187 | {
|
---|
3188 | bool fInserted = RTAvlrFileOffsetInsert(pIoStorage->pTreeMetaXfers, &pMetaXfer->Core);
|
---|
3189 | Assert(fInserted);
|
---|
3190 | }
|
---|
3191 | else
|
---|
3192 | RTMemFree(pMetaXfer);
|
---|
3193 |
|
---|
3194 | if (RT_SUCCESS(rc))
|
---|
3195 | {
|
---|
3196 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
|
---|
3197 | vdIoTaskFree(pDisk, pIoTask);
|
---|
3198 | }
|
---|
3199 | else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS && !pfnComplete)
|
---|
3200 | rc = VERR_VD_NOT_ENOUGH_METADATA;
|
---|
3201 | }
|
---|
3202 |
|
---|
3203 | Assert(VALID_PTR(pMetaXfer) || RT_FAILURE(rc));
|
---|
3204 |
|
---|
3205 | if (RT_SUCCESS(rc) || rc == VERR_VD_NOT_ENOUGH_METADATA || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
3206 | {
|
---|
3207 | /* If it is pending add the request to the list. */
|
---|
3208 | if (VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_READ)
|
---|
3209 | {
|
---|
3210 | PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
|
---|
3211 | AssertPtr(pDeferred);
|
---|
3212 |
|
---|
3213 | RTListInit(&pDeferred->NodeDeferred);
|
---|
3214 | pDeferred->pIoCtx = pIoCtx;
|
---|
3215 |
|
---|
3216 | ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
|
---|
3217 | RTListAppend(&pMetaXfer->ListIoCtxWaiting, &pDeferred->NodeDeferred);
|
---|
3218 | rc = VERR_VD_NOT_ENOUGH_METADATA;
|
---|
3219 | }
|
---|
3220 | else
|
---|
3221 | {
|
---|
3222 | /* Transfer the data. */
|
---|
3223 | pMetaXfer->cRefs++;
|
---|
3224 | Assert(pMetaXfer->cbMeta >= cbRead);
|
---|
3225 | Assert(pMetaXfer->Core.Key == (RTFOFF)uOffset);
|
---|
3226 | memcpy(pvBuf, pMetaXfer->abData, cbRead);
|
---|
3227 | *ppMetaXfer = pMetaXfer;
|
---|
3228 | }
|
---|
3229 | }
|
---|
3230 |
|
---|
3231 | return rc;
|
---|
3232 | }
|
---|
3233 |
|
---|
3234 | static int vdIOIntWriteMetaAsync(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
3235 | uint64_t uOffset, void *pvBuf,
|
---|
3236 | size_t cbWrite, PVDIOCTX pIoCtx,
|
---|
3237 | PFNVDXFERCOMPLETED pfnComplete,
|
---|
3238 | void *pvCompleteUser)
|
---|
3239 | {
|
---|
3240 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
3241 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
3242 | int rc = VINF_SUCCESS;
|
---|
3243 | RTSGSEG Seg;
|
---|
3244 | PVDIOTASK pIoTask;
|
---|
3245 | PVDMETAXFER pMetaXfer = NULL;
|
---|
3246 | bool fInTree = false;
|
---|
3247 | void *pvTask = NULL;
|
---|
3248 |
|
---|
3249 | LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pvBuf=%#p cbWrite=%u\n",
|
---|
3250 | pvUser, pIoStorage, uOffset, pvBuf, cbWrite));
|
---|
3251 |
|
---|
3252 | VD_THREAD_IS_CRITSECT_OWNER(pDisk);
|
---|
3253 |
|
---|
3254 | pMetaXfer = (PVDMETAXFER)RTAvlrFileOffsetGet(pIoStorage->pTreeMetaXfers, uOffset);
|
---|
3255 | if (!pMetaXfer)
|
---|
3256 | {
|
---|
3257 | /* Allocate a new meta transfer. */
|
---|
3258 | pMetaXfer = vdMetaXferAlloc(pIoStorage, uOffset, cbWrite);
|
---|
3259 | if (!pMetaXfer)
|
---|
3260 | return VERR_NO_MEMORY;
|
---|
3261 | }
|
---|
3262 | else
|
---|
3263 | {
|
---|
3264 | Assert(pMetaXfer->cbMeta >= cbWrite);
|
---|
3265 | Assert(pMetaXfer->Core.Key == (RTFOFF)uOffset);
|
---|
3266 | fInTree = true;
|
---|
3267 | }
|
---|
3268 |
|
---|
3269 | Assert(VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE);
|
---|
3270 |
|
---|
3271 | pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvCompleteUser, pMetaXfer);
|
---|
3272 | if (!pIoTask)
|
---|
3273 | {
|
---|
3274 | RTMemFree(pMetaXfer);
|
---|
3275 | return VERR_NO_MEMORY;
|
---|
3276 | }
|
---|
3277 |
|
---|
3278 | memcpy(pMetaXfer->abData, pvBuf, cbWrite);
|
---|
3279 | Seg.cbSeg = cbWrite;
|
---|
3280 | Seg.pvSeg = pMetaXfer->abData;
|
---|
3281 |
|
---|
3282 | ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
|
---|
3283 |
|
---|
3284 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_WRITE);
|
---|
3285 | rc = pVDIo->pInterfaceIOCallbacks->pfnWriteAsync(pVDIo->pInterfaceIO->pvUser,
|
---|
3286 | pIoStorage->pStorage,
|
---|
3287 | uOffset, &Seg, 1,
|
---|
3288 | cbWrite, pIoTask,
|
---|
3289 | &pvTask);
|
---|
3290 | if (RT_SUCCESS(rc))
|
---|
3291 | {
|
---|
3292 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
|
---|
3293 | ASMAtomicDecU32(&pIoCtx->cMetaTransfersPending);
|
---|
3294 | vdIoTaskFree(pDisk, pIoTask);
|
---|
3295 | if (fInTree && !pMetaXfer->cRefs)
|
---|
3296 | {
|
---|
3297 | LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
|
---|
3298 | bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
|
---|
3299 | AssertMsg(fRemoved, ("Metadata transfer wasn't removed\n"));
|
---|
3300 | RTMemFree(pMetaXfer);
|
---|
3301 | pMetaXfer = NULL;
|
---|
3302 | }
|
---|
3303 | }
|
---|
3304 | else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
3305 | {
|
---|
3306 | PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
|
---|
3307 | AssertPtr(pDeferred);
|
---|
3308 |
|
---|
3309 | RTListInit(&pDeferred->NodeDeferred);
|
---|
3310 | pDeferred->pIoCtx = pIoCtx;
|
---|
3311 |
|
---|
3312 | if (!fInTree)
|
---|
3313 | {
|
---|
3314 | bool fInserted = RTAvlrFileOffsetInsert(pIoStorage->pTreeMetaXfers, &pMetaXfer->Core);
|
---|
3315 | Assert(fInserted);
|
---|
3316 | }
|
---|
3317 |
|
---|
3318 | RTListAppend(&pMetaXfer->ListIoCtxWaiting, &pDeferred->NodeDeferred);
|
---|
3319 | }
|
---|
3320 | else
|
---|
3321 | {
|
---|
3322 | RTMemFree(pMetaXfer);
|
---|
3323 | pMetaXfer = NULL;
|
---|
3324 | }
|
---|
3325 |
|
---|
3326 | return rc;
|
---|
3327 | }
|
---|
3328 |
|
---|
3329 | static void vdIOIntMetaXferRelease(void *pvUser, PVDMETAXFER pMetaXfer)
|
---|
3330 | {
|
---|
3331 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
3332 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
3333 | PVDIOSTORAGE pIoStorage = pMetaXfer->pIoStorage;
|
---|
3334 |
|
---|
3335 | VD_THREAD_IS_CRITSECT_OWNER(pDisk);
|
---|
3336 |
|
---|
3337 | Assert( VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE
|
---|
3338 | || VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_WRITE);
|
---|
3339 | Assert(pMetaXfer->cRefs > 0);
|
---|
3340 |
|
---|
3341 | pMetaXfer->cRefs--;
|
---|
3342 | if ( !pMetaXfer->cRefs
|
---|
3343 | && RTListIsEmpty(&pMetaXfer->ListIoCtxWaiting)
|
---|
3344 | && VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE)
|
---|
3345 | {
|
---|
3346 | /* Free the meta data entry. */
|
---|
3347 | LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
|
---|
3348 | bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
|
---|
3349 | AssertMsg(fRemoved, ("Metadata transfer wasn't removed\n"));
|
---|
3350 |
|
---|
3351 | RTMemFree(pMetaXfer);
|
---|
3352 | }
|
---|
3353 | }
|
---|
3354 |
|
---|
3355 | static int vdIOIntFlushAsync(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
3356 | PVDIOCTX pIoCtx, PFNVDXFERCOMPLETED pfnComplete,
|
---|
3357 | void *pvCompleteUser)
|
---|
3358 | {
|
---|
3359 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
3360 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
3361 | int rc = VINF_SUCCESS;
|
---|
3362 | PVDIOTASK pIoTask;
|
---|
3363 | PVDMETAXFER pMetaXfer = NULL;
|
---|
3364 | void *pvTask = NULL;
|
---|
3365 |
|
---|
3366 | VD_THREAD_IS_CRITSECT_OWNER(pDisk);
|
---|
3367 |
|
---|
3368 | LogFlowFunc(("pvUser=%#p pIoStorage=%#p pIoCtx=%#p\n",
|
---|
3369 | pvUser, pIoStorage, pIoCtx));
|
---|
3370 |
|
---|
3371 | /* Allocate a new meta transfer. */
|
---|
3372 | pMetaXfer = vdMetaXferAlloc(pIoStorage, 0, 0);
|
---|
3373 | if (!pMetaXfer)
|
---|
3374 | return VERR_NO_MEMORY;
|
---|
3375 |
|
---|
3376 | pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvUser, pMetaXfer);
|
---|
3377 | if (!pIoTask)
|
---|
3378 | {
|
---|
3379 | RTMemFree(pMetaXfer);
|
---|
3380 | return VERR_NO_MEMORY;
|
---|
3381 | }
|
---|
3382 |
|
---|
3383 | ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
|
---|
3384 |
|
---|
3385 | PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
|
---|
3386 | AssertPtr(pDeferred);
|
---|
3387 |
|
---|
3388 | RTListInit(&pDeferred->NodeDeferred);
|
---|
3389 | pDeferred->pIoCtx = pIoCtx;
|
---|
3390 |
|
---|
3391 | RTListAppend(&pMetaXfer->ListIoCtxWaiting, &pDeferred->NodeDeferred);
|
---|
3392 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_FLUSH);
|
---|
3393 | rc = pVDIo->pInterfaceIOCallbacks->pfnFlushAsync(pVDIo->pInterfaceIO->pvUser,
|
---|
3394 | pIoStorage->pStorage,
|
---|
3395 | pIoTask, &pvTask);
|
---|
3396 | if (RT_SUCCESS(rc))
|
---|
3397 | {
|
---|
3398 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
|
---|
3399 | ASMAtomicDecU32(&pIoCtx->cMetaTransfersPending);
|
---|
3400 | vdIoTaskFree(pDisk, pIoTask);
|
---|
3401 | RTMemFree(pDeferred);
|
---|
3402 | RTMemFree(pMetaXfer);
|
---|
3403 | }
|
---|
3404 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
3405 | RTMemFree(pMetaXfer);
|
---|
3406 |
|
---|
3407 | return rc;
|
---|
3408 | }
|
---|
3409 |
|
---|
3410 | static size_t vdIOIntIoCtxCopyTo(void *pvUser, PVDIOCTX pIoCtx,
|
---|
3411 | void *pvBuf, size_t cbBuf)
|
---|
3412 | {
|
---|
3413 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
3414 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
3415 | size_t cbCopied = 0;
|
---|
3416 |
|
---|
3417 | VD_THREAD_IS_CRITSECT_OWNER(pDisk);
|
---|
3418 |
|
---|
3419 | cbCopied = vdIoCtxCopyTo(pIoCtx, (uint8_t *)pvBuf, cbBuf);
|
---|
3420 | Assert(cbCopied == cbBuf);
|
---|
3421 |
|
---|
3422 | ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbCopied);
|
---|
3423 |
|
---|
3424 | return cbCopied;
|
---|
3425 | }
|
---|
3426 |
|
---|
3427 | static size_t vdIOIntIoCtxCopyFrom(void *pvUser, PVDIOCTX pIoCtx,
|
---|
3428 | void *pvBuf, size_t cbBuf)
|
---|
3429 | {
|
---|
3430 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
3431 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
3432 | size_t cbCopied = 0;
|
---|
3433 |
|
---|
3434 | VD_THREAD_IS_CRITSECT_OWNER(pDisk);
|
---|
3435 |
|
---|
3436 | cbCopied = vdIoCtxCopyFrom(pIoCtx, (uint8_t *)pvBuf, cbBuf);
|
---|
3437 | Assert(cbCopied == cbBuf);
|
---|
3438 |
|
---|
3439 | ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbCopied);
|
---|
3440 |
|
---|
3441 | return cbCopied;
|
---|
3442 | }
|
---|
3443 |
|
---|
3444 | static size_t vdIOIntIoCtxSet(void *pvUser, PVDIOCTX pIoCtx, int ch, size_t cb)
|
---|
3445 | {
|
---|
3446 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
3447 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
3448 | size_t cbSet = 0;
|
---|
3449 |
|
---|
3450 | VD_THREAD_IS_CRITSECT_OWNER(pDisk);
|
---|
3451 |
|
---|
3452 | cbSet = vdIoCtxSet(pIoCtx, ch, cb);
|
---|
3453 | Assert(cbSet == cb);
|
---|
3454 |
|
---|
3455 | ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbSet);
|
---|
3456 |
|
---|
3457 | return cbSet;
|
---|
3458 | }
|
---|
3459 |
|
---|
3460 | static size_t vdIOIntIoCtxSegArrayCreate(void *pvUser, PVDIOCTX pIoCtx,
|
---|
3461 | PRTSGSEG paSeg, unsigned *pcSeg,
|
---|
3462 | size_t cbData)
|
---|
3463 | {
|
---|
3464 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
3465 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
3466 | size_t cbCreated = 0;
|
---|
3467 |
|
---|
3468 | VD_THREAD_IS_CRITSECT_OWNER(pDisk);
|
---|
3469 |
|
---|
3470 | cbCreated = RTSgBufSegArrayCreate(&pIoCtx->SgBuf, paSeg, pcSeg, cbData);
|
---|
3471 | Assert(!paSeg || cbData == cbCreated);
|
---|
3472 |
|
---|
3473 | return cbCreated;
|
---|
3474 | }
|
---|
3475 |
|
---|
3476 | static void vdIOIntIoCtxCompleted(void *pvUser, PVDIOCTX pIoCtx, int rcReq,
|
---|
3477 | size_t cbCompleted)
|
---|
3478 | {
|
---|
3479 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
3480 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
3481 |
|
---|
3482 | /*
|
---|
3483 | * Grab the disk critical section to avoid races with other threads which
|
---|
3484 | * might still modify the I/O context.
|
---|
3485 | * Example is that iSCSI is doing an asynchronous write but calls us already
|
---|
3486 | * while the other thread is still hanging in vdWriteHelperAsync and couldn't update
|
---|
3487 | * the fBlocked state yet.
|
---|
3488 | * It can overwrite the state to true before we call vdIoCtxContinue and the
|
---|
3489 | * the request would hang indefinite.
|
---|
3490 | */
|
---|
3491 | int rc = RTCritSectEnter(&pDisk->CritSect);
|
---|
3492 | AssertRC(rc);
|
---|
3493 |
|
---|
3494 | /* Continue */
|
---|
3495 | pIoCtx->fBlocked = false;
|
---|
3496 | ASMAtomicSubU32(&pIoCtx->cbTransferLeft, cbCompleted);
|
---|
3497 |
|
---|
3498 | /* Clear the pointer to next transfer function in case we have nothing to transfer anymore.
|
---|
3499 | * @todo: Find a better way to prevent vdIoCtxContinue from calling the read/write helper again. */
|
---|
3500 | if (!pIoCtx->cbTransferLeft)
|
---|
3501 | pIoCtx->pfnIoCtxTransfer = NULL;
|
---|
3502 |
|
---|
3503 | vdIoCtxContinue(pIoCtx, rcReq);
|
---|
3504 |
|
---|
3505 | rc = RTCritSectLeave(&pDisk->CritSect);
|
---|
3506 | AssertRC(rc);
|
---|
3507 | }
|
---|
3508 |
|
---|
3509 | /**
|
---|
3510 | * VD I/O interface callback for opening a file (limited version for VDGetFormat).
|
---|
3511 | */
|
---|
3512 | static int vdIOIntOpenLimited(void *pvUser, const char *pszLocation,
|
---|
3513 | uint32_t fOpen, PPVDIOSTORAGE ppIoStorage)
|
---|
3514 | {
|
---|
3515 | int rc = VINF_SUCCESS;
|
---|
3516 | PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
|
---|
3517 | PVDIOSTORAGE pIoStorage = (PVDIOSTORAGE)RTMemAllocZ(sizeof(VDIOSTORAGE));
|
---|
3518 |
|
---|
3519 | if (!pIoStorage)
|
---|
3520 | return VERR_NO_MEMORY;
|
---|
3521 |
|
---|
3522 | rc = pInterfaceIOCallbacks->pfnOpen(NULL, pszLocation, fOpen,
|
---|
3523 | NULL, &pIoStorage->pStorage);
|
---|
3524 | if (RT_SUCCESS(rc))
|
---|
3525 | *ppIoStorage = pIoStorage;
|
---|
3526 | else
|
---|
3527 | RTMemFree(pIoStorage);
|
---|
3528 |
|
---|
3529 | return rc;
|
---|
3530 | }
|
---|
3531 |
|
---|
3532 | static int vdIOIntCloseLimited(void *pvUser, PVDIOSTORAGE pIoStorage)
|
---|
3533 | {
|
---|
3534 | PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
|
---|
3535 | int rc = pInterfaceIOCallbacks->pfnClose(NULL, pIoStorage->pStorage);
|
---|
3536 | AssertRC(rc);
|
---|
3537 |
|
---|
3538 | RTMemFree(pIoStorage);
|
---|
3539 | return VINF_SUCCESS;
|
---|
3540 | }
|
---|
3541 |
|
---|
3542 | static int vdIOIntDeleteLimited(void *pvUser, const char *pcszFilename)
|
---|
3543 | {
|
---|
3544 | PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
|
---|
3545 | return pInterfaceIOCallbacks->pfnDelete(NULL, pcszFilename);
|
---|
3546 | }
|
---|
3547 |
|
---|
3548 | static int vdIOIntMoveLimited(void *pvUser, const char *pcszSrc,
|
---|
3549 | const char *pcszDst, unsigned fMove)
|
---|
3550 | {
|
---|
3551 | PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
|
---|
3552 | return pInterfaceIOCallbacks->pfnMove(NULL, pcszSrc, pcszDst, fMove);
|
---|
3553 | }
|
---|
3554 |
|
---|
3555 | static int vdIOIntGetFreeSpaceLimited(void *pvUser, const char *pcszFilename,
|
---|
3556 | int64_t *pcbFreeSpace)
|
---|
3557 | {
|
---|
3558 | PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
|
---|
3559 | return pInterfaceIOCallbacks->pfnGetFreeSpace(NULL, pcszFilename, pcbFreeSpace);
|
---|
3560 | }
|
---|
3561 |
|
---|
3562 | static int vdIOIntGetModificationTimeLimited(void *pvUser,
|
---|
3563 | const char *pcszFilename,
|
---|
3564 | PRTTIMESPEC pModificationTime)
|
---|
3565 | {
|
---|
3566 | PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
|
---|
3567 | return pInterfaceIOCallbacks->pfnGetModificationTime(NULL, pcszFilename, pModificationTime);
|
---|
3568 | }
|
---|
3569 |
|
---|
3570 | static int vdIOIntGetSizeLimited(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
3571 | uint64_t *pcbSize)
|
---|
3572 | {
|
---|
3573 | PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
|
---|
3574 | return pInterfaceIOCallbacks->pfnGetSize(NULL, pIoStorage->pStorage, pcbSize);
|
---|
3575 | }
|
---|
3576 |
|
---|
3577 | static int vdIOIntSetSizeLimited(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
3578 | uint64_t cbSize)
|
---|
3579 | {
|
---|
3580 | PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
|
---|
3581 | return pInterfaceIOCallbacks->pfnSetSize(NULL, pIoStorage->pStorage, cbSize);
|
---|
3582 | }
|
---|
3583 |
|
---|
3584 | static int vdIOIntWriteSyncLimited(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
3585 | uint64_t uOffset, const void *pvBuf,
|
---|
3586 | size_t cbWrite, size_t *pcbWritten)
|
---|
3587 | {
|
---|
3588 | PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
|
---|
3589 | return pInterfaceIOCallbacks->pfnWriteSync(NULL, pIoStorage->pStorage, uOffset, pvBuf, cbWrite, pcbWritten);
|
---|
3590 | }
|
---|
3591 |
|
---|
3592 | static int vdIOIntReadSyncLimited(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
3593 | uint64_t uOffset, void *pvBuf, size_t cbRead,
|
---|
3594 | size_t *pcbRead)
|
---|
3595 | {
|
---|
3596 | PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
|
---|
3597 | return pInterfaceIOCallbacks->pfnReadSync(NULL, pIoStorage->pStorage, uOffset, pvBuf, cbRead, pcbRead);
|
---|
3598 | }
|
---|
3599 |
|
---|
3600 | static int vdIOIntFlushSyncLimited(void *pvUser, PVDIOSTORAGE pIoStorage)
|
---|
3601 | {
|
---|
3602 | PVDINTERFACEIO pInterfaceIOCallbacks = (PVDINTERFACEIO)pvUser;
|
---|
3603 | return pInterfaceIOCallbacks->pfnFlushSync(NULL, pIoStorage->pStorage);
|
---|
3604 | }
|
---|
3605 |
|
---|
3606 | /**
|
---|
3607 | * internal: send output to the log (unconditionally).
|
---|
3608 | */
|
---|
3609 | int vdLogMessage(void *pvUser, const char *pszFormat, va_list args)
|
---|
3610 | {
|
---|
3611 | NOREF(pvUser);
|
---|
3612 | RTLogPrintfV(pszFormat, args);
|
---|
3613 | return VINF_SUCCESS;
|
---|
3614 | }
|
---|
3615 |
|
---|
3616 | DECLINLINE(int) vdMessageWrapper(PVBOXHDD pDisk, const char *pszFormat, ...)
|
---|
3617 | {
|
---|
3618 | va_list va;
|
---|
3619 | va_start(va, pszFormat);
|
---|
3620 | int rc = pDisk->pInterfaceErrorCallbacks->pfnMessage(pDisk->pInterfaceError->pvUser,
|
---|
3621 | pszFormat, va);
|
---|
3622 | va_end(va);
|
---|
3623 | return rc;
|
---|
3624 | }
|
---|
3625 |
|
---|
3626 |
|
---|
3627 | /**
|
---|
3628 | * internal: adjust PCHS geometry
|
---|
3629 | */
|
---|
3630 | static void vdFixupPCHSGeometry(PVDGEOMETRY pPCHS, uint64_t cbSize)
|
---|
3631 | {
|
---|
3632 | /* Fix broken PCHS geometry. Can happen for two reasons: either the backend
|
---|
3633 | * mixes up PCHS and LCHS, or the application used to create the source
|
---|
3634 | * image has put garbage in it. Additionally, if the PCHS geometry covers
|
---|
3635 | * more than the image size, set it back to the default. */
|
---|
3636 | if ( pPCHS->cHeads > 16
|
---|
3637 | || pPCHS->cSectors > 63
|
---|
3638 | || pPCHS->cCylinders == 0
|
---|
3639 | || (uint64_t)pPCHS->cHeads * pPCHS->cSectors * pPCHS->cCylinders * 512 > cbSize)
|
---|
3640 | {
|
---|
3641 | Assert(!(RT_MIN(cbSize / 512 / 16 / 63, 16383) - (uint32_t)RT_MIN(cbSize / 512 / 16 / 63, 16383)));
|
---|
3642 | pPCHS->cCylinders = (uint32_t)RT_MIN(cbSize / 512 / 16 / 63, 16383);
|
---|
3643 | pPCHS->cHeads = 16;
|
---|
3644 | pPCHS->cSectors = 63;
|
---|
3645 | }
|
---|
3646 | }
|
---|
3647 |
|
---|
3648 | /**
|
---|
3649 | * internal: adjust PCHS geometry
|
---|
3650 | */
|
---|
3651 | static void vdFixupLCHSGeometry(PVDGEOMETRY pLCHS, uint64_t cbSize)
|
---|
3652 | {
|
---|
3653 | /* Fix broken LCHS geometry. Can happen for two reasons: either the backend
|
---|
3654 | * mixes up PCHS and LCHS, or the application used to create the source
|
---|
3655 | * image has put garbage in it. The fix in this case is to clear the LCHS
|
---|
3656 | * geometry to trigger autodetection when it is used next. If the geometry
|
---|
3657 | * already says "please autodetect" (cylinders=0) keep it. */
|
---|
3658 | if ( ( pLCHS->cHeads > 255
|
---|
3659 | || pLCHS->cHeads == 0
|
---|
3660 | || pLCHS->cSectors > 63
|
---|
3661 | || pLCHS->cSectors == 0)
|
---|
3662 | && pLCHS->cCylinders != 0)
|
---|
3663 | {
|
---|
3664 | pLCHS->cCylinders = 0;
|
---|
3665 | pLCHS->cHeads = 0;
|
---|
3666 | pLCHS->cSectors = 0;
|
---|
3667 | }
|
---|
3668 | /* Always recompute the number of cylinders stored in the LCHS
|
---|
3669 | * geometry if it isn't set to "autotedetect" at the moment.
|
---|
3670 | * This is very useful if the destination image size is
|
---|
3671 | * larger or smaller than the source image size. Do not modify
|
---|
3672 | * the number of heads and sectors. Windows guests hate it. */
|
---|
3673 | if ( pLCHS->cCylinders != 0
|
---|
3674 | && pLCHS->cHeads != 0 /* paranoia */
|
---|
3675 | && pLCHS->cSectors != 0 /* paranoia */)
|
---|
3676 | {
|
---|
3677 | Assert(!(RT_MIN(cbSize / 512 / pLCHS->cHeads / pLCHS->cSectors, 1024) - (uint32_t)RT_MIN(cbSize / 512 / pLCHS->cHeads / pLCHS->cSectors, 1024)));
|
---|
3678 | pLCHS->cCylinders = (uint32_t)RT_MIN(cbSize / 512 / pLCHS->cHeads / pLCHS->cSectors, 1024);
|
---|
3679 | }
|
---|
3680 | }
|
---|
3681 |
|
---|
3682 | /**
|
---|
3683 | * Initializes HDD backends.
|
---|
3684 | *
|
---|
3685 | * @returns VBox status code.
|
---|
3686 | */
|
---|
3687 | VBOXDDU_DECL(int) VDInit(void)
|
---|
3688 | {
|
---|
3689 | int rc = vdAddBackends(aStaticBackends, RT_ELEMENTS(aStaticBackends));
|
---|
3690 | if (RT_SUCCESS(rc))
|
---|
3691 | {
|
---|
3692 | rc = vdAddCacheBackends(aStaticCacheBackends, RT_ELEMENTS(aStaticCacheBackends));
|
---|
3693 | if (RT_SUCCESS(rc))
|
---|
3694 | {
|
---|
3695 | rc = vdLoadDynamicBackends();
|
---|
3696 | if (RT_SUCCESS(rc))
|
---|
3697 | rc = vdLoadDynamicCacheBackends();
|
---|
3698 | }
|
---|
3699 | }
|
---|
3700 | LogRel(("VDInit finished\n"));
|
---|
3701 | return rc;
|
---|
3702 | }
|
---|
3703 |
|
---|
3704 | /**
|
---|
3705 | * Destroys loaded HDD backends.
|
---|
3706 | *
|
---|
3707 | * @returns VBox status code.
|
---|
3708 | */
|
---|
3709 | VBOXDDU_DECL(int) VDShutdown(void)
|
---|
3710 | {
|
---|
3711 | PVBOXHDDBACKEND *pBackends = g_apBackends;
|
---|
3712 | PVDCACHEBACKEND *pCacheBackends = g_apCacheBackends;
|
---|
3713 | unsigned cBackends = g_cBackends;
|
---|
3714 |
|
---|
3715 | if (!pBackends)
|
---|
3716 | return VERR_INTERNAL_ERROR;
|
---|
3717 |
|
---|
3718 | g_cBackends = 0;
|
---|
3719 | g_apBackends = NULL;
|
---|
3720 |
|
---|
3721 | #ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
3722 | for (unsigned i = 0; i < cBackends; i++)
|
---|
3723 | if (pBackends[i]->hPlugin != NIL_RTLDRMOD)
|
---|
3724 | RTLdrClose(pBackends[i]->hPlugin);
|
---|
3725 | #endif
|
---|
3726 |
|
---|
3727 | /* Clear the supported cache backends. */
|
---|
3728 | cBackends = g_cCacheBackends;
|
---|
3729 | g_cCacheBackends = 0;
|
---|
3730 | g_apCacheBackends = NULL;
|
---|
3731 |
|
---|
3732 | #ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
3733 | for (unsigned i = 0; i < cBackends; i++)
|
---|
3734 | if (pCacheBackends[i]->hPlugin != NIL_RTLDRMOD)
|
---|
3735 | RTLdrClose(pCacheBackends[i]->hPlugin);
|
---|
3736 | #endif
|
---|
3737 |
|
---|
3738 | if (pCacheBackends)
|
---|
3739 | RTMemFree(pCacheBackends);
|
---|
3740 | RTMemFree(pBackends);
|
---|
3741 | return VINF_SUCCESS;
|
---|
3742 | }
|
---|
3743 |
|
---|
3744 |
|
---|
3745 | /**
|
---|
3746 | * Lists all HDD backends and their capabilities in a caller-provided buffer.
|
---|
3747 | *
|
---|
3748 | * @returns VBox status code.
|
---|
3749 | * VERR_BUFFER_OVERFLOW if not enough space is passed.
|
---|
3750 | * @param cEntriesAlloc Number of list entries available.
|
---|
3751 | * @param pEntries Pointer to array for the entries.
|
---|
3752 | * @param pcEntriesUsed Number of entries returned.
|
---|
3753 | */
|
---|
3754 | VBOXDDU_DECL(int) VDBackendInfo(unsigned cEntriesAlloc, PVDBACKENDINFO pEntries,
|
---|
3755 | unsigned *pcEntriesUsed)
|
---|
3756 | {
|
---|
3757 | int rc = VINF_SUCCESS;
|
---|
3758 | PRTDIR pPluginDir = NULL;
|
---|
3759 | unsigned cEntries = 0;
|
---|
3760 |
|
---|
3761 | LogFlowFunc(("cEntriesAlloc=%u pEntries=%#p pcEntriesUsed=%#p\n", cEntriesAlloc, pEntries, pcEntriesUsed));
|
---|
3762 | /* Check arguments. */
|
---|
3763 | AssertMsgReturn(cEntriesAlloc,
|
---|
3764 | ("cEntriesAlloc=%u\n", cEntriesAlloc),
|
---|
3765 | VERR_INVALID_PARAMETER);
|
---|
3766 | AssertMsgReturn(VALID_PTR(pEntries),
|
---|
3767 | ("pEntries=%#p\n", pEntries),
|
---|
3768 | VERR_INVALID_PARAMETER);
|
---|
3769 | AssertMsgReturn(VALID_PTR(pcEntriesUsed),
|
---|
3770 | ("pcEntriesUsed=%#p\n", pcEntriesUsed),
|
---|
3771 | VERR_INVALID_PARAMETER);
|
---|
3772 | if (!g_apBackends)
|
---|
3773 | VDInit();
|
---|
3774 |
|
---|
3775 | if (cEntriesAlloc < g_cBackends)
|
---|
3776 | {
|
---|
3777 | *pcEntriesUsed = g_cBackends;
|
---|
3778 | return VERR_BUFFER_OVERFLOW;
|
---|
3779 | }
|
---|
3780 |
|
---|
3781 | for (unsigned i = 0; i < g_cBackends; i++)
|
---|
3782 | {
|
---|
3783 | pEntries[i].pszBackend = g_apBackends[i]->pszBackendName;
|
---|
3784 | pEntries[i].uBackendCaps = g_apBackends[i]->uBackendCaps;
|
---|
3785 | pEntries[i].paFileExtensions = g_apBackends[i]->paFileExtensions;
|
---|
3786 | pEntries[i].paConfigInfo = g_apBackends[i]->paConfigInfo;
|
---|
3787 | pEntries[i].pfnComposeLocation = g_apBackends[i]->pfnComposeLocation;
|
---|
3788 | pEntries[i].pfnComposeName = g_apBackends[i]->pfnComposeName;
|
---|
3789 | }
|
---|
3790 |
|
---|
3791 | LogFlowFunc(("returns %Rrc *pcEntriesUsed=%u\n", rc, cEntries));
|
---|
3792 | *pcEntriesUsed = g_cBackends;
|
---|
3793 | return rc;
|
---|
3794 | }
|
---|
3795 |
|
---|
3796 | /**
|
---|
3797 | * Lists the capabilities of a backend identified by its name.
|
---|
3798 | *
|
---|
3799 | * @returns VBox status code.
|
---|
3800 | * @param pszBackend The backend name.
|
---|
3801 | * @param pEntries Pointer to an entry.
|
---|
3802 | */
|
---|
3803 | VBOXDDU_DECL(int) VDBackendInfoOne(const char *pszBackend, PVDBACKENDINFO pEntry)
|
---|
3804 | {
|
---|
3805 | LogFlowFunc(("pszBackend=%#p pEntry=%#p\n", pszBackend, pEntry));
|
---|
3806 | /* Check arguments. */
|
---|
3807 | AssertMsgReturn(VALID_PTR(pszBackend),
|
---|
3808 | ("pszBackend=%#p\n", pszBackend),
|
---|
3809 | VERR_INVALID_PARAMETER);
|
---|
3810 | AssertMsgReturn(VALID_PTR(pEntry),
|
---|
3811 | ("pEntry=%#p\n", pEntry),
|
---|
3812 | VERR_INVALID_PARAMETER);
|
---|
3813 | if (!g_apBackends)
|
---|
3814 | VDInit();
|
---|
3815 |
|
---|
3816 | /* Go through loaded backends. */
|
---|
3817 | for (unsigned i = 0; i < g_cBackends; i++)
|
---|
3818 | {
|
---|
3819 | if (!RTStrICmp(pszBackend, g_apBackends[i]->pszBackendName))
|
---|
3820 | {
|
---|
3821 | pEntry->pszBackend = g_apBackends[i]->pszBackendName;
|
---|
3822 | pEntry->uBackendCaps = g_apBackends[i]->uBackendCaps;
|
---|
3823 | pEntry->paFileExtensions = g_apBackends[i]->paFileExtensions;
|
---|
3824 | pEntry->paConfigInfo = g_apBackends[i]->paConfigInfo;
|
---|
3825 | return VINF_SUCCESS;
|
---|
3826 | }
|
---|
3827 | }
|
---|
3828 |
|
---|
3829 | return VERR_NOT_FOUND;
|
---|
3830 | }
|
---|
3831 |
|
---|
3832 | /**
|
---|
3833 | * Allocates and initializes an empty HDD container.
|
---|
3834 | * No image files are opened.
|
---|
3835 | *
|
---|
3836 | * @returns VBox status code.
|
---|
3837 | * @param pVDIfsDisk Pointer to the per-disk VD interface list.
|
---|
3838 | * @param enmType Type of the image container.
|
---|
3839 | * @param ppDisk Where to store the reference to HDD container.
|
---|
3840 | */
|
---|
3841 | VBOXDDU_DECL(int) VDCreate(PVDINTERFACE pVDIfsDisk, VDTYPE enmType, PVBOXHDD *ppDisk)
|
---|
3842 | {
|
---|
3843 | int rc = VINF_SUCCESS;
|
---|
3844 | PVBOXHDD pDisk = NULL;
|
---|
3845 |
|
---|
3846 | LogFlowFunc(("pVDIfsDisk=%#p\n", pVDIfsDisk));
|
---|
3847 | do
|
---|
3848 | {
|
---|
3849 | /* Check arguments. */
|
---|
3850 | AssertMsgBreakStmt(VALID_PTR(ppDisk),
|
---|
3851 | ("ppDisk=%#p\n", ppDisk),
|
---|
3852 | rc = VERR_INVALID_PARAMETER);
|
---|
3853 |
|
---|
3854 | pDisk = (PVBOXHDD)RTMemAllocZ(sizeof(VBOXHDD));
|
---|
3855 | if (pDisk)
|
---|
3856 | {
|
---|
3857 | pDisk->u32Signature = VBOXHDDDISK_SIGNATURE;
|
---|
3858 | pDisk->enmType = enmType;
|
---|
3859 | pDisk->cImages = 0;
|
---|
3860 | pDisk->pBase = NULL;
|
---|
3861 | pDisk->pLast = NULL;
|
---|
3862 | pDisk->cbSize = 0;
|
---|
3863 | pDisk->PCHSGeometry.cCylinders = 0;
|
---|
3864 | pDisk->PCHSGeometry.cHeads = 0;
|
---|
3865 | pDisk->PCHSGeometry.cSectors = 0;
|
---|
3866 | pDisk->LCHSGeometry.cCylinders = 0;
|
---|
3867 | pDisk->LCHSGeometry.cHeads = 0;
|
---|
3868 | pDisk->LCHSGeometry.cSectors = 0;
|
---|
3869 | pDisk->pVDIfsDisk = pVDIfsDisk;
|
---|
3870 | pDisk->pInterfaceError = NULL;
|
---|
3871 | pDisk->pInterfaceErrorCallbacks = NULL;
|
---|
3872 | pDisk->pInterfaceThreadSync = NULL;
|
---|
3873 | pDisk->pInterfaceThreadSyncCallbacks = NULL;
|
---|
3874 | pDisk->fLocked = false;
|
---|
3875 | pDisk->pIoCtxLockOwner = NULL;
|
---|
3876 | RTListInit(&pDisk->ListWriteLocked);
|
---|
3877 |
|
---|
3878 | /* Create the I/O ctx cache */
|
---|
3879 | rc = RTMemCacheCreate(&pDisk->hMemCacheIoCtx, sizeof(VDIOCTX), 0, UINT32_MAX,
|
---|
3880 | NULL, NULL, NULL, 0);
|
---|
3881 | if (RT_FAILURE(rc))
|
---|
3882 | {
|
---|
3883 | RTMemFree(pDisk);
|
---|
3884 | break;
|
---|
3885 | }
|
---|
3886 |
|
---|
3887 | /* Create the I/O task cache */
|
---|
3888 | rc = RTMemCacheCreate(&pDisk->hMemCacheIoTask, sizeof(VDIOTASK), 0, UINT32_MAX,
|
---|
3889 | NULL, NULL, NULL, 0);
|
---|
3890 | if (RT_FAILURE(rc))
|
---|
3891 | {
|
---|
3892 | RTMemCacheDestroy(pDisk->hMemCacheIoCtx);
|
---|
3893 | RTMemFree(pDisk);
|
---|
3894 | break;
|
---|
3895 | }
|
---|
3896 |
|
---|
3897 | /* Create critical section. */
|
---|
3898 | rc = RTCritSectInit(&pDisk->CritSect);
|
---|
3899 | if (RT_FAILURE(rc))
|
---|
3900 | {
|
---|
3901 | RTMemCacheDestroy(pDisk->hMemCacheIoCtx);
|
---|
3902 | RTMemCacheDestroy(pDisk->hMemCacheIoTask);
|
---|
3903 | RTMemFree(pDisk);
|
---|
3904 | break;
|
---|
3905 | }
|
---|
3906 |
|
---|
3907 | pDisk->pInterfaceError = VDInterfaceGet(pVDIfsDisk, VDINTERFACETYPE_ERROR);
|
---|
3908 | if (pDisk->pInterfaceError)
|
---|
3909 | pDisk->pInterfaceErrorCallbacks = VDGetInterfaceError(pDisk->pInterfaceError);
|
---|
3910 |
|
---|
3911 | pDisk->pInterfaceThreadSync = VDInterfaceGet(pVDIfsDisk, VDINTERFACETYPE_THREADSYNC);
|
---|
3912 | if (pDisk->pInterfaceThreadSync)
|
---|
3913 | pDisk->pInterfaceThreadSyncCallbacks = VDGetInterfaceThreadSync(pDisk->pInterfaceThreadSync);
|
---|
3914 |
|
---|
3915 | /* Create fallback I/O callback table */
|
---|
3916 | pDisk->VDIIOCallbacks.cbSize = sizeof(VDINTERFACEIO);
|
---|
3917 | pDisk->VDIIOCallbacks.enmInterface = VDINTERFACETYPE_IO;
|
---|
3918 | pDisk->VDIIOCallbacks.pfnOpen = vdIOOpenFallback;
|
---|
3919 | pDisk->VDIIOCallbacks.pfnClose = vdIOCloseFallback;
|
---|
3920 | pDisk->VDIIOCallbacks.pfnDelete = vdIODeleteFallback;
|
---|
3921 | pDisk->VDIIOCallbacks.pfnMove = vdIOMoveFallback;
|
---|
3922 | pDisk->VDIIOCallbacks.pfnGetFreeSpace = vdIOGetFreeSpaceFallback;
|
---|
3923 | pDisk->VDIIOCallbacks.pfnGetModificationTime = vdIOGetModificationTimeFallback;
|
---|
3924 | pDisk->VDIIOCallbacks.pfnGetSize = vdIOGetSizeFallback;
|
---|
3925 | pDisk->VDIIOCallbacks.pfnSetSize = vdIOSetSizeFallback;
|
---|
3926 | pDisk->VDIIOCallbacks.pfnReadSync = vdIOReadSyncFallback;
|
---|
3927 | pDisk->VDIIOCallbacks.pfnWriteSync = vdIOWriteSyncFallback;
|
---|
3928 | pDisk->VDIIOCallbacks.pfnFlushSync = vdIOFlushSyncFallback;
|
---|
3929 | pDisk->VDIIOCallbacks.pfnReadAsync = vdIOReadAsyncFallback;
|
---|
3930 | pDisk->VDIIOCallbacks.pfnWriteAsync = vdIOWriteAsyncFallback;
|
---|
3931 | pDisk->VDIIOCallbacks.pfnFlushAsync = vdIOFlushAsyncFallback;
|
---|
3932 |
|
---|
3933 | /*
|
---|
3934 | * Create the internal I/O callback table.
|
---|
3935 | * The interface is per-image but no need to duplicate the
|
---|
3936 | * callback table every time.
|
---|
3937 | */
|
---|
3938 | pDisk->VDIIOIntCallbacks.cbSize = sizeof(VDINTERFACEIOINT);
|
---|
3939 | pDisk->VDIIOIntCallbacks.enmInterface = VDINTERFACETYPE_IOINT;
|
---|
3940 | pDisk->VDIIOIntCallbacks.pfnOpen = vdIOIntOpen;
|
---|
3941 | pDisk->VDIIOIntCallbacks.pfnClose = vdIOIntClose;
|
---|
3942 | pDisk->VDIIOIntCallbacks.pfnDelete = vdIOIntDelete;
|
---|
3943 | pDisk->VDIIOIntCallbacks.pfnMove = vdIOIntMove;
|
---|
3944 | pDisk->VDIIOIntCallbacks.pfnGetFreeSpace = vdIOIntGetFreeSpace;
|
---|
3945 | pDisk->VDIIOIntCallbacks.pfnGetModificationTime = vdIOIntGetModificationTime;
|
---|
3946 | pDisk->VDIIOIntCallbacks.pfnGetSize = vdIOIntGetSize;
|
---|
3947 | pDisk->VDIIOIntCallbacks.pfnSetSize = vdIOIntSetSize;
|
---|
3948 | pDisk->VDIIOIntCallbacks.pfnReadSync = vdIOIntReadSync;
|
---|
3949 | pDisk->VDIIOIntCallbacks.pfnWriteSync = vdIOIntWriteSync;
|
---|
3950 | pDisk->VDIIOIntCallbacks.pfnFlushSync = vdIOIntFlushSync;
|
---|
3951 | pDisk->VDIIOIntCallbacks.pfnReadUserAsync = vdIOIntReadUserAsync;
|
---|
3952 | pDisk->VDIIOIntCallbacks.pfnWriteUserAsync = vdIOIntWriteUserAsync;
|
---|
3953 | pDisk->VDIIOIntCallbacks.pfnReadMetaAsync = vdIOIntReadMetaAsync;
|
---|
3954 | pDisk->VDIIOIntCallbacks.pfnWriteMetaAsync = vdIOIntWriteMetaAsync;
|
---|
3955 | pDisk->VDIIOIntCallbacks.pfnMetaXferRelease = vdIOIntMetaXferRelease;
|
---|
3956 | pDisk->VDIIOIntCallbacks.pfnFlushAsync = vdIOIntFlushAsync;
|
---|
3957 | pDisk->VDIIOIntCallbacks.pfnIoCtxCopyFrom = vdIOIntIoCtxCopyFrom;
|
---|
3958 | pDisk->VDIIOIntCallbacks.pfnIoCtxCopyTo = vdIOIntIoCtxCopyTo;
|
---|
3959 | pDisk->VDIIOIntCallbacks.pfnIoCtxSet = vdIOIntIoCtxSet;
|
---|
3960 | pDisk->VDIIOIntCallbacks.pfnIoCtxSegArrayCreate = vdIOIntIoCtxSegArrayCreate;
|
---|
3961 | pDisk->VDIIOIntCallbacks.pfnIoCtxCompleted = vdIOIntIoCtxCompleted;
|
---|
3962 |
|
---|
3963 | *ppDisk = pDisk;
|
---|
3964 | }
|
---|
3965 | else
|
---|
3966 | {
|
---|
3967 | rc = VERR_NO_MEMORY;
|
---|
3968 | break;
|
---|
3969 | }
|
---|
3970 | } while (0);
|
---|
3971 |
|
---|
3972 | LogFlowFunc(("returns %Rrc (pDisk=%#p)\n", rc, pDisk));
|
---|
3973 | return rc;
|
---|
3974 | }
|
---|
3975 |
|
---|
3976 | /**
|
---|
3977 | * Destroys HDD container.
|
---|
3978 | * If container has opened image files they will be closed.
|
---|
3979 | *
|
---|
3980 | * @param pDisk Pointer to HDD container.
|
---|
3981 | */
|
---|
3982 | VBOXDDU_DECL(void) VDDestroy(PVBOXHDD pDisk)
|
---|
3983 | {
|
---|
3984 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
3985 | do
|
---|
3986 | {
|
---|
3987 | /* sanity check */
|
---|
3988 | AssertPtrBreak(pDisk);
|
---|
3989 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
3990 | VDCloseAll(pDisk);
|
---|
3991 | RTCritSectDelete(&pDisk->CritSect);
|
---|
3992 | RTMemCacheDestroy(pDisk->hMemCacheIoCtx);
|
---|
3993 | RTMemCacheDestroy(pDisk->hMemCacheIoTask);
|
---|
3994 | RTMemFree(pDisk);
|
---|
3995 | } while (0);
|
---|
3996 | LogFlowFunc(("returns\n"));
|
---|
3997 | }
|
---|
3998 |
|
---|
3999 | /**
|
---|
4000 | * Try to get the backend name which can use this image.
|
---|
4001 | *
|
---|
4002 | * @returns VBox status code.
|
---|
4003 | * VINF_SUCCESS if a plugin was found.
|
---|
4004 | * ppszFormat contains the string which can be used as backend name.
|
---|
4005 | * VERR_NOT_SUPPORTED if no backend was found.
|
---|
4006 | * @param pVDIfsDisk Pointer to the per-disk VD interface list.
|
---|
4007 | * @param pVDIfsImage Pointer to the per-image VD interface list.
|
---|
4008 | * @param pszFilename Name of the image file for which the backend is queried.
|
---|
4009 | * @param ppszFormat Receives pointer of the UTF-8 string which contains the format name.
|
---|
4010 | * The returned pointer must be freed using RTStrFree().
|
---|
4011 | */
|
---|
4012 | VBOXDDU_DECL(int) VDGetFormat(PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
|
---|
4013 | const char *pszFilename, char **ppszFormat, VDTYPE *penmType)
|
---|
4014 | {
|
---|
4015 | int rc = VERR_NOT_SUPPORTED;
|
---|
4016 | VDINTERFACEIOINT VDIIOIntCallbacks;
|
---|
4017 | VDINTERFACE VDIIOInt;
|
---|
4018 | VDINTERFACEIO VDIIOCallbacksFallback;
|
---|
4019 | PVDINTERFACE pInterfaceIO;
|
---|
4020 | PVDINTERFACEIO pInterfaceIOCallbacks;
|
---|
4021 |
|
---|
4022 | LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
|
---|
4023 | /* Check arguments. */
|
---|
4024 | AssertMsgReturn(VALID_PTR(pszFilename) && *pszFilename,
|
---|
4025 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
4026 | VERR_INVALID_PARAMETER);
|
---|
4027 | AssertMsgReturn(VALID_PTR(ppszFormat),
|
---|
4028 | ("ppszFormat=%#p\n", ppszFormat),
|
---|
4029 | VERR_INVALID_PARAMETER);
|
---|
4030 | AssertMsgReturn(VALID_PTR(ppszFormat),
|
---|
4031 | ("penmType=%#p\n", penmType),
|
---|
4032 | VERR_INVALID_PARAMETER);
|
---|
4033 |
|
---|
4034 | if (!g_apBackends)
|
---|
4035 | VDInit();
|
---|
4036 |
|
---|
4037 | pInterfaceIO = VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IO);
|
---|
4038 | if (!pInterfaceIO)
|
---|
4039 | {
|
---|
4040 | /*
|
---|
4041 | * Caller doesn't provide an I/O interface, create our own using the
|
---|
4042 | * native file API.
|
---|
4043 | */
|
---|
4044 | VDIIOCallbacksFallback.cbSize = sizeof(VDINTERFACEIO);
|
---|
4045 | VDIIOCallbacksFallback.enmInterface = VDINTERFACETYPE_IO;
|
---|
4046 | VDIIOCallbacksFallback.pfnOpen = vdIOOpenFallback;
|
---|
4047 | VDIIOCallbacksFallback.pfnClose = vdIOCloseFallback;
|
---|
4048 | VDIIOCallbacksFallback.pfnDelete = vdIODeleteFallback;
|
---|
4049 | VDIIOCallbacksFallback.pfnMove = vdIOMoveFallback;
|
---|
4050 | VDIIOCallbacksFallback.pfnGetFreeSpace = vdIOGetFreeSpaceFallback;
|
---|
4051 | VDIIOCallbacksFallback.pfnGetModificationTime = vdIOGetModificationTimeFallback;
|
---|
4052 | VDIIOCallbacksFallback.pfnGetSize = vdIOGetSizeFallback;
|
---|
4053 | VDIIOCallbacksFallback.pfnSetSize = vdIOSetSizeFallback;
|
---|
4054 | VDIIOCallbacksFallback.pfnReadSync = vdIOReadSyncFallback;
|
---|
4055 | VDIIOCallbacksFallback.pfnWriteSync = vdIOWriteSyncFallback;
|
---|
4056 | VDIIOCallbacksFallback.pfnFlushSync = vdIOFlushSyncFallback;
|
---|
4057 | pInterfaceIOCallbacks = &VDIIOCallbacksFallback;
|
---|
4058 | }
|
---|
4059 | else
|
---|
4060 | pInterfaceIOCallbacks = VDGetInterfaceIO(pInterfaceIO);
|
---|
4061 |
|
---|
4062 | /* Set up the internal I/O interface. */
|
---|
4063 | AssertReturn(!VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IOINT),
|
---|
4064 | VERR_INVALID_PARAMETER);
|
---|
4065 | VDIIOIntCallbacks.cbSize = sizeof(VDINTERFACEIOINT);
|
---|
4066 | VDIIOIntCallbacks.enmInterface = VDINTERFACETYPE_IOINT;
|
---|
4067 | VDIIOIntCallbacks.pfnOpen = vdIOIntOpenLimited;
|
---|
4068 | VDIIOIntCallbacks.pfnClose = vdIOIntCloseLimited;
|
---|
4069 | VDIIOIntCallbacks.pfnDelete = vdIOIntDeleteLimited;
|
---|
4070 | VDIIOIntCallbacks.pfnMove = vdIOIntMoveLimited;
|
---|
4071 | VDIIOIntCallbacks.pfnGetFreeSpace = vdIOIntGetFreeSpaceLimited;
|
---|
4072 | VDIIOIntCallbacks.pfnGetModificationTime = vdIOIntGetModificationTimeLimited;
|
---|
4073 | VDIIOIntCallbacks.pfnGetSize = vdIOIntGetSizeLimited;
|
---|
4074 | VDIIOIntCallbacks.pfnSetSize = vdIOIntSetSizeLimited;
|
---|
4075 | VDIIOIntCallbacks.pfnReadSync = vdIOIntReadSyncLimited;
|
---|
4076 | VDIIOIntCallbacks.pfnWriteSync = vdIOIntWriteSyncLimited;
|
---|
4077 | VDIIOIntCallbacks.pfnFlushSync = vdIOIntFlushSyncLimited;
|
---|
4078 | VDIIOIntCallbacks.pfnReadUserAsync = NULL;
|
---|
4079 | VDIIOIntCallbacks.pfnWriteUserAsync = NULL;
|
---|
4080 | VDIIOIntCallbacks.pfnReadMetaAsync = NULL;
|
---|
4081 | VDIIOIntCallbacks.pfnWriteMetaAsync = NULL;
|
---|
4082 | VDIIOIntCallbacks.pfnFlushAsync = NULL;
|
---|
4083 | rc = VDInterfaceAdd(&VDIIOInt, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
4084 | &VDIIOIntCallbacks, pInterfaceIOCallbacks, &pVDIfsImage);
|
---|
4085 | AssertRC(rc);
|
---|
4086 |
|
---|
4087 | /* Find the backend supporting this file format. */
|
---|
4088 | for (unsigned i = 0; i < g_cBackends; i++)
|
---|
4089 | {
|
---|
4090 | if (g_apBackends[i]->pfnCheckIfValid)
|
---|
4091 | {
|
---|
4092 | rc = g_apBackends[i]->pfnCheckIfValid(pszFilename, pVDIfsDisk,
|
---|
4093 | pVDIfsImage, penmType);
|
---|
4094 | if ( RT_SUCCESS(rc)
|
---|
4095 | /* The correct backend has been found, but there is a small
|
---|
4096 | * incompatibility so that the file cannot be used. Stop here
|
---|
4097 | * and signal success - the actual open will of course fail,
|
---|
4098 | * but that will create a really sensible error message. */
|
---|
4099 | || ( rc != VERR_VD_GEN_INVALID_HEADER
|
---|
4100 | && rc != VERR_VD_VDI_INVALID_HEADER
|
---|
4101 | && rc != VERR_VD_VMDK_INVALID_HEADER
|
---|
4102 | && rc != VERR_VD_ISCSI_INVALID_HEADER
|
---|
4103 | && rc != VERR_VD_VHD_INVALID_HEADER
|
---|
4104 | && rc != VERR_VD_RAW_INVALID_HEADER
|
---|
4105 | && rc != VERR_VD_PARALLELS_INVALID_HEADER
|
---|
4106 | && rc != VERR_VD_DMG_INVALID_HEADER))
|
---|
4107 | {
|
---|
4108 | /* Copy the name into the new string. */
|
---|
4109 | char *pszFormat = RTStrDup(g_apBackends[i]->pszBackendName);
|
---|
4110 | if (!pszFormat)
|
---|
4111 | {
|
---|
4112 | rc = VERR_NO_MEMORY;
|
---|
4113 | break;
|
---|
4114 | }
|
---|
4115 | *ppszFormat = pszFormat;
|
---|
4116 | rc = VINF_SUCCESS;
|
---|
4117 | break;
|
---|
4118 | }
|
---|
4119 | rc = VERR_NOT_SUPPORTED;
|
---|
4120 | }
|
---|
4121 | }
|
---|
4122 |
|
---|
4123 | /* Try the cache backends. */
|
---|
4124 | if (rc == VERR_NOT_SUPPORTED)
|
---|
4125 | {
|
---|
4126 | for (unsigned i = 0; i < g_cCacheBackends; i++)
|
---|
4127 | {
|
---|
4128 | if (g_apCacheBackends[i]->pfnProbe)
|
---|
4129 | {
|
---|
4130 | rc = g_apCacheBackends[i]->pfnProbe(pszFilename, pVDIfsDisk,
|
---|
4131 | pVDIfsImage);
|
---|
4132 | if ( RT_SUCCESS(rc)
|
---|
4133 | || (rc != VERR_VD_GEN_INVALID_HEADER))
|
---|
4134 | {
|
---|
4135 | /* Copy the name into the new string. */
|
---|
4136 | char *pszFormat = RTStrDup(g_apBackends[i]->pszBackendName);
|
---|
4137 | if (!pszFormat)
|
---|
4138 | {
|
---|
4139 | rc = VERR_NO_MEMORY;
|
---|
4140 | break;
|
---|
4141 | }
|
---|
4142 | *ppszFormat = pszFormat;
|
---|
4143 | rc = VINF_SUCCESS;
|
---|
4144 | break;
|
---|
4145 | }
|
---|
4146 | rc = VERR_NOT_SUPPORTED;
|
---|
4147 | }
|
---|
4148 | }
|
---|
4149 | }
|
---|
4150 |
|
---|
4151 | LogFlowFunc(("returns %Rrc *ppszFormat=\"%s\"\n", rc, *ppszFormat));
|
---|
4152 | return rc;
|
---|
4153 | }
|
---|
4154 |
|
---|
4155 | /**
|
---|
4156 | * Opens an image file.
|
---|
4157 | *
|
---|
4158 | * The first opened image file in HDD container must have a base image type,
|
---|
4159 | * others (next opened images) must be a differencing or undo images.
|
---|
4160 | * Linkage is checked for differencing image to be in consistence with the previously opened image.
|
---|
4161 | * When another differencing image is opened and the last image was opened in read/write access
|
---|
4162 | * mode, then the last image is reopened in read-only with deny write sharing mode. This allows
|
---|
4163 | * other processes to use images in read-only mode too.
|
---|
4164 | *
|
---|
4165 | * Note that the image is opened in read-only mode if a read/write open is not possible.
|
---|
4166 | * Use VDIsReadOnly to check open mode.
|
---|
4167 | *
|
---|
4168 | * @returns VBox status code.
|
---|
4169 | * @param pDisk Pointer to HDD container.
|
---|
4170 | * @param pszBackend Name of the image file backend to use.
|
---|
4171 | * @param pszFilename Name of the image file to open.
|
---|
4172 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
4173 | * @param pVDIfsImage Pointer to the per-image VD interface list.
|
---|
4174 | */
|
---|
4175 | VBOXDDU_DECL(int) VDOpen(PVBOXHDD pDisk, const char *pszBackend,
|
---|
4176 | const char *pszFilename, unsigned uOpenFlags,
|
---|
4177 | PVDINTERFACE pVDIfsImage)
|
---|
4178 | {
|
---|
4179 | int rc = VINF_SUCCESS;
|
---|
4180 | int rc2;
|
---|
4181 | bool fLockWrite = false;
|
---|
4182 | PVDIMAGE pImage = NULL;
|
---|
4183 |
|
---|
4184 | LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uOpenFlags=%#x, pVDIfsImage=%#p\n",
|
---|
4185 | pDisk, pszBackend, pszFilename, uOpenFlags, pVDIfsImage));
|
---|
4186 |
|
---|
4187 | do
|
---|
4188 | {
|
---|
4189 | /* sanity check */
|
---|
4190 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
4191 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
4192 |
|
---|
4193 | /* Check arguments. */
|
---|
4194 | AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
|
---|
4195 | ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
|
---|
4196 | rc = VERR_INVALID_PARAMETER);
|
---|
4197 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
4198 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
4199 | rc = VERR_INVALID_PARAMETER);
|
---|
4200 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
4201 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
4202 | rc = VERR_INVALID_PARAMETER);
|
---|
4203 |
|
---|
4204 | /* Set up image descriptor. */
|
---|
4205 | pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
|
---|
4206 | if (!pImage)
|
---|
4207 | {
|
---|
4208 | rc = VERR_NO_MEMORY;
|
---|
4209 | break;
|
---|
4210 | }
|
---|
4211 | pImage->pszFilename = RTStrDup(pszFilename);
|
---|
4212 | if (!pImage->pszFilename)
|
---|
4213 | {
|
---|
4214 | rc = VERR_NO_MEMORY;
|
---|
4215 | break;
|
---|
4216 | }
|
---|
4217 |
|
---|
4218 | pImage->VDIo.pDisk = pDisk;
|
---|
4219 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
4220 |
|
---|
4221 | rc = vdFindBackend(pszBackend, &pImage->Backend);
|
---|
4222 | if (RT_FAILURE(rc))
|
---|
4223 | break;
|
---|
4224 | if (!pImage->Backend)
|
---|
4225 | {
|
---|
4226 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
4227 | N_("VD: unknown backend name '%s'"), pszBackend);
|
---|
4228 | break;
|
---|
4229 | }
|
---|
4230 |
|
---|
4231 | /*
|
---|
4232 | * Fail if the the backend can't do async I/O but the
|
---|
4233 | * flag is set.
|
---|
4234 | */
|
---|
4235 | if ( !(pImage->Backend->uBackendCaps & VD_CAP_ASYNC)
|
---|
4236 | && (uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO))
|
---|
4237 | {
|
---|
4238 | rc = vdError(pDisk, VERR_NOT_SUPPORTED, RT_SRC_POS,
|
---|
4239 | N_("VD: Backend '%s' does not support async I/O"), pszBackend);
|
---|
4240 | break;
|
---|
4241 | }
|
---|
4242 |
|
---|
4243 | /* Set up the I/O interface. */
|
---|
4244 | pImage->VDIo.pInterfaceIO = VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IO);
|
---|
4245 | if (pImage->VDIo.pInterfaceIO)
|
---|
4246 | pImage->VDIo.pInterfaceIOCallbacks = VDGetInterfaceIO(pImage->VDIo.pInterfaceIO);
|
---|
4247 | else
|
---|
4248 | {
|
---|
4249 | rc = VDInterfaceAdd(&pImage->VDIo.VDIIO, "VD_IO", VDINTERFACETYPE_IO,
|
---|
4250 | &pDisk->VDIIOCallbacks, pDisk, &pVDIfsImage);
|
---|
4251 | pImage->VDIo.pInterfaceIO = &pImage->VDIo.VDIIO;
|
---|
4252 | pImage->VDIo.pInterfaceIOCallbacks = &pDisk->VDIIOCallbacks;
|
---|
4253 | }
|
---|
4254 |
|
---|
4255 | /* Set up the internal I/O interface. */
|
---|
4256 | AssertBreakStmt(!VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IOINT),
|
---|
4257 | rc = VERR_INVALID_PARAMETER);
|
---|
4258 | rc = VDInterfaceAdd(&pImage->VDIo.VDIIOInt, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
4259 | &pDisk->VDIIOIntCallbacks, &pImage->VDIo, &pImage->pVDIfsImage);
|
---|
4260 | AssertRC(rc);
|
---|
4261 |
|
---|
4262 | pImage->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
|
---|
4263 | rc = pImage->Backend->pfnOpen(pImage->pszFilename,
|
---|
4264 | uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
|
---|
4265 | pDisk->pVDIfsDisk,
|
---|
4266 | pImage->pVDIfsImage,
|
---|
4267 | pDisk->enmType,
|
---|
4268 | &pImage->pBackendData);
|
---|
4269 | /* If the open in read-write mode failed, retry in read-only mode. */
|
---|
4270 | if (RT_FAILURE(rc))
|
---|
4271 | {
|
---|
4272 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
4273 | && ( rc == VERR_ACCESS_DENIED
|
---|
4274 | || rc == VERR_PERMISSION_DENIED
|
---|
4275 | || rc == VERR_WRITE_PROTECT
|
---|
4276 | || rc == VERR_SHARING_VIOLATION
|
---|
4277 | || rc == VERR_FILE_LOCK_FAILED))
|
---|
4278 | rc = pImage->Backend->pfnOpen(pImage->pszFilename,
|
---|
4279 | (uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME)
|
---|
4280 | | VD_OPEN_FLAGS_READONLY,
|
---|
4281 | pDisk->pVDIfsDisk,
|
---|
4282 | pImage->pVDIfsImage,
|
---|
4283 | pDisk->enmType,
|
---|
4284 | &pImage->pBackendData);
|
---|
4285 | if (RT_FAILURE(rc))
|
---|
4286 | {
|
---|
4287 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
4288 | N_("VD: error %Rrc opening image file '%s'"), rc, pszFilename);
|
---|
4289 | break;
|
---|
4290 | }
|
---|
4291 | }
|
---|
4292 |
|
---|
4293 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
4294 | rc2 = vdThreadStartWrite(pDisk);
|
---|
4295 | AssertRC(rc2);
|
---|
4296 | fLockWrite = true;
|
---|
4297 |
|
---|
4298 | pImage->VDIo.pBackendData = pImage->pBackendData;
|
---|
4299 |
|
---|
4300 | /* Check image type. As the image itself has only partial knowledge
|
---|
4301 | * whether it's a base image or not, this info is derived here. The
|
---|
4302 | * base image can be fixed or normal, all others must be normal or
|
---|
4303 | * diff images. Some image formats don't distinguish between normal
|
---|
4304 | * and diff images, so this must be corrected here. */
|
---|
4305 | unsigned uImageFlags;
|
---|
4306 | uImageFlags = pImage->Backend->pfnGetImageFlags(pImage->pBackendData);
|
---|
4307 | if (RT_FAILURE(rc))
|
---|
4308 | uImageFlags = VD_IMAGE_FLAGS_NONE;
|
---|
4309 | if ( RT_SUCCESS(rc)
|
---|
4310 | && !(uOpenFlags & VD_OPEN_FLAGS_INFO))
|
---|
4311 | {
|
---|
4312 | if ( pDisk->cImages == 0
|
---|
4313 | && (uImageFlags & VD_IMAGE_FLAGS_DIFF))
|
---|
4314 | {
|
---|
4315 | rc = VERR_VD_INVALID_TYPE;
|
---|
4316 | break;
|
---|
4317 | }
|
---|
4318 | else if (pDisk->cImages != 0)
|
---|
4319 | {
|
---|
4320 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
4321 | {
|
---|
4322 | rc = VERR_VD_INVALID_TYPE;
|
---|
4323 | break;
|
---|
4324 | }
|
---|
4325 | else
|
---|
4326 | uImageFlags |= VD_IMAGE_FLAGS_DIFF;
|
---|
4327 | }
|
---|
4328 | }
|
---|
4329 |
|
---|
4330 | /* Ensure we always get correct diff information, even if the backend
|
---|
4331 | * doesn't actually have a stored flag for this. It must not return
|
---|
4332 | * bogus information for the parent UUID if it is not a diff image. */
|
---|
4333 | RTUUID parentUuid;
|
---|
4334 | RTUuidClear(&parentUuid);
|
---|
4335 | rc2 = pImage->Backend->pfnGetParentUuid(pImage->pBackendData, &parentUuid);
|
---|
4336 | if (RT_SUCCESS(rc2) && !RTUuidIsNull(&parentUuid))
|
---|
4337 | uImageFlags |= VD_IMAGE_FLAGS_DIFF;
|
---|
4338 |
|
---|
4339 | pImage->uImageFlags = uImageFlags;
|
---|
4340 |
|
---|
4341 | /* Force sane optimization settings. It's not worth avoiding writes
|
---|
4342 | * to fixed size images. The overhead would have almost no payback. */
|
---|
4343 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
4344 | pImage->uOpenFlags |= VD_OPEN_FLAGS_HONOR_SAME;
|
---|
4345 |
|
---|
4346 | /** @todo optionally check UUIDs */
|
---|
4347 |
|
---|
4348 | /* Cache disk information. */
|
---|
4349 | pDisk->cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
|
---|
4350 |
|
---|
4351 | /* Cache PCHS geometry. */
|
---|
4352 | rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
4353 | &pDisk->PCHSGeometry);
|
---|
4354 | if (RT_FAILURE(rc2))
|
---|
4355 | {
|
---|
4356 | pDisk->PCHSGeometry.cCylinders = 0;
|
---|
4357 | pDisk->PCHSGeometry.cHeads = 0;
|
---|
4358 | pDisk->PCHSGeometry.cSectors = 0;
|
---|
4359 | }
|
---|
4360 | else
|
---|
4361 | {
|
---|
4362 | /* Make sure the PCHS geometry is properly clipped. */
|
---|
4363 | pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
|
---|
4364 | pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
|
---|
4365 | pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
|
---|
4366 | }
|
---|
4367 |
|
---|
4368 | /* Cache LCHS geometry. */
|
---|
4369 | rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
4370 | &pDisk->LCHSGeometry);
|
---|
4371 | if (RT_FAILURE(rc2))
|
---|
4372 | {
|
---|
4373 | pDisk->LCHSGeometry.cCylinders = 0;
|
---|
4374 | pDisk->LCHSGeometry.cHeads = 0;
|
---|
4375 | pDisk->LCHSGeometry.cSectors = 0;
|
---|
4376 | }
|
---|
4377 | else
|
---|
4378 | {
|
---|
4379 | /* Make sure the LCHS geometry is properly clipped. */
|
---|
4380 | pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
|
---|
4381 | pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
|
---|
4382 | }
|
---|
4383 |
|
---|
4384 | if (pDisk->cImages != 0)
|
---|
4385 | {
|
---|
4386 | /* Switch previous image to read-only mode. */
|
---|
4387 | unsigned uOpenFlagsPrevImg;
|
---|
4388 | uOpenFlagsPrevImg = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pBackendData);
|
---|
4389 | if (!(uOpenFlagsPrevImg & VD_OPEN_FLAGS_READONLY))
|
---|
4390 | {
|
---|
4391 | uOpenFlagsPrevImg |= VD_OPEN_FLAGS_READONLY;
|
---|
4392 | rc = pDisk->pLast->Backend->pfnSetOpenFlags(pDisk->pLast->pBackendData, uOpenFlagsPrevImg);
|
---|
4393 | }
|
---|
4394 | }
|
---|
4395 |
|
---|
4396 | if (RT_SUCCESS(rc))
|
---|
4397 | {
|
---|
4398 | /* Image successfully opened, make it the last image. */
|
---|
4399 | vdAddImageToList(pDisk, pImage);
|
---|
4400 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
4401 | pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
|
---|
4402 | }
|
---|
4403 | else
|
---|
4404 | {
|
---|
4405 | /* Error detected, but image opened. Close image. */
|
---|
4406 | rc2 = pImage->Backend->pfnClose(pImage->pBackendData, false);
|
---|
4407 | AssertRC(rc2);
|
---|
4408 | pImage->pBackendData = NULL;
|
---|
4409 | }
|
---|
4410 | } while (0);
|
---|
4411 |
|
---|
4412 | if (RT_UNLIKELY(fLockWrite))
|
---|
4413 | {
|
---|
4414 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
4415 | AssertRC(rc2);
|
---|
4416 | }
|
---|
4417 |
|
---|
4418 | if (RT_FAILURE(rc))
|
---|
4419 | {
|
---|
4420 | if (pImage)
|
---|
4421 | {
|
---|
4422 | if (pImage->pszFilename)
|
---|
4423 | RTStrFree(pImage->pszFilename);
|
---|
4424 | RTMemFree(pImage);
|
---|
4425 | }
|
---|
4426 | }
|
---|
4427 |
|
---|
4428 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
4429 | return rc;
|
---|
4430 | }
|
---|
4431 |
|
---|
4432 | /**
|
---|
4433 | * Opens a cache image.
|
---|
4434 | *
|
---|
4435 | * @return VBox status code.
|
---|
4436 | * @param pDisk Pointer to the HDD container which should use the cache image.
|
---|
4437 | * @param pszBackend Name of the cache file backend to use (case insensitive).
|
---|
4438 | * @param pszFilename Name of the cache image to open.
|
---|
4439 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
4440 | * @param pVDIfsCache Pointer to the per-cache VD interface list.
|
---|
4441 | */
|
---|
4442 | VBOXDDU_DECL(int) VDCacheOpen(PVBOXHDD pDisk, const char *pszBackend,
|
---|
4443 | const char *pszFilename, unsigned uOpenFlags,
|
---|
4444 | PVDINTERFACE pVDIfsCache)
|
---|
4445 | {
|
---|
4446 | int rc = VINF_SUCCESS;
|
---|
4447 | int rc2;
|
---|
4448 | bool fLockWrite = false;
|
---|
4449 | PVDCACHE pCache = NULL;
|
---|
4450 |
|
---|
4451 | LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uOpenFlags=%#x, pVDIfsCache=%#p\n",
|
---|
4452 | pDisk, pszBackend, pszFilename, uOpenFlags, pVDIfsCache));
|
---|
4453 |
|
---|
4454 | do
|
---|
4455 | {
|
---|
4456 | /* sanity check */
|
---|
4457 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
4458 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
4459 |
|
---|
4460 | /* Check arguments. */
|
---|
4461 | AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
|
---|
4462 | ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
|
---|
4463 | rc = VERR_INVALID_PARAMETER);
|
---|
4464 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
4465 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
4466 | rc = VERR_INVALID_PARAMETER);
|
---|
4467 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
4468 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
4469 | rc = VERR_INVALID_PARAMETER);
|
---|
4470 |
|
---|
4471 | /* Set up image descriptor. */
|
---|
4472 | pCache = (PVDCACHE)RTMemAllocZ(sizeof(VDCACHE));
|
---|
4473 | if (!pCache)
|
---|
4474 | {
|
---|
4475 | rc = VERR_NO_MEMORY;
|
---|
4476 | break;
|
---|
4477 | }
|
---|
4478 | pCache->pszFilename = RTStrDup(pszFilename);
|
---|
4479 | if (!pCache->pszFilename)
|
---|
4480 | {
|
---|
4481 | rc = VERR_NO_MEMORY;
|
---|
4482 | break;
|
---|
4483 | }
|
---|
4484 |
|
---|
4485 | pCache->VDIo.pDisk = pDisk;
|
---|
4486 | pCache->pVDIfsCache = pVDIfsCache;
|
---|
4487 |
|
---|
4488 | rc = vdFindCacheBackend(pszBackend, &pCache->Backend);
|
---|
4489 | if (RT_FAILURE(rc))
|
---|
4490 | break;
|
---|
4491 | if (!pCache->Backend)
|
---|
4492 | {
|
---|
4493 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
4494 | N_("VD: unknown backend name '%s'"), pszBackend);
|
---|
4495 | break;
|
---|
4496 | }
|
---|
4497 |
|
---|
4498 | /* Set up the I/O interface. */
|
---|
4499 | pCache->VDIo.pInterfaceIO = VDInterfaceGet(pVDIfsCache, VDINTERFACETYPE_IO);
|
---|
4500 | if (pCache->VDIo.pInterfaceIO)
|
---|
4501 | pCache->VDIo.pInterfaceIOCallbacks = VDGetInterfaceIO(pCache->VDIo.pInterfaceIO);
|
---|
4502 | else
|
---|
4503 | {
|
---|
4504 | rc = VDInterfaceAdd(&pCache->VDIo.VDIIO, "VD_IO", VDINTERFACETYPE_IO,
|
---|
4505 | &pDisk->VDIIOCallbacks, pDisk, &pVDIfsCache);
|
---|
4506 | pCache->VDIo.pInterfaceIO = &pCache->VDIo.VDIIO;
|
---|
4507 | pCache->VDIo.pInterfaceIOCallbacks = &pDisk->VDIIOCallbacks;
|
---|
4508 | }
|
---|
4509 |
|
---|
4510 | /* Set up the internal I/O interface. */
|
---|
4511 | AssertBreakStmt(!VDInterfaceGet(pVDIfsCache, VDINTERFACETYPE_IOINT),
|
---|
4512 | rc = VERR_INVALID_PARAMETER);
|
---|
4513 | rc = VDInterfaceAdd(&pCache->VDIo.VDIIOInt, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
4514 | &pDisk->VDIIOIntCallbacks, &pCache->VDIo, &pCache->pVDIfsCache);
|
---|
4515 | AssertRC(rc);
|
---|
4516 |
|
---|
4517 | pCache->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
|
---|
4518 | rc = pCache->Backend->pfnOpen(pCache->pszFilename,
|
---|
4519 | uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
|
---|
4520 | pDisk->pVDIfsDisk,
|
---|
4521 | pCache->pVDIfsCache,
|
---|
4522 | &pCache->pBackendData);
|
---|
4523 | /* If the open in read-write mode failed, retry in read-only mode. */
|
---|
4524 | if (RT_FAILURE(rc))
|
---|
4525 | {
|
---|
4526 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
4527 | && ( rc == VERR_ACCESS_DENIED
|
---|
4528 | || rc == VERR_PERMISSION_DENIED
|
---|
4529 | || rc == VERR_WRITE_PROTECT
|
---|
4530 | || rc == VERR_SHARING_VIOLATION
|
---|
4531 | || rc == VERR_FILE_LOCK_FAILED))
|
---|
4532 | rc = pCache->Backend->pfnOpen(pCache->pszFilename,
|
---|
4533 | (uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME)
|
---|
4534 | | VD_OPEN_FLAGS_READONLY,
|
---|
4535 | pDisk->pVDIfsDisk,
|
---|
4536 | pCache->pVDIfsCache,
|
---|
4537 | &pCache->pBackendData);
|
---|
4538 | if (RT_FAILURE(rc))
|
---|
4539 | {
|
---|
4540 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
4541 | N_("VD: error %Rrc opening image file '%s'"), rc, pszFilename);
|
---|
4542 | break;
|
---|
4543 | }
|
---|
4544 | }
|
---|
4545 |
|
---|
4546 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
4547 | rc2 = vdThreadStartWrite(pDisk);
|
---|
4548 | AssertRC(rc2);
|
---|
4549 | fLockWrite = true;
|
---|
4550 |
|
---|
4551 | /*
|
---|
4552 | * Check that the modification UUID of the cache and last image
|
---|
4553 | * match. If not the image was modified in-between without the cache.
|
---|
4554 | * The cache might contain stale data.
|
---|
4555 | */
|
---|
4556 | RTUUID UuidImage, UuidCache;
|
---|
4557 |
|
---|
4558 | rc = pCache->Backend->pfnGetModificationUuid(pCache->pBackendData,
|
---|
4559 | &UuidCache);
|
---|
4560 | if (RT_SUCCESS(rc))
|
---|
4561 | {
|
---|
4562 | rc = pDisk->pLast->Backend->pfnGetModificationUuid(pDisk->pLast->pBackendData,
|
---|
4563 | &UuidImage);
|
---|
4564 | if (RT_SUCCESS(rc))
|
---|
4565 | {
|
---|
4566 | if (RTUuidCompare(&UuidImage, &UuidCache))
|
---|
4567 | rc = VERR_VD_CACHE_NOT_UP_TO_DATE;
|
---|
4568 | }
|
---|
4569 | }
|
---|
4570 |
|
---|
4571 | /*
|
---|
4572 | * We assume that the user knows what he is doing if one of the images
|
---|
4573 | * doesn't support the modification uuid.
|
---|
4574 | */
|
---|
4575 | if (rc == VERR_NOT_SUPPORTED)
|
---|
4576 | rc = VINF_SUCCESS;
|
---|
4577 |
|
---|
4578 | if (RT_SUCCESS(rc))
|
---|
4579 | {
|
---|
4580 | /* Cache successfully opened, make it the current one. */
|
---|
4581 | if (!pDisk->pCache)
|
---|
4582 | pDisk->pCache = pCache;
|
---|
4583 | else
|
---|
4584 | rc = VERR_VD_CACHE_ALREADY_EXISTS;
|
---|
4585 | }
|
---|
4586 |
|
---|
4587 | if (RT_FAILURE(rc))
|
---|
4588 | {
|
---|
4589 | /* Error detected, but image opened. Close image. */
|
---|
4590 | rc2 = pCache->Backend->pfnClose(pCache->pBackendData, false);
|
---|
4591 | AssertRC(rc2);
|
---|
4592 | pCache->pBackendData = NULL;
|
---|
4593 | }
|
---|
4594 | } while (0);
|
---|
4595 |
|
---|
4596 | if (RT_UNLIKELY(fLockWrite))
|
---|
4597 | {
|
---|
4598 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
4599 | AssertRC(rc2);
|
---|
4600 | }
|
---|
4601 |
|
---|
4602 | if (RT_FAILURE(rc))
|
---|
4603 | {
|
---|
4604 | if (pCache)
|
---|
4605 | {
|
---|
4606 | if (pCache->pszFilename)
|
---|
4607 | RTStrFree(pCache->pszFilename);
|
---|
4608 | RTMemFree(pCache);
|
---|
4609 | }
|
---|
4610 | }
|
---|
4611 |
|
---|
4612 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
4613 | return rc;
|
---|
4614 | }
|
---|
4615 |
|
---|
4616 | /**
|
---|
4617 | * Creates and opens a new base image file.
|
---|
4618 | *
|
---|
4619 | * @returns VBox status code.
|
---|
4620 | * @param pDisk Pointer to HDD container.
|
---|
4621 | * @param pszBackend Name of the image file backend to use.
|
---|
4622 | * @param pszFilename Name of the image file to create.
|
---|
4623 | * @param cbSize Image size in bytes.
|
---|
4624 | * @param uImageFlags Flags specifying special image features.
|
---|
4625 | * @param pszComment Pointer to image comment. NULL is ok.
|
---|
4626 | * @param pPCHSGeometry Pointer to physical disk geometry <= (16383,16,63). Not NULL.
|
---|
4627 | * @param pLCHSGeometry Pointer to logical disk geometry <= (x,255,63). Not NULL.
|
---|
4628 | * @param pUuid New UUID of the image. If NULL, a new UUID is created.
|
---|
4629 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
4630 | * @param pVDIfsImage Pointer to the per-image VD interface list.
|
---|
4631 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
4632 | */
|
---|
4633 | VBOXDDU_DECL(int) VDCreateBase(PVBOXHDD pDisk, const char *pszBackend,
|
---|
4634 | const char *pszFilename, uint64_t cbSize,
|
---|
4635 | unsigned uImageFlags, const char *pszComment,
|
---|
4636 | PCVDGEOMETRY pPCHSGeometry,
|
---|
4637 | PCVDGEOMETRY pLCHSGeometry,
|
---|
4638 | PCRTUUID pUuid, unsigned uOpenFlags,
|
---|
4639 | PVDINTERFACE pVDIfsImage,
|
---|
4640 | PVDINTERFACE pVDIfsOperation)
|
---|
4641 | {
|
---|
4642 | int rc = VINF_SUCCESS;
|
---|
4643 | int rc2;
|
---|
4644 | bool fLockWrite = false, fLockRead = false;
|
---|
4645 | PVDIMAGE pImage = NULL;
|
---|
4646 | RTUUID uuid;
|
---|
4647 |
|
---|
4648 | LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" PCHS=%u/%u/%u LCHS=%u/%u/%u Uuid=%RTuuid uOpenFlags=%#x pVDIfsImage=%#p pVDIfsOperation=%#p\n",
|
---|
4649 | pDisk, pszBackend, pszFilename, cbSize, uImageFlags, pszComment,
|
---|
4650 | pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
|
---|
4651 | pPCHSGeometry->cSectors, pLCHSGeometry->cCylinders,
|
---|
4652 | pLCHSGeometry->cHeads, pLCHSGeometry->cSectors, pUuid,
|
---|
4653 | uOpenFlags, pVDIfsImage, pVDIfsOperation));
|
---|
4654 |
|
---|
4655 | PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
|
---|
4656 | VDINTERFACETYPE_PROGRESS);
|
---|
4657 | PVDINTERFACEPROGRESS pCbProgress = NULL;
|
---|
4658 | if (pIfProgress)
|
---|
4659 | pCbProgress = VDGetInterfaceProgress(pIfProgress);
|
---|
4660 |
|
---|
4661 | do
|
---|
4662 | {
|
---|
4663 | /* sanity check */
|
---|
4664 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
4665 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
4666 |
|
---|
4667 | /* Check arguments. */
|
---|
4668 | AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
|
---|
4669 | ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
|
---|
4670 | rc = VERR_INVALID_PARAMETER);
|
---|
4671 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
4672 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
4673 | rc = VERR_INVALID_PARAMETER);
|
---|
4674 | AssertMsgBreakStmt(cbSize,
|
---|
4675 | ("cbSize=%llu\n", cbSize),
|
---|
4676 | rc = VERR_INVALID_PARAMETER);
|
---|
4677 | AssertMsgBreakStmt( ((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0)
|
---|
4678 | || ((uImageFlags & (VD_IMAGE_FLAGS_FIXED | VD_IMAGE_FLAGS_DIFF)) != VD_IMAGE_FLAGS_FIXED),
|
---|
4679 | ("uImageFlags=%#x\n", uImageFlags),
|
---|
4680 | rc = VERR_INVALID_PARAMETER);
|
---|
4681 | /* The PCHS geometry fields may be 0 to leave it for later. */
|
---|
4682 | AssertMsgBreakStmt( VALID_PTR(pPCHSGeometry)
|
---|
4683 | && pPCHSGeometry->cHeads <= 16
|
---|
4684 | && pPCHSGeometry->cSectors <= 63,
|
---|
4685 | ("pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pPCHSGeometry,
|
---|
4686 | pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
|
---|
4687 | pPCHSGeometry->cSectors),
|
---|
4688 | rc = VERR_INVALID_PARAMETER);
|
---|
4689 | /* The LCHS geometry fields may be 0 to leave it to later autodetection. */
|
---|
4690 | AssertMsgBreakStmt( VALID_PTR(pLCHSGeometry)
|
---|
4691 | && pLCHSGeometry->cHeads <= 255
|
---|
4692 | && pLCHSGeometry->cSectors <= 63,
|
---|
4693 | ("pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pLCHSGeometry,
|
---|
4694 | pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads,
|
---|
4695 | pLCHSGeometry->cSectors),
|
---|
4696 | rc = VERR_INVALID_PARAMETER);
|
---|
4697 | /* The UUID may be NULL. */
|
---|
4698 | AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
|
---|
4699 | ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
|
---|
4700 | rc = VERR_INVALID_PARAMETER);
|
---|
4701 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
4702 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
4703 | rc = VERR_INVALID_PARAMETER);
|
---|
4704 |
|
---|
4705 | /* Check state. Needs a temporary read lock. Holding the write lock
|
---|
4706 | * all the time would be blocking other activities for too long. */
|
---|
4707 | rc2 = vdThreadStartRead(pDisk);
|
---|
4708 | AssertRC(rc2);
|
---|
4709 | fLockRead = true;
|
---|
4710 | AssertMsgBreakStmt(pDisk->cImages == 0,
|
---|
4711 | ("Create base image cannot be done with other images open\n"),
|
---|
4712 | rc = VERR_VD_INVALID_STATE);
|
---|
4713 | rc2 = vdThreadFinishRead(pDisk);
|
---|
4714 | AssertRC(rc2);
|
---|
4715 | fLockRead = false;
|
---|
4716 |
|
---|
4717 | /* Set up image descriptor. */
|
---|
4718 | pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
|
---|
4719 | if (!pImage)
|
---|
4720 | {
|
---|
4721 | rc = VERR_NO_MEMORY;
|
---|
4722 | break;
|
---|
4723 | }
|
---|
4724 | pImage->pszFilename = RTStrDup(pszFilename);
|
---|
4725 | if (!pImage->pszFilename)
|
---|
4726 | {
|
---|
4727 | rc = VERR_NO_MEMORY;
|
---|
4728 | break;
|
---|
4729 | }
|
---|
4730 | pImage->VDIo.pDisk = pDisk;
|
---|
4731 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
4732 |
|
---|
4733 | /* Set up the I/O interface. */
|
---|
4734 | pImage->VDIo.pInterfaceIO = VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IO);
|
---|
4735 | if (pImage->VDIo.pInterfaceIO)
|
---|
4736 | pImage->VDIo.pInterfaceIOCallbacks = VDGetInterfaceIO(pImage->VDIo.pInterfaceIO);
|
---|
4737 | else
|
---|
4738 | {
|
---|
4739 | rc = VDInterfaceAdd(&pImage->VDIo.VDIIO, "VD_IO", VDINTERFACETYPE_IO,
|
---|
4740 | &pDisk->VDIIOCallbacks, pDisk, &pVDIfsImage);
|
---|
4741 | pImage->VDIo.pInterfaceIO = &pImage->VDIo.VDIIO;
|
---|
4742 | pImage->VDIo.pInterfaceIOCallbacks = &pDisk->VDIIOCallbacks;
|
---|
4743 | }
|
---|
4744 |
|
---|
4745 | /* Set up the internal I/O interface. */
|
---|
4746 | AssertBreakStmt(!VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IOINT),
|
---|
4747 | rc = VERR_INVALID_PARAMETER);
|
---|
4748 | rc = VDInterfaceAdd(&pImage->VDIo.VDIIOInt, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
4749 | &pDisk->VDIIOIntCallbacks, &pImage->VDIo, &pImage->pVDIfsImage);
|
---|
4750 | AssertRC(rc);
|
---|
4751 |
|
---|
4752 | rc = vdFindBackend(pszBackend, &pImage->Backend);
|
---|
4753 | if (RT_FAILURE(rc))
|
---|
4754 | break;
|
---|
4755 | if (!pImage->Backend)
|
---|
4756 | {
|
---|
4757 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
4758 | N_("VD: unknown backend name '%s'"), pszBackend);
|
---|
4759 | break;
|
---|
4760 | }
|
---|
4761 | if (!(pImage->Backend->uBackendCaps & ( VD_CAP_CREATE_FIXED
|
---|
4762 | | VD_CAP_CREATE_DYNAMIC)))
|
---|
4763 | {
|
---|
4764 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
4765 | N_("VD: backend '%s' cannot create base images"), pszBackend);
|
---|
4766 | break;
|
---|
4767 | }
|
---|
4768 |
|
---|
4769 | /* Create UUID if the caller didn't specify one. */
|
---|
4770 | if (!pUuid)
|
---|
4771 | {
|
---|
4772 | rc = RTUuidCreate(&uuid);
|
---|
4773 | if (RT_FAILURE(rc))
|
---|
4774 | {
|
---|
4775 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
4776 | N_("VD: cannot generate UUID for image '%s'"),
|
---|
4777 | pszFilename);
|
---|
4778 | break;
|
---|
4779 | }
|
---|
4780 | pUuid = &uuid;
|
---|
4781 | }
|
---|
4782 |
|
---|
4783 | pImage->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
|
---|
4784 | uImageFlags &= ~VD_IMAGE_FLAGS_DIFF;
|
---|
4785 | rc = pImage->Backend->pfnCreate(pImage->pszFilename, cbSize,
|
---|
4786 | uImageFlags, pszComment, pPCHSGeometry,
|
---|
4787 | pLCHSGeometry, pUuid,
|
---|
4788 | uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
|
---|
4789 | 0, 99,
|
---|
4790 | pDisk->pVDIfsDisk,
|
---|
4791 | pImage->pVDIfsImage,
|
---|
4792 | pVDIfsOperation,
|
---|
4793 | &pImage->pBackendData);
|
---|
4794 |
|
---|
4795 | if (RT_SUCCESS(rc))
|
---|
4796 | {
|
---|
4797 | pImage->VDIo.pBackendData = pImage->pBackendData;
|
---|
4798 | pImage->uImageFlags = uImageFlags;
|
---|
4799 |
|
---|
4800 | /* Force sane optimization settings. It's not worth avoiding writes
|
---|
4801 | * to fixed size images. The overhead would have almost no payback. */
|
---|
4802 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
4803 | pImage->uOpenFlags |= VD_OPEN_FLAGS_HONOR_SAME;
|
---|
4804 |
|
---|
4805 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
4806 | rc2 = vdThreadStartWrite(pDisk);
|
---|
4807 | AssertRC(rc2);
|
---|
4808 | fLockWrite = true;
|
---|
4809 |
|
---|
4810 | /** @todo optionally check UUIDs */
|
---|
4811 |
|
---|
4812 | /* Re-check state, as the lock wasn't held and another image
|
---|
4813 | * creation call could have been done by another thread. */
|
---|
4814 | AssertMsgStmt(pDisk->cImages == 0,
|
---|
4815 | ("Create base image cannot be done with other images open\n"),
|
---|
4816 | rc = VERR_VD_INVALID_STATE);
|
---|
4817 | }
|
---|
4818 |
|
---|
4819 | if (RT_SUCCESS(rc))
|
---|
4820 | {
|
---|
4821 | /* Cache disk information. */
|
---|
4822 | pDisk->cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
|
---|
4823 |
|
---|
4824 | /* Cache PCHS geometry. */
|
---|
4825 | rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
4826 | &pDisk->PCHSGeometry);
|
---|
4827 | if (RT_FAILURE(rc2))
|
---|
4828 | {
|
---|
4829 | pDisk->PCHSGeometry.cCylinders = 0;
|
---|
4830 | pDisk->PCHSGeometry.cHeads = 0;
|
---|
4831 | pDisk->PCHSGeometry.cSectors = 0;
|
---|
4832 | }
|
---|
4833 | else
|
---|
4834 | {
|
---|
4835 | /* Make sure the CHS geometry is properly clipped. */
|
---|
4836 | pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
|
---|
4837 | pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
|
---|
4838 | pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
|
---|
4839 | }
|
---|
4840 |
|
---|
4841 | /* Cache LCHS geometry. */
|
---|
4842 | rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
4843 | &pDisk->LCHSGeometry);
|
---|
4844 | if (RT_FAILURE(rc2))
|
---|
4845 | {
|
---|
4846 | pDisk->LCHSGeometry.cCylinders = 0;
|
---|
4847 | pDisk->LCHSGeometry.cHeads = 0;
|
---|
4848 | pDisk->LCHSGeometry.cSectors = 0;
|
---|
4849 | }
|
---|
4850 | else
|
---|
4851 | {
|
---|
4852 | /* Make sure the CHS geometry is properly clipped. */
|
---|
4853 | pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
|
---|
4854 | pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
|
---|
4855 | }
|
---|
4856 |
|
---|
4857 | /* Image successfully opened, make it the last image. */
|
---|
4858 | vdAddImageToList(pDisk, pImage);
|
---|
4859 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
4860 | pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
|
---|
4861 | }
|
---|
4862 | else
|
---|
4863 | {
|
---|
4864 | /* Error detected, image may or may not be opened. Close and delete
|
---|
4865 | * image if it was opened. */
|
---|
4866 | if (pImage->pBackendData)
|
---|
4867 | {
|
---|
4868 | rc2 = pImage->Backend->pfnClose(pImage->pBackendData, true);
|
---|
4869 | AssertRC(rc2);
|
---|
4870 | pImage->pBackendData = NULL;
|
---|
4871 | }
|
---|
4872 | }
|
---|
4873 | } while (0);
|
---|
4874 |
|
---|
4875 | if (RT_UNLIKELY(fLockWrite))
|
---|
4876 | {
|
---|
4877 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
4878 | AssertRC(rc2);
|
---|
4879 | }
|
---|
4880 | else if (RT_UNLIKELY(fLockRead))
|
---|
4881 | {
|
---|
4882 | rc2 = vdThreadFinishRead(pDisk);
|
---|
4883 | AssertRC(rc2);
|
---|
4884 | }
|
---|
4885 |
|
---|
4886 | if (RT_FAILURE(rc))
|
---|
4887 | {
|
---|
4888 | if (pImage)
|
---|
4889 | {
|
---|
4890 | if (pImage->pszFilename)
|
---|
4891 | RTStrFree(pImage->pszFilename);
|
---|
4892 | RTMemFree(pImage);
|
---|
4893 | }
|
---|
4894 | }
|
---|
4895 |
|
---|
4896 | if (RT_SUCCESS(rc) && pCbProgress && pCbProgress->pfnProgress)
|
---|
4897 | pCbProgress->pfnProgress(pIfProgress->pvUser, 100);
|
---|
4898 |
|
---|
4899 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
4900 | return rc;
|
---|
4901 | }
|
---|
4902 |
|
---|
4903 | /**
|
---|
4904 | * Creates and opens a new differencing image file in HDD container.
|
---|
4905 | * See comments for VDOpen function about differencing images.
|
---|
4906 | *
|
---|
4907 | * @returns VBox status code.
|
---|
4908 | * @param pDisk Pointer to HDD container.
|
---|
4909 | * @param pszBackend Name of the image file backend to use.
|
---|
4910 | * @param pszFilename Name of the differencing image file to create.
|
---|
4911 | * @param uImageFlags Flags specifying special image features.
|
---|
4912 | * @param pszComment Pointer to image comment. NULL is ok.
|
---|
4913 | * @param pUuid New UUID of the image. If NULL, a new UUID is created.
|
---|
4914 | * @param pParentUuid New parent UUID of the image. If NULL, the UUID is queried automatically.
|
---|
4915 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
4916 | * @param pVDIfsImage Pointer to the per-image VD interface list.
|
---|
4917 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
4918 | */
|
---|
4919 | VBOXDDU_DECL(int) VDCreateDiff(PVBOXHDD pDisk, const char *pszBackend,
|
---|
4920 | const char *pszFilename, unsigned uImageFlags,
|
---|
4921 | const char *pszComment, PCRTUUID pUuid,
|
---|
4922 | PCRTUUID pParentUuid, unsigned uOpenFlags,
|
---|
4923 | PVDINTERFACE pVDIfsImage,
|
---|
4924 | PVDINTERFACE pVDIfsOperation)
|
---|
4925 | {
|
---|
4926 | int rc = VINF_SUCCESS;
|
---|
4927 | int rc2;
|
---|
4928 | bool fLockWrite = false, fLockRead = false;
|
---|
4929 | PVDIMAGE pImage = NULL;
|
---|
4930 | RTUUID uuid;
|
---|
4931 |
|
---|
4932 | LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uImageFlags=%#x pszComment=\"%s\" Uuid=%RTuuid uOpenFlags=%#x pVDIfsImage=%#p pVDIfsOperation=%#p\n",
|
---|
4933 | pDisk, pszBackend, pszFilename, uImageFlags, pszComment, pUuid, uOpenFlags, pVDIfsImage, pVDIfsOperation));
|
---|
4934 |
|
---|
4935 | PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
|
---|
4936 | VDINTERFACETYPE_PROGRESS);
|
---|
4937 | PVDINTERFACEPROGRESS pCbProgress = NULL;
|
---|
4938 | if (pIfProgress)
|
---|
4939 | pCbProgress = VDGetInterfaceProgress(pIfProgress);
|
---|
4940 |
|
---|
4941 | do
|
---|
4942 | {
|
---|
4943 | /* sanity check */
|
---|
4944 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
4945 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
4946 |
|
---|
4947 | /* Check arguments. */
|
---|
4948 | AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
|
---|
4949 | ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
|
---|
4950 | rc = VERR_INVALID_PARAMETER);
|
---|
4951 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
4952 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
4953 | rc = VERR_INVALID_PARAMETER);
|
---|
4954 | AssertMsgBreakStmt((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0,
|
---|
4955 | ("uImageFlags=%#x\n", uImageFlags),
|
---|
4956 | rc = VERR_INVALID_PARAMETER);
|
---|
4957 | /* The UUID may be NULL. */
|
---|
4958 | AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
|
---|
4959 | ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
|
---|
4960 | rc = VERR_INVALID_PARAMETER);
|
---|
4961 | /* The parent UUID may be NULL. */
|
---|
4962 | AssertMsgBreakStmt(pParentUuid == NULL || VALID_PTR(pParentUuid),
|
---|
4963 | ("pParentUuid=%#p ParentUUID=%RTuuid\n", pParentUuid, pParentUuid),
|
---|
4964 | rc = VERR_INVALID_PARAMETER);
|
---|
4965 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
4966 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
4967 | rc = VERR_INVALID_PARAMETER);
|
---|
4968 |
|
---|
4969 | /* Check state. Needs a temporary read lock. Holding the write lock
|
---|
4970 | * all the time would be blocking other activities for too long. */
|
---|
4971 | rc2 = vdThreadStartRead(pDisk);
|
---|
4972 | AssertRC(rc2);
|
---|
4973 | fLockRead = true;
|
---|
4974 | AssertMsgBreakStmt(pDisk->cImages != 0,
|
---|
4975 | ("Create diff image cannot be done without other images open\n"),
|
---|
4976 | rc = VERR_VD_INVALID_STATE);
|
---|
4977 | rc2 = vdThreadFinishRead(pDisk);
|
---|
4978 | AssertRC(rc2);
|
---|
4979 | fLockRead = false;
|
---|
4980 |
|
---|
4981 | /* Set up image descriptor. */
|
---|
4982 | pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
|
---|
4983 | if (!pImage)
|
---|
4984 | {
|
---|
4985 | rc = VERR_NO_MEMORY;
|
---|
4986 | break;
|
---|
4987 | }
|
---|
4988 | pImage->pszFilename = RTStrDup(pszFilename);
|
---|
4989 | if (!pImage->pszFilename)
|
---|
4990 | {
|
---|
4991 | rc = VERR_NO_MEMORY;
|
---|
4992 | break;
|
---|
4993 | }
|
---|
4994 |
|
---|
4995 | rc = vdFindBackend(pszBackend, &pImage->Backend);
|
---|
4996 | if (RT_FAILURE(rc))
|
---|
4997 | break;
|
---|
4998 | if (!pImage->Backend)
|
---|
4999 | {
|
---|
5000 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
5001 | N_("VD: unknown backend name '%s'"), pszBackend);
|
---|
5002 | break;
|
---|
5003 | }
|
---|
5004 | if ( !(pImage->Backend->uBackendCaps & VD_CAP_DIFF)
|
---|
5005 | || !(pImage->Backend->uBackendCaps & ( VD_CAP_CREATE_FIXED
|
---|
5006 | | VD_CAP_CREATE_DYNAMIC)))
|
---|
5007 | {
|
---|
5008 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
5009 | N_("VD: backend '%s' cannot create diff images"), pszBackend);
|
---|
5010 | break;
|
---|
5011 | }
|
---|
5012 |
|
---|
5013 | pImage->VDIo.pDisk = pDisk;
|
---|
5014 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
5015 |
|
---|
5016 | /* Set up the I/O interface. */
|
---|
5017 | pImage->VDIo.pInterfaceIO = VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IO);
|
---|
5018 | if (pImage->VDIo.pInterfaceIO)
|
---|
5019 | pImage->VDIo.pInterfaceIOCallbacks = VDGetInterfaceIO(pImage->VDIo.pInterfaceIO);
|
---|
5020 | else
|
---|
5021 | {
|
---|
5022 | rc = VDInterfaceAdd(&pImage->VDIo.VDIIO, "VD_IO", VDINTERFACETYPE_IO,
|
---|
5023 | &pDisk->VDIIOCallbacks, pDisk, &pVDIfsImage);
|
---|
5024 | pImage->VDIo.pInterfaceIO = &pImage->VDIo.VDIIO;
|
---|
5025 | pImage->VDIo.pInterfaceIOCallbacks = &pDisk->VDIIOCallbacks;
|
---|
5026 | }
|
---|
5027 |
|
---|
5028 | /* Set up the internal I/O interface. */
|
---|
5029 | AssertBreakStmt(!VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IOINT),
|
---|
5030 | rc = VERR_INVALID_PARAMETER);
|
---|
5031 | rc = VDInterfaceAdd(&pImage->VDIo.VDIIOInt, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
5032 | &pDisk->VDIIOIntCallbacks, &pImage->VDIo, &pImage->pVDIfsImage);
|
---|
5033 | AssertRC(rc);
|
---|
5034 |
|
---|
5035 | /* Create UUID if the caller didn't specify one. */
|
---|
5036 | if (!pUuid)
|
---|
5037 | {
|
---|
5038 | rc = RTUuidCreate(&uuid);
|
---|
5039 | if (RT_FAILURE(rc))
|
---|
5040 | {
|
---|
5041 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
5042 | N_("VD: cannot generate UUID for image '%s'"),
|
---|
5043 | pszFilename);
|
---|
5044 | break;
|
---|
5045 | }
|
---|
5046 | pUuid = &uuid;
|
---|
5047 | }
|
---|
5048 |
|
---|
5049 | pImage->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
|
---|
5050 | uImageFlags |= VD_IMAGE_FLAGS_DIFF;
|
---|
5051 | rc = pImage->Backend->pfnCreate(pImage->pszFilename, pDisk->cbSize,
|
---|
5052 | uImageFlags | VD_IMAGE_FLAGS_DIFF,
|
---|
5053 | pszComment, &pDisk->PCHSGeometry,
|
---|
5054 | &pDisk->LCHSGeometry, pUuid,
|
---|
5055 | uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
|
---|
5056 | 0, 99,
|
---|
5057 | pDisk->pVDIfsDisk,
|
---|
5058 | pImage->pVDIfsImage,
|
---|
5059 | pVDIfsOperation,
|
---|
5060 | &pImage->pBackendData);
|
---|
5061 |
|
---|
5062 | if (RT_SUCCESS(rc))
|
---|
5063 | {
|
---|
5064 | pImage->VDIo.pBackendData = pImage->pBackendData;
|
---|
5065 | pImage->uImageFlags = uImageFlags;
|
---|
5066 |
|
---|
5067 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
5068 | rc2 = vdThreadStartWrite(pDisk);
|
---|
5069 | AssertRC(rc2);
|
---|
5070 | fLockWrite = true;
|
---|
5071 |
|
---|
5072 | /* Switch previous image to read-only mode. */
|
---|
5073 | unsigned uOpenFlagsPrevImg;
|
---|
5074 | uOpenFlagsPrevImg = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pBackendData);
|
---|
5075 | if (!(uOpenFlagsPrevImg & VD_OPEN_FLAGS_READONLY))
|
---|
5076 | {
|
---|
5077 | uOpenFlagsPrevImg |= VD_OPEN_FLAGS_READONLY;
|
---|
5078 | rc = pDisk->pLast->Backend->pfnSetOpenFlags(pDisk->pLast->pBackendData, uOpenFlagsPrevImg);
|
---|
5079 | }
|
---|
5080 |
|
---|
5081 | /** @todo optionally check UUIDs */
|
---|
5082 |
|
---|
5083 | /* Re-check state, as the lock wasn't held and another image
|
---|
5084 | * creation call could have been done by another thread. */
|
---|
5085 | AssertMsgStmt(pDisk->cImages != 0,
|
---|
5086 | ("Create diff image cannot be done without other images open\n"),
|
---|
5087 | rc = VERR_VD_INVALID_STATE);
|
---|
5088 | }
|
---|
5089 |
|
---|
5090 | if (RT_SUCCESS(rc))
|
---|
5091 | {
|
---|
5092 | RTUUID Uuid;
|
---|
5093 | RTTIMESPEC ts;
|
---|
5094 |
|
---|
5095 | if (pParentUuid && !RTUuidIsNull(pParentUuid))
|
---|
5096 | {
|
---|
5097 | Uuid = *pParentUuid;
|
---|
5098 | pImage->Backend->pfnSetParentUuid(pImage->pBackendData, &Uuid);
|
---|
5099 | }
|
---|
5100 | else
|
---|
5101 | {
|
---|
5102 | rc2 = pDisk->pLast->Backend->pfnGetUuid(pDisk->pLast->pBackendData,
|
---|
5103 | &Uuid);
|
---|
5104 | if (RT_SUCCESS(rc2))
|
---|
5105 | pImage->Backend->pfnSetParentUuid(pImage->pBackendData, &Uuid);
|
---|
5106 | }
|
---|
5107 | rc2 = pDisk->pLast->Backend->pfnGetModificationUuid(pDisk->pLast->pBackendData,
|
---|
5108 | &Uuid);
|
---|
5109 | if (RT_SUCCESS(rc2))
|
---|
5110 | pImage->Backend->pfnSetParentModificationUuid(pImage->pBackendData,
|
---|
5111 | &Uuid);
|
---|
5112 | if (pDisk->pLast->Backend->pfnGetTimeStamp)
|
---|
5113 | rc2 = pDisk->pLast->Backend->pfnGetTimeStamp(pDisk->pLast->pBackendData,
|
---|
5114 | &ts);
|
---|
5115 | else
|
---|
5116 | rc2 = VERR_NOT_IMPLEMENTED;
|
---|
5117 | if (RT_SUCCESS(rc2) && pImage->Backend->pfnSetParentTimeStamp)
|
---|
5118 | pImage->Backend->pfnSetParentTimeStamp(pImage->pBackendData, &ts);
|
---|
5119 |
|
---|
5120 | if (pImage->Backend->pfnSetParentFilename)
|
---|
5121 | rc2 = pImage->Backend->pfnSetParentFilename(pImage->pBackendData, pDisk->pLast->pszFilename);
|
---|
5122 | }
|
---|
5123 |
|
---|
5124 | if (RT_SUCCESS(rc))
|
---|
5125 | {
|
---|
5126 | /* Image successfully opened, make it the last image. */
|
---|
5127 | vdAddImageToList(pDisk, pImage);
|
---|
5128 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
5129 | pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
|
---|
5130 | }
|
---|
5131 | else
|
---|
5132 | {
|
---|
5133 | /* Error detected, but image opened. Close and delete image. */
|
---|
5134 | rc2 = pImage->Backend->pfnClose(pImage->pBackendData, true);
|
---|
5135 | AssertRC(rc2);
|
---|
5136 | pImage->pBackendData = NULL;
|
---|
5137 | }
|
---|
5138 | } while (0);
|
---|
5139 |
|
---|
5140 | if (RT_UNLIKELY(fLockWrite))
|
---|
5141 | {
|
---|
5142 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
5143 | AssertRC(rc2);
|
---|
5144 | }
|
---|
5145 | else if (RT_UNLIKELY(fLockRead))
|
---|
5146 | {
|
---|
5147 | rc2 = vdThreadFinishRead(pDisk);
|
---|
5148 | AssertRC(rc2);
|
---|
5149 | }
|
---|
5150 |
|
---|
5151 | if (RT_FAILURE(rc))
|
---|
5152 | {
|
---|
5153 | if (pImage)
|
---|
5154 | {
|
---|
5155 | if (pImage->pszFilename)
|
---|
5156 | RTStrFree(pImage->pszFilename);
|
---|
5157 | RTMemFree(pImage);
|
---|
5158 | }
|
---|
5159 | }
|
---|
5160 |
|
---|
5161 | if (RT_SUCCESS(rc) && pCbProgress && pCbProgress->pfnProgress)
|
---|
5162 | pCbProgress->pfnProgress(pIfProgress->pvUser, 100);
|
---|
5163 |
|
---|
5164 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
5165 | return rc;
|
---|
5166 | }
|
---|
5167 |
|
---|
5168 |
|
---|
5169 | /**
|
---|
5170 | * Creates and opens new cache image file in HDD container.
|
---|
5171 | *
|
---|
5172 | * @return VBox status code.
|
---|
5173 | * @param pDisk Name of the cache file backend to use (case insensitive).
|
---|
5174 | * @param pszFilename Name of the differencing cache file to create.
|
---|
5175 | * @param cbSize Maximum size of the cache.
|
---|
5176 | * @param uImageFlags Flags specifying special cache features.
|
---|
5177 | * @param pszComment Pointer to image comment. NULL is ok.
|
---|
5178 | * @param pUuid New UUID of the image. If NULL, a new UUID is created.
|
---|
5179 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
5180 | * @param pVDIfsCache Pointer to the per-cache VD interface list.
|
---|
5181 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
5182 | */
|
---|
5183 | VBOXDDU_DECL(int) VDCreateCache(PVBOXHDD pDisk, const char *pszBackend,
|
---|
5184 | const char *pszFilename, uint64_t cbSize,
|
---|
5185 | unsigned uImageFlags, const char *pszComment,
|
---|
5186 | PCRTUUID pUuid, unsigned uOpenFlags,
|
---|
5187 | PVDINTERFACE pVDIfsCache, PVDINTERFACE pVDIfsOperation)
|
---|
5188 | {
|
---|
5189 | int rc = VINF_SUCCESS;
|
---|
5190 | int rc2;
|
---|
5191 | bool fLockWrite = false, fLockRead = false;
|
---|
5192 | PVDCACHE pCache = NULL;
|
---|
5193 | RTUUID uuid;
|
---|
5194 |
|
---|
5195 | LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" Uuid=%RTuuid uOpenFlags=%#x pVDIfsImage=%#p pVDIfsOperation=%#p\n",
|
---|
5196 | pDisk, pszBackend, pszFilename, cbSize, uImageFlags, pszComment, pUuid, uOpenFlags, pVDIfsCache, pVDIfsOperation));
|
---|
5197 |
|
---|
5198 | PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
|
---|
5199 | VDINTERFACETYPE_PROGRESS);
|
---|
5200 | PVDINTERFACEPROGRESS pCbProgress = NULL;
|
---|
5201 | if (pIfProgress)
|
---|
5202 | pCbProgress = VDGetInterfaceProgress(pIfProgress);
|
---|
5203 |
|
---|
5204 | do
|
---|
5205 | {
|
---|
5206 | /* sanity check */
|
---|
5207 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
5208 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
5209 |
|
---|
5210 | /* Check arguments. */
|
---|
5211 | AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
|
---|
5212 | ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
|
---|
5213 | rc = VERR_INVALID_PARAMETER);
|
---|
5214 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
5215 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
5216 | rc = VERR_INVALID_PARAMETER);
|
---|
5217 | AssertMsgBreakStmt(cbSize,
|
---|
5218 | ("cbSize=%llu\n", cbSize),
|
---|
5219 | rc = VERR_INVALID_PARAMETER);
|
---|
5220 | AssertMsgBreakStmt((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0,
|
---|
5221 | ("uImageFlags=%#x\n", uImageFlags),
|
---|
5222 | rc = VERR_INVALID_PARAMETER);
|
---|
5223 | /* The UUID may be NULL. */
|
---|
5224 | AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
|
---|
5225 | ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
|
---|
5226 | rc = VERR_INVALID_PARAMETER);
|
---|
5227 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
5228 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
5229 | rc = VERR_INVALID_PARAMETER);
|
---|
5230 |
|
---|
5231 | /* Check state. Needs a temporary read lock. Holding the write lock
|
---|
5232 | * all the time would be blocking other activities for too long. */
|
---|
5233 | rc2 = vdThreadStartRead(pDisk);
|
---|
5234 | AssertRC(rc2);
|
---|
5235 | fLockRead = true;
|
---|
5236 | AssertMsgBreakStmt(!pDisk->pCache,
|
---|
5237 | ("Create cache image cannot be done with a cache already attached\n"),
|
---|
5238 | rc = VERR_VD_CACHE_ALREADY_EXISTS);
|
---|
5239 | rc2 = vdThreadFinishRead(pDisk);
|
---|
5240 | AssertRC(rc2);
|
---|
5241 | fLockRead = false;
|
---|
5242 |
|
---|
5243 | /* Set up image descriptor. */
|
---|
5244 | pCache = (PVDCACHE)RTMemAllocZ(sizeof(VDCACHE));
|
---|
5245 | if (!pCache)
|
---|
5246 | {
|
---|
5247 | rc = VERR_NO_MEMORY;
|
---|
5248 | break;
|
---|
5249 | }
|
---|
5250 | pCache->pszFilename = RTStrDup(pszFilename);
|
---|
5251 | if (!pCache->pszFilename)
|
---|
5252 | {
|
---|
5253 | rc = VERR_NO_MEMORY;
|
---|
5254 | break;
|
---|
5255 | }
|
---|
5256 |
|
---|
5257 | rc = vdFindCacheBackend(pszBackend, &pCache->Backend);
|
---|
5258 | if (RT_FAILURE(rc))
|
---|
5259 | break;
|
---|
5260 | if (!pCache->Backend)
|
---|
5261 | {
|
---|
5262 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
5263 | N_("VD: unknown backend name '%s'"), pszBackend);
|
---|
5264 | break;
|
---|
5265 | }
|
---|
5266 |
|
---|
5267 | pCache->VDIo.pDisk = pDisk;
|
---|
5268 | pCache->pVDIfsCache = pVDIfsCache;
|
---|
5269 |
|
---|
5270 | /* Set up the I/O interface. */
|
---|
5271 | pCache->VDIo.pInterfaceIO = VDInterfaceGet(pVDIfsCache, VDINTERFACETYPE_IO);
|
---|
5272 | if (pCache->VDIo.pInterfaceIO)
|
---|
5273 | pCache->VDIo.pInterfaceIOCallbacks = VDGetInterfaceIO(pCache->VDIo.pInterfaceIO);
|
---|
5274 | else
|
---|
5275 | {
|
---|
5276 | rc = VDInterfaceAdd(&pCache->VDIo.VDIIO, "VD_IO", VDINTERFACETYPE_IO,
|
---|
5277 | &pDisk->VDIIOCallbacks, pDisk, &pVDIfsCache);
|
---|
5278 | pCache->VDIo.pInterfaceIO = &pCache->VDIo.VDIIO;
|
---|
5279 | pCache->VDIo.pInterfaceIOCallbacks = &pDisk->VDIIOCallbacks;
|
---|
5280 | }
|
---|
5281 |
|
---|
5282 | /* Set up the internal I/O interface. */
|
---|
5283 | AssertBreakStmt(!VDInterfaceGet(pVDIfsCache, VDINTERFACETYPE_IOINT),
|
---|
5284 | rc = VERR_INVALID_PARAMETER);
|
---|
5285 | rc = VDInterfaceAdd(&pCache->VDIo.VDIIOInt, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
5286 | &pDisk->VDIIOIntCallbacks, &pCache->VDIo, &pCache->pVDIfsCache);
|
---|
5287 | AssertRC(rc);
|
---|
5288 |
|
---|
5289 | /* Create UUID if the caller didn't specify one. */
|
---|
5290 | if (!pUuid)
|
---|
5291 | {
|
---|
5292 | rc = RTUuidCreate(&uuid);
|
---|
5293 | if (RT_FAILURE(rc))
|
---|
5294 | {
|
---|
5295 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
5296 | N_("VD: cannot generate UUID for image '%s'"),
|
---|
5297 | pszFilename);
|
---|
5298 | break;
|
---|
5299 | }
|
---|
5300 | pUuid = &uuid;
|
---|
5301 | }
|
---|
5302 |
|
---|
5303 | pCache->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
|
---|
5304 | rc = pCache->Backend->pfnCreate(pCache->pszFilename, cbSize,
|
---|
5305 | uImageFlags,
|
---|
5306 | pszComment, pUuid,
|
---|
5307 | uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
|
---|
5308 | 0, 99,
|
---|
5309 | pDisk->pVDIfsDisk,
|
---|
5310 | pCache->pVDIfsCache,
|
---|
5311 | pVDIfsOperation,
|
---|
5312 | &pCache->pBackendData);
|
---|
5313 |
|
---|
5314 | if (RT_SUCCESS(rc))
|
---|
5315 | {
|
---|
5316 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
5317 | rc2 = vdThreadStartWrite(pDisk);
|
---|
5318 | AssertRC(rc2);
|
---|
5319 | fLockWrite = true;
|
---|
5320 |
|
---|
5321 | pCache->VDIo.pBackendData = pCache->pBackendData;
|
---|
5322 |
|
---|
5323 | /* Re-check state, as the lock wasn't held and another image
|
---|
5324 | * creation call could have been done by another thread. */
|
---|
5325 | AssertMsgStmt(!pDisk->pCache,
|
---|
5326 | ("Create cache image cannot be done with another cache open\n"),
|
---|
5327 | rc = VERR_VD_CACHE_ALREADY_EXISTS);
|
---|
5328 | }
|
---|
5329 |
|
---|
5330 | if ( RT_SUCCESS(rc)
|
---|
5331 | && pDisk->pLast)
|
---|
5332 | {
|
---|
5333 | RTUUID UuidModification;
|
---|
5334 |
|
---|
5335 | /* Set same modification Uuid as the last image. */
|
---|
5336 | rc = pDisk->pLast->Backend->pfnGetModificationUuid(pDisk->pLast->pBackendData,
|
---|
5337 | &UuidModification);
|
---|
5338 | if (RT_SUCCESS(rc))
|
---|
5339 | {
|
---|
5340 | rc = pCache->Backend->pfnSetModificationUuid(pCache->pBackendData,
|
---|
5341 | &UuidModification);
|
---|
5342 | }
|
---|
5343 |
|
---|
5344 | if (rc == VERR_NOT_SUPPORTED)
|
---|
5345 | rc = VINF_SUCCESS;
|
---|
5346 | }
|
---|
5347 |
|
---|
5348 | if (RT_SUCCESS(rc))
|
---|
5349 | {
|
---|
5350 | /* Cache successfully created. */
|
---|
5351 | pDisk->pCache = pCache;
|
---|
5352 | }
|
---|
5353 | else
|
---|
5354 | {
|
---|
5355 | /* Error detected, but image opened. Close and delete image. */
|
---|
5356 | rc2 = pCache->Backend->pfnClose(pCache->pBackendData, true);
|
---|
5357 | AssertRC(rc2);
|
---|
5358 | pCache->pBackendData = NULL;
|
---|
5359 | }
|
---|
5360 | } while (0);
|
---|
5361 |
|
---|
5362 | if (RT_UNLIKELY(fLockWrite))
|
---|
5363 | {
|
---|
5364 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
5365 | AssertRC(rc2);
|
---|
5366 | }
|
---|
5367 | else if (RT_UNLIKELY(fLockRead))
|
---|
5368 | {
|
---|
5369 | rc2 = vdThreadFinishRead(pDisk);
|
---|
5370 | AssertRC(rc2);
|
---|
5371 | }
|
---|
5372 |
|
---|
5373 | if (RT_FAILURE(rc))
|
---|
5374 | {
|
---|
5375 | if (pCache)
|
---|
5376 | {
|
---|
5377 | if (pCache->pszFilename)
|
---|
5378 | RTStrFree(pCache->pszFilename);
|
---|
5379 | RTMemFree(pCache);
|
---|
5380 | }
|
---|
5381 | }
|
---|
5382 |
|
---|
5383 | if (RT_SUCCESS(rc) && pCbProgress && pCbProgress->pfnProgress)
|
---|
5384 | pCbProgress->pfnProgress(pIfProgress->pvUser, 100);
|
---|
5385 |
|
---|
5386 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
5387 | return rc;
|
---|
5388 | }
|
---|
5389 |
|
---|
5390 | /**
|
---|
5391 | * Merges two images (not necessarily with direct parent/child relationship).
|
---|
5392 | * As a side effect the source image and potentially the other images which
|
---|
5393 | * are also merged to the destination are deleted from both the disk and the
|
---|
5394 | * images in the HDD container.
|
---|
5395 | *
|
---|
5396 | * @returns VBox status code.
|
---|
5397 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
5398 | * @param pDisk Pointer to HDD container.
|
---|
5399 | * @param nImageFrom Name of the image file to merge from.
|
---|
5400 | * @param nImageTo Name of the image file to merge to.
|
---|
5401 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
5402 | */
|
---|
5403 | VBOXDDU_DECL(int) VDMerge(PVBOXHDD pDisk, unsigned nImageFrom,
|
---|
5404 | unsigned nImageTo, PVDINTERFACE pVDIfsOperation)
|
---|
5405 | {
|
---|
5406 | int rc = VINF_SUCCESS;
|
---|
5407 | int rc2;
|
---|
5408 | bool fLockWrite = false, fLockRead = false;
|
---|
5409 | void *pvBuf = NULL;
|
---|
5410 |
|
---|
5411 | LogFlowFunc(("pDisk=%#p nImageFrom=%u nImageTo=%u pVDIfsOperation=%#p\n",
|
---|
5412 | pDisk, nImageFrom, nImageTo, pVDIfsOperation));
|
---|
5413 |
|
---|
5414 | PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
|
---|
5415 | VDINTERFACETYPE_PROGRESS);
|
---|
5416 | PVDINTERFACEPROGRESS pCbProgress = NULL;
|
---|
5417 | if (pIfProgress)
|
---|
5418 | pCbProgress = VDGetInterfaceProgress(pIfProgress);
|
---|
5419 |
|
---|
5420 | do
|
---|
5421 | {
|
---|
5422 | /* sanity check */
|
---|
5423 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
5424 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
5425 |
|
---|
5426 | /* For simplicity reasons lock for writing as the image reopen below
|
---|
5427 | * might need it. After all the reopen is usually needed. */
|
---|
5428 | rc2 = vdThreadStartWrite(pDisk);
|
---|
5429 | AssertRC(rc2);
|
---|
5430 | fLockWrite = true;
|
---|
5431 | PVDIMAGE pImageFrom = vdGetImageByNumber(pDisk, nImageFrom);
|
---|
5432 | PVDIMAGE pImageTo = vdGetImageByNumber(pDisk, nImageTo);
|
---|
5433 | if (!pImageFrom || !pImageTo)
|
---|
5434 | {
|
---|
5435 | rc = VERR_VD_IMAGE_NOT_FOUND;
|
---|
5436 | break;
|
---|
5437 | }
|
---|
5438 | AssertBreakStmt(pImageFrom != pImageTo, rc = VERR_INVALID_PARAMETER);
|
---|
5439 |
|
---|
5440 | /* Make sure destination image is writable. */
|
---|
5441 | unsigned uOpenFlags = pImageTo->Backend->pfnGetOpenFlags(pImageTo->pBackendData);
|
---|
5442 | if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
5443 | {
|
---|
5444 | uOpenFlags &= ~VD_OPEN_FLAGS_READONLY;
|
---|
5445 | rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pBackendData,
|
---|
5446 | uOpenFlags);
|
---|
5447 | if (RT_FAILURE(rc))
|
---|
5448 | break;
|
---|
5449 | }
|
---|
5450 |
|
---|
5451 | /* Get size of destination image. */
|
---|
5452 | uint64_t cbSize = pImageTo->Backend->pfnGetSize(pImageTo->pBackendData);
|
---|
5453 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
5454 | AssertRC(rc2);
|
---|
5455 | fLockWrite = false;
|
---|
5456 |
|
---|
5457 | /* Allocate tmp buffer. */
|
---|
5458 | pvBuf = RTMemTmpAlloc(VD_MERGE_BUFFER_SIZE);
|
---|
5459 | if (!pvBuf)
|
---|
5460 | {
|
---|
5461 | rc = VERR_NO_MEMORY;
|
---|
5462 | break;
|
---|
5463 | }
|
---|
5464 |
|
---|
5465 | /* Merging is done directly on the images itself. This potentially
|
---|
5466 | * causes trouble if the disk is full in the middle of operation. */
|
---|
5467 | if (nImageFrom < nImageTo)
|
---|
5468 | {
|
---|
5469 | /* Merge parent state into child. This means writing all not
|
---|
5470 | * allocated blocks in the destination image which are allocated in
|
---|
5471 | * the images to be merged. */
|
---|
5472 | uint64_t uOffset = 0;
|
---|
5473 | uint64_t cbRemaining = cbSize;
|
---|
5474 | do
|
---|
5475 | {
|
---|
5476 | size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
|
---|
5477 |
|
---|
5478 | /* Need to hold the write lock during a read-write operation. */
|
---|
5479 | rc2 = vdThreadStartWrite(pDisk);
|
---|
5480 | AssertRC(rc2);
|
---|
5481 | fLockWrite = true;
|
---|
5482 |
|
---|
5483 | rc = pImageTo->Backend->pfnRead(pImageTo->pBackendData,
|
---|
5484 | uOffset, pvBuf, cbThisRead,
|
---|
5485 | &cbThisRead);
|
---|
5486 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
5487 | {
|
---|
5488 | /* Search for image with allocated block. Do not attempt to
|
---|
5489 | * read more than the previous reads marked as valid.
|
---|
5490 | * Otherwise this would return stale data when different
|
---|
5491 | * block sizes are used for the images. */
|
---|
5492 | for (PVDIMAGE pCurrImage = pImageTo->pPrev;
|
---|
5493 | pCurrImage != NULL && pCurrImage != pImageFrom->pPrev && rc == VERR_VD_BLOCK_FREE;
|
---|
5494 | pCurrImage = pCurrImage->pPrev)
|
---|
5495 | {
|
---|
5496 | rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
|
---|
5497 | uOffset, pvBuf,
|
---|
5498 | cbThisRead,
|
---|
5499 | &cbThisRead);
|
---|
5500 | }
|
---|
5501 |
|
---|
5502 | if (rc != VERR_VD_BLOCK_FREE)
|
---|
5503 | {
|
---|
5504 | if (RT_FAILURE(rc))
|
---|
5505 | break;
|
---|
5506 | /* Updating the cache is required because this might be a live merge. */
|
---|
5507 | rc = vdWriteHelper(pDisk, pImageTo, pImageFrom->pPrev,
|
---|
5508 | uOffset, pvBuf, cbThisRead,
|
---|
5509 | true /* fUpdateCache */);
|
---|
5510 | if (RT_FAILURE(rc))
|
---|
5511 | break;
|
---|
5512 | }
|
---|
5513 | else
|
---|
5514 | rc = VINF_SUCCESS;
|
---|
5515 | }
|
---|
5516 | else if (RT_FAILURE(rc))
|
---|
5517 | break;
|
---|
5518 |
|
---|
5519 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
5520 | AssertRC(rc2);
|
---|
5521 | fLockWrite = false;
|
---|
5522 |
|
---|
5523 | uOffset += cbThisRead;
|
---|
5524 | cbRemaining -= cbThisRead;
|
---|
5525 |
|
---|
5526 | if (pCbProgress && pCbProgress->pfnProgress)
|
---|
5527 | {
|
---|
5528 | /** @todo r=klaus: this can update the progress to the same
|
---|
5529 | * percentage over and over again if the image format makes
|
---|
5530 | * relatively small increments. */
|
---|
5531 | rc = pCbProgress->pfnProgress(pIfProgress->pvUser,
|
---|
5532 | uOffset * 99 / cbSize);
|
---|
5533 | if (RT_FAILURE(rc))
|
---|
5534 | break;
|
---|
5535 | }
|
---|
5536 | } while (uOffset < cbSize);
|
---|
5537 | }
|
---|
5538 | else
|
---|
5539 | {
|
---|
5540 | /*
|
---|
5541 | * We may need to update the parent uuid of the child coming after
|
---|
5542 | * the last image to be merged. We have to reopen it read/write.
|
---|
5543 | *
|
---|
5544 | * This is done before we do the actual merge to prevent an
|
---|
5545 | * inconsistent chain if the mode change fails for some reason.
|
---|
5546 | */
|
---|
5547 | if (pImageFrom->pNext)
|
---|
5548 | {
|
---|
5549 | PVDIMAGE pImageChild = pImageFrom->pNext;
|
---|
5550 |
|
---|
5551 | /* Take the write lock. */
|
---|
5552 | rc2 = vdThreadStartWrite(pDisk);
|
---|
5553 | AssertRC(rc2);
|
---|
5554 | fLockWrite = true;
|
---|
5555 |
|
---|
5556 | /* We need to open the image in read/write mode. */
|
---|
5557 | uOpenFlags = pImageChild->Backend->pfnGetOpenFlags(pImageChild->pBackendData);
|
---|
5558 |
|
---|
5559 | if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
5560 | {
|
---|
5561 | uOpenFlags &= ~VD_OPEN_FLAGS_READONLY;
|
---|
5562 | rc = pImageChild->Backend->pfnSetOpenFlags(pImageChild->pBackendData,
|
---|
5563 | uOpenFlags);
|
---|
5564 | if (RT_FAILURE(rc))
|
---|
5565 | break;
|
---|
5566 | }
|
---|
5567 |
|
---|
5568 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
5569 | AssertRC(rc2);
|
---|
5570 | fLockWrite = false;
|
---|
5571 | }
|
---|
5572 |
|
---|
5573 | /* If the merge is from the last image we have to relay all writes
|
---|
5574 | * to the merge destination as well, so that concurrent writes
|
---|
5575 | * (in case of a live merge) are handled correctly. */
|
---|
5576 | if (!pImageFrom->pNext)
|
---|
5577 | {
|
---|
5578 | /* Take the write lock. */
|
---|
5579 | rc2 = vdThreadStartWrite(pDisk);
|
---|
5580 | AssertRC(rc2);
|
---|
5581 | fLockWrite = true;
|
---|
5582 |
|
---|
5583 | pDisk->pImageRelay = pImageTo;
|
---|
5584 |
|
---|
5585 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
5586 | AssertRC(rc2);
|
---|
5587 | fLockWrite = false;
|
---|
5588 | }
|
---|
5589 |
|
---|
5590 | /* Merge child state into parent. This means writing all blocks
|
---|
5591 | * which are allocated in the image up to the source image to the
|
---|
5592 | * destination image. */
|
---|
5593 | uint64_t uOffset = 0;
|
---|
5594 | uint64_t cbRemaining = cbSize;
|
---|
5595 | do
|
---|
5596 | {
|
---|
5597 | size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
|
---|
5598 | rc = VERR_VD_BLOCK_FREE;
|
---|
5599 |
|
---|
5600 | /* Need to hold the write lock during a read-write operation. */
|
---|
5601 | rc2 = vdThreadStartWrite(pDisk);
|
---|
5602 | AssertRC(rc2);
|
---|
5603 | fLockWrite = true;
|
---|
5604 |
|
---|
5605 | /* Search for image with allocated block. Do not attempt to
|
---|
5606 | * read more than the previous reads marked as valid. Otherwise
|
---|
5607 | * this would return stale data when different block sizes are
|
---|
5608 | * used for the images. */
|
---|
5609 | for (PVDIMAGE pCurrImage = pImageFrom;
|
---|
5610 | pCurrImage != NULL && pCurrImage != pImageTo && rc == VERR_VD_BLOCK_FREE;
|
---|
5611 | pCurrImage = pCurrImage->pPrev)
|
---|
5612 | {
|
---|
5613 | rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
|
---|
5614 | uOffset, pvBuf,
|
---|
5615 | cbThisRead, &cbThisRead);
|
---|
5616 | }
|
---|
5617 |
|
---|
5618 | if (rc != VERR_VD_BLOCK_FREE)
|
---|
5619 | {
|
---|
5620 | if (RT_FAILURE(rc))
|
---|
5621 | break;
|
---|
5622 | rc = vdWriteHelper(pDisk, pImageTo, NULL, uOffset, pvBuf,
|
---|
5623 | cbThisRead, true /* fUpdateCache */);
|
---|
5624 | if (RT_FAILURE(rc))
|
---|
5625 | break;
|
---|
5626 | }
|
---|
5627 | else
|
---|
5628 | rc = VINF_SUCCESS;
|
---|
5629 |
|
---|
5630 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
5631 | AssertRC(rc2);
|
---|
5632 | fLockWrite = false;
|
---|
5633 |
|
---|
5634 | uOffset += cbThisRead;
|
---|
5635 | cbRemaining -= cbThisRead;
|
---|
5636 |
|
---|
5637 | if (pCbProgress && pCbProgress->pfnProgress)
|
---|
5638 | {
|
---|
5639 | /** @todo r=klaus: this can update the progress to the same
|
---|
5640 | * percentage over and over again if the image format makes
|
---|
5641 | * relatively small increments. */
|
---|
5642 | rc = pCbProgress->pfnProgress(pIfProgress->pvUser,
|
---|
5643 | uOffset * 99 / cbSize);
|
---|
5644 | if (RT_FAILURE(rc))
|
---|
5645 | break;
|
---|
5646 | }
|
---|
5647 | } while (uOffset < cbSize);
|
---|
5648 |
|
---|
5649 | /* In case we set up a "write proxy" image above we must clear
|
---|
5650 | * this again now to prevent stray writes. Failure or not. */
|
---|
5651 | if (!pImageFrom->pNext)
|
---|
5652 | {
|
---|
5653 | /* Take the write lock. */
|
---|
5654 | rc2 = vdThreadStartWrite(pDisk);
|
---|
5655 | AssertRC(rc2);
|
---|
5656 | fLockWrite = true;
|
---|
5657 |
|
---|
5658 | pDisk->pImageRelay = NULL;
|
---|
5659 |
|
---|
5660 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
5661 | AssertRC(rc2);
|
---|
5662 | fLockWrite = false;
|
---|
5663 | }
|
---|
5664 | }
|
---|
5665 |
|
---|
5666 | /*
|
---|
5667 | * Leave in case of an error to avoid corrupted data in the image chain
|
---|
5668 | * (includes cancelling the operation by the user).
|
---|
5669 | */
|
---|
5670 | if (RT_FAILURE(rc))
|
---|
5671 | break;
|
---|
5672 |
|
---|
5673 | /* Need to hold the write lock while finishing the merge. */
|
---|
5674 | rc2 = vdThreadStartWrite(pDisk);
|
---|
5675 | AssertRC(rc2);
|
---|
5676 | fLockWrite = true;
|
---|
5677 |
|
---|
5678 | /* Update parent UUID so that image chain is consistent. */
|
---|
5679 | RTUUID Uuid;
|
---|
5680 | PVDIMAGE pImageChild = NULL;
|
---|
5681 | if (nImageFrom < nImageTo)
|
---|
5682 | {
|
---|
5683 | if (pImageFrom->pPrev)
|
---|
5684 | {
|
---|
5685 | rc = pImageFrom->pPrev->Backend->pfnGetUuid(pImageFrom->pPrev->pBackendData,
|
---|
5686 | &Uuid);
|
---|
5687 | AssertRC(rc);
|
---|
5688 | }
|
---|
5689 | else
|
---|
5690 | RTUuidClear(&Uuid);
|
---|
5691 | rc = pImageTo->Backend->pfnSetParentUuid(pImageTo->pBackendData,
|
---|
5692 | &Uuid);
|
---|
5693 | AssertRC(rc);
|
---|
5694 | }
|
---|
5695 | else
|
---|
5696 | {
|
---|
5697 | /* Update the parent uuid of the child of the last merged image. */
|
---|
5698 | if (pImageFrom->pNext)
|
---|
5699 | {
|
---|
5700 | rc = pImageTo->Backend->pfnGetUuid(pImageTo->pBackendData,
|
---|
5701 | &Uuid);
|
---|
5702 | AssertRC(rc);
|
---|
5703 |
|
---|
5704 | rc = pImageFrom->Backend->pfnSetParentUuid(pImageFrom->pNext->pBackendData,
|
---|
5705 | &Uuid);
|
---|
5706 | AssertRC(rc);
|
---|
5707 |
|
---|
5708 | pImageChild = pImageFrom->pNext;
|
---|
5709 | }
|
---|
5710 | }
|
---|
5711 |
|
---|
5712 | /* Delete the no longer needed images. */
|
---|
5713 | PVDIMAGE pImg = pImageFrom, pTmp;
|
---|
5714 | while (pImg != pImageTo)
|
---|
5715 | {
|
---|
5716 | if (nImageFrom < nImageTo)
|
---|
5717 | pTmp = pImg->pNext;
|
---|
5718 | else
|
---|
5719 | pTmp = pImg->pPrev;
|
---|
5720 | vdRemoveImageFromList(pDisk, pImg);
|
---|
5721 | pImg->Backend->pfnClose(pImg->pBackendData, true);
|
---|
5722 | RTMemFree(pImg->pszFilename);
|
---|
5723 | RTMemFree(pImg);
|
---|
5724 | pImg = pTmp;
|
---|
5725 | }
|
---|
5726 |
|
---|
5727 | /* Make sure destination image is back to read only if necessary. */
|
---|
5728 | if (pImageTo != pDisk->pLast)
|
---|
5729 | {
|
---|
5730 | uOpenFlags = pImageTo->Backend->pfnGetOpenFlags(pImageTo->pBackendData);
|
---|
5731 | uOpenFlags |= VD_OPEN_FLAGS_READONLY;
|
---|
5732 | rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pBackendData,
|
---|
5733 | uOpenFlags);
|
---|
5734 | if (RT_FAILURE(rc))
|
---|
5735 | break;
|
---|
5736 | }
|
---|
5737 |
|
---|
5738 | /*
|
---|
5739 | * Make sure the child is readonly
|
---|
5740 | * for the child -> parent merge direction
|
---|
5741 | * if necessary.
|
---|
5742 | */
|
---|
5743 | if ( nImageFrom > nImageTo
|
---|
5744 | && pImageChild
|
---|
5745 | && pImageChild != pDisk->pLast)
|
---|
5746 | {
|
---|
5747 | uOpenFlags = pImageChild->Backend->pfnGetOpenFlags(pImageChild->pBackendData);
|
---|
5748 | uOpenFlags |= VD_OPEN_FLAGS_READONLY;
|
---|
5749 | rc = pImageChild->Backend->pfnSetOpenFlags(pImageChild->pBackendData,
|
---|
5750 | uOpenFlags);
|
---|
5751 | if (RT_FAILURE(rc))
|
---|
5752 | break;
|
---|
5753 | }
|
---|
5754 | } while (0);
|
---|
5755 |
|
---|
5756 | if (RT_UNLIKELY(fLockWrite))
|
---|
5757 | {
|
---|
5758 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
5759 | AssertRC(rc2);
|
---|
5760 | }
|
---|
5761 | else if (RT_UNLIKELY(fLockRead))
|
---|
5762 | {
|
---|
5763 | rc2 = vdThreadFinishRead(pDisk);
|
---|
5764 | AssertRC(rc2);
|
---|
5765 | }
|
---|
5766 |
|
---|
5767 | if (pvBuf)
|
---|
5768 | RTMemTmpFree(pvBuf);
|
---|
5769 |
|
---|
5770 | if (RT_SUCCESS(rc) && pCbProgress && pCbProgress->pfnProgress)
|
---|
5771 | pCbProgress->pfnProgress(pIfProgress->pvUser, 100);
|
---|
5772 |
|
---|
5773 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
5774 | return rc;
|
---|
5775 | }
|
---|
5776 |
|
---|
5777 | /**
|
---|
5778 | * Copies an image from one HDD container to another.
|
---|
5779 | * The copy is opened in the target HDD container.
|
---|
5780 | * It is possible to convert between different image formats, because the
|
---|
5781 | * backend for the destination may be different from the source.
|
---|
5782 | * If both the source and destination reference the same HDD container,
|
---|
5783 | * then the image is moved (by copying/deleting or renaming) to the new location.
|
---|
5784 | * The source container is unchanged if the move operation fails, otherwise
|
---|
5785 | * the image at the new location is opened in the same way as the old one was.
|
---|
5786 | *
|
---|
5787 | * @returns VBox status code.
|
---|
5788 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
5789 | * @param pDiskFrom Pointer to source HDD container.
|
---|
5790 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
5791 | * @param pDiskTo Pointer to destination HDD container.
|
---|
5792 | * @param pszBackend Name of the image file backend to use.
|
---|
5793 | * @param pszFilename New name of the image (may be NULL if pDiskFrom == pDiskTo).
|
---|
5794 | * @param fMoveByRename If true, attempt to perform a move by renaming (if successful the new size is ignored).
|
---|
5795 | * @param cbSize New image size (0 means leave unchanged).
|
---|
5796 | * @param uImageFlags Flags specifying special destination image features.
|
---|
5797 | * @param pDstUuid New UUID of the destination image. If NULL, a new UUID is created.
|
---|
5798 | * This parameter is used if and only if a true copy is created.
|
---|
5799 | * In all rename/move cases the UUIDs are copied over.
|
---|
5800 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
5801 | * Only used if the destination image is created.
|
---|
5802 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
5803 | * @param pDstVDIfsImage Pointer to the per-image VD interface list, for the
|
---|
5804 | * destination image.
|
---|
5805 | * @param pDstVDIfsOperation Pointer to the per-image VD interface list,
|
---|
5806 | * for the destination image.
|
---|
5807 | */
|
---|
5808 | VBOXDDU_DECL(int) VDCopy(PVBOXHDD pDiskFrom, unsigned nImage, PVBOXHDD pDiskTo,
|
---|
5809 | const char *pszBackend, const char *pszFilename,
|
---|
5810 | bool fMoveByRename, uint64_t cbSize,
|
---|
5811 | unsigned uImageFlags, PCRTUUID pDstUuid,
|
---|
5812 | unsigned uOpenFlags, PVDINTERFACE pVDIfsOperation,
|
---|
5813 | PVDINTERFACE pDstVDIfsImage,
|
---|
5814 | PVDINTERFACE pDstVDIfsOperation)
|
---|
5815 | {
|
---|
5816 | int rc = VINF_SUCCESS;
|
---|
5817 | int rc2;
|
---|
5818 | bool fLockReadFrom = false, fLockWriteFrom = false, fLockWriteTo = false;
|
---|
5819 | void *pvBuf = NULL;
|
---|
5820 | PVDIMAGE pImageTo = NULL;
|
---|
5821 |
|
---|
5822 | LogFlowFunc(("pDiskFrom=%#p nImage=%u pDiskTo=%#p pszBackend=\"%s\" pszFilename=\"%s\" fMoveByRename=%d cbSize=%llu uImageFlags=%#x pDstUuid=%#p uOpenFlags=%#x pVDIfsOperation=%#p pDstVDIfsImage=%#p pDstVDIfsOperation=%#p\n",
|
---|
5823 | pDiskFrom, nImage, pDiskTo, pszBackend, pszFilename, fMoveByRename, cbSize, uImageFlags, pDstUuid, uOpenFlags, pVDIfsOperation, pDstVDIfsImage, pDstVDIfsOperation));
|
---|
5824 |
|
---|
5825 | PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
|
---|
5826 | VDINTERFACETYPE_PROGRESS);
|
---|
5827 | PVDINTERFACEPROGRESS pCbProgress = NULL;
|
---|
5828 | if (pIfProgress)
|
---|
5829 | pCbProgress = VDGetInterfaceProgress(pIfProgress);
|
---|
5830 |
|
---|
5831 | PVDINTERFACE pDstIfProgress = VDInterfaceGet(pDstVDIfsOperation,
|
---|
5832 | VDINTERFACETYPE_PROGRESS);
|
---|
5833 | PVDINTERFACEPROGRESS pDstCbProgress = NULL;
|
---|
5834 | if (pDstIfProgress)
|
---|
5835 | pDstCbProgress = VDGetInterfaceProgress(pDstIfProgress);
|
---|
5836 |
|
---|
5837 | do {
|
---|
5838 | /* Check arguments. */
|
---|
5839 | AssertMsgBreakStmt(VALID_PTR(pDiskFrom), ("pDiskFrom=%#p\n", pDiskFrom),
|
---|
5840 | rc = VERR_INVALID_PARAMETER);
|
---|
5841 | AssertMsg(pDiskFrom->u32Signature == VBOXHDDDISK_SIGNATURE,
|
---|
5842 | ("u32Signature=%08x\n", pDiskFrom->u32Signature));
|
---|
5843 |
|
---|
5844 | rc2 = vdThreadStartRead(pDiskFrom);
|
---|
5845 | AssertRC(rc2);
|
---|
5846 | fLockReadFrom = true;
|
---|
5847 | PVDIMAGE pImageFrom = vdGetImageByNumber(pDiskFrom, nImage);
|
---|
5848 | AssertPtrBreakStmt(pImageFrom, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
5849 | AssertMsgBreakStmt(VALID_PTR(pDiskTo), ("pDiskTo=%#p\n", pDiskTo),
|
---|
5850 | rc = VERR_INVALID_PARAMETER);
|
---|
5851 | AssertMsg(pDiskTo->u32Signature == VBOXHDDDISK_SIGNATURE,
|
---|
5852 | ("u32Signature=%08x\n", pDiskTo->u32Signature));
|
---|
5853 |
|
---|
5854 | /* Move the image. */
|
---|
5855 | if (pDiskFrom == pDiskTo)
|
---|
5856 | {
|
---|
5857 | /* Rename only works when backends are the same, are file based
|
---|
5858 | * and the rename method is implemented. */
|
---|
5859 | if ( fMoveByRename
|
---|
5860 | && !RTStrICmp(pszBackend, pImageFrom->Backend->pszBackendName)
|
---|
5861 | && pImageFrom->Backend->uBackendCaps & VD_CAP_FILE
|
---|
5862 | && pImageFrom->Backend->pfnRename)
|
---|
5863 | {
|
---|
5864 | rc2 = vdThreadFinishRead(pDiskFrom);
|
---|
5865 | AssertRC(rc2);
|
---|
5866 | fLockReadFrom = false;
|
---|
5867 |
|
---|
5868 | rc2 = vdThreadStartWrite(pDiskFrom);
|
---|
5869 | AssertRC(rc2);
|
---|
5870 | fLockWriteFrom = true;
|
---|
5871 | rc = pImageFrom->Backend->pfnRename(pImageFrom->pBackendData, pszFilename ? pszFilename : pImageFrom->pszFilename);
|
---|
5872 | break;
|
---|
5873 | }
|
---|
5874 |
|
---|
5875 | /** @todo Moving (including shrinking/growing) of the image is
|
---|
5876 | * requested, but the rename attempt failed or it wasn't possible.
|
---|
5877 | * Must now copy image to temp location. */
|
---|
5878 | AssertReleaseMsgFailed(("VDCopy: moving by copy/delete not implemented\n"));
|
---|
5879 | }
|
---|
5880 |
|
---|
5881 | /* pszFilename is allowed to be NULL, as this indicates copy to the existing image. */
|
---|
5882 | AssertMsgBreakStmt(pszFilename == NULL || (VALID_PTR(pszFilename) && *pszFilename),
|
---|
5883 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
5884 | rc = VERR_INVALID_PARAMETER);
|
---|
5885 |
|
---|
5886 | uint64_t cbSizeFrom;
|
---|
5887 | cbSizeFrom = pImageFrom->Backend->pfnGetSize(pImageFrom->pBackendData);
|
---|
5888 | if (cbSizeFrom == 0)
|
---|
5889 | {
|
---|
5890 | rc = VERR_VD_VALUE_NOT_FOUND;
|
---|
5891 | break;
|
---|
5892 | }
|
---|
5893 |
|
---|
5894 | VDGEOMETRY PCHSGeometryFrom = {0, 0, 0};
|
---|
5895 | VDGEOMETRY LCHSGeometryFrom = {0, 0, 0};
|
---|
5896 | pImageFrom->Backend->pfnGetPCHSGeometry(pImageFrom->pBackendData, &PCHSGeometryFrom);
|
---|
5897 | pImageFrom->Backend->pfnGetLCHSGeometry(pImageFrom->pBackendData, &LCHSGeometryFrom);
|
---|
5898 |
|
---|
5899 | RTUUID ImageUuid, ImageModificationUuid;
|
---|
5900 | if (pDiskFrom != pDiskTo)
|
---|
5901 | {
|
---|
5902 | if (pDstUuid)
|
---|
5903 | ImageUuid = *pDstUuid;
|
---|
5904 | else
|
---|
5905 | RTUuidCreate(&ImageUuid);
|
---|
5906 | }
|
---|
5907 | else
|
---|
5908 | {
|
---|
5909 | rc = pImageFrom->Backend->pfnGetUuid(pImageFrom->pBackendData, &ImageUuid);
|
---|
5910 | if (RT_FAILURE(rc))
|
---|
5911 | RTUuidCreate(&ImageUuid);
|
---|
5912 | }
|
---|
5913 | rc = pImageFrom->Backend->pfnGetModificationUuid(pImageFrom->pBackendData, &ImageModificationUuid);
|
---|
5914 | if (RT_FAILURE(rc))
|
---|
5915 | RTUuidClear(&ImageModificationUuid);
|
---|
5916 |
|
---|
5917 | char szComment[1024];
|
---|
5918 | rc = pImageFrom->Backend->pfnGetComment(pImageFrom->pBackendData, szComment, sizeof(szComment));
|
---|
5919 | if (RT_FAILURE(rc))
|
---|
5920 | szComment[0] = '\0';
|
---|
5921 | else
|
---|
5922 | szComment[sizeof(szComment) - 1] = '\0';
|
---|
5923 |
|
---|
5924 | rc2 = vdThreadFinishRead(pDiskFrom);
|
---|
5925 | AssertRC(rc2);
|
---|
5926 | fLockReadFrom = false;
|
---|
5927 |
|
---|
5928 | rc2 = vdThreadStartRead(pDiskTo);
|
---|
5929 | AssertRC(rc2);
|
---|
5930 | unsigned cImagesTo = pDiskTo->cImages;
|
---|
5931 | rc2 = vdThreadFinishRead(pDiskTo);
|
---|
5932 | AssertRC(rc2);
|
---|
5933 |
|
---|
5934 | if (pszFilename)
|
---|
5935 | {
|
---|
5936 | if (cbSize == 0)
|
---|
5937 | cbSize = cbSizeFrom;
|
---|
5938 |
|
---|
5939 | /* Create destination image with the properties of source image. */
|
---|
5940 | /** @todo replace the VDCreateDiff/VDCreateBase calls by direct
|
---|
5941 | * calls to the backend. Unifies the code and reduces the API
|
---|
5942 | * dependencies. Would also make the synchronization explicit. */
|
---|
5943 | if (cImagesTo > 0)
|
---|
5944 | {
|
---|
5945 | rc = VDCreateDiff(pDiskTo, pszBackend, pszFilename,
|
---|
5946 | uImageFlags, szComment, &ImageUuid,
|
---|
5947 | NULL /* pParentUuid */,
|
---|
5948 | uOpenFlags & ~VD_OPEN_FLAGS_READONLY,
|
---|
5949 | pDstVDIfsImage, NULL);
|
---|
5950 |
|
---|
5951 | rc2 = vdThreadStartWrite(pDiskTo);
|
---|
5952 | AssertRC(rc2);
|
---|
5953 | fLockWriteTo = true;
|
---|
5954 | } else {
|
---|
5955 | /** @todo hack to force creation of a fixed image for
|
---|
5956 | * the RAW backend, which can't handle anything else. */
|
---|
5957 | if (!RTStrICmp(pszBackend, "RAW"))
|
---|
5958 | uImageFlags |= VD_IMAGE_FLAGS_FIXED;
|
---|
5959 |
|
---|
5960 | vdFixupPCHSGeometry(&PCHSGeometryFrom, cbSize);
|
---|
5961 | vdFixupLCHSGeometry(&LCHSGeometryFrom, cbSize);
|
---|
5962 |
|
---|
5963 | rc = VDCreateBase(pDiskTo, pszBackend, pszFilename, cbSize,
|
---|
5964 | uImageFlags, szComment,
|
---|
5965 | &PCHSGeometryFrom, &LCHSGeometryFrom,
|
---|
5966 | NULL, uOpenFlags & ~VD_OPEN_FLAGS_READONLY,
|
---|
5967 | pDstVDIfsImage, NULL);
|
---|
5968 |
|
---|
5969 | rc2 = vdThreadStartWrite(pDiskTo);
|
---|
5970 | AssertRC(rc2);
|
---|
5971 | fLockWriteTo = true;
|
---|
5972 |
|
---|
5973 | if (RT_SUCCESS(rc) && !RTUuidIsNull(&ImageUuid))
|
---|
5974 | pDiskTo->pLast->Backend->pfnSetUuid(pDiskTo->pLast->pBackendData, &ImageUuid);
|
---|
5975 | }
|
---|
5976 | if (RT_FAILURE(rc))
|
---|
5977 | break;
|
---|
5978 |
|
---|
5979 | pImageTo = pDiskTo->pLast;
|
---|
5980 | AssertPtrBreakStmt(pImageTo, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
5981 |
|
---|
5982 | cbSize = RT_MIN(cbSize, cbSizeFrom);
|
---|
5983 | }
|
---|
5984 | else
|
---|
5985 | {
|
---|
5986 | pImageTo = pDiskTo->pLast;
|
---|
5987 | AssertPtrBreakStmt(pImageTo, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
5988 |
|
---|
5989 | uint64_t cbSizeTo;
|
---|
5990 | cbSizeTo = pImageTo->Backend->pfnGetSize(pImageTo->pBackendData);
|
---|
5991 | if (cbSizeTo == 0)
|
---|
5992 | {
|
---|
5993 | rc = VERR_VD_VALUE_NOT_FOUND;
|
---|
5994 | break;
|
---|
5995 | }
|
---|
5996 |
|
---|
5997 | if (cbSize == 0)
|
---|
5998 | cbSize = RT_MIN(cbSizeFrom, cbSizeTo);
|
---|
5999 |
|
---|
6000 | vdFixupPCHSGeometry(&PCHSGeometryFrom, cbSize);
|
---|
6001 | vdFixupLCHSGeometry(&LCHSGeometryFrom, cbSize);
|
---|
6002 |
|
---|
6003 | /* Update the geometry in the destination image. */
|
---|
6004 | pImageTo->Backend->pfnSetPCHSGeometry(pImageTo->pBackendData, &PCHSGeometryFrom);
|
---|
6005 | pImageTo->Backend->pfnSetLCHSGeometry(pImageTo->pBackendData, &LCHSGeometryFrom);
|
---|
6006 | }
|
---|
6007 |
|
---|
6008 | rc2 = vdThreadFinishWrite(pDiskTo);
|
---|
6009 | AssertRC(rc2);
|
---|
6010 | fLockWriteTo = false;
|
---|
6011 |
|
---|
6012 | /* Allocate tmp buffer. */
|
---|
6013 | pvBuf = RTMemTmpAlloc(VD_MERGE_BUFFER_SIZE);
|
---|
6014 | if (!pvBuf)
|
---|
6015 | {
|
---|
6016 | rc = VERR_NO_MEMORY;
|
---|
6017 | break;
|
---|
6018 | }
|
---|
6019 |
|
---|
6020 | /* Whether we can take the optimized copy path (false) or not.
|
---|
6021 | * Don't optimize if the image existed or if it is a child image. */
|
---|
6022 | bool fRegularRead = (pszFilename == NULL) || (cImagesTo > 0);
|
---|
6023 |
|
---|
6024 | /* Copy the data. */
|
---|
6025 | uint64_t uOffset = 0;
|
---|
6026 | uint64_t cbRemaining = cbSize;
|
---|
6027 |
|
---|
6028 | do
|
---|
6029 | {
|
---|
6030 | size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
|
---|
6031 |
|
---|
6032 | /* Note that we don't attempt to synchronize cross-disk accesses.
|
---|
6033 | * It wouldn't be very difficult to do, just the lock order would
|
---|
6034 | * need to be defined somehow to prevent deadlocks. Postpone such
|
---|
6035 | * magic as there is no use case for this. */
|
---|
6036 |
|
---|
6037 | rc2 = vdThreadStartRead(pDiskFrom);
|
---|
6038 | AssertRC(rc2);
|
---|
6039 | fLockReadFrom = true;
|
---|
6040 |
|
---|
6041 | /*
|
---|
6042 | * Updating the cache doesn't make any sense
|
---|
6043 | * as we are looping once through the image.
|
---|
6044 | */
|
---|
6045 | rc = vdReadHelper(pDiskFrom, pImageFrom, NULL, uOffset, pvBuf,
|
---|
6046 | cbThisRead, fRegularRead,
|
---|
6047 | false /* fUpdateCache */);
|
---|
6048 | if (RT_FAILURE(rc) && rc != VERR_VD_BLOCK_FREE)
|
---|
6049 | break;
|
---|
6050 |
|
---|
6051 | rc2 = vdThreadFinishRead(pDiskFrom);
|
---|
6052 | AssertRC(rc2);
|
---|
6053 | fLockReadFrom = false;
|
---|
6054 |
|
---|
6055 | if (rc != VERR_VD_BLOCK_FREE)
|
---|
6056 | {
|
---|
6057 | rc2 = vdThreadStartWrite(pDiskTo);
|
---|
6058 | AssertRC(rc2);
|
---|
6059 | fLockWriteTo = true;
|
---|
6060 |
|
---|
6061 | rc = vdWriteHelper(pDiskTo, pImageTo, NULL, uOffset, pvBuf,
|
---|
6062 | cbThisRead, false /* fUpdateCache */);
|
---|
6063 | if (RT_FAILURE(rc))
|
---|
6064 | break;
|
---|
6065 |
|
---|
6066 | rc2 = vdThreadFinishWrite(pDiskTo);
|
---|
6067 | AssertRC(rc2);
|
---|
6068 | fLockWriteTo = false;
|
---|
6069 | }
|
---|
6070 | else /* Don't propagate the error to the outside */
|
---|
6071 | rc = VINF_SUCCESS;
|
---|
6072 |
|
---|
6073 | uOffset += cbThisRead;
|
---|
6074 | cbRemaining -= cbThisRead;
|
---|
6075 |
|
---|
6076 | if (pCbProgress && pCbProgress->pfnProgress)
|
---|
6077 | {
|
---|
6078 | /** @todo r=klaus: this can update the progress to the same
|
---|
6079 | * percentage over and over again if the image format makes
|
---|
6080 | * relatively small increments. */
|
---|
6081 | rc = pCbProgress->pfnProgress(pIfProgress->pvUser,
|
---|
6082 | uOffset * 99 / cbSize);
|
---|
6083 | if (RT_FAILURE(rc))
|
---|
6084 | break;
|
---|
6085 | }
|
---|
6086 | if (pDstCbProgress && pDstCbProgress->pfnProgress)
|
---|
6087 | {
|
---|
6088 | /** @todo r=klaus: this can update the progress to the same
|
---|
6089 | * percentage over and over again if the image format makes
|
---|
6090 | * relatively small increments. */
|
---|
6091 | rc = pDstCbProgress->pfnProgress(pDstIfProgress->pvUser,
|
---|
6092 | uOffset * 99 / cbSize);
|
---|
6093 | if (RT_FAILURE(rc))
|
---|
6094 | break;
|
---|
6095 | }
|
---|
6096 | } while (uOffset < cbSize);
|
---|
6097 |
|
---|
6098 | if (RT_SUCCESS(rc))
|
---|
6099 | {
|
---|
6100 | rc2 = vdThreadStartWrite(pDiskTo);
|
---|
6101 | AssertRC(rc2);
|
---|
6102 | fLockWriteTo = true;
|
---|
6103 |
|
---|
6104 | /* Only set modification UUID if it is non-null, since the source
|
---|
6105 | * backend might not provide a valid modification UUID. */
|
---|
6106 | if (!RTUuidIsNull(&ImageModificationUuid))
|
---|
6107 | pImageTo->Backend->pfnSetModificationUuid(pImageTo->pBackendData, &ImageModificationUuid);
|
---|
6108 |
|
---|
6109 | /* Set the requested open flags if they differ from the value
|
---|
6110 | * required for creating the image and copying the contents. */
|
---|
6111 | if ( pImageTo && pszFilename
|
---|
6112 | && uOpenFlags != (uOpenFlags & ~VD_OPEN_FLAGS_READONLY))
|
---|
6113 | rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pBackendData,
|
---|
6114 | uOpenFlags);
|
---|
6115 | }
|
---|
6116 | } while (0);
|
---|
6117 |
|
---|
6118 | if (RT_FAILURE(rc) && pImageTo && pszFilename)
|
---|
6119 | {
|
---|
6120 | /* Take the write lock only if it is not taken. Not worth making the
|
---|
6121 | * above code even more complicated. */
|
---|
6122 | if (RT_UNLIKELY(!fLockWriteTo))
|
---|
6123 | {
|
---|
6124 | rc2 = vdThreadStartWrite(pDiskTo);
|
---|
6125 | AssertRC(rc2);
|
---|
6126 | fLockWriteTo = true;
|
---|
6127 | }
|
---|
6128 | /* Error detected, but new image created. Remove image from list. */
|
---|
6129 | vdRemoveImageFromList(pDiskTo, pImageTo);
|
---|
6130 |
|
---|
6131 | /* Close and delete image. */
|
---|
6132 | rc2 = pImageTo->Backend->pfnClose(pImageTo->pBackendData, true);
|
---|
6133 | AssertRC(rc2);
|
---|
6134 | pImageTo->pBackendData = NULL;
|
---|
6135 |
|
---|
6136 | /* Free remaining resources. */
|
---|
6137 | if (pImageTo->pszFilename)
|
---|
6138 | RTStrFree(pImageTo->pszFilename);
|
---|
6139 |
|
---|
6140 | RTMemFree(pImageTo);
|
---|
6141 | }
|
---|
6142 |
|
---|
6143 | if (RT_UNLIKELY(fLockWriteTo))
|
---|
6144 | {
|
---|
6145 | rc2 = vdThreadFinishWrite(pDiskTo);
|
---|
6146 | AssertRC(rc2);
|
---|
6147 | }
|
---|
6148 | if (RT_UNLIKELY(fLockWriteFrom))
|
---|
6149 | {
|
---|
6150 | rc2 = vdThreadFinishWrite(pDiskFrom);
|
---|
6151 | AssertRC(rc2);
|
---|
6152 | }
|
---|
6153 | else if (RT_UNLIKELY(fLockReadFrom))
|
---|
6154 | {
|
---|
6155 | rc2 = vdThreadFinishRead(pDiskFrom);
|
---|
6156 | AssertRC(rc2);
|
---|
6157 | }
|
---|
6158 |
|
---|
6159 | if (pvBuf)
|
---|
6160 | RTMemTmpFree(pvBuf);
|
---|
6161 |
|
---|
6162 | if (RT_SUCCESS(rc))
|
---|
6163 | {
|
---|
6164 | if (pCbProgress && pCbProgress->pfnProgress)
|
---|
6165 | pCbProgress->pfnProgress(pIfProgress->pvUser, 100);
|
---|
6166 | if (pDstCbProgress && pDstCbProgress->pfnProgress)
|
---|
6167 | pDstCbProgress->pfnProgress(pDstIfProgress->pvUser, 100);
|
---|
6168 | }
|
---|
6169 |
|
---|
6170 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6171 | return rc;
|
---|
6172 | }
|
---|
6173 |
|
---|
6174 | /**
|
---|
6175 | * Optimizes the storage consumption of an image. Typically the unused blocks
|
---|
6176 | * have to be wiped with zeroes to achieve a substantial reduced storage use.
|
---|
6177 | * Another optimization done is reordering the image blocks, which can provide
|
---|
6178 | * a significant performance boost, as reads and writes tend to use less random
|
---|
6179 | * file offsets.
|
---|
6180 | *
|
---|
6181 | * @return VBox status code.
|
---|
6182 | * @return VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
6183 | * @return VERR_VD_IMAGE_READ_ONLY if image is not writable.
|
---|
6184 | * @return VERR_NOT_SUPPORTED if this kind of image can be compacted, but
|
---|
6185 | * the code for this isn't implemented yet.
|
---|
6186 | * @param pDisk Pointer to HDD container.
|
---|
6187 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
6188 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
6189 | */
|
---|
6190 | VBOXDDU_DECL(int) VDCompact(PVBOXHDD pDisk, unsigned nImage,
|
---|
6191 | PVDINTERFACE pVDIfsOperation)
|
---|
6192 | {
|
---|
6193 | int rc = VINF_SUCCESS;
|
---|
6194 | int rc2;
|
---|
6195 | bool fLockRead = false, fLockWrite = false;
|
---|
6196 | void *pvBuf = NULL;
|
---|
6197 | void *pvTmp = NULL;
|
---|
6198 |
|
---|
6199 | LogFlowFunc(("pDisk=%#p nImage=%u pVDIfsOperation=%#p\n",
|
---|
6200 | pDisk, nImage, pVDIfsOperation));
|
---|
6201 |
|
---|
6202 | PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
|
---|
6203 | VDINTERFACETYPE_PROGRESS);
|
---|
6204 | PVDINTERFACEPROGRESS pCbProgress = NULL;
|
---|
6205 | if (pIfProgress)
|
---|
6206 | pCbProgress = VDGetInterfaceProgress(pIfProgress);
|
---|
6207 |
|
---|
6208 | do {
|
---|
6209 | /* Check arguments. */
|
---|
6210 | AssertMsgBreakStmt(VALID_PTR(pDisk), ("pDisk=%#p\n", pDisk),
|
---|
6211 | rc = VERR_INVALID_PARAMETER);
|
---|
6212 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE,
|
---|
6213 | ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6214 |
|
---|
6215 | rc2 = vdThreadStartRead(pDisk);
|
---|
6216 | AssertRC(rc2);
|
---|
6217 | fLockRead = true;
|
---|
6218 |
|
---|
6219 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
6220 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
6221 |
|
---|
6222 | /* If there is no compact callback for not file based backends then
|
---|
6223 | * the backend doesn't need compaction. No need to make much fuss about
|
---|
6224 | * this. For file based ones signal this as not yet supported. */
|
---|
6225 | if (!pImage->Backend->pfnCompact)
|
---|
6226 | {
|
---|
6227 | if (pImage->Backend->uBackendCaps & VD_CAP_FILE)
|
---|
6228 | rc = VERR_NOT_SUPPORTED;
|
---|
6229 | else
|
---|
6230 | rc = VINF_SUCCESS;
|
---|
6231 | break;
|
---|
6232 | }
|
---|
6233 |
|
---|
6234 | /* Insert interface for reading parent state into per-operation list,
|
---|
6235 | * if there is a parent image. */
|
---|
6236 | VDINTERFACE IfOpParent;
|
---|
6237 | VDINTERFACEPARENTSTATE ParentCb;
|
---|
6238 | VDPARENTSTATEDESC ParentUser;
|
---|
6239 | if (pImage->pPrev)
|
---|
6240 | {
|
---|
6241 | ParentCb.cbSize = sizeof(ParentCb);
|
---|
6242 | ParentCb.enmInterface = VDINTERFACETYPE_PARENTSTATE;
|
---|
6243 | ParentCb.pfnParentRead = vdParentRead;
|
---|
6244 | ParentUser.pDisk = pDisk;
|
---|
6245 | ParentUser.pImage = pImage->pPrev;
|
---|
6246 | rc = VDInterfaceAdd(&IfOpParent, "VDCompact_ParentState", VDINTERFACETYPE_PARENTSTATE,
|
---|
6247 | &ParentCb, &ParentUser, &pVDIfsOperation);
|
---|
6248 | AssertRC(rc);
|
---|
6249 | }
|
---|
6250 |
|
---|
6251 | rc2 = vdThreadFinishRead(pDisk);
|
---|
6252 | AssertRC(rc2);
|
---|
6253 | fLockRead = false;
|
---|
6254 |
|
---|
6255 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6256 | AssertRC(rc2);
|
---|
6257 | fLockWrite = true;
|
---|
6258 |
|
---|
6259 | rc = pImage->Backend->pfnCompact(pImage->pBackendData,
|
---|
6260 | 0, 99,
|
---|
6261 | pDisk->pVDIfsDisk,
|
---|
6262 | pImage->pVDIfsImage,
|
---|
6263 | pVDIfsOperation);
|
---|
6264 | } while (0);
|
---|
6265 |
|
---|
6266 | if (RT_UNLIKELY(fLockWrite))
|
---|
6267 | {
|
---|
6268 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6269 | AssertRC(rc2);
|
---|
6270 | }
|
---|
6271 | else if (RT_UNLIKELY(fLockRead))
|
---|
6272 | {
|
---|
6273 | rc2 = vdThreadFinishRead(pDisk);
|
---|
6274 | AssertRC(rc2);
|
---|
6275 | }
|
---|
6276 |
|
---|
6277 | if (pvBuf)
|
---|
6278 | RTMemTmpFree(pvBuf);
|
---|
6279 | if (pvTmp)
|
---|
6280 | RTMemTmpFree(pvTmp);
|
---|
6281 |
|
---|
6282 | if (RT_SUCCESS(rc))
|
---|
6283 | {
|
---|
6284 | if (pCbProgress && pCbProgress->pfnProgress)
|
---|
6285 | pCbProgress->pfnProgress(pIfProgress->pvUser, 100);
|
---|
6286 | }
|
---|
6287 |
|
---|
6288 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6289 | return rc;
|
---|
6290 | }
|
---|
6291 |
|
---|
6292 | /**
|
---|
6293 | * Resizes the the given disk image to the given size.
|
---|
6294 | *
|
---|
6295 | * @return VBox status
|
---|
6296 | * @return VERR_VD_IMAGE_READ_ONLY if image is not writable.
|
---|
6297 | * @return VERR_NOT_SUPPORTED if this kind of image can be compacted, but
|
---|
6298 | *
|
---|
6299 | * @param pDisk Pointer to the HDD container.
|
---|
6300 | * @param cbSize New size of the image.
|
---|
6301 | * @param pPCHSGeometry Pointer to the new physical disk geometry <= (16383,16,63). Not NULL.
|
---|
6302 | * @param pLCHSGeometry Pointer to the new logical disk geometry <= (x,255,63). Not NULL.
|
---|
6303 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
6304 | */
|
---|
6305 | VBOXDDU_DECL(int) VDResize(PVBOXHDD pDisk, uint64_t cbSize,
|
---|
6306 | PCVDGEOMETRY pPCHSGeometry,
|
---|
6307 | PCVDGEOMETRY pLCHSGeometry,
|
---|
6308 | PVDINTERFACE pVDIfsOperation)
|
---|
6309 | {
|
---|
6310 | /** @todo r=klaus resizing was designed to be part of VDCopy, so having a separate function is not desirable. */
|
---|
6311 | int rc = VINF_SUCCESS;
|
---|
6312 | int rc2;
|
---|
6313 | bool fLockRead = false, fLockWrite = false;
|
---|
6314 |
|
---|
6315 | LogFlowFunc(("pDisk=%#p cbSize=%llu pVDIfsOperation=%#p\n",
|
---|
6316 | pDisk, cbSize, pVDIfsOperation));
|
---|
6317 |
|
---|
6318 | PVDINTERFACE pIfProgress = VDInterfaceGet(pVDIfsOperation,
|
---|
6319 | VDINTERFACETYPE_PROGRESS);
|
---|
6320 | PVDINTERFACEPROGRESS pCbProgress = NULL;
|
---|
6321 | if (pIfProgress)
|
---|
6322 | pCbProgress = VDGetInterfaceProgress(pIfProgress);
|
---|
6323 |
|
---|
6324 | do {
|
---|
6325 | /* Check arguments. */
|
---|
6326 | AssertMsgBreakStmt(VALID_PTR(pDisk), ("pDisk=%#p\n", pDisk),
|
---|
6327 | rc = VERR_INVALID_PARAMETER);
|
---|
6328 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE,
|
---|
6329 | ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6330 |
|
---|
6331 | rc2 = vdThreadStartRead(pDisk);
|
---|
6332 | AssertRC(rc2);
|
---|
6333 | fLockRead = true;
|
---|
6334 |
|
---|
6335 | /* Not supported if the disk has child images attached. */
|
---|
6336 | AssertMsgBreakStmt(pDisk->cImages == 1, ("cImages=%u\n", pDisk->cImages),
|
---|
6337 | rc = VERR_NOT_SUPPORTED);
|
---|
6338 |
|
---|
6339 | PVDIMAGE pImage = pDisk->pBase;
|
---|
6340 |
|
---|
6341 | /* If there is no compact callback for not file based backends then
|
---|
6342 | * the backend doesn't need compaction. No need to make much fuss about
|
---|
6343 | * this. For file based ones signal this as not yet supported. */
|
---|
6344 | if (!pImage->Backend->pfnResize)
|
---|
6345 | {
|
---|
6346 | if (pImage->Backend->uBackendCaps & VD_CAP_FILE)
|
---|
6347 | rc = VERR_NOT_SUPPORTED;
|
---|
6348 | else
|
---|
6349 | rc = VINF_SUCCESS;
|
---|
6350 | break;
|
---|
6351 | }
|
---|
6352 |
|
---|
6353 | rc2 = vdThreadFinishRead(pDisk);
|
---|
6354 | AssertRC(rc2);
|
---|
6355 | fLockRead = false;
|
---|
6356 |
|
---|
6357 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6358 | AssertRC(rc2);
|
---|
6359 | fLockWrite = true;
|
---|
6360 |
|
---|
6361 | VDGEOMETRY PCHSGeometryOld;
|
---|
6362 | VDGEOMETRY LCHSGeometryOld;
|
---|
6363 | PCVDGEOMETRY pPCHSGeometryNew;
|
---|
6364 | PCVDGEOMETRY pLCHSGeometryNew;
|
---|
6365 |
|
---|
6366 | if (pPCHSGeometry->cCylinders == 0)
|
---|
6367 | {
|
---|
6368 | /* Auto-detect marker, calculate new value ourself. */
|
---|
6369 | rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData, &PCHSGeometryOld);
|
---|
6370 | if (RT_SUCCESS(rc) && (PCHSGeometryOld.cCylinders != 0))
|
---|
6371 | PCHSGeometryOld.cCylinders = RT_MIN(cbSize / 512 / PCHSGeometryOld.cHeads / PCHSGeometryOld.cSectors, 16383);
|
---|
6372 | else if (rc == VERR_VD_GEOMETRY_NOT_SET)
|
---|
6373 | rc = VINF_SUCCESS;
|
---|
6374 |
|
---|
6375 | pPCHSGeometryNew = &PCHSGeometryOld;
|
---|
6376 | }
|
---|
6377 | else
|
---|
6378 | pPCHSGeometryNew = pPCHSGeometry;
|
---|
6379 |
|
---|
6380 | if (pLCHSGeometry->cCylinders == 0)
|
---|
6381 | {
|
---|
6382 | /* Auto-detect marker, calculate new value ourself. */
|
---|
6383 | rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData, &LCHSGeometryOld);
|
---|
6384 | if (RT_SUCCESS(rc) && (LCHSGeometryOld.cCylinders != 0))
|
---|
6385 | LCHSGeometryOld.cCylinders = cbSize / 512 / LCHSGeometryOld.cHeads / LCHSGeometryOld.cSectors;
|
---|
6386 | else if (rc == VERR_VD_GEOMETRY_NOT_SET)
|
---|
6387 | rc = VINF_SUCCESS;
|
---|
6388 |
|
---|
6389 | pLCHSGeometryNew = &LCHSGeometryOld;
|
---|
6390 | }
|
---|
6391 | else
|
---|
6392 | pLCHSGeometryNew = pLCHSGeometry;
|
---|
6393 |
|
---|
6394 | if (RT_SUCCESS(rc))
|
---|
6395 | rc = pImage->Backend->pfnResize(pImage->pBackendData,
|
---|
6396 | cbSize,
|
---|
6397 | pPCHSGeometryNew,
|
---|
6398 | pLCHSGeometryNew,
|
---|
6399 | 0, 99,
|
---|
6400 | pDisk->pVDIfsDisk,
|
---|
6401 | pImage->pVDIfsImage,
|
---|
6402 | pVDIfsOperation);
|
---|
6403 | } while (0);
|
---|
6404 |
|
---|
6405 | if (RT_UNLIKELY(fLockWrite))
|
---|
6406 | {
|
---|
6407 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6408 | AssertRC(rc2);
|
---|
6409 | }
|
---|
6410 | else if (RT_UNLIKELY(fLockRead))
|
---|
6411 | {
|
---|
6412 | rc2 = vdThreadFinishRead(pDisk);
|
---|
6413 | AssertRC(rc2);
|
---|
6414 | }
|
---|
6415 |
|
---|
6416 | if (RT_SUCCESS(rc))
|
---|
6417 | {
|
---|
6418 | if (pCbProgress && pCbProgress->pfnProgress)
|
---|
6419 | pCbProgress->pfnProgress(pIfProgress->pvUser, 100);
|
---|
6420 | }
|
---|
6421 |
|
---|
6422 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6423 | return rc;
|
---|
6424 | }
|
---|
6425 |
|
---|
6426 | /**
|
---|
6427 | * Closes the last opened image file in HDD container.
|
---|
6428 | * If previous image file was opened in read-only mode (the normal case) and
|
---|
6429 | * the last opened image is in read-write mode then the previous image will be
|
---|
6430 | * reopened in read/write mode.
|
---|
6431 | *
|
---|
6432 | * @returns VBox status code.
|
---|
6433 | * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
|
---|
6434 | * @param pDisk Pointer to HDD container.
|
---|
6435 | * @param fDelete If true, delete the image from the host disk.
|
---|
6436 | */
|
---|
6437 | VBOXDDU_DECL(int) VDClose(PVBOXHDD pDisk, bool fDelete)
|
---|
6438 | {
|
---|
6439 | int rc = VINF_SUCCESS;
|
---|
6440 | int rc2;
|
---|
6441 | bool fLockWrite = false;
|
---|
6442 |
|
---|
6443 | LogFlowFunc(("pDisk=%#p fDelete=%d\n", pDisk, fDelete));
|
---|
6444 | do
|
---|
6445 | {
|
---|
6446 | /* sanity check */
|
---|
6447 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
6448 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6449 |
|
---|
6450 | /* Not worth splitting this up into a read lock phase and write
|
---|
6451 | * lock phase, as closing an image is a relatively fast operation
|
---|
6452 | * dominated by the part which needs the write lock. */
|
---|
6453 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6454 | AssertRC(rc2);
|
---|
6455 | fLockWrite = true;
|
---|
6456 |
|
---|
6457 | PVDIMAGE pImage = pDisk->pLast;
|
---|
6458 | if (!pImage)
|
---|
6459 | {
|
---|
6460 | rc = VERR_VD_NOT_OPENED;
|
---|
6461 | break;
|
---|
6462 | }
|
---|
6463 | unsigned uOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
|
---|
6464 | /* Remove image from list of opened images. */
|
---|
6465 | vdRemoveImageFromList(pDisk, pImage);
|
---|
6466 | /* Close (and optionally delete) image. */
|
---|
6467 | rc = pImage->Backend->pfnClose(pImage->pBackendData, fDelete);
|
---|
6468 | /* Free remaining resources related to the image. */
|
---|
6469 | RTStrFree(pImage->pszFilename);
|
---|
6470 | RTMemFree(pImage);
|
---|
6471 |
|
---|
6472 | pImage = pDisk->pLast;
|
---|
6473 | if (!pImage)
|
---|
6474 | break;
|
---|
6475 |
|
---|
6476 | /* If disk was previously in read/write mode, make sure it will stay
|
---|
6477 | * like this (if possible) after closing this image. Set the open flags
|
---|
6478 | * accordingly. */
|
---|
6479 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
6480 | {
|
---|
6481 | uOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
|
---|
6482 | uOpenFlags &= ~ VD_OPEN_FLAGS_READONLY;
|
---|
6483 | rc = pImage->Backend->pfnSetOpenFlags(pImage->pBackendData, uOpenFlags);
|
---|
6484 | }
|
---|
6485 |
|
---|
6486 | /* Cache disk information. */
|
---|
6487 | pDisk->cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
|
---|
6488 |
|
---|
6489 | /* Cache PCHS geometry. */
|
---|
6490 | rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
6491 | &pDisk->PCHSGeometry);
|
---|
6492 | if (RT_FAILURE(rc2))
|
---|
6493 | {
|
---|
6494 | pDisk->PCHSGeometry.cCylinders = 0;
|
---|
6495 | pDisk->PCHSGeometry.cHeads = 0;
|
---|
6496 | pDisk->PCHSGeometry.cSectors = 0;
|
---|
6497 | }
|
---|
6498 | else
|
---|
6499 | {
|
---|
6500 | /* Make sure the PCHS geometry is properly clipped. */
|
---|
6501 | pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
|
---|
6502 | pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
|
---|
6503 | pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
|
---|
6504 | }
|
---|
6505 |
|
---|
6506 | /* Cache LCHS geometry. */
|
---|
6507 | rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
6508 | &pDisk->LCHSGeometry);
|
---|
6509 | if (RT_FAILURE(rc2))
|
---|
6510 | {
|
---|
6511 | pDisk->LCHSGeometry.cCylinders = 0;
|
---|
6512 | pDisk->LCHSGeometry.cHeads = 0;
|
---|
6513 | pDisk->LCHSGeometry.cSectors = 0;
|
---|
6514 | }
|
---|
6515 | else
|
---|
6516 | {
|
---|
6517 | /* Make sure the LCHS geometry is properly clipped. */
|
---|
6518 | pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
|
---|
6519 | pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
|
---|
6520 | }
|
---|
6521 | } while (0);
|
---|
6522 |
|
---|
6523 | if (RT_UNLIKELY(fLockWrite))
|
---|
6524 | {
|
---|
6525 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6526 | AssertRC(rc2);
|
---|
6527 | }
|
---|
6528 |
|
---|
6529 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6530 | return rc;
|
---|
6531 | }
|
---|
6532 |
|
---|
6533 | /**
|
---|
6534 | * Closes the currently opened cache image file in HDD container.
|
---|
6535 | *
|
---|
6536 | * @return VBox status code.
|
---|
6537 | * @return VERR_VD_NOT_OPENED if no cache is opened in HDD container.
|
---|
6538 | * @param pDisk Pointer to HDD container.
|
---|
6539 | * @param fDelete If true, delete the image from the host disk.
|
---|
6540 | */
|
---|
6541 | VBOXDDU_DECL(int) VDCacheClose(PVBOXHDD pDisk, bool fDelete)
|
---|
6542 | {
|
---|
6543 | int rc = VINF_SUCCESS;
|
---|
6544 | int rc2;
|
---|
6545 | bool fLockWrite = false;
|
---|
6546 | PVDCACHE pCache = NULL;
|
---|
6547 |
|
---|
6548 | LogFlowFunc(("pDisk=%#p fDelete=%d\n", pDisk, fDelete));
|
---|
6549 |
|
---|
6550 | do
|
---|
6551 | {
|
---|
6552 | /* sanity check */
|
---|
6553 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
6554 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6555 |
|
---|
6556 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6557 | AssertRC(rc2);
|
---|
6558 | fLockWrite = true;
|
---|
6559 |
|
---|
6560 | AssertPtrBreakStmt(pDisk->pCache, rc = VERR_VD_CACHE_NOT_FOUND);
|
---|
6561 |
|
---|
6562 | pCache = pDisk->pCache;
|
---|
6563 | pDisk->pCache = NULL;
|
---|
6564 |
|
---|
6565 | pCache->Backend->pfnClose(pCache->pBackendData, fDelete);
|
---|
6566 | if (pCache->pszFilename)
|
---|
6567 | RTStrFree(pCache->pszFilename);
|
---|
6568 | RTMemFree(pCache);
|
---|
6569 | } while (0);
|
---|
6570 |
|
---|
6571 | if (RT_LIKELY(fLockWrite))
|
---|
6572 | {
|
---|
6573 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6574 | AssertRC(rc2);
|
---|
6575 | }
|
---|
6576 |
|
---|
6577 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6578 | return rc;
|
---|
6579 | }
|
---|
6580 |
|
---|
6581 | /**
|
---|
6582 | * Closes all opened image files in HDD container.
|
---|
6583 | *
|
---|
6584 | * @returns VBox status code.
|
---|
6585 | * @param pDisk Pointer to HDD container.
|
---|
6586 | */
|
---|
6587 | VBOXDDU_DECL(int) VDCloseAll(PVBOXHDD pDisk)
|
---|
6588 | {
|
---|
6589 | int rc = VINF_SUCCESS;
|
---|
6590 | int rc2;
|
---|
6591 | bool fLockWrite = false;
|
---|
6592 |
|
---|
6593 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
6594 | do
|
---|
6595 | {
|
---|
6596 | /* sanity check */
|
---|
6597 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
6598 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6599 |
|
---|
6600 | /* Lock the entire operation. */
|
---|
6601 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6602 | AssertRC(rc2);
|
---|
6603 | fLockWrite = true;
|
---|
6604 |
|
---|
6605 | PVDCACHE pCache = pDisk->pCache;
|
---|
6606 | if (pCache)
|
---|
6607 | {
|
---|
6608 | rc2 = pCache->Backend->pfnClose(pCache->pBackendData, false);
|
---|
6609 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
6610 | rc = rc2;
|
---|
6611 |
|
---|
6612 | if (pCache->pszFilename)
|
---|
6613 | RTStrFree(pCache->pszFilename);
|
---|
6614 | RTMemFree(pCache);
|
---|
6615 | }
|
---|
6616 |
|
---|
6617 | PVDIMAGE pImage = pDisk->pLast;
|
---|
6618 | while (VALID_PTR(pImage))
|
---|
6619 | {
|
---|
6620 | PVDIMAGE pPrev = pImage->pPrev;
|
---|
6621 | /* Remove image from list of opened images. */
|
---|
6622 | vdRemoveImageFromList(pDisk, pImage);
|
---|
6623 | /* Close image. */
|
---|
6624 | rc2 = pImage->Backend->pfnClose(pImage->pBackendData, false);
|
---|
6625 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
6626 | rc = rc2;
|
---|
6627 | /* Free remaining resources related to the image. */
|
---|
6628 | RTStrFree(pImage->pszFilename);
|
---|
6629 | RTMemFree(pImage);
|
---|
6630 | pImage = pPrev;
|
---|
6631 | }
|
---|
6632 | Assert(!VALID_PTR(pDisk->pLast));
|
---|
6633 | } while (0);
|
---|
6634 |
|
---|
6635 | if (RT_UNLIKELY(fLockWrite))
|
---|
6636 | {
|
---|
6637 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6638 | AssertRC(rc2);
|
---|
6639 | }
|
---|
6640 |
|
---|
6641 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6642 | return rc;
|
---|
6643 | }
|
---|
6644 |
|
---|
6645 | /**
|
---|
6646 | * Read data from virtual HDD.
|
---|
6647 | *
|
---|
6648 | * @returns VBox status code.
|
---|
6649 | * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
|
---|
6650 | * @param pDisk Pointer to HDD container.
|
---|
6651 | * @param uOffset Offset of first reading byte from start of disk.
|
---|
6652 | * @param pvBuf Pointer to buffer for reading data.
|
---|
6653 | * @param cbRead Number of bytes to read.
|
---|
6654 | */
|
---|
6655 | VBOXDDU_DECL(int) VDRead(PVBOXHDD pDisk, uint64_t uOffset, void *pvBuf,
|
---|
6656 | size_t cbRead)
|
---|
6657 | {
|
---|
6658 | int rc = VINF_SUCCESS;
|
---|
6659 | int rc2;
|
---|
6660 | bool fLockRead = false;
|
---|
6661 |
|
---|
6662 | LogFlowFunc(("pDisk=%#p uOffset=%llu pvBuf=%p cbRead=%zu\n",
|
---|
6663 | pDisk, uOffset, pvBuf, cbRead));
|
---|
6664 | do
|
---|
6665 | {
|
---|
6666 | /* sanity check */
|
---|
6667 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
6668 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6669 |
|
---|
6670 | /* Check arguments. */
|
---|
6671 | AssertMsgBreakStmt(VALID_PTR(pvBuf),
|
---|
6672 | ("pvBuf=%#p\n", pvBuf),
|
---|
6673 | rc = VERR_INVALID_PARAMETER);
|
---|
6674 | AssertMsgBreakStmt(cbRead,
|
---|
6675 | ("cbRead=%zu\n", cbRead),
|
---|
6676 | rc = VERR_INVALID_PARAMETER);
|
---|
6677 |
|
---|
6678 | rc2 = vdThreadStartRead(pDisk);
|
---|
6679 | AssertRC(rc2);
|
---|
6680 | fLockRead = true;
|
---|
6681 |
|
---|
6682 | AssertMsgBreakStmt(uOffset + cbRead <= pDisk->cbSize,
|
---|
6683 | ("uOffset=%llu cbRead=%zu pDisk->cbSize=%llu\n",
|
---|
6684 | uOffset, cbRead, pDisk->cbSize),
|
---|
6685 | rc = VERR_INVALID_PARAMETER);
|
---|
6686 |
|
---|
6687 | PVDIMAGE pImage = pDisk->pLast;
|
---|
6688 | AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
|
---|
6689 |
|
---|
6690 | rc = vdReadHelper(pDisk, pImage, NULL, uOffset, pvBuf, cbRead,
|
---|
6691 | true /* fZeroFreeBlocks */,
|
---|
6692 | true /* fUpdateCache */);
|
---|
6693 | } while (0);
|
---|
6694 |
|
---|
6695 | if (RT_UNLIKELY(fLockRead))
|
---|
6696 | {
|
---|
6697 | rc2 = vdThreadFinishRead(pDisk);
|
---|
6698 | AssertRC(rc2);
|
---|
6699 | }
|
---|
6700 |
|
---|
6701 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6702 | return rc;
|
---|
6703 | }
|
---|
6704 |
|
---|
6705 | /**
|
---|
6706 | * Write data to virtual HDD.
|
---|
6707 | *
|
---|
6708 | * @returns VBox status code.
|
---|
6709 | * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
|
---|
6710 | * @param pDisk Pointer to HDD container.
|
---|
6711 | * @param uOffset Offset of the first byte being
|
---|
6712 | * written from start of disk.
|
---|
6713 | * @param pvBuf Pointer to buffer for writing data.
|
---|
6714 | * @param cbWrite Number of bytes to write.
|
---|
6715 | */
|
---|
6716 | VBOXDDU_DECL(int) VDWrite(PVBOXHDD pDisk, uint64_t uOffset, const void *pvBuf,
|
---|
6717 | size_t cbWrite)
|
---|
6718 | {
|
---|
6719 | int rc = VINF_SUCCESS;
|
---|
6720 | int rc2;
|
---|
6721 | bool fLockWrite = false;
|
---|
6722 |
|
---|
6723 | LogFlowFunc(("pDisk=%#p uOffset=%llu pvBuf=%p cbWrite=%zu\n",
|
---|
6724 | pDisk, uOffset, pvBuf, cbWrite));
|
---|
6725 | do
|
---|
6726 | {
|
---|
6727 | /* sanity check */
|
---|
6728 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
6729 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6730 |
|
---|
6731 | /* Check arguments. */
|
---|
6732 | AssertMsgBreakStmt(VALID_PTR(pvBuf),
|
---|
6733 | ("pvBuf=%#p\n", pvBuf),
|
---|
6734 | rc = VERR_INVALID_PARAMETER);
|
---|
6735 | AssertMsgBreakStmt(cbWrite,
|
---|
6736 | ("cbWrite=%zu\n", cbWrite),
|
---|
6737 | rc = VERR_INVALID_PARAMETER);
|
---|
6738 |
|
---|
6739 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6740 | AssertRC(rc2);
|
---|
6741 | fLockWrite = true;
|
---|
6742 |
|
---|
6743 | AssertMsgBreakStmt(uOffset + cbWrite <= pDisk->cbSize,
|
---|
6744 | ("uOffset=%llu cbWrite=%zu pDisk->cbSize=%llu\n",
|
---|
6745 | uOffset, cbWrite, pDisk->cbSize),
|
---|
6746 | rc = VERR_INVALID_PARAMETER);
|
---|
6747 |
|
---|
6748 | PVDIMAGE pImage = pDisk->pLast;
|
---|
6749 | AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
|
---|
6750 |
|
---|
6751 | vdSetModifiedFlag(pDisk);
|
---|
6752 | rc = vdWriteHelper(pDisk, pImage, NULL, uOffset, pvBuf, cbWrite,
|
---|
6753 | true /* fUpdateCache */);
|
---|
6754 | if (RT_FAILURE(rc))
|
---|
6755 | break;
|
---|
6756 |
|
---|
6757 | /* If there is a merge (in the direction towards a parent) running
|
---|
6758 | * concurrently then we have to also "relay" the write to this parent,
|
---|
6759 | * as the merge position might be already past the position where
|
---|
6760 | * this write is going. The "context" of the write can come from the
|
---|
6761 | * natural chain, since merging either already did or will take care
|
---|
6762 | * of the "other" content which is might be needed to fill the block
|
---|
6763 | * to a full allocation size. The cache doesn't need to be touched
|
---|
6764 | * as this write is covered by the previous one. */
|
---|
6765 | if (RT_UNLIKELY(pDisk->pImageRelay))
|
---|
6766 | rc = vdWriteHelper(pDisk, pDisk->pImageRelay, NULL, uOffset,
|
---|
6767 | pvBuf, cbWrite, false /* fUpdateCache */);
|
---|
6768 | } while (0);
|
---|
6769 |
|
---|
6770 | if (RT_UNLIKELY(fLockWrite))
|
---|
6771 | {
|
---|
6772 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6773 | AssertRC(rc2);
|
---|
6774 | }
|
---|
6775 |
|
---|
6776 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6777 | return rc;
|
---|
6778 | }
|
---|
6779 |
|
---|
6780 | /**
|
---|
6781 | * Make sure the on disk representation of a virtual HDD is up to date.
|
---|
6782 | *
|
---|
6783 | * @returns VBox status code.
|
---|
6784 | * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
|
---|
6785 | * @param pDisk Pointer to HDD container.
|
---|
6786 | */
|
---|
6787 | VBOXDDU_DECL(int) VDFlush(PVBOXHDD pDisk)
|
---|
6788 | {
|
---|
6789 | int rc = VINF_SUCCESS;
|
---|
6790 | int rc2;
|
---|
6791 | bool fLockWrite = false;
|
---|
6792 |
|
---|
6793 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
6794 | do
|
---|
6795 | {
|
---|
6796 | /* sanity check */
|
---|
6797 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
6798 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6799 |
|
---|
6800 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6801 | AssertRC(rc2);
|
---|
6802 | fLockWrite = true;
|
---|
6803 |
|
---|
6804 | PVDIMAGE pImage = pDisk->pLast;
|
---|
6805 | AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
|
---|
6806 |
|
---|
6807 | vdResetModifiedFlag(pDisk);
|
---|
6808 | rc = pImage->Backend->pfnFlush(pImage->pBackendData);
|
---|
6809 |
|
---|
6810 | if ( RT_SUCCESS(rc)
|
---|
6811 | && pDisk->pCache)
|
---|
6812 | rc = pDisk->pCache->Backend->pfnFlush(pDisk->pCache->pBackendData);
|
---|
6813 | } while (0);
|
---|
6814 |
|
---|
6815 | if (RT_UNLIKELY(fLockWrite))
|
---|
6816 | {
|
---|
6817 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6818 | AssertRC(rc2);
|
---|
6819 | }
|
---|
6820 |
|
---|
6821 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6822 | return rc;
|
---|
6823 | }
|
---|
6824 |
|
---|
6825 | /**
|
---|
6826 | * Get number of opened images in HDD container.
|
---|
6827 | *
|
---|
6828 | * @returns Number of opened images for HDD container. 0 if no images have been opened.
|
---|
6829 | * @param pDisk Pointer to HDD container.
|
---|
6830 | */
|
---|
6831 | VBOXDDU_DECL(unsigned) VDGetCount(PVBOXHDD pDisk)
|
---|
6832 | {
|
---|
6833 | unsigned cImages;
|
---|
6834 | int rc2;
|
---|
6835 | bool fLockRead = false;
|
---|
6836 |
|
---|
6837 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
6838 | do
|
---|
6839 | {
|
---|
6840 | /* sanity check */
|
---|
6841 | AssertPtrBreakStmt(pDisk, cImages = 0);
|
---|
6842 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6843 |
|
---|
6844 | rc2 = vdThreadStartRead(pDisk);
|
---|
6845 | AssertRC(rc2);
|
---|
6846 | fLockRead = true;
|
---|
6847 |
|
---|
6848 | cImages = pDisk->cImages;
|
---|
6849 | } while (0);
|
---|
6850 |
|
---|
6851 | if (RT_UNLIKELY(fLockRead))
|
---|
6852 | {
|
---|
6853 | rc2 = vdThreadFinishRead(pDisk);
|
---|
6854 | AssertRC(rc2);
|
---|
6855 | }
|
---|
6856 |
|
---|
6857 | LogFlowFunc(("returns %u\n", cImages));
|
---|
6858 | return cImages;
|
---|
6859 | }
|
---|
6860 |
|
---|
6861 | /**
|
---|
6862 | * Get read/write mode of HDD container.
|
---|
6863 | *
|
---|
6864 | * @returns Virtual disk ReadOnly status.
|
---|
6865 | * @returns true if no image is opened in HDD container.
|
---|
6866 | * @param pDisk Pointer to HDD container.
|
---|
6867 | */
|
---|
6868 | VBOXDDU_DECL(bool) VDIsReadOnly(PVBOXHDD pDisk)
|
---|
6869 | {
|
---|
6870 | bool fReadOnly;
|
---|
6871 | int rc2;
|
---|
6872 | bool fLockRead = false;
|
---|
6873 |
|
---|
6874 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
6875 | do
|
---|
6876 | {
|
---|
6877 | /* sanity check */
|
---|
6878 | AssertPtrBreakStmt(pDisk, fReadOnly = false);
|
---|
6879 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6880 |
|
---|
6881 | rc2 = vdThreadStartRead(pDisk);
|
---|
6882 | AssertRC(rc2);
|
---|
6883 | fLockRead = true;
|
---|
6884 |
|
---|
6885 | PVDIMAGE pImage = pDisk->pLast;
|
---|
6886 | AssertPtrBreakStmt(pImage, fReadOnly = true);
|
---|
6887 |
|
---|
6888 | unsigned uOpenFlags;
|
---|
6889 | uOpenFlags = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pBackendData);
|
---|
6890 | fReadOnly = !!(uOpenFlags & VD_OPEN_FLAGS_READONLY);
|
---|
6891 | } while (0);
|
---|
6892 |
|
---|
6893 | if (RT_UNLIKELY(fLockRead))
|
---|
6894 | {
|
---|
6895 | rc2 = vdThreadFinishRead(pDisk);
|
---|
6896 | AssertRC(rc2);
|
---|
6897 | }
|
---|
6898 |
|
---|
6899 | LogFlowFunc(("returns %d\n", fReadOnly));
|
---|
6900 | return fReadOnly;
|
---|
6901 | }
|
---|
6902 |
|
---|
6903 | /**
|
---|
6904 | * Get total capacity of an image in HDD container.
|
---|
6905 | *
|
---|
6906 | * @returns Virtual disk size in bytes.
|
---|
6907 | * @returns 0 if no image with specified number was not opened.
|
---|
6908 | * @param pDisk Pointer to HDD container.
|
---|
6909 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
6910 | */
|
---|
6911 | VBOXDDU_DECL(uint64_t) VDGetSize(PVBOXHDD pDisk, unsigned nImage)
|
---|
6912 | {
|
---|
6913 | uint64_t cbSize;
|
---|
6914 | int rc2;
|
---|
6915 | bool fLockRead = false;
|
---|
6916 |
|
---|
6917 | LogFlowFunc(("pDisk=%#p nImage=%u\n", pDisk, nImage));
|
---|
6918 | do
|
---|
6919 | {
|
---|
6920 | /* sanity check */
|
---|
6921 | AssertPtrBreakStmt(pDisk, cbSize = 0);
|
---|
6922 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6923 |
|
---|
6924 | rc2 = vdThreadStartRead(pDisk);
|
---|
6925 | AssertRC(rc2);
|
---|
6926 | fLockRead = true;
|
---|
6927 |
|
---|
6928 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
6929 | AssertPtrBreakStmt(pImage, cbSize = 0);
|
---|
6930 | cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
|
---|
6931 | } while (0);
|
---|
6932 |
|
---|
6933 | if (RT_UNLIKELY(fLockRead))
|
---|
6934 | {
|
---|
6935 | rc2 = vdThreadFinishRead(pDisk);
|
---|
6936 | AssertRC(rc2);
|
---|
6937 | }
|
---|
6938 |
|
---|
6939 | LogFlowFunc(("returns %llu\n", cbSize));
|
---|
6940 | return cbSize;
|
---|
6941 | }
|
---|
6942 |
|
---|
6943 | /**
|
---|
6944 | * Get total file size of an image in HDD container.
|
---|
6945 | *
|
---|
6946 | * @returns Virtual disk size in bytes.
|
---|
6947 | * @returns 0 if no image is opened in HDD container.
|
---|
6948 | * @param pDisk Pointer to HDD container.
|
---|
6949 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
6950 | */
|
---|
6951 | VBOXDDU_DECL(uint64_t) VDGetFileSize(PVBOXHDD pDisk, unsigned nImage)
|
---|
6952 | {
|
---|
6953 | uint64_t cbSize;
|
---|
6954 | int rc2;
|
---|
6955 | bool fLockRead = false;
|
---|
6956 |
|
---|
6957 | LogFlowFunc(("pDisk=%#p nImage=%u\n", pDisk, nImage));
|
---|
6958 | do
|
---|
6959 | {
|
---|
6960 | /* sanity check */
|
---|
6961 | AssertPtrBreakStmt(pDisk, cbSize = 0);
|
---|
6962 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6963 |
|
---|
6964 | rc2 = vdThreadStartRead(pDisk);
|
---|
6965 | AssertRC(rc2);
|
---|
6966 | fLockRead = true;
|
---|
6967 |
|
---|
6968 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
6969 | AssertPtrBreakStmt(pImage, cbSize = 0);
|
---|
6970 | cbSize = pImage->Backend->pfnGetFileSize(pImage->pBackendData);
|
---|
6971 | } while (0);
|
---|
6972 |
|
---|
6973 | if (RT_UNLIKELY(fLockRead))
|
---|
6974 | {
|
---|
6975 | rc2 = vdThreadFinishRead(pDisk);
|
---|
6976 | AssertRC(rc2);
|
---|
6977 | }
|
---|
6978 |
|
---|
6979 | LogFlowFunc(("returns %llu\n", cbSize));
|
---|
6980 | return cbSize;
|
---|
6981 | }
|
---|
6982 |
|
---|
6983 | /**
|
---|
6984 | * Get virtual disk PCHS geometry stored in HDD container.
|
---|
6985 | *
|
---|
6986 | * @returns VBox status code.
|
---|
6987 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
6988 | * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
|
---|
6989 | * @param pDisk Pointer to HDD container.
|
---|
6990 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
6991 | * @param pPCHSGeometry Where to store PCHS geometry. Not NULL.
|
---|
6992 | */
|
---|
6993 | VBOXDDU_DECL(int) VDGetPCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
|
---|
6994 | PVDGEOMETRY pPCHSGeometry)
|
---|
6995 | {
|
---|
6996 | int rc = VINF_SUCCESS;
|
---|
6997 | int rc2;
|
---|
6998 | bool fLockRead = false;
|
---|
6999 |
|
---|
7000 | LogFlowFunc(("pDisk=%#p nImage=%u pPCHSGeometry=%#p\n",
|
---|
7001 | pDisk, nImage, pPCHSGeometry));
|
---|
7002 | do
|
---|
7003 | {
|
---|
7004 | /* sanity check */
|
---|
7005 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7006 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7007 |
|
---|
7008 | /* Check arguments. */
|
---|
7009 | AssertMsgBreakStmt(VALID_PTR(pPCHSGeometry),
|
---|
7010 | ("pPCHSGeometry=%#p\n", pPCHSGeometry),
|
---|
7011 | rc = VERR_INVALID_PARAMETER);
|
---|
7012 |
|
---|
7013 | rc2 = vdThreadStartRead(pDisk);
|
---|
7014 | AssertRC(rc2);
|
---|
7015 | fLockRead = true;
|
---|
7016 |
|
---|
7017 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
7018 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
7019 |
|
---|
7020 | if (pImage == pDisk->pLast)
|
---|
7021 | {
|
---|
7022 | /* Use cached information if possible. */
|
---|
7023 | if (pDisk->PCHSGeometry.cCylinders != 0)
|
---|
7024 | *pPCHSGeometry = pDisk->PCHSGeometry;
|
---|
7025 | else
|
---|
7026 | rc = VERR_VD_GEOMETRY_NOT_SET;
|
---|
7027 | }
|
---|
7028 | else
|
---|
7029 | rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
7030 | pPCHSGeometry);
|
---|
7031 | } while (0);
|
---|
7032 |
|
---|
7033 | if (RT_UNLIKELY(fLockRead))
|
---|
7034 | {
|
---|
7035 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7036 | AssertRC(rc2);
|
---|
7037 | }
|
---|
7038 |
|
---|
7039 | LogFlowFunc(("%Rrc (PCHS=%u/%u/%u)\n", rc,
|
---|
7040 | pDisk->PCHSGeometry.cCylinders, pDisk->PCHSGeometry.cHeads,
|
---|
7041 | pDisk->PCHSGeometry.cSectors));
|
---|
7042 | return rc;
|
---|
7043 | }
|
---|
7044 |
|
---|
7045 | /**
|
---|
7046 | * Store virtual disk PCHS geometry in HDD container.
|
---|
7047 | *
|
---|
7048 | * Note that in case of unrecoverable error all images in HDD container will be closed.
|
---|
7049 | *
|
---|
7050 | * @returns VBox status code.
|
---|
7051 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
7052 | * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
|
---|
7053 | * @param pDisk Pointer to HDD container.
|
---|
7054 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
7055 | * @param pPCHSGeometry Where to load PCHS geometry from. Not NULL.
|
---|
7056 | */
|
---|
7057 | VBOXDDU_DECL(int) VDSetPCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
|
---|
7058 | PCVDGEOMETRY pPCHSGeometry)
|
---|
7059 | {
|
---|
7060 | int rc = VINF_SUCCESS;
|
---|
7061 | int rc2;
|
---|
7062 | bool fLockWrite = false;
|
---|
7063 |
|
---|
7064 | LogFlowFunc(("pDisk=%#p nImage=%u pPCHSGeometry=%#p PCHS=%u/%u/%u\n",
|
---|
7065 | pDisk, nImage, pPCHSGeometry, pPCHSGeometry->cCylinders,
|
---|
7066 | pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
|
---|
7067 | do
|
---|
7068 | {
|
---|
7069 | /* sanity check */
|
---|
7070 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7071 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7072 |
|
---|
7073 | /* Check arguments. */
|
---|
7074 | AssertMsgBreakStmt( VALID_PTR(pPCHSGeometry)
|
---|
7075 | && pPCHSGeometry->cHeads <= 16
|
---|
7076 | && pPCHSGeometry->cSectors <= 63,
|
---|
7077 | ("pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pPCHSGeometry,
|
---|
7078 | pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
|
---|
7079 | pPCHSGeometry->cSectors),
|
---|
7080 | rc = VERR_INVALID_PARAMETER);
|
---|
7081 |
|
---|
7082 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7083 | AssertRC(rc2);
|
---|
7084 | fLockWrite = true;
|
---|
7085 |
|
---|
7086 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
7087 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
7088 |
|
---|
7089 | if (pImage == pDisk->pLast)
|
---|
7090 | {
|
---|
7091 | if ( pPCHSGeometry->cCylinders != pDisk->PCHSGeometry.cCylinders
|
---|
7092 | || pPCHSGeometry->cHeads != pDisk->PCHSGeometry.cHeads
|
---|
7093 | || pPCHSGeometry->cSectors != pDisk->PCHSGeometry.cSectors)
|
---|
7094 | {
|
---|
7095 | /* Only update geometry if it is changed. Avoids similar checks
|
---|
7096 | * in every backend. Most of the time the new geometry is set
|
---|
7097 | * to the previous values, so no need to go through the hassle
|
---|
7098 | * of updating an image which could be opened in read-only mode
|
---|
7099 | * right now. */
|
---|
7100 | rc = pImage->Backend->pfnSetPCHSGeometry(pImage->pBackendData,
|
---|
7101 | pPCHSGeometry);
|
---|
7102 |
|
---|
7103 | /* Cache new geometry values in any case. */
|
---|
7104 | rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
7105 | &pDisk->PCHSGeometry);
|
---|
7106 | if (RT_FAILURE(rc2))
|
---|
7107 | {
|
---|
7108 | pDisk->PCHSGeometry.cCylinders = 0;
|
---|
7109 | pDisk->PCHSGeometry.cHeads = 0;
|
---|
7110 | pDisk->PCHSGeometry.cSectors = 0;
|
---|
7111 | }
|
---|
7112 | else
|
---|
7113 | {
|
---|
7114 | /* Make sure the CHS geometry is properly clipped. */
|
---|
7115 | pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 255);
|
---|
7116 | pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
|
---|
7117 | }
|
---|
7118 | }
|
---|
7119 | }
|
---|
7120 | else
|
---|
7121 | {
|
---|
7122 | VDGEOMETRY PCHS;
|
---|
7123 | rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
7124 | &PCHS);
|
---|
7125 | if ( RT_FAILURE(rc)
|
---|
7126 | || pPCHSGeometry->cCylinders != PCHS.cCylinders
|
---|
7127 | || pPCHSGeometry->cHeads != PCHS.cHeads
|
---|
7128 | || pPCHSGeometry->cSectors != PCHS.cSectors)
|
---|
7129 | {
|
---|
7130 | /* Only update geometry if it is changed. Avoids similar checks
|
---|
7131 | * in every backend. Most of the time the new geometry is set
|
---|
7132 | * to the previous values, so no need to go through the hassle
|
---|
7133 | * of updating an image which could be opened in read-only mode
|
---|
7134 | * right now. */
|
---|
7135 | rc = pImage->Backend->pfnSetPCHSGeometry(pImage->pBackendData,
|
---|
7136 | pPCHSGeometry);
|
---|
7137 | }
|
---|
7138 | }
|
---|
7139 | } while (0);
|
---|
7140 |
|
---|
7141 | if (RT_UNLIKELY(fLockWrite))
|
---|
7142 | {
|
---|
7143 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7144 | AssertRC(rc2);
|
---|
7145 | }
|
---|
7146 |
|
---|
7147 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
7148 | return rc;
|
---|
7149 | }
|
---|
7150 |
|
---|
7151 | /**
|
---|
7152 | * Get virtual disk LCHS geometry stored in HDD container.
|
---|
7153 | *
|
---|
7154 | * @returns VBox status code.
|
---|
7155 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
7156 | * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
|
---|
7157 | * @param pDisk Pointer to HDD container.
|
---|
7158 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
7159 | * @param pLCHSGeometry Where to store LCHS geometry. Not NULL.
|
---|
7160 | */
|
---|
7161 | VBOXDDU_DECL(int) VDGetLCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
|
---|
7162 | PVDGEOMETRY pLCHSGeometry)
|
---|
7163 | {
|
---|
7164 | int rc = VINF_SUCCESS;
|
---|
7165 | int rc2;
|
---|
7166 | bool fLockRead = false;
|
---|
7167 |
|
---|
7168 | LogFlowFunc(("pDisk=%#p nImage=%u pLCHSGeometry=%#p\n",
|
---|
7169 | pDisk, nImage, pLCHSGeometry));
|
---|
7170 | do
|
---|
7171 | {
|
---|
7172 | /* sanity check */
|
---|
7173 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7174 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7175 |
|
---|
7176 | /* Check arguments. */
|
---|
7177 | AssertMsgBreakStmt(VALID_PTR(pLCHSGeometry),
|
---|
7178 | ("pLCHSGeometry=%#p\n", pLCHSGeometry),
|
---|
7179 | rc = VERR_INVALID_PARAMETER);
|
---|
7180 |
|
---|
7181 | rc2 = vdThreadStartRead(pDisk);
|
---|
7182 | AssertRC(rc2);
|
---|
7183 | fLockRead = true;
|
---|
7184 |
|
---|
7185 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
7186 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
7187 |
|
---|
7188 | if (pImage == pDisk->pLast)
|
---|
7189 | {
|
---|
7190 | /* Use cached information if possible. */
|
---|
7191 | if (pDisk->LCHSGeometry.cCylinders != 0)
|
---|
7192 | *pLCHSGeometry = pDisk->LCHSGeometry;
|
---|
7193 | else
|
---|
7194 | rc = VERR_VD_GEOMETRY_NOT_SET;
|
---|
7195 | }
|
---|
7196 | else
|
---|
7197 | rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
7198 | pLCHSGeometry);
|
---|
7199 | } while (0);
|
---|
7200 |
|
---|
7201 | if (RT_UNLIKELY(fLockRead))
|
---|
7202 | {
|
---|
7203 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7204 | AssertRC(rc2);
|
---|
7205 | }
|
---|
7206 |
|
---|
7207 | LogFlowFunc((": %Rrc (LCHS=%u/%u/%u)\n", rc,
|
---|
7208 | pDisk->LCHSGeometry.cCylinders, pDisk->LCHSGeometry.cHeads,
|
---|
7209 | pDisk->LCHSGeometry.cSectors));
|
---|
7210 | return rc;
|
---|
7211 | }
|
---|
7212 |
|
---|
7213 | /**
|
---|
7214 | * Store virtual disk LCHS geometry in HDD container.
|
---|
7215 | *
|
---|
7216 | * Note that in case of unrecoverable error all images in HDD container will be closed.
|
---|
7217 | *
|
---|
7218 | * @returns VBox status code.
|
---|
7219 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
7220 | * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
|
---|
7221 | * @param pDisk Pointer to HDD container.
|
---|
7222 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
7223 | * @param pLCHSGeometry Where to load LCHS geometry from. Not NULL.
|
---|
7224 | */
|
---|
7225 | VBOXDDU_DECL(int) VDSetLCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
|
---|
7226 | PCVDGEOMETRY pLCHSGeometry)
|
---|
7227 | {
|
---|
7228 | int rc = VINF_SUCCESS;
|
---|
7229 | int rc2;
|
---|
7230 | bool fLockWrite = false;
|
---|
7231 |
|
---|
7232 | LogFlowFunc(("pDisk=%#p nImage=%u pLCHSGeometry=%#p LCHS=%u/%u/%u\n",
|
---|
7233 | pDisk, nImage, pLCHSGeometry, pLCHSGeometry->cCylinders,
|
---|
7234 | pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
|
---|
7235 | do
|
---|
7236 | {
|
---|
7237 | /* sanity check */
|
---|
7238 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7239 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7240 |
|
---|
7241 | /* Check arguments. */
|
---|
7242 | AssertMsgBreakStmt( VALID_PTR(pLCHSGeometry)
|
---|
7243 | && pLCHSGeometry->cHeads <= 255
|
---|
7244 | && pLCHSGeometry->cSectors <= 63,
|
---|
7245 | ("pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pLCHSGeometry,
|
---|
7246 | pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads,
|
---|
7247 | pLCHSGeometry->cSectors),
|
---|
7248 | rc = VERR_INVALID_PARAMETER);
|
---|
7249 |
|
---|
7250 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7251 | AssertRC(rc2);
|
---|
7252 | fLockWrite = true;
|
---|
7253 |
|
---|
7254 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
7255 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
7256 |
|
---|
7257 | if (pImage == pDisk->pLast)
|
---|
7258 | {
|
---|
7259 | if ( pLCHSGeometry->cCylinders != pDisk->LCHSGeometry.cCylinders
|
---|
7260 | || pLCHSGeometry->cHeads != pDisk->LCHSGeometry.cHeads
|
---|
7261 | || pLCHSGeometry->cSectors != pDisk->LCHSGeometry.cSectors)
|
---|
7262 | {
|
---|
7263 | /* Only update geometry if it is changed. Avoids similar checks
|
---|
7264 | * in every backend. Most of the time the new geometry is set
|
---|
7265 | * to the previous values, so no need to go through the hassle
|
---|
7266 | * of updating an image which could be opened in read-only mode
|
---|
7267 | * right now. */
|
---|
7268 | rc = pImage->Backend->pfnSetLCHSGeometry(pImage->pBackendData,
|
---|
7269 | pLCHSGeometry);
|
---|
7270 |
|
---|
7271 | /* Cache new geometry values in any case. */
|
---|
7272 | rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
7273 | &pDisk->LCHSGeometry);
|
---|
7274 | if (RT_FAILURE(rc2))
|
---|
7275 | {
|
---|
7276 | pDisk->LCHSGeometry.cCylinders = 0;
|
---|
7277 | pDisk->LCHSGeometry.cHeads = 0;
|
---|
7278 | pDisk->LCHSGeometry.cSectors = 0;
|
---|
7279 | }
|
---|
7280 | else
|
---|
7281 | {
|
---|
7282 | /* Make sure the CHS geometry is properly clipped. */
|
---|
7283 | pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
|
---|
7284 | pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
|
---|
7285 | }
|
---|
7286 | }
|
---|
7287 | }
|
---|
7288 | else
|
---|
7289 | {
|
---|
7290 | VDGEOMETRY LCHS;
|
---|
7291 | rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
7292 | &LCHS);
|
---|
7293 | if ( RT_FAILURE(rc)
|
---|
7294 | || pLCHSGeometry->cCylinders != LCHS.cCylinders
|
---|
7295 | || pLCHSGeometry->cHeads != LCHS.cHeads
|
---|
7296 | || pLCHSGeometry->cSectors != LCHS.cSectors)
|
---|
7297 | {
|
---|
7298 | /* Only update geometry if it is changed. Avoids similar checks
|
---|
7299 | * in every backend. Most of the time the new geometry is set
|
---|
7300 | * to the previous values, so no need to go through the hassle
|
---|
7301 | * of updating an image which could be opened in read-only mode
|
---|
7302 | * right now. */
|
---|
7303 | rc = pImage->Backend->pfnSetLCHSGeometry(pImage->pBackendData,
|
---|
7304 | pLCHSGeometry);
|
---|
7305 | }
|
---|
7306 | }
|
---|
7307 | } while (0);
|
---|
7308 |
|
---|
7309 | if (RT_UNLIKELY(fLockWrite))
|
---|
7310 | {
|
---|
7311 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7312 | AssertRC(rc2);
|
---|
7313 | }
|
---|
7314 |
|
---|
7315 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
7316 | return rc;
|
---|
7317 | }
|
---|
7318 |
|
---|
7319 | /**
|
---|
7320 | * Get version of image in HDD container.
|
---|
7321 | *
|
---|
7322 | * @returns VBox status code.
|
---|
7323 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
7324 | * @param pDisk Pointer to HDD container.
|
---|
7325 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
7326 | * @param puVersion Where to store the image version.
|
---|
7327 | */
|
---|
7328 | VBOXDDU_DECL(int) VDGetVersion(PVBOXHDD pDisk, unsigned nImage,
|
---|
7329 | unsigned *puVersion)
|
---|
7330 | {
|
---|
7331 | int rc = VINF_SUCCESS;
|
---|
7332 | int rc2;
|
---|
7333 | bool fLockRead = false;
|
---|
7334 |
|
---|
7335 | LogFlowFunc(("pDisk=%#p nImage=%u puVersion=%#p\n",
|
---|
7336 | pDisk, nImage, puVersion));
|
---|
7337 | do
|
---|
7338 | {
|
---|
7339 | /* sanity check */
|
---|
7340 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7341 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7342 |
|
---|
7343 | /* Check arguments. */
|
---|
7344 | AssertMsgBreakStmt(VALID_PTR(puVersion),
|
---|
7345 | ("puVersion=%#p\n", puVersion),
|
---|
7346 | rc = VERR_INVALID_PARAMETER);
|
---|
7347 |
|
---|
7348 | rc2 = vdThreadStartRead(pDisk);
|
---|
7349 | AssertRC(rc2);
|
---|
7350 | fLockRead = true;
|
---|
7351 |
|
---|
7352 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
7353 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
7354 |
|
---|
7355 | *puVersion = pImage->Backend->pfnGetVersion(pImage->pBackendData);
|
---|
7356 | } while (0);
|
---|
7357 |
|
---|
7358 | if (RT_UNLIKELY(fLockRead))
|
---|
7359 | {
|
---|
7360 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7361 | AssertRC(rc2);
|
---|
7362 | }
|
---|
7363 |
|
---|
7364 | LogFlowFunc(("returns %Rrc uVersion=%#x\n", rc, *puVersion));
|
---|
7365 | return rc;
|
---|
7366 | }
|
---|
7367 |
|
---|
7368 | /**
|
---|
7369 | * List the capabilities of image backend in HDD container.
|
---|
7370 | *
|
---|
7371 | * @returns VBox status code.
|
---|
7372 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
7373 | * @param pDisk Pointer to the HDD container.
|
---|
7374 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
7375 | * @param pbackendInfo Where to store the backend information.
|
---|
7376 | */
|
---|
7377 | VBOXDDU_DECL(int) VDBackendInfoSingle(PVBOXHDD pDisk, unsigned nImage,
|
---|
7378 | PVDBACKENDINFO pBackendInfo)
|
---|
7379 | {
|
---|
7380 | int rc = VINF_SUCCESS;
|
---|
7381 | int rc2;
|
---|
7382 | bool fLockRead = false;
|
---|
7383 |
|
---|
7384 | LogFlowFunc(("pDisk=%#p nImage=%u pBackendInfo=%#p\n",
|
---|
7385 | pDisk, nImage, pBackendInfo));
|
---|
7386 | do
|
---|
7387 | {
|
---|
7388 | /* sanity check */
|
---|
7389 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7390 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7391 |
|
---|
7392 | /* Check arguments. */
|
---|
7393 | AssertMsgBreakStmt(VALID_PTR(pBackendInfo),
|
---|
7394 | ("pBackendInfo=%#p\n", pBackendInfo),
|
---|
7395 | rc = VERR_INVALID_PARAMETER);
|
---|
7396 |
|
---|
7397 | rc2 = vdThreadStartRead(pDisk);
|
---|
7398 | AssertRC(rc2);
|
---|
7399 | fLockRead = true;
|
---|
7400 |
|
---|
7401 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
7402 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
7403 |
|
---|
7404 | pBackendInfo->pszBackend = pImage->Backend->pszBackendName;
|
---|
7405 | pBackendInfo->uBackendCaps = pImage->Backend->uBackendCaps;
|
---|
7406 | pBackendInfo->paFileExtensions = pImage->Backend->paFileExtensions;
|
---|
7407 | pBackendInfo->paConfigInfo = pImage->Backend->paConfigInfo;
|
---|
7408 | } while (0);
|
---|
7409 |
|
---|
7410 | if (RT_UNLIKELY(fLockRead))
|
---|
7411 | {
|
---|
7412 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7413 | AssertRC(rc2);
|
---|
7414 | }
|
---|
7415 |
|
---|
7416 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
7417 | return rc;
|
---|
7418 | }
|
---|
7419 |
|
---|
7420 | /**
|
---|
7421 | * Get flags of image in HDD container.
|
---|
7422 | *
|
---|
7423 | * @returns VBox status code.
|
---|
7424 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
7425 | * @param pDisk Pointer to HDD container.
|
---|
7426 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
7427 | * @param puImageFlags Where to store the image flags.
|
---|
7428 | */
|
---|
7429 | VBOXDDU_DECL(int) VDGetImageFlags(PVBOXHDD pDisk, unsigned nImage,
|
---|
7430 | unsigned *puImageFlags)
|
---|
7431 | {
|
---|
7432 | int rc = VINF_SUCCESS;
|
---|
7433 | int rc2;
|
---|
7434 | bool fLockRead = false;
|
---|
7435 |
|
---|
7436 | LogFlowFunc(("pDisk=%#p nImage=%u puImageFlags=%#p\n",
|
---|
7437 | pDisk, nImage, puImageFlags));
|
---|
7438 | do
|
---|
7439 | {
|
---|
7440 | /* sanity check */
|
---|
7441 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7442 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7443 |
|
---|
7444 | /* Check arguments. */
|
---|
7445 | AssertMsgBreakStmt(VALID_PTR(puImageFlags),
|
---|
7446 | ("puImageFlags=%#p\n", puImageFlags),
|
---|
7447 | rc = VERR_INVALID_PARAMETER);
|
---|
7448 |
|
---|
7449 | rc2 = vdThreadStartRead(pDisk);
|
---|
7450 | AssertRC(rc2);
|
---|
7451 | fLockRead = true;
|
---|
7452 |
|
---|
7453 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
7454 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
7455 |
|
---|
7456 | *puImageFlags = pImage->uImageFlags;
|
---|
7457 | } while (0);
|
---|
7458 |
|
---|
7459 | if (RT_UNLIKELY(fLockRead))
|
---|
7460 | {
|
---|
7461 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7462 | AssertRC(rc2);
|
---|
7463 | }
|
---|
7464 |
|
---|
7465 | LogFlowFunc(("returns %Rrc uImageFlags=%#x\n", rc, *puImageFlags));
|
---|
7466 | return rc;
|
---|
7467 | }
|
---|
7468 |
|
---|
7469 | /**
|
---|
7470 | * Get open flags of image in HDD container.
|
---|
7471 | *
|
---|
7472 | * @returns VBox status code.
|
---|
7473 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
7474 | * @param pDisk Pointer to HDD container.
|
---|
7475 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
7476 | * @param puOpenFlags Where to store the image open flags.
|
---|
7477 | */
|
---|
7478 | VBOXDDU_DECL(int) VDGetOpenFlags(PVBOXHDD pDisk, unsigned nImage,
|
---|
7479 | unsigned *puOpenFlags)
|
---|
7480 | {
|
---|
7481 | int rc = VINF_SUCCESS;
|
---|
7482 | int rc2;
|
---|
7483 | bool fLockRead = false;
|
---|
7484 |
|
---|
7485 | LogFlowFunc(("pDisk=%#p nImage=%u puOpenFlags=%#p\n",
|
---|
7486 | pDisk, nImage, puOpenFlags));
|
---|
7487 | do
|
---|
7488 | {
|
---|
7489 | /* sanity check */
|
---|
7490 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7491 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7492 |
|
---|
7493 | /* Check arguments. */
|
---|
7494 | AssertMsgBreakStmt(VALID_PTR(puOpenFlags),
|
---|
7495 | ("puOpenFlags=%#p\n", puOpenFlags),
|
---|
7496 | rc = VERR_INVALID_PARAMETER);
|
---|
7497 |
|
---|
7498 | rc2 = vdThreadStartRead(pDisk);
|
---|
7499 | AssertRC(rc2);
|
---|
7500 | fLockRead = true;
|
---|
7501 |
|
---|
7502 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
7503 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
7504 |
|
---|
7505 | *puOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
|
---|
7506 | } while (0);
|
---|
7507 |
|
---|
7508 | if (RT_UNLIKELY(fLockRead))
|
---|
7509 | {
|
---|
7510 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7511 | AssertRC(rc2);
|
---|
7512 | }
|
---|
7513 |
|
---|
7514 | LogFlowFunc(("returns %Rrc uOpenFlags=%#x\n", rc, *puOpenFlags));
|
---|
7515 | return rc;
|
---|
7516 | }
|
---|
7517 |
|
---|
7518 | /**
|
---|
7519 | * Set open flags of image in HDD container.
|
---|
7520 | * This operation may cause file locking changes and/or files being reopened.
|
---|
7521 | * Note that in case of unrecoverable error all images in HDD container will be closed.
|
---|
7522 | *
|
---|
7523 | * @returns VBox status code.
|
---|
7524 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
7525 | * @param pDisk Pointer to HDD container.
|
---|
7526 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
7527 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
7528 | */
|
---|
7529 | VBOXDDU_DECL(int) VDSetOpenFlags(PVBOXHDD pDisk, unsigned nImage,
|
---|
7530 | unsigned uOpenFlags)
|
---|
7531 | {
|
---|
7532 | int rc;
|
---|
7533 | int rc2;
|
---|
7534 | bool fLockWrite = false;
|
---|
7535 |
|
---|
7536 | LogFlowFunc(("pDisk=%#p uOpenFlags=%#u\n", pDisk, uOpenFlags));
|
---|
7537 | do
|
---|
7538 | {
|
---|
7539 | /* sanity check */
|
---|
7540 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7541 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7542 |
|
---|
7543 | /* Check arguments. */
|
---|
7544 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
7545 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
7546 | rc = VERR_INVALID_PARAMETER);
|
---|
7547 |
|
---|
7548 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7549 | AssertRC(rc2);
|
---|
7550 | fLockWrite = true;
|
---|
7551 |
|
---|
7552 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
7553 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
7554 |
|
---|
7555 | rc = pImage->Backend->pfnSetOpenFlags(pImage->pBackendData,
|
---|
7556 | uOpenFlags);
|
---|
7557 | } while (0);
|
---|
7558 |
|
---|
7559 | if (RT_UNLIKELY(fLockWrite))
|
---|
7560 | {
|
---|
7561 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7562 | AssertRC(rc2);
|
---|
7563 | }
|
---|
7564 |
|
---|
7565 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
7566 | return rc;
|
---|
7567 | }
|
---|
7568 |
|
---|
7569 | /**
|
---|
7570 | * Get base filename of image in HDD container. Some image formats use
|
---|
7571 | * other filenames as well, so don't use this for anything but informational
|
---|
7572 | * purposes.
|
---|
7573 | *
|
---|
7574 | * @returns VBox status code.
|
---|
7575 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
7576 | * @returns VERR_BUFFER_OVERFLOW if pszFilename buffer too small to hold filename.
|
---|
7577 | * @param pDisk Pointer to HDD container.
|
---|
7578 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
7579 | * @param pszFilename Where to store the image file name.
|
---|
7580 | * @param cbFilename Size of buffer pszFilename points to.
|
---|
7581 | */
|
---|
7582 | VBOXDDU_DECL(int) VDGetFilename(PVBOXHDD pDisk, unsigned nImage,
|
---|
7583 | char *pszFilename, unsigned cbFilename)
|
---|
7584 | {
|
---|
7585 | int rc;
|
---|
7586 | int rc2;
|
---|
7587 | bool fLockRead = false;
|
---|
7588 |
|
---|
7589 | LogFlowFunc(("pDisk=%#p nImage=%u pszFilename=%#p cbFilename=%u\n",
|
---|
7590 | pDisk, nImage, pszFilename, cbFilename));
|
---|
7591 | do
|
---|
7592 | {
|
---|
7593 | /* sanity check */
|
---|
7594 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7595 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7596 |
|
---|
7597 | /* Check arguments. */
|
---|
7598 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
7599 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
7600 | rc = VERR_INVALID_PARAMETER);
|
---|
7601 | AssertMsgBreakStmt(cbFilename,
|
---|
7602 | ("cbFilename=%u\n", cbFilename),
|
---|
7603 | rc = VERR_INVALID_PARAMETER);
|
---|
7604 |
|
---|
7605 | rc2 = vdThreadStartRead(pDisk);
|
---|
7606 | AssertRC(rc2);
|
---|
7607 | fLockRead = true;
|
---|
7608 |
|
---|
7609 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
7610 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
7611 |
|
---|
7612 | size_t cb = strlen(pImage->pszFilename);
|
---|
7613 | if (cb <= cbFilename)
|
---|
7614 | {
|
---|
7615 | strcpy(pszFilename, pImage->pszFilename);
|
---|
7616 | rc = VINF_SUCCESS;
|
---|
7617 | }
|
---|
7618 | else
|
---|
7619 | {
|
---|
7620 | strncpy(pszFilename, pImage->pszFilename, cbFilename - 1);
|
---|
7621 | pszFilename[cbFilename - 1] = '\0';
|
---|
7622 | rc = VERR_BUFFER_OVERFLOW;
|
---|
7623 | }
|
---|
7624 | } while (0);
|
---|
7625 |
|
---|
7626 | if (RT_UNLIKELY(fLockRead))
|
---|
7627 | {
|
---|
7628 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7629 | AssertRC(rc2);
|
---|
7630 | }
|
---|
7631 |
|
---|
7632 | LogFlowFunc(("returns %Rrc, pszFilename=\"%s\"\n", rc, pszFilename));
|
---|
7633 | return rc;
|
---|
7634 | }
|
---|
7635 |
|
---|
7636 | /**
|
---|
7637 | * Get the comment line of image in HDD container.
|
---|
7638 | *
|
---|
7639 | * @returns VBox status code.
|
---|
7640 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
7641 | * @returns VERR_BUFFER_OVERFLOW if pszComment buffer too small to hold comment text.
|
---|
7642 | * @param pDisk Pointer to HDD container.
|
---|
7643 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
7644 | * @param pszComment Where to store the comment string of image. NULL is ok.
|
---|
7645 | * @param cbComment The size of pszComment buffer. 0 is ok.
|
---|
7646 | */
|
---|
7647 | VBOXDDU_DECL(int) VDGetComment(PVBOXHDD pDisk, unsigned nImage,
|
---|
7648 | char *pszComment, unsigned cbComment)
|
---|
7649 | {
|
---|
7650 | int rc;
|
---|
7651 | int rc2;
|
---|
7652 | bool fLockRead = false;
|
---|
7653 |
|
---|
7654 | LogFlowFunc(("pDisk=%#p nImage=%u pszComment=%#p cbComment=%u\n",
|
---|
7655 | pDisk, nImage, pszComment, cbComment));
|
---|
7656 | do
|
---|
7657 | {
|
---|
7658 | /* sanity check */
|
---|
7659 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7660 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7661 |
|
---|
7662 | /* Check arguments. */
|
---|
7663 | AssertMsgBreakStmt(VALID_PTR(pszComment),
|
---|
7664 | ("pszComment=%#p \"%s\"\n", pszComment, pszComment),
|
---|
7665 | rc = VERR_INVALID_PARAMETER);
|
---|
7666 | AssertMsgBreakStmt(cbComment,
|
---|
7667 | ("cbComment=%u\n", cbComment),
|
---|
7668 | rc = VERR_INVALID_PARAMETER);
|
---|
7669 |
|
---|
7670 | rc2 = vdThreadStartRead(pDisk);
|
---|
7671 | AssertRC(rc2);
|
---|
7672 | fLockRead = true;
|
---|
7673 |
|
---|
7674 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
7675 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
7676 |
|
---|
7677 | rc = pImage->Backend->pfnGetComment(pImage->pBackendData, pszComment,
|
---|
7678 | cbComment);
|
---|
7679 | } while (0);
|
---|
7680 |
|
---|
7681 | if (RT_UNLIKELY(fLockRead))
|
---|
7682 | {
|
---|
7683 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7684 | AssertRC(rc2);
|
---|
7685 | }
|
---|
7686 |
|
---|
7687 | LogFlowFunc(("returns %Rrc, pszComment=\"%s\"\n", rc, pszComment));
|
---|
7688 | return rc;
|
---|
7689 | }
|
---|
7690 |
|
---|
7691 | /**
|
---|
7692 | * Changes the comment line of image in HDD container.
|
---|
7693 | *
|
---|
7694 | * @returns VBox status code.
|
---|
7695 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
7696 | * @param pDisk Pointer to HDD container.
|
---|
7697 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
7698 | * @param pszComment New comment string (UTF-8). NULL is allowed to reset the comment.
|
---|
7699 | */
|
---|
7700 | VBOXDDU_DECL(int) VDSetComment(PVBOXHDD pDisk, unsigned nImage,
|
---|
7701 | const char *pszComment)
|
---|
7702 | {
|
---|
7703 | int rc;
|
---|
7704 | int rc2;
|
---|
7705 | bool fLockWrite = false;
|
---|
7706 |
|
---|
7707 | LogFlowFunc(("pDisk=%#p nImage=%u pszComment=%#p \"%s\"\n",
|
---|
7708 | pDisk, nImage, pszComment, pszComment));
|
---|
7709 | do
|
---|
7710 | {
|
---|
7711 | /* sanity check */
|
---|
7712 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7713 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7714 |
|
---|
7715 | /* Check arguments. */
|
---|
7716 | AssertMsgBreakStmt(VALID_PTR(pszComment) || pszComment == NULL,
|
---|
7717 | ("pszComment=%#p \"%s\"\n", pszComment, pszComment),
|
---|
7718 | rc = VERR_INVALID_PARAMETER);
|
---|
7719 |
|
---|
7720 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7721 | AssertRC(rc2);
|
---|
7722 | fLockWrite = true;
|
---|
7723 |
|
---|
7724 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
7725 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
7726 |
|
---|
7727 | rc = pImage->Backend->pfnSetComment(pImage->pBackendData, pszComment);
|
---|
7728 | } while (0);
|
---|
7729 |
|
---|
7730 | if (RT_UNLIKELY(fLockWrite))
|
---|
7731 | {
|
---|
7732 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7733 | AssertRC(rc2);
|
---|
7734 | }
|
---|
7735 |
|
---|
7736 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
7737 | return rc;
|
---|
7738 | }
|
---|
7739 |
|
---|
7740 |
|
---|
7741 | /**
|
---|
7742 | * Get UUID of image in HDD container.
|
---|
7743 | *
|
---|
7744 | * @returns VBox status code.
|
---|
7745 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
7746 | * @param pDisk Pointer to HDD container.
|
---|
7747 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
7748 | * @param pUuid Where to store the image creation UUID.
|
---|
7749 | */
|
---|
7750 | VBOXDDU_DECL(int) VDGetUuid(PVBOXHDD pDisk, unsigned nImage, PRTUUID pUuid)
|
---|
7751 | {
|
---|
7752 | int rc;
|
---|
7753 | int rc2;
|
---|
7754 | bool fLockRead = false;
|
---|
7755 |
|
---|
7756 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
|
---|
7757 | do
|
---|
7758 | {
|
---|
7759 | /* sanity check */
|
---|
7760 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7761 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7762 |
|
---|
7763 | /* Check arguments. */
|
---|
7764 | AssertMsgBreakStmt(VALID_PTR(pUuid),
|
---|
7765 | ("pUuid=%#p\n", pUuid),
|
---|
7766 | rc = VERR_INVALID_PARAMETER);
|
---|
7767 |
|
---|
7768 | rc2 = vdThreadStartRead(pDisk);
|
---|
7769 | AssertRC(rc2);
|
---|
7770 | fLockRead = true;
|
---|
7771 |
|
---|
7772 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
7773 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
7774 |
|
---|
7775 | rc = pImage->Backend->pfnGetUuid(pImage->pBackendData, pUuid);
|
---|
7776 | } while (0);
|
---|
7777 |
|
---|
7778 | if (RT_UNLIKELY(fLockRead))
|
---|
7779 | {
|
---|
7780 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7781 | AssertRC(rc2);
|
---|
7782 | }
|
---|
7783 |
|
---|
7784 | LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
|
---|
7785 | return rc;
|
---|
7786 | }
|
---|
7787 |
|
---|
7788 | /**
|
---|
7789 | * Set the image's UUID. Should not be used by normal applications.
|
---|
7790 | *
|
---|
7791 | * @returns VBox status code.
|
---|
7792 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
7793 | * @param pDisk Pointer to HDD container.
|
---|
7794 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
7795 | * @param pUuid New UUID of the image. If NULL, a new UUID is created.
|
---|
7796 | */
|
---|
7797 | VBOXDDU_DECL(int) VDSetUuid(PVBOXHDD pDisk, unsigned nImage, PCRTUUID pUuid)
|
---|
7798 | {
|
---|
7799 | int rc;
|
---|
7800 | int rc2;
|
---|
7801 | bool fLockWrite = false;
|
---|
7802 |
|
---|
7803 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
|
---|
7804 | pDisk, nImage, pUuid, pUuid));
|
---|
7805 | do
|
---|
7806 | {
|
---|
7807 | /* sanity check */
|
---|
7808 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7809 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7810 |
|
---|
7811 | AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
|
---|
7812 | ("pUuid=%#p\n", pUuid),
|
---|
7813 | rc = VERR_INVALID_PARAMETER);
|
---|
7814 |
|
---|
7815 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7816 | AssertRC(rc2);
|
---|
7817 | fLockWrite = true;
|
---|
7818 |
|
---|
7819 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
7820 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
7821 |
|
---|
7822 | RTUUID Uuid;
|
---|
7823 | if (!pUuid)
|
---|
7824 | {
|
---|
7825 | RTUuidCreate(&Uuid);
|
---|
7826 | pUuid = &Uuid;
|
---|
7827 | }
|
---|
7828 | rc = pImage->Backend->pfnSetUuid(pImage->pBackendData, pUuid);
|
---|
7829 | } while (0);
|
---|
7830 |
|
---|
7831 | if (RT_UNLIKELY(fLockWrite))
|
---|
7832 | {
|
---|
7833 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7834 | AssertRC(rc2);
|
---|
7835 | }
|
---|
7836 |
|
---|
7837 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
7838 | return rc;
|
---|
7839 | }
|
---|
7840 |
|
---|
7841 | /**
|
---|
7842 | * Get last modification UUID of image in HDD container.
|
---|
7843 | *
|
---|
7844 | * @returns VBox status code.
|
---|
7845 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
7846 | * @param pDisk Pointer to HDD container.
|
---|
7847 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
7848 | * @param pUuid Where to store the image modification UUID.
|
---|
7849 | */
|
---|
7850 | VBOXDDU_DECL(int) VDGetModificationUuid(PVBOXHDD pDisk, unsigned nImage, PRTUUID pUuid)
|
---|
7851 | {
|
---|
7852 | int rc = VINF_SUCCESS;
|
---|
7853 | int rc2;
|
---|
7854 | bool fLockRead = false;
|
---|
7855 |
|
---|
7856 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
|
---|
7857 | do
|
---|
7858 | {
|
---|
7859 | /* sanity check */
|
---|
7860 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7861 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7862 |
|
---|
7863 | /* Check arguments. */
|
---|
7864 | AssertMsgBreakStmt(VALID_PTR(pUuid),
|
---|
7865 | ("pUuid=%#p\n", pUuid),
|
---|
7866 | rc = VERR_INVALID_PARAMETER);
|
---|
7867 |
|
---|
7868 | rc2 = vdThreadStartRead(pDisk);
|
---|
7869 | AssertRC(rc2);
|
---|
7870 | fLockRead = true;
|
---|
7871 |
|
---|
7872 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
7873 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
7874 |
|
---|
7875 | rc = pImage->Backend->pfnGetModificationUuid(pImage->pBackendData,
|
---|
7876 | pUuid);
|
---|
7877 | } while (0);
|
---|
7878 |
|
---|
7879 | if (RT_UNLIKELY(fLockRead))
|
---|
7880 | {
|
---|
7881 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7882 | AssertRC(rc2);
|
---|
7883 | }
|
---|
7884 |
|
---|
7885 | LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
|
---|
7886 | return rc;
|
---|
7887 | }
|
---|
7888 |
|
---|
7889 | /**
|
---|
7890 | * Set the image's last modification UUID. Should not be used by normal applications.
|
---|
7891 | *
|
---|
7892 | * @returns VBox status code.
|
---|
7893 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
7894 | * @param pDisk Pointer to HDD container.
|
---|
7895 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
7896 | * @param pUuid New modification UUID of the image. If NULL, a new UUID is created.
|
---|
7897 | */
|
---|
7898 | VBOXDDU_DECL(int) VDSetModificationUuid(PVBOXHDD pDisk, unsigned nImage, PCRTUUID pUuid)
|
---|
7899 | {
|
---|
7900 | int rc;
|
---|
7901 | int rc2;
|
---|
7902 | bool fLockWrite = false;
|
---|
7903 |
|
---|
7904 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
|
---|
7905 | pDisk, nImage, pUuid, pUuid));
|
---|
7906 | do
|
---|
7907 | {
|
---|
7908 | /* sanity check */
|
---|
7909 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7910 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7911 |
|
---|
7912 | /* Check arguments. */
|
---|
7913 | AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
|
---|
7914 | ("pUuid=%#p\n", pUuid),
|
---|
7915 | rc = VERR_INVALID_PARAMETER);
|
---|
7916 |
|
---|
7917 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7918 | AssertRC(rc2);
|
---|
7919 | fLockWrite = true;
|
---|
7920 |
|
---|
7921 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
7922 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
7923 |
|
---|
7924 | RTUUID Uuid;
|
---|
7925 | if (!pUuid)
|
---|
7926 | {
|
---|
7927 | RTUuidCreate(&Uuid);
|
---|
7928 | pUuid = &Uuid;
|
---|
7929 | }
|
---|
7930 | rc = pImage->Backend->pfnSetModificationUuid(pImage->pBackendData,
|
---|
7931 | pUuid);
|
---|
7932 | } while (0);
|
---|
7933 |
|
---|
7934 | if (RT_UNLIKELY(fLockWrite))
|
---|
7935 | {
|
---|
7936 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7937 | AssertRC(rc2);
|
---|
7938 | }
|
---|
7939 |
|
---|
7940 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
7941 | return rc;
|
---|
7942 | }
|
---|
7943 |
|
---|
7944 | /**
|
---|
7945 | * Get parent UUID of image in HDD container.
|
---|
7946 | *
|
---|
7947 | * @returns VBox status code.
|
---|
7948 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
7949 | * @param pDisk Pointer to HDD container.
|
---|
7950 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
7951 | * @param pUuid Where to store the parent image UUID.
|
---|
7952 | */
|
---|
7953 | VBOXDDU_DECL(int) VDGetParentUuid(PVBOXHDD pDisk, unsigned nImage,
|
---|
7954 | PRTUUID pUuid)
|
---|
7955 | {
|
---|
7956 | int rc = VINF_SUCCESS;
|
---|
7957 | int rc2;
|
---|
7958 | bool fLockRead = false;
|
---|
7959 |
|
---|
7960 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
|
---|
7961 | do
|
---|
7962 | {
|
---|
7963 | /* sanity check */
|
---|
7964 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7965 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7966 |
|
---|
7967 | /* Check arguments. */
|
---|
7968 | AssertMsgBreakStmt(VALID_PTR(pUuid),
|
---|
7969 | ("pUuid=%#p\n", pUuid),
|
---|
7970 | rc = VERR_INVALID_PARAMETER);
|
---|
7971 |
|
---|
7972 | rc2 = vdThreadStartRead(pDisk);
|
---|
7973 | AssertRC(rc2);
|
---|
7974 | fLockRead = true;
|
---|
7975 |
|
---|
7976 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
7977 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
7978 |
|
---|
7979 | rc = pImage->Backend->pfnGetParentUuid(pImage->pBackendData, pUuid);
|
---|
7980 | } while (0);
|
---|
7981 |
|
---|
7982 | if (RT_UNLIKELY(fLockRead))
|
---|
7983 | {
|
---|
7984 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7985 | AssertRC(rc2);
|
---|
7986 | }
|
---|
7987 |
|
---|
7988 | LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
|
---|
7989 | return rc;
|
---|
7990 | }
|
---|
7991 |
|
---|
7992 | /**
|
---|
7993 | * Set the image's parent UUID. Should not be used by normal applications.
|
---|
7994 | *
|
---|
7995 | * @returns VBox status code.
|
---|
7996 | * @param pDisk Pointer to HDD container.
|
---|
7997 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
7998 | * @param pUuid New parent UUID of the image. If NULL, a new UUID is created.
|
---|
7999 | */
|
---|
8000 | VBOXDDU_DECL(int) VDSetParentUuid(PVBOXHDD pDisk, unsigned nImage,
|
---|
8001 | PCRTUUID pUuid)
|
---|
8002 | {
|
---|
8003 | int rc;
|
---|
8004 | int rc2;
|
---|
8005 | bool fLockWrite = false;
|
---|
8006 |
|
---|
8007 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
|
---|
8008 | pDisk, nImage, pUuid, pUuid));
|
---|
8009 | do
|
---|
8010 | {
|
---|
8011 | /* sanity check */
|
---|
8012 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8013 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8014 |
|
---|
8015 | /* Check arguments. */
|
---|
8016 | AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
|
---|
8017 | ("pUuid=%#p\n", pUuid),
|
---|
8018 | rc = VERR_INVALID_PARAMETER);
|
---|
8019 |
|
---|
8020 | rc2 = vdThreadStartWrite(pDisk);
|
---|
8021 | AssertRC(rc2);
|
---|
8022 | fLockWrite = true;
|
---|
8023 |
|
---|
8024 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
8025 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
8026 |
|
---|
8027 | RTUUID Uuid;
|
---|
8028 | if (!pUuid)
|
---|
8029 | {
|
---|
8030 | RTUuidCreate(&Uuid);
|
---|
8031 | pUuid = &Uuid;
|
---|
8032 | }
|
---|
8033 | rc = pImage->Backend->pfnSetParentUuid(pImage->pBackendData, pUuid);
|
---|
8034 | } while (0);
|
---|
8035 |
|
---|
8036 | if (RT_UNLIKELY(fLockWrite))
|
---|
8037 | {
|
---|
8038 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
8039 | AssertRC(rc2);
|
---|
8040 | }
|
---|
8041 |
|
---|
8042 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8043 | return rc;
|
---|
8044 | }
|
---|
8045 |
|
---|
8046 |
|
---|
8047 | /**
|
---|
8048 | * Debug helper - dumps all opened images in HDD container into the log file.
|
---|
8049 | *
|
---|
8050 | * @param pDisk Pointer to HDD container.
|
---|
8051 | */
|
---|
8052 | VBOXDDU_DECL(void) VDDumpImages(PVBOXHDD pDisk)
|
---|
8053 | {
|
---|
8054 | int rc2;
|
---|
8055 | bool fLockRead = false;
|
---|
8056 |
|
---|
8057 | do
|
---|
8058 | {
|
---|
8059 | /* sanity check */
|
---|
8060 | AssertPtrBreak(pDisk);
|
---|
8061 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8062 |
|
---|
8063 | if (!pDisk->pInterfaceErrorCallbacks || !VALID_PTR(pDisk->pInterfaceErrorCallbacks->pfnMessage))
|
---|
8064 | pDisk->pInterfaceErrorCallbacks->pfnMessage = vdLogMessage;
|
---|
8065 |
|
---|
8066 | rc2 = vdThreadStartRead(pDisk);
|
---|
8067 | AssertRC(rc2);
|
---|
8068 | fLockRead = true;
|
---|
8069 |
|
---|
8070 | vdMessageWrapper(pDisk, "--- Dumping VD Disk, Images=%u\n", pDisk->cImages);
|
---|
8071 | for (PVDIMAGE pImage = pDisk->pBase; pImage; pImage = pImage->pNext)
|
---|
8072 | {
|
---|
8073 | vdMessageWrapper(pDisk, "Dumping VD image \"%s\" (Backend=%s)\n",
|
---|
8074 | pImage->pszFilename, pImage->Backend->pszBackendName);
|
---|
8075 | pImage->Backend->pfnDump(pImage->pBackendData);
|
---|
8076 | }
|
---|
8077 | } while (0);
|
---|
8078 |
|
---|
8079 | if (RT_UNLIKELY(fLockRead))
|
---|
8080 | {
|
---|
8081 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8082 | AssertRC(rc2);
|
---|
8083 | }
|
---|
8084 | }
|
---|
8085 |
|
---|
8086 |
|
---|
8087 | VBOXDDU_DECL(int) VDAsyncRead(PVBOXHDD pDisk, uint64_t uOffset, size_t cbRead,
|
---|
8088 | PCRTSGBUF pcSgBuf,
|
---|
8089 | PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
8090 | void *pvUser1, void *pvUser2)
|
---|
8091 | {
|
---|
8092 | int rc = VERR_VD_BLOCK_FREE;
|
---|
8093 | int rc2;
|
---|
8094 | bool fLockRead = false;
|
---|
8095 | PVDIOCTX pIoCtx = NULL;
|
---|
8096 |
|
---|
8097 | LogFlowFunc(("pDisk=%#p uOffset=%llu pcSgBuf=%#p cbRead=%zu pvUser1=%#p pvUser2=%#p\n",
|
---|
8098 | pDisk, uOffset, pcSgBuf, cbRead, pvUser1, pvUser2));
|
---|
8099 |
|
---|
8100 | do
|
---|
8101 | {
|
---|
8102 | /* sanity check */
|
---|
8103 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8104 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8105 |
|
---|
8106 | /* Check arguments. */
|
---|
8107 | AssertMsgBreakStmt(cbRead,
|
---|
8108 | ("cbRead=%zu\n", cbRead),
|
---|
8109 | rc = VERR_INVALID_PARAMETER);
|
---|
8110 | AssertMsgBreakStmt(VALID_PTR(pcSgBuf),
|
---|
8111 | ("pcSgBuf=%#p\n", pcSgBuf),
|
---|
8112 | rc = VERR_INVALID_PARAMETER);
|
---|
8113 |
|
---|
8114 | rc2 = vdThreadStartRead(pDisk);
|
---|
8115 | AssertRC(rc2);
|
---|
8116 | fLockRead = true;
|
---|
8117 |
|
---|
8118 | AssertMsgBreakStmt(uOffset + cbRead <= pDisk->cbSize,
|
---|
8119 | ("uOffset=%llu cbRead=%zu pDisk->cbSize=%llu\n",
|
---|
8120 | uOffset, cbRead, pDisk->cbSize),
|
---|
8121 | rc = VERR_INVALID_PARAMETER);
|
---|
8122 | AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
|
---|
8123 |
|
---|
8124 | pIoCtx = vdIoCtxRootAlloc(pDisk, VDIOCTXTXDIR_READ, uOffset,
|
---|
8125 | cbRead, pDisk->pLast, pcSgBuf,
|
---|
8126 | pfnComplete, pvUser1, pvUser2,
|
---|
8127 | NULL, vdReadHelperAsync);
|
---|
8128 | if (!pIoCtx)
|
---|
8129 | {
|
---|
8130 | rc = VERR_NO_MEMORY;
|
---|
8131 | break;
|
---|
8132 | }
|
---|
8133 |
|
---|
8134 | rc = vdIoCtxProcess(pIoCtx);
|
---|
8135 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
8136 | {
|
---|
8137 | if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
|
---|
8138 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
8139 | else
|
---|
8140 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
|
---|
8141 | }
|
---|
8142 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
|
---|
8143 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
8144 |
|
---|
8145 | } while (0);
|
---|
8146 |
|
---|
8147 | if (RT_UNLIKELY(fLockRead) && ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
8148 | || rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
8149 | {
|
---|
8150 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8151 | AssertRC(rc2);
|
---|
8152 | }
|
---|
8153 |
|
---|
8154 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8155 | return rc;
|
---|
8156 | }
|
---|
8157 |
|
---|
8158 |
|
---|
8159 | VBOXDDU_DECL(int) VDAsyncWrite(PVBOXHDD pDisk, uint64_t uOffset, size_t cbWrite,
|
---|
8160 | PCRTSGBUF pcSgBuf,
|
---|
8161 | PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
8162 | void *pvUser1, void *pvUser2)
|
---|
8163 | {
|
---|
8164 | int rc;
|
---|
8165 | int rc2;
|
---|
8166 | bool fLockWrite = false;
|
---|
8167 | PVDIOCTX pIoCtx = NULL;
|
---|
8168 |
|
---|
8169 | LogFlowFunc(("pDisk=%#p uOffset=%llu cSgBuf=%#p cbWrite=%zu pvUser1=%#p pvUser2=%#p\n",
|
---|
8170 | pDisk, uOffset, pcSgBuf, cbWrite, pvUser1, pvUser2));
|
---|
8171 | do
|
---|
8172 | {
|
---|
8173 | /* sanity check */
|
---|
8174 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8175 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8176 |
|
---|
8177 | /* Check arguments. */
|
---|
8178 | AssertMsgBreakStmt(cbWrite,
|
---|
8179 | ("cbWrite=%zu\n", cbWrite),
|
---|
8180 | rc = VERR_INVALID_PARAMETER);
|
---|
8181 | AssertMsgBreakStmt(VALID_PTR(pcSgBuf),
|
---|
8182 | ("pcSgBuf=%#p\n", pcSgBuf),
|
---|
8183 | rc = VERR_INVALID_PARAMETER);
|
---|
8184 |
|
---|
8185 | rc2 = vdThreadStartWrite(pDisk);
|
---|
8186 | AssertRC(rc2);
|
---|
8187 | fLockWrite = true;
|
---|
8188 |
|
---|
8189 | AssertMsgBreakStmt(uOffset + cbWrite <= pDisk->cbSize,
|
---|
8190 | ("uOffset=%llu cbWrite=%zu pDisk->cbSize=%llu\n",
|
---|
8191 | uOffset, cbWrite, pDisk->cbSize),
|
---|
8192 | rc = VERR_INVALID_PARAMETER);
|
---|
8193 | AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
|
---|
8194 |
|
---|
8195 | pIoCtx = vdIoCtxRootAlloc(pDisk, VDIOCTXTXDIR_WRITE, uOffset,
|
---|
8196 | cbWrite, pDisk->pLast, pcSgBuf,
|
---|
8197 | pfnComplete, pvUser1, pvUser2,
|
---|
8198 | NULL, vdWriteHelperAsync);
|
---|
8199 | if (!pIoCtx)
|
---|
8200 | {
|
---|
8201 | rc = VERR_NO_MEMORY;
|
---|
8202 | break;
|
---|
8203 | }
|
---|
8204 |
|
---|
8205 | rc = vdIoCtxProcess(pIoCtx);
|
---|
8206 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
8207 | {
|
---|
8208 | if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
|
---|
8209 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
8210 | else
|
---|
8211 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
|
---|
8212 | }
|
---|
8213 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
|
---|
8214 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
8215 | } while (0);
|
---|
8216 |
|
---|
8217 | if (RT_UNLIKELY(fLockWrite) && ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
8218 | || rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
8219 | {
|
---|
8220 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
8221 | AssertRC(rc2);
|
---|
8222 | }
|
---|
8223 |
|
---|
8224 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8225 | return rc;
|
---|
8226 | }
|
---|
8227 |
|
---|
8228 |
|
---|
8229 | VBOXDDU_DECL(int) VDAsyncFlush(PVBOXHDD pDisk, PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
8230 | void *pvUser1, void *pvUser2)
|
---|
8231 | {
|
---|
8232 | int rc;
|
---|
8233 | int rc2;
|
---|
8234 | bool fLockWrite = false;
|
---|
8235 | PVDIOCTX pIoCtx = NULL;
|
---|
8236 |
|
---|
8237 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
8238 |
|
---|
8239 | do
|
---|
8240 | {
|
---|
8241 | /* sanity check */
|
---|
8242 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8243 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8244 |
|
---|
8245 | rc2 = vdThreadStartWrite(pDisk);
|
---|
8246 | AssertRC(rc2);
|
---|
8247 | fLockWrite = true;
|
---|
8248 |
|
---|
8249 | AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
|
---|
8250 |
|
---|
8251 | pIoCtx = vdIoCtxRootAlloc(pDisk, VDIOCTXTXDIR_FLUSH, 0,
|
---|
8252 | 0, pDisk->pLast, NULL,
|
---|
8253 | pfnComplete, pvUser1, pvUser2,
|
---|
8254 | NULL, vdFlushHelperAsync);
|
---|
8255 | if (!pIoCtx)
|
---|
8256 | {
|
---|
8257 | rc = VERR_NO_MEMORY;
|
---|
8258 | break;
|
---|
8259 | }
|
---|
8260 |
|
---|
8261 | rc = vdIoCtxProcess(pIoCtx);
|
---|
8262 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
8263 | {
|
---|
8264 | if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
|
---|
8265 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
8266 | else
|
---|
8267 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
|
---|
8268 | }
|
---|
8269 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
|
---|
8270 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
8271 | } while (0);
|
---|
8272 |
|
---|
8273 | if (RT_UNLIKELY(fLockWrite) && ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
8274 | || rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
8275 | {
|
---|
8276 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
8277 | AssertRC(rc2);
|
---|
8278 | }
|
---|
8279 |
|
---|
8280 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8281 | return rc;
|
---|
8282 | }
|
---|
8283 |
|
---|