1 | /* $Id: VD.cpp 63567 2016-08-16 14:06:54Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxHDD - VBox HDD Container implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2016 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_VD
|
---|
23 | #include <VBox/vd.h>
|
---|
24 | #include <VBox/err.h>
|
---|
25 | #include <VBox/sup.h>
|
---|
26 | #include <VBox/log.h>
|
---|
27 |
|
---|
28 | #include <iprt/alloc.h>
|
---|
29 | #include <iprt/assert.h>
|
---|
30 | #include <iprt/uuid.h>
|
---|
31 | #include <iprt/file.h>
|
---|
32 | #include <iprt/string.h>
|
---|
33 | #include <iprt/asm.h>
|
---|
34 | #include <iprt/ldr.h>
|
---|
35 | #include <iprt/dir.h>
|
---|
36 | #include <iprt/path.h>
|
---|
37 | #include <iprt/param.h>
|
---|
38 | #include <iprt/memcache.h>
|
---|
39 | #include <iprt/sg.h>
|
---|
40 | #include <iprt/list.h>
|
---|
41 | #include <iprt/avl.h>
|
---|
42 | #include <iprt/semaphore.h>
|
---|
43 |
|
---|
44 | #include <VBox/vd-plugin.h>
|
---|
45 |
|
---|
46 | #include "VDBackends.h"
|
---|
47 |
|
---|
48 | /** Disable dynamic backends on non x86 architectures. This feature
|
---|
49 | * requires the SUPR3 library which is not available there.
|
---|
50 | */
|
---|
51 | #if !defined(VBOX_HDD_NO_DYNAMIC_BACKENDS) && !defined(RT_ARCH_X86) && !defined(RT_ARCH_AMD64)
|
---|
52 | # define VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | #define VBOXHDDDISK_SIGNATURE 0x6f0e2a7d
|
---|
56 |
|
---|
57 | /** Buffer size used for merging images. */
|
---|
58 | #define VD_MERGE_BUFFER_SIZE (16 * _1M)
|
---|
59 |
|
---|
60 | /** Maximum number of segments in one I/O task. */
|
---|
61 | #define VD_IO_TASK_SEGMENTS_MAX 64
|
---|
62 |
|
---|
63 | /** Threshold after not recently used blocks are removed from the list. */
|
---|
64 | #define VD_DISCARD_REMOVE_THRESHOLD (10 * _1M) /** @todo experiment */
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * VD async I/O interface storage descriptor.
|
---|
68 | */
|
---|
69 | typedef struct VDIIOFALLBACKSTORAGE
|
---|
70 | {
|
---|
71 | /** File handle. */
|
---|
72 | RTFILE File;
|
---|
73 | /** Completion callback. */
|
---|
74 | PFNVDCOMPLETED pfnCompleted;
|
---|
75 | /** Thread for async access. */
|
---|
76 | RTTHREAD ThreadAsync;
|
---|
77 | } VDIIOFALLBACKSTORAGE, *PVDIIOFALLBACKSTORAGE;
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Structure containing everything I/O related
|
---|
81 | * for the image and cache descriptors.
|
---|
82 | */
|
---|
83 | typedef struct VDIO
|
---|
84 | {
|
---|
85 | /** I/O interface to the upper layer. */
|
---|
86 | PVDINTERFACEIO pInterfaceIo;
|
---|
87 |
|
---|
88 | /** Per image internal I/O interface. */
|
---|
89 | VDINTERFACEIOINT VDIfIoInt;
|
---|
90 |
|
---|
91 | /** Fallback I/O interface, only used if the caller doesn't provide it. */
|
---|
92 | VDINTERFACEIO VDIfIo;
|
---|
93 |
|
---|
94 | /** Opaque backend data. */
|
---|
95 | void *pBackendData;
|
---|
96 | /** Disk this image is part of */
|
---|
97 | PVBOXHDD pDisk;
|
---|
98 | /** Flag whether to ignore flush requests. */
|
---|
99 | bool fIgnoreFlush;
|
---|
100 | } VDIO, *PVDIO;
|
---|
101 |
|
---|
102 | /** Forward declaration of an I/O task */
|
---|
103 | typedef struct VDIOTASK *PVDIOTASK;
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * VBox HDD Container image descriptor.
|
---|
107 | */
|
---|
108 | typedef struct VDIMAGE
|
---|
109 | {
|
---|
110 | /** Link to parent image descriptor, if any. */
|
---|
111 | struct VDIMAGE *pPrev;
|
---|
112 | /** Link to child image descriptor, if any. */
|
---|
113 | struct VDIMAGE *pNext;
|
---|
114 | /** Container base filename. (UTF-8) */
|
---|
115 | char *pszFilename;
|
---|
116 | /** Data managed by the backend which keeps the actual info. */
|
---|
117 | void *pBackendData;
|
---|
118 | /** Cached sanitized image flags. */
|
---|
119 | unsigned uImageFlags;
|
---|
120 | /** Image open flags (only those handled generically in this code and which
|
---|
121 | * the backends will never ever see). */
|
---|
122 | unsigned uOpenFlags;
|
---|
123 |
|
---|
124 | /** Function pointers for the various backend methods. */
|
---|
125 | PCVBOXHDDBACKEND Backend;
|
---|
126 | /** Pointer to list of VD interfaces, per-image. */
|
---|
127 | PVDINTERFACE pVDIfsImage;
|
---|
128 | /** I/O related things. */
|
---|
129 | VDIO VDIo;
|
---|
130 | } VDIMAGE, *PVDIMAGE;
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * uModified bit flags.
|
---|
134 | */
|
---|
135 | #define VD_IMAGE_MODIFIED_FLAG RT_BIT(0)
|
---|
136 | #define VD_IMAGE_MODIFIED_FIRST RT_BIT(1)
|
---|
137 | #define VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE RT_BIT(2)
|
---|
138 |
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * VBox HDD Cache image descriptor.
|
---|
142 | */
|
---|
143 | typedef struct VDCACHE
|
---|
144 | {
|
---|
145 | /** Cache base filename. (UTF-8) */
|
---|
146 | char *pszFilename;
|
---|
147 | /** Data managed by the backend which keeps the actual info. */
|
---|
148 | void *pBackendData;
|
---|
149 | /** Cached sanitized image flags. */
|
---|
150 | unsigned uImageFlags;
|
---|
151 | /** Image open flags (only those handled generically in this code and which
|
---|
152 | * the backends will never ever see). */
|
---|
153 | unsigned uOpenFlags;
|
---|
154 |
|
---|
155 | /** Function pointers for the various backend methods. */
|
---|
156 | PCVDCACHEBACKEND Backend;
|
---|
157 |
|
---|
158 | /** Pointer to list of VD interfaces, per-cache. */
|
---|
159 | PVDINTERFACE pVDIfsCache;
|
---|
160 | /** I/O related things. */
|
---|
161 | VDIO VDIo;
|
---|
162 | } VDCACHE, *PVDCACHE;
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * A block waiting for a discard.
|
---|
166 | */
|
---|
167 | typedef struct VDDISCARDBLOCK
|
---|
168 | {
|
---|
169 | /** AVL core. */
|
---|
170 | AVLRU64NODECORE Core;
|
---|
171 | /** LRU list node. */
|
---|
172 | RTLISTNODE NodeLru;
|
---|
173 | /** Number of bytes to discard. */
|
---|
174 | size_t cbDiscard;
|
---|
175 | /** Bitmap of allocated sectors. */
|
---|
176 | void *pbmAllocated;
|
---|
177 | } VDDISCARDBLOCK, *PVDDISCARDBLOCK;
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * VD discard state.
|
---|
181 | */
|
---|
182 | typedef struct VDDISCARDSTATE
|
---|
183 | {
|
---|
184 | /** Number of bytes waiting for a discard. */
|
---|
185 | size_t cbDiscarding;
|
---|
186 | /** AVL tree with blocks waiting for a discard.
|
---|
187 | * The uOffset + cbDiscard range is the search key. */
|
---|
188 | PAVLRU64TREE pTreeBlocks;
|
---|
189 | /** LRU list of the least frequently discarded blocks.
|
---|
190 | * If there are to many blocks waiting the least frequently used
|
---|
191 | * will be removed and the range will be set to 0.
|
---|
192 | */
|
---|
193 | RTLISTNODE ListLru;
|
---|
194 | } VDDISCARDSTATE, *PVDDISCARDSTATE;
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * VD filter instance.
|
---|
198 | */
|
---|
199 | typedef struct VDFILTER
|
---|
200 | {
|
---|
201 | /** List node for the read filter chain. */
|
---|
202 | RTLISTNODE ListNodeChainRead;
|
---|
203 | /** List node for the write filter chain. */
|
---|
204 | RTLISTNODE ListNodeChainWrite;
|
---|
205 | /** Number of references to this filter. */
|
---|
206 | uint32_t cRefs;
|
---|
207 | /** Opaque VD filter backend instance data. */
|
---|
208 | void *pvBackendData;
|
---|
209 | /** Pointer to the filter backend interface. */
|
---|
210 | PCVDFILTERBACKEND pBackend;
|
---|
211 | /** Pointer to list of VD interfaces, per-filter. */
|
---|
212 | PVDINTERFACE pVDIfsFilter;
|
---|
213 | /** I/O related things. */
|
---|
214 | VDIO VDIo;
|
---|
215 | } VDFILTER;
|
---|
216 | /** Pointer to a VD filter instance. */
|
---|
217 | typedef VDFILTER *PVDFILTER;
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * VBox HDD Container main structure, private part.
|
---|
221 | */
|
---|
222 | struct VBOXHDD
|
---|
223 | {
|
---|
224 | /** Structure signature (VBOXHDDDISK_SIGNATURE). */
|
---|
225 | uint32_t u32Signature;
|
---|
226 |
|
---|
227 | /** Image type. */
|
---|
228 | VDTYPE enmType;
|
---|
229 |
|
---|
230 | /** Number of opened images. */
|
---|
231 | unsigned cImages;
|
---|
232 |
|
---|
233 | /** Base image. */
|
---|
234 | PVDIMAGE pBase;
|
---|
235 |
|
---|
236 | /** Last opened image in the chain.
|
---|
237 | * The same as pBase if only one image is used. */
|
---|
238 | PVDIMAGE pLast;
|
---|
239 |
|
---|
240 | /** If a merge to one of the parents is running this may be non-NULL
|
---|
241 | * to indicate to what image the writes should be additionally relayed. */
|
---|
242 | PVDIMAGE pImageRelay;
|
---|
243 |
|
---|
244 | /** Flags representing the modification state. */
|
---|
245 | unsigned uModified;
|
---|
246 |
|
---|
247 | /** Cached size of this disk. */
|
---|
248 | uint64_t cbSize;
|
---|
249 | /** Cached PCHS geometry for this disk. */
|
---|
250 | VDGEOMETRY PCHSGeometry;
|
---|
251 | /** Cached LCHS geometry for this disk. */
|
---|
252 | VDGEOMETRY LCHSGeometry;
|
---|
253 |
|
---|
254 | /** Pointer to list of VD interfaces, per-disk. */
|
---|
255 | PVDINTERFACE pVDIfsDisk;
|
---|
256 | /** Pointer to the common interface structure for error reporting. */
|
---|
257 | PVDINTERFACEERROR pInterfaceError;
|
---|
258 | /** Pointer to the optional thread synchronization callbacks. */
|
---|
259 | PVDINTERFACETHREADSYNC pInterfaceThreadSync;
|
---|
260 |
|
---|
261 | /** Memory cache for I/O contexts */
|
---|
262 | RTMEMCACHE hMemCacheIoCtx;
|
---|
263 | /** Memory cache for I/O tasks. */
|
---|
264 | RTMEMCACHE hMemCacheIoTask;
|
---|
265 | /** An I/O context is currently using the disk structures
|
---|
266 | * Every I/O context must be placed on one of the lists below. */
|
---|
267 | volatile bool fLocked;
|
---|
268 | /** Head of pending I/O tasks waiting for completion - LIFO order. */
|
---|
269 | volatile PVDIOTASK pIoTasksPendingHead;
|
---|
270 | /** Head of newly queued I/O contexts - LIFO order. */
|
---|
271 | volatile PVDIOCTX pIoCtxHead;
|
---|
272 | /** Head of halted I/O contexts which are given back to generic
|
---|
273 | * disk framework by the backend. - LIFO order. */
|
---|
274 | volatile PVDIOCTX pIoCtxHaltedHead;
|
---|
275 |
|
---|
276 | /** Head of blocked I/O contexts, processed only
|
---|
277 | * after pIoCtxLockOwner was freed - LIFO order. */
|
---|
278 | volatile PVDIOCTX pIoCtxBlockedHead;
|
---|
279 | /** I/O context which locked the disk for a growing write or flush request.
|
---|
280 | * Other flush or growing write requests need to wait until
|
---|
281 | * the current one completes. - NIL_VDIOCTX if unlocked. */
|
---|
282 | volatile PVDIOCTX pIoCtxLockOwner;
|
---|
283 | /** If the disk was locked by a growing write, flush or discard request this
|
---|
284 | * contains the start offset to check for interfering I/O while it is in progress. */
|
---|
285 | uint64_t uOffsetStartLocked;
|
---|
286 | /** If the disk was locked by a growing write, flush or discard request this contains
|
---|
287 | * the first non affected offset to check for interfering I/O while it is in progress. */
|
---|
288 | uint64_t uOffsetEndLocked;
|
---|
289 |
|
---|
290 | /** Pointer to the L2 disk cache if any. */
|
---|
291 | PVDCACHE pCache;
|
---|
292 | /** Pointer to the discard state if any. */
|
---|
293 | PVDDISCARDSTATE pDiscard;
|
---|
294 |
|
---|
295 | /** Read filter chain - PVDFILTER. */
|
---|
296 | RTLISTANCHOR ListFilterChainRead;
|
---|
297 | /** Write filter chain - PVDFILTER. */
|
---|
298 | RTLISTANCHOR ListFilterChainWrite;
|
---|
299 | };
|
---|
300 |
|
---|
301 | # define VD_IS_LOCKED(a_pDisk) \
|
---|
302 | do \
|
---|
303 | { \
|
---|
304 | NOREF(a_pDisk); \
|
---|
305 | AssertMsg((a_pDisk)->fLocked, \
|
---|
306 | ("Lock not held\n"));\
|
---|
307 | } while(0)
|
---|
308 |
|
---|
309 | /**
|
---|
310 | * VBox parent read descriptor, used internally for compaction.
|
---|
311 | */
|
---|
312 | typedef struct VDPARENTSTATEDESC
|
---|
313 | {
|
---|
314 | /** Pointer to disk descriptor. */
|
---|
315 | PVBOXHDD pDisk;
|
---|
316 | /** Pointer to image descriptor. */
|
---|
317 | PVDIMAGE pImage;
|
---|
318 | } VDPARENTSTATEDESC, *PVDPARENTSTATEDESC;
|
---|
319 |
|
---|
320 | /**
|
---|
321 | * Transfer direction.
|
---|
322 | */
|
---|
323 | typedef enum VDIOCTXTXDIR
|
---|
324 | {
|
---|
325 | /** Read */
|
---|
326 | VDIOCTXTXDIR_READ = 0,
|
---|
327 | /** Write */
|
---|
328 | VDIOCTXTXDIR_WRITE,
|
---|
329 | /** Flush */
|
---|
330 | VDIOCTXTXDIR_FLUSH,
|
---|
331 | /** Discard */
|
---|
332 | VDIOCTXTXDIR_DISCARD,
|
---|
333 | /** 32bit hack */
|
---|
334 | VDIOCTXTXDIR_32BIT_HACK = 0x7fffffff
|
---|
335 | } VDIOCTXTXDIR, *PVDIOCTXTXDIR;
|
---|
336 |
|
---|
337 | /** Transfer function */
|
---|
338 | typedef DECLCALLBACK(int) FNVDIOCTXTRANSFER (PVDIOCTX pIoCtx);
|
---|
339 | /** Pointer to a transfer function. */
|
---|
340 | typedef FNVDIOCTXTRANSFER *PFNVDIOCTXTRANSFER;
|
---|
341 |
|
---|
342 | /**
|
---|
343 | * I/O context
|
---|
344 | */
|
---|
345 | typedef struct VDIOCTX
|
---|
346 | {
|
---|
347 | /** Pointer to the next I/O context. */
|
---|
348 | struct VDIOCTX * volatile pIoCtxNext;
|
---|
349 | /** Disk this is request is for. */
|
---|
350 | PVBOXHDD pDisk;
|
---|
351 | /** Return code. */
|
---|
352 | int rcReq;
|
---|
353 | /** Various flags for the I/O context. */
|
---|
354 | uint32_t fFlags;
|
---|
355 | /** Number of data transfers currently pending. */
|
---|
356 | volatile uint32_t cDataTransfersPending;
|
---|
357 | /** How many meta data transfers are pending. */
|
---|
358 | volatile uint32_t cMetaTransfersPending;
|
---|
359 | /** Flag whether the request finished */
|
---|
360 | volatile bool fComplete;
|
---|
361 | /** Temporary allocated memory which is freed
|
---|
362 | * when the context completes. */
|
---|
363 | void *pvAllocation;
|
---|
364 | /** Transfer function. */
|
---|
365 | PFNVDIOCTXTRANSFER pfnIoCtxTransfer;
|
---|
366 | /** Next transfer part after the current one completed. */
|
---|
367 | PFNVDIOCTXTRANSFER pfnIoCtxTransferNext;
|
---|
368 | /** Transfer direction */
|
---|
369 | VDIOCTXTXDIR enmTxDir;
|
---|
370 | /** Request type dependent data. */
|
---|
371 | union
|
---|
372 | {
|
---|
373 | /** I/O request (read/write). */
|
---|
374 | struct
|
---|
375 | {
|
---|
376 | /** Number of bytes left until this context completes. */
|
---|
377 | volatile uint32_t cbTransferLeft;
|
---|
378 | /** Current offset */
|
---|
379 | volatile uint64_t uOffset;
|
---|
380 | /** Number of bytes to transfer */
|
---|
381 | volatile size_t cbTransfer;
|
---|
382 | /** Current image in the chain. */
|
---|
383 | PVDIMAGE pImageCur;
|
---|
384 | /** Start image to read from. pImageCur is reset to this
|
---|
385 | * value after it reached the first image in the chain. */
|
---|
386 | PVDIMAGE pImageStart;
|
---|
387 | /** S/G buffer */
|
---|
388 | RTSGBUF SgBuf;
|
---|
389 | /** Number of bytes to clear in the buffer before the current read. */
|
---|
390 | size_t cbBufClear;
|
---|
391 | /** Number of images to read. */
|
---|
392 | unsigned cImagesRead;
|
---|
393 | /** Override for the parent image to start reading from. */
|
---|
394 | PVDIMAGE pImageParentOverride;
|
---|
395 | /** Original offset of the transfer - required for filtering read requests. */
|
---|
396 | uint64_t uOffsetXferOrig;
|
---|
397 | /** Original size of the transfer - required for fitlering read requests. */
|
---|
398 | size_t cbXferOrig;
|
---|
399 | } Io;
|
---|
400 | /** Discard requests. */
|
---|
401 | struct
|
---|
402 | {
|
---|
403 | /** Pointer to the range descriptor array. */
|
---|
404 | PCRTRANGE paRanges;
|
---|
405 | /** Number of ranges in the array. */
|
---|
406 | unsigned cRanges;
|
---|
407 | /** Range descriptor index which is processed. */
|
---|
408 | unsigned idxRange;
|
---|
409 | /** Start offset to discard currently. */
|
---|
410 | uint64_t offCur;
|
---|
411 | /** How many bytes left to discard in the current range. */
|
---|
412 | size_t cbDiscardLeft;
|
---|
413 | /** How many bytes to discard in the current block (<= cbDiscardLeft). */
|
---|
414 | size_t cbThisDiscard;
|
---|
415 | /** Discard block handled currently. */
|
---|
416 | PVDDISCARDBLOCK pBlock;
|
---|
417 | } Discard;
|
---|
418 | } Req;
|
---|
419 | /** Parent I/O context if any. Sets the type of the context (root/child) */
|
---|
420 | PVDIOCTX pIoCtxParent;
|
---|
421 | /** Type dependent data (root/child) */
|
---|
422 | union
|
---|
423 | {
|
---|
424 | /** Root data */
|
---|
425 | struct
|
---|
426 | {
|
---|
427 | /** Completion callback */
|
---|
428 | PFNVDASYNCTRANSFERCOMPLETE pfnComplete;
|
---|
429 | /** User argument 1 passed on completion. */
|
---|
430 | void *pvUser1;
|
---|
431 | /** User argument 2 passed on completion. */
|
---|
432 | void *pvUser2;
|
---|
433 | } Root;
|
---|
434 | /** Child data */
|
---|
435 | struct
|
---|
436 | {
|
---|
437 | /** Saved start offset */
|
---|
438 | uint64_t uOffsetSaved;
|
---|
439 | /** Saved transfer size */
|
---|
440 | size_t cbTransferLeftSaved;
|
---|
441 | /** Number of bytes transferred from the parent if this context completes. */
|
---|
442 | size_t cbTransferParent;
|
---|
443 | /** Number of bytes to pre read */
|
---|
444 | size_t cbPreRead;
|
---|
445 | /** Number of bytes to post read. */
|
---|
446 | size_t cbPostRead;
|
---|
447 | /** Number of bytes to write left in the parent. */
|
---|
448 | size_t cbWriteParent;
|
---|
449 | /** Write type dependent data. */
|
---|
450 | union
|
---|
451 | {
|
---|
452 | /** Optimized */
|
---|
453 | struct
|
---|
454 | {
|
---|
455 | /** Bytes to fill to satisfy the block size. Not part of the virtual disk. */
|
---|
456 | size_t cbFill;
|
---|
457 | /** Bytes to copy instead of reading from the parent */
|
---|
458 | size_t cbWriteCopy;
|
---|
459 | /** Bytes to read from the image. */
|
---|
460 | size_t cbReadImage;
|
---|
461 | } Optimized;
|
---|
462 | } Write;
|
---|
463 | } Child;
|
---|
464 | } Type;
|
---|
465 | } VDIOCTX;
|
---|
466 |
|
---|
467 | /** Default flags for an I/O context, i.e. unblocked and async. */
|
---|
468 | #define VDIOCTX_FLAGS_DEFAULT (0)
|
---|
469 | /** Flag whether the context is blocked. */
|
---|
470 | #define VDIOCTX_FLAGS_BLOCKED RT_BIT_32(0)
|
---|
471 | /** Flag whether the I/O context is using synchronous I/O. */
|
---|
472 | #define VDIOCTX_FLAGS_SYNC RT_BIT_32(1)
|
---|
473 | /** Flag whether the read should update the cache. */
|
---|
474 | #define VDIOCTX_FLAGS_READ_UPDATE_CACHE RT_BIT_32(2)
|
---|
475 | /** Flag whether free blocks should be zeroed.
|
---|
476 | * If false and no image has data for sepcified
|
---|
477 | * range VERR_VD_BLOCK_FREE is returned for the I/O context.
|
---|
478 | * Note that unallocated blocks are still zeroed
|
---|
479 | * if at least one image has valid data for a part
|
---|
480 | * of the range.
|
---|
481 | */
|
---|
482 | #define VDIOCTX_FLAGS_ZERO_FREE_BLOCKS RT_BIT_32(3)
|
---|
483 | /** Don't free the I/O context when complete because
|
---|
484 | * it was alloacted elsewhere (stack, ...). */
|
---|
485 | #define VDIOCTX_FLAGS_DONT_FREE RT_BIT_32(4)
|
---|
486 | /** Don't set the modified flag for this I/O context when writing. */
|
---|
487 | #define VDIOCTX_FLAGS_DONT_SET_MODIFIED_FLAG RT_BIT_32(5)
|
---|
488 | /** The write filter was applied already and shouldn't be applied a second time.
|
---|
489 | * Used at the beginning of vdWriteHelperAsync() because it might be called
|
---|
490 | * multiple times.
|
---|
491 | */
|
---|
492 | #define VDIOCTX_FLAGS_WRITE_FILTER_APPLIED RT_BIT_32(6)
|
---|
493 |
|
---|
494 | /** NIL I/O context pointer value. */
|
---|
495 | #define NIL_VDIOCTX ((PVDIOCTX)0)
|
---|
496 |
|
---|
497 | /**
|
---|
498 | * List node for deferred I/O contexts.
|
---|
499 | */
|
---|
500 | typedef struct VDIOCTXDEFERRED
|
---|
501 | {
|
---|
502 | /** Node in the list of deferred requests.
|
---|
503 | * A request can be deferred if the image is growing
|
---|
504 | * and the request accesses the same range or if
|
---|
505 | * the backend needs to read or write metadata from the disk
|
---|
506 | * before it can continue. */
|
---|
507 | RTLISTNODE NodeDeferred;
|
---|
508 | /** I/O context this entry points to. */
|
---|
509 | PVDIOCTX pIoCtx;
|
---|
510 | } VDIOCTXDEFERRED, *PVDIOCTXDEFERRED;
|
---|
511 |
|
---|
512 | /**
|
---|
513 | * I/O task.
|
---|
514 | */
|
---|
515 | typedef struct VDIOTASK
|
---|
516 | {
|
---|
517 | /** Next I/O task waiting in the list. */
|
---|
518 | struct VDIOTASK * volatile pNext;
|
---|
519 | /** Storage this task belongs to. */
|
---|
520 | PVDIOSTORAGE pIoStorage;
|
---|
521 | /** Optional completion callback. */
|
---|
522 | PFNVDXFERCOMPLETED pfnComplete;
|
---|
523 | /** Opaque user data. */
|
---|
524 | void *pvUser;
|
---|
525 | /** Completion status code for the task. */
|
---|
526 | int rcReq;
|
---|
527 | /** Flag whether this is a meta data transfer. */
|
---|
528 | bool fMeta;
|
---|
529 | /** Type dependent data. */
|
---|
530 | union
|
---|
531 | {
|
---|
532 | /** User data transfer. */
|
---|
533 | struct
|
---|
534 | {
|
---|
535 | /** Number of bytes this task transferred. */
|
---|
536 | uint32_t cbTransfer;
|
---|
537 | /** Pointer to the I/O context the task belongs. */
|
---|
538 | PVDIOCTX pIoCtx;
|
---|
539 | } User;
|
---|
540 | /** Meta data transfer. */
|
---|
541 | struct
|
---|
542 | {
|
---|
543 | /** Meta transfer this task is for. */
|
---|
544 | PVDMETAXFER pMetaXfer;
|
---|
545 | } Meta;
|
---|
546 | } Type;
|
---|
547 | } VDIOTASK;
|
---|
548 |
|
---|
549 | /**
|
---|
550 | * Storage handle.
|
---|
551 | */
|
---|
552 | typedef struct VDIOSTORAGE
|
---|
553 | {
|
---|
554 | /** Image I/O state this storage handle belongs to. */
|
---|
555 | PVDIO pVDIo;
|
---|
556 | /** AVL tree for pending async metadata transfers. */
|
---|
557 | PAVLRFOFFTREE pTreeMetaXfers;
|
---|
558 | /** Storage handle */
|
---|
559 | void *pStorage;
|
---|
560 | } VDIOSTORAGE;
|
---|
561 |
|
---|
562 | /**
|
---|
563 | * Metadata transfer.
|
---|
564 | *
|
---|
565 | * @note This entry can't be freed if either the list is not empty or
|
---|
566 | * the reference counter is not 0.
|
---|
567 | * The assumption is that the backends don't need to read huge amounts of
|
---|
568 | * metadata to complete a transfer so the additional memory overhead should
|
---|
569 | * be relatively small.
|
---|
570 | */
|
---|
571 | typedef struct VDMETAXFER
|
---|
572 | {
|
---|
573 | /** AVL core for fast search (the file offset is the key) */
|
---|
574 | AVLRFOFFNODECORE Core;
|
---|
575 | /** I/O storage for this transfer. */
|
---|
576 | PVDIOSTORAGE pIoStorage;
|
---|
577 | /** Flags. */
|
---|
578 | uint32_t fFlags;
|
---|
579 | /** List of I/O contexts waiting for this metadata transfer to complete. */
|
---|
580 | RTLISTNODE ListIoCtxWaiting;
|
---|
581 | /** Number of references to this entry. */
|
---|
582 | unsigned cRefs;
|
---|
583 | /** Size of the data stored with this entry. */
|
---|
584 | size_t cbMeta;
|
---|
585 | /** Shadow buffer which is used in case a write is still active and other
|
---|
586 | * writes update the shadow buffer. */
|
---|
587 | uint8_t *pbDataShw;
|
---|
588 | /** List of I/O contexts updating the shadow buffer while there is a write
|
---|
589 | * in progress. */
|
---|
590 | RTLISTNODE ListIoCtxShwWrites;
|
---|
591 | /** Data stored - variable size. */
|
---|
592 | uint8_t abData[1];
|
---|
593 | } VDMETAXFER;
|
---|
594 |
|
---|
595 | /**
|
---|
596 | * The transfer direction for the metadata.
|
---|
597 | */
|
---|
598 | #define VDMETAXFER_TXDIR_MASK 0x3
|
---|
599 | #define VDMETAXFER_TXDIR_NONE 0x0
|
---|
600 | #define VDMETAXFER_TXDIR_WRITE 0x1
|
---|
601 | #define VDMETAXFER_TXDIR_READ 0x2
|
---|
602 | #define VDMETAXFER_TXDIR_FLUSH 0x3
|
---|
603 | #define VDMETAXFER_TXDIR_GET(flags) ((flags) & VDMETAXFER_TXDIR_MASK)
|
---|
604 | #define VDMETAXFER_TXDIR_SET(flags, dir) ((flags) = (flags & ~VDMETAXFER_TXDIR_MASK) | (dir))
|
---|
605 |
|
---|
606 | /**
|
---|
607 | * Plugin structure.
|
---|
608 | */
|
---|
609 | typedef struct VDPLUGIN
|
---|
610 | {
|
---|
611 | /** Pointer to the next plugin structure. */
|
---|
612 | RTLISTNODE NodePlugin;
|
---|
613 | /** Handle of loaded plugin library. */
|
---|
614 | RTLDRMOD hPlugin;
|
---|
615 | /** Filename of the loaded plugin. */
|
---|
616 | char *pszFilename;
|
---|
617 | } VDPLUGIN;
|
---|
618 | /** Pointer to a plugin structure. */
|
---|
619 | typedef VDPLUGIN *PVDPLUGIN;
|
---|
620 |
|
---|
621 | /** Head of loaded plugin list. */
|
---|
622 | static RTLISTANCHOR g_ListPluginsLoaded;
|
---|
623 |
|
---|
624 | /** Number of image backends supported. */
|
---|
625 | static unsigned g_cBackends = 0;
|
---|
626 | /** Array of pointers to the image backends. */
|
---|
627 | static PCVBOXHDDBACKEND *g_apBackends = NULL;
|
---|
628 | /** Array of handles to the corresponding plugin. */
|
---|
629 | static RTLDRMOD *g_ahBackendPlugins = NULL;
|
---|
630 | /** Builtin image backends. */
|
---|
631 | static PCVBOXHDDBACKEND aStaticBackends[] =
|
---|
632 | {
|
---|
633 | &g_VmdkBackend,
|
---|
634 | &g_VDIBackend,
|
---|
635 | &g_VhdBackend,
|
---|
636 | &g_ParallelsBackend,
|
---|
637 | &g_DmgBackend,
|
---|
638 | &g_QedBackend,
|
---|
639 | &g_QCowBackend,
|
---|
640 | &g_VhdxBackend,
|
---|
641 | &g_RawBackend,
|
---|
642 | &g_ISCSIBackend
|
---|
643 | };
|
---|
644 |
|
---|
645 | /** Number of supported cache backends. */
|
---|
646 | static unsigned g_cCacheBackends = 0;
|
---|
647 | /** Array of pointers to the cache backends. */
|
---|
648 | static PCVDCACHEBACKEND *g_apCacheBackends = NULL;
|
---|
649 | /** Array of handles to the corresponding plugin. */
|
---|
650 | static RTLDRMOD *g_ahCacheBackendPlugins = NULL;
|
---|
651 | /** Builtin cache backends. */
|
---|
652 | static PCVDCACHEBACKEND aStaticCacheBackends[] =
|
---|
653 | {
|
---|
654 | &g_VciCacheBackend
|
---|
655 | };
|
---|
656 |
|
---|
657 | /** Number of supported filter backends. */
|
---|
658 | static unsigned g_cFilterBackends = 0;
|
---|
659 | /** Array of pointers to the filters backends. */
|
---|
660 | static PCVDFILTERBACKEND *g_apFilterBackends = NULL;
|
---|
661 | #ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
662 | /** Array of handles to the corresponding plugin. */
|
---|
663 | static PRTLDRMOD g_pahFilterBackendPlugins = NULL;
|
---|
664 | #endif
|
---|
665 |
|
---|
666 | /** Forward declaration of the async discard helper. */
|
---|
667 | static DECLCALLBACK(int) vdDiscardHelperAsync(PVDIOCTX pIoCtx);
|
---|
668 | static DECLCALLBACK(int) vdWriteHelperAsync(PVDIOCTX pIoCtx);
|
---|
669 | static void vdDiskProcessBlockedIoCtx(PVBOXHDD pDisk);
|
---|
670 | static int vdDiskUnlock(PVBOXHDD pDisk, PVDIOCTX pIoCtxRc);
|
---|
671 | static DECLCALLBACK(void) vdIoCtxSyncComplete(void *pvUser1, void *pvUser2, int rcReq);
|
---|
672 |
|
---|
673 | /**
|
---|
674 | * internal: add several backends.
|
---|
675 | */
|
---|
676 | static int vdAddBackends(RTLDRMOD hPlugin, PCVBOXHDDBACKEND *ppBackends, unsigned cBackends)
|
---|
677 | {
|
---|
678 | PCVBOXHDDBACKEND *pTmp = (PCVBOXHDDBACKEND*)RTMemRealloc(g_apBackends,
|
---|
679 | (g_cBackends + cBackends) * sizeof(PCVBOXHDDBACKEND));
|
---|
680 | if (RT_UNLIKELY(!pTmp))
|
---|
681 | return VERR_NO_MEMORY;
|
---|
682 | g_apBackends = pTmp;
|
---|
683 |
|
---|
684 | RTLDRMOD *pTmpPlugins = (RTLDRMOD*)RTMemRealloc(g_ahBackendPlugins,
|
---|
685 | (g_cBackends + cBackends) * sizeof(RTLDRMOD));
|
---|
686 | if (RT_UNLIKELY(!pTmpPlugins))
|
---|
687 | return VERR_NO_MEMORY;
|
---|
688 | g_ahBackendPlugins = pTmpPlugins;
|
---|
689 | memcpy(&g_apBackends[g_cBackends], ppBackends, cBackends * sizeof(PCVBOXHDDBACKEND));
|
---|
690 | for (unsigned i = g_cBackends; i < g_cBackends + cBackends; i++)
|
---|
691 | g_ahBackendPlugins[i] = hPlugin;
|
---|
692 | g_cBackends += cBackends;
|
---|
693 | return VINF_SUCCESS;
|
---|
694 | }
|
---|
695 |
|
---|
696 | #ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
697 | /**
|
---|
698 | * internal: add single backend.
|
---|
699 | */
|
---|
700 | DECLINLINE(int) vdAddBackend(RTLDRMOD hPlugin, PCVBOXHDDBACKEND pBackend)
|
---|
701 | {
|
---|
702 | return vdAddBackends(hPlugin, &pBackend, 1);
|
---|
703 | }
|
---|
704 | #endif
|
---|
705 |
|
---|
706 | /**
|
---|
707 | * internal: add several cache backends.
|
---|
708 | */
|
---|
709 | static int vdAddCacheBackends(RTLDRMOD hPlugin, PCVDCACHEBACKEND *ppBackends, unsigned cBackends)
|
---|
710 | {
|
---|
711 | PCVDCACHEBACKEND *pTmp = (PCVDCACHEBACKEND*)RTMemRealloc(g_apCacheBackends,
|
---|
712 | (g_cCacheBackends + cBackends) * sizeof(PCVDCACHEBACKEND));
|
---|
713 | if (RT_UNLIKELY(!pTmp))
|
---|
714 | return VERR_NO_MEMORY;
|
---|
715 | g_apCacheBackends = pTmp;
|
---|
716 |
|
---|
717 | RTLDRMOD *pTmpPlugins = (RTLDRMOD*)RTMemRealloc(g_ahCacheBackendPlugins,
|
---|
718 | (g_cCacheBackends + cBackends) * sizeof(RTLDRMOD));
|
---|
719 | if (RT_UNLIKELY(!pTmpPlugins))
|
---|
720 | return VERR_NO_MEMORY;
|
---|
721 | g_ahCacheBackendPlugins = pTmpPlugins;
|
---|
722 | memcpy(&g_apCacheBackends[g_cCacheBackends], ppBackends, cBackends * sizeof(PCVDCACHEBACKEND));
|
---|
723 | for (unsigned i = g_cCacheBackends; i < g_cCacheBackends + cBackends; i++)
|
---|
724 | g_ahCacheBackendPlugins[i] = hPlugin;
|
---|
725 | g_cCacheBackends += cBackends;
|
---|
726 | return VINF_SUCCESS;
|
---|
727 | }
|
---|
728 |
|
---|
729 | #ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
730 |
|
---|
731 | /**
|
---|
732 | * internal: add single cache backend.
|
---|
733 | */
|
---|
734 | DECLINLINE(int) vdAddCacheBackend(RTLDRMOD hPlugin, PCVDCACHEBACKEND pBackend)
|
---|
735 | {
|
---|
736 | return vdAddCacheBackends(hPlugin, &pBackend, 1);
|
---|
737 | }
|
---|
738 |
|
---|
739 |
|
---|
740 | /**
|
---|
741 | * Add several filter backends.
|
---|
742 | *
|
---|
743 | * @returns VBox status code.
|
---|
744 | * @param hPlugin Plugin handle to add.
|
---|
745 | * @param ppBackends Array of filter backends to add.
|
---|
746 | * @param cBackends Number of backends to add.
|
---|
747 | */
|
---|
748 | static int vdAddFilterBackends(RTLDRMOD hPlugin, PCVDFILTERBACKEND *ppBackends, unsigned cBackends)
|
---|
749 | {
|
---|
750 | PCVDFILTERBACKEND *pTmp = (PCVDFILTERBACKEND *)RTMemRealloc(g_apFilterBackends,
|
---|
751 | (g_cFilterBackends + cBackends) * sizeof(PCVDFILTERBACKEND));
|
---|
752 | if (RT_UNLIKELY(!pTmp))
|
---|
753 | return VERR_NO_MEMORY;
|
---|
754 | g_apFilterBackends = pTmp;
|
---|
755 |
|
---|
756 | PRTLDRMOD pTmpPlugins = (PRTLDRMOD)RTMemRealloc(g_pahFilterBackendPlugins,
|
---|
757 | (g_cFilterBackends + cBackends) * sizeof(RTLDRMOD));
|
---|
758 | if (RT_UNLIKELY(!pTmpPlugins))
|
---|
759 | return VERR_NO_MEMORY;
|
---|
760 |
|
---|
761 | g_pahFilterBackendPlugins = pTmpPlugins;
|
---|
762 | memcpy(&g_apFilterBackends[g_cFilterBackends], ppBackends, cBackends * sizeof(PCVDFILTERBACKEND));
|
---|
763 | for (unsigned i = g_cFilterBackends; i < g_cFilterBackends + cBackends; i++)
|
---|
764 | g_pahFilterBackendPlugins[i] = hPlugin;
|
---|
765 | g_cFilterBackends += cBackends;
|
---|
766 | return VINF_SUCCESS;
|
---|
767 | }
|
---|
768 |
|
---|
769 |
|
---|
770 | /**
|
---|
771 | * Add a single filter backend to the list of supported filters.
|
---|
772 | *
|
---|
773 | * @returns VBox status code.
|
---|
774 | * @param hPlugin Plugin handle to add.
|
---|
775 | * @param pBackend The backend to add.
|
---|
776 | */
|
---|
777 | DECLINLINE(int) vdAddFilterBackend(RTLDRMOD hPlugin, PCVDFILTERBACKEND pBackend)
|
---|
778 | {
|
---|
779 | return vdAddFilterBackends(hPlugin, &pBackend, 1);
|
---|
780 | }
|
---|
781 |
|
---|
782 | #endif /* VBOX_HDD_NO_DYNAMIC_BACKENDS*/
|
---|
783 |
|
---|
784 | /**
|
---|
785 | * internal: issue error message.
|
---|
786 | */
|
---|
787 | static int vdError(PVBOXHDD pDisk, int rc, RT_SRC_POS_DECL,
|
---|
788 | const char *pszFormat, ...)
|
---|
789 | {
|
---|
790 | va_list va;
|
---|
791 | va_start(va, pszFormat);
|
---|
792 | if (pDisk->pInterfaceError)
|
---|
793 | pDisk->pInterfaceError->pfnError(pDisk->pInterfaceError->Core.pvUser, rc, RT_SRC_POS_ARGS, pszFormat, va);
|
---|
794 | va_end(va);
|
---|
795 | return rc;
|
---|
796 | }
|
---|
797 |
|
---|
798 | /**
|
---|
799 | * internal: thread synchronization, start read.
|
---|
800 | */
|
---|
801 | DECLINLINE(int) vdThreadStartRead(PVBOXHDD pDisk)
|
---|
802 | {
|
---|
803 | int rc = VINF_SUCCESS;
|
---|
804 | if (RT_UNLIKELY(pDisk->pInterfaceThreadSync))
|
---|
805 | rc = pDisk->pInterfaceThreadSync->pfnStartRead(pDisk->pInterfaceThreadSync->Core.pvUser);
|
---|
806 | return rc;
|
---|
807 | }
|
---|
808 |
|
---|
809 | /**
|
---|
810 | * internal: thread synchronization, finish read.
|
---|
811 | */
|
---|
812 | DECLINLINE(int) vdThreadFinishRead(PVBOXHDD pDisk)
|
---|
813 | {
|
---|
814 | int rc = VINF_SUCCESS;
|
---|
815 | if (RT_UNLIKELY(pDisk->pInterfaceThreadSync))
|
---|
816 | rc = pDisk->pInterfaceThreadSync->pfnFinishRead(pDisk->pInterfaceThreadSync->Core.pvUser);
|
---|
817 | return rc;
|
---|
818 | }
|
---|
819 |
|
---|
820 | /**
|
---|
821 | * internal: thread synchronization, start write.
|
---|
822 | */
|
---|
823 | DECLINLINE(int) vdThreadStartWrite(PVBOXHDD pDisk)
|
---|
824 | {
|
---|
825 | int rc = VINF_SUCCESS;
|
---|
826 | if (RT_UNLIKELY(pDisk->pInterfaceThreadSync))
|
---|
827 | rc = pDisk->pInterfaceThreadSync->pfnStartWrite(pDisk->pInterfaceThreadSync->Core.pvUser);
|
---|
828 | return rc;
|
---|
829 | }
|
---|
830 |
|
---|
831 | /**
|
---|
832 | * internal: thread synchronization, finish write.
|
---|
833 | */
|
---|
834 | DECLINLINE(int) vdThreadFinishWrite(PVBOXHDD pDisk)
|
---|
835 | {
|
---|
836 | int rc = VINF_SUCCESS;
|
---|
837 | if (RT_UNLIKELY(pDisk->pInterfaceThreadSync))
|
---|
838 | rc = pDisk->pInterfaceThreadSync->pfnFinishWrite(pDisk->pInterfaceThreadSync->Core.pvUser);
|
---|
839 | return rc;
|
---|
840 | }
|
---|
841 |
|
---|
842 | /**
|
---|
843 | * internal: find image format backend.
|
---|
844 | */
|
---|
845 | static int vdFindBackend(const char *pszBackend, PCVBOXHDDBACKEND *ppBackend)
|
---|
846 | {
|
---|
847 | int rc = VINF_SUCCESS;
|
---|
848 | PCVBOXHDDBACKEND pBackend = NULL;
|
---|
849 |
|
---|
850 | if (!g_apBackends)
|
---|
851 | VDInit();
|
---|
852 |
|
---|
853 | for (unsigned i = 0; i < g_cBackends; i++)
|
---|
854 | {
|
---|
855 | if (!RTStrICmp(pszBackend, g_apBackends[i]->pszBackendName))
|
---|
856 | {
|
---|
857 | pBackend = g_apBackends[i];
|
---|
858 | break;
|
---|
859 | }
|
---|
860 | }
|
---|
861 | *ppBackend = pBackend;
|
---|
862 | return rc;
|
---|
863 | }
|
---|
864 |
|
---|
865 | /**
|
---|
866 | * internal: find cache format backend.
|
---|
867 | */
|
---|
868 | static int vdFindCacheBackend(const char *pszBackend, PCVDCACHEBACKEND *ppBackend)
|
---|
869 | {
|
---|
870 | int rc = VINF_SUCCESS;
|
---|
871 | PCVDCACHEBACKEND pBackend = NULL;
|
---|
872 |
|
---|
873 | if (!g_apCacheBackends)
|
---|
874 | VDInit();
|
---|
875 |
|
---|
876 | for (unsigned i = 0; i < g_cCacheBackends; i++)
|
---|
877 | {
|
---|
878 | if (!RTStrICmp(pszBackend, g_apCacheBackends[i]->pszBackendName))
|
---|
879 | {
|
---|
880 | pBackend = g_apCacheBackends[i];
|
---|
881 | break;
|
---|
882 | }
|
---|
883 | }
|
---|
884 | *ppBackend = pBackend;
|
---|
885 | return rc;
|
---|
886 | }
|
---|
887 |
|
---|
888 | /**
|
---|
889 | * internal: find filter backend.
|
---|
890 | */
|
---|
891 | static int vdFindFilterBackend(const char *pszFilter, PCVDFILTERBACKEND *ppBackend)
|
---|
892 | {
|
---|
893 | int rc = VINF_SUCCESS;
|
---|
894 | PCVDFILTERBACKEND pBackend = NULL;
|
---|
895 |
|
---|
896 | for (unsigned i = 0; i < g_cFilterBackends; i++)
|
---|
897 | {
|
---|
898 | if (!RTStrICmp(pszFilter, g_apFilterBackends[i]->pszBackendName))
|
---|
899 | {
|
---|
900 | pBackend = g_apFilterBackends[i];
|
---|
901 | break;
|
---|
902 | }
|
---|
903 | }
|
---|
904 | *ppBackend = pBackend;
|
---|
905 | return rc;
|
---|
906 | }
|
---|
907 |
|
---|
908 |
|
---|
909 | /**
|
---|
910 | * internal: add image structure to the end of images list.
|
---|
911 | */
|
---|
912 | static void vdAddImageToList(PVBOXHDD pDisk, PVDIMAGE pImage)
|
---|
913 | {
|
---|
914 | pImage->pPrev = NULL;
|
---|
915 | pImage->pNext = NULL;
|
---|
916 |
|
---|
917 | if (pDisk->pBase)
|
---|
918 | {
|
---|
919 | Assert(pDisk->cImages > 0);
|
---|
920 | pImage->pPrev = pDisk->pLast;
|
---|
921 | pDisk->pLast->pNext = pImage;
|
---|
922 | pDisk->pLast = pImage;
|
---|
923 | }
|
---|
924 | else
|
---|
925 | {
|
---|
926 | Assert(pDisk->cImages == 0);
|
---|
927 | pDisk->pBase = pImage;
|
---|
928 | pDisk->pLast = pImage;
|
---|
929 | }
|
---|
930 |
|
---|
931 | pDisk->cImages++;
|
---|
932 | }
|
---|
933 |
|
---|
934 | /**
|
---|
935 | * internal: remove image structure from the images list.
|
---|
936 | */
|
---|
937 | static void vdRemoveImageFromList(PVBOXHDD pDisk, PVDIMAGE pImage)
|
---|
938 | {
|
---|
939 | Assert(pDisk->cImages > 0);
|
---|
940 |
|
---|
941 | if (pImage->pPrev)
|
---|
942 | pImage->pPrev->pNext = pImage->pNext;
|
---|
943 | else
|
---|
944 | pDisk->pBase = pImage->pNext;
|
---|
945 |
|
---|
946 | if (pImage->pNext)
|
---|
947 | pImage->pNext->pPrev = pImage->pPrev;
|
---|
948 | else
|
---|
949 | pDisk->pLast = pImage->pPrev;
|
---|
950 |
|
---|
951 | pImage->pPrev = NULL;
|
---|
952 | pImage->pNext = NULL;
|
---|
953 |
|
---|
954 | pDisk->cImages--;
|
---|
955 | }
|
---|
956 |
|
---|
957 | /**
|
---|
958 | * Release a referene to the filter decrementing the counter and destroying the filter
|
---|
959 | * when the counter reaches zero.
|
---|
960 | *
|
---|
961 | * @returns The new reference count.
|
---|
962 | * @param pFilter The filter to release.
|
---|
963 | */
|
---|
964 | static uint32_t vdFilterRelease(PVDFILTER pFilter)
|
---|
965 | {
|
---|
966 | uint32_t cRefs = ASMAtomicDecU32(&pFilter->cRefs);
|
---|
967 | if (!cRefs)
|
---|
968 | {
|
---|
969 | pFilter->pBackend->pfnDestroy(pFilter->pvBackendData);
|
---|
970 | RTMemFree(pFilter);
|
---|
971 | }
|
---|
972 |
|
---|
973 | return cRefs;
|
---|
974 | }
|
---|
975 |
|
---|
976 | /**
|
---|
977 | * Increments the reference counter of the given filter.
|
---|
978 | *
|
---|
979 | * @return The new reference count.
|
---|
980 | * @param pFilter The filter.
|
---|
981 | */
|
---|
982 | static uint32_t vdFilterRetain(PVDFILTER pFilter)
|
---|
983 | {
|
---|
984 | return ASMAtomicIncU32(&pFilter->cRefs);
|
---|
985 | }
|
---|
986 |
|
---|
987 | /**
|
---|
988 | * internal: find image by index into the images list.
|
---|
989 | */
|
---|
990 | static PVDIMAGE vdGetImageByNumber(PVBOXHDD pDisk, unsigned nImage)
|
---|
991 | {
|
---|
992 | PVDIMAGE pImage = pDisk->pBase;
|
---|
993 | if (nImage == VD_LAST_IMAGE)
|
---|
994 | return pDisk->pLast;
|
---|
995 | while (pImage && nImage)
|
---|
996 | {
|
---|
997 | pImage = pImage->pNext;
|
---|
998 | nImage--;
|
---|
999 | }
|
---|
1000 | return pImage;
|
---|
1001 | }
|
---|
1002 |
|
---|
1003 | /**
|
---|
1004 | * Applies the filter chain to the given write request.
|
---|
1005 | *
|
---|
1006 | * @returns VBox status code.
|
---|
1007 | * @param pDisk The HDD container.
|
---|
1008 | * @param uOffset The start offset of the write.
|
---|
1009 | * @param cbWrite Number of bytes to write.
|
---|
1010 | * @param pIoCtx The I/O context associated with the request.
|
---|
1011 | */
|
---|
1012 | static int vdFilterChainApplyWrite(PVBOXHDD pDisk, uint64_t uOffset, size_t cbWrite,
|
---|
1013 | PVDIOCTX pIoCtx)
|
---|
1014 | {
|
---|
1015 | int rc = VINF_SUCCESS;
|
---|
1016 |
|
---|
1017 | VD_IS_LOCKED(pDisk);
|
---|
1018 |
|
---|
1019 | PVDFILTER pFilter;
|
---|
1020 | RTListForEach(&pDisk->ListFilterChainWrite, pFilter, VDFILTER, ListNodeChainWrite)
|
---|
1021 | {
|
---|
1022 | rc = pFilter->pBackend->pfnFilterWrite(pFilter->pvBackendData, uOffset, cbWrite, pIoCtx);
|
---|
1023 | if (RT_FAILURE(rc))
|
---|
1024 | break;
|
---|
1025 | /* Reset S/G buffer for the next filter. */
|
---|
1026 | RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
|
---|
1027 | }
|
---|
1028 |
|
---|
1029 | return rc;
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | /**
|
---|
1033 | * Applies the filter chain to the given read request.
|
---|
1034 | *
|
---|
1035 | * @returns VBox status code.
|
---|
1036 | * @param pDisk The HDD container.
|
---|
1037 | * @param uOffset The start offset of the read.
|
---|
1038 | * @param cbRead Number of bytes read.
|
---|
1039 | * @param pIoCtx The I/O context associated with the request.
|
---|
1040 | */
|
---|
1041 | static int vdFilterChainApplyRead(PVBOXHDD pDisk, uint64_t uOffset, size_t cbRead,
|
---|
1042 | PVDIOCTX pIoCtx)
|
---|
1043 | {
|
---|
1044 | int rc = VINF_SUCCESS;
|
---|
1045 |
|
---|
1046 | VD_IS_LOCKED(pDisk);
|
---|
1047 |
|
---|
1048 | /* Reset buffer before starting. */
|
---|
1049 | RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
|
---|
1050 |
|
---|
1051 | PVDFILTER pFilter;
|
---|
1052 | RTListForEach(&pDisk->ListFilterChainRead, pFilter, VDFILTER, ListNodeChainRead)
|
---|
1053 | {
|
---|
1054 | rc = pFilter->pBackend->pfnFilterRead(pFilter->pvBackendData, uOffset, cbRead, pIoCtx);
|
---|
1055 | if (RT_FAILURE(rc))
|
---|
1056 | break;
|
---|
1057 | /* Reset S/G buffer for the next filter. */
|
---|
1058 | RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | return rc;
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | DECLINLINE(void) vdIoCtxRootComplete(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
|
---|
1065 | {
|
---|
1066 | if ( RT_SUCCESS(pIoCtx->rcReq)
|
---|
1067 | && pIoCtx->enmTxDir == VDIOCTXTXDIR_READ)
|
---|
1068 | pIoCtx->rcReq = vdFilterChainApplyRead(pDisk, pIoCtx->Req.Io.uOffsetXferOrig,
|
---|
1069 | pIoCtx->Req.Io.cbXferOrig, pIoCtx);
|
---|
1070 |
|
---|
1071 | pIoCtx->Type.Root.pfnComplete(pIoCtx->Type.Root.pvUser1,
|
---|
1072 | pIoCtx->Type.Root.pvUser2,
|
---|
1073 | pIoCtx->rcReq);
|
---|
1074 | }
|
---|
1075 |
|
---|
1076 | /**
|
---|
1077 | * Initialize the structure members of a given I/O context.
|
---|
1078 | */
|
---|
1079 | DECLINLINE(void) vdIoCtxInit(PVDIOCTX pIoCtx, PVBOXHDD pDisk, VDIOCTXTXDIR enmTxDir,
|
---|
1080 | uint64_t uOffset, size_t cbTransfer, PVDIMAGE pImageStart,
|
---|
1081 | PCRTSGBUF pcSgBuf, void *pvAllocation,
|
---|
1082 | PFNVDIOCTXTRANSFER pfnIoCtxTransfer, uint32_t fFlags)
|
---|
1083 | {
|
---|
1084 | pIoCtx->pDisk = pDisk;
|
---|
1085 | pIoCtx->enmTxDir = enmTxDir;
|
---|
1086 | pIoCtx->Req.Io.cbTransferLeft = (uint32_t)cbTransfer; Assert((uint32_t)cbTransfer == cbTransfer);
|
---|
1087 | pIoCtx->Req.Io.uOffset = uOffset;
|
---|
1088 | pIoCtx->Req.Io.cbTransfer = cbTransfer;
|
---|
1089 | pIoCtx->Req.Io.pImageStart = pImageStart;
|
---|
1090 | pIoCtx->Req.Io.pImageCur = pImageStart;
|
---|
1091 | pIoCtx->Req.Io.cbBufClear = 0;
|
---|
1092 | pIoCtx->Req.Io.pImageParentOverride = NULL;
|
---|
1093 | pIoCtx->Req.Io.uOffsetXferOrig = uOffset;
|
---|
1094 | pIoCtx->Req.Io.cbXferOrig = cbTransfer;
|
---|
1095 | pIoCtx->cDataTransfersPending = 0;
|
---|
1096 | pIoCtx->cMetaTransfersPending = 0;
|
---|
1097 | pIoCtx->fComplete = false;
|
---|
1098 | pIoCtx->fFlags = fFlags;
|
---|
1099 | pIoCtx->pvAllocation = pvAllocation;
|
---|
1100 | pIoCtx->pfnIoCtxTransfer = pfnIoCtxTransfer;
|
---|
1101 | pIoCtx->pfnIoCtxTransferNext = NULL;
|
---|
1102 | pIoCtx->rcReq = VINF_SUCCESS;
|
---|
1103 | pIoCtx->pIoCtxParent = NULL;
|
---|
1104 |
|
---|
1105 | /* There is no S/G list for a flush request. */
|
---|
1106 | if ( enmTxDir != VDIOCTXTXDIR_FLUSH
|
---|
1107 | && enmTxDir != VDIOCTXTXDIR_DISCARD)
|
---|
1108 | RTSgBufClone(&pIoCtx->Req.Io.SgBuf, pcSgBuf);
|
---|
1109 | else
|
---|
1110 | memset(&pIoCtx->Req.Io.SgBuf, 0, sizeof(RTSGBUF));
|
---|
1111 | }
|
---|
1112 |
|
---|
1113 | /**
|
---|
1114 | * Internal: Tries to read the desired range from the given cache.
|
---|
1115 | *
|
---|
1116 | * @returns VBox status code.
|
---|
1117 | * @retval VERR_VD_BLOCK_FREE if the block is not in the cache.
|
---|
1118 | * pcbRead will be set to the number of bytes not in the cache.
|
---|
1119 | * Everything thereafter might be in the cache.
|
---|
1120 | * @param pCache The cache to read from.
|
---|
1121 | * @param uOffset Offset of the virtual disk to read.
|
---|
1122 | * @param cbRead How much to read.
|
---|
1123 | * @param pIoCtx The I/O context to read into.
|
---|
1124 | * @param pcbRead Where to store the number of bytes actually read.
|
---|
1125 | * On success this indicates the number of bytes read from the cache.
|
---|
1126 | * If VERR_VD_BLOCK_FREE is returned this gives the number of bytes
|
---|
1127 | * which are not in the cache.
|
---|
1128 | * In both cases everything beyond this value
|
---|
1129 | * might or might not be in the cache.
|
---|
1130 | */
|
---|
1131 | static int vdCacheReadHelper(PVDCACHE pCache, uint64_t uOffset,
|
---|
1132 | size_t cbRead, PVDIOCTX pIoCtx, size_t *pcbRead)
|
---|
1133 | {
|
---|
1134 | int rc = VINF_SUCCESS;
|
---|
1135 |
|
---|
1136 | LogFlowFunc(("pCache=%#p uOffset=%llu pIoCtx=%p cbRead=%zu pcbRead=%#p\n",
|
---|
1137 | pCache, uOffset, pIoCtx, cbRead, pcbRead));
|
---|
1138 |
|
---|
1139 | AssertPtr(pCache);
|
---|
1140 | AssertPtr(pcbRead);
|
---|
1141 |
|
---|
1142 | rc = pCache->Backend->pfnRead(pCache->pBackendData, uOffset, cbRead,
|
---|
1143 | pIoCtx, pcbRead);
|
---|
1144 |
|
---|
1145 | LogFlowFunc(("returns rc=%Rrc pcbRead=%zu\n", rc, *pcbRead));
|
---|
1146 | return rc;
|
---|
1147 | }
|
---|
1148 |
|
---|
1149 | /**
|
---|
1150 | * Internal: Writes data for the given block into the cache.
|
---|
1151 | *
|
---|
1152 | * @returns VBox status code.
|
---|
1153 | * @param pCache The cache to write to.
|
---|
1154 | * @param uOffset Offset of the virtual disk to write to the cache.
|
---|
1155 | * @param cbWrite How much to write.
|
---|
1156 | * @param pIoCtx The I/O context to write from.
|
---|
1157 | * @param pcbWritten How much data could be written, optional.
|
---|
1158 | */
|
---|
1159 | static int vdCacheWriteHelper(PVDCACHE pCache, uint64_t uOffset, size_t cbWrite,
|
---|
1160 | PVDIOCTX pIoCtx, size_t *pcbWritten)
|
---|
1161 | {
|
---|
1162 | int rc = VINF_SUCCESS;
|
---|
1163 |
|
---|
1164 | LogFlowFunc(("pCache=%#p uOffset=%llu pIoCtx=%p cbWrite=%zu pcbWritten=%#p\n",
|
---|
1165 | pCache, uOffset, pIoCtx, cbWrite, pcbWritten));
|
---|
1166 |
|
---|
1167 | AssertPtr(pCache);
|
---|
1168 | AssertPtr(pIoCtx);
|
---|
1169 | Assert(cbWrite > 0);
|
---|
1170 |
|
---|
1171 | if (pcbWritten)
|
---|
1172 | rc = pCache->Backend->pfnWrite(pCache->pBackendData, uOffset, cbWrite,
|
---|
1173 | pIoCtx, pcbWritten);
|
---|
1174 | else
|
---|
1175 | {
|
---|
1176 | size_t cbWritten = 0;
|
---|
1177 |
|
---|
1178 | do
|
---|
1179 | {
|
---|
1180 | rc = pCache->Backend->pfnWrite(pCache->pBackendData, uOffset, cbWrite,
|
---|
1181 | pIoCtx, &cbWritten);
|
---|
1182 | uOffset += cbWritten;
|
---|
1183 | cbWrite -= cbWritten;
|
---|
1184 | } while ( cbWrite
|
---|
1185 | && ( RT_SUCCESS(rc)
|
---|
1186 | || rc == VERR_VD_ASYNC_IO_IN_PROGRESS));
|
---|
1187 | }
|
---|
1188 |
|
---|
1189 | LogFlowFunc(("returns rc=%Rrc pcbWritten=%zu\n",
|
---|
1190 | rc, pcbWritten ? *pcbWritten : cbWrite));
|
---|
1191 | return rc;
|
---|
1192 | }
|
---|
1193 |
|
---|
1194 | /**
|
---|
1195 | * Creates a new empty discard state.
|
---|
1196 | *
|
---|
1197 | * @returns Pointer to the new discard state or NULL if out of memory.
|
---|
1198 | */
|
---|
1199 | static PVDDISCARDSTATE vdDiscardStateCreate(void)
|
---|
1200 | {
|
---|
1201 | PVDDISCARDSTATE pDiscard = (PVDDISCARDSTATE)RTMemAllocZ(sizeof(VDDISCARDSTATE));
|
---|
1202 |
|
---|
1203 | if (pDiscard)
|
---|
1204 | {
|
---|
1205 | RTListInit(&pDiscard->ListLru);
|
---|
1206 | pDiscard->pTreeBlocks = (PAVLRU64TREE)RTMemAllocZ(sizeof(AVLRU64TREE));
|
---|
1207 | if (!pDiscard->pTreeBlocks)
|
---|
1208 | {
|
---|
1209 | RTMemFree(pDiscard);
|
---|
1210 | pDiscard = NULL;
|
---|
1211 | }
|
---|
1212 | }
|
---|
1213 |
|
---|
1214 | return pDiscard;
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 | /**
|
---|
1218 | * Removes the least recently used blocks from the waiting list until
|
---|
1219 | * the new value is reached.
|
---|
1220 | *
|
---|
1221 | * @returns VBox status code.
|
---|
1222 | * @param pDisk VD disk container.
|
---|
1223 | * @param pDiscard The discard state.
|
---|
1224 | * @param cbDiscardingNew How many bytes should be waiting on success.
|
---|
1225 | * The number of bytes waiting can be less.
|
---|
1226 | */
|
---|
1227 | static int vdDiscardRemoveBlocks(PVBOXHDD pDisk, PVDDISCARDSTATE pDiscard, size_t cbDiscardingNew)
|
---|
1228 | {
|
---|
1229 | int rc = VINF_SUCCESS;
|
---|
1230 |
|
---|
1231 | LogFlowFunc(("pDisk=%#p pDiscard=%#p cbDiscardingNew=%zu\n",
|
---|
1232 | pDisk, pDiscard, cbDiscardingNew));
|
---|
1233 |
|
---|
1234 | while (pDiscard->cbDiscarding > cbDiscardingNew)
|
---|
1235 | {
|
---|
1236 | PVDDISCARDBLOCK pBlock = RTListGetLast(&pDiscard->ListLru, VDDISCARDBLOCK, NodeLru);
|
---|
1237 |
|
---|
1238 | Assert(!RTListIsEmpty(&pDiscard->ListLru));
|
---|
1239 |
|
---|
1240 | /* Go over the allocation bitmap and mark all discarded sectors as unused. */
|
---|
1241 | uint64_t offStart = pBlock->Core.Key;
|
---|
1242 | uint32_t idxStart = 0;
|
---|
1243 | size_t cbLeft = pBlock->cbDiscard;
|
---|
1244 | bool fAllocated = ASMBitTest(pBlock->pbmAllocated, idxStart);
|
---|
1245 | uint32_t cSectors = (uint32_t)(pBlock->cbDiscard / 512);
|
---|
1246 |
|
---|
1247 | while (cbLeft > 0)
|
---|
1248 | {
|
---|
1249 | int32_t idxEnd;
|
---|
1250 | size_t cbThis = cbLeft;
|
---|
1251 |
|
---|
1252 | if (fAllocated)
|
---|
1253 | {
|
---|
1254 | /* Check for the first unallocated bit. */
|
---|
1255 | idxEnd = ASMBitNextClear(pBlock->pbmAllocated, cSectors, idxStart);
|
---|
1256 | if (idxEnd != -1)
|
---|
1257 | {
|
---|
1258 | cbThis = (idxEnd - idxStart) * 512;
|
---|
1259 | fAllocated = false;
|
---|
1260 | }
|
---|
1261 | }
|
---|
1262 | else
|
---|
1263 | {
|
---|
1264 | /* Mark as unused and check for the first set bit. */
|
---|
1265 | idxEnd = ASMBitNextSet(pBlock->pbmAllocated, cSectors, idxStart);
|
---|
1266 | if (idxEnd != -1)
|
---|
1267 | cbThis = (idxEnd - idxStart) * 512;
|
---|
1268 |
|
---|
1269 |
|
---|
1270 | VDIOCTX IoCtx;
|
---|
1271 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_DISCARD, 0, 0, NULL,
|
---|
1272 | NULL, NULL, NULL, VDIOCTX_FLAGS_SYNC);
|
---|
1273 | rc = pDisk->pLast->Backend->pfnDiscard(pDisk->pLast->pBackendData,
|
---|
1274 | &IoCtx, offStart, cbThis, NULL,
|
---|
1275 | NULL, &cbThis, NULL,
|
---|
1276 | VD_DISCARD_MARK_UNUSED);
|
---|
1277 | if (RT_FAILURE(rc))
|
---|
1278 | break;
|
---|
1279 |
|
---|
1280 | fAllocated = true;
|
---|
1281 | }
|
---|
1282 |
|
---|
1283 | idxStart = idxEnd;
|
---|
1284 | offStart += cbThis;
|
---|
1285 | cbLeft -= cbThis;
|
---|
1286 | }
|
---|
1287 |
|
---|
1288 | if (RT_FAILURE(rc))
|
---|
1289 | break;
|
---|
1290 |
|
---|
1291 | PVDDISCARDBLOCK pBlockRemove = (PVDDISCARDBLOCK)RTAvlrU64RangeRemove(pDiscard->pTreeBlocks, pBlock->Core.Key);
|
---|
1292 | Assert(pBlockRemove == pBlock); NOREF(pBlockRemove);
|
---|
1293 | RTListNodeRemove(&pBlock->NodeLru);
|
---|
1294 |
|
---|
1295 | pDiscard->cbDiscarding -= pBlock->cbDiscard;
|
---|
1296 | RTMemFree(pBlock->pbmAllocated);
|
---|
1297 | RTMemFree(pBlock);
|
---|
1298 | }
|
---|
1299 |
|
---|
1300 | Assert(RT_FAILURE(rc) || pDiscard->cbDiscarding <= cbDiscardingNew);
|
---|
1301 |
|
---|
1302 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
1303 | return rc;
|
---|
1304 | }
|
---|
1305 |
|
---|
1306 | /**
|
---|
1307 | * Destroys the current discard state, writing any waiting blocks to the image.
|
---|
1308 | *
|
---|
1309 | * @returns VBox status code.
|
---|
1310 | * @param pDisk VD disk container.
|
---|
1311 | */
|
---|
1312 | static int vdDiscardStateDestroy(PVBOXHDD pDisk)
|
---|
1313 | {
|
---|
1314 | int rc = VINF_SUCCESS;
|
---|
1315 |
|
---|
1316 | if (pDisk->pDiscard)
|
---|
1317 | {
|
---|
1318 | rc = vdDiscardRemoveBlocks(pDisk, pDisk->pDiscard, 0 /* Remove all blocks. */);
|
---|
1319 | AssertRC(rc);
|
---|
1320 | RTMemFree(pDisk->pDiscard->pTreeBlocks);
|
---|
1321 | RTMemFree(pDisk->pDiscard);
|
---|
1322 | pDisk->pDiscard = NULL;
|
---|
1323 | }
|
---|
1324 |
|
---|
1325 | return rc;
|
---|
1326 | }
|
---|
1327 |
|
---|
1328 | /**
|
---|
1329 | * Marks the given range as allocated in the image.
|
---|
1330 | * Required if there are discards in progress and a write to a block which can get discarded
|
---|
1331 | * is written to.
|
---|
1332 | *
|
---|
1333 | * @returns VBox status code.
|
---|
1334 | * @param pDisk VD container data.
|
---|
1335 | * @param uOffset First byte to mark as allocated.
|
---|
1336 | * @param cbRange Number of bytes to mark as allocated.
|
---|
1337 | */
|
---|
1338 | static int vdDiscardSetRangeAllocated(PVBOXHDD pDisk, uint64_t uOffset, size_t cbRange)
|
---|
1339 | {
|
---|
1340 | PVDDISCARDSTATE pDiscard = pDisk->pDiscard;
|
---|
1341 | int rc = VINF_SUCCESS;
|
---|
1342 |
|
---|
1343 | if (pDiscard)
|
---|
1344 | {
|
---|
1345 | do
|
---|
1346 | {
|
---|
1347 | size_t cbThisRange = cbRange;
|
---|
1348 | PVDDISCARDBLOCK pBlock = (PVDDISCARDBLOCK)RTAvlrU64RangeGet(pDiscard->pTreeBlocks, uOffset);
|
---|
1349 |
|
---|
1350 | if (pBlock)
|
---|
1351 | {
|
---|
1352 | int32_t idxStart, idxEnd;
|
---|
1353 |
|
---|
1354 | Assert(!(cbThisRange % 512));
|
---|
1355 | Assert(!((uOffset - pBlock->Core.Key) % 512));
|
---|
1356 |
|
---|
1357 | cbThisRange = RT_MIN(cbThisRange, pBlock->Core.KeyLast - uOffset + 1);
|
---|
1358 |
|
---|
1359 | idxStart = (uOffset - pBlock->Core.Key) / 512;
|
---|
1360 | idxEnd = idxStart + (int32_t)(cbThisRange / 512);
|
---|
1361 | ASMBitSetRange(pBlock->pbmAllocated, idxStart, idxEnd);
|
---|
1362 | }
|
---|
1363 | else
|
---|
1364 | {
|
---|
1365 | pBlock = (PVDDISCARDBLOCK)RTAvlrU64GetBestFit(pDiscard->pTreeBlocks, uOffset, true);
|
---|
1366 | if (pBlock)
|
---|
1367 | cbThisRange = RT_MIN(cbThisRange, pBlock->Core.Key - uOffset);
|
---|
1368 | }
|
---|
1369 |
|
---|
1370 | Assert(cbRange >= cbThisRange);
|
---|
1371 |
|
---|
1372 | uOffset += cbThisRange;
|
---|
1373 | cbRange -= cbThisRange;
|
---|
1374 | } while (cbRange != 0);
|
---|
1375 | }
|
---|
1376 |
|
---|
1377 | return rc;
|
---|
1378 | }
|
---|
1379 |
|
---|
1380 | DECLINLINE(PVDIOCTX) vdIoCtxAlloc(PVBOXHDD pDisk, VDIOCTXTXDIR enmTxDir,
|
---|
1381 | uint64_t uOffset, size_t cbTransfer,
|
---|
1382 | PVDIMAGE pImageStart,PCRTSGBUF pcSgBuf,
|
---|
1383 | void *pvAllocation, PFNVDIOCTXTRANSFER pfnIoCtxTransfer,
|
---|
1384 | uint32_t fFlags)
|
---|
1385 | {
|
---|
1386 | PVDIOCTX pIoCtx = NULL;
|
---|
1387 |
|
---|
1388 | pIoCtx = (PVDIOCTX)RTMemCacheAlloc(pDisk->hMemCacheIoCtx);
|
---|
1389 | if (RT_LIKELY(pIoCtx))
|
---|
1390 | {
|
---|
1391 | vdIoCtxInit(pIoCtx, pDisk, enmTxDir, uOffset, cbTransfer, pImageStart,
|
---|
1392 | pcSgBuf, pvAllocation, pfnIoCtxTransfer, fFlags);
|
---|
1393 | }
|
---|
1394 |
|
---|
1395 | return pIoCtx;
|
---|
1396 | }
|
---|
1397 |
|
---|
1398 | DECLINLINE(PVDIOCTX) vdIoCtxRootAlloc(PVBOXHDD pDisk, VDIOCTXTXDIR enmTxDir,
|
---|
1399 | uint64_t uOffset, size_t cbTransfer,
|
---|
1400 | PVDIMAGE pImageStart, PCRTSGBUF pcSgBuf,
|
---|
1401 | PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
1402 | void *pvUser1, void *pvUser2,
|
---|
1403 | void *pvAllocation,
|
---|
1404 | PFNVDIOCTXTRANSFER pfnIoCtxTransfer,
|
---|
1405 | uint32_t fFlags)
|
---|
1406 | {
|
---|
1407 | PVDIOCTX pIoCtx = vdIoCtxAlloc(pDisk, enmTxDir, uOffset, cbTransfer, pImageStart,
|
---|
1408 | pcSgBuf, pvAllocation, pfnIoCtxTransfer, fFlags);
|
---|
1409 |
|
---|
1410 | if (RT_LIKELY(pIoCtx))
|
---|
1411 | {
|
---|
1412 | pIoCtx->pIoCtxParent = NULL;
|
---|
1413 | pIoCtx->Type.Root.pfnComplete = pfnComplete;
|
---|
1414 | pIoCtx->Type.Root.pvUser1 = pvUser1;
|
---|
1415 | pIoCtx->Type.Root.pvUser2 = pvUser2;
|
---|
1416 | }
|
---|
1417 |
|
---|
1418 | LogFlow(("Allocated root I/O context %#p\n", pIoCtx));
|
---|
1419 | return pIoCtx;
|
---|
1420 | }
|
---|
1421 |
|
---|
1422 | DECLINLINE(void) vdIoCtxDiscardInit(PVDIOCTX pIoCtx, PVBOXHDD pDisk, PCRTRANGE paRanges,
|
---|
1423 | unsigned cRanges, PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
1424 | void *pvUser1, void *pvUser2, void *pvAllocation,
|
---|
1425 | PFNVDIOCTXTRANSFER pfnIoCtxTransfer, uint32_t fFlags)
|
---|
1426 | {
|
---|
1427 | pIoCtx->pIoCtxNext = NULL;
|
---|
1428 | pIoCtx->pDisk = pDisk;
|
---|
1429 | pIoCtx->enmTxDir = VDIOCTXTXDIR_DISCARD;
|
---|
1430 | pIoCtx->cDataTransfersPending = 0;
|
---|
1431 | pIoCtx->cMetaTransfersPending = 0;
|
---|
1432 | pIoCtx->fComplete = false;
|
---|
1433 | pIoCtx->fFlags = fFlags;
|
---|
1434 | pIoCtx->pvAllocation = pvAllocation;
|
---|
1435 | pIoCtx->pfnIoCtxTransfer = pfnIoCtxTransfer;
|
---|
1436 | pIoCtx->pfnIoCtxTransferNext = NULL;
|
---|
1437 | pIoCtx->rcReq = VINF_SUCCESS;
|
---|
1438 | pIoCtx->Req.Discard.paRanges = paRanges;
|
---|
1439 | pIoCtx->Req.Discard.cRanges = cRanges;
|
---|
1440 | pIoCtx->Req.Discard.idxRange = 0;
|
---|
1441 | pIoCtx->Req.Discard.cbDiscardLeft = 0;
|
---|
1442 | pIoCtx->Req.Discard.offCur = 0;
|
---|
1443 | pIoCtx->Req.Discard.cbThisDiscard = 0;
|
---|
1444 |
|
---|
1445 | pIoCtx->pIoCtxParent = NULL;
|
---|
1446 | pIoCtx->Type.Root.pfnComplete = pfnComplete;
|
---|
1447 | pIoCtx->Type.Root.pvUser1 = pvUser1;
|
---|
1448 | pIoCtx->Type.Root.pvUser2 = pvUser2;
|
---|
1449 | }
|
---|
1450 |
|
---|
1451 | DECLINLINE(PVDIOCTX) vdIoCtxDiscardAlloc(PVBOXHDD pDisk, PCRTRANGE paRanges,
|
---|
1452 | unsigned cRanges,
|
---|
1453 | PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
1454 | void *pvUser1, void *pvUser2,
|
---|
1455 | void *pvAllocation,
|
---|
1456 | PFNVDIOCTXTRANSFER pfnIoCtxTransfer,
|
---|
1457 | uint32_t fFlags)
|
---|
1458 | {
|
---|
1459 | PVDIOCTX pIoCtx = NULL;
|
---|
1460 |
|
---|
1461 | pIoCtx = (PVDIOCTX)RTMemCacheAlloc(pDisk->hMemCacheIoCtx);
|
---|
1462 | if (RT_LIKELY(pIoCtx))
|
---|
1463 | {
|
---|
1464 | vdIoCtxDiscardInit(pIoCtx, pDisk, paRanges, cRanges, pfnComplete, pvUser1,
|
---|
1465 | pvUser2, pvAllocation, pfnIoCtxTransfer, fFlags);
|
---|
1466 | }
|
---|
1467 |
|
---|
1468 | LogFlow(("Allocated discard I/O context %#p\n", pIoCtx));
|
---|
1469 | return pIoCtx;
|
---|
1470 | }
|
---|
1471 |
|
---|
1472 | DECLINLINE(PVDIOCTX) vdIoCtxChildAlloc(PVBOXHDD pDisk, VDIOCTXTXDIR enmTxDir,
|
---|
1473 | uint64_t uOffset, size_t cbTransfer,
|
---|
1474 | PVDIMAGE pImageStart, PCRTSGBUF pcSgBuf,
|
---|
1475 | PVDIOCTX pIoCtxParent, size_t cbTransferParent,
|
---|
1476 | size_t cbWriteParent, void *pvAllocation,
|
---|
1477 | PFNVDIOCTXTRANSFER pfnIoCtxTransfer)
|
---|
1478 | {
|
---|
1479 | PVDIOCTX pIoCtx = vdIoCtxAlloc(pDisk, enmTxDir, uOffset, cbTransfer, pImageStart,
|
---|
1480 | pcSgBuf, pvAllocation, pfnIoCtxTransfer, pIoCtxParent->fFlags & ~VDIOCTX_FLAGS_DONT_FREE);
|
---|
1481 |
|
---|
1482 | AssertPtr(pIoCtxParent);
|
---|
1483 | Assert(!pIoCtxParent->pIoCtxParent);
|
---|
1484 |
|
---|
1485 | if (RT_LIKELY(pIoCtx))
|
---|
1486 | {
|
---|
1487 | pIoCtx->pIoCtxParent = pIoCtxParent;
|
---|
1488 | pIoCtx->Type.Child.uOffsetSaved = uOffset;
|
---|
1489 | pIoCtx->Type.Child.cbTransferLeftSaved = cbTransfer;
|
---|
1490 | pIoCtx->Type.Child.cbTransferParent = cbTransferParent;
|
---|
1491 | pIoCtx->Type.Child.cbWriteParent = cbWriteParent;
|
---|
1492 | }
|
---|
1493 |
|
---|
1494 | LogFlow(("Allocated child I/O context %#p\n", pIoCtx));
|
---|
1495 | return pIoCtx;
|
---|
1496 | }
|
---|
1497 |
|
---|
1498 | DECLINLINE(PVDIOTASK) vdIoTaskUserAlloc(PVDIOSTORAGE pIoStorage, PFNVDXFERCOMPLETED pfnComplete, void *pvUser, PVDIOCTX pIoCtx, uint32_t cbTransfer)
|
---|
1499 | {
|
---|
1500 | PVDIOTASK pIoTask = NULL;
|
---|
1501 |
|
---|
1502 | pIoTask = (PVDIOTASK)RTMemCacheAlloc(pIoStorage->pVDIo->pDisk->hMemCacheIoTask);
|
---|
1503 | if (pIoTask)
|
---|
1504 | {
|
---|
1505 | pIoTask->pIoStorage = pIoStorage;
|
---|
1506 | pIoTask->pfnComplete = pfnComplete;
|
---|
1507 | pIoTask->pvUser = pvUser;
|
---|
1508 | pIoTask->fMeta = false;
|
---|
1509 | pIoTask->Type.User.cbTransfer = cbTransfer;
|
---|
1510 | pIoTask->Type.User.pIoCtx = pIoCtx;
|
---|
1511 | }
|
---|
1512 |
|
---|
1513 | return pIoTask;
|
---|
1514 | }
|
---|
1515 |
|
---|
1516 | DECLINLINE(PVDIOTASK) vdIoTaskMetaAlloc(PVDIOSTORAGE pIoStorage, PFNVDXFERCOMPLETED pfnComplete, void *pvUser, PVDMETAXFER pMetaXfer)
|
---|
1517 | {
|
---|
1518 | PVDIOTASK pIoTask = NULL;
|
---|
1519 |
|
---|
1520 | pIoTask = (PVDIOTASK)RTMemCacheAlloc(pIoStorage->pVDIo->pDisk->hMemCacheIoTask);
|
---|
1521 | if (pIoTask)
|
---|
1522 | {
|
---|
1523 | pIoTask->pIoStorage = pIoStorage;
|
---|
1524 | pIoTask->pfnComplete = pfnComplete;
|
---|
1525 | pIoTask->pvUser = pvUser;
|
---|
1526 | pIoTask->fMeta = true;
|
---|
1527 | pIoTask->Type.Meta.pMetaXfer = pMetaXfer;
|
---|
1528 | }
|
---|
1529 |
|
---|
1530 | return pIoTask;
|
---|
1531 | }
|
---|
1532 |
|
---|
1533 | DECLINLINE(void) vdIoCtxFree(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
|
---|
1534 | {
|
---|
1535 | Log(("Freeing I/O context %#p\n", pIoCtx));
|
---|
1536 |
|
---|
1537 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_DONT_FREE))
|
---|
1538 | {
|
---|
1539 | if (pIoCtx->pvAllocation)
|
---|
1540 | RTMemFree(pIoCtx->pvAllocation);
|
---|
1541 | #ifdef DEBUG
|
---|
1542 | memset(&pIoCtx->pDisk, 0xff, sizeof(void *));
|
---|
1543 | #endif
|
---|
1544 | RTMemCacheFree(pDisk->hMemCacheIoCtx, pIoCtx);
|
---|
1545 | }
|
---|
1546 | }
|
---|
1547 |
|
---|
1548 | DECLINLINE(void) vdIoTaskFree(PVBOXHDD pDisk, PVDIOTASK pIoTask)
|
---|
1549 | {
|
---|
1550 | #ifdef DEBUG
|
---|
1551 | memset(pIoTask, 0xff, sizeof(VDIOTASK));
|
---|
1552 | #endif
|
---|
1553 | RTMemCacheFree(pDisk->hMemCacheIoTask, pIoTask);
|
---|
1554 | }
|
---|
1555 |
|
---|
1556 | DECLINLINE(void) vdIoCtxChildReset(PVDIOCTX pIoCtx)
|
---|
1557 | {
|
---|
1558 | AssertPtr(pIoCtx->pIoCtxParent);
|
---|
1559 |
|
---|
1560 | RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
|
---|
1561 | pIoCtx->Req.Io.uOffset = pIoCtx->Type.Child.uOffsetSaved;
|
---|
1562 | pIoCtx->Req.Io.cbTransferLeft = (uint32_t)pIoCtx->Type.Child.cbTransferLeftSaved;
|
---|
1563 | Assert((uint32_t)pIoCtx->Type.Child.cbTransferLeftSaved == pIoCtx->Type.Child.cbTransferLeftSaved);
|
---|
1564 | }
|
---|
1565 |
|
---|
1566 | DECLINLINE(PVDMETAXFER) vdMetaXferAlloc(PVDIOSTORAGE pIoStorage, uint64_t uOffset, size_t cb)
|
---|
1567 | {
|
---|
1568 | PVDMETAXFER pMetaXfer = (PVDMETAXFER)RTMemAlloc(RT_OFFSETOF(VDMETAXFER, abData[cb]));
|
---|
1569 |
|
---|
1570 | if (RT_LIKELY(pMetaXfer))
|
---|
1571 | {
|
---|
1572 | pMetaXfer->Core.Key = uOffset;
|
---|
1573 | pMetaXfer->Core.KeyLast = uOffset + cb - 1;
|
---|
1574 | pMetaXfer->fFlags = VDMETAXFER_TXDIR_NONE;
|
---|
1575 | pMetaXfer->cbMeta = cb;
|
---|
1576 | pMetaXfer->pIoStorage = pIoStorage;
|
---|
1577 | pMetaXfer->cRefs = 0;
|
---|
1578 | pMetaXfer->pbDataShw = NULL;
|
---|
1579 | RTListInit(&pMetaXfer->ListIoCtxWaiting);
|
---|
1580 | RTListInit(&pMetaXfer->ListIoCtxShwWrites);
|
---|
1581 | }
|
---|
1582 | return pMetaXfer;
|
---|
1583 | }
|
---|
1584 |
|
---|
1585 | DECLINLINE(void) vdIoCtxAddToWaitingList(volatile PVDIOCTX *ppList, PVDIOCTX pIoCtx)
|
---|
1586 | {
|
---|
1587 | /* Put it on the waiting list. */
|
---|
1588 | PVDIOCTX pNext = ASMAtomicUoReadPtrT(ppList, PVDIOCTX);
|
---|
1589 | PVDIOCTX pHeadOld;
|
---|
1590 | pIoCtx->pIoCtxNext = pNext;
|
---|
1591 | while (!ASMAtomicCmpXchgExPtr(ppList, pIoCtx, pNext, &pHeadOld))
|
---|
1592 | {
|
---|
1593 | pNext = pHeadOld;
|
---|
1594 | Assert(pNext != pIoCtx);
|
---|
1595 | pIoCtx->pIoCtxNext = pNext;
|
---|
1596 | ASMNopPause();
|
---|
1597 | }
|
---|
1598 | }
|
---|
1599 |
|
---|
1600 | DECLINLINE(void) vdIoCtxDefer(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
|
---|
1601 | {
|
---|
1602 | LogFlowFunc(("Deferring I/O context pIoCtx=%#p\n", pIoCtx));
|
---|
1603 |
|
---|
1604 | Assert(!pIoCtx->pIoCtxParent && !(pIoCtx->fFlags & VDIOCTX_FLAGS_BLOCKED));
|
---|
1605 | pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
|
---|
1606 | vdIoCtxAddToWaitingList(&pDisk->pIoCtxBlockedHead, pIoCtx);
|
---|
1607 | }
|
---|
1608 |
|
---|
1609 | static size_t vdIoCtxCopy(PVDIOCTX pIoCtxDst, PVDIOCTX pIoCtxSrc, size_t cbData)
|
---|
1610 | {
|
---|
1611 | return RTSgBufCopy(&pIoCtxDst->Req.Io.SgBuf, &pIoCtxSrc->Req.Io.SgBuf, cbData);
|
---|
1612 | }
|
---|
1613 |
|
---|
1614 | #if 0 /* unused */
|
---|
1615 | static int vdIoCtxCmp(PVDIOCTX pIoCtx1, PVDIOCTX pIoCtx2, size_t cbData)
|
---|
1616 | {
|
---|
1617 | return RTSgBufCmp(&pIoCtx1->Req.Io.SgBuf, &pIoCtx2->Req.Io.SgBuf, cbData);
|
---|
1618 | }
|
---|
1619 | #endif
|
---|
1620 |
|
---|
1621 | static size_t vdIoCtxCopyTo(PVDIOCTX pIoCtx, const uint8_t *pbData, size_t cbData)
|
---|
1622 | {
|
---|
1623 | return RTSgBufCopyFromBuf(&pIoCtx->Req.Io.SgBuf, pbData, cbData);
|
---|
1624 | }
|
---|
1625 |
|
---|
1626 | static size_t vdIoCtxCopyFrom(PVDIOCTX pIoCtx, uint8_t *pbData, size_t cbData)
|
---|
1627 | {
|
---|
1628 | return RTSgBufCopyToBuf(&pIoCtx->Req.Io.SgBuf, pbData, cbData);
|
---|
1629 | }
|
---|
1630 |
|
---|
1631 | static size_t vdIoCtxSet(PVDIOCTX pIoCtx, uint8_t ch, size_t cbData)
|
---|
1632 | {
|
---|
1633 | return RTSgBufSet(&pIoCtx->Req.Io.SgBuf, ch, cbData);
|
---|
1634 | }
|
---|
1635 |
|
---|
1636 | /**
|
---|
1637 | * Process the I/O context, core method which assumes that the I/O context
|
---|
1638 | * acquired the lock.
|
---|
1639 | *
|
---|
1640 | * @returns VBox status code.
|
---|
1641 | * @param pIoCtx I/O context to process.
|
---|
1642 | */
|
---|
1643 | static int vdIoCtxProcessLocked(PVDIOCTX pIoCtx)
|
---|
1644 | {
|
---|
1645 | int rc = VINF_SUCCESS;
|
---|
1646 |
|
---|
1647 | VD_IS_LOCKED(pIoCtx->pDisk);
|
---|
1648 |
|
---|
1649 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
1650 |
|
---|
1651 | if ( !pIoCtx->cMetaTransfersPending
|
---|
1652 | && !pIoCtx->cDataTransfersPending
|
---|
1653 | && !pIoCtx->pfnIoCtxTransfer)
|
---|
1654 | {
|
---|
1655 | rc = VINF_VD_ASYNC_IO_FINISHED;
|
---|
1656 | goto out;
|
---|
1657 | }
|
---|
1658 |
|
---|
1659 | /*
|
---|
1660 | * We complete the I/O context in case of an error
|
---|
1661 | * if there is no I/O task pending.
|
---|
1662 | */
|
---|
1663 | if ( RT_FAILURE(pIoCtx->rcReq)
|
---|
1664 | && !pIoCtx->cMetaTransfersPending
|
---|
1665 | && !pIoCtx->cDataTransfersPending)
|
---|
1666 | {
|
---|
1667 | rc = VINF_VD_ASYNC_IO_FINISHED;
|
---|
1668 | goto out;
|
---|
1669 | }
|
---|
1670 |
|
---|
1671 | /* Don't change anything if there is a metadata transfer pending or we are blocked. */
|
---|
1672 | if ( pIoCtx->cMetaTransfersPending
|
---|
1673 | || (pIoCtx->fFlags & VDIOCTX_FLAGS_BLOCKED))
|
---|
1674 | {
|
---|
1675 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1676 | goto out;
|
---|
1677 | }
|
---|
1678 |
|
---|
1679 | if (pIoCtx->pfnIoCtxTransfer)
|
---|
1680 | {
|
---|
1681 | /* Call the transfer function advancing to the next while there is no error. */
|
---|
1682 | while ( pIoCtx->pfnIoCtxTransfer
|
---|
1683 | && !pIoCtx->cMetaTransfersPending
|
---|
1684 | && RT_SUCCESS(rc))
|
---|
1685 | {
|
---|
1686 | LogFlowFunc(("calling transfer function %#p\n", pIoCtx->pfnIoCtxTransfer));
|
---|
1687 | rc = pIoCtx->pfnIoCtxTransfer(pIoCtx);
|
---|
1688 |
|
---|
1689 | /* Advance to the next part of the transfer if the current one succeeded. */
|
---|
1690 | if (RT_SUCCESS(rc))
|
---|
1691 | {
|
---|
1692 | pIoCtx->pfnIoCtxTransfer = pIoCtx->pfnIoCtxTransferNext;
|
---|
1693 | pIoCtx->pfnIoCtxTransferNext = NULL;
|
---|
1694 | }
|
---|
1695 | }
|
---|
1696 | }
|
---|
1697 |
|
---|
1698 | if ( RT_SUCCESS(rc)
|
---|
1699 | && !pIoCtx->cMetaTransfersPending
|
---|
1700 | && !pIoCtx->cDataTransfersPending
|
---|
1701 | && !(pIoCtx->fFlags & VDIOCTX_FLAGS_BLOCKED))
|
---|
1702 | rc = VINF_VD_ASYNC_IO_FINISHED;
|
---|
1703 | else if ( RT_SUCCESS(rc)
|
---|
1704 | || rc == VERR_VD_NOT_ENOUGH_METADATA
|
---|
1705 | || rc == VERR_VD_IOCTX_HALT)
|
---|
1706 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1707 | else if ( RT_FAILURE(rc)
|
---|
1708 | && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
1709 | {
|
---|
1710 | ASMAtomicCmpXchgS32(&pIoCtx->rcReq, rc, VINF_SUCCESS);
|
---|
1711 |
|
---|
1712 | /*
|
---|
1713 | * The I/O context completed if we have an error and there is no data
|
---|
1714 | * or meta data transfer pending.
|
---|
1715 | */
|
---|
1716 | if ( !pIoCtx->cMetaTransfersPending
|
---|
1717 | && !pIoCtx->cDataTransfersPending)
|
---|
1718 | rc = VINF_VD_ASYNC_IO_FINISHED;
|
---|
1719 | else
|
---|
1720 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1721 | }
|
---|
1722 |
|
---|
1723 | out:
|
---|
1724 | LogFlowFunc(("pIoCtx=%#p rc=%Rrc cDataTransfersPending=%u cMetaTransfersPending=%u fComplete=%RTbool\n",
|
---|
1725 | pIoCtx, rc, pIoCtx->cDataTransfersPending, pIoCtx->cMetaTransfersPending,
|
---|
1726 | pIoCtx->fComplete));
|
---|
1727 |
|
---|
1728 | return rc;
|
---|
1729 | }
|
---|
1730 |
|
---|
1731 | /**
|
---|
1732 | * Processes the list of waiting I/O contexts.
|
---|
1733 | *
|
---|
1734 | * @returns VBox status code, only valid if pIoCtxRc is not NULL, treat as void
|
---|
1735 | * function otherwise.
|
---|
1736 | * @param pDisk The disk structure.
|
---|
1737 | * @param pIoCtxRc An I/O context handle which waits on the list. When processed
|
---|
1738 | * The status code is returned. NULL if there is no I/O context
|
---|
1739 | * to return the status code for.
|
---|
1740 | */
|
---|
1741 | static int vdDiskProcessWaitingIoCtx(PVBOXHDD pDisk, PVDIOCTX pIoCtxRc)
|
---|
1742 | {
|
---|
1743 | int rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1744 |
|
---|
1745 | LogFlowFunc(("pDisk=%#p pIoCtxRc=%#p\n", pDisk, pIoCtxRc));
|
---|
1746 |
|
---|
1747 | VD_IS_LOCKED(pDisk);
|
---|
1748 |
|
---|
1749 | /* Get the waiting list and process it in FIFO order. */
|
---|
1750 | PVDIOCTX pIoCtxHead = ASMAtomicXchgPtrT(&pDisk->pIoCtxHead, NULL, PVDIOCTX);
|
---|
1751 |
|
---|
1752 | /* Reverse it. */
|
---|
1753 | PVDIOCTX pCur = pIoCtxHead;
|
---|
1754 | pIoCtxHead = NULL;
|
---|
1755 | while (pCur)
|
---|
1756 | {
|
---|
1757 | PVDIOCTX pInsert = pCur;
|
---|
1758 | pCur = pCur->pIoCtxNext;
|
---|
1759 | pInsert->pIoCtxNext = pIoCtxHead;
|
---|
1760 | pIoCtxHead = pInsert;
|
---|
1761 | }
|
---|
1762 |
|
---|
1763 | /* Process now. */
|
---|
1764 | pCur = pIoCtxHead;
|
---|
1765 | while (pCur)
|
---|
1766 | {
|
---|
1767 | int rcTmp;
|
---|
1768 | PVDIOCTX pTmp = pCur;
|
---|
1769 |
|
---|
1770 | pCur = pCur->pIoCtxNext;
|
---|
1771 | pTmp->pIoCtxNext = NULL;
|
---|
1772 |
|
---|
1773 | /*
|
---|
1774 | * Need to clear the sync flag here if there is a new I/O context
|
---|
1775 | * with it set and the context is not given in pIoCtxRc.
|
---|
1776 | * This happens most likely on a different thread and that one shouldn't
|
---|
1777 | * process the context synchronously.
|
---|
1778 | *
|
---|
1779 | * The thread who issued the context will wait on the event semaphore
|
---|
1780 | * anyway which is signalled when the completion handler is called.
|
---|
1781 | */
|
---|
1782 | if ( pTmp->fFlags & VDIOCTX_FLAGS_SYNC
|
---|
1783 | && pTmp != pIoCtxRc)
|
---|
1784 | pTmp->fFlags &= ~VDIOCTX_FLAGS_SYNC;
|
---|
1785 |
|
---|
1786 | rcTmp = vdIoCtxProcessLocked(pTmp);
|
---|
1787 | if (pTmp == pIoCtxRc)
|
---|
1788 | {
|
---|
1789 | if ( rcTmp == VINF_VD_ASYNC_IO_FINISHED
|
---|
1790 | && RT_SUCCESS(pTmp->rcReq)
|
---|
1791 | && pTmp->enmTxDir == VDIOCTXTXDIR_READ)
|
---|
1792 | {
|
---|
1793 | int rc2 = vdFilterChainApplyRead(pDisk, pTmp->Req.Io.uOffsetXferOrig,
|
---|
1794 | pTmp->Req.Io.cbXferOrig, pTmp);
|
---|
1795 | if (RT_FAILURE(rc2))
|
---|
1796 | rcTmp = rc2;
|
---|
1797 | }
|
---|
1798 |
|
---|
1799 | /* The given I/O context was processed, pass the return code to the caller. */
|
---|
1800 | if ( rcTmp == VINF_VD_ASYNC_IO_FINISHED
|
---|
1801 | && (pTmp->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
1802 | rc = pTmp->rcReq;
|
---|
1803 | else
|
---|
1804 | rc = rcTmp;
|
---|
1805 | }
|
---|
1806 | else if ( rcTmp == VINF_VD_ASYNC_IO_FINISHED
|
---|
1807 | && ASMAtomicCmpXchgBool(&pTmp->fComplete, true, false))
|
---|
1808 | {
|
---|
1809 | LogFlowFunc(("Waiting I/O context completed pTmp=%#p\n", pTmp));
|
---|
1810 | vdThreadFinishWrite(pDisk);
|
---|
1811 | vdIoCtxRootComplete(pDisk, pTmp);
|
---|
1812 | vdIoCtxFree(pDisk, pTmp);
|
---|
1813 | }
|
---|
1814 | }
|
---|
1815 |
|
---|
1816 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
1817 | return rc;
|
---|
1818 | }
|
---|
1819 |
|
---|
1820 | /**
|
---|
1821 | * Processes the list of blocked I/O contexts.
|
---|
1822 | *
|
---|
1823 | * @returns nothing.
|
---|
1824 | * @param pDisk The disk structure.
|
---|
1825 | */
|
---|
1826 | static void vdDiskProcessBlockedIoCtx(PVBOXHDD pDisk)
|
---|
1827 | {
|
---|
1828 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
1829 |
|
---|
1830 | VD_IS_LOCKED(pDisk);
|
---|
1831 |
|
---|
1832 | /* Get the waiting list and process it in FIFO order. */
|
---|
1833 | PVDIOCTX pIoCtxHead = ASMAtomicXchgPtrT(&pDisk->pIoCtxBlockedHead, NULL, PVDIOCTX);
|
---|
1834 |
|
---|
1835 | /* Reverse it. */
|
---|
1836 | PVDIOCTX pCur = pIoCtxHead;
|
---|
1837 | pIoCtxHead = NULL;
|
---|
1838 | while (pCur)
|
---|
1839 | {
|
---|
1840 | PVDIOCTX pInsert = pCur;
|
---|
1841 | pCur = pCur->pIoCtxNext;
|
---|
1842 | pInsert->pIoCtxNext = pIoCtxHead;
|
---|
1843 | pIoCtxHead = pInsert;
|
---|
1844 | }
|
---|
1845 |
|
---|
1846 | /* Process now. */
|
---|
1847 | pCur = pIoCtxHead;
|
---|
1848 | while (pCur)
|
---|
1849 | {
|
---|
1850 | int rc;
|
---|
1851 | PVDIOCTX pTmp = pCur;
|
---|
1852 |
|
---|
1853 | pCur = pCur->pIoCtxNext;
|
---|
1854 | pTmp->pIoCtxNext = NULL;
|
---|
1855 |
|
---|
1856 | Assert(!pTmp->pIoCtxParent);
|
---|
1857 | Assert(pTmp->fFlags & VDIOCTX_FLAGS_BLOCKED);
|
---|
1858 | pTmp->fFlags &= ~VDIOCTX_FLAGS_BLOCKED;
|
---|
1859 |
|
---|
1860 | rc = vdIoCtxProcessLocked(pTmp);
|
---|
1861 | if ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
1862 | && ASMAtomicCmpXchgBool(&pTmp->fComplete, true, false))
|
---|
1863 | {
|
---|
1864 | LogFlowFunc(("Waiting I/O context completed pTmp=%#p\n", pTmp));
|
---|
1865 | vdThreadFinishWrite(pDisk);
|
---|
1866 | vdIoCtxRootComplete(pDisk, pTmp);
|
---|
1867 | vdIoCtxFree(pDisk, pTmp);
|
---|
1868 | }
|
---|
1869 | }
|
---|
1870 |
|
---|
1871 | LogFlowFunc(("returns\n"));
|
---|
1872 | }
|
---|
1873 |
|
---|
1874 | /**
|
---|
1875 | * Processes the I/O context trying to lock the criticial section.
|
---|
1876 | * The context is deferred if the critical section is busy.
|
---|
1877 | *
|
---|
1878 | * @returns VBox status code.
|
---|
1879 | * @param pIoCtx The I/O context to process.
|
---|
1880 | */
|
---|
1881 | static int vdIoCtxProcessTryLockDefer(PVDIOCTX pIoCtx)
|
---|
1882 | {
|
---|
1883 | int rc = VINF_SUCCESS;
|
---|
1884 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
1885 |
|
---|
1886 | Log(("Defer pIoCtx=%#p\n", pIoCtx));
|
---|
1887 |
|
---|
1888 | /* Put it on the waiting list first. */
|
---|
1889 | vdIoCtxAddToWaitingList(&pDisk->pIoCtxHead, pIoCtx);
|
---|
1890 |
|
---|
1891 | if (ASMAtomicCmpXchgBool(&pDisk->fLocked, true, false))
|
---|
1892 | {
|
---|
1893 | /* Leave it again, the context will be processed just before leaving the lock. */
|
---|
1894 | LogFlowFunc(("Successfully acquired the lock\n"));
|
---|
1895 | rc = vdDiskUnlock(pDisk, pIoCtx);
|
---|
1896 | }
|
---|
1897 | else
|
---|
1898 | {
|
---|
1899 | LogFlowFunc(("Lock is held\n"));
|
---|
1900 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1901 | }
|
---|
1902 |
|
---|
1903 | return rc;
|
---|
1904 | }
|
---|
1905 |
|
---|
1906 | /**
|
---|
1907 | * Process the I/O context in a synchronous manner, waiting
|
---|
1908 | * for it to complete.
|
---|
1909 | *
|
---|
1910 | * @returns VBox status code of the completed request.
|
---|
1911 | * @param pIoCtx The sync I/O context.
|
---|
1912 | * @param hEventComplete Event sempahore to wait on for completion.
|
---|
1913 | */
|
---|
1914 | static int vdIoCtxProcessSync(PVDIOCTX pIoCtx, RTSEMEVENT hEventComplete)
|
---|
1915 | {
|
---|
1916 | int rc = VINF_SUCCESS;
|
---|
1917 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
1918 |
|
---|
1919 | LogFlowFunc(("pIoCtx=%p\n", pIoCtx));
|
---|
1920 |
|
---|
1921 | AssertMsg(pIoCtx->fFlags & (VDIOCTX_FLAGS_SYNC | VDIOCTX_FLAGS_DONT_FREE),
|
---|
1922 | ("I/O context is not marked as synchronous\n"));
|
---|
1923 |
|
---|
1924 | rc = vdIoCtxProcessTryLockDefer(pIoCtx);
|
---|
1925 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
1926 | rc = VINF_SUCCESS;
|
---|
1927 |
|
---|
1928 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
1929 | {
|
---|
1930 | rc = RTSemEventWait(hEventComplete, RT_INDEFINITE_WAIT);
|
---|
1931 | AssertRC(rc);
|
---|
1932 | }
|
---|
1933 |
|
---|
1934 | rc = pIoCtx->rcReq;
|
---|
1935 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
1936 |
|
---|
1937 | return rc;
|
---|
1938 | }
|
---|
1939 |
|
---|
1940 | DECLINLINE(bool) vdIoCtxIsDiskLockOwner(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
|
---|
1941 | {
|
---|
1942 | return pDisk->pIoCtxLockOwner == pIoCtx;
|
---|
1943 | }
|
---|
1944 |
|
---|
1945 | static int vdIoCtxLockDisk(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
|
---|
1946 | {
|
---|
1947 | int rc = VINF_SUCCESS;
|
---|
1948 |
|
---|
1949 | VD_IS_LOCKED(pDisk);
|
---|
1950 |
|
---|
1951 | LogFlowFunc(("pDisk=%#p pIoCtx=%#p\n", pDisk, pIoCtx));
|
---|
1952 |
|
---|
1953 | if (!ASMAtomicCmpXchgPtr(&pDisk->pIoCtxLockOwner, pIoCtx, NIL_VDIOCTX))
|
---|
1954 | {
|
---|
1955 | Assert(pDisk->pIoCtxLockOwner != pIoCtx); /* No nesting allowed. */
|
---|
1956 | vdIoCtxDefer(pDisk, pIoCtx);
|
---|
1957 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1958 | }
|
---|
1959 |
|
---|
1960 | LogFlowFunc(("returns -> %Rrc\n", rc));
|
---|
1961 | return rc;
|
---|
1962 | }
|
---|
1963 |
|
---|
1964 | static void vdIoCtxUnlockDisk(PVBOXHDD pDisk, PVDIOCTX pIoCtx, bool fProcessBlockedReqs)
|
---|
1965 | {
|
---|
1966 | RT_NOREF1(pIoCtx);
|
---|
1967 | LogFlowFunc(("pDisk=%#p pIoCtx=%#p fProcessBlockedReqs=%RTbool\n",
|
---|
1968 | pDisk, pIoCtx, fProcessBlockedReqs));
|
---|
1969 |
|
---|
1970 | VD_IS_LOCKED(pDisk);
|
---|
1971 |
|
---|
1972 | LogFlow(("Unlocking disk lock owner is %#p\n", pDisk->pIoCtxLockOwner));
|
---|
1973 | Assert(pDisk->pIoCtxLockOwner == pIoCtx);
|
---|
1974 | ASMAtomicXchgPtrT(&pDisk->pIoCtxLockOwner, NIL_VDIOCTX, PVDIOCTX);
|
---|
1975 |
|
---|
1976 | if (fProcessBlockedReqs)
|
---|
1977 | {
|
---|
1978 | /* Process any blocked writes if the current request didn't caused another growing. */
|
---|
1979 | vdDiskProcessBlockedIoCtx(pDisk);
|
---|
1980 | }
|
---|
1981 |
|
---|
1982 | LogFlowFunc(("returns\n"));
|
---|
1983 | }
|
---|
1984 |
|
---|
1985 | /**
|
---|
1986 | * Internal: Reads a given amount of data from the image chain of the disk.
|
---|
1987 | **/
|
---|
1988 | static int vdDiskReadHelper(PVBOXHDD pDisk, PVDIMAGE pImage, PVDIMAGE pImageParentOverride,
|
---|
1989 | uint64_t uOffset, size_t cbRead, PVDIOCTX pIoCtx, size_t *pcbThisRead)
|
---|
1990 | {
|
---|
1991 | RT_NOREF1(pDisk);
|
---|
1992 | int rc = VINF_SUCCESS;
|
---|
1993 | size_t cbThisRead = cbRead;
|
---|
1994 |
|
---|
1995 | AssertPtr(pcbThisRead);
|
---|
1996 |
|
---|
1997 | *pcbThisRead = 0;
|
---|
1998 |
|
---|
1999 | /*
|
---|
2000 | * Try to read from the given image.
|
---|
2001 | * If the block is not allocated read from override chain if present.
|
---|
2002 | */
|
---|
2003 | rc = pImage->Backend->pfnRead(pImage->pBackendData,
|
---|
2004 | uOffset, cbThisRead, pIoCtx,
|
---|
2005 | &cbThisRead);
|
---|
2006 |
|
---|
2007 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
2008 | {
|
---|
2009 | for (PVDIMAGE pCurrImage = pImageParentOverride ? pImageParentOverride : pImage->pPrev;
|
---|
2010 | pCurrImage != NULL && rc == VERR_VD_BLOCK_FREE;
|
---|
2011 | pCurrImage = pCurrImage->pPrev)
|
---|
2012 | {
|
---|
2013 | rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
|
---|
2014 | uOffset, cbThisRead, pIoCtx,
|
---|
2015 | &cbThisRead);
|
---|
2016 | }
|
---|
2017 | }
|
---|
2018 |
|
---|
2019 | if (RT_SUCCESS(rc) || rc == VERR_VD_BLOCK_FREE)
|
---|
2020 | *pcbThisRead = cbThisRead;
|
---|
2021 |
|
---|
2022 | return rc;
|
---|
2023 | }
|
---|
2024 |
|
---|
2025 | /**
|
---|
2026 | * internal: read the specified amount of data in whatever blocks the backend
|
---|
2027 | * will give us - async version.
|
---|
2028 | */
|
---|
2029 | static DECLCALLBACK(int) vdReadHelperAsync(PVDIOCTX pIoCtx)
|
---|
2030 | {
|
---|
2031 | int rc;
|
---|
2032 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
2033 | size_t cbToRead = pIoCtx->Req.Io.cbTransfer;
|
---|
2034 | uint64_t uOffset = pIoCtx->Req.Io.uOffset;
|
---|
2035 | PVDIMAGE pCurrImage = pIoCtx->Req.Io.pImageCur;
|
---|
2036 | PVDIMAGE pImageParentOverride = pIoCtx->Req.Io.pImageParentOverride;
|
---|
2037 | unsigned cImagesRead = pIoCtx->Req.Io.cImagesRead;
|
---|
2038 | size_t cbThisRead;
|
---|
2039 |
|
---|
2040 | /*
|
---|
2041 | * Check whether there is a full block write in progress which was not allocated.
|
---|
2042 | * Defer I/O if the range interferes but only if it does not belong to the
|
---|
2043 | * write doing the allocation.
|
---|
2044 | */
|
---|
2045 | if ( pDisk->pIoCtxLockOwner != NIL_VDIOCTX
|
---|
2046 | && uOffset >= pDisk->uOffsetStartLocked
|
---|
2047 | && uOffset < pDisk->uOffsetEndLocked
|
---|
2048 | && ( !pIoCtx->pIoCtxParent
|
---|
2049 | || pIoCtx->pIoCtxParent != pDisk->pIoCtxLockOwner))
|
---|
2050 | {
|
---|
2051 | Log(("Interferring read while allocating a new block => deferring read\n"));
|
---|
2052 | vdIoCtxDefer(pDisk, pIoCtx);
|
---|
2053 | return VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
2054 | }
|
---|
2055 |
|
---|
2056 | /* Loop until all reads started or we have a backend which needs to read metadata. */
|
---|
2057 | do
|
---|
2058 | {
|
---|
2059 | /* Search for image with allocated block. Do not attempt to read more
|
---|
2060 | * than the previous reads marked as valid. Otherwise this would return
|
---|
2061 | * stale data when different block sizes are used for the images. */
|
---|
2062 | cbThisRead = cbToRead;
|
---|
2063 |
|
---|
2064 | if ( pDisk->pCache
|
---|
2065 | && !pImageParentOverride)
|
---|
2066 | {
|
---|
2067 | rc = vdCacheReadHelper(pDisk->pCache, uOffset, cbThisRead,
|
---|
2068 | pIoCtx, &cbThisRead);
|
---|
2069 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
2070 | {
|
---|
2071 | rc = vdDiskReadHelper(pDisk, pCurrImage, NULL, uOffset, cbThisRead,
|
---|
2072 | pIoCtx, &cbThisRead);
|
---|
2073 |
|
---|
2074 | /* If the read was successful, write the data back into the cache. */
|
---|
2075 | if ( RT_SUCCESS(rc)
|
---|
2076 | && pIoCtx->fFlags & VDIOCTX_FLAGS_READ_UPDATE_CACHE)
|
---|
2077 | {
|
---|
2078 | rc = vdCacheWriteHelper(pDisk->pCache, uOffset, cbThisRead,
|
---|
2079 | pIoCtx, NULL);
|
---|
2080 | }
|
---|
2081 | }
|
---|
2082 | }
|
---|
2083 | else
|
---|
2084 | {
|
---|
2085 | /*
|
---|
2086 | * Try to read from the given image.
|
---|
2087 | * If the block is not allocated read from override chain if present.
|
---|
2088 | */
|
---|
2089 | rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
|
---|
2090 | uOffset, cbThisRead, pIoCtx,
|
---|
2091 | &cbThisRead);
|
---|
2092 |
|
---|
2093 | if ( rc == VERR_VD_BLOCK_FREE
|
---|
2094 | && cImagesRead != 1)
|
---|
2095 | {
|
---|
2096 | unsigned cImagesToProcess = cImagesRead;
|
---|
2097 |
|
---|
2098 | pCurrImage = pImageParentOverride ? pImageParentOverride : pCurrImage->pPrev;
|
---|
2099 | pIoCtx->Req.Io.pImageParentOverride = NULL;
|
---|
2100 |
|
---|
2101 | while (pCurrImage && rc == VERR_VD_BLOCK_FREE)
|
---|
2102 | {
|
---|
2103 | rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
|
---|
2104 | uOffset, cbThisRead,
|
---|
2105 | pIoCtx, &cbThisRead);
|
---|
2106 | if (cImagesToProcess == 1)
|
---|
2107 | break;
|
---|
2108 | else if (cImagesToProcess > 0)
|
---|
2109 | cImagesToProcess--;
|
---|
2110 |
|
---|
2111 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
2112 | pCurrImage = pCurrImage->pPrev;
|
---|
2113 | }
|
---|
2114 | }
|
---|
2115 | }
|
---|
2116 |
|
---|
2117 | /* The task state will be updated on success already, don't do it here!. */
|
---|
2118 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
2119 | {
|
---|
2120 | /* No image in the chain contains the data for the block. */
|
---|
2121 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbThisRead); Assert(cbThisRead == (uint32_t)cbThisRead);
|
---|
2122 |
|
---|
2123 | /* Fill the free space with 0 if we are told to do so
|
---|
2124 | * or a previous read returned valid data. */
|
---|
2125 | if (pIoCtx->fFlags & VDIOCTX_FLAGS_ZERO_FREE_BLOCKS)
|
---|
2126 | vdIoCtxSet(pIoCtx, '\0', cbThisRead);
|
---|
2127 | else
|
---|
2128 | pIoCtx->Req.Io.cbBufClear += cbThisRead;
|
---|
2129 |
|
---|
2130 | if (pIoCtx->Req.Io.pImageCur->uOpenFlags & VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS)
|
---|
2131 | rc = VINF_VD_NEW_ZEROED_BLOCK;
|
---|
2132 | else
|
---|
2133 | rc = VINF_SUCCESS;
|
---|
2134 | }
|
---|
2135 | else if (rc == VERR_VD_IOCTX_HALT)
|
---|
2136 | {
|
---|
2137 | uOffset += cbThisRead;
|
---|
2138 | cbToRead -= cbThisRead;
|
---|
2139 | pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
|
---|
2140 | }
|
---|
2141 | else if ( RT_SUCCESS(rc)
|
---|
2142 | || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
2143 | {
|
---|
2144 | /* First not free block, fill the space before with 0. */
|
---|
2145 | if ( pIoCtx->Req.Io.cbBufClear
|
---|
2146 | && !(pIoCtx->fFlags & VDIOCTX_FLAGS_ZERO_FREE_BLOCKS))
|
---|
2147 | {
|
---|
2148 | RTSGBUF SgBuf;
|
---|
2149 | RTSgBufClone(&SgBuf, &pIoCtx->Req.Io.SgBuf);
|
---|
2150 | RTSgBufReset(&SgBuf);
|
---|
2151 | RTSgBufSet(&SgBuf, 0, pIoCtx->Req.Io.cbBufClear);
|
---|
2152 | pIoCtx->Req.Io.cbBufClear = 0;
|
---|
2153 | pIoCtx->fFlags |= VDIOCTX_FLAGS_ZERO_FREE_BLOCKS;
|
---|
2154 | }
|
---|
2155 | rc = VINF_SUCCESS;
|
---|
2156 | }
|
---|
2157 |
|
---|
2158 | if (RT_FAILURE(rc))
|
---|
2159 | break;
|
---|
2160 |
|
---|
2161 | cbToRead -= cbThisRead;
|
---|
2162 | uOffset += cbThisRead;
|
---|
2163 | pCurrImage = pIoCtx->Req.Io.pImageStart; /* Start with the highest image in the chain. */
|
---|
2164 | } while (cbToRead != 0 && RT_SUCCESS(rc));
|
---|
2165 |
|
---|
2166 | if ( rc == VERR_VD_NOT_ENOUGH_METADATA
|
---|
2167 | || rc == VERR_VD_IOCTX_HALT)
|
---|
2168 | {
|
---|
2169 | /* Save the current state. */
|
---|
2170 | pIoCtx->Req.Io.uOffset = uOffset;
|
---|
2171 | pIoCtx->Req.Io.cbTransfer = cbToRead;
|
---|
2172 | pIoCtx->Req.Io.pImageCur = pCurrImage ? pCurrImage : pIoCtx->Req.Io.pImageStart;
|
---|
2173 | }
|
---|
2174 |
|
---|
2175 | return (!(pIoCtx->fFlags & VDIOCTX_FLAGS_ZERO_FREE_BLOCKS))
|
---|
2176 | ? VERR_VD_BLOCK_FREE
|
---|
2177 | : rc;
|
---|
2178 | }
|
---|
2179 |
|
---|
2180 | /**
|
---|
2181 | * internal: parent image read wrapper for compacting.
|
---|
2182 | */
|
---|
2183 | static DECLCALLBACK(int) vdParentRead(void *pvUser, uint64_t uOffset, void *pvBuf,
|
---|
2184 | size_t cbRead)
|
---|
2185 | {
|
---|
2186 | PVDPARENTSTATEDESC pParentState = (PVDPARENTSTATEDESC)pvUser;
|
---|
2187 |
|
---|
2188 | /** @todo
|
---|
2189 | * Only used for compaction so far which is not possible to mix with async I/O.
|
---|
2190 | * Needs to be changed if we want to support online compaction of images.
|
---|
2191 | */
|
---|
2192 | bool fLocked = ASMAtomicXchgBool(&pParentState->pDisk->fLocked, true);
|
---|
2193 | AssertMsgReturn(!fLocked,
|
---|
2194 | ("Calling synchronous parent read while another thread holds the disk lock\n"),
|
---|
2195 | VERR_VD_INVALID_STATE);
|
---|
2196 |
|
---|
2197 | /* Fake an I/O context. */
|
---|
2198 | RTSGSEG Segment;
|
---|
2199 | RTSGBUF SgBuf;
|
---|
2200 | VDIOCTX IoCtx;
|
---|
2201 |
|
---|
2202 | Segment.pvSeg = pvBuf;
|
---|
2203 | Segment.cbSeg = cbRead;
|
---|
2204 | RTSgBufInit(&SgBuf, &Segment, 1);
|
---|
2205 | vdIoCtxInit(&IoCtx, pParentState->pDisk, VDIOCTXTXDIR_READ, uOffset, cbRead, pParentState->pImage,
|
---|
2206 | &SgBuf, NULL, NULL, VDIOCTX_FLAGS_SYNC | VDIOCTX_FLAGS_ZERO_FREE_BLOCKS);
|
---|
2207 | int rc = vdReadHelperAsync(&IoCtx);
|
---|
2208 | ASMAtomicXchgBool(&pParentState->pDisk->fLocked, false);
|
---|
2209 | return rc;
|
---|
2210 | }
|
---|
2211 |
|
---|
2212 | /**
|
---|
2213 | * Extended version of vdReadHelper(), implementing certain optimizations
|
---|
2214 | * for image cloning.
|
---|
2215 | *
|
---|
2216 | * @returns VBox status code.
|
---|
2217 | * @param pDisk The disk to read from.
|
---|
2218 | * @param pImage The image to start reading from.
|
---|
2219 | * @param pImageParentOverride The parent image to read from
|
---|
2220 | * if the starting image returns a free block.
|
---|
2221 | * If NULL is passed the real parent of the image
|
---|
2222 | * in the chain is used.
|
---|
2223 | * @param uOffset Offset in the disk to start reading from.
|
---|
2224 | * @param pvBuf Where to store the read data.
|
---|
2225 | * @param cbRead How much to read.
|
---|
2226 | * @param fZeroFreeBlocks Flag whether free blocks should be zeroed.
|
---|
2227 | * If false and no image has data for sepcified
|
---|
2228 | * range VERR_VD_BLOCK_FREE is returned.
|
---|
2229 | * Note that unallocated blocks are still zeroed
|
---|
2230 | * if at least one image has valid data for a part
|
---|
2231 | * of the range.
|
---|
2232 | * @param fUpdateCache Flag whether to update the attached cache if
|
---|
2233 | * available.
|
---|
2234 | * @param cImagesRead Number of images in the chain to read until
|
---|
2235 | * the read is cut off. A value of 0 disables the cut off.
|
---|
2236 | */
|
---|
2237 | static int vdReadHelperEx(PVBOXHDD pDisk, PVDIMAGE pImage, PVDIMAGE pImageParentOverride,
|
---|
2238 | uint64_t uOffset, void *pvBuf, size_t cbRead,
|
---|
2239 | bool fZeroFreeBlocks, bool fUpdateCache, unsigned cImagesRead)
|
---|
2240 | {
|
---|
2241 | int rc = VINF_SUCCESS;
|
---|
2242 | uint32_t fFlags = VDIOCTX_FLAGS_SYNC | VDIOCTX_FLAGS_DONT_FREE;
|
---|
2243 | RTSGSEG Segment;
|
---|
2244 | RTSGBUF SgBuf;
|
---|
2245 | VDIOCTX IoCtx;
|
---|
2246 | RTSEMEVENT hEventComplete = NIL_RTSEMEVENT;
|
---|
2247 |
|
---|
2248 | rc = RTSemEventCreate(&hEventComplete);
|
---|
2249 | if (RT_FAILURE(rc))
|
---|
2250 | return rc;
|
---|
2251 |
|
---|
2252 | if (fZeroFreeBlocks)
|
---|
2253 | fFlags |= VDIOCTX_FLAGS_ZERO_FREE_BLOCKS;
|
---|
2254 | if (fUpdateCache)
|
---|
2255 | fFlags |= VDIOCTX_FLAGS_READ_UPDATE_CACHE;
|
---|
2256 |
|
---|
2257 | Segment.pvSeg = pvBuf;
|
---|
2258 | Segment.cbSeg = cbRead;
|
---|
2259 | RTSgBufInit(&SgBuf, &Segment, 1);
|
---|
2260 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_READ, uOffset, cbRead, pImage, &SgBuf,
|
---|
2261 | NULL, vdReadHelperAsync, fFlags);
|
---|
2262 |
|
---|
2263 | IoCtx.Req.Io.pImageParentOverride = pImageParentOverride;
|
---|
2264 | IoCtx.Req.Io.cImagesRead = cImagesRead;
|
---|
2265 | IoCtx.Type.Root.pfnComplete = vdIoCtxSyncComplete;
|
---|
2266 | IoCtx.Type.Root.pvUser1 = pDisk;
|
---|
2267 | IoCtx.Type.Root.pvUser2 = hEventComplete;
|
---|
2268 | rc = vdIoCtxProcessSync(&IoCtx, hEventComplete);
|
---|
2269 | RTSemEventDestroy(hEventComplete);
|
---|
2270 | return rc;
|
---|
2271 | }
|
---|
2272 |
|
---|
2273 | /**
|
---|
2274 | * internal: read the specified amount of data in whatever blocks the backend
|
---|
2275 | * will give us.
|
---|
2276 | */
|
---|
2277 | static int vdReadHelper(PVBOXHDD pDisk, PVDIMAGE pImage, uint64_t uOffset,
|
---|
2278 | void *pvBuf, size_t cbRead, bool fUpdateCache)
|
---|
2279 | {
|
---|
2280 | return vdReadHelperEx(pDisk, pImage, NULL, uOffset, pvBuf, cbRead,
|
---|
2281 | true /* fZeroFreeBlocks */, fUpdateCache, 0);
|
---|
2282 | }
|
---|
2283 |
|
---|
2284 | /**
|
---|
2285 | * internal: mark the disk as not modified.
|
---|
2286 | */
|
---|
2287 | static void vdResetModifiedFlag(PVBOXHDD pDisk)
|
---|
2288 | {
|
---|
2289 | if (pDisk->uModified & VD_IMAGE_MODIFIED_FLAG)
|
---|
2290 | {
|
---|
2291 | /* generate new last-modified uuid */
|
---|
2292 | if (!(pDisk->uModified & VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
|
---|
2293 | {
|
---|
2294 | RTUUID Uuid;
|
---|
2295 |
|
---|
2296 | RTUuidCreate(&Uuid);
|
---|
2297 | pDisk->pLast->Backend->pfnSetModificationUuid(pDisk->pLast->pBackendData,
|
---|
2298 | &Uuid);
|
---|
2299 |
|
---|
2300 | if (pDisk->pCache)
|
---|
2301 | pDisk->pCache->Backend->pfnSetModificationUuid(pDisk->pCache->pBackendData,
|
---|
2302 | &Uuid);
|
---|
2303 | }
|
---|
2304 |
|
---|
2305 | pDisk->uModified &= ~VD_IMAGE_MODIFIED_FLAG;
|
---|
2306 | }
|
---|
2307 | }
|
---|
2308 |
|
---|
2309 | /**
|
---|
2310 | * internal: mark the disk as modified.
|
---|
2311 | */
|
---|
2312 | static void vdSetModifiedFlag(PVBOXHDD pDisk)
|
---|
2313 | {
|
---|
2314 | pDisk->uModified |= VD_IMAGE_MODIFIED_FLAG;
|
---|
2315 | if (pDisk->uModified & VD_IMAGE_MODIFIED_FIRST)
|
---|
2316 | {
|
---|
2317 | pDisk->uModified &= ~VD_IMAGE_MODIFIED_FIRST;
|
---|
2318 |
|
---|
2319 | /* First modify, so create a UUID and ensure it's written to disk. */
|
---|
2320 | vdResetModifiedFlag(pDisk);
|
---|
2321 |
|
---|
2322 | if (!(pDisk->uModified & VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
|
---|
2323 | {
|
---|
2324 | VDIOCTX IoCtx;
|
---|
2325 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_FLUSH, 0, 0, NULL,
|
---|
2326 | NULL, NULL, NULL, VDIOCTX_FLAGS_SYNC);
|
---|
2327 | pDisk->pLast->Backend->pfnFlush(pDisk->pLast->pBackendData, &IoCtx);
|
---|
2328 | }
|
---|
2329 | }
|
---|
2330 | }
|
---|
2331 |
|
---|
2332 | /**
|
---|
2333 | * internal: write buffer to the image, taking care of block boundaries and
|
---|
2334 | * write optimizations.
|
---|
2335 | */
|
---|
2336 | static int vdWriteHelperEx(PVBOXHDD pDisk, PVDIMAGE pImage,
|
---|
2337 | PVDIMAGE pImageParentOverride, uint64_t uOffset,
|
---|
2338 | const void *pvBuf, size_t cbWrite,
|
---|
2339 | uint32_t fFlags, unsigned cImagesRead)
|
---|
2340 | {
|
---|
2341 | int rc = VINF_SUCCESS;
|
---|
2342 | RTSGSEG Segment;
|
---|
2343 | RTSGBUF SgBuf;
|
---|
2344 | VDIOCTX IoCtx;
|
---|
2345 | RTSEMEVENT hEventComplete = NIL_RTSEMEVENT;
|
---|
2346 |
|
---|
2347 | rc = RTSemEventCreate(&hEventComplete);
|
---|
2348 | if (RT_FAILURE(rc))
|
---|
2349 | return rc;
|
---|
2350 |
|
---|
2351 | fFlags |= VDIOCTX_FLAGS_SYNC | VDIOCTX_FLAGS_DONT_FREE;
|
---|
2352 |
|
---|
2353 | Segment.pvSeg = (void *)pvBuf;
|
---|
2354 | Segment.cbSeg = cbWrite;
|
---|
2355 | RTSgBufInit(&SgBuf, &Segment, 1);
|
---|
2356 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_WRITE, uOffset, cbWrite, pImage, &SgBuf,
|
---|
2357 | NULL, vdWriteHelperAsync, fFlags);
|
---|
2358 |
|
---|
2359 | IoCtx.Req.Io.pImageParentOverride = pImageParentOverride;
|
---|
2360 | IoCtx.Req.Io.cImagesRead = cImagesRead;
|
---|
2361 | IoCtx.pIoCtxParent = NULL;
|
---|
2362 | IoCtx.Type.Root.pfnComplete = vdIoCtxSyncComplete;
|
---|
2363 | IoCtx.Type.Root.pvUser1 = pDisk;
|
---|
2364 | IoCtx.Type.Root.pvUser2 = hEventComplete;
|
---|
2365 | if (RT_SUCCESS(rc))
|
---|
2366 | rc = vdIoCtxProcessSync(&IoCtx, hEventComplete);
|
---|
2367 |
|
---|
2368 | RTSemEventDestroy(hEventComplete);
|
---|
2369 | return rc;
|
---|
2370 | }
|
---|
2371 |
|
---|
2372 | /**
|
---|
2373 | * internal: write buffer to the image, taking care of block boundaries and
|
---|
2374 | * write optimizations.
|
---|
2375 | */
|
---|
2376 | static int vdWriteHelper(PVBOXHDD pDisk, PVDIMAGE pImage, uint64_t uOffset,
|
---|
2377 | const void *pvBuf, size_t cbWrite, uint32_t fFlags)
|
---|
2378 | {
|
---|
2379 | return vdWriteHelperEx(pDisk, pImage, NULL, uOffset, pvBuf, cbWrite,
|
---|
2380 | fFlags, 0);
|
---|
2381 | }
|
---|
2382 |
|
---|
2383 | /**
|
---|
2384 | * Internal: Copies the content of one disk to another one applying optimizations
|
---|
2385 | * to speed up the copy process if possible.
|
---|
2386 | */
|
---|
2387 | static int vdCopyHelper(PVBOXHDD pDiskFrom, PVDIMAGE pImageFrom, PVBOXHDD pDiskTo,
|
---|
2388 | uint64_t cbSize, unsigned cImagesFromRead, unsigned cImagesToRead,
|
---|
2389 | bool fSuppressRedundantIo, PVDINTERFACEPROGRESS pIfProgress,
|
---|
2390 | PVDINTERFACEPROGRESS pDstIfProgress)
|
---|
2391 | {
|
---|
2392 | int rc = VINF_SUCCESS;
|
---|
2393 | int rc2;
|
---|
2394 | uint64_t uOffset = 0;
|
---|
2395 | uint64_t cbRemaining = cbSize;
|
---|
2396 | void *pvBuf = NULL;
|
---|
2397 | bool fLockReadFrom = false;
|
---|
2398 | bool fLockWriteTo = false;
|
---|
2399 | bool fBlockwiseCopy = false;
|
---|
2400 | unsigned uProgressOld = 0;
|
---|
2401 |
|
---|
2402 | LogFlowFunc(("pDiskFrom=%#p pImageFrom=%#p pDiskTo=%#p cbSize=%llu cImagesFromRead=%u cImagesToRead=%u fSuppressRedundantIo=%RTbool pIfProgress=%#p pDstIfProgress=%#p\n",
|
---|
2403 | pDiskFrom, pImageFrom, pDiskTo, cbSize, cImagesFromRead, cImagesToRead, fSuppressRedundantIo, pDstIfProgress, pDstIfProgress));
|
---|
2404 |
|
---|
2405 | if ( (fSuppressRedundantIo || (cImagesFromRead > 0))
|
---|
2406 | && RTListIsEmpty(&pDiskFrom->ListFilterChainRead))
|
---|
2407 | fBlockwiseCopy = true;
|
---|
2408 |
|
---|
2409 | /* Allocate tmp buffer. */
|
---|
2410 | pvBuf = RTMemTmpAlloc(VD_MERGE_BUFFER_SIZE);
|
---|
2411 | if (!pvBuf)
|
---|
2412 | return rc;
|
---|
2413 |
|
---|
2414 | do
|
---|
2415 | {
|
---|
2416 | size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
|
---|
2417 |
|
---|
2418 | /* Note that we don't attempt to synchronize cross-disk accesses.
|
---|
2419 | * It wouldn't be very difficult to do, just the lock order would
|
---|
2420 | * need to be defined somehow to prevent deadlocks. Postpone such
|
---|
2421 | * magic as there is no use case for this. */
|
---|
2422 |
|
---|
2423 | rc2 = vdThreadStartRead(pDiskFrom);
|
---|
2424 | AssertRC(rc2);
|
---|
2425 | fLockReadFrom = true;
|
---|
2426 |
|
---|
2427 | if (fBlockwiseCopy)
|
---|
2428 | {
|
---|
2429 | RTSGSEG SegmentBuf;
|
---|
2430 | RTSGBUF SgBuf;
|
---|
2431 | VDIOCTX IoCtx;
|
---|
2432 |
|
---|
2433 | SegmentBuf.pvSeg = pvBuf;
|
---|
2434 | SegmentBuf.cbSeg = VD_MERGE_BUFFER_SIZE;
|
---|
2435 | RTSgBufInit(&SgBuf, &SegmentBuf, 1);
|
---|
2436 | vdIoCtxInit(&IoCtx, pDiskFrom, VDIOCTXTXDIR_READ, 0, 0, NULL,
|
---|
2437 | &SgBuf, NULL, NULL, VDIOCTX_FLAGS_SYNC);
|
---|
2438 |
|
---|
2439 | /* Read the source data. */
|
---|
2440 | rc = pImageFrom->Backend->pfnRead(pImageFrom->pBackendData,
|
---|
2441 | uOffset, cbThisRead, &IoCtx,
|
---|
2442 | &cbThisRead);
|
---|
2443 |
|
---|
2444 | if ( rc == VERR_VD_BLOCK_FREE
|
---|
2445 | && cImagesFromRead != 1)
|
---|
2446 | {
|
---|
2447 | unsigned cImagesToProcess = cImagesFromRead;
|
---|
2448 |
|
---|
2449 | for (PVDIMAGE pCurrImage = pImageFrom->pPrev;
|
---|
2450 | pCurrImage != NULL && rc == VERR_VD_BLOCK_FREE;
|
---|
2451 | pCurrImage = pCurrImage->pPrev)
|
---|
2452 | {
|
---|
2453 | rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
|
---|
2454 | uOffset, cbThisRead,
|
---|
2455 | &IoCtx, &cbThisRead);
|
---|
2456 | if (cImagesToProcess == 1)
|
---|
2457 | break;
|
---|
2458 | else if (cImagesToProcess > 0)
|
---|
2459 | cImagesToProcess--;
|
---|
2460 | }
|
---|
2461 | }
|
---|
2462 | }
|
---|
2463 | else
|
---|
2464 | rc = vdReadHelper(pDiskFrom, pImageFrom, uOffset, pvBuf, cbThisRead,
|
---|
2465 | false /* fUpdateCache */);
|
---|
2466 |
|
---|
2467 | if (RT_FAILURE(rc) && rc != VERR_VD_BLOCK_FREE)
|
---|
2468 | break;
|
---|
2469 |
|
---|
2470 | rc2 = vdThreadFinishRead(pDiskFrom);
|
---|
2471 | AssertRC(rc2);
|
---|
2472 | fLockReadFrom = false;
|
---|
2473 |
|
---|
2474 | if (rc != VERR_VD_BLOCK_FREE)
|
---|
2475 | {
|
---|
2476 | rc2 = vdThreadStartWrite(pDiskTo);
|
---|
2477 | AssertRC(rc2);
|
---|
2478 | fLockWriteTo = true;
|
---|
2479 |
|
---|
2480 | /* Only do collapsed I/O if we are copying the data blockwise. */
|
---|
2481 | rc = vdWriteHelperEx(pDiskTo, pDiskTo->pLast, NULL, uOffset, pvBuf,
|
---|
2482 | cbThisRead, VDIOCTX_FLAGS_DONT_SET_MODIFIED_FLAG /* fFlags */,
|
---|
2483 | fBlockwiseCopy ? cImagesToRead : 0);
|
---|
2484 | if (RT_FAILURE(rc))
|
---|
2485 | break;
|
---|
2486 |
|
---|
2487 | rc2 = vdThreadFinishWrite(pDiskTo);
|
---|
2488 | AssertRC(rc2);
|
---|
2489 | fLockWriteTo = false;
|
---|
2490 | }
|
---|
2491 | else /* Don't propagate the error to the outside */
|
---|
2492 | rc = VINF_SUCCESS;
|
---|
2493 |
|
---|
2494 | uOffset += cbThisRead;
|
---|
2495 | cbRemaining -= cbThisRead;
|
---|
2496 |
|
---|
2497 | unsigned uProgressNew = uOffset * 99 / cbSize;
|
---|
2498 | if (uProgressNew != uProgressOld)
|
---|
2499 | {
|
---|
2500 | uProgressOld = uProgressNew;
|
---|
2501 |
|
---|
2502 | if (pIfProgress && pIfProgress->pfnProgress)
|
---|
2503 | {
|
---|
2504 | rc = pIfProgress->pfnProgress(pIfProgress->Core.pvUser,
|
---|
2505 | uProgressOld);
|
---|
2506 | if (RT_FAILURE(rc))
|
---|
2507 | break;
|
---|
2508 | }
|
---|
2509 | if (pDstIfProgress && pDstIfProgress->pfnProgress)
|
---|
2510 | {
|
---|
2511 | rc = pDstIfProgress->pfnProgress(pDstIfProgress->Core.pvUser,
|
---|
2512 | uProgressOld);
|
---|
2513 | if (RT_FAILURE(rc))
|
---|
2514 | break;
|
---|
2515 | }
|
---|
2516 | }
|
---|
2517 | } while (uOffset < cbSize);
|
---|
2518 |
|
---|
2519 | RTMemFree(pvBuf);
|
---|
2520 |
|
---|
2521 | if (fLockReadFrom)
|
---|
2522 | {
|
---|
2523 | rc2 = vdThreadFinishRead(pDiskFrom);
|
---|
2524 | AssertRC(rc2);
|
---|
2525 | }
|
---|
2526 |
|
---|
2527 | if (fLockWriteTo)
|
---|
2528 | {
|
---|
2529 | rc2 = vdThreadFinishWrite(pDiskTo);
|
---|
2530 | AssertRC(rc2);
|
---|
2531 | }
|
---|
2532 |
|
---|
2533 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
2534 | return rc;
|
---|
2535 | }
|
---|
2536 |
|
---|
2537 | /**
|
---|
2538 | * Flush helper async version.
|
---|
2539 | */
|
---|
2540 | static DECLCALLBACK(int) vdSetModifiedHelperAsync(PVDIOCTX pIoCtx)
|
---|
2541 | {
|
---|
2542 | int rc = VINF_SUCCESS;
|
---|
2543 | PVDIMAGE pImage = pIoCtx->Req.Io.pImageCur;
|
---|
2544 |
|
---|
2545 | rc = pImage->Backend->pfnFlush(pImage->pBackendData, pIoCtx);
|
---|
2546 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
2547 | rc = VINF_SUCCESS;
|
---|
2548 |
|
---|
2549 | return rc;
|
---|
2550 | }
|
---|
2551 |
|
---|
2552 | /**
|
---|
2553 | * internal: mark the disk as modified - async version.
|
---|
2554 | */
|
---|
2555 | static int vdSetModifiedFlagAsync(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
|
---|
2556 | {
|
---|
2557 | int rc = VINF_SUCCESS;
|
---|
2558 |
|
---|
2559 | VD_IS_LOCKED(pDisk);
|
---|
2560 |
|
---|
2561 | pDisk->uModified |= VD_IMAGE_MODIFIED_FLAG;
|
---|
2562 | if (pDisk->uModified & VD_IMAGE_MODIFIED_FIRST)
|
---|
2563 | {
|
---|
2564 | rc = vdIoCtxLockDisk(pDisk, pIoCtx);
|
---|
2565 | if (RT_SUCCESS(rc))
|
---|
2566 | {
|
---|
2567 | pDisk->uModified &= ~VD_IMAGE_MODIFIED_FIRST;
|
---|
2568 |
|
---|
2569 | /* First modify, so create a UUID and ensure it's written to disk. */
|
---|
2570 | vdResetModifiedFlag(pDisk);
|
---|
2571 |
|
---|
2572 | if (!(pDisk->uModified & VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
|
---|
2573 | {
|
---|
2574 | PVDIOCTX pIoCtxFlush = vdIoCtxChildAlloc(pDisk, VDIOCTXTXDIR_FLUSH,
|
---|
2575 | 0, 0, pDisk->pLast,
|
---|
2576 | NULL, pIoCtx, 0, 0, NULL,
|
---|
2577 | vdSetModifiedHelperAsync);
|
---|
2578 |
|
---|
2579 | if (pIoCtxFlush)
|
---|
2580 | {
|
---|
2581 | rc = vdIoCtxProcessLocked(pIoCtxFlush);
|
---|
2582 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
2583 | {
|
---|
2584 | vdIoCtxUnlockDisk(pDisk, pIoCtx, false /* fProcessDeferredReqs */);
|
---|
2585 | vdIoCtxFree(pDisk, pIoCtxFlush);
|
---|
2586 | }
|
---|
2587 | else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
2588 | {
|
---|
2589 | ASMAtomicIncU32(&pIoCtx->cDataTransfersPending);
|
---|
2590 | pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
|
---|
2591 | }
|
---|
2592 | else /* Another error */
|
---|
2593 | vdIoCtxFree(pDisk, pIoCtxFlush);
|
---|
2594 | }
|
---|
2595 | else
|
---|
2596 | rc = VERR_NO_MEMORY;
|
---|
2597 | }
|
---|
2598 | }
|
---|
2599 | }
|
---|
2600 |
|
---|
2601 | return rc;
|
---|
2602 | }
|
---|
2603 |
|
---|
2604 | static DECLCALLBACK(int) vdWriteHelperCommitAsync(PVDIOCTX pIoCtx)
|
---|
2605 | {
|
---|
2606 | int rc = VINF_SUCCESS;
|
---|
2607 | PVDIMAGE pImage = pIoCtx->Req.Io.pImageStart;
|
---|
2608 | size_t cbPreRead = pIoCtx->Type.Child.cbPreRead;
|
---|
2609 | size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
|
---|
2610 | size_t cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
|
---|
2611 |
|
---|
2612 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
2613 | rc = pImage->Backend->pfnWrite(pImage->pBackendData,
|
---|
2614 | pIoCtx->Req.Io.uOffset - cbPreRead,
|
---|
2615 | cbPreRead + cbThisWrite + cbPostRead,
|
---|
2616 | pIoCtx, NULL, &cbPreRead, &cbPostRead, 0);
|
---|
2617 | Assert(rc != VERR_VD_BLOCK_FREE);
|
---|
2618 | Assert(rc == VERR_VD_NOT_ENOUGH_METADATA || cbPreRead == 0);
|
---|
2619 | Assert(rc == VERR_VD_NOT_ENOUGH_METADATA || cbPostRead == 0);
|
---|
2620 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
2621 | rc = VINF_SUCCESS;
|
---|
2622 | else if (rc == VERR_VD_IOCTX_HALT)
|
---|
2623 | {
|
---|
2624 | pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
|
---|
2625 | rc = VINF_SUCCESS;
|
---|
2626 | }
|
---|
2627 |
|
---|
2628 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
2629 | return rc;
|
---|
2630 | }
|
---|
2631 |
|
---|
2632 | static DECLCALLBACK(int) vdWriteHelperOptimizedCmpAndWriteAsync(PVDIOCTX pIoCtx)
|
---|
2633 | {
|
---|
2634 | int rc = VINF_SUCCESS;
|
---|
2635 | size_t cbThisWrite = 0;
|
---|
2636 | size_t cbPreRead = pIoCtx->Type.Child.cbPreRead;
|
---|
2637 | size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
|
---|
2638 | size_t cbWriteCopy = pIoCtx->Type.Child.Write.Optimized.cbWriteCopy;
|
---|
2639 | size_t cbFill = pIoCtx->Type.Child.Write.Optimized.cbFill;
|
---|
2640 | size_t cbReadImage = pIoCtx->Type.Child.Write.Optimized.cbReadImage;
|
---|
2641 | PVDIOCTX pIoCtxParent = pIoCtx->pIoCtxParent;
|
---|
2642 |
|
---|
2643 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
2644 |
|
---|
2645 | AssertPtr(pIoCtxParent);
|
---|
2646 | Assert(!pIoCtxParent->pIoCtxParent);
|
---|
2647 | Assert(!pIoCtx->Req.Io.cbTransferLeft && !pIoCtx->cMetaTransfersPending);
|
---|
2648 |
|
---|
2649 | vdIoCtxChildReset(pIoCtx);
|
---|
2650 | cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
|
---|
2651 | RTSgBufAdvance(&pIoCtx->Req.Io.SgBuf, cbPreRead);
|
---|
2652 |
|
---|
2653 | /* Check if the write would modify anything in this block. */
|
---|
2654 | if (!RTSgBufCmp(&pIoCtx->Req.Io.SgBuf, &pIoCtxParent->Req.Io.SgBuf, cbThisWrite))
|
---|
2655 | {
|
---|
2656 | RTSGBUF SgBufSrcTmp;
|
---|
2657 |
|
---|
2658 | RTSgBufClone(&SgBufSrcTmp, &pIoCtxParent->Req.Io.SgBuf);
|
---|
2659 | RTSgBufAdvance(&SgBufSrcTmp, cbThisWrite);
|
---|
2660 | RTSgBufAdvance(&pIoCtx->Req.Io.SgBuf, cbThisWrite);
|
---|
2661 |
|
---|
2662 | if (!cbWriteCopy || !RTSgBufCmp(&pIoCtx->Req.Io.SgBuf, &SgBufSrcTmp, cbWriteCopy))
|
---|
2663 | {
|
---|
2664 | /* Block is completely unchanged, so no need to write anything. */
|
---|
2665 | LogFlowFunc(("Block didn't changed\n"));
|
---|
2666 | ASMAtomicWriteU32(&pIoCtx->Req.Io.cbTransferLeft, 0);
|
---|
2667 | RTSgBufAdvance(&pIoCtxParent->Req.Io.SgBuf, cbThisWrite);
|
---|
2668 | return VINF_VD_ASYNC_IO_FINISHED;
|
---|
2669 | }
|
---|
2670 | }
|
---|
2671 |
|
---|
2672 | /* Copy the data to the right place in the buffer. */
|
---|
2673 | RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
|
---|
2674 | RTSgBufAdvance(&pIoCtx->Req.Io.SgBuf, cbPreRead);
|
---|
2675 | vdIoCtxCopy(pIoCtx, pIoCtxParent, cbThisWrite);
|
---|
2676 |
|
---|
2677 | /* Handle the data that goes after the write to fill the block. */
|
---|
2678 | if (cbPostRead)
|
---|
2679 | {
|
---|
2680 | /* Now assemble the remaining data. */
|
---|
2681 | if (cbWriteCopy)
|
---|
2682 | {
|
---|
2683 | /*
|
---|
2684 | * The S/G buffer of the parent needs to be cloned because
|
---|
2685 | * it is not allowed to modify the state.
|
---|
2686 | */
|
---|
2687 | RTSGBUF SgBufParentTmp;
|
---|
2688 |
|
---|
2689 | RTSgBufClone(&SgBufParentTmp, &pIoCtxParent->Req.Io.SgBuf);
|
---|
2690 | RTSgBufCopy(&pIoCtx->Req.Io.SgBuf, &SgBufParentTmp, cbWriteCopy);
|
---|
2691 | }
|
---|
2692 |
|
---|
2693 | /* Zero out the remainder of this block. Will never be visible, as this
|
---|
2694 | * is beyond the limit of the image. */
|
---|
2695 | if (cbFill)
|
---|
2696 | {
|
---|
2697 | RTSgBufAdvance(&pIoCtx->Req.Io.SgBuf, cbReadImage);
|
---|
2698 | vdIoCtxSet(pIoCtx, '\0', cbFill);
|
---|
2699 | }
|
---|
2700 | }
|
---|
2701 |
|
---|
2702 | /* Write the full block to the virtual disk. */
|
---|
2703 | RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
|
---|
2704 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperCommitAsync;
|
---|
2705 |
|
---|
2706 | return rc;
|
---|
2707 | }
|
---|
2708 |
|
---|
2709 | static DECLCALLBACK(int) vdWriteHelperOptimizedPreReadAsync(PVDIOCTX pIoCtx)
|
---|
2710 | {
|
---|
2711 | int rc = VINF_SUCCESS;
|
---|
2712 |
|
---|
2713 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
2714 |
|
---|
2715 | pIoCtx->fFlags |= VDIOCTX_FLAGS_ZERO_FREE_BLOCKS;
|
---|
2716 |
|
---|
2717 | if ( pIoCtx->Req.Io.cbTransferLeft
|
---|
2718 | && !pIoCtx->cDataTransfersPending)
|
---|
2719 | rc = vdReadHelperAsync(pIoCtx);
|
---|
2720 |
|
---|
2721 | if ( ( RT_SUCCESS(rc)
|
---|
2722 | || (rc == VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
2723 | && ( pIoCtx->Req.Io.cbTransferLeft
|
---|
2724 | || pIoCtx->cMetaTransfersPending))
|
---|
2725 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
2726 | else
|
---|
2727 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperOptimizedCmpAndWriteAsync;
|
---|
2728 |
|
---|
2729 | return rc;
|
---|
2730 | }
|
---|
2731 |
|
---|
2732 | /**
|
---|
2733 | * internal: write a complete block (only used for diff images), taking the
|
---|
2734 | * remaining data from parent images. This implementation optimizes out writes
|
---|
2735 | * that do not change the data relative to the state as of the parent images.
|
---|
2736 | * All backends which support differential/growing images support this - async version.
|
---|
2737 | */
|
---|
2738 | static DECLCALLBACK(int) vdWriteHelperOptimizedAsync(PVDIOCTX pIoCtx)
|
---|
2739 | {
|
---|
2740 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
2741 | uint64_t uOffset = pIoCtx->Type.Child.uOffsetSaved;
|
---|
2742 | size_t cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
|
---|
2743 | size_t cbPreRead = pIoCtx->Type.Child.cbPreRead;
|
---|
2744 | size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
|
---|
2745 | size_t cbWrite = pIoCtx->Type.Child.cbWriteParent;
|
---|
2746 | size_t cbFill = 0;
|
---|
2747 | size_t cbWriteCopy = 0;
|
---|
2748 | size_t cbReadImage = 0;
|
---|
2749 |
|
---|
2750 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
2751 |
|
---|
2752 | AssertPtr(pIoCtx->pIoCtxParent);
|
---|
2753 | Assert(!pIoCtx->pIoCtxParent->pIoCtxParent);
|
---|
2754 |
|
---|
2755 | if (cbPostRead)
|
---|
2756 | {
|
---|
2757 | /* Figure out how much we cannot read from the image, because
|
---|
2758 | * the last block to write might exceed the nominal size of the
|
---|
2759 | * image for technical reasons. */
|
---|
2760 | if (uOffset + cbThisWrite + cbPostRead > pDisk->cbSize)
|
---|
2761 | cbFill = uOffset + cbThisWrite + cbPostRead - pDisk->cbSize;
|
---|
2762 |
|
---|
2763 | /* If we have data to be written, use that instead of reading
|
---|
2764 | * data from the image. */
|
---|
2765 | if (cbWrite > cbThisWrite)
|
---|
2766 | cbWriteCopy = RT_MIN(cbWrite - cbThisWrite, cbPostRead);
|
---|
2767 |
|
---|
2768 | /* The rest must be read from the image. */
|
---|
2769 | cbReadImage = cbPostRead - cbWriteCopy - cbFill;
|
---|
2770 | }
|
---|
2771 |
|
---|
2772 | pIoCtx->Type.Child.Write.Optimized.cbFill = cbFill;
|
---|
2773 | pIoCtx->Type.Child.Write.Optimized.cbWriteCopy = cbWriteCopy;
|
---|
2774 | pIoCtx->Type.Child.Write.Optimized.cbReadImage = cbReadImage;
|
---|
2775 |
|
---|
2776 | /* Read the entire data of the block so that we can compare whether it will
|
---|
2777 | * be modified by the write or not. */
|
---|
2778 | size_t cbTmp = cbPreRead + cbThisWrite + cbPostRead - cbFill; Assert(cbTmp == (uint32_t)cbTmp);
|
---|
2779 | pIoCtx->Req.Io.cbTransferLeft = (uint32_t)cbTmp;
|
---|
2780 | pIoCtx->Req.Io.cbTransfer = pIoCtx->Req.Io.cbTransferLeft;
|
---|
2781 | pIoCtx->Req.Io.uOffset -= cbPreRead;
|
---|
2782 |
|
---|
2783 | /* Next step */
|
---|
2784 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperOptimizedPreReadAsync;
|
---|
2785 | return VINF_SUCCESS;
|
---|
2786 | }
|
---|
2787 |
|
---|
2788 | static DECLCALLBACK(int) vdWriteHelperStandardReadImageAsync(PVDIOCTX pIoCtx)
|
---|
2789 | {
|
---|
2790 | int rc = VINF_SUCCESS;
|
---|
2791 |
|
---|
2792 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
2793 |
|
---|
2794 | pIoCtx->fFlags |= VDIOCTX_FLAGS_ZERO_FREE_BLOCKS;
|
---|
2795 |
|
---|
2796 | if ( pIoCtx->Req.Io.cbTransferLeft
|
---|
2797 | && !pIoCtx->cDataTransfersPending)
|
---|
2798 | rc = vdReadHelperAsync(pIoCtx);
|
---|
2799 |
|
---|
2800 | if ( RT_SUCCESS(rc)
|
---|
2801 | && ( pIoCtx->Req.Io.cbTransferLeft
|
---|
2802 | || pIoCtx->cMetaTransfersPending))
|
---|
2803 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
2804 | else
|
---|
2805 | {
|
---|
2806 | size_t cbFill = pIoCtx->Type.Child.Write.Optimized.cbFill;
|
---|
2807 |
|
---|
2808 | /* Zero out the remainder of this block. Will never be visible, as this
|
---|
2809 | * is beyond the limit of the image. */
|
---|
2810 | if (cbFill)
|
---|
2811 | vdIoCtxSet(pIoCtx, '\0', cbFill);
|
---|
2812 |
|
---|
2813 | /* Write the full block to the virtual disk. */
|
---|
2814 | RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
|
---|
2815 |
|
---|
2816 | vdIoCtxChildReset(pIoCtx);
|
---|
2817 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperCommitAsync;
|
---|
2818 | }
|
---|
2819 |
|
---|
2820 | return rc;
|
---|
2821 | }
|
---|
2822 |
|
---|
2823 | static DECLCALLBACK(int) vdWriteHelperStandardAssemble(PVDIOCTX pIoCtx)
|
---|
2824 | {
|
---|
2825 | int rc = VINF_SUCCESS;
|
---|
2826 | size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
|
---|
2827 | size_t cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
|
---|
2828 | PVDIOCTX pIoCtxParent = pIoCtx->pIoCtxParent;
|
---|
2829 |
|
---|
2830 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
2831 |
|
---|
2832 | vdIoCtxCopy(pIoCtx, pIoCtxParent, cbThisWrite);
|
---|
2833 | if (cbPostRead)
|
---|
2834 | {
|
---|
2835 | size_t cbFill = pIoCtx->Type.Child.Write.Optimized.cbFill;
|
---|
2836 | size_t cbWriteCopy = pIoCtx->Type.Child.Write.Optimized.cbWriteCopy;
|
---|
2837 | size_t cbReadImage = pIoCtx->Type.Child.Write.Optimized.cbReadImage;
|
---|
2838 |
|
---|
2839 | /* Now assemble the remaining data. */
|
---|
2840 | if (cbWriteCopy)
|
---|
2841 | {
|
---|
2842 | /*
|
---|
2843 | * The S/G buffer of the parent needs to be cloned because
|
---|
2844 | * it is not allowed to modify the state.
|
---|
2845 | */
|
---|
2846 | RTSGBUF SgBufParentTmp;
|
---|
2847 |
|
---|
2848 | RTSgBufClone(&SgBufParentTmp, &pIoCtxParent->Req.Io.SgBuf);
|
---|
2849 | RTSgBufCopy(&pIoCtx->Req.Io.SgBuf, &SgBufParentTmp, cbWriteCopy);
|
---|
2850 | }
|
---|
2851 |
|
---|
2852 | if (cbReadImage)
|
---|
2853 | {
|
---|
2854 | /* Read remaining data. */
|
---|
2855 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperStandardReadImageAsync;
|
---|
2856 |
|
---|
2857 | /* Read the data that goes before the write to fill the block. */
|
---|
2858 | pIoCtx->Req.Io.cbTransferLeft = (uint32_t)cbReadImage; Assert(cbReadImage == (uint32_t)cbReadImage);
|
---|
2859 | pIoCtx->Req.Io.cbTransfer = pIoCtx->Req.Io.cbTransferLeft;
|
---|
2860 | pIoCtx->Req.Io.uOffset += cbWriteCopy;
|
---|
2861 | }
|
---|
2862 | else
|
---|
2863 | {
|
---|
2864 | /* Zero out the remainder of this block. Will never be visible, as this
|
---|
2865 | * is beyond the limit of the image. */
|
---|
2866 | if (cbFill)
|
---|
2867 | vdIoCtxSet(pIoCtx, '\0', cbFill);
|
---|
2868 |
|
---|
2869 | /* Write the full block to the virtual disk. */
|
---|
2870 | RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
|
---|
2871 | vdIoCtxChildReset(pIoCtx);
|
---|
2872 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperCommitAsync;
|
---|
2873 | }
|
---|
2874 | }
|
---|
2875 | else
|
---|
2876 | {
|
---|
2877 | /* Write the full block to the virtual disk. */
|
---|
2878 | RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
|
---|
2879 | vdIoCtxChildReset(pIoCtx);
|
---|
2880 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperCommitAsync;
|
---|
2881 | }
|
---|
2882 |
|
---|
2883 | return rc;
|
---|
2884 | }
|
---|
2885 |
|
---|
2886 | static DECLCALLBACK(int) vdWriteHelperStandardPreReadAsync(PVDIOCTX pIoCtx)
|
---|
2887 | {
|
---|
2888 | int rc = VINF_SUCCESS;
|
---|
2889 |
|
---|
2890 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
2891 |
|
---|
2892 | pIoCtx->fFlags |= VDIOCTX_FLAGS_ZERO_FREE_BLOCKS;
|
---|
2893 |
|
---|
2894 | if ( pIoCtx->Req.Io.cbTransferLeft
|
---|
2895 | && !pIoCtx->cDataTransfersPending)
|
---|
2896 | rc = vdReadHelperAsync(pIoCtx);
|
---|
2897 |
|
---|
2898 | if ( RT_SUCCESS(rc)
|
---|
2899 | && ( pIoCtx->Req.Io.cbTransferLeft
|
---|
2900 | || pIoCtx->cMetaTransfersPending))
|
---|
2901 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
2902 | else
|
---|
2903 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperStandardAssemble;
|
---|
2904 |
|
---|
2905 | return rc;
|
---|
2906 | }
|
---|
2907 |
|
---|
2908 | static DECLCALLBACK(int) vdWriteHelperStandardAsync(PVDIOCTX pIoCtx)
|
---|
2909 | {
|
---|
2910 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
2911 | uint64_t uOffset = pIoCtx->Type.Child.uOffsetSaved;
|
---|
2912 | size_t cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
|
---|
2913 | size_t cbPreRead = pIoCtx->Type.Child.cbPreRead;
|
---|
2914 | size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
|
---|
2915 | size_t cbWrite = pIoCtx->Type.Child.cbWriteParent;
|
---|
2916 | size_t cbFill = 0;
|
---|
2917 | size_t cbWriteCopy = 0;
|
---|
2918 | size_t cbReadImage = 0;
|
---|
2919 |
|
---|
2920 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
2921 |
|
---|
2922 | AssertPtr(pIoCtx->pIoCtxParent);
|
---|
2923 | Assert(!pIoCtx->pIoCtxParent->pIoCtxParent);
|
---|
2924 |
|
---|
2925 | /* Calculate the amount of data to read that goes after the write to fill the block. */
|
---|
2926 | if (cbPostRead)
|
---|
2927 | {
|
---|
2928 | /* If we have data to be written, use that instead of reading
|
---|
2929 | * data from the image. */
|
---|
2930 | if (cbWrite > cbThisWrite)
|
---|
2931 | cbWriteCopy = RT_MIN(cbWrite - cbThisWrite, cbPostRead);
|
---|
2932 | else
|
---|
2933 | cbWriteCopy = 0;
|
---|
2934 |
|
---|
2935 | /* Figure out how much we cannot read from the image, because
|
---|
2936 | * the last block to write might exceed the nominal size of the
|
---|
2937 | * image for technical reasons. */
|
---|
2938 | if (uOffset + cbThisWrite + cbPostRead > pDisk->cbSize)
|
---|
2939 | cbFill = uOffset + cbThisWrite + cbPostRead - pDisk->cbSize;
|
---|
2940 |
|
---|
2941 | /* The rest must be read from the image. */
|
---|
2942 | cbReadImage = cbPostRead - cbWriteCopy - cbFill;
|
---|
2943 | }
|
---|
2944 |
|
---|
2945 | pIoCtx->Type.Child.Write.Optimized.cbFill = cbFill;
|
---|
2946 | pIoCtx->Type.Child.Write.Optimized.cbWriteCopy = cbWriteCopy;
|
---|
2947 | pIoCtx->Type.Child.Write.Optimized.cbReadImage = cbReadImage;
|
---|
2948 |
|
---|
2949 | /* Next step */
|
---|
2950 | if (cbPreRead)
|
---|
2951 | {
|
---|
2952 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperStandardPreReadAsync;
|
---|
2953 |
|
---|
2954 | /* Read the data that goes before the write to fill the block. */
|
---|
2955 | pIoCtx->Req.Io.cbTransferLeft = (uint32_t)cbPreRead; Assert(cbPreRead == (uint32_t)cbPreRead);
|
---|
2956 | pIoCtx->Req.Io.cbTransfer = pIoCtx->Req.Io.cbTransferLeft;
|
---|
2957 | pIoCtx->Req.Io.uOffset -= cbPreRead;
|
---|
2958 | }
|
---|
2959 | else
|
---|
2960 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperStandardAssemble;
|
---|
2961 |
|
---|
2962 | return VINF_SUCCESS;
|
---|
2963 | }
|
---|
2964 |
|
---|
2965 | /**
|
---|
2966 | * internal: write buffer to the image, taking care of block boundaries and
|
---|
2967 | * write optimizations - async version.
|
---|
2968 | */
|
---|
2969 | static DECLCALLBACK(int) vdWriteHelperAsync(PVDIOCTX pIoCtx)
|
---|
2970 | {
|
---|
2971 | int rc;
|
---|
2972 | size_t cbWrite = pIoCtx->Req.Io.cbTransfer;
|
---|
2973 | uint64_t uOffset = pIoCtx->Req.Io.uOffset;
|
---|
2974 | PVDIMAGE pImage = pIoCtx->Req.Io.pImageCur;
|
---|
2975 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
2976 | unsigned fWrite;
|
---|
2977 | size_t cbThisWrite;
|
---|
2978 | size_t cbPreRead, cbPostRead;
|
---|
2979 |
|
---|
2980 | /* Apply write filter chain here if it was not done already. */
|
---|
2981 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_WRITE_FILTER_APPLIED))
|
---|
2982 | {
|
---|
2983 | rc = vdFilterChainApplyWrite(pDisk, uOffset, cbWrite, pIoCtx);
|
---|
2984 | if (RT_FAILURE(rc))
|
---|
2985 | return rc;
|
---|
2986 | pIoCtx->fFlags |= VDIOCTX_FLAGS_WRITE_FILTER_APPLIED;
|
---|
2987 | }
|
---|
2988 |
|
---|
2989 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_DONT_SET_MODIFIED_FLAG))
|
---|
2990 | {
|
---|
2991 | rc = vdSetModifiedFlagAsync(pDisk, pIoCtx);
|
---|
2992 | if (RT_FAILURE(rc)) /* Includes I/O in progress. */
|
---|
2993 | return rc;
|
---|
2994 | }
|
---|
2995 |
|
---|
2996 | rc = vdDiscardSetRangeAllocated(pDisk, uOffset, cbWrite);
|
---|
2997 | if (RT_FAILURE(rc))
|
---|
2998 | return rc;
|
---|
2999 |
|
---|
3000 | /* Loop until all written. */
|
---|
3001 | do
|
---|
3002 | {
|
---|
3003 | /* Try to write the possibly partial block to the last opened image.
|
---|
3004 | * This works when the block is already allocated in this image or
|
---|
3005 | * if it is a full-block write (and allocation isn't suppressed below).
|
---|
3006 | * For image formats which don't support zero blocks, it's beneficial
|
---|
3007 | * to avoid unnecessarily allocating unchanged blocks. This prevents
|
---|
3008 | * unwanted expanding of images. VMDK is an example. */
|
---|
3009 | cbThisWrite = cbWrite;
|
---|
3010 |
|
---|
3011 | /*
|
---|
3012 | * Check whether there is a full block write in progress which was not allocated.
|
---|
3013 | * Defer I/O if the range interferes.
|
---|
3014 | */
|
---|
3015 | if ( pDisk->pIoCtxLockOwner != NIL_VDIOCTX
|
---|
3016 | && uOffset >= pDisk->uOffsetStartLocked
|
---|
3017 | && uOffset < pDisk->uOffsetEndLocked)
|
---|
3018 | {
|
---|
3019 | Log(("Interferring write while allocating a new block => deferring write\n"));
|
---|
3020 | vdIoCtxDefer(pDisk, pIoCtx);
|
---|
3021 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
3022 | break;
|
---|
3023 | }
|
---|
3024 |
|
---|
3025 | fWrite = (pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME)
|
---|
3026 | ? 0 : VD_WRITE_NO_ALLOC;
|
---|
3027 | rc = pImage->Backend->pfnWrite(pImage->pBackendData, uOffset, cbThisWrite,
|
---|
3028 | pIoCtx, &cbThisWrite, &cbPreRead, &cbPostRead,
|
---|
3029 | fWrite);
|
---|
3030 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
3031 | {
|
---|
3032 | /* Lock the disk .*/
|
---|
3033 | rc = vdIoCtxLockDisk(pDisk, pIoCtx);
|
---|
3034 | if (RT_SUCCESS(rc))
|
---|
3035 | {
|
---|
3036 | /*
|
---|
3037 | * Allocate segment and buffer in one go.
|
---|
3038 | * A bit hackish but avoids the need to allocate memory twice.
|
---|
3039 | */
|
---|
3040 | PRTSGBUF pTmp = (PRTSGBUF)RTMemAlloc(cbPreRead + cbThisWrite + cbPostRead + sizeof(RTSGSEG) + sizeof(RTSGBUF));
|
---|
3041 | AssertBreakStmt(pTmp, rc = VERR_NO_MEMORY);
|
---|
3042 | PRTSGSEG pSeg = (PRTSGSEG)(pTmp + 1);
|
---|
3043 |
|
---|
3044 | pSeg->pvSeg = pSeg + 1;
|
---|
3045 | pSeg->cbSeg = cbPreRead + cbThisWrite + cbPostRead;
|
---|
3046 | RTSgBufInit(pTmp, pSeg, 1);
|
---|
3047 |
|
---|
3048 | PVDIOCTX pIoCtxWrite = vdIoCtxChildAlloc(pDisk, VDIOCTXTXDIR_WRITE,
|
---|
3049 | uOffset, pSeg->cbSeg, pImage,
|
---|
3050 | pTmp,
|
---|
3051 | pIoCtx, cbThisWrite,
|
---|
3052 | cbWrite,
|
---|
3053 | pTmp,
|
---|
3054 | (pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME)
|
---|
3055 | ? vdWriteHelperStandardAsync
|
---|
3056 | : vdWriteHelperOptimizedAsync);
|
---|
3057 | if (!VALID_PTR(pIoCtxWrite))
|
---|
3058 | {
|
---|
3059 | RTMemTmpFree(pTmp);
|
---|
3060 | rc = VERR_NO_MEMORY;
|
---|
3061 | break;
|
---|
3062 | }
|
---|
3063 |
|
---|
3064 | LogFlowFunc(("Disk is growing because of pIoCtx=%#p pIoCtxWrite=%#p\n",
|
---|
3065 | pIoCtx, pIoCtxWrite));
|
---|
3066 |
|
---|
3067 | /* Save the current range for the growing operation to check for intersecting requests later. */
|
---|
3068 | pDisk->uOffsetStartLocked = uOffset - cbPreRead;
|
---|
3069 | pDisk->uOffsetEndLocked = uOffset + cbThisWrite + cbPostRead;
|
---|
3070 |
|
---|
3071 | pIoCtxWrite->Type.Child.cbPreRead = cbPreRead;
|
---|
3072 | pIoCtxWrite->Type.Child.cbPostRead = cbPostRead;
|
---|
3073 | pIoCtxWrite->Req.Io.pImageParentOverride = pIoCtx->Req.Io.pImageParentOverride;
|
---|
3074 |
|
---|
3075 | /* Process the write request */
|
---|
3076 | rc = vdIoCtxProcessLocked(pIoCtxWrite);
|
---|
3077 |
|
---|
3078 | if (RT_FAILURE(rc) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
3079 | {
|
---|
3080 | vdIoCtxUnlockDisk(pDisk, pIoCtx, false /* fProcessDeferredReqs*/ );
|
---|
3081 | vdIoCtxFree(pDisk, pIoCtxWrite);
|
---|
3082 | break;
|
---|
3083 | }
|
---|
3084 | else if ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
3085 | && ASMAtomicCmpXchgBool(&pIoCtxWrite->fComplete, true, false))
|
---|
3086 | {
|
---|
3087 | LogFlow(("Child write request completed\n"));
|
---|
3088 | Assert(pIoCtx->Req.Io.cbTransferLeft >= cbThisWrite);
|
---|
3089 | Assert(cbThisWrite == (uint32_t)cbThisWrite);
|
---|
3090 | rc = pIoCtxWrite->rcReq;
|
---|
3091 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbThisWrite);
|
---|
3092 | vdIoCtxUnlockDisk(pDisk, pIoCtx, false /* fProcessDeferredReqs*/ );
|
---|
3093 | vdIoCtxFree(pDisk, pIoCtxWrite);
|
---|
3094 | }
|
---|
3095 | else
|
---|
3096 | {
|
---|
3097 | LogFlow(("Child write pending\n"));
|
---|
3098 | ASMAtomicIncU32(&pIoCtx->cDataTransfersPending);
|
---|
3099 | pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
|
---|
3100 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
3101 | cbWrite -= cbThisWrite;
|
---|
3102 | uOffset += cbThisWrite;
|
---|
3103 | break;
|
---|
3104 | }
|
---|
3105 | }
|
---|
3106 | else
|
---|
3107 | {
|
---|
3108 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
3109 | break;
|
---|
3110 | }
|
---|
3111 | }
|
---|
3112 |
|
---|
3113 | if (rc == VERR_VD_IOCTX_HALT)
|
---|
3114 | {
|
---|
3115 | cbWrite -= cbThisWrite;
|
---|
3116 | uOffset += cbThisWrite;
|
---|
3117 | pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
|
---|
3118 | break;
|
---|
3119 | }
|
---|
3120 | else if (rc == VERR_VD_NOT_ENOUGH_METADATA)
|
---|
3121 | break;
|
---|
3122 |
|
---|
3123 | cbWrite -= cbThisWrite;
|
---|
3124 | uOffset += cbThisWrite;
|
---|
3125 | } while (cbWrite != 0 && (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS));
|
---|
3126 |
|
---|
3127 | if ( rc == VERR_VD_ASYNC_IO_IN_PROGRESS
|
---|
3128 | || rc == VERR_VD_NOT_ENOUGH_METADATA
|
---|
3129 | || rc == VERR_VD_IOCTX_HALT)
|
---|
3130 | {
|
---|
3131 | /*
|
---|
3132 | * Tell the caller that we don't need to go back here because all
|
---|
3133 | * writes are initiated.
|
---|
3134 | */
|
---|
3135 | if ( !cbWrite
|
---|
3136 | && rc != VERR_VD_IOCTX_HALT)
|
---|
3137 | rc = VINF_SUCCESS;
|
---|
3138 |
|
---|
3139 | pIoCtx->Req.Io.uOffset = uOffset;
|
---|
3140 | pIoCtx->Req.Io.cbTransfer = cbWrite;
|
---|
3141 | }
|
---|
3142 |
|
---|
3143 | return rc;
|
---|
3144 | }
|
---|
3145 |
|
---|
3146 | /**
|
---|
3147 | * Flush helper async version.
|
---|
3148 | */
|
---|
3149 | static DECLCALLBACK(int) vdFlushHelperAsync(PVDIOCTX pIoCtx)
|
---|
3150 | {
|
---|
3151 | int rc = VINF_SUCCESS;
|
---|
3152 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
3153 | PVDIMAGE pImage = pIoCtx->Req.Io.pImageCur;
|
---|
3154 |
|
---|
3155 | rc = vdIoCtxLockDisk(pDisk, pIoCtx);
|
---|
3156 | if (RT_SUCCESS(rc))
|
---|
3157 | {
|
---|
3158 | /* Mark the whole disk as locked. */
|
---|
3159 | pDisk->uOffsetStartLocked = 0;
|
---|
3160 | pDisk->uOffsetEndLocked = UINT64_C(0xffffffffffffffff);
|
---|
3161 |
|
---|
3162 | vdResetModifiedFlag(pDisk);
|
---|
3163 | rc = pImage->Backend->pfnFlush(pImage->pBackendData, pIoCtx);
|
---|
3164 | if ( ( RT_SUCCESS(rc)
|
---|
3165 | || rc == VERR_VD_ASYNC_IO_IN_PROGRESS
|
---|
3166 | || rc == VERR_VD_IOCTX_HALT)
|
---|
3167 | && pDisk->pCache)
|
---|
3168 | {
|
---|
3169 | rc = pDisk->pCache->Backend->pfnFlush(pDisk->pCache->pBackendData, pIoCtx);
|
---|
3170 | if ( RT_SUCCESS(rc)
|
---|
3171 | || ( rc != VERR_VD_ASYNC_IO_IN_PROGRESS
|
---|
3172 | && rc != VERR_VD_IOCTX_HALT))
|
---|
3173 | vdIoCtxUnlockDisk(pDisk, pIoCtx, true /* fProcessBlockedReqs */);
|
---|
3174 | else if (rc != VERR_VD_IOCTX_HALT)
|
---|
3175 | rc = VINF_SUCCESS;
|
---|
3176 | }
|
---|
3177 | else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
3178 | rc = VINF_SUCCESS;
|
---|
3179 | else if (rc != VERR_VD_IOCTX_HALT)/* Some other error. */
|
---|
3180 | vdIoCtxUnlockDisk(pDisk, pIoCtx, true /* fProcessBlockedReqs */);
|
---|
3181 | }
|
---|
3182 |
|
---|
3183 | return rc;
|
---|
3184 | }
|
---|
3185 |
|
---|
3186 | /**
|
---|
3187 | * Async discard helper - discards a whole block which is recorded in the block
|
---|
3188 | * tree.
|
---|
3189 | *
|
---|
3190 | * @returns VBox status code.
|
---|
3191 | * @param pIoCtx The I/O context to operate on.
|
---|
3192 | */
|
---|
3193 | static DECLCALLBACK(int) vdDiscardWholeBlockAsync(PVDIOCTX pIoCtx)
|
---|
3194 | {
|
---|
3195 | int rc = VINF_SUCCESS;
|
---|
3196 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
3197 | PVDDISCARDSTATE pDiscard = pDisk->pDiscard;
|
---|
3198 | PVDDISCARDBLOCK pBlock = pIoCtx->Req.Discard.pBlock;
|
---|
3199 | size_t cbPreAllocated, cbPostAllocated, cbActuallyDiscarded;
|
---|
3200 |
|
---|
3201 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
3202 |
|
---|
3203 | AssertPtr(pBlock);
|
---|
3204 |
|
---|
3205 | rc = pDisk->pLast->Backend->pfnDiscard(pDisk->pLast->pBackendData, pIoCtx,
|
---|
3206 | pBlock->Core.Key, pBlock->cbDiscard,
|
---|
3207 | &cbPreAllocated, &cbPostAllocated,
|
---|
3208 | &cbActuallyDiscarded, NULL, 0);
|
---|
3209 | Assert(rc != VERR_VD_DISCARD_ALIGNMENT_NOT_MET);
|
---|
3210 | Assert(!cbPreAllocated);
|
---|
3211 | Assert(!cbPostAllocated);
|
---|
3212 | Assert(cbActuallyDiscarded == pBlock->cbDiscard || RT_FAILURE(rc));
|
---|
3213 |
|
---|
3214 | /* Remove the block on success. */
|
---|
3215 | if ( RT_SUCCESS(rc)
|
---|
3216 | || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
3217 | {
|
---|
3218 | PVDDISCARDBLOCK pBlockRemove = (PVDDISCARDBLOCK)RTAvlrU64RangeRemove(pDiscard->pTreeBlocks, pBlock->Core.Key);
|
---|
3219 | Assert(pBlockRemove == pBlock); RT_NOREF1(pBlockRemove);
|
---|
3220 |
|
---|
3221 | pDiscard->cbDiscarding -= pBlock->cbDiscard;
|
---|
3222 | RTListNodeRemove(&pBlock->NodeLru);
|
---|
3223 | RTMemFree(pBlock->pbmAllocated);
|
---|
3224 | RTMemFree(pBlock);
|
---|
3225 | pIoCtx->Req.Discard.pBlock = NULL;/* Safety precaution. */
|
---|
3226 | pIoCtx->pfnIoCtxTransferNext = vdDiscardHelperAsync; /* Next part. */
|
---|
3227 | rc = VINF_SUCCESS;
|
---|
3228 | }
|
---|
3229 |
|
---|
3230 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
3231 | return rc;
|
---|
3232 | }
|
---|
3233 |
|
---|
3234 | /**
|
---|
3235 | * Removes the least recently used blocks from the waiting list until
|
---|
3236 | * the new value is reached - version for async I/O.
|
---|
3237 | *
|
---|
3238 | * @returns VBox status code.
|
---|
3239 | * @param pDisk VD disk container.
|
---|
3240 | * @param pDiscard The discard state.
|
---|
3241 | * @param cbDiscardingNew How many bytes should be waiting on success.
|
---|
3242 | * The number of bytes waiting can be less.
|
---|
3243 | */
|
---|
3244 | static int vdDiscardRemoveBlocksAsync(PVBOXHDD pDisk, PVDIOCTX pIoCtx, size_t cbDiscardingNew)
|
---|
3245 | {
|
---|
3246 | int rc = VINF_SUCCESS;
|
---|
3247 | PVDDISCARDSTATE pDiscard = pDisk->pDiscard;
|
---|
3248 |
|
---|
3249 | LogFlowFunc(("pDisk=%#p pDiscard=%#p cbDiscardingNew=%zu\n",
|
---|
3250 | pDisk, pDiscard, cbDiscardingNew));
|
---|
3251 |
|
---|
3252 | while (pDiscard->cbDiscarding > cbDiscardingNew)
|
---|
3253 | {
|
---|
3254 | PVDDISCARDBLOCK pBlock = RTListGetLast(&pDiscard->ListLru, VDDISCARDBLOCK, NodeLru);
|
---|
3255 |
|
---|
3256 | Assert(!RTListIsEmpty(&pDiscard->ListLru));
|
---|
3257 |
|
---|
3258 | /* Go over the allocation bitmap and mark all discarded sectors as unused. */
|
---|
3259 | uint64_t offStart = pBlock->Core.Key;
|
---|
3260 | uint32_t idxStart = 0;
|
---|
3261 | size_t cbLeft = pBlock->cbDiscard;
|
---|
3262 | bool fAllocated = ASMBitTest(pBlock->pbmAllocated, idxStart);
|
---|
3263 | uint32_t cSectors = (uint32_t)(pBlock->cbDiscard / 512);
|
---|
3264 |
|
---|
3265 | while (cbLeft > 0)
|
---|
3266 | {
|
---|
3267 | int32_t idxEnd;
|
---|
3268 | size_t cbThis = cbLeft;
|
---|
3269 |
|
---|
3270 | if (fAllocated)
|
---|
3271 | {
|
---|
3272 | /* Check for the first unallocated bit. */
|
---|
3273 | idxEnd = ASMBitNextClear(pBlock->pbmAllocated, cSectors, idxStart);
|
---|
3274 | if (idxEnd != -1)
|
---|
3275 | {
|
---|
3276 | cbThis = (idxEnd - idxStart) * 512;
|
---|
3277 | fAllocated = false;
|
---|
3278 | }
|
---|
3279 | }
|
---|
3280 | else
|
---|
3281 | {
|
---|
3282 | /* Mark as unused and check for the first set bit. */
|
---|
3283 | idxEnd = ASMBitNextSet(pBlock->pbmAllocated, cSectors, idxStart);
|
---|
3284 | if (idxEnd != -1)
|
---|
3285 | cbThis = (idxEnd - idxStart) * 512;
|
---|
3286 |
|
---|
3287 | rc = pDisk->pLast->Backend->pfnDiscard(pDisk->pLast->pBackendData, pIoCtx,
|
---|
3288 | offStart, cbThis, NULL, NULL, &cbThis,
|
---|
3289 | NULL, VD_DISCARD_MARK_UNUSED);
|
---|
3290 | if ( RT_FAILURE(rc)
|
---|
3291 | && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
3292 | break;
|
---|
3293 |
|
---|
3294 | fAllocated = true;
|
---|
3295 | }
|
---|
3296 |
|
---|
3297 | idxStart = idxEnd;
|
---|
3298 | offStart += cbThis;
|
---|
3299 | cbLeft -= cbThis;
|
---|
3300 | }
|
---|
3301 |
|
---|
3302 | if ( RT_FAILURE(rc)
|
---|
3303 | && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
3304 | break;
|
---|
3305 |
|
---|
3306 | PVDDISCARDBLOCK pBlockRemove = (PVDDISCARDBLOCK)RTAvlrU64RangeRemove(pDiscard->pTreeBlocks, pBlock->Core.Key);
|
---|
3307 | Assert(pBlockRemove == pBlock); NOREF(pBlockRemove);
|
---|
3308 | RTListNodeRemove(&pBlock->NodeLru);
|
---|
3309 |
|
---|
3310 | pDiscard->cbDiscarding -= pBlock->cbDiscard;
|
---|
3311 | RTMemFree(pBlock->pbmAllocated);
|
---|
3312 | RTMemFree(pBlock);
|
---|
3313 | }
|
---|
3314 |
|
---|
3315 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
3316 | rc = VINF_SUCCESS;
|
---|
3317 |
|
---|
3318 | Assert(RT_FAILURE(rc) || pDiscard->cbDiscarding <= cbDiscardingNew);
|
---|
3319 |
|
---|
3320 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
3321 | return rc;
|
---|
3322 | }
|
---|
3323 |
|
---|
3324 | /**
|
---|
3325 | * Async discard helper - discards the current range if there is no matching
|
---|
3326 | * block in the tree.
|
---|
3327 | *
|
---|
3328 | * @returns VBox status code.
|
---|
3329 | * @param pIoCtx The I/O context to operate on.
|
---|
3330 | */
|
---|
3331 | static DECLCALLBACK(int) vdDiscardCurrentRangeAsync(PVDIOCTX pIoCtx)
|
---|
3332 | {
|
---|
3333 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
3334 | PVDDISCARDSTATE pDiscard = pDisk->pDiscard;
|
---|
3335 | uint64_t offStart = pIoCtx->Req.Discard.offCur;
|
---|
3336 | size_t cbThisDiscard = pIoCtx->Req.Discard.cbThisDiscard;
|
---|
3337 | void *pbmAllocated = NULL;
|
---|
3338 | size_t cbPreAllocated, cbPostAllocated;
|
---|
3339 | int rc = VINF_SUCCESS;
|
---|
3340 |
|
---|
3341 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
3342 |
|
---|
3343 | /* No block found, try to discard using the backend first. */
|
---|
3344 | rc = pDisk->pLast->Backend->pfnDiscard(pDisk->pLast->pBackendData, pIoCtx,
|
---|
3345 | offStart, cbThisDiscard, &cbPreAllocated,
|
---|
3346 | &cbPostAllocated, &cbThisDiscard,
|
---|
3347 | &pbmAllocated, 0);
|
---|
3348 | if (rc == VERR_VD_DISCARD_ALIGNMENT_NOT_MET)
|
---|
3349 | {
|
---|
3350 | /* Create new discard block. */
|
---|
3351 | PVDDISCARDBLOCK pBlock = (PVDDISCARDBLOCK)RTMemAllocZ(sizeof(VDDISCARDBLOCK));
|
---|
3352 | if (pBlock)
|
---|
3353 | {
|
---|
3354 | pBlock->Core.Key = offStart - cbPreAllocated;
|
---|
3355 | pBlock->Core.KeyLast = offStart + cbThisDiscard + cbPostAllocated - 1;
|
---|
3356 | pBlock->cbDiscard = cbPreAllocated + cbThisDiscard + cbPostAllocated;
|
---|
3357 | pBlock->pbmAllocated = pbmAllocated;
|
---|
3358 | bool fInserted = RTAvlrU64Insert(pDiscard->pTreeBlocks, &pBlock->Core);
|
---|
3359 | Assert(fInserted); NOREF(fInserted);
|
---|
3360 |
|
---|
3361 | RTListPrepend(&pDiscard->ListLru, &pBlock->NodeLru);
|
---|
3362 | pDiscard->cbDiscarding += pBlock->cbDiscard;
|
---|
3363 |
|
---|
3364 | Assert(pIoCtx->Req.Discard.cbDiscardLeft >= cbThisDiscard);
|
---|
3365 | pIoCtx->Req.Discard.cbDiscardLeft -= cbThisDiscard;
|
---|
3366 | pIoCtx->Req.Discard.offCur += cbThisDiscard;
|
---|
3367 | pIoCtx->Req.Discard.cbThisDiscard = cbThisDiscard;
|
---|
3368 |
|
---|
3369 | if (pDiscard->cbDiscarding > VD_DISCARD_REMOVE_THRESHOLD)
|
---|
3370 | rc = vdDiscardRemoveBlocksAsync(pDisk, pIoCtx, VD_DISCARD_REMOVE_THRESHOLD);
|
---|
3371 | else
|
---|
3372 | rc = VINF_SUCCESS;
|
---|
3373 |
|
---|
3374 | if (RT_SUCCESS(rc))
|
---|
3375 | pIoCtx->pfnIoCtxTransferNext = vdDiscardHelperAsync; /* Next part. */
|
---|
3376 | }
|
---|
3377 | else
|
---|
3378 | {
|
---|
3379 | RTMemFree(pbmAllocated);
|
---|
3380 | rc = VERR_NO_MEMORY;
|
---|
3381 | }
|
---|
3382 | }
|
---|
3383 | else if ( RT_SUCCESS(rc)
|
---|
3384 | || rc == VERR_VD_ASYNC_IO_IN_PROGRESS) /* Save state and andvance to next range. */
|
---|
3385 | {
|
---|
3386 | Assert(pIoCtx->Req.Discard.cbDiscardLeft >= cbThisDiscard);
|
---|
3387 | pIoCtx->Req.Discard.cbDiscardLeft -= cbThisDiscard;
|
---|
3388 | pIoCtx->Req.Discard.offCur += cbThisDiscard;
|
---|
3389 | pIoCtx->Req.Discard.cbThisDiscard = cbThisDiscard;
|
---|
3390 | pIoCtx->pfnIoCtxTransferNext = vdDiscardHelperAsync;
|
---|
3391 | rc = VINF_SUCCESS;
|
---|
3392 | }
|
---|
3393 |
|
---|
3394 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
3395 | return rc;
|
---|
3396 | }
|
---|
3397 |
|
---|
3398 | /**
|
---|
3399 | * Async discard helper - entry point.
|
---|
3400 | *
|
---|
3401 | * @returns VBox status code.
|
---|
3402 | * @param pIoCtx The I/O context to operate on.
|
---|
3403 | */
|
---|
3404 | static DECLCALLBACK(int) vdDiscardHelperAsync(PVDIOCTX pIoCtx)
|
---|
3405 | {
|
---|
3406 | int rc = VINF_SUCCESS;
|
---|
3407 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
3408 | PCRTRANGE paRanges = pIoCtx->Req.Discard.paRanges;
|
---|
3409 | unsigned cRanges = pIoCtx->Req.Discard.cRanges;
|
---|
3410 | PVDDISCARDSTATE pDiscard = pDisk->pDiscard;
|
---|
3411 |
|
---|
3412 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
3413 |
|
---|
3414 | /* Check if the I/O context processed all ranges. */
|
---|
3415 | if ( pIoCtx->Req.Discard.idxRange == cRanges
|
---|
3416 | && !pIoCtx->Req.Discard.cbDiscardLeft)
|
---|
3417 | {
|
---|
3418 | LogFlowFunc(("All ranges discarded, completing\n"));
|
---|
3419 | vdIoCtxUnlockDisk(pDisk, pIoCtx, true /* fProcessDeferredReqs*/);
|
---|
3420 | return VINF_SUCCESS;
|
---|
3421 | }
|
---|
3422 |
|
---|
3423 | if (pDisk->pIoCtxLockOwner != pIoCtx)
|
---|
3424 | rc = vdIoCtxLockDisk(pDisk, pIoCtx);
|
---|
3425 |
|
---|
3426 | if (RT_SUCCESS(rc))
|
---|
3427 | {
|
---|
3428 | uint64_t offStart = pIoCtx->Req.Discard.offCur;
|
---|
3429 | size_t cbDiscardLeft = pIoCtx->Req.Discard.cbDiscardLeft;
|
---|
3430 | size_t cbThisDiscard;
|
---|
3431 |
|
---|
3432 | pDisk->uOffsetStartLocked = offStart;
|
---|
3433 | pDisk->uOffsetEndLocked = offStart + cbDiscardLeft;
|
---|
3434 |
|
---|
3435 | if (RT_UNLIKELY(!pDiscard))
|
---|
3436 | {
|
---|
3437 | pDiscard = vdDiscardStateCreate();
|
---|
3438 | if (!pDiscard)
|
---|
3439 | return VERR_NO_MEMORY;
|
---|
3440 |
|
---|
3441 | pDisk->pDiscard = pDiscard;
|
---|
3442 | }
|
---|
3443 |
|
---|
3444 | if (!pIoCtx->Req.Discard.cbDiscardLeft)
|
---|
3445 | {
|
---|
3446 | offStart = paRanges[pIoCtx->Req.Discard.idxRange].offStart;
|
---|
3447 | cbDiscardLeft = paRanges[pIoCtx->Req.Discard.idxRange].cbRange;
|
---|
3448 | LogFlowFunc(("New range descriptor loaded (%u) offStart=%llu cbDiscard=%zu\n",
|
---|
3449 | pIoCtx->Req.Discard.idxRange, offStart, cbDiscardLeft));
|
---|
3450 | pIoCtx->Req.Discard.idxRange++;
|
---|
3451 | }
|
---|
3452 |
|
---|
3453 | /* Look for a matching block in the AVL tree first. */
|
---|
3454 | PVDDISCARDBLOCK pBlock = (PVDDISCARDBLOCK)RTAvlrU64GetBestFit(pDiscard->pTreeBlocks, offStart, false);
|
---|
3455 | if (!pBlock || pBlock->Core.KeyLast < offStart)
|
---|
3456 | {
|
---|
3457 | PVDDISCARDBLOCK pBlockAbove = (PVDDISCARDBLOCK)RTAvlrU64GetBestFit(pDiscard->pTreeBlocks, offStart, true);
|
---|
3458 |
|
---|
3459 | /* Clip range to remain in the current block. */
|
---|
3460 | if (pBlockAbove)
|
---|
3461 | cbThisDiscard = RT_MIN(cbDiscardLeft, pBlockAbove->Core.KeyLast - offStart + 1);
|
---|
3462 | else
|
---|
3463 | cbThisDiscard = cbDiscardLeft;
|
---|
3464 |
|
---|
3465 | Assert(!(cbThisDiscard % 512));
|
---|
3466 | pIoCtx->Req.Discard.pBlock = NULL;
|
---|
3467 | pIoCtx->pfnIoCtxTransferNext = vdDiscardCurrentRangeAsync;
|
---|
3468 | }
|
---|
3469 | else
|
---|
3470 | {
|
---|
3471 | /* Range lies partly in the block, update allocation bitmap. */
|
---|
3472 | int32_t idxStart, idxEnd;
|
---|
3473 |
|
---|
3474 | cbThisDiscard = RT_MIN(cbDiscardLeft, pBlock->Core.KeyLast - offStart + 1);
|
---|
3475 |
|
---|
3476 | AssertPtr(pBlock);
|
---|
3477 |
|
---|
3478 | Assert(!(cbThisDiscard % 512));
|
---|
3479 | Assert(!((offStart - pBlock->Core.Key) % 512));
|
---|
3480 |
|
---|
3481 | idxStart = (offStart - pBlock->Core.Key) / 512;
|
---|
3482 | idxEnd = idxStart + (int32_t)(cbThisDiscard / 512);
|
---|
3483 |
|
---|
3484 | ASMBitClearRange(pBlock->pbmAllocated, idxStart, idxEnd);
|
---|
3485 |
|
---|
3486 | cbDiscardLeft -= cbThisDiscard;
|
---|
3487 | offStart += cbThisDiscard;
|
---|
3488 |
|
---|
3489 | /* Call the backend to discard the block if it is completely unallocated now. */
|
---|
3490 | if (ASMBitFirstSet((volatile void *)pBlock->pbmAllocated, (uint32_t)(pBlock->cbDiscard / 512)) == -1)
|
---|
3491 | {
|
---|
3492 | pIoCtx->Req.Discard.pBlock = pBlock;
|
---|
3493 | pIoCtx->pfnIoCtxTransferNext = vdDiscardWholeBlockAsync;
|
---|
3494 | rc = VINF_SUCCESS;
|
---|
3495 | }
|
---|
3496 | else
|
---|
3497 | {
|
---|
3498 | RTListNodeRemove(&pBlock->NodeLru);
|
---|
3499 | RTListPrepend(&pDiscard->ListLru, &pBlock->NodeLru);
|
---|
3500 |
|
---|
3501 | /* Start with next range. */
|
---|
3502 | pIoCtx->pfnIoCtxTransferNext = vdDiscardHelperAsync;
|
---|
3503 | rc = VINF_SUCCESS;
|
---|
3504 | }
|
---|
3505 | }
|
---|
3506 |
|
---|
3507 | /* Save state in the context. */
|
---|
3508 | pIoCtx->Req.Discard.offCur = offStart;
|
---|
3509 | pIoCtx->Req.Discard.cbDiscardLeft = cbDiscardLeft;
|
---|
3510 | pIoCtx->Req.Discard.cbThisDiscard = cbThisDiscard;
|
---|
3511 | }
|
---|
3512 |
|
---|
3513 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
3514 | return rc;
|
---|
3515 | }
|
---|
3516 |
|
---|
3517 | #ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
3518 |
|
---|
3519 | /**
|
---|
3520 | * @interface_method_impl{VDBACKENDREGISTER,pfnRegisterImage}
|
---|
3521 | */
|
---|
3522 | static DECLCALLBACK(int) vdPluginRegisterImage(void *pvUser, PCVBOXHDDBACKEND pBackend)
|
---|
3523 | {
|
---|
3524 | int rc = VINF_SUCCESS;
|
---|
3525 |
|
---|
3526 | if (pBackend->cbSize == sizeof(VBOXHDDBACKEND))
|
---|
3527 | vdAddBackend((RTLDRMOD)pvUser, pBackend);
|
---|
3528 | else
|
---|
3529 | {
|
---|
3530 | LogFunc(("ignored plugin: pBackend->cbSize=%d rc=%Rrc\n", pBackend->cbSize, rc));
|
---|
3531 | rc = VERR_IGNORED;
|
---|
3532 | }
|
---|
3533 |
|
---|
3534 | return rc;
|
---|
3535 | }
|
---|
3536 |
|
---|
3537 | /**
|
---|
3538 | * @interface_method_impl{VDBACKENDREGISTER,pfnRegisterCache}
|
---|
3539 | */
|
---|
3540 | static DECLCALLBACK(int) vdPluginRegisterCache(void *pvUser, PCVDCACHEBACKEND pBackend)
|
---|
3541 | {
|
---|
3542 | int rc = VINF_SUCCESS;
|
---|
3543 |
|
---|
3544 | if (pBackend->cbSize == sizeof(VDCACHEBACKEND))
|
---|
3545 | vdAddCacheBackend((RTLDRMOD)pvUser, pBackend);
|
---|
3546 | else
|
---|
3547 | {
|
---|
3548 | LogFunc(("ignored plugin: pBackend->cbSize=%d rc=%Rrc\n", pBackend->cbSize, rc));
|
---|
3549 | rc = VERR_IGNORED;
|
---|
3550 | }
|
---|
3551 |
|
---|
3552 | return rc;
|
---|
3553 | }
|
---|
3554 |
|
---|
3555 | /**
|
---|
3556 | * @interface_method_impl{VDBACKENDREGISTER,pfnRegisterFilter}
|
---|
3557 | */
|
---|
3558 | static DECLCALLBACK(int) vdPluginRegisterFilter(void *pvUser, PCVDFILTERBACKEND pBackend)
|
---|
3559 | {
|
---|
3560 | int rc = VINF_SUCCESS;
|
---|
3561 |
|
---|
3562 | if (pBackend->cbSize == sizeof(VDFILTERBACKEND))
|
---|
3563 | vdAddFilterBackend((RTLDRMOD)pvUser, pBackend);
|
---|
3564 | else
|
---|
3565 | {
|
---|
3566 | LogFunc(("ignored plugin: pBackend->cbSize=%d rc=%Rrc\n", pBackend->cbSize, rc));
|
---|
3567 | rc = VERR_IGNORED;
|
---|
3568 | }
|
---|
3569 |
|
---|
3570 | return rc;
|
---|
3571 | }
|
---|
3572 |
|
---|
3573 | /**
|
---|
3574 | * Checks whether the given plugin filename was already loaded.
|
---|
3575 | *
|
---|
3576 | * @returns true if the plugin was already loaded, false otherwise.
|
---|
3577 | * @param pszFilename The filename to check.
|
---|
3578 | */
|
---|
3579 | static bool vdPluginFind(const char *pszFilename)
|
---|
3580 | {
|
---|
3581 | PVDPLUGIN pIt = NULL;
|
---|
3582 |
|
---|
3583 | RTListForEach(&g_ListPluginsLoaded, pIt, VDPLUGIN, NodePlugin)
|
---|
3584 | {
|
---|
3585 | if (!RTStrCmp(pIt->pszFilename, pszFilename))
|
---|
3586 | return true;
|
---|
3587 | }
|
---|
3588 |
|
---|
3589 | return false;
|
---|
3590 | }
|
---|
3591 |
|
---|
3592 | /**
|
---|
3593 | * Adds a plugin to the list of loaded plugins.
|
---|
3594 | *
|
---|
3595 | * @returns VBox status code.
|
---|
3596 | * @param hPlugin Plugin handle to add.
|
---|
3597 | * @param pszFilename The associated filename, used for finding duplicates.
|
---|
3598 | */
|
---|
3599 | static int vdAddPlugin(RTLDRMOD hPlugin, const char *pszFilename)
|
---|
3600 | {
|
---|
3601 | int rc = VINF_SUCCESS;
|
---|
3602 | PVDPLUGIN pPlugin = (PVDPLUGIN)RTMemAllocZ(sizeof(VDPLUGIN));
|
---|
3603 |
|
---|
3604 | if (pPlugin)
|
---|
3605 | {
|
---|
3606 | pPlugin->hPlugin = hPlugin;
|
---|
3607 | pPlugin->pszFilename = RTStrDup(pszFilename);
|
---|
3608 | if (pPlugin->pszFilename)
|
---|
3609 | RTListAppend(&g_ListPluginsLoaded, &pPlugin->NodePlugin);
|
---|
3610 | else
|
---|
3611 | {
|
---|
3612 | RTMemFree(pPlugin);
|
---|
3613 | rc = VERR_NO_MEMORY;
|
---|
3614 | }
|
---|
3615 | }
|
---|
3616 | else
|
---|
3617 | rc = VERR_NO_MEMORY;
|
---|
3618 |
|
---|
3619 | return rc;
|
---|
3620 | }
|
---|
3621 |
|
---|
3622 | static int vdRemovePlugin(const char *pszFilename)
|
---|
3623 | {
|
---|
3624 | /* Find plugin to be removed from the list. */
|
---|
3625 | PVDPLUGIN pIt = NULL;
|
---|
3626 | RTListForEach(&g_ListPluginsLoaded, pIt, VDPLUGIN, NodePlugin)
|
---|
3627 | {
|
---|
3628 | if (!RTStrCmp(pIt->pszFilename, pszFilename))
|
---|
3629 | break;
|
---|
3630 | }
|
---|
3631 | if (!pIt)
|
---|
3632 | return VINF_SUCCESS;
|
---|
3633 |
|
---|
3634 | /** @todo r=klaus: need to add a plugin entry point for unregistering the
|
---|
3635 | * backends. Only if this doesn't exist (or fails to work) we should fall
|
---|
3636 | * back to the following uncoordinated backend cleanup. */
|
---|
3637 | for (unsigned i = 0; i < g_cBackends; i++)
|
---|
3638 | {
|
---|
3639 | while (i < g_cBackends && g_ahBackendPlugins[i] == pIt->hPlugin)
|
---|
3640 | {
|
---|
3641 | memcpy(&g_apBackends[i], &g_apBackends[i + 1], (g_cBackends - i - 1) * sizeof(PCVBOXHDDBACKEND));
|
---|
3642 | memcpy(&g_ahBackendPlugins[i], &g_ahBackendPlugins[i + 1], (g_cBackends - i - 1) * sizeof(RTLDRMOD));
|
---|
3643 | /** @todo for now skip reallocating, doesn't save much */
|
---|
3644 | g_cBackends--;
|
---|
3645 | }
|
---|
3646 | }
|
---|
3647 | for (unsigned i = 0; i < g_cCacheBackends; i++)
|
---|
3648 | {
|
---|
3649 | while (i < g_cCacheBackends && g_ahCacheBackendPlugins[i] == pIt->hPlugin)
|
---|
3650 | {
|
---|
3651 | memcpy(&g_apCacheBackends[i], &g_apCacheBackends[i + 1], (g_cCacheBackends - i - 1) * sizeof(PCVBOXHDDBACKEND));
|
---|
3652 | memcpy(&g_ahCacheBackendPlugins[i], &g_ahCacheBackendPlugins[i + 1], (g_cCacheBackends - i - 1) * sizeof(RTLDRMOD));
|
---|
3653 | /** @todo for now skip reallocating, doesn't save much */
|
---|
3654 | g_cCacheBackends--;
|
---|
3655 | }
|
---|
3656 | }
|
---|
3657 | for (unsigned i = 0; i < g_cFilterBackends; i++)
|
---|
3658 | {
|
---|
3659 | while (i < g_cFilterBackends && g_pahFilterBackendPlugins[i] == pIt->hPlugin)
|
---|
3660 | {
|
---|
3661 | memcpy(&g_apFilterBackends[i], &g_apFilterBackends[i + 1], (g_cFilterBackends - i - 1) * sizeof(PCVBOXHDDBACKEND));
|
---|
3662 | memcpy(&g_pahFilterBackendPlugins[i], &g_pahFilterBackendPlugins[i + 1], (g_cFilterBackends - i - 1) * sizeof(RTLDRMOD));
|
---|
3663 | /** @todo for now skip reallocating, doesn't save much */
|
---|
3664 | g_cFilterBackends--;
|
---|
3665 | }
|
---|
3666 | }
|
---|
3667 |
|
---|
3668 | /* Remove the plugin node now, all traces of it are gone. */
|
---|
3669 | RTListNodeRemove(&pIt->NodePlugin);
|
---|
3670 | RTLdrClose(pIt->hPlugin);
|
---|
3671 | RTStrFree(pIt->pszFilename);
|
---|
3672 | RTMemFree(pIt);
|
---|
3673 |
|
---|
3674 | return VINF_SUCCESS;
|
---|
3675 | }
|
---|
3676 |
|
---|
3677 | #endif /* !VBOX_HDD_NO_DYNAMIC_BACKENDS */
|
---|
3678 |
|
---|
3679 | /**
|
---|
3680 | * Worker for VDPluginLoadFromFilename() and vdPluginLoadFromPath().
|
---|
3681 | *
|
---|
3682 | * @returns VBox status code.
|
---|
3683 | * @param pszFilename The plugin filename to load.
|
---|
3684 | */
|
---|
3685 | static int vdPluginLoadFromFilename(const char *pszFilename)
|
---|
3686 | {
|
---|
3687 | #ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
3688 | /* Plugin loaded? Nothing to do. */
|
---|
3689 | if (vdPluginFind(pszFilename))
|
---|
3690 | return VINF_SUCCESS;
|
---|
3691 |
|
---|
3692 | RTLDRMOD hPlugin = NIL_RTLDRMOD;
|
---|
3693 | int rc = SUPR3HardenedLdrLoadPlugIn(pszFilename, &hPlugin, NULL);
|
---|
3694 | if (RT_SUCCESS(rc))
|
---|
3695 | {
|
---|
3696 | VDBACKENDREGISTER BackendRegister;
|
---|
3697 | PFNVDPLUGINLOAD pfnVDPluginLoad = NULL;
|
---|
3698 |
|
---|
3699 | BackendRegister.pfnRegisterImage = vdPluginRegisterImage;
|
---|
3700 | BackendRegister.pfnRegisterCache = vdPluginRegisterCache;
|
---|
3701 | BackendRegister.pfnRegisterFilter = vdPluginRegisterFilter;
|
---|
3702 |
|
---|
3703 | rc = RTLdrGetSymbol(hPlugin, VD_PLUGIN_LOAD_NAME, (void**)&pfnVDPluginLoad);
|
---|
3704 | if (RT_FAILURE(rc) || !pfnVDPluginLoad)
|
---|
3705 | {
|
---|
3706 | LogFunc(("error resolving the entry point %s in plugin %s, rc=%Rrc, pfnVDPluginLoad=%#p\n",
|
---|
3707 | VD_PLUGIN_LOAD_NAME, pszFilename, rc, pfnVDPluginLoad));
|
---|
3708 | if (RT_SUCCESS(rc))
|
---|
3709 | rc = VERR_SYMBOL_NOT_FOUND;
|
---|
3710 | }
|
---|
3711 |
|
---|
3712 | if (RT_SUCCESS(rc))
|
---|
3713 | {
|
---|
3714 | /* Get the function table. */
|
---|
3715 | rc = pfnVDPluginLoad(hPlugin, &BackendRegister);
|
---|
3716 | }
|
---|
3717 | else
|
---|
3718 | LogFunc(("ignored plugin '%s': rc=%Rrc\n", pszFilename, rc));
|
---|
3719 |
|
---|
3720 | /* Create a plugin entry on success. */
|
---|
3721 | if (RT_SUCCESS(rc))
|
---|
3722 | vdAddPlugin(hPlugin, pszFilename);
|
---|
3723 | else
|
---|
3724 | RTLdrClose(hPlugin);
|
---|
3725 | }
|
---|
3726 |
|
---|
3727 | return rc;
|
---|
3728 | #else
|
---|
3729 | RT_NOREF1(pszFilename);
|
---|
3730 | return VERR_NOT_IMPLEMENTED;
|
---|
3731 | #endif
|
---|
3732 | }
|
---|
3733 |
|
---|
3734 | /**
|
---|
3735 | * Worker for VDPluginLoadFromPath() and vdLoadDynamicBackends().
|
---|
3736 | *
|
---|
3737 | * @returns VBox status code.
|
---|
3738 | * @param pszPath The path to load plugins from.
|
---|
3739 | */
|
---|
3740 | static int vdPluginLoadFromPath(const char *pszPath)
|
---|
3741 | {
|
---|
3742 | #ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
3743 | /* To get all entries with VBoxHDD as prefix. */
|
---|
3744 | char *pszPluginFilter = RTPathJoinA(pszPath, VD_PLUGIN_PREFIX "*");
|
---|
3745 | if (!pszPluginFilter)
|
---|
3746 | return VERR_NO_STR_MEMORY;
|
---|
3747 |
|
---|
3748 | PRTDIRENTRYEX pPluginDirEntry = NULL;
|
---|
3749 | PRTDIR pPluginDir = NULL;
|
---|
3750 | size_t cbPluginDirEntry = sizeof(RTDIRENTRYEX);
|
---|
3751 | int rc = RTDirOpenFiltered(&pPluginDir, pszPluginFilter, RTDIRFILTER_WINNT, 0);
|
---|
3752 | if (RT_FAILURE(rc))
|
---|
3753 | {
|
---|
3754 | /* On Windows the above immediately signals that there are no
|
---|
3755 | * files matching, while on other platforms enumerating the
|
---|
3756 | * files below fails. Either way: no plugins. */
|
---|
3757 | goto out;
|
---|
3758 | }
|
---|
3759 |
|
---|
3760 | pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(sizeof(RTDIRENTRYEX));
|
---|
3761 | if (!pPluginDirEntry)
|
---|
3762 | {
|
---|
3763 | rc = VERR_NO_MEMORY;
|
---|
3764 | goto out;
|
---|
3765 | }
|
---|
3766 |
|
---|
3767 | while ( (rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK))
|
---|
3768 | != VERR_NO_MORE_FILES)
|
---|
3769 | {
|
---|
3770 | char *pszPluginPath = NULL;
|
---|
3771 |
|
---|
3772 | if (rc == VERR_BUFFER_OVERFLOW)
|
---|
3773 | {
|
---|
3774 | /* allocate new buffer. */
|
---|
3775 | RTMemFree(pPluginDirEntry);
|
---|
3776 | pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(cbPluginDirEntry);
|
---|
3777 | if (!pPluginDirEntry)
|
---|
3778 | {
|
---|
3779 | rc = VERR_NO_MEMORY;
|
---|
3780 | break;
|
---|
3781 | }
|
---|
3782 | /* Retry. */
|
---|
3783 | rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
|
---|
3784 | if (RT_FAILURE(rc))
|
---|
3785 | break;
|
---|
3786 | }
|
---|
3787 | else if (RT_FAILURE(rc))
|
---|
3788 | break;
|
---|
3789 |
|
---|
3790 | /* We got the new entry. */
|
---|
3791 | if (!RTFS_IS_FILE(pPluginDirEntry->Info.Attr.fMode))
|
---|
3792 | continue;
|
---|
3793 |
|
---|
3794 | /* Prepend the path to the libraries. */
|
---|
3795 | pszPluginPath = RTPathJoinA(pszPath, pPluginDirEntry->szName);
|
---|
3796 | if (!pszPluginPath)
|
---|
3797 | {
|
---|
3798 | rc = VERR_NO_STR_MEMORY;
|
---|
3799 | break;
|
---|
3800 | }
|
---|
3801 |
|
---|
3802 | rc = vdPluginLoadFromFilename(pszPluginPath);
|
---|
3803 | RTStrFree(pszPluginPath);
|
---|
3804 | }
|
---|
3805 | out:
|
---|
3806 | if (rc == VERR_NO_MORE_FILES)
|
---|
3807 | rc = VINF_SUCCESS;
|
---|
3808 | RTStrFree(pszPluginFilter);
|
---|
3809 | if (pPluginDirEntry)
|
---|
3810 | RTMemFree(pPluginDirEntry);
|
---|
3811 | if (pPluginDir)
|
---|
3812 | RTDirClose(pPluginDir);
|
---|
3813 | return rc;
|
---|
3814 | #else
|
---|
3815 | RT_NOREF1(pszPath);
|
---|
3816 | return VERR_NOT_IMPLEMENTED;
|
---|
3817 | #endif
|
---|
3818 | }
|
---|
3819 |
|
---|
3820 | /**
|
---|
3821 | * internal: scans plugin directory and loads found plugins.
|
---|
3822 | */
|
---|
3823 | static int vdLoadDynamicBackends(void)
|
---|
3824 | {
|
---|
3825 | #ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
3826 | /*
|
---|
3827 | * Enumerate plugin backends from the application directory where the other
|
---|
3828 | * shared libraries are.
|
---|
3829 | */
|
---|
3830 | char szPath[RTPATH_MAX];
|
---|
3831 | int rc = RTPathAppPrivateArch(szPath, sizeof(szPath));
|
---|
3832 | if (RT_FAILURE(rc))
|
---|
3833 | return rc;
|
---|
3834 |
|
---|
3835 | return vdPluginLoadFromPath(szPath);
|
---|
3836 | #else
|
---|
3837 | return VINF_SUCCESS;
|
---|
3838 | #endif
|
---|
3839 | }
|
---|
3840 |
|
---|
3841 | /**
|
---|
3842 | * Worker for VDPluginUnloadFromFilename() and vdPluginUnloadFromPath().
|
---|
3843 | *
|
---|
3844 | * @returns VBox status code.
|
---|
3845 | * @param pszFilename The plugin filename to unload.
|
---|
3846 | */
|
---|
3847 | static int vdPluginUnloadFromFilename(const char *pszFilename)
|
---|
3848 | {
|
---|
3849 | #ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
3850 | return vdRemovePlugin(pszFilename);
|
---|
3851 | #else
|
---|
3852 | RT_NOREF1(pszFilename);
|
---|
3853 | return VERR_NOT_IMPLEMENTED;
|
---|
3854 | #endif
|
---|
3855 | }
|
---|
3856 |
|
---|
3857 | /**
|
---|
3858 | * Worker for VDPluginUnloadFromPath().
|
---|
3859 | *
|
---|
3860 | * @returns VBox status code.
|
---|
3861 | * @param pszPath The path to unload plugins from.
|
---|
3862 | */
|
---|
3863 | static int vdPluginUnloadFromPath(const char *pszPath)
|
---|
3864 | {
|
---|
3865 | #ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
3866 | /* To get all entries with VBoxHDD as prefix. */
|
---|
3867 | char *pszPluginFilter = RTPathJoinA(pszPath, VD_PLUGIN_PREFIX "*");
|
---|
3868 | if (!pszPluginFilter)
|
---|
3869 | return VERR_NO_STR_MEMORY;
|
---|
3870 |
|
---|
3871 | PRTDIRENTRYEX pPluginDirEntry = NULL;
|
---|
3872 | PRTDIR pPluginDir = NULL;
|
---|
3873 | size_t cbPluginDirEntry = sizeof(RTDIRENTRYEX);
|
---|
3874 | int rc = RTDirOpenFiltered(&pPluginDir, pszPluginFilter, RTDIRFILTER_WINNT, 0);
|
---|
3875 | if (RT_FAILURE(rc))
|
---|
3876 | {
|
---|
3877 | /* On Windows the above immediately signals that there are no
|
---|
3878 | * files matching, while on other platforms enumerating the
|
---|
3879 | * files below fails. Either way: no plugins. */
|
---|
3880 | goto out;
|
---|
3881 | }
|
---|
3882 |
|
---|
3883 | pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(sizeof(RTDIRENTRYEX));
|
---|
3884 | if (!pPluginDirEntry)
|
---|
3885 | {
|
---|
3886 | rc = VERR_NO_MEMORY;
|
---|
3887 | goto out;
|
---|
3888 | }
|
---|
3889 |
|
---|
3890 | while ((rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK)) != VERR_NO_MORE_FILES)
|
---|
3891 | {
|
---|
3892 | char *pszPluginPath = NULL;
|
---|
3893 |
|
---|
3894 | if (rc == VERR_BUFFER_OVERFLOW)
|
---|
3895 | {
|
---|
3896 | /* allocate new buffer. */
|
---|
3897 | RTMemFree(pPluginDirEntry);
|
---|
3898 | pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(cbPluginDirEntry);
|
---|
3899 | if (!pPluginDirEntry)
|
---|
3900 | {
|
---|
3901 | rc = VERR_NO_MEMORY;
|
---|
3902 | break;
|
---|
3903 | }
|
---|
3904 | /* Retry. */
|
---|
3905 | rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
|
---|
3906 | if (RT_FAILURE(rc))
|
---|
3907 | break;
|
---|
3908 | }
|
---|
3909 | else if (RT_FAILURE(rc))
|
---|
3910 | break;
|
---|
3911 |
|
---|
3912 | /* We got the new entry. */
|
---|
3913 | if (!RTFS_IS_FILE(pPluginDirEntry->Info.Attr.fMode))
|
---|
3914 | continue;
|
---|
3915 |
|
---|
3916 | /* Prepend the path to the libraries. */
|
---|
3917 | pszPluginPath = RTPathJoinA(pszPath, pPluginDirEntry->szName);
|
---|
3918 | if (!pszPluginPath)
|
---|
3919 | {
|
---|
3920 | rc = VERR_NO_STR_MEMORY;
|
---|
3921 | break;
|
---|
3922 | }
|
---|
3923 |
|
---|
3924 | rc = vdPluginUnloadFromFilename(pszPluginPath);
|
---|
3925 | RTStrFree(pszPluginPath);
|
---|
3926 | }
|
---|
3927 | out:
|
---|
3928 | if (rc == VERR_NO_MORE_FILES)
|
---|
3929 | rc = VINF_SUCCESS;
|
---|
3930 | RTStrFree(pszPluginFilter);
|
---|
3931 | if (pPluginDirEntry)
|
---|
3932 | RTMemFree(pPluginDirEntry);
|
---|
3933 | if (pPluginDir)
|
---|
3934 | RTDirClose(pPluginDir);
|
---|
3935 | return rc;
|
---|
3936 | #else
|
---|
3937 | RT_NOREF1(pszPath);
|
---|
3938 | return VERR_NOT_IMPLEMENTED;
|
---|
3939 | #endif
|
---|
3940 | }
|
---|
3941 |
|
---|
3942 | /**
|
---|
3943 | * VD async I/O interface open callback.
|
---|
3944 | */
|
---|
3945 | static DECLCALLBACK(int) vdIOOpenFallback(void *pvUser, const char *pszLocation,
|
---|
3946 | uint32_t fOpen, PFNVDCOMPLETED pfnCompleted,
|
---|
3947 | void **ppStorage)
|
---|
3948 | {
|
---|
3949 | RT_NOREF1(pvUser);
|
---|
3950 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)RTMemAllocZ(sizeof(VDIIOFALLBACKSTORAGE));
|
---|
3951 |
|
---|
3952 | if (!pStorage)
|
---|
3953 | return VERR_NO_MEMORY;
|
---|
3954 |
|
---|
3955 | pStorage->pfnCompleted = pfnCompleted;
|
---|
3956 |
|
---|
3957 | /* Open the file. */
|
---|
3958 | int rc = RTFileOpen(&pStorage->File, pszLocation, fOpen);
|
---|
3959 | if (RT_SUCCESS(rc))
|
---|
3960 | {
|
---|
3961 | *ppStorage = pStorage;
|
---|
3962 | return VINF_SUCCESS;
|
---|
3963 | }
|
---|
3964 |
|
---|
3965 | RTMemFree(pStorage);
|
---|
3966 | return rc;
|
---|
3967 | }
|
---|
3968 |
|
---|
3969 | /**
|
---|
3970 | * VD async I/O interface close callback.
|
---|
3971 | */
|
---|
3972 | static DECLCALLBACK(int) vdIOCloseFallback(void *pvUser, void *pvStorage)
|
---|
3973 | {
|
---|
3974 | RT_NOREF1(pvUser);
|
---|
3975 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
3976 |
|
---|
3977 | RTFileClose(pStorage->File);
|
---|
3978 | RTMemFree(pStorage);
|
---|
3979 | return VINF_SUCCESS;
|
---|
3980 | }
|
---|
3981 |
|
---|
3982 | static DECLCALLBACK(int) vdIODeleteFallback(void *pvUser, const char *pcszFilename)
|
---|
3983 | {
|
---|
3984 | RT_NOREF1(pvUser);
|
---|
3985 | return RTFileDelete(pcszFilename);
|
---|
3986 | }
|
---|
3987 |
|
---|
3988 | static DECLCALLBACK(int) vdIOMoveFallback(void *pvUser, const char *pcszSrc, const char *pcszDst, unsigned fMove)
|
---|
3989 | {
|
---|
3990 | RT_NOREF1(pvUser);
|
---|
3991 | return RTFileMove(pcszSrc, pcszDst, fMove);
|
---|
3992 | }
|
---|
3993 |
|
---|
3994 | static DECLCALLBACK(int) vdIOGetFreeSpaceFallback(void *pvUser, const char *pcszFilename, int64_t *pcbFreeSpace)
|
---|
3995 | {
|
---|
3996 | RT_NOREF1(pvUser);
|
---|
3997 | return RTFsQuerySizes(pcszFilename, NULL, pcbFreeSpace, NULL, NULL);
|
---|
3998 | }
|
---|
3999 |
|
---|
4000 | static DECLCALLBACK(int) vdIOGetModificationTimeFallback(void *pvUser, const char *pcszFilename, PRTTIMESPEC pModificationTime)
|
---|
4001 | {
|
---|
4002 | RT_NOREF1(pvUser);
|
---|
4003 | RTFSOBJINFO info;
|
---|
4004 | int rc = RTPathQueryInfo(pcszFilename, &info, RTFSOBJATTRADD_NOTHING);
|
---|
4005 | if (RT_SUCCESS(rc))
|
---|
4006 | *pModificationTime = info.ModificationTime;
|
---|
4007 | return rc;
|
---|
4008 | }
|
---|
4009 |
|
---|
4010 | /**
|
---|
4011 | * VD async I/O interface callback for retrieving the file size.
|
---|
4012 | */
|
---|
4013 | static DECLCALLBACK(int) vdIOGetSizeFallback(void *pvUser, void *pvStorage, uint64_t *pcbSize)
|
---|
4014 | {
|
---|
4015 | RT_NOREF1(pvUser);
|
---|
4016 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
4017 |
|
---|
4018 | return RTFileGetSize(pStorage->File, pcbSize);
|
---|
4019 | }
|
---|
4020 |
|
---|
4021 | /**
|
---|
4022 | * VD async I/O interface callback for setting the file size.
|
---|
4023 | */
|
---|
4024 | static DECLCALLBACK(int) vdIOSetSizeFallback(void *pvUser, void *pvStorage, uint64_t cbSize)
|
---|
4025 | {
|
---|
4026 | RT_NOREF1(pvUser);
|
---|
4027 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
4028 |
|
---|
4029 | return RTFileSetSize(pStorage->File, cbSize);
|
---|
4030 | }
|
---|
4031 |
|
---|
4032 | /**
|
---|
4033 | * VD async I/O interface callback for setting the file allocation size.
|
---|
4034 | */
|
---|
4035 | static DECLCALLBACK(int) vdIOSetAllocationSizeFallback(void *pvUser, void *pvStorage, uint64_t cbSize,
|
---|
4036 | uint32_t fFlags)
|
---|
4037 | {
|
---|
4038 | RT_NOREF2(pvUser, fFlags);
|
---|
4039 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
4040 |
|
---|
4041 | return RTFileSetAllocationSize(pStorage->File, cbSize, RTFILE_ALLOC_SIZE_F_DEFAULT);
|
---|
4042 | }
|
---|
4043 |
|
---|
4044 | /**
|
---|
4045 | * VD async I/O interface callback for a synchronous write to the file.
|
---|
4046 | */
|
---|
4047 | static DECLCALLBACK(int) vdIOWriteSyncFallback(void *pvUser, void *pvStorage, uint64_t uOffset,
|
---|
4048 | const void *pvBuf, size_t cbWrite, size_t *pcbWritten)
|
---|
4049 | {
|
---|
4050 | RT_NOREF1(pvUser);
|
---|
4051 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
4052 |
|
---|
4053 | return RTFileWriteAt(pStorage->File, uOffset, pvBuf, cbWrite, pcbWritten);
|
---|
4054 | }
|
---|
4055 |
|
---|
4056 | /**
|
---|
4057 | * VD async I/O interface callback for a synchronous read from the file.
|
---|
4058 | */
|
---|
4059 | static DECLCALLBACK(int) vdIOReadSyncFallback(void *pvUser, void *pvStorage, uint64_t uOffset,
|
---|
4060 | void *pvBuf, size_t cbRead, size_t *pcbRead)
|
---|
4061 | {
|
---|
4062 | RT_NOREF1(pvUser);
|
---|
4063 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
4064 |
|
---|
4065 | return RTFileReadAt(pStorage->File, uOffset, pvBuf, cbRead, pcbRead);
|
---|
4066 | }
|
---|
4067 |
|
---|
4068 | /**
|
---|
4069 | * VD async I/O interface callback for a synchronous flush of the file data.
|
---|
4070 | */
|
---|
4071 | static DECLCALLBACK(int) vdIOFlushSyncFallback(void *pvUser, void *pvStorage)
|
---|
4072 | {
|
---|
4073 | RT_NOREF1(pvUser);
|
---|
4074 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
4075 |
|
---|
4076 | return RTFileFlush(pStorage->File);
|
---|
4077 | }
|
---|
4078 |
|
---|
4079 | /**
|
---|
4080 | * VD async I/O interface callback for a asynchronous read from the file.
|
---|
4081 | */
|
---|
4082 | static DECLCALLBACK(int) vdIOReadAsyncFallback(void *pvUser, void *pStorage, uint64_t uOffset,
|
---|
4083 | PCRTSGSEG paSegments, size_t cSegments,
|
---|
4084 | size_t cbRead, void *pvCompletion,
|
---|
4085 | void **ppTask)
|
---|
4086 | {
|
---|
4087 | RT_NOREF8(pvUser, pStorage, uOffset, paSegments, cSegments, cbRead, pvCompletion, ppTask);
|
---|
4088 | return VERR_NOT_IMPLEMENTED;
|
---|
4089 | }
|
---|
4090 |
|
---|
4091 | /**
|
---|
4092 | * VD async I/O interface callback for a asynchronous write to the file.
|
---|
4093 | */
|
---|
4094 | static DECLCALLBACK(int) vdIOWriteAsyncFallback(void *pvUser, void *pStorage, uint64_t uOffset,
|
---|
4095 | PCRTSGSEG paSegments, size_t cSegments,
|
---|
4096 | size_t cbWrite, void *pvCompletion,
|
---|
4097 | void **ppTask)
|
---|
4098 | {
|
---|
4099 | RT_NOREF8(pvUser, pStorage, uOffset, paSegments, cSegments, cbWrite, pvCompletion, ppTask);
|
---|
4100 | return VERR_NOT_IMPLEMENTED;
|
---|
4101 | }
|
---|
4102 |
|
---|
4103 | /**
|
---|
4104 | * VD async I/O interface callback for a asynchronous flush of the file data.
|
---|
4105 | */
|
---|
4106 | static DECLCALLBACK(int) vdIOFlushAsyncFallback(void *pvUser, void *pStorage,
|
---|
4107 | void *pvCompletion, void **ppTask)
|
---|
4108 | {
|
---|
4109 | RT_NOREF4(pvUser, pStorage, pvCompletion, ppTask);
|
---|
4110 | return VERR_NOT_IMPLEMENTED;
|
---|
4111 | }
|
---|
4112 |
|
---|
4113 | /**
|
---|
4114 | * Internal - Continues an I/O context after
|
---|
4115 | * it was halted because of an active transfer.
|
---|
4116 | */
|
---|
4117 | static int vdIoCtxContinue(PVDIOCTX pIoCtx, int rcReq)
|
---|
4118 | {
|
---|
4119 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
4120 | int rc = VINF_SUCCESS;
|
---|
4121 |
|
---|
4122 | VD_IS_LOCKED(pDisk);
|
---|
4123 |
|
---|
4124 | if (RT_FAILURE(rcReq))
|
---|
4125 | ASMAtomicCmpXchgS32(&pIoCtx->rcReq, rcReq, VINF_SUCCESS);
|
---|
4126 |
|
---|
4127 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_BLOCKED))
|
---|
4128 | {
|
---|
4129 | /* Continue the transfer */
|
---|
4130 | rc = vdIoCtxProcessLocked(pIoCtx);
|
---|
4131 |
|
---|
4132 | if ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
4133 | && ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
|
---|
4134 | {
|
---|
4135 | LogFlowFunc(("I/O context completed pIoCtx=%#p\n", pIoCtx));
|
---|
4136 | if (pIoCtx->pIoCtxParent)
|
---|
4137 | {
|
---|
4138 | PVDIOCTX pIoCtxParent = pIoCtx->pIoCtxParent;
|
---|
4139 |
|
---|
4140 | Assert(!pIoCtxParent->pIoCtxParent);
|
---|
4141 | if (RT_FAILURE(pIoCtx->rcReq))
|
---|
4142 | ASMAtomicCmpXchgS32(&pIoCtxParent->rcReq, pIoCtx->rcReq, VINF_SUCCESS);
|
---|
4143 |
|
---|
4144 | ASMAtomicDecU32(&pIoCtxParent->cDataTransfersPending);
|
---|
4145 |
|
---|
4146 | if (pIoCtx->enmTxDir == VDIOCTXTXDIR_WRITE)
|
---|
4147 | {
|
---|
4148 | LogFlowFunc(("I/O context transferred %u bytes for the parent pIoCtxParent=%p\n",
|
---|
4149 | pIoCtx->Type.Child.cbTransferParent, pIoCtxParent));
|
---|
4150 |
|
---|
4151 | /* Update the parent state. */
|
---|
4152 | Assert(pIoCtxParent->Req.Io.cbTransferLeft >= pIoCtx->Type.Child.cbTransferParent);
|
---|
4153 | ASMAtomicSubU32(&pIoCtxParent->Req.Io.cbTransferLeft, (uint32_t)pIoCtx->Type.Child.cbTransferParent);
|
---|
4154 | }
|
---|
4155 | else
|
---|
4156 | Assert(pIoCtx->enmTxDir == VDIOCTXTXDIR_FLUSH);
|
---|
4157 |
|
---|
4158 | /*
|
---|
4159 | * A completed child write means that we finished growing the image.
|
---|
4160 | * We have to process any pending writes now.
|
---|
4161 | */
|
---|
4162 | vdIoCtxUnlockDisk(pDisk, pIoCtxParent, false /* fProcessDeferredReqs */);
|
---|
4163 |
|
---|
4164 | /* Unblock the parent */
|
---|
4165 | pIoCtxParent->fFlags &= ~VDIOCTX_FLAGS_BLOCKED;
|
---|
4166 |
|
---|
4167 | rc = vdIoCtxProcessLocked(pIoCtxParent);
|
---|
4168 |
|
---|
4169 | if ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
4170 | && ASMAtomicCmpXchgBool(&pIoCtxParent->fComplete, true, false))
|
---|
4171 | {
|
---|
4172 | LogFlowFunc(("Parent I/O context completed pIoCtxParent=%#p rcReq=%Rrc\n", pIoCtxParent, pIoCtxParent->rcReq));
|
---|
4173 | vdIoCtxRootComplete(pDisk, pIoCtxParent);
|
---|
4174 | vdThreadFinishWrite(pDisk);
|
---|
4175 | vdIoCtxFree(pDisk, pIoCtxParent);
|
---|
4176 | vdDiskProcessBlockedIoCtx(pDisk);
|
---|
4177 | }
|
---|
4178 | else if (!vdIoCtxIsDiskLockOwner(pDisk, pIoCtx))
|
---|
4179 | {
|
---|
4180 | /* Process any pending writes if the current request didn't caused another growing. */
|
---|
4181 | vdDiskProcessBlockedIoCtx(pDisk);
|
---|
4182 | }
|
---|
4183 | }
|
---|
4184 | else
|
---|
4185 | {
|
---|
4186 | if (pIoCtx->enmTxDir == VDIOCTXTXDIR_FLUSH)
|
---|
4187 | {
|
---|
4188 | vdIoCtxUnlockDisk(pDisk, pIoCtx, true /* fProcessDerredReqs */);
|
---|
4189 | vdThreadFinishWrite(pDisk);
|
---|
4190 | }
|
---|
4191 | else if ( pIoCtx->enmTxDir == VDIOCTXTXDIR_WRITE
|
---|
4192 | || pIoCtx->enmTxDir == VDIOCTXTXDIR_DISCARD)
|
---|
4193 | vdThreadFinishWrite(pDisk);
|
---|
4194 | else
|
---|
4195 | {
|
---|
4196 | Assert(pIoCtx->enmTxDir == VDIOCTXTXDIR_READ);
|
---|
4197 | vdThreadFinishRead(pDisk);
|
---|
4198 | }
|
---|
4199 |
|
---|
4200 | LogFlowFunc(("I/O context completed pIoCtx=%#p rcReq=%Rrc\n", pIoCtx, pIoCtx->rcReq));
|
---|
4201 | vdIoCtxRootComplete(pDisk, pIoCtx);
|
---|
4202 | }
|
---|
4203 |
|
---|
4204 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
4205 | }
|
---|
4206 | }
|
---|
4207 |
|
---|
4208 | return VINF_SUCCESS;
|
---|
4209 | }
|
---|
4210 |
|
---|
4211 | /**
|
---|
4212 | * Internal - Called when user transfer completed.
|
---|
4213 | */
|
---|
4214 | static int vdUserXferCompleted(PVDIOSTORAGE pIoStorage, PVDIOCTX pIoCtx,
|
---|
4215 | PFNVDXFERCOMPLETED pfnComplete, void *pvUser,
|
---|
4216 | size_t cbTransfer, int rcReq)
|
---|
4217 | {
|
---|
4218 | int rc = VINF_SUCCESS;
|
---|
4219 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
4220 |
|
---|
4221 | LogFlowFunc(("pIoStorage=%#p pIoCtx=%#p pfnComplete=%#p pvUser=%#p cbTransfer=%zu rcReq=%Rrc\n",
|
---|
4222 | pIoStorage, pIoCtx, pfnComplete, pvUser, cbTransfer, rcReq));
|
---|
4223 |
|
---|
4224 | VD_IS_LOCKED(pDisk);
|
---|
4225 |
|
---|
4226 | Assert(pIoCtx->Req.Io.cbTransferLeft >= cbTransfer);
|
---|
4227 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbTransfer); Assert(cbTransfer == (uint32_t)cbTransfer);
|
---|
4228 | ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
|
---|
4229 |
|
---|
4230 | if (pfnComplete)
|
---|
4231 | rc = pfnComplete(pIoStorage->pVDIo->pBackendData, pIoCtx, pvUser, rcReq);
|
---|
4232 |
|
---|
4233 | if (RT_SUCCESS(rc))
|
---|
4234 | rc = vdIoCtxContinue(pIoCtx, rcReq);
|
---|
4235 | else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
4236 | rc = VINF_SUCCESS;
|
---|
4237 |
|
---|
4238 | return rc;
|
---|
4239 | }
|
---|
4240 |
|
---|
4241 | static void vdIoCtxContinueDeferredList(PVDIOSTORAGE pIoStorage, PRTLISTANCHOR pListWaiting,
|
---|
4242 | PFNVDXFERCOMPLETED pfnComplete, void *pvUser, int rcReq)
|
---|
4243 | {
|
---|
4244 | LogFlowFunc(("pIoStorage=%#p pListWaiting=%#p pfnComplete=%#p pvUser=%#p rcReq=%Rrc\n",
|
---|
4245 | pIoStorage, pListWaiting, pfnComplete, pvUser, rcReq));
|
---|
4246 |
|
---|
4247 | /* Go through the waiting list and continue the I/O contexts. */
|
---|
4248 | while (!RTListIsEmpty(pListWaiting))
|
---|
4249 | {
|
---|
4250 | int rc = VINF_SUCCESS;
|
---|
4251 | PVDIOCTXDEFERRED pDeferred = RTListGetFirst(pListWaiting, VDIOCTXDEFERRED, NodeDeferred);
|
---|
4252 | PVDIOCTX pIoCtx = pDeferred->pIoCtx;
|
---|
4253 | RTListNodeRemove(&pDeferred->NodeDeferred);
|
---|
4254 |
|
---|
4255 | RTMemFree(pDeferred);
|
---|
4256 | ASMAtomicDecU32(&pIoCtx->cMetaTransfersPending);
|
---|
4257 |
|
---|
4258 | if (pfnComplete)
|
---|
4259 | rc = pfnComplete(pIoStorage->pVDIo->pBackendData, pIoCtx, pvUser, rcReq);
|
---|
4260 |
|
---|
4261 | LogFlow(("Completion callback for I/O context %#p returned %Rrc\n", pIoCtx, rc));
|
---|
4262 |
|
---|
4263 | if (RT_SUCCESS(rc))
|
---|
4264 | {
|
---|
4265 | rc = vdIoCtxContinue(pIoCtx, rcReq);
|
---|
4266 | AssertRC(rc);
|
---|
4267 | }
|
---|
4268 | else
|
---|
4269 | Assert(rc == VERR_VD_ASYNC_IO_IN_PROGRESS);
|
---|
4270 | }
|
---|
4271 | }
|
---|
4272 |
|
---|
4273 | /**
|
---|
4274 | * Internal - Called when a meta transfer completed.
|
---|
4275 | */
|
---|
4276 | static int vdMetaXferCompleted(PVDIOSTORAGE pIoStorage, PFNVDXFERCOMPLETED pfnComplete, void *pvUser,
|
---|
4277 | PVDMETAXFER pMetaXfer, int rcReq)
|
---|
4278 | {
|
---|
4279 | PVBOXHDD pDisk = pIoStorage->pVDIo->pDisk;
|
---|
4280 | RTLISTNODE ListIoCtxWaiting;
|
---|
4281 | bool fFlush;
|
---|
4282 |
|
---|
4283 | LogFlowFunc(("pIoStorage=%#p pfnComplete=%#p pvUser=%#p pMetaXfer=%#p rcReq=%Rrc\n",
|
---|
4284 | pIoStorage, pfnComplete, pvUser, pMetaXfer, rcReq));
|
---|
4285 |
|
---|
4286 | VD_IS_LOCKED(pDisk);
|
---|
4287 |
|
---|
4288 | fFlush = VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_FLUSH;
|
---|
4289 |
|
---|
4290 | if (!fFlush)
|
---|
4291 | {
|
---|
4292 | RTListMove(&ListIoCtxWaiting, &pMetaXfer->ListIoCtxWaiting);
|
---|
4293 |
|
---|
4294 | if (RT_FAILURE(rcReq))
|
---|
4295 | {
|
---|
4296 | /* Remove from the AVL tree. */
|
---|
4297 | LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
|
---|
4298 | bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
|
---|
4299 | Assert(fRemoved); NOREF(fRemoved);
|
---|
4300 | /* If this was a write check if there is a shadow buffer with updated data. */
|
---|
4301 | if (pMetaXfer->pbDataShw)
|
---|
4302 | {
|
---|
4303 | Assert(VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_WRITE);
|
---|
4304 | Assert(!RTListIsEmpty(&pMetaXfer->ListIoCtxShwWrites));
|
---|
4305 | RTListConcatenate(&ListIoCtxWaiting, &pMetaXfer->ListIoCtxShwWrites);
|
---|
4306 | RTMemFree(pMetaXfer->pbDataShw);
|
---|
4307 | pMetaXfer->pbDataShw = NULL;
|
---|
4308 | }
|
---|
4309 | RTMemFree(pMetaXfer);
|
---|
4310 | }
|
---|
4311 | else
|
---|
4312 | {
|
---|
4313 | /* Increase the reference counter to make sure it doesn't go away before the last context is processed. */
|
---|
4314 | pMetaXfer->cRefs++;
|
---|
4315 | }
|
---|
4316 | }
|
---|
4317 | else
|
---|
4318 | RTListMove(&ListIoCtxWaiting, &pMetaXfer->ListIoCtxWaiting);
|
---|
4319 |
|
---|
4320 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
|
---|
4321 | vdIoCtxContinueDeferredList(pIoStorage, &ListIoCtxWaiting, pfnComplete, pvUser, rcReq);
|
---|
4322 |
|
---|
4323 | /*
|
---|
4324 | * If there is a shadow buffer and the previous write was successful update with the
|
---|
4325 | * new data and trigger a new write.
|
---|
4326 | */
|
---|
4327 | if ( pMetaXfer->pbDataShw
|
---|
4328 | && RT_SUCCESS(rcReq)
|
---|
4329 | && VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE)
|
---|
4330 | {
|
---|
4331 | LogFlowFunc(("pMetaXfer=%#p Updating from shadow buffer and triggering new write\n", pMetaXfer));
|
---|
4332 | memcpy(pMetaXfer->abData, pMetaXfer->pbDataShw, pMetaXfer->cbMeta);
|
---|
4333 | RTMemFree(pMetaXfer->pbDataShw);
|
---|
4334 | pMetaXfer->pbDataShw = NULL;
|
---|
4335 | Assert(!RTListIsEmpty(&pMetaXfer->ListIoCtxShwWrites));
|
---|
4336 |
|
---|
4337 | /* Setup a new I/O write. */
|
---|
4338 | PVDIOTASK pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvUser, pMetaXfer);
|
---|
4339 | if (RT_LIKELY(pIoTask))
|
---|
4340 | {
|
---|
4341 | void *pvTask = NULL;
|
---|
4342 | RTSGSEG Seg;
|
---|
4343 |
|
---|
4344 | Seg.cbSeg = pMetaXfer->cbMeta;
|
---|
4345 | Seg.pvSeg = pMetaXfer->abData;
|
---|
4346 |
|
---|
4347 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_WRITE);
|
---|
4348 | rcReq = pIoStorage->pVDIo->pInterfaceIo->pfnWriteAsync(pIoStorage->pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4349 | pIoStorage->pStorage,
|
---|
4350 | pMetaXfer->Core.Key, &Seg, 1,
|
---|
4351 | pMetaXfer->cbMeta, pIoTask,
|
---|
4352 | &pvTask);
|
---|
4353 | if ( RT_SUCCESS(rcReq)
|
---|
4354 | || rcReq != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
4355 | {
|
---|
4356 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
|
---|
4357 | vdIoTaskFree(pDisk, pIoTask);
|
---|
4358 | }
|
---|
4359 | else
|
---|
4360 | RTListMove(&pMetaXfer->ListIoCtxWaiting, &pMetaXfer->ListIoCtxShwWrites);
|
---|
4361 | }
|
---|
4362 | else
|
---|
4363 | rcReq = VERR_NO_MEMORY;
|
---|
4364 |
|
---|
4365 | /* Cleanup if there was an error or the request completed already. */
|
---|
4366 | if (rcReq != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
4367 | vdIoCtxContinueDeferredList(pIoStorage, &pMetaXfer->ListIoCtxShwWrites, pfnComplete, pvUser, rcReq);
|
---|
4368 | }
|
---|
4369 |
|
---|
4370 | /* Remove if not used anymore. */
|
---|
4371 | if (!fFlush)
|
---|
4372 | {
|
---|
4373 | pMetaXfer->cRefs--;
|
---|
4374 | if (!pMetaXfer->cRefs && RTListIsEmpty(&pMetaXfer->ListIoCtxWaiting))
|
---|
4375 | {
|
---|
4376 | /* Remove from the AVL tree. */
|
---|
4377 | LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
|
---|
4378 | bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
|
---|
4379 | Assert(fRemoved); NOREF(fRemoved);
|
---|
4380 | RTMemFree(pMetaXfer);
|
---|
4381 | }
|
---|
4382 | }
|
---|
4383 | else if (fFlush)
|
---|
4384 | RTMemFree(pMetaXfer);
|
---|
4385 |
|
---|
4386 | return VINF_SUCCESS;
|
---|
4387 | }
|
---|
4388 |
|
---|
4389 | /**
|
---|
4390 | * Processes a list of waiting I/O tasks. The disk lock must be held by caller.
|
---|
4391 | *
|
---|
4392 | * @returns nothing.
|
---|
4393 | * @param pDisk The disk to process the list for.
|
---|
4394 | */
|
---|
4395 | static void vdIoTaskProcessWaitingList(PVBOXHDD pDisk)
|
---|
4396 | {
|
---|
4397 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
4398 |
|
---|
4399 | VD_IS_LOCKED(pDisk);
|
---|
4400 |
|
---|
4401 | PVDIOTASK pHead = ASMAtomicXchgPtrT(&pDisk->pIoTasksPendingHead, NULL, PVDIOTASK);
|
---|
4402 |
|
---|
4403 | Log(("I/O task list cleared\n"));
|
---|
4404 |
|
---|
4405 | /* Reverse order. */
|
---|
4406 | PVDIOTASK pCur = pHead;
|
---|
4407 | pHead = NULL;
|
---|
4408 | while (pCur)
|
---|
4409 | {
|
---|
4410 | PVDIOTASK pInsert = pCur;
|
---|
4411 | pCur = pCur->pNext;
|
---|
4412 | pInsert->pNext = pHead;
|
---|
4413 | pHead = pInsert;
|
---|
4414 | }
|
---|
4415 |
|
---|
4416 | while (pHead)
|
---|
4417 | {
|
---|
4418 | PVDIOSTORAGE pIoStorage = pHead->pIoStorage;
|
---|
4419 |
|
---|
4420 | if (!pHead->fMeta)
|
---|
4421 | vdUserXferCompleted(pIoStorage, pHead->Type.User.pIoCtx,
|
---|
4422 | pHead->pfnComplete, pHead->pvUser,
|
---|
4423 | pHead->Type.User.cbTransfer, pHead->rcReq);
|
---|
4424 | else
|
---|
4425 | vdMetaXferCompleted(pIoStorage, pHead->pfnComplete, pHead->pvUser,
|
---|
4426 | pHead->Type.Meta.pMetaXfer, pHead->rcReq);
|
---|
4427 |
|
---|
4428 | pCur = pHead;
|
---|
4429 | pHead = pHead->pNext;
|
---|
4430 | vdIoTaskFree(pDisk, pCur);
|
---|
4431 | }
|
---|
4432 | }
|
---|
4433 |
|
---|
4434 | /**
|
---|
4435 | * Process any I/O context on the halted list.
|
---|
4436 | *
|
---|
4437 | * @returns nothing.
|
---|
4438 | * @param pDisk The disk.
|
---|
4439 | */
|
---|
4440 | static void vdIoCtxProcessHaltedList(PVBOXHDD pDisk)
|
---|
4441 | {
|
---|
4442 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
4443 |
|
---|
4444 | VD_IS_LOCKED(pDisk);
|
---|
4445 |
|
---|
4446 | /* Get the waiting list and process it in FIFO order. */
|
---|
4447 | PVDIOCTX pIoCtxHead = ASMAtomicXchgPtrT(&pDisk->pIoCtxHaltedHead, NULL, PVDIOCTX);
|
---|
4448 |
|
---|
4449 | /* Reverse it. */
|
---|
4450 | PVDIOCTX pCur = pIoCtxHead;
|
---|
4451 | pIoCtxHead = NULL;
|
---|
4452 | while (pCur)
|
---|
4453 | {
|
---|
4454 | PVDIOCTX pInsert = pCur;
|
---|
4455 | pCur = pCur->pIoCtxNext;
|
---|
4456 | pInsert->pIoCtxNext = pIoCtxHead;
|
---|
4457 | pIoCtxHead = pInsert;
|
---|
4458 | }
|
---|
4459 |
|
---|
4460 | /* Process now. */
|
---|
4461 | pCur = pIoCtxHead;
|
---|
4462 | while (pCur)
|
---|
4463 | {
|
---|
4464 | PVDIOCTX pTmp = pCur;
|
---|
4465 |
|
---|
4466 | pCur = pCur->pIoCtxNext;
|
---|
4467 | pTmp->pIoCtxNext = NULL;
|
---|
4468 |
|
---|
4469 | /* Continue */
|
---|
4470 | pTmp->fFlags &= ~VDIOCTX_FLAGS_BLOCKED;
|
---|
4471 | vdIoCtxContinue(pTmp, pTmp->rcReq);
|
---|
4472 | }
|
---|
4473 | }
|
---|
4474 |
|
---|
4475 | /**
|
---|
4476 | * Unlock the disk and process pending tasks.
|
---|
4477 | *
|
---|
4478 | * @returns VBox status code.
|
---|
4479 | * @param pDisk The disk to unlock.
|
---|
4480 | */
|
---|
4481 | static int vdDiskUnlock(PVBOXHDD pDisk, PVDIOCTX pIoCtxRc)
|
---|
4482 | {
|
---|
4483 | int rc = VINF_SUCCESS;
|
---|
4484 |
|
---|
4485 | VD_IS_LOCKED(pDisk);
|
---|
4486 |
|
---|
4487 | /*
|
---|
4488 | * Process the list of waiting I/O tasks first
|
---|
4489 | * because they might complete I/O contexts.
|
---|
4490 | * Same for the list of halted I/O contexts.
|
---|
4491 | * Afterwards comes the list of new I/O contexts.
|
---|
4492 | */
|
---|
4493 | vdIoTaskProcessWaitingList(pDisk);
|
---|
4494 | vdIoCtxProcessHaltedList(pDisk);
|
---|
4495 | rc = vdDiskProcessWaitingIoCtx(pDisk, pIoCtxRc);
|
---|
4496 | ASMAtomicXchgBool(&pDisk->fLocked, false);
|
---|
4497 |
|
---|
4498 | /*
|
---|
4499 | * Need to check for new I/O tasks and waiting I/O contexts now
|
---|
4500 | * again as other threads might added them while we processed
|
---|
4501 | * previous lists.
|
---|
4502 | */
|
---|
4503 | while ( ASMAtomicUoReadPtrT(&pDisk->pIoCtxHead, PVDIOCTX) != NULL
|
---|
4504 | || ASMAtomicUoReadPtrT(&pDisk->pIoTasksPendingHead, PVDIOTASK) != NULL
|
---|
4505 | || ASMAtomicUoReadPtrT(&pDisk->pIoCtxHaltedHead, PVDIOCTX) != NULL)
|
---|
4506 | {
|
---|
4507 | /* Try lock disk again. */
|
---|
4508 | if (ASMAtomicCmpXchgBool(&pDisk->fLocked, true, false))
|
---|
4509 | {
|
---|
4510 | vdIoTaskProcessWaitingList(pDisk);
|
---|
4511 | vdIoCtxProcessHaltedList(pDisk);
|
---|
4512 | vdDiskProcessWaitingIoCtx(pDisk, NULL);
|
---|
4513 | ASMAtomicXchgBool(&pDisk->fLocked, false);
|
---|
4514 | }
|
---|
4515 | else /* Let the other thread everything when he unlocks the disk. */
|
---|
4516 | break;
|
---|
4517 | }
|
---|
4518 |
|
---|
4519 | return rc;
|
---|
4520 | }
|
---|
4521 |
|
---|
4522 | /**
|
---|
4523 | * Try to lock the disk to complete pressing of the I/O task.
|
---|
4524 | * The completion is deferred if the disk is locked already.
|
---|
4525 | *
|
---|
4526 | * @returns nothing.
|
---|
4527 | * @param pIoTask The I/O task to complete.
|
---|
4528 | */
|
---|
4529 | static void vdXferTryLockDiskDeferIoTask(PVDIOTASK pIoTask)
|
---|
4530 | {
|
---|
4531 | PVDIOSTORAGE pIoStorage = pIoTask->pIoStorage;
|
---|
4532 | PVBOXHDD pDisk = pIoStorage->pVDIo->pDisk;
|
---|
4533 |
|
---|
4534 | Log(("Deferring I/O task pIoTask=%p\n", pIoTask));
|
---|
4535 |
|
---|
4536 | /* Put it on the waiting list. */
|
---|
4537 | PVDIOTASK pNext = ASMAtomicUoReadPtrT(&pDisk->pIoTasksPendingHead, PVDIOTASK);
|
---|
4538 | PVDIOTASK pHeadOld;
|
---|
4539 | pIoTask->pNext = pNext;
|
---|
4540 | while (!ASMAtomicCmpXchgExPtr(&pDisk->pIoTasksPendingHead, pIoTask, pNext, &pHeadOld))
|
---|
4541 | {
|
---|
4542 | pNext = pHeadOld;
|
---|
4543 | Assert(pNext != pIoTask);
|
---|
4544 | pIoTask->pNext = pNext;
|
---|
4545 | ASMNopPause();
|
---|
4546 | }
|
---|
4547 |
|
---|
4548 | if (ASMAtomicCmpXchgBool(&pDisk->fLocked, true, false))
|
---|
4549 | {
|
---|
4550 | /* Release disk lock, it will take care of processing all lists. */
|
---|
4551 | vdDiskUnlock(pDisk, NULL);
|
---|
4552 | }
|
---|
4553 | }
|
---|
4554 |
|
---|
4555 | static DECLCALLBACK(int) vdIOIntReqCompleted(void *pvUser, int rcReq)
|
---|
4556 | {
|
---|
4557 | PVDIOTASK pIoTask = (PVDIOTASK)pvUser;
|
---|
4558 |
|
---|
4559 | LogFlowFunc(("Task completed pIoTask=%#p\n", pIoTask));
|
---|
4560 |
|
---|
4561 | pIoTask->rcReq = rcReq;
|
---|
4562 | vdXferTryLockDiskDeferIoTask(pIoTask);
|
---|
4563 | return VINF_SUCCESS;
|
---|
4564 | }
|
---|
4565 |
|
---|
4566 | /**
|
---|
4567 | * VD I/O interface callback for opening a file.
|
---|
4568 | */
|
---|
4569 | static DECLCALLBACK(int) vdIOIntOpen(void *pvUser, const char *pszLocation,
|
---|
4570 | unsigned uOpenFlags, PPVDIOSTORAGE ppIoStorage)
|
---|
4571 | {
|
---|
4572 | int rc = VINF_SUCCESS;
|
---|
4573 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4574 | PVDIOSTORAGE pIoStorage = (PVDIOSTORAGE)RTMemAllocZ(sizeof(VDIOSTORAGE));
|
---|
4575 |
|
---|
4576 | if (!pIoStorage)
|
---|
4577 | return VERR_NO_MEMORY;
|
---|
4578 |
|
---|
4579 | /* Create the AVl tree. */
|
---|
4580 | pIoStorage->pTreeMetaXfers = (PAVLRFOFFTREE)RTMemAllocZ(sizeof(AVLRFOFFTREE));
|
---|
4581 | if (pIoStorage->pTreeMetaXfers)
|
---|
4582 | {
|
---|
4583 | rc = pVDIo->pInterfaceIo->pfnOpen(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4584 | pszLocation, uOpenFlags,
|
---|
4585 | vdIOIntReqCompleted,
|
---|
4586 | &pIoStorage->pStorage);
|
---|
4587 | if (RT_SUCCESS(rc))
|
---|
4588 | {
|
---|
4589 | pIoStorage->pVDIo = pVDIo;
|
---|
4590 | *ppIoStorage = pIoStorage;
|
---|
4591 | return VINF_SUCCESS;
|
---|
4592 | }
|
---|
4593 |
|
---|
4594 | RTMemFree(pIoStorage->pTreeMetaXfers);
|
---|
4595 | }
|
---|
4596 | else
|
---|
4597 | rc = VERR_NO_MEMORY;
|
---|
4598 |
|
---|
4599 | RTMemFree(pIoStorage);
|
---|
4600 | return rc;
|
---|
4601 | }
|
---|
4602 |
|
---|
4603 | static DECLCALLBACK(int) vdIOIntTreeMetaXferDestroy(PAVLRFOFFNODECORE pNode, void *pvUser)
|
---|
4604 | {
|
---|
4605 | RT_NOREF2(pNode, pvUser);
|
---|
4606 | AssertMsgFailed(("Tree should be empty at this point!\n"));
|
---|
4607 | return VINF_SUCCESS;
|
---|
4608 | }
|
---|
4609 |
|
---|
4610 | static DECLCALLBACK(int) vdIOIntClose(void *pvUser, PVDIOSTORAGE pIoStorage)
|
---|
4611 | {
|
---|
4612 | int rc = VINF_SUCCESS;
|
---|
4613 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4614 |
|
---|
4615 | /* We free everything here, even if closing the file failed for some reason. */
|
---|
4616 | rc = pVDIo->pInterfaceIo->pfnClose(pVDIo->pInterfaceIo->Core.pvUser, pIoStorage->pStorage);
|
---|
4617 | RTAvlrFileOffsetDestroy(pIoStorage->pTreeMetaXfers, vdIOIntTreeMetaXferDestroy, NULL);
|
---|
4618 | RTMemFree(pIoStorage->pTreeMetaXfers);
|
---|
4619 | RTMemFree(pIoStorage);
|
---|
4620 | return rc;
|
---|
4621 | }
|
---|
4622 |
|
---|
4623 | static DECLCALLBACK(int) vdIOIntDelete(void *pvUser, const char *pcszFilename)
|
---|
4624 | {
|
---|
4625 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4626 | return pVDIo->pInterfaceIo->pfnDelete(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4627 | pcszFilename);
|
---|
4628 | }
|
---|
4629 |
|
---|
4630 | static DECLCALLBACK(int) vdIOIntMove(void *pvUser, const char *pcszSrc, const char *pcszDst,
|
---|
4631 | unsigned fMove)
|
---|
4632 | {
|
---|
4633 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4634 | return pVDIo->pInterfaceIo->pfnMove(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4635 | pcszSrc, pcszDst, fMove);
|
---|
4636 | }
|
---|
4637 |
|
---|
4638 | static DECLCALLBACK(int) vdIOIntGetFreeSpace(void *pvUser, const char *pcszFilename,
|
---|
4639 | int64_t *pcbFreeSpace)
|
---|
4640 | {
|
---|
4641 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4642 | return pVDIo->pInterfaceIo->pfnGetFreeSpace(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4643 | pcszFilename, pcbFreeSpace);
|
---|
4644 | }
|
---|
4645 |
|
---|
4646 | static DECLCALLBACK(int) vdIOIntGetModificationTime(void *pvUser, const char *pcszFilename,
|
---|
4647 | PRTTIMESPEC pModificationTime)
|
---|
4648 | {
|
---|
4649 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4650 | return pVDIo->pInterfaceIo->pfnGetModificationTime(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4651 | pcszFilename, pModificationTime);
|
---|
4652 | }
|
---|
4653 |
|
---|
4654 | static DECLCALLBACK(int) vdIOIntGetSize(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
4655 | uint64_t *pcbSize)
|
---|
4656 | {
|
---|
4657 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4658 | return pVDIo->pInterfaceIo->pfnGetSize(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4659 | pIoStorage->pStorage, pcbSize);
|
---|
4660 | }
|
---|
4661 |
|
---|
4662 | static DECLCALLBACK(int) vdIOIntSetSize(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
4663 | uint64_t cbSize)
|
---|
4664 | {
|
---|
4665 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4666 | return pVDIo->pInterfaceIo->pfnSetSize(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4667 | pIoStorage->pStorage, cbSize);
|
---|
4668 | }
|
---|
4669 |
|
---|
4670 | static DECLCALLBACK(int) vdIOIntSetAllocationSize(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
4671 | uint64_t cbSize, uint32_t fFlags,
|
---|
4672 | PFNVDPROGRESS pfnProgress,
|
---|
4673 | void *pvUserProgess, unsigned uPercentStart,
|
---|
4674 | unsigned uPercentSpan)
|
---|
4675 | {
|
---|
4676 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4677 | int rc = pVDIo->pInterfaceIo->pfnSetAllocationSize(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4678 | pIoStorage->pStorage, cbSize, fFlags);
|
---|
4679 | if (rc == VERR_NOT_SUPPORTED)
|
---|
4680 | {
|
---|
4681 | /* Fallback if the underlying medium does not support optimized storage allocation. */
|
---|
4682 | uint64_t cbSizeCur = 0;
|
---|
4683 | rc = pVDIo->pInterfaceIo->pfnGetSize(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4684 | pIoStorage->pStorage, &cbSizeCur);
|
---|
4685 | if (RT_SUCCESS(rc))
|
---|
4686 | {
|
---|
4687 | if (cbSizeCur < cbSize)
|
---|
4688 | {
|
---|
4689 | const size_t cbBuf = 128 * _1K;
|
---|
4690 | void *pvBuf = RTMemTmpAllocZ(cbBuf);
|
---|
4691 | if (RT_LIKELY(pvBuf))
|
---|
4692 | {
|
---|
4693 | uint64_t cbFill = cbSize - cbSizeCur;
|
---|
4694 | uint64_t uOff = 0;
|
---|
4695 |
|
---|
4696 | /* Write data to all blocks. */
|
---|
4697 | while ( uOff < cbFill
|
---|
4698 | && RT_SUCCESS(rc))
|
---|
4699 | {
|
---|
4700 | size_t cbChunk = (size_t)RT_MIN(cbFill - uOff, cbBuf);
|
---|
4701 |
|
---|
4702 | rc = pVDIo->pInterfaceIo->pfnWriteSync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4703 | pIoStorage->pStorage, cbSizeCur + uOff,
|
---|
4704 | pvBuf, cbChunk, NULL);
|
---|
4705 | if (RT_SUCCESS(rc))
|
---|
4706 | {
|
---|
4707 | uOff += cbChunk;
|
---|
4708 |
|
---|
4709 | if (pfnProgress)
|
---|
4710 | rc = pfnProgress(pvUserProgess, uPercentStart + uOff * uPercentSpan / cbFill);
|
---|
4711 | }
|
---|
4712 | }
|
---|
4713 |
|
---|
4714 | RTMemTmpFree(pvBuf);
|
---|
4715 | }
|
---|
4716 | else
|
---|
4717 | rc = VERR_NO_MEMORY;
|
---|
4718 | }
|
---|
4719 | else if (cbSizeCur > cbSize)
|
---|
4720 | rc = pVDIo->pInterfaceIo->pfnSetSize(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4721 | pIoStorage->pStorage, cbSize);
|
---|
4722 | }
|
---|
4723 | }
|
---|
4724 |
|
---|
4725 | if (RT_SUCCESS(rc) && pfnProgress)
|
---|
4726 | rc = pfnProgress(pvUserProgess, uPercentStart + uPercentSpan);
|
---|
4727 |
|
---|
4728 | return rc;
|
---|
4729 | }
|
---|
4730 |
|
---|
4731 | static DECLCALLBACK(int) vdIOIntReadUser(void *pvUser, PVDIOSTORAGE pIoStorage, uint64_t uOffset,
|
---|
4732 | PVDIOCTX pIoCtx, size_t cbRead)
|
---|
4733 | {
|
---|
4734 | int rc = VINF_SUCCESS;
|
---|
4735 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4736 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
4737 |
|
---|
4738 | LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pIoCtx=%#p cbRead=%u\n",
|
---|
4739 | pvUser, pIoStorage, uOffset, pIoCtx, cbRead));
|
---|
4740 |
|
---|
4741 | /** @todo Enable check for sync I/O later. */
|
---|
4742 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
4743 | VD_IS_LOCKED(pDisk);
|
---|
4744 |
|
---|
4745 | Assert(cbRead > 0);
|
---|
4746 |
|
---|
4747 | if (pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC)
|
---|
4748 | {
|
---|
4749 | RTSGSEG Seg;
|
---|
4750 | unsigned cSegments = 1;
|
---|
4751 | size_t cbTaskRead = 0;
|
---|
4752 |
|
---|
4753 | /* Synchronous I/O contexts only have one buffer segment. */
|
---|
4754 | AssertMsgReturn(pIoCtx->Req.Io.SgBuf.cSegs == 1,
|
---|
4755 | ("Invalid number of buffer segments for synchronous I/O context"),
|
---|
4756 | VERR_INVALID_PARAMETER);
|
---|
4757 |
|
---|
4758 | cbTaskRead = RTSgBufSegArrayCreate(&pIoCtx->Req.Io.SgBuf, &Seg, &cSegments, cbRead);
|
---|
4759 | Assert(cbRead == cbTaskRead);
|
---|
4760 | Assert(cSegments == 1);
|
---|
4761 | rc = pVDIo->pInterfaceIo->pfnReadSync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4762 | pIoStorage->pStorage, uOffset,
|
---|
4763 | Seg.pvSeg, cbRead, NULL);
|
---|
4764 | if (RT_SUCCESS(rc))
|
---|
4765 | {
|
---|
4766 | Assert(cbRead == (uint32_t)cbRead);
|
---|
4767 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbRead);
|
---|
4768 | }
|
---|
4769 | }
|
---|
4770 | else
|
---|
4771 | {
|
---|
4772 | /* Build the S/G array and spawn a new I/O task */
|
---|
4773 | while (cbRead)
|
---|
4774 | {
|
---|
4775 | RTSGSEG aSeg[VD_IO_TASK_SEGMENTS_MAX];
|
---|
4776 | unsigned cSegments = VD_IO_TASK_SEGMENTS_MAX;
|
---|
4777 | size_t cbTaskRead = RTSgBufSegArrayCreate(&pIoCtx->Req.Io.SgBuf, aSeg, &cSegments, cbRead);
|
---|
4778 |
|
---|
4779 | Assert(cSegments > 0);
|
---|
4780 | Assert(cbTaskRead > 0);
|
---|
4781 | AssertMsg(cbTaskRead <= cbRead, ("Invalid number of bytes to read\n"));
|
---|
4782 |
|
---|
4783 | LogFlow(("Reading %u bytes into %u segments\n", cbTaskRead, cSegments));
|
---|
4784 |
|
---|
4785 | #ifdef RT_STRICT
|
---|
4786 | for (unsigned i = 0; i < cSegments; i++)
|
---|
4787 | AssertMsg(aSeg[i].pvSeg && !(aSeg[i].cbSeg % 512),
|
---|
4788 | ("Segment %u is invalid\n", i));
|
---|
4789 | #endif
|
---|
4790 |
|
---|
4791 | Assert(cbTaskRead == (uint32_t)cbTaskRead);
|
---|
4792 | PVDIOTASK pIoTask = vdIoTaskUserAlloc(pIoStorage, NULL, NULL, pIoCtx, (uint32_t)cbTaskRead);
|
---|
4793 |
|
---|
4794 | if (!pIoTask)
|
---|
4795 | return VERR_NO_MEMORY;
|
---|
4796 |
|
---|
4797 | ASMAtomicIncU32(&pIoCtx->cDataTransfersPending);
|
---|
4798 |
|
---|
4799 | void *pvTask;
|
---|
4800 | Log(("Spawning pIoTask=%p pIoCtx=%p\n", pIoTask, pIoCtx));
|
---|
4801 | rc = pVDIo->pInterfaceIo->pfnReadAsync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4802 | pIoStorage->pStorage, uOffset,
|
---|
4803 | aSeg, cSegments, cbTaskRead, pIoTask,
|
---|
4804 | &pvTask);
|
---|
4805 | if (RT_SUCCESS(rc))
|
---|
4806 | {
|
---|
4807 | AssertMsg(cbTaskRead <= pIoCtx->Req.Io.cbTransferLeft, ("Impossible!\n"));
|
---|
4808 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbTaskRead);
|
---|
4809 | ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
|
---|
4810 | vdIoTaskFree(pDisk, pIoTask);
|
---|
4811 | }
|
---|
4812 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
4813 | {
|
---|
4814 | ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
|
---|
4815 | vdIoTaskFree(pDisk, pIoTask);
|
---|
4816 | break;
|
---|
4817 | }
|
---|
4818 |
|
---|
4819 | uOffset += cbTaskRead;
|
---|
4820 | cbRead -= cbTaskRead;
|
---|
4821 | }
|
---|
4822 | }
|
---|
4823 |
|
---|
4824 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
4825 | return rc;
|
---|
4826 | }
|
---|
4827 |
|
---|
4828 | static DECLCALLBACK(int) vdIOIntWriteUser(void *pvUser, PVDIOSTORAGE pIoStorage, uint64_t uOffset,
|
---|
4829 | PVDIOCTX pIoCtx, size_t cbWrite, PFNVDXFERCOMPLETED pfnComplete,
|
---|
4830 | void *pvCompleteUser)
|
---|
4831 | {
|
---|
4832 | int rc = VINF_SUCCESS;
|
---|
4833 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4834 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
4835 |
|
---|
4836 | LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pIoCtx=%#p cbWrite=%u\n",
|
---|
4837 | pvUser, pIoStorage, uOffset, pIoCtx, cbWrite));
|
---|
4838 |
|
---|
4839 | /** @todo Enable check for sync I/O later. */
|
---|
4840 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
4841 | VD_IS_LOCKED(pDisk);
|
---|
4842 |
|
---|
4843 | Assert(cbWrite > 0);
|
---|
4844 |
|
---|
4845 | if (pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC)
|
---|
4846 | {
|
---|
4847 | RTSGSEG Seg;
|
---|
4848 | unsigned cSegments = 1;
|
---|
4849 | size_t cbTaskWrite = 0;
|
---|
4850 |
|
---|
4851 | /* Synchronous I/O contexts only have one buffer segment. */
|
---|
4852 | AssertMsgReturn(pIoCtx->Req.Io.SgBuf.cSegs == 1,
|
---|
4853 | ("Invalid number of buffer segments for synchronous I/O context"),
|
---|
4854 | VERR_INVALID_PARAMETER);
|
---|
4855 |
|
---|
4856 | cbTaskWrite = RTSgBufSegArrayCreate(&pIoCtx->Req.Io.SgBuf, &Seg, &cSegments, cbWrite);
|
---|
4857 | Assert(cbWrite == cbTaskWrite);
|
---|
4858 | Assert(cSegments == 1);
|
---|
4859 | rc = pVDIo->pInterfaceIo->pfnWriteSync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4860 | pIoStorage->pStorage, uOffset,
|
---|
4861 | Seg.pvSeg, cbWrite, NULL);
|
---|
4862 | if (RT_SUCCESS(rc))
|
---|
4863 | {
|
---|
4864 | Assert(pIoCtx->Req.Io.cbTransferLeft >= cbWrite);
|
---|
4865 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbWrite);
|
---|
4866 | }
|
---|
4867 | }
|
---|
4868 | else
|
---|
4869 | {
|
---|
4870 | /* Build the S/G array and spawn a new I/O task */
|
---|
4871 | while (cbWrite)
|
---|
4872 | {
|
---|
4873 | RTSGSEG aSeg[VD_IO_TASK_SEGMENTS_MAX];
|
---|
4874 | unsigned cSegments = VD_IO_TASK_SEGMENTS_MAX;
|
---|
4875 | size_t cbTaskWrite = 0;
|
---|
4876 |
|
---|
4877 | cbTaskWrite = RTSgBufSegArrayCreate(&pIoCtx->Req.Io.SgBuf, aSeg, &cSegments, cbWrite);
|
---|
4878 |
|
---|
4879 | Assert(cSegments > 0);
|
---|
4880 | Assert(cbTaskWrite > 0);
|
---|
4881 | AssertMsg(cbTaskWrite <= cbWrite, ("Invalid number of bytes to write\n"));
|
---|
4882 |
|
---|
4883 | LogFlow(("Writing %u bytes from %u segments\n", cbTaskWrite, cSegments));
|
---|
4884 |
|
---|
4885 | #ifdef DEBUG
|
---|
4886 | for (unsigned i = 0; i < cSegments; i++)
|
---|
4887 | AssertMsg(aSeg[i].pvSeg && !(aSeg[i].cbSeg % 512),
|
---|
4888 | ("Segment %u is invalid\n", i));
|
---|
4889 | #endif
|
---|
4890 |
|
---|
4891 | Assert(cbTaskWrite == (uint32_t)cbTaskWrite);
|
---|
4892 | PVDIOTASK pIoTask = vdIoTaskUserAlloc(pIoStorage, pfnComplete, pvCompleteUser, pIoCtx, (uint32_t)cbTaskWrite);
|
---|
4893 |
|
---|
4894 | if (!pIoTask)
|
---|
4895 | return VERR_NO_MEMORY;
|
---|
4896 |
|
---|
4897 | ASMAtomicIncU32(&pIoCtx->cDataTransfersPending);
|
---|
4898 |
|
---|
4899 | void *pvTask;
|
---|
4900 | Log(("Spawning pIoTask=%p pIoCtx=%p\n", pIoTask, pIoCtx));
|
---|
4901 | rc = pVDIo->pInterfaceIo->pfnWriteAsync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4902 | pIoStorage->pStorage,
|
---|
4903 | uOffset, aSeg, cSegments,
|
---|
4904 | cbTaskWrite, pIoTask, &pvTask);
|
---|
4905 | if (RT_SUCCESS(rc))
|
---|
4906 | {
|
---|
4907 | AssertMsg(cbTaskWrite <= pIoCtx->Req.Io.cbTransferLeft, ("Impossible!\n"));
|
---|
4908 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbTaskWrite);
|
---|
4909 | ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
|
---|
4910 | vdIoTaskFree(pDisk, pIoTask);
|
---|
4911 | }
|
---|
4912 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
4913 | {
|
---|
4914 | ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
|
---|
4915 | vdIoTaskFree(pDisk, pIoTask);
|
---|
4916 | break;
|
---|
4917 | }
|
---|
4918 |
|
---|
4919 | uOffset += cbTaskWrite;
|
---|
4920 | cbWrite -= cbTaskWrite;
|
---|
4921 | }
|
---|
4922 | }
|
---|
4923 |
|
---|
4924 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
4925 | return rc;
|
---|
4926 | }
|
---|
4927 |
|
---|
4928 | static DECLCALLBACK(int) vdIOIntReadMeta(void *pvUser, PVDIOSTORAGE pIoStorage, uint64_t uOffset,
|
---|
4929 | void *pvBuf, size_t cbRead, PVDIOCTX pIoCtx,
|
---|
4930 | PPVDMETAXFER ppMetaXfer, PFNVDXFERCOMPLETED pfnComplete,
|
---|
4931 | void *pvCompleteUser)
|
---|
4932 | {
|
---|
4933 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4934 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
4935 | int rc = VINF_SUCCESS;
|
---|
4936 | RTSGSEG Seg;
|
---|
4937 | PVDIOTASK pIoTask;
|
---|
4938 | PVDMETAXFER pMetaXfer = NULL;
|
---|
4939 | void *pvTask = NULL;
|
---|
4940 |
|
---|
4941 | LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pvBuf=%#p cbRead=%u\n",
|
---|
4942 | pvUser, pIoStorage, uOffset, pvBuf, cbRead));
|
---|
4943 |
|
---|
4944 | AssertMsgReturn( pIoCtx
|
---|
4945 | || (!ppMetaXfer && !pfnComplete && !pvCompleteUser),
|
---|
4946 | ("A synchronous metadata read is requested but the parameters are wrong\n"),
|
---|
4947 | VERR_INVALID_POINTER);
|
---|
4948 |
|
---|
4949 | /** @todo Enable check for sync I/O later. */
|
---|
4950 | if ( pIoCtx
|
---|
4951 | && !(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
4952 | VD_IS_LOCKED(pDisk);
|
---|
4953 |
|
---|
4954 | if ( !pIoCtx
|
---|
4955 | || pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC)
|
---|
4956 | {
|
---|
4957 | /* Handle synchronous metadata I/O. */
|
---|
4958 | /** @todo Integrate with metadata transfers below. */
|
---|
4959 | rc = pVDIo->pInterfaceIo->pfnReadSync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4960 | pIoStorage->pStorage, uOffset,
|
---|
4961 | pvBuf, cbRead, NULL);
|
---|
4962 | if (ppMetaXfer)
|
---|
4963 | *ppMetaXfer = NULL;
|
---|
4964 | }
|
---|
4965 | else
|
---|
4966 | {
|
---|
4967 | pMetaXfer = (PVDMETAXFER)RTAvlrFileOffsetGet(pIoStorage->pTreeMetaXfers, uOffset);
|
---|
4968 | if (!pMetaXfer)
|
---|
4969 | {
|
---|
4970 | #ifdef RT_STRICT
|
---|
4971 | pMetaXfer = (PVDMETAXFER)RTAvlrFileOffsetGetBestFit(pIoStorage->pTreeMetaXfers, uOffset, false /* fAbove */);
|
---|
4972 | AssertMsg(!pMetaXfer || (pMetaXfer->Core.Key + (RTFOFF)pMetaXfer->cbMeta <= (RTFOFF)uOffset),
|
---|
4973 | ("Overlapping meta transfers!\n"));
|
---|
4974 | #endif
|
---|
4975 |
|
---|
4976 | /* Allocate a new meta transfer. */
|
---|
4977 | pMetaXfer = vdMetaXferAlloc(pIoStorage, uOffset, cbRead);
|
---|
4978 | if (!pMetaXfer)
|
---|
4979 | return VERR_NO_MEMORY;
|
---|
4980 |
|
---|
4981 | pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvCompleteUser, pMetaXfer);
|
---|
4982 | if (!pIoTask)
|
---|
4983 | {
|
---|
4984 | RTMemFree(pMetaXfer);
|
---|
4985 | return VERR_NO_MEMORY;
|
---|
4986 | }
|
---|
4987 |
|
---|
4988 | Seg.cbSeg = cbRead;
|
---|
4989 | Seg.pvSeg = pMetaXfer->abData;
|
---|
4990 |
|
---|
4991 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_READ);
|
---|
4992 | rc = pVDIo->pInterfaceIo->pfnReadAsync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4993 | pIoStorage->pStorage,
|
---|
4994 | uOffset, &Seg, 1,
|
---|
4995 | cbRead, pIoTask, &pvTask);
|
---|
4996 |
|
---|
4997 | if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
4998 | {
|
---|
4999 | bool fInserted = RTAvlrFileOffsetInsert(pIoStorage->pTreeMetaXfers, &pMetaXfer->Core);
|
---|
5000 | Assert(fInserted); NOREF(fInserted);
|
---|
5001 | }
|
---|
5002 | else
|
---|
5003 | RTMemFree(pMetaXfer);
|
---|
5004 |
|
---|
5005 | if (RT_SUCCESS(rc))
|
---|
5006 | {
|
---|
5007 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
|
---|
5008 | vdIoTaskFree(pDisk, pIoTask);
|
---|
5009 | }
|
---|
5010 | else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS && !pfnComplete)
|
---|
5011 | rc = VERR_VD_NOT_ENOUGH_METADATA;
|
---|
5012 | }
|
---|
5013 |
|
---|
5014 | Assert(VALID_PTR(pMetaXfer) || RT_FAILURE(rc));
|
---|
5015 |
|
---|
5016 | if (RT_SUCCESS(rc) || rc == VERR_VD_NOT_ENOUGH_METADATA || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
5017 | {
|
---|
5018 | /* If it is pending add the request to the list. */
|
---|
5019 | if (VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_READ)
|
---|
5020 | {
|
---|
5021 | PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
|
---|
5022 | AssertPtr(pDeferred);
|
---|
5023 |
|
---|
5024 | RTListInit(&pDeferred->NodeDeferred);
|
---|
5025 | pDeferred->pIoCtx = pIoCtx;
|
---|
5026 |
|
---|
5027 | ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
|
---|
5028 | RTListAppend(&pMetaXfer->ListIoCtxWaiting, &pDeferred->NodeDeferred);
|
---|
5029 | rc = VERR_VD_NOT_ENOUGH_METADATA;
|
---|
5030 | }
|
---|
5031 | else
|
---|
5032 | {
|
---|
5033 | /* Transfer the data. */
|
---|
5034 | pMetaXfer->cRefs++;
|
---|
5035 | Assert(pMetaXfer->cbMeta >= cbRead);
|
---|
5036 | Assert(pMetaXfer->Core.Key == (RTFOFF)uOffset);
|
---|
5037 | if (pMetaXfer->pbDataShw)
|
---|
5038 | memcpy(pvBuf, pMetaXfer->pbDataShw, cbRead);
|
---|
5039 | else
|
---|
5040 | memcpy(pvBuf, pMetaXfer->abData, cbRead);
|
---|
5041 | *ppMetaXfer = pMetaXfer;
|
---|
5042 | }
|
---|
5043 | }
|
---|
5044 | }
|
---|
5045 |
|
---|
5046 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
5047 | return rc;
|
---|
5048 | }
|
---|
5049 |
|
---|
5050 | static DECLCALLBACK(int) vdIOIntWriteMeta(void *pvUser, PVDIOSTORAGE pIoStorage, uint64_t uOffset,
|
---|
5051 | const void *pvBuf, size_t cbWrite, PVDIOCTX pIoCtx,
|
---|
5052 | PFNVDXFERCOMPLETED pfnComplete, void *pvCompleteUser)
|
---|
5053 | {
|
---|
5054 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
5055 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
5056 | int rc = VINF_SUCCESS;
|
---|
5057 | RTSGSEG Seg;
|
---|
5058 | PVDIOTASK pIoTask;
|
---|
5059 | PVDMETAXFER pMetaXfer = NULL;
|
---|
5060 | bool fInTree = false;
|
---|
5061 | void *pvTask = NULL;
|
---|
5062 |
|
---|
5063 | LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pvBuf=%#p cbWrite=%u\n",
|
---|
5064 | pvUser, pIoStorage, uOffset, pvBuf, cbWrite));
|
---|
5065 |
|
---|
5066 | AssertMsgReturn( pIoCtx
|
---|
5067 | || (!pfnComplete && !pvCompleteUser),
|
---|
5068 | ("A synchronous metadata write is requested but the parameters are wrong\n"),
|
---|
5069 | VERR_INVALID_POINTER);
|
---|
5070 |
|
---|
5071 | /** @todo Enable check for sync I/O later. */
|
---|
5072 | if ( pIoCtx
|
---|
5073 | && !(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
5074 | VD_IS_LOCKED(pDisk);
|
---|
5075 |
|
---|
5076 | if ( !pIoCtx
|
---|
5077 | || pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC)
|
---|
5078 | {
|
---|
5079 | /* Handle synchronous metadata I/O. */
|
---|
5080 | /** @todo Integrate with metadata transfers below. */
|
---|
5081 | rc = pVDIo->pInterfaceIo->pfnWriteSync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
5082 | pIoStorage->pStorage, uOffset,
|
---|
5083 | pvBuf, cbWrite, NULL);
|
---|
5084 | }
|
---|
5085 | else
|
---|
5086 | {
|
---|
5087 | pMetaXfer = (PVDMETAXFER)RTAvlrFileOffsetGet(pIoStorage->pTreeMetaXfers, uOffset);
|
---|
5088 | if (!pMetaXfer)
|
---|
5089 | {
|
---|
5090 | /* Allocate a new meta transfer. */
|
---|
5091 | pMetaXfer = vdMetaXferAlloc(pIoStorage, uOffset, cbWrite);
|
---|
5092 | if (!pMetaXfer)
|
---|
5093 | return VERR_NO_MEMORY;
|
---|
5094 | }
|
---|
5095 | else
|
---|
5096 | {
|
---|
5097 | Assert(pMetaXfer->cbMeta >= cbWrite);
|
---|
5098 | Assert(pMetaXfer->Core.Key == (RTFOFF)uOffset);
|
---|
5099 | fInTree = true;
|
---|
5100 | }
|
---|
5101 |
|
---|
5102 | if (VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE)
|
---|
5103 | {
|
---|
5104 | pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvCompleteUser, pMetaXfer);
|
---|
5105 | if (!pIoTask)
|
---|
5106 | {
|
---|
5107 | RTMemFree(pMetaXfer);
|
---|
5108 | return VERR_NO_MEMORY;
|
---|
5109 | }
|
---|
5110 |
|
---|
5111 | memcpy(pMetaXfer->abData, pvBuf, cbWrite);
|
---|
5112 | Seg.cbSeg = cbWrite;
|
---|
5113 | Seg.pvSeg = pMetaXfer->abData;
|
---|
5114 |
|
---|
5115 | ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
|
---|
5116 |
|
---|
5117 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_WRITE);
|
---|
5118 | rc = pVDIo->pInterfaceIo->pfnWriteAsync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
5119 | pIoStorage->pStorage,
|
---|
5120 | uOffset, &Seg, 1, cbWrite, pIoTask,
|
---|
5121 | &pvTask);
|
---|
5122 | if (RT_SUCCESS(rc))
|
---|
5123 | {
|
---|
5124 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
|
---|
5125 | ASMAtomicDecU32(&pIoCtx->cMetaTransfersPending);
|
---|
5126 | vdIoTaskFree(pDisk, pIoTask);
|
---|
5127 | if (fInTree && !pMetaXfer->cRefs)
|
---|
5128 | {
|
---|
5129 | LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
|
---|
5130 | bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
|
---|
5131 | AssertMsg(fRemoved, ("Metadata transfer wasn't removed\n")); NOREF(fRemoved);
|
---|
5132 | RTMemFree(pMetaXfer);
|
---|
5133 | pMetaXfer = NULL;
|
---|
5134 | }
|
---|
5135 | }
|
---|
5136 | else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
5137 | {
|
---|
5138 | PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
|
---|
5139 | AssertPtr(pDeferred);
|
---|
5140 |
|
---|
5141 | RTListInit(&pDeferred->NodeDeferred);
|
---|
5142 | pDeferred->pIoCtx = pIoCtx;
|
---|
5143 |
|
---|
5144 | if (!fInTree)
|
---|
5145 | {
|
---|
5146 | bool fInserted = RTAvlrFileOffsetInsert(pIoStorage->pTreeMetaXfers, &pMetaXfer->Core);
|
---|
5147 | Assert(fInserted); NOREF(fInserted);
|
---|
5148 | }
|
---|
5149 |
|
---|
5150 | RTListAppend(&pMetaXfer->ListIoCtxWaiting, &pDeferred->NodeDeferred);
|
---|
5151 | }
|
---|
5152 | else
|
---|
5153 | {
|
---|
5154 | RTMemFree(pMetaXfer);
|
---|
5155 | pMetaXfer = NULL;
|
---|
5156 | }
|
---|
5157 | }
|
---|
5158 | else
|
---|
5159 | {
|
---|
5160 | /* I/O is in progress, update shadow buffer and add to waiting list. */
|
---|
5161 | Assert(VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_WRITE);
|
---|
5162 | if (!pMetaXfer->pbDataShw)
|
---|
5163 | {
|
---|
5164 | /* Allocate shadow buffer and set initial state. */
|
---|
5165 | LogFlowFunc(("pMetaXfer=%#p Creating shadow buffer\n", pMetaXfer));
|
---|
5166 | pMetaXfer->pbDataShw = (uint8_t *)RTMemAlloc(pMetaXfer->cbMeta);
|
---|
5167 | if (RT_LIKELY(pMetaXfer->pbDataShw))
|
---|
5168 | memcpy(pMetaXfer->pbDataShw, pMetaXfer->abData, pMetaXfer->cbMeta);
|
---|
5169 | else
|
---|
5170 | rc = VERR_NO_MEMORY;
|
---|
5171 | }
|
---|
5172 |
|
---|
5173 | if (RT_SUCCESS(rc))
|
---|
5174 | {
|
---|
5175 | /* Update with written data and append to waiting list. */
|
---|
5176 | PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
|
---|
5177 | if (pDeferred)
|
---|
5178 | {
|
---|
5179 | LogFlowFunc(("pMetaXfer=%#p Updating shadow buffer\n", pMetaXfer));
|
---|
5180 |
|
---|
5181 | RTListInit(&pDeferred->NodeDeferred);
|
---|
5182 | pDeferred->pIoCtx = pIoCtx;
|
---|
5183 | ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
|
---|
5184 | memcpy(pMetaXfer->pbDataShw, pvBuf, cbWrite);
|
---|
5185 | RTListAppend(&pMetaXfer->ListIoCtxShwWrites, &pDeferred->NodeDeferred);
|
---|
5186 | }
|
---|
5187 | else
|
---|
5188 | {
|
---|
5189 | /*
|
---|
5190 | * Free shadow buffer if there is no one depending on it, i.e.
|
---|
5191 | * we just allocated it.
|
---|
5192 | */
|
---|
5193 | if (RTListIsEmpty(&pMetaXfer->ListIoCtxShwWrites))
|
---|
5194 | {
|
---|
5195 | RTMemFree(pMetaXfer->pbDataShw);
|
---|
5196 | pMetaXfer->pbDataShw = NULL;
|
---|
5197 | }
|
---|
5198 | rc = VERR_NO_MEMORY;
|
---|
5199 | }
|
---|
5200 | }
|
---|
5201 | }
|
---|
5202 | }
|
---|
5203 |
|
---|
5204 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
5205 | return rc;
|
---|
5206 | }
|
---|
5207 |
|
---|
5208 | static DECLCALLBACK(void) vdIOIntMetaXferRelease(void *pvUser, PVDMETAXFER pMetaXfer)
|
---|
5209 | {
|
---|
5210 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
5211 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
5212 | PVDIOSTORAGE pIoStorage;
|
---|
5213 |
|
---|
5214 | /*
|
---|
5215 | * It is possible that we get called with a NULL metadata xfer handle
|
---|
5216 | * for synchronous I/O. Just exit.
|
---|
5217 | */
|
---|
5218 | if (!pMetaXfer)
|
---|
5219 | return;
|
---|
5220 |
|
---|
5221 | pIoStorage = pMetaXfer->pIoStorage;
|
---|
5222 |
|
---|
5223 | VD_IS_LOCKED(pDisk);
|
---|
5224 |
|
---|
5225 | Assert( VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE
|
---|
5226 | || VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_WRITE);
|
---|
5227 | Assert(pMetaXfer->cRefs > 0);
|
---|
5228 |
|
---|
5229 | pMetaXfer->cRefs--;
|
---|
5230 | if ( !pMetaXfer->cRefs
|
---|
5231 | && RTListIsEmpty(&pMetaXfer->ListIoCtxWaiting)
|
---|
5232 | && VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE)
|
---|
5233 | {
|
---|
5234 | /* Free the meta data entry. */
|
---|
5235 | LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
|
---|
5236 | bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
|
---|
5237 | AssertMsg(fRemoved, ("Metadata transfer wasn't removed\n")); NOREF(fRemoved);
|
---|
5238 |
|
---|
5239 | RTMemFree(pMetaXfer);
|
---|
5240 | }
|
---|
5241 | }
|
---|
5242 |
|
---|
5243 | static DECLCALLBACK(int) vdIOIntFlush(void *pvUser, PVDIOSTORAGE pIoStorage, PVDIOCTX pIoCtx,
|
---|
5244 | PFNVDXFERCOMPLETED pfnComplete, void *pvCompleteUser)
|
---|
5245 | {
|
---|
5246 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
5247 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
5248 | int rc = VINF_SUCCESS;
|
---|
5249 | PVDIOTASK pIoTask;
|
---|
5250 | PVDMETAXFER pMetaXfer = NULL;
|
---|
5251 | void *pvTask = NULL;
|
---|
5252 |
|
---|
5253 | LogFlowFunc(("pvUser=%#p pIoStorage=%#p pIoCtx=%#p\n",
|
---|
5254 | pvUser, pIoStorage, pIoCtx));
|
---|
5255 |
|
---|
5256 | AssertMsgReturn( pIoCtx
|
---|
5257 | || (!pfnComplete && !pvCompleteUser),
|
---|
5258 | ("A synchronous metadata write is requested but the parameters are wrong\n"),
|
---|
5259 | VERR_INVALID_POINTER);
|
---|
5260 |
|
---|
5261 | /** @todo Enable check for sync I/O later. */
|
---|
5262 | if ( pIoCtx
|
---|
5263 | && !(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
5264 | VD_IS_LOCKED(pDisk);
|
---|
5265 |
|
---|
5266 | if (pVDIo->fIgnoreFlush)
|
---|
5267 | return VINF_SUCCESS;
|
---|
5268 |
|
---|
5269 | if ( !pIoCtx
|
---|
5270 | || pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC)
|
---|
5271 | {
|
---|
5272 | /* Handle synchronous flushes. */
|
---|
5273 | /** @todo Integrate with metadata transfers below. */
|
---|
5274 | rc = pVDIo->pInterfaceIo->pfnFlushSync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
5275 | pIoStorage->pStorage);
|
---|
5276 | }
|
---|
5277 | else
|
---|
5278 | {
|
---|
5279 | /* Allocate a new meta transfer. */
|
---|
5280 | pMetaXfer = vdMetaXferAlloc(pIoStorage, 0, 0);
|
---|
5281 | if (!pMetaXfer)
|
---|
5282 | return VERR_NO_MEMORY;
|
---|
5283 |
|
---|
5284 | pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvUser, pMetaXfer);
|
---|
5285 | if (!pIoTask)
|
---|
5286 | {
|
---|
5287 | RTMemFree(pMetaXfer);
|
---|
5288 | return VERR_NO_MEMORY;
|
---|
5289 | }
|
---|
5290 |
|
---|
5291 | ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
|
---|
5292 |
|
---|
5293 | PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
|
---|
5294 | AssertPtr(pDeferred);
|
---|
5295 |
|
---|
5296 | RTListInit(&pDeferred->NodeDeferred);
|
---|
5297 | pDeferred->pIoCtx = pIoCtx;
|
---|
5298 |
|
---|
5299 | RTListAppend(&pMetaXfer->ListIoCtxWaiting, &pDeferred->NodeDeferred);
|
---|
5300 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_FLUSH);
|
---|
5301 | rc = pVDIo->pInterfaceIo->pfnFlushAsync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
5302 | pIoStorage->pStorage,
|
---|
5303 | pIoTask, &pvTask);
|
---|
5304 | if (RT_SUCCESS(rc))
|
---|
5305 | {
|
---|
5306 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
|
---|
5307 | ASMAtomicDecU32(&pIoCtx->cMetaTransfersPending);
|
---|
5308 | vdIoTaskFree(pDisk, pIoTask);
|
---|
5309 | RTMemFree(pDeferred);
|
---|
5310 | RTMemFree(pMetaXfer);
|
---|
5311 | }
|
---|
5312 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
5313 | RTMemFree(pMetaXfer);
|
---|
5314 | }
|
---|
5315 |
|
---|
5316 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
5317 | return rc;
|
---|
5318 | }
|
---|
5319 |
|
---|
5320 | static DECLCALLBACK(size_t) vdIOIntIoCtxCopyTo(void *pvUser, PVDIOCTX pIoCtx,
|
---|
5321 | const void *pvBuf, size_t cbBuf)
|
---|
5322 | {
|
---|
5323 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
5324 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
5325 | size_t cbCopied = 0;
|
---|
5326 |
|
---|
5327 | /** @todo Enable check for sync I/O later. */
|
---|
5328 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
5329 | VD_IS_LOCKED(pDisk);
|
---|
5330 |
|
---|
5331 | cbCopied = vdIoCtxCopyTo(pIoCtx, (uint8_t *)pvBuf, cbBuf);
|
---|
5332 | Assert(cbCopied == cbBuf);
|
---|
5333 |
|
---|
5334 | /// @todo Assert(pIoCtx->Req.Io.cbTransferLeft >= cbCopied); - triggers with vdCopyHelper/dmgRead.
|
---|
5335 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbCopied);
|
---|
5336 |
|
---|
5337 | return cbCopied;
|
---|
5338 | }
|
---|
5339 |
|
---|
5340 | static DECLCALLBACK(size_t) vdIOIntIoCtxCopyFrom(void *pvUser, PVDIOCTX pIoCtx,
|
---|
5341 | void *pvBuf, size_t cbBuf)
|
---|
5342 | {
|
---|
5343 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
5344 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
5345 | size_t cbCopied = 0;
|
---|
5346 |
|
---|
5347 | /** @todo Enable check for sync I/O later. */
|
---|
5348 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
5349 | VD_IS_LOCKED(pDisk);
|
---|
5350 |
|
---|
5351 | cbCopied = vdIoCtxCopyFrom(pIoCtx, (uint8_t *)pvBuf, cbBuf);
|
---|
5352 | Assert(cbCopied == cbBuf);
|
---|
5353 |
|
---|
5354 | /// @todo Assert(pIoCtx->Req.Io.cbTransferLeft > cbCopied); - triggers with vdCopyHelper/dmgRead.
|
---|
5355 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbCopied);
|
---|
5356 |
|
---|
5357 | return cbCopied;
|
---|
5358 | }
|
---|
5359 |
|
---|
5360 | static DECLCALLBACK(size_t) vdIOIntIoCtxSet(void *pvUser, PVDIOCTX pIoCtx, int ch, size_t cb)
|
---|
5361 | {
|
---|
5362 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
5363 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
5364 | size_t cbSet = 0;
|
---|
5365 |
|
---|
5366 | /** @todo Enable check for sync I/O later. */
|
---|
5367 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
5368 | VD_IS_LOCKED(pDisk);
|
---|
5369 |
|
---|
5370 | cbSet = vdIoCtxSet(pIoCtx, ch, cb);
|
---|
5371 | Assert(cbSet == cb);
|
---|
5372 |
|
---|
5373 | /// @todo Assert(pIoCtx->Req.Io.cbTransferLeft >= cbSet); - triggers with vdCopyHelper/dmgRead.
|
---|
5374 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbSet);
|
---|
5375 |
|
---|
5376 | return cbSet;
|
---|
5377 | }
|
---|
5378 |
|
---|
5379 | static DECLCALLBACK(size_t) vdIOIntIoCtxSegArrayCreate(void *pvUser, PVDIOCTX pIoCtx,
|
---|
5380 | PRTSGSEG paSeg, unsigned *pcSeg,
|
---|
5381 | size_t cbData)
|
---|
5382 | {
|
---|
5383 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
5384 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
5385 | size_t cbCreated = 0;
|
---|
5386 |
|
---|
5387 | /** @todo It is possible that this gets called from a filter plugin
|
---|
5388 | * outside of the disk lock. Refine assertion or remove completely. */
|
---|
5389 | #if 0
|
---|
5390 | /** @todo Enable check for sync I/O later. */
|
---|
5391 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
5392 | VD_IS_LOCKED(pDisk);
|
---|
5393 | #else
|
---|
5394 | NOREF(pDisk);
|
---|
5395 | #endif
|
---|
5396 |
|
---|
5397 | cbCreated = RTSgBufSegArrayCreate(&pIoCtx->Req.Io.SgBuf, paSeg, pcSeg, cbData);
|
---|
5398 | Assert(!paSeg || cbData == cbCreated);
|
---|
5399 |
|
---|
5400 | return cbCreated;
|
---|
5401 | }
|
---|
5402 |
|
---|
5403 | static DECLCALLBACK(void) vdIOIntIoCtxCompleted(void *pvUser, PVDIOCTX pIoCtx, int rcReq,
|
---|
5404 | size_t cbCompleted)
|
---|
5405 | {
|
---|
5406 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
5407 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
5408 |
|
---|
5409 | LogFlowFunc(("pvUser=%#p pIoCtx=%#p rcReq=%Rrc cbCompleted=%zu\n",
|
---|
5410 | pvUser, pIoCtx, rcReq, cbCompleted));
|
---|
5411 |
|
---|
5412 | /*
|
---|
5413 | * Grab the disk critical section to avoid races with other threads which
|
---|
5414 | * might still modify the I/O context.
|
---|
5415 | * Example is that iSCSI is doing an asynchronous write but calls us already
|
---|
5416 | * while the other thread is still hanging in vdWriteHelperAsync and couldn't update
|
---|
5417 | * the blocked state yet.
|
---|
5418 | * It can overwrite the state to true before we call vdIoCtxContinue and the
|
---|
5419 | * the request would hang indefinite.
|
---|
5420 | */
|
---|
5421 | ASMAtomicCmpXchgS32(&pIoCtx->rcReq, rcReq, VINF_SUCCESS);
|
---|
5422 | Assert(pIoCtx->Req.Io.cbTransferLeft >= cbCompleted);
|
---|
5423 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbCompleted);
|
---|
5424 |
|
---|
5425 | /* Set next transfer function if the current one finished.
|
---|
5426 | * @todo: Find a better way to prevent vdIoCtxContinue from calling the current helper again. */
|
---|
5427 | if (!pIoCtx->Req.Io.cbTransferLeft)
|
---|
5428 | {
|
---|
5429 | pIoCtx->pfnIoCtxTransfer = pIoCtx->pfnIoCtxTransferNext;
|
---|
5430 | pIoCtx->pfnIoCtxTransferNext = NULL;
|
---|
5431 | }
|
---|
5432 |
|
---|
5433 | vdIoCtxAddToWaitingList(&pDisk->pIoCtxHaltedHead, pIoCtx);
|
---|
5434 | if (ASMAtomicCmpXchgBool(&pDisk->fLocked, true, false))
|
---|
5435 | {
|
---|
5436 | /* Immediately drop the lock again, it will take care of processing the list. */
|
---|
5437 | vdDiskUnlock(pDisk, NULL);
|
---|
5438 | }
|
---|
5439 | }
|
---|
5440 |
|
---|
5441 | static DECLCALLBACK(bool) vdIOIntIoCtxIsSynchronous(void *pvUser, PVDIOCTX pIoCtx)
|
---|
5442 | {
|
---|
5443 | NOREF(pvUser);
|
---|
5444 | return !!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC);
|
---|
5445 | }
|
---|
5446 |
|
---|
5447 | static DECLCALLBACK(bool) vdIOIntIoCtxIsZero(void *pvUser, PVDIOCTX pIoCtx, size_t cbCheck,
|
---|
5448 | bool fAdvance)
|
---|
5449 | {
|
---|
5450 | NOREF(pvUser);
|
---|
5451 |
|
---|
5452 | bool fIsZero = RTSgBufIsZero(&pIoCtx->Req.Io.SgBuf, cbCheck);
|
---|
5453 | if (fIsZero && fAdvance)
|
---|
5454 | RTSgBufAdvance(&pIoCtx->Req.Io.SgBuf, cbCheck);
|
---|
5455 |
|
---|
5456 | return fIsZero;
|
---|
5457 | }
|
---|
5458 |
|
---|
5459 | static DECLCALLBACK(size_t) vdIOIntIoCtxGetDataUnitSize(void *pvUser, PVDIOCTX pIoCtx)
|
---|
5460 | {
|
---|
5461 | RT_NOREF1(pIoCtx);
|
---|
5462 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
5463 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
5464 |
|
---|
5465 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, VD_LAST_IMAGE);
|
---|
5466 | AssertPtrReturn(pImage, 0);
|
---|
5467 | return pImage->Backend->pfnGetSectorSize(pImage->pBackendData);
|
---|
5468 | }
|
---|
5469 |
|
---|
5470 | /**
|
---|
5471 | * VD I/O interface callback for opening a file (limited version for VDGetFormat).
|
---|
5472 | */
|
---|
5473 | static DECLCALLBACK(int) vdIOIntOpenLimited(void *pvUser, const char *pszLocation,
|
---|
5474 | uint32_t fOpen, PPVDIOSTORAGE ppIoStorage)
|
---|
5475 | {
|
---|
5476 | int rc = VINF_SUCCESS;
|
---|
5477 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
5478 | PVDIOSTORAGE pIoStorage = (PVDIOSTORAGE)RTMemAllocZ(sizeof(VDIOSTORAGE));
|
---|
5479 |
|
---|
5480 | if (!pIoStorage)
|
---|
5481 | return VERR_NO_MEMORY;
|
---|
5482 |
|
---|
5483 | rc = pInterfaceIo->pfnOpen(NULL, pszLocation, fOpen, NULL, &pIoStorage->pStorage);
|
---|
5484 | if (RT_SUCCESS(rc))
|
---|
5485 | *ppIoStorage = pIoStorage;
|
---|
5486 | else
|
---|
5487 | RTMemFree(pIoStorage);
|
---|
5488 |
|
---|
5489 | return rc;
|
---|
5490 | }
|
---|
5491 |
|
---|
5492 | static DECLCALLBACK(int) vdIOIntCloseLimited(void *pvUser, PVDIOSTORAGE pIoStorage)
|
---|
5493 | {
|
---|
5494 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
5495 | int rc = pInterfaceIo->pfnClose(NULL, pIoStorage->pStorage);
|
---|
5496 |
|
---|
5497 | RTMemFree(pIoStorage);
|
---|
5498 | return rc;
|
---|
5499 | }
|
---|
5500 |
|
---|
5501 | static DECLCALLBACK(int) vdIOIntDeleteLimited(void *pvUser, const char *pcszFilename)
|
---|
5502 | {
|
---|
5503 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
5504 | return pInterfaceIo->pfnDelete(NULL, pcszFilename);
|
---|
5505 | }
|
---|
5506 |
|
---|
5507 | static DECLCALLBACK(int) vdIOIntMoveLimited(void *pvUser, const char *pcszSrc,
|
---|
5508 | const char *pcszDst, unsigned fMove)
|
---|
5509 | {
|
---|
5510 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
5511 | return pInterfaceIo->pfnMove(NULL, pcszSrc, pcszDst, fMove);
|
---|
5512 | }
|
---|
5513 |
|
---|
5514 | static DECLCALLBACK(int) vdIOIntGetFreeSpaceLimited(void *pvUser, const char *pcszFilename,
|
---|
5515 | int64_t *pcbFreeSpace)
|
---|
5516 | {
|
---|
5517 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
5518 | return pInterfaceIo->pfnGetFreeSpace(NULL, pcszFilename, pcbFreeSpace);
|
---|
5519 | }
|
---|
5520 |
|
---|
5521 | static DECLCALLBACK(int) vdIOIntGetModificationTimeLimited(void *pvUser,
|
---|
5522 | const char *pcszFilename,
|
---|
5523 | PRTTIMESPEC pModificationTime)
|
---|
5524 | {
|
---|
5525 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
5526 | return pInterfaceIo->pfnGetModificationTime(NULL, pcszFilename, pModificationTime);
|
---|
5527 | }
|
---|
5528 |
|
---|
5529 | static DECLCALLBACK(int) vdIOIntGetSizeLimited(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
5530 | uint64_t *pcbSize)
|
---|
5531 | {
|
---|
5532 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
5533 | return pInterfaceIo->pfnGetSize(NULL, pIoStorage->pStorage, pcbSize);
|
---|
5534 | }
|
---|
5535 |
|
---|
5536 | static DECLCALLBACK(int) vdIOIntSetSizeLimited(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
5537 | uint64_t cbSize)
|
---|
5538 | {
|
---|
5539 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
5540 | return pInterfaceIo->pfnSetSize(NULL, pIoStorage->pStorage, cbSize);
|
---|
5541 | }
|
---|
5542 |
|
---|
5543 | static DECLCALLBACK(int) vdIOIntWriteUserLimited(void *pvUser, PVDIOSTORAGE pStorage,
|
---|
5544 | uint64_t uOffset, PVDIOCTX pIoCtx,
|
---|
5545 | size_t cbWrite,
|
---|
5546 | PFNVDXFERCOMPLETED pfnComplete,
|
---|
5547 | void *pvCompleteUser)
|
---|
5548 | {
|
---|
5549 | NOREF(pvUser);
|
---|
5550 | NOREF(pStorage);
|
---|
5551 | NOREF(uOffset);
|
---|
5552 | NOREF(pIoCtx);
|
---|
5553 | NOREF(cbWrite);
|
---|
5554 | NOREF(pfnComplete);
|
---|
5555 | NOREF(pvCompleteUser);
|
---|
5556 | AssertMsgFailedReturn(("This needs to be implemented when called\n"), VERR_NOT_IMPLEMENTED);
|
---|
5557 | }
|
---|
5558 |
|
---|
5559 | static DECLCALLBACK(int) vdIOIntReadUserLimited(void *pvUser, PVDIOSTORAGE pStorage,
|
---|
5560 | uint64_t uOffset, PVDIOCTX pIoCtx,
|
---|
5561 | size_t cbRead)
|
---|
5562 | {
|
---|
5563 | NOREF(pvUser);
|
---|
5564 | NOREF(pStorage);
|
---|
5565 | NOREF(uOffset);
|
---|
5566 | NOREF(pIoCtx);
|
---|
5567 | NOREF(cbRead);
|
---|
5568 | AssertMsgFailedReturn(("This needs to be implemented when called\n"), VERR_NOT_IMPLEMENTED);
|
---|
5569 | }
|
---|
5570 |
|
---|
5571 | static DECLCALLBACK(int) vdIOIntWriteMetaLimited(void *pvUser, PVDIOSTORAGE pStorage,
|
---|
5572 | uint64_t uOffset, const void *pvBuffer,
|
---|
5573 | size_t cbBuffer, PVDIOCTX pIoCtx,
|
---|
5574 | PFNVDXFERCOMPLETED pfnComplete,
|
---|
5575 | void *pvCompleteUser)
|
---|
5576 | {
|
---|
5577 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
5578 |
|
---|
5579 | AssertMsgReturn(!pIoCtx && !pfnComplete && !pvCompleteUser,
|
---|
5580 | ("Async I/O not implemented for the limited interface"),
|
---|
5581 | VERR_NOT_SUPPORTED);
|
---|
5582 |
|
---|
5583 | return pInterfaceIo->pfnWriteSync(NULL, pStorage->pStorage, uOffset, pvBuffer, cbBuffer, NULL);
|
---|
5584 | }
|
---|
5585 |
|
---|
5586 | static DECLCALLBACK(int) vdIOIntReadMetaLimited(void *pvUser, PVDIOSTORAGE pStorage,
|
---|
5587 | uint64_t uOffset, void *pvBuffer,
|
---|
5588 | size_t cbBuffer, PVDIOCTX pIoCtx,
|
---|
5589 | PPVDMETAXFER ppMetaXfer,
|
---|
5590 | PFNVDXFERCOMPLETED pfnComplete,
|
---|
5591 | void *pvCompleteUser)
|
---|
5592 | {
|
---|
5593 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
5594 |
|
---|
5595 | AssertMsgReturn(!pIoCtx && !ppMetaXfer && !pfnComplete && !pvCompleteUser,
|
---|
5596 | ("Async I/O not implemented for the limited interface"),
|
---|
5597 | VERR_NOT_SUPPORTED);
|
---|
5598 |
|
---|
5599 | return pInterfaceIo->pfnReadSync(NULL, pStorage->pStorage, uOffset, pvBuffer, cbBuffer, NULL);
|
---|
5600 | }
|
---|
5601 |
|
---|
5602 | #if 0 /* unsed */
|
---|
5603 | static int vdIOIntMetaXferReleaseLimited(void *pvUser, PVDMETAXFER pMetaXfer)
|
---|
5604 | {
|
---|
5605 | /* This is a NOP in this case. */
|
---|
5606 | NOREF(pvUser);
|
---|
5607 | NOREF(pMetaXfer);
|
---|
5608 | return VINF_SUCCESS;
|
---|
5609 | }
|
---|
5610 | #endif
|
---|
5611 |
|
---|
5612 | static DECLCALLBACK(int) vdIOIntFlushLimited(void *pvUser, PVDIOSTORAGE pStorage,
|
---|
5613 | PVDIOCTX pIoCtx,
|
---|
5614 | PFNVDXFERCOMPLETED pfnComplete,
|
---|
5615 | void *pvCompleteUser)
|
---|
5616 | {
|
---|
5617 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
5618 |
|
---|
5619 | AssertMsgReturn(!pIoCtx && !pfnComplete && !pvCompleteUser,
|
---|
5620 | ("Async I/O not implemented for the limited interface"),
|
---|
5621 | VERR_NOT_SUPPORTED);
|
---|
5622 |
|
---|
5623 | return pInterfaceIo->pfnFlushSync(NULL, pStorage->pStorage);
|
---|
5624 | }
|
---|
5625 |
|
---|
5626 | /**
|
---|
5627 | * internal: send output to the log (unconditionally).
|
---|
5628 | */
|
---|
5629 | static DECLCALLBACK(int) vdLogMessage(void *pvUser, const char *pszFormat, va_list args)
|
---|
5630 | {
|
---|
5631 | NOREF(pvUser);
|
---|
5632 | RTLogPrintfV(pszFormat, args);
|
---|
5633 | return VINF_SUCCESS;
|
---|
5634 | }
|
---|
5635 |
|
---|
5636 | DECLINLINE(int) vdMessageWrapper(PVBOXHDD pDisk, const char *pszFormat, ...)
|
---|
5637 | {
|
---|
5638 | va_list va;
|
---|
5639 | va_start(va, pszFormat);
|
---|
5640 | int rc = pDisk->pInterfaceError->pfnMessage(pDisk->pInterfaceError->Core.pvUser,
|
---|
5641 | pszFormat, va);
|
---|
5642 | va_end(va);
|
---|
5643 | return rc;
|
---|
5644 | }
|
---|
5645 |
|
---|
5646 |
|
---|
5647 | /**
|
---|
5648 | * internal: adjust PCHS geometry
|
---|
5649 | */
|
---|
5650 | static void vdFixupPCHSGeometry(PVDGEOMETRY pPCHS, uint64_t cbSize)
|
---|
5651 | {
|
---|
5652 | /* Fix broken PCHS geometry. Can happen for two reasons: either the backend
|
---|
5653 | * mixes up PCHS and LCHS, or the application used to create the source
|
---|
5654 | * image has put garbage in it. Additionally, if the PCHS geometry covers
|
---|
5655 | * more than the image size, set it back to the default. */
|
---|
5656 | if ( pPCHS->cHeads > 16
|
---|
5657 | || pPCHS->cSectors > 63
|
---|
5658 | || pPCHS->cCylinders == 0
|
---|
5659 | || (uint64_t)pPCHS->cHeads * pPCHS->cSectors * pPCHS->cCylinders * 512 > cbSize)
|
---|
5660 | {
|
---|
5661 | Assert(!(RT_MIN(cbSize / 512 / 16 / 63, 16383) - (uint32_t)RT_MIN(cbSize / 512 / 16 / 63, 16383)));
|
---|
5662 | pPCHS->cCylinders = (uint32_t)RT_MIN(cbSize / 512 / 16 / 63, 16383);
|
---|
5663 | pPCHS->cHeads = 16;
|
---|
5664 | pPCHS->cSectors = 63;
|
---|
5665 | }
|
---|
5666 | }
|
---|
5667 |
|
---|
5668 | /**
|
---|
5669 | * internal: adjust PCHS geometry
|
---|
5670 | */
|
---|
5671 | static void vdFixupLCHSGeometry(PVDGEOMETRY pLCHS, uint64_t cbSize)
|
---|
5672 | {
|
---|
5673 | /* Fix broken LCHS geometry. Can happen for two reasons: either the backend
|
---|
5674 | * mixes up PCHS and LCHS, or the application used to create the source
|
---|
5675 | * image has put garbage in it. The fix in this case is to clear the LCHS
|
---|
5676 | * geometry to trigger autodetection when it is used next. If the geometry
|
---|
5677 | * already says "please autodetect" (cylinders=0) keep it. */
|
---|
5678 | if ( ( pLCHS->cHeads > 255
|
---|
5679 | || pLCHS->cHeads == 0
|
---|
5680 | || pLCHS->cSectors > 63
|
---|
5681 | || pLCHS->cSectors == 0)
|
---|
5682 | && pLCHS->cCylinders != 0)
|
---|
5683 | {
|
---|
5684 | pLCHS->cCylinders = 0;
|
---|
5685 | pLCHS->cHeads = 0;
|
---|
5686 | pLCHS->cSectors = 0;
|
---|
5687 | }
|
---|
5688 | /* Always recompute the number of cylinders stored in the LCHS
|
---|
5689 | * geometry if it isn't set to "autotedetect" at the moment.
|
---|
5690 | * This is very useful if the destination image size is
|
---|
5691 | * larger or smaller than the source image size. Do not modify
|
---|
5692 | * the number of heads and sectors. Windows guests hate it. */
|
---|
5693 | if ( pLCHS->cCylinders != 0
|
---|
5694 | && pLCHS->cHeads != 0 /* paranoia */
|
---|
5695 | && pLCHS->cSectors != 0 /* paranoia */)
|
---|
5696 | {
|
---|
5697 | Assert(!(RT_MIN(cbSize / 512 / pLCHS->cHeads / pLCHS->cSectors, 1024) - (uint32_t)RT_MIN(cbSize / 512 / pLCHS->cHeads / pLCHS->cSectors, 1024)));
|
---|
5698 | pLCHS->cCylinders = (uint32_t)RT_MIN(cbSize / 512 / pLCHS->cHeads / pLCHS->cSectors, 1024);
|
---|
5699 | }
|
---|
5700 | }
|
---|
5701 |
|
---|
5702 | /**
|
---|
5703 | * Sets the I/O callbacks of the given interface to the fallback methods
|
---|
5704 | *
|
---|
5705 | * @returns nothing.
|
---|
5706 | * @param pIfIo The I/O interface to setup.
|
---|
5707 | */
|
---|
5708 | static void vdIfIoFallbackCallbacksSetup(PVDINTERFACEIO pIfIo)
|
---|
5709 | {
|
---|
5710 | pIfIo->pfnOpen = vdIOOpenFallback;
|
---|
5711 | pIfIo->pfnClose = vdIOCloseFallback;
|
---|
5712 | pIfIo->pfnDelete = vdIODeleteFallback;
|
---|
5713 | pIfIo->pfnMove = vdIOMoveFallback;
|
---|
5714 | pIfIo->pfnGetFreeSpace = vdIOGetFreeSpaceFallback;
|
---|
5715 | pIfIo->pfnGetModificationTime = vdIOGetModificationTimeFallback;
|
---|
5716 | pIfIo->pfnGetSize = vdIOGetSizeFallback;
|
---|
5717 | pIfIo->pfnSetSize = vdIOSetSizeFallback;
|
---|
5718 | pIfIo->pfnSetAllocationSize = vdIOSetAllocationSizeFallback;
|
---|
5719 | pIfIo->pfnReadSync = vdIOReadSyncFallback;
|
---|
5720 | pIfIo->pfnWriteSync = vdIOWriteSyncFallback;
|
---|
5721 | pIfIo->pfnFlushSync = vdIOFlushSyncFallback;
|
---|
5722 | pIfIo->pfnReadAsync = vdIOReadAsyncFallback;
|
---|
5723 | pIfIo->pfnWriteAsync = vdIOWriteAsyncFallback;
|
---|
5724 | pIfIo->pfnFlushAsync = vdIOFlushAsyncFallback;
|
---|
5725 | }
|
---|
5726 |
|
---|
5727 | /**
|
---|
5728 | * Sets the internal I/O callbacks of the given interface.
|
---|
5729 | *
|
---|
5730 | * @returns nothing.
|
---|
5731 | * @param pIfIoInt The internal I/O interface to setup.
|
---|
5732 | */
|
---|
5733 | static void vdIfIoIntCallbacksSetup(PVDINTERFACEIOINT pIfIoInt)
|
---|
5734 | {
|
---|
5735 | pIfIoInt->pfnOpen = vdIOIntOpen;
|
---|
5736 | pIfIoInt->pfnClose = vdIOIntClose;
|
---|
5737 | pIfIoInt->pfnDelete = vdIOIntDelete;
|
---|
5738 | pIfIoInt->pfnMove = vdIOIntMove;
|
---|
5739 | pIfIoInt->pfnGetFreeSpace = vdIOIntGetFreeSpace;
|
---|
5740 | pIfIoInt->pfnGetModificationTime = vdIOIntGetModificationTime;
|
---|
5741 | pIfIoInt->pfnGetSize = vdIOIntGetSize;
|
---|
5742 | pIfIoInt->pfnSetSize = vdIOIntSetSize;
|
---|
5743 | pIfIoInt->pfnSetAllocationSize = vdIOIntSetAllocationSize;
|
---|
5744 | pIfIoInt->pfnReadUser = vdIOIntReadUser;
|
---|
5745 | pIfIoInt->pfnWriteUser = vdIOIntWriteUser;
|
---|
5746 | pIfIoInt->pfnReadMeta = vdIOIntReadMeta;
|
---|
5747 | pIfIoInt->pfnWriteMeta = vdIOIntWriteMeta;
|
---|
5748 | pIfIoInt->pfnMetaXferRelease = vdIOIntMetaXferRelease;
|
---|
5749 | pIfIoInt->pfnFlush = vdIOIntFlush;
|
---|
5750 | pIfIoInt->pfnIoCtxCopyFrom = vdIOIntIoCtxCopyFrom;
|
---|
5751 | pIfIoInt->pfnIoCtxCopyTo = vdIOIntIoCtxCopyTo;
|
---|
5752 | pIfIoInt->pfnIoCtxSet = vdIOIntIoCtxSet;
|
---|
5753 | pIfIoInt->pfnIoCtxSegArrayCreate = vdIOIntIoCtxSegArrayCreate;
|
---|
5754 | pIfIoInt->pfnIoCtxCompleted = vdIOIntIoCtxCompleted;
|
---|
5755 | pIfIoInt->pfnIoCtxIsSynchronous = vdIOIntIoCtxIsSynchronous;
|
---|
5756 | pIfIoInt->pfnIoCtxIsZero = vdIOIntIoCtxIsZero;
|
---|
5757 | pIfIoInt->pfnIoCtxGetDataUnitSize = vdIOIntIoCtxGetDataUnitSize;
|
---|
5758 | }
|
---|
5759 |
|
---|
5760 | /**
|
---|
5761 | * Internally used completion handler for synchronous I/O contexts.
|
---|
5762 | */
|
---|
5763 | static DECLCALLBACK(void) vdIoCtxSyncComplete(void *pvUser1, void *pvUser2, int rcReq)
|
---|
5764 | {
|
---|
5765 | RT_NOREF2(pvUser1, rcReq);
|
---|
5766 | RTSEMEVENT hEvent = (RTSEMEVENT)pvUser2;
|
---|
5767 |
|
---|
5768 | RTSemEventSignal(hEvent);
|
---|
5769 | }
|
---|
5770 |
|
---|
5771 | /**
|
---|
5772 | * Initializes HDD backends.
|
---|
5773 | *
|
---|
5774 | * @returns VBox status code.
|
---|
5775 | */
|
---|
5776 | VBOXDDU_DECL(int) VDInit(void)
|
---|
5777 | {
|
---|
5778 | int rc = vdAddBackends(NIL_RTLDRMOD, aStaticBackends, RT_ELEMENTS(aStaticBackends));
|
---|
5779 | if (RT_SUCCESS(rc))
|
---|
5780 | {
|
---|
5781 | rc = vdAddCacheBackends(NIL_RTLDRMOD, aStaticCacheBackends, RT_ELEMENTS(aStaticCacheBackends));
|
---|
5782 | if (RT_SUCCESS(rc))
|
---|
5783 | {
|
---|
5784 | RTListInit(&g_ListPluginsLoaded);
|
---|
5785 | rc = vdLoadDynamicBackends();
|
---|
5786 | }
|
---|
5787 | }
|
---|
5788 | LogRel(("VD: VDInit finished\n"));
|
---|
5789 | return rc;
|
---|
5790 | }
|
---|
5791 |
|
---|
5792 | /**
|
---|
5793 | * Destroys loaded HDD backends.
|
---|
5794 | *
|
---|
5795 | * @returns VBox status code.
|
---|
5796 | */
|
---|
5797 | VBOXDDU_DECL(int) VDShutdown(void)
|
---|
5798 | {
|
---|
5799 | if (!g_apBackends)
|
---|
5800 | return VERR_INTERNAL_ERROR;
|
---|
5801 |
|
---|
5802 | if (g_apCacheBackends)
|
---|
5803 | RTMemFree(g_apCacheBackends);
|
---|
5804 | RTMemFree(g_apBackends);
|
---|
5805 |
|
---|
5806 | g_cBackends = 0;
|
---|
5807 | g_apBackends = NULL;
|
---|
5808 |
|
---|
5809 | /* Clear the supported cache backends. */
|
---|
5810 | g_cCacheBackends = 0;
|
---|
5811 | g_apCacheBackends = NULL;
|
---|
5812 |
|
---|
5813 | #ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
5814 | PVDPLUGIN pPlugin, pPluginNext;
|
---|
5815 |
|
---|
5816 | RTListForEachSafe(&g_ListPluginsLoaded, pPlugin, pPluginNext, VDPLUGIN, NodePlugin)
|
---|
5817 | {
|
---|
5818 | RTLdrClose(pPlugin->hPlugin);
|
---|
5819 | RTStrFree(pPlugin->pszFilename);
|
---|
5820 | RTListNodeRemove(&pPlugin->NodePlugin);
|
---|
5821 | RTMemFree(pPlugin);
|
---|
5822 | }
|
---|
5823 | #endif
|
---|
5824 |
|
---|
5825 | return VINF_SUCCESS;
|
---|
5826 | }
|
---|
5827 |
|
---|
5828 | /**
|
---|
5829 | * Loads a single plugin given by filename.
|
---|
5830 | *
|
---|
5831 | * @returns VBox status code.
|
---|
5832 | * @param pszFilename The plugin filename to load.
|
---|
5833 | */
|
---|
5834 | VBOXDDU_DECL(int) VDPluginLoadFromFilename(const char *pszFilename)
|
---|
5835 | {
|
---|
5836 | if (!g_apBackends)
|
---|
5837 | {
|
---|
5838 | int rc = VDInit();
|
---|
5839 | if (RT_FAILURE(rc))
|
---|
5840 | return rc;
|
---|
5841 | }
|
---|
5842 |
|
---|
5843 | return vdPluginLoadFromFilename(pszFilename);
|
---|
5844 | }
|
---|
5845 |
|
---|
5846 | /**
|
---|
5847 | * Load all plugins from a given path.
|
---|
5848 | *
|
---|
5849 | * @returns VBox statuse code.
|
---|
5850 | * @param pszPath The path to load plugins from.
|
---|
5851 | */
|
---|
5852 | VBOXDDU_DECL(int) VDPluginLoadFromPath(const char *pszPath)
|
---|
5853 | {
|
---|
5854 | if (!g_apBackends)
|
---|
5855 | {
|
---|
5856 | int rc = VDInit();
|
---|
5857 | if (RT_FAILURE(rc))
|
---|
5858 | return rc;
|
---|
5859 | }
|
---|
5860 |
|
---|
5861 | return vdPluginLoadFromPath(pszPath);
|
---|
5862 | }
|
---|
5863 |
|
---|
5864 | /**
|
---|
5865 | * Unloads a single plugin given by filename.
|
---|
5866 | *
|
---|
5867 | * @returns VBox status code.
|
---|
5868 | * @param pszFilename The plugin filename to unload.
|
---|
5869 | */
|
---|
5870 | VBOXDDU_DECL(int) VDPluginUnloadFromFilename(const char *pszFilename)
|
---|
5871 | {
|
---|
5872 | if (!g_apBackends)
|
---|
5873 | {
|
---|
5874 | int rc = VDInit();
|
---|
5875 | if (RT_FAILURE(rc))
|
---|
5876 | return rc;
|
---|
5877 | }
|
---|
5878 |
|
---|
5879 | return vdPluginUnloadFromFilename(pszFilename);
|
---|
5880 | }
|
---|
5881 |
|
---|
5882 | /**
|
---|
5883 | * Unload all plugins from a given path.
|
---|
5884 | *
|
---|
5885 | * @returns VBox statuse code.
|
---|
5886 | * @param pszPath The path to unload plugins from.
|
---|
5887 | */
|
---|
5888 | VBOXDDU_DECL(int) VDPluginUnloadFromPath(const char *pszPath)
|
---|
5889 | {
|
---|
5890 | if (!g_apBackends)
|
---|
5891 | {
|
---|
5892 | int rc = VDInit();
|
---|
5893 | if (RT_FAILURE(rc))
|
---|
5894 | return rc;
|
---|
5895 | }
|
---|
5896 |
|
---|
5897 | return vdPluginUnloadFromPath(pszPath);
|
---|
5898 | }
|
---|
5899 |
|
---|
5900 | /**
|
---|
5901 | * Lists all HDD backends and their capabilities in a caller-provided buffer.
|
---|
5902 | *
|
---|
5903 | * @returns VBox status code.
|
---|
5904 | * VERR_BUFFER_OVERFLOW if not enough space is passed.
|
---|
5905 | * @param cEntriesAlloc Number of list entries available.
|
---|
5906 | * @param pEntries Pointer to array for the entries.
|
---|
5907 | * @param pcEntriesUsed Number of entries returned.
|
---|
5908 | */
|
---|
5909 | VBOXDDU_DECL(int) VDBackendInfo(unsigned cEntriesAlloc, PVDBACKENDINFO pEntries,
|
---|
5910 | unsigned *pcEntriesUsed)
|
---|
5911 | {
|
---|
5912 | int rc = VINF_SUCCESS;
|
---|
5913 |
|
---|
5914 | LogFlowFunc(("cEntriesAlloc=%u pEntries=%#p pcEntriesUsed=%#p\n", cEntriesAlloc, pEntries, pcEntriesUsed));
|
---|
5915 | /* Check arguments. */
|
---|
5916 | AssertMsgReturn(cEntriesAlloc,
|
---|
5917 | ("cEntriesAlloc=%u\n", cEntriesAlloc),
|
---|
5918 | VERR_INVALID_PARAMETER);
|
---|
5919 | AssertMsgReturn(VALID_PTR(pEntries),
|
---|
5920 | ("pEntries=%#p\n", pEntries),
|
---|
5921 | VERR_INVALID_PARAMETER);
|
---|
5922 | AssertMsgReturn(VALID_PTR(pcEntriesUsed),
|
---|
5923 | ("pcEntriesUsed=%#p\n", pcEntriesUsed),
|
---|
5924 | VERR_INVALID_PARAMETER);
|
---|
5925 | if (!g_apBackends)
|
---|
5926 | VDInit();
|
---|
5927 |
|
---|
5928 | if (cEntriesAlloc < g_cBackends)
|
---|
5929 | {
|
---|
5930 | *pcEntriesUsed = g_cBackends;
|
---|
5931 | return VERR_BUFFER_OVERFLOW;
|
---|
5932 | }
|
---|
5933 |
|
---|
5934 | for (unsigned i = 0; i < g_cBackends; i++)
|
---|
5935 | {
|
---|
5936 | pEntries[i].pszBackend = g_apBackends[i]->pszBackendName;
|
---|
5937 | pEntries[i].uBackendCaps = g_apBackends[i]->uBackendCaps;
|
---|
5938 | pEntries[i].paFileExtensions = g_apBackends[i]->paFileExtensions;
|
---|
5939 | pEntries[i].paConfigInfo = g_apBackends[i]->paConfigInfo;
|
---|
5940 | pEntries[i].pfnComposeLocation = g_apBackends[i]->pfnComposeLocation;
|
---|
5941 | pEntries[i].pfnComposeName = g_apBackends[i]->pfnComposeName;
|
---|
5942 | }
|
---|
5943 |
|
---|
5944 | LogFlowFunc(("returns %Rrc *pcEntriesUsed=%u\n", rc, g_cBackends));
|
---|
5945 | *pcEntriesUsed = g_cBackends;
|
---|
5946 | return rc;
|
---|
5947 | }
|
---|
5948 |
|
---|
5949 | /**
|
---|
5950 | * Lists the capabilities of a backend identified by its name.
|
---|
5951 | *
|
---|
5952 | * @returns VBox status code.
|
---|
5953 | * @param pszBackend The backend name.
|
---|
5954 | * @param pEntries Pointer to an entry.
|
---|
5955 | */
|
---|
5956 | VBOXDDU_DECL(int) VDBackendInfoOne(const char *pszBackend, PVDBACKENDINFO pEntry)
|
---|
5957 | {
|
---|
5958 | LogFlowFunc(("pszBackend=%#p pEntry=%#p\n", pszBackend, pEntry));
|
---|
5959 | /* Check arguments. */
|
---|
5960 | AssertMsgReturn(VALID_PTR(pszBackend),
|
---|
5961 | ("pszBackend=%#p\n", pszBackend),
|
---|
5962 | VERR_INVALID_PARAMETER);
|
---|
5963 | AssertMsgReturn(VALID_PTR(pEntry),
|
---|
5964 | ("pEntry=%#p\n", pEntry),
|
---|
5965 | VERR_INVALID_PARAMETER);
|
---|
5966 | if (!g_apBackends)
|
---|
5967 | VDInit();
|
---|
5968 |
|
---|
5969 | /* Go through loaded backends. */
|
---|
5970 | for (unsigned i = 0; i < g_cBackends; i++)
|
---|
5971 | {
|
---|
5972 | if (!RTStrICmp(pszBackend, g_apBackends[i]->pszBackendName))
|
---|
5973 | {
|
---|
5974 | pEntry->pszBackend = g_apBackends[i]->pszBackendName;
|
---|
5975 | pEntry->uBackendCaps = g_apBackends[i]->uBackendCaps;
|
---|
5976 | pEntry->paFileExtensions = g_apBackends[i]->paFileExtensions;
|
---|
5977 | pEntry->paConfigInfo = g_apBackends[i]->paConfigInfo;
|
---|
5978 | return VINF_SUCCESS;
|
---|
5979 | }
|
---|
5980 | }
|
---|
5981 |
|
---|
5982 | return VERR_NOT_FOUND;
|
---|
5983 | }
|
---|
5984 |
|
---|
5985 | /**
|
---|
5986 | * Lists all filters and their capabilities in a caller-provided buffer.
|
---|
5987 | *
|
---|
5988 | * @return VBox status code.
|
---|
5989 | * VERR_BUFFER_OVERFLOW if not enough space is passed.
|
---|
5990 | * @param cEntriesAlloc Number of list entries available.
|
---|
5991 | * @param pEntries Pointer to array for the entries.
|
---|
5992 | * @param pcEntriesUsed Number of entries returned.
|
---|
5993 | */
|
---|
5994 | VBOXDDU_DECL(int) VDFilterInfo(unsigned cEntriesAlloc, PVDFILTERINFO pEntries,
|
---|
5995 | unsigned *pcEntriesUsed)
|
---|
5996 | {
|
---|
5997 | int rc = VINF_SUCCESS;
|
---|
5998 |
|
---|
5999 | LogFlowFunc(("cEntriesAlloc=%u pEntries=%#p pcEntriesUsed=%#p\n", cEntriesAlloc, pEntries, pcEntriesUsed));
|
---|
6000 | /* Check arguments. */
|
---|
6001 | AssertMsgReturn(cEntriesAlloc,
|
---|
6002 | ("cEntriesAlloc=%u\n", cEntriesAlloc),
|
---|
6003 | VERR_INVALID_PARAMETER);
|
---|
6004 | AssertMsgReturn(VALID_PTR(pEntries),
|
---|
6005 | ("pEntries=%#p\n", pEntries),
|
---|
6006 | VERR_INVALID_PARAMETER);
|
---|
6007 | AssertMsgReturn(VALID_PTR(pcEntriesUsed),
|
---|
6008 | ("pcEntriesUsed=%#p\n", pcEntriesUsed),
|
---|
6009 | VERR_INVALID_PARAMETER);
|
---|
6010 | if (!g_apBackends)
|
---|
6011 | VDInit();
|
---|
6012 |
|
---|
6013 | if (cEntriesAlloc < g_cFilterBackends)
|
---|
6014 | {
|
---|
6015 | *pcEntriesUsed = g_cFilterBackends;
|
---|
6016 | return VERR_BUFFER_OVERFLOW;
|
---|
6017 | }
|
---|
6018 |
|
---|
6019 | for (unsigned i = 0; i < g_cFilterBackends; i++)
|
---|
6020 | {
|
---|
6021 | pEntries[i].pszFilter = g_apFilterBackends[i]->pszBackendName;
|
---|
6022 | pEntries[i].paConfigInfo = g_apFilterBackends[i]->paConfigInfo;
|
---|
6023 | }
|
---|
6024 |
|
---|
6025 | LogFlowFunc(("returns %Rrc *pcEntriesUsed=%u\n", rc, g_cFilterBackends));
|
---|
6026 | *pcEntriesUsed = g_cFilterBackends;
|
---|
6027 | return rc;
|
---|
6028 | }
|
---|
6029 |
|
---|
6030 | /**
|
---|
6031 | * Lists the capabilities of a filter identified by its name.
|
---|
6032 | *
|
---|
6033 | * @return VBox status code.
|
---|
6034 | * @param pszFilter The filter name (case insensitive).
|
---|
6035 | * @param pEntries Pointer to an entry.
|
---|
6036 | */
|
---|
6037 | VBOXDDU_DECL(int) VDFilterInfoOne(const char *pszFilter, PVDFILTERINFO pEntry)
|
---|
6038 | {
|
---|
6039 | LogFlowFunc(("pszFilter=%#p pEntry=%#p\n", pszFilter, pEntry));
|
---|
6040 | /* Check arguments. */
|
---|
6041 | AssertMsgReturn(VALID_PTR(pszFilter),
|
---|
6042 | ("pszFilter=%#p\n", pszFilter),
|
---|
6043 | VERR_INVALID_PARAMETER);
|
---|
6044 | AssertMsgReturn(VALID_PTR(pEntry),
|
---|
6045 | ("pEntry=%#p\n", pEntry),
|
---|
6046 | VERR_INVALID_PARAMETER);
|
---|
6047 | if (!g_apBackends)
|
---|
6048 | VDInit();
|
---|
6049 |
|
---|
6050 | /* Go through loaded backends. */
|
---|
6051 | for (unsigned i = 0; i < g_cFilterBackends; i++)
|
---|
6052 | {
|
---|
6053 | if (!RTStrICmp(pszFilter, g_apFilterBackends[i]->pszBackendName))
|
---|
6054 | {
|
---|
6055 | pEntry->pszFilter = g_apFilterBackends[i]->pszBackendName;
|
---|
6056 | pEntry->paConfigInfo = g_apFilterBackends[i]->paConfigInfo;
|
---|
6057 | return VINF_SUCCESS;
|
---|
6058 | }
|
---|
6059 | }
|
---|
6060 |
|
---|
6061 | return VERR_NOT_FOUND;
|
---|
6062 | }
|
---|
6063 |
|
---|
6064 | /**
|
---|
6065 | * Allocates and initializes an empty HDD container.
|
---|
6066 | * No image files are opened.
|
---|
6067 | *
|
---|
6068 | * @returns VBox status code.
|
---|
6069 | * @param pVDIfsDisk Pointer to the per-disk VD interface list.
|
---|
6070 | * @param enmType Type of the image container.
|
---|
6071 | * @param ppDisk Where to store the reference to HDD container.
|
---|
6072 | */
|
---|
6073 | VBOXDDU_DECL(int) VDCreate(PVDINTERFACE pVDIfsDisk, VDTYPE enmType, PVBOXHDD *ppDisk)
|
---|
6074 | {
|
---|
6075 | int rc = VINF_SUCCESS;
|
---|
6076 | PVBOXHDD pDisk = NULL;
|
---|
6077 |
|
---|
6078 | LogFlowFunc(("pVDIfsDisk=%#p\n", pVDIfsDisk));
|
---|
6079 | do
|
---|
6080 | {
|
---|
6081 | /* Check arguments. */
|
---|
6082 | AssertMsgBreakStmt(VALID_PTR(ppDisk),
|
---|
6083 | ("ppDisk=%#p\n", ppDisk),
|
---|
6084 | rc = VERR_INVALID_PARAMETER);
|
---|
6085 |
|
---|
6086 | pDisk = (PVBOXHDD)RTMemAllocZ(sizeof(VBOXHDD));
|
---|
6087 | if (pDisk)
|
---|
6088 | {
|
---|
6089 | pDisk->u32Signature = VBOXHDDDISK_SIGNATURE;
|
---|
6090 | pDisk->enmType = enmType;
|
---|
6091 | pDisk->cImages = 0;
|
---|
6092 | pDisk->pBase = NULL;
|
---|
6093 | pDisk->pLast = NULL;
|
---|
6094 | pDisk->cbSize = 0;
|
---|
6095 | pDisk->PCHSGeometry.cCylinders = 0;
|
---|
6096 | pDisk->PCHSGeometry.cHeads = 0;
|
---|
6097 | pDisk->PCHSGeometry.cSectors = 0;
|
---|
6098 | pDisk->LCHSGeometry.cCylinders = 0;
|
---|
6099 | pDisk->LCHSGeometry.cHeads = 0;
|
---|
6100 | pDisk->LCHSGeometry.cSectors = 0;
|
---|
6101 | pDisk->pVDIfsDisk = pVDIfsDisk;
|
---|
6102 | pDisk->pInterfaceError = NULL;
|
---|
6103 | pDisk->pInterfaceThreadSync = NULL;
|
---|
6104 | pDisk->pIoCtxLockOwner = NULL;
|
---|
6105 | pDisk->pIoCtxHead = NULL;
|
---|
6106 | pDisk->fLocked = false;
|
---|
6107 | pDisk->hMemCacheIoCtx = NIL_RTMEMCACHE;
|
---|
6108 | pDisk->hMemCacheIoTask = NIL_RTMEMCACHE;
|
---|
6109 | RTListInit(&pDisk->ListFilterChainWrite);
|
---|
6110 | RTListInit(&pDisk->ListFilterChainRead);
|
---|
6111 |
|
---|
6112 | /* Create the I/O ctx cache */
|
---|
6113 | rc = RTMemCacheCreate(&pDisk->hMemCacheIoCtx, sizeof(VDIOCTX), 0, UINT32_MAX,
|
---|
6114 | NULL, NULL, NULL, 0);
|
---|
6115 | if (RT_FAILURE(rc))
|
---|
6116 | break;
|
---|
6117 |
|
---|
6118 | /* Create the I/O task cache */
|
---|
6119 | rc = RTMemCacheCreate(&pDisk->hMemCacheIoTask, sizeof(VDIOTASK), 0, UINT32_MAX,
|
---|
6120 | NULL, NULL, NULL, 0);
|
---|
6121 | if (RT_FAILURE(rc))
|
---|
6122 | break;
|
---|
6123 |
|
---|
6124 | pDisk->pInterfaceError = VDIfErrorGet(pVDIfsDisk);
|
---|
6125 | pDisk->pInterfaceThreadSync = VDIfThreadSyncGet(pVDIfsDisk);
|
---|
6126 |
|
---|
6127 | *ppDisk = pDisk;
|
---|
6128 | }
|
---|
6129 | else
|
---|
6130 | {
|
---|
6131 | rc = VERR_NO_MEMORY;
|
---|
6132 | break;
|
---|
6133 | }
|
---|
6134 | } while (0);
|
---|
6135 |
|
---|
6136 | if ( RT_FAILURE(rc)
|
---|
6137 | && pDisk)
|
---|
6138 | {
|
---|
6139 | if (pDisk->hMemCacheIoCtx != NIL_RTMEMCACHE)
|
---|
6140 | RTMemCacheDestroy(pDisk->hMemCacheIoCtx);
|
---|
6141 | if (pDisk->hMemCacheIoTask != NIL_RTMEMCACHE)
|
---|
6142 | RTMemCacheDestroy(pDisk->hMemCacheIoTask);
|
---|
6143 | }
|
---|
6144 |
|
---|
6145 | LogFlowFunc(("returns %Rrc (pDisk=%#p)\n", rc, pDisk));
|
---|
6146 | return rc;
|
---|
6147 | }
|
---|
6148 |
|
---|
6149 | /**
|
---|
6150 | * Destroys HDD container.
|
---|
6151 | * If container has opened image files they will be closed.
|
---|
6152 | *
|
---|
6153 | * @returns VBox status code.
|
---|
6154 | * @param pDisk Pointer to HDD container.
|
---|
6155 | */
|
---|
6156 | VBOXDDU_DECL(int) VDDestroy(PVBOXHDD pDisk)
|
---|
6157 | {
|
---|
6158 | int rc = VINF_SUCCESS;
|
---|
6159 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
6160 | do
|
---|
6161 | {
|
---|
6162 | /* sanity check */
|
---|
6163 | AssertPtrBreak(pDisk);
|
---|
6164 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6165 | Assert(!pDisk->fLocked);
|
---|
6166 |
|
---|
6167 | rc = VDCloseAll(pDisk);
|
---|
6168 | int rc2 = VDFilterRemoveAll(pDisk);
|
---|
6169 | if (RT_SUCCESS(rc))
|
---|
6170 | rc = rc2;
|
---|
6171 |
|
---|
6172 | RTMemCacheDestroy(pDisk->hMemCacheIoCtx);
|
---|
6173 | RTMemCacheDestroy(pDisk->hMemCacheIoTask);
|
---|
6174 | RTMemFree(pDisk);
|
---|
6175 | } while (0);
|
---|
6176 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6177 | return rc;
|
---|
6178 | }
|
---|
6179 |
|
---|
6180 | /**
|
---|
6181 | * Try to get the backend name which can use this image.
|
---|
6182 | *
|
---|
6183 | * @returns VBox status code.
|
---|
6184 | * VINF_SUCCESS if a plugin was found.
|
---|
6185 | * ppszFormat contains the string which can be used as backend name.
|
---|
6186 | * VERR_NOT_SUPPORTED if no backend was found.
|
---|
6187 | * @param pVDIfsDisk Pointer to the per-disk VD interface list.
|
---|
6188 | * @param pVDIfsImage Pointer to the per-image VD interface list.
|
---|
6189 | * @param pszFilename Name of the image file for which the backend is queried.
|
---|
6190 | * @param ppszFormat Receives pointer of the UTF-8 string which contains the format name.
|
---|
6191 | * The returned pointer must be freed using RTStrFree().
|
---|
6192 | */
|
---|
6193 | VBOXDDU_DECL(int) VDGetFormat(PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
|
---|
6194 | const char *pszFilename, char **ppszFormat, VDTYPE *penmType)
|
---|
6195 | {
|
---|
6196 | int rc = VERR_NOT_SUPPORTED;
|
---|
6197 | VDINTERFACEIOINT VDIfIoInt;
|
---|
6198 | VDINTERFACEIO VDIfIoFallback;
|
---|
6199 | PVDINTERFACEIO pInterfaceIo;
|
---|
6200 |
|
---|
6201 | LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
|
---|
6202 | /* Check arguments. */
|
---|
6203 | AssertMsgReturn(VALID_PTR(pszFilename) && *pszFilename,
|
---|
6204 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
6205 | VERR_INVALID_PARAMETER);
|
---|
6206 | AssertMsgReturn(VALID_PTR(ppszFormat),
|
---|
6207 | ("ppszFormat=%#p\n", ppszFormat),
|
---|
6208 | VERR_INVALID_PARAMETER);
|
---|
6209 | AssertMsgReturn(VALID_PTR(penmType),
|
---|
6210 | ("penmType=%#p\n", penmType),
|
---|
6211 | VERR_INVALID_PARAMETER);
|
---|
6212 |
|
---|
6213 | if (!g_apBackends)
|
---|
6214 | VDInit();
|
---|
6215 |
|
---|
6216 | pInterfaceIo = VDIfIoGet(pVDIfsImage);
|
---|
6217 | if (!pInterfaceIo)
|
---|
6218 | {
|
---|
6219 | /*
|
---|
6220 | * Caller doesn't provide an I/O interface, create our own using the
|
---|
6221 | * native file API.
|
---|
6222 | */
|
---|
6223 | vdIfIoFallbackCallbacksSetup(&VDIfIoFallback);
|
---|
6224 | pInterfaceIo = &VDIfIoFallback;
|
---|
6225 | }
|
---|
6226 |
|
---|
6227 | /* Set up the internal I/O interface. */
|
---|
6228 | AssertReturn(!VDIfIoIntGet(pVDIfsImage), VERR_INVALID_PARAMETER);
|
---|
6229 | VDIfIoInt.pfnOpen = vdIOIntOpenLimited;
|
---|
6230 | VDIfIoInt.pfnClose = vdIOIntCloseLimited;
|
---|
6231 | VDIfIoInt.pfnDelete = vdIOIntDeleteLimited;
|
---|
6232 | VDIfIoInt.pfnMove = vdIOIntMoveLimited;
|
---|
6233 | VDIfIoInt.pfnGetFreeSpace = vdIOIntGetFreeSpaceLimited;
|
---|
6234 | VDIfIoInt.pfnGetModificationTime = vdIOIntGetModificationTimeLimited;
|
---|
6235 | VDIfIoInt.pfnGetSize = vdIOIntGetSizeLimited;
|
---|
6236 | VDIfIoInt.pfnSetSize = vdIOIntSetSizeLimited;
|
---|
6237 | VDIfIoInt.pfnReadUser = vdIOIntReadUserLimited;
|
---|
6238 | VDIfIoInt.pfnWriteUser = vdIOIntWriteUserLimited;
|
---|
6239 | VDIfIoInt.pfnReadMeta = vdIOIntReadMetaLimited;
|
---|
6240 | VDIfIoInt.pfnWriteMeta = vdIOIntWriteMetaLimited;
|
---|
6241 | VDIfIoInt.pfnFlush = vdIOIntFlushLimited;
|
---|
6242 | rc = VDInterfaceAdd(&VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
6243 | pInterfaceIo, sizeof(VDINTERFACEIOINT), &pVDIfsImage);
|
---|
6244 | AssertRC(rc);
|
---|
6245 |
|
---|
6246 | /* Find the backend supporting this file format. */
|
---|
6247 | for (unsigned i = 0; i < g_cBackends; i++)
|
---|
6248 | {
|
---|
6249 | if (g_apBackends[i]->pfnCheckIfValid)
|
---|
6250 | {
|
---|
6251 | rc = g_apBackends[i]->pfnCheckIfValid(pszFilename, pVDIfsDisk,
|
---|
6252 | pVDIfsImage, penmType);
|
---|
6253 | if ( RT_SUCCESS(rc)
|
---|
6254 | /* The correct backend has been found, but there is a small
|
---|
6255 | * incompatibility so that the file cannot be used. Stop here
|
---|
6256 | * and signal success - the actual open will of course fail,
|
---|
6257 | * but that will create a really sensible error message. */
|
---|
6258 | || ( rc != VERR_VD_GEN_INVALID_HEADER
|
---|
6259 | && rc != VERR_VD_VDI_INVALID_HEADER
|
---|
6260 | && rc != VERR_VD_VMDK_INVALID_HEADER
|
---|
6261 | && rc != VERR_VD_ISCSI_INVALID_HEADER
|
---|
6262 | && rc != VERR_VD_VHD_INVALID_HEADER
|
---|
6263 | && rc != VERR_VD_RAW_INVALID_HEADER
|
---|
6264 | && rc != VERR_VD_RAW_SIZE_MODULO_512
|
---|
6265 | && rc != VERR_VD_RAW_SIZE_MODULO_2048
|
---|
6266 | && rc != VERR_VD_RAW_SIZE_OPTICAL_TOO_SMALL
|
---|
6267 | && rc != VERR_VD_RAW_SIZE_FLOPPY_TOO_BIG
|
---|
6268 | && rc != VERR_VD_PARALLELS_INVALID_HEADER
|
---|
6269 | && rc != VERR_VD_DMG_INVALID_HEADER))
|
---|
6270 | {
|
---|
6271 | /* Copy the name into the new string. */
|
---|
6272 | char *pszFormat = RTStrDup(g_apBackends[i]->pszBackendName);
|
---|
6273 | if (!pszFormat)
|
---|
6274 | {
|
---|
6275 | rc = VERR_NO_MEMORY;
|
---|
6276 | break;
|
---|
6277 | }
|
---|
6278 | *ppszFormat = pszFormat;
|
---|
6279 | /* Do not consider the typical file access errors as success,
|
---|
6280 | * which allows the caller to deal with such issues. */
|
---|
6281 | if ( rc != VERR_ACCESS_DENIED
|
---|
6282 | && rc != VERR_PATH_NOT_FOUND
|
---|
6283 | && rc != VERR_FILE_NOT_FOUND)
|
---|
6284 | rc = VINF_SUCCESS;
|
---|
6285 | break;
|
---|
6286 | }
|
---|
6287 | rc = VERR_NOT_SUPPORTED;
|
---|
6288 | }
|
---|
6289 | }
|
---|
6290 |
|
---|
6291 | /* Try the cache backends. */
|
---|
6292 | if (rc == VERR_NOT_SUPPORTED)
|
---|
6293 | {
|
---|
6294 | for (unsigned i = 0; i < g_cCacheBackends; i++)
|
---|
6295 | {
|
---|
6296 | if (g_apCacheBackends[i]->pfnProbe)
|
---|
6297 | {
|
---|
6298 | rc = g_apCacheBackends[i]->pfnProbe(pszFilename, pVDIfsDisk,
|
---|
6299 | pVDIfsImage);
|
---|
6300 | if ( RT_SUCCESS(rc)
|
---|
6301 | || (rc != VERR_VD_GEN_INVALID_HEADER))
|
---|
6302 | {
|
---|
6303 | /* Copy the name into the new string. */
|
---|
6304 | char *pszFormat = RTStrDup(g_apBackends[i]->pszBackendName);
|
---|
6305 | if (!pszFormat)
|
---|
6306 | {
|
---|
6307 | rc = VERR_NO_MEMORY;
|
---|
6308 | break;
|
---|
6309 | }
|
---|
6310 | *ppszFormat = pszFormat;
|
---|
6311 | rc = VINF_SUCCESS;
|
---|
6312 | break;
|
---|
6313 | }
|
---|
6314 | rc = VERR_NOT_SUPPORTED;
|
---|
6315 | }
|
---|
6316 | }
|
---|
6317 | }
|
---|
6318 |
|
---|
6319 | LogFlowFunc(("returns %Rrc *ppszFormat=\"%s\"\n", rc, *ppszFormat));
|
---|
6320 | return rc;
|
---|
6321 | }
|
---|
6322 |
|
---|
6323 | /**
|
---|
6324 | * Opens an image file.
|
---|
6325 | *
|
---|
6326 | * The first opened image file in HDD container must have a base image type,
|
---|
6327 | * others (next opened images) must be a differencing or undo images.
|
---|
6328 | * Linkage is checked for differencing image to be in consistence with the previously opened image.
|
---|
6329 | * When another differencing image is opened and the last image was opened in read/write access
|
---|
6330 | * mode, then the last image is reopened in read-only with deny write sharing mode. This allows
|
---|
6331 | * other processes to use images in read-only mode too.
|
---|
6332 | *
|
---|
6333 | * Note that the image is opened in read-only mode if a read/write open is not possible.
|
---|
6334 | * Use VDIsReadOnly to check open mode.
|
---|
6335 | *
|
---|
6336 | * @returns VBox status code.
|
---|
6337 | * @param pDisk Pointer to HDD container.
|
---|
6338 | * @param pszBackend Name of the image file backend to use.
|
---|
6339 | * @param pszFilename Name of the image file to open.
|
---|
6340 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
6341 | * @param pVDIfsImage Pointer to the per-image VD interface list.
|
---|
6342 | */
|
---|
6343 | VBOXDDU_DECL(int) VDOpen(PVBOXHDD pDisk, const char *pszBackend,
|
---|
6344 | const char *pszFilename, unsigned uOpenFlags,
|
---|
6345 | PVDINTERFACE pVDIfsImage)
|
---|
6346 | {
|
---|
6347 | int rc = VINF_SUCCESS;
|
---|
6348 | int rc2;
|
---|
6349 | bool fLockWrite = false;
|
---|
6350 | PVDIMAGE pImage = NULL;
|
---|
6351 |
|
---|
6352 | LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uOpenFlags=%#x, pVDIfsImage=%#p\n",
|
---|
6353 | pDisk, pszBackend, pszFilename, uOpenFlags, pVDIfsImage));
|
---|
6354 |
|
---|
6355 | do
|
---|
6356 | {
|
---|
6357 | /* sanity check */
|
---|
6358 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
6359 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6360 |
|
---|
6361 | /* Check arguments. */
|
---|
6362 | AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
|
---|
6363 | ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
|
---|
6364 | rc = VERR_INVALID_PARAMETER);
|
---|
6365 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
6366 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
6367 | rc = VERR_INVALID_PARAMETER);
|
---|
6368 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
6369 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
6370 | rc = VERR_INVALID_PARAMETER);
|
---|
6371 | AssertMsgBreakStmt( !(uOpenFlags & VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)
|
---|
6372 | || (uOpenFlags & VD_OPEN_FLAGS_READONLY),
|
---|
6373 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
6374 | rc = VERR_INVALID_PARAMETER);
|
---|
6375 |
|
---|
6376 | /*
|
---|
6377 | * Destroy the current discard state first which might still have pending blocks
|
---|
6378 | * for the currently opened image which will be switched to readonly mode.
|
---|
6379 | */
|
---|
6380 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
6381 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6382 | AssertRC(rc2);
|
---|
6383 | fLockWrite = true;
|
---|
6384 | rc = vdDiscardStateDestroy(pDisk);
|
---|
6385 | if (RT_FAILURE(rc))
|
---|
6386 | break;
|
---|
6387 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6388 | AssertRC(rc2);
|
---|
6389 | fLockWrite = false;
|
---|
6390 |
|
---|
6391 | /* Set up image descriptor. */
|
---|
6392 | pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
|
---|
6393 | if (!pImage)
|
---|
6394 | {
|
---|
6395 | rc = VERR_NO_MEMORY;
|
---|
6396 | break;
|
---|
6397 | }
|
---|
6398 | pImage->pszFilename = RTStrDup(pszFilename);
|
---|
6399 | if (!pImage->pszFilename)
|
---|
6400 | {
|
---|
6401 | rc = VERR_NO_MEMORY;
|
---|
6402 | break;
|
---|
6403 | }
|
---|
6404 |
|
---|
6405 | pImage->VDIo.pDisk = pDisk;
|
---|
6406 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
6407 |
|
---|
6408 | rc = vdFindBackend(pszBackend, &pImage->Backend);
|
---|
6409 | if (RT_FAILURE(rc))
|
---|
6410 | break;
|
---|
6411 | if (!pImage->Backend)
|
---|
6412 | {
|
---|
6413 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
6414 | N_("VD: unknown backend name '%s'"), pszBackend);
|
---|
6415 | break;
|
---|
6416 | }
|
---|
6417 |
|
---|
6418 | /*
|
---|
6419 | * Fail if the backend can't do async I/O but the
|
---|
6420 | * flag is set.
|
---|
6421 | */
|
---|
6422 | if ( !(pImage->Backend->uBackendCaps & VD_CAP_ASYNC)
|
---|
6423 | && (uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO))
|
---|
6424 | {
|
---|
6425 | rc = vdError(pDisk, VERR_NOT_SUPPORTED, RT_SRC_POS,
|
---|
6426 | N_("VD: Backend '%s' does not support async I/O"), pszBackend);
|
---|
6427 | break;
|
---|
6428 | }
|
---|
6429 |
|
---|
6430 | /*
|
---|
6431 | * Fail if the backend doesn't support the discard operation but the
|
---|
6432 | * flag is set.
|
---|
6433 | */
|
---|
6434 | if ( !(pImage->Backend->uBackendCaps & VD_CAP_DISCARD)
|
---|
6435 | && (uOpenFlags & VD_OPEN_FLAGS_DISCARD))
|
---|
6436 | {
|
---|
6437 | rc = vdError(pDisk, VERR_VD_DISCARD_NOT_SUPPORTED, RT_SRC_POS,
|
---|
6438 | N_("VD: Backend '%s' does not support discard"), pszBackend);
|
---|
6439 | break;
|
---|
6440 | }
|
---|
6441 |
|
---|
6442 | /* Set up the I/O interface. */
|
---|
6443 | pImage->VDIo.pInterfaceIo = VDIfIoGet(pVDIfsImage);
|
---|
6444 | if (!pImage->VDIo.pInterfaceIo)
|
---|
6445 | {
|
---|
6446 | vdIfIoFallbackCallbacksSetup(&pImage->VDIo.VDIfIo);
|
---|
6447 | rc = VDInterfaceAdd(&pImage->VDIo.VDIfIo.Core, "VD_IO", VDINTERFACETYPE_IO,
|
---|
6448 | pDisk, sizeof(VDINTERFACEIO), &pVDIfsImage);
|
---|
6449 | pImage->VDIo.pInterfaceIo = &pImage->VDIo.VDIfIo;
|
---|
6450 | }
|
---|
6451 |
|
---|
6452 | /* Set up the internal I/O interface. */
|
---|
6453 | AssertBreakStmt(!VDIfIoIntGet(pVDIfsImage), rc = VERR_INVALID_PARAMETER);
|
---|
6454 | vdIfIoIntCallbacksSetup(&pImage->VDIo.VDIfIoInt);
|
---|
6455 | rc = VDInterfaceAdd(&pImage->VDIo.VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
6456 | &pImage->VDIo, sizeof(VDINTERFACEIOINT), &pImage->pVDIfsImage);
|
---|
6457 | AssertRC(rc);
|
---|
6458 |
|
---|
6459 | pImage->uOpenFlags = uOpenFlags & (VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_DISCARD | VD_OPEN_FLAGS_IGNORE_FLUSH | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS);
|
---|
6460 | pImage->VDIo.fIgnoreFlush = (uOpenFlags & VD_OPEN_FLAGS_IGNORE_FLUSH) != 0;
|
---|
6461 | rc = pImage->Backend->pfnOpen(pImage->pszFilename,
|
---|
6462 | uOpenFlags & ~(VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_IGNORE_FLUSH | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS),
|
---|
6463 | pDisk->pVDIfsDisk,
|
---|
6464 | pImage->pVDIfsImage,
|
---|
6465 | pDisk->enmType,
|
---|
6466 | &pImage->pBackendData);
|
---|
6467 | /*
|
---|
6468 | * If the image is corrupted and there is a repair method try to repair it
|
---|
6469 | * first if it was openend in read-write mode and open again afterwards.
|
---|
6470 | */
|
---|
6471 | if ( RT_UNLIKELY(rc == VERR_VD_IMAGE_CORRUPTED)
|
---|
6472 | && !(uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
6473 | && pImage->Backend->pfnRepair)
|
---|
6474 | {
|
---|
6475 | rc = pImage->Backend->pfnRepair(pszFilename, pDisk->pVDIfsDisk, pImage->pVDIfsImage, 0 /* fFlags */);
|
---|
6476 | if (RT_SUCCESS(rc))
|
---|
6477 | rc = pImage->Backend->pfnOpen(pImage->pszFilename,
|
---|
6478 | uOpenFlags & ~(VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_IGNORE_FLUSH | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS),
|
---|
6479 | pDisk->pVDIfsDisk,
|
---|
6480 | pImage->pVDIfsImage,
|
---|
6481 | pDisk->enmType,
|
---|
6482 | &pImage->pBackendData);
|
---|
6483 | else
|
---|
6484 | {
|
---|
6485 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
6486 | N_("VD: error %Rrc repairing corrupted image file '%s'"), rc, pszFilename);
|
---|
6487 | break;
|
---|
6488 | }
|
---|
6489 | }
|
---|
6490 | else if (RT_UNLIKELY(rc == VERR_VD_IMAGE_CORRUPTED))
|
---|
6491 | {
|
---|
6492 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
6493 | N_("VD: Image file '%s' is corrupted and can't be opened"), pszFilename);
|
---|
6494 | break;
|
---|
6495 | }
|
---|
6496 |
|
---|
6497 | /* If the open in read-write mode failed, retry in read-only mode. */
|
---|
6498 | if (RT_FAILURE(rc))
|
---|
6499 | {
|
---|
6500 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
6501 | && ( rc == VERR_ACCESS_DENIED
|
---|
6502 | || rc == VERR_PERMISSION_DENIED
|
---|
6503 | || rc == VERR_WRITE_PROTECT
|
---|
6504 | || rc == VERR_SHARING_VIOLATION
|
---|
6505 | || rc == VERR_FILE_LOCK_FAILED))
|
---|
6506 | rc = pImage->Backend->pfnOpen(pImage->pszFilename,
|
---|
6507 | (uOpenFlags & ~(VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS))
|
---|
6508 | | VD_OPEN_FLAGS_READONLY,
|
---|
6509 | pDisk->pVDIfsDisk,
|
---|
6510 | pImage->pVDIfsImage,
|
---|
6511 | pDisk->enmType,
|
---|
6512 | &pImage->pBackendData);
|
---|
6513 | if (RT_FAILURE(rc))
|
---|
6514 | {
|
---|
6515 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
6516 | N_("VD: error %Rrc opening image file '%s'"), rc, pszFilename);
|
---|
6517 | break;
|
---|
6518 | }
|
---|
6519 | }
|
---|
6520 |
|
---|
6521 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
6522 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6523 | AssertRC(rc2);
|
---|
6524 | fLockWrite = true;
|
---|
6525 |
|
---|
6526 | pImage->VDIo.pBackendData = pImage->pBackendData;
|
---|
6527 |
|
---|
6528 | /* Check image type. As the image itself has only partial knowledge
|
---|
6529 | * whether it's a base image or not, this info is derived here. The
|
---|
6530 | * base image can be fixed or normal, all others must be normal or
|
---|
6531 | * diff images. Some image formats don't distinguish between normal
|
---|
6532 | * and diff images, so this must be corrected here. */
|
---|
6533 | unsigned uImageFlags;
|
---|
6534 | uImageFlags = pImage->Backend->pfnGetImageFlags(pImage->pBackendData);
|
---|
6535 | if (RT_FAILURE(rc))
|
---|
6536 | uImageFlags = VD_IMAGE_FLAGS_NONE;
|
---|
6537 | if ( RT_SUCCESS(rc)
|
---|
6538 | && !(uOpenFlags & VD_OPEN_FLAGS_INFO))
|
---|
6539 | {
|
---|
6540 | if ( pDisk->cImages == 0
|
---|
6541 | && (uImageFlags & VD_IMAGE_FLAGS_DIFF))
|
---|
6542 | {
|
---|
6543 | rc = VERR_VD_INVALID_TYPE;
|
---|
6544 | break;
|
---|
6545 | }
|
---|
6546 | else if (pDisk->cImages != 0)
|
---|
6547 | {
|
---|
6548 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
6549 | {
|
---|
6550 | rc = VERR_VD_INVALID_TYPE;
|
---|
6551 | break;
|
---|
6552 | }
|
---|
6553 | else
|
---|
6554 | uImageFlags |= VD_IMAGE_FLAGS_DIFF;
|
---|
6555 | }
|
---|
6556 | }
|
---|
6557 |
|
---|
6558 | /* Ensure we always get correct diff information, even if the backend
|
---|
6559 | * doesn't actually have a stored flag for this. It must not return
|
---|
6560 | * bogus information for the parent UUID if it is not a diff image. */
|
---|
6561 | RTUUID parentUuid;
|
---|
6562 | RTUuidClear(&parentUuid);
|
---|
6563 | rc2 = pImage->Backend->pfnGetParentUuid(pImage->pBackendData, &parentUuid);
|
---|
6564 | if (RT_SUCCESS(rc2) && !RTUuidIsNull(&parentUuid))
|
---|
6565 | uImageFlags |= VD_IMAGE_FLAGS_DIFF;
|
---|
6566 |
|
---|
6567 | pImage->uImageFlags = uImageFlags;
|
---|
6568 |
|
---|
6569 | /* Force sane optimization settings. It's not worth avoiding writes
|
---|
6570 | * to fixed size images. The overhead would have almost no payback. */
|
---|
6571 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
6572 | pImage->uOpenFlags |= VD_OPEN_FLAGS_HONOR_SAME;
|
---|
6573 |
|
---|
6574 | /** @todo optionally check UUIDs */
|
---|
6575 |
|
---|
6576 | /* Cache disk information. */
|
---|
6577 | pDisk->cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
|
---|
6578 |
|
---|
6579 | /* Cache PCHS geometry. */
|
---|
6580 | rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
6581 | &pDisk->PCHSGeometry);
|
---|
6582 | if (RT_FAILURE(rc2))
|
---|
6583 | {
|
---|
6584 | pDisk->PCHSGeometry.cCylinders = 0;
|
---|
6585 | pDisk->PCHSGeometry.cHeads = 0;
|
---|
6586 | pDisk->PCHSGeometry.cSectors = 0;
|
---|
6587 | }
|
---|
6588 | else
|
---|
6589 | {
|
---|
6590 | /* Make sure the PCHS geometry is properly clipped. */
|
---|
6591 | pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
|
---|
6592 | pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
|
---|
6593 | pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
|
---|
6594 | }
|
---|
6595 |
|
---|
6596 | /* Cache LCHS geometry. */
|
---|
6597 | rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
6598 | &pDisk->LCHSGeometry);
|
---|
6599 | if (RT_FAILURE(rc2))
|
---|
6600 | {
|
---|
6601 | pDisk->LCHSGeometry.cCylinders = 0;
|
---|
6602 | pDisk->LCHSGeometry.cHeads = 0;
|
---|
6603 | pDisk->LCHSGeometry.cSectors = 0;
|
---|
6604 | }
|
---|
6605 | else
|
---|
6606 | {
|
---|
6607 | /* Make sure the LCHS geometry is properly clipped. */
|
---|
6608 | pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
|
---|
6609 | pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
|
---|
6610 | }
|
---|
6611 |
|
---|
6612 | if (pDisk->cImages != 0)
|
---|
6613 | {
|
---|
6614 | /* Switch previous image to read-only mode. */
|
---|
6615 | unsigned uOpenFlagsPrevImg;
|
---|
6616 | uOpenFlagsPrevImg = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pBackendData);
|
---|
6617 | if (!(uOpenFlagsPrevImg & VD_OPEN_FLAGS_READONLY))
|
---|
6618 | {
|
---|
6619 | uOpenFlagsPrevImg |= VD_OPEN_FLAGS_READONLY;
|
---|
6620 | rc = pDisk->pLast->Backend->pfnSetOpenFlags(pDisk->pLast->pBackendData, uOpenFlagsPrevImg);
|
---|
6621 | }
|
---|
6622 | }
|
---|
6623 |
|
---|
6624 | if (RT_SUCCESS(rc))
|
---|
6625 | {
|
---|
6626 | /* Image successfully opened, make it the last image. */
|
---|
6627 | vdAddImageToList(pDisk, pImage);
|
---|
6628 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
6629 | pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
|
---|
6630 | }
|
---|
6631 | else
|
---|
6632 | {
|
---|
6633 | /* Error detected, but image opened. Close image. */
|
---|
6634 | rc2 = pImage->Backend->pfnClose(pImage->pBackendData, false);
|
---|
6635 | AssertRC(rc2);
|
---|
6636 | pImage->pBackendData = NULL;
|
---|
6637 | }
|
---|
6638 | } while (0);
|
---|
6639 |
|
---|
6640 | if (RT_UNLIKELY(fLockWrite))
|
---|
6641 | {
|
---|
6642 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6643 | AssertRC(rc2);
|
---|
6644 | }
|
---|
6645 |
|
---|
6646 | if (RT_FAILURE(rc))
|
---|
6647 | {
|
---|
6648 | if (pImage)
|
---|
6649 | {
|
---|
6650 | if (pImage->pszFilename)
|
---|
6651 | RTStrFree(pImage->pszFilename);
|
---|
6652 | RTMemFree(pImage);
|
---|
6653 | }
|
---|
6654 | }
|
---|
6655 |
|
---|
6656 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6657 | return rc;
|
---|
6658 | }
|
---|
6659 |
|
---|
6660 | /**
|
---|
6661 | * Opens a cache image.
|
---|
6662 | *
|
---|
6663 | * @return VBox status code.
|
---|
6664 | * @param pDisk Pointer to the HDD container which should use the cache image.
|
---|
6665 | * @param pszBackend Name of the cache file backend to use (case insensitive).
|
---|
6666 | * @param pszFilename Name of the cache image to open.
|
---|
6667 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
6668 | * @param pVDIfsCache Pointer to the per-cache VD interface list.
|
---|
6669 | */
|
---|
6670 | VBOXDDU_DECL(int) VDCacheOpen(PVBOXHDD pDisk, const char *pszBackend,
|
---|
6671 | const char *pszFilename, unsigned uOpenFlags,
|
---|
6672 | PVDINTERFACE pVDIfsCache)
|
---|
6673 | {
|
---|
6674 | int rc = VINF_SUCCESS;
|
---|
6675 | int rc2;
|
---|
6676 | bool fLockWrite = false;
|
---|
6677 | PVDCACHE pCache = NULL;
|
---|
6678 |
|
---|
6679 | LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uOpenFlags=%#x, pVDIfsCache=%#p\n",
|
---|
6680 | pDisk, pszBackend, pszFilename, uOpenFlags, pVDIfsCache));
|
---|
6681 |
|
---|
6682 | do
|
---|
6683 | {
|
---|
6684 | /* sanity check */
|
---|
6685 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
6686 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6687 |
|
---|
6688 | /* Check arguments. */
|
---|
6689 | AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
|
---|
6690 | ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
|
---|
6691 | rc = VERR_INVALID_PARAMETER);
|
---|
6692 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
6693 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
6694 | rc = VERR_INVALID_PARAMETER);
|
---|
6695 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
6696 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
6697 | rc = VERR_INVALID_PARAMETER);
|
---|
6698 |
|
---|
6699 | /* Set up image descriptor. */
|
---|
6700 | pCache = (PVDCACHE)RTMemAllocZ(sizeof(VDCACHE));
|
---|
6701 | if (!pCache)
|
---|
6702 | {
|
---|
6703 | rc = VERR_NO_MEMORY;
|
---|
6704 | break;
|
---|
6705 | }
|
---|
6706 | pCache->pszFilename = RTStrDup(pszFilename);
|
---|
6707 | if (!pCache->pszFilename)
|
---|
6708 | {
|
---|
6709 | rc = VERR_NO_MEMORY;
|
---|
6710 | break;
|
---|
6711 | }
|
---|
6712 |
|
---|
6713 | pCache->VDIo.pDisk = pDisk;
|
---|
6714 | pCache->pVDIfsCache = pVDIfsCache;
|
---|
6715 |
|
---|
6716 | rc = vdFindCacheBackend(pszBackend, &pCache->Backend);
|
---|
6717 | if (RT_FAILURE(rc))
|
---|
6718 | break;
|
---|
6719 | if (!pCache->Backend)
|
---|
6720 | {
|
---|
6721 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
6722 | N_("VD: unknown backend name '%s'"), pszBackend);
|
---|
6723 | break;
|
---|
6724 | }
|
---|
6725 |
|
---|
6726 | /* Set up the I/O interface. */
|
---|
6727 | pCache->VDIo.pInterfaceIo = VDIfIoGet(pVDIfsCache);
|
---|
6728 | if (!pCache->VDIo.pInterfaceIo)
|
---|
6729 | {
|
---|
6730 | vdIfIoFallbackCallbacksSetup(&pCache->VDIo.VDIfIo);
|
---|
6731 | rc = VDInterfaceAdd(&pCache->VDIo.VDIfIo.Core, "VD_IO", VDINTERFACETYPE_IO,
|
---|
6732 | pDisk, sizeof(VDINTERFACEIO), &pVDIfsCache);
|
---|
6733 | pCache->VDIo.pInterfaceIo = &pCache->VDIo.VDIfIo;
|
---|
6734 | }
|
---|
6735 |
|
---|
6736 | /* Set up the internal I/O interface. */
|
---|
6737 | AssertBreakStmt(!VDIfIoIntGet(pVDIfsCache), rc = VERR_INVALID_PARAMETER);
|
---|
6738 | vdIfIoIntCallbacksSetup(&pCache->VDIo.VDIfIoInt);
|
---|
6739 | rc = VDInterfaceAdd(&pCache->VDIo.VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
6740 | &pCache->VDIo, sizeof(VDINTERFACEIOINT), &pCache->pVDIfsCache);
|
---|
6741 | AssertRC(rc);
|
---|
6742 |
|
---|
6743 | pCache->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
|
---|
6744 | rc = pCache->Backend->pfnOpen(pCache->pszFilename,
|
---|
6745 | uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
|
---|
6746 | pDisk->pVDIfsDisk,
|
---|
6747 | pCache->pVDIfsCache,
|
---|
6748 | &pCache->pBackendData);
|
---|
6749 | /* If the open in read-write mode failed, retry in read-only mode. */
|
---|
6750 | if (RT_FAILURE(rc))
|
---|
6751 | {
|
---|
6752 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
6753 | && ( rc == VERR_ACCESS_DENIED
|
---|
6754 | || rc == VERR_PERMISSION_DENIED
|
---|
6755 | || rc == VERR_WRITE_PROTECT
|
---|
6756 | || rc == VERR_SHARING_VIOLATION
|
---|
6757 | || rc == VERR_FILE_LOCK_FAILED))
|
---|
6758 | rc = pCache->Backend->pfnOpen(pCache->pszFilename,
|
---|
6759 | (uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME)
|
---|
6760 | | VD_OPEN_FLAGS_READONLY,
|
---|
6761 | pDisk->pVDIfsDisk,
|
---|
6762 | pCache->pVDIfsCache,
|
---|
6763 | &pCache->pBackendData);
|
---|
6764 | if (RT_FAILURE(rc))
|
---|
6765 | {
|
---|
6766 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
6767 | N_("VD: error %Rrc opening image file '%s'"), rc, pszFilename);
|
---|
6768 | break;
|
---|
6769 | }
|
---|
6770 | }
|
---|
6771 |
|
---|
6772 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
6773 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6774 | AssertRC(rc2);
|
---|
6775 | fLockWrite = true;
|
---|
6776 |
|
---|
6777 | /*
|
---|
6778 | * Check that the modification UUID of the cache and last image
|
---|
6779 | * match. If not the image was modified in-between without the cache.
|
---|
6780 | * The cache might contain stale data.
|
---|
6781 | */
|
---|
6782 | RTUUID UuidImage, UuidCache;
|
---|
6783 |
|
---|
6784 | rc = pCache->Backend->pfnGetModificationUuid(pCache->pBackendData,
|
---|
6785 | &UuidCache);
|
---|
6786 | if (RT_SUCCESS(rc))
|
---|
6787 | {
|
---|
6788 | rc = pDisk->pLast->Backend->pfnGetModificationUuid(pDisk->pLast->pBackendData,
|
---|
6789 | &UuidImage);
|
---|
6790 | if (RT_SUCCESS(rc))
|
---|
6791 | {
|
---|
6792 | if (RTUuidCompare(&UuidImage, &UuidCache))
|
---|
6793 | rc = VERR_VD_CACHE_NOT_UP_TO_DATE;
|
---|
6794 | }
|
---|
6795 | }
|
---|
6796 |
|
---|
6797 | /*
|
---|
6798 | * We assume that the user knows what he is doing if one of the images
|
---|
6799 | * doesn't support the modification uuid.
|
---|
6800 | */
|
---|
6801 | if (rc == VERR_NOT_SUPPORTED)
|
---|
6802 | rc = VINF_SUCCESS;
|
---|
6803 |
|
---|
6804 | if (RT_SUCCESS(rc))
|
---|
6805 | {
|
---|
6806 | /* Cache successfully opened, make it the current one. */
|
---|
6807 | if (!pDisk->pCache)
|
---|
6808 | pDisk->pCache = pCache;
|
---|
6809 | else
|
---|
6810 | rc = VERR_VD_CACHE_ALREADY_EXISTS;
|
---|
6811 | }
|
---|
6812 |
|
---|
6813 | if (RT_FAILURE(rc))
|
---|
6814 | {
|
---|
6815 | /* Error detected, but image opened. Close image. */
|
---|
6816 | rc2 = pCache->Backend->pfnClose(pCache->pBackendData, false);
|
---|
6817 | AssertRC(rc2);
|
---|
6818 | pCache->pBackendData = NULL;
|
---|
6819 | }
|
---|
6820 | } while (0);
|
---|
6821 |
|
---|
6822 | if (RT_UNLIKELY(fLockWrite))
|
---|
6823 | {
|
---|
6824 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6825 | AssertRC(rc2);
|
---|
6826 | }
|
---|
6827 |
|
---|
6828 | if (RT_FAILURE(rc))
|
---|
6829 | {
|
---|
6830 | if (pCache)
|
---|
6831 | {
|
---|
6832 | if (pCache->pszFilename)
|
---|
6833 | RTStrFree(pCache->pszFilename);
|
---|
6834 | RTMemFree(pCache);
|
---|
6835 | }
|
---|
6836 | }
|
---|
6837 |
|
---|
6838 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6839 | return rc;
|
---|
6840 | }
|
---|
6841 |
|
---|
6842 | VBOXDDU_DECL(int) VDFilterAdd(PVBOXHDD pDisk, const char *pszFilter, uint32_t fFlags,
|
---|
6843 | PVDINTERFACE pVDIfsFilter)
|
---|
6844 | {
|
---|
6845 | int rc = VINF_SUCCESS;
|
---|
6846 | int rc2;
|
---|
6847 | bool fLockWrite = false;
|
---|
6848 | PVDFILTER pFilter = NULL;
|
---|
6849 |
|
---|
6850 | LogFlowFunc(("pDisk=%#p pszFilter=\"%s\" pVDIfsFilter=%#p\n",
|
---|
6851 | pDisk, pszFilter, pVDIfsFilter));
|
---|
6852 |
|
---|
6853 | do
|
---|
6854 | {
|
---|
6855 | /* sanity check */
|
---|
6856 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
6857 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6858 |
|
---|
6859 | /* Check arguments. */
|
---|
6860 | AssertMsgBreakStmt(VALID_PTR(pszFilter) && *pszFilter,
|
---|
6861 | ("pszFilter=%#p \"%s\"\n", pszFilter, pszFilter),
|
---|
6862 | rc = VERR_INVALID_PARAMETER);
|
---|
6863 |
|
---|
6864 | AssertMsgBreakStmt(!(fFlags & ~VD_FILTER_FLAGS_MASK),
|
---|
6865 | ("Invalid flags set (fFlags=%#x)\n", fFlags),
|
---|
6866 | rc = VERR_INVALID_PARAMETER);
|
---|
6867 |
|
---|
6868 | /* Set up image descriptor. */
|
---|
6869 | pFilter = (PVDFILTER)RTMemAllocZ(sizeof(VDFILTER));
|
---|
6870 | if (!pFilter)
|
---|
6871 | {
|
---|
6872 | rc = VERR_NO_MEMORY;
|
---|
6873 | break;
|
---|
6874 | }
|
---|
6875 |
|
---|
6876 | rc = vdFindFilterBackend(pszFilter, &pFilter->pBackend);
|
---|
6877 | if (RT_FAILURE(rc))
|
---|
6878 | break;
|
---|
6879 | if (!pFilter->pBackend)
|
---|
6880 | {
|
---|
6881 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
6882 | N_("VD: unknown filter backend name '%s'"), pszFilter);
|
---|
6883 | break;
|
---|
6884 | }
|
---|
6885 |
|
---|
6886 | pFilter->VDIo.pDisk = pDisk;
|
---|
6887 | pFilter->pVDIfsFilter = pVDIfsFilter;
|
---|
6888 |
|
---|
6889 | /* Set up the internal I/O interface. */
|
---|
6890 | AssertBreakStmt(!VDIfIoIntGet(pVDIfsFilter), rc = VERR_INVALID_PARAMETER);
|
---|
6891 | vdIfIoIntCallbacksSetup(&pFilter->VDIo.VDIfIoInt);
|
---|
6892 | rc = VDInterfaceAdd(&pFilter->VDIo.VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
6893 | &pFilter->VDIo, sizeof(VDINTERFACEIOINT), &pFilter->pVDIfsFilter);
|
---|
6894 | AssertRC(rc);
|
---|
6895 |
|
---|
6896 | rc = pFilter->pBackend->pfnCreate(pDisk->pVDIfsDisk, fFlags & VD_FILTER_FLAGS_INFO,
|
---|
6897 | pFilter->pVDIfsFilter, &pFilter->pvBackendData);
|
---|
6898 | if (RT_FAILURE(rc))
|
---|
6899 | break;
|
---|
6900 |
|
---|
6901 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
6902 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6903 | AssertRC(rc2);
|
---|
6904 | fLockWrite = true;
|
---|
6905 |
|
---|
6906 | /* Add filter to chains. */
|
---|
6907 | if (fFlags & VD_FILTER_FLAGS_WRITE)
|
---|
6908 | {
|
---|
6909 | RTListAppend(&pDisk->ListFilterChainWrite, &pFilter->ListNodeChainWrite);
|
---|
6910 | vdFilterRetain(pFilter);
|
---|
6911 | }
|
---|
6912 |
|
---|
6913 | if (fFlags & VD_FILTER_FLAGS_READ)
|
---|
6914 | {
|
---|
6915 | RTListAppend(&pDisk->ListFilterChainRead, &pFilter->ListNodeChainRead);
|
---|
6916 | vdFilterRetain(pFilter);
|
---|
6917 | }
|
---|
6918 | } while (0);
|
---|
6919 |
|
---|
6920 | if (RT_UNLIKELY(fLockWrite))
|
---|
6921 | {
|
---|
6922 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6923 | AssertRC(rc2);
|
---|
6924 | }
|
---|
6925 |
|
---|
6926 | if (RT_FAILURE(rc))
|
---|
6927 | {
|
---|
6928 | if (pFilter)
|
---|
6929 | RTMemFree(pFilter);
|
---|
6930 | }
|
---|
6931 |
|
---|
6932 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6933 | return rc;
|
---|
6934 | }
|
---|
6935 |
|
---|
6936 | /**
|
---|
6937 | * Creates and opens a new base image file.
|
---|
6938 | *
|
---|
6939 | * @returns VBox status code.
|
---|
6940 | * @param pDisk Pointer to HDD container.
|
---|
6941 | * @param pszBackend Name of the image file backend to use.
|
---|
6942 | * @param pszFilename Name of the image file to create.
|
---|
6943 | * @param cbSize Image size in bytes.
|
---|
6944 | * @param uImageFlags Flags specifying special image features.
|
---|
6945 | * @param pszComment Pointer to image comment. NULL is ok.
|
---|
6946 | * @param pPCHSGeometry Pointer to physical disk geometry <= (16383,16,63). Not NULL.
|
---|
6947 | * @param pLCHSGeometry Pointer to logical disk geometry <= (x,255,63). Not NULL.
|
---|
6948 | * @param pUuid New UUID of the image. If NULL, a new UUID is created.
|
---|
6949 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
6950 | * @param pVDIfsImage Pointer to the per-image VD interface list.
|
---|
6951 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
6952 | */
|
---|
6953 | VBOXDDU_DECL(int) VDCreateBase(PVBOXHDD pDisk, const char *pszBackend,
|
---|
6954 | const char *pszFilename, uint64_t cbSize,
|
---|
6955 | unsigned uImageFlags, const char *pszComment,
|
---|
6956 | PCVDGEOMETRY pPCHSGeometry,
|
---|
6957 | PCVDGEOMETRY pLCHSGeometry,
|
---|
6958 | PCRTUUID pUuid, unsigned uOpenFlags,
|
---|
6959 | PVDINTERFACE pVDIfsImage,
|
---|
6960 | PVDINTERFACE pVDIfsOperation)
|
---|
6961 | {
|
---|
6962 | int rc = VINF_SUCCESS;
|
---|
6963 | int rc2;
|
---|
6964 | bool fLockWrite = false, fLockRead = false;
|
---|
6965 | PVDIMAGE pImage = NULL;
|
---|
6966 | RTUUID uuid;
|
---|
6967 |
|
---|
6968 | 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",
|
---|
6969 | pDisk, pszBackend, pszFilename, cbSize, uImageFlags, pszComment,
|
---|
6970 | pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
|
---|
6971 | pPCHSGeometry->cSectors, pLCHSGeometry->cCylinders,
|
---|
6972 | pLCHSGeometry->cHeads, pLCHSGeometry->cSectors, pUuid,
|
---|
6973 | uOpenFlags, pVDIfsImage, pVDIfsOperation));
|
---|
6974 |
|
---|
6975 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
6976 |
|
---|
6977 | do
|
---|
6978 | {
|
---|
6979 | /* sanity check */
|
---|
6980 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
6981 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6982 |
|
---|
6983 | /* Check arguments. */
|
---|
6984 | AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
|
---|
6985 | ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
|
---|
6986 | rc = VERR_INVALID_PARAMETER);
|
---|
6987 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
6988 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
6989 | rc = VERR_INVALID_PARAMETER);
|
---|
6990 | AssertMsgBreakStmt(cbSize,
|
---|
6991 | ("cbSize=%llu\n", cbSize),
|
---|
6992 | rc = VERR_INVALID_PARAMETER);
|
---|
6993 | AssertMsgBreakStmt( ((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0)
|
---|
6994 | || ((uImageFlags & (VD_IMAGE_FLAGS_FIXED | VD_IMAGE_FLAGS_DIFF)) != VD_IMAGE_FLAGS_FIXED),
|
---|
6995 | ("uImageFlags=%#x\n", uImageFlags),
|
---|
6996 | rc = VERR_INVALID_PARAMETER);
|
---|
6997 | /* The PCHS geometry fields may be 0 to leave it for later. */
|
---|
6998 | AssertMsgBreakStmt( VALID_PTR(pPCHSGeometry)
|
---|
6999 | && pPCHSGeometry->cHeads <= 16
|
---|
7000 | && pPCHSGeometry->cSectors <= 63,
|
---|
7001 | ("pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pPCHSGeometry,
|
---|
7002 | pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
|
---|
7003 | pPCHSGeometry->cSectors),
|
---|
7004 | rc = VERR_INVALID_PARAMETER);
|
---|
7005 | /* The LCHS geometry fields may be 0 to leave it to later autodetection. */
|
---|
7006 | AssertMsgBreakStmt( VALID_PTR(pLCHSGeometry)
|
---|
7007 | && pLCHSGeometry->cHeads <= 255
|
---|
7008 | && pLCHSGeometry->cSectors <= 63,
|
---|
7009 | ("pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pLCHSGeometry,
|
---|
7010 | pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads,
|
---|
7011 | pLCHSGeometry->cSectors),
|
---|
7012 | rc = VERR_INVALID_PARAMETER);
|
---|
7013 | /* The UUID may be NULL. */
|
---|
7014 | AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
|
---|
7015 | ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
|
---|
7016 | rc = VERR_INVALID_PARAMETER);
|
---|
7017 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
7018 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
7019 | rc = VERR_INVALID_PARAMETER);
|
---|
7020 |
|
---|
7021 | /* Check state. Needs a temporary read lock. Holding the write lock
|
---|
7022 | * all the time would be blocking other activities for too long. */
|
---|
7023 | rc2 = vdThreadStartRead(pDisk);
|
---|
7024 | AssertRC(rc2);
|
---|
7025 | fLockRead = true;
|
---|
7026 | AssertMsgBreakStmt(pDisk->cImages == 0,
|
---|
7027 | ("Create base image cannot be done with other images open\n"),
|
---|
7028 | rc = VERR_VD_INVALID_STATE);
|
---|
7029 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7030 | AssertRC(rc2);
|
---|
7031 | fLockRead = false;
|
---|
7032 |
|
---|
7033 | /* Set up image descriptor. */
|
---|
7034 | pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
|
---|
7035 | if (!pImage)
|
---|
7036 | {
|
---|
7037 | rc = VERR_NO_MEMORY;
|
---|
7038 | break;
|
---|
7039 | }
|
---|
7040 | pImage->pszFilename = RTStrDup(pszFilename);
|
---|
7041 | if (!pImage->pszFilename)
|
---|
7042 | {
|
---|
7043 | rc = VERR_NO_MEMORY;
|
---|
7044 | break;
|
---|
7045 | }
|
---|
7046 | pImage->VDIo.pDisk = pDisk;
|
---|
7047 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
7048 |
|
---|
7049 | /* Set up the I/O interface. */
|
---|
7050 | pImage->VDIo.pInterfaceIo = VDIfIoGet(pVDIfsImage);
|
---|
7051 | if (!pImage->VDIo.pInterfaceIo)
|
---|
7052 | {
|
---|
7053 | vdIfIoFallbackCallbacksSetup(&pImage->VDIo.VDIfIo);
|
---|
7054 | rc = VDInterfaceAdd(&pImage->VDIo.VDIfIo.Core, "VD_IO", VDINTERFACETYPE_IO,
|
---|
7055 | pDisk, sizeof(VDINTERFACEIO), &pVDIfsImage);
|
---|
7056 | pImage->VDIo.pInterfaceIo = &pImage->VDIo.VDIfIo;
|
---|
7057 | }
|
---|
7058 |
|
---|
7059 | /* Set up the internal I/O interface. */
|
---|
7060 | AssertBreakStmt(!VDIfIoIntGet(pVDIfsImage), rc = VERR_INVALID_PARAMETER);
|
---|
7061 | vdIfIoIntCallbacksSetup(&pImage->VDIo.VDIfIoInt);
|
---|
7062 | rc = VDInterfaceAdd(&pImage->VDIo.VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
7063 | &pImage->VDIo, sizeof(VDINTERFACEIOINT), &pImage->pVDIfsImage);
|
---|
7064 | AssertRC(rc);
|
---|
7065 |
|
---|
7066 | rc = vdFindBackend(pszBackend, &pImage->Backend);
|
---|
7067 | if (RT_FAILURE(rc))
|
---|
7068 | break;
|
---|
7069 | if (!pImage->Backend)
|
---|
7070 | {
|
---|
7071 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
7072 | N_("VD: unknown backend name '%s'"), pszBackend);
|
---|
7073 | break;
|
---|
7074 | }
|
---|
7075 | if (!(pImage->Backend->uBackendCaps & ( VD_CAP_CREATE_FIXED
|
---|
7076 | | VD_CAP_CREATE_DYNAMIC)))
|
---|
7077 | {
|
---|
7078 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
7079 | N_("VD: backend '%s' cannot create base images"), pszBackend);
|
---|
7080 | break;
|
---|
7081 | }
|
---|
7082 |
|
---|
7083 | /* Create UUID if the caller didn't specify one. */
|
---|
7084 | if (!pUuid)
|
---|
7085 | {
|
---|
7086 | rc = RTUuidCreate(&uuid);
|
---|
7087 | if (RT_FAILURE(rc))
|
---|
7088 | {
|
---|
7089 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
7090 | N_("VD: cannot generate UUID for image '%s'"),
|
---|
7091 | pszFilename);
|
---|
7092 | break;
|
---|
7093 | }
|
---|
7094 | pUuid = &uuid;
|
---|
7095 | }
|
---|
7096 |
|
---|
7097 | pImage->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
|
---|
7098 | uImageFlags &= ~VD_IMAGE_FLAGS_DIFF;
|
---|
7099 | pImage->VDIo.fIgnoreFlush = (uOpenFlags & VD_OPEN_FLAGS_IGNORE_FLUSH) != 0;
|
---|
7100 | rc = pImage->Backend->pfnCreate(pImage->pszFilename, cbSize,
|
---|
7101 | uImageFlags, pszComment, pPCHSGeometry,
|
---|
7102 | pLCHSGeometry, pUuid,
|
---|
7103 | uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
|
---|
7104 | 0, 99,
|
---|
7105 | pDisk->pVDIfsDisk,
|
---|
7106 | pImage->pVDIfsImage,
|
---|
7107 | pVDIfsOperation,
|
---|
7108 | pDisk->enmType,
|
---|
7109 | &pImage->pBackendData);
|
---|
7110 |
|
---|
7111 | if (RT_SUCCESS(rc))
|
---|
7112 | {
|
---|
7113 | pImage->VDIo.pBackendData = pImage->pBackendData;
|
---|
7114 | pImage->uImageFlags = uImageFlags;
|
---|
7115 |
|
---|
7116 | /* Force sane optimization settings. It's not worth avoiding writes
|
---|
7117 | * to fixed size images. The overhead would have almost no payback. */
|
---|
7118 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
7119 | pImage->uOpenFlags |= VD_OPEN_FLAGS_HONOR_SAME;
|
---|
7120 |
|
---|
7121 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
7122 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7123 | AssertRC(rc2);
|
---|
7124 | fLockWrite = true;
|
---|
7125 |
|
---|
7126 | /** @todo optionally check UUIDs */
|
---|
7127 |
|
---|
7128 | /* Re-check state, as the lock wasn't held and another image
|
---|
7129 | * creation call could have been done by another thread. */
|
---|
7130 | AssertMsgStmt(pDisk->cImages == 0,
|
---|
7131 | ("Create base image cannot be done with other images open\n"),
|
---|
7132 | rc = VERR_VD_INVALID_STATE);
|
---|
7133 | }
|
---|
7134 |
|
---|
7135 | if (RT_SUCCESS(rc))
|
---|
7136 | {
|
---|
7137 | /* Cache disk information. */
|
---|
7138 | pDisk->cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
|
---|
7139 |
|
---|
7140 | /* Cache PCHS geometry. */
|
---|
7141 | rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
7142 | &pDisk->PCHSGeometry);
|
---|
7143 | if (RT_FAILURE(rc2))
|
---|
7144 | {
|
---|
7145 | pDisk->PCHSGeometry.cCylinders = 0;
|
---|
7146 | pDisk->PCHSGeometry.cHeads = 0;
|
---|
7147 | pDisk->PCHSGeometry.cSectors = 0;
|
---|
7148 | }
|
---|
7149 | else
|
---|
7150 | {
|
---|
7151 | /* Make sure the CHS geometry is properly clipped. */
|
---|
7152 | pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
|
---|
7153 | pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
|
---|
7154 | pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
|
---|
7155 | }
|
---|
7156 |
|
---|
7157 | /* Cache LCHS geometry. */
|
---|
7158 | rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
7159 | &pDisk->LCHSGeometry);
|
---|
7160 | if (RT_FAILURE(rc2))
|
---|
7161 | {
|
---|
7162 | pDisk->LCHSGeometry.cCylinders = 0;
|
---|
7163 | pDisk->LCHSGeometry.cHeads = 0;
|
---|
7164 | pDisk->LCHSGeometry.cSectors = 0;
|
---|
7165 | }
|
---|
7166 | else
|
---|
7167 | {
|
---|
7168 | /* Make sure the CHS geometry is properly clipped. */
|
---|
7169 | pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
|
---|
7170 | pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
|
---|
7171 | }
|
---|
7172 |
|
---|
7173 | /* Image successfully opened, make it the last image. */
|
---|
7174 | vdAddImageToList(pDisk, pImage);
|
---|
7175 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
7176 | pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
|
---|
7177 | }
|
---|
7178 | else
|
---|
7179 | {
|
---|
7180 | /* Error detected, image may or may not be opened. Close and delete
|
---|
7181 | * image if it was opened. */
|
---|
7182 | if (pImage->pBackendData)
|
---|
7183 | {
|
---|
7184 | rc2 = pImage->Backend->pfnClose(pImage->pBackendData, true);
|
---|
7185 | AssertRC(rc2);
|
---|
7186 | pImage->pBackendData = NULL;
|
---|
7187 | }
|
---|
7188 | }
|
---|
7189 | } while (0);
|
---|
7190 |
|
---|
7191 | if (RT_UNLIKELY(fLockWrite))
|
---|
7192 | {
|
---|
7193 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7194 | AssertRC(rc2);
|
---|
7195 | }
|
---|
7196 | else if (RT_UNLIKELY(fLockRead))
|
---|
7197 | {
|
---|
7198 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7199 | AssertRC(rc2);
|
---|
7200 | }
|
---|
7201 |
|
---|
7202 | if (RT_FAILURE(rc))
|
---|
7203 | {
|
---|
7204 | if (pImage)
|
---|
7205 | {
|
---|
7206 | if (pImage->pszFilename)
|
---|
7207 | RTStrFree(pImage->pszFilename);
|
---|
7208 | RTMemFree(pImage);
|
---|
7209 | }
|
---|
7210 | }
|
---|
7211 |
|
---|
7212 | if (RT_SUCCESS(rc) && pIfProgress && pIfProgress->pfnProgress)
|
---|
7213 | pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
|
---|
7214 |
|
---|
7215 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
7216 | return rc;
|
---|
7217 | }
|
---|
7218 |
|
---|
7219 | /**
|
---|
7220 | * Creates and opens a new differencing image file in HDD container.
|
---|
7221 | * See comments for VDOpen function about differencing images.
|
---|
7222 | *
|
---|
7223 | * @returns VBox status code.
|
---|
7224 | * @param pDisk Pointer to HDD container.
|
---|
7225 | * @param pszBackend Name of the image file backend to use.
|
---|
7226 | * @param pszFilename Name of the differencing image file to create.
|
---|
7227 | * @param uImageFlags Flags specifying special image features.
|
---|
7228 | * @param pszComment Pointer to image comment. NULL is ok.
|
---|
7229 | * @param pUuid New UUID of the image. If NULL, a new UUID is created.
|
---|
7230 | * @param pParentUuid New parent UUID of the image. If NULL, the UUID is queried automatically.
|
---|
7231 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
7232 | * @param pVDIfsImage Pointer to the per-image VD interface list.
|
---|
7233 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
7234 | */
|
---|
7235 | VBOXDDU_DECL(int) VDCreateDiff(PVBOXHDD pDisk, const char *pszBackend,
|
---|
7236 | const char *pszFilename, unsigned uImageFlags,
|
---|
7237 | const char *pszComment, PCRTUUID pUuid,
|
---|
7238 | PCRTUUID pParentUuid, unsigned uOpenFlags,
|
---|
7239 | PVDINTERFACE pVDIfsImage,
|
---|
7240 | PVDINTERFACE pVDIfsOperation)
|
---|
7241 | {
|
---|
7242 | int rc = VINF_SUCCESS;
|
---|
7243 | int rc2;
|
---|
7244 | bool fLockWrite = false, fLockRead = false;
|
---|
7245 | PVDIMAGE pImage = NULL;
|
---|
7246 | RTUUID uuid;
|
---|
7247 |
|
---|
7248 | LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uImageFlags=%#x pszComment=\"%s\" Uuid=%RTuuid uOpenFlags=%#x pVDIfsImage=%#p pVDIfsOperation=%#p\n",
|
---|
7249 | pDisk, pszBackend, pszFilename, uImageFlags, pszComment, pUuid, uOpenFlags, pVDIfsImage, pVDIfsOperation));
|
---|
7250 |
|
---|
7251 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
7252 |
|
---|
7253 | do
|
---|
7254 | {
|
---|
7255 | /* sanity check */
|
---|
7256 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7257 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7258 |
|
---|
7259 | /* Check arguments. */
|
---|
7260 | AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
|
---|
7261 | ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
|
---|
7262 | rc = VERR_INVALID_PARAMETER);
|
---|
7263 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
7264 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
7265 | rc = VERR_INVALID_PARAMETER);
|
---|
7266 | AssertMsgBreakStmt((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0,
|
---|
7267 | ("uImageFlags=%#x\n", uImageFlags),
|
---|
7268 | rc = VERR_INVALID_PARAMETER);
|
---|
7269 | /* The UUID may be NULL. */
|
---|
7270 | AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
|
---|
7271 | ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
|
---|
7272 | rc = VERR_INVALID_PARAMETER);
|
---|
7273 | /* The parent UUID may be NULL. */
|
---|
7274 | AssertMsgBreakStmt(pParentUuid == NULL || VALID_PTR(pParentUuid),
|
---|
7275 | ("pParentUuid=%#p ParentUUID=%RTuuid\n", pParentUuid, pParentUuid),
|
---|
7276 | rc = VERR_INVALID_PARAMETER);
|
---|
7277 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
7278 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
7279 | rc = VERR_INVALID_PARAMETER);
|
---|
7280 |
|
---|
7281 | /* Check state. Needs a temporary read lock. Holding the write lock
|
---|
7282 | * all the time would be blocking other activities for too long. */
|
---|
7283 | rc2 = vdThreadStartRead(pDisk);
|
---|
7284 | AssertRC(rc2);
|
---|
7285 | fLockRead = true;
|
---|
7286 | AssertMsgBreakStmt(pDisk->cImages != 0,
|
---|
7287 | ("Create diff image cannot be done without other images open\n"),
|
---|
7288 | rc = VERR_VD_INVALID_STATE);
|
---|
7289 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7290 | AssertRC(rc2);
|
---|
7291 | fLockRead = false;
|
---|
7292 |
|
---|
7293 | /*
|
---|
7294 | * Destroy the current discard state first which might still have pending blocks
|
---|
7295 | * for the currently opened image which will be switched to readonly mode.
|
---|
7296 | */
|
---|
7297 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
7298 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7299 | AssertRC(rc2);
|
---|
7300 | fLockWrite = true;
|
---|
7301 | rc = vdDiscardStateDestroy(pDisk);
|
---|
7302 | if (RT_FAILURE(rc))
|
---|
7303 | break;
|
---|
7304 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7305 | AssertRC(rc2);
|
---|
7306 | fLockWrite = false;
|
---|
7307 |
|
---|
7308 | /* Set up image descriptor. */
|
---|
7309 | pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
|
---|
7310 | if (!pImage)
|
---|
7311 | {
|
---|
7312 | rc = VERR_NO_MEMORY;
|
---|
7313 | break;
|
---|
7314 | }
|
---|
7315 | pImage->pszFilename = RTStrDup(pszFilename);
|
---|
7316 | if (!pImage->pszFilename)
|
---|
7317 | {
|
---|
7318 | rc = VERR_NO_MEMORY;
|
---|
7319 | break;
|
---|
7320 | }
|
---|
7321 |
|
---|
7322 | rc = vdFindBackend(pszBackend, &pImage->Backend);
|
---|
7323 | if (RT_FAILURE(rc))
|
---|
7324 | break;
|
---|
7325 | if (!pImage->Backend)
|
---|
7326 | {
|
---|
7327 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
7328 | N_("VD: unknown backend name '%s'"), pszBackend);
|
---|
7329 | break;
|
---|
7330 | }
|
---|
7331 | if ( !(pImage->Backend->uBackendCaps & VD_CAP_DIFF)
|
---|
7332 | || !(pImage->Backend->uBackendCaps & ( VD_CAP_CREATE_FIXED
|
---|
7333 | | VD_CAP_CREATE_DYNAMIC)))
|
---|
7334 | {
|
---|
7335 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
7336 | N_("VD: backend '%s' cannot create diff images"), pszBackend);
|
---|
7337 | break;
|
---|
7338 | }
|
---|
7339 |
|
---|
7340 | pImage->VDIo.pDisk = pDisk;
|
---|
7341 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
7342 |
|
---|
7343 | /* Set up the I/O interface. */
|
---|
7344 | pImage->VDIo.pInterfaceIo = VDIfIoGet(pVDIfsImage);
|
---|
7345 | if (!pImage->VDIo.pInterfaceIo)
|
---|
7346 | {
|
---|
7347 | vdIfIoFallbackCallbacksSetup(&pImage->VDIo.VDIfIo);
|
---|
7348 | rc = VDInterfaceAdd(&pImage->VDIo.VDIfIo.Core, "VD_IO", VDINTERFACETYPE_IO,
|
---|
7349 | pDisk, sizeof(VDINTERFACEIO), &pVDIfsImage);
|
---|
7350 | pImage->VDIo.pInterfaceIo = &pImage->VDIo.VDIfIo;
|
---|
7351 | }
|
---|
7352 |
|
---|
7353 | /* Set up the internal I/O interface. */
|
---|
7354 | AssertBreakStmt(!VDIfIoIntGet(pVDIfsImage), rc = VERR_INVALID_PARAMETER);
|
---|
7355 | vdIfIoIntCallbacksSetup(&pImage->VDIo.VDIfIoInt);
|
---|
7356 | rc = VDInterfaceAdd(&pImage->VDIo.VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
7357 | &pImage->VDIo, sizeof(VDINTERFACEIOINT), &pImage->pVDIfsImage);
|
---|
7358 | AssertRC(rc);
|
---|
7359 |
|
---|
7360 | /* Create UUID if the caller didn't specify one. */
|
---|
7361 | if (!pUuid)
|
---|
7362 | {
|
---|
7363 | rc = RTUuidCreate(&uuid);
|
---|
7364 | if (RT_FAILURE(rc))
|
---|
7365 | {
|
---|
7366 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
7367 | N_("VD: cannot generate UUID for image '%s'"),
|
---|
7368 | pszFilename);
|
---|
7369 | break;
|
---|
7370 | }
|
---|
7371 | pUuid = &uuid;
|
---|
7372 | }
|
---|
7373 |
|
---|
7374 | pImage->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
|
---|
7375 | pImage->VDIo.fIgnoreFlush = (uOpenFlags & VD_OPEN_FLAGS_IGNORE_FLUSH) != 0;
|
---|
7376 | uImageFlags |= VD_IMAGE_FLAGS_DIFF;
|
---|
7377 | rc = pImage->Backend->pfnCreate(pImage->pszFilename, pDisk->cbSize,
|
---|
7378 | uImageFlags | VD_IMAGE_FLAGS_DIFF,
|
---|
7379 | pszComment, &pDisk->PCHSGeometry,
|
---|
7380 | &pDisk->LCHSGeometry, pUuid,
|
---|
7381 | uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
|
---|
7382 | 0, 99,
|
---|
7383 | pDisk->pVDIfsDisk,
|
---|
7384 | pImage->pVDIfsImage,
|
---|
7385 | pVDIfsOperation,
|
---|
7386 | pDisk->enmType,
|
---|
7387 | &pImage->pBackendData);
|
---|
7388 |
|
---|
7389 | if (RT_SUCCESS(rc))
|
---|
7390 | {
|
---|
7391 | pImage->VDIo.pBackendData = pImage->pBackendData;
|
---|
7392 | pImage->uImageFlags = uImageFlags;
|
---|
7393 |
|
---|
7394 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
7395 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7396 | AssertRC(rc2);
|
---|
7397 | fLockWrite = true;
|
---|
7398 |
|
---|
7399 | /* Switch previous image to read-only mode. */
|
---|
7400 | unsigned uOpenFlagsPrevImg;
|
---|
7401 | uOpenFlagsPrevImg = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pBackendData);
|
---|
7402 | if (!(uOpenFlagsPrevImg & VD_OPEN_FLAGS_READONLY))
|
---|
7403 | {
|
---|
7404 | uOpenFlagsPrevImg |= VD_OPEN_FLAGS_READONLY;
|
---|
7405 | rc = pDisk->pLast->Backend->pfnSetOpenFlags(pDisk->pLast->pBackendData, uOpenFlagsPrevImg);
|
---|
7406 | }
|
---|
7407 |
|
---|
7408 | /** @todo optionally check UUIDs */
|
---|
7409 |
|
---|
7410 | /* Re-check state, as the lock wasn't held and another image
|
---|
7411 | * creation call could have been done by another thread. */
|
---|
7412 | AssertMsgStmt(pDisk->cImages != 0,
|
---|
7413 | ("Create diff image cannot be done without other images open\n"),
|
---|
7414 | rc = VERR_VD_INVALID_STATE);
|
---|
7415 | }
|
---|
7416 |
|
---|
7417 | if (RT_SUCCESS(rc))
|
---|
7418 | {
|
---|
7419 | RTUUID Uuid;
|
---|
7420 | RTTIMESPEC ts;
|
---|
7421 |
|
---|
7422 | if (pParentUuid && !RTUuidIsNull(pParentUuid))
|
---|
7423 | {
|
---|
7424 | Uuid = *pParentUuid;
|
---|
7425 | pImage->Backend->pfnSetParentUuid(pImage->pBackendData, &Uuid);
|
---|
7426 | }
|
---|
7427 | else
|
---|
7428 | {
|
---|
7429 | rc2 = pDisk->pLast->Backend->pfnGetUuid(pDisk->pLast->pBackendData,
|
---|
7430 | &Uuid);
|
---|
7431 | if (RT_SUCCESS(rc2))
|
---|
7432 | pImage->Backend->pfnSetParentUuid(pImage->pBackendData, &Uuid);
|
---|
7433 | }
|
---|
7434 | rc2 = pDisk->pLast->Backend->pfnGetModificationUuid(pDisk->pLast->pBackendData,
|
---|
7435 | &Uuid);
|
---|
7436 | if (RT_SUCCESS(rc2))
|
---|
7437 | pImage->Backend->pfnSetParentModificationUuid(pImage->pBackendData,
|
---|
7438 | &Uuid);
|
---|
7439 | if (pDisk->pLast->Backend->pfnGetTimestamp)
|
---|
7440 | rc2 = pDisk->pLast->Backend->pfnGetTimestamp(pDisk->pLast->pBackendData,
|
---|
7441 | &ts);
|
---|
7442 | else
|
---|
7443 | rc2 = VERR_NOT_IMPLEMENTED;
|
---|
7444 | if (RT_SUCCESS(rc2) && pImage->Backend->pfnSetParentTimestamp)
|
---|
7445 | pImage->Backend->pfnSetParentTimestamp(pImage->pBackendData, &ts);
|
---|
7446 |
|
---|
7447 | if (pImage->Backend->pfnSetParentFilename)
|
---|
7448 | rc2 = pImage->Backend->pfnSetParentFilename(pImage->pBackendData, pDisk->pLast->pszFilename);
|
---|
7449 | }
|
---|
7450 |
|
---|
7451 | if (RT_SUCCESS(rc))
|
---|
7452 | {
|
---|
7453 | /* Image successfully opened, make it the last image. */
|
---|
7454 | vdAddImageToList(pDisk, pImage);
|
---|
7455 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
7456 | pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
|
---|
7457 | }
|
---|
7458 | else
|
---|
7459 | {
|
---|
7460 | /* Error detected, but image opened. Close and delete image. */
|
---|
7461 | rc2 = pImage->Backend->pfnClose(pImage->pBackendData, true);
|
---|
7462 | AssertRC(rc2);
|
---|
7463 | pImage->pBackendData = NULL;
|
---|
7464 | }
|
---|
7465 | } while (0);
|
---|
7466 |
|
---|
7467 | if (RT_UNLIKELY(fLockWrite))
|
---|
7468 | {
|
---|
7469 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7470 | AssertRC(rc2);
|
---|
7471 | }
|
---|
7472 | else if (RT_UNLIKELY(fLockRead))
|
---|
7473 | {
|
---|
7474 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7475 | AssertRC(rc2);
|
---|
7476 | }
|
---|
7477 |
|
---|
7478 | if (RT_FAILURE(rc))
|
---|
7479 | {
|
---|
7480 | if (pImage)
|
---|
7481 | {
|
---|
7482 | if (pImage->pszFilename)
|
---|
7483 | RTStrFree(pImage->pszFilename);
|
---|
7484 | RTMemFree(pImage);
|
---|
7485 | }
|
---|
7486 | }
|
---|
7487 |
|
---|
7488 | if (RT_SUCCESS(rc) && pIfProgress && pIfProgress->pfnProgress)
|
---|
7489 | pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
|
---|
7490 |
|
---|
7491 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
7492 | return rc;
|
---|
7493 | }
|
---|
7494 |
|
---|
7495 |
|
---|
7496 | /**
|
---|
7497 | * Creates and opens new cache image file in HDD container.
|
---|
7498 | *
|
---|
7499 | * @return VBox status code.
|
---|
7500 | * @param pDisk Name of the cache file backend to use (case insensitive).
|
---|
7501 | * @param pszFilename Name of the differencing cache file to create.
|
---|
7502 | * @param cbSize Maximum size of the cache.
|
---|
7503 | * @param uImageFlags Flags specifying special cache features.
|
---|
7504 | * @param pszComment Pointer to image comment. NULL is ok.
|
---|
7505 | * @param pUuid New UUID of the image. If NULL, a new UUID is created.
|
---|
7506 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
7507 | * @param pVDIfsCache Pointer to the per-cache VD interface list.
|
---|
7508 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
7509 | */
|
---|
7510 | VBOXDDU_DECL(int) VDCreateCache(PVBOXHDD pDisk, const char *pszBackend,
|
---|
7511 | const char *pszFilename, uint64_t cbSize,
|
---|
7512 | unsigned uImageFlags, const char *pszComment,
|
---|
7513 | PCRTUUID pUuid, unsigned uOpenFlags,
|
---|
7514 | PVDINTERFACE pVDIfsCache, PVDINTERFACE pVDIfsOperation)
|
---|
7515 | {
|
---|
7516 | int rc = VINF_SUCCESS;
|
---|
7517 | int rc2;
|
---|
7518 | bool fLockWrite = false, fLockRead = false;
|
---|
7519 | PVDCACHE pCache = NULL;
|
---|
7520 | RTUUID uuid;
|
---|
7521 |
|
---|
7522 | LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" Uuid=%RTuuid uOpenFlags=%#x pVDIfsImage=%#p pVDIfsOperation=%#p\n",
|
---|
7523 | pDisk, pszBackend, pszFilename, cbSize, uImageFlags, pszComment, pUuid, uOpenFlags, pVDIfsCache, pVDIfsOperation));
|
---|
7524 |
|
---|
7525 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
7526 |
|
---|
7527 | do
|
---|
7528 | {
|
---|
7529 | /* sanity check */
|
---|
7530 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7531 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7532 |
|
---|
7533 | /* Check arguments. */
|
---|
7534 | AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
|
---|
7535 | ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
|
---|
7536 | rc = VERR_INVALID_PARAMETER);
|
---|
7537 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
7538 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
7539 | rc = VERR_INVALID_PARAMETER);
|
---|
7540 | AssertMsgBreakStmt(cbSize,
|
---|
7541 | ("cbSize=%llu\n", cbSize),
|
---|
7542 | rc = VERR_INVALID_PARAMETER);
|
---|
7543 | AssertMsgBreakStmt((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0,
|
---|
7544 | ("uImageFlags=%#x\n", uImageFlags),
|
---|
7545 | rc = VERR_INVALID_PARAMETER);
|
---|
7546 | /* The UUID may be NULL. */
|
---|
7547 | AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
|
---|
7548 | ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
|
---|
7549 | rc = VERR_INVALID_PARAMETER);
|
---|
7550 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
7551 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
7552 | rc = VERR_INVALID_PARAMETER);
|
---|
7553 |
|
---|
7554 | /* Check state. Needs a temporary read lock. Holding the write lock
|
---|
7555 | * all the time would be blocking other activities for too long. */
|
---|
7556 | rc2 = vdThreadStartRead(pDisk);
|
---|
7557 | AssertRC(rc2);
|
---|
7558 | fLockRead = true;
|
---|
7559 | AssertMsgBreakStmt(!pDisk->pCache,
|
---|
7560 | ("Create cache image cannot be done with a cache already attached\n"),
|
---|
7561 | rc = VERR_VD_CACHE_ALREADY_EXISTS);
|
---|
7562 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7563 | AssertRC(rc2);
|
---|
7564 | fLockRead = false;
|
---|
7565 |
|
---|
7566 | /* Set up image descriptor. */
|
---|
7567 | pCache = (PVDCACHE)RTMemAllocZ(sizeof(VDCACHE));
|
---|
7568 | if (!pCache)
|
---|
7569 | {
|
---|
7570 | rc = VERR_NO_MEMORY;
|
---|
7571 | break;
|
---|
7572 | }
|
---|
7573 | pCache->pszFilename = RTStrDup(pszFilename);
|
---|
7574 | if (!pCache->pszFilename)
|
---|
7575 | {
|
---|
7576 | rc = VERR_NO_MEMORY;
|
---|
7577 | break;
|
---|
7578 | }
|
---|
7579 |
|
---|
7580 | rc = vdFindCacheBackend(pszBackend, &pCache->Backend);
|
---|
7581 | if (RT_FAILURE(rc))
|
---|
7582 | break;
|
---|
7583 | if (!pCache->Backend)
|
---|
7584 | {
|
---|
7585 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
7586 | N_("VD: unknown backend name '%s'"), pszBackend);
|
---|
7587 | break;
|
---|
7588 | }
|
---|
7589 |
|
---|
7590 | pCache->VDIo.pDisk = pDisk;
|
---|
7591 | pCache->pVDIfsCache = pVDIfsCache;
|
---|
7592 |
|
---|
7593 | /* Set up the I/O interface. */
|
---|
7594 | pCache->VDIo.pInterfaceIo = VDIfIoGet(pVDIfsCache);
|
---|
7595 | if (!pCache->VDIo.pInterfaceIo)
|
---|
7596 | {
|
---|
7597 | vdIfIoFallbackCallbacksSetup(&pCache->VDIo.VDIfIo);
|
---|
7598 | rc = VDInterfaceAdd(&pCache->VDIo.VDIfIo.Core, "VD_IO", VDINTERFACETYPE_IO,
|
---|
7599 | pDisk, sizeof(VDINTERFACEIO), &pVDIfsCache);
|
---|
7600 | pCache->VDIo.pInterfaceIo = &pCache->VDIo.VDIfIo;
|
---|
7601 | }
|
---|
7602 |
|
---|
7603 | /* Set up the internal I/O interface. */
|
---|
7604 | AssertBreakStmt(!VDIfIoIntGet(pVDIfsCache), rc = VERR_INVALID_PARAMETER);
|
---|
7605 | vdIfIoIntCallbacksSetup(&pCache->VDIo.VDIfIoInt);
|
---|
7606 | rc = VDInterfaceAdd(&pCache->VDIo.VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
7607 | &pCache->VDIo, sizeof(VDINTERFACEIOINT), &pCache->pVDIfsCache);
|
---|
7608 | AssertRC(rc);
|
---|
7609 |
|
---|
7610 | /* Create UUID if the caller didn't specify one. */
|
---|
7611 | if (!pUuid)
|
---|
7612 | {
|
---|
7613 | rc = RTUuidCreate(&uuid);
|
---|
7614 | if (RT_FAILURE(rc))
|
---|
7615 | {
|
---|
7616 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
7617 | N_("VD: cannot generate UUID for image '%s'"),
|
---|
7618 | pszFilename);
|
---|
7619 | break;
|
---|
7620 | }
|
---|
7621 | pUuid = &uuid;
|
---|
7622 | }
|
---|
7623 |
|
---|
7624 | pCache->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
|
---|
7625 | pCache->VDIo.fIgnoreFlush = (uOpenFlags & VD_OPEN_FLAGS_IGNORE_FLUSH) != 0;
|
---|
7626 | rc = pCache->Backend->pfnCreate(pCache->pszFilename, cbSize,
|
---|
7627 | uImageFlags,
|
---|
7628 | pszComment, pUuid,
|
---|
7629 | uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
|
---|
7630 | 0, 99,
|
---|
7631 | pDisk->pVDIfsDisk,
|
---|
7632 | pCache->pVDIfsCache,
|
---|
7633 | pVDIfsOperation,
|
---|
7634 | &pCache->pBackendData);
|
---|
7635 |
|
---|
7636 | if (RT_SUCCESS(rc))
|
---|
7637 | {
|
---|
7638 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
7639 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7640 | AssertRC(rc2);
|
---|
7641 | fLockWrite = true;
|
---|
7642 |
|
---|
7643 | pCache->VDIo.pBackendData = pCache->pBackendData;
|
---|
7644 |
|
---|
7645 | /* Re-check state, as the lock wasn't held and another image
|
---|
7646 | * creation call could have been done by another thread. */
|
---|
7647 | AssertMsgStmt(!pDisk->pCache,
|
---|
7648 | ("Create cache image cannot be done with another cache open\n"),
|
---|
7649 | rc = VERR_VD_CACHE_ALREADY_EXISTS);
|
---|
7650 | }
|
---|
7651 |
|
---|
7652 | if ( RT_SUCCESS(rc)
|
---|
7653 | && pDisk->pLast)
|
---|
7654 | {
|
---|
7655 | RTUUID UuidModification;
|
---|
7656 |
|
---|
7657 | /* Set same modification Uuid as the last image. */
|
---|
7658 | rc = pDisk->pLast->Backend->pfnGetModificationUuid(pDisk->pLast->pBackendData,
|
---|
7659 | &UuidModification);
|
---|
7660 | if (RT_SUCCESS(rc))
|
---|
7661 | {
|
---|
7662 | rc = pCache->Backend->pfnSetModificationUuid(pCache->pBackendData,
|
---|
7663 | &UuidModification);
|
---|
7664 | }
|
---|
7665 |
|
---|
7666 | if (rc == VERR_NOT_SUPPORTED)
|
---|
7667 | rc = VINF_SUCCESS;
|
---|
7668 | }
|
---|
7669 |
|
---|
7670 | if (RT_SUCCESS(rc))
|
---|
7671 | {
|
---|
7672 | /* Cache successfully created. */
|
---|
7673 | pDisk->pCache = pCache;
|
---|
7674 | }
|
---|
7675 | else
|
---|
7676 | {
|
---|
7677 | /* Error detected, but image opened. Close and delete image. */
|
---|
7678 | rc2 = pCache->Backend->pfnClose(pCache->pBackendData, true);
|
---|
7679 | AssertRC(rc2);
|
---|
7680 | pCache->pBackendData = NULL;
|
---|
7681 | }
|
---|
7682 | } while (0);
|
---|
7683 |
|
---|
7684 | if (RT_UNLIKELY(fLockWrite))
|
---|
7685 | {
|
---|
7686 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7687 | AssertRC(rc2);
|
---|
7688 | }
|
---|
7689 | else if (RT_UNLIKELY(fLockRead))
|
---|
7690 | {
|
---|
7691 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7692 | AssertRC(rc2);
|
---|
7693 | }
|
---|
7694 |
|
---|
7695 | if (RT_FAILURE(rc))
|
---|
7696 | {
|
---|
7697 | if (pCache)
|
---|
7698 | {
|
---|
7699 | if (pCache->pszFilename)
|
---|
7700 | RTStrFree(pCache->pszFilename);
|
---|
7701 | RTMemFree(pCache);
|
---|
7702 | }
|
---|
7703 | }
|
---|
7704 |
|
---|
7705 | if (RT_SUCCESS(rc) && pIfProgress && pIfProgress->pfnProgress)
|
---|
7706 | pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
|
---|
7707 |
|
---|
7708 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
7709 | return rc;
|
---|
7710 | }
|
---|
7711 |
|
---|
7712 | /**
|
---|
7713 | * Merges two images (not necessarily with direct parent/child relationship).
|
---|
7714 | * As a side effect the source image and potentially the other images which
|
---|
7715 | * are also merged to the destination are deleted from both the disk and the
|
---|
7716 | * images in the HDD container.
|
---|
7717 | *
|
---|
7718 | * @returns VBox status code.
|
---|
7719 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
7720 | * @param pDisk Pointer to HDD container.
|
---|
7721 | * @param nImageFrom Name of the image file to merge from.
|
---|
7722 | * @param nImageTo Name of the image file to merge to.
|
---|
7723 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
7724 | */
|
---|
7725 | VBOXDDU_DECL(int) VDMerge(PVBOXHDD pDisk, unsigned nImageFrom,
|
---|
7726 | unsigned nImageTo, PVDINTERFACE pVDIfsOperation)
|
---|
7727 | {
|
---|
7728 | int rc = VINF_SUCCESS;
|
---|
7729 | int rc2;
|
---|
7730 | bool fLockWrite = false, fLockRead = false;
|
---|
7731 | void *pvBuf = NULL;
|
---|
7732 |
|
---|
7733 | LogFlowFunc(("pDisk=%#p nImageFrom=%u nImageTo=%u pVDIfsOperation=%#p\n",
|
---|
7734 | pDisk, nImageFrom, nImageTo, pVDIfsOperation));
|
---|
7735 |
|
---|
7736 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
7737 |
|
---|
7738 | do
|
---|
7739 | {
|
---|
7740 | /* sanity check */
|
---|
7741 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7742 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7743 |
|
---|
7744 | /* For simplicity reasons lock for writing as the image reopen below
|
---|
7745 | * might need it. After all the reopen is usually needed. */
|
---|
7746 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7747 | AssertRC(rc2);
|
---|
7748 | fLockWrite = true;
|
---|
7749 | PVDIMAGE pImageFrom = vdGetImageByNumber(pDisk, nImageFrom);
|
---|
7750 | PVDIMAGE pImageTo = vdGetImageByNumber(pDisk, nImageTo);
|
---|
7751 | if (!pImageFrom || !pImageTo)
|
---|
7752 | {
|
---|
7753 | rc = VERR_VD_IMAGE_NOT_FOUND;
|
---|
7754 | break;
|
---|
7755 | }
|
---|
7756 | AssertBreakStmt(pImageFrom != pImageTo, rc = VERR_INVALID_PARAMETER);
|
---|
7757 |
|
---|
7758 | /* Make sure destination image is writable. */
|
---|
7759 | unsigned uOpenFlags = pImageTo->Backend->pfnGetOpenFlags(pImageTo->pBackendData);
|
---|
7760 | if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
7761 | {
|
---|
7762 | /*
|
---|
7763 | * Clear skip consistency checks because the image is made writable now and
|
---|
7764 | * skipping consistency checks is only possible for readonly images.
|
---|
7765 | */
|
---|
7766 | uOpenFlags &= ~(VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS);
|
---|
7767 | rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pBackendData,
|
---|
7768 | uOpenFlags);
|
---|
7769 | if (RT_FAILURE(rc))
|
---|
7770 | break;
|
---|
7771 | }
|
---|
7772 |
|
---|
7773 | /* Get size of destination image. */
|
---|
7774 | uint64_t cbSize = pImageTo->Backend->pfnGetSize(pImageTo->pBackendData);
|
---|
7775 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7776 | AssertRC(rc2);
|
---|
7777 | fLockWrite = false;
|
---|
7778 |
|
---|
7779 | /* Allocate tmp buffer. */
|
---|
7780 | pvBuf = RTMemTmpAlloc(VD_MERGE_BUFFER_SIZE);
|
---|
7781 | if (!pvBuf)
|
---|
7782 | {
|
---|
7783 | rc = VERR_NO_MEMORY;
|
---|
7784 | break;
|
---|
7785 | }
|
---|
7786 |
|
---|
7787 | /* Merging is done directly on the images itself. This potentially
|
---|
7788 | * causes trouble if the disk is full in the middle of operation. */
|
---|
7789 | if (nImageFrom < nImageTo)
|
---|
7790 | {
|
---|
7791 | /* Merge parent state into child. This means writing all not
|
---|
7792 | * allocated blocks in the destination image which are allocated in
|
---|
7793 | * the images to be merged. */
|
---|
7794 | uint64_t uOffset = 0;
|
---|
7795 | uint64_t cbRemaining = cbSize;
|
---|
7796 | do
|
---|
7797 | {
|
---|
7798 | size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
|
---|
7799 | RTSGSEG SegmentBuf;
|
---|
7800 | RTSGBUF SgBuf;
|
---|
7801 | VDIOCTX IoCtx;
|
---|
7802 |
|
---|
7803 | SegmentBuf.pvSeg = pvBuf;
|
---|
7804 | SegmentBuf.cbSeg = VD_MERGE_BUFFER_SIZE;
|
---|
7805 | RTSgBufInit(&SgBuf, &SegmentBuf, 1);
|
---|
7806 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_READ, 0, 0, NULL,
|
---|
7807 | &SgBuf, NULL, NULL, VDIOCTX_FLAGS_SYNC);
|
---|
7808 |
|
---|
7809 | /* Need to hold the write lock during a read-write operation. */
|
---|
7810 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7811 | AssertRC(rc2);
|
---|
7812 | fLockWrite = true;
|
---|
7813 |
|
---|
7814 | rc = pImageTo->Backend->pfnRead(pImageTo->pBackendData,
|
---|
7815 | uOffset, cbThisRead,
|
---|
7816 | &IoCtx, &cbThisRead);
|
---|
7817 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
7818 | {
|
---|
7819 | /* Search for image with allocated block. Do not attempt to
|
---|
7820 | * read more than the previous reads marked as valid.
|
---|
7821 | * Otherwise this would return stale data when different
|
---|
7822 | * block sizes are used for the images. */
|
---|
7823 | for (PVDIMAGE pCurrImage = pImageTo->pPrev;
|
---|
7824 | pCurrImage != NULL && pCurrImage != pImageFrom->pPrev && rc == VERR_VD_BLOCK_FREE;
|
---|
7825 | pCurrImage = pCurrImage->pPrev)
|
---|
7826 | {
|
---|
7827 | rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
|
---|
7828 | uOffset, cbThisRead,
|
---|
7829 | &IoCtx, &cbThisRead);
|
---|
7830 | }
|
---|
7831 |
|
---|
7832 | if (rc != VERR_VD_BLOCK_FREE)
|
---|
7833 | {
|
---|
7834 | if (RT_FAILURE(rc))
|
---|
7835 | break;
|
---|
7836 | /* Updating the cache is required because this might be a live merge. */
|
---|
7837 | rc = vdWriteHelperEx(pDisk, pImageTo, pImageFrom->pPrev,
|
---|
7838 | uOffset, pvBuf, cbThisRead,
|
---|
7839 | VDIOCTX_FLAGS_READ_UPDATE_CACHE, 0);
|
---|
7840 | if (RT_FAILURE(rc))
|
---|
7841 | break;
|
---|
7842 | }
|
---|
7843 | else
|
---|
7844 | rc = VINF_SUCCESS;
|
---|
7845 | }
|
---|
7846 | else if (RT_FAILURE(rc))
|
---|
7847 | break;
|
---|
7848 |
|
---|
7849 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7850 | AssertRC(rc2);
|
---|
7851 | fLockWrite = false;
|
---|
7852 |
|
---|
7853 | uOffset += cbThisRead;
|
---|
7854 | cbRemaining -= cbThisRead;
|
---|
7855 |
|
---|
7856 | if (pIfProgress && pIfProgress->pfnProgress)
|
---|
7857 | {
|
---|
7858 | /** @todo r=klaus: this can update the progress to the same
|
---|
7859 | * percentage over and over again if the image format makes
|
---|
7860 | * relatively small increments. */
|
---|
7861 | rc = pIfProgress->pfnProgress(pIfProgress->Core.pvUser,
|
---|
7862 | uOffset * 99 / cbSize);
|
---|
7863 | if (RT_FAILURE(rc))
|
---|
7864 | break;
|
---|
7865 | }
|
---|
7866 | } while (uOffset < cbSize);
|
---|
7867 | }
|
---|
7868 | else
|
---|
7869 | {
|
---|
7870 | /*
|
---|
7871 | * We may need to update the parent uuid of the child coming after
|
---|
7872 | * the last image to be merged. We have to reopen it read/write.
|
---|
7873 | *
|
---|
7874 | * This is done before we do the actual merge to prevent an
|
---|
7875 | * inconsistent chain if the mode change fails for some reason.
|
---|
7876 | */
|
---|
7877 | if (pImageFrom->pNext)
|
---|
7878 | {
|
---|
7879 | PVDIMAGE pImageChild = pImageFrom->pNext;
|
---|
7880 |
|
---|
7881 | /* Take the write lock. */
|
---|
7882 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7883 | AssertRC(rc2);
|
---|
7884 | fLockWrite = true;
|
---|
7885 |
|
---|
7886 | /* We need to open the image in read/write mode. */
|
---|
7887 | uOpenFlags = pImageChild->Backend->pfnGetOpenFlags(pImageChild->pBackendData);
|
---|
7888 |
|
---|
7889 | if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
7890 | {
|
---|
7891 | uOpenFlags &= ~VD_OPEN_FLAGS_READONLY;
|
---|
7892 | rc = pImageChild->Backend->pfnSetOpenFlags(pImageChild->pBackendData,
|
---|
7893 | uOpenFlags);
|
---|
7894 | if (RT_FAILURE(rc))
|
---|
7895 | break;
|
---|
7896 | }
|
---|
7897 |
|
---|
7898 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7899 | AssertRC(rc2);
|
---|
7900 | fLockWrite = false;
|
---|
7901 | }
|
---|
7902 |
|
---|
7903 | /* If the merge is from the last image we have to relay all writes
|
---|
7904 | * to the merge destination as well, so that concurrent writes
|
---|
7905 | * (in case of a live merge) are handled correctly. */
|
---|
7906 | if (!pImageFrom->pNext)
|
---|
7907 | {
|
---|
7908 | /* Take the write lock. */
|
---|
7909 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7910 | AssertRC(rc2);
|
---|
7911 | fLockWrite = true;
|
---|
7912 |
|
---|
7913 | pDisk->pImageRelay = pImageTo;
|
---|
7914 |
|
---|
7915 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7916 | AssertRC(rc2);
|
---|
7917 | fLockWrite = false;
|
---|
7918 | }
|
---|
7919 |
|
---|
7920 | /* Merge child state into parent. This means writing all blocks
|
---|
7921 | * which are allocated in the image up to the source image to the
|
---|
7922 | * destination image. */
|
---|
7923 | uint64_t uOffset = 0;
|
---|
7924 | uint64_t cbRemaining = cbSize;
|
---|
7925 | do
|
---|
7926 | {
|
---|
7927 | size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
|
---|
7928 | RTSGSEG SegmentBuf;
|
---|
7929 | RTSGBUF SgBuf;
|
---|
7930 | VDIOCTX IoCtx;
|
---|
7931 |
|
---|
7932 | rc = VERR_VD_BLOCK_FREE;
|
---|
7933 |
|
---|
7934 | SegmentBuf.pvSeg = pvBuf;
|
---|
7935 | SegmentBuf.cbSeg = VD_MERGE_BUFFER_SIZE;
|
---|
7936 | RTSgBufInit(&SgBuf, &SegmentBuf, 1);
|
---|
7937 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_READ, 0, 0, NULL,
|
---|
7938 | &SgBuf, NULL, NULL, VDIOCTX_FLAGS_SYNC);
|
---|
7939 |
|
---|
7940 | /* Need to hold the write lock during a read-write operation. */
|
---|
7941 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7942 | AssertRC(rc2);
|
---|
7943 | fLockWrite = true;
|
---|
7944 |
|
---|
7945 | /* Search for image with allocated block. Do not attempt to
|
---|
7946 | * read more than the previous reads marked as valid. Otherwise
|
---|
7947 | * this would return stale data when different block sizes are
|
---|
7948 | * used for the images. */
|
---|
7949 | for (PVDIMAGE pCurrImage = pImageFrom;
|
---|
7950 | pCurrImage != NULL && pCurrImage != pImageTo && rc == VERR_VD_BLOCK_FREE;
|
---|
7951 | pCurrImage = pCurrImage->pPrev)
|
---|
7952 | {
|
---|
7953 | rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
|
---|
7954 | uOffset, cbThisRead,
|
---|
7955 | &IoCtx, &cbThisRead);
|
---|
7956 | }
|
---|
7957 |
|
---|
7958 | if (rc != VERR_VD_BLOCK_FREE)
|
---|
7959 | {
|
---|
7960 | if (RT_FAILURE(rc))
|
---|
7961 | break;
|
---|
7962 | rc = vdWriteHelper(pDisk, pImageTo, uOffset, pvBuf,
|
---|
7963 | cbThisRead, VDIOCTX_FLAGS_READ_UPDATE_CACHE);
|
---|
7964 | if (RT_FAILURE(rc))
|
---|
7965 | break;
|
---|
7966 | }
|
---|
7967 | else
|
---|
7968 | rc = VINF_SUCCESS;
|
---|
7969 |
|
---|
7970 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7971 | AssertRC(rc2);
|
---|
7972 | fLockWrite = false;
|
---|
7973 |
|
---|
7974 | uOffset += cbThisRead;
|
---|
7975 | cbRemaining -= cbThisRead;
|
---|
7976 |
|
---|
7977 | if (pIfProgress && pIfProgress->pfnProgress)
|
---|
7978 | {
|
---|
7979 | /** @todo r=klaus: this can update the progress to the same
|
---|
7980 | * percentage over and over again if the image format makes
|
---|
7981 | * relatively small increments. */
|
---|
7982 | rc = pIfProgress->pfnProgress(pIfProgress->Core.pvUser,
|
---|
7983 | uOffset * 99 / cbSize);
|
---|
7984 | if (RT_FAILURE(rc))
|
---|
7985 | break;
|
---|
7986 | }
|
---|
7987 | } while (uOffset < cbSize);
|
---|
7988 |
|
---|
7989 | /* In case we set up a "write proxy" image above we must clear
|
---|
7990 | * this again now to prevent stray writes. Failure or not. */
|
---|
7991 | if (!pImageFrom->pNext)
|
---|
7992 | {
|
---|
7993 | /* Take the write lock. */
|
---|
7994 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7995 | AssertRC(rc2);
|
---|
7996 | fLockWrite = true;
|
---|
7997 |
|
---|
7998 | pDisk->pImageRelay = NULL;
|
---|
7999 |
|
---|
8000 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
8001 | AssertRC(rc2);
|
---|
8002 | fLockWrite = false;
|
---|
8003 | }
|
---|
8004 | }
|
---|
8005 |
|
---|
8006 | /*
|
---|
8007 | * Leave in case of an error to avoid corrupted data in the image chain
|
---|
8008 | * (includes cancelling the operation by the user).
|
---|
8009 | */
|
---|
8010 | if (RT_FAILURE(rc))
|
---|
8011 | break;
|
---|
8012 |
|
---|
8013 | /* Need to hold the write lock while finishing the merge. */
|
---|
8014 | rc2 = vdThreadStartWrite(pDisk);
|
---|
8015 | AssertRC(rc2);
|
---|
8016 | fLockWrite = true;
|
---|
8017 |
|
---|
8018 | /* Update parent UUID so that image chain is consistent.
|
---|
8019 | * The two attempts work around the problem that some backends
|
---|
8020 | * (e.g. iSCSI) do not support UUIDs, so we exploit the fact that
|
---|
8021 | * so far there can only be one such image in the chain. */
|
---|
8022 | /** @todo needs a better long-term solution, passing the UUID
|
---|
8023 | * knowledge from the caller or some such */
|
---|
8024 | RTUUID Uuid;
|
---|
8025 | PVDIMAGE pImageChild = NULL;
|
---|
8026 | if (nImageFrom < nImageTo)
|
---|
8027 | {
|
---|
8028 | if (pImageFrom->pPrev)
|
---|
8029 | {
|
---|
8030 | /* plan A: ask the parent itself for its UUID */
|
---|
8031 | rc = pImageFrom->pPrev->Backend->pfnGetUuid(pImageFrom->pPrev->pBackendData,
|
---|
8032 | &Uuid);
|
---|
8033 | if (RT_FAILURE(rc))
|
---|
8034 | {
|
---|
8035 | /* plan B: ask the child of the parent for parent UUID */
|
---|
8036 | rc = pImageFrom->Backend->pfnGetParentUuid(pImageFrom->pBackendData,
|
---|
8037 | &Uuid);
|
---|
8038 | }
|
---|
8039 | AssertRC(rc);
|
---|
8040 | }
|
---|
8041 | else
|
---|
8042 | RTUuidClear(&Uuid);
|
---|
8043 | rc = pImageTo->Backend->pfnSetParentUuid(pImageTo->pBackendData,
|
---|
8044 | &Uuid);
|
---|
8045 | AssertRC(rc);
|
---|
8046 | }
|
---|
8047 | else
|
---|
8048 | {
|
---|
8049 | /* Update the parent uuid of the child of the last merged image. */
|
---|
8050 | if (pImageFrom->pNext)
|
---|
8051 | {
|
---|
8052 | /* plan A: ask the parent itself for its UUID */
|
---|
8053 | rc = pImageTo->Backend->pfnGetUuid(pImageTo->pBackendData,
|
---|
8054 | &Uuid);
|
---|
8055 | if (RT_FAILURE(rc))
|
---|
8056 | {
|
---|
8057 | /* plan B: ask the child of the parent for parent UUID */
|
---|
8058 | rc = pImageTo->pNext->Backend->pfnGetParentUuid(pImageTo->pNext->pBackendData,
|
---|
8059 | &Uuid);
|
---|
8060 | }
|
---|
8061 | AssertRC(rc);
|
---|
8062 |
|
---|
8063 | rc = pImageFrom->Backend->pfnSetParentUuid(pImageFrom->pNext->pBackendData,
|
---|
8064 | &Uuid);
|
---|
8065 | AssertRC(rc);
|
---|
8066 |
|
---|
8067 | pImageChild = pImageFrom->pNext;
|
---|
8068 | }
|
---|
8069 | }
|
---|
8070 |
|
---|
8071 | /* Delete the no longer needed images. */
|
---|
8072 | PVDIMAGE pImg = pImageFrom, pTmp;
|
---|
8073 | while (pImg != pImageTo)
|
---|
8074 | {
|
---|
8075 | if (nImageFrom < nImageTo)
|
---|
8076 | pTmp = pImg->pNext;
|
---|
8077 | else
|
---|
8078 | pTmp = pImg->pPrev;
|
---|
8079 | vdRemoveImageFromList(pDisk, pImg);
|
---|
8080 | pImg->Backend->pfnClose(pImg->pBackendData, true);
|
---|
8081 | RTMemFree(pImg->pszFilename);
|
---|
8082 | RTMemFree(pImg);
|
---|
8083 | pImg = pTmp;
|
---|
8084 | }
|
---|
8085 |
|
---|
8086 | /* Make sure destination image is back to read only if necessary. */
|
---|
8087 | if (pImageTo != pDisk->pLast)
|
---|
8088 | {
|
---|
8089 | uOpenFlags = pImageTo->Backend->pfnGetOpenFlags(pImageTo->pBackendData);
|
---|
8090 | uOpenFlags |= VD_OPEN_FLAGS_READONLY;
|
---|
8091 | rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pBackendData,
|
---|
8092 | uOpenFlags);
|
---|
8093 | if (RT_FAILURE(rc))
|
---|
8094 | break;
|
---|
8095 | }
|
---|
8096 |
|
---|
8097 | /*
|
---|
8098 | * Make sure the child is readonly
|
---|
8099 | * for the child -> parent merge direction
|
---|
8100 | * if necessary.
|
---|
8101 | */
|
---|
8102 | if ( nImageFrom > nImageTo
|
---|
8103 | && pImageChild
|
---|
8104 | && pImageChild != pDisk->pLast)
|
---|
8105 | {
|
---|
8106 | uOpenFlags = pImageChild->Backend->pfnGetOpenFlags(pImageChild->pBackendData);
|
---|
8107 | uOpenFlags |= VD_OPEN_FLAGS_READONLY;
|
---|
8108 | rc = pImageChild->Backend->pfnSetOpenFlags(pImageChild->pBackendData,
|
---|
8109 | uOpenFlags);
|
---|
8110 | if (RT_FAILURE(rc))
|
---|
8111 | break;
|
---|
8112 | }
|
---|
8113 | } while (0);
|
---|
8114 |
|
---|
8115 | if (RT_UNLIKELY(fLockWrite))
|
---|
8116 | {
|
---|
8117 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
8118 | AssertRC(rc2);
|
---|
8119 | }
|
---|
8120 | else if (RT_UNLIKELY(fLockRead))
|
---|
8121 | {
|
---|
8122 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8123 | AssertRC(rc2);
|
---|
8124 | }
|
---|
8125 |
|
---|
8126 | if (pvBuf)
|
---|
8127 | RTMemTmpFree(pvBuf);
|
---|
8128 |
|
---|
8129 | if (RT_SUCCESS(rc) && pIfProgress && pIfProgress->pfnProgress)
|
---|
8130 | pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
|
---|
8131 |
|
---|
8132 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8133 | return rc;
|
---|
8134 | }
|
---|
8135 |
|
---|
8136 | /**
|
---|
8137 | * Copies an image from one HDD container to another - extended version.
|
---|
8138 | * The copy is opened in the target HDD container.
|
---|
8139 | * It is possible to convert between different image formats, because the
|
---|
8140 | * backend for the destination may be different from the source.
|
---|
8141 | * If both the source and destination reference the same HDD container,
|
---|
8142 | * then the image is moved (by copying/deleting or renaming) to the new location.
|
---|
8143 | * The source container is unchanged if the move operation fails, otherwise
|
---|
8144 | * the image at the new location is opened in the same way as the old one was.
|
---|
8145 | *
|
---|
8146 | * @note The read/write accesses across disks are not synchronized, just the
|
---|
8147 | * accesses to each disk. Once there is a use case which requires a defined
|
---|
8148 | * read/write behavior in this situation this needs to be extended.
|
---|
8149 | *
|
---|
8150 | * @return VBox status code.
|
---|
8151 | * @return VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
8152 | * @param pDiskFrom Pointer to source HDD container.
|
---|
8153 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
8154 | * @param pDiskTo Pointer to destination HDD container.
|
---|
8155 | * @param pszBackend Name of the image file backend to use (may be NULL to use the same as the source, case insensitive).
|
---|
8156 | * @param pszFilename New name of the image (may be NULL to specify that the
|
---|
8157 | * copy destination is the destination container, or
|
---|
8158 | * if pDiskFrom == pDiskTo, i.e. when moving).
|
---|
8159 | * @param fMoveByRename If true, attempt to perform a move by renaming (if successful the new size is ignored).
|
---|
8160 | * @param cbSize New image size (0 means leave unchanged).
|
---|
8161 | * @param nImageSameFrom todo
|
---|
8162 | * @param nImageSameTo todo
|
---|
8163 | * @param uImageFlags Flags specifying special destination image features.
|
---|
8164 | * @param pDstUuid New UUID of the destination image. If NULL, a new UUID is created.
|
---|
8165 | * This parameter is used if and only if a true copy is created.
|
---|
8166 | * In all rename/move cases or copy to existing image cases the modification UUIDs are copied over.
|
---|
8167 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
8168 | * Only used if the destination image is created.
|
---|
8169 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
8170 | * @param pDstVDIfsImage Pointer to the per-image VD interface list, for the
|
---|
8171 | * destination image.
|
---|
8172 | * @param pDstVDIfsOperation Pointer to the per-operation VD interface list,
|
---|
8173 | * for the destination operation.
|
---|
8174 | */
|
---|
8175 | VBOXDDU_DECL(int) VDCopyEx(PVBOXHDD pDiskFrom, unsigned nImage, PVBOXHDD pDiskTo,
|
---|
8176 | const char *pszBackend, const char *pszFilename,
|
---|
8177 | bool fMoveByRename, uint64_t cbSize,
|
---|
8178 | unsigned nImageFromSame, unsigned nImageToSame,
|
---|
8179 | unsigned uImageFlags, PCRTUUID pDstUuid,
|
---|
8180 | unsigned uOpenFlags, PVDINTERFACE pVDIfsOperation,
|
---|
8181 | PVDINTERFACE pDstVDIfsImage,
|
---|
8182 | PVDINTERFACE pDstVDIfsOperation)
|
---|
8183 | {
|
---|
8184 | int rc = VINF_SUCCESS;
|
---|
8185 | int rc2;
|
---|
8186 | bool fLockReadFrom = false, fLockWriteFrom = false, fLockWriteTo = false;
|
---|
8187 | PVDIMAGE pImageTo = NULL;
|
---|
8188 |
|
---|
8189 | LogFlowFunc(("pDiskFrom=%#p nImage=%u pDiskTo=%#p pszBackend=\"%s\" pszFilename=\"%s\" fMoveByRename=%d cbSize=%llu nImageFromSame=%u nImageToSame=%u uImageFlags=%#x pDstUuid=%#p uOpenFlags=%#x pVDIfsOperation=%#p pDstVDIfsImage=%#p pDstVDIfsOperation=%#p\n",
|
---|
8190 | pDiskFrom, nImage, pDiskTo, pszBackend, pszFilename, fMoveByRename, cbSize, nImageFromSame, nImageToSame, uImageFlags, pDstUuid, uOpenFlags, pVDIfsOperation, pDstVDIfsImage, pDstVDIfsOperation));
|
---|
8191 |
|
---|
8192 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
8193 | PVDINTERFACEPROGRESS pDstIfProgress = VDIfProgressGet(pDstVDIfsOperation);
|
---|
8194 |
|
---|
8195 | do {
|
---|
8196 | /* Check arguments. */
|
---|
8197 | AssertMsgBreakStmt(VALID_PTR(pDiskFrom), ("pDiskFrom=%#p\n", pDiskFrom),
|
---|
8198 | rc = VERR_INVALID_PARAMETER);
|
---|
8199 | AssertMsg(pDiskFrom->u32Signature == VBOXHDDDISK_SIGNATURE,
|
---|
8200 | ("u32Signature=%08x\n", pDiskFrom->u32Signature));
|
---|
8201 |
|
---|
8202 | rc2 = vdThreadStartRead(pDiskFrom);
|
---|
8203 | AssertRC(rc2);
|
---|
8204 | fLockReadFrom = true;
|
---|
8205 | PVDIMAGE pImageFrom = vdGetImageByNumber(pDiskFrom, nImage);
|
---|
8206 | AssertPtrBreakStmt(pImageFrom, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
8207 | AssertMsgBreakStmt(VALID_PTR(pDiskTo), ("pDiskTo=%#p\n", pDiskTo),
|
---|
8208 | rc = VERR_INVALID_PARAMETER);
|
---|
8209 | AssertMsg(pDiskTo->u32Signature == VBOXHDDDISK_SIGNATURE,
|
---|
8210 | ("u32Signature=%08x\n", pDiskTo->u32Signature));
|
---|
8211 | AssertMsgBreakStmt( (nImageFromSame < nImage || nImageFromSame == VD_IMAGE_CONTENT_UNKNOWN)
|
---|
8212 | && (nImageToSame < pDiskTo->cImages || nImageToSame == VD_IMAGE_CONTENT_UNKNOWN)
|
---|
8213 | && ( (nImageFromSame == VD_IMAGE_CONTENT_UNKNOWN && nImageToSame == VD_IMAGE_CONTENT_UNKNOWN)
|
---|
8214 | || (nImageFromSame != VD_IMAGE_CONTENT_UNKNOWN && nImageToSame != VD_IMAGE_CONTENT_UNKNOWN)),
|
---|
8215 | ("nImageFromSame=%u nImageToSame=%u\n", nImageFromSame, nImageToSame),
|
---|
8216 | rc = VERR_INVALID_PARAMETER);
|
---|
8217 |
|
---|
8218 | /* Move the image. */
|
---|
8219 | if (pDiskFrom == pDiskTo)
|
---|
8220 | {
|
---|
8221 | /* Rename only works when backends are the same, are file based
|
---|
8222 | * and the rename method is implemented. */
|
---|
8223 | if ( fMoveByRename
|
---|
8224 | && !RTStrICmp(pszBackend, pImageFrom->Backend->pszBackendName)
|
---|
8225 | && pImageFrom->Backend->uBackendCaps & VD_CAP_FILE
|
---|
8226 | && pImageFrom->Backend->pfnRename)
|
---|
8227 | {
|
---|
8228 | rc2 = vdThreadFinishRead(pDiskFrom);
|
---|
8229 | AssertRC(rc2);
|
---|
8230 | fLockReadFrom = false;
|
---|
8231 |
|
---|
8232 | rc2 = vdThreadStartWrite(pDiskFrom);
|
---|
8233 | AssertRC(rc2);
|
---|
8234 | fLockWriteFrom = true;
|
---|
8235 | rc = pImageFrom->Backend->pfnRename(pImageFrom->pBackendData, pszFilename ? pszFilename : pImageFrom->pszFilename);
|
---|
8236 | break;
|
---|
8237 | }
|
---|
8238 |
|
---|
8239 | /** @todo Moving (including shrinking/growing) of the image is
|
---|
8240 | * requested, but the rename attempt failed or it wasn't possible.
|
---|
8241 | * Must now copy image to temp location. */
|
---|
8242 | AssertReleaseMsgFailed(("VDCopy: moving by copy/delete not implemented\n"));
|
---|
8243 | }
|
---|
8244 |
|
---|
8245 | /* pszFilename is allowed to be NULL, as this indicates copy to the existing image. */
|
---|
8246 | AssertMsgBreakStmt(pszFilename == NULL || (VALID_PTR(pszFilename) && *pszFilename),
|
---|
8247 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
8248 | rc = VERR_INVALID_PARAMETER);
|
---|
8249 |
|
---|
8250 | uint64_t cbSizeFrom;
|
---|
8251 | cbSizeFrom = pImageFrom->Backend->pfnGetSize(pImageFrom->pBackendData);
|
---|
8252 | if (cbSizeFrom == 0)
|
---|
8253 | {
|
---|
8254 | rc = VERR_VD_VALUE_NOT_FOUND;
|
---|
8255 | break;
|
---|
8256 | }
|
---|
8257 |
|
---|
8258 | VDGEOMETRY PCHSGeometryFrom = {0, 0, 0};
|
---|
8259 | VDGEOMETRY LCHSGeometryFrom = {0, 0, 0};
|
---|
8260 | pImageFrom->Backend->pfnGetPCHSGeometry(pImageFrom->pBackendData, &PCHSGeometryFrom);
|
---|
8261 | pImageFrom->Backend->pfnGetLCHSGeometry(pImageFrom->pBackendData, &LCHSGeometryFrom);
|
---|
8262 |
|
---|
8263 | RTUUID ImageUuid, ImageModificationUuid;
|
---|
8264 | if (pDiskFrom != pDiskTo)
|
---|
8265 | {
|
---|
8266 | if (pDstUuid)
|
---|
8267 | ImageUuid = *pDstUuid;
|
---|
8268 | else
|
---|
8269 | RTUuidCreate(&ImageUuid);
|
---|
8270 | }
|
---|
8271 | else
|
---|
8272 | {
|
---|
8273 | rc = pImageFrom->Backend->pfnGetUuid(pImageFrom->pBackendData, &ImageUuid);
|
---|
8274 | if (RT_FAILURE(rc))
|
---|
8275 | RTUuidCreate(&ImageUuid);
|
---|
8276 | }
|
---|
8277 | rc = pImageFrom->Backend->pfnGetModificationUuid(pImageFrom->pBackendData, &ImageModificationUuid);
|
---|
8278 | if (RT_FAILURE(rc))
|
---|
8279 | RTUuidClear(&ImageModificationUuid);
|
---|
8280 |
|
---|
8281 | char szComment[1024];
|
---|
8282 | rc = pImageFrom->Backend->pfnGetComment(pImageFrom->pBackendData, szComment, sizeof(szComment));
|
---|
8283 | if (RT_FAILURE(rc))
|
---|
8284 | szComment[0] = '\0';
|
---|
8285 | else
|
---|
8286 | szComment[sizeof(szComment) - 1] = '\0';
|
---|
8287 |
|
---|
8288 | rc2 = vdThreadFinishRead(pDiskFrom);
|
---|
8289 | AssertRC(rc2);
|
---|
8290 | fLockReadFrom = false;
|
---|
8291 |
|
---|
8292 | rc2 = vdThreadStartRead(pDiskTo);
|
---|
8293 | AssertRC(rc2);
|
---|
8294 | unsigned cImagesTo = pDiskTo->cImages;
|
---|
8295 | rc2 = vdThreadFinishRead(pDiskTo);
|
---|
8296 | AssertRC(rc2);
|
---|
8297 |
|
---|
8298 | if (pszFilename)
|
---|
8299 | {
|
---|
8300 | if (cbSize == 0)
|
---|
8301 | cbSize = cbSizeFrom;
|
---|
8302 |
|
---|
8303 | /* Create destination image with the properties of source image. */
|
---|
8304 | /** @todo replace the VDCreateDiff/VDCreateBase calls by direct
|
---|
8305 | * calls to the backend. Unifies the code and reduces the API
|
---|
8306 | * dependencies. Would also make the synchronization explicit. */
|
---|
8307 | if (cImagesTo > 0)
|
---|
8308 | {
|
---|
8309 | rc = VDCreateDiff(pDiskTo, pszBackend, pszFilename,
|
---|
8310 | uImageFlags, szComment, &ImageUuid,
|
---|
8311 | NULL /* pParentUuid */,
|
---|
8312 | uOpenFlags & ~VD_OPEN_FLAGS_READONLY,
|
---|
8313 | pDstVDIfsImage, NULL);
|
---|
8314 |
|
---|
8315 | rc2 = vdThreadStartWrite(pDiskTo);
|
---|
8316 | AssertRC(rc2);
|
---|
8317 | fLockWriteTo = true;
|
---|
8318 | } else {
|
---|
8319 | /** @todo hack to force creation of a fixed image for
|
---|
8320 | * the RAW backend, which can't handle anything else. */
|
---|
8321 | if (!RTStrICmp(pszBackend, "RAW"))
|
---|
8322 | uImageFlags |= VD_IMAGE_FLAGS_FIXED;
|
---|
8323 |
|
---|
8324 | vdFixupPCHSGeometry(&PCHSGeometryFrom, cbSize);
|
---|
8325 | vdFixupLCHSGeometry(&LCHSGeometryFrom, cbSize);
|
---|
8326 |
|
---|
8327 | rc = VDCreateBase(pDiskTo, pszBackend, pszFilename, cbSize,
|
---|
8328 | uImageFlags, szComment,
|
---|
8329 | &PCHSGeometryFrom, &LCHSGeometryFrom,
|
---|
8330 | NULL, uOpenFlags & ~VD_OPEN_FLAGS_READONLY,
|
---|
8331 | pDstVDIfsImage, NULL);
|
---|
8332 |
|
---|
8333 | rc2 = vdThreadStartWrite(pDiskTo);
|
---|
8334 | AssertRC(rc2);
|
---|
8335 | fLockWriteTo = true;
|
---|
8336 |
|
---|
8337 | if (RT_SUCCESS(rc) && !RTUuidIsNull(&ImageUuid))
|
---|
8338 | pDiskTo->pLast->Backend->pfnSetUuid(pDiskTo->pLast->pBackendData, &ImageUuid);
|
---|
8339 | }
|
---|
8340 | if (RT_FAILURE(rc))
|
---|
8341 | break;
|
---|
8342 |
|
---|
8343 | pImageTo = pDiskTo->pLast;
|
---|
8344 | AssertPtrBreakStmt(pImageTo, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
8345 |
|
---|
8346 | cbSize = RT_MIN(cbSize, cbSizeFrom);
|
---|
8347 | }
|
---|
8348 | else
|
---|
8349 | {
|
---|
8350 | pImageTo = pDiskTo->pLast;
|
---|
8351 | AssertPtrBreakStmt(pImageTo, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
8352 |
|
---|
8353 | uint64_t cbSizeTo;
|
---|
8354 | cbSizeTo = pImageTo->Backend->pfnGetSize(pImageTo->pBackendData);
|
---|
8355 | if (cbSizeTo == 0)
|
---|
8356 | {
|
---|
8357 | rc = VERR_VD_VALUE_NOT_FOUND;
|
---|
8358 | break;
|
---|
8359 | }
|
---|
8360 |
|
---|
8361 | if (cbSize == 0)
|
---|
8362 | cbSize = RT_MIN(cbSizeFrom, cbSizeTo);
|
---|
8363 |
|
---|
8364 | vdFixupPCHSGeometry(&PCHSGeometryFrom, cbSize);
|
---|
8365 | vdFixupLCHSGeometry(&LCHSGeometryFrom, cbSize);
|
---|
8366 |
|
---|
8367 | /* Update the geometry in the destination image. */
|
---|
8368 | pImageTo->Backend->pfnSetPCHSGeometry(pImageTo->pBackendData, &PCHSGeometryFrom);
|
---|
8369 | pImageTo->Backend->pfnSetLCHSGeometry(pImageTo->pBackendData, &LCHSGeometryFrom);
|
---|
8370 | }
|
---|
8371 |
|
---|
8372 | rc2 = vdThreadFinishWrite(pDiskTo);
|
---|
8373 | AssertRC(rc2);
|
---|
8374 | fLockWriteTo = false;
|
---|
8375 |
|
---|
8376 | /* Whether we can take the optimized copy path (false) or not.
|
---|
8377 | * Don't optimize if the image existed or if it is a child image. */
|
---|
8378 | bool fSuppressRedundantIo = ( !(pszFilename == NULL || cImagesTo > 0)
|
---|
8379 | || (nImageToSame != VD_IMAGE_CONTENT_UNKNOWN));
|
---|
8380 | unsigned cImagesFromReadBack, cImagesToReadBack;
|
---|
8381 |
|
---|
8382 | if (nImageFromSame == VD_IMAGE_CONTENT_UNKNOWN)
|
---|
8383 | cImagesFromReadBack = 0;
|
---|
8384 | else
|
---|
8385 | {
|
---|
8386 | if (nImage == VD_LAST_IMAGE)
|
---|
8387 | cImagesFromReadBack = pDiskFrom->cImages - nImageFromSame - 1;
|
---|
8388 | else
|
---|
8389 | cImagesFromReadBack = nImage - nImageFromSame;
|
---|
8390 | }
|
---|
8391 |
|
---|
8392 | if (nImageToSame == VD_IMAGE_CONTENT_UNKNOWN)
|
---|
8393 | cImagesToReadBack = 0;
|
---|
8394 | else
|
---|
8395 | cImagesToReadBack = pDiskTo->cImages - nImageToSame - 1;
|
---|
8396 |
|
---|
8397 | /* Copy the data. */
|
---|
8398 | rc = vdCopyHelper(pDiskFrom, pImageFrom, pDiskTo, cbSize,
|
---|
8399 | cImagesFromReadBack, cImagesToReadBack,
|
---|
8400 | fSuppressRedundantIo, pIfProgress, pDstIfProgress);
|
---|
8401 |
|
---|
8402 | if (RT_SUCCESS(rc))
|
---|
8403 | {
|
---|
8404 | rc2 = vdThreadStartWrite(pDiskTo);
|
---|
8405 | AssertRC(rc2);
|
---|
8406 | fLockWriteTo = true;
|
---|
8407 |
|
---|
8408 | /* Only set modification UUID if it is non-null, since the source
|
---|
8409 | * backend might not provide a valid modification UUID. */
|
---|
8410 | if (!RTUuidIsNull(&ImageModificationUuid))
|
---|
8411 | pImageTo->Backend->pfnSetModificationUuid(pImageTo->pBackendData, &ImageModificationUuid);
|
---|
8412 |
|
---|
8413 | /* Set the requested open flags if they differ from the value
|
---|
8414 | * required for creating the image and copying the contents. */
|
---|
8415 | if ( pImageTo && pszFilename
|
---|
8416 | && uOpenFlags != (uOpenFlags & ~VD_OPEN_FLAGS_READONLY))
|
---|
8417 | rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pBackendData,
|
---|
8418 | uOpenFlags);
|
---|
8419 | }
|
---|
8420 | } while (0);
|
---|
8421 |
|
---|
8422 | if (RT_FAILURE(rc) && pImageTo && pszFilename)
|
---|
8423 | {
|
---|
8424 | /* Take the write lock only if it is not taken. Not worth making the
|
---|
8425 | * above code even more complicated. */
|
---|
8426 | if (RT_UNLIKELY(!fLockWriteTo))
|
---|
8427 | {
|
---|
8428 | rc2 = vdThreadStartWrite(pDiskTo);
|
---|
8429 | AssertRC(rc2);
|
---|
8430 | fLockWriteTo = true;
|
---|
8431 | }
|
---|
8432 | /* Error detected, but new image created. Remove image from list. */
|
---|
8433 | vdRemoveImageFromList(pDiskTo, pImageTo);
|
---|
8434 |
|
---|
8435 | /* Close and delete image. */
|
---|
8436 | rc2 = pImageTo->Backend->pfnClose(pImageTo->pBackendData, true);
|
---|
8437 | AssertRC(rc2);
|
---|
8438 | pImageTo->pBackendData = NULL;
|
---|
8439 |
|
---|
8440 | /* Free remaining resources. */
|
---|
8441 | if (pImageTo->pszFilename)
|
---|
8442 | RTStrFree(pImageTo->pszFilename);
|
---|
8443 |
|
---|
8444 | RTMemFree(pImageTo);
|
---|
8445 | }
|
---|
8446 |
|
---|
8447 | if (RT_UNLIKELY(fLockWriteTo))
|
---|
8448 | {
|
---|
8449 | rc2 = vdThreadFinishWrite(pDiskTo);
|
---|
8450 | AssertRC(rc2);
|
---|
8451 | }
|
---|
8452 | if (RT_UNLIKELY(fLockWriteFrom))
|
---|
8453 | {
|
---|
8454 | rc2 = vdThreadFinishWrite(pDiskFrom);
|
---|
8455 | AssertRC(rc2);
|
---|
8456 | }
|
---|
8457 | else if (RT_UNLIKELY(fLockReadFrom))
|
---|
8458 | {
|
---|
8459 | rc2 = vdThreadFinishRead(pDiskFrom);
|
---|
8460 | AssertRC(rc2);
|
---|
8461 | }
|
---|
8462 |
|
---|
8463 | if (RT_SUCCESS(rc))
|
---|
8464 | {
|
---|
8465 | if (pIfProgress && pIfProgress->pfnProgress)
|
---|
8466 | pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
|
---|
8467 | if (pDstIfProgress && pDstIfProgress->pfnProgress)
|
---|
8468 | pDstIfProgress->pfnProgress(pDstIfProgress->Core.pvUser, 100);
|
---|
8469 | }
|
---|
8470 |
|
---|
8471 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8472 | return rc;
|
---|
8473 | }
|
---|
8474 |
|
---|
8475 | /**
|
---|
8476 | * Copies an image from one HDD container to another.
|
---|
8477 | * The copy is opened in the target HDD container.
|
---|
8478 | * It is possible to convert between different image formats, because the
|
---|
8479 | * backend for the destination may be different from the source.
|
---|
8480 | * If both the source and destination reference the same HDD container,
|
---|
8481 | * then the image is moved (by copying/deleting or renaming) to the new location.
|
---|
8482 | * The source container is unchanged if the move operation fails, otherwise
|
---|
8483 | * the image at the new location is opened in the same way as the old one was.
|
---|
8484 | *
|
---|
8485 | * @returns VBox status code.
|
---|
8486 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
8487 | * @param pDiskFrom Pointer to source HDD container.
|
---|
8488 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
8489 | * @param pDiskTo Pointer to destination HDD container.
|
---|
8490 | * @param pszBackend Name of the image file backend to use.
|
---|
8491 | * @param pszFilename New name of the image (may be NULL if pDiskFrom == pDiskTo).
|
---|
8492 | * @param fMoveByRename If true, attempt to perform a move by renaming (if successful the new size is ignored).
|
---|
8493 | * @param cbSize New image size (0 means leave unchanged).
|
---|
8494 | * @param uImageFlags Flags specifying special destination image features.
|
---|
8495 | * @param pDstUuid New UUID of the destination image. If NULL, a new UUID is created.
|
---|
8496 | * This parameter is used if and only if a true copy is created.
|
---|
8497 | * In all rename/move cases the UUIDs are copied over.
|
---|
8498 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
8499 | * Only used if the destination image is created.
|
---|
8500 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
8501 | * @param pDstVDIfsImage Pointer to the per-image VD interface list, for the
|
---|
8502 | * destination image.
|
---|
8503 | * @param pDstVDIfsOperation Pointer to the per-image VD interface list,
|
---|
8504 | * for the destination image.
|
---|
8505 | */
|
---|
8506 | VBOXDDU_DECL(int) VDCopy(PVBOXHDD pDiskFrom, unsigned nImage, PVBOXHDD pDiskTo,
|
---|
8507 | const char *pszBackend, const char *pszFilename,
|
---|
8508 | bool fMoveByRename, uint64_t cbSize,
|
---|
8509 | unsigned uImageFlags, PCRTUUID pDstUuid,
|
---|
8510 | unsigned uOpenFlags, PVDINTERFACE pVDIfsOperation,
|
---|
8511 | PVDINTERFACE pDstVDIfsImage,
|
---|
8512 | PVDINTERFACE pDstVDIfsOperation)
|
---|
8513 | {
|
---|
8514 | return VDCopyEx(pDiskFrom, nImage, pDiskTo, pszBackend, pszFilename, fMoveByRename,
|
---|
8515 | cbSize, VD_IMAGE_CONTENT_UNKNOWN, VD_IMAGE_CONTENT_UNKNOWN,
|
---|
8516 | uImageFlags, pDstUuid, uOpenFlags, pVDIfsOperation,
|
---|
8517 | pDstVDIfsImage, pDstVDIfsOperation);
|
---|
8518 | }
|
---|
8519 |
|
---|
8520 | /**
|
---|
8521 | * Optimizes the storage consumption of an image. Typically the unused blocks
|
---|
8522 | * have to be wiped with zeroes to achieve a substantial reduced storage use.
|
---|
8523 | * Another optimization done is reordering the image blocks, which can provide
|
---|
8524 | * a significant performance boost, as reads and writes tend to use less random
|
---|
8525 | * file offsets.
|
---|
8526 | *
|
---|
8527 | * @return VBox status code.
|
---|
8528 | * @return VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
8529 | * @return VERR_VD_IMAGE_READ_ONLY if image is not writable.
|
---|
8530 | * @return VERR_NOT_SUPPORTED if this kind of image can be compacted, but
|
---|
8531 | * the code for this isn't implemented yet.
|
---|
8532 | * @param pDisk Pointer to HDD container.
|
---|
8533 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
8534 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
8535 | */
|
---|
8536 | VBOXDDU_DECL(int) VDCompact(PVBOXHDD pDisk, unsigned nImage,
|
---|
8537 | PVDINTERFACE pVDIfsOperation)
|
---|
8538 | {
|
---|
8539 | int rc = VINF_SUCCESS;
|
---|
8540 | int rc2;
|
---|
8541 | bool fLockRead = false, fLockWrite = false;
|
---|
8542 | void *pvBuf = NULL;
|
---|
8543 | void *pvTmp = NULL;
|
---|
8544 |
|
---|
8545 | LogFlowFunc(("pDisk=%#p nImage=%u pVDIfsOperation=%#p\n",
|
---|
8546 | pDisk, nImage, pVDIfsOperation));
|
---|
8547 |
|
---|
8548 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
8549 |
|
---|
8550 | do {
|
---|
8551 | /* Check arguments. */
|
---|
8552 | AssertMsgBreakStmt(VALID_PTR(pDisk), ("pDisk=%#p\n", pDisk),
|
---|
8553 | rc = VERR_INVALID_PARAMETER);
|
---|
8554 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE,
|
---|
8555 | ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8556 |
|
---|
8557 | rc2 = vdThreadStartRead(pDisk);
|
---|
8558 | AssertRC(rc2);
|
---|
8559 | fLockRead = true;
|
---|
8560 |
|
---|
8561 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
8562 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
8563 |
|
---|
8564 | /* If there is no compact callback for not file based backends then
|
---|
8565 | * the backend doesn't need compaction. No need to make much fuss about
|
---|
8566 | * this. For file based ones signal this as not yet supported. */
|
---|
8567 | if (!pImage->Backend->pfnCompact)
|
---|
8568 | {
|
---|
8569 | if (pImage->Backend->uBackendCaps & VD_CAP_FILE)
|
---|
8570 | rc = VERR_NOT_SUPPORTED;
|
---|
8571 | else
|
---|
8572 | rc = VINF_SUCCESS;
|
---|
8573 | break;
|
---|
8574 | }
|
---|
8575 |
|
---|
8576 | /* Insert interface for reading parent state into per-operation list,
|
---|
8577 | * if there is a parent image. */
|
---|
8578 | VDINTERFACEPARENTSTATE VDIfParent;
|
---|
8579 | VDPARENTSTATEDESC ParentUser;
|
---|
8580 | if (pImage->pPrev)
|
---|
8581 | {
|
---|
8582 | VDIfParent.pfnParentRead = vdParentRead;
|
---|
8583 | ParentUser.pDisk = pDisk;
|
---|
8584 | ParentUser.pImage = pImage->pPrev;
|
---|
8585 | rc = VDInterfaceAdd(&VDIfParent.Core, "VDCompact_ParentState", VDINTERFACETYPE_PARENTSTATE,
|
---|
8586 | &ParentUser, sizeof(VDINTERFACEPARENTSTATE), &pVDIfsOperation);
|
---|
8587 | AssertRC(rc);
|
---|
8588 | }
|
---|
8589 |
|
---|
8590 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8591 | AssertRC(rc2);
|
---|
8592 | fLockRead = false;
|
---|
8593 |
|
---|
8594 | rc2 = vdThreadStartWrite(pDisk);
|
---|
8595 | AssertRC(rc2);
|
---|
8596 | fLockWrite = true;
|
---|
8597 |
|
---|
8598 | rc = pImage->Backend->pfnCompact(pImage->pBackendData,
|
---|
8599 | 0, 99,
|
---|
8600 | pDisk->pVDIfsDisk,
|
---|
8601 | pImage->pVDIfsImage,
|
---|
8602 | pVDIfsOperation);
|
---|
8603 | } while (0);
|
---|
8604 |
|
---|
8605 | if (RT_UNLIKELY(fLockWrite))
|
---|
8606 | {
|
---|
8607 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
8608 | AssertRC(rc2);
|
---|
8609 | }
|
---|
8610 | else if (RT_UNLIKELY(fLockRead))
|
---|
8611 | {
|
---|
8612 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8613 | AssertRC(rc2);
|
---|
8614 | }
|
---|
8615 |
|
---|
8616 | if (pvBuf)
|
---|
8617 | RTMemTmpFree(pvBuf);
|
---|
8618 | if (pvTmp)
|
---|
8619 | RTMemTmpFree(pvTmp);
|
---|
8620 |
|
---|
8621 | if (RT_SUCCESS(rc))
|
---|
8622 | {
|
---|
8623 | if (pIfProgress && pIfProgress->pfnProgress)
|
---|
8624 | pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
|
---|
8625 | }
|
---|
8626 |
|
---|
8627 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8628 | return rc;
|
---|
8629 | }
|
---|
8630 |
|
---|
8631 | /**
|
---|
8632 | * Resizes the given disk image to the given size.
|
---|
8633 | *
|
---|
8634 | * @return VBox status
|
---|
8635 | * @return VERR_VD_IMAGE_READ_ONLY if image is not writable.
|
---|
8636 | * @return VERR_NOT_SUPPORTED if this kind of image can be compacted, but
|
---|
8637 | *
|
---|
8638 | * @param pDisk Pointer to the HDD container.
|
---|
8639 | * @param cbSize New size of the image.
|
---|
8640 | * @param pPCHSGeometry Pointer to the new physical disk geometry <= (16383,16,63). Not NULL.
|
---|
8641 | * @param pLCHSGeometry Pointer to the new logical disk geometry <= (x,255,63). Not NULL.
|
---|
8642 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
8643 | */
|
---|
8644 | VBOXDDU_DECL(int) VDResize(PVBOXHDD pDisk, uint64_t cbSize,
|
---|
8645 | PCVDGEOMETRY pPCHSGeometry,
|
---|
8646 | PCVDGEOMETRY pLCHSGeometry,
|
---|
8647 | PVDINTERFACE pVDIfsOperation)
|
---|
8648 | {
|
---|
8649 | /** @todo r=klaus resizing was designed to be part of VDCopy, so having a separate function is not desirable. */
|
---|
8650 | int rc = VINF_SUCCESS;
|
---|
8651 | int rc2;
|
---|
8652 | bool fLockRead = false, fLockWrite = false;
|
---|
8653 |
|
---|
8654 | LogFlowFunc(("pDisk=%#p cbSize=%llu pVDIfsOperation=%#p\n",
|
---|
8655 | pDisk, cbSize, pVDIfsOperation));
|
---|
8656 |
|
---|
8657 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
8658 |
|
---|
8659 | do {
|
---|
8660 | /* Check arguments. */
|
---|
8661 | AssertMsgBreakStmt(VALID_PTR(pDisk), ("pDisk=%#p\n", pDisk),
|
---|
8662 | rc = VERR_INVALID_PARAMETER);
|
---|
8663 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE,
|
---|
8664 | ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8665 |
|
---|
8666 | rc2 = vdThreadStartRead(pDisk);
|
---|
8667 | AssertRC(rc2);
|
---|
8668 | fLockRead = true;
|
---|
8669 |
|
---|
8670 | /* Must have at least one image in the chain, will resize last. */
|
---|
8671 | AssertMsgBreakStmt(pDisk->cImages >= 1, ("cImages=%u\n", pDisk->cImages),
|
---|
8672 | rc = VERR_NOT_SUPPORTED);
|
---|
8673 |
|
---|
8674 | PVDIMAGE pImage = pDisk->pLast;
|
---|
8675 |
|
---|
8676 | /* If there is no compact callback for not file based backends then
|
---|
8677 | * the backend doesn't need compaction. No need to make much fuss about
|
---|
8678 | * this. For file based ones signal this as not yet supported. */
|
---|
8679 | if (!pImage->Backend->pfnResize)
|
---|
8680 | {
|
---|
8681 | if (pImage->Backend->uBackendCaps & VD_CAP_FILE)
|
---|
8682 | rc = VERR_NOT_SUPPORTED;
|
---|
8683 | else
|
---|
8684 | rc = VINF_SUCCESS;
|
---|
8685 | break;
|
---|
8686 | }
|
---|
8687 |
|
---|
8688 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8689 | AssertRC(rc2);
|
---|
8690 | fLockRead = false;
|
---|
8691 |
|
---|
8692 | rc2 = vdThreadStartWrite(pDisk);
|
---|
8693 | AssertRC(rc2);
|
---|
8694 | fLockWrite = true;
|
---|
8695 |
|
---|
8696 | VDGEOMETRY PCHSGeometryOld;
|
---|
8697 | VDGEOMETRY LCHSGeometryOld;
|
---|
8698 | PCVDGEOMETRY pPCHSGeometryNew;
|
---|
8699 | PCVDGEOMETRY pLCHSGeometryNew;
|
---|
8700 |
|
---|
8701 | if (pPCHSGeometry->cCylinders == 0)
|
---|
8702 | {
|
---|
8703 | /* Auto-detect marker, calculate new value ourself. */
|
---|
8704 | rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData, &PCHSGeometryOld);
|
---|
8705 | if (RT_SUCCESS(rc) && (PCHSGeometryOld.cCylinders != 0))
|
---|
8706 | PCHSGeometryOld.cCylinders = RT_MIN(cbSize / 512 / PCHSGeometryOld.cHeads / PCHSGeometryOld.cSectors, 16383);
|
---|
8707 | else if (rc == VERR_VD_GEOMETRY_NOT_SET)
|
---|
8708 | rc = VINF_SUCCESS;
|
---|
8709 |
|
---|
8710 | pPCHSGeometryNew = &PCHSGeometryOld;
|
---|
8711 | }
|
---|
8712 | else
|
---|
8713 | pPCHSGeometryNew = pPCHSGeometry;
|
---|
8714 |
|
---|
8715 | if (pLCHSGeometry->cCylinders == 0)
|
---|
8716 | {
|
---|
8717 | /* Auto-detect marker, calculate new value ourself. */
|
---|
8718 | rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData, &LCHSGeometryOld);
|
---|
8719 | if (RT_SUCCESS(rc) && (LCHSGeometryOld.cCylinders != 0))
|
---|
8720 | LCHSGeometryOld.cCylinders = cbSize / 512 / LCHSGeometryOld.cHeads / LCHSGeometryOld.cSectors;
|
---|
8721 | else if (rc == VERR_VD_GEOMETRY_NOT_SET)
|
---|
8722 | rc = VINF_SUCCESS;
|
---|
8723 |
|
---|
8724 | pLCHSGeometryNew = &LCHSGeometryOld;
|
---|
8725 | }
|
---|
8726 | else
|
---|
8727 | pLCHSGeometryNew = pLCHSGeometry;
|
---|
8728 |
|
---|
8729 | if (RT_SUCCESS(rc))
|
---|
8730 | rc = pImage->Backend->pfnResize(pImage->pBackendData,
|
---|
8731 | cbSize,
|
---|
8732 | pPCHSGeometryNew,
|
---|
8733 | pLCHSGeometryNew,
|
---|
8734 | 0, 99,
|
---|
8735 | pDisk->pVDIfsDisk,
|
---|
8736 | pImage->pVDIfsImage,
|
---|
8737 | pVDIfsOperation);
|
---|
8738 | } while (0);
|
---|
8739 |
|
---|
8740 | if (RT_UNLIKELY(fLockWrite))
|
---|
8741 | {
|
---|
8742 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
8743 | AssertRC(rc2);
|
---|
8744 | }
|
---|
8745 | else if (RT_UNLIKELY(fLockRead))
|
---|
8746 | {
|
---|
8747 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8748 | AssertRC(rc2);
|
---|
8749 | }
|
---|
8750 |
|
---|
8751 | if (RT_SUCCESS(rc))
|
---|
8752 | {
|
---|
8753 | if (pIfProgress && pIfProgress->pfnProgress)
|
---|
8754 | pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
|
---|
8755 |
|
---|
8756 | pDisk->cbSize = cbSize;
|
---|
8757 | }
|
---|
8758 |
|
---|
8759 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8760 | return rc;
|
---|
8761 | }
|
---|
8762 |
|
---|
8763 | VBOXDDU_DECL(int) VDPrepareWithFilters(PVBOXHDD pDisk, PVDINTERFACE pVDIfsOperation)
|
---|
8764 | {
|
---|
8765 | int rc = VINF_SUCCESS;
|
---|
8766 | int rc2;
|
---|
8767 | bool fLockRead = false, fLockWrite = false;
|
---|
8768 |
|
---|
8769 | LogFlowFunc(("pDisk=%#p pVDIfsOperation=%#p\n", pDisk, pVDIfsOperation));
|
---|
8770 |
|
---|
8771 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
8772 |
|
---|
8773 | do {
|
---|
8774 | /* Check arguments. */
|
---|
8775 | AssertMsgBreakStmt(VALID_PTR(pDisk), ("pDisk=%#p\n", pDisk),
|
---|
8776 | rc = VERR_INVALID_PARAMETER);
|
---|
8777 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE,
|
---|
8778 | ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8779 |
|
---|
8780 | rc2 = vdThreadStartRead(pDisk);
|
---|
8781 | AssertRC(rc2);
|
---|
8782 | fLockRead = true;
|
---|
8783 |
|
---|
8784 | /* Must have at least one image in the chain. */
|
---|
8785 | AssertMsgBreakStmt(pDisk->cImages >= 1, ("cImages=%u\n", pDisk->cImages),
|
---|
8786 | rc = VERR_VD_NOT_OPENED);
|
---|
8787 |
|
---|
8788 | unsigned uOpenFlags = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pBackendData);
|
---|
8789 | AssertMsgBreakStmt(!(uOpenFlags & VD_OPEN_FLAGS_READONLY),
|
---|
8790 | ("Last image should be read write"),
|
---|
8791 | rc = VERR_VD_IMAGE_READ_ONLY);
|
---|
8792 |
|
---|
8793 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8794 | AssertRC(rc2);
|
---|
8795 | fLockRead = false;
|
---|
8796 |
|
---|
8797 | rc2 = vdThreadStartWrite(pDisk);
|
---|
8798 | AssertRC(rc2);
|
---|
8799 | fLockWrite = true;
|
---|
8800 |
|
---|
8801 | /*
|
---|
8802 | * Open all images in the chain in read write mode first to avoid running
|
---|
8803 | * into an error in the middle of the process.
|
---|
8804 | */
|
---|
8805 | PVDIMAGE pImage = pDisk->pBase;
|
---|
8806 |
|
---|
8807 | while (pImage)
|
---|
8808 | {
|
---|
8809 | uOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
|
---|
8810 | if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
8811 | {
|
---|
8812 | /*
|
---|
8813 | * Clear skip consistency checks because the image is made writable now and
|
---|
8814 | * skipping consistency checks is only possible for readonly images.
|
---|
8815 | */
|
---|
8816 | uOpenFlags &= ~(VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS);
|
---|
8817 | rc = pImage->Backend->pfnSetOpenFlags(pImage->pBackendData, uOpenFlags);
|
---|
8818 | if (RT_FAILURE(rc))
|
---|
8819 | break;
|
---|
8820 | }
|
---|
8821 | pImage = pImage->pNext;
|
---|
8822 | }
|
---|
8823 |
|
---|
8824 | if (RT_SUCCESS(rc))
|
---|
8825 | {
|
---|
8826 | unsigned cImgCur = 0;
|
---|
8827 | unsigned uPercentStart = 0;
|
---|
8828 | unsigned uPercentSpan = 100 / pDisk->cImages - 1;
|
---|
8829 |
|
---|
8830 | /* Allocate tmp buffer. */
|
---|
8831 | void *pvBuf = RTMemTmpAlloc(VD_MERGE_BUFFER_SIZE);
|
---|
8832 | if (!pvBuf)
|
---|
8833 | {
|
---|
8834 | rc = VERR_NO_MEMORY;
|
---|
8835 | break;
|
---|
8836 | }
|
---|
8837 |
|
---|
8838 | pImage = pDisk->pBase;
|
---|
8839 | pDisk->fLocked = true;
|
---|
8840 |
|
---|
8841 | while ( pImage
|
---|
8842 | && RT_SUCCESS(rc))
|
---|
8843 | {
|
---|
8844 | /* Get size of image. */
|
---|
8845 | uint64_t cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
|
---|
8846 | uint64_t cbSizeFile = pImage->Backend->pfnGetFileSize(pImage->pBackendData);
|
---|
8847 | uint64_t cbFileWritten = 0;
|
---|
8848 | uint64_t uOffset = 0;
|
---|
8849 | uint64_t cbRemaining = cbSize;
|
---|
8850 |
|
---|
8851 | do
|
---|
8852 | {
|
---|
8853 | size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
|
---|
8854 | RTSGSEG SegmentBuf;
|
---|
8855 | RTSGBUF SgBuf;
|
---|
8856 | VDIOCTX IoCtx;
|
---|
8857 |
|
---|
8858 | SegmentBuf.pvSeg = pvBuf;
|
---|
8859 | SegmentBuf.cbSeg = VD_MERGE_BUFFER_SIZE;
|
---|
8860 | RTSgBufInit(&SgBuf, &SegmentBuf, 1);
|
---|
8861 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_READ, 0, 0, NULL,
|
---|
8862 | &SgBuf, NULL, NULL, VDIOCTX_FLAGS_SYNC);
|
---|
8863 |
|
---|
8864 | rc = pImage->Backend->pfnRead(pImage->pBackendData, uOffset,
|
---|
8865 | cbThisRead, &IoCtx, &cbThisRead);
|
---|
8866 | if (rc != VERR_VD_BLOCK_FREE)
|
---|
8867 | {
|
---|
8868 | if (RT_FAILURE(rc))
|
---|
8869 | break;
|
---|
8870 |
|
---|
8871 | /* Apply filter chains. */
|
---|
8872 | rc = vdFilterChainApplyRead(pDisk, uOffset, cbThisRead, &IoCtx);
|
---|
8873 | if (RT_FAILURE(rc))
|
---|
8874 | break;
|
---|
8875 |
|
---|
8876 | rc = vdFilterChainApplyWrite(pDisk, uOffset, cbThisRead, &IoCtx);
|
---|
8877 | if (RT_FAILURE(rc))
|
---|
8878 | break;
|
---|
8879 |
|
---|
8880 | RTSgBufReset(&SgBuf);
|
---|
8881 | size_t cbThisWrite = 0;
|
---|
8882 | size_t cbPreRead = 0;
|
---|
8883 | size_t cbPostRead = 0;
|
---|
8884 | rc = pImage->Backend->pfnWrite(pImage->pBackendData, uOffset,
|
---|
8885 | cbThisRead, &IoCtx, &cbThisWrite,
|
---|
8886 | &cbPreRead, &cbPostRead, 0);
|
---|
8887 | if (RT_FAILURE(rc))
|
---|
8888 | break;
|
---|
8889 | Assert(cbThisWrite == cbThisRead);
|
---|
8890 | cbFileWritten += cbThisWrite;
|
---|
8891 | }
|
---|
8892 | else
|
---|
8893 | rc = VINF_SUCCESS;
|
---|
8894 |
|
---|
8895 | uOffset += cbThisRead;
|
---|
8896 | cbRemaining -= cbThisRead;
|
---|
8897 |
|
---|
8898 | if (pIfProgress && pIfProgress->pfnProgress)
|
---|
8899 | {
|
---|
8900 | rc2 = pIfProgress->pfnProgress(pIfProgress->Core.pvUser,
|
---|
8901 | uPercentStart + cbFileWritten * uPercentSpan / cbSizeFile);
|
---|
8902 | AssertRC(rc2); /* Cancelling this operation without leaving an inconsistent state is not possible. */
|
---|
8903 | }
|
---|
8904 | } while (uOffset < cbSize);
|
---|
8905 |
|
---|
8906 | pImage = pImage->pNext;
|
---|
8907 | cImgCur++;
|
---|
8908 | uPercentStart += uPercentSpan;
|
---|
8909 | }
|
---|
8910 |
|
---|
8911 | pDisk->fLocked = false;
|
---|
8912 | if (pvBuf)
|
---|
8913 | RTMemTmpFree(pvBuf);
|
---|
8914 | }
|
---|
8915 |
|
---|
8916 | /* Change images except last one back to readonly. */
|
---|
8917 | pImage = pDisk->pBase;
|
---|
8918 | while ( pImage != pDisk->pLast
|
---|
8919 | && pImage)
|
---|
8920 | {
|
---|
8921 | uOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
|
---|
8922 | uOpenFlags |= VD_OPEN_FLAGS_READONLY;
|
---|
8923 | rc2 = pImage->Backend->pfnSetOpenFlags(pImage->pBackendData, uOpenFlags);
|
---|
8924 | if (RT_FAILURE(rc2))
|
---|
8925 | {
|
---|
8926 | if (RT_SUCCESS(rc))
|
---|
8927 | rc = rc2;
|
---|
8928 | break;
|
---|
8929 | }
|
---|
8930 | pImage = pImage->pNext;
|
---|
8931 | }
|
---|
8932 | } while (0);
|
---|
8933 |
|
---|
8934 | if (RT_UNLIKELY(fLockWrite))
|
---|
8935 | {
|
---|
8936 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
8937 | AssertRC(rc2);
|
---|
8938 | }
|
---|
8939 | else if (RT_UNLIKELY(fLockRead))
|
---|
8940 | {
|
---|
8941 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8942 | AssertRC(rc2);
|
---|
8943 | }
|
---|
8944 |
|
---|
8945 | if ( RT_SUCCESS(rc)
|
---|
8946 | && pIfProgress
|
---|
8947 | && pIfProgress->pfnProgress)
|
---|
8948 | pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
|
---|
8949 |
|
---|
8950 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8951 | return rc;
|
---|
8952 | }
|
---|
8953 |
|
---|
8954 | /**
|
---|
8955 | * Closes the last opened image file in HDD container.
|
---|
8956 | * If previous image file was opened in read-only mode (the normal case) and
|
---|
8957 | * the last opened image is in read-write mode then the previous image will be
|
---|
8958 | * reopened in read/write mode.
|
---|
8959 | *
|
---|
8960 | * @returns VBox status code.
|
---|
8961 | * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
|
---|
8962 | * @param pDisk Pointer to HDD container.
|
---|
8963 | * @param fDelete If true, delete the image from the host disk.
|
---|
8964 | */
|
---|
8965 | VBOXDDU_DECL(int) VDClose(PVBOXHDD pDisk, bool fDelete)
|
---|
8966 | {
|
---|
8967 | int rc = VINF_SUCCESS;
|
---|
8968 | int rc2;
|
---|
8969 | bool fLockWrite = false;
|
---|
8970 |
|
---|
8971 | LogFlowFunc(("pDisk=%#p fDelete=%d\n", pDisk, fDelete));
|
---|
8972 | do
|
---|
8973 | {
|
---|
8974 | /* sanity check */
|
---|
8975 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8976 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8977 |
|
---|
8978 | /* Not worth splitting this up into a read lock phase and write
|
---|
8979 | * lock phase, as closing an image is a relatively fast operation
|
---|
8980 | * dominated by the part which needs the write lock. */
|
---|
8981 | rc2 = vdThreadStartWrite(pDisk);
|
---|
8982 | AssertRC(rc2);
|
---|
8983 | fLockWrite = true;
|
---|
8984 |
|
---|
8985 | PVDIMAGE pImage = pDisk->pLast;
|
---|
8986 | if (!pImage)
|
---|
8987 | {
|
---|
8988 | rc = VERR_VD_NOT_OPENED;
|
---|
8989 | break;
|
---|
8990 | }
|
---|
8991 |
|
---|
8992 | /* Destroy the current discard state first which might still have pending blocks. */
|
---|
8993 | rc = vdDiscardStateDestroy(pDisk);
|
---|
8994 | if (RT_FAILURE(rc))
|
---|
8995 | break;
|
---|
8996 |
|
---|
8997 | unsigned uOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
|
---|
8998 | /* Remove image from list of opened images. */
|
---|
8999 | vdRemoveImageFromList(pDisk, pImage);
|
---|
9000 | /* Close (and optionally delete) image. */
|
---|
9001 | rc = pImage->Backend->pfnClose(pImage->pBackendData, fDelete);
|
---|
9002 | /* Free remaining resources related to the image. */
|
---|
9003 | RTStrFree(pImage->pszFilename);
|
---|
9004 | RTMemFree(pImage);
|
---|
9005 |
|
---|
9006 | pImage = pDisk->pLast;
|
---|
9007 | if (!pImage)
|
---|
9008 | break;
|
---|
9009 |
|
---|
9010 | /* If disk was previously in read/write mode, make sure it will stay
|
---|
9011 | * like this (if possible) after closing this image. Set the open flags
|
---|
9012 | * accordingly. */
|
---|
9013 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
9014 | {
|
---|
9015 | uOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
|
---|
9016 | uOpenFlags &= ~ VD_OPEN_FLAGS_READONLY;
|
---|
9017 | rc = pImage->Backend->pfnSetOpenFlags(pImage->pBackendData, uOpenFlags);
|
---|
9018 | }
|
---|
9019 |
|
---|
9020 | /* Cache disk information. */
|
---|
9021 | pDisk->cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
|
---|
9022 |
|
---|
9023 | /* Cache PCHS geometry. */
|
---|
9024 | rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
9025 | &pDisk->PCHSGeometry);
|
---|
9026 | if (RT_FAILURE(rc2))
|
---|
9027 | {
|
---|
9028 | pDisk->PCHSGeometry.cCylinders = 0;
|
---|
9029 | pDisk->PCHSGeometry.cHeads = 0;
|
---|
9030 | pDisk->PCHSGeometry.cSectors = 0;
|
---|
9031 | }
|
---|
9032 | else
|
---|
9033 | {
|
---|
9034 | /* Make sure the PCHS geometry is properly clipped. */
|
---|
9035 | pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
|
---|
9036 | pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
|
---|
9037 | pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
|
---|
9038 | }
|
---|
9039 |
|
---|
9040 | /* Cache LCHS geometry. */
|
---|
9041 | rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
9042 | &pDisk->LCHSGeometry);
|
---|
9043 | if (RT_FAILURE(rc2))
|
---|
9044 | {
|
---|
9045 | pDisk->LCHSGeometry.cCylinders = 0;
|
---|
9046 | pDisk->LCHSGeometry.cHeads = 0;
|
---|
9047 | pDisk->LCHSGeometry.cSectors = 0;
|
---|
9048 | }
|
---|
9049 | else
|
---|
9050 | {
|
---|
9051 | /* Make sure the LCHS geometry is properly clipped. */
|
---|
9052 | pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
|
---|
9053 | pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
|
---|
9054 | }
|
---|
9055 | } while (0);
|
---|
9056 |
|
---|
9057 | if (RT_UNLIKELY(fLockWrite))
|
---|
9058 | {
|
---|
9059 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
9060 | AssertRC(rc2);
|
---|
9061 | }
|
---|
9062 |
|
---|
9063 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9064 | return rc;
|
---|
9065 | }
|
---|
9066 |
|
---|
9067 | /**
|
---|
9068 | * Closes the currently opened cache image file in HDD container.
|
---|
9069 | *
|
---|
9070 | * @return VBox status code.
|
---|
9071 | * @return VERR_VD_NOT_OPENED if no cache is opened in HDD container.
|
---|
9072 | * @param pDisk Pointer to HDD container.
|
---|
9073 | * @param fDelete If true, delete the image from the host disk.
|
---|
9074 | */
|
---|
9075 | VBOXDDU_DECL(int) VDCacheClose(PVBOXHDD pDisk, bool fDelete)
|
---|
9076 | {
|
---|
9077 | int rc = VINF_SUCCESS;
|
---|
9078 | int rc2;
|
---|
9079 | bool fLockWrite = false;
|
---|
9080 | PVDCACHE pCache = NULL;
|
---|
9081 |
|
---|
9082 | LogFlowFunc(("pDisk=%#p fDelete=%d\n", pDisk, fDelete));
|
---|
9083 |
|
---|
9084 | do
|
---|
9085 | {
|
---|
9086 | /* sanity check */
|
---|
9087 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9088 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9089 |
|
---|
9090 | rc2 = vdThreadStartWrite(pDisk);
|
---|
9091 | AssertRC(rc2);
|
---|
9092 | fLockWrite = true;
|
---|
9093 |
|
---|
9094 | AssertPtrBreakStmt(pDisk->pCache, rc = VERR_VD_CACHE_NOT_FOUND);
|
---|
9095 |
|
---|
9096 | pCache = pDisk->pCache;
|
---|
9097 | pDisk->pCache = NULL;
|
---|
9098 |
|
---|
9099 | pCache->Backend->pfnClose(pCache->pBackendData, fDelete);
|
---|
9100 | if (pCache->pszFilename)
|
---|
9101 | RTStrFree(pCache->pszFilename);
|
---|
9102 | RTMemFree(pCache);
|
---|
9103 | } while (0);
|
---|
9104 |
|
---|
9105 | if (RT_LIKELY(fLockWrite))
|
---|
9106 | {
|
---|
9107 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
9108 | AssertRC(rc2);
|
---|
9109 | }
|
---|
9110 |
|
---|
9111 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9112 | return rc;
|
---|
9113 | }
|
---|
9114 |
|
---|
9115 | VBOXDDU_DECL(int) VDFilterRemove(PVBOXHDD pDisk, uint32_t fFlags)
|
---|
9116 | {
|
---|
9117 | int rc = VINF_SUCCESS;
|
---|
9118 | int rc2;
|
---|
9119 | bool fLockWrite = false;
|
---|
9120 | PVDFILTER pFilter = NULL;
|
---|
9121 |
|
---|
9122 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
9123 |
|
---|
9124 | do
|
---|
9125 | {
|
---|
9126 | /* sanity check */
|
---|
9127 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9128 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9129 |
|
---|
9130 | AssertMsgBreakStmt(!(fFlags & ~VD_FILTER_FLAGS_MASK),
|
---|
9131 | ("Invalid flags set (fFlags=%#x)\n", fFlags),
|
---|
9132 | rc = VERR_INVALID_PARAMETER);
|
---|
9133 |
|
---|
9134 | rc2 = vdThreadStartWrite(pDisk);
|
---|
9135 | AssertRC(rc2);
|
---|
9136 | fLockWrite = true;
|
---|
9137 |
|
---|
9138 | if (fFlags & VD_FILTER_FLAGS_WRITE)
|
---|
9139 | {
|
---|
9140 | AssertBreakStmt(!RTListIsEmpty(&pDisk->ListFilterChainWrite), rc = VERR_VD_NOT_OPENED);
|
---|
9141 | pFilter = RTListGetLast(&pDisk->ListFilterChainWrite, VDFILTER, ListNodeChainWrite);
|
---|
9142 | AssertPtr(pFilter);
|
---|
9143 | RTListNodeRemove(&pFilter->ListNodeChainWrite);
|
---|
9144 | vdFilterRelease(pFilter);
|
---|
9145 | }
|
---|
9146 |
|
---|
9147 | if (fFlags & VD_FILTER_FLAGS_READ)
|
---|
9148 | {
|
---|
9149 | AssertBreakStmt(!RTListIsEmpty(&pDisk->ListFilterChainRead), rc = VERR_VD_NOT_OPENED);
|
---|
9150 | pFilter = RTListGetLast(&pDisk->ListFilterChainRead, VDFILTER, ListNodeChainRead);
|
---|
9151 | AssertPtr(pFilter);
|
---|
9152 | RTListNodeRemove(&pFilter->ListNodeChainRead);
|
---|
9153 | vdFilterRelease(pFilter);
|
---|
9154 | }
|
---|
9155 | } while (0);
|
---|
9156 |
|
---|
9157 | if (RT_LIKELY(fLockWrite))
|
---|
9158 | {
|
---|
9159 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
9160 | AssertRC(rc2);
|
---|
9161 | }
|
---|
9162 |
|
---|
9163 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9164 | return rc;
|
---|
9165 | }
|
---|
9166 |
|
---|
9167 | /**
|
---|
9168 | * Closes all opened image files in HDD container.
|
---|
9169 | *
|
---|
9170 | * @returns VBox status code.
|
---|
9171 | * @param pDisk Pointer to HDD container.
|
---|
9172 | */
|
---|
9173 | VBOXDDU_DECL(int) VDCloseAll(PVBOXHDD pDisk)
|
---|
9174 | {
|
---|
9175 | int rc = VINF_SUCCESS;
|
---|
9176 | int rc2;
|
---|
9177 | bool fLockWrite = false;
|
---|
9178 |
|
---|
9179 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
9180 | do
|
---|
9181 | {
|
---|
9182 | /* sanity check */
|
---|
9183 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9184 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9185 |
|
---|
9186 | /* Lock the entire operation. */
|
---|
9187 | rc2 = vdThreadStartWrite(pDisk);
|
---|
9188 | AssertRC(rc2);
|
---|
9189 | fLockWrite = true;
|
---|
9190 |
|
---|
9191 | PVDCACHE pCache = pDisk->pCache;
|
---|
9192 | if (pCache)
|
---|
9193 | {
|
---|
9194 | rc2 = pCache->Backend->pfnClose(pCache->pBackendData, false);
|
---|
9195 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
9196 | rc = rc2;
|
---|
9197 |
|
---|
9198 | if (pCache->pszFilename)
|
---|
9199 | RTStrFree(pCache->pszFilename);
|
---|
9200 | RTMemFree(pCache);
|
---|
9201 | }
|
---|
9202 |
|
---|
9203 | PVDIMAGE pImage = pDisk->pLast;
|
---|
9204 | while (VALID_PTR(pImage))
|
---|
9205 | {
|
---|
9206 | PVDIMAGE pPrev = pImage->pPrev;
|
---|
9207 | /* Remove image from list of opened images. */
|
---|
9208 | vdRemoveImageFromList(pDisk, pImage);
|
---|
9209 | /* Close image. */
|
---|
9210 | rc2 = pImage->Backend->pfnClose(pImage->pBackendData, false);
|
---|
9211 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
9212 | rc = rc2;
|
---|
9213 | /* Free remaining resources related to the image. */
|
---|
9214 | RTStrFree(pImage->pszFilename);
|
---|
9215 | RTMemFree(pImage);
|
---|
9216 | pImage = pPrev;
|
---|
9217 | }
|
---|
9218 | Assert(!VALID_PTR(pDisk->pLast));
|
---|
9219 | } while (0);
|
---|
9220 |
|
---|
9221 | if (RT_UNLIKELY(fLockWrite))
|
---|
9222 | {
|
---|
9223 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
9224 | AssertRC(rc2);
|
---|
9225 | }
|
---|
9226 |
|
---|
9227 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9228 | return rc;
|
---|
9229 | }
|
---|
9230 |
|
---|
9231 | /**
|
---|
9232 | * Removes all filters of the given HDD container.
|
---|
9233 | *
|
---|
9234 | * @return VBox status code.
|
---|
9235 | * @param pDisk Pointer to HDD container.
|
---|
9236 | */
|
---|
9237 | VBOXDDU_DECL(int) VDFilterRemoveAll(PVBOXHDD pDisk)
|
---|
9238 | {
|
---|
9239 | int rc = VINF_SUCCESS;
|
---|
9240 | int rc2;
|
---|
9241 | bool fLockWrite = false;
|
---|
9242 |
|
---|
9243 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
9244 | do
|
---|
9245 | {
|
---|
9246 | /* sanity check */
|
---|
9247 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9248 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9249 |
|
---|
9250 | /* Lock the entire operation. */
|
---|
9251 | rc2 = vdThreadStartWrite(pDisk);
|
---|
9252 | AssertRC(rc2);
|
---|
9253 | fLockWrite = true;
|
---|
9254 |
|
---|
9255 | PVDFILTER pFilter, pFilterNext;
|
---|
9256 | RTListForEachSafe(&pDisk->ListFilterChainWrite, pFilter, pFilterNext, VDFILTER, ListNodeChainWrite)
|
---|
9257 | {
|
---|
9258 | RTListNodeRemove(&pFilter->ListNodeChainWrite);
|
---|
9259 | vdFilterRelease(pFilter);
|
---|
9260 | }
|
---|
9261 |
|
---|
9262 | RTListForEachSafe(&pDisk->ListFilterChainRead, pFilter, pFilterNext, VDFILTER, ListNodeChainRead)
|
---|
9263 | {
|
---|
9264 | RTListNodeRemove(&pFilter->ListNodeChainRead);
|
---|
9265 | vdFilterRelease(pFilter);
|
---|
9266 | }
|
---|
9267 | Assert(RTListIsEmpty(&pDisk->ListFilterChainRead));
|
---|
9268 | Assert(RTListIsEmpty(&pDisk->ListFilterChainWrite));
|
---|
9269 | } while (0);
|
---|
9270 |
|
---|
9271 | if (RT_UNLIKELY(fLockWrite))
|
---|
9272 | {
|
---|
9273 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
9274 | AssertRC(rc2);
|
---|
9275 | }
|
---|
9276 |
|
---|
9277 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9278 | return rc;
|
---|
9279 | }
|
---|
9280 |
|
---|
9281 | /**
|
---|
9282 | * Read data from virtual HDD.
|
---|
9283 | *
|
---|
9284 | * @returns VBox status code.
|
---|
9285 | * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
|
---|
9286 | * @param pDisk Pointer to HDD container.
|
---|
9287 | * @param uOffset Offset of first reading byte from start of disk.
|
---|
9288 | * @param pvBuf Pointer to buffer for reading data.
|
---|
9289 | * @param cbRead Number of bytes to read.
|
---|
9290 | */
|
---|
9291 | VBOXDDU_DECL(int) VDRead(PVBOXHDD pDisk, uint64_t uOffset, void *pvBuf,
|
---|
9292 | size_t cbRead)
|
---|
9293 | {
|
---|
9294 | int rc = VINF_SUCCESS;
|
---|
9295 | int rc2;
|
---|
9296 | bool fLockRead = false;
|
---|
9297 |
|
---|
9298 | LogFlowFunc(("pDisk=%#p uOffset=%llu pvBuf=%p cbRead=%zu\n",
|
---|
9299 | pDisk, uOffset, pvBuf, cbRead));
|
---|
9300 | do
|
---|
9301 | {
|
---|
9302 | /* sanity check */
|
---|
9303 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9304 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9305 |
|
---|
9306 | /* Check arguments. */
|
---|
9307 | AssertMsgBreakStmt(VALID_PTR(pvBuf),
|
---|
9308 | ("pvBuf=%#p\n", pvBuf),
|
---|
9309 | rc = VERR_INVALID_PARAMETER);
|
---|
9310 | AssertMsgBreakStmt(cbRead,
|
---|
9311 | ("cbRead=%zu\n", cbRead),
|
---|
9312 | rc = VERR_INVALID_PARAMETER);
|
---|
9313 |
|
---|
9314 | rc2 = vdThreadStartRead(pDisk);
|
---|
9315 | AssertRC(rc2);
|
---|
9316 | fLockRead = true;
|
---|
9317 |
|
---|
9318 | PVDIMAGE pImage = pDisk->pLast;
|
---|
9319 | AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
|
---|
9320 |
|
---|
9321 | if (uOffset + cbRead > pDisk->cbSize)
|
---|
9322 | {
|
---|
9323 | /* Floppy images might be smaller than the standard expected by
|
---|
9324 | the floppy controller code. So, we won't fail here. */
|
---|
9325 | AssertMsgBreakStmt(pDisk->enmType == VDTYPE_FLOPPY,
|
---|
9326 | ("uOffset=%llu cbRead=%zu pDisk->cbSize=%llu\n",
|
---|
9327 | uOffset, cbRead, pDisk->cbSize),
|
---|
9328 | rc = VERR_EOF);
|
---|
9329 | memset(pvBuf, 0xf6, cbRead); /* f6h = format.com filler byte */
|
---|
9330 | if (uOffset >= pDisk->cbSize)
|
---|
9331 | break;
|
---|
9332 | cbRead = pDisk->cbSize - uOffset;
|
---|
9333 | }
|
---|
9334 |
|
---|
9335 | rc = vdReadHelper(pDisk, pImage, uOffset, pvBuf, cbRead,
|
---|
9336 | true /* fUpdateCache */);
|
---|
9337 | } while (0);
|
---|
9338 |
|
---|
9339 | if (RT_UNLIKELY(fLockRead))
|
---|
9340 | {
|
---|
9341 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9342 | AssertRC(rc2);
|
---|
9343 | }
|
---|
9344 |
|
---|
9345 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9346 | return rc;
|
---|
9347 | }
|
---|
9348 |
|
---|
9349 | /**
|
---|
9350 | * Write data to virtual HDD.
|
---|
9351 | *
|
---|
9352 | * @returns VBox status code.
|
---|
9353 | * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
|
---|
9354 | * @param pDisk Pointer to HDD container.
|
---|
9355 | * @param uOffset Offset of the first byte being
|
---|
9356 | * written from start of disk.
|
---|
9357 | * @param pvBuf Pointer to buffer for writing data.
|
---|
9358 | * @param cbWrite Number of bytes to write.
|
---|
9359 | */
|
---|
9360 | VBOXDDU_DECL(int) VDWrite(PVBOXHDD pDisk, uint64_t uOffset, const void *pvBuf,
|
---|
9361 | size_t cbWrite)
|
---|
9362 | {
|
---|
9363 | int rc = VINF_SUCCESS;
|
---|
9364 | int rc2;
|
---|
9365 | bool fLockWrite = false;
|
---|
9366 |
|
---|
9367 | LogFlowFunc(("pDisk=%#p uOffset=%llu pvBuf=%p cbWrite=%zu\n",
|
---|
9368 | pDisk, uOffset, pvBuf, cbWrite));
|
---|
9369 | do
|
---|
9370 | {
|
---|
9371 | /* sanity check */
|
---|
9372 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9373 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9374 |
|
---|
9375 | /* Check arguments. */
|
---|
9376 | AssertMsgBreakStmt(VALID_PTR(pvBuf),
|
---|
9377 | ("pvBuf=%#p\n", pvBuf),
|
---|
9378 | rc = VERR_INVALID_PARAMETER);
|
---|
9379 | AssertMsgBreakStmt(cbWrite,
|
---|
9380 | ("cbWrite=%zu\n", cbWrite),
|
---|
9381 | rc = VERR_INVALID_PARAMETER);
|
---|
9382 |
|
---|
9383 | rc2 = vdThreadStartWrite(pDisk);
|
---|
9384 | AssertRC(rc2);
|
---|
9385 | fLockWrite = true;
|
---|
9386 |
|
---|
9387 | AssertMsgBreakStmt(uOffset + cbWrite <= pDisk->cbSize,
|
---|
9388 | ("uOffset=%llu cbWrite=%zu pDisk->cbSize=%llu\n",
|
---|
9389 | uOffset, cbWrite, pDisk->cbSize),
|
---|
9390 | rc = VERR_INVALID_PARAMETER);
|
---|
9391 |
|
---|
9392 | PVDIMAGE pImage = pDisk->pLast;
|
---|
9393 | AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
|
---|
9394 |
|
---|
9395 | vdSetModifiedFlag(pDisk);
|
---|
9396 | rc = vdWriteHelper(pDisk, pImage, uOffset, pvBuf, cbWrite,
|
---|
9397 | VDIOCTX_FLAGS_READ_UPDATE_CACHE);
|
---|
9398 | if (RT_FAILURE(rc))
|
---|
9399 | break;
|
---|
9400 |
|
---|
9401 | /* If there is a merge (in the direction towards a parent) running
|
---|
9402 | * concurrently then we have to also "relay" the write to this parent,
|
---|
9403 | * as the merge position might be already past the position where
|
---|
9404 | * this write is going. The "context" of the write can come from the
|
---|
9405 | * natural chain, since merging either already did or will take care
|
---|
9406 | * of the "other" content which is might be needed to fill the block
|
---|
9407 | * to a full allocation size. The cache doesn't need to be touched
|
---|
9408 | * as this write is covered by the previous one. */
|
---|
9409 | if (RT_UNLIKELY(pDisk->pImageRelay))
|
---|
9410 | rc = vdWriteHelper(pDisk, pDisk->pImageRelay, uOffset,
|
---|
9411 | pvBuf, cbWrite, VDIOCTX_FLAGS_DEFAULT);
|
---|
9412 | } while (0);
|
---|
9413 |
|
---|
9414 | if (RT_UNLIKELY(fLockWrite))
|
---|
9415 | {
|
---|
9416 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
9417 | AssertRC(rc2);
|
---|
9418 | }
|
---|
9419 |
|
---|
9420 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9421 | return rc;
|
---|
9422 | }
|
---|
9423 |
|
---|
9424 | /**
|
---|
9425 | * Make sure the on disk representation of a virtual HDD is up to date.
|
---|
9426 | *
|
---|
9427 | * @returns VBox status code.
|
---|
9428 | * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
|
---|
9429 | * @param pDisk Pointer to HDD container.
|
---|
9430 | */
|
---|
9431 | VBOXDDU_DECL(int) VDFlush(PVBOXHDD pDisk)
|
---|
9432 | {
|
---|
9433 | int rc = VINF_SUCCESS;
|
---|
9434 | int rc2;
|
---|
9435 | bool fLockWrite = false;
|
---|
9436 |
|
---|
9437 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
9438 | do
|
---|
9439 | {
|
---|
9440 | /* sanity check */
|
---|
9441 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9442 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9443 |
|
---|
9444 | rc2 = vdThreadStartWrite(pDisk);
|
---|
9445 | AssertRC(rc2);
|
---|
9446 | fLockWrite = true;
|
---|
9447 |
|
---|
9448 | PVDIMAGE pImage = pDisk->pLast;
|
---|
9449 | AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
|
---|
9450 |
|
---|
9451 | VDIOCTX IoCtx;
|
---|
9452 | RTSEMEVENT hEventComplete = NIL_RTSEMEVENT;
|
---|
9453 |
|
---|
9454 | rc = RTSemEventCreate(&hEventComplete);
|
---|
9455 | if (RT_FAILURE(rc))
|
---|
9456 | break;
|
---|
9457 |
|
---|
9458 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_FLUSH, 0, 0, pImage, NULL,
|
---|
9459 | NULL, vdFlushHelperAsync, VDIOCTX_FLAGS_SYNC | VDIOCTX_FLAGS_DONT_FREE);
|
---|
9460 |
|
---|
9461 | IoCtx.Type.Root.pfnComplete = vdIoCtxSyncComplete;
|
---|
9462 | IoCtx.Type.Root.pvUser1 = pDisk;
|
---|
9463 | IoCtx.Type.Root.pvUser2 = hEventComplete;
|
---|
9464 | rc = vdIoCtxProcessSync(&IoCtx, hEventComplete);
|
---|
9465 |
|
---|
9466 | RTSemEventDestroy(hEventComplete);
|
---|
9467 | } while (0);
|
---|
9468 |
|
---|
9469 | if (RT_UNLIKELY(fLockWrite))
|
---|
9470 | {
|
---|
9471 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
9472 | AssertRC(rc2);
|
---|
9473 | }
|
---|
9474 |
|
---|
9475 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9476 | return rc;
|
---|
9477 | }
|
---|
9478 |
|
---|
9479 | /**
|
---|
9480 | * Get number of opened images in HDD container.
|
---|
9481 | *
|
---|
9482 | * @returns Number of opened images for HDD container. 0 if no images have been opened.
|
---|
9483 | * @param pDisk Pointer to HDD container.
|
---|
9484 | */
|
---|
9485 | VBOXDDU_DECL(unsigned) VDGetCount(PVBOXHDD pDisk)
|
---|
9486 | {
|
---|
9487 | unsigned cImages;
|
---|
9488 | int rc2;
|
---|
9489 | bool fLockRead = false;
|
---|
9490 |
|
---|
9491 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
9492 | do
|
---|
9493 | {
|
---|
9494 | /* sanity check */
|
---|
9495 | AssertPtrBreakStmt(pDisk, cImages = 0);
|
---|
9496 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9497 |
|
---|
9498 | rc2 = vdThreadStartRead(pDisk);
|
---|
9499 | AssertRC(rc2);
|
---|
9500 | fLockRead = true;
|
---|
9501 |
|
---|
9502 | cImages = pDisk->cImages;
|
---|
9503 | } while (0);
|
---|
9504 |
|
---|
9505 | if (RT_UNLIKELY(fLockRead))
|
---|
9506 | {
|
---|
9507 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9508 | AssertRC(rc2);
|
---|
9509 | }
|
---|
9510 |
|
---|
9511 | LogFlowFunc(("returns %u\n", cImages));
|
---|
9512 | return cImages;
|
---|
9513 | }
|
---|
9514 |
|
---|
9515 | /**
|
---|
9516 | * Get read/write mode of HDD container.
|
---|
9517 | *
|
---|
9518 | * @returns Virtual disk ReadOnly status.
|
---|
9519 | * @returns true if no image is opened in HDD container.
|
---|
9520 | * @param pDisk Pointer to HDD container.
|
---|
9521 | */
|
---|
9522 | VBOXDDU_DECL(bool) VDIsReadOnly(PVBOXHDD pDisk)
|
---|
9523 | {
|
---|
9524 | bool fReadOnly;
|
---|
9525 | int rc2;
|
---|
9526 | bool fLockRead = false;
|
---|
9527 |
|
---|
9528 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
9529 | do
|
---|
9530 | {
|
---|
9531 | /* sanity check */
|
---|
9532 | AssertPtrBreakStmt(pDisk, fReadOnly = false);
|
---|
9533 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9534 |
|
---|
9535 | rc2 = vdThreadStartRead(pDisk);
|
---|
9536 | AssertRC(rc2);
|
---|
9537 | fLockRead = true;
|
---|
9538 |
|
---|
9539 | PVDIMAGE pImage = pDisk->pLast;
|
---|
9540 | AssertPtrBreakStmt(pImage, fReadOnly = true);
|
---|
9541 |
|
---|
9542 | unsigned uOpenFlags;
|
---|
9543 | uOpenFlags = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pBackendData);
|
---|
9544 | fReadOnly = !!(uOpenFlags & VD_OPEN_FLAGS_READONLY);
|
---|
9545 | } while (0);
|
---|
9546 |
|
---|
9547 | if (RT_UNLIKELY(fLockRead))
|
---|
9548 | {
|
---|
9549 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9550 | AssertRC(rc2);
|
---|
9551 | }
|
---|
9552 |
|
---|
9553 | LogFlowFunc(("returns %d\n", fReadOnly));
|
---|
9554 | return fReadOnly;
|
---|
9555 | }
|
---|
9556 |
|
---|
9557 | /**
|
---|
9558 | * Get sector size of an image in HDD container.
|
---|
9559 | *
|
---|
9560 | * @return Virtual disk sector size in bytes.
|
---|
9561 | * @return 0 if image with specified number was not opened.
|
---|
9562 | * @param pDisk Pointer to HDD container.
|
---|
9563 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9564 | */
|
---|
9565 | VBOXDDU_DECL(uint32_t) VDGetSectorSize(PVBOXHDD pDisk, unsigned nImage)
|
---|
9566 | {
|
---|
9567 | uint64_t cbSector;
|
---|
9568 | int rc2;
|
---|
9569 | bool fLockRead = false;
|
---|
9570 |
|
---|
9571 | LogFlowFunc(("pDisk=%#p nImage=%u\n", pDisk, nImage));
|
---|
9572 | do
|
---|
9573 | {
|
---|
9574 | /* sanity check */
|
---|
9575 | AssertPtrBreakStmt(pDisk, cbSector = 0);
|
---|
9576 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9577 |
|
---|
9578 | rc2 = vdThreadStartRead(pDisk);
|
---|
9579 | AssertRC(rc2);
|
---|
9580 | fLockRead = true;
|
---|
9581 |
|
---|
9582 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9583 | AssertPtrBreakStmt(pImage, cbSector = 0);
|
---|
9584 | cbSector = pImage->Backend->pfnGetSectorSize(pImage->pBackendData);
|
---|
9585 | } while (0);
|
---|
9586 |
|
---|
9587 | if (RT_UNLIKELY(fLockRead))
|
---|
9588 | {
|
---|
9589 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9590 | AssertRC(rc2);
|
---|
9591 | }
|
---|
9592 |
|
---|
9593 | LogFlowFunc(("returns %u\n", cbSector));
|
---|
9594 | return cbSector;
|
---|
9595 | }
|
---|
9596 |
|
---|
9597 | /**
|
---|
9598 | * Get total capacity of an image in HDD container.
|
---|
9599 | *
|
---|
9600 | * @returns Virtual disk size in bytes.
|
---|
9601 | * @returns 0 if no image with specified number was not opened.
|
---|
9602 | * @param pDisk Pointer to HDD container.
|
---|
9603 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9604 | */
|
---|
9605 | VBOXDDU_DECL(uint64_t) VDGetSize(PVBOXHDD pDisk, unsigned nImage)
|
---|
9606 | {
|
---|
9607 | uint64_t cbSize;
|
---|
9608 | int rc2;
|
---|
9609 | bool fLockRead = false;
|
---|
9610 |
|
---|
9611 | LogFlowFunc(("pDisk=%#p nImage=%u\n", pDisk, nImage));
|
---|
9612 | do
|
---|
9613 | {
|
---|
9614 | /* sanity check */
|
---|
9615 | AssertPtrBreakStmt(pDisk, cbSize = 0);
|
---|
9616 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9617 |
|
---|
9618 | rc2 = vdThreadStartRead(pDisk);
|
---|
9619 | AssertRC(rc2);
|
---|
9620 | fLockRead = true;
|
---|
9621 |
|
---|
9622 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9623 | AssertPtrBreakStmt(pImage, cbSize = 0);
|
---|
9624 | cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
|
---|
9625 | } while (0);
|
---|
9626 |
|
---|
9627 | if (RT_UNLIKELY(fLockRead))
|
---|
9628 | {
|
---|
9629 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9630 | AssertRC(rc2);
|
---|
9631 | }
|
---|
9632 |
|
---|
9633 | LogFlowFunc(("returns %llu\n", cbSize));
|
---|
9634 | return cbSize;
|
---|
9635 | }
|
---|
9636 |
|
---|
9637 | /**
|
---|
9638 | * Get total file size of an image in HDD container.
|
---|
9639 | *
|
---|
9640 | * @returns Virtual disk size in bytes.
|
---|
9641 | * @returns 0 if no image is opened in HDD container.
|
---|
9642 | * @param pDisk Pointer to HDD container.
|
---|
9643 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9644 | */
|
---|
9645 | VBOXDDU_DECL(uint64_t) VDGetFileSize(PVBOXHDD pDisk, unsigned nImage)
|
---|
9646 | {
|
---|
9647 | uint64_t cbSize;
|
---|
9648 | int rc2;
|
---|
9649 | bool fLockRead = false;
|
---|
9650 |
|
---|
9651 | LogFlowFunc(("pDisk=%#p nImage=%u\n", pDisk, nImage));
|
---|
9652 | do
|
---|
9653 | {
|
---|
9654 | /* sanity check */
|
---|
9655 | AssertPtrBreakStmt(pDisk, cbSize = 0);
|
---|
9656 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9657 |
|
---|
9658 | rc2 = vdThreadStartRead(pDisk);
|
---|
9659 | AssertRC(rc2);
|
---|
9660 | fLockRead = true;
|
---|
9661 |
|
---|
9662 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9663 | AssertPtrBreakStmt(pImage, cbSize = 0);
|
---|
9664 | cbSize = pImage->Backend->pfnGetFileSize(pImage->pBackendData);
|
---|
9665 | } while (0);
|
---|
9666 |
|
---|
9667 | if (RT_UNLIKELY(fLockRead))
|
---|
9668 | {
|
---|
9669 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9670 | AssertRC(rc2);
|
---|
9671 | }
|
---|
9672 |
|
---|
9673 | LogFlowFunc(("returns %llu\n", cbSize));
|
---|
9674 | return cbSize;
|
---|
9675 | }
|
---|
9676 |
|
---|
9677 | /**
|
---|
9678 | * Get virtual disk PCHS geometry stored in HDD container.
|
---|
9679 | *
|
---|
9680 | * @returns VBox status code.
|
---|
9681 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9682 | * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
|
---|
9683 | * @param pDisk Pointer to HDD container.
|
---|
9684 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9685 | * @param pPCHSGeometry Where to store PCHS geometry. Not NULL.
|
---|
9686 | */
|
---|
9687 | VBOXDDU_DECL(int) VDGetPCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
|
---|
9688 | PVDGEOMETRY pPCHSGeometry)
|
---|
9689 | {
|
---|
9690 | int rc = VINF_SUCCESS;
|
---|
9691 | int rc2;
|
---|
9692 | bool fLockRead = false;
|
---|
9693 |
|
---|
9694 | LogFlowFunc(("pDisk=%#p nImage=%u pPCHSGeometry=%#p\n",
|
---|
9695 | pDisk, nImage, pPCHSGeometry));
|
---|
9696 | do
|
---|
9697 | {
|
---|
9698 | /* sanity check */
|
---|
9699 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9700 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9701 |
|
---|
9702 | /* Check arguments. */
|
---|
9703 | AssertMsgBreakStmt(VALID_PTR(pPCHSGeometry),
|
---|
9704 | ("pPCHSGeometry=%#p\n", pPCHSGeometry),
|
---|
9705 | rc = VERR_INVALID_PARAMETER);
|
---|
9706 |
|
---|
9707 | rc2 = vdThreadStartRead(pDisk);
|
---|
9708 | AssertRC(rc2);
|
---|
9709 | fLockRead = true;
|
---|
9710 |
|
---|
9711 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9712 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9713 |
|
---|
9714 | if (pImage == pDisk->pLast)
|
---|
9715 | {
|
---|
9716 | /* Use cached information if possible. */
|
---|
9717 | if (pDisk->PCHSGeometry.cCylinders != 0)
|
---|
9718 | *pPCHSGeometry = pDisk->PCHSGeometry;
|
---|
9719 | else
|
---|
9720 | rc = VERR_VD_GEOMETRY_NOT_SET;
|
---|
9721 | }
|
---|
9722 | else
|
---|
9723 | rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
9724 | pPCHSGeometry);
|
---|
9725 | } while (0);
|
---|
9726 |
|
---|
9727 | if (RT_UNLIKELY(fLockRead))
|
---|
9728 | {
|
---|
9729 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9730 | AssertRC(rc2);
|
---|
9731 | }
|
---|
9732 |
|
---|
9733 | LogFlowFunc(("%Rrc (PCHS=%u/%u/%u)\n", rc,
|
---|
9734 | pDisk->PCHSGeometry.cCylinders, pDisk->PCHSGeometry.cHeads,
|
---|
9735 | pDisk->PCHSGeometry.cSectors));
|
---|
9736 | return rc;
|
---|
9737 | }
|
---|
9738 |
|
---|
9739 | /**
|
---|
9740 | * Store virtual disk PCHS geometry in HDD container.
|
---|
9741 | *
|
---|
9742 | * Note that in case of unrecoverable error all images in HDD container will be closed.
|
---|
9743 | *
|
---|
9744 | * @returns VBox status code.
|
---|
9745 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9746 | * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
|
---|
9747 | * @param pDisk Pointer to HDD container.
|
---|
9748 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9749 | * @param pPCHSGeometry Where to load PCHS geometry from. Not NULL.
|
---|
9750 | */
|
---|
9751 | VBOXDDU_DECL(int) VDSetPCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
|
---|
9752 | PCVDGEOMETRY pPCHSGeometry)
|
---|
9753 | {
|
---|
9754 | int rc = VINF_SUCCESS;
|
---|
9755 | int rc2;
|
---|
9756 | bool fLockWrite = false;
|
---|
9757 |
|
---|
9758 | LogFlowFunc(("pDisk=%#p nImage=%u pPCHSGeometry=%#p PCHS=%u/%u/%u\n",
|
---|
9759 | pDisk, nImage, pPCHSGeometry, pPCHSGeometry->cCylinders,
|
---|
9760 | pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
|
---|
9761 | do
|
---|
9762 | {
|
---|
9763 | /* sanity check */
|
---|
9764 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9765 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9766 |
|
---|
9767 | /* Check arguments. */
|
---|
9768 | AssertMsgBreakStmt( VALID_PTR(pPCHSGeometry)
|
---|
9769 | && pPCHSGeometry->cHeads <= 16
|
---|
9770 | && pPCHSGeometry->cSectors <= 63,
|
---|
9771 | ("pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pPCHSGeometry,
|
---|
9772 | pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
|
---|
9773 | pPCHSGeometry->cSectors),
|
---|
9774 | rc = VERR_INVALID_PARAMETER);
|
---|
9775 |
|
---|
9776 | rc2 = vdThreadStartWrite(pDisk);
|
---|
9777 | AssertRC(rc2);
|
---|
9778 | fLockWrite = true;
|
---|
9779 |
|
---|
9780 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9781 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9782 |
|
---|
9783 | if (pImage == pDisk->pLast)
|
---|
9784 | {
|
---|
9785 | if ( pPCHSGeometry->cCylinders != pDisk->PCHSGeometry.cCylinders
|
---|
9786 | || pPCHSGeometry->cHeads != pDisk->PCHSGeometry.cHeads
|
---|
9787 | || pPCHSGeometry->cSectors != pDisk->PCHSGeometry.cSectors)
|
---|
9788 | {
|
---|
9789 | /* Only update geometry if it is changed. Avoids similar checks
|
---|
9790 | * in every backend. Most of the time the new geometry is set
|
---|
9791 | * to the previous values, so no need to go through the hassle
|
---|
9792 | * of updating an image which could be opened in read-only mode
|
---|
9793 | * right now. */
|
---|
9794 | rc = pImage->Backend->pfnSetPCHSGeometry(pImage->pBackendData,
|
---|
9795 | pPCHSGeometry);
|
---|
9796 |
|
---|
9797 | /* Cache new geometry values in any case. */
|
---|
9798 | rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
9799 | &pDisk->PCHSGeometry);
|
---|
9800 | if (RT_FAILURE(rc2))
|
---|
9801 | {
|
---|
9802 | pDisk->PCHSGeometry.cCylinders = 0;
|
---|
9803 | pDisk->PCHSGeometry.cHeads = 0;
|
---|
9804 | pDisk->PCHSGeometry.cSectors = 0;
|
---|
9805 | }
|
---|
9806 | else
|
---|
9807 | {
|
---|
9808 | /* Make sure the CHS geometry is properly clipped. */
|
---|
9809 | pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 255);
|
---|
9810 | pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
|
---|
9811 | }
|
---|
9812 | }
|
---|
9813 | }
|
---|
9814 | else
|
---|
9815 | {
|
---|
9816 | VDGEOMETRY PCHS;
|
---|
9817 | rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
9818 | &PCHS);
|
---|
9819 | if ( RT_FAILURE(rc)
|
---|
9820 | || pPCHSGeometry->cCylinders != PCHS.cCylinders
|
---|
9821 | || pPCHSGeometry->cHeads != PCHS.cHeads
|
---|
9822 | || pPCHSGeometry->cSectors != PCHS.cSectors)
|
---|
9823 | {
|
---|
9824 | /* Only update geometry if it is changed. Avoids similar checks
|
---|
9825 | * in every backend. Most of the time the new geometry is set
|
---|
9826 | * to the previous values, so no need to go through the hassle
|
---|
9827 | * of updating an image which could be opened in read-only mode
|
---|
9828 | * right now. */
|
---|
9829 | rc = pImage->Backend->pfnSetPCHSGeometry(pImage->pBackendData,
|
---|
9830 | pPCHSGeometry);
|
---|
9831 | }
|
---|
9832 | }
|
---|
9833 | } while (0);
|
---|
9834 |
|
---|
9835 | if (RT_UNLIKELY(fLockWrite))
|
---|
9836 | {
|
---|
9837 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
9838 | AssertRC(rc2);
|
---|
9839 | }
|
---|
9840 |
|
---|
9841 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9842 | return rc;
|
---|
9843 | }
|
---|
9844 |
|
---|
9845 | /**
|
---|
9846 | * Get virtual disk LCHS geometry stored in HDD container.
|
---|
9847 | *
|
---|
9848 | * @returns VBox status code.
|
---|
9849 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9850 | * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
|
---|
9851 | * @param pDisk Pointer to HDD container.
|
---|
9852 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9853 | * @param pLCHSGeometry Where to store LCHS geometry. Not NULL.
|
---|
9854 | */
|
---|
9855 | VBOXDDU_DECL(int) VDGetLCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
|
---|
9856 | PVDGEOMETRY pLCHSGeometry)
|
---|
9857 | {
|
---|
9858 | int rc = VINF_SUCCESS;
|
---|
9859 | int rc2;
|
---|
9860 | bool fLockRead = false;
|
---|
9861 |
|
---|
9862 | LogFlowFunc(("pDisk=%#p nImage=%u pLCHSGeometry=%#p\n",
|
---|
9863 | pDisk, nImage, pLCHSGeometry));
|
---|
9864 | do
|
---|
9865 | {
|
---|
9866 | /* sanity check */
|
---|
9867 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9868 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9869 |
|
---|
9870 | /* Check arguments. */
|
---|
9871 | AssertMsgBreakStmt(VALID_PTR(pLCHSGeometry),
|
---|
9872 | ("pLCHSGeometry=%#p\n", pLCHSGeometry),
|
---|
9873 | rc = VERR_INVALID_PARAMETER);
|
---|
9874 |
|
---|
9875 | rc2 = vdThreadStartRead(pDisk);
|
---|
9876 | AssertRC(rc2);
|
---|
9877 | fLockRead = true;
|
---|
9878 |
|
---|
9879 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9880 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9881 |
|
---|
9882 | if (pImage == pDisk->pLast)
|
---|
9883 | {
|
---|
9884 | /* Use cached information if possible. */
|
---|
9885 | if (pDisk->LCHSGeometry.cCylinders != 0)
|
---|
9886 | *pLCHSGeometry = pDisk->LCHSGeometry;
|
---|
9887 | else
|
---|
9888 | rc = VERR_VD_GEOMETRY_NOT_SET;
|
---|
9889 | }
|
---|
9890 | else
|
---|
9891 | rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
9892 | pLCHSGeometry);
|
---|
9893 | } while (0);
|
---|
9894 |
|
---|
9895 | if (RT_UNLIKELY(fLockRead))
|
---|
9896 | {
|
---|
9897 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9898 | AssertRC(rc2);
|
---|
9899 | }
|
---|
9900 |
|
---|
9901 | LogFlowFunc((": %Rrc (LCHS=%u/%u/%u)\n", rc,
|
---|
9902 | pDisk->LCHSGeometry.cCylinders, pDisk->LCHSGeometry.cHeads,
|
---|
9903 | pDisk->LCHSGeometry.cSectors));
|
---|
9904 | return rc;
|
---|
9905 | }
|
---|
9906 |
|
---|
9907 | /**
|
---|
9908 | * Store virtual disk LCHS geometry in HDD container.
|
---|
9909 | *
|
---|
9910 | * Note that in case of unrecoverable error all images in HDD container will be closed.
|
---|
9911 | *
|
---|
9912 | * @returns VBox status code.
|
---|
9913 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9914 | * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
|
---|
9915 | * @param pDisk Pointer to HDD container.
|
---|
9916 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9917 | * @param pLCHSGeometry Where to load LCHS geometry from. Not NULL.
|
---|
9918 | */
|
---|
9919 | VBOXDDU_DECL(int) VDSetLCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
|
---|
9920 | PCVDGEOMETRY pLCHSGeometry)
|
---|
9921 | {
|
---|
9922 | int rc = VINF_SUCCESS;
|
---|
9923 | int rc2;
|
---|
9924 | bool fLockWrite = false;
|
---|
9925 |
|
---|
9926 | LogFlowFunc(("pDisk=%#p nImage=%u pLCHSGeometry=%#p LCHS=%u/%u/%u\n",
|
---|
9927 | pDisk, nImage, pLCHSGeometry, pLCHSGeometry->cCylinders,
|
---|
9928 | pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
|
---|
9929 | do
|
---|
9930 | {
|
---|
9931 | /* sanity check */
|
---|
9932 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9933 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9934 |
|
---|
9935 | /* Check arguments. */
|
---|
9936 | AssertMsgBreakStmt( VALID_PTR(pLCHSGeometry)
|
---|
9937 | && pLCHSGeometry->cHeads <= 255
|
---|
9938 | && pLCHSGeometry->cSectors <= 63,
|
---|
9939 | ("pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pLCHSGeometry,
|
---|
9940 | pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads,
|
---|
9941 | pLCHSGeometry->cSectors),
|
---|
9942 | rc = VERR_INVALID_PARAMETER);
|
---|
9943 |
|
---|
9944 | rc2 = vdThreadStartWrite(pDisk);
|
---|
9945 | AssertRC(rc2);
|
---|
9946 | fLockWrite = true;
|
---|
9947 |
|
---|
9948 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9949 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9950 |
|
---|
9951 | if (pImage == pDisk->pLast)
|
---|
9952 | {
|
---|
9953 | if ( pLCHSGeometry->cCylinders != pDisk->LCHSGeometry.cCylinders
|
---|
9954 | || pLCHSGeometry->cHeads != pDisk->LCHSGeometry.cHeads
|
---|
9955 | || pLCHSGeometry->cSectors != pDisk->LCHSGeometry.cSectors)
|
---|
9956 | {
|
---|
9957 | /* Only update geometry if it is changed. Avoids similar checks
|
---|
9958 | * in every backend. Most of the time the new geometry is set
|
---|
9959 | * to the previous values, so no need to go through the hassle
|
---|
9960 | * of updating an image which could be opened in read-only mode
|
---|
9961 | * right now. */
|
---|
9962 | rc = pImage->Backend->pfnSetLCHSGeometry(pImage->pBackendData,
|
---|
9963 | pLCHSGeometry);
|
---|
9964 |
|
---|
9965 | /* Cache new geometry values in any case. */
|
---|
9966 | rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
9967 | &pDisk->LCHSGeometry);
|
---|
9968 | if (RT_FAILURE(rc2))
|
---|
9969 | {
|
---|
9970 | pDisk->LCHSGeometry.cCylinders = 0;
|
---|
9971 | pDisk->LCHSGeometry.cHeads = 0;
|
---|
9972 | pDisk->LCHSGeometry.cSectors = 0;
|
---|
9973 | }
|
---|
9974 | else
|
---|
9975 | {
|
---|
9976 | /* Make sure the CHS geometry is properly clipped. */
|
---|
9977 | pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
|
---|
9978 | pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
|
---|
9979 | }
|
---|
9980 | }
|
---|
9981 | }
|
---|
9982 | else
|
---|
9983 | {
|
---|
9984 | VDGEOMETRY LCHS;
|
---|
9985 | rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
9986 | &LCHS);
|
---|
9987 | if ( RT_FAILURE(rc)
|
---|
9988 | || pLCHSGeometry->cCylinders != LCHS.cCylinders
|
---|
9989 | || pLCHSGeometry->cHeads != LCHS.cHeads
|
---|
9990 | || pLCHSGeometry->cSectors != LCHS.cSectors)
|
---|
9991 | {
|
---|
9992 | /* Only update geometry if it is changed. Avoids similar checks
|
---|
9993 | * in every backend. Most of the time the new geometry is set
|
---|
9994 | * to the previous values, so no need to go through the hassle
|
---|
9995 | * of updating an image which could be opened in read-only mode
|
---|
9996 | * right now. */
|
---|
9997 | rc = pImage->Backend->pfnSetLCHSGeometry(pImage->pBackendData,
|
---|
9998 | pLCHSGeometry);
|
---|
9999 | }
|
---|
10000 | }
|
---|
10001 | } while (0);
|
---|
10002 |
|
---|
10003 | if (RT_UNLIKELY(fLockWrite))
|
---|
10004 | {
|
---|
10005 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
10006 | AssertRC(rc2);
|
---|
10007 | }
|
---|
10008 |
|
---|
10009 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
10010 | return rc;
|
---|
10011 | }
|
---|
10012 |
|
---|
10013 | /**
|
---|
10014 | * Get version of image in HDD container.
|
---|
10015 | *
|
---|
10016 | * @returns VBox status code.
|
---|
10017 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
10018 | * @param pDisk Pointer to HDD container.
|
---|
10019 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
10020 | * @param puVersion Where to store the image version.
|
---|
10021 | */
|
---|
10022 | VBOXDDU_DECL(int) VDGetVersion(PVBOXHDD pDisk, unsigned nImage,
|
---|
10023 | unsigned *puVersion)
|
---|
10024 | {
|
---|
10025 | int rc = VINF_SUCCESS;
|
---|
10026 | int rc2;
|
---|
10027 | bool fLockRead = false;
|
---|
10028 |
|
---|
10029 | LogFlowFunc(("pDisk=%#p nImage=%u puVersion=%#p\n",
|
---|
10030 | pDisk, nImage, puVersion));
|
---|
10031 | do
|
---|
10032 | {
|
---|
10033 | /* sanity check */
|
---|
10034 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10035 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10036 |
|
---|
10037 | /* Check arguments. */
|
---|
10038 | AssertMsgBreakStmt(VALID_PTR(puVersion),
|
---|
10039 | ("puVersion=%#p\n", puVersion),
|
---|
10040 | rc = VERR_INVALID_PARAMETER);
|
---|
10041 |
|
---|
10042 | rc2 = vdThreadStartRead(pDisk);
|
---|
10043 | AssertRC(rc2);
|
---|
10044 | fLockRead = true;
|
---|
10045 |
|
---|
10046 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
10047 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
10048 |
|
---|
10049 | *puVersion = pImage->Backend->pfnGetVersion(pImage->pBackendData);
|
---|
10050 | } while (0);
|
---|
10051 |
|
---|
10052 | if (RT_UNLIKELY(fLockRead))
|
---|
10053 | {
|
---|
10054 | rc2 = vdThreadFinishRead(pDisk);
|
---|
10055 | AssertRC(rc2);
|
---|
10056 | }
|
---|
10057 |
|
---|
10058 | LogFlowFunc(("returns %Rrc uVersion=%#x\n", rc, *puVersion));
|
---|
10059 | return rc;
|
---|
10060 | }
|
---|
10061 |
|
---|
10062 | /**
|
---|
10063 | * List the capabilities of image backend in HDD container.
|
---|
10064 | *
|
---|
10065 | * @returns VBox status code.
|
---|
10066 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
10067 | * @param pDisk Pointer to the HDD container.
|
---|
10068 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
10069 | * @param pbackendInfo Where to store the backend information.
|
---|
10070 | */
|
---|
10071 | VBOXDDU_DECL(int) VDBackendInfoSingle(PVBOXHDD pDisk, unsigned nImage,
|
---|
10072 | PVDBACKENDINFO pBackendInfo)
|
---|
10073 | {
|
---|
10074 | int rc = VINF_SUCCESS;
|
---|
10075 | int rc2;
|
---|
10076 | bool fLockRead = false;
|
---|
10077 |
|
---|
10078 | LogFlowFunc(("pDisk=%#p nImage=%u pBackendInfo=%#p\n",
|
---|
10079 | pDisk, nImage, pBackendInfo));
|
---|
10080 | do
|
---|
10081 | {
|
---|
10082 | /* sanity check */
|
---|
10083 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10084 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10085 |
|
---|
10086 | /* Check arguments. */
|
---|
10087 | AssertMsgBreakStmt(VALID_PTR(pBackendInfo),
|
---|
10088 | ("pBackendInfo=%#p\n", pBackendInfo),
|
---|
10089 | rc = VERR_INVALID_PARAMETER);
|
---|
10090 |
|
---|
10091 | rc2 = vdThreadStartRead(pDisk);
|
---|
10092 | AssertRC(rc2);
|
---|
10093 | fLockRead = true;
|
---|
10094 |
|
---|
10095 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
10096 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
10097 |
|
---|
10098 | pBackendInfo->pszBackend = pImage->Backend->pszBackendName;
|
---|
10099 | pBackendInfo->uBackendCaps = pImage->Backend->uBackendCaps;
|
---|
10100 | pBackendInfo->paFileExtensions = pImage->Backend->paFileExtensions;
|
---|
10101 | pBackendInfo->paConfigInfo = pImage->Backend->paConfigInfo;
|
---|
10102 | } while (0);
|
---|
10103 |
|
---|
10104 | if (RT_UNLIKELY(fLockRead))
|
---|
10105 | {
|
---|
10106 | rc2 = vdThreadFinishRead(pDisk);
|
---|
10107 | AssertRC(rc2);
|
---|
10108 | }
|
---|
10109 |
|
---|
10110 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
10111 | return rc;
|
---|
10112 | }
|
---|
10113 |
|
---|
10114 | /**
|
---|
10115 | * Get flags of image in HDD container.
|
---|
10116 | *
|
---|
10117 | * @returns VBox status code.
|
---|
10118 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
10119 | * @param pDisk Pointer to HDD container.
|
---|
10120 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
10121 | * @param puImageFlags Where to store the image flags.
|
---|
10122 | */
|
---|
10123 | VBOXDDU_DECL(int) VDGetImageFlags(PVBOXHDD pDisk, unsigned nImage,
|
---|
10124 | unsigned *puImageFlags)
|
---|
10125 | {
|
---|
10126 | int rc = VINF_SUCCESS;
|
---|
10127 | int rc2;
|
---|
10128 | bool fLockRead = false;
|
---|
10129 |
|
---|
10130 | LogFlowFunc(("pDisk=%#p nImage=%u puImageFlags=%#p\n",
|
---|
10131 | pDisk, nImage, puImageFlags));
|
---|
10132 | do
|
---|
10133 | {
|
---|
10134 | /* sanity check */
|
---|
10135 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10136 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10137 |
|
---|
10138 | /* Check arguments. */
|
---|
10139 | AssertMsgBreakStmt(VALID_PTR(puImageFlags),
|
---|
10140 | ("puImageFlags=%#p\n", puImageFlags),
|
---|
10141 | rc = VERR_INVALID_PARAMETER);
|
---|
10142 |
|
---|
10143 | rc2 = vdThreadStartRead(pDisk);
|
---|
10144 | AssertRC(rc2);
|
---|
10145 | fLockRead = true;
|
---|
10146 |
|
---|
10147 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
10148 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
10149 |
|
---|
10150 | *puImageFlags = pImage->uImageFlags;
|
---|
10151 | } while (0);
|
---|
10152 |
|
---|
10153 | if (RT_UNLIKELY(fLockRead))
|
---|
10154 | {
|
---|
10155 | rc2 = vdThreadFinishRead(pDisk);
|
---|
10156 | AssertRC(rc2);
|
---|
10157 | }
|
---|
10158 |
|
---|
10159 | LogFlowFunc(("returns %Rrc uImageFlags=%#x\n", rc, *puImageFlags));
|
---|
10160 | return rc;
|
---|
10161 | }
|
---|
10162 |
|
---|
10163 | /**
|
---|
10164 | * Get open flags of image in HDD container.
|
---|
10165 | *
|
---|
10166 | * @returns VBox status code.
|
---|
10167 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
10168 | * @param pDisk Pointer to HDD container.
|
---|
10169 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
10170 | * @param puOpenFlags Where to store the image open flags.
|
---|
10171 | */
|
---|
10172 | VBOXDDU_DECL(int) VDGetOpenFlags(PVBOXHDD pDisk, unsigned nImage,
|
---|
10173 | unsigned *puOpenFlags)
|
---|
10174 | {
|
---|
10175 | int rc = VINF_SUCCESS;
|
---|
10176 | int rc2;
|
---|
10177 | bool fLockRead = false;
|
---|
10178 |
|
---|
10179 | LogFlowFunc(("pDisk=%#p nImage=%u puOpenFlags=%#p\n",
|
---|
10180 | pDisk, nImage, puOpenFlags));
|
---|
10181 | do
|
---|
10182 | {
|
---|
10183 | /* sanity check */
|
---|
10184 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10185 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10186 |
|
---|
10187 | /* Check arguments. */
|
---|
10188 | AssertMsgBreakStmt(VALID_PTR(puOpenFlags),
|
---|
10189 | ("puOpenFlags=%#p\n", puOpenFlags),
|
---|
10190 | rc = VERR_INVALID_PARAMETER);
|
---|
10191 |
|
---|
10192 | rc2 = vdThreadStartRead(pDisk);
|
---|
10193 | AssertRC(rc2);
|
---|
10194 | fLockRead = true;
|
---|
10195 |
|
---|
10196 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
10197 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
10198 |
|
---|
10199 | *puOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
|
---|
10200 | } while (0);
|
---|
10201 |
|
---|
10202 | if (RT_UNLIKELY(fLockRead))
|
---|
10203 | {
|
---|
10204 | rc2 = vdThreadFinishRead(pDisk);
|
---|
10205 | AssertRC(rc2);
|
---|
10206 | }
|
---|
10207 |
|
---|
10208 | LogFlowFunc(("returns %Rrc uOpenFlags=%#x\n", rc, *puOpenFlags));
|
---|
10209 | return rc;
|
---|
10210 | }
|
---|
10211 |
|
---|
10212 | /**
|
---|
10213 | * Set open flags of image in HDD container.
|
---|
10214 | * This operation may cause file locking changes and/or files being reopened.
|
---|
10215 | * Note that in case of unrecoverable error all images in HDD container will be closed.
|
---|
10216 | *
|
---|
10217 | * @returns VBox status code.
|
---|
10218 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
10219 | * @param pDisk Pointer to HDD container.
|
---|
10220 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
10221 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
10222 | */
|
---|
10223 | VBOXDDU_DECL(int) VDSetOpenFlags(PVBOXHDD pDisk, unsigned nImage,
|
---|
10224 | unsigned uOpenFlags)
|
---|
10225 | {
|
---|
10226 | int rc;
|
---|
10227 | int rc2;
|
---|
10228 | bool fLockWrite = false;
|
---|
10229 |
|
---|
10230 | LogFlowFunc(("pDisk=%#p uOpenFlags=%#u\n", pDisk, uOpenFlags));
|
---|
10231 | do
|
---|
10232 | {
|
---|
10233 | /* sanity check */
|
---|
10234 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10235 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10236 |
|
---|
10237 | /* Check arguments. */
|
---|
10238 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
10239 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
10240 | rc = VERR_INVALID_PARAMETER);
|
---|
10241 |
|
---|
10242 | rc2 = vdThreadStartWrite(pDisk);
|
---|
10243 | AssertRC(rc2);
|
---|
10244 | fLockWrite = true;
|
---|
10245 |
|
---|
10246 | /* Destroy any discard state because the image might be changed to readonly mode. */
|
---|
10247 | rc = vdDiscardStateDestroy(pDisk);
|
---|
10248 | if (RT_FAILURE(rc))
|
---|
10249 | break;
|
---|
10250 |
|
---|
10251 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
10252 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
10253 |
|
---|
10254 | rc = pImage->Backend->pfnSetOpenFlags(pImage->pBackendData,
|
---|
10255 | uOpenFlags & ~(VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_IGNORE_FLUSH | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS));
|
---|
10256 | if (RT_SUCCESS(rc))
|
---|
10257 | pImage->uOpenFlags = uOpenFlags & (VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_DISCARD | VD_OPEN_FLAGS_IGNORE_FLUSH | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS);
|
---|
10258 | } while (0);
|
---|
10259 |
|
---|
10260 | if (RT_UNLIKELY(fLockWrite))
|
---|
10261 | {
|
---|
10262 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
10263 | AssertRC(rc2);
|
---|
10264 | }
|
---|
10265 |
|
---|
10266 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
10267 | return rc;
|
---|
10268 | }
|
---|
10269 |
|
---|
10270 | /**
|
---|
10271 | * Get base filename of image in HDD container. Some image formats use
|
---|
10272 | * other filenames as well, so don't use this for anything but informational
|
---|
10273 | * purposes.
|
---|
10274 | *
|
---|
10275 | * @returns VBox status code.
|
---|
10276 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
10277 | * @returns VERR_BUFFER_OVERFLOW if pszFilename buffer too small to hold filename.
|
---|
10278 | * @param pDisk Pointer to HDD container.
|
---|
10279 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
10280 | * @param pszFilename Where to store the image file name.
|
---|
10281 | * @param cbFilename Size of buffer pszFilename points to.
|
---|
10282 | */
|
---|
10283 | VBOXDDU_DECL(int) VDGetFilename(PVBOXHDD pDisk, unsigned nImage,
|
---|
10284 | char *pszFilename, unsigned cbFilename)
|
---|
10285 | {
|
---|
10286 | int rc;
|
---|
10287 | int rc2;
|
---|
10288 | bool fLockRead = false;
|
---|
10289 |
|
---|
10290 | LogFlowFunc(("pDisk=%#p nImage=%u pszFilename=%#p cbFilename=%u\n",
|
---|
10291 | pDisk, nImage, pszFilename, cbFilename));
|
---|
10292 | do
|
---|
10293 | {
|
---|
10294 | /* sanity check */
|
---|
10295 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10296 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10297 |
|
---|
10298 | /* Check arguments. */
|
---|
10299 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
10300 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
10301 | rc = VERR_INVALID_PARAMETER);
|
---|
10302 | AssertMsgBreakStmt(cbFilename,
|
---|
10303 | ("cbFilename=%u\n", cbFilename),
|
---|
10304 | rc = VERR_INVALID_PARAMETER);
|
---|
10305 |
|
---|
10306 | rc2 = vdThreadStartRead(pDisk);
|
---|
10307 | AssertRC(rc2);
|
---|
10308 | fLockRead = true;
|
---|
10309 |
|
---|
10310 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
10311 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
10312 |
|
---|
10313 | size_t cb = strlen(pImage->pszFilename);
|
---|
10314 | if (cb <= cbFilename)
|
---|
10315 | {
|
---|
10316 | strcpy(pszFilename, pImage->pszFilename);
|
---|
10317 | rc = VINF_SUCCESS;
|
---|
10318 | }
|
---|
10319 | else
|
---|
10320 | {
|
---|
10321 | strncpy(pszFilename, pImage->pszFilename, cbFilename - 1);
|
---|
10322 | pszFilename[cbFilename - 1] = '\0';
|
---|
10323 | rc = VERR_BUFFER_OVERFLOW;
|
---|
10324 | }
|
---|
10325 | } while (0);
|
---|
10326 |
|
---|
10327 | if (RT_UNLIKELY(fLockRead))
|
---|
10328 | {
|
---|
10329 | rc2 = vdThreadFinishRead(pDisk);
|
---|
10330 | AssertRC(rc2);
|
---|
10331 | }
|
---|
10332 |
|
---|
10333 | LogFlowFunc(("returns %Rrc, pszFilename=\"%s\"\n", rc, pszFilename));
|
---|
10334 | return rc;
|
---|
10335 | }
|
---|
10336 |
|
---|
10337 | /**
|
---|
10338 | * Get the comment line of image in HDD container.
|
---|
10339 | *
|
---|
10340 | * @returns VBox status code.
|
---|
10341 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
10342 | * @returns VERR_BUFFER_OVERFLOW if pszComment buffer too small to hold comment text.
|
---|
10343 | * @param pDisk Pointer to HDD container.
|
---|
10344 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
10345 | * @param pszComment Where to store the comment string of image. NULL is ok.
|
---|
10346 | * @param cbComment The size of pszComment buffer. 0 is ok.
|
---|
10347 | */
|
---|
10348 | VBOXDDU_DECL(int) VDGetComment(PVBOXHDD pDisk, unsigned nImage,
|
---|
10349 | char *pszComment, unsigned cbComment)
|
---|
10350 | {
|
---|
10351 | int rc;
|
---|
10352 | int rc2;
|
---|
10353 | bool fLockRead = false;
|
---|
10354 |
|
---|
10355 | LogFlowFunc(("pDisk=%#p nImage=%u pszComment=%#p cbComment=%u\n",
|
---|
10356 | pDisk, nImage, pszComment, cbComment));
|
---|
10357 | do
|
---|
10358 | {
|
---|
10359 | /* sanity check */
|
---|
10360 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10361 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10362 |
|
---|
10363 | /* Check arguments. */
|
---|
10364 | AssertMsgBreakStmt(VALID_PTR(pszComment),
|
---|
10365 | ("pszComment=%#p \"%s\"\n", pszComment, pszComment),
|
---|
10366 | rc = VERR_INVALID_PARAMETER);
|
---|
10367 | AssertMsgBreakStmt(cbComment,
|
---|
10368 | ("cbComment=%u\n", cbComment),
|
---|
10369 | rc = VERR_INVALID_PARAMETER);
|
---|
10370 |
|
---|
10371 | rc2 = vdThreadStartRead(pDisk);
|
---|
10372 | AssertRC(rc2);
|
---|
10373 | fLockRead = true;
|
---|
10374 |
|
---|
10375 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
10376 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
10377 |
|
---|
10378 | rc = pImage->Backend->pfnGetComment(pImage->pBackendData, pszComment,
|
---|
10379 | cbComment);
|
---|
10380 | } while (0);
|
---|
10381 |
|
---|
10382 | if (RT_UNLIKELY(fLockRead))
|
---|
10383 | {
|
---|
10384 | rc2 = vdThreadFinishRead(pDisk);
|
---|
10385 | AssertRC(rc2);
|
---|
10386 | }
|
---|
10387 |
|
---|
10388 | LogFlowFunc(("returns %Rrc, pszComment=\"%s\"\n", rc, pszComment));
|
---|
10389 | return rc;
|
---|
10390 | }
|
---|
10391 |
|
---|
10392 | /**
|
---|
10393 | * Changes the comment line of image in HDD container.
|
---|
10394 | *
|
---|
10395 | * @returns VBox status code.
|
---|
10396 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
10397 | * @param pDisk Pointer to HDD container.
|
---|
10398 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
10399 | * @param pszComment New comment string (UTF-8). NULL is allowed to reset the comment.
|
---|
10400 | */
|
---|
10401 | VBOXDDU_DECL(int) VDSetComment(PVBOXHDD pDisk, unsigned nImage,
|
---|
10402 | const char *pszComment)
|
---|
10403 | {
|
---|
10404 | int rc;
|
---|
10405 | int rc2;
|
---|
10406 | bool fLockWrite = false;
|
---|
10407 |
|
---|
10408 | LogFlowFunc(("pDisk=%#p nImage=%u pszComment=%#p \"%s\"\n",
|
---|
10409 | pDisk, nImage, pszComment, pszComment));
|
---|
10410 | do
|
---|
10411 | {
|
---|
10412 | /* sanity check */
|
---|
10413 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10414 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10415 |
|
---|
10416 | /* Check arguments. */
|
---|
10417 | AssertMsgBreakStmt(VALID_PTR(pszComment) || pszComment == NULL,
|
---|
10418 | ("pszComment=%#p \"%s\"\n", pszComment, pszComment),
|
---|
10419 | rc = VERR_INVALID_PARAMETER);
|
---|
10420 |
|
---|
10421 | rc2 = vdThreadStartWrite(pDisk);
|
---|
10422 | AssertRC(rc2);
|
---|
10423 | fLockWrite = true;
|
---|
10424 |
|
---|
10425 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
10426 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
10427 |
|
---|
10428 | rc = pImage->Backend->pfnSetComment(pImage->pBackendData, pszComment);
|
---|
10429 | } while (0);
|
---|
10430 |
|
---|
10431 | if (RT_UNLIKELY(fLockWrite))
|
---|
10432 | {
|
---|
10433 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
10434 | AssertRC(rc2);
|
---|
10435 | }
|
---|
10436 |
|
---|
10437 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
10438 | return rc;
|
---|
10439 | }
|
---|
10440 |
|
---|
10441 |
|
---|
10442 | /**
|
---|
10443 | * Get UUID of image in HDD container.
|
---|
10444 | *
|
---|
10445 | * @returns VBox status code.
|
---|
10446 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
10447 | * @param pDisk Pointer to HDD container.
|
---|
10448 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
10449 | * @param pUuid Where to store the image creation UUID.
|
---|
10450 | */
|
---|
10451 | VBOXDDU_DECL(int) VDGetUuid(PVBOXHDD pDisk, unsigned nImage, PRTUUID pUuid)
|
---|
10452 | {
|
---|
10453 | int rc;
|
---|
10454 | int rc2;
|
---|
10455 | bool fLockRead = false;
|
---|
10456 |
|
---|
10457 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
|
---|
10458 | do
|
---|
10459 | {
|
---|
10460 | /* sanity check */
|
---|
10461 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10462 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10463 |
|
---|
10464 | /* Check arguments. */
|
---|
10465 | AssertMsgBreakStmt(VALID_PTR(pUuid),
|
---|
10466 | ("pUuid=%#p\n", pUuid),
|
---|
10467 | rc = VERR_INVALID_PARAMETER);
|
---|
10468 |
|
---|
10469 | rc2 = vdThreadStartRead(pDisk);
|
---|
10470 | AssertRC(rc2);
|
---|
10471 | fLockRead = true;
|
---|
10472 |
|
---|
10473 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
10474 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
10475 |
|
---|
10476 | rc = pImage->Backend->pfnGetUuid(pImage->pBackendData, pUuid);
|
---|
10477 | } while (0);
|
---|
10478 |
|
---|
10479 | if (RT_UNLIKELY(fLockRead))
|
---|
10480 | {
|
---|
10481 | rc2 = vdThreadFinishRead(pDisk);
|
---|
10482 | AssertRC(rc2);
|
---|
10483 | }
|
---|
10484 |
|
---|
10485 | LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
|
---|
10486 | return rc;
|
---|
10487 | }
|
---|
10488 |
|
---|
10489 | /**
|
---|
10490 | * Set the image's UUID. Should not be used by normal applications.
|
---|
10491 | *
|
---|
10492 | * @returns VBox status code.
|
---|
10493 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
10494 | * @param pDisk Pointer to HDD container.
|
---|
10495 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
10496 | * @param pUuid New UUID of the image. If NULL, a new UUID is created.
|
---|
10497 | */
|
---|
10498 | VBOXDDU_DECL(int) VDSetUuid(PVBOXHDD pDisk, unsigned nImage, PCRTUUID pUuid)
|
---|
10499 | {
|
---|
10500 | int rc;
|
---|
10501 | int rc2;
|
---|
10502 | bool fLockWrite = false;
|
---|
10503 |
|
---|
10504 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
|
---|
10505 | pDisk, nImage, pUuid, pUuid));
|
---|
10506 | do
|
---|
10507 | {
|
---|
10508 | /* sanity check */
|
---|
10509 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10510 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10511 |
|
---|
10512 | AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
|
---|
10513 | ("pUuid=%#p\n", pUuid),
|
---|
10514 | rc = VERR_INVALID_PARAMETER);
|
---|
10515 |
|
---|
10516 | rc2 = vdThreadStartWrite(pDisk);
|
---|
10517 | AssertRC(rc2);
|
---|
10518 | fLockWrite = true;
|
---|
10519 |
|
---|
10520 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
10521 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
10522 |
|
---|
10523 | RTUUID Uuid;
|
---|
10524 | if (!pUuid)
|
---|
10525 | {
|
---|
10526 | RTUuidCreate(&Uuid);
|
---|
10527 | pUuid = &Uuid;
|
---|
10528 | }
|
---|
10529 | rc = pImage->Backend->pfnSetUuid(pImage->pBackendData, pUuid);
|
---|
10530 | } while (0);
|
---|
10531 |
|
---|
10532 | if (RT_UNLIKELY(fLockWrite))
|
---|
10533 | {
|
---|
10534 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
10535 | AssertRC(rc2);
|
---|
10536 | }
|
---|
10537 |
|
---|
10538 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
10539 | return rc;
|
---|
10540 | }
|
---|
10541 |
|
---|
10542 | /**
|
---|
10543 | * Get last modification UUID of image in HDD container.
|
---|
10544 | *
|
---|
10545 | * @returns VBox status code.
|
---|
10546 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
10547 | * @param pDisk Pointer to HDD container.
|
---|
10548 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
10549 | * @param pUuid Where to store the image modification UUID.
|
---|
10550 | */
|
---|
10551 | VBOXDDU_DECL(int) VDGetModificationUuid(PVBOXHDD pDisk, unsigned nImage, PRTUUID pUuid)
|
---|
10552 | {
|
---|
10553 | int rc = VINF_SUCCESS;
|
---|
10554 | int rc2;
|
---|
10555 | bool fLockRead = false;
|
---|
10556 |
|
---|
10557 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
|
---|
10558 | do
|
---|
10559 | {
|
---|
10560 | /* sanity check */
|
---|
10561 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10562 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10563 |
|
---|
10564 | /* Check arguments. */
|
---|
10565 | AssertMsgBreakStmt(VALID_PTR(pUuid),
|
---|
10566 | ("pUuid=%#p\n", pUuid),
|
---|
10567 | rc = VERR_INVALID_PARAMETER);
|
---|
10568 |
|
---|
10569 | rc2 = vdThreadStartRead(pDisk);
|
---|
10570 | AssertRC(rc2);
|
---|
10571 | fLockRead = true;
|
---|
10572 |
|
---|
10573 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
10574 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
10575 |
|
---|
10576 | rc = pImage->Backend->pfnGetModificationUuid(pImage->pBackendData,
|
---|
10577 | pUuid);
|
---|
10578 | } while (0);
|
---|
10579 |
|
---|
10580 | if (RT_UNLIKELY(fLockRead))
|
---|
10581 | {
|
---|
10582 | rc2 = vdThreadFinishRead(pDisk);
|
---|
10583 | AssertRC(rc2);
|
---|
10584 | }
|
---|
10585 |
|
---|
10586 | LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
|
---|
10587 | return rc;
|
---|
10588 | }
|
---|
10589 |
|
---|
10590 | /**
|
---|
10591 | * Set the image's last modification UUID. Should not be used by normal applications.
|
---|
10592 | *
|
---|
10593 | * @returns VBox status code.
|
---|
10594 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
10595 | * @param pDisk Pointer to HDD container.
|
---|
10596 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
10597 | * @param pUuid New modification UUID of the image. If NULL, a new UUID is created.
|
---|
10598 | */
|
---|
10599 | VBOXDDU_DECL(int) VDSetModificationUuid(PVBOXHDD pDisk, unsigned nImage, PCRTUUID pUuid)
|
---|
10600 | {
|
---|
10601 | int rc;
|
---|
10602 | int rc2;
|
---|
10603 | bool fLockWrite = false;
|
---|
10604 |
|
---|
10605 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
|
---|
10606 | pDisk, nImage, pUuid, pUuid));
|
---|
10607 | do
|
---|
10608 | {
|
---|
10609 | /* sanity check */
|
---|
10610 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10611 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10612 |
|
---|
10613 | /* Check arguments. */
|
---|
10614 | AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
|
---|
10615 | ("pUuid=%#p\n", pUuid),
|
---|
10616 | rc = VERR_INVALID_PARAMETER);
|
---|
10617 |
|
---|
10618 | rc2 = vdThreadStartWrite(pDisk);
|
---|
10619 | AssertRC(rc2);
|
---|
10620 | fLockWrite = true;
|
---|
10621 |
|
---|
10622 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
10623 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
10624 |
|
---|
10625 | RTUUID Uuid;
|
---|
10626 | if (!pUuid)
|
---|
10627 | {
|
---|
10628 | RTUuidCreate(&Uuid);
|
---|
10629 | pUuid = &Uuid;
|
---|
10630 | }
|
---|
10631 | rc = pImage->Backend->pfnSetModificationUuid(pImage->pBackendData,
|
---|
10632 | pUuid);
|
---|
10633 | } while (0);
|
---|
10634 |
|
---|
10635 | if (RT_UNLIKELY(fLockWrite))
|
---|
10636 | {
|
---|
10637 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
10638 | AssertRC(rc2);
|
---|
10639 | }
|
---|
10640 |
|
---|
10641 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
10642 | return rc;
|
---|
10643 | }
|
---|
10644 |
|
---|
10645 | /**
|
---|
10646 | * Get parent UUID of image in HDD container.
|
---|
10647 | *
|
---|
10648 | * @returns VBox status code.
|
---|
10649 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
10650 | * @param pDisk Pointer to HDD container.
|
---|
10651 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
10652 | * @param pUuid Where to store the parent image UUID.
|
---|
10653 | */
|
---|
10654 | VBOXDDU_DECL(int) VDGetParentUuid(PVBOXHDD pDisk, unsigned nImage,
|
---|
10655 | PRTUUID pUuid)
|
---|
10656 | {
|
---|
10657 | int rc = VINF_SUCCESS;
|
---|
10658 | int rc2;
|
---|
10659 | bool fLockRead = false;
|
---|
10660 |
|
---|
10661 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
|
---|
10662 | do
|
---|
10663 | {
|
---|
10664 | /* sanity check */
|
---|
10665 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10666 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10667 |
|
---|
10668 | /* Check arguments. */
|
---|
10669 | AssertMsgBreakStmt(VALID_PTR(pUuid),
|
---|
10670 | ("pUuid=%#p\n", pUuid),
|
---|
10671 | rc = VERR_INVALID_PARAMETER);
|
---|
10672 |
|
---|
10673 | rc2 = vdThreadStartRead(pDisk);
|
---|
10674 | AssertRC(rc2);
|
---|
10675 | fLockRead = true;
|
---|
10676 |
|
---|
10677 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
10678 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
10679 |
|
---|
10680 | rc = pImage->Backend->pfnGetParentUuid(pImage->pBackendData, pUuid);
|
---|
10681 | } while (0);
|
---|
10682 |
|
---|
10683 | if (RT_UNLIKELY(fLockRead))
|
---|
10684 | {
|
---|
10685 | rc2 = vdThreadFinishRead(pDisk);
|
---|
10686 | AssertRC(rc2);
|
---|
10687 | }
|
---|
10688 |
|
---|
10689 | LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
|
---|
10690 | return rc;
|
---|
10691 | }
|
---|
10692 |
|
---|
10693 | /**
|
---|
10694 | * Set the image's parent UUID. Should not be used by normal applications.
|
---|
10695 | *
|
---|
10696 | * @returns VBox status code.
|
---|
10697 | * @param pDisk Pointer to HDD container.
|
---|
10698 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
10699 | * @param pUuid New parent UUID of the image. If NULL, a new UUID is created.
|
---|
10700 | */
|
---|
10701 | VBOXDDU_DECL(int) VDSetParentUuid(PVBOXHDD pDisk, unsigned nImage,
|
---|
10702 | PCRTUUID pUuid)
|
---|
10703 | {
|
---|
10704 | int rc;
|
---|
10705 | int rc2;
|
---|
10706 | bool fLockWrite = false;
|
---|
10707 |
|
---|
10708 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
|
---|
10709 | pDisk, nImage, pUuid, pUuid));
|
---|
10710 | do
|
---|
10711 | {
|
---|
10712 | /* sanity check */
|
---|
10713 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10714 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10715 |
|
---|
10716 | /* Check arguments. */
|
---|
10717 | AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
|
---|
10718 | ("pUuid=%#p\n", pUuid),
|
---|
10719 | rc = VERR_INVALID_PARAMETER);
|
---|
10720 |
|
---|
10721 | rc2 = vdThreadStartWrite(pDisk);
|
---|
10722 | AssertRC(rc2);
|
---|
10723 | fLockWrite = true;
|
---|
10724 |
|
---|
10725 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
10726 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
10727 |
|
---|
10728 | RTUUID Uuid;
|
---|
10729 | if (!pUuid)
|
---|
10730 | {
|
---|
10731 | RTUuidCreate(&Uuid);
|
---|
10732 | pUuid = &Uuid;
|
---|
10733 | }
|
---|
10734 | rc = pImage->Backend->pfnSetParentUuid(pImage->pBackendData, pUuid);
|
---|
10735 | } while (0);
|
---|
10736 |
|
---|
10737 | if (RT_UNLIKELY(fLockWrite))
|
---|
10738 | {
|
---|
10739 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
10740 | AssertRC(rc2);
|
---|
10741 | }
|
---|
10742 |
|
---|
10743 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
10744 | return rc;
|
---|
10745 | }
|
---|
10746 |
|
---|
10747 |
|
---|
10748 | /**
|
---|
10749 | * Debug helper - dumps all opened images in HDD container into the log file.
|
---|
10750 | *
|
---|
10751 | * @param pDisk Pointer to HDD container.
|
---|
10752 | */
|
---|
10753 | VBOXDDU_DECL(void) VDDumpImages(PVBOXHDD pDisk)
|
---|
10754 | {
|
---|
10755 | int rc2;
|
---|
10756 | bool fLockRead = false;
|
---|
10757 |
|
---|
10758 | do
|
---|
10759 | {
|
---|
10760 | /* sanity check */
|
---|
10761 | AssertPtrBreak(pDisk);
|
---|
10762 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10763 |
|
---|
10764 | if (!pDisk->pInterfaceError || !VALID_PTR(pDisk->pInterfaceError->pfnMessage))
|
---|
10765 | pDisk->pInterfaceError->pfnMessage = vdLogMessage;
|
---|
10766 |
|
---|
10767 | rc2 = vdThreadStartRead(pDisk);
|
---|
10768 | AssertRC(rc2);
|
---|
10769 | fLockRead = true;
|
---|
10770 |
|
---|
10771 | vdMessageWrapper(pDisk, "--- Dumping VD Disk, Images=%u\n", pDisk->cImages);
|
---|
10772 | for (PVDIMAGE pImage = pDisk->pBase; pImage; pImage = pImage->pNext)
|
---|
10773 | {
|
---|
10774 | vdMessageWrapper(pDisk, "Dumping VD image \"%s\" (Backend=%s)\n",
|
---|
10775 | pImage->pszFilename, pImage->Backend->pszBackendName);
|
---|
10776 | pImage->Backend->pfnDump(pImage->pBackendData);
|
---|
10777 | }
|
---|
10778 | } while (0);
|
---|
10779 |
|
---|
10780 | if (RT_UNLIKELY(fLockRead))
|
---|
10781 | {
|
---|
10782 | rc2 = vdThreadFinishRead(pDisk);
|
---|
10783 | AssertRC(rc2);
|
---|
10784 | }
|
---|
10785 | }
|
---|
10786 |
|
---|
10787 |
|
---|
10788 | VBOXDDU_DECL(int) VDDiscardRanges(PVBOXHDD pDisk, PCRTRANGE paRanges, unsigned cRanges)
|
---|
10789 | {
|
---|
10790 | int rc;
|
---|
10791 | int rc2;
|
---|
10792 | bool fLockWrite = false;
|
---|
10793 |
|
---|
10794 | LogFlowFunc(("pDisk=%#p paRanges=%#p cRanges=%u\n",
|
---|
10795 | pDisk, paRanges, cRanges));
|
---|
10796 | do
|
---|
10797 | {
|
---|
10798 | /* sanity check */
|
---|
10799 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10800 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10801 |
|
---|
10802 | /* Check arguments. */
|
---|
10803 | AssertMsgBreakStmt(cRanges,
|
---|
10804 | ("cRanges=%u\n", cRanges),
|
---|
10805 | rc = VERR_INVALID_PARAMETER);
|
---|
10806 | AssertMsgBreakStmt(VALID_PTR(paRanges),
|
---|
10807 | ("paRanges=%#p\n", paRanges),
|
---|
10808 | rc = VERR_INVALID_PARAMETER);
|
---|
10809 |
|
---|
10810 | rc2 = vdThreadStartWrite(pDisk);
|
---|
10811 | AssertRC(rc2);
|
---|
10812 | fLockWrite = true;
|
---|
10813 |
|
---|
10814 | AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
|
---|
10815 |
|
---|
10816 | AssertMsgBreakStmt(pDisk->pLast->uOpenFlags & VD_OPEN_FLAGS_DISCARD,
|
---|
10817 | ("Discarding not supported\n"),
|
---|
10818 | rc = VERR_NOT_SUPPORTED);
|
---|
10819 |
|
---|
10820 | VDIOCTX IoCtx;
|
---|
10821 | RTSEMEVENT hEventComplete = NIL_RTSEMEVENT;
|
---|
10822 |
|
---|
10823 | rc = RTSemEventCreate(&hEventComplete);
|
---|
10824 | if (RT_FAILURE(rc))
|
---|
10825 | break;
|
---|
10826 |
|
---|
10827 | vdIoCtxDiscardInit(&IoCtx, pDisk, paRanges, cRanges,
|
---|
10828 | vdIoCtxSyncComplete, pDisk, hEventComplete, NULL,
|
---|
10829 | vdDiscardHelperAsync, VDIOCTX_FLAGS_SYNC | VDIOCTX_FLAGS_DONT_FREE);
|
---|
10830 | rc = vdIoCtxProcessSync(&IoCtx, hEventComplete);
|
---|
10831 |
|
---|
10832 | RTSemEventDestroy(hEventComplete);
|
---|
10833 | } while (0);
|
---|
10834 |
|
---|
10835 | if (RT_UNLIKELY(fLockWrite))
|
---|
10836 | {
|
---|
10837 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
10838 | AssertRC(rc2);
|
---|
10839 | }
|
---|
10840 |
|
---|
10841 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
10842 | return rc;
|
---|
10843 | }
|
---|
10844 |
|
---|
10845 |
|
---|
10846 | VBOXDDU_DECL(int) VDAsyncRead(PVBOXHDD pDisk, uint64_t uOffset, size_t cbRead,
|
---|
10847 | PCRTSGBUF pcSgBuf,
|
---|
10848 | PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
10849 | void *pvUser1, void *pvUser2)
|
---|
10850 | {
|
---|
10851 | int rc = VERR_VD_BLOCK_FREE;
|
---|
10852 | int rc2;
|
---|
10853 | bool fLockRead = false;
|
---|
10854 | PVDIOCTX pIoCtx = NULL;
|
---|
10855 |
|
---|
10856 | LogFlowFunc(("pDisk=%#p uOffset=%llu pcSgBuf=%#p cbRead=%zu pvUser1=%#p pvUser2=%#p\n",
|
---|
10857 | pDisk, uOffset, pcSgBuf, cbRead, pvUser1, pvUser2));
|
---|
10858 |
|
---|
10859 | do
|
---|
10860 | {
|
---|
10861 | /* sanity check */
|
---|
10862 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10863 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10864 |
|
---|
10865 | /* Check arguments. */
|
---|
10866 | AssertMsgBreakStmt(cbRead,
|
---|
10867 | ("cbRead=%zu\n", cbRead),
|
---|
10868 | rc = VERR_INVALID_PARAMETER);
|
---|
10869 | AssertMsgBreakStmt(VALID_PTR(pcSgBuf),
|
---|
10870 | ("pcSgBuf=%#p\n", pcSgBuf),
|
---|
10871 | rc = VERR_INVALID_PARAMETER);
|
---|
10872 |
|
---|
10873 | rc2 = vdThreadStartRead(pDisk);
|
---|
10874 | AssertRC(rc2);
|
---|
10875 | fLockRead = true;
|
---|
10876 |
|
---|
10877 | AssertMsgBreakStmt(uOffset + cbRead <= pDisk->cbSize,
|
---|
10878 | ("uOffset=%llu cbRead=%zu pDisk->cbSize=%llu\n",
|
---|
10879 | uOffset, cbRead, pDisk->cbSize),
|
---|
10880 | rc = VERR_INVALID_PARAMETER);
|
---|
10881 | AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
|
---|
10882 |
|
---|
10883 | pIoCtx = vdIoCtxRootAlloc(pDisk, VDIOCTXTXDIR_READ, uOffset,
|
---|
10884 | cbRead, pDisk->pLast, pcSgBuf,
|
---|
10885 | pfnComplete, pvUser1, pvUser2,
|
---|
10886 | NULL, vdReadHelperAsync,
|
---|
10887 | VDIOCTX_FLAGS_ZERO_FREE_BLOCKS);
|
---|
10888 | if (!pIoCtx)
|
---|
10889 | {
|
---|
10890 | rc = VERR_NO_MEMORY;
|
---|
10891 | break;
|
---|
10892 | }
|
---|
10893 |
|
---|
10894 | rc = vdIoCtxProcessTryLockDefer(pIoCtx);
|
---|
10895 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
10896 | {
|
---|
10897 | if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
|
---|
10898 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
10899 | else
|
---|
10900 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
|
---|
10901 | }
|
---|
10902 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
|
---|
10903 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
10904 |
|
---|
10905 | } while (0);
|
---|
10906 |
|
---|
10907 | if (RT_UNLIKELY(fLockRead) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
10908 | {
|
---|
10909 | rc2 = vdThreadFinishRead(pDisk);
|
---|
10910 | AssertRC(rc2);
|
---|
10911 | }
|
---|
10912 |
|
---|
10913 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
10914 | return rc;
|
---|
10915 | }
|
---|
10916 |
|
---|
10917 |
|
---|
10918 | VBOXDDU_DECL(int) VDAsyncWrite(PVBOXHDD pDisk, uint64_t uOffset, size_t cbWrite,
|
---|
10919 | PCRTSGBUF pcSgBuf,
|
---|
10920 | PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
10921 | void *pvUser1, void *pvUser2)
|
---|
10922 | {
|
---|
10923 | int rc;
|
---|
10924 | int rc2;
|
---|
10925 | bool fLockWrite = false;
|
---|
10926 | PVDIOCTX pIoCtx = NULL;
|
---|
10927 |
|
---|
10928 | LogFlowFunc(("pDisk=%#p uOffset=%llu cSgBuf=%#p cbWrite=%zu pvUser1=%#p pvUser2=%#p\n",
|
---|
10929 | pDisk, uOffset, pcSgBuf, cbWrite, pvUser1, pvUser2));
|
---|
10930 | do
|
---|
10931 | {
|
---|
10932 | /* sanity check */
|
---|
10933 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10934 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10935 |
|
---|
10936 | /* Check arguments. */
|
---|
10937 | AssertMsgBreakStmt(cbWrite,
|
---|
10938 | ("cbWrite=%zu\n", cbWrite),
|
---|
10939 | rc = VERR_INVALID_PARAMETER);
|
---|
10940 | AssertMsgBreakStmt(VALID_PTR(pcSgBuf),
|
---|
10941 | ("pcSgBuf=%#p\n", pcSgBuf),
|
---|
10942 | rc = VERR_INVALID_PARAMETER);
|
---|
10943 |
|
---|
10944 | rc2 = vdThreadStartWrite(pDisk);
|
---|
10945 | AssertRC(rc2);
|
---|
10946 | fLockWrite = true;
|
---|
10947 |
|
---|
10948 | AssertMsgBreakStmt(uOffset + cbWrite <= pDisk->cbSize,
|
---|
10949 | ("uOffset=%llu cbWrite=%zu pDisk->cbSize=%llu\n",
|
---|
10950 | uOffset, cbWrite, pDisk->cbSize),
|
---|
10951 | rc = VERR_INVALID_PARAMETER);
|
---|
10952 | AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
|
---|
10953 |
|
---|
10954 | pIoCtx = vdIoCtxRootAlloc(pDisk, VDIOCTXTXDIR_WRITE, uOffset,
|
---|
10955 | cbWrite, pDisk->pLast, pcSgBuf,
|
---|
10956 | pfnComplete, pvUser1, pvUser2,
|
---|
10957 | NULL, vdWriteHelperAsync,
|
---|
10958 | VDIOCTX_FLAGS_DEFAULT);
|
---|
10959 | if (!pIoCtx)
|
---|
10960 | {
|
---|
10961 | rc = VERR_NO_MEMORY;
|
---|
10962 | break;
|
---|
10963 | }
|
---|
10964 |
|
---|
10965 | rc = vdIoCtxProcessTryLockDefer(pIoCtx);
|
---|
10966 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
10967 | {
|
---|
10968 | if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
|
---|
10969 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
10970 | else
|
---|
10971 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
|
---|
10972 | }
|
---|
10973 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
|
---|
10974 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
10975 | } while (0);
|
---|
10976 |
|
---|
10977 | if (RT_UNLIKELY(fLockWrite) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
10978 | {
|
---|
10979 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
10980 | AssertRC(rc2);
|
---|
10981 | }
|
---|
10982 |
|
---|
10983 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
10984 | return rc;
|
---|
10985 | }
|
---|
10986 |
|
---|
10987 |
|
---|
10988 | VBOXDDU_DECL(int) VDAsyncFlush(PVBOXHDD pDisk, PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
10989 | void *pvUser1, void *pvUser2)
|
---|
10990 | {
|
---|
10991 | int rc;
|
---|
10992 | int rc2;
|
---|
10993 | bool fLockWrite = false;
|
---|
10994 | PVDIOCTX pIoCtx = NULL;
|
---|
10995 |
|
---|
10996 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
10997 |
|
---|
10998 | do
|
---|
10999 | {
|
---|
11000 | /* sanity check */
|
---|
11001 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
11002 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
11003 |
|
---|
11004 | rc2 = vdThreadStartWrite(pDisk);
|
---|
11005 | AssertRC(rc2);
|
---|
11006 | fLockWrite = true;
|
---|
11007 |
|
---|
11008 | AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
|
---|
11009 |
|
---|
11010 | pIoCtx = vdIoCtxRootAlloc(pDisk, VDIOCTXTXDIR_FLUSH, 0,
|
---|
11011 | 0, pDisk->pLast, NULL,
|
---|
11012 | pfnComplete, pvUser1, pvUser2,
|
---|
11013 | NULL, vdFlushHelperAsync,
|
---|
11014 | VDIOCTX_FLAGS_DEFAULT);
|
---|
11015 | if (!pIoCtx)
|
---|
11016 | {
|
---|
11017 | rc = VERR_NO_MEMORY;
|
---|
11018 | break;
|
---|
11019 | }
|
---|
11020 |
|
---|
11021 | rc = vdIoCtxProcessTryLockDefer(pIoCtx);
|
---|
11022 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
11023 | {
|
---|
11024 | if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
|
---|
11025 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
11026 | else
|
---|
11027 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
|
---|
11028 | }
|
---|
11029 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
|
---|
11030 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
11031 | } while (0);
|
---|
11032 |
|
---|
11033 | if (RT_UNLIKELY(fLockWrite) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
11034 | {
|
---|
11035 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
11036 | AssertRC(rc2);
|
---|
11037 | }
|
---|
11038 |
|
---|
11039 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
11040 | return rc;
|
---|
11041 | }
|
---|
11042 |
|
---|
11043 | VBOXDDU_DECL(int) VDAsyncDiscardRanges(PVBOXHDD pDisk, PCRTRANGE paRanges, unsigned cRanges,
|
---|
11044 | PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
11045 | void *pvUser1, void *pvUser2)
|
---|
11046 | {
|
---|
11047 | int rc;
|
---|
11048 | int rc2;
|
---|
11049 | bool fLockWrite = false;
|
---|
11050 | PVDIOCTX pIoCtx = NULL;
|
---|
11051 |
|
---|
11052 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
11053 |
|
---|
11054 | do
|
---|
11055 | {
|
---|
11056 | /* sanity check */
|
---|
11057 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
11058 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
11059 |
|
---|
11060 | rc2 = vdThreadStartWrite(pDisk);
|
---|
11061 | AssertRC(rc2);
|
---|
11062 | fLockWrite = true;
|
---|
11063 |
|
---|
11064 | AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
|
---|
11065 |
|
---|
11066 | pIoCtx = vdIoCtxDiscardAlloc(pDisk, paRanges, cRanges,
|
---|
11067 | pfnComplete, pvUser1, pvUser2, NULL,
|
---|
11068 | vdDiscardHelperAsync,
|
---|
11069 | VDIOCTX_FLAGS_DEFAULT);
|
---|
11070 | if (!pIoCtx)
|
---|
11071 | {
|
---|
11072 | rc = VERR_NO_MEMORY;
|
---|
11073 | break;
|
---|
11074 | }
|
---|
11075 |
|
---|
11076 | rc = vdIoCtxProcessTryLockDefer(pIoCtx);
|
---|
11077 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
11078 | {
|
---|
11079 | if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
|
---|
11080 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
11081 | else
|
---|
11082 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
|
---|
11083 | }
|
---|
11084 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
|
---|
11085 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
11086 | } while (0);
|
---|
11087 |
|
---|
11088 | if (RT_UNLIKELY(fLockWrite) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
11089 | {
|
---|
11090 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
11091 | AssertRC(rc2);
|
---|
11092 | }
|
---|
11093 |
|
---|
11094 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
11095 | return rc;
|
---|
11096 | }
|
---|
11097 |
|
---|
11098 | VBOXDDU_DECL(int) VDRepair(PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
|
---|
11099 | const char *pszFilename, const char *pszBackend,
|
---|
11100 | uint32_t fFlags)
|
---|
11101 | {
|
---|
11102 | int rc = VERR_NOT_SUPPORTED;
|
---|
11103 | PCVBOXHDDBACKEND pBackend = NULL;
|
---|
11104 | VDINTERFACEIOINT VDIfIoInt;
|
---|
11105 | VDINTERFACEIO VDIfIoFallback;
|
---|
11106 | PVDINTERFACEIO pInterfaceIo;
|
---|
11107 |
|
---|
11108 | LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
|
---|
11109 | /* Check arguments. */
|
---|
11110 | AssertMsgReturn(VALID_PTR(pszFilename) && *pszFilename,
|
---|
11111 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
11112 | VERR_INVALID_PARAMETER);
|
---|
11113 | AssertMsgReturn(VALID_PTR(pszBackend),
|
---|
11114 | ("pszBackend=%#p\n", pszBackend),
|
---|
11115 | VERR_INVALID_PARAMETER);
|
---|
11116 | AssertMsgReturn((fFlags & ~VD_REPAIR_FLAGS_MASK) == 0,
|
---|
11117 | ("fFlags=%#x\n", fFlags),
|
---|
11118 | VERR_INVALID_PARAMETER);
|
---|
11119 |
|
---|
11120 | pInterfaceIo = VDIfIoGet(pVDIfsImage);
|
---|
11121 | if (!pInterfaceIo)
|
---|
11122 | {
|
---|
11123 | /*
|
---|
11124 | * Caller doesn't provide an I/O interface, create our own using the
|
---|
11125 | * native file API.
|
---|
11126 | */
|
---|
11127 | vdIfIoFallbackCallbacksSetup(&VDIfIoFallback);
|
---|
11128 | pInterfaceIo = &VDIfIoFallback;
|
---|
11129 | }
|
---|
11130 |
|
---|
11131 | /* Set up the internal I/O interface. */
|
---|
11132 | AssertReturn(!VDIfIoIntGet(pVDIfsImage), VERR_INVALID_PARAMETER);
|
---|
11133 | VDIfIoInt.pfnOpen = vdIOIntOpenLimited;
|
---|
11134 | VDIfIoInt.pfnClose = vdIOIntCloseLimited;
|
---|
11135 | VDIfIoInt.pfnDelete = vdIOIntDeleteLimited;
|
---|
11136 | VDIfIoInt.pfnMove = vdIOIntMoveLimited;
|
---|
11137 | VDIfIoInt.pfnGetFreeSpace = vdIOIntGetFreeSpaceLimited;
|
---|
11138 | VDIfIoInt.pfnGetModificationTime = vdIOIntGetModificationTimeLimited;
|
---|
11139 | VDIfIoInt.pfnGetSize = vdIOIntGetSizeLimited;
|
---|
11140 | VDIfIoInt.pfnSetSize = vdIOIntSetSizeLimited;
|
---|
11141 | VDIfIoInt.pfnReadUser = vdIOIntReadUserLimited;
|
---|
11142 | VDIfIoInt.pfnWriteUser = vdIOIntWriteUserLimited;
|
---|
11143 | VDIfIoInt.pfnReadMeta = vdIOIntReadMetaLimited;
|
---|
11144 | VDIfIoInt.pfnWriteMeta = vdIOIntWriteMetaLimited;
|
---|
11145 | VDIfIoInt.pfnFlush = vdIOIntFlushLimited;
|
---|
11146 | rc = VDInterfaceAdd(&VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
11147 | pInterfaceIo, sizeof(VDINTERFACEIOINT), &pVDIfsImage);
|
---|
11148 | AssertRC(rc);
|
---|
11149 |
|
---|
11150 | rc = vdFindBackend(pszBackend, &pBackend);
|
---|
11151 | if (RT_SUCCESS(rc))
|
---|
11152 | {
|
---|
11153 | if (pBackend->pfnRepair)
|
---|
11154 | rc = pBackend->pfnRepair(pszFilename, pVDIfsDisk, pVDIfsImage, fFlags);
|
---|
11155 | else
|
---|
11156 | rc = VERR_VD_IMAGE_REPAIR_NOT_SUPPORTED;
|
---|
11157 | }
|
---|
11158 |
|
---|
11159 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
11160 | return rc;
|
---|
11161 | }
|
---|
11162 |
|
---|
11163 |
|
---|
11164 | /*
|
---|
11165 | * generic plugin functions
|
---|
11166 | */
|
---|
11167 |
|
---|
11168 | /**
|
---|
11169 | * @interface_method_impl{VBOXHDDBACKEND,pfnComposeLocation}
|
---|
11170 | */
|
---|
11171 | DECLCALLBACK(int) genericFileComposeLocation(PVDINTERFACE pConfig, char **pszLocation)
|
---|
11172 | {
|
---|
11173 | RT_NOREF1(pConfig);
|
---|
11174 | *pszLocation = NULL;
|
---|
11175 | return VINF_SUCCESS;
|
---|
11176 | }
|
---|
11177 |
|
---|
11178 | /**
|
---|
11179 | * @interface_method_impl{VBOXHDDBACKEND,pfnComposeName}
|
---|
11180 | */
|
---|
11181 | DECLCALLBACK(int) genericFileComposeName(PVDINTERFACE pConfig, char **pszName)
|
---|
11182 | {
|
---|
11183 | RT_NOREF1(pConfig);
|
---|
11184 | *pszName = NULL;
|
---|
11185 | return VINF_SUCCESS;
|
---|
11186 | }
|
---|
11187 |
|
---|