1 | /* $Id: VD.cpp 52359 2014-08-12 08:51:58Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxHDD - VBox HDD Container implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2013 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*******************************************************************************
|
---|
19 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 | #define LOG_GROUP LOG_GROUP_VD
|
---|
22 | #include <VBox/vd.h>
|
---|
23 | #include <VBox/err.h>
|
---|
24 | #include <VBox/sup.h>
|
---|
25 | #include <VBox/log.h>
|
---|
26 |
|
---|
27 | #include <iprt/alloc.h>
|
---|
28 | #include <iprt/assert.h>
|
---|
29 | #include <iprt/uuid.h>
|
---|
30 | #include <iprt/file.h>
|
---|
31 | #include <iprt/string.h>
|
---|
32 | #include <iprt/asm.h>
|
---|
33 | #include <iprt/ldr.h>
|
---|
34 | #include <iprt/dir.h>
|
---|
35 | #include <iprt/path.h>
|
---|
36 | #include <iprt/param.h>
|
---|
37 | #include <iprt/memcache.h>
|
---|
38 | #include <iprt/sg.h>
|
---|
39 | #include <iprt/list.h>
|
---|
40 | #include <iprt/avl.h>
|
---|
41 | #include <iprt/semaphore.h>
|
---|
42 |
|
---|
43 | #include <VBox/vd-plugin.h>
|
---|
44 |
|
---|
45 | #include "VDBackends.h"
|
---|
46 |
|
---|
47 | /** Disable dynamic backends on non x86 architectures. This feature
|
---|
48 | * requires the SUPR3 library which is not available there.
|
---|
49 | */
|
---|
50 | #if !defined(VBOX_HDD_NO_DYNAMIC_BACKENDS) && !defined(RT_ARCH_X86) && !defined(RT_ARCH_AMD64)
|
---|
51 | # define VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | #define VBOXHDDDISK_SIGNATURE 0x6f0e2a7d
|
---|
55 |
|
---|
56 | /** Buffer size used for merging images. */
|
---|
57 | #define VD_MERGE_BUFFER_SIZE (16 * _1M)
|
---|
58 |
|
---|
59 | /** Maximum number of segments in one I/O task. */
|
---|
60 | #define VD_IO_TASK_SEGMENTS_MAX 64
|
---|
61 |
|
---|
62 | /** Threshold after not recently used blocks are removed from the list. */
|
---|
63 | #define VD_DISCARD_REMOVE_THRESHOLD (10 * _1M) /** @todo: experiment */
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * VD async I/O interface storage descriptor.
|
---|
67 | */
|
---|
68 | typedef struct VDIIOFALLBACKSTORAGE
|
---|
69 | {
|
---|
70 | /** File handle. */
|
---|
71 | RTFILE File;
|
---|
72 | /** Completion callback. */
|
---|
73 | PFNVDCOMPLETED pfnCompleted;
|
---|
74 | /** Thread for async access. */
|
---|
75 | RTTHREAD ThreadAsync;
|
---|
76 | } VDIIOFALLBACKSTORAGE, *PVDIIOFALLBACKSTORAGE;
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Structure containing everything I/O related
|
---|
80 | * for the image and cache descriptors.
|
---|
81 | */
|
---|
82 | typedef struct VDIO
|
---|
83 | {
|
---|
84 | /** I/O interface to the upper layer. */
|
---|
85 | PVDINTERFACEIO pInterfaceIo;
|
---|
86 |
|
---|
87 | /** Per image internal I/O interface. */
|
---|
88 | VDINTERFACEIOINT VDIfIoInt;
|
---|
89 |
|
---|
90 | /** Fallback I/O interface, only used if the caller doesn't provide it. */
|
---|
91 | VDINTERFACEIO VDIfIo;
|
---|
92 |
|
---|
93 | /** Opaque backend data. */
|
---|
94 | void *pBackendData;
|
---|
95 | /** Disk this image is part of */
|
---|
96 | PVBOXHDD pDisk;
|
---|
97 | /** Flag whether to ignore flush requests. */
|
---|
98 | bool fIgnoreFlush;
|
---|
99 | } VDIO, *PVDIO;
|
---|
100 |
|
---|
101 | /** Forward declaration of an I/O task */
|
---|
102 | typedef struct VDIOTASK *PVDIOTASK;
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * VBox HDD Container image descriptor.
|
---|
106 | */
|
---|
107 | typedef struct VDIMAGE
|
---|
108 | {
|
---|
109 | /** Link to parent image descriptor, if any. */
|
---|
110 | struct VDIMAGE *pPrev;
|
---|
111 | /** Link to child image descriptor, if any. */
|
---|
112 | struct VDIMAGE *pNext;
|
---|
113 | /** Container base filename. (UTF-8) */
|
---|
114 | char *pszFilename;
|
---|
115 | /** Data managed by the backend which keeps the actual info. */
|
---|
116 | void *pBackendData;
|
---|
117 | /** Cached sanitized image flags. */
|
---|
118 | unsigned uImageFlags;
|
---|
119 | /** Image open flags (only those handled generically in this code and which
|
---|
120 | * the backends will never ever see). */
|
---|
121 | unsigned uOpenFlags;
|
---|
122 |
|
---|
123 | /** Function pointers for the various backend methods. */
|
---|
124 | PCVBOXHDDBACKEND Backend;
|
---|
125 | /** Pointer to list of VD interfaces, per-image. */
|
---|
126 | PVDINTERFACE pVDIfsImage;
|
---|
127 | /** I/O related things. */
|
---|
128 | VDIO VDIo;
|
---|
129 | } VDIMAGE, *PVDIMAGE;
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * uModified bit flags.
|
---|
133 | */
|
---|
134 | #define VD_IMAGE_MODIFIED_FLAG RT_BIT(0)
|
---|
135 | #define VD_IMAGE_MODIFIED_FIRST RT_BIT(1)
|
---|
136 | #define VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE RT_BIT(2)
|
---|
137 |
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * VBox HDD Cache image descriptor.
|
---|
141 | */
|
---|
142 | typedef struct VDCACHE
|
---|
143 | {
|
---|
144 | /** Cache base filename. (UTF-8) */
|
---|
145 | char *pszFilename;
|
---|
146 | /** Data managed by the backend which keeps the actual info. */
|
---|
147 | void *pBackendData;
|
---|
148 | /** Cached sanitized image flags. */
|
---|
149 | unsigned uImageFlags;
|
---|
150 | /** Image open flags (only those handled generically in this code and which
|
---|
151 | * the backends will never ever see). */
|
---|
152 | unsigned uOpenFlags;
|
---|
153 |
|
---|
154 | /** Function pointers for the various backend methods. */
|
---|
155 | PCVDCACHEBACKEND Backend;
|
---|
156 |
|
---|
157 | /** Pointer to list of VD interfaces, per-cache. */
|
---|
158 | PVDINTERFACE pVDIfsCache;
|
---|
159 | /** I/O related things. */
|
---|
160 | VDIO VDIo;
|
---|
161 | } VDCACHE, *PVDCACHE;
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * A block waiting for a discard.
|
---|
165 | */
|
---|
166 | typedef struct VDDISCARDBLOCK
|
---|
167 | {
|
---|
168 | /** AVL core. */
|
---|
169 | AVLRU64NODECORE Core;
|
---|
170 | /** LRU list node. */
|
---|
171 | RTLISTNODE NodeLru;
|
---|
172 | /** Number of bytes to discard. */
|
---|
173 | size_t cbDiscard;
|
---|
174 | /** Bitmap of allocated sectors. */
|
---|
175 | void *pbmAllocated;
|
---|
176 | } VDDISCARDBLOCK, *PVDDISCARDBLOCK;
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * VD discard state.
|
---|
180 | */
|
---|
181 | typedef struct VDDISCARDSTATE
|
---|
182 | {
|
---|
183 | /** Number of bytes waiting for a discard. */
|
---|
184 | size_t cbDiscarding;
|
---|
185 | /** AVL tree with blocks waiting for a discard.
|
---|
186 | * The uOffset + cbDiscard range is the search key. */
|
---|
187 | PAVLRU64TREE pTreeBlocks;
|
---|
188 | /** LRU list of the least frequently discarded blocks.
|
---|
189 | * If there are to many blocks waiting the least frequently used
|
---|
190 | * will be removed and the range will be set to 0.
|
---|
191 | */
|
---|
192 | RTLISTNODE ListLru;
|
---|
193 | } VDDISCARDSTATE, *PVDDISCARDSTATE;
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * VD filter instance.
|
---|
197 | */
|
---|
198 | typedef struct VDFILTER
|
---|
199 | {
|
---|
200 | /** Pointer to the previous filter. */
|
---|
201 | struct VDFILTER *pPrev;
|
---|
202 | /** Pointer to the next filter. */
|
---|
203 | struct VDFILTER *pNext;
|
---|
204 | /** Opaque VD filter backend instance data. */
|
---|
205 | void *pvBackendData;
|
---|
206 | /** Pointer to the filter backend interface. */
|
---|
207 | PCVDFILTERBACKEND pBackend;
|
---|
208 | /** Pointer to list of VD interfaces, per-filter. */
|
---|
209 | PVDINTERFACE pVDIfsFilter;
|
---|
210 | /** I/O related things. */
|
---|
211 | VDIO VDIo;
|
---|
212 | } VDFILTER;
|
---|
213 | /** Pointer to a VD filter instance. */
|
---|
214 | typedef VDFILTER *PVDFILTER;
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * VBox HDD Container main structure, private part.
|
---|
218 | */
|
---|
219 | struct VBOXHDD
|
---|
220 | {
|
---|
221 | /** Structure signature (VBOXHDDDISK_SIGNATURE). */
|
---|
222 | uint32_t u32Signature;
|
---|
223 |
|
---|
224 | /** Image type. */
|
---|
225 | VDTYPE enmType;
|
---|
226 |
|
---|
227 | /** Number of opened images. */
|
---|
228 | unsigned cImages;
|
---|
229 |
|
---|
230 | /** Base image. */
|
---|
231 | PVDIMAGE pBase;
|
---|
232 |
|
---|
233 | /** Last opened image in the chain.
|
---|
234 | * The same as pBase if only one image is used. */
|
---|
235 | PVDIMAGE pLast;
|
---|
236 |
|
---|
237 | /** If a merge to one of the parents is running this may be non-NULL
|
---|
238 | * to indicate to what image the writes should be additionally relayed. */
|
---|
239 | PVDIMAGE pImageRelay;
|
---|
240 |
|
---|
241 | /** Flags representing the modification state. */
|
---|
242 | unsigned uModified;
|
---|
243 |
|
---|
244 | /** Cached size of this disk. */
|
---|
245 | uint64_t cbSize;
|
---|
246 | /** Cached PCHS geometry for this disk. */
|
---|
247 | VDGEOMETRY PCHSGeometry;
|
---|
248 | /** Cached LCHS geometry for this disk. */
|
---|
249 | VDGEOMETRY LCHSGeometry;
|
---|
250 |
|
---|
251 | /** Pointer to list of VD interfaces, per-disk. */
|
---|
252 | PVDINTERFACE pVDIfsDisk;
|
---|
253 | /** Pointer to the common interface structure for error reporting. */
|
---|
254 | PVDINTERFACEERROR pInterfaceError;
|
---|
255 | /** Pointer to the optional thread synchronization callbacks. */
|
---|
256 | PVDINTERFACETHREADSYNC pInterfaceThreadSync;
|
---|
257 |
|
---|
258 | /** Memory cache for I/O contexts */
|
---|
259 | RTMEMCACHE hMemCacheIoCtx;
|
---|
260 | /** Memory cache for I/O tasks. */
|
---|
261 | RTMEMCACHE hMemCacheIoTask;
|
---|
262 | /** An I/O context is currently using the disk structures
|
---|
263 | * Every I/O context must be placed on one of the lists below. */
|
---|
264 | volatile bool fLocked;
|
---|
265 | /** Head of pending I/O tasks waiting for completion - LIFO order. */
|
---|
266 | volatile PVDIOTASK pIoTasksPendingHead;
|
---|
267 | /** Head of newly queued I/O contexts - LIFO order. */
|
---|
268 | volatile PVDIOCTX pIoCtxHead;
|
---|
269 | /** Head of halted I/O contexts which are given back to generic
|
---|
270 | * disk framework by the backend. - LIFO order. */
|
---|
271 | volatile PVDIOCTX pIoCtxHaltedHead;
|
---|
272 |
|
---|
273 | /** Head of blocked I/O contexts, processed only
|
---|
274 | * after pIoCtxLockOwner was freed - LIFO order. */
|
---|
275 | volatile PVDIOCTX pIoCtxBlockedHead;
|
---|
276 | /** I/O context which locked the disk for a growing write or flush request.
|
---|
277 | * Other flush or growing write requests need to wait until
|
---|
278 | * the current one completes. - NIL_VDIOCTX if unlocked. */
|
---|
279 | volatile PVDIOCTX pIoCtxLockOwner;
|
---|
280 | /** If the disk was locked by a growing write, flush or discard request this
|
---|
281 | * contains the start offset to check for interfering I/O while it is in progress. */
|
---|
282 | uint64_t uOffsetStartLocked;
|
---|
283 | /** If the disk was locked by a growing write, flush or discard request this contains
|
---|
284 | * the first non affected offset to check for interfering I/O while it is in progress. */
|
---|
285 | uint64_t uOffsetEndLocked;
|
---|
286 |
|
---|
287 | /** Pointer to the L2 disk cache if any. */
|
---|
288 | PVDCACHE pCache;
|
---|
289 | /** Pointer to the discard state if any. */
|
---|
290 | PVDDISCARDSTATE pDiscard;
|
---|
291 |
|
---|
292 | /** Pointer to the first filter in the chain. */
|
---|
293 | PVDFILTER pFilterHead;
|
---|
294 | /** Pointer to the last filter in the chain. */
|
---|
295 | PVDFILTER pFilterTail;
|
---|
296 | };
|
---|
297 |
|
---|
298 | # define VD_IS_LOCKED(a_pDisk) \
|
---|
299 | do \
|
---|
300 | { \
|
---|
301 | AssertMsg(a_pDisk->fLocked, \
|
---|
302 | ("Lock not held\n"));\
|
---|
303 | } while(0)
|
---|
304 |
|
---|
305 | /**
|
---|
306 | * VBox parent read descriptor, used internally for compaction.
|
---|
307 | */
|
---|
308 | typedef struct VDPARENTSTATEDESC
|
---|
309 | {
|
---|
310 | /** Pointer to disk descriptor. */
|
---|
311 | PVBOXHDD pDisk;
|
---|
312 | /** Pointer to image descriptor. */
|
---|
313 | PVDIMAGE pImage;
|
---|
314 | } VDPARENTSTATEDESC, *PVDPARENTSTATEDESC;
|
---|
315 |
|
---|
316 | /**
|
---|
317 | * Transfer direction.
|
---|
318 | */
|
---|
319 | typedef enum VDIOCTXTXDIR
|
---|
320 | {
|
---|
321 | /** Read */
|
---|
322 | VDIOCTXTXDIR_READ = 0,
|
---|
323 | /** Write */
|
---|
324 | VDIOCTXTXDIR_WRITE,
|
---|
325 | /** Flush */
|
---|
326 | VDIOCTXTXDIR_FLUSH,
|
---|
327 | /** Discard */
|
---|
328 | VDIOCTXTXDIR_DISCARD,
|
---|
329 | /** 32bit hack */
|
---|
330 | VDIOCTXTXDIR_32BIT_HACK = 0x7fffffff
|
---|
331 | } VDIOCTXTXDIR, *PVDIOCTXTXDIR;
|
---|
332 |
|
---|
333 | /** Transfer function */
|
---|
334 | typedef DECLCALLBACK(int) FNVDIOCTXTRANSFER (PVDIOCTX pIoCtx);
|
---|
335 | /** Pointer to a transfer function. */
|
---|
336 | typedef FNVDIOCTXTRANSFER *PFNVDIOCTXTRANSFER;
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * I/O context
|
---|
340 | */
|
---|
341 | typedef struct VDIOCTX
|
---|
342 | {
|
---|
343 | /** Pointer to the next I/O context. */
|
---|
344 | struct VDIOCTX * volatile pIoCtxNext;
|
---|
345 | /** Disk this is request is for. */
|
---|
346 | PVBOXHDD pDisk;
|
---|
347 | /** Return code. */
|
---|
348 | int rcReq;
|
---|
349 | /** Various flags for the I/O context. */
|
---|
350 | uint32_t fFlags;
|
---|
351 | /** Number of data transfers currently pending. */
|
---|
352 | volatile uint32_t cDataTransfersPending;
|
---|
353 | /** How many meta data transfers are pending. */
|
---|
354 | volatile uint32_t cMetaTransfersPending;
|
---|
355 | /** Flag whether the request finished */
|
---|
356 | volatile bool fComplete;
|
---|
357 | /** Temporary allocated memory which is freed
|
---|
358 | * when the context completes. */
|
---|
359 | void *pvAllocation;
|
---|
360 | /** Transfer function. */
|
---|
361 | PFNVDIOCTXTRANSFER pfnIoCtxTransfer;
|
---|
362 | /** Next transfer part after the current one completed. */
|
---|
363 | PFNVDIOCTXTRANSFER pfnIoCtxTransferNext;
|
---|
364 | /** Transfer direction */
|
---|
365 | VDIOCTXTXDIR enmTxDir;
|
---|
366 | /** Request type dependent data. */
|
---|
367 | union
|
---|
368 | {
|
---|
369 | /** I/O request (read/write). */
|
---|
370 | struct
|
---|
371 | {
|
---|
372 | /** Number of bytes left until this context completes. */
|
---|
373 | volatile uint32_t cbTransferLeft;
|
---|
374 | /** Current offset */
|
---|
375 | volatile uint64_t uOffset;
|
---|
376 | /** Number of bytes to transfer */
|
---|
377 | volatile size_t cbTransfer;
|
---|
378 | /** Current image in the chain. */
|
---|
379 | PVDIMAGE pImageCur;
|
---|
380 | /** Start image to read from. pImageCur is reset to this
|
---|
381 | * value after it reached the first image in the chain. */
|
---|
382 | PVDIMAGE pImageStart;
|
---|
383 | /** S/G buffer */
|
---|
384 | RTSGBUF SgBuf;
|
---|
385 | /** Number of bytes to clear in the buffer before the current read. */
|
---|
386 | size_t cbBufClear;
|
---|
387 | /** Number of images to read. */
|
---|
388 | unsigned cImagesRead;
|
---|
389 | /** Override for the parent image to start reading from. */
|
---|
390 | PVDIMAGE pImageParentOverride;
|
---|
391 | /** Original offset of the transfer - required for filtering read requests. */
|
---|
392 | uint64_t uOffsetXferOrig;
|
---|
393 | /** Original size of the transfer - required for fitlering read requests. */
|
---|
394 | size_t cbXferOrig;
|
---|
395 | } Io;
|
---|
396 | /** Discard requests. */
|
---|
397 | struct
|
---|
398 | {
|
---|
399 | /** Pointer to the range descriptor array. */
|
---|
400 | PCRTRANGE paRanges;
|
---|
401 | /** Number of ranges in the array. */
|
---|
402 | unsigned cRanges;
|
---|
403 | /** Range descriptor index which is processed. */
|
---|
404 | unsigned idxRange;
|
---|
405 | /** Start offset to discard currently. */
|
---|
406 | uint64_t offCur;
|
---|
407 | /** How many bytes left to discard in the current range. */
|
---|
408 | size_t cbDiscardLeft;
|
---|
409 | /** How many bytes to discard in the current block (<= cbDiscardLeft). */
|
---|
410 | size_t cbThisDiscard;
|
---|
411 | /** Discard block handled currently. */
|
---|
412 | PVDDISCARDBLOCK pBlock;
|
---|
413 | } Discard;
|
---|
414 | } Req;
|
---|
415 | /** Parent I/O context if any. Sets the type of the context (root/child) */
|
---|
416 | PVDIOCTX pIoCtxParent;
|
---|
417 | /** Type dependent data (root/child) */
|
---|
418 | union
|
---|
419 | {
|
---|
420 | /** Root data */
|
---|
421 | struct
|
---|
422 | {
|
---|
423 | /** Completion callback */
|
---|
424 | PFNVDASYNCTRANSFERCOMPLETE pfnComplete;
|
---|
425 | /** User argument 1 passed on completion. */
|
---|
426 | void *pvUser1;
|
---|
427 | /** User argument 2 passed on completion. */
|
---|
428 | void *pvUser2;
|
---|
429 | } Root;
|
---|
430 | /** Child data */
|
---|
431 | struct
|
---|
432 | {
|
---|
433 | /** Saved start offset */
|
---|
434 | uint64_t uOffsetSaved;
|
---|
435 | /** Saved transfer size */
|
---|
436 | size_t cbTransferLeftSaved;
|
---|
437 | /** Number of bytes transferred from the parent if this context completes. */
|
---|
438 | size_t cbTransferParent;
|
---|
439 | /** Number of bytes to pre read */
|
---|
440 | size_t cbPreRead;
|
---|
441 | /** Number of bytes to post read. */
|
---|
442 | size_t cbPostRead;
|
---|
443 | /** Number of bytes to write left in the parent. */
|
---|
444 | size_t cbWriteParent;
|
---|
445 | /** Write type dependent data. */
|
---|
446 | union
|
---|
447 | {
|
---|
448 | /** Optimized */
|
---|
449 | struct
|
---|
450 | {
|
---|
451 | /** Bytes to fill to satisfy the block size. Not part of the virtual disk. */
|
---|
452 | size_t cbFill;
|
---|
453 | /** Bytes to copy instead of reading from the parent */
|
---|
454 | size_t cbWriteCopy;
|
---|
455 | /** Bytes to read from the image. */
|
---|
456 | size_t cbReadImage;
|
---|
457 | } Optimized;
|
---|
458 | } Write;
|
---|
459 | } Child;
|
---|
460 | } Type;
|
---|
461 | } VDIOCTX;
|
---|
462 |
|
---|
463 | /** Default flags for an I/O context, i.e. unblocked and async. */
|
---|
464 | #define VDIOCTX_FLAGS_DEFAULT (0)
|
---|
465 | /** Flag whether the context is blocked. */
|
---|
466 | #define VDIOCTX_FLAGS_BLOCKED RT_BIT_32(0)
|
---|
467 | /** Flag whether the I/O context is using synchronous I/O. */
|
---|
468 | #define VDIOCTX_FLAGS_SYNC RT_BIT_32(1)
|
---|
469 | /** Flag whether the read should update the cache. */
|
---|
470 | #define VDIOCTX_FLAGS_READ_UPDATE_CACHE RT_BIT_32(2)
|
---|
471 | /** Flag whether free blocks should be zeroed.
|
---|
472 | * If false and no image has data for sepcified
|
---|
473 | * range VERR_VD_BLOCK_FREE is returned for the I/O context.
|
---|
474 | * Note that unallocated blocks are still zeroed
|
---|
475 | * if at least one image has valid data for a part
|
---|
476 | * of the range.
|
---|
477 | */
|
---|
478 | #define VDIOCTX_FLAGS_ZERO_FREE_BLOCKS RT_BIT_32(3)
|
---|
479 | /** Don't free the I/O context when complete because
|
---|
480 | * it was alloacted elsewhere (stack, ...). */
|
---|
481 | #define VDIOCTX_FLAGS_DONT_FREE RT_BIT_32(4)
|
---|
482 | /** Don't set the modified flag for this I/O context when writing. */
|
---|
483 | #define VDIOCTX_FLAGS_DONT_SET_MODIFIED_FLAG RT_BIT_32(5)
|
---|
484 | /** The write filter was applied already and shouldn't be applied a second time.
|
---|
485 | * Used at the beginning of vdWriteHelperAsync() because it might be called
|
---|
486 | * multiple times.
|
---|
487 | */
|
---|
488 | #define VDIOCTX_FLAGS_WRITE_FILTER_APPLIED RT_BIT_32(6)
|
---|
489 |
|
---|
490 | /** NIL I/O context pointer value. */
|
---|
491 | #define NIL_VDIOCTX ((PVDIOCTX)0)
|
---|
492 |
|
---|
493 | /**
|
---|
494 | * List node for deferred I/O contexts.
|
---|
495 | */
|
---|
496 | typedef struct VDIOCTXDEFERRED
|
---|
497 | {
|
---|
498 | /** Node in the list of deferred requests.
|
---|
499 | * A request can be deferred if the image is growing
|
---|
500 | * and the request accesses the same range or if
|
---|
501 | * the backend needs to read or write metadata from the disk
|
---|
502 | * before it can continue. */
|
---|
503 | RTLISTNODE NodeDeferred;
|
---|
504 | /** I/O context this entry points to. */
|
---|
505 | PVDIOCTX pIoCtx;
|
---|
506 | } VDIOCTXDEFERRED, *PVDIOCTXDEFERRED;
|
---|
507 |
|
---|
508 | /**
|
---|
509 | * I/O task.
|
---|
510 | */
|
---|
511 | typedef struct VDIOTASK
|
---|
512 | {
|
---|
513 | /** Next I/O task waiting in the list. */
|
---|
514 | struct VDIOTASK * volatile pNext;
|
---|
515 | /** Storage this task belongs to. */
|
---|
516 | PVDIOSTORAGE pIoStorage;
|
---|
517 | /** Optional completion callback. */
|
---|
518 | PFNVDXFERCOMPLETED pfnComplete;
|
---|
519 | /** Opaque user data. */
|
---|
520 | void *pvUser;
|
---|
521 | /** Completion status code for the task. */
|
---|
522 | int rcReq;
|
---|
523 | /** Flag whether this is a meta data transfer. */
|
---|
524 | bool fMeta;
|
---|
525 | /** Type dependent data. */
|
---|
526 | union
|
---|
527 | {
|
---|
528 | /** User data transfer. */
|
---|
529 | struct
|
---|
530 | {
|
---|
531 | /** Number of bytes this task transferred. */
|
---|
532 | uint32_t cbTransfer;
|
---|
533 | /** Pointer to the I/O context the task belongs. */
|
---|
534 | PVDIOCTX pIoCtx;
|
---|
535 | } User;
|
---|
536 | /** Meta data transfer. */
|
---|
537 | struct
|
---|
538 | {
|
---|
539 | /** Meta transfer this task is for. */
|
---|
540 | PVDMETAXFER pMetaXfer;
|
---|
541 | } Meta;
|
---|
542 | } Type;
|
---|
543 | } VDIOTASK;
|
---|
544 |
|
---|
545 | /**
|
---|
546 | * Storage handle.
|
---|
547 | */
|
---|
548 | typedef struct VDIOSTORAGE
|
---|
549 | {
|
---|
550 | /** Image I/O state this storage handle belongs to. */
|
---|
551 | PVDIO pVDIo;
|
---|
552 | /** AVL tree for pending async metadata transfers. */
|
---|
553 | PAVLRFOFFTREE pTreeMetaXfers;
|
---|
554 | /** Storage handle */
|
---|
555 | void *pStorage;
|
---|
556 | } VDIOSTORAGE;
|
---|
557 |
|
---|
558 | /**
|
---|
559 | * Metadata transfer.
|
---|
560 | *
|
---|
561 | * @note This entry can't be freed if either the list is not empty or
|
---|
562 | * the reference counter is not 0.
|
---|
563 | * The assumption is that the backends don't need to read huge amounts of
|
---|
564 | * metadata to complete a transfer so the additional memory overhead should
|
---|
565 | * be relatively small.
|
---|
566 | */
|
---|
567 | typedef struct VDMETAXFER
|
---|
568 | {
|
---|
569 | /** AVL core for fast search (the file offset is the key) */
|
---|
570 | AVLRFOFFNODECORE Core;
|
---|
571 | /** I/O storage for this transfer. */
|
---|
572 | PVDIOSTORAGE pIoStorage;
|
---|
573 | /** Flags. */
|
---|
574 | uint32_t fFlags;
|
---|
575 | /** List of I/O contexts waiting for this metadata transfer to complete. */
|
---|
576 | RTLISTNODE ListIoCtxWaiting;
|
---|
577 | /** Number of references to this entry. */
|
---|
578 | unsigned cRefs;
|
---|
579 | /** Size of the data stored with this entry. */
|
---|
580 | size_t cbMeta;
|
---|
581 | /** Shadow buffer which is used in case a write is still active and other
|
---|
582 | * writes update the shadow buffer. */
|
---|
583 | uint8_t *pbDataShw;
|
---|
584 | /** List of I/O contexts updating the shadow buffer while there is a write
|
---|
585 | * in progress. */
|
---|
586 | RTLISTNODE ListIoCtxShwWrites;
|
---|
587 | /** Data stored - variable size. */
|
---|
588 | uint8_t abData[1];
|
---|
589 | } VDMETAXFER;
|
---|
590 |
|
---|
591 | /**
|
---|
592 | * The transfer direction for the metadata.
|
---|
593 | */
|
---|
594 | #define VDMETAXFER_TXDIR_MASK 0x3
|
---|
595 | #define VDMETAXFER_TXDIR_NONE 0x0
|
---|
596 | #define VDMETAXFER_TXDIR_WRITE 0x1
|
---|
597 | #define VDMETAXFER_TXDIR_READ 0x2
|
---|
598 | #define VDMETAXFER_TXDIR_FLUSH 0x3
|
---|
599 | #define VDMETAXFER_TXDIR_GET(flags) ((flags) & VDMETAXFER_TXDIR_MASK)
|
---|
600 | #define VDMETAXFER_TXDIR_SET(flags, dir) ((flags) = (flags & ~VDMETAXFER_TXDIR_MASK) | (dir))
|
---|
601 |
|
---|
602 | /**
|
---|
603 | * Plugin structure.
|
---|
604 | */
|
---|
605 | typedef struct VDPLUGIN
|
---|
606 | {
|
---|
607 | /** Pointer to the next plugin structure. */
|
---|
608 | RTLISTNODE NodePlugin;
|
---|
609 | /** Handle of loaded plugin library. */
|
---|
610 | RTLDRMOD hPlugin;
|
---|
611 | /** Filename of the loaded plugin. */
|
---|
612 | char *pszFilename;
|
---|
613 | } VDPLUGIN;
|
---|
614 | /** Pointer to a plugin structure. */
|
---|
615 | typedef VDPLUGIN *PVDPLUGIN;
|
---|
616 |
|
---|
617 | /** Head of loaded plugin list. */
|
---|
618 | static RTLISTANCHOR g_ListPluginsLoaded;
|
---|
619 |
|
---|
620 | /** Number of image backends supported. */
|
---|
621 | static unsigned g_cBackends = 0;
|
---|
622 | /** Array of pointers to the image backends. */
|
---|
623 | static PCVBOXHDDBACKEND *g_apBackends = NULL;
|
---|
624 | /** Builtin image backends. */
|
---|
625 | static PCVBOXHDDBACKEND aStaticBackends[] =
|
---|
626 | {
|
---|
627 | &g_VmdkBackend,
|
---|
628 | &g_VDIBackend,
|
---|
629 | &g_VhdBackend,
|
---|
630 | &g_ParallelsBackend,
|
---|
631 | &g_DmgBackend,
|
---|
632 | &g_QedBackend,
|
---|
633 | &g_QCowBackend,
|
---|
634 | &g_VhdxBackend,
|
---|
635 | &g_RawBackend,
|
---|
636 | &g_ISCSIBackend
|
---|
637 | };
|
---|
638 |
|
---|
639 | /** Number of supported cache backends. */
|
---|
640 | static unsigned g_cCacheBackends = 0;
|
---|
641 | /** Array of pointers to the cache backends. */
|
---|
642 | static PCVDCACHEBACKEND *g_apCacheBackends = NULL;
|
---|
643 | /** Builtin cache backends. */
|
---|
644 | static PCVDCACHEBACKEND aStaticCacheBackends[] =
|
---|
645 | {
|
---|
646 | &g_VciCacheBackend
|
---|
647 | };
|
---|
648 |
|
---|
649 | /** Number of supported filter backends. */
|
---|
650 | static unsigned g_cFilterBackends = 0;
|
---|
651 | /** Array of pointers to the filters backends. */
|
---|
652 | static PCVDFILTERBACKEND *g_apFilterBackends = NULL;
|
---|
653 |
|
---|
654 | /** Forward declaration of the async discard helper. */
|
---|
655 | static int vdDiscardHelperAsync(PVDIOCTX pIoCtx);
|
---|
656 | static int vdWriteHelperAsync(PVDIOCTX pIoCtx);
|
---|
657 | static void vdDiskProcessBlockedIoCtx(PVBOXHDD pDisk);
|
---|
658 | static int vdDiskUnlock(PVBOXHDD pDisk, PVDIOCTX pIoCtxRc);
|
---|
659 | static DECLCALLBACK(void) vdIoCtxSyncComplete(void *pvUser1, void *pvUser2, int rcReq);
|
---|
660 |
|
---|
661 | /**
|
---|
662 | * internal: add several backends.
|
---|
663 | */
|
---|
664 | static int vdAddBackends(PCVBOXHDDBACKEND *ppBackends, unsigned cBackends)
|
---|
665 | {
|
---|
666 | PCVBOXHDDBACKEND *pTmp = (PCVBOXHDDBACKEND*)RTMemRealloc(g_apBackends,
|
---|
667 | (g_cBackends + cBackends) * sizeof(PCVBOXHDDBACKEND));
|
---|
668 | if (RT_UNLIKELY(!pTmp))
|
---|
669 | return VERR_NO_MEMORY;
|
---|
670 | g_apBackends = pTmp;
|
---|
671 | memcpy(&g_apBackends[g_cBackends], ppBackends, cBackends * sizeof(PCVBOXHDDBACKEND));
|
---|
672 | g_cBackends += cBackends;
|
---|
673 | return VINF_SUCCESS;
|
---|
674 | }
|
---|
675 |
|
---|
676 | /**
|
---|
677 | * internal: add single backend.
|
---|
678 | */
|
---|
679 | DECLINLINE(int) vdAddBackend(PCVBOXHDDBACKEND pBackend)
|
---|
680 | {
|
---|
681 | return vdAddBackends(&pBackend, 1);
|
---|
682 | }
|
---|
683 |
|
---|
684 | /**
|
---|
685 | * internal: add several cache backends.
|
---|
686 | */
|
---|
687 | static int vdAddCacheBackends(PCVDCACHEBACKEND *ppBackends, unsigned cBackends)
|
---|
688 | {
|
---|
689 | PCVDCACHEBACKEND *pTmp = (PCVDCACHEBACKEND*)RTMemRealloc(g_apCacheBackends,
|
---|
690 | (g_cCacheBackends + cBackends) * sizeof(PCVDCACHEBACKEND));
|
---|
691 | if (RT_UNLIKELY(!pTmp))
|
---|
692 | return VERR_NO_MEMORY;
|
---|
693 | g_apCacheBackends = pTmp;
|
---|
694 | memcpy(&g_apCacheBackends[g_cCacheBackends], ppBackends, cBackends * sizeof(PCVDCACHEBACKEND));
|
---|
695 | g_cCacheBackends += cBackends;
|
---|
696 | return VINF_SUCCESS;
|
---|
697 | }
|
---|
698 |
|
---|
699 | /**
|
---|
700 | * internal: add single cache backend.
|
---|
701 | */
|
---|
702 | DECLINLINE(int) vdAddCacheBackend(PCVDCACHEBACKEND pBackend)
|
---|
703 | {
|
---|
704 | return vdAddCacheBackends(&pBackend, 1);
|
---|
705 | }
|
---|
706 |
|
---|
707 | /**
|
---|
708 | * Add several filter bakends.
|
---|
709 | *
|
---|
710 | * @returns VBox status code.
|
---|
711 | * @param ppBackends Array of filter backends to add.
|
---|
712 | * @param cBackends Number of backends to add.
|
---|
713 | */
|
---|
714 | static int vdAddFilterBackends(PCVDFILTERBACKEND *ppBackends, unsigned cBackends)
|
---|
715 | {
|
---|
716 | PCVDFILTERBACKEND *pTmp = (PCVDFILTERBACKEND *)RTMemRealloc(g_apFilterBackends,
|
---|
717 | (g_cFilterBackends + cBackends) * sizeof(PCVDFILTERBACKEND));
|
---|
718 | if (RT_UNLIKELY(!pTmp))
|
---|
719 | return VERR_NO_MEMORY;
|
---|
720 | g_apFilterBackends = pTmp;
|
---|
721 | memcpy(&g_apFilterBackends[g_cFilterBackends], ppBackends, cBackends * sizeof(PCVDFILTERBACKEND));
|
---|
722 | g_cFilterBackends += cBackends;
|
---|
723 | return VINF_SUCCESS;
|
---|
724 | }
|
---|
725 |
|
---|
726 | /**
|
---|
727 | * Add a single filter backend to the list of supported filters.
|
---|
728 | *
|
---|
729 | * @returns VBox status code.
|
---|
730 | * @param pBackend The backend to add.
|
---|
731 | */
|
---|
732 | DECLINLINE(int) vdAddFilterBackend(PCVDFILTERBACKEND pBackend)
|
---|
733 | {
|
---|
734 | return vdAddFilterBackends(&pBackend, 1);
|
---|
735 | }
|
---|
736 |
|
---|
737 | /**
|
---|
738 | * internal: issue error message.
|
---|
739 | */
|
---|
740 | static int vdError(PVBOXHDD pDisk, int rc, RT_SRC_POS_DECL,
|
---|
741 | const char *pszFormat, ...)
|
---|
742 | {
|
---|
743 | va_list va;
|
---|
744 | va_start(va, pszFormat);
|
---|
745 | if (pDisk->pInterfaceError)
|
---|
746 | pDisk->pInterfaceError->pfnError(pDisk->pInterfaceError->Core.pvUser, rc, RT_SRC_POS_ARGS, pszFormat, va);
|
---|
747 | va_end(va);
|
---|
748 | return rc;
|
---|
749 | }
|
---|
750 |
|
---|
751 | /**
|
---|
752 | * internal: thread synchronization, start read.
|
---|
753 | */
|
---|
754 | DECLINLINE(int) vdThreadStartRead(PVBOXHDD pDisk)
|
---|
755 | {
|
---|
756 | int rc = VINF_SUCCESS;
|
---|
757 | if (RT_UNLIKELY(pDisk->pInterfaceThreadSync))
|
---|
758 | rc = pDisk->pInterfaceThreadSync->pfnStartRead(pDisk->pInterfaceThreadSync->Core.pvUser);
|
---|
759 | return rc;
|
---|
760 | }
|
---|
761 |
|
---|
762 | /**
|
---|
763 | * internal: thread synchronization, finish read.
|
---|
764 | */
|
---|
765 | DECLINLINE(int) vdThreadFinishRead(PVBOXHDD pDisk)
|
---|
766 | {
|
---|
767 | int rc = VINF_SUCCESS;
|
---|
768 | if (RT_UNLIKELY(pDisk->pInterfaceThreadSync))
|
---|
769 | rc = pDisk->pInterfaceThreadSync->pfnFinishRead(pDisk->pInterfaceThreadSync->Core.pvUser);
|
---|
770 | return rc;
|
---|
771 | }
|
---|
772 |
|
---|
773 | /**
|
---|
774 | * internal: thread synchronization, start write.
|
---|
775 | */
|
---|
776 | DECLINLINE(int) vdThreadStartWrite(PVBOXHDD pDisk)
|
---|
777 | {
|
---|
778 | int rc = VINF_SUCCESS;
|
---|
779 | if (RT_UNLIKELY(pDisk->pInterfaceThreadSync))
|
---|
780 | rc = pDisk->pInterfaceThreadSync->pfnStartWrite(pDisk->pInterfaceThreadSync->Core.pvUser);
|
---|
781 | return rc;
|
---|
782 | }
|
---|
783 |
|
---|
784 | /**
|
---|
785 | * internal: thread synchronization, finish write.
|
---|
786 | */
|
---|
787 | DECLINLINE(int) vdThreadFinishWrite(PVBOXHDD pDisk)
|
---|
788 | {
|
---|
789 | int rc = VINF_SUCCESS;
|
---|
790 | if (RT_UNLIKELY(pDisk->pInterfaceThreadSync))
|
---|
791 | rc = pDisk->pInterfaceThreadSync->pfnFinishWrite(pDisk->pInterfaceThreadSync->Core.pvUser);
|
---|
792 | return rc;
|
---|
793 | }
|
---|
794 |
|
---|
795 | /**
|
---|
796 | * internal: find image format backend.
|
---|
797 | */
|
---|
798 | static int vdFindBackend(const char *pszBackend, PCVBOXHDDBACKEND *ppBackend)
|
---|
799 | {
|
---|
800 | int rc = VINF_SUCCESS;
|
---|
801 | PCVBOXHDDBACKEND pBackend = NULL;
|
---|
802 |
|
---|
803 | if (!g_apBackends)
|
---|
804 | VDInit();
|
---|
805 |
|
---|
806 | for (unsigned i = 0; i < g_cBackends; i++)
|
---|
807 | {
|
---|
808 | if (!RTStrICmp(pszBackend, g_apBackends[i]->pszBackendName))
|
---|
809 | {
|
---|
810 | pBackend = g_apBackends[i];
|
---|
811 | break;
|
---|
812 | }
|
---|
813 | }
|
---|
814 | *ppBackend = pBackend;
|
---|
815 | return rc;
|
---|
816 | }
|
---|
817 |
|
---|
818 | /**
|
---|
819 | * internal: find cache format backend.
|
---|
820 | */
|
---|
821 | static int vdFindCacheBackend(const char *pszBackend, PCVDCACHEBACKEND *ppBackend)
|
---|
822 | {
|
---|
823 | int rc = VINF_SUCCESS;
|
---|
824 | PCVDCACHEBACKEND pBackend = NULL;
|
---|
825 |
|
---|
826 | if (!g_apCacheBackends)
|
---|
827 | VDInit();
|
---|
828 |
|
---|
829 | for (unsigned i = 0; i < g_cCacheBackends; i++)
|
---|
830 | {
|
---|
831 | if (!RTStrICmp(pszBackend, g_apCacheBackends[i]->pszBackendName))
|
---|
832 | {
|
---|
833 | pBackend = g_apCacheBackends[i];
|
---|
834 | break;
|
---|
835 | }
|
---|
836 | }
|
---|
837 | *ppBackend = pBackend;
|
---|
838 | return rc;
|
---|
839 | }
|
---|
840 |
|
---|
841 | /**
|
---|
842 | * internal: find filter backend.
|
---|
843 | */
|
---|
844 | static int vdFindFilterBackend(const char *pszFilter, PCVDFILTERBACKEND *ppBackend)
|
---|
845 | {
|
---|
846 | int rc = VINF_SUCCESS;
|
---|
847 | PCVDFILTERBACKEND pBackend = NULL;
|
---|
848 |
|
---|
849 | for (unsigned i = 0; i < g_cFilterBackends; i++)
|
---|
850 | {
|
---|
851 | if (!RTStrICmp(pszFilter, g_apFilterBackends[i]->pszBackendName))
|
---|
852 | {
|
---|
853 | pBackend = g_apFilterBackends[i];
|
---|
854 | break;
|
---|
855 | }
|
---|
856 | }
|
---|
857 | *ppBackend = pBackend;
|
---|
858 | return rc;
|
---|
859 | }
|
---|
860 |
|
---|
861 |
|
---|
862 | /**
|
---|
863 | * internal: add image structure to the end of images list.
|
---|
864 | */
|
---|
865 | static void vdAddImageToList(PVBOXHDD pDisk, PVDIMAGE pImage)
|
---|
866 | {
|
---|
867 | pImage->pPrev = NULL;
|
---|
868 | pImage->pNext = NULL;
|
---|
869 |
|
---|
870 | if (pDisk->pBase)
|
---|
871 | {
|
---|
872 | Assert(pDisk->cImages > 0);
|
---|
873 | pImage->pPrev = pDisk->pLast;
|
---|
874 | pDisk->pLast->pNext = pImage;
|
---|
875 | pDisk->pLast = pImage;
|
---|
876 | }
|
---|
877 | else
|
---|
878 | {
|
---|
879 | Assert(pDisk->cImages == 0);
|
---|
880 | pDisk->pBase = pImage;
|
---|
881 | pDisk->pLast = pImage;
|
---|
882 | }
|
---|
883 |
|
---|
884 | pDisk->cImages++;
|
---|
885 | }
|
---|
886 |
|
---|
887 | /**
|
---|
888 | * internal: remove image structure from the images list.
|
---|
889 | */
|
---|
890 | static void vdRemoveImageFromList(PVBOXHDD pDisk, PVDIMAGE pImage)
|
---|
891 | {
|
---|
892 | Assert(pDisk->cImages > 0);
|
---|
893 |
|
---|
894 | if (pImage->pPrev)
|
---|
895 | pImage->pPrev->pNext = pImage->pNext;
|
---|
896 | else
|
---|
897 | pDisk->pBase = pImage->pNext;
|
---|
898 |
|
---|
899 | if (pImage->pNext)
|
---|
900 | pImage->pNext->pPrev = pImage->pPrev;
|
---|
901 | else
|
---|
902 | pDisk->pLast = pImage->pPrev;
|
---|
903 |
|
---|
904 | pImage->pPrev = NULL;
|
---|
905 | pImage->pNext = NULL;
|
---|
906 |
|
---|
907 | pDisk->cImages--;
|
---|
908 | }
|
---|
909 |
|
---|
910 | /**
|
---|
911 | * internal: add filter structure to the end of filter list.
|
---|
912 | */
|
---|
913 | static void vdAddFilterToList(PVBOXHDD pDisk, PVDFILTER pFilter)
|
---|
914 | {
|
---|
915 | pFilter->pPrev = NULL;
|
---|
916 | pFilter->pNext = NULL;
|
---|
917 |
|
---|
918 | if (pDisk->pFilterHead)
|
---|
919 | {
|
---|
920 | pFilter->pPrev = pDisk->pFilterTail;
|
---|
921 | pDisk->pFilterTail->pNext = pFilter;
|
---|
922 | pDisk->pFilterTail = pFilter;
|
---|
923 | }
|
---|
924 | else
|
---|
925 | {
|
---|
926 | pDisk->pFilterHead = pFilter;
|
---|
927 | pDisk->pFilterTail = pFilter;
|
---|
928 | }
|
---|
929 | }
|
---|
930 |
|
---|
931 | /**
|
---|
932 | * internal: Remove last filter structure from the filter list.
|
---|
933 | */
|
---|
934 | static void vdRemoveFilterFromList(PVBOXHDD pDisk, PVDFILTER pFilter)
|
---|
935 | {
|
---|
936 | Assert(pDisk->pFilterHead != NULL && pDisk->pFilterTail != NULL);
|
---|
937 |
|
---|
938 | if (pFilter->pPrev)
|
---|
939 | pFilter->pPrev->pNext = pFilter->pNext;
|
---|
940 | else
|
---|
941 | pDisk->pFilterHead = pFilter->pNext;
|
---|
942 |
|
---|
943 | if (pFilter->pNext)
|
---|
944 | pFilter->pNext->pPrev = pFilter->pPrev;
|
---|
945 | else
|
---|
946 | pDisk->pFilterTail = pFilter->pPrev;
|
---|
947 |
|
---|
948 | pFilter->pPrev = NULL;
|
---|
949 | pFilter->pNext = NULL;
|
---|
950 | }
|
---|
951 |
|
---|
952 | /**
|
---|
953 | * internal: find image by index into the images list.
|
---|
954 | */
|
---|
955 | static PVDIMAGE vdGetImageByNumber(PVBOXHDD pDisk, unsigned nImage)
|
---|
956 | {
|
---|
957 | PVDIMAGE pImage = pDisk->pBase;
|
---|
958 | if (nImage == VD_LAST_IMAGE)
|
---|
959 | return pDisk->pLast;
|
---|
960 | while (pImage && nImage)
|
---|
961 | {
|
---|
962 | pImage = pImage->pNext;
|
---|
963 | nImage--;
|
---|
964 | }
|
---|
965 | return pImage;
|
---|
966 | }
|
---|
967 |
|
---|
968 | /**
|
---|
969 | * Applies the filter chain to the given write request.
|
---|
970 | *
|
---|
971 | * @returns VBox status code.
|
---|
972 | * @param pDisk The HDD container.
|
---|
973 | * @param uOffset The start offset of the write.
|
---|
974 | * @param cbWrite Number of bytes to write.
|
---|
975 | * @param pIoCtx The I/O context associated with the request.
|
---|
976 | */
|
---|
977 | static int vdFilterChainApplyWrite(PVBOXHDD pDisk, uint64_t uOffset, size_t cbWrite,
|
---|
978 | PVDIOCTX pIoCtx)
|
---|
979 | {
|
---|
980 | int rc = VINF_SUCCESS;
|
---|
981 |
|
---|
982 | VD_IS_LOCKED(pDisk);
|
---|
983 |
|
---|
984 | if (pDisk->pFilterHead)
|
---|
985 | {
|
---|
986 | PVDFILTER pFilterCurr = pDisk->pFilterHead;
|
---|
987 |
|
---|
988 | do
|
---|
989 | {
|
---|
990 | rc = pFilterCurr->pBackend->pfnFilterWrite(pFilterCurr->pvBackendData, uOffset, cbWrite, pIoCtx);
|
---|
991 | /* Reset S/G buffer for the next filter. */
|
---|
992 | RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
|
---|
993 |
|
---|
994 | pFilterCurr = pFilterCurr->pNext;
|
---|
995 | } while ( RT_SUCCESS(rc)
|
---|
996 | && pFilterCurr);
|
---|
997 | }
|
---|
998 |
|
---|
999 | return rc;
|
---|
1000 | }
|
---|
1001 |
|
---|
1002 | /**
|
---|
1003 | * Applies the filter chain to the given read request.
|
---|
1004 | *
|
---|
1005 | * @returns VBox status code.
|
---|
1006 | * @param pDisk The HDD container.
|
---|
1007 | * @param uOffset The start offset of the read.
|
---|
1008 | * @param cbRead Number of bytes read.
|
---|
1009 | * @param pIoCtx The I/O context associated with the request.
|
---|
1010 | */
|
---|
1011 | static int vdFilterChainApplyRead(PVBOXHDD pDisk, uint64_t uOffset, size_t cbRead,
|
---|
1012 | PVDIOCTX pIoCtx)
|
---|
1013 | {
|
---|
1014 | int rc = VINF_SUCCESS;
|
---|
1015 |
|
---|
1016 | VD_IS_LOCKED(pDisk);
|
---|
1017 |
|
---|
1018 | if (pDisk->pFilterHead)
|
---|
1019 | {
|
---|
1020 | PVDFILTER pFilterCurr = pDisk->pFilterHead;
|
---|
1021 |
|
---|
1022 | /* Reset buffer before starting. */
|
---|
1023 | RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
|
---|
1024 |
|
---|
1025 | do
|
---|
1026 | {
|
---|
1027 | rc = pFilterCurr->pBackend->pfnFilterRead(pFilterCurr->pvBackendData, uOffset, cbRead, pIoCtx);
|
---|
1028 | /* Reset S/G buffer for the next filter. */
|
---|
1029 | RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
|
---|
1030 |
|
---|
1031 | pFilterCurr = pFilterCurr->pNext;
|
---|
1032 | } while ( RT_SUCCESS(rc)
|
---|
1033 | && pFilterCurr);
|
---|
1034 | }
|
---|
1035 |
|
---|
1036 | return rc;
|
---|
1037 | }
|
---|
1038 |
|
---|
1039 | DECLINLINE(void) vdIoCtxRootComplete(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
|
---|
1040 | {
|
---|
1041 | if ( RT_SUCCESS(pIoCtx->rcReq)
|
---|
1042 | && pIoCtx->enmTxDir == VDIOCTXTXDIR_READ)
|
---|
1043 | pIoCtx->rcReq = vdFilterChainApplyRead(pDisk, pIoCtx->Req.Io.uOffsetXferOrig,
|
---|
1044 | pIoCtx->Req.Io.cbXferOrig, pIoCtx);
|
---|
1045 |
|
---|
1046 | pIoCtx->Type.Root.pfnComplete(pIoCtx->Type.Root.pvUser1,
|
---|
1047 | pIoCtx->Type.Root.pvUser2,
|
---|
1048 | pIoCtx->rcReq);
|
---|
1049 | }
|
---|
1050 |
|
---|
1051 | /**
|
---|
1052 | * Initialize the structure members of a given I/O context.
|
---|
1053 | */
|
---|
1054 | DECLINLINE(void) vdIoCtxInit(PVDIOCTX pIoCtx, PVBOXHDD pDisk, VDIOCTXTXDIR enmTxDir,
|
---|
1055 | uint64_t uOffset, size_t cbTransfer, PVDIMAGE pImageStart,
|
---|
1056 | PCRTSGBUF pcSgBuf, void *pvAllocation,
|
---|
1057 | PFNVDIOCTXTRANSFER pfnIoCtxTransfer, uint32_t fFlags)
|
---|
1058 | {
|
---|
1059 | pIoCtx->pDisk = pDisk;
|
---|
1060 | pIoCtx->enmTxDir = enmTxDir;
|
---|
1061 | pIoCtx->Req.Io.cbTransferLeft = (uint32_t)cbTransfer; Assert((uint32_t)cbTransfer == cbTransfer);
|
---|
1062 | pIoCtx->Req.Io.uOffset = uOffset;
|
---|
1063 | pIoCtx->Req.Io.cbTransfer = cbTransfer;
|
---|
1064 | pIoCtx->Req.Io.pImageStart = pImageStart;
|
---|
1065 | pIoCtx->Req.Io.pImageCur = pImageStart;
|
---|
1066 | pIoCtx->Req.Io.cbBufClear = 0;
|
---|
1067 | pIoCtx->Req.Io.pImageParentOverride = NULL;
|
---|
1068 | pIoCtx->Req.Io.uOffsetXferOrig = uOffset;
|
---|
1069 | pIoCtx->Req.Io.cbXferOrig = cbTransfer;
|
---|
1070 | pIoCtx->cDataTransfersPending = 0;
|
---|
1071 | pIoCtx->cMetaTransfersPending = 0;
|
---|
1072 | pIoCtx->fComplete = false;
|
---|
1073 | pIoCtx->fFlags = fFlags;
|
---|
1074 | pIoCtx->pvAllocation = pvAllocation;
|
---|
1075 | pIoCtx->pfnIoCtxTransfer = pfnIoCtxTransfer;
|
---|
1076 | pIoCtx->pfnIoCtxTransferNext = NULL;
|
---|
1077 | pIoCtx->rcReq = VINF_SUCCESS;
|
---|
1078 | pIoCtx->pIoCtxParent = NULL;
|
---|
1079 |
|
---|
1080 | /* There is no S/G list for a flush request. */
|
---|
1081 | if ( enmTxDir != VDIOCTXTXDIR_FLUSH
|
---|
1082 | && enmTxDir != VDIOCTXTXDIR_DISCARD)
|
---|
1083 | RTSgBufClone(&pIoCtx->Req.Io.SgBuf, pcSgBuf);
|
---|
1084 | else
|
---|
1085 | memset(&pIoCtx->Req.Io.SgBuf, 0, sizeof(RTSGBUF));
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | /**
|
---|
1089 | * Internal: Tries to read the desired range from the given cache.
|
---|
1090 | *
|
---|
1091 | * @returns VBox status code.
|
---|
1092 | * @retval VERR_VD_BLOCK_FREE if the block is not in the cache.
|
---|
1093 | * pcbRead will be set to the number of bytes not in the cache.
|
---|
1094 | * Everything thereafter might be in the cache.
|
---|
1095 | * @param pCache The cache to read from.
|
---|
1096 | * @param uOffset Offset of the virtual disk to read.
|
---|
1097 | * @param cbRead How much to read.
|
---|
1098 | * @param pIoCtx The I/O context to read into.
|
---|
1099 | * @param pcbRead Where to store the number of bytes actually read.
|
---|
1100 | * On success this indicates the number of bytes read from the cache.
|
---|
1101 | * If VERR_VD_BLOCK_FREE is returned this gives the number of bytes
|
---|
1102 | * which are not in the cache.
|
---|
1103 | * In both cases everything beyond this value
|
---|
1104 | * might or might not be in the cache.
|
---|
1105 | */
|
---|
1106 | static int vdCacheReadHelper(PVDCACHE pCache, uint64_t uOffset,
|
---|
1107 | size_t cbRead, PVDIOCTX pIoCtx, size_t *pcbRead)
|
---|
1108 | {
|
---|
1109 | int rc = VINF_SUCCESS;
|
---|
1110 |
|
---|
1111 | LogFlowFunc(("pCache=%#p uOffset=%llu pIoCtx=%p cbRead=%zu pcbRead=%#p\n",
|
---|
1112 | pCache, uOffset, pIoCtx, cbRead, pcbRead));
|
---|
1113 |
|
---|
1114 | AssertPtr(pCache);
|
---|
1115 | AssertPtr(pcbRead);
|
---|
1116 |
|
---|
1117 | rc = pCache->Backend->pfnRead(pCache->pBackendData, uOffset, cbRead,
|
---|
1118 | pIoCtx, pcbRead);
|
---|
1119 |
|
---|
1120 | LogFlowFunc(("returns rc=%Rrc pcbRead=%zu\n", rc, *pcbRead));
|
---|
1121 | return rc;
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 | /**
|
---|
1125 | * Internal: Writes data for the given block into the cache.
|
---|
1126 | *
|
---|
1127 | * @returns VBox status code.
|
---|
1128 | * @param pCache The cache to write to.
|
---|
1129 | * @param uOffset Offset of the virtual disk to write to the cache.
|
---|
1130 | * @param cbWrite How much to write.
|
---|
1131 | * @param pIoCtx The I/O context to ẃrite from.
|
---|
1132 | * @param pcbWritten How much data could be written, optional.
|
---|
1133 | */
|
---|
1134 | static int vdCacheWriteHelper(PVDCACHE pCache, uint64_t uOffset, size_t cbWrite,
|
---|
1135 | PVDIOCTX pIoCtx, size_t *pcbWritten)
|
---|
1136 | {
|
---|
1137 | int rc = VINF_SUCCESS;
|
---|
1138 |
|
---|
1139 | LogFlowFunc(("pCache=%#p uOffset=%llu pIoCtx=%p cbWrite=%zu pcbWritten=%#p\n",
|
---|
1140 | pCache, uOffset, pIoCtx, cbWrite, pcbWritten));
|
---|
1141 |
|
---|
1142 | AssertPtr(pCache);
|
---|
1143 | AssertPtr(pIoCtx);
|
---|
1144 | Assert(cbWrite > 0);
|
---|
1145 |
|
---|
1146 | if (pcbWritten)
|
---|
1147 | rc = pCache->Backend->pfnWrite(pCache->pBackendData, uOffset, cbWrite,
|
---|
1148 | pIoCtx, pcbWritten);
|
---|
1149 | else
|
---|
1150 | {
|
---|
1151 | size_t cbWritten = 0;
|
---|
1152 |
|
---|
1153 | do
|
---|
1154 | {
|
---|
1155 | rc = pCache->Backend->pfnWrite(pCache->pBackendData, uOffset, cbWrite,
|
---|
1156 | pIoCtx, &cbWritten);
|
---|
1157 | uOffset += cbWritten;
|
---|
1158 | cbWrite -= cbWritten;
|
---|
1159 | } while ( cbWrite
|
---|
1160 | && ( RT_SUCCESS(rc)
|
---|
1161 | || rc == VERR_VD_ASYNC_IO_IN_PROGRESS));
|
---|
1162 | }
|
---|
1163 |
|
---|
1164 | LogFlowFunc(("returns rc=%Rrc pcbWritten=%zu\n",
|
---|
1165 | rc, pcbWritten ? *pcbWritten : cbWrite));
|
---|
1166 | return rc;
|
---|
1167 | }
|
---|
1168 |
|
---|
1169 | /**
|
---|
1170 | * Creates a new empty discard state.
|
---|
1171 | *
|
---|
1172 | * @returns Pointer to the new discard state or NULL if out of memory.
|
---|
1173 | */
|
---|
1174 | static PVDDISCARDSTATE vdDiscardStateCreate(void)
|
---|
1175 | {
|
---|
1176 | PVDDISCARDSTATE pDiscard = (PVDDISCARDSTATE)RTMemAllocZ(sizeof(VDDISCARDSTATE));
|
---|
1177 |
|
---|
1178 | if (pDiscard)
|
---|
1179 | {
|
---|
1180 | RTListInit(&pDiscard->ListLru);
|
---|
1181 | pDiscard->pTreeBlocks = (PAVLRU64TREE)RTMemAllocZ(sizeof(AVLRU64TREE));
|
---|
1182 | if (!pDiscard->pTreeBlocks)
|
---|
1183 | {
|
---|
1184 | RTMemFree(pDiscard);
|
---|
1185 | pDiscard = NULL;
|
---|
1186 | }
|
---|
1187 | }
|
---|
1188 |
|
---|
1189 | return pDiscard;
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 | /**
|
---|
1193 | * Removes the least recently used blocks from the waiting list until
|
---|
1194 | * the new value is reached.
|
---|
1195 | *
|
---|
1196 | * @returns VBox status code.
|
---|
1197 | * @param pDisk VD disk container.
|
---|
1198 | * @param pDiscard The discard state.
|
---|
1199 | * @param cbDiscardingNew How many bytes should be waiting on success.
|
---|
1200 | * The number of bytes waiting can be less.
|
---|
1201 | */
|
---|
1202 | static int vdDiscardRemoveBlocks(PVBOXHDD pDisk, PVDDISCARDSTATE pDiscard, size_t cbDiscardingNew)
|
---|
1203 | {
|
---|
1204 | int rc = VINF_SUCCESS;
|
---|
1205 |
|
---|
1206 | LogFlowFunc(("pDisk=%#p pDiscard=%#p cbDiscardingNew=%zu\n",
|
---|
1207 | pDisk, pDiscard, cbDiscardingNew));
|
---|
1208 |
|
---|
1209 | while (pDiscard->cbDiscarding > cbDiscardingNew)
|
---|
1210 | {
|
---|
1211 | PVDDISCARDBLOCK pBlock = RTListGetLast(&pDiscard->ListLru, VDDISCARDBLOCK, NodeLru);
|
---|
1212 |
|
---|
1213 | Assert(!RTListIsEmpty(&pDiscard->ListLru));
|
---|
1214 |
|
---|
1215 | /* Go over the allocation bitmap and mark all discarded sectors as unused. */
|
---|
1216 | uint64_t offStart = pBlock->Core.Key;
|
---|
1217 | uint32_t idxStart = 0;
|
---|
1218 | size_t cbLeft = pBlock->cbDiscard;
|
---|
1219 | bool fAllocated = ASMBitTest(pBlock->pbmAllocated, idxStart);
|
---|
1220 | uint32_t cSectors = (uint32_t)(pBlock->cbDiscard / 512);
|
---|
1221 |
|
---|
1222 | while (cbLeft > 0)
|
---|
1223 | {
|
---|
1224 | int32_t idxEnd;
|
---|
1225 | size_t cbThis = cbLeft;
|
---|
1226 |
|
---|
1227 | if (fAllocated)
|
---|
1228 | {
|
---|
1229 | /* Check for the first unallocated bit. */
|
---|
1230 | idxEnd = ASMBitNextClear(pBlock->pbmAllocated, cSectors, idxStart);
|
---|
1231 | if (idxEnd != -1)
|
---|
1232 | {
|
---|
1233 | cbThis = (idxEnd - idxStart) * 512;
|
---|
1234 | fAllocated = false;
|
---|
1235 | }
|
---|
1236 | }
|
---|
1237 | else
|
---|
1238 | {
|
---|
1239 | /* Mark as unused and check for the first set bit. */
|
---|
1240 | idxEnd = ASMBitNextSet(pBlock->pbmAllocated, cSectors, idxStart);
|
---|
1241 | if (idxEnd != -1)
|
---|
1242 | cbThis = (idxEnd - idxStart) * 512;
|
---|
1243 |
|
---|
1244 |
|
---|
1245 | VDIOCTX IoCtx;
|
---|
1246 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_DISCARD, 0, 0, NULL,
|
---|
1247 | NULL, NULL, NULL, VDIOCTX_FLAGS_SYNC);
|
---|
1248 | rc = pDisk->pLast->Backend->pfnDiscard(pDisk->pLast->pBackendData,
|
---|
1249 | &IoCtx, offStart, cbThis, NULL,
|
---|
1250 | NULL, &cbThis, NULL,
|
---|
1251 | VD_DISCARD_MARK_UNUSED);
|
---|
1252 | if (RT_FAILURE(rc))
|
---|
1253 | break;
|
---|
1254 |
|
---|
1255 | fAllocated = true;
|
---|
1256 | }
|
---|
1257 |
|
---|
1258 | idxStart = idxEnd;
|
---|
1259 | offStart += cbThis;
|
---|
1260 | cbLeft -= cbThis;
|
---|
1261 | }
|
---|
1262 |
|
---|
1263 | if (RT_FAILURE(rc))
|
---|
1264 | break;
|
---|
1265 |
|
---|
1266 | PVDDISCARDBLOCK pBlockRemove = (PVDDISCARDBLOCK)RTAvlrU64RangeRemove(pDiscard->pTreeBlocks, pBlock->Core.Key);
|
---|
1267 | Assert(pBlockRemove == pBlock);
|
---|
1268 | RTListNodeRemove(&pBlock->NodeLru);
|
---|
1269 |
|
---|
1270 | pDiscard->cbDiscarding -= pBlock->cbDiscard;
|
---|
1271 | RTMemFree(pBlock->pbmAllocated);
|
---|
1272 | RTMemFree(pBlock);
|
---|
1273 | }
|
---|
1274 |
|
---|
1275 | Assert(RT_FAILURE(rc) || pDiscard->cbDiscarding <= cbDiscardingNew);
|
---|
1276 |
|
---|
1277 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
1278 | return rc;
|
---|
1279 | }
|
---|
1280 |
|
---|
1281 | /**
|
---|
1282 | * Destroys the current discard state, writing any waiting blocks to the image.
|
---|
1283 | *
|
---|
1284 | * @returns VBox status code.
|
---|
1285 | * @param pDisk VD disk container.
|
---|
1286 | */
|
---|
1287 | static int vdDiscardStateDestroy(PVBOXHDD pDisk)
|
---|
1288 | {
|
---|
1289 | int rc = VINF_SUCCESS;
|
---|
1290 |
|
---|
1291 | if (pDisk->pDiscard)
|
---|
1292 | {
|
---|
1293 | rc = vdDiscardRemoveBlocks(pDisk, pDisk->pDiscard, 0 /* Remove all blocks. */);
|
---|
1294 | AssertRC(rc);
|
---|
1295 | RTMemFree(pDisk->pDiscard->pTreeBlocks);
|
---|
1296 | RTMemFree(pDisk->pDiscard);
|
---|
1297 | pDisk->pDiscard = NULL;
|
---|
1298 | }
|
---|
1299 |
|
---|
1300 | return rc;
|
---|
1301 | }
|
---|
1302 |
|
---|
1303 | /**
|
---|
1304 | * Marks the given range as allocated in the image.
|
---|
1305 | * Required if there are discards in progress and a write to a block which can get discarded
|
---|
1306 | * is written to.
|
---|
1307 | *
|
---|
1308 | * @returns VBox status code.
|
---|
1309 | * @param pDisk VD container data.
|
---|
1310 | * @param uOffset First byte to mark as allocated.
|
---|
1311 | * @param cbRange Number of bytes to mark as allocated.
|
---|
1312 | */
|
---|
1313 | static int vdDiscardSetRangeAllocated(PVBOXHDD pDisk, uint64_t uOffset, size_t cbRange)
|
---|
1314 | {
|
---|
1315 | PVDDISCARDSTATE pDiscard = pDisk->pDiscard;
|
---|
1316 | int rc = VINF_SUCCESS;
|
---|
1317 |
|
---|
1318 | if (pDiscard)
|
---|
1319 | {
|
---|
1320 | do
|
---|
1321 | {
|
---|
1322 | size_t cbThisRange = cbRange;
|
---|
1323 | PVDDISCARDBLOCK pBlock = (PVDDISCARDBLOCK)RTAvlrU64RangeGet(pDiscard->pTreeBlocks, uOffset);
|
---|
1324 |
|
---|
1325 | if (pBlock)
|
---|
1326 | {
|
---|
1327 | int32_t idxStart, idxEnd;
|
---|
1328 |
|
---|
1329 | Assert(!(cbThisRange % 512));
|
---|
1330 | Assert(!((uOffset - pBlock->Core.Key) % 512));
|
---|
1331 |
|
---|
1332 | cbThisRange = RT_MIN(cbThisRange, pBlock->Core.KeyLast - uOffset + 1);
|
---|
1333 |
|
---|
1334 | idxStart = (uOffset - pBlock->Core.Key) / 512;
|
---|
1335 | idxEnd = idxStart + (int32_t)(cbThisRange / 512);
|
---|
1336 | ASMBitSetRange(pBlock->pbmAllocated, idxStart, idxEnd);
|
---|
1337 | }
|
---|
1338 | else
|
---|
1339 | {
|
---|
1340 | pBlock = (PVDDISCARDBLOCK)RTAvlrU64GetBestFit(pDiscard->pTreeBlocks, uOffset, true);
|
---|
1341 | if (pBlock)
|
---|
1342 | cbThisRange = RT_MIN(cbThisRange, pBlock->Core.Key - uOffset);
|
---|
1343 | }
|
---|
1344 |
|
---|
1345 | Assert(cbRange >= cbThisRange);
|
---|
1346 |
|
---|
1347 | uOffset += cbThisRange;
|
---|
1348 | cbRange -= cbThisRange;
|
---|
1349 | } while (cbRange != 0);
|
---|
1350 | }
|
---|
1351 |
|
---|
1352 | return rc;
|
---|
1353 | }
|
---|
1354 |
|
---|
1355 | DECLINLINE(PVDIOCTX) vdIoCtxAlloc(PVBOXHDD pDisk, VDIOCTXTXDIR enmTxDir,
|
---|
1356 | uint64_t uOffset, size_t cbTransfer,
|
---|
1357 | PVDIMAGE pImageStart,PCRTSGBUF pcSgBuf,
|
---|
1358 | void *pvAllocation, PFNVDIOCTXTRANSFER pfnIoCtxTransfer,
|
---|
1359 | uint32_t fFlags)
|
---|
1360 | {
|
---|
1361 | PVDIOCTX pIoCtx = NULL;
|
---|
1362 |
|
---|
1363 | pIoCtx = (PVDIOCTX)RTMemCacheAlloc(pDisk->hMemCacheIoCtx);
|
---|
1364 | if (RT_LIKELY(pIoCtx))
|
---|
1365 | {
|
---|
1366 | vdIoCtxInit(pIoCtx, pDisk, enmTxDir, uOffset, cbTransfer, pImageStart,
|
---|
1367 | pcSgBuf, pvAllocation, pfnIoCtxTransfer, fFlags);
|
---|
1368 | }
|
---|
1369 |
|
---|
1370 | return pIoCtx;
|
---|
1371 | }
|
---|
1372 |
|
---|
1373 | DECLINLINE(PVDIOCTX) vdIoCtxRootAlloc(PVBOXHDD pDisk, VDIOCTXTXDIR enmTxDir,
|
---|
1374 | uint64_t uOffset, size_t cbTransfer,
|
---|
1375 | PVDIMAGE pImageStart, PCRTSGBUF pcSgBuf,
|
---|
1376 | PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
1377 | void *pvUser1, void *pvUser2,
|
---|
1378 | void *pvAllocation,
|
---|
1379 | PFNVDIOCTXTRANSFER pfnIoCtxTransfer,
|
---|
1380 | uint32_t fFlags)
|
---|
1381 | {
|
---|
1382 | PVDIOCTX pIoCtx = vdIoCtxAlloc(pDisk, enmTxDir, uOffset, cbTransfer, pImageStart,
|
---|
1383 | pcSgBuf, pvAllocation, pfnIoCtxTransfer, fFlags);
|
---|
1384 |
|
---|
1385 | if (RT_LIKELY(pIoCtx))
|
---|
1386 | {
|
---|
1387 | pIoCtx->pIoCtxParent = NULL;
|
---|
1388 | pIoCtx->Type.Root.pfnComplete = pfnComplete;
|
---|
1389 | pIoCtx->Type.Root.pvUser1 = pvUser1;
|
---|
1390 | pIoCtx->Type.Root.pvUser2 = pvUser2;
|
---|
1391 | }
|
---|
1392 |
|
---|
1393 | LogFlow(("Allocated root I/O context %#p\n", pIoCtx));
|
---|
1394 | return pIoCtx;
|
---|
1395 | }
|
---|
1396 |
|
---|
1397 | DECLINLINE(void) vdIoCtxDiscardInit(PVDIOCTX pIoCtx, PVBOXHDD pDisk, PCRTRANGE paRanges,
|
---|
1398 | unsigned cRanges, PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
1399 | void *pvUser1, void *pvUser2, void *pvAllocation,
|
---|
1400 | PFNVDIOCTXTRANSFER pfnIoCtxTransfer, uint32_t fFlags)
|
---|
1401 | {
|
---|
1402 | pIoCtx->pIoCtxNext = NULL;
|
---|
1403 | pIoCtx->pDisk = pDisk;
|
---|
1404 | pIoCtx->enmTxDir = VDIOCTXTXDIR_DISCARD;
|
---|
1405 | pIoCtx->cDataTransfersPending = 0;
|
---|
1406 | pIoCtx->cMetaTransfersPending = 0;
|
---|
1407 | pIoCtx->fComplete = false;
|
---|
1408 | pIoCtx->fFlags = fFlags;
|
---|
1409 | pIoCtx->pvAllocation = pvAllocation;
|
---|
1410 | pIoCtx->pfnIoCtxTransfer = pfnIoCtxTransfer;
|
---|
1411 | pIoCtx->pfnIoCtxTransferNext = NULL;
|
---|
1412 | pIoCtx->rcReq = VINF_SUCCESS;
|
---|
1413 | pIoCtx->Req.Discard.paRanges = paRanges;
|
---|
1414 | pIoCtx->Req.Discard.cRanges = cRanges;
|
---|
1415 | pIoCtx->Req.Discard.idxRange = 0;
|
---|
1416 | pIoCtx->Req.Discard.cbDiscardLeft = 0;
|
---|
1417 | pIoCtx->Req.Discard.offCur = 0;
|
---|
1418 | pIoCtx->Req.Discard.cbThisDiscard = 0;
|
---|
1419 |
|
---|
1420 | pIoCtx->pIoCtxParent = NULL;
|
---|
1421 | pIoCtx->Type.Root.pfnComplete = pfnComplete;
|
---|
1422 | pIoCtx->Type.Root.pvUser1 = pvUser1;
|
---|
1423 | pIoCtx->Type.Root.pvUser2 = pvUser2;
|
---|
1424 | }
|
---|
1425 |
|
---|
1426 | DECLINLINE(PVDIOCTX) vdIoCtxDiscardAlloc(PVBOXHDD pDisk, PCRTRANGE paRanges,
|
---|
1427 | unsigned cRanges,
|
---|
1428 | PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
1429 | void *pvUser1, void *pvUser2,
|
---|
1430 | void *pvAllocation,
|
---|
1431 | PFNVDIOCTXTRANSFER pfnIoCtxTransfer,
|
---|
1432 | uint32_t fFlags)
|
---|
1433 | {
|
---|
1434 | PVDIOCTX pIoCtx = NULL;
|
---|
1435 |
|
---|
1436 | pIoCtx = (PVDIOCTX)RTMemCacheAlloc(pDisk->hMemCacheIoCtx);
|
---|
1437 | if (RT_LIKELY(pIoCtx))
|
---|
1438 | {
|
---|
1439 | vdIoCtxDiscardInit(pIoCtx, pDisk, paRanges, cRanges, pfnComplete, pvUser1,
|
---|
1440 | pvUser2, pvAllocation, pfnIoCtxTransfer, fFlags);
|
---|
1441 | }
|
---|
1442 |
|
---|
1443 | LogFlow(("Allocated discard I/O context %#p\n", pIoCtx));
|
---|
1444 | return pIoCtx;
|
---|
1445 | }
|
---|
1446 |
|
---|
1447 | DECLINLINE(PVDIOCTX) vdIoCtxChildAlloc(PVBOXHDD pDisk, VDIOCTXTXDIR enmTxDir,
|
---|
1448 | uint64_t uOffset, size_t cbTransfer,
|
---|
1449 | PVDIMAGE pImageStart, PCRTSGBUF pcSgBuf,
|
---|
1450 | PVDIOCTX pIoCtxParent, size_t cbTransferParent,
|
---|
1451 | size_t cbWriteParent, void *pvAllocation,
|
---|
1452 | PFNVDIOCTXTRANSFER pfnIoCtxTransfer)
|
---|
1453 | {
|
---|
1454 | PVDIOCTX pIoCtx = vdIoCtxAlloc(pDisk, enmTxDir, uOffset, cbTransfer, pImageStart,
|
---|
1455 | pcSgBuf, pvAllocation, pfnIoCtxTransfer, pIoCtxParent->fFlags & ~VDIOCTX_FLAGS_DONT_FREE);
|
---|
1456 |
|
---|
1457 | AssertPtr(pIoCtxParent);
|
---|
1458 | Assert(!pIoCtxParent->pIoCtxParent);
|
---|
1459 |
|
---|
1460 | if (RT_LIKELY(pIoCtx))
|
---|
1461 | {
|
---|
1462 | pIoCtx->pIoCtxParent = pIoCtxParent;
|
---|
1463 | pIoCtx->Type.Child.uOffsetSaved = uOffset;
|
---|
1464 | pIoCtx->Type.Child.cbTransferLeftSaved = cbTransfer;
|
---|
1465 | pIoCtx->Type.Child.cbTransferParent = cbTransferParent;
|
---|
1466 | pIoCtx->Type.Child.cbWriteParent = cbWriteParent;
|
---|
1467 | }
|
---|
1468 |
|
---|
1469 | LogFlow(("Allocated child I/O context %#p\n", pIoCtx));
|
---|
1470 | return pIoCtx;
|
---|
1471 | }
|
---|
1472 |
|
---|
1473 | DECLINLINE(PVDIOTASK) vdIoTaskUserAlloc(PVDIOSTORAGE pIoStorage, PFNVDXFERCOMPLETED pfnComplete, void *pvUser, PVDIOCTX pIoCtx, uint32_t cbTransfer)
|
---|
1474 | {
|
---|
1475 | PVDIOTASK pIoTask = NULL;
|
---|
1476 |
|
---|
1477 | pIoTask = (PVDIOTASK)RTMemCacheAlloc(pIoStorage->pVDIo->pDisk->hMemCacheIoTask);
|
---|
1478 | if (pIoTask)
|
---|
1479 | {
|
---|
1480 | pIoTask->pIoStorage = pIoStorage;
|
---|
1481 | pIoTask->pfnComplete = pfnComplete;
|
---|
1482 | pIoTask->pvUser = pvUser;
|
---|
1483 | pIoTask->fMeta = false;
|
---|
1484 | pIoTask->Type.User.cbTransfer = cbTransfer;
|
---|
1485 | pIoTask->Type.User.pIoCtx = pIoCtx;
|
---|
1486 | }
|
---|
1487 |
|
---|
1488 | return pIoTask;
|
---|
1489 | }
|
---|
1490 |
|
---|
1491 | DECLINLINE(PVDIOTASK) vdIoTaskMetaAlloc(PVDIOSTORAGE pIoStorage, PFNVDXFERCOMPLETED pfnComplete, void *pvUser, PVDMETAXFER pMetaXfer)
|
---|
1492 | {
|
---|
1493 | PVDIOTASK pIoTask = NULL;
|
---|
1494 |
|
---|
1495 | pIoTask = (PVDIOTASK)RTMemCacheAlloc(pIoStorage->pVDIo->pDisk->hMemCacheIoTask);
|
---|
1496 | if (pIoTask)
|
---|
1497 | {
|
---|
1498 | pIoTask->pIoStorage = pIoStorage;
|
---|
1499 | pIoTask->pfnComplete = pfnComplete;
|
---|
1500 | pIoTask->pvUser = pvUser;
|
---|
1501 | pIoTask->fMeta = true;
|
---|
1502 | pIoTask->Type.Meta.pMetaXfer = pMetaXfer;
|
---|
1503 | }
|
---|
1504 |
|
---|
1505 | return pIoTask;
|
---|
1506 | }
|
---|
1507 |
|
---|
1508 | DECLINLINE(void) vdIoCtxFree(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
|
---|
1509 | {
|
---|
1510 | Log(("Freeing I/O context %#p\n", pIoCtx));
|
---|
1511 |
|
---|
1512 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_DONT_FREE))
|
---|
1513 | {
|
---|
1514 | if (pIoCtx->pvAllocation)
|
---|
1515 | RTMemFree(pIoCtx->pvAllocation);
|
---|
1516 | #ifdef DEBUG
|
---|
1517 | memset(&pIoCtx->pDisk, 0xff, sizeof(void *));
|
---|
1518 | #endif
|
---|
1519 | RTMemCacheFree(pDisk->hMemCacheIoCtx, pIoCtx);
|
---|
1520 | }
|
---|
1521 | }
|
---|
1522 |
|
---|
1523 | DECLINLINE(void) vdIoTaskFree(PVBOXHDD pDisk, PVDIOTASK pIoTask)
|
---|
1524 | {
|
---|
1525 | #ifdef DEBUG
|
---|
1526 | memset(pIoTask, 0xff, sizeof(VDIOTASK));
|
---|
1527 | #endif
|
---|
1528 | RTMemCacheFree(pDisk->hMemCacheIoTask, pIoTask);
|
---|
1529 | }
|
---|
1530 |
|
---|
1531 | DECLINLINE(void) vdIoCtxChildReset(PVDIOCTX pIoCtx)
|
---|
1532 | {
|
---|
1533 | AssertPtr(pIoCtx->pIoCtxParent);
|
---|
1534 |
|
---|
1535 | RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
|
---|
1536 | pIoCtx->Req.Io.uOffset = pIoCtx->Type.Child.uOffsetSaved;
|
---|
1537 | pIoCtx->Req.Io.cbTransferLeft = (uint32_t)pIoCtx->Type.Child.cbTransferLeftSaved;
|
---|
1538 | Assert((uint32_t)pIoCtx->Type.Child.cbTransferLeftSaved == pIoCtx->Type.Child.cbTransferLeftSaved);
|
---|
1539 | }
|
---|
1540 |
|
---|
1541 | DECLINLINE(PVDMETAXFER) vdMetaXferAlloc(PVDIOSTORAGE pIoStorage, uint64_t uOffset, size_t cb)
|
---|
1542 | {
|
---|
1543 | PVDMETAXFER pMetaXfer = (PVDMETAXFER)RTMemAlloc(RT_OFFSETOF(VDMETAXFER, abData[cb]));
|
---|
1544 |
|
---|
1545 | if (RT_LIKELY(pMetaXfer))
|
---|
1546 | {
|
---|
1547 | pMetaXfer->Core.Key = uOffset;
|
---|
1548 | pMetaXfer->Core.KeyLast = uOffset + cb - 1;
|
---|
1549 | pMetaXfer->fFlags = VDMETAXFER_TXDIR_NONE;
|
---|
1550 | pMetaXfer->cbMeta = cb;
|
---|
1551 | pMetaXfer->pIoStorage = pIoStorage;
|
---|
1552 | pMetaXfer->cRefs = 0;
|
---|
1553 | pMetaXfer->pbDataShw = NULL;
|
---|
1554 | RTListInit(&pMetaXfer->ListIoCtxWaiting);
|
---|
1555 | RTListInit(&pMetaXfer->ListIoCtxShwWrites);
|
---|
1556 | }
|
---|
1557 | return pMetaXfer;
|
---|
1558 | }
|
---|
1559 |
|
---|
1560 | DECLINLINE(void) vdIoCtxAddToWaitingList(volatile PVDIOCTX *ppList, PVDIOCTX pIoCtx)
|
---|
1561 | {
|
---|
1562 | /* Put it on the waiting list. */
|
---|
1563 | PVDIOCTX pNext = ASMAtomicUoReadPtrT(ppList, PVDIOCTX);
|
---|
1564 | PVDIOCTX pHeadOld;
|
---|
1565 | pIoCtx->pIoCtxNext = pNext;
|
---|
1566 | while (!ASMAtomicCmpXchgExPtr(ppList, pIoCtx, pNext, &pHeadOld))
|
---|
1567 | {
|
---|
1568 | pNext = pHeadOld;
|
---|
1569 | Assert(pNext != pIoCtx);
|
---|
1570 | pIoCtx->pIoCtxNext = pNext;
|
---|
1571 | ASMNopPause();
|
---|
1572 | }
|
---|
1573 | }
|
---|
1574 |
|
---|
1575 | DECLINLINE(void) vdIoCtxDefer(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
|
---|
1576 | {
|
---|
1577 | LogFlowFunc(("Deferring I/O context pIoCtx=%#p\n", pIoCtx));
|
---|
1578 |
|
---|
1579 | Assert(!pIoCtx->pIoCtxParent && !(pIoCtx->fFlags & VDIOCTX_FLAGS_BLOCKED));
|
---|
1580 | pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
|
---|
1581 | vdIoCtxAddToWaitingList(&pDisk->pIoCtxBlockedHead, pIoCtx);
|
---|
1582 | }
|
---|
1583 |
|
---|
1584 | static size_t vdIoCtxCopy(PVDIOCTX pIoCtxDst, PVDIOCTX pIoCtxSrc, size_t cbData)
|
---|
1585 | {
|
---|
1586 | return RTSgBufCopy(&pIoCtxDst->Req.Io.SgBuf, &pIoCtxSrc->Req.Io.SgBuf, cbData);
|
---|
1587 | }
|
---|
1588 |
|
---|
1589 | static int vdIoCtxCmp(PVDIOCTX pIoCtx1, PVDIOCTX pIoCtx2, size_t cbData)
|
---|
1590 | {
|
---|
1591 | return RTSgBufCmp(&pIoCtx1->Req.Io.SgBuf, &pIoCtx2->Req.Io.SgBuf, cbData);
|
---|
1592 | }
|
---|
1593 |
|
---|
1594 | static size_t vdIoCtxCopyTo(PVDIOCTX pIoCtx, const uint8_t *pbData, size_t cbData)
|
---|
1595 | {
|
---|
1596 | return RTSgBufCopyFromBuf(&pIoCtx->Req.Io.SgBuf, pbData, cbData);
|
---|
1597 | }
|
---|
1598 |
|
---|
1599 | static size_t vdIoCtxCopyFrom(PVDIOCTX pIoCtx, uint8_t *pbData, size_t cbData)
|
---|
1600 | {
|
---|
1601 | return RTSgBufCopyToBuf(&pIoCtx->Req.Io.SgBuf, pbData, cbData);
|
---|
1602 | }
|
---|
1603 |
|
---|
1604 | static size_t vdIoCtxSet(PVDIOCTX pIoCtx, uint8_t ch, size_t cbData)
|
---|
1605 | {
|
---|
1606 | return RTSgBufSet(&pIoCtx->Req.Io.SgBuf, ch, cbData);
|
---|
1607 | }
|
---|
1608 |
|
---|
1609 | /**
|
---|
1610 | * Process the I/O context, core method which assumes that the I/O context
|
---|
1611 | * acquired the lock.
|
---|
1612 | *
|
---|
1613 | * @returns VBox status code.
|
---|
1614 | * @param pIoCtx I/O context to process.
|
---|
1615 | */
|
---|
1616 | static int vdIoCtxProcessLocked(PVDIOCTX pIoCtx)
|
---|
1617 | {
|
---|
1618 | int rc = VINF_SUCCESS;
|
---|
1619 |
|
---|
1620 | VD_IS_LOCKED(pIoCtx->pDisk);
|
---|
1621 |
|
---|
1622 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
1623 |
|
---|
1624 | if ( !pIoCtx->cMetaTransfersPending
|
---|
1625 | && !pIoCtx->cDataTransfersPending
|
---|
1626 | && !pIoCtx->pfnIoCtxTransfer)
|
---|
1627 | {
|
---|
1628 | rc = VINF_VD_ASYNC_IO_FINISHED;
|
---|
1629 | goto out;
|
---|
1630 | }
|
---|
1631 |
|
---|
1632 | /*
|
---|
1633 | * We complete the I/O context in case of an error
|
---|
1634 | * if there is no I/O task pending.
|
---|
1635 | */
|
---|
1636 | if ( RT_FAILURE(pIoCtx->rcReq)
|
---|
1637 | && !pIoCtx->cMetaTransfersPending
|
---|
1638 | && !pIoCtx->cDataTransfersPending)
|
---|
1639 | {
|
---|
1640 | rc = VINF_VD_ASYNC_IO_FINISHED;
|
---|
1641 | goto out;
|
---|
1642 | }
|
---|
1643 |
|
---|
1644 | /* Don't change anything if there is a metadata transfer pending or we are blocked. */
|
---|
1645 | if ( pIoCtx->cMetaTransfersPending
|
---|
1646 | || (pIoCtx->fFlags & VDIOCTX_FLAGS_BLOCKED))
|
---|
1647 | {
|
---|
1648 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1649 | goto out;
|
---|
1650 | }
|
---|
1651 |
|
---|
1652 | if (pIoCtx->pfnIoCtxTransfer)
|
---|
1653 | {
|
---|
1654 | /* Call the transfer function advancing to the next while there is no error. */
|
---|
1655 | while ( pIoCtx->pfnIoCtxTransfer
|
---|
1656 | && !pIoCtx->cMetaTransfersPending
|
---|
1657 | && RT_SUCCESS(rc))
|
---|
1658 | {
|
---|
1659 | LogFlowFunc(("calling transfer function %#p\n", pIoCtx->pfnIoCtxTransfer));
|
---|
1660 | rc = pIoCtx->pfnIoCtxTransfer(pIoCtx);
|
---|
1661 |
|
---|
1662 | /* Advance to the next part of the transfer if the current one succeeded. */
|
---|
1663 | if (RT_SUCCESS(rc))
|
---|
1664 | {
|
---|
1665 | pIoCtx->pfnIoCtxTransfer = pIoCtx->pfnIoCtxTransferNext;
|
---|
1666 | pIoCtx->pfnIoCtxTransferNext = NULL;
|
---|
1667 | }
|
---|
1668 | }
|
---|
1669 | }
|
---|
1670 |
|
---|
1671 | if ( RT_SUCCESS(rc)
|
---|
1672 | && !pIoCtx->cMetaTransfersPending
|
---|
1673 | && !pIoCtx->cDataTransfersPending
|
---|
1674 | && !(pIoCtx->fFlags & VDIOCTX_FLAGS_BLOCKED))
|
---|
1675 | rc = VINF_VD_ASYNC_IO_FINISHED;
|
---|
1676 | else if ( RT_SUCCESS(rc)
|
---|
1677 | || rc == VERR_VD_NOT_ENOUGH_METADATA
|
---|
1678 | || rc == VERR_VD_IOCTX_HALT)
|
---|
1679 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1680 | else if ( RT_FAILURE(rc)
|
---|
1681 | && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
1682 | {
|
---|
1683 | ASMAtomicCmpXchgS32(&pIoCtx->rcReq, rc, VINF_SUCCESS);
|
---|
1684 |
|
---|
1685 | /*
|
---|
1686 | * The I/O context completed if we have an error and there is no data
|
---|
1687 | * or meta data transfer pending.
|
---|
1688 | */
|
---|
1689 | if ( !pIoCtx->cMetaTransfersPending
|
---|
1690 | && !pIoCtx->cDataTransfersPending)
|
---|
1691 | rc = VINF_VD_ASYNC_IO_FINISHED;
|
---|
1692 | else
|
---|
1693 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1694 | }
|
---|
1695 |
|
---|
1696 | out:
|
---|
1697 | LogFlowFunc(("pIoCtx=%#p rc=%Rrc cDataTransfersPending=%u cMetaTransfersPending=%u fComplete=%RTbool\n",
|
---|
1698 | pIoCtx, rc, pIoCtx->cDataTransfersPending, pIoCtx->cMetaTransfersPending,
|
---|
1699 | pIoCtx->fComplete));
|
---|
1700 |
|
---|
1701 | return rc;
|
---|
1702 | }
|
---|
1703 |
|
---|
1704 | /**
|
---|
1705 | * Processes the list of waiting I/O contexts.
|
---|
1706 | *
|
---|
1707 | * @returns VBox status code, only valid if pIoCtxRc is not NULL, treat as void
|
---|
1708 | * function otherwise.
|
---|
1709 | * @param pDisk The disk structure.
|
---|
1710 | * @param pIoCtxRc An I/O context handle which waits on the list. When processed
|
---|
1711 | * The status code is returned. NULL if there is no I/O context
|
---|
1712 | * to return the status code for.
|
---|
1713 | */
|
---|
1714 | static int vdDiskProcessWaitingIoCtx(PVBOXHDD pDisk, PVDIOCTX pIoCtxRc)
|
---|
1715 | {
|
---|
1716 | int rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1717 |
|
---|
1718 | LogFlowFunc(("pDisk=%#p pIoCtxRc=%#p\n", pDisk, pIoCtxRc));
|
---|
1719 |
|
---|
1720 | VD_IS_LOCKED(pDisk);
|
---|
1721 |
|
---|
1722 | /* Get the waiting list and process it in FIFO order. */
|
---|
1723 | PVDIOCTX pIoCtxHead = ASMAtomicXchgPtrT(&pDisk->pIoCtxHead, NULL, PVDIOCTX);
|
---|
1724 |
|
---|
1725 | /* Reverse it. */
|
---|
1726 | PVDIOCTX pCur = pIoCtxHead;
|
---|
1727 | pIoCtxHead = NULL;
|
---|
1728 | while (pCur)
|
---|
1729 | {
|
---|
1730 | PVDIOCTX pInsert = pCur;
|
---|
1731 | pCur = pCur->pIoCtxNext;
|
---|
1732 | pInsert->pIoCtxNext = pIoCtxHead;
|
---|
1733 | pIoCtxHead = pInsert;
|
---|
1734 | }
|
---|
1735 |
|
---|
1736 | /* Process now. */
|
---|
1737 | pCur = pIoCtxHead;
|
---|
1738 | while (pCur)
|
---|
1739 | {
|
---|
1740 | int rcTmp;
|
---|
1741 | PVDIOCTX pTmp = pCur;
|
---|
1742 |
|
---|
1743 | pCur = pCur->pIoCtxNext;
|
---|
1744 | pTmp->pIoCtxNext = NULL;
|
---|
1745 |
|
---|
1746 | /*
|
---|
1747 | * Need to clear the sync flag here if there is a new I/O context
|
---|
1748 | * with it set and the context is not given in pIoCtxRc.
|
---|
1749 | * This happens most likely on a different thread and that one shouldn't
|
---|
1750 | * process the context synchronously.
|
---|
1751 | *
|
---|
1752 | * The thread who issued the context will wait on the event semaphore
|
---|
1753 | * anyway which is signalled when the completion handler is called.
|
---|
1754 | */
|
---|
1755 | if ( pTmp->fFlags & VDIOCTX_FLAGS_SYNC
|
---|
1756 | && pTmp != pIoCtxRc)
|
---|
1757 | pTmp->fFlags &= ~VDIOCTX_FLAGS_SYNC;
|
---|
1758 |
|
---|
1759 | rcTmp = vdIoCtxProcessLocked(pTmp);
|
---|
1760 | if (pTmp == pIoCtxRc)
|
---|
1761 | {
|
---|
1762 | if ( rcTmp == VINF_VD_ASYNC_IO_FINISHED
|
---|
1763 | && RT_SUCCESS(pTmp->rcReq)
|
---|
1764 | && pTmp->enmTxDir == VDIOCTXTXDIR_READ)
|
---|
1765 | {
|
---|
1766 | int rc2 = vdFilterChainApplyRead(pDisk, pTmp->Req.Io.uOffsetXferOrig,
|
---|
1767 | pTmp->Req.Io.cbXferOrig, pTmp);
|
---|
1768 | if (RT_FAILURE(rc2))
|
---|
1769 | rcTmp = rc2;
|
---|
1770 | }
|
---|
1771 |
|
---|
1772 | /* The given I/O context was processed, pass the return code to the caller. */
|
---|
1773 | if ( rcTmp == VINF_VD_ASYNC_IO_FINISHED
|
---|
1774 | && (pTmp->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
1775 | rc = pTmp->rcReq;
|
---|
1776 | else
|
---|
1777 | rc = rcTmp;
|
---|
1778 | }
|
---|
1779 | else if ( rcTmp == VINF_VD_ASYNC_IO_FINISHED
|
---|
1780 | && ASMAtomicCmpXchgBool(&pTmp->fComplete, true, false))
|
---|
1781 | {
|
---|
1782 | LogFlowFunc(("Waiting I/O context completed pTmp=%#p\n", pTmp));
|
---|
1783 | vdThreadFinishWrite(pDisk);
|
---|
1784 | vdIoCtxRootComplete(pDisk, pTmp);
|
---|
1785 | vdIoCtxFree(pDisk, pTmp);
|
---|
1786 | }
|
---|
1787 | }
|
---|
1788 |
|
---|
1789 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
1790 | return rc;
|
---|
1791 | }
|
---|
1792 |
|
---|
1793 | /**
|
---|
1794 | * Processes the list of blocked I/O contexts.
|
---|
1795 | *
|
---|
1796 | * @returns nothing.
|
---|
1797 | * @param pDisk The disk structure.
|
---|
1798 | */
|
---|
1799 | static void vdDiskProcessBlockedIoCtx(PVBOXHDD pDisk)
|
---|
1800 | {
|
---|
1801 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
1802 |
|
---|
1803 | VD_IS_LOCKED(pDisk);
|
---|
1804 |
|
---|
1805 | /* Get the waiting list and process it in FIFO order. */
|
---|
1806 | PVDIOCTX pIoCtxHead = ASMAtomicXchgPtrT(&pDisk->pIoCtxBlockedHead, NULL, PVDIOCTX);
|
---|
1807 |
|
---|
1808 | /* Reverse it. */
|
---|
1809 | PVDIOCTX pCur = pIoCtxHead;
|
---|
1810 | pIoCtxHead = NULL;
|
---|
1811 | while (pCur)
|
---|
1812 | {
|
---|
1813 | PVDIOCTX pInsert = pCur;
|
---|
1814 | pCur = pCur->pIoCtxNext;
|
---|
1815 | pInsert->pIoCtxNext = pIoCtxHead;
|
---|
1816 | pIoCtxHead = pInsert;
|
---|
1817 | }
|
---|
1818 |
|
---|
1819 | /* Process now. */
|
---|
1820 | pCur = pIoCtxHead;
|
---|
1821 | while (pCur)
|
---|
1822 | {
|
---|
1823 | int rc;
|
---|
1824 | PVDIOCTX pTmp = pCur;
|
---|
1825 |
|
---|
1826 | pCur = pCur->pIoCtxNext;
|
---|
1827 | pTmp->pIoCtxNext = NULL;
|
---|
1828 |
|
---|
1829 | Assert(!pTmp->pIoCtxParent);
|
---|
1830 | Assert(pTmp->fFlags & VDIOCTX_FLAGS_BLOCKED);
|
---|
1831 | pTmp->fFlags &= ~VDIOCTX_FLAGS_BLOCKED;
|
---|
1832 |
|
---|
1833 | rc = vdIoCtxProcessLocked(pTmp);
|
---|
1834 | if ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
1835 | && ASMAtomicCmpXchgBool(&pTmp->fComplete, true, false))
|
---|
1836 | {
|
---|
1837 | LogFlowFunc(("Waiting I/O context completed pTmp=%#p\n", pTmp));
|
---|
1838 | vdThreadFinishWrite(pDisk);
|
---|
1839 | vdIoCtxRootComplete(pDisk, pTmp);
|
---|
1840 | vdIoCtxFree(pDisk, pTmp);
|
---|
1841 | }
|
---|
1842 | }
|
---|
1843 |
|
---|
1844 | LogFlowFunc(("returns\n"));
|
---|
1845 | }
|
---|
1846 |
|
---|
1847 | /**
|
---|
1848 | * Processes the I/O context trying to lock the criticial section.
|
---|
1849 | * The context is deferred if the critical section is busy.
|
---|
1850 | *
|
---|
1851 | * @returns VBox status code.
|
---|
1852 | * @param pIoCtx The I/O context to process.
|
---|
1853 | */
|
---|
1854 | static int vdIoCtxProcessTryLockDefer(PVDIOCTX pIoCtx)
|
---|
1855 | {
|
---|
1856 | int rc = VINF_SUCCESS;
|
---|
1857 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
1858 |
|
---|
1859 | Log(("Defer pIoCtx=%#p\n", pIoCtx));
|
---|
1860 |
|
---|
1861 | /* Put it on the waiting list first. */
|
---|
1862 | vdIoCtxAddToWaitingList(&pDisk->pIoCtxHead, pIoCtx);
|
---|
1863 |
|
---|
1864 | if (ASMAtomicCmpXchgBool(&pDisk->fLocked, true, false))
|
---|
1865 | {
|
---|
1866 | /* Leave it again, the context will be processed just before leaving the lock. */
|
---|
1867 | LogFlowFunc(("Successfully acquired the lock\n"));
|
---|
1868 | rc = vdDiskUnlock(pDisk, pIoCtx);
|
---|
1869 | }
|
---|
1870 | else
|
---|
1871 | {
|
---|
1872 | LogFlowFunc(("Lock is held\n"));
|
---|
1873 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1874 | }
|
---|
1875 |
|
---|
1876 | return rc;
|
---|
1877 | }
|
---|
1878 |
|
---|
1879 | /**
|
---|
1880 | * Process the I/O context in a synchronous manner, waiting
|
---|
1881 | * for it to complete.
|
---|
1882 | *
|
---|
1883 | * @returns VBox status code of the completed request.
|
---|
1884 | * @param pIoCtx The sync I/O context.
|
---|
1885 | * @param hEventComplete Event sempahore to wait on for completion.
|
---|
1886 | */
|
---|
1887 | static int vdIoCtxProcessSync(PVDIOCTX pIoCtx, RTSEMEVENT hEventComplete)
|
---|
1888 | {
|
---|
1889 | int rc = VINF_SUCCESS;
|
---|
1890 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
1891 |
|
---|
1892 | LogFlowFunc(("pIoCtx=%p\n", pIoCtx));
|
---|
1893 |
|
---|
1894 | AssertMsg(pIoCtx->fFlags & (VDIOCTX_FLAGS_SYNC | VDIOCTX_FLAGS_DONT_FREE),
|
---|
1895 | ("I/O context is not marked as synchronous\n"));
|
---|
1896 |
|
---|
1897 | rc = vdIoCtxProcessTryLockDefer(pIoCtx);
|
---|
1898 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
1899 | rc = VINF_SUCCESS;
|
---|
1900 |
|
---|
1901 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
1902 | {
|
---|
1903 | rc = RTSemEventWait(hEventComplete, RT_INDEFINITE_WAIT);
|
---|
1904 | AssertRC(rc);
|
---|
1905 | }
|
---|
1906 |
|
---|
1907 | rc = pIoCtx->rcReq;
|
---|
1908 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
1909 |
|
---|
1910 | return rc;
|
---|
1911 | }
|
---|
1912 |
|
---|
1913 | DECLINLINE(bool) vdIoCtxIsDiskLockOwner(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
|
---|
1914 | {
|
---|
1915 | return pDisk->pIoCtxLockOwner == pIoCtx;
|
---|
1916 | }
|
---|
1917 |
|
---|
1918 | static int vdIoCtxLockDisk(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
|
---|
1919 | {
|
---|
1920 | int rc = VINF_SUCCESS;
|
---|
1921 |
|
---|
1922 | VD_IS_LOCKED(pDisk);
|
---|
1923 |
|
---|
1924 | LogFlowFunc(("pDisk=%#p pIoCtx=%#p\n", pDisk, pIoCtx));
|
---|
1925 |
|
---|
1926 | if (!ASMAtomicCmpXchgPtr(&pDisk->pIoCtxLockOwner, pIoCtx, NIL_VDIOCTX))
|
---|
1927 | {
|
---|
1928 | Assert(pDisk->pIoCtxLockOwner != pIoCtx); /* No nesting allowed. */
|
---|
1929 | vdIoCtxDefer(pDisk, pIoCtx);
|
---|
1930 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1931 | }
|
---|
1932 |
|
---|
1933 | LogFlowFunc(("returns -> %Rrc\n", rc));
|
---|
1934 | return rc;
|
---|
1935 | }
|
---|
1936 |
|
---|
1937 | static void vdIoCtxUnlockDisk(PVBOXHDD pDisk, PVDIOCTX pIoCtx, bool fProcessBlockedReqs)
|
---|
1938 | {
|
---|
1939 | LogFlowFunc(("pDisk=%#p pIoCtx=%#p fProcessBlockedReqs=%RTbool\n",
|
---|
1940 | pDisk, pIoCtx, fProcessBlockedReqs));
|
---|
1941 |
|
---|
1942 | VD_IS_LOCKED(pDisk);
|
---|
1943 |
|
---|
1944 | LogFlow(("Unlocking disk lock owner is %#p\n", pDisk->pIoCtxLockOwner));
|
---|
1945 | Assert(pDisk->pIoCtxLockOwner == pIoCtx);
|
---|
1946 | ASMAtomicXchgPtrT(&pDisk->pIoCtxLockOwner, NIL_VDIOCTX, PVDIOCTX);
|
---|
1947 |
|
---|
1948 | if (fProcessBlockedReqs)
|
---|
1949 | {
|
---|
1950 | /* Process any blocked writes if the current request didn't caused another growing. */
|
---|
1951 | vdDiskProcessBlockedIoCtx(pDisk);
|
---|
1952 | }
|
---|
1953 |
|
---|
1954 | LogFlowFunc(("returns\n"));
|
---|
1955 | }
|
---|
1956 |
|
---|
1957 | /**
|
---|
1958 | * Internal: Reads a given amount of data from the image chain of the disk.
|
---|
1959 | **/
|
---|
1960 | static int vdDiskReadHelper(PVBOXHDD pDisk, PVDIMAGE pImage, PVDIMAGE pImageParentOverride,
|
---|
1961 | uint64_t uOffset, size_t cbRead, PVDIOCTX pIoCtx, size_t *pcbThisRead)
|
---|
1962 | {
|
---|
1963 | int rc = VINF_SUCCESS;
|
---|
1964 | size_t cbThisRead = cbRead;
|
---|
1965 |
|
---|
1966 | AssertPtr(pcbThisRead);
|
---|
1967 |
|
---|
1968 | *pcbThisRead = 0;
|
---|
1969 |
|
---|
1970 | /*
|
---|
1971 | * Try to read from the given image.
|
---|
1972 | * If the block is not allocated read from override chain if present.
|
---|
1973 | */
|
---|
1974 | rc = pImage->Backend->pfnRead(pImage->pBackendData,
|
---|
1975 | uOffset, cbThisRead, pIoCtx,
|
---|
1976 | &cbThisRead);
|
---|
1977 |
|
---|
1978 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
1979 | {
|
---|
1980 | for (PVDIMAGE pCurrImage = pImageParentOverride ? pImageParentOverride : pImage->pPrev;
|
---|
1981 | pCurrImage != NULL && rc == VERR_VD_BLOCK_FREE;
|
---|
1982 | pCurrImage = pCurrImage->pPrev)
|
---|
1983 | {
|
---|
1984 | rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
|
---|
1985 | uOffset, cbThisRead, pIoCtx,
|
---|
1986 | &cbThisRead);
|
---|
1987 | }
|
---|
1988 | }
|
---|
1989 |
|
---|
1990 | if (RT_SUCCESS(rc) || rc == VERR_VD_BLOCK_FREE)
|
---|
1991 | *pcbThisRead = cbThisRead;
|
---|
1992 |
|
---|
1993 | return rc;
|
---|
1994 | }
|
---|
1995 |
|
---|
1996 | /**
|
---|
1997 | * internal: read the specified amount of data in whatever blocks the backend
|
---|
1998 | * will give us - async version.
|
---|
1999 | */
|
---|
2000 | static int vdReadHelperAsync(PVDIOCTX pIoCtx)
|
---|
2001 | {
|
---|
2002 | int rc;
|
---|
2003 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
2004 | size_t cbToRead = pIoCtx->Req.Io.cbTransfer;
|
---|
2005 | uint64_t uOffset = pIoCtx->Req.Io.uOffset;
|
---|
2006 | PVDIMAGE pCurrImage = pIoCtx->Req.Io.pImageCur;
|
---|
2007 | PVDIMAGE pImageParentOverride = pIoCtx->Req.Io.pImageParentOverride;
|
---|
2008 | unsigned cImagesRead = pIoCtx->Req.Io.cImagesRead;
|
---|
2009 | size_t cbThisRead;
|
---|
2010 |
|
---|
2011 | /*
|
---|
2012 | * Check whether there is a full block write in progress which was not allocated.
|
---|
2013 | * Defer I/O if the range interferes but only if it does not belong to the
|
---|
2014 | * write doing the allocation.
|
---|
2015 | */
|
---|
2016 | if ( pDisk->pIoCtxLockOwner != NIL_VDIOCTX
|
---|
2017 | && uOffset >= pDisk->uOffsetStartLocked
|
---|
2018 | && uOffset < pDisk->uOffsetEndLocked
|
---|
2019 | && ( !pIoCtx->pIoCtxParent
|
---|
2020 | || pIoCtx->pIoCtxParent != pDisk->pIoCtxLockOwner))
|
---|
2021 | {
|
---|
2022 | Log(("Interferring read while allocating a new block => deferring read\n"));
|
---|
2023 | vdIoCtxDefer(pDisk, pIoCtx);
|
---|
2024 | return VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
2025 | }
|
---|
2026 |
|
---|
2027 | /* Loop until all reads started or we have a backend which needs to read metadata. */
|
---|
2028 | do
|
---|
2029 | {
|
---|
2030 | /* Search for image with allocated block. Do not attempt to read more
|
---|
2031 | * than the previous reads marked as valid. Otherwise this would return
|
---|
2032 | * stale data when different block sizes are used for the images. */
|
---|
2033 | cbThisRead = cbToRead;
|
---|
2034 |
|
---|
2035 | if ( pDisk->pCache
|
---|
2036 | && !pImageParentOverride)
|
---|
2037 | {
|
---|
2038 | rc = vdCacheReadHelper(pDisk->pCache, uOffset, cbThisRead,
|
---|
2039 | pIoCtx, &cbThisRead);
|
---|
2040 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
2041 | {
|
---|
2042 | rc = vdDiskReadHelper(pDisk, pCurrImage, NULL, uOffset, cbThisRead,
|
---|
2043 | pIoCtx, &cbThisRead);
|
---|
2044 |
|
---|
2045 | /* If the read was successful, write the data back into the cache. */
|
---|
2046 | if ( RT_SUCCESS(rc)
|
---|
2047 | && pIoCtx->fFlags & VDIOCTX_FLAGS_READ_UPDATE_CACHE)
|
---|
2048 | {
|
---|
2049 | rc = vdCacheWriteHelper(pDisk->pCache, uOffset, cbThisRead,
|
---|
2050 | pIoCtx, NULL);
|
---|
2051 | }
|
---|
2052 | }
|
---|
2053 | }
|
---|
2054 | else
|
---|
2055 | {
|
---|
2056 | /*
|
---|
2057 | * Try to read from the given image.
|
---|
2058 | * If the block is not allocated read from override chain if present.
|
---|
2059 | */
|
---|
2060 | rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
|
---|
2061 | uOffset, cbThisRead, pIoCtx,
|
---|
2062 | &cbThisRead);
|
---|
2063 |
|
---|
2064 | if ( rc == VERR_VD_BLOCK_FREE
|
---|
2065 | && cImagesRead != 1)
|
---|
2066 | {
|
---|
2067 | unsigned cImagesToProcess = cImagesRead;
|
---|
2068 |
|
---|
2069 | pCurrImage = pImageParentOverride ? pImageParentOverride : pCurrImage->pPrev;
|
---|
2070 | pIoCtx->Req.Io.pImageParentOverride = NULL;
|
---|
2071 |
|
---|
2072 | while (pCurrImage && rc == VERR_VD_BLOCK_FREE)
|
---|
2073 | {
|
---|
2074 | rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
|
---|
2075 | uOffset, cbThisRead,
|
---|
2076 | pIoCtx, &cbThisRead);
|
---|
2077 | if (cImagesToProcess == 1)
|
---|
2078 | break;
|
---|
2079 | else if (cImagesToProcess > 0)
|
---|
2080 | cImagesToProcess--;
|
---|
2081 |
|
---|
2082 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
2083 | pCurrImage = pCurrImage->pPrev;
|
---|
2084 | }
|
---|
2085 | }
|
---|
2086 | }
|
---|
2087 |
|
---|
2088 | /* The task state will be updated on success already, don't do it here!. */
|
---|
2089 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
2090 | {
|
---|
2091 | /* No image in the chain contains the data for the block. */
|
---|
2092 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbThisRead); Assert(cbThisRead == (uint32_t)cbThisRead);
|
---|
2093 |
|
---|
2094 | /* Fill the free space with 0 if we are told to do so
|
---|
2095 | * or a previous read returned valid data. */
|
---|
2096 | if (pIoCtx->fFlags & VDIOCTX_FLAGS_ZERO_FREE_BLOCKS)
|
---|
2097 | vdIoCtxSet(pIoCtx, '\0', cbThisRead);
|
---|
2098 | else
|
---|
2099 | pIoCtx->Req.Io.cbBufClear += cbThisRead;
|
---|
2100 |
|
---|
2101 | if (pIoCtx->Req.Io.pImageCur->uOpenFlags & VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS)
|
---|
2102 | rc = VINF_VD_NEW_ZEROED_BLOCK;
|
---|
2103 | else
|
---|
2104 | rc = VINF_SUCCESS;
|
---|
2105 | }
|
---|
2106 | else if (rc == VERR_VD_IOCTX_HALT)
|
---|
2107 | {
|
---|
2108 | uOffset += cbThisRead;
|
---|
2109 | cbToRead -= cbThisRead;
|
---|
2110 | pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
|
---|
2111 | }
|
---|
2112 | else if ( RT_SUCCESS(rc)
|
---|
2113 | || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
2114 | {
|
---|
2115 | /* First not free block, fill the space before with 0. */
|
---|
2116 | if ( pIoCtx->Req.Io.cbBufClear
|
---|
2117 | && !(pIoCtx->fFlags & VDIOCTX_FLAGS_ZERO_FREE_BLOCKS))
|
---|
2118 | {
|
---|
2119 | RTSGBUF SgBuf;
|
---|
2120 | RTSgBufClone(&SgBuf, &pIoCtx->Req.Io.SgBuf);
|
---|
2121 | RTSgBufReset(&SgBuf);
|
---|
2122 | RTSgBufSet(&SgBuf, 0, pIoCtx->Req.Io.cbBufClear);
|
---|
2123 | pIoCtx->Req.Io.cbBufClear = 0;
|
---|
2124 | pIoCtx->fFlags |= VDIOCTX_FLAGS_ZERO_FREE_BLOCKS;
|
---|
2125 | }
|
---|
2126 | rc = VINF_SUCCESS;
|
---|
2127 | }
|
---|
2128 |
|
---|
2129 | if (RT_FAILURE(rc))
|
---|
2130 | break;
|
---|
2131 |
|
---|
2132 | cbToRead -= cbThisRead;
|
---|
2133 | uOffset += cbThisRead;
|
---|
2134 | pCurrImage = pIoCtx->Req.Io.pImageStart; /* Start with the highest image in the chain. */
|
---|
2135 | } while (cbToRead != 0 && RT_SUCCESS(rc));
|
---|
2136 |
|
---|
2137 | if ( rc == VERR_VD_NOT_ENOUGH_METADATA
|
---|
2138 | || rc == VERR_VD_IOCTX_HALT)
|
---|
2139 | {
|
---|
2140 | /* Save the current state. */
|
---|
2141 | pIoCtx->Req.Io.uOffset = uOffset;
|
---|
2142 | pIoCtx->Req.Io.cbTransfer = cbToRead;
|
---|
2143 | pIoCtx->Req.Io.pImageCur = pCurrImage ? pCurrImage : pIoCtx->Req.Io.pImageStart;
|
---|
2144 | }
|
---|
2145 |
|
---|
2146 | return (!(pIoCtx->fFlags & VDIOCTX_FLAGS_ZERO_FREE_BLOCKS))
|
---|
2147 | ? VERR_VD_BLOCK_FREE
|
---|
2148 | : rc;
|
---|
2149 | }
|
---|
2150 |
|
---|
2151 | /**
|
---|
2152 | * internal: parent image read wrapper for compacting.
|
---|
2153 | */
|
---|
2154 | static int vdParentRead(void *pvUser, uint64_t uOffset, void *pvBuf,
|
---|
2155 | size_t cbRead)
|
---|
2156 | {
|
---|
2157 | PVDPARENTSTATEDESC pParentState = (PVDPARENTSTATEDESC)pvUser;
|
---|
2158 |
|
---|
2159 | /** @todo
|
---|
2160 | * Only used for compaction so far which is not possible to mix with async I/O.
|
---|
2161 | * Needs to be changed if we want to support online compaction of images.
|
---|
2162 | */
|
---|
2163 | bool fLocked = ASMAtomicXchgBool(&pParentState->pDisk->fLocked, true);
|
---|
2164 | AssertMsgReturn(!fLocked,
|
---|
2165 | ("Calling synchronous parent read while another thread holds the disk lock\n"),
|
---|
2166 | VERR_VD_INVALID_STATE);
|
---|
2167 |
|
---|
2168 | /* Fake an I/O context. */
|
---|
2169 | RTSGSEG Segment;
|
---|
2170 | RTSGBUF SgBuf;
|
---|
2171 | VDIOCTX IoCtx;
|
---|
2172 |
|
---|
2173 | Segment.pvSeg = pvBuf;
|
---|
2174 | Segment.cbSeg = cbRead;
|
---|
2175 | RTSgBufInit(&SgBuf, &Segment, 1);
|
---|
2176 | vdIoCtxInit(&IoCtx, pParentState->pDisk, VDIOCTXTXDIR_READ, uOffset, cbRead, pParentState->pImage,
|
---|
2177 | &SgBuf, NULL, NULL, VDIOCTX_FLAGS_SYNC | VDIOCTX_FLAGS_ZERO_FREE_BLOCKS);
|
---|
2178 | int rc = vdReadHelperAsync(&IoCtx);
|
---|
2179 | ASMAtomicXchgBool(&pParentState->pDisk->fLocked, false);
|
---|
2180 | return rc;
|
---|
2181 | }
|
---|
2182 |
|
---|
2183 | /**
|
---|
2184 | * Extended version of vdReadHelper(), implementing certain optimizations
|
---|
2185 | * for image cloning.
|
---|
2186 | *
|
---|
2187 | * @returns VBox status code.
|
---|
2188 | * @param pDisk The disk to read from.
|
---|
2189 | * @param pImage The image to start reading from.
|
---|
2190 | * @param pImageParentOverride The parent image to read from
|
---|
2191 | * if the starting image returns a free block.
|
---|
2192 | * If NULL is passed the real parent of the image
|
---|
2193 | * in the chain is used.
|
---|
2194 | * @param uOffset Offset in the disk to start reading from.
|
---|
2195 | * @param pvBuf Where to store the read data.
|
---|
2196 | * @param cbRead How much to read.
|
---|
2197 | * @param fZeroFreeBlocks Flag whether free blocks should be zeroed.
|
---|
2198 | * If false and no image has data for sepcified
|
---|
2199 | * range VERR_VD_BLOCK_FREE is returned.
|
---|
2200 | * Note that unallocated blocks are still zeroed
|
---|
2201 | * if at least one image has valid data for a part
|
---|
2202 | * of the range.
|
---|
2203 | * @param fUpdateCache Flag whether to update the attached cache if
|
---|
2204 | * available.
|
---|
2205 | * @param cImagesRead Number of images in the chain to read until
|
---|
2206 | * the read is cut off. A value of 0 disables the cut off.
|
---|
2207 | */
|
---|
2208 | static int vdReadHelperEx(PVBOXHDD pDisk, PVDIMAGE pImage, PVDIMAGE pImageParentOverride,
|
---|
2209 | uint64_t uOffset, void *pvBuf, size_t cbRead,
|
---|
2210 | bool fZeroFreeBlocks, bool fUpdateCache, unsigned cImagesRead)
|
---|
2211 | {
|
---|
2212 | int rc = VINF_SUCCESS;
|
---|
2213 | uint32_t fFlags = VDIOCTX_FLAGS_SYNC | VDIOCTX_FLAGS_DONT_FREE;
|
---|
2214 | RTSGSEG Segment;
|
---|
2215 | RTSGBUF SgBuf;
|
---|
2216 | VDIOCTX IoCtx;
|
---|
2217 | RTSEMEVENT hEventComplete = NIL_RTSEMEVENT;
|
---|
2218 |
|
---|
2219 | rc = RTSemEventCreate(&hEventComplete);
|
---|
2220 | if (RT_FAILURE(rc))
|
---|
2221 | return rc;
|
---|
2222 |
|
---|
2223 | if (fZeroFreeBlocks)
|
---|
2224 | fFlags |= VDIOCTX_FLAGS_ZERO_FREE_BLOCKS;
|
---|
2225 | if (fUpdateCache)
|
---|
2226 | fFlags |= VDIOCTX_FLAGS_READ_UPDATE_CACHE;
|
---|
2227 |
|
---|
2228 | Segment.pvSeg = pvBuf;
|
---|
2229 | Segment.cbSeg = cbRead;
|
---|
2230 | RTSgBufInit(&SgBuf, &Segment, 1);
|
---|
2231 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_READ, uOffset, cbRead, pImage, &SgBuf,
|
---|
2232 | NULL, vdReadHelperAsync, fFlags);
|
---|
2233 |
|
---|
2234 | IoCtx.Req.Io.pImageParentOverride = pImageParentOverride;
|
---|
2235 | IoCtx.Req.Io.cImagesRead = cImagesRead;
|
---|
2236 | IoCtx.Type.Root.pfnComplete = vdIoCtxSyncComplete;
|
---|
2237 | IoCtx.Type.Root.pvUser1 = pDisk;
|
---|
2238 | IoCtx.Type.Root.pvUser2 = hEventComplete;
|
---|
2239 | rc = vdIoCtxProcessSync(&IoCtx, hEventComplete);
|
---|
2240 | RTSemEventDestroy(hEventComplete);
|
---|
2241 | return rc;
|
---|
2242 | }
|
---|
2243 |
|
---|
2244 | /**
|
---|
2245 | * internal: read the specified amount of data in whatever blocks the backend
|
---|
2246 | * will give us.
|
---|
2247 | */
|
---|
2248 | static int vdReadHelper(PVBOXHDD pDisk, PVDIMAGE pImage, uint64_t uOffset,
|
---|
2249 | void *pvBuf, size_t cbRead, bool fUpdateCache)
|
---|
2250 | {
|
---|
2251 | return vdReadHelperEx(pDisk, pImage, NULL, uOffset, pvBuf, cbRead,
|
---|
2252 | true /* fZeroFreeBlocks */, fUpdateCache, 0);
|
---|
2253 | }
|
---|
2254 |
|
---|
2255 | /**
|
---|
2256 | * internal: mark the disk as not modified.
|
---|
2257 | */
|
---|
2258 | static void vdResetModifiedFlag(PVBOXHDD pDisk)
|
---|
2259 | {
|
---|
2260 | if (pDisk->uModified & VD_IMAGE_MODIFIED_FLAG)
|
---|
2261 | {
|
---|
2262 | /* generate new last-modified uuid */
|
---|
2263 | if (!(pDisk->uModified & VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
|
---|
2264 | {
|
---|
2265 | RTUUID Uuid;
|
---|
2266 |
|
---|
2267 | RTUuidCreate(&Uuid);
|
---|
2268 | pDisk->pLast->Backend->pfnSetModificationUuid(pDisk->pLast->pBackendData,
|
---|
2269 | &Uuid);
|
---|
2270 |
|
---|
2271 | if (pDisk->pCache)
|
---|
2272 | pDisk->pCache->Backend->pfnSetModificationUuid(pDisk->pCache->pBackendData,
|
---|
2273 | &Uuid);
|
---|
2274 | }
|
---|
2275 |
|
---|
2276 | pDisk->uModified &= ~VD_IMAGE_MODIFIED_FLAG;
|
---|
2277 | }
|
---|
2278 | }
|
---|
2279 |
|
---|
2280 | /**
|
---|
2281 | * internal: mark the disk as modified.
|
---|
2282 | */
|
---|
2283 | static void vdSetModifiedFlag(PVBOXHDD pDisk)
|
---|
2284 | {
|
---|
2285 | pDisk->uModified |= VD_IMAGE_MODIFIED_FLAG;
|
---|
2286 | if (pDisk->uModified & VD_IMAGE_MODIFIED_FIRST)
|
---|
2287 | {
|
---|
2288 | pDisk->uModified &= ~VD_IMAGE_MODIFIED_FIRST;
|
---|
2289 |
|
---|
2290 | /* First modify, so create a UUID and ensure it's written to disk. */
|
---|
2291 | vdResetModifiedFlag(pDisk);
|
---|
2292 |
|
---|
2293 | if (!(pDisk->uModified & VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
|
---|
2294 | {
|
---|
2295 | VDIOCTX IoCtx;
|
---|
2296 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_FLUSH, 0, 0, NULL,
|
---|
2297 | NULL, NULL, NULL, VDIOCTX_FLAGS_SYNC);
|
---|
2298 | pDisk->pLast->Backend->pfnFlush(pDisk->pLast->pBackendData, &IoCtx);
|
---|
2299 | }
|
---|
2300 | }
|
---|
2301 | }
|
---|
2302 |
|
---|
2303 | /**
|
---|
2304 | * internal: write buffer to the image, taking care of block boundaries and
|
---|
2305 | * write optimizations.
|
---|
2306 | */
|
---|
2307 | static int vdWriteHelperEx(PVBOXHDD pDisk, PVDIMAGE pImage,
|
---|
2308 | PVDIMAGE pImageParentOverride, uint64_t uOffset,
|
---|
2309 | const void *pvBuf, size_t cbWrite,
|
---|
2310 | uint32_t fFlags, unsigned cImagesRead)
|
---|
2311 | {
|
---|
2312 | int rc = VINF_SUCCESS;
|
---|
2313 | RTSGSEG Segment;
|
---|
2314 | RTSGBUF SgBuf;
|
---|
2315 | VDIOCTX IoCtx;
|
---|
2316 | RTSEMEVENT hEventComplete = NIL_RTSEMEVENT;
|
---|
2317 |
|
---|
2318 | rc = RTSemEventCreate(&hEventComplete);
|
---|
2319 | if (RT_FAILURE(rc))
|
---|
2320 | return rc;
|
---|
2321 |
|
---|
2322 | fFlags |= VDIOCTX_FLAGS_SYNC | VDIOCTX_FLAGS_DONT_FREE;
|
---|
2323 |
|
---|
2324 | Segment.pvSeg = (void *)pvBuf;
|
---|
2325 | Segment.cbSeg = cbWrite;
|
---|
2326 | RTSgBufInit(&SgBuf, &Segment, 1);
|
---|
2327 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_WRITE, uOffset, cbWrite, pImage, &SgBuf,
|
---|
2328 | NULL, vdWriteHelperAsync, fFlags);
|
---|
2329 |
|
---|
2330 | IoCtx.Req.Io.pImageParentOverride = pImageParentOverride;
|
---|
2331 | IoCtx.Req.Io.cImagesRead = cImagesRead;
|
---|
2332 | IoCtx.pIoCtxParent = NULL;
|
---|
2333 | IoCtx.Type.Root.pfnComplete = vdIoCtxSyncComplete;
|
---|
2334 | IoCtx.Type.Root.pvUser1 = pDisk;
|
---|
2335 | IoCtx.Type.Root.pvUser2 = hEventComplete;
|
---|
2336 | if (RT_SUCCESS(rc))
|
---|
2337 | rc = vdIoCtxProcessSync(&IoCtx, hEventComplete);
|
---|
2338 |
|
---|
2339 | RTSemEventDestroy(hEventComplete);
|
---|
2340 | return rc;
|
---|
2341 | }
|
---|
2342 |
|
---|
2343 | /**
|
---|
2344 | * internal: write buffer to the image, taking care of block boundaries and
|
---|
2345 | * write optimizations.
|
---|
2346 | */
|
---|
2347 | static int vdWriteHelper(PVBOXHDD pDisk, PVDIMAGE pImage, uint64_t uOffset,
|
---|
2348 | const void *pvBuf, size_t cbWrite, uint32_t fFlags)
|
---|
2349 | {
|
---|
2350 | return vdWriteHelperEx(pDisk, pImage, NULL, uOffset, pvBuf, cbWrite,
|
---|
2351 | fFlags, 0);
|
---|
2352 | }
|
---|
2353 |
|
---|
2354 | /**
|
---|
2355 | * Internal: Copies the content of one disk to another one applying optimizations
|
---|
2356 | * to speed up the copy process if possible.
|
---|
2357 | */
|
---|
2358 | static int vdCopyHelper(PVBOXHDD pDiskFrom, PVDIMAGE pImageFrom, PVBOXHDD pDiskTo,
|
---|
2359 | uint64_t cbSize, unsigned cImagesFromRead, unsigned cImagesToRead,
|
---|
2360 | bool fSuppressRedundantIo, PVDINTERFACEPROGRESS pIfProgress,
|
---|
2361 | PVDINTERFACEPROGRESS pDstIfProgress)
|
---|
2362 | {
|
---|
2363 | int rc = VINF_SUCCESS;
|
---|
2364 | int rc2;
|
---|
2365 | uint64_t uOffset = 0;
|
---|
2366 | uint64_t cbRemaining = cbSize;
|
---|
2367 | void *pvBuf = NULL;
|
---|
2368 | bool fLockReadFrom = false;
|
---|
2369 | bool fLockWriteTo = false;
|
---|
2370 | bool fBlockwiseCopy = fSuppressRedundantIo || (cImagesFromRead > 0);
|
---|
2371 | unsigned uProgressOld = 0;
|
---|
2372 |
|
---|
2373 | LogFlowFunc(("pDiskFrom=%#p pImageFrom=%#p pDiskTo=%#p cbSize=%llu cImagesFromRead=%u cImagesToRead=%u fSuppressRedundantIo=%RTbool pIfProgress=%#p pDstIfProgress=%#p\n",
|
---|
2374 | pDiskFrom, pImageFrom, pDiskTo, cbSize, cImagesFromRead, cImagesToRead, fSuppressRedundantIo, pDstIfProgress, pDstIfProgress));
|
---|
2375 |
|
---|
2376 | /* Allocate tmp buffer. */
|
---|
2377 | pvBuf = RTMemTmpAlloc(VD_MERGE_BUFFER_SIZE);
|
---|
2378 | if (!pvBuf)
|
---|
2379 | return rc;
|
---|
2380 |
|
---|
2381 | do
|
---|
2382 | {
|
---|
2383 | size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
|
---|
2384 |
|
---|
2385 | /* Note that we don't attempt to synchronize cross-disk accesses.
|
---|
2386 | * It wouldn't be very difficult to do, just the lock order would
|
---|
2387 | * need to be defined somehow to prevent deadlocks. Postpone such
|
---|
2388 | * magic as there is no use case for this. */
|
---|
2389 |
|
---|
2390 | rc2 = vdThreadStartRead(pDiskFrom);
|
---|
2391 | AssertRC(rc2);
|
---|
2392 | fLockReadFrom = true;
|
---|
2393 |
|
---|
2394 | if (fBlockwiseCopy)
|
---|
2395 | {
|
---|
2396 | RTSGSEG SegmentBuf;
|
---|
2397 | RTSGBUF SgBuf;
|
---|
2398 | VDIOCTX IoCtx;
|
---|
2399 |
|
---|
2400 | SegmentBuf.pvSeg = pvBuf;
|
---|
2401 | SegmentBuf.cbSeg = VD_MERGE_BUFFER_SIZE;
|
---|
2402 | RTSgBufInit(&SgBuf, &SegmentBuf, 1);
|
---|
2403 | vdIoCtxInit(&IoCtx, pDiskFrom, VDIOCTXTXDIR_READ, 0, 0, NULL,
|
---|
2404 | &SgBuf, NULL, NULL, VDIOCTX_FLAGS_SYNC);
|
---|
2405 |
|
---|
2406 | /* Read the source data. */
|
---|
2407 | rc = pImageFrom->Backend->pfnRead(pImageFrom->pBackendData,
|
---|
2408 | uOffset, cbThisRead, &IoCtx,
|
---|
2409 | &cbThisRead);
|
---|
2410 |
|
---|
2411 | if ( rc == VERR_VD_BLOCK_FREE
|
---|
2412 | && cImagesFromRead != 1)
|
---|
2413 | {
|
---|
2414 | unsigned cImagesToProcess = cImagesFromRead;
|
---|
2415 |
|
---|
2416 | for (PVDIMAGE pCurrImage = pImageFrom->pPrev;
|
---|
2417 | pCurrImage != NULL && rc == VERR_VD_BLOCK_FREE;
|
---|
2418 | pCurrImage = pCurrImage->pPrev)
|
---|
2419 | {
|
---|
2420 | rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
|
---|
2421 | uOffset, cbThisRead,
|
---|
2422 | &IoCtx, &cbThisRead);
|
---|
2423 | if (cImagesToProcess == 1)
|
---|
2424 | break;
|
---|
2425 | else if (cImagesToProcess > 0)
|
---|
2426 | cImagesToProcess--;
|
---|
2427 | }
|
---|
2428 | }
|
---|
2429 | }
|
---|
2430 | else
|
---|
2431 | rc = vdReadHelper(pDiskFrom, pImageFrom, uOffset, pvBuf, cbThisRead,
|
---|
2432 | false /* fUpdateCache */);
|
---|
2433 |
|
---|
2434 | if (RT_FAILURE(rc) && rc != VERR_VD_BLOCK_FREE)
|
---|
2435 | break;
|
---|
2436 |
|
---|
2437 | rc2 = vdThreadFinishRead(pDiskFrom);
|
---|
2438 | AssertRC(rc2);
|
---|
2439 | fLockReadFrom = false;
|
---|
2440 |
|
---|
2441 | if (rc != VERR_VD_BLOCK_FREE)
|
---|
2442 | {
|
---|
2443 | rc2 = vdThreadStartWrite(pDiskTo);
|
---|
2444 | AssertRC(rc2);
|
---|
2445 | fLockWriteTo = true;
|
---|
2446 |
|
---|
2447 | /* Only do collapsed I/O if we are copying the data blockwise. */
|
---|
2448 | rc = vdWriteHelperEx(pDiskTo, pDiskTo->pLast, NULL, uOffset, pvBuf,
|
---|
2449 | cbThisRead, VDIOCTX_FLAGS_DONT_SET_MODIFIED_FLAG /* fFlags */,
|
---|
2450 | fBlockwiseCopy ? cImagesToRead : 0);
|
---|
2451 | if (RT_FAILURE(rc))
|
---|
2452 | break;
|
---|
2453 |
|
---|
2454 | rc2 = vdThreadFinishWrite(pDiskTo);
|
---|
2455 | AssertRC(rc2);
|
---|
2456 | fLockWriteTo = false;
|
---|
2457 | }
|
---|
2458 | else /* Don't propagate the error to the outside */
|
---|
2459 | rc = VINF_SUCCESS;
|
---|
2460 |
|
---|
2461 | uOffset += cbThisRead;
|
---|
2462 | cbRemaining -= cbThisRead;
|
---|
2463 |
|
---|
2464 | unsigned uProgressNew = uOffset * 99 / cbSize;
|
---|
2465 | if (uProgressNew != uProgressOld)
|
---|
2466 | {
|
---|
2467 | uProgressOld = uProgressNew;
|
---|
2468 |
|
---|
2469 | if (pIfProgress && pIfProgress->pfnProgress)
|
---|
2470 | {
|
---|
2471 | rc = pIfProgress->pfnProgress(pIfProgress->Core.pvUser,
|
---|
2472 | uProgressOld);
|
---|
2473 | if (RT_FAILURE(rc))
|
---|
2474 | break;
|
---|
2475 | }
|
---|
2476 | if (pDstIfProgress && pDstIfProgress->pfnProgress)
|
---|
2477 | {
|
---|
2478 | rc = pDstIfProgress->pfnProgress(pDstIfProgress->Core.pvUser,
|
---|
2479 | uProgressOld);
|
---|
2480 | if (RT_FAILURE(rc))
|
---|
2481 | break;
|
---|
2482 | }
|
---|
2483 | }
|
---|
2484 | } while (uOffset < cbSize);
|
---|
2485 |
|
---|
2486 | RTMemFree(pvBuf);
|
---|
2487 |
|
---|
2488 | if (fLockReadFrom)
|
---|
2489 | {
|
---|
2490 | rc2 = vdThreadFinishRead(pDiskFrom);
|
---|
2491 | AssertRC(rc2);
|
---|
2492 | }
|
---|
2493 |
|
---|
2494 | if (fLockWriteTo)
|
---|
2495 | {
|
---|
2496 | rc2 = vdThreadFinishWrite(pDiskTo);
|
---|
2497 | AssertRC(rc2);
|
---|
2498 | }
|
---|
2499 |
|
---|
2500 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
2501 | return rc;
|
---|
2502 | }
|
---|
2503 |
|
---|
2504 | /**
|
---|
2505 | * Flush helper async version.
|
---|
2506 | */
|
---|
2507 | static int vdSetModifiedHelperAsync(PVDIOCTX pIoCtx)
|
---|
2508 | {
|
---|
2509 | int rc = VINF_SUCCESS;
|
---|
2510 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
2511 | PVDIMAGE pImage = pIoCtx->Req.Io.pImageCur;
|
---|
2512 |
|
---|
2513 | rc = pImage->Backend->pfnFlush(pImage->pBackendData, pIoCtx);
|
---|
2514 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
2515 | rc = VINF_SUCCESS;
|
---|
2516 |
|
---|
2517 | return rc;
|
---|
2518 | }
|
---|
2519 |
|
---|
2520 | /**
|
---|
2521 | * internal: mark the disk as modified - async version.
|
---|
2522 | */
|
---|
2523 | static int vdSetModifiedFlagAsync(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
|
---|
2524 | {
|
---|
2525 | int rc = VINF_SUCCESS;
|
---|
2526 |
|
---|
2527 | VD_IS_LOCKED(pDisk);
|
---|
2528 |
|
---|
2529 | pDisk->uModified |= VD_IMAGE_MODIFIED_FLAG;
|
---|
2530 | if (pDisk->uModified & VD_IMAGE_MODIFIED_FIRST)
|
---|
2531 | {
|
---|
2532 | rc = vdIoCtxLockDisk(pDisk, pIoCtx);
|
---|
2533 | if (RT_SUCCESS(rc))
|
---|
2534 | {
|
---|
2535 | pDisk->uModified &= ~VD_IMAGE_MODIFIED_FIRST;
|
---|
2536 |
|
---|
2537 | /* First modify, so create a UUID and ensure it's written to disk. */
|
---|
2538 | vdResetModifiedFlag(pDisk);
|
---|
2539 |
|
---|
2540 | if (!(pDisk->uModified & VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
|
---|
2541 | {
|
---|
2542 | PVDIOCTX pIoCtxFlush = vdIoCtxChildAlloc(pDisk, VDIOCTXTXDIR_FLUSH,
|
---|
2543 | 0, 0, pDisk->pLast,
|
---|
2544 | NULL, pIoCtx, 0, 0, NULL,
|
---|
2545 | vdSetModifiedHelperAsync);
|
---|
2546 |
|
---|
2547 | if (pIoCtxFlush)
|
---|
2548 | {
|
---|
2549 | rc = vdIoCtxProcessLocked(pIoCtxFlush);
|
---|
2550 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
2551 | {
|
---|
2552 | vdIoCtxUnlockDisk(pDisk, pIoCtx, false /* fProcessDeferredReqs */);
|
---|
2553 | vdIoCtxFree(pDisk, pIoCtxFlush);
|
---|
2554 | }
|
---|
2555 | else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
2556 | {
|
---|
2557 | ASMAtomicIncU32(&pIoCtx->cDataTransfersPending);
|
---|
2558 | pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
|
---|
2559 | }
|
---|
2560 | else /* Another error */
|
---|
2561 | vdIoCtxFree(pDisk, pIoCtxFlush);
|
---|
2562 | }
|
---|
2563 | else
|
---|
2564 | rc = VERR_NO_MEMORY;
|
---|
2565 | }
|
---|
2566 | }
|
---|
2567 | }
|
---|
2568 |
|
---|
2569 | return rc;
|
---|
2570 | }
|
---|
2571 |
|
---|
2572 | static int vdWriteHelperCommitAsync(PVDIOCTX pIoCtx)
|
---|
2573 | {
|
---|
2574 | int rc = VINF_SUCCESS;
|
---|
2575 | PVDIMAGE pImage = pIoCtx->Req.Io.pImageStart;
|
---|
2576 | size_t cbPreRead = pIoCtx->Type.Child.cbPreRead;
|
---|
2577 | size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
|
---|
2578 | size_t cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
|
---|
2579 |
|
---|
2580 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
2581 | rc = pImage->Backend->pfnWrite(pImage->pBackendData,
|
---|
2582 | pIoCtx->Req.Io.uOffset - cbPreRead,
|
---|
2583 | cbPreRead + cbThisWrite + cbPostRead,
|
---|
2584 | pIoCtx, NULL, &cbPreRead, &cbPostRead, 0);
|
---|
2585 | Assert(rc != VERR_VD_BLOCK_FREE);
|
---|
2586 | Assert(rc == VERR_VD_NOT_ENOUGH_METADATA || cbPreRead == 0);
|
---|
2587 | Assert(rc == VERR_VD_NOT_ENOUGH_METADATA || cbPostRead == 0);
|
---|
2588 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
2589 | rc = VINF_SUCCESS;
|
---|
2590 | else if (rc == VERR_VD_IOCTX_HALT)
|
---|
2591 | {
|
---|
2592 | pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
|
---|
2593 | rc = VINF_SUCCESS;
|
---|
2594 | }
|
---|
2595 |
|
---|
2596 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
2597 | return rc;
|
---|
2598 | }
|
---|
2599 |
|
---|
2600 | static int vdWriteHelperOptimizedCmpAndWriteAsync(PVDIOCTX pIoCtx)
|
---|
2601 | {
|
---|
2602 | int rc = VINF_SUCCESS;
|
---|
2603 | PVDIMAGE pImage = pIoCtx->Req.Io.pImageCur;
|
---|
2604 | size_t cbThisWrite = 0;
|
---|
2605 | size_t cbPreRead = pIoCtx->Type.Child.cbPreRead;
|
---|
2606 | size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
|
---|
2607 | size_t cbWriteCopy = pIoCtx->Type.Child.Write.Optimized.cbWriteCopy;
|
---|
2608 | size_t cbFill = pIoCtx->Type.Child.Write.Optimized.cbFill;
|
---|
2609 | size_t cbReadImage = pIoCtx->Type.Child.Write.Optimized.cbReadImage;
|
---|
2610 | PVDIOCTX pIoCtxParent = pIoCtx->pIoCtxParent;
|
---|
2611 |
|
---|
2612 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
2613 |
|
---|
2614 | AssertPtr(pIoCtxParent);
|
---|
2615 | Assert(!pIoCtxParent->pIoCtxParent);
|
---|
2616 | Assert(!pIoCtx->Req.Io.cbTransferLeft && !pIoCtx->cMetaTransfersPending);
|
---|
2617 |
|
---|
2618 | vdIoCtxChildReset(pIoCtx);
|
---|
2619 | cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
|
---|
2620 | RTSgBufAdvance(&pIoCtx->Req.Io.SgBuf, cbPreRead);
|
---|
2621 |
|
---|
2622 | /* Check if the write would modify anything in this block. */
|
---|
2623 | if (!RTSgBufCmp(&pIoCtx->Req.Io.SgBuf, &pIoCtxParent->Req.Io.SgBuf, cbThisWrite))
|
---|
2624 | {
|
---|
2625 | RTSGBUF SgBufSrcTmp;
|
---|
2626 |
|
---|
2627 | RTSgBufClone(&SgBufSrcTmp, &pIoCtxParent->Req.Io.SgBuf);
|
---|
2628 | RTSgBufAdvance(&SgBufSrcTmp, cbThisWrite);
|
---|
2629 | RTSgBufAdvance(&pIoCtx->Req.Io.SgBuf, cbThisWrite);
|
---|
2630 |
|
---|
2631 | if (!cbWriteCopy || !RTSgBufCmp(&pIoCtx->Req.Io.SgBuf, &SgBufSrcTmp, cbWriteCopy))
|
---|
2632 | {
|
---|
2633 | /* Block is completely unchanged, so no need to write anything. */
|
---|
2634 | LogFlowFunc(("Block didn't changed\n"));
|
---|
2635 | ASMAtomicWriteU32(&pIoCtx->Req.Io.cbTransferLeft, 0);
|
---|
2636 | RTSgBufAdvance(&pIoCtxParent->Req.Io.SgBuf, cbThisWrite);
|
---|
2637 | return VINF_VD_ASYNC_IO_FINISHED;
|
---|
2638 | }
|
---|
2639 | }
|
---|
2640 |
|
---|
2641 | /* Copy the data to the right place in the buffer. */
|
---|
2642 | RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
|
---|
2643 | RTSgBufAdvance(&pIoCtx->Req.Io.SgBuf, cbPreRead);
|
---|
2644 | vdIoCtxCopy(pIoCtx, pIoCtxParent, cbThisWrite);
|
---|
2645 |
|
---|
2646 | /* Handle the data that goes after the write to fill the block. */
|
---|
2647 | if (cbPostRead)
|
---|
2648 | {
|
---|
2649 | /* Now assemble the remaining data. */
|
---|
2650 | if (cbWriteCopy)
|
---|
2651 | {
|
---|
2652 | /*
|
---|
2653 | * The S/G buffer of the parent needs to be cloned because
|
---|
2654 | * it is not allowed to modify the state.
|
---|
2655 | */
|
---|
2656 | RTSGBUF SgBufParentTmp;
|
---|
2657 |
|
---|
2658 | RTSgBufClone(&SgBufParentTmp, &pIoCtxParent->Req.Io.SgBuf);
|
---|
2659 | RTSgBufCopy(&pIoCtx->Req.Io.SgBuf, &SgBufParentTmp, cbWriteCopy);
|
---|
2660 | }
|
---|
2661 |
|
---|
2662 | /* Zero out the remainder of this block. Will never be visible, as this
|
---|
2663 | * is beyond the limit of the image. */
|
---|
2664 | if (cbFill)
|
---|
2665 | {
|
---|
2666 | RTSgBufAdvance(&pIoCtx->Req.Io.SgBuf, cbReadImage);
|
---|
2667 | vdIoCtxSet(pIoCtx, '\0', cbFill);
|
---|
2668 | }
|
---|
2669 | }
|
---|
2670 |
|
---|
2671 | /* Write the full block to the virtual disk. */
|
---|
2672 | RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
|
---|
2673 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperCommitAsync;
|
---|
2674 |
|
---|
2675 | return rc;
|
---|
2676 | }
|
---|
2677 |
|
---|
2678 | static int vdWriteHelperOptimizedPreReadAsync(PVDIOCTX pIoCtx)
|
---|
2679 | {
|
---|
2680 | int rc = VINF_SUCCESS;
|
---|
2681 |
|
---|
2682 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
2683 |
|
---|
2684 | pIoCtx->fFlags |= VDIOCTX_FLAGS_ZERO_FREE_BLOCKS;
|
---|
2685 |
|
---|
2686 | if ( pIoCtx->Req.Io.cbTransferLeft
|
---|
2687 | && !pIoCtx->cDataTransfersPending)
|
---|
2688 | rc = vdReadHelperAsync(pIoCtx);
|
---|
2689 |
|
---|
2690 | if ( ( RT_SUCCESS(rc)
|
---|
2691 | || (rc == VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
2692 | && ( pIoCtx->Req.Io.cbTransferLeft
|
---|
2693 | || pIoCtx->cMetaTransfersPending))
|
---|
2694 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
2695 | else
|
---|
2696 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperOptimizedCmpAndWriteAsync;
|
---|
2697 |
|
---|
2698 | return rc;
|
---|
2699 | }
|
---|
2700 |
|
---|
2701 | /**
|
---|
2702 | * internal: write a complete block (only used for diff images), taking the
|
---|
2703 | * remaining data from parent images. This implementation optimizes out writes
|
---|
2704 | * that do not change the data relative to the state as of the parent images.
|
---|
2705 | * All backends which support differential/growing images support this - async version.
|
---|
2706 | */
|
---|
2707 | static int vdWriteHelperOptimizedAsync(PVDIOCTX pIoCtx)
|
---|
2708 | {
|
---|
2709 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
2710 | uint64_t uOffset = pIoCtx->Type.Child.uOffsetSaved;
|
---|
2711 | size_t cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
|
---|
2712 | size_t cbPreRead = pIoCtx->Type.Child.cbPreRead;
|
---|
2713 | size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
|
---|
2714 | size_t cbWrite = pIoCtx->Type.Child.cbWriteParent;
|
---|
2715 | size_t cbFill = 0;
|
---|
2716 | size_t cbWriteCopy = 0;
|
---|
2717 | size_t cbReadImage = 0;
|
---|
2718 |
|
---|
2719 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
2720 |
|
---|
2721 | AssertPtr(pIoCtx->pIoCtxParent);
|
---|
2722 | Assert(!pIoCtx->pIoCtxParent->pIoCtxParent);
|
---|
2723 |
|
---|
2724 | if (cbPostRead)
|
---|
2725 | {
|
---|
2726 | /* Figure out how much we cannot read from the image, because
|
---|
2727 | * the last block to write might exceed the nominal size of the
|
---|
2728 | * image for technical reasons. */
|
---|
2729 | if (uOffset + cbThisWrite + cbPostRead > pDisk->cbSize)
|
---|
2730 | cbFill = uOffset + cbThisWrite + cbPostRead - pDisk->cbSize;
|
---|
2731 |
|
---|
2732 | /* If we have data to be written, use that instead of reading
|
---|
2733 | * data from the image. */
|
---|
2734 | if (cbWrite > cbThisWrite)
|
---|
2735 | cbWriteCopy = RT_MIN(cbWrite - cbThisWrite, cbPostRead);
|
---|
2736 |
|
---|
2737 | /* The rest must be read from the image. */
|
---|
2738 | cbReadImage = cbPostRead - cbWriteCopy - cbFill;
|
---|
2739 | }
|
---|
2740 |
|
---|
2741 | pIoCtx->Type.Child.Write.Optimized.cbFill = cbFill;
|
---|
2742 | pIoCtx->Type.Child.Write.Optimized.cbWriteCopy = cbWriteCopy;
|
---|
2743 | pIoCtx->Type.Child.Write.Optimized.cbReadImage = cbReadImage;
|
---|
2744 |
|
---|
2745 | /* Read the entire data of the block so that we can compare whether it will
|
---|
2746 | * be modified by the write or not. */
|
---|
2747 | size_t cbTmp = cbPreRead + cbThisWrite + cbPostRead - cbFill; Assert(cbTmp == (uint32_t)cbTmp);
|
---|
2748 | pIoCtx->Req.Io.cbTransferLeft = (uint32_t)cbTmp;
|
---|
2749 | pIoCtx->Req.Io.cbTransfer = pIoCtx->Req.Io.cbTransferLeft;
|
---|
2750 | pIoCtx->Req.Io.uOffset -= cbPreRead;
|
---|
2751 |
|
---|
2752 | /* Next step */
|
---|
2753 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperOptimizedPreReadAsync;
|
---|
2754 | return VINF_SUCCESS;
|
---|
2755 | }
|
---|
2756 |
|
---|
2757 | static int vdWriteHelperStandardReadImageAsync(PVDIOCTX pIoCtx)
|
---|
2758 | {
|
---|
2759 | int rc = VINF_SUCCESS;
|
---|
2760 |
|
---|
2761 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
2762 |
|
---|
2763 | pIoCtx->fFlags |= VDIOCTX_FLAGS_ZERO_FREE_BLOCKS;
|
---|
2764 |
|
---|
2765 | if ( pIoCtx->Req.Io.cbTransferLeft
|
---|
2766 | && !pIoCtx->cDataTransfersPending)
|
---|
2767 | rc = vdReadHelperAsync(pIoCtx);
|
---|
2768 |
|
---|
2769 | if ( RT_SUCCESS(rc)
|
---|
2770 | && ( pIoCtx->Req.Io.cbTransferLeft
|
---|
2771 | || pIoCtx->cMetaTransfersPending))
|
---|
2772 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
2773 | else
|
---|
2774 | {
|
---|
2775 | size_t cbFill = pIoCtx->Type.Child.Write.Optimized.cbFill;
|
---|
2776 |
|
---|
2777 | /* Zero out the remainder of this block. Will never be visible, as this
|
---|
2778 | * is beyond the limit of the image. */
|
---|
2779 | if (cbFill)
|
---|
2780 | vdIoCtxSet(pIoCtx, '\0', cbFill);
|
---|
2781 |
|
---|
2782 | /* Write the full block to the virtual disk. */
|
---|
2783 | RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
|
---|
2784 |
|
---|
2785 | vdIoCtxChildReset(pIoCtx);
|
---|
2786 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperCommitAsync;
|
---|
2787 | }
|
---|
2788 |
|
---|
2789 | return rc;
|
---|
2790 | }
|
---|
2791 |
|
---|
2792 | static int vdWriteHelperStandardAssemble(PVDIOCTX pIoCtx)
|
---|
2793 | {
|
---|
2794 | int rc = VINF_SUCCESS;
|
---|
2795 | size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
|
---|
2796 | size_t cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
|
---|
2797 | PVDIOCTX pIoCtxParent = pIoCtx->pIoCtxParent;
|
---|
2798 |
|
---|
2799 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
2800 |
|
---|
2801 | vdIoCtxCopy(pIoCtx, pIoCtxParent, cbThisWrite);
|
---|
2802 | if (cbPostRead)
|
---|
2803 | {
|
---|
2804 | size_t cbFill = pIoCtx->Type.Child.Write.Optimized.cbFill;
|
---|
2805 | size_t cbWriteCopy = pIoCtx->Type.Child.Write.Optimized.cbWriteCopy;
|
---|
2806 | size_t cbReadImage = pIoCtx->Type.Child.Write.Optimized.cbReadImage;
|
---|
2807 |
|
---|
2808 | /* Now assemble the remaining data. */
|
---|
2809 | if (cbWriteCopy)
|
---|
2810 | {
|
---|
2811 | /*
|
---|
2812 | * The S/G buffer of the parent needs to be cloned because
|
---|
2813 | * it is not allowed to modify the state.
|
---|
2814 | */
|
---|
2815 | RTSGBUF SgBufParentTmp;
|
---|
2816 |
|
---|
2817 | RTSgBufClone(&SgBufParentTmp, &pIoCtxParent->Req.Io.SgBuf);
|
---|
2818 | RTSgBufCopy(&pIoCtx->Req.Io.SgBuf, &SgBufParentTmp, cbWriteCopy);
|
---|
2819 | }
|
---|
2820 |
|
---|
2821 | if (cbReadImage)
|
---|
2822 | {
|
---|
2823 | /* Read remaining data. */
|
---|
2824 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperStandardReadImageAsync;
|
---|
2825 |
|
---|
2826 | /* Read the data that goes before the write to fill the block. */
|
---|
2827 | pIoCtx->Req.Io.cbTransferLeft = (uint32_t)cbReadImage; Assert(cbReadImage == (uint32_t)cbReadImage);
|
---|
2828 | pIoCtx->Req.Io.cbTransfer = pIoCtx->Req.Io.cbTransferLeft;
|
---|
2829 | pIoCtx->Req.Io.uOffset += cbWriteCopy;
|
---|
2830 | }
|
---|
2831 | else
|
---|
2832 | {
|
---|
2833 | /* Zero out the remainder of this block. Will never be visible, as this
|
---|
2834 | * is beyond the limit of the image. */
|
---|
2835 | if (cbFill)
|
---|
2836 | vdIoCtxSet(pIoCtx, '\0', cbFill);
|
---|
2837 |
|
---|
2838 | /* Write the full block to the virtual disk. */
|
---|
2839 | RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
|
---|
2840 | vdIoCtxChildReset(pIoCtx);
|
---|
2841 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperCommitAsync;
|
---|
2842 | }
|
---|
2843 | }
|
---|
2844 | else
|
---|
2845 | {
|
---|
2846 | /* Write the full block to the virtual disk. */
|
---|
2847 | RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
|
---|
2848 | vdIoCtxChildReset(pIoCtx);
|
---|
2849 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperCommitAsync;
|
---|
2850 | }
|
---|
2851 |
|
---|
2852 | return rc;
|
---|
2853 | }
|
---|
2854 |
|
---|
2855 | static int vdWriteHelperStandardPreReadAsync(PVDIOCTX pIoCtx)
|
---|
2856 | {
|
---|
2857 | int rc = VINF_SUCCESS;
|
---|
2858 |
|
---|
2859 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
2860 |
|
---|
2861 | pIoCtx->fFlags |= VDIOCTX_FLAGS_ZERO_FREE_BLOCKS;
|
---|
2862 |
|
---|
2863 | if ( pIoCtx->Req.Io.cbTransferLeft
|
---|
2864 | && !pIoCtx->cDataTransfersPending)
|
---|
2865 | rc = vdReadHelperAsync(pIoCtx);
|
---|
2866 |
|
---|
2867 | if ( RT_SUCCESS(rc)
|
---|
2868 | && ( pIoCtx->Req.Io.cbTransferLeft
|
---|
2869 | || pIoCtx->cMetaTransfersPending))
|
---|
2870 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
2871 | else
|
---|
2872 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperStandardAssemble;
|
---|
2873 |
|
---|
2874 | return rc;
|
---|
2875 | }
|
---|
2876 |
|
---|
2877 | static int vdWriteHelperStandardAsync(PVDIOCTX pIoCtx)
|
---|
2878 | {
|
---|
2879 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
2880 | uint64_t uOffset = pIoCtx->Type.Child.uOffsetSaved;
|
---|
2881 | size_t cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
|
---|
2882 | size_t cbPreRead = pIoCtx->Type.Child.cbPreRead;
|
---|
2883 | size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
|
---|
2884 | size_t cbWrite = pIoCtx->Type.Child.cbWriteParent;
|
---|
2885 | size_t cbFill = 0;
|
---|
2886 | size_t cbWriteCopy = 0;
|
---|
2887 | size_t cbReadImage = 0;
|
---|
2888 |
|
---|
2889 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
2890 |
|
---|
2891 | AssertPtr(pIoCtx->pIoCtxParent);
|
---|
2892 | Assert(!pIoCtx->pIoCtxParent->pIoCtxParent);
|
---|
2893 |
|
---|
2894 | /* Calculate the amount of data to read that goes after the write to fill the block. */
|
---|
2895 | if (cbPostRead)
|
---|
2896 | {
|
---|
2897 | /* If we have data to be written, use that instead of reading
|
---|
2898 | * data from the image. */
|
---|
2899 | if (cbWrite > cbThisWrite)
|
---|
2900 | cbWriteCopy = RT_MIN(cbWrite - cbThisWrite, cbPostRead);
|
---|
2901 | else
|
---|
2902 | cbWriteCopy = 0;
|
---|
2903 |
|
---|
2904 | /* Figure out how much we cannot read from the image, because
|
---|
2905 | * the last block to write might exceed the nominal size of the
|
---|
2906 | * image for technical reasons. */
|
---|
2907 | if (uOffset + cbThisWrite + cbPostRead > pDisk->cbSize)
|
---|
2908 | cbFill = uOffset + cbThisWrite + cbPostRead - pDisk->cbSize;
|
---|
2909 |
|
---|
2910 | /* The rest must be read from the image. */
|
---|
2911 | cbReadImage = cbPostRead - cbWriteCopy - cbFill;
|
---|
2912 | }
|
---|
2913 |
|
---|
2914 | pIoCtx->Type.Child.Write.Optimized.cbFill = cbFill;
|
---|
2915 | pIoCtx->Type.Child.Write.Optimized.cbWriteCopy = cbWriteCopy;
|
---|
2916 | pIoCtx->Type.Child.Write.Optimized.cbReadImage = cbReadImage;
|
---|
2917 |
|
---|
2918 | /* Next step */
|
---|
2919 | if (cbPreRead)
|
---|
2920 | {
|
---|
2921 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperStandardPreReadAsync;
|
---|
2922 |
|
---|
2923 | /* Read the data that goes before the write to fill the block. */
|
---|
2924 | pIoCtx->Req.Io.cbTransferLeft = (uint32_t)cbPreRead; Assert(cbPreRead == (uint32_t)cbPreRead);
|
---|
2925 | pIoCtx->Req.Io.cbTransfer = pIoCtx->Req.Io.cbTransferLeft;
|
---|
2926 | pIoCtx->Req.Io.uOffset -= cbPreRead;
|
---|
2927 | }
|
---|
2928 | else
|
---|
2929 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperStandardAssemble;
|
---|
2930 |
|
---|
2931 | return VINF_SUCCESS;
|
---|
2932 | }
|
---|
2933 |
|
---|
2934 | /**
|
---|
2935 | * internal: write buffer to the image, taking care of block boundaries and
|
---|
2936 | * write optimizations - async version.
|
---|
2937 | */
|
---|
2938 | static int vdWriteHelperAsync(PVDIOCTX pIoCtx)
|
---|
2939 | {
|
---|
2940 | int rc;
|
---|
2941 | size_t cbWrite = pIoCtx->Req.Io.cbTransfer;
|
---|
2942 | uint64_t uOffset = pIoCtx->Req.Io.uOffset;
|
---|
2943 | PVDIMAGE pImage = pIoCtx->Req.Io.pImageCur;
|
---|
2944 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
2945 | unsigned fWrite;
|
---|
2946 | size_t cbThisWrite;
|
---|
2947 | size_t cbPreRead, cbPostRead;
|
---|
2948 |
|
---|
2949 | /* Apply write filter chain here if it was not done already. */
|
---|
2950 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_WRITE_FILTER_APPLIED))
|
---|
2951 | {
|
---|
2952 | rc = vdFilterChainApplyWrite(pDisk, uOffset, cbWrite, pIoCtx);
|
---|
2953 | if (RT_FAILURE(rc))
|
---|
2954 | return rc;
|
---|
2955 | pIoCtx->fFlags |= VDIOCTX_FLAGS_WRITE_FILTER_APPLIED;
|
---|
2956 | }
|
---|
2957 |
|
---|
2958 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_DONT_SET_MODIFIED_FLAG))
|
---|
2959 | {
|
---|
2960 | rc = vdSetModifiedFlagAsync(pDisk, pIoCtx);
|
---|
2961 | if (RT_FAILURE(rc)) /* Includes I/O in progress. */
|
---|
2962 | return rc;
|
---|
2963 | }
|
---|
2964 |
|
---|
2965 | rc = vdDiscardSetRangeAllocated(pDisk, uOffset, cbWrite);
|
---|
2966 | if (RT_FAILURE(rc))
|
---|
2967 | return rc;
|
---|
2968 |
|
---|
2969 | /* Loop until all written. */
|
---|
2970 | do
|
---|
2971 | {
|
---|
2972 | /* Try to write the possibly partial block to the last opened image.
|
---|
2973 | * This works when the block is already allocated in this image or
|
---|
2974 | * if it is a full-block write (and allocation isn't suppressed below).
|
---|
2975 | * For image formats which don't support zero blocks, it's beneficial
|
---|
2976 | * to avoid unnecessarily allocating unchanged blocks. This prevents
|
---|
2977 | * unwanted expanding of images. VMDK is an example. */
|
---|
2978 | cbThisWrite = cbWrite;
|
---|
2979 |
|
---|
2980 | /*
|
---|
2981 | * Check whether there is a full block write in progress which was not allocated.
|
---|
2982 | * Defer I/O if the range interferes.
|
---|
2983 | */
|
---|
2984 | if ( pDisk->pIoCtxLockOwner != NIL_VDIOCTX
|
---|
2985 | && uOffset >= pDisk->uOffsetStartLocked
|
---|
2986 | && uOffset < pDisk->uOffsetEndLocked)
|
---|
2987 | {
|
---|
2988 | Log(("Interferring write while allocating a new block => deferring write\n"));
|
---|
2989 | vdIoCtxDefer(pDisk, pIoCtx);
|
---|
2990 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
2991 | break;
|
---|
2992 | }
|
---|
2993 |
|
---|
2994 | fWrite = (pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME)
|
---|
2995 | ? 0 : VD_WRITE_NO_ALLOC;
|
---|
2996 | rc = pImage->Backend->pfnWrite(pImage->pBackendData, uOffset, cbThisWrite,
|
---|
2997 | pIoCtx, &cbThisWrite, &cbPreRead, &cbPostRead,
|
---|
2998 | fWrite);
|
---|
2999 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
3000 | {
|
---|
3001 | /* Lock the disk .*/
|
---|
3002 | rc = vdIoCtxLockDisk(pDisk, pIoCtx);
|
---|
3003 | if (RT_SUCCESS(rc))
|
---|
3004 | {
|
---|
3005 | /*
|
---|
3006 | * Allocate segment and buffer in one go.
|
---|
3007 | * A bit hackish but avoids the need to allocate memory twice.
|
---|
3008 | */
|
---|
3009 | PRTSGBUF pTmp = (PRTSGBUF)RTMemAlloc(cbPreRead + cbThisWrite + cbPostRead + sizeof(RTSGSEG) + sizeof(RTSGBUF));
|
---|
3010 | AssertBreakStmt(VALID_PTR(pTmp), rc = VERR_NO_MEMORY);
|
---|
3011 | PRTSGSEG pSeg = (PRTSGSEG)(pTmp + 1);
|
---|
3012 |
|
---|
3013 | pSeg->pvSeg = pSeg + 1;
|
---|
3014 | pSeg->cbSeg = cbPreRead + cbThisWrite + cbPostRead;
|
---|
3015 | RTSgBufInit(pTmp, pSeg, 1);
|
---|
3016 |
|
---|
3017 | PVDIOCTX pIoCtxWrite = vdIoCtxChildAlloc(pDisk, VDIOCTXTXDIR_WRITE,
|
---|
3018 | uOffset, pSeg->cbSeg, pImage,
|
---|
3019 | pTmp,
|
---|
3020 | pIoCtx, cbThisWrite,
|
---|
3021 | cbWrite,
|
---|
3022 | pTmp,
|
---|
3023 | (pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME)
|
---|
3024 | ? vdWriteHelperStandardAsync
|
---|
3025 | : vdWriteHelperOptimizedAsync);
|
---|
3026 | if (!VALID_PTR(pIoCtxWrite))
|
---|
3027 | {
|
---|
3028 | RTMemTmpFree(pTmp);
|
---|
3029 | rc = VERR_NO_MEMORY;
|
---|
3030 | break;
|
---|
3031 | }
|
---|
3032 |
|
---|
3033 | LogFlowFunc(("Disk is growing because of pIoCtx=%#p pIoCtxWrite=%#p\n",
|
---|
3034 | pIoCtx, pIoCtxWrite));
|
---|
3035 |
|
---|
3036 | /* Save the current range for the growing operation to check for intersecting requests later. */
|
---|
3037 | pDisk->uOffsetStartLocked = uOffset - cbPreRead;
|
---|
3038 | pDisk->uOffsetEndLocked = uOffset + cbThisWrite + cbPostRead;
|
---|
3039 |
|
---|
3040 | pIoCtxWrite->Type.Child.cbPreRead = cbPreRead;
|
---|
3041 | pIoCtxWrite->Type.Child.cbPostRead = cbPostRead;
|
---|
3042 | pIoCtxWrite->Req.Io.pImageParentOverride = pIoCtx->Req.Io.pImageParentOverride;
|
---|
3043 |
|
---|
3044 | /* Process the write request */
|
---|
3045 | rc = vdIoCtxProcessLocked(pIoCtxWrite);
|
---|
3046 |
|
---|
3047 | if (RT_FAILURE(rc) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
3048 | {
|
---|
3049 | vdIoCtxFree(pDisk, pIoCtxWrite);
|
---|
3050 | break;
|
---|
3051 | }
|
---|
3052 | else if ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
3053 | && ASMAtomicCmpXchgBool(&pIoCtxWrite->fComplete, true, false))
|
---|
3054 | {
|
---|
3055 | LogFlow(("Child write request completed\n"));
|
---|
3056 | Assert(pIoCtx->Req.Io.cbTransferLeft >= cbThisWrite);
|
---|
3057 | Assert(cbThisWrite == (uint32_t)cbThisWrite);
|
---|
3058 | rc = pIoCtxWrite->rcReq;
|
---|
3059 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbThisWrite);
|
---|
3060 | vdIoCtxUnlockDisk(pDisk, pIoCtx, false /* fProcessDeferredReqs*/ );
|
---|
3061 | vdIoCtxFree(pDisk, pIoCtxWrite);
|
---|
3062 | }
|
---|
3063 | else
|
---|
3064 | {
|
---|
3065 | LogFlow(("Child write pending\n"));
|
---|
3066 | ASMAtomicIncU32(&pIoCtx->cDataTransfersPending);
|
---|
3067 | pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
|
---|
3068 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
3069 | cbWrite -= cbThisWrite;
|
---|
3070 | uOffset += cbThisWrite;
|
---|
3071 | break;
|
---|
3072 | }
|
---|
3073 | }
|
---|
3074 | else
|
---|
3075 | {
|
---|
3076 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
3077 | break;
|
---|
3078 | }
|
---|
3079 | }
|
---|
3080 |
|
---|
3081 | if (rc == VERR_VD_IOCTX_HALT)
|
---|
3082 | {
|
---|
3083 | cbWrite -= cbThisWrite;
|
---|
3084 | uOffset += cbThisWrite;
|
---|
3085 | pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
|
---|
3086 | break;
|
---|
3087 | }
|
---|
3088 | else if (rc == VERR_VD_NOT_ENOUGH_METADATA)
|
---|
3089 | break;
|
---|
3090 |
|
---|
3091 | cbWrite -= cbThisWrite;
|
---|
3092 | uOffset += cbThisWrite;
|
---|
3093 | } while (cbWrite != 0 && (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS));
|
---|
3094 |
|
---|
3095 | if ( rc == VERR_VD_ASYNC_IO_IN_PROGRESS
|
---|
3096 | || rc == VERR_VD_NOT_ENOUGH_METADATA
|
---|
3097 | || rc == VERR_VD_IOCTX_HALT)
|
---|
3098 | {
|
---|
3099 | /*
|
---|
3100 | * Tell the caller that we don't need to go back here because all
|
---|
3101 | * writes are initiated.
|
---|
3102 | */
|
---|
3103 | if ( !cbWrite
|
---|
3104 | && rc != VERR_VD_IOCTX_HALT)
|
---|
3105 | rc = VINF_SUCCESS;
|
---|
3106 |
|
---|
3107 | pIoCtx->Req.Io.uOffset = uOffset;
|
---|
3108 | pIoCtx->Req.Io.cbTransfer = cbWrite;
|
---|
3109 | }
|
---|
3110 |
|
---|
3111 | return rc;
|
---|
3112 | }
|
---|
3113 |
|
---|
3114 | /**
|
---|
3115 | * Flush helper async version.
|
---|
3116 | */
|
---|
3117 | static int vdFlushHelperAsync(PVDIOCTX pIoCtx)
|
---|
3118 | {
|
---|
3119 | int rc = VINF_SUCCESS;
|
---|
3120 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
3121 | PVDIMAGE pImage = pIoCtx->Req.Io.pImageCur;
|
---|
3122 |
|
---|
3123 | rc = vdIoCtxLockDisk(pDisk, pIoCtx);
|
---|
3124 | if (RT_SUCCESS(rc))
|
---|
3125 | {
|
---|
3126 | /* Mark the whole disk as locked. */
|
---|
3127 | pDisk->uOffsetStartLocked = 0;
|
---|
3128 | pDisk->uOffsetEndLocked = UINT64_C(0xffffffffffffffff);
|
---|
3129 |
|
---|
3130 | vdResetModifiedFlag(pDisk);
|
---|
3131 | rc = pImage->Backend->pfnFlush(pImage->pBackendData, pIoCtx);
|
---|
3132 | if ( ( RT_SUCCESS(rc)
|
---|
3133 | || rc == VERR_VD_ASYNC_IO_IN_PROGRESS
|
---|
3134 | || rc == VERR_VD_IOCTX_HALT)
|
---|
3135 | && pDisk->pCache)
|
---|
3136 | {
|
---|
3137 | rc = pDisk->pCache->Backend->pfnFlush(pDisk->pCache->pBackendData, pIoCtx);
|
---|
3138 | if ( RT_SUCCESS(rc)
|
---|
3139 | || ( rc != VERR_VD_ASYNC_IO_IN_PROGRESS
|
---|
3140 | && rc != VERR_VD_IOCTX_HALT))
|
---|
3141 | vdIoCtxUnlockDisk(pDisk, pIoCtx, true /* fProcessBlockedReqs */);
|
---|
3142 | else if (rc != VERR_VD_IOCTX_HALT)
|
---|
3143 | rc = VINF_SUCCESS;
|
---|
3144 | }
|
---|
3145 | else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
3146 | rc = VINF_SUCCESS;
|
---|
3147 | else if (rc != VERR_VD_IOCTX_HALT)/* Some other error. */
|
---|
3148 | vdIoCtxUnlockDisk(pDisk, pIoCtx, true /* fProcessBlockedReqs */);
|
---|
3149 | }
|
---|
3150 |
|
---|
3151 | return rc;
|
---|
3152 | }
|
---|
3153 |
|
---|
3154 | /**
|
---|
3155 | * Async discard helper - discards a whole block which is recorded in the block
|
---|
3156 | * tree.
|
---|
3157 | *
|
---|
3158 | * @returns VBox status code.
|
---|
3159 | * @param pIoCtx The I/O context to operate on.
|
---|
3160 | */
|
---|
3161 | static int vdDiscardWholeBlockAsync(PVDIOCTX pIoCtx)
|
---|
3162 | {
|
---|
3163 | int rc = VINF_SUCCESS;
|
---|
3164 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
3165 | PVDDISCARDSTATE pDiscard = pDisk->pDiscard;
|
---|
3166 | PVDDISCARDBLOCK pBlock = pIoCtx->Req.Discard.pBlock;
|
---|
3167 | size_t cbPreAllocated, cbPostAllocated, cbActuallyDiscarded;
|
---|
3168 |
|
---|
3169 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
3170 |
|
---|
3171 | AssertPtr(pBlock);
|
---|
3172 |
|
---|
3173 | rc = pDisk->pLast->Backend->pfnDiscard(pDisk->pLast->pBackendData, pIoCtx,
|
---|
3174 | pBlock->Core.Key, pBlock->cbDiscard,
|
---|
3175 | &cbPreAllocated, &cbPostAllocated,
|
---|
3176 | &cbActuallyDiscarded, NULL, 0);
|
---|
3177 | Assert(rc != VERR_VD_DISCARD_ALIGNMENT_NOT_MET);
|
---|
3178 | Assert(!cbPreAllocated);
|
---|
3179 | Assert(!cbPostAllocated);
|
---|
3180 | Assert(cbActuallyDiscarded == pBlock->cbDiscard || RT_FAILURE(rc));
|
---|
3181 |
|
---|
3182 | /* Remove the block on success. */
|
---|
3183 | if ( RT_SUCCESS(rc)
|
---|
3184 | || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
3185 | {
|
---|
3186 | PVDDISCARDBLOCK pBlockRemove = (PVDDISCARDBLOCK)RTAvlrU64RangeRemove(pDiscard->pTreeBlocks, pBlock->Core.Key);
|
---|
3187 | Assert(pBlockRemove == pBlock);
|
---|
3188 |
|
---|
3189 | pDiscard->cbDiscarding -= pBlock->cbDiscard;
|
---|
3190 | RTListNodeRemove(&pBlock->NodeLru);
|
---|
3191 | RTMemFree(pBlock->pbmAllocated);
|
---|
3192 | RTMemFree(pBlock);
|
---|
3193 | pIoCtx->Req.Discard.pBlock = NULL;/* Safety precaution. */
|
---|
3194 | pIoCtx->pfnIoCtxTransferNext = vdDiscardHelperAsync; /* Next part. */
|
---|
3195 | rc = VINF_SUCCESS;
|
---|
3196 | }
|
---|
3197 |
|
---|
3198 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
3199 | return rc;
|
---|
3200 | }
|
---|
3201 |
|
---|
3202 | /**
|
---|
3203 | * Removes the least recently used blocks from the waiting list until
|
---|
3204 | * the new value is reached - version for async I/O.
|
---|
3205 | *
|
---|
3206 | * @returns VBox status code.
|
---|
3207 | * @param pDisk VD disk container.
|
---|
3208 | * @param pDiscard The discard state.
|
---|
3209 | * @param cbDiscardingNew How many bytes should be waiting on success.
|
---|
3210 | * The number of bytes waiting can be less.
|
---|
3211 | */
|
---|
3212 | static int vdDiscardRemoveBlocksAsync(PVBOXHDD pDisk, PVDIOCTX pIoCtx, size_t cbDiscardingNew)
|
---|
3213 | {
|
---|
3214 | int rc = VINF_SUCCESS;
|
---|
3215 | PVDDISCARDSTATE pDiscard = pDisk->pDiscard;
|
---|
3216 |
|
---|
3217 | LogFlowFunc(("pDisk=%#p pDiscard=%#p cbDiscardingNew=%zu\n",
|
---|
3218 | pDisk, pDiscard, cbDiscardingNew));
|
---|
3219 |
|
---|
3220 | while (pDiscard->cbDiscarding > cbDiscardingNew)
|
---|
3221 | {
|
---|
3222 | PVDDISCARDBLOCK pBlock = RTListGetLast(&pDiscard->ListLru, VDDISCARDBLOCK, NodeLru);
|
---|
3223 |
|
---|
3224 | Assert(!RTListIsEmpty(&pDiscard->ListLru));
|
---|
3225 |
|
---|
3226 | /* Go over the allocation bitmap and mark all discarded sectors as unused. */
|
---|
3227 | uint64_t offStart = pBlock->Core.Key;
|
---|
3228 | uint32_t idxStart = 0;
|
---|
3229 | size_t cbLeft = pBlock->cbDiscard;
|
---|
3230 | bool fAllocated = ASMBitTest(pBlock->pbmAllocated, idxStart);
|
---|
3231 | uint32_t cSectors = (uint32_t)(pBlock->cbDiscard / 512);
|
---|
3232 |
|
---|
3233 | while (cbLeft > 0)
|
---|
3234 | {
|
---|
3235 | int32_t idxEnd;
|
---|
3236 | size_t cbThis = cbLeft;
|
---|
3237 |
|
---|
3238 | if (fAllocated)
|
---|
3239 | {
|
---|
3240 | /* Check for the first unallocated bit. */
|
---|
3241 | idxEnd = ASMBitNextClear(pBlock->pbmAllocated, cSectors, idxStart);
|
---|
3242 | if (idxEnd != -1)
|
---|
3243 | {
|
---|
3244 | cbThis = (idxEnd - idxStart) * 512;
|
---|
3245 | fAllocated = false;
|
---|
3246 | }
|
---|
3247 | }
|
---|
3248 | else
|
---|
3249 | {
|
---|
3250 | /* Mark as unused and check for the first set bit. */
|
---|
3251 | idxEnd = ASMBitNextSet(pBlock->pbmAllocated, cSectors, idxStart);
|
---|
3252 | if (idxEnd != -1)
|
---|
3253 | cbThis = (idxEnd - idxStart) * 512;
|
---|
3254 |
|
---|
3255 | rc = pDisk->pLast->Backend->pfnDiscard(pDisk->pLast->pBackendData, pIoCtx,
|
---|
3256 | offStart, cbThis, NULL, NULL, &cbThis,
|
---|
3257 | NULL, VD_DISCARD_MARK_UNUSED);
|
---|
3258 | if ( RT_FAILURE(rc)
|
---|
3259 | && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
3260 | break;
|
---|
3261 |
|
---|
3262 | fAllocated = true;
|
---|
3263 | }
|
---|
3264 |
|
---|
3265 | idxStart = idxEnd;
|
---|
3266 | offStart += cbThis;
|
---|
3267 | cbLeft -= cbThis;
|
---|
3268 | }
|
---|
3269 |
|
---|
3270 | if ( RT_FAILURE(rc)
|
---|
3271 | && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
3272 | break;
|
---|
3273 |
|
---|
3274 | PVDDISCARDBLOCK pBlockRemove = (PVDDISCARDBLOCK)RTAvlrU64RangeRemove(pDiscard->pTreeBlocks, pBlock->Core.Key);
|
---|
3275 | Assert(pBlockRemove == pBlock);
|
---|
3276 | RTListNodeRemove(&pBlock->NodeLru);
|
---|
3277 |
|
---|
3278 | pDiscard->cbDiscarding -= pBlock->cbDiscard;
|
---|
3279 | RTMemFree(pBlock->pbmAllocated);
|
---|
3280 | RTMemFree(pBlock);
|
---|
3281 | }
|
---|
3282 |
|
---|
3283 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
3284 | rc = VINF_SUCCESS;
|
---|
3285 |
|
---|
3286 | Assert(RT_FAILURE(rc) || pDiscard->cbDiscarding <= cbDiscardingNew);
|
---|
3287 |
|
---|
3288 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
3289 | return rc;
|
---|
3290 | }
|
---|
3291 |
|
---|
3292 | /**
|
---|
3293 | * Async discard helper - discards the current range if there is no matching
|
---|
3294 | * block in the tree.
|
---|
3295 | *
|
---|
3296 | * @returns VBox status code.
|
---|
3297 | * @param pIoCtx The I/O context to operate on.
|
---|
3298 | */
|
---|
3299 | static int vdDiscardCurrentRangeAsync(PVDIOCTX pIoCtx)
|
---|
3300 | {
|
---|
3301 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
3302 | PVDDISCARDSTATE pDiscard = pDisk->pDiscard;
|
---|
3303 | uint64_t offStart = pIoCtx->Req.Discard.offCur;
|
---|
3304 | size_t cbThisDiscard = pIoCtx->Req.Discard.cbThisDiscard;
|
---|
3305 | void *pbmAllocated = NULL;
|
---|
3306 | size_t cbPreAllocated, cbPostAllocated;
|
---|
3307 | int rc = VINF_SUCCESS;
|
---|
3308 |
|
---|
3309 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
3310 |
|
---|
3311 | /* No block found, try to discard using the backend first. */
|
---|
3312 | rc = pDisk->pLast->Backend->pfnDiscard(pDisk->pLast->pBackendData, pIoCtx,
|
---|
3313 | offStart, cbThisDiscard, &cbPreAllocated,
|
---|
3314 | &cbPostAllocated, &cbThisDiscard,
|
---|
3315 | &pbmAllocated, 0);
|
---|
3316 | if (rc == VERR_VD_DISCARD_ALIGNMENT_NOT_MET)
|
---|
3317 | {
|
---|
3318 | /* Create new discard block. */
|
---|
3319 | PVDDISCARDBLOCK pBlock = (PVDDISCARDBLOCK)RTMemAllocZ(sizeof(VDDISCARDBLOCK));
|
---|
3320 | if (pBlock)
|
---|
3321 | {
|
---|
3322 | pBlock->Core.Key = offStart - cbPreAllocated;
|
---|
3323 | pBlock->Core.KeyLast = offStart + cbThisDiscard + cbPostAllocated - 1;
|
---|
3324 | pBlock->cbDiscard = cbPreAllocated + cbThisDiscard + cbPostAllocated;
|
---|
3325 | pBlock->pbmAllocated = pbmAllocated;
|
---|
3326 | bool fInserted = RTAvlrU64Insert(pDiscard->pTreeBlocks, &pBlock->Core);
|
---|
3327 | Assert(fInserted);
|
---|
3328 |
|
---|
3329 | RTListPrepend(&pDiscard->ListLru, &pBlock->NodeLru);
|
---|
3330 | pDiscard->cbDiscarding += pBlock->cbDiscard;
|
---|
3331 |
|
---|
3332 | Assert(pIoCtx->Req.Discard.cbDiscardLeft >= cbThisDiscard);
|
---|
3333 | pIoCtx->Req.Discard.cbDiscardLeft -= cbThisDiscard;
|
---|
3334 | pIoCtx->Req.Discard.offCur += cbThisDiscard;
|
---|
3335 | pIoCtx->Req.Discard.cbThisDiscard = cbThisDiscard;
|
---|
3336 |
|
---|
3337 | if (pDiscard->cbDiscarding > VD_DISCARD_REMOVE_THRESHOLD)
|
---|
3338 | rc = vdDiscardRemoveBlocksAsync(pDisk, pIoCtx, VD_DISCARD_REMOVE_THRESHOLD);
|
---|
3339 | else
|
---|
3340 | rc = VINF_SUCCESS;
|
---|
3341 |
|
---|
3342 | if (RT_SUCCESS(rc))
|
---|
3343 | pIoCtx->pfnIoCtxTransferNext = vdDiscardHelperAsync; /* Next part. */
|
---|
3344 | }
|
---|
3345 | else
|
---|
3346 | {
|
---|
3347 | RTMemFree(pbmAllocated);
|
---|
3348 | rc = VERR_NO_MEMORY;
|
---|
3349 | }
|
---|
3350 | }
|
---|
3351 | else if ( RT_SUCCESS(rc)
|
---|
3352 | || rc == VERR_VD_ASYNC_IO_IN_PROGRESS) /* Save state and andvance to next range. */
|
---|
3353 | {
|
---|
3354 | Assert(pIoCtx->Req.Discard.cbDiscardLeft >= cbThisDiscard);
|
---|
3355 | pIoCtx->Req.Discard.cbDiscardLeft -= cbThisDiscard;
|
---|
3356 | pIoCtx->Req.Discard.offCur += cbThisDiscard;
|
---|
3357 | pIoCtx->Req.Discard.cbThisDiscard = cbThisDiscard;
|
---|
3358 | pIoCtx->pfnIoCtxTransferNext = vdDiscardHelperAsync;
|
---|
3359 | rc = VINF_SUCCESS;
|
---|
3360 | }
|
---|
3361 |
|
---|
3362 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
3363 | return rc;
|
---|
3364 | }
|
---|
3365 |
|
---|
3366 | /**
|
---|
3367 | * Async discard helper - entry point.
|
---|
3368 | *
|
---|
3369 | * @returns VBox status code.
|
---|
3370 | * @param pIoCtx The I/O context to operate on.
|
---|
3371 | */
|
---|
3372 | static int vdDiscardHelperAsync(PVDIOCTX pIoCtx)
|
---|
3373 | {
|
---|
3374 | int rc = VINF_SUCCESS;
|
---|
3375 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
3376 | PCRTRANGE paRanges = pIoCtx->Req.Discard.paRanges;
|
---|
3377 | unsigned cRanges = pIoCtx->Req.Discard.cRanges;
|
---|
3378 | PVDDISCARDSTATE pDiscard = pDisk->pDiscard;
|
---|
3379 |
|
---|
3380 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
3381 |
|
---|
3382 | /* Check if the I/O context processed all ranges. */
|
---|
3383 | if ( pIoCtx->Req.Discard.idxRange == cRanges
|
---|
3384 | && !pIoCtx->Req.Discard.cbDiscardLeft)
|
---|
3385 | {
|
---|
3386 | LogFlowFunc(("All ranges discarded, completing\n"));
|
---|
3387 | vdIoCtxUnlockDisk(pDisk, pIoCtx, true /* fProcessDeferredReqs*/);
|
---|
3388 | return VINF_SUCCESS;
|
---|
3389 | }
|
---|
3390 |
|
---|
3391 | if (pDisk->pIoCtxLockOwner != pIoCtx)
|
---|
3392 | rc = vdIoCtxLockDisk(pDisk, pIoCtx);
|
---|
3393 |
|
---|
3394 | if (RT_SUCCESS(rc))
|
---|
3395 | {
|
---|
3396 | uint64_t offStart = pIoCtx->Req.Discard.offCur;
|
---|
3397 | size_t cbDiscardLeft = pIoCtx->Req.Discard.cbDiscardLeft;
|
---|
3398 | size_t cbThisDiscard;
|
---|
3399 |
|
---|
3400 | pDisk->uOffsetStartLocked = offStart;
|
---|
3401 | pDisk->uOffsetEndLocked = offStart + cbDiscardLeft;
|
---|
3402 |
|
---|
3403 | if (RT_UNLIKELY(!pDiscard))
|
---|
3404 | {
|
---|
3405 | pDiscard = vdDiscardStateCreate();
|
---|
3406 | if (!pDiscard)
|
---|
3407 | return VERR_NO_MEMORY;
|
---|
3408 |
|
---|
3409 | pDisk->pDiscard = pDiscard;
|
---|
3410 | }
|
---|
3411 |
|
---|
3412 | if (!pIoCtx->Req.Discard.cbDiscardLeft)
|
---|
3413 | {
|
---|
3414 | offStart = paRanges[pIoCtx->Req.Discard.idxRange].offStart;
|
---|
3415 | cbDiscardLeft = paRanges[pIoCtx->Req.Discard.idxRange].cbRange;
|
---|
3416 | LogFlowFunc(("New range descriptor loaded (%u) offStart=%llu cbDiscard=%zu\n",
|
---|
3417 | pIoCtx->Req.Discard.idxRange, offStart, cbDiscardLeft));
|
---|
3418 | pIoCtx->Req.Discard.idxRange++;
|
---|
3419 | }
|
---|
3420 |
|
---|
3421 | /* Look for a matching block in the AVL tree first. */
|
---|
3422 | PVDDISCARDBLOCK pBlock = (PVDDISCARDBLOCK)RTAvlrU64GetBestFit(pDiscard->pTreeBlocks, offStart, false);
|
---|
3423 | if (!pBlock || pBlock->Core.KeyLast < offStart)
|
---|
3424 | {
|
---|
3425 | PVDDISCARDBLOCK pBlockAbove = (PVDDISCARDBLOCK)RTAvlrU64GetBestFit(pDiscard->pTreeBlocks, offStart, true);
|
---|
3426 |
|
---|
3427 | /* Clip range to remain in the current block. */
|
---|
3428 | if (pBlockAbove)
|
---|
3429 | cbThisDiscard = RT_MIN(cbDiscardLeft, pBlockAbove->Core.KeyLast - offStart + 1);
|
---|
3430 | else
|
---|
3431 | cbThisDiscard = cbDiscardLeft;
|
---|
3432 |
|
---|
3433 | Assert(!(cbThisDiscard % 512));
|
---|
3434 | pIoCtx->Req.Discard.pBlock = NULL;
|
---|
3435 | pIoCtx->pfnIoCtxTransferNext = vdDiscardCurrentRangeAsync;
|
---|
3436 | }
|
---|
3437 | else
|
---|
3438 | {
|
---|
3439 | /* Range lies partly in the block, update allocation bitmap. */
|
---|
3440 | int32_t idxStart, idxEnd;
|
---|
3441 |
|
---|
3442 | cbThisDiscard = RT_MIN(cbDiscardLeft, pBlock->Core.KeyLast - offStart + 1);
|
---|
3443 |
|
---|
3444 | AssertPtr(pBlock);
|
---|
3445 |
|
---|
3446 | Assert(!(cbThisDiscard % 512));
|
---|
3447 | Assert(!((offStart - pBlock->Core.Key) % 512));
|
---|
3448 |
|
---|
3449 | idxStart = (offStart - pBlock->Core.Key) / 512;
|
---|
3450 | idxEnd = idxStart + (int32_t)(cbThisDiscard / 512);
|
---|
3451 |
|
---|
3452 | ASMBitClearRange(pBlock->pbmAllocated, idxStart, idxEnd);
|
---|
3453 |
|
---|
3454 | cbDiscardLeft -= cbThisDiscard;
|
---|
3455 | offStart += cbThisDiscard;
|
---|
3456 |
|
---|
3457 | /* Call the backend to discard the block if it is completely unallocated now. */
|
---|
3458 | if (ASMBitFirstSet((volatile void *)pBlock->pbmAllocated, (uint32_t)(pBlock->cbDiscard / 512)) == -1)
|
---|
3459 | {
|
---|
3460 | pIoCtx->Req.Discard.pBlock = pBlock;
|
---|
3461 | pIoCtx->pfnIoCtxTransferNext = vdDiscardWholeBlockAsync;
|
---|
3462 | rc = VINF_SUCCESS;
|
---|
3463 | }
|
---|
3464 | else
|
---|
3465 | {
|
---|
3466 | RTListNodeRemove(&pBlock->NodeLru);
|
---|
3467 | RTListPrepend(&pDiscard->ListLru, &pBlock->NodeLru);
|
---|
3468 |
|
---|
3469 | /* Start with next range. */
|
---|
3470 | pIoCtx->pfnIoCtxTransferNext = vdDiscardHelperAsync;
|
---|
3471 | rc = VINF_SUCCESS;
|
---|
3472 | }
|
---|
3473 | }
|
---|
3474 |
|
---|
3475 | /* Save state in the context. */
|
---|
3476 | pIoCtx->Req.Discard.offCur = offStart;
|
---|
3477 | pIoCtx->Req.Discard.cbDiscardLeft = cbDiscardLeft;
|
---|
3478 | pIoCtx->Req.Discard.cbThisDiscard = cbThisDiscard;
|
---|
3479 | }
|
---|
3480 |
|
---|
3481 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
3482 | return rc;
|
---|
3483 | }
|
---|
3484 |
|
---|
3485 | #ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
3486 | /**
|
---|
3487 | * @copydoc VDPLUGIN::pfnRegisterImage
|
---|
3488 | */
|
---|
3489 | static DECLCALLBACK(int) vdPluginRegisterImage(void *pvUser, PCVBOXHDDBACKEND pBackend)
|
---|
3490 | {
|
---|
3491 | int rc = VINF_SUCCESS;
|
---|
3492 |
|
---|
3493 | if (pBackend->cbSize == sizeof(VBOXHDDBACKEND))
|
---|
3494 | vdAddBackend(pBackend);
|
---|
3495 | else
|
---|
3496 | {
|
---|
3497 | LogFunc(("ignored plugin: pBackend->cbSize=%d rc=%Rrc\n", pBackend->cbSize));
|
---|
3498 | rc = VERR_IGNORED;
|
---|
3499 | }
|
---|
3500 |
|
---|
3501 | return rc;
|
---|
3502 | }
|
---|
3503 |
|
---|
3504 | /**
|
---|
3505 | * @copydoc VDPLUGIN::pfnRegisterCache
|
---|
3506 | */
|
---|
3507 | static DECLCALLBACK(int) vdPluginRegisterCache(void *pvUser, PCVDCACHEBACKEND pBackend)
|
---|
3508 | {
|
---|
3509 | int rc = VINF_SUCCESS;
|
---|
3510 |
|
---|
3511 | if (pBackend->cbSize == sizeof(VDCACHEBACKEND))
|
---|
3512 | vdAddCacheBackend(pBackend);
|
---|
3513 | else
|
---|
3514 | {
|
---|
3515 | LogFunc(("ignored plugin: pBackend->cbSize=%d rc=%Rrc\n", pBackend->cbSize));
|
---|
3516 | rc = VERR_IGNORED;
|
---|
3517 | }
|
---|
3518 |
|
---|
3519 | return rc;
|
---|
3520 | }
|
---|
3521 |
|
---|
3522 | /**
|
---|
3523 | * @copydoc VDPLUGIN::pfnRegisterFilter
|
---|
3524 | */
|
---|
3525 | static DECLCALLBACK(int) vdPluginRegisterFilter(void *pvUser, PCVDFILTERBACKEND pBackend)
|
---|
3526 | {
|
---|
3527 | int rc = VINF_SUCCESS;
|
---|
3528 |
|
---|
3529 | if (pBackend->cbSize == sizeof(VDFILTERBACKEND))
|
---|
3530 | vdAddFilterBackend(pBackend);
|
---|
3531 | else
|
---|
3532 | {
|
---|
3533 | LogFunc(("ignored plugin: pBackend->cbSize=%d rc=%Rrc\n", pBackend->cbSize));
|
---|
3534 | rc = VERR_IGNORED;
|
---|
3535 | }
|
---|
3536 |
|
---|
3537 | return rc;
|
---|
3538 | }
|
---|
3539 |
|
---|
3540 | /**
|
---|
3541 | * Checks whether the given plugin filename was already loaded.
|
---|
3542 | *
|
---|
3543 | * @returns true if the plugin was already loaded, false otherwise.
|
---|
3544 | * @param pszFilename The filename to check.
|
---|
3545 | */
|
---|
3546 | static bool vdPluginFind(const char *pszFilename)
|
---|
3547 | {
|
---|
3548 | PVDPLUGIN pIt = NULL;
|
---|
3549 |
|
---|
3550 | RTListForEach(&g_ListPluginsLoaded, pIt, VDPLUGIN, NodePlugin)
|
---|
3551 | {
|
---|
3552 | if (!RTStrCmp(pIt->pszFilename, pszFilename))
|
---|
3553 | return true;
|
---|
3554 | }
|
---|
3555 |
|
---|
3556 | return false;
|
---|
3557 | }
|
---|
3558 |
|
---|
3559 | /**
|
---|
3560 | * Adds a plugin to the list of loaded plugins.
|
---|
3561 | *
|
---|
3562 | * @returns VBox status code.
|
---|
3563 | * @param hPlugin Plugin handle to add.
|
---|
3564 | * @param pszFilename The associated filename, sued for finding duplicates.
|
---|
3565 | */
|
---|
3566 | static int vdAddPlugin(RTLDRMOD hPlugin, const char *pszFilename)
|
---|
3567 | {
|
---|
3568 | int rc = VINF_SUCCESS;
|
---|
3569 | PVDPLUGIN pPlugin = (PVDPLUGIN)RTMemAllocZ(sizeof(VDPLUGIN));
|
---|
3570 |
|
---|
3571 | if (pPlugin)
|
---|
3572 | {
|
---|
3573 | pPlugin->hPlugin = hPlugin;
|
---|
3574 | pPlugin->pszFilename = RTStrDup(pszFilename);
|
---|
3575 | if (pPlugin->pszFilename)
|
---|
3576 | RTListAppend(&g_ListPluginsLoaded, &pPlugin->NodePlugin);
|
---|
3577 | else
|
---|
3578 | {
|
---|
3579 | RTMemFree(pPlugin);
|
---|
3580 | rc = VERR_NO_MEMORY;
|
---|
3581 | }
|
---|
3582 | }
|
---|
3583 | else
|
---|
3584 | rc = VERR_NO_MEMORY;
|
---|
3585 |
|
---|
3586 | return rc;
|
---|
3587 | }
|
---|
3588 | #endif
|
---|
3589 |
|
---|
3590 | /**
|
---|
3591 | * Worker for VDPluginLoadFromFilename() and vdPluginLoadFromPath().
|
---|
3592 | *
|
---|
3593 | * @returns VBox status code.
|
---|
3594 | * @param pszFilename The plugin filename to load.
|
---|
3595 | */
|
---|
3596 | static int vdPluginLoadFromFilename(const char *pszFilename)
|
---|
3597 | {
|
---|
3598 | #ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
3599 | /* Plugin loaded? Nothing to do. */
|
---|
3600 | if (vdPluginFind(pszFilename))
|
---|
3601 | return VINF_SUCCESS;
|
---|
3602 |
|
---|
3603 | RTLDRMOD hPlugin = NIL_RTLDRMOD;
|
---|
3604 | int rc = SUPR3HardenedLdrLoadPlugIn(pszFilename, &hPlugin, NULL);
|
---|
3605 | if (RT_SUCCESS(rc))
|
---|
3606 | {
|
---|
3607 | VDBACKENDREGISTER BackendRegister;
|
---|
3608 | PFNVDPLUGINLOAD pfnVDPluginLoad = NULL;
|
---|
3609 |
|
---|
3610 | BackendRegister.pfnRegisterImage = vdPluginRegisterImage;
|
---|
3611 | BackendRegister.pfnRegisterCache = vdPluginRegisterCache;
|
---|
3612 | BackendRegister.pfnRegisterFilter = vdPluginRegisterFilter;
|
---|
3613 |
|
---|
3614 | rc = RTLdrGetSymbol(hPlugin, VD_PLUGIN_LOAD_NAME, (void**)&pfnVDPluginLoad);
|
---|
3615 | if (RT_FAILURE(rc) || !pfnVDPluginLoad)
|
---|
3616 | {
|
---|
3617 | LogFunc(("error resolving the entry point %s in plugin %s, rc=%Rrc, pfnVDPluginLoad=%#p\n",
|
---|
3618 | VD_PLUGIN_LOAD_NAME, pszFilename, rc, pfnVDPluginLoad));
|
---|
3619 | if (RT_SUCCESS(rc))
|
---|
3620 | rc = VERR_SYMBOL_NOT_FOUND;
|
---|
3621 | }
|
---|
3622 |
|
---|
3623 | if (RT_SUCCESS(rc))
|
---|
3624 | {
|
---|
3625 | /* Get the function table. */
|
---|
3626 | rc = pfnVDPluginLoad(NULL, &BackendRegister);
|
---|
3627 | }
|
---|
3628 | else
|
---|
3629 | LogFunc(("ignored plugin '%s': rc=%Rrc\n", pszFilename, rc));
|
---|
3630 |
|
---|
3631 | /* Create a plugin entry on success. */
|
---|
3632 | if (RT_SUCCESS(rc))
|
---|
3633 | vdAddPlugin(hPlugin, pszFilename);
|
---|
3634 | else
|
---|
3635 | RTLdrClose(hPlugin);
|
---|
3636 | }
|
---|
3637 |
|
---|
3638 | return rc;
|
---|
3639 | #else
|
---|
3640 | return VERR_NOT_IMPLEMENTED;
|
---|
3641 | #endif
|
---|
3642 | }
|
---|
3643 |
|
---|
3644 | /**
|
---|
3645 | * Worker for VDPluginLoadFromPath() and vdLoadDynamicBackends().
|
---|
3646 | *
|
---|
3647 | * @returns VBox status code.
|
---|
3648 | * @param pszPath The path to load plugins from.
|
---|
3649 | */
|
---|
3650 | static int vdPluginLoadFromPath(const char *pszPath)
|
---|
3651 | {
|
---|
3652 | #ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
3653 | /* To get all entries with VBoxHDD as prefix. */
|
---|
3654 | char *pszPluginFilter = RTPathJoinA(pszPath, VD_PLUGIN_PREFIX "*");
|
---|
3655 | if (!pszPluginFilter)
|
---|
3656 | return VERR_NO_STR_MEMORY;
|
---|
3657 |
|
---|
3658 | PRTDIRENTRYEX pPluginDirEntry = NULL;
|
---|
3659 | PRTDIR pPluginDir = NULL;
|
---|
3660 | size_t cbPluginDirEntry = sizeof(RTDIRENTRYEX);
|
---|
3661 | int rc = RTDirOpenFiltered(&pPluginDir, pszPluginFilter, RTDIRFILTER_WINNT, 0);
|
---|
3662 | if (RT_FAILURE(rc))
|
---|
3663 | {
|
---|
3664 | /* On Windows the above immediately signals that there are no
|
---|
3665 | * files matching, while on other platforms enumerating the
|
---|
3666 | * files below fails. Either way: no plugins. */
|
---|
3667 | goto out;
|
---|
3668 | }
|
---|
3669 |
|
---|
3670 | pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(sizeof(RTDIRENTRYEX));
|
---|
3671 | if (!pPluginDirEntry)
|
---|
3672 | {
|
---|
3673 | rc = VERR_NO_MEMORY;
|
---|
3674 | goto out;
|
---|
3675 | }
|
---|
3676 |
|
---|
3677 | while ((rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK)) != VERR_NO_MORE_FILES)
|
---|
3678 | {
|
---|
3679 | char *pszPluginPath = NULL;
|
---|
3680 |
|
---|
3681 | if (rc == VERR_BUFFER_OVERFLOW)
|
---|
3682 | {
|
---|
3683 | /* allocate new buffer. */
|
---|
3684 | RTMemFree(pPluginDirEntry);
|
---|
3685 | pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(cbPluginDirEntry);
|
---|
3686 | if (!pPluginDirEntry)
|
---|
3687 | {
|
---|
3688 | rc = VERR_NO_MEMORY;
|
---|
3689 | break;
|
---|
3690 | }
|
---|
3691 | /* Retry. */
|
---|
3692 | rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
|
---|
3693 | if (RT_FAILURE(rc))
|
---|
3694 | break;
|
---|
3695 | }
|
---|
3696 | else if (RT_FAILURE(rc))
|
---|
3697 | break;
|
---|
3698 |
|
---|
3699 | /* We got the new entry. */
|
---|
3700 | if (!RTFS_IS_FILE(pPluginDirEntry->Info.Attr.fMode))
|
---|
3701 | continue;
|
---|
3702 |
|
---|
3703 | /* Prepend the path to the libraries. */
|
---|
3704 | pszPluginPath = RTPathJoinA(pszPath, pPluginDirEntry->szName);
|
---|
3705 | if (!pszPluginPath)
|
---|
3706 | {
|
---|
3707 | rc = VERR_NO_STR_MEMORY;
|
---|
3708 | break;
|
---|
3709 | }
|
---|
3710 |
|
---|
3711 | rc = vdPluginLoadFromFilename(pszPluginPath);
|
---|
3712 | RTStrFree(pszPluginPath);
|
---|
3713 | }
|
---|
3714 | out:
|
---|
3715 | if (rc == VERR_NO_MORE_FILES)
|
---|
3716 | rc = VINF_SUCCESS;
|
---|
3717 | RTStrFree(pszPluginFilter);
|
---|
3718 | if (pPluginDirEntry)
|
---|
3719 | RTMemFree(pPluginDirEntry);
|
---|
3720 | if (pPluginDir)
|
---|
3721 | RTDirClose(pPluginDir);
|
---|
3722 | return rc;
|
---|
3723 | #else
|
---|
3724 | return VERR_NOT_IMPLEMENTED;
|
---|
3725 | #endif
|
---|
3726 | }
|
---|
3727 |
|
---|
3728 | /**
|
---|
3729 | * internal: scans plugin directory and loads found plugins.
|
---|
3730 | */
|
---|
3731 | static int vdLoadDynamicBackends()
|
---|
3732 | {
|
---|
3733 | #ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
3734 | /*
|
---|
3735 | * Enumerate plugin backends from the application directory where the other
|
---|
3736 | * shared libraries are.
|
---|
3737 | */
|
---|
3738 | char szPath[RTPATH_MAX];
|
---|
3739 | int rc = RTPathAppPrivateArch(szPath, sizeof(szPath));
|
---|
3740 | if (RT_FAILURE(rc))
|
---|
3741 | return rc;
|
---|
3742 |
|
---|
3743 | return vdPluginLoadFromPath(szPath);
|
---|
3744 | #else
|
---|
3745 | return VINF_SUCCESS;
|
---|
3746 | #endif
|
---|
3747 | }
|
---|
3748 |
|
---|
3749 | /**
|
---|
3750 | * VD async I/O interface open callback.
|
---|
3751 | */
|
---|
3752 | static int vdIOOpenFallback(void *pvUser, const char *pszLocation,
|
---|
3753 | uint32_t fOpen, PFNVDCOMPLETED pfnCompleted,
|
---|
3754 | void **ppStorage)
|
---|
3755 | {
|
---|
3756 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)RTMemAllocZ(sizeof(VDIIOFALLBACKSTORAGE));
|
---|
3757 |
|
---|
3758 | if (!pStorage)
|
---|
3759 | return VERR_NO_MEMORY;
|
---|
3760 |
|
---|
3761 | pStorage->pfnCompleted = pfnCompleted;
|
---|
3762 |
|
---|
3763 | /* Open the file. */
|
---|
3764 | int rc = RTFileOpen(&pStorage->File, pszLocation, fOpen);
|
---|
3765 | if (RT_SUCCESS(rc))
|
---|
3766 | {
|
---|
3767 | *ppStorage = pStorage;
|
---|
3768 | return VINF_SUCCESS;
|
---|
3769 | }
|
---|
3770 |
|
---|
3771 | RTMemFree(pStorage);
|
---|
3772 | return rc;
|
---|
3773 | }
|
---|
3774 |
|
---|
3775 | /**
|
---|
3776 | * VD async I/O interface close callback.
|
---|
3777 | */
|
---|
3778 | static int vdIOCloseFallback(void *pvUser, void *pvStorage)
|
---|
3779 | {
|
---|
3780 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
3781 |
|
---|
3782 | RTFileClose(pStorage->File);
|
---|
3783 | RTMemFree(pStorage);
|
---|
3784 | return VINF_SUCCESS;
|
---|
3785 | }
|
---|
3786 |
|
---|
3787 | static int vdIODeleteFallback(void *pvUser, const char *pcszFilename)
|
---|
3788 | {
|
---|
3789 | return RTFileDelete(pcszFilename);
|
---|
3790 | }
|
---|
3791 |
|
---|
3792 | static int vdIOMoveFallback(void *pvUser, const char *pcszSrc, const char *pcszDst, unsigned fMove)
|
---|
3793 | {
|
---|
3794 | return RTFileMove(pcszSrc, pcszDst, fMove);
|
---|
3795 | }
|
---|
3796 |
|
---|
3797 | static int vdIOGetFreeSpaceFallback(void *pvUser, const char *pcszFilename, int64_t *pcbFreeSpace)
|
---|
3798 | {
|
---|
3799 | return RTFsQuerySizes(pcszFilename, NULL, pcbFreeSpace, NULL, NULL);
|
---|
3800 | }
|
---|
3801 |
|
---|
3802 | static int vdIOGetModificationTimeFallback(void *pvUser, const char *pcszFilename, PRTTIMESPEC pModificationTime)
|
---|
3803 | {
|
---|
3804 | RTFSOBJINFO info;
|
---|
3805 | int rc = RTPathQueryInfo(pcszFilename, &info, RTFSOBJATTRADD_NOTHING);
|
---|
3806 | if (RT_SUCCESS(rc))
|
---|
3807 | *pModificationTime = info.ModificationTime;
|
---|
3808 | return rc;
|
---|
3809 | }
|
---|
3810 |
|
---|
3811 | /**
|
---|
3812 | * VD async I/O interface callback for retrieving the file size.
|
---|
3813 | */
|
---|
3814 | static int vdIOGetSizeFallback(void *pvUser, void *pvStorage, uint64_t *pcbSize)
|
---|
3815 | {
|
---|
3816 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
3817 |
|
---|
3818 | return RTFileGetSize(pStorage->File, pcbSize);
|
---|
3819 | }
|
---|
3820 |
|
---|
3821 | /**
|
---|
3822 | * VD async I/O interface callback for setting the file size.
|
---|
3823 | */
|
---|
3824 | static int vdIOSetSizeFallback(void *pvUser, void *pvStorage, uint64_t cbSize)
|
---|
3825 | {
|
---|
3826 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
3827 |
|
---|
3828 | return RTFileSetSize(pStorage->File, cbSize);
|
---|
3829 | }
|
---|
3830 |
|
---|
3831 | /**
|
---|
3832 | * VD async I/O interface callback for a synchronous write to the file.
|
---|
3833 | */
|
---|
3834 | static int vdIOWriteSyncFallback(void *pvUser, void *pvStorage, uint64_t uOffset,
|
---|
3835 | const void *pvBuf, size_t cbWrite, size_t *pcbWritten)
|
---|
3836 | {
|
---|
3837 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
3838 |
|
---|
3839 | return RTFileWriteAt(pStorage->File, uOffset, pvBuf, cbWrite, pcbWritten);
|
---|
3840 | }
|
---|
3841 |
|
---|
3842 | /**
|
---|
3843 | * VD async I/O interface callback for a synchronous read from the file.
|
---|
3844 | */
|
---|
3845 | static int vdIOReadSyncFallback(void *pvUser, void *pvStorage, uint64_t uOffset,
|
---|
3846 | void *pvBuf, size_t cbRead, size_t *pcbRead)
|
---|
3847 | {
|
---|
3848 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
3849 |
|
---|
3850 | return RTFileReadAt(pStorage->File, uOffset, pvBuf, cbRead, pcbRead);
|
---|
3851 | }
|
---|
3852 |
|
---|
3853 | /**
|
---|
3854 | * VD async I/O interface callback for a synchronous flush of the file data.
|
---|
3855 | */
|
---|
3856 | static int vdIOFlushSyncFallback(void *pvUser, void *pvStorage)
|
---|
3857 | {
|
---|
3858 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
3859 |
|
---|
3860 | return RTFileFlush(pStorage->File);
|
---|
3861 | }
|
---|
3862 |
|
---|
3863 | /**
|
---|
3864 | * VD async I/O interface callback for a asynchronous read from the file.
|
---|
3865 | */
|
---|
3866 | static int vdIOReadAsyncFallback(void *pvUser, void *pStorage, uint64_t uOffset,
|
---|
3867 | PCRTSGSEG paSegments, size_t cSegments,
|
---|
3868 | size_t cbRead, void *pvCompletion,
|
---|
3869 | void **ppTask)
|
---|
3870 | {
|
---|
3871 | return VERR_NOT_IMPLEMENTED;
|
---|
3872 | }
|
---|
3873 |
|
---|
3874 | /**
|
---|
3875 | * VD async I/O interface callback for a asynchronous write to the file.
|
---|
3876 | */
|
---|
3877 | static int vdIOWriteAsyncFallback(void *pvUser, void *pStorage, uint64_t uOffset,
|
---|
3878 | PCRTSGSEG paSegments, size_t cSegments,
|
---|
3879 | size_t cbWrite, void *pvCompletion,
|
---|
3880 | void **ppTask)
|
---|
3881 | {
|
---|
3882 | return VERR_NOT_IMPLEMENTED;
|
---|
3883 | }
|
---|
3884 |
|
---|
3885 | /**
|
---|
3886 | * VD async I/O interface callback for a asynchronous flush of the file data.
|
---|
3887 | */
|
---|
3888 | static int vdIOFlushAsyncFallback(void *pvUser, void *pStorage,
|
---|
3889 | void *pvCompletion, void **ppTask)
|
---|
3890 | {
|
---|
3891 | return VERR_NOT_IMPLEMENTED;
|
---|
3892 | }
|
---|
3893 |
|
---|
3894 | /**
|
---|
3895 | * Internal - Continues an I/O context after
|
---|
3896 | * it was halted because of an active transfer.
|
---|
3897 | */
|
---|
3898 | static int vdIoCtxContinue(PVDIOCTX pIoCtx, int rcReq)
|
---|
3899 | {
|
---|
3900 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
3901 | int rc = VINF_SUCCESS;
|
---|
3902 |
|
---|
3903 | VD_IS_LOCKED(pDisk);
|
---|
3904 |
|
---|
3905 | if (RT_FAILURE(rcReq))
|
---|
3906 | ASMAtomicCmpXchgS32(&pIoCtx->rcReq, rcReq, VINF_SUCCESS);
|
---|
3907 |
|
---|
3908 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_BLOCKED))
|
---|
3909 | {
|
---|
3910 | /* Continue the transfer */
|
---|
3911 | rc = vdIoCtxProcessLocked(pIoCtx);
|
---|
3912 |
|
---|
3913 | if ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
3914 | && ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
|
---|
3915 | {
|
---|
3916 | LogFlowFunc(("I/O context completed pIoCtx=%#p\n", pIoCtx));
|
---|
3917 | if (pIoCtx->pIoCtxParent)
|
---|
3918 | {
|
---|
3919 | PVDIOCTX pIoCtxParent = pIoCtx->pIoCtxParent;
|
---|
3920 |
|
---|
3921 | Assert(!pIoCtxParent->pIoCtxParent);
|
---|
3922 | if (RT_FAILURE(pIoCtx->rcReq))
|
---|
3923 | ASMAtomicCmpXchgS32(&pIoCtxParent->rcReq, pIoCtx->rcReq, VINF_SUCCESS);
|
---|
3924 |
|
---|
3925 | ASMAtomicDecU32(&pIoCtxParent->cDataTransfersPending);
|
---|
3926 |
|
---|
3927 | if (pIoCtx->enmTxDir == VDIOCTXTXDIR_WRITE)
|
---|
3928 | {
|
---|
3929 | LogFlowFunc(("I/O context transferred %u bytes for the parent pIoCtxParent=%p\n",
|
---|
3930 | pIoCtx->Type.Child.cbTransferParent, pIoCtxParent));
|
---|
3931 |
|
---|
3932 | /* Update the parent state. */
|
---|
3933 | Assert(pIoCtxParent->Req.Io.cbTransferLeft >= pIoCtx->Type.Child.cbTransferParent);
|
---|
3934 | ASMAtomicSubU32(&pIoCtxParent->Req.Io.cbTransferLeft, (uint32_t)pIoCtx->Type.Child.cbTransferParent);
|
---|
3935 | }
|
---|
3936 | else
|
---|
3937 | Assert(pIoCtx->enmTxDir == VDIOCTXTXDIR_FLUSH);
|
---|
3938 |
|
---|
3939 | /*
|
---|
3940 | * A completed child write means that we finished growing the image.
|
---|
3941 | * We have to process any pending writes now.
|
---|
3942 | */
|
---|
3943 | vdIoCtxUnlockDisk(pDisk, pIoCtxParent, false /* fProcessDeferredReqs */);
|
---|
3944 |
|
---|
3945 | /* Unblock the parent */
|
---|
3946 | pIoCtxParent->fFlags &= ~VDIOCTX_FLAGS_BLOCKED;
|
---|
3947 |
|
---|
3948 | rc = vdIoCtxProcessLocked(pIoCtxParent);
|
---|
3949 |
|
---|
3950 | if ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
3951 | && ASMAtomicCmpXchgBool(&pIoCtxParent->fComplete, true, false))
|
---|
3952 | {
|
---|
3953 | LogFlowFunc(("Parent I/O context completed pIoCtxParent=%#p rcReq=%Rrc\n", pIoCtxParent, pIoCtxParent->rcReq));
|
---|
3954 | vdIoCtxRootComplete(pDisk, pIoCtxParent);
|
---|
3955 | vdThreadFinishWrite(pDisk);
|
---|
3956 | vdIoCtxFree(pDisk, pIoCtxParent);
|
---|
3957 | vdDiskProcessBlockedIoCtx(pDisk);
|
---|
3958 | }
|
---|
3959 | else if (!vdIoCtxIsDiskLockOwner(pDisk, pIoCtx))
|
---|
3960 | {
|
---|
3961 | /* Process any pending writes if the current request didn't caused another growing. */
|
---|
3962 | vdDiskProcessBlockedIoCtx(pDisk);
|
---|
3963 | }
|
---|
3964 | }
|
---|
3965 | else
|
---|
3966 | {
|
---|
3967 | if (pIoCtx->enmTxDir == VDIOCTXTXDIR_FLUSH)
|
---|
3968 | {
|
---|
3969 | vdIoCtxUnlockDisk(pDisk, pIoCtx, true /* fProcessDerredReqs */);
|
---|
3970 | vdThreadFinishWrite(pDisk);
|
---|
3971 | }
|
---|
3972 | else if ( pIoCtx->enmTxDir == VDIOCTXTXDIR_WRITE
|
---|
3973 | || pIoCtx->enmTxDir == VDIOCTXTXDIR_DISCARD)
|
---|
3974 | vdThreadFinishWrite(pDisk);
|
---|
3975 | else
|
---|
3976 | {
|
---|
3977 | Assert(pIoCtx->enmTxDir == VDIOCTXTXDIR_READ);
|
---|
3978 | vdThreadFinishRead(pDisk);
|
---|
3979 | }
|
---|
3980 |
|
---|
3981 | LogFlowFunc(("I/O context completed pIoCtx=%#p rcReq=%Rrc\n", pIoCtx, pIoCtx->rcReq));
|
---|
3982 | vdIoCtxRootComplete(pDisk, pIoCtx);
|
---|
3983 | }
|
---|
3984 |
|
---|
3985 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
3986 | }
|
---|
3987 | }
|
---|
3988 |
|
---|
3989 | return VINF_SUCCESS;
|
---|
3990 | }
|
---|
3991 |
|
---|
3992 | /**
|
---|
3993 | * Internal - Called when user transfer completed.
|
---|
3994 | */
|
---|
3995 | static int vdUserXferCompleted(PVDIOSTORAGE pIoStorage, PVDIOCTX pIoCtx,
|
---|
3996 | PFNVDXFERCOMPLETED pfnComplete, void *pvUser,
|
---|
3997 | size_t cbTransfer, int rcReq)
|
---|
3998 | {
|
---|
3999 | int rc = VINF_SUCCESS;
|
---|
4000 | bool fIoCtxContinue = true;
|
---|
4001 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
4002 |
|
---|
4003 | LogFlowFunc(("pIoStorage=%#p pIoCtx=%#p pfnComplete=%#p pvUser=%#p cbTransfer=%zu rcReq=%Rrc\n",
|
---|
4004 | pIoStorage, pIoCtx, pfnComplete, pvUser, cbTransfer, rcReq));
|
---|
4005 |
|
---|
4006 | VD_IS_LOCKED(pDisk);
|
---|
4007 |
|
---|
4008 | Assert(pIoCtx->Req.Io.cbTransferLeft >= cbTransfer);
|
---|
4009 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbTransfer); Assert(cbTransfer == (uint32_t)cbTransfer);
|
---|
4010 | ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
|
---|
4011 |
|
---|
4012 | if (pfnComplete)
|
---|
4013 | rc = pfnComplete(pIoStorage->pVDIo->pBackendData, pIoCtx, pvUser, rcReq);
|
---|
4014 |
|
---|
4015 | if (RT_SUCCESS(rc))
|
---|
4016 | rc = vdIoCtxContinue(pIoCtx, rcReq);
|
---|
4017 | else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
4018 | rc = VINF_SUCCESS;
|
---|
4019 |
|
---|
4020 | return rc;
|
---|
4021 | }
|
---|
4022 |
|
---|
4023 | static void vdIoCtxContinueDeferredList(PVDIOSTORAGE pIoStorage, PRTLISTANCHOR pListWaiting,
|
---|
4024 | PFNVDXFERCOMPLETED pfnComplete, void *pvUser, int rcReq)
|
---|
4025 | {
|
---|
4026 | LogFlowFunc(("pIoStorage=%#p pListWaiting=%#p pfnComplete=%#p pvUser=%#p rcReq=%Rrc\n",
|
---|
4027 | pIoStorage, pListWaiting, pfnComplete, pvUser, rcReq));
|
---|
4028 |
|
---|
4029 | /* Go through the waiting list and continue the I/O contexts. */
|
---|
4030 | while (!RTListIsEmpty(pListWaiting))
|
---|
4031 | {
|
---|
4032 | int rc = VINF_SUCCESS;
|
---|
4033 | PVDIOCTXDEFERRED pDeferred = RTListGetFirst(pListWaiting, VDIOCTXDEFERRED, NodeDeferred);
|
---|
4034 | PVDIOCTX pIoCtx = pDeferred->pIoCtx;
|
---|
4035 | RTListNodeRemove(&pDeferred->NodeDeferred);
|
---|
4036 |
|
---|
4037 | RTMemFree(pDeferred);
|
---|
4038 | ASMAtomicDecU32(&pIoCtx->cMetaTransfersPending);
|
---|
4039 |
|
---|
4040 | if (pfnComplete)
|
---|
4041 | rc = pfnComplete(pIoStorage->pVDIo->pBackendData, pIoCtx, pvUser, rcReq);
|
---|
4042 |
|
---|
4043 | LogFlow(("Completion callback for I/O context %#p returned %Rrc\n", pIoCtx, rc));
|
---|
4044 |
|
---|
4045 | if (RT_SUCCESS(rc))
|
---|
4046 | {
|
---|
4047 | rc = vdIoCtxContinue(pIoCtx, rcReq);
|
---|
4048 | AssertRC(rc);
|
---|
4049 | }
|
---|
4050 | else
|
---|
4051 | Assert(rc == VERR_VD_ASYNC_IO_IN_PROGRESS);
|
---|
4052 | }
|
---|
4053 | }
|
---|
4054 |
|
---|
4055 | /**
|
---|
4056 | * Internal - Called when a meta transfer completed.
|
---|
4057 | */
|
---|
4058 | static int vdMetaXferCompleted(PVDIOSTORAGE pIoStorage, PFNVDXFERCOMPLETED pfnComplete, void *pvUser,
|
---|
4059 | PVDMETAXFER pMetaXfer, int rcReq)
|
---|
4060 | {
|
---|
4061 | PVBOXHDD pDisk = pIoStorage->pVDIo->pDisk;
|
---|
4062 | RTLISTNODE ListIoCtxWaiting;
|
---|
4063 | bool fFlush;
|
---|
4064 |
|
---|
4065 | LogFlowFunc(("pIoStorage=%#p pfnComplete=%#p pvUser=%#p pMetaXfer=%#p rcReq=%Rrc\n",
|
---|
4066 | pIoStorage, pfnComplete, pvUser, pMetaXfer, rcReq));
|
---|
4067 |
|
---|
4068 | VD_IS_LOCKED(pDisk);
|
---|
4069 |
|
---|
4070 | fFlush = VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_FLUSH;
|
---|
4071 |
|
---|
4072 | if (!fFlush)
|
---|
4073 | {
|
---|
4074 | RTListMove(&ListIoCtxWaiting, &pMetaXfer->ListIoCtxWaiting);
|
---|
4075 |
|
---|
4076 | if (RT_FAILURE(rcReq))
|
---|
4077 | {
|
---|
4078 | /* Remove from the AVL tree. */
|
---|
4079 | LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
|
---|
4080 | bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
|
---|
4081 | Assert(fRemoved);
|
---|
4082 | /* If this was a write check if there is a shadow buffer with updated data. */
|
---|
4083 | if (pMetaXfer->pbDataShw)
|
---|
4084 | {
|
---|
4085 | Assert(VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_WRITE);
|
---|
4086 | Assert(!RTListIsEmpty(&pMetaXfer->ListIoCtxShwWrites));
|
---|
4087 | RTListConcatenate(&ListIoCtxWaiting, &pMetaXfer->ListIoCtxShwWrites);
|
---|
4088 | RTMemFree(pMetaXfer->pbDataShw);
|
---|
4089 | pMetaXfer->pbDataShw = NULL;
|
---|
4090 | }
|
---|
4091 | RTMemFree(pMetaXfer);
|
---|
4092 | }
|
---|
4093 | else
|
---|
4094 | {
|
---|
4095 | /* Increase the reference counter to make sure it doesn't go away before the last context is processed. */
|
---|
4096 | pMetaXfer->cRefs++;
|
---|
4097 | }
|
---|
4098 | }
|
---|
4099 | else
|
---|
4100 | RTListMove(&ListIoCtxWaiting, &pMetaXfer->ListIoCtxWaiting);
|
---|
4101 |
|
---|
4102 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
|
---|
4103 | vdIoCtxContinueDeferredList(pIoStorage, &ListIoCtxWaiting, pfnComplete, pvUser, rcReq);
|
---|
4104 |
|
---|
4105 | /*
|
---|
4106 | * If there is a shadow buffer and the previous write was successful update with the
|
---|
4107 | * new data and trigger a new write.
|
---|
4108 | */
|
---|
4109 | if ( pMetaXfer->pbDataShw
|
---|
4110 | && RT_SUCCESS(rcReq)
|
---|
4111 | && VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE)
|
---|
4112 | {
|
---|
4113 | LogFlowFunc(("pMetaXfer=%#p Updating from shadow buffer and triggering new write\n", pMetaXfer));
|
---|
4114 | memcpy(pMetaXfer->abData, pMetaXfer->pbDataShw, pMetaXfer->cbMeta);
|
---|
4115 | RTMemFree(pMetaXfer->pbDataShw);
|
---|
4116 | pMetaXfer->pbDataShw = NULL;
|
---|
4117 | Assert(!RTListIsEmpty(&pMetaXfer->ListIoCtxShwWrites));
|
---|
4118 |
|
---|
4119 | /* Setup a new I/O write. */
|
---|
4120 | PVDIOTASK pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvUser, pMetaXfer);
|
---|
4121 | if (RT_LIKELY(pIoTask))
|
---|
4122 | {
|
---|
4123 | void *pvTask = NULL;
|
---|
4124 | RTSGSEG Seg;
|
---|
4125 |
|
---|
4126 | Seg.cbSeg = pMetaXfer->cbMeta;
|
---|
4127 | Seg.pvSeg = pMetaXfer->abData;
|
---|
4128 |
|
---|
4129 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_WRITE);
|
---|
4130 | rcReq = pIoStorage->pVDIo->pInterfaceIo->pfnWriteAsync(pIoStorage->pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4131 | pIoStorage->pStorage,
|
---|
4132 | pMetaXfer->Core.Key, &Seg, 1,
|
---|
4133 | pMetaXfer->cbMeta, pIoTask,
|
---|
4134 | &pvTask);
|
---|
4135 | if ( RT_SUCCESS(rcReq)
|
---|
4136 | || rcReq != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
4137 | {
|
---|
4138 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
|
---|
4139 | vdIoTaskFree(pDisk, pIoTask);
|
---|
4140 | }
|
---|
4141 | else
|
---|
4142 | RTListMove(&pMetaXfer->ListIoCtxWaiting, &pMetaXfer->ListIoCtxShwWrites);
|
---|
4143 | }
|
---|
4144 | else
|
---|
4145 | rcReq = VERR_NO_MEMORY;
|
---|
4146 |
|
---|
4147 | /* Cleanup if there was an error or the request completed already. */
|
---|
4148 | if (rcReq != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
4149 | vdIoCtxContinueDeferredList(pIoStorage, &pMetaXfer->ListIoCtxShwWrites, pfnComplete, pvUser, rcReq);
|
---|
4150 | }
|
---|
4151 |
|
---|
4152 | /* Remove if not used anymore. */
|
---|
4153 | if (!fFlush)
|
---|
4154 | {
|
---|
4155 | pMetaXfer->cRefs--;
|
---|
4156 | if (!pMetaXfer->cRefs && RTListIsEmpty(&pMetaXfer->ListIoCtxWaiting))
|
---|
4157 | {
|
---|
4158 | /* Remove from the AVL tree. */
|
---|
4159 | LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
|
---|
4160 | bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
|
---|
4161 | Assert(fRemoved);
|
---|
4162 | RTMemFree(pMetaXfer);
|
---|
4163 | }
|
---|
4164 | }
|
---|
4165 | else if (fFlush)
|
---|
4166 | RTMemFree(pMetaXfer);
|
---|
4167 |
|
---|
4168 | return VINF_SUCCESS;
|
---|
4169 | }
|
---|
4170 |
|
---|
4171 | /**
|
---|
4172 | * Processes a list of waiting I/O tasks. The disk lock must be held by caller.
|
---|
4173 | *
|
---|
4174 | * @returns nothing.
|
---|
4175 | * @param pDisk The disk to process the list for.
|
---|
4176 | */
|
---|
4177 | static void vdIoTaskProcessWaitingList(PVBOXHDD pDisk)
|
---|
4178 | {
|
---|
4179 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
4180 |
|
---|
4181 | VD_IS_LOCKED(pDisk);
|
---|
4182 |
|
---|
4183 | PVDIOTASK pHead = ASMAtomicXchgPtrT(&pDisk->pIoTasksPendingHead, NULL, PVDIOTASK);
|
---|
4184 |
|
---|
4185 | Log(("I/O task list cleared\n"));
|
---|
4186 |
|
---|
4187 | /* Reverse order. */
|
---|
4188 | PVDIOTASK pCur = pHead;
|
---|
4189 | pHead = NULL;
|
---|
4190 | while (pCur)
|
---|
4191 | {
|
---|
4192 | PVDIOTASK pInsert = pCur;
|
---|
4193 | pCur = pCur->pNext;
|
---|
4194 | pInsert->pNext = pHead;
|
---|
4195 | pHead = pInsert;
|
---|
4196 | }
|
---|
4197 |
|
---|
4198 | while (pHead)
|
---|
4199 | {
|
---|
4200 | PVDIOSTORAGE pIoStorage = pHead->pIoStorage;
|
---|
4201 |
|
---|
4202 | if (!pHead->fMeta)
|
---|
4203 | vdUserXferCompleted(pIoStorage, pHead->Type.User.pIoCtx,
|
---|
4204 | pHead->pfnComplete, pHead->pvUser,
|
---|
4205 | pHead->Type.User.cbTransfer, pHead->rcReq);
|
---|
4206 | else
|
---|
4207 | vdMetaXferCompleted(pIoStorage, pHead->pfnComplete, pHead->pvUser,
|
---|
4208 | pHead->Type.Meta.pMetaXfer, pHead->rcReq);
|
---|
4209 |
|
---|
4210 | pCur = pHead;
|
---|
4211 | pHead = pHead->pNext;
|
---|
4212 | vdIoTaskFree(pDisk, pCur);
|
---|
4213 | }
|
---|
4214 | }
|
---|
4215 |
|
---|
4216 | /**
|
---|
4217 | * Process any I/O context on the halted list.
|
---|
4218 | *
|
---|
4219 | * @returns nothing.
|
---|
4220 | * @param pDisk The disk.
|
---|
4221 | */
|
---|
4222 | static void vdIoCtxProcessHaltedList(PVBOXHDD pDisk)
|
---|
4223 | {
|
---|
4224 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
4225 |
|
---|
4226 | VD_IS_LOCKED(pDisk);
|
---|
4227 |
|
---|
4228 | /* Get the waiting list and process it in FIFO order. */
|
---|
4229 | PVDIOCTX pIoCtxHead = ASMAtomicXchgPtrT(&pDisk->pIoCtxHaltedHead, NULL, PVDIOCTX);
|
---|
4230 |
|
---|
4231 | /* Reverse it. */
|
---|
4232 | PVDIOCTX pCur = pIoCtxHead;
|
---|
4233 | pIoCtxHead = NULL;
|
---|
4234 | while (pCur)
|
---|
4235 | {
|
---|
4236 | PVDIOCTX pInsert = pCur;
|
---|
4237 | pCur = pCur->pIoCtxNext;
|
---|
4238 | pInsert->pIoCtxNext = pIoCtxHead;
|
---|
4239 | pIoCtxHead = pInsert;
|
---|
4240 | }
|
---|
4241 |
|
---|
4242 | /* Process now. */
|
---|
4243 | pCur = pIoCtxHead;
|
---|
4244 | while (pCur)
|
---|
4245 | {
|
---|
4246 | PVDIOCTX pTmp = pCur;
|
---|
4247 |
|
---|
4248 | pCur = pCur->pIoCtxNext;
|
---|
4249 | pTmp->pIoCtxNext = NULL;
|
---|
4250 |
|
---|
4251 | /* Continue */
|
---|
4252 | pTmp->fFlags &= ~VDIOCTX_FLAGS_BLOCKED;
|
---|
4253 | vdIoCtxContinue(pTmp, pTmp->rcReq);
|
---|
4254 | }
|
---|
4255 | }
|
---|
4256 |
|
---|
4257 | /**
|
---|
4258 | * Unlock the disk and process pending tasks.
|
---|
4259 | *
|
---|
4260 | * @returns VBox status code.
|
---|
4261 | * @param pDisk The disk to unlock.
|
---|
4262 | */
|
---|
4263 | static int vdDiskUnlock(PVBOXHDD pDisk, PVDIOCTX pIoCtxRc)
|
---|
4264 | {
|
---|
4265 | int rc = VINF_SUCCESS;
|
---|
4266 |
|
---|
4267 | VD_IS_LOCKED(pDisk);
|
---|
4268 |
|
---|
4269 | /*
|
---|
4270 | * Process the list of waiting I/O tasks first
|
---|
4271 | * because they might complete I/O contexts.
|
---|
4272 | * Same for the list of halted I/O contexts.
|
---|
4273 | * Afterwards comes the list of new I/O contexts.
|
---|
4274 | */
|
---|
4275 | vdIoTaskProcessWaitingList(pDisk);
|
---|
4276 | vdIoCtxProcessHaltedList(pDisk);
|
---|
4277 | rc = vdDiskProcessWaitingIoCtx(pDisk, pIoCtxRc);
|
---|
4278 | ASMAtomicXchgBool(&pDisk->fLocked, false);
|
---|
4279 |
|
---|
4280 | /*
|
---|
4281 | * Need to check for new I/O tasks and waiting I/O contexts now
|
---|
4282 | * again as other threads might added them while we processed
|
---|
4283 | * previous lists.
|
---|
4284 | */
|
---|
4285 | while ( ASMAtomicUoReadPtrT(&pDisk->pIoCtxHead, PVDIOCTX) != NULL
|
---|
4286 | || ASMAtomicUoReadPtrT(&pDisk->pIoTasksPendingHead, PVDIOTASK) != NULL
|
---|
4287 | || ASMAtomicUoReadPtrT(&pDisk->pIoCtxHaltedHead, PVDIOCTX) != NULL)
|
---|
4288 | {
|
---|
4289 | /* Try lock disk again. */
|
---|
4290 | if (ASMAtomicCmpXchgBool(&pDisk->fLocked, true, false))
|
---|
4291 | {
|
---|
4292 | vdIoTaskProcessWaitingList(pDisk);
|
---|
4293 | vdIoCtxProcessHaltedList(pDisk);
|
---|
4294 | vdDiskProcessWaitingIoCtx(pDisk, NULL);
|
---|
4295 | ASMAtomicXchgBool(&pDisk->fLocked, false);
|
---|
4296 | }
|
---|
4297 | else /* Let the other thread everything when he unlocks the disk. */
|
---|
4298 | break;
|
---|
4299 | }
|
---|
4300 |
|
---|
4301 | return rc;
|
---|
4302 | }
|
---|
4303 |
|
---|
4304 | /**
|
---|
4305 | * Try to lock the disk to complete pressing of the I/O task.
|
---|
4306 | * The completion is deferred if the disk is locked already.
|
---|
4307 | *
|
---|
4308 | * @returns nothing.
|
---|
4309 | * @param pIoTask The I/O task to complete.
|
---|
4310 | */
|
---|
4311 | static void vdXferTryLockDiskDeferIoTask(PVDIOTASK pIoTask)
|
---|
4312 | {
|
---|
4313 | PVDIOSTORAGE pIoStorage = pIoTask->pIoStorage;
|
---|
4314 | PVBOXHDD pDisk = pIoStorage->pVDIo->pDisk;
|
---|
4315 |
|
---|
4316 | Log(("Deferring I/O task pIoTask=%p\n", pIoTask));
|
---|
4317 |
|
---|
4318 | /* Put it on the waiting list. */
|
---|
4319 | PVDIOTASK pNext = ASMAtomicUoReadPtrT(&pDisk->pIoTasksPendingHead, PVDIOTASK);
|
---|
4320 | PVDIOTASK pHeadOld;
|
---|
4321 | pIoTask->pNext = pNext;
|
---|
4322 | while (!ASMAtomicCmpXchgExPtr(&pDisk->pIoTasksPendingHead, pIoTask, pNext, &pHeadOld))
|
---|
4323 | {
|
---|
4324 | pNext = pHeadOld;
|
---|
4325 | Assert(pNext != pIoTask);
|
---|
4326 | pIoTask->pNext = pNext;
|
---|
4327 | ASMNopPause();
|
---|
4328 | }
|
---|
4329 |
|
---|
4330 | if (ASMAtomicCmpXchgBool(&pDisk->fLocked, true, false))
|
---|
4331 | {
|
---|
4332 | /* Release disk lock, it will take care of processing all lists. */
|
---|
4333 | vdDiskUnlock(pDisk, NULL);
|
---|
4334 | }
|
---|
4335 | }
|
---|
4336 |
|
---|
4337 | static int vdIOIntReqCompleted(void *pvUser, int rcReq)
|
---|
4338 | {
|
---|
4339 | PVDIOTASK pIoTask = (PVDIOTASK)pvUser;
|
---|
4340 |
|
---|
4341 | LogFlowFunc(("Task completed pIoTask=%#p\n", pIoTask));
|
---|
4342 |
|
---|
4343 | pIoTask->rcReq = rcReq;
|
---|
4344 | vdXferTryLockDiskDeferIoTask(pIoTask);
|
---|
4345 | return VINF_SUCCESS;
|
---|
4346 | }
|
---|
4347 |
|
---|
4348 | /**
|
---|
4349 | * VD I/O interface callback for opening a file.
|
---|
4350 | */
|
---|
4351 | static int vdIOIntOpen(void *pvUser, const char *pszLocation,
|
---|
4352 | unsigned uOpenFlags, PPVDIOSTORAGE ppIoStorage)
|
---|
4353 | {
|
---|
4354 | int rc = VINF_SUCCESS;
|
---|
4355 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4356 | PVDIOSTORAGE pIoStorage = (PVDIOSTORAGE)RTMemAllocZ(sizeof(VDIOSTORAGE));
|
---|
4357 |
|
---|
4358 | if (!pIoStorage)
|
---|
4359 | return VERR_NO_MEMORY;
|
---|
4360 |
|
---|
4361 | /* Create the AVl tree. */
|
---|
4362 | pIoStorage->pTreeMetaXfers = (PAVLRFOFFTREE)RTMemAllocZ(sizeof(AVLRFOFFTREE));
|
---|
4363 | if (pIoStorage->pTreeMetaXfers)
|
---|
4364 | {
|
---|
4365 | rc = pVDIo->pInterfaceIo->pfnOpen(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4366 | pszLocation, uOpenFlags,
|
---|
4367 | vdIOIntReqCompleted,
|
---|
4368 | &pIoStorage->pStorage);
|
---|
4369 | if (RT_SUCCESS(rc))
|
---|
4370 | {
|
---|
4371 | pIoStorage->pVDIo = pVDIo;
|
---|
4372 | *ppIoStorage = pIoStorage;
|
---|
4373 | return VINF_SUCCESS;
|
---|
4374 | }
|
---|
4375 |
|
---|
4376 | RTMemFree(pIoStorage->pTreeMetaXfers);
|
---|
4377 | }
|
---|
4378 | else
|
---|
4379 | rc = VERR_NO_MEMORY;
|
---|
4380 |
|
---|
4381 | RTMemFree(pIoStorage);
|
---|
4382 | return rc;
|
---|
4383 | }
|
---|
4384 |
|
---|
4385 | static int vdIOIntTreeMetaXferDestroy(PAVLRFOFFNODECORE pNode, void *pvUser)
|
---|
4386 | {
|
---|
4387 | AssertMsgFailed(("Tree should be empty at this point!\n"));
|
---|
4388 | return VINF_SUCCESS;
|
---|
4389 | }
|
---|
4390 |
|
---|
4391 | static int vdIOIntClose(void *pvUser, PVDIOSTORAGE pIoStorage)
|
---|
4392 | {
|
---|
4393 | int rc = VINF_SUCCESS;
|
---|
4394 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4395 |
|
---|
4396 | /* We free everything here, even if closing the file failed for some reason. */
|
---|
4397 | rc = pVDIo->pInterfaceIo->pfnClose(pVDIo->pInterfaceIo->Core.pvUser, pIoStorage->pStorage);
|
---|
4398 | RTAvlrFileOffsetDestroy(pIoStorage->pTreeMetaXfers, vdIOIntTreeMetaXferDestroy, NULL);
|
---|
4399 | RTMemFree(pIoStorage->pTreeMetaXfers);
|
---|
4400 | RTMemFree(pIoStorage);
|
---|
4401 | return rc;
|
---|
4402 | }
|
---|
4403 |
|
---|
4404 | static int vdIOIntDelete(void *pvUser, const char *pcszFilename)
|
---|
4405 | {
|
---|
4406 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4407 | return pVDIo->pInterfaceIo->pfnDelete(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4408 | pcszFilename);
|
---|
4409 | }
|
---|
4410 |
|
---|
4411 | static int vdIOIntMove(void *pvUser, const char *pcszSrc, const char *pcszDst,
|
---|
4412 | unsigned fMove)
|
---|
4413 | {
|
---|
4414 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4415 | return pVDIo->pInterfaceIo->pfnMove(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4416 | pcszSrc, pcszDst, fMove);
|
---|
4417 | }
|
---|
4418 |
|
---|
4419 | static int vdIOIntGetFreeSpace(void *pvUser, const char *pcszFilename,
|
---|
4420 | int64_t *pcbFreeSpace)
|
---|
4421 | {
|
---|
4422 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4423 | return pVDIo->pInterfaceIo->pfnGetFreeSpace(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4424 | pcszFilename, pcbFreeSpace);
|
---|
4425 | }
|
---|
4426 |
|
---|
4427 | static int vdIOIntGetModificationTime(void *pvUser, const char *pcszFilename,
|
---|
4428 | PRTTIMESPEC pModificationTime)
|
---|
4429 | {
|
---|
4430 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4431 | return pVDIo->pInterfaceIo->pfnGetModificationTime(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4432 | pcszFilename, pModificationTime);
|
---|
4433 | }
|
---|
4434 |
|
---|
4435 | static int vdIOIntGetSize(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
4436 | uint64_t *pcbSize)
|
---|
4437 | {
|
---|
4438 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4439 | return pVDIo->pInterfaceIo->pfnGetSize(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4440 | pIoStorage->pStorage, pcbSize);
|
---|
4441 | }
|
---|
4442 |
|
---|
4443 | static int vdIOIntSetSize(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
4444 | uint64_t cbSize)
|
---|
4445 | {
|
---|
4446 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4447 | return pVDIo->pInterfaceIo->pfnSetSize(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4448 | pIoStorage->pStorage, cbSize);
|
---|
4449 | }
|
---|
4450 |
|
---|
4451 | static int vdIOIntReadUser(void *pvUser, PVDIOSTORAGE pIoStorage, uint64_t uOffset,
|
---|
4452 | PVDIOCTX pIoCtx, size_t cbRead)
|
---|
4453 | {
|
---|
4454 | int rc = VINF_SUCCESS;
|
---|
4455 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4456 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
4457 |
|
---|
4458 | LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pIoCtx=%#p cbRead=%u\n",
|
---|
4459 | pvUser, pIoStorage, uOffset, pIoCtx, cbRead));
|
---|
4460 |
|
---|
4461 | /** @todo: Enable check for sync I/O later. */
|
---|
4462 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
4463 | VD_IS_LOCKED(pDisk);
|
---|
4464 |
|
---|
4465 | Assert(cbRead > 0);
|
---|
4466 |
|
---|
4467 | if (pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC)
|
---|
4468 | {
|
---|
4469 | RTSGSEG Seg;
|
---|
4470 | unsigned cSegments = 1;
|
---|
4471 | size_t cbTaskRead = 0;
|
---|
4472 |
|
---|
4473 | /* Synchronous I/O contexts only have one buffer segment. */
|
---|
4474 | AssertMsgReturn(pIoCtx->Req.Io.SgBuf.cSegs == 1,
|
---|
4475 | ("Invalid number of buffer segments for synchronous I/O context"),
|
---|
4476 | VERR_INVALID_PARAMETER);
|
---|
4477 |
|
---|
4478 | cbTaskRead = RTSgBufSegArrayCreate(&pIoCtx->Req.Io.SgBuf, &Seg, &cSegments, cbRead);
|
---|
4479 | Assert(cbRead == cbTaskRead);
|
---|
4480 | Assert(cSegments == 1);
|
---|
4481 | rc = pVDIo->pInterfaceIo->pfnReadSync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4482 | pIoStorage->pStorage, uOffset,
|
---|
4483 | Seg.pvSeg, cbRead, NULL);
|
---|
4484 | if (RT_SUCCESS(rc))
|
---|
4485 | {
|
---|
4486 | Assert(cbRead == (uint32_t)cbRead);
|
---|
4487 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbRead);
|
---|
4488 | }
|
---|
4489 | }
|
---|
4490 | else
|
---|
4491 | {
|
---|
4492 | /* Build the S/G array and spawn a new I/O task */
|
---|
4493 | while (cbRead)
|
---|
4494 | {
|
---|
4495 | RTSGSEG aSeg[VD_IO_TASK_SEGMENTS_MAX];
|
---|
4496 | unsigned cSegments = VD_IO_TASK_SEGMENTS_MAX;
|
---|
4497 | size_t cbTaskRead = RTSgBufSegArrayCreate(&pIoCtx->Req.Io.SgBuf, aSeg, &cSegments, cbRead);
|
---|
4498 |
|
---|
4499 | Assert(cSegments > 0);
|
---|
4500 | Assert(cbTaskRead > 0);
|
---|
4501 | AssertMsg(cbTaskRead <= cbRead, ("Invalid number of bytes to read\n"));
|
---|
4502 |
|
---|
4503 | LogFlow(("Reading %u bytes into %u segments\n", cbTaskRead, cSegments));
|
---|
4504 |
|
---|
4505 | #ifdef RT_STRICT
|
---|
4506 | for (unsigned i = 0; i < cSegments; i++)
|
---|
4507 | AssertMsg(aSeg[i].pvSeg && !(aSeg[i].cbSeg % 512),
|
---|
4508 | ("Segment %u is invalid\n", i));
|
---|
4509 | #endif
|
---|
4510 |
|
---|
4511 | Assert(cbTaskRead == (uint32_t)cbTaskRead);
|
---|
4512 | PVDIOTASK pIoTask = vdIoTaskUserAlloc(pIoStorage, NULL, NULL, pIoCtx, (uint32_t)cbTaskRead);
|
---|
4513 |
|
---|
4514 | if (!pIoTask)
|
---|
4515 | return VERR_NO_MEMORY;
|
---|
4516 |
|
---|
4517 | ASMAtomicIncU32(&pIoCtx->cDataTransfersPending);
|
---|
4518 |
|
---|
4519 | void *pvTask;
|
---|
4520 | Log(("Spawning pIoTask=%p pIoCtx=%p\n", pIoTask, pIoCtx));
|
---|
4521 | rc = pVDIo->pInterfaceIo->pfnReadAsync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4522 | pIoStorage->pStorage, uOffset,
|
---|
4523 | aSeg, cSegments, cbTaskRead, pIoTask,
|
---|
4524 | &pvTask);
|
---|
4525 | if (RT_SUCCESS(rc))
|
---|
4526 | {
|
---|
4527 | AssertMsg(cbTaskRead <= pIoCtx->Req.Io.cbTransferLeft, ("Impossible!\n"));
|
---|
4528 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbTaskRead);
|
---|
4529 | ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
|
---|
4530 | vdIoTaskFree(pDisk, pIoTask);
|
---|
4531 | }
|
---|
4532 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
4533 | {
|
---|
4534 | ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
|
---|
4535 | vdIoTaskFree(pDisk, pIoTask);
|
---|
4536 | break;
|
---|
4537 | }
|
---|
4538 |
|
---|
4539 | uOffset += cbTaskRead;
|
---|
4540 | cbRead -= cbTaskRead;
|
---|
4541 | }
|
---|
4542 | }
|
---|
4543 |
|
---|
4544 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
4545 | return rc;
|
---|
4546 | }
|
---|
4547 |
|
---|
4548 | static int vdIOIntWriteUser(void *pvUser, PVDIOSTORAGE pIoStorage, uint64_t uOffset,
|
---|
4549 | PVDIOCTX pIoCtx, size_t cbWrite, PFNVDXFERCOMPLETED pfnComplete,
|
---|
4550 | void *pvCompleteUser)
|
---|
4551 | {
|
---|
4552 | int rc = VINF_SUCCESS;
|
---|
4553 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4554 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
4555 |
|
---|
4556 | LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pIoCtx=%#p cbWrite=%u\n",
|
---|
4557 | pvUser, pIoStorage, uOffset, pIoCtx, cbWrite));
|
---|
4558 |
|
---|
4559 | /** @todo: Enable check for sync I/O later. */
|
---|
4560 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
4561 | VD_IS_LOCKED(pDisk);
|
---|
4562 |
|
---|
4563 | Assert(cbWrite > 0);
|
---|
4564 |
|
---|
4565 | if (pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC)
|
---|
4566 | {
|
---|
4567 | RTSGSEG Seg;
|
---|
4568 | unsigned cSegments = 1;
|
---|
4569 | size_t cbTaskWrite = 0;
|
---|
4570 |
|
---|
4571 | /* Synchronous I/O contexts only have one buffer segment. */
|
---|
4572 | AssertMsgReturn(pIoCtx->Req.Io.SgBuf.cSegs == 1,
|
---|
4573 | ("Invalid number of buffer segments for synchronous I/O context"),
|
---|
4574 | VERR_INVALID_PARAMETER);
|
---|
4575 |
|
---|
4576 | cbTaskWrite = RTSgBufSegArrayCreate(&pIoCtx->Req.Io.SgBuf, &Seg, &cSegments, cbWrite);
|
---|
4577 | Assert(cbWrite == cbTaskWrite);
|
---|
4578 | Assert(cSegments == 1);
|
---|
4579 | rc = pVDIo->pInterfaceIo->pfnWriteSync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4580 | pIoStorage->pStorage, uOffset,
|
---|
4581 | Seg.pvSeg, cbWrite, NULL);
|
---|
4582 | if (RT_SUCCESS(rc))
|
---|
4583 | {
|
---|
4584 | Assert(pIoCtx->Req.Io.cbTransferLeft >= cbWrite);
|
---|
4585 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbWrite);
|
---|
4586 | }
|
---|
4587 | }
|
---|
4588 | else
|
---|
4589 | {
|
---|
4590 | /* Build the S/G array and spawn a new I/O task */
|
---|
4591 | while (cbWrite)
|
---|
4592 | {
|
---|
4593 | RTSGSEG aSeg[VD_IO_TASK_SEGMENTS_MAX];
|
---|
4594 | unsigned cSegments = VD_IO_TASK_SEGMENTS_MAX;
|
---|
4595 | size_t cbTaskWrite = 0;
|
---|
4596 |
|
---|
4597 | cbTaskWrite = RTSgBufSegArrayCreate(&pIoCtx->Req.Io.SgBuf, aSeg, &cSegments, cbWrite);
|
---|
4598 |
|
---|
4599 | Assert(cSegments > 0);
|
---|
4600 | Assert(cbTaskWrite > 0);
|
---|
4601 | AssertMsg(cbTaskWrite <= cbWrite, ("Invalid number of bytes to write\n"));
|
---|
4602 |
|
---|
4603 | LogFlow(("Writing %u bytes from %u segments\n", cbTaskWrite, cSegments));
|
---|
4604 |
|
---|
4605 | #ifdef DEBUG
|
---|
4606 | for (unsigned i = 0; i < cSegments; i++)
|
---|
4607 | AssertMsg(aSeg[i].pvSeg && !(aSeg[i].cbSeg % 512),
|
---|
4608 | ("Segment %u is invalid\n", i));
|
---|
4609 | #endif
|
---|
4610 |
|
---|
4611 | Assert(cbTaskWrite == (uint32_t)cbTaskWrite);
|
---|
4612 | PVDIOTASK pIoTask = vdIoTaskUserAlloc(pIoStorage, pfnComplete, pvCompleteUser, pIoCtx, (uint32_t)cbTaskWrite);
|
---|
4613 |
|
---|
4614 | if (!pIoTask)
|
---|
4615 | return VERR_NO_MEMORY;
|
---|
4616 |
|
---|
4617 | ASMAtomicIncU32(&pIoCtx->cDataTransfersPending);
|
---|
4618 |
|
---|
4619 | void *pvTask;
|
---|
4620 | Log(("Spawning pIoTask=%p pIoCtx=%p\n", pIoTask, pIoCtx));
|
---|
4621 | rc = pVDIo->pInterfaceIo->pfnWriteAsync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4622 | pIoStorage->pStorage,
|
---|
4623 | uOffset, aSeg, cSegments,
|
---|
4624 | cbTaskWrite, pIoTask, &pvTask);
|
---|
4625 | if (RT_SUCCESS(rc))
|
---|
4626 | {
|
---|
4627 | AssertMsg(cbTaskWrite <= pIoCtx->Req.Io.cbTransferLeft, ("Impossible!\n"));
|
---|
4628 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbTaskWrite);
|
---|
4629 | ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
|
---|
4630 | vdIoTaskFree(pDisk, pIoTask);
|
---|
4631 | }
|
---|
4632 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
4633 | {
|
---|
4634 | ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
|
---|
4635 | vdIoTaskFree(pDisk, pIoTask);
|
---|
4636 | break;
|
---|
4637 | }
|
---|
4638 |
|
---|
4639 | uOffset += cbTaskWrite;
|
---|
4640 | cbWrite -= cbTaskWrite;
|
---|
4641 | }
|
---|
4642 | }
|
---|
4643 |
|
---|
4644 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
4645 | return rc;
|
---|
4646 | }
|
---|
4647 |
|
---|
4648 | static int vdIOIntReadMeta(void *pvUser, PVDIOSTORAGE pIoStorage, uint64_t uOffset,
|
---|
4649 | void *pvBuf, size_t cbRead, PVDIOCTX pIoCtx,
|
---|
4650 | PPVDMETAXFER ppMetaXfer, PFNVDXFERCOMPLETED pfnComplete,
|
---|
4651 | void *pvCompleteUser)
|
---|
4652 | {
|
---|
4653 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4654 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
4655 | int rc = VINF_SUCCESS;
|
---|
4656 | RTSGSEG Seg;
|
---|
4657 | PVDIOTASK pIoTask;
|
---|
4658 | PVDMETAXFER pMetaXfer = NULL;
|
---|
4659 | void *pvTask = NULL;
|
---|
4660 |
|
---|
4661 | LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pvBuf=%#p cbRead=%u\n",
|
---|
4662 | pvUser, pIoStorage, uOffset, pvBuf, cbRead));
|
---|
4663 |
|
---|
4664 | AssertMsgReturn( pIoCtx
|
---|
4665 | || (!ppMetaXfer && !pfnComplete && !pvCompleteUser),
|
---|
4666 | ("A synchronous metadata read is requested but the parameters are wrong\n"),
|
---|
4667 | VERR_INVALID_POINTER);
|
---|
4668 |
|
---|
4669 | /** @todo: Enable check for sync I/O later. */
|
---|
4670 | if ( pIoCtx
|
---|
4671 | && !(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
4672 | VD_IS_LOCKED(pDisk);
|
---|
4673 |
|
---|
4674 | if ( !pIoCtx
|
---|
4675 | || pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC)
|
---|
4676 | {
|
---|
4677 | /* Handle synchronous metadata I/O. */
|
---|
4678 | /** @todo: Integrate with metadata transfers below. */
|
---|
4679 | rc = pVDIo->pInterfaceIo->pfnReadSync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4680 | pIoStorage->pStorage, uOffset,
|
---|
4681 | pvBuf, cbRead, NULL);
|
---|
4682 | if (ppMetaXfer)
|
---|
4683 | *ppMetaXfer = NULL;
|
---|
4684 | }
|
---|
4685 | else
|
---|
4686 | {
|
---|
4687 | pMetaXfer = (PVDMETAXFER)RTAvlrFileOffsetGet(pIoStorage->pTreeMetaXfers, uOffset);
|
---|
4688 | if (!pMetaXfer)
|
---|
4689 | {
|
---|
4690 | #ifdef RT_STRICT
|
---|
4691 | pMetaXfer = (PVDMETAXFER)RTAvlrFileOffsetGetBestFit(pIoStorage->pTreeMetaXfers, uOffset, false /* fAbove */);
|
---|
4692 | AssertMsg(!pMetaXfer || (pMetaXfer->Core.Key + (RTFOFF)pMetaXfer->cbMeta <= (RTFOFF)uOffset),
|
---|
4693 | ("Overlapping meta transfers!\n"));
|
---|
4694 | #endif
|
---|
4695 |
|
---|
4696 | /* Allocate a new meta transfer. */
|
---|
4697 | pMetaXfer = vdMetaXferAlloc(pIoStorage, uOffset, cbRead);
|
---|
4698 | if (!pMetaXfer)
|
---|
4699 | return VERR_NO_MEMORY;
|
---|
4700 |
|
---|
4701 | pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvCompleteUser, pMetaXfer);
|
---|
4702 | if (!pIoTask)
|
---|
4703 | {
|
---|
4704 | RTMemFree(pMetaXfer);
|
---|
4705 | return VERR_NO_MEMORY;
|
---|
4706 | }
|
---|
4707 |
|
---|
4708 | Seg.cbSeg = cbRead;
|
---|
4709 | Seg.pvSeg = pMetaXfer->abData;
|
---|
4710 |
|
---|
4711 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_READ);
|
---|
4712 | rc = pVDIo->pInterfaceIo->pfnReadAsync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4713 | pIoStorage->pStorage,
|
---|
4714 | uOffset, &Seg, 1,
|
---|
4715 | cbRead, pIoTask, &pvTask);
|
---|
4716 |
|
---|
4717 | if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
4718 | {
|
---|
4719 | bool fInserted = RTAvlrFileOffsetInsert(pIoStorage->pTreeMetaXfers, &pMetaXfer->Core);
|
---|
4720 | Assert(fInserted);
|
---|
4721 | }
|
---|
4722 | else
|
---|
4723 | RTMemFree(pMetaXfer);
|
---|
4724 |
|
---|
4725 | if (RT_SUCCESS(rc))
|
---|
4726 | {
|
---|
4727 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
|
---|
4728 | vdIoTaskFree(pDisk, pIoTask);
|
---|
4729 | }
|
---|
4730 | else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS && !pfnComplete)
|
---|
4731 | rc = VERR_VD_NOT_ENOUGH_METADATA;
|
---|
4732 | }
|
---|
4733 |
|
---|
4734 | Assert(VALID_PTR(pMetaXfer) || RT_FAILURE(rc));
|
---|
4735 |
|
---|
4736 | if (RT_SUCCESS(rc) || rc == VERR_VD_NOT_ENOUGH_METADATA || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
4737 | {
|
---|
4738 | /* If it is pending add the request to the list. */
|
---|
4739 | if (VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_READ)
|
---|
4740 | {
|
---|
4741 | PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
|
---|
4742 | AssertPtr(pDeferred);
|
---|
4743 |
|
---|
4744 | RTListInit(&pDeferred->NodeDeferred);
|
---|
4745 | pDeferred->pIoCtx = pIoCtx;
|
---|
4746 |
|
---|
4747 | ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
|
---|
4748 | RTListAppend(&pMetaXfer->ListIoCtxWaiting, &pDeferred->NodeDeferred);
|
---|
4749 | rc = VERR_VD_NOT_ENOUGH_METADATA;
|
---|
4750 | }
|
---|
4751 | else
|
---|
4752 | {
|
---|
4753 | /* Transfer the data. */
|
---|
4754 | pMetaXfer->cRefs++;
|
---|
4755 | Assert(pMetaXfer->cbMeta >= cbRead);
|
---|
4756 | Assert(pMetaXfer->Core.Key == (RTFOFF)uOffset);
|
---|
4757 | if (pMetaXfer->pbDataShw)
|
---|
4758 | memcpy(pvBuf, pMetaXfer->pbDataShw, cbRead);
|
---|
4759 | else
|
---|
4760 | memcpy(pvBuf, pMetaXfer->abData, cbRead);
|
---|
4761 | *ppMetaXfer = pMetaXfer;
|
---|
4762 | }
|
---|
4763 | }
|
---|
4764 | }
|
---|
4765 |
|
---|
4766 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
4767 | return rc;
|
---|
4768 | }
|
---|
4769 |
|
---|
4770 | static int vdIOIntWriteMeta(void *pvUser, PVDIOSTORAGE pIoStorage, uint64_t uOffset,
|
---|
4771 | const void *pvBuf, size_t cbWrite, PVDIOCTX pIoCtx,
|
---|
4772 | PFNVDXFERCOMPLETED pfnComplete, void *pvCompleteUser)
|
---|
4773 | {
|
---|
4774 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4775 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
4776 | int rc = VINF_SUCCESS;
|
---|
4777 | RTSGSEG Seg;
|
---|
4778 | PVDIOTASK pIoTask;
|
---|
4779 | PVDMETAXFER pMetaXfer = NULL;
|
---|
4780 | bool fInTree = false;
|
---|
4781 | void *pvTask = NULL;
|
---|
4782 |
|
---|
4783 | LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pvBuf=%#p cbWrite=%u\n",
|
---|
4784 | pvUser, pIoStorage, uOffset, pvBuf, cbWrite));
|
---|
4785 |
|
---|
4786 | AssertMsgReturn( pIoCtx
|
---|
4787 | || (!pfnComplete && !pvCompleteUser),
|
---|
4788 | ("A synchronous metadata write is requested but the parameters are wrong\n"),
|
---|
4789 | VERR_INVALID_POINTER);
|
---|
4790 |
|
---|
4791 | /** @todo: Enable check for sync I/O later. */
|
---|
4792 | if ( pIoCtx
|
---|
4793 | && !(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
4794 | VD_IS_LOCKED(pDisk);
|
---|
4795 |
|
---|
4796 | if ( !pIoCtx
|
---|
4797 | || pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC)
|
---|
4798 | {
|
---|
4799 | /* Handle synchronous metadata I/O. */
|
---|
4800 | /** @todo: Integrate with metadata transfers below. */
|
---|
4801 | rc = pVDIo->pInterfaceIo->pfnWriteSync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4802 | pIoStorage->pStorage, uOffset,
|
---|
4803 | pvBuf, cbWrite, NULL);
|
---|
4804 | }
|
---|
4805 | else
|
---|
4806 | {
|
---|
4807 | pMetaXfer = (PVDMETAXFER)RTAvlrFileOffsetGet(pIoStorage->pTreeMetaXfers, uOffset);
|
---|
4808 | if (!pMetaXfer)
|
---|
4809 | {
|
---|
4810 | /* Allocate a new meta transfer. */
|
---|
4811 | pMetaXfer = vdMetaXferAlloc(pIoStorage, uOffset, cbWrite);
|
---|
4812 | if (!pMetaXfer)
|
---|
4813 | return VERR_NO_MEMORY;
|
---|
4814 | }
|
---|
4815 | else
|
---|
4816 | {
|
---|
4817 | Assert(pMetaXfer->cbMeta >= cbWrite);
|
---|
4818 | Assert(pMetaXfer->Core.Key == (RTFOFF)uOffset);
|
---|
4819 | fInTree = true;
|
---|
4820 | }
|
---|
4821 |
|
---|
4822 | if (VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE)
|
---|
4823 | {
|
---|
4824 | pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvCompleteUser, pMetaXfer);
|
---|
4825 | if (!pIoTask)
|
---|
4826 | {
|
---|
4827 | RTMemFree(pMetaXfer);
|
---|
4828 | return VERR_NO_MEMORY;
|
---|
4829 | }
|
---|
4830 |
|
---|
4831 | memcpy(pMetaXfer->abData, pvBuf, cbWrite);
|
---|
4832 | Seg.cbSeg = cbWrite;
|
---|
4833 | Seg.pvSeg = pMetaXfer->abData;
|
---|
4834 |
|
---|
4835 | ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
|
---|
4836 |
|
---|
4837 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_WRITE);
|
---|
4838 | rc = pVDIo->pInterfaceIo->pfnWriteAsync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4839 | pIoStorage->pStorage,
|
---|
4840 | uOffset, &Seg, 1, cbWrite, pIoTask,
|
---|
4841 | &pvTask);
|
---|
4842 | if (RT_SUCCESS(rc))
|
---|
4843 | {
|
---|
4844 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
|
---|
4845 | ASMAtomicDecU32(&pIoCtx->cMetaTransfersPending);
|
---|
4846 | vdIoTaskFree(pDisk, pIoTask);
|
---|
4847 | if (fInTree && !pMetaXfer->cRefs)
|
---|
4848 | {
|
---|
4849 | LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
|
---|
4850 | bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
|
---|
4851 | AssertMsg(fRemoved, ("Metadata transfer wasn't removed\n"));
|
---|
4852 | RTMemFree(pMetaXfer);
|
---|
4853 | pMetaXfer = NULL;
|
---|
4854 | }
|
---|
4855 | }
|
---|
4856 | else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
4857 | {
|
---|
4858 | PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
|
---|
4859 | AssertPtr(pDeferred);
|
---|
4860 |
|
---|
4861 | RTListInit(&pDeferred->NodeDeferred);
|
---|
4862 | pDeferred->pIoCtx = pIoCtx;
|
---|
4863 |
|
---|
4864 | if (!fInTree)
|
---|
4865 | {
|
---|
4866 | bool fInserted = RTAvlrFileOffsetInsert(pIoStorage->pTreeMetaXfers, &pMetaXfer->Core);
|
---|
4867 | Assert(fInserted);
|
---|
4868 | }
|
---|
4869 |
|
---|
4870 | RTListAppend(&pMetaXfer->ListIoCtxWaiting, &pDeferred->NodeDeferred);
|
---|
4871 | }
|
---|
4872 | else
|
---|
4873 | {
|
---|
4874 | RTMemFree(pMetaXfer);
|
---|
4875 | pMetaXfer = NULL;
|
---|
4876 | }
|
---|
4877 | }
|
---|
4878 | else
|
---|
4879 | {
|
---|
4880 | /* I/O is in progress, update shadow buffer and add to waiting list. */
|
---|
4881 | Assert(VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_WRITE);
|
---|
4882 | if (!pMetaXfer->pbDataShw)
|
---|
4883 | {
|
---|
4884 | /* Allocate shadow buffer and set initial state. */
|
---|
4885 | LogFlowFunc(("pMetaXfer=%#p Creating shadow buffer\n", pMetaXfer));
|
---|
4886 | pMetaXfer->pbDataShw = (uint8_t *)RTMemAlloc(pMetaXfer->cbMeta);
|
---|
4887 | if (RT_LIKELY(pMetaXfer->pbDataShw))
|
---|
4888 | memcpy(pMetaXfer->pbDataShw, pMetaXfer->abData, pMetaXfer->cbMeta);
|
---|
4889 | else
|
---|
4890 | rc = VERR_NO_MEMORY;
|
---|
4891 | }
|
---|
4892 |
|
---|
4893 | if (RT_SUCCESS(rc))
|
---|
4894 | {
|
---|
4895 | /* Update with written data and append to waiting list. */
|
---|
4896 | PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
|
---|
4897 | if (pDeferred)
|
---|
4898 | {
|
---|
4899 | LogFlowFunc(("pMetaXfer=%#p Updating shadow buffer\n", pMetaXfer));
|
---|
4900 |
|
---|
4901 | RTListInit(&pDeferred->NodeDeferred);
|
---|
4902 | pDeferred->pIoCtx = pIoCtx;
|
---|
4903 | ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
|
---|
4904 | memcpy(pMetaXfer->pbDataShw, pvBuf, cbWrite);
|
---|
4905 | RTListAppend(&pMetaXfer->ListIoCtxShwWrites, &pDeferred->NodeDeferred);
|
---|
4906 | }
|
---|
4907 | else
|
---|
4908 | {
|
---|
4909 | /*
|
---|
4910 | * Free shadow buffer if there is no one depending on it, i.e.
|
---|
4911 | * we just allocated it.
|
---|
4912 | */
|
---|
4913 | if (RTListIsEmpty(&pMetaXfer->ListIoCtxShwWrites))
|
---|
4914 | {
|
---|
4915 | RTMemFree(pMetaXfer->pbDataShw);
|
---|
4916 | pMetaXfer->pbDataShw = NULL;
|
---|
4917 | }
|
---|
4918 | rc = VERR_NO_MEMORY;
|
---|
4919 | }
|
---|
4920 | }
|
---|
4921 | }
|
---|
4922 | }
|
---|
4923 |
|
---|
4924 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
4925 | return rc;
|
---|
4926 | }
|
---|
4927 |
|
---|
4928 | static void vdIOIntMetaXferRelease(void *pvUser, PVDMETAXFER pMetaXfer)
|
---|
4929 | {
|
---|
4930 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4931 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
4932 | PVDIOSTORAGE pIoStorage;
|
---|
4933 |
|
---|
4934 | /*
|
---|
4935 | * It is possible that we get called with a NULL metadata xfer handle
|
---|
4936 | * for synchronous I/O. Just exit.
|
---|
4937 | */
|
---|
4938 | if (!pMetaXfer)
|
---|
4939 | return;
|
---|
4940 |
|
---|
4941 | pIoStorage = pMetaXfer->pIoStorage;
|
---|
4942 |
|
---|
4943 | VD_IS_LOCKED(pDisk);
|
---|
4944 |
|
---|
4945 | Assert( VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE
|
---|
4946 | || VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_WRITE);
|
---|
4947 | Assert(pMetaXfer->cRefs > 0);
|
---|
4948 |
|
---|
4949 | pMetaXfer->cRefs--;
|
---|
4950 | if ( !pMetaXfer->cRefs
|
---|
4951 | && RTListIsEmpty(&pMetaXfer->ListIoCtxWaiting)
|
---|
4952 | && VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE)
|
---|
4953 | {
|
---|
4954 | /* Free the meta data entry. */
|
---|
4955 | LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
|
---|
4956 | bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
|
---|
4957 | AssertMsg(fRemoved, ("Metadata transfer wasn't removed\n"));
|
---|
4958 |
|
---|
4959 | RTMemFree(pMetaXfer);
|
---|
4960 | }
|
---|
4961 | }
|
---|
4962 |
|
---|
4963 | static int vdIOIntFlush(void *pvUser, PVDIOSTORAGE pIoStorage, PVDIOCTX pIoCtx,
|
---|
4964 | PFNVDXFERCOMPLETED pfnComplete, void *pvCompleteUser)
|
---|
4965 | {
|
---|
4966 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4967 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
4968 | int rc = VINF_SUCCESS;
|
---|
4969 | PVDIOTASK pIoTask;
|
---|
4970 | PVDMETAXFER pMetaXfer = NULL;
|
---|
4971 | void *pvTask = NULL;
|
---|
4972 |
|
---|
4973 | LogFlowFunc(("pvUser=%#p pIoStorage=%#p pIoCtx=%#p\n",
|
---|
4974 | pvUser, pIoStorage, pIoCtx));
|
---|
4975 |
|
---|
4976 | AssertMsgReturn( pIoCtx
|
---|
4977 | || (!pfnComplete && !pvCompleteUser),
|
---|
4978 | ("A synchronous metadata write is requested but the parameters are wrong\n"),
|
---|
4979 | VERR_INVALID_POINTER);
|
---|
4980 |
|
---|
4981 | /** @todo: Enable check for sync I/O later. */
|
---|
4982 | if ( pIoCtx
|
---|
4983 | && !(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
4984 | VD_IS_LOCKED(pDisk);
|
---|
4985 |
|
---|
4986 | if (pVDIo->fIgnoreFlush)
|
---|
4987 | return VINF_SUCCESS;
|
---|
4988 |
|
---|
4989 | if ( !pIoCtx
|
---|
4990 | || pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC)
|
---|
4991 | {
|
---|
4992 | /* Handle synchronous flushes. */
|
---|
4993 | /** @todo: Integrate with metadata transfers below. */
|
---|
4994 | rc = pVDIo->pInterfaceIo->pfnFlushSync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4995 | pIoStorage->pStorage);
|
---|
4996 | }
|
---|
4997 | else
|
---|
4998 | {
|
---|
4999 | /* Allocate a new meta transfer. */
|
---|
5000 | pMetaXfer = vdMetaXferAlloc(pIoStorage, 0, 0);
|
---|
5001 | if (!pMetaXfer)
|
---|
5002 | return VERR_NO_MEMORY;
|
---|
5003 |
|
---|
5004 | pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvUser, pMetaXfer);
|
---|
5005 | if (!pIoTask)
|
---|
5006 | {
|
---|
5007 | RTMemFree(pMetaXfer);
|
---|
5008 | return VERR_NO_MEMORY;
|
---|
5009 | }
|
---|
5010 |
|
---|
5011 | ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
|
---|
5012 |
|
---|
5013 | PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
|
---|
5014 | AssertPtr(pDeferred);
|
---|
5015 |
|
---|
5016 | RTListInit(&pDeferred->NodeDeferred);
|
---|
5017 | pDeferred->pIoCtx = pIoCtx;
|
---|
5018 |
|
---|
5019 | RTListAppend(&pMetaXfer->ListIoCtxWaiting, &pDeferred->NodeDeferred);
|
---|
5020 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_FLUSH);
|
---|
5021 | rc = pVDIo->pInterfaceIo->pfnFlushAsync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
5022 | pIoStorage->pStorage,
|
---|
5023 | pIoTask, &pvTask);
|
---|
5024 | if (RT_SUCCESS(rc))
|
---|
5025 | {
|
---|
5026 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
|
---|
5027 | ASMAtomicDecU32(&pIoCtx->cMetaTransfersPending);
|
---|
5028 | vdIoTaskFree(pDisk, pIoTask);
|
---|
5029 | RTMemFree(pDeferred);
|
---|
5030 | RTMemFree(pMetaXfer);
|
---|
5031 | }
|
---|
5032 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
5033 | RTMemFree(pMetaXfer);
|
---|
5034 | }
|
---|
5035 |
|
---|
5036 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
5037 | return rc;
|
---|
5038 | }
|
---|
5039 |
|
---|
5040 | static size_t vdIOIntIoCtxCopyTo(void *pvUser, PVDIOCTX pIoCtx,
|
---|
5041 | const void *pvBuf, size_t cbBuf)
|
---|
5042 | {
|
---|
5043 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
5044 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
5045 | size_t cbCopied = 0;
|
---|
5046 |
|
---|
5047 | /** @todo: Enable check for sync I/O later. */
|
---|
5048 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
5049 | VD_IS_LOCKED(pDisk);
|
---|
5050 |
|
---|
5051 | cbCopied = vdIoCtxCopyTo(pIoCtx, (uint8_t *)pvBuf, cbBuf);
|
---|
5052 | Assert(cbCopied == cbBuf);
|
---|
5053 |
|
---|
5054 | /// @todo Assert(pIoCtx->Req.Io.cbTransferLeft >= cbCopied); - triggers with vdCopyHelper/dmgRead.
|
---|
5055 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbCopied);
|
---|
5056 |
|
---|
5057 | return cbCopied;
|
---|
5058 | }
|
---|
5059 |
|
---|
5060 | static size_t vdIOIntIoCtxCopyFrom(void *pvUser, PVDIOCTX pIoCtx,
|
---|
5061 | void *pvBuf, size_t cbBuf)
|
---|
5062 | {
|
---|
5063 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
5064 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
5065 | size_t cbCopied = 0;
|
---|
5066 |
|
---|
5067 | /** @todo: Enable check for sync I/O later. */
|
---|
5068 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
5069 | VD_IS_LOCKED(pDisk);
|
---|
5070 |
|
---|
5071 | cbCopied = vdIoCtxCopyFrom(pIoCtx, (uint8_t *)pvBuf, cbBuf);
|
---|
5072 | Assert(cbCopied == cbBuf);
|
---|
5073 |
|
---|
5074 | /// @todo Assert(pIoCtx->Req.Io.cbTransferLeft > cbCopied); - triggers with vdCopyHelper/dmgRead.
|
---|
5075 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbCopied);
|
---|
5076 |
|
---|
5077 | return cbCopied;
|
---|
5078 | }
|
---|
5079 |
|
---|
5080 | static size_t vdIOIntIoCtxSet(void *pvUser, PVDIOCTX pIoCtx, int ch, size_t cb)
|
---|
5081 | {
|
---|
5082 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
5083 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
5084 | size_t cbSet = 0;
|
---|
5085 |
|
---|
5086 | /** @todo: Enable check for sync I/O later. */
|
---|
5087 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
5088 | VD_IS_LOCKED(pDisk);
|
---|
5089 |
|
---|
5090 | cbSet = vdIoCtxSet(pIoCtx, ch, cb);
|
---|
5091 | Assert(cbSet == cb);
|
---|
5092 |
|
---|
5093 | /// @todo Assert(pIoCtx->Req.Io.cbTransferLeft >= cbSet); - triggers with vdCopyHelper/dmgRead.
|
---|
5094 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbSet);
|
---|
5095 |
|
---|
5096 | return cbSet;
|
---|
5097 | }
|
---|
5098 |
|
---|
5099 | static size_t vdIOIntIoCtxSegArrayCreate(void *pvUser, PVDIOCTX pIoCtx,
|
---|
5100 | PRTSGSEG paSeg, unsigned *pcSeg,
|
---|
5101 | size_t cbData)
|
---|
5102 | {
|
---|
5103 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
5104 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
5105 | size_t cbCreated = 0;
|
---|
5106 |
|
---|
5107 | /** @todo: It is possible that this gets called from a filter plugin
|
---|
5108 | * outside of the disk lock. Refine assertion or remove completely. */
|
---|
5109 | #if 0
|
---|
5110 | /** @todo: Enable check for sync I/O later. */
|
---|
5111 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
5112 | VD_IS_LOCKED(pDisk);
|
---|
5113 | #endif
|
---|
5114 |
|
---|
5115 | cbCreated = RTSgBufSegArrayCreate(&pIoCtx->Req.Io.SgBuf, paSeg, pcSeg, cbData);
|
---|
5116 | Assert(!paSeg || cbData == cbCreated);
|
---|
5117 |
|
---|
5118 | return cbCreated;
|
---|
5119 | }
|
---|
5120 |
|
---|
5121 | static void vdIOIntIoCtxCompleted(void *pvUser, PVDIOCTX pIoCtx, int rcReq,
|
---|
5122 | size_t cbCompleted)
|
---|
5123 | {
|
---|
5124 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
5125 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
5126 |
|
---|
5127 | LogFlowFunc(("pvUser=%#p pIoCtx=%#p rcReq=%Rrc cbCompleted=%zu\n",
|
---|
5128 | pvUser, pIoCtx, rcReq, cbCompleted));
|
---|
5129 |
|
---|
5130 | /*
|
---|
5131 | * Grab the disk critical section to avoid races with other threads which
|
---|
5132 | * might still modify the I/O context.
|
---|
5133 | * Example is that iSCSI is doing an asynchronous write but calls us already
|
---|
5134 | * while the other thread is still hanging in vdWriteHelperAsync and couldn't update
|
---|
5135 | * the blocked state yet.
|
---|
5136 | * It can overwrite the state to true before we call vdIoCtxContinue and the
|
---|
5137 | * the request would hang indefinite.
|
---|
5138 | */
|
---|
5139 | ASMAtomicCmpXchgS32(&pIoCtx->rcReq, rcReq, VINF_SUCCESS);
|
---|
5140 | Assert(pIoCtx->Req.Io.cbTransferLeft >= cbCompleted);
|
---|
5141 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, (uint32_t)cbCompleted);
|
---|
5142 |
|
---|
5143 | /* Set next transfer function if the current one finished.
|
---|
5144 | * @todo: Find a better way to prevent vdIoCtxContinue from calling the current helper again. */
|
---|
5145 | if (!pIoCtx->Req.Io.cbTransferLeft)
|
---|
5146 | {
|
---|
5147 | pIoCtx->pfnIoCtxTransfer = pIoCtx->pfnIoCtxTransferNext;
|
---|
5148 | pIoCtx->pfnIoCtxTransferNext = NULL;
|
---|
5149 | }
|
---|
5150 |
|
---|
5151 | vdIoCtxAddToWaitingList(&pDisk->pIoCtxHaltedHead, pIoCtx);
|
---|
5152 | if (ASMAtomicCmpXchgBool(&pDisk->fLocked, true, false))
|
---|
5153 | {
|
---|
5154 | /* Immediately drop the lock again, it will take care of processing the list. */
|
---|
5155 | vdDiskUnlock(pDisk, NULL);
|
---|
5156 | }
|
---|
5157 | }
|
---|
5158 |
|
---|
5159 | static DECLCALLBACK(bool) vdIOIntIoCtxIsSynchronous(void *pvUser, PVDIOCTX pIoCtx)
|
---|
5160 | {
|
---|
5161 | NOREF(pvUser);
|
---|
5162 | return !!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC);
|
---|
5163 | }
|
---|
5164 |
|
---|
5165 | static DECLCALLBACK(bool) vdIOIntIoCtxIsZero(void *pvUser, PVDIOCTX pIoCtx, size_t cbCheck,
|
---|
5166 | bool fAdvance)
|
---|
5167 | {
|
---|
5168 | NOREF(pvUser);
|
---|
5169 |
|
---|
5170 | bool fIsZero = RTSgBufIsZero(&pIoCtx->Req.Io.SgBuf, cbCheck);
|
---|
5171 | if (fIsZero && fAdvance)
|
---|
5172 | RTSgBufAdvance(&pIoCtx->Req.Io.SgBuf, cbCheck);
|
---|
5173 |
|
---|
5174 | return fIsZero;
|
---|
5175 | }
|
---|
5176 |
|
---|
5177 | static DECLCALLBACK(size_t) vdIOIntIoCtxGetDataUnitSize(void *pvUser, PVDIOCTX pIoCtx)
|
---|
5178 | {
|
---|
5179 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
5180 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
5181 |
|
---|
5182 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, VD_LAST_IMAGE);
|
---|
5183 | AssertPtrReturn(pImage, 0);
|
---|
5184 | return pImage->Backend->pfnGetSectorSize(pImage->pBackendData);
|
---|
5185 | }
|
---|
5186 |
|
---|
5187 | /**
|
---|
5188 | * VD I/O interface callback for opening a file (limited version for VDGetFormat).
|
---|
5189 | */
|
---|
5190 | static int vdIOIntOpenLimited(void *pvUser, const char *pszLocation,
|
---|
5191 | uint32_t fOpen, PPVDIOSTORAGE ppIoStorage)
|
---|
5192 | {
|
---|
5193 | int rc = VINF_SUCCESS;
|
---|
5194 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
5195 | PVDIOSTORAGE pIoStorage = (PVDIOSTORAGE)RTMemAllocZ(sizeof(VDIOSTORAGE));
|
---|
5196 |
|
---|
5197 | if (!pIoStorage)
|
---|
5198 | return VERR_NO_MEMORY;
|
---|
5199 |
|
---|
5200 | rc = pInterfaceIo->pfnOpen(NULL, pszLocation, fOpen, NULL, &pIoStorage->pStorage);
|
---|
5201 | if (RT_SUCCESS(rc))
|
---|
5202 | *ppIoStorage = pIoStorage;
|
---|
5203 | else
|
---|
5204 | RTMemFree(pIoStorage);
|
---|
5205 |
|
---|
5206 | return rc;
|
---|
5207 | }
|
---|
5208 |
|
---|
5209 | static int vdIOIntCloseLimited(void *pvUser, PVDIOSTORAGE pIoStorage)
|
---|
5210 | {
|
---|
5211 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
5212 | int rc = pInterfaceIo->pfnClose(NULL, pIoStorage->pStorage);
|
---|
5213 |
|
---|
5214 | RTMemFree(pIoStorage);
|
---|
5215 | return rc;
|
---|
5216 | }
|
---|
5217 |
|
---|
5218 | static int vdIOIntDeleteLimited(void *pvUser, const char *pcszFilename)
|
---|
5219 | {
|
---|
5220 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
5221 | return pInterfaceIo->pfnDelete(NULL, pcszFilename);
|
---|
5222 | }
|
---|
5223 |
|
---|
5224 | static int vdIOIntMoveLimited(void *pvUser, const char *pcszSrc,
|
---|
5225 | const char *pcszDst, unsigned fMove)
|
---|
5226 | {
|
---|
5227 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
5228 | return pInterfaceIo->pfnMove(NULL, pcszSrc, pcszDst, fMove);
|
---|
5229 | }
|
---|
5230 |
|
---|
5231 | static int vdIOIntGetFreeSpaceLimited(void *pvUser, const char *pcszFilename,
|
---|
5232 | int64_t *pcbFreeSpace)
|
---|
5233 | {
|
---|
5234 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
5235 | return pInterfaceIo->pfnGetFreeSpace(NULL, pcszFilename, pcbFreeSpace);
|
---|
5236 | }
|
---|
5237 |
|
---|
5238 | static int vdIOIntGetModificationTimeLimited(void *pvUser,
|
---|
5239 | const char *pcszFilename,
|
---|
5240 | PRTTIMESPEC pModificationTime)
|
---|
5241 | {
|
---|
5242 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
5243 | return pInterfaceIo->pfnGetModificationTime(NULL, pcszFilename, pModificationTime);
|
---|
5244 | }
|
---|
5245 |
|
---|
5246 | static int vdIOIntGetSizeLimited(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
5247 | uint64_t *pcbSize)
|
---|
5248 | {
|
---|
5249 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
5250 | return pInterfaceIo->pfnGetSize(NULL, pIoStorage->pStorage, pcbSize);
|
---|
5251 | }
|
---|
5252 |
|
---|
5253 | static int vdIOIntSetSizeLimited(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
5254 | uint64_t cbSize)
|
---|
5255 | {
|
---|
5256 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
5257 | return pInterfaceIo->pfnSetSize(NULL, pIoStorage->pStorage, cbSize);
|
---|
5258 | }
|
---|
5259 |
|
---|
5260 | static int vdIOIntWriteUserLimited(void *pvUser, PVDIOSTORAGE pStorage,
|
---|
5261 | uint64_t uOffset, PVDIOCTX pIoCtx,
|
---|
5262 | size_t cbWrite,
|
---|
5263 | PFNVDXFERCOMPLETED pfnComplete,
|
---|
5264 | void *pvCompleteUser)
|
---|
5265 | {
|
---|
5266 | NOREF(pvUser);
|
---|
5267 | NOREF(pStorage);
|
---|
5268 | NOREF(uOffset);
|
---|
5269 | NOREF(pIoCtx);
|
---|
5270 | NOREF(cbWrite);
|
---|
5271 | NOREF(pfnComplete);
|
---|
5272 | NOREF(pvCompleteUser);
|
---|
5273 | AssertMsgFailedReturn(("This needs to be implemented when called\n"), VERR_NOT_IMPLEMENTED);
|
---|
5274 | }
|
---|
5275 |
|
---|
5276 | static int vdIOIntReadUserLimited(void *pvUser, PVDIOSTORAGE pStorage,
|
---|
5277 | uint64_t uOffset, PVDIOCTX pIoCtx,
|
---|
5278 | size_t cbRead)
|
---|
5279 | {
|
---|
5280 | NOREF(pvUser);
|
---|
5281 | NOREF(pStorage);
|
---|
5282 | NOREF(uOffset);
|
---|
5283 | NOREF(pIoCtx);
|
---|
5284 | NOREF(cbRead);
|
---|
5285 | AssertMsgFailedReturn(("This needs to be implemented when called\n"), VERR_NOT_IMPLEMENTED);
|
---|
5286 | }
|
---|
5287 |
|
---|
5288 | static int vdIOIntWriteMetaLimited(void *pvUser, PVDIOSTORAGE pStorage,
|
---|
5289 | uint64_t uOffset, const void *pvBuffer,
|
---|
5290 | size_t cbBuffer, PVDIOCTX pIoCtx,
|
---|
5291 | PFNVDXFERCOMPLETED pfnComplete,
|
---|
5292 | void *pvCompleteUser)
|
---|
5293 | {
|
---|
5294 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
5295 |
|
---|
5296 | AssertMsgReturn(!pIoCtx && !pfnComplete && !pvCompleteUser,
|
---|
5297 | ("Async I/O not implemented for the limited interface"),
|
---|
5298 | VERR_NOT_SUPPORTED);
|
---|
5299 |
|
---|
5300 | return pInterfaceIo->pfnWriteSync(NULL, pStorage->pStorage, uOffset, pvBuffer, cbBuffer, NULL);
|
---|
5301 | }
|
---|
5302 |
|
---|
5303 | static int vdIOIntReadMetaLimited(void *pvUser, PVDIOSTORAGE pStorage,
|
---|
5304 | uint64_t uOffset, void *pvBuffer,
|
---|
5305 | size_t cbBuffer, PVDIOCTX pIoCtx,
|
---|
5306 | PPVDMETAXFER ppMetaXfer,
|
---|
5307 | PFNVDXFERCOMPLETED pfnComplete,
|
---|
5308 | void *pvCompleteUser)
|
---|
5309 | {
|
---|
5310 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
5311 |
|
---|
5312 | AssertMsgReturn(!pIoCtx && !ppMetaXfer && !pfnComplete && !pvCompleteUser,
|
---|
5313 | ("Async I/O not implemented for the limited interface"),
|
---|
5314 | VERR_NOT_SUPPORTED);
|
---|
5315 |
|
---|
5316 | return pInterfaceIo->pfnReadSync(NULL, pStorage->pStorage, uOffset, pvBuffer, cbBuffer, NULL);
|
---|
5317 | }
|
---|
5318 |
|
---|
5319 | static int vdIOIntMetaXferReleaseLimited(void *pvUser, PVDMETAXFER pMetaXfer)
|
---|
5320 | {
|
---|
5321 | /* This is a NOP in this case. */
|
---|
5322 | NOREF(pvUser);
|
---|
5323 | NOREF(pMetaXfer);
|
---|
5324 | return VINF_SUCCESS;
|
---|
5325 | }
|
---|
5326 |
|
---|
5327 | static int vdIOIntFlushLimited(void *pvUser, PVDIOSTORAGE pStorage,
|
---|
5328 | PVDIOCTX pIoCtx,
|
---|
5329 | PFNVDXFERCOMPLETED pfnComplete,
|
---|
5330 | void *pvCompleteUser)
|
---|
5331 | {
|
---|
5332 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
5333 |
|
---|
5334 | AssertMsgReturn(!pIoCtx && !pfnComplete && !pvCompleteUser,
|
---|
5335 | ("Async I/O not implemented for the limited interface"),
|
---|
5336 | VERR_NOT_SUPPORTED);
|
---|
5337 |
|
---|
5338 | return pInterfaceIo->pfnFlushSync(NULL, pStorage->pStorage);
|
---|
5339 | }
|
---|
5340 |
|
---|
5341 | /**
|
---|
5342 | * internal: send output to the log (unconditionally).
|
---|
5343 | */
|
---|
5344 | int vdLogMessage(void *pvUser, const char *pszFormat, va_list args)
|
---|
5345 | {
|
---|
5346 | NOREF(pvUser);
|
---|
5347 | RTLogPrintfV(pszFormat, args);
|
---|
5348 | return VINF_SUCCESS;
|
---|
5349 | }
|
---|
5350 |
|
---|
5351 | DECLINLINE(int) vdMessageWrapper(PVBOXHDD pDisk, const char *pszFormat, ...)
|
---|
5352 | {
|
---|
5353 | va_list va;
|
---|
5354 | va_start(va, pszFormat);
|
---|
5355 | int rc = pDisk->pInterfaceError->pfnMessage(pDisk->pInterfaceError->Core.pvUser,
|
---|
5356 | pszFormat, va);
|
---|
5357 | va_end(va);
|
---|
5358 | return rc;
|
---|
5359 | }
|
---|
5360 |
|
---|
5361 |
|
---|
5362 | /**
|
---|
5363 | * internal: adjust PCHS geometry
|
---|
5364 | */
|
---|
5365 | static void vdFixupPCHSGeometry(PVDGEOMETRY pPCHS, uint64_t cbSize)
|
---|
5366 | {
|
---|
5367 | /* Fix broken PCHS geometry. Can happen for two reasons: either the backend
|
---|
5368 | * mixes up PCHS and LCHS, or the application used to create the source
|
---|
5369 | * image has put garbage in it. Additionally, if the PCHS geometry covers
|
---|
5370 | * more than the image size, set it back to the default. */
|
---|
5371 | if ( pPCHS->cHeads > 16
|
---|
5372 | || pPCHS->cSectors > 63
|
---|
5373 | || pPCHS->cCylinders == 0
|
---|
5374 | || (uint64_t)pPCHS->cHeads * pPCHS->cSectors * pPCHS->cCylinders * 512 > cbSize)
|
---|
5375 | {
|
---|
5376 | Assert(!(RT_MIN(cbSize / 512 / 16 / 63, 16383) - (uint32_t)RT_MIN(cbSize / 512 / 16 / 63, 16383)));
|
---|
5377 | pPCHS->cCylinders = (uint32_t)RT_MIN(cbSize / 512 / 16 / 63, 16383);
|
---|
5378 | pPCHS->cHeads = 16;
|
---|
5379 | pPCHS->cSectors = 63;
|
---|
5380 | }
|
---|
5381 | }
|
---|
5382 |
|
---|
5383 | /**
|
---|
5384 | * internal: adjust PCHS geometry
|
---|
5385 | */
|
---|
5386 | static void vdFixupLCHSGeometry(PVDGEOMETRY pLCHS, uint64_t cbSize)
|
---|
5387 | {
|
---|
5388 | /* Fix broken LCHS geometry. Can happen for two reasons: either the backend
|
---|
5389 | * mixes up PCHS and LCHS, or the application used to create the source
|
---|
5390 | * image has put garbage in it. The fix in this case is to clear the LCHS
|
---|
5391 | * geometry to trigger autodetection when it is used next. If the geometry
|
---|
5392 | * already says "please autodetect" (cylinders=0) keep it. */
|
---|
5393 | if ( ( pLCHS->cHeads > 255
|
---|
5394 | || pLCHS->cHeads == 0
|
---|
5395 | || pLCHS->cSectors > 63
|
---|
5396 | || pLCHS->cSectors == 0)
|
---|
5397 | && pLCHS->cCylinders != 0)
|
---|
5398 | {
|
---|
5399 | pLCHS->cCylinders = 0;
|
---|
5400 | pLCHS->cHeads = 0;
|
---|
5401 | pLCHS->cSectors = 0;
|
---|
5402 | }
|
---|
5403 | /* Always recompute the number of cylinders stored in the LCHS
|
---|
5404 | * geometry if it isn't set to "autotedetect" at the moment.
|
---|
5405 | * This is very useful if the destination image size is
|
---|
5406 | * larger or smaller than the source image size. Do not modify
|
---|
5407 | * the number of heads and sectors. Windows guests hate it. */
|
---|
5408 | if ( pLCHS->cCylinders != 0
|
---|
5409 | && pLCHS->cHeads != 0 /* paranoia */
|
---|
5410 | && pLCHS->cSectors != 0 /* paranoia */)
|
---|
5411 | {
|
---|
5412 | Assert(!(RT_MIN(cbSize / 512 / pLCHS->cHeads / pLCHS->cSectors, 1024) - (uint32_t)RT_MIN(cbSize / 512 / pLCHS->cHeads / pLCHS->cSectors, 1024)));
|
---|
5413 | pLCHS->cCylinders = (uint32_t)RT_MIN(cbSize / 512 / pLCHS->cHeads / pLCHS->cSectors, 1024);
|
---|
5414 | }
|
---|
5415 | }
|
---|
5416 |
|
---|
5417 | /**
|
---|
5418 | * Sets the I/O callbacks of the given interface to the fallback methods
|
---|
5419 | *
|
---|
5420 | * @returns nothing.
|
---|
5421 | * @param pIfIo The I/O interface to setup.
|
---|
5422 | */
|
---|
5423 | static void vdIfIoFallbackCallbacksSetup(PVDINTERFACEIO pIfIo)
|
---|
5424 | {
|
---|
5425 | pIfIo->pfnOpen = vdIOOpenFallback;
|
---|
5426 | pIfIo->pfnClose = vdIOCloseFallback;
|
---|
5427 | pIfIo->pfnDelete = vdIODeleteFallback;
|
---|
5428 | pIfIo->pfnMove = vdIOMoveFallback;
|
---|
5429 | pIfIo->pfnGetFreeSpace = vdIOGetFreeSpaceFallback;
|
---|
5430 | pIfIo->pfnGetModificationTime = vdIOGetModificationTimeFallback;
|
---|
5431 | pIfIo->pfnGetSize = vdIOGetSizeFallback;
|
---|
5432 | pIfIo->pfnSetSize = vdIOSetSizeFallback;
|
---|
5433 | pIfIo->pfnReadSync = vdIOReadSyncFallback;
|
---|
5434 | pIfIo->pfnWriteSync = vdIOWriteSyncFallback;
|
---|
5435 | pIfIo->pfnFlushSync = vdIOFlushSyncFallback;
|
---|
5436 | pIfIo->pfnReadAsync = vdIOReadAsyncFallback;
|
---|
5437 | pIfIo->pfnWriteAsync = vdIOWriteAsyncFallback;
|
---|
5438 | pIfIo->pfnFlushAsync = vdIOFlushAsyncFallback;
|
---|
5439 | }
|
---|
5440 |
|
---|
5441 | /**
|
---|
5442 | * Sets the internal I/O callbacks of the given interface.
|
---|
5443 | *
|
---|
5444 | * @returns nothing.
|
---|
5445 | * @param pIfIoInt The internal I/O interface to setup.
|
---|
5446 | */
|
---|
5447 | static void vdIfIoIntCallbacksSetup(PVDINTERFACEIOINT pIfIoInt)
|
---|
5448 | {
|
---|
5449 | pIfIoInt->pfnOpen = vdIOIntOpen;
|
---|
5450 | pIfIoInt->pfnClose = vdIOIntClose;
|
---|
5451 | pIfIoInt->pfnDelete = vdIOIntDelete;
|
---|
5452 | pIfIoInt->pfnMove = vdIOIntMove;
|
---|
5453 | pIfIoInt->pfnGetFreeSpace = vdIOIntGetFreeSpace;
|
---|
5454 | pIfIoInt->pfnGetModificationTime = vdIOIntGetModificationTime;
|
---|
5455 | pIfIoInt->pfnGetSize = vdIOIntGetSize;
|
---|
5456 | pIfIoInt->pfnSetSize = vdIOIntSetSize;
|
---|
5457 | pIfIoInt->pfnReadUser = vdIOIntReadUser;
|
---|
5458 | pIfIoInt->pfnWriteUser = vdIOIntWriteUser;
|
---|
5459 | pIfIoInt->pfnReadMeta = vdIOIntReadMeta;
|
---|
5460 | pIfIoInt->pfnWriteMeta = vdIOIntWriteMeta;
|
---|
5461 | pIfIoInt->pfnMetaXferRelease = vdIOIntMetaXferRelease;
|
---|
5462 | pIfIoInt->pfnFlush = vdIOIntFlush;
|
---|
5463 | pIfIoInt->pfnIoCtxCopyFrom = vdIOIntIoCtxCopyFrom;
|
---|
5464 | pIfIoInt->pfnIoCtxCopyTo = vdIOIntIoCtxCopyTo;
|
---|
5465 | pIfIoInt->pfnIoCtxSet = vdIOIntIoCtxSet;
|
---|
5466 | pIfIoInt->pfnIoCtxSegArrayCreate = vdIOIntIoCtxSegArrayCreate;
|
---|
5467 | pIfIoInt->pfnIoCtxCompleted = vdIOIntIoCtxCompleted;
|
---|
5468 | pIfIoInt->pfnIoCtxIsSynchronous = vdIOIntIoCtxIsSynchronous;
|
---|
5469 | pIfIoInt->pfnIoCtxIsZero = vdIOIntIoCtxIsZero;
|
---|
5470 | pIfIoInt->pfnIoCtxGetDataUnitSize = vdIOIntIoCtxGetDataUnitSize;
|
---|
5471 | }
|
---|
5472 |
|
---|
5473 | /**
|
---|
5474 | * Internally used completion handler for synchronous I/O contexts.
|
---|
5475 | */
|
---|
5476 | static DECLCALLBACK(void) vdIoCtxSyncComplete(void *pvUser1, void *pvUser2, int rcReq)
|
---|
5477 | {
|
---|
5478 | PVBOXHDD pDisk = (PVBOXHDD)pvUser1;
|
---|
5479 | RTSEMEVENT hEvent = (RTSEMEVENT)pvUser2;
|
---|
5480 |
|
---|
5481 | RTSemEventSignal(hEvent);
|
---|
5482 | }
|
---|
5483 |
|
---|
5484 | /**
|
---|
5485 | * Initializes HDD backends.
|
---|
5486 | *
|
---|
5487 | * @returns VBox status code.
|
---|
5488 | */
|
---|
5489 | VBOXDDU_DECL(int) VDInit(void)
|
---|
5490 | {
|
---|
5491 | int rc = vdAddBackends(aStaticBackends, RT_ELEMENTS(aStaticBackends));
|
---|
5492 | if (RT_SUCCESS(rc))
|
---|
5493 | {
|
---|
5494 | rc = vdAddCacheBackends(aStaticCacheBackends, RT_ELEMENTS(aStaticCacheBackends));
|
---|
5495 | if (RT_SUCCESS(rc))
|
---|
5496 | {
|
---|
5497 | RTListInit(&g_ListPluginsLoaded);
|
---|
5498 | rc = vdLoadDynamicBackends();
|
---|
5499 | }
|
---|
5500 | }
|
---|
5501 | LogRel(("VDInit finished\n"));
|
---|
5502 | return rc;
|
---|
5503 | }
|
---|
5504 |
|
---|
5505 | /**
|
---|
5506 | * Destroys loaded HDD backends.
|
---|
5507 | *
|
---|
5508 | * @returns VBox status code.
|
---|
5509 | */
|
---|
5510 | VBOXDDU_DECL(int) VDShutdown(void)
|
---|
5511 | {
|
---|
5512 | PCVBOXHDDBACKEND *pBackends = g_apBackends;
|
---|
5513 | PCVDCACHEBACKEND *pCacheBackends = g_apCacheBackends;
|
---|
5514 | unsigned cBackends = g_cBackends;
|
---|
5515 |
|
---|
5516 | if (!g_apBackends)
|
---|
5517 | return VERR_INTERNAL_ERROR;
|
---|
5518 |
|
---|
5519 | if (g_apCacheBackends)
|
---|
5520 | RTMemFree(g_apCacheBackends);
|
---|
5521 | RTMemFree(g_apBackends);
|
---|
5522 |
|
---|
5523 | g_cBackends = 0;
|
---|
5524 | g_apBackends = NULL;
|
---|
5525 |
|
---|
5526 | /* Clear the supported cache backends. */
|
---|
5527 | g_cCacheBackends = 0;
|
---|
5528 | g_apCacheBackends = NULL;
|
---|
5529 |
|
---|
5530 | #ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
5531 | PVDPLUGIN pPlugin, pPluginNext;
|
---|
5532 |
|
---|
5533 | RTListForEachSafe(&g_ListPluginsLoaded, pPlugin, pPluginNext, VDPLUGIN, NodePlugin)
|
---|
5534 | {
|
---|
5535 | RTLdrClose(pPlugin->hPlugin);
|
---|
5536 | RTStrFree(pPlugin->pszFilename);
|
---|
5537 | RTListNodeRemove(&pPlugin->NodePlugin);
|
---|
5538 | RTMemFree(pPlugin);
|
---|
5539 | }
|
---|
5540 | #endif
|
---|
5541 |
|
---|
5542 | return VINF_SUCCESS;
|
---|
5543 | }
|
---|
5544 |
|
---|
5545 | /**
|
---|
5546 | * Loads a single plugin given by filename.
|
---|
5547 | *
|
---|
5548 | * @returns VBox status code.
|
---|
5549 | * @param pszFilename The plugin filename to load.
|
---|
5550 | */
|
---|
5551 | VBOXDDU_DECL(int) VDPluginLoadFromFilename(const char *pszFilename)
|
---|
5552 | {
|
---|
5553 | if (!g_apBackends)
|
---|
5554 | {
|
---|
5555 | int rc = VDInit();
|
---|
5556 | if (RT_FAILURE(rc))
|
---|
5557 | return rc;
|
---|
5558 | }
|
---|
5559 |
|
---|
5560 | return vdPluginLoadFromFilename(pszFilename);
|
---|
5561 | }
|
---|
5562 |
|
---|
5563 | /**
|
---|
5564 | * Load all plugins from a given path.
|
---|
5565 | *
|
---|
5566 | * @returns VBox statuse code.
|
---|
5567 | * @param pszPath The path to load plugins from.
|
---|
5568 | */
|
---|
5569 | VBOXDDU_DECL(int) VDPluginLoadFromPath(const char *pszPath)
|
---|
5570 | {
|
---|
5571 | if (!g_apBackends)
|
---|
5572 | {
|
---|
5573 | int rc = VDInit();
|
---|
5574 | if (RT_FAILURE(rc))
|
---|
5575 | return rc;
|
---|
5576 | }
|
---|
5577 |
|
---|
5578 | return vdPluginLoadFromPath(pszPath);
|
---|
5579 | }
|
---|
5580 |
|
---|
5581 | /**
|
---|
5582 | * Lists all HDD backends and their capabilities in a caller-provided buffer.
|
---|
5583 | *
|
---|
5584 | * @returns VBox status code.
|
---|
5585 | * VERR_BUFFER_OVERFLOW if not enough space is passed.
|
---|
5586 | * @param cEntriesAlloc Number of list entries available.
|
---|
5587 | * @param pEntries Pointer to array for the entries.
|
---|
5588 | * @param pcEntriesUsed Number of entries returned.
|
---|
5589 | */
|
---|
5590 | VBOXDDU_DECL(int) VDBackendInfo(unsigned cEntriesAlloc, PVDBACKENDINFO pEntries,
|
---|
5591 | unsigned *pcEntriesUsed)
|
---|
5592 | {
|
---|
5593 | int rc = VINF_SUCCESS;
|
---|
5594 | PRTDIR pPluginDir = NULL;
|
---|
5595 | unsigned cEntries = 0;
|
---|
5596 |
|
---|
5597 | LogFlowFunc(("cEntriesAlloc=%u pEntries=%#p pcEntriesUsed=%#p\n", cEntriesAlloc, pEntries, pcEntriesUsed));
|
---|
5598 | /* Check arguments. */
|
---|
5599 | AssertMsgReturn(cEntriesAlloc,
|
---|
5600 | ("cEntriesAlloc=%u\n", cEntriesAlloc),
|
---|
5601 | VERR_INVALID_PARAMETER);
|
---|
5602 | AssertMsgReturn(VALID_PTR(pEntries),
|
---|
5603 | ("pEntries=%#p\n", pEntries),
|
---|
5604 | VERR_INVALID_PARAMETER);
|
---|
5605 | AssertMsgReturn(VALID_PTR(pcEntriesUsed),
|
---|
5606 | ("pcEntriesUsed=%#p\n", pcEntriesUsed),
|
---|
5607 | VERR_INVALID_PARAMETER);
|
---|
5608 | if (!g_apBackends)
|
---|
5609 | VDInit();
|
---|
5610 |
|
---|
5611 | if (cEntriesAlloc < g_cBackends)
|
---|
5612 | {
|
---|
5613 | *pcEntriesUsed = g_cBackends;
|
---|
5614 | return VERR_BUFFER_OVERFLOW;
|
---|
5615 | }
|
---|
5616 |
|
---|
5617 | for (unsigned i = 0; i < g_cBackends; i++)
|
---|
5618 | {
|
---|
5619 | pEntries[i].pszBackend = g_apBackends[i]->pszBackendName;
|
---|
5620 | pEntries[i].uBackendCaps = g_apBackends[i]->uBackendCaps;
|
---|
5621 | pEntries[i].paFileExtensions = g_apBackends[i]->paFileExtensions;
|
---|
5622 | pEntries[i].paConfigInfo = g_apBackends[i]->paConfigInfo;
|
---|
5623 | pEntries[i].pfnComposeLocation = g_apBackends[i]->pfnComposeLocation;
|
---|
5624 | pEntries[i].pfnComposeName = g_apBackends[i]->pfnComposeName;
|
---|
5625 | }
|
---|
5626 |
|
---|
5627 | LogFlowFunc(("returns %Rrc *pcEntriesUsed=%u\n", rc, cEntries));
|
---|
5628 | *pcEntriesUsed = g_cBackends;
|
---|
5629 | return rc;
|
---|
5630 | }
|
---|
5631 |
|
---|
5632 | /**
|
---|
5633 | * Lists the capabilities of a backend identified by its name.
|
---|
5634 | *
|
---|
5635 | * @returns VBox status code.
|
---|
5636 | * @param pszBackend The backend name.
|
---|
5637 | * @param pEntries Pointer to an entry.
|
---|
5638 | */
|
---|
5639 | VBOXDDU_DECL(int) VDBackendInfoOne(const char *pszBackend, PVDBACKENDINFO pEntry)
|
---|
5640 | {
|
---|
5641 | LogFlowFunc(("pszBackend=%#p pEntry=%#p\n", pszBackend, pEntry));
|
---|
5642 | /* Check arguments. */
|
---|
5643 | AssertMsgReturn(VALID_PTR(pszBackend),
|
---|
5644 | ("pszBackend=%#p\n", pszBackend),
|
---|
5645 | VERR_INVALID_PARAMETER);
|
---|
5646 | AssertMsgReturn(VALID_PTR(pEntry),
|
---|
5647 | ("pEntry=%#p\n", pEntry),
|
---|
5648 | VERR_INVALID_PARAMETER);
|
---|
5649 | if (!g_apBackends)
|
---|
5650 | VDInit();
|
---|
5651 |
|
---|
5652 | /* Go through loaded backends. */
|
---|
5653 | for (unsigned i = 0; i < g_cBackends; i++)
|
---|
5654 | {
|
---|
5655 | if (!RTStrICmp(pszBackend, g_apBackends[i]->pszBackendName))
|
---|
5656 | {
|
---|
5657 | pEntry->pszBackend = g_apBackends[i]->pszBackendName;
|
---|
5658 | pEntry->uBackendCaps = g_apBackends[i]->uBackendCaps;
|
---|
5659 | pEntry->paFileExtensions = g_apBackends[i]->paFileExtensions;
|
---|
5660 | pEntry->paConfigInfo = g_apBackends[i]->paConfigInfo;
|
---|
5661 | return VINF_SUCCESS;
|
---|
5662 | }
|
---|
5663 | }
|
---|
5664 |
|
---|
5665 | return VERR_NOT_FOUND;
|
---|
5666 | }
|
---|
5667 |
|
---|
5668 | /**
|
---|
5669 | * Lists all filters and their capabilities in a caller-provided buffer.
|
---|
5670 | *
|
---|
5671 | * @return VBox status code.
|
---|
5672 | * VERR_BUFFER_OVERFLOW if not enough space is passed.
|
---|
5673 | * @param cEntriesAlloc Number of list entries available.
|
---|
5674 | * @param pEntries Pointer to array for the entries.
|
---|
5675 | * @param pcEntriesUsed Number of entries returned.
|
---|
5676 | */
|
---|
5677 | VBOXDDU_DECL(int) VDFilterInfo(unsigned cEntriesAlloc, PVDFILTERINFO pEntries,
|
---|
5678 | unsigned *pcEntriesUsed)
|
---|
5679 | {
|
---|
5680 | int rc = VINF_SUCCESS;
|
---|
5681 | unsigned cEntries = 0;
|
---|
5682 |
|
---|
5683 | LogFlowFunc(("cEntriesAlloc=%u pEntries=%#p pcEntriesUsed=%#p\n", cEntriesAlloc, pEntries, pcEntriesUsed));
|
---|
5684 | /* Check arguments. */
|
---|
5685 | AssertMsgReturn(cEntriesAlloc,
|
---|
5686 | ("cEntriesAlloc=%u\n", cEntriesAlloc),
|
---|
5687 | VERR_INVALID_PARAMETER);
|
---|
5688 | AssertMsgReturn(VALID_PTR(pEntries),
|
---|
5689 | ("pEntries=%#p\n", pEntries),
|
---|
5690 | VERR_INVALID_PARAMETER);
|
---|
5691 | AssertMsgReturn(VALID_PTR(pcEntriesUsed),
|
---|
5692 | ("pcEntriesUsed=%#p\n", pcEntriesUsed),
|
---|
5693 | VERR_INVALID_PARAMETER);
|
---|
5694 | if (!g_apBackends)
|
---|
5695 | VDInit();
|
---|
5696 |
|
---|
5697 | if (cEntriesAlloc < g_cFilterBackends)
|
---|
5698 | {
|
---|
5699 | *pcEntriesUsed = g_cFilterBackends;
|
---|
5700 | return VERR_BUFFER_OVERFLOW;
|
---|
5701 | }
|
---|
5702 |
|
---|
5703 | for (unsigned i = 0; i < g_cFilterBackends; i++)
|
---|
5704 | {
|
---|
5705 | pEntries[i].pszFilter = g_apFilterBackends[i]->pszBackendName;
|
---|
5706 | pEntries[i].paConfigInfo = g_apFilterBackends[i]->paConfigInfo;
|
---|
5707 | }
|
---|
5708 |
|
---|
5709 | LogFlowFunc(("returns %Rrc *pcEntriesUsed=%u\n", rc, cEntries));
|
---|
5710 | *pcEntriesUsed = g_cFilterBackends;
|
---|
5711 | return rc;
|
---|
5712 | }
|
---|
5713 |
|
---|
5714 | /**
|
---|
5715 | * Lists the capabilities of a filter identified by its name.
|
---|
5716 | *
|
---|
5717 | * @return VBox status code.
|
---|
5718 | * @param pszFilter The filter name (case insensitive).
|
---|
5719 | * @param pEntries Pointer to an entry.
|
---|
5720 | */
|
---|
5721 | VBOXDDU_DECL(int) VDFilterInfoOne(const char *pszFilter, PVDFILTERINFO pEntry)
|
---|
5722 | {
|
---|
5723 | LogFlowFunc(("pszFilter=%#p pEntry=%#p\n", pszFilter, pEntry));
|
---|
5724 | /* Check arguments. */
|
---|
5725 | AssertMsgReturn(VALID_PTR(pszFilter),
|
---|
5726 | ("pszFilter=%#p\n", pszFilter),
|
---|
5727 | VERR_INVALID_PARAMETER);
|
---|
5728 | AssertMsgReturn(VALID_PTR(pEntry),
|
---|
5729 | ("pEntry=%#p\n", pEntry),
|
---|
5730 | VERR_INVALID_PARAMETER);
|
---|
5731 | if (!g_apBackends)
|
---|
5732 | VDInit();
|
---|
5733 |
|
---|
5734 | /* Go through loaded backends. */
|
---|
5735 | for (unsigned i = 0; i < g_cFilterBackends; i++)
|
---|
5736 | {
|
---|
5737 | if (!RTStrICmp(pszFilter, g_apFilterBackends[i]->pszBackendName))
|
---|
5738 | {
|
---|
5739 | pEntry->pszFilter = g_apFilterBackends[i]->pszBackendName;
|
---|
5740 | pEntry->paConfigInfo = g_apFilterBackends[i]->paConfigInfo;
|
---|
5741 | return VINF_SUCCESS;
|
---|
5742 | }
|
---|
5743 | }
|
---|
5744 |
|
---|
5745 | return VERR_NOT_FOUND;
|
---|
5746 | }
|
---|
5747 |
|
---|
5748 | /**
|
---|
5749 | * Allocates and initializes an empty HDD container.
|
---|
5750 | * No image files are opened.
|
---|
5751 | *
|
---|
5752 | * @returns VBox status code.
|
---|
5753 | * @param pVDIfsDisk Pointer to the per-disk VD interface list.
|
---|
5754 | * @param enmType Type of the image container.
|
---|
5755 | * @param ppDisk Where to store the reference to HDD container.
|
---|
5756 | */
|
---|
5757 | VBOXDDU_DECL(int) VDCreate(PVDINTERFACE pVDIfsDisk, VDTYPE enmType, PVBOXHDD *ppDisk)
|
---|
5758 | {
|
---|
5759 | int rc = VINF_SUCCESS;
|
---|
5760 | PVBOXHDD pDisk = NULL;
|
---|
5761 |
|
---|
5762 | LogFlowFunc(("pVDIfsDisk=%#p\n", pVDIfsDisk));
|
---|
5763 | do
|
---|
5764 | {
|
---|
5765 | /* Check arguments. */
|
---|
5766 | AssertMsgBreakStmt(VALID_PTR(ppDisk),
|
---|
5767 | ("ppDisk=%#p\n", ppDisk),
|
---|
5768 | rc = VERR_INVALID_PARAMETER);
|
---|
5769 |
|
---|
5770 | pDisk = (PVBOXHDD)RTMemAllocZ(sizeof(VBOXHDD));
|
---|
5771 | if (pDisk)
|
---|
5772 | {
|
---|
5773 | pDisk->u32Signature = VBOXHDDDISK_SIGNATURE;
|
---|
5774 | pDisk->enmType = enmType;
|
---|
5775 | pDisk->cImages = 0;
|
---|
5776 | pDisk->pBase = NULL;
|
---|
5777 | pDisk->pLast = NULL;
|
---|
5778 | pDisk->cbSize = 0;
|
---|
5779 | pDisk->PCHSGeometry.cCylinders = 0;
|
---|
5780 | pDisk->PCHSGeometry.cHeads = 0;
|
---|
5781 | pDisk->PCHSGeometry.cSectors = 0;
|
---|
5782 | pDisk->LCHSGeometry.cCylinders = 0;
|
---|
5783 | pDisk->LCHSGeometry.cHeads = 0;
|
---|
5784 | pDisk->LCHSGeometry.cSectors = 0;
|
---|
5785 | pDisk->pVDIfsDisk = pVDIfsDisk;
|
---|
5786 | pDisk->pInterfaceError = NULL;
|
---|
5787 | pDisk->pInterfaceThreadSync = NULL;
|
---|
5788 | pDisk->pIoCtxLockOwner = NULL;
|
---|
5789 | pDisk->pIoCtxHead = NULL;
|
---|
5790 | pDisk->fLocked = false;
|
---|
5791 | pDisk->hMemCacheIoCtx = NIL_RTMEMCACHE;
|
---|
5792 | pDisk->hMemCacheIoTask = NIL_RTMEMCACHE;
|
---|
5793 | pDisk->pFilterHead = NULL;
|
---|
5794 | pDisk->pFilterTail = NULL;
|
---|
5795 |
|
---|
5796 | /* Create the I/O ctx cache */
|
---|
5797 | rc = RTMemCacheCreate(&pDisk->hMemCacheIoCtx, sizeof(VDIOCTX), 0, UINT32_MAX,
|
---|
5798 | NULL, NULL, NULL, 0);
|
---|
5799 | if (RT_FAILURE(rc))
|
---|
5800 | break;
|
---|
5801 |
|
---|
5802 | /* Create the I/O task cache */
|
---|
5803 | rc = RTMemCacheCreate(&pDisk->hMemCacheIoTask, sizeof(VDIOTASK), 0, UINT32_MAX,
|
---|
5804 | NULL, NULL, NULL, 0);
|
---|
5805 | if (RT_FAILURE(rc))
|
---|
5806 | break;
|
---|
5807 |
|
---|
5808 | pDisk->pInterfaceError = VDIfErrorGet(pVDIfsDisk);
|
---|
5809 | pDisk->pInterfaceThreadSync = VDIfThreadSyncGet(pVDIfsDisk);
|
---|
5810 |
|
---|
5811 | *ppDisk = pDisk;
|
---|
5812 | }
|
---|
5813 | else
|
---|
5814 | {
|
---|
5815 | rc = VERR_NO_MEMORY;
|
---|
5816 | break;
|
---|
5817 | }
|
---|
5818 | } while (0);
|
---|
5819 |
|
---|
5820 | if ( RT_FAILURE(rc)
|
---|
5821 | && pDisk)
|
---|
5822 | {
|
---|
5823 | if (pDisk->hMemCacheIoCtx != NIL_RTMEMCACHE)
|
---|
5824 | RTMemCacheDestroy(pDisk->hMemCacheIoCtx);
|
---|
5825 | if (pDisk->hMemCacheIoTask != NIL_RTMEMCACHE)
|
---|
5826 | RTMemCacheDestroy(pDisk->hMemCacheIoTask);
|
---|
5827 | }
|
---|
5828 |
|
---|
5829 | LogFlowFunc(("returns %Rrc (pDisk=%#p)\n", rc, pDisk));
|
---|
5830 | return rc;
|
---|
5831 | }
|
---|
5832 |
|
---|
5833 | /**
|
---|
5834 | * Destroys HDD container.
|
---|
5835 | * If container has opened image files they will be closed.
|
---|
5836 | *
|
---|
5837 | * @returns VBox status code.
|
---|
5838 | * @param pDisk Pointer to HDD container.
|
---|
5839 | */
|
---|
5840 | VBOXDDU_DECL(int) VDDestroy(PVBOXHDD pDisk)
|
---|
5841 | {
|
---|
5842 | int rc = VINF_SUCCESS;
|
---|
5843 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
5844 | do
|
---|
5845 | {
|
---|
5846 | /* sanity check */
|
---|
5847 | AssertPtrBreak(pDisk);
|
---|
5848 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
5849 | Assert(!pDisk->fLocked);
|
---|
5850 |
|
---|
5851 | rc = VDCloseAll(pDisk);
|
---|
5852 | int rc2 = VDFilterRemoveAll(pDisk);
|
---|
5853 | if (RT_SUCCESS(rc))
|
---|
5854 | rc = rc2;
|
---|
5855 |
|
---|
5856 | RTMemCacheDestroy(pDisk->hMemCacheIoCtx);
|
---|
5857 | RTMemCacheDestroy(pDisk->hMemCacheIoTask);
|
---|
5858 | RTMemFree(pDisk);
|
---|
5859 | } while (0);
|
---|
5860 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
5861 | return rc;
|
---|
5862 | }
|
---|
5863 |
|
---|
5864 | /**
|
---|
5865 | * Try to get the backend name which can use this image.
|
---|
5866 | *
|
---|
5867 | * @returns VBox status code.
|
---|
5868 | * VINF_SUCCESS if a plugin was found.
|
---|
5869 | * ppszFormat contains the string which can be used as backend name.
|
---|
5870 | * VERR_NOT_SUPPORTED if no backend was found.
|
---|
5871 | * @param pVDIfsDisk Pointer to the per-disk VD interface list.
|
---|
5872 | * @param pVDIfsImage Pointer to the per-image VD interface list.
|
---|
5873 | * @param pszFilename Name of the image file for which the backend is queried.
|
---|
5874 | * @param ppszFormat Receives pointer of the UTF-8 string which contains the format name.
|
---|
5875 | * The returned pointer must be freed using RTStrFree().
|
---|
5876 | */
|
---|
5877 | VBOXDDU_DECL(int) VDGetFormat(PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
|
---|
5878 | const char *pszFilename, char **ppszFormat, VDTYPE *penmType)
|
---|
5879 | {
|
---|
5880 | int rc = VERR_NOT_SUPPORTED;
|
---|
5881 | VDINTERFACEIOINT VDIfIoInt;
|
---|
5882 | VDINTERFACEIO VDIfIoFallback;
|
---|
5883 | PVDINTERFACEIO pInterfaceIo;
|
---|
5884 |
|
---|
5885 | LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
|
---|
5886 | /* Check arguments. */
|
---|
5887 | AssertMsgReturn(VALID_PTR(pszFilename) && *pszFilename,
|
---|
5888 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
5889 | VERR_INVALID_PARAMETER);
|
---|
5890 | AssertMsgReturn(VALID_PTR(ppszFormat),
|
---|
5891 | ("ppszFormat=%#p\n", ppszFormat),
|
---|
5892 | VERR_INVALID_PARAMETER);
|
---|
5893 | AssertMsgReturn(VALID_PTR(penmType),
|
---|
5894 | ("penmType=%#p\n", penmType),
|
---|
5895 | VERR_INVALID_PARAMETER);
|
---|
5896 |
|
---|
5897 | if (!g_apBackends)
|
---|
5898 | VDInit();
|
---|
5899 |
|
---|
5900 | pInterfaceIo = VDIfIoGet(pVDIfsImage);
|
---|
5901 | if (!pInterfaceIo)
|
---|
5902 | {
|
---|
5903 | /*
|
---|
5904 | * Caller doesn't provide an I/O interface, create our own using the
|
---|
5905 | * native file API.
|
---|
5906 | */
|
---|
5907 | vdIfIoFallbackCallbacksSetup(&VDIfIoFallback);
|
---|
5908 | pInterfaceIo = &VDIfIoFallback;
|
---|
5909 | }
|
---|
5910 |
|
---|
5911 | /* Set up the internal I/O interface. */
|
---|
5912 | AssertReturn(!VDIfIoIntGet(pVDIfsImage), VERR_INVALID_PARAMETER);
|
---|
5913 | VDIfIoInt.pfnOpen = vdIOIntOpenLimited;
|
---|
5914 | VDIfIoInt.pfnClose = vdIOIntCloseLimited;
|
---|
5915 | VDIfIoInt.pfnDelete = vdIOIntDeleteLimited;
|
---|
5916 | VDIfIoInt.pfnMove = vdIOIntMoveLimited;
|
---|
5917 | VDIfIoInt.pfnGetFreeSpace = vdIOIntGetFreeSpaceLimited;
|
---|
5918 | VDIfIoInt.pfnGetModificationTime = vdIOIntGetModificationTimeLimited;
|
---|
5919 | VDIfIoInt.pfnGetSize = vdIOIntGetSizeLimited;
|
---|
5920 | VDIfIoInt.pfnSetSize = vdIOIntSetSizeLimited;
|
---|
5921 | VDIfIoInt.pfnReadUser = vdIOIntReadUserLimited;
|
---|
5922 | VDIfIoInt.pfnWriteUser = vdIOIntWriteUserLimited;
|
---|
5923 | VDIfIoInt.pfnReadMeta = vdIOIntReadMetaLimited;
|
---|
5924 | VDIfIoInt.pfnWriteMeta = vdIOIntWriteMetaLimited;
|
---|
5925 | VDIfIoInt.pfnFlush = vdIOIntFlushLimited;
|
---|
5926 | rc = VDInterfaceAdd(&VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
5927 | pInterfaceIo, sizeof(VDINTERFACEIOINT), &pVDIfsImage);
|
---|
5928 | AssertRC(rc);
|
---|
5929 |
|
---|
5930 | /* Find the backend supporting this file format. */
|
---|
5931 | for (unsigned i = 0; i < g_cBackends; i++)
|
---|
5932 | {
|
---|
5933 | if (g_apBackends[i]->pfnCheckIfValid)
|
---|
5934 | {
|
---|
5935 | rc = g_apBackends[i]->pfnCheckIfValid(pszFilename, pVDIfsDisk,
|
---|
5936 | pVDIfsImage, penmType);
|
---|
5937 | if ( RT_SUCCESS(rc)
|
---|
5938 | /* The correct backend has been found, but there is a small
|
---|
5939 | * incompatibility so that the file cannot be used. Stop here
|
---|
5940 | * and signal success - the actual open will of course fail,
|
---|
5941 | * but that will create a really sensible error message. */
|
---|
5942 | || ( rc != VERR_VD_GEN_INVALID_HEADER
|
---|
5943 | && rc != VERR_VD_VDI_INVALID_HEADER
|
---|
5944 | && rc != VERR_VD_VMDK_INVALID_HEADER
|
---|
5945 | && rc != VERR_VD_ISCSI_INVALID_HEADER
|
---|
5946 | && rc != VERR_VD_VHD_INVALID_HEADER
|
---|
5947 | && rc != VERR_VD_RAW_INVALID_HEADER
|
---|
5948 | && rc != VERR_VD_PARALLELS_INVALID_HEADER
|
---|
5949 | && rc != VERR_VD_DMG_INVALID_HEADER))
|
---|
5950 | {
|
---|
5951 | /* Copy the name into the new string. */
|
---|
5952 | char *pszFormat = RTStrDup(g_apBackends[i]->pszBackendName);
|
---|
5953 | if (!pszFormat)
|
---|
5954 | {
|
---|
5955 | rc = VERR_NO_MEMORY;
|
---|
5956 | break;
|
---|
5957 | }
|
---|
5958 | *ppszFormat = pszFormat;
|
---|
5959 | /* Do not consider the typical file access errors as success,
|
---|
5960 | * which allows the caller to deal with such issues. */
|
---|
5961 | if ( rc != VERR_ACCESS_DENIED
|
---|
5962 | && rc != VERR_PATH_NOT_FOUND
|
---|
5963 | && rc != VERR_FILE_NOT_FOUND)
|
---|
5964 | rc = VINF_SUCCESS;
|
---|
5965 | break;
|
---|
5966 | }
|
---|
5967 | rc = VERR_NOT_SUPPORTED;
|
---|
5968 | }
|
---|
5969 | }
|
---|
5970 |
|
---|
5971 | /* Try the cache backends. */
|
---|
5972 | if (rc == VERR_NOT_SUPPORTED)
|
---|
5973 | {
|
---|
5974 | for (unsigned i = 0; i < g_cCacheBackends; i++)
|
---|
5975 | {
|
---|
5976 | if (g_apCacheBackends[i]->pfnProbe)
|
---|
5977 | {
|
---|
5978 | rc = g_apCacheBackends[i]->pfnProbe(pszFilename, pVDIfsDisk,
|
---|
5979 | pVDIfsImage);
|
---|
5980 | if ( RT_SUCCESS(rc)
|
---|
5981 | || (rc != VERR_VD_GEN_INVALID_HEADER))
|
---|
5982 | {
|
---|
5983 | /* Copy the name into the new string. */
|
---|
5984 | char *pszFormat = RTStrDup(g_apBackends[i]->pszBackendName);
|
---|
5985 | if (!pszFormat)
|
---|
5986 | {
|
---|
5987 | rc = VERR_NO_MEMORY;
|
---|
5988 | break;
|
---|
5989 | }
|
---|
5990 | *ppszFormat = pszFormat;
|
---|
5991 | rc = VINF_SUCCESS;
|
---|
5992 | break;
|
---|
5993 | }
|
---|
5994 | rc = VERR_NOT_SUPPORTED;
|
---|
5995 | }
|
---|
5996 | }
|
---|
5997 | }
|
---|
5998 |
|
---|
5999 | LogFlowFunc(("returns %Rrc *ppszFormat=\"%s\"\n", rc, *ppszFormat));
|
---|
6000 | return rc;
|
---|
6001 | }
|
---|
6002 |
|
---|
6003 | /**
|
---|
6004 | * Opens an image file.
|
---|
6005 | *
|
---|
6006 | * The first opened image file in HDD container must have a base image type,
|
---|
6007 | * others (next opened images) must be a differencing or undo images.
|
---|
6008 | * Linkage is checked for differencing image to be in consistence with the previously opened image.
|
---|
6009 | * When another differencing image is opened and the last image was opened in read/write access
|
---|
6010 | * mode, then the last image is reopened in read-only with deny write sharing mode. This allows
|
---|
6011 | * other processes to use images in read-only mode too.
|
---|
6012 | *
|
---|
6013 | * Note that the image is opened in read-only mode if a read/write open is not possible.
|
---|
6014 | * Use VDIsReadOnly to check open mode.
|
---|
6015 | *
|
---|
6016 | * @returns VBox status code.
|
---|
6017 | * @param pDisk Pointer to HDD container.
|
---|
6018 | * @param pszBackend Name of the image file backend to use.
|
---|
6019 | * @param pszFilename Name of the image file to open.
|
---|
6020 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
6021 | * @param pVDIfsImage Pointer to the per-image VD interface list.
|
---|
6022 | */
|
---|
6023 | VBOXDDU_DECL(int) VDOpen(PVBOXHDD pDisk, const char *pszBackend,
|
---|
6024 | const char *pszFilename, unsigned uOpenFlags,
|
---|
6025 | PVDINTERFACE pVDIfsImage)
|
---|
6026 | {
|
---|
6027 | int rc = VINF_SUCCESS;
|
---|
6028 | int rc2;
|
---|
6029 | bool fLockWrite = false;
|
---|
6030 | PVDIMAGE pImage = NULL;
|
---|
6031 |
|
---|
6032 | LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uOpenFlags=%#x, pVDIfsImage=%#p\n",
|
---|
6033 | pDisk, pszBackend, pszFilename, uOpenFlags, pVDIfsImage));
|
---|
6034 |
|
---|
6035 | do
|
---|
6036 | {
|
---|
6037 | /* sanity check */
|
---|
6038 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
6039 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6040 |
|
---|
6041 | /* Check arguments. */
|
---|
6042 | AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
|
---|
6043 | ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
|
---|
6044 | rc = VERR_INVALID_PARAMETER);
|
---|
6045 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
6046 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
6047 | rc = VERR_INVALID_PARAMETER);
|
---|
6048 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
6049 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
6050 | rc = VERR_INVALID_PARAMETER);
|
---|
6051 | AssertMsgBreakStmt( !(uOpenFlags & VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)
|
---|
6052 | || (uOpenFlags & VD_OPEN_FLAGS_READONLY),
|
---|
6053 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
6054 | rc = VERR_INVALID_PARAMETER);
|
---|
6055 |
|
---|
6056 | /*
|
---|
6057 | * Destroy the current discard state first which might still have pending blocks
|
---|
6058 | * for the currently opened image which will be switched to readonly mode.
|
---|
6059 | */
|
---|
6060 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
6061 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6062 | AssertRC(rc2);
|
---|
6063 | fLockWrite = true;
|
---|
6064 | rc = vdDiscardStateDestroy(pDisk);
|
---|
6065 | if (RT_FAILURE(rc))
|
---|
6066 | break;
|
---|
6067 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6068 | AssertRC(rc2);
|
---|
6069 | fLockWrite = false;
|
---|
6070 |
|
---|
6071 | /* Set up image descriptor. */
|
---|
6072 | pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
|
---|
6073 | if (!pImage)
|
---|
6074 | {
|
---|
6075 | rc = VERR_NO_MEMORY;
|
---|
6076 | break;
|
---|
6077 | }
|
---|
6078 | pImage->pszFilename = RTStrDup(pszFilename);
|
---|
6079 | if (!pImage->pszFilename)
|
---|
6080 | {
|
---|
6081 | rc = VERR_NO_MEMORY;
|
---|
6082 | break;
|
---|
6083 | }
|
---|
6084 |
|
---|
6085 | pImage->VDIo.pDisk = pDisk;
|
---|
6086 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
6087 |
|
---|
6088 | rc = vdFindBackend(pszBackend, &pImage->Backend);
|
---|
6089 | if (RT_FAILURE(rc))
|
---|
6090 | break;
|
---|
6091 | if (!pImage->Backend)
|
---|
6092 | {
|
---|
6093 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
6094 | N_("VD: unknown backend name '%s'"), pszBackend);
|
---|
6095 | break;
|
---|
6096 | }
|
---|
6097 |
|
---|
6098 | /*
|
---|
6099 | * Fail if the backend can't do async I/O but the
|
---|
6100 | * flag is set.
|
---|
6101 | */
|
---|
6102 | if ( !(pImage->Backend->uBackendCaps & VD_CAP_ASYNC)
|
---|
6103 | && (uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO))
|
---|
6104 | {
|
---|
6105 | rc = vdError(pDisk, VERR_NOT_SUPPORTED, RT_SRC_POS,
|
---|
6106 | N_("VD: Backend '%s' does not support async I/O"), pszBackend);
|
---|
6107 | break;
|
---|
6108 | }
|
---|
6109 |
|
---|
6110 | /*
|
---|
6111 | * Fail if the backend doesn't support the discard operation but the
|
---|
6112 | * flag is set.
|
---|
6113 | */
|
---|
6114 | if ( !(pImage->Backend->uBackendCaps & VD_CAP_DISCARD)
|
---|
6115 | && (uOpenFlags & VD_OPEN_FLAGS_DISCARD))
|
---|
6116 | {
|
---|
6117 | rc = vdError(pDisk, VERR_VD_DISCARD_NOT_SUPPORTED, RT_SRC_POS,
|
---|
6118 | N_("VD: Backend '%s' does not support discard"), pszBackend);
|
---|
6119 | break;
|
---|
6120 | }
|
---|
6121 |
|
---|
6122 | /* Set up the I/O interface. */
|
---|
6123 | pImage->VDIo.pInterfaceIo = VDIfIoGet(pVDIfsImage);
|
---|
6124 | if (!pImage->VDIo.pInterfaceIo)
|
---|
6125 | {
|
---|
6126 | vdIfIoFallbackCallbacksSetup(&pImage->VDIo.VDIfIo);
|
---|
6127 | rc = VDInterfaceAdd(&pImage->VDIo.VDIfIo.Core, "VD_IO", VDINTERFACETYPE_IO,
|
---|
6128 | pDisk, sizeof(VDINTERFACEIO), &pVDIfsImage);
|
---|
6129 | pImage->VDIo.pInterfaceIo = &pImage->VDIo.VDIfIo;
|
---|
6130 | }
|
---|
6131 |
|
---|
6132 | /* Set up the internal I/O interface. */
|
---|
6133 | AssertBreakStmt(!VDIfIoIntGet(pVDIfsImage), rc = VERR_INVALID_PARAMETER);
|
---|
6134 | vdIfIoIntCallbacksSetup(&pImage->VDIo.VDIfIoInt);
|
---|
6135 | rc = VDInterfaceAdd(&pImage->VDIo.VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
6136 | &pImage->VDIo, sizeof(VDINTERFACEIOINT), &pImage->pVDIfsImage);
|
---|
6137 | AssertRC(rc);
|
---|
6138 |
|
---|
6139 | pImage->uOpenFlags = uOpenFlags & (VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_DISCARD | VD_OPEN_FLAGS_IGNORE_FLUSH | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS);
|
---|
6140 | pImage->VDIo.fIgnoreFlush = (uOpenFlags & VD_OPEN_FLAGS_IGNORE_FLUSH) != 0;
|
---|
6141 | rc = pImage->Backend->pfnOpen(pImage->pszFilename,
|
---|
6142 | uOpenFlags & ~(VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_IGNORE_FLUSH | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS),
|
---|
6143 | pDisk->pVDIfsDisk,
|
---|
6144 | pImage->pVDIfsImage,
|
---|
6145 | pDisk->enmType,
|
---|
6146 | &pImage->pBackendData);
|
---|
6147 | /*
|
---|
6148 | * If the image is corrupted and there is a repair method try to repair it
|
---|
6149 | * first if it was openend in read-write mode and open again afterwards.
|
---|
6150 | */
|
---|
6151 | if ( RT_UNLIKELY(rc == VERR_VD_IMAGE_CORRUPTED)
|
---|
6152 | && !(uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
6153 | && pImage->Backend->pfnRepair)
|
---|
6154 | {
|
---|
6155 | rc = pImage->Backend->pfnRepair(pszFilename, pDisk->pVDIfsDisk, pImage->pVDIfsImage, 0 /* fFlags */);
|
---|
6156 | if (RT_SUCCESS(rc))
|
---|
6157 | rc = pImage->Backend->pfnOpen(pImage->pszFilename,
|
---|
6158 | uOpenFlags & ~(VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_IGNORE_FLUSH | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS),
|
---|
6159 | pDisk->pVDIfsDisk,
|
---|
6160 | pImage->pVDIfsImage,
|
---|
6161 | pDisk->enmType,
|
---|
6162 | &pImage->pBackendData);
|
---|
6163 | else
|
---|
6164 | {
|
---|
6165 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
6166 | N_("VD: error %Rrc repairing corrupted image file '%s'"), rc, pszFilename);
|
---|
6167 | break;
|
---|
6168 | }
|
---|
6169 | }
|
---|
6170 | else if (RT_UNLIKELY(rc == VERR_VD_IMAGE_CORRUPTED))
|
---|
6171 | {
|
---|
6172 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
6173 | N_("VD: Image file '%s' is corrupted and can't be opened"), pszFilename);
|
---|
6174 | break;
|
---|
6175 | }
|
---|
6176 |
|
---|
6177 | /* If the open in read-write mode failed, retry in read-only mode. */
|
---|
6178 | if (RT_FAILURE(rc))
|
---|
6179 | {
|
---|
6180 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
6181 | && ( rc == VERR_ACCESS_DENIED
|
---|
6182 | || rc == VERR_PERMISSION_DENIED
|
---|
6183 | || rc == VERR_WRITE_PROTECT
|
---|
6184 | || rc == VERR_SHARING_VIOLATION
|
---|
6185 | || rc == VERR_FILE_LOCK_FAILED))
|
---|
6186 | rc = pImage->Backend->pfnOpen(pImage->pszFilename,
|
---|
6187 | (uOpenFlags & ~(VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS))
|
---|
6188 | | VD_OPEN_FLAGS_READONLY,
|
---|
6189 | pDisk->pVDIfsDisk,
|
---|
6190 | pImage->pVDIfsImage,
|
---|
6191 | pDisk->enmType,
|
---|
6192 | &pImage->pBackendData);
|
---|
6193 | if (RT_FAILURE(rc))
|
---|
6194 | {
|
---|
6195 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
6196 | N_("VD: error %Rrc opening image file '%s'"), rc, pszFilename);
|
---|
6197 | break;
|
---|
6198 | }
|
---|
6199 | }
|
---|
6200 |
|
---|
6201 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
6202 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6203 | AssertRC(rc2);
|
---|
6204 | fLockWrite = true;
|
---|
6205 |
|
---|
6206 | pImage->VDIo.pBackendData = pImage->pBackendData;
|
---|
6207 |
|
---|
6208 | /* Check image type. As the image itself has only partial knowledge
|
---|
6209 | * whether it's a base image or not, this info is derived here. The
|
---|
6210 | * base image can be fixed or normal, all others must be normal or
|
---|
6211 | * diff images. Some image formats don't distinguish between normal
|
---|
6212 | * and diff images, so this must be corrected here. */
|
---|
6213 | unsigned uImageFlags;
|
---|
6214 | uImageFlags = pImage->Backend->pfnGetImageFlags(pImage->pBackendData);
|
---|
6215 | if (RT_FAILURE(rc))
|
---|
6216 | uImageFlags = VD_IMAGE_FLAGS_NONE;
|
---|
6217 | if ( RT_SUCCESS(rc)
|
---|
6218 | && !(uOpenFlags & VD_OPEN_FLAGS_INFO))
|
---|
6219 | {
|
---|
6220 | if ( pDisk->cImages == 0
|
---|
6221 | && (uImageFlags & VD_IMAGE_FLAGS_DIFF))
|
---|
6222 | {
|
---|
6223 | rc = VERR_VD_INVALID_TYPE;
|
---|
6224 | break;
|
---|
6225 | }
|
---|
6226 | else if (pDisk->cImages != 0)
|
---|
6227 | {
|
---|
6228 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
6229 | {
|
---|
6230 | rc = VERR_VD_INVALID_TYPE;
|
---|
6231 | break;
|
---|
6232 | }
|
---|
6233 | else
|
---|
6234 | uImageFlags |= VD_IMAGE_FLAGS_DIFF;
|
---|
6235 | }
|
---|
6236 | }
|
---|
6237 |
|
---|
6238 | /* Ensure we always get correct diff information, even if the backend
|
---|
6239 | * doesn't actually have a stored flag for this. It must not return
|
---|
6240 | * bogus information for the parent UUID if it is not a diff image. */
|
---|
6241 | RTUUID parentUuid;
|
---|
6242 | RTUuidClear(&parentUuid);
|
---|
6243 | rc2 = pImage->Backend->pfnGetParentUuid(pImage->pBackendData, &parentUuid);
|
---|
6244 | if (RT_SUCCESS(rc2) && !RTUuidIsNull(&parentUuid))
|
---|
6245 | uImageFlags |= VD_IMAGE_FLAGS_DIFF;
|
---|
6246 |
|
---|
6247 | pImage->uImageFlags = uImageFlags;
|
---|
6248 |
|
---|
6249 | /* Force sane optimization settings. It's not worth avoiding writes
|
---|
6250 | * to fixed size images. The overhead would have almost no payback. */
|
---|
6251 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
6252 | pImage->uOpenFlags |= VD_OPEN_FLAGS_HONOR_SAME;
|
---|
6253 |
|
---|
6254 | /** @todo optionally check UUIDs */
|
---|
6255 |
|
---|
6256 | /* Cache disk information. */
|
---|
6257 | pDisk->cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
|
---|
6258 |
|
---|
6259 | /* Cache PCHS geometry. */
|
---|
6260 | rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
6261 | &pDisk->PCHSGeometry);
|
---|
6262 | if (RT_FAILURE(rc2))
|
---|
6263 | {
|
---|
6264 | pDisk->PCHSGeometry.cCylinders = 0;
|
---|
6265 | pDisk->PCHSGeometry.cHeads = 0;
|
---|
6266 | pDisk->PCHSGeometry.cSectors = 0;
|
---|
6267 | }
|
---|
6268 | else
|
---|
6269 | {
|
---|
6270 | /* Make sure the PCHS geometry is properly clipped. */
|
---|
6271 | pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
|
---|
6272 | pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
|
---|
6273 | pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
|
---|
6274 | }
|
---|
6275 |
|
---|
6276 | /* Cache LCHS geometry. */
|
---|
6277 | rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
6278 | &pDisk->LCHSGeometry);
|
---|
6279 | if (RT_FAILURE(rc2))
|
---|
6280 | {
|
---|
6281 | pDisk->LCHSGeometry.cCylinders = 0;
|
---|
6282 | pDisk->LCHSGeometry.cHeads = 0;
|
---|
6283 | pDisk->LCHSGeometry.cSectors = 0;
|
---|
6284 | }
|
---|
6285 | else
|
---|
6286 | {
|
---|
6287 | /* Make sure the LCHS geometry is properly clipped. */
|
---|
6288 | pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
|
---|
6289 | pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
|
---|
6290 | }
|
---|
6291 |
|
---|
6292 | if (pDisk->cImages != 0)
|
---|
6293 | {
|
---|
6294 | /* Switch previous image to read-only mode. */
|
---|
6295 | unsigned uOpenFlagsPrevImg;
|
---|
6296 | uOpenFlagsPrevImg = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pBackendData);
|
---|
6297 | if (!(uOpenFlagsPrevImg & VD_OPEN_FLAGS_READONLY))
|
---|
6298 | {
|
---|
6299 | uOpenFlagsPrevImg |= VD_OPEN_FLAGS_READONLY;
|
---|
6300 | rc = pDisk->pLast->Backend->pfnSetOpenFlags(pDisk->pLast->pBackendData, uOpenFlagsPrevImg);
|
---|
6301 | }
|
---|
6302 | }
|
---|
6303 |
|
---|
6304 | if (RT_SUCCESS(rc))
|
---|
6305 | {
|
---|
6306 | /* Image successfully opened, make it the last image. */
|
---|
6307 | vdAddImageToList(pDisk, pImage);
|
---|
6308 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
6309 | pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
|
---|
6310 | }
|
---|
6311 | else
|
---|
6312 | {
|
---|
6313 | /* Error detected, but image opened. Close image. */
|
---|
6314 | rc2 = pImage->Backend->pfnClose(pImage->pBackendData, false);
|
---|
6315 | AssertRC(rc2);
|
---|
6316 | pImage->pBackendData = NULL;
|
---|
6317 | }
|
---|
6318 | } while (0);
|
---|
6319 |
|
---|
6320 | if (RT_UNLIKELY(fLockWrite))
|
---|
6321 | {
|
---|
6322 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6323 | AssertRC(rc2);
|
---|
6324 | }
|
---|
6325 |
|
---|
6326 | if (RT_FAILURE(rc))
|
---|
6327 | {
|
---|
6328 | if (pImage)
|
---|
6329 | {
|
---|
6330 | if (pImage->pszFilename)
|
---|
6331 | RTStrFree(pImage->pszFilename);
|
---|
6332 | RTMemFree(pImage);
|
---|
6333 | }
|
---|
6334 | }
|
---|
6335 |
|
---|
6336 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6337 | return rc;
|
---|
6338 | }
|
---|
6339 |
|
---|
6340 | /**
|
---|
6341 | * Opens a cache image.
|
---|
6342 | *
|
---|
6343 | * @return VBox status code.
|
---|
6344 | * @param pDisk Pointer to the HDD container which should use the cache image.
|
---|
6345 | * @param pszBackend Name of the cache file backend to use (case insensitive).
|
---|
6346 | * @param pszFilename Name of the cache image to open.
|
---|
6347 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
6348 | * @param pVDIfsCache Pointer to the per-cache VD interface list.
|
---|
6349 | */
|
---|
6350 | VBOXDDU_DECL(int) VDCacheOpen(PVBOXHDD pDisk, const char *pszBackend,
|
---|
6351 | const char *pszFilename, unsigned uOpenFlags,
|
---|
6352 | PVDINTERFACE pVDIfsCache)
|
---|
6353 | {
|
---|
6354 | int rc = VINF_SUCCESS;
|
---|
6355 | int rc2;
|
---|
6356 | bool fLockWrite = false;
|
---|
6357 | PVDCACHE pCache = NULL;
|
---|
6358 |
|
---|
6359 | LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uOpenFlags=%#x, pVDIfsCache=%#p\n",
|
---|
6360 | pDisk, pszBackend, pszFilename, uOpenFlags, pVDIfsCache));
|
---|
6361 |
|
---|
6362 | do
|
---|
6363 | {
|
---|
6364 | /* sanity check */
|
---|
6365 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
6366 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6367 |
|
---|
6368 | /* Check arguments. */
|
---|
6369 | AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
|
---|
6370 | ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
|
---|
6371 | rc = VERR_INVALID_PARAMETER);
|
---|
6372 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
6373 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
6374 | rc = VERR_INVALID_PARAMETER);
|
---|
6375 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
6376 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
6377 | rc = VERR_INVALID_PARAMETER);
|
---|
6378 |
|
---|
6379 | /* Set up image descriptor. */
|
---|
6380 | pCache = (PVDCACHE)RTMemAllocZ(sizeof(VDCACHE));
|
---|
6381 | if (!pCache)
|
---|
6382 | {
|
---|
6383 | rc = VERR_NO_MEMORY;
|
---|
6384 | break;
|
---|
6385 | }
|
---|
6386 | pCache->pszFilename = RTStrDup(pszFilename);
|
---|
6387 | if (!pCache->pszFilename)
|
---|
6388 | {
|
---|
6389 | rc = VERR_NO_MEMORY;
|
---|
6390 | break;
|
---|
6391 | }
|
---|
6392 |
|
---|
6393 | pCache->VDIo.pDisk = pDisk;
|
---|
6394 | pCache->pVDIfsCache = pVDIfsCache;
|
---|
6395 |
|
---|
6396 | rc = vdFindCacheBackend(pszBackend, &pCache->Backend);
|
---|
6397 | if (RT_FAILURE(rc))
|
---|
6398 | break;
|
---|
6399 | if (!pCache->Backend)
|
---|
6400 | {
|
---|
6401 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
6402 | N_("VD: unknown backend name '%s'"), pszBackend);
|
---|
6403 | break;
|
---|
6404 | }
|
---|
6405 |
|
---|
6406 | /* Set up the I/O interface. */
|
---|
6407 | pCache->VDIo.pInterfaceIo = VDIfIoGet(pVDIfsCache);
|
---|
6408 | if (!pCache->VDIo.pInterfaceIo)
|
---|
6409 | {
|
---|
6410 | vdIfIoFallbackCallbacksSetup(&pCache->VDIo.VDIfIo);
|
---|
6411 | rc = VDInterfaceAdd(&pCache->VDIo.VDIfIo.Core, "VD_IO", VDINTERFACETYPE_IO,
|
---|
6412 | pDisk, sizeof(VDINTERFACEIO), &pVDIfsCache);
|
---|
6413 | pCache->VDIo.pInterfaceIo = &pCache->VDIo.VDIfIo;
|
---|
6414 | }
|
---|
6415 |
|
---|
6416 | /* Set up the internal I/O interface. */
|
---|
6417 | AssertBreakStmt(!VDIfIoIntGet(pVDIfsCache), rc = VERR_INVALID_PARAMETER);
|
---|
6418 | vdIfIoIntCallbacksSetup(&pCache->VDIo.VDIfIoInt);
|
---|
6419 | rc = VDInterfaceAdd(&pCache->VDIo.VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
6420 | &pCache->VDIo, sizeof(VDINTERFACEIOINT), &pCache->pVDIfsCache);
|
---|
6421 | AssertRC(rc);
|
---|
6422 |
|
---|
6423 | pCache->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
|
---|
6424 | rc = pCache->Backend->pfnOpen(pCache->pszFilename,
|
---|
6425 | uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
|
---|
6426 | pDisk->pVDIfsDisk,
|
---|
6427 | pCache->pVDIfsCache,
|
---|
6428 | &pCache->pBackendData);
|
---|
6429 | /* If the open in read-write mode failed, retry in read-only mode. */
|
---|
6430 | if (RT_FAILURE(rc))
|
---|
6431 | {
|
---|
6432 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
6433 | && ( rc == VERR_ACCESS_DENIED
|
---|
6434 | || rc == VERR_PERMISSION_DENIED
|
---|
6435 | || rc == VERR_WRITE_PROTECT
|
---|
6436 | || rc == VERR_SHARING_VIOLATION
|
---|
6437 | || rc == VERR_FILE_LOCK_FAILED))
|
---|
6438 | rc = pCache->Backend->pfnOpen(pCache->pszFilename,
|
---|
6439 | (uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME)
|
---|
6440 | | VD_OPEN_FLAGS_READONLY,
|
---|
6441 | pDisk->pVDIfsDisk,
|
---|
6442 | pCache->pVDIfsCache,
|
---|
6443 | &pCache->pBackendData);
|
---|
6444 | if (RT_FAILURE(rc))
|
---|
6445 | {
|
---|
6446 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
6447 | N_("VD: error %Rrc opening image file '%s'"), rc, pszFilename);
|
---|
6448 | break;
|
---|
6449 | }
|
---|
6450 | }
|
---|
6451 |
|
---|
6452 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
6453 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6454 | AssertRC(rc2);
|
---|
6455 | fLockWrite = true;
|
---|
6456 |
|
---|
6457 | /*
|
---|
6458 | * Check that the modification UUID of the cache and last image
|
---|
6459 | * match. If not the image was modified in-between without the cache.
|
---|
6460 | * The cache might contain stale data.
|
---|
6461 | */
|
---|
6462 | RTUUID UuidImage, UuidCache;
|
---|
6463 |
|
---|
6464 | rc = pCache->Backend->pfnGetModificationUuid(pCache->pBackendData,
|
---|
6465 | &UuidCache);
|
---|
6466 | if (RT_SUCCESS(rc))
|
---|
6467 | {
|
---|
6468 | rc = pDisk->pLast->Backend->pfnGetModificationUuid(pDisk->pLast->pBackendData,
|
---|
6469 | &UuidImage);
|
---|
6470 | if (RT_SUCCESS(rc))
|
---|
6471 | {
|
---|
6472 | if (RTUuidCompare(&UuidImage, &UuidCache))
|
---|
6473 | rc = VERR_VD_CACHE_NOT_UP_TO_DATE;
|
---|
6474 | }
|
---|
6475 | }
|
---|
6476 |
|
---|
6477 | /*
|
---|
6478 | * We assume that the user knows what he is doing if one of the images
|
---|
6479 | * doesn't support the modification uuid.
|
---|
6480 | */
|
---|
6481 | if (rc == VERR_NOT_SUPPORTED)
|
---|
6482 | rc = VINF_SUCCESS;
|
---|
6483 |
|
---|
6484 | if (RT_SUCCESS(rc))
|
---|
6485 | {
|
---|
6486 | /* Cache successfully opened, make it the current one. */
|
---|
6487 | if (!pDisk->pCache)
|
---|
6488 | pDisk->pCache = pCache;
|
---|
6489 | else
|
---|
6490 | rc = VERR_VD_CACHE_ALREADY_EXISTS;
|
---|
6491 | }
|
---|
6492 |
|
---|
6493 | if (RT_FAILURE(rc))
|
---|
6494 | {
|
---|
6495 | /* Error detected, but image opened. Close image. */
|
---|
6496 | rc2 = pCache->Backend->pfnClose(pCache->pBackendData, false);
|
---|
6497 | AssertRC(rc2);
|
---|
6498 | pCache->pBackendData = NULL;
|
---|
6499 | }
|
---|
6500 | } while (0);
|
---|
6501 |
|
---|
6502 | if (RT_UNLIKELY(fLockWrite))
|
---|
6503 | {
|
---|
6504 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6505 | AssertRC(rc2);
|
---|
6506 | }
|
---|
6507 |
|
---|
6508 | if (RT_FAILURE(rc))
|
---|
6509 | {
|
---|
6510 | if (pCache)
|
---|
6511 | {
|
---|
6512 | if (pCache->pszFilename)
|
---|
6513 | RTStrFree(pCache->pszFilename);
|
---|
6514 | RTMemFree(pCache);
|
---|
6515 | }
|
---|
6516 | }
|
---|
6517 |
|
---|
6518 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6519 | return rc;
|
---|
6520 | }
|
---|
6521 |
|
---|
6522 | /**
|
---|
6523 | * Adds a filter to the disk.
|
---|
6524 | *
|
---|
6525 | * @returns VBox status code.
|
---|
6526 | * @param pDisk Pointer to the HDD container which should use the filter.
|
---|
6527 | * @param pszFilter Name of the filter backend to use (case insensitive).
|
---|
6528 | * @param pVDIfsFilter Pointer to the per-filter VD interface list.
|
---|
6529 | */
|
---|
6530 | VBOXDDU_DECL(int) VDFilterAdd(PVBOXHDD pDisk, const char *pszFilter,
|
---|
6531 | PVDINTERFACE pVDIfsFilter)
|
---|
6532 | {
|
---|
6533 | int rc = VINF_SUCCESS;
|
---|
6534 | int rc2;
|
---|
6535 | bool fLockWrite = false;
|
---|
6536 | PVDFILTER pFilter = NULL;
|
---|
6537 |
|
---|
6538 | LogFlowFunc(("pDisk=%#p pszFilter=\"%s\" pVDIfsFilter=%#p\n",
|
---|
6539 | pDisk, pszFilter, pVDIfsFilter));
|
---|
6540 |
|
---|
6541 | do
|
---|
6542 | {
|
---|
6543 | /* sanity check */
|
---|
6544 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
6545 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6546 |
|
---|
6547 | /* Check arguments. */
|
---|
6548 | AssertMsgBreakStmt(VALID_PTR(pszFilter) && *pszFilter,
|
---|
6549 | ("pszFilter=%#p \"%s\"\n", pszFilter, pszFilter),
|
---|
6550 | rc = VERR_INVALID_PARAMETER);
|
---|
6551 |
|
---|
6552 | /* Set up image descriptor. */
|
---|
6553 | pFilter = (PVDFILTER)RTMemAllocZ(sizeof(VDFILTER));
|
---|
6554 | if (!pFilter)
|
---|
6555 | {
|
---|
6556 | rc = VERR_NO_MEMORY;
|
---|
6557 | break;
|
---|
6558 | }
|
---|
6559 |
|
---|
6560 | rc = vdFindFilterBackend(pszFilter, &pFilter->pBackend);
|
---|
6561 | if (RT_FAILURE(rc))
|
---|
6562 | break;
|
---|
6563 | if (!pFilter->pBackend)
|
---|
6564 | {
|
---|
6565 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
6566 | N_("VD: unknown filter backend name '%s'"), pszFilter);
|
---|
6567 | break;
|
---|
6568 | }
|
---|
6569 |
|
---|
6570 | pFilter->VDIo.pDisk = pDisk;
|
---|
6571 | pFilter->pVDIfsFilter = pVDIfsFilter;
|
---|
6572 |
|
---|
6573 | /* Set up the internal I/O interface. */
|
---|
6574 | AssertBreakStmt(!VDIfIoIntGet(pVDIfsFilter), rc = VERR_INVALID_PARAMETER);
|
---|
6575 | vdIfIoIntCallbacksSetup(&pFilter->VDIo.VDIfIoInt);
|
---|
6576 | rc = VDInterfaceAdd(&pFilter->VDIo.VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
6577 | &pFilter->VDIo, sizeof(VDINTERFACEIOINT), &pFilter->pVDIfsFilter);
|
---|
6578 | AssertRC(rc);
|
---|
6579 |
|
---|
6580 | rc = pFilter->pBackend->pfnCreate(pDisk->pVDIfsDisk, pFilter->pVDIfsFilter,
|
---|
6581 | &pFilter->pvBackendData);
|
---|
6582 |
|
---|
6583 | /* If the open in read-write mode failed, retry in read-only mode. */
|
---|
6584 | if (RT_FAILURE(rc))
|
---|
6585 | {
|
---|
6586 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
6587 | N_("VD: error %Rrc creating filter '%s'"), rc, pszFilter);
|
---|
6588 | break;
|
---|
6589 | }
|
---|
6590 |
|
---|
6591 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
6592 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6593 | AssertRC(rc2);
|
---|
6594 | fLockWrite = true;
|
---|
6595 |
|
---|
6596 | /* Add filter to chain. */
|
---|
6597 | vdAddFilterToList(pDisk, pFilter);
|
---|
6598 | } while (0);
|
---|
6599 |
|
---|
6600 | if (RT_UNLIKELY(fLockWrite))
|
---|
6601 | {
|
---|
6602 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6603 | AssertRC(rc2);
|
---|
6604 | }
|
---|
6605 |
|
---|
6606 | if (RT_FAILURE(rc))
|
---|
6607 | {
|
---|
6608 | if (pFilter)
|
---|
6609 | RTMemFree(pFilter);
|
---|
6610 | }
|
---|
6611 |
|
---|
6612 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6613 | return rc;
|
---|
6614 | }
|
---|
6615 |
|
---|
6616 | /**
|
---|
6617 | * Creates and opens a new base image file.
|
---|
6618 | *
|
---|
6619 | * @returns VBox status code.
|
---|
6620 | * @param pDisk Pointer to HDD container.
|
---|
6621 | * @param pszBackend Name of the image file backend to use.
|
---|
6622 | * @param pszFilename Name of the image file to create.
|
---|
6623 | * @param cbSize Image size in bytes.
|
---|
6624 | * @param uImageFlags Flags specifying special image features.
|
---|
6625 | * @param pszComment Pointer to image comment. NULL is ok.
|
---|
6626 | * @param pPCHSGeometry Pointer to physical disk geometry <= (16383,16,63). Not NULL.
|
---|
6627 | * @param pLCHSGeometry Pointer to logical disk geometry <= (x,255,63). Not NULL.
|
---|
6628 | * @param pUuid New UUID of the image. If NULL, a new UUID is created.
|
---|
6629 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
6630 | * @param pVDIfsImage Pointer to the per-image VD interface list.
|
---|
6631 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
6632 | */
|
---|
6633 | VBOXDDU_DECL(int) VDCreateBase(PVBOXHDD pDisk, const char *pszBackend,
|
---|
6634 | const char *pszFilename, uint64_t cbSize,
|
---|
6635 | unsigned uImageFlags, const char *pszComment,
|
---|
6636 | PCVDGEOMETRY pPCHSGeometry,
|
---|
6637 | PCVDGEOMETRY pLCHSGeometry,
|
---|
6638 | PCRTUUID pUuid, unsigned uOpenFlags,
|
---|
6639 | PVDINTERFACE pVDIfsImage,
|
---|
6640 | PVDINTERFACE pVDIfsOperation)
|
---|
6641 | {
|
---|
6642 | int rc = VINF_SUCCESS;
|
---|
6643 | int rc2;
|
---|
6644 | bool fLockWrite = false, fLockRead = false;
|
---|
6645 | PVDIMAGE pImage = NULL;
|
---|
6646 | RTUUID uuid;
|
---|
6647 |
|
---|
6648 | 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",
|
---|
6649 | pDisk, pszBackend, pszFilename, cbSize, uImageFlags, pszComment,
|
---|
6650 | pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
|
---|
6651 | pPCHSGeometry->cSectors, pLCHSGeometry->cCylinders,
|
---|
6652 | pLCHSGeometry->cHeads, pLCHSGeometry->cSectors, pUuid,
|
---|
6653 | uOpenFlags, pVDIfsImage, pVDIfsOperation));
|
---|
6654 |
|
---|
6655 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
6656 |
|
---|
6657 | do
|
---|
6658 | {
|
---|
6659 | /* sanity check */
|
---|
6660 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
6661 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6662 |
|
---|
6663 | /* Check arguments. */
|
---|
6664 | AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
|
---|
6665 | ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
|
---|
6666 | rc = VERR_INVALID_PARAMETER);
|
---|
6667 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
6668 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
6669 | rc = VERR_INVALID_PARAMETER);
|
---|
6670 | AssertMsgBreakStmt(cbSize,
|
---|
6671 | ("cbSize=%llu\n", cbSize),
|
---|
6672 | rc = VERR_INVALID_PARAMETER);
|
---|
6673 | AssertMsgBreakStmt( ((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0)
|
---|
6674 | || ((uImageFlags & (VD_IMAGE_FLAGS_FIXED | VD_IMAGE_FLAGS_DIFF)) != VD_IMAGE_FLAGS_FIXED),
|
---|
6675 | ("uImageFlags=%#x\n", uImageFlags),
|
---|
6676 | rc = VERR_INVALID_PARAMETER);
|
---|
6677 | /* The PCHS geometry fields may be 0 to leave it for later. */
|
---|
6678 | AssertMsgBreakStmt( VALID_PTR(pPCHSGeometry)
|
---|
6679 | && pPCHSGeometry->cHeads <= 16
|
---|
6680 | && pPCHSGeometry->cSectors <= 63,
|
---|
6681 | ("pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pPCHSGeometry,
|
---|
6682 | pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
|
---|
6683 | pPCHSGeometry->cSectors),
|
---|
6684 | rc = VERR_INVALID_PARAMETER);
|
---|
6685 | /* The LCHS geometry fields may be 0 to leave it to later autodetection. */
|
---|
6686 | AssertMsgBreakStmt( VALID_PTR(pLCHSGeometry)
|
---|
6687 | && pLCHSGeometry->cHeads <= 255
|
---|
6688 | && pLCHSGeometry->cSectors <= 63,
|
---|
6689 | ("pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pLCHSGeometry,
|
---|
6690 | pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads,
|
---|
6691 | pLCHSGeometry->cSectors),
|
---|
6692 | rc = VERR_INVALID_PARAMETER);
|
---|
6693 | /* The UUID may be NULL. */
|
---|
6694 | AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
|
---|
6695 | ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
|
---|
6696 | rc = VERR_INVALID_PARAMETER);
|
---|
6697 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
6698 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
6699 | rc = VERR_INVALID_PARAMETER);
|
---|
6700 |
|
---|
6701 | /* Check state. Needs a temporary read lock. Holding the write lock
|
---|
6702 | * all the time would be blocking other activities for too long. */
|
---|
6703 | rc2 = vdThreadStartRead(pDisk);
|
---|
6704 | AssertRC(rc2);
|
---|
6705 | fLockRead = true;
|
---|
6706 | AssertMsgBreakStmt(pDisk->cImages == 0,
|
---|
6707 | ("Create base image cannot be done with other images open\n"),
|
---|
6708 | rc = VERR_VD_INVALID_STATE);
|
---|
6709 | rc2 = vdThreadFinishRead(pDisk);
|
---|
6710 | AssertRC(rc2);
|
---|
6711 | fLockRead = false;
|
---|
6712 |
|
---|
6713 | /* Set up image descriptor. */
|
---|
6714 | pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
|
---|
6715 | if (!pImage)
|
---|
6716 | {
|
---|
6717 | rc = VERR_NO_MEMORY;
|
---|
6718 | break;
|
---|
6719 | }
|
---|
6720 | pImage->pszFilename = RTStrDup(pszFilename);
|
---|
6721 | if (!pImage->pszFilename)
|
---|
6722 | {
|
---|
6723 | rc = VERR_NO_MEMORY;
|
---|
6724 | break;
|
---|
6725 | }
|
---|
6726 | pImage->VDIo.pDisk = pDisk;
|
---|
6727 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
6728 |
|
---|
6729 | /* Set up the I/O interface. */
|
---|
6730 | pImage->VDIo.pInterfaceIo = VDIfIoGet(pVDIfsImage);
|
---|
6731 | if (!pImage->VDIo.pInterfaceIo)
|
---|
6732 | {
|
---|
6733 | vdIfIoFallbackCallbacksSetup(&pImage->VDIo.VDIfIo);
|
---|
6734 | rc = VDInterfaceAdd(&pImage->VDIo.VDIfIo.Core, "VD_IO", VDINTERFACETYPE_IO,
|
---|
6735 | pDisk, sizeof(VDINTERFACEIO), &pVDIfsImage);
|
---|
6736 | pImage->VDIo.pInterfaceIo = &pImage->VDIo.VDIfIo;
|
---|
6737 | }
|
---|
6738 |
|
---|
6739 | /* Set up the internal I/O interface. */
|
---|
6740 | AssertBreakStmt(!VDIfIoIntGet(pVDIfsImage), rc = VERR_INVALID_PARAMETER);
|
---|
6741 | vdIfIoIntCallbacksSetup(&pImage->VDIo.VDIfIoInt);
|
---|
6742 | rc = VDInterfaceAdd(&pImage->VDIo.VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
6743 | &pImage->VDIo, sizeof(VDINTERFACEIOINT), &pImage->pVDIfsImage);
|
---|
6744 | AssertRC(rc);
|
---|
6745 |
|
---|
6746 | rc = vdFindBackend(pszBackend, &pImage->Backend);
|
---|
6747 | if (RT_FAILURE(rc))
|
---|
6748 | break;
|
---|
6749 | if (!pImage->Backend)
|
---|
6750 | {
|
---|
6751 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
6752 | N_("VD: unknown backend name '%s'"), pszBackend);
|
---|
6753 | break;
|
---|
6754 | }
|
---|
6755 | if (!(pImage->Backend->uBackendCaps & ( VD_CAP_CREATE_FIXED
|
---|
6756 | | VD_CAP_CREATE_DYNAMIC)))
|
---|
6757 | {
|
---|
6758 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
6759 | N_("VD: backend '%s' cannot create base images"), pszBackend);
|
---|
6760 | break;
|
---|
6761 | }
|
---|
6762 |
|
---|
6763 | /* Create UUID if the caller didn't specify one. */
|
---|
6764 | if (!pUuid)
|
---|
6765 | {
|
---|
6766 | rc = RTUuidCreate(&uuid);
|
---|
6767 | if (RT_FAILURE(rc))
|
---|
6768 | {
|
---|
6769 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
6770 | N_("VD: cannot generate UUID for image '%s'"),
|
---|
6771 | pszFilename);
|
---|
6772 | break;
|
---|
6773 | }
|
---|
6774 | pUuid = &uuid;
|
---|
6775 | }
|
---|
6776 |
|
---|
6777 | pImage->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
|
---|
6778 | uImageFlags &= ~VD_IMAGE_FLAGS_DIFF;
|
---|
6779 | pImage->VDIo.fIgnoreFlush = (uOpenFlags & VD_OPEN_FLAGS_IGNORE_FLUSH) != 0;
|
---|
6780 | rc = pImage->Backend->pfnCreate(pImage->pszFilename, cbSize,
|
---|
6781 | uImageFlags, pszComment, pPCHSGeometry,
|
---|
6782 | pLCHSGeometry, pUuid,
|
---|
6783 | uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
|
---|
6784 | 0, 99,
|
---|
6785 | pDisk->pVDIfsDisk,
|
---|
6786 | pImage->pVDIfsImage,
|
---|
6787 | pVDIfsOperation,
|
---|
6788 | &pImage->pBackendData);
|
---|
6789 |
|
---|
6790 | if (RT_SUCCESS(rc))
|
---|
6791 | {
|
---|
6792 | pImage->VDIo.pBackendData = pImage->pBackendData;
|
---|
6793 | pImage->uImageFlags = uImageFlags;
|
---|
6794 |
|
---|
6795 | /* Force sane optimization settings. It's not worth avoiding writes
|
---|
6796 | * to fixed size images. The overhead would have almost no payback. */
|
---|
6797 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
6798 | pImage->uOpenFlags |= VD_OPEN_FLAGS_HONOR_SAME;
|
---|
6799 |
|
---|
6800 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
6801 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6802 | AssertRC(rc2);
|
---|
6803 | fLockWrite = true;
|
---|
6804 |
|
---|
6805 | /** @todo optionally check UUIDs */
|
---|
6806 |
|
---|
6807 | /* Re-check state, as the lock wasn't held and another image
|
---|
6808 | * creation call could have been done by another thread. */
|
---|
6809 | AssertMsgStmt(pDisk->cImages == 0,
|
---|
6810 | ("Create base image cannot be done with other images open\n"),
|
---|
6811 | rc = VERR_VD_INVALID_STATE);
|
---|
6812 | }
|
---|
6813 |
|
---|
6814 | if (RT_SUCCESS(rc))
|
---|
6815 | {
|
---|
6816 | /* Cache disk information. */
|
---|
6817 | pDisk->cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
|
---|
6818 |
|
---|
6819 | /* Cache PCHS geometry. */
|
---|
6820 | rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
6821 | &pDisk->PCHSGeometry);
|
---|
6822 | if (RT_FAILURE(rc2))
|
---|
6823 | {
|
---|
6824 | pDisk->PCHSGeometry.cCylinders = 0;
|
---|
6825 | pDisk->PCHSGeometry.cHeads = 0;
|
---|
6826 | pDisk->PCHSGeometry.cSectors = 0;
|
---|
6827 | }
|
---|
6828 | else
|
---|
6829 | {
|
---|
6830 | /* Make sure the CHS geometry is properly clipped. */
|
---|
6831 | pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
|
---|
6832 | pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
|
---|
6833 | pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
|
---|
6834 | }
|
---|
6835 |
|
---|
6836 | /* Cache LCHS geometry. */
|
---|
6837 | rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
6838 | &pDisk->LCHSGeometry);
|
---|
6839 | if (RT_FAILURE(rc2))
|
---|
6840 | {
|
---|
6841 | pDisk->LCHSGeometry.cCylinders = 0;
|
---|
6842 | pDisk->LCHSGeometry.cHeads = 0;
|
---|
6843 | pDisk->LCHSGeometry.cSectors = 0;
|
---|
6844 | }
|
---|
6845 | else
|
---|
6846 | {
|
---|
6847 | /* Make sure the CHS geometry is properly clipped. */
|
---|
6848 | pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
|
---|
6849 | pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
|
---|
6850 | }
|
---|
6851 |
|
---|
6852 | /* Image successfully opened, make it the last image. */
|
---|
6853 | vdAddImageToList(pDisk, pImage);
|
---|
6854 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
6855 | pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
|
---|
6856 | }
|
---|
6857 | else
|
---|
6858 | {
|
---|
6859 | /* Error detected, image may or may not be opened. Close and delete
|
---|
6860 | * image if it was opened. */
|
---|
6861 | if (pImage->pBackendData)
|
---|
6862 | {
|
---|
6863 | rc2 = pImage->Backend->pfnClose(pImage->pBackendData, true);
|
---|
6864 | AssertRC(rc2);
|
---|
6865 | pImage->pBackendData = NULL;
|
---|
6866 | }
|
---|
6867 | }
|
---|
6868 | } while (0);
|
---|
6869 |
|
---|
6870 | if (RT_UNLIKELY(fLockWrite))
|
---|
6871 | {
|
---|
6872 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6873 | AssertRC(rc2);
|
---|
6874 | }
|
---|
6875 | else if (RT_UNLIKELY(fLockRead))
|
---|
6876 | {
|
---|
6877 | rc2 = vdThreadFinishRead(pDisk);
|
---|
6878 | AssertRC(rc2);
|
---|
6879 | }
|
---|
6880 |
|
---|
6881 | if (RT_FAILURE(rc))
|
---|
6882 | {
|
---|
6883 | if (pImage)
|
---|
6884 | {
|
---|
6885 | if (pImage->pszFilename)
|
---|
6886 | RTStrFree(pImage->pszFilename);
|
---|
6887 | RTMemFree(pImage);
|
---|
6888 | }
|
---|
6889 | }
|
---|
6890 |
|
---|
6891 | if (RT_SUCCESS(rc) && pIfProgress && pIfProgress->pfnProgress)
|
---|
6892 | pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
|
---|
6893 |
|
---|
6894 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6895 | return rc;
|
---|
6896 | }
|
---|
6897 |
|
---|
6898 | /**
|
---|
6899 | * Creates and opens a new differencing image file in HDD container.
|
---|
6900 | * See comments for VDOpen function about differencing images.
|
---|
6901 | *
|
---|
6902 | * @returns VBox status code.
|
---|
6903 | * @param pDisk Pointer to HDD container.
|
---|
6904 | * @param pszBackend Name of the image file backend to use.
|
---|
6905 | * @param pszFilename Name of the differencing image file to create.
|
---|
6906 | * @param uImageFlags Flags specifying special image features.
|
---|
6907 | * @param pszComment Pointer to image comment. NULL is ok.
|
---|
6908 | * @param pUuid New UUID of the image. If NULL, a new UUID is created.
|
---|
6909 | * @param pParentUuid New parent UUID of the image. If NULL, the UUID is queried automatically.
|
---|
6910 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
6911 | * @param pVDIfsImage Pointer to the per-image VD interface list.
|
---|
6912 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
6913 | */
|
---|
6914 | VBOXDDU_DECL(int) VDCreateDiff(PVBOXHDD pDisk, const char *pszBackend,
|
---|
6915 | const char *pszFilename, unsigned uImageFlags,
|
---|
6916 | const char *pszComment, PCRTUUID pUuid,
|
---|
6917 | PCRTUUID pParentUuid, unsigned uOpenFlags,
|
---|
6918 | PVDINTERFACE pVDIfsImage,
|
---|
6919 | PVDINTERFACE pVDIfsOperation)
|
---|
6920 | {
|
---|
6921 | int rc = VINF_SUCCESS;
|
---|
6922 | int rc2;
|
---|
6923 | bool fLockWrite = false, fLockRead = false;
|
---|
6924 | PVDIMAGE pImage = NULL;
|
---|
6925 | RTUUID uuid;
|
---|
6926 |
|
---|
6927 | LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uImageFlags=%#x pszComment=\"%s\" Uuid=%RTuuid uOpenFlags=%#x pVDIfsImage=%#p pVDIfsOperation=%#p\n",
|
---|
6928 | pDisk, pszBackend, pszFilename, uImageFlags, pszComment, pUuid, uOpenFlags, pVDIfsImage, pVDIfsOperation));
|
---|
6929 |
|
---|
6930 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
6931 |
|
---|
6932 | do
|
---|
6933 | {
|
---|
6934 | /* sanity check */
|
---|
6935 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
6936 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6937 |
|
---|
6938 | /* Check arguments. */
|
---|
6939 | AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
|
---|
6940 | ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
|
---|
6941 | rc = VERR_INVALID_PARAMETER);
|
---|
6942 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
6943 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
6944 | rc = VERR_INVALID_PARAMETER);
|
---|
6945 | AssertMsgBreakStmt((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0,
|
---|
6946 | ("uImageFlags=%#x\n", uImageFlags),
|
---|
6947 | rc = VERR_INVALID_PARAMETER);
|
---|
6948 | /* The UUID may be NULL. */
|
---|
6949 | AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
|
---|
6950 | ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
|
---|
6951 | rc = VERR_INVALID_PARAMETER);
|
---|
6952 | /* The parent UUID may be NULL. */
|
---|
6953 | AssertMsgBreakStmt(pParentUuid == NULL || VALID_PTR(pParentUuid),
|
---|
6954 | ("pParentUuid=%#p ParentUUID=%RTuuid\n", pParentUuid, pParentUuid),
|
---|
6955 | rc = VERR_INVALID_PARAMETER);
|
---|
6956 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
6957 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
6958 | rc = VERR_INVALID_PARAMETER);
|
---|
6959 |
|
---|
6960 | /* Check state. Needs a temporary read lock. Holding the write lock
|
---|
6961 | * all the time would be blocking other activities for too long. */
|
---|
6962 | rc2 = vdThreadStartRead(pDisk);
|
---|
6963 | AssertRC(rc2);
|
---|
6964 | fLockRead = true;
|
---|
6965 | AssertMsgBreakStmt(pDisk->cImages != 0,
|
---|
6966 | ("Create diff image cannot be done without other images open\n"),
|
---|
6967 | rc = VERR_VD_INVALID_STATE);
|
---|
6968 | rc2 = vdThreadFinishRead(pDisk);
|
---|
6969 | AssertRC(rc2);
|
---|
6970 | fLockRead = false;
|
---|
6971 |
|
---|
6972 | /*
|
---|
6973 | * Destroy the current discard state first which might still have pending blocks
|
---|
6974 | * for the currently opened image which will be switched to readonly mode.
|
---|
6975 | */
|
---|
6976 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
6977 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6978 | AssertRC(rc2);
|
---|
6979 | fLockWrite = true;
|
---|
6980 | rc = vdDiscardStateDestroy(pDisk);
|
---|
6981 | if (RT_FAILURE(rc))
|
---|
6982 | break;
|
---|
6983 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6984 | AssertRC(rc2);
|
---|
6985 | fLockWrite = false;
|
---|
6986 |
|
---|
6987 | /* Set up image descriptor. */
|
---|
6988 | pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
|
---|
6989 | if (!pImage)
|
---|
6990 | {
|
---|
6991 | rc = VERR_NO_MEMORY;
|
---|
6992 | break;
|
---|
6993 | }
|
---|
6994 | pImage->pszFilename = RTStrDup(pszFilename);
|
---|
6995 | if (!pImage->pszFilename)
|
---|
6996 | {
|
---|
6997 | rc = VERR_NO_MEMORY;
|
---|
6998 | break;
|
---|
6999 | }
|
---|
7000 |
|
---|
7001 | rc = vdFindBackend(pszBackend, &pImage->Backend);
|
---|
7002 | if (RT_FAILURE(rc))
|
---|
7003 | break;
|
---|
7004 | if (!pImage->Backend)
|
---|
7005 | {
|
---|
7006 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
7007 | N_("VD: unknown backend name '%s'"), pszBackend);
|
---|
7008 | break;
|
---|
7009 | }
|
---|
7010 | if ( !(pImage->Backend->uBackendCaps & VD_CAP_DIFF)
|
---|
7011 | || !(pImage->Backend->uBackendCaps & ( VD_CAP_CREATE_FIXED
|
---|
7012 | | VD_CAP_CREATE_DYNAMIC)))
|
---|
7013 | {
|
---|
7014 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
7015 | N_("VD: backend '%s' cannot create diff images"), pszBackend);
|
---|
7016 | break;
|
---|
7017 | }
|
---|
7018 |
|
---|
7019 | pImage->VDIo.pDisk = pDisk;
|
---|
7020 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
7021 |
|
---|
7022 | /* Set up the I/O interface. */
|
---|
7023 | pImage->VDIo.pInterfaceIo = VDIfIoGet(pVDIfsImage);
|
---|
7024 | if (!pImage->VDIo.pInterfaceIo)
|
---|
7025 | {
|
---|
7026 | vdIfIoFallbackCallbacksSetup(&pImage->VDIo.VDIfIo);
|
---|
7027 | rc = VDInterfaceAdd(&pImage->VDIo.VDIfIo.Core, "VD_IO", VDINTERFACETYPE_IO,
|
---|
7028 | pDisk, sizeof(VDINTERFACEIO), &pVDIfsImage);
|
---|
7029 | pImage->VDIo.pInterfaceIo = &pImage->VDIo.VDIfIo;
|
---|
7030 | }
|
---|
7031 |
|
---|
7032 | /* Set up the internal I/O interface. */
|
---|
7033 | AssertBreakStmt(!VDIfIoIntGet(pVDIfsImage), rc = VERR_INVALID_PARAMETER);
|
---|
7034 | vdIfIoIntCallbacksSetup(&pImage->VDIo.VDIfIoInt);
|
---|
7035 | rc = VDInterfaceAdd(&pImage->VDIo.VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
7036 | &pImage->VDIo, sizeof(VDINTERFACEIOINT), &pImage->pVDIfsImage);
|
---|
7037 | AssertRC(rc);
|
---|
7038 |
|
---|
7039 | /* Create UUID if the caller didn't specify one. */
|
---|
7040 | if (!pUuid)
|
---|
7041 | {
|
---|
7042 | rc = RTUuidCreate(&uuid);
|
---|
7043 | if (RT_FAILURE(rc))
|
---|
7044 | {
|
---|
7045 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
7046 | N_("VD: cannot generate UUID for image '%s'"),
|
---|
7047 | pszFilename);
|
---|
7048 | break;
|
---|
7049 | }
|
---|
7050 | pUuid = &uuid;
|
---|
7051 | }
|
---|
7052 |
|
---|
7053 | pImage->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
|
---|
7054 | pImage->VDIo.fIgnoreFlush = (uOpenFlags & VD_OPEN_FLAGS_IGNORE_FLUSH) != 0;
|
---|
7055 | uImageFlags |= VD_IMAGE_FLAGS_DIFF;
|
---|
7056 | rc = pImage->Backend->pfnCreate(pImage->pszFilename, pDisk->cbSize,
|
---|
7057 | uImageFlags | VD_IMAGE_FLAGS_DIFF,
|
---|
7058 | pszComment, &pDisk->PCHSGeometry,
|
---|
7059 | &pDisk->LCHSGeometry, pUuid,
|
---|
7060 | uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
|
---|
7061 | 0, 99,
|
---|
7062 | pDisk->pVDIfsDisk,
|
---|
7063 | pImage->pVDIfsImage,
|
---|
7064 | pVDIfsOperation,
|
---|
7065 | &pImage->pBackendData);
|
---|
7066 |
|
---|
7067 | if (RT_SUCCESS(rc))
|
---|
7068 | {
|
---|
7069 | pImage->VDIo.pBackendData = pImage->pBackendData;
|
---|
7070 | pImage->uImageFlags = uImageFlags;
|
---|
7071 |
|
---|
7072 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
7073 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7074 | AssertRC(rc2);
|
---|
7075 | fLockWrite = true;
|
---|
7076 |
|
---|
7077 | /* Switch previous image to read-only mode. */
|
---|
7078 | unsigned uOpenFlagsPrevImg;
|
---|
7079 | uOpenFlagsPrevImg = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pBackendData);
|
---|
7080 | if (!(uOpenFlagsPrevImg & VD_OPEN_FLAGS_READONLY))
|
---|
7081 | {
|
---|
7082 | uOpenFlagsPrevImg |= VD_OPEN_FLAGS_READONLY;
|
---|
7083 | rc = pDisk->pLast->Backend->pfnSetOpenFlags(pDisk->pLast->pBackendData, uOpenFlagsPrevImg);
|
---|
7084 | }
|
---|
7085 |
|
---|
7086 | /** @todo optionally check UUIDs */
|
---|
7087 |
|
---|
7088 | /* Re-check state, as the lock wasn't held and another image
|
---|
7089 | * creation call could have been done by another thread. */
|
---|
7090 | AssertMsgStmt(pDisk->cImages != 0,
|
---|
7091 | ("Create diff image cannot be done without other images open\n"),
|
---|
7092 | rc = VERR_VD_INVALID_STATE);
|
---|
7093 | }
|
---|
7094 |
|
---|
7095 | if (RT_SUCCESS(rc))
|
---|
7096 | {
|
---|
7097 | RTUUID Uuid;
|
---|
7098 | RTTIMESPEC ts;
|
---|
7099 |
|
---|
7100 | if (pParentUuid && !RTUuidIsNull(pParentUuid))
|
---|
7101 | {
|
---|
7102 | Uuid = *pParentUuid;
|
---|
7103 | pImage->Backend->pfnSetParentUuid(pImage->pBackendData, &Uuid);
|
---|
7104 | }
|
---|
7105 | else
|
---|
7106 | {
|
---|
7107 | rc2 = pDisk->pLast->Backend->pfnGetUuid(pDisk->pLast->pBackendData,
|
---|
7108 | &Uuid);
|
---|
7109 | if (RT_SUCCESS(rc2))
|
---|
7110 | pImage->Backend->pfnSetParentUuid(pImage->pBackendData, &Uuid);
|
---|
7111 | }
|
---|
7112 | rc2 = pDisk->pLast->Backend->pfnGetModificationUuid(pDisk->pLast->pBackendData,
|
---|
7113 | &Uuid);
|
---|
7114 | if (RT_SUCCESS(rc2))
|
---|
7115 | pImage->Backend->pfnSetParentModificationUuid(pImage->pBackendData,
|
---|
7116 | &Uuid);
|
---|
7117 | if (pDisk->pLast->Backend->pfnGetTimeStamp)
|
---|
7118 | rc2 = pDisk->pLast->Backend->pfnGetTimeStamp(pDisk->pLast->pBackendData,
|
---|
7119 | &ts);
|
---|
7120 | else
|
---|
7121 | rc2 = VERR_NOT_IMPLEMENTED;
|
---|
7122 | if (RT_SUCCESS(rc2) && pImage->Backend->pfnSetParentTimeStamp)
|
---|
7123 | pImage->Backend->pfnSetParentTimeStamp(pImage->pBackendData, &ts);
|
---|
7124 |
|
---|
7125 | if (pImage->Backend->pfnSetParentFilename)
|
---|
7126 | rc2 = pImage->Backend->pfnSetParentFilename(pImage->pBackendData, pDisk->pLast->pszFilename);
|
---|
7127 | }
|
---|
7128 |
|
---|
7129 | if (RT_SUCCESS(rc))
|
---|
7130 | {
|
---|
7131 | /* Image successfully opened, make it the last image. */
|
---|
7132 | vdAddImageToList(pDisk, pImage);
|
---|
7133 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
7134 | pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
|
---|
7135 | }
|
---|
7136 | else
|
---|
7137 | {
|
---|
7138 | /* Error detected, but image opened. Close and delete image. */
|
---|
7139 | rc2 = pImage->Backend->pfnClose(pImage->pBackendData, true);
|
---|
7140 | AssertRC(rc2);
|
---|
7141 | pImage->pBackendData = NULL;
|
---|
7142 | }
|
---|
7143 | } while (0);
|
---|
7144 |
|
---|
7145 | if (RT_UNLIKELY(fLockWrite))
|
---|
7146 | {
|
---|
7147 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7148 | AssertRC(rc2);
|
---|
7149 | }
|
---|
7150 | else if (RT_UNLIKELY(fLockRead))
|
---|
7151 | {
|
---|
7152 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7153 | AssertRC(rc2);
|
---|
7154 | }
|
---|
7155 |
|
---|
7156 | if (RT_FAILURE(rc))
|
---|
7157 | {
|
---|
7158 | if (pImage)
|
---|
7159 | {
|
---|
7160 | if (pImage->pszFilename)
|
---|
7161 | RTStrFree(pImage->pszFilename);
|
---|
7162 | RTMemFree(pImage);
|
---|
7163 | }
|
---|
7164 | }
|
---|
7165 |
|
---|
7166 | if (RT_SUCCESS(rc) && pIfProgress && pIfProgress->pfnProgress)
|
---|
7167 | pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
|
---|
7168 |
|
---|
7169 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
7170 | return rc;
|
---|
7171 | }
|
---|
7172 |
|
---|
7173 |
|
---|
7174 | /**
|
---|
7175 | * Creates and opens new cache image file in HDD container.
|
---|
7176 | *
|
---|
7177 | * @return VBox status code.
|
---|
7178 | * @param pDisk Name of the cache file backend to use (case insensitive).
|
---|
7179 | * @param pszFilename Name of the differencing cache file to create.
|
---|
7180 | * @param cbSize Maximum size of the cache.
|
---|
7181 | * @param uImageFlags Flags specifying special cache features.
|
---|
7182 | * @param pszComment Pointer to image comment. NULL is ok.
|
---|
7183 | * @param pUuid New UUID of the image. If NULL, a new UUID is created.
|
---|
7184 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
7185 | * @param pVDIfsCache Pointer to the per-cache VD interface list.
|
---|
7186 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
7187 | */
|
---|
7188 | VBOXDDU_DECL(int) VDCreateCache(PVBOXHDD pDisk, const char *pszBackend,
|
---|
7189 | const char *pszFilename, uint64_t cbSize,
|
---|
7190 | unsigned uImageFlags, const char *pszComment,
|
---|
7191 | PCRTUUID pUuid, unsigned uOpenFlags,
|
---|
7192 | PVDINTERFACE pVDIfsCache, PVDINTERFACE pVDIfsOperation)
|
---|
7193 | {
|
---|
7194 | int rc = VINF_SUCCESS;
|
---|
7195 | int rc2;
|
---|
7196 | bool fLockWrite = false, fLockRead = false;
|
---|
7197 | PVDCACHE pCache = NULL;
|
---|
7198 | RTUUID uuid;
|
---|
7199 |
|
---|
7200 | LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" Uuid=%RTuuid uOpenFlags=%#x pVDIfsImage=%#p pVDIfsOperation=%#p\n",
|
---|
7201 | pDisk, pszBackend, pszFilename, cbSize, uImageFlags, pszComment, pUuid, uOpenFlags, pVDIfsCache, pVDIfsOperation));
|
---|
7202 |
|
---|
7203 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
7204 |
|
---|
7205 | do
|
---|
7206 | {
|
---|
7207 | /* sanity check */
|
---|
7208 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7209 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7210 |
|
---|
7211 | /* Check arguments. */
|
---|
7212 | AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
|
---|
7213 | ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
|
---|
7214 | rc = VERR_INVALID_PARAMETER);
|
---|
7215 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
7216 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
7217 | rc = VERR_INVALID_PARAMETER);
|
---|
7218 | AssertMsgBreakStmt(cbSize,
|
---|
7219 | ("cbSize=%llu\n", cbSize),
|
---|
7220 | rc = VERR_INVALID_PARAMETER);
|
---|
7221 | AssertMsgBreakStmt((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0,
|
---|
7222 | ("uImageFlags=%#x\n", uImageFlags),
|
---|
7223 | rc = VERR_INVALID_PARAMETER);
|
---|
7224 | /* The UUID may be NULL. */
|
---|
7225 | AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
|
---|
7226 | ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
|
---|
7227 | rc = VERR_INVALID_PARAMETER);
|
---|
7228 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
7229 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
7230 | rc = VERR_INVALID_PARAMETER);
|
---|
7231 |
|
---|
7232 | /* Check state. Needs a temporary read lock. Holding the write lock
|
---|
7233 | * all the time would be blocking other activities for too long. */
|
---|
7234 | rc2 = vdThreadStartRead(pDisk);
|
---|
7235 | AssertRC(rc2);
|
---|
7236 | fLockRead = true;
|
---|
7237 | AssertMsgBreakStmt(!pDisk->pCache,
|
---|
7238 | ("Create cache image cannot be done with a cache already attached\n"),
|
---|
7239 | rc = VERR_VD_CACHE_ALREADY_EXISTS);
|
---|
7240 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7241 | AssertRC(rc2);
|
---|
7242 | fLockRead = false;
|
---|
7243 |
|
---|
7244 | /* Set up image descriptor. */
|
---|
7245 | pCache = (PVDCACHE)RTMemAllocZ(sizeof(VDCACHE));
|
---|
7246 | if (!pCache)
|
---|
7247 | {
|
---|
7248 | rc = VERR_NO_MEMORY;
|
---|
7249 | break;
|
---|
7250 | }
|
---|
7251 | pCache->pszFilename = RTStrDup(pszFilename);
|
---|
7252 | if (!pCache->pszFilename)
|
---|
7253 | {
|
---|
7254 | rc = VERR_NO_MEMORY;
|
---|
7255 | break;
|
---|
7256 | }
|
---|
7257 |
|
---|
7258 | rc = vdFindCacheBackend(pszBackend, &pCache->Backend);
|
---|
7259 | if (RT_FAILURE(rc))
|
---|
7260 | break;
|
---|
7261 | if (!pCache->Backend)
|
---|
7262 | {
|
---|
7263 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
7264 | N_("VD: unknown backend name '%s'"), pszBackend);
|
---|
7265 | break;
|
---|
7266 | }
|
---|
7267 |
|
---|
7268 | pCache->VDIo.pDisk = pDisk;
|
---|
7269 | pCache->pVDIfsCache = pVDIfsCache;
|
---|
7270 |
|
---|
7271 | /* Set up the I/O interface. */
|
---|
7272 | pCache->VDIo.pInterfaceIo = VDIfIoGet(pVDIfsCache);
|
---|
7273 | if (!pCache->VDIo.pInterfaceIo)
|
---|
7274 | {
|
---|
7275 | vdIfIoFallbackCallbacksSetup(&pCache->VDIo.VDIfIo);
|
---|
7276 | rc = VDInterfaceAdd(&pCache->VDIo.VDIfIo.Core, "VD_IO", VDINTERFACETYPE_IO,
|
---|
7277 | pDisk, sizeof(VDINTERFACEIO), &pVDIfsCache);
|
---|
7278 | pCache->VDIo.pInterfaceIo = &pCache->VDIo.VDIfIo;
|
---|
7279 | }
|
---|
7280 |
|
---|
7281 | /* Set up the internal I/O interface. */
|
---|
7282 | AssertBreakStmt(!VDIfIoIntGet(pVDIfsCache), rc = VERR_INVALID_PARAMETER);
|
---|
7283 | vdIfIoIntCallbacksSetup(&pCache->VDIo.VDIfIoInt);
|
---|
7284 | rc = VDInterfaceAdd(&pCache->VDIo.VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
7285 | &pCache->VDIo, sizeof(VDINTERFACEIOINT), &pCache->pVDIfsCache);
|
---|
7286 | AssertRC(rc);
|
---|
7287 |
|
---|
7288 | /* Create UUID if the caller didn't specify one. */
|
---|
7289 | if (!pUuid)
|
---|
7290 | {
|
---|
7291 | rc = RTUuidCreate(&uuid);
|
---|
7292 | if (RT_FAILURE(rc))
|
---|
7293 | {
|
---|
7294 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
7295 | N_("VD: cannot generate UUID for image '%s'"),
|
---|
7296 | pszFilename);
|
---|
7297 | break;
|
---|
7298 | }
|
---|
7299 | pUuid = &uuid;
|
---|
7300 | }
|
---|
7301 |
|
---|
7302 | pCache->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
|
---|
7303 | pCache->VDIo.fIgnoreFlush = (uOpenFlags & VD_OPEN_FLAGS_IGNORE_FLUSH) != 0;
|
---|
7304 | rc = pCache->Backend->pfnCreate(pCache->pszFilename, cbSize,
|
---|
7305 | uImageFlags,
|
---|
7306 | pszComment, pUuid,
|
---|
7307 | uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
|
---|
7308 | 0, 99,
|
---|
7309 | pDisk->pVDIfsDisk,
|
---|
7310 | pCache->pVDIfsCache,
|
---|
7311 | pVDIfsOperation,
|
---|
7312 | &pCache->pBackendData);
|
---|
7313 |
|
---|
7314 | if (RT_SUCCESS(rc))
|
---|
7315 | {
|
---|
7316 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
7317 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7318 | AssertRC(rc2);
|
---|
7319 | fLockWrite = true;
|
---|
7320 |
|
---|
7321 | pCache->VDIo.pBackendData = pCache->pBackendData;
|
---|
7322 |
|
---|
7323 | /* Re-check state, as the lock wasn't held and another image
|
---|
7324 | * creation call could have been done by another thread. */
|
---|
7325 | AssertMsgStmt(!pDisk->pCache,
|
---|
7326 | ("Create cache image cannot be done with another cache open\n"),
|
---|
7327 | rc = VERR_VD_CACHE_ALREADY_EXISTS);
|
---|
7328 | }
|
---|
7329 |
|
---|
7330 | if ( RT_SUCCESS(rc)
|
---|
7331 | && pDisk->pLast)
|
---|
7332 | {
|
---|
7333 | RTUUID UuidModification;
|
---|
7334 |
|
---|
7335 | /* Set same modification Uuid as the last image. */
|
---|
7336 | rc = pDisk->pLast->Backend->pfnGetModificationUuid(pDisk->pLast->pBackendData,
|
---|
7337 | &UuidModification);
|
---|
7338 | if (RT_SUCCESS(rc))
|
---|
7339 | {
|
---|
7340 | rc = pCache->Backend->pfnSetModificationUuid(pCache->pBackendData,
|
---|
7341 | &UuidModification);
|
---|
7342 | }
|
---|
7343 |
|
---|
7344 | if (rc == VERR_NOT_SUPPORTED)
|
---|
7345 | rc = VINF_SUCCESS;
|
---|
7346 | }
|
---|
7347 |
|
---|
7348 | if (RT_SUCCESS(rc))
|
---|
7349 | {
|
---|
7350 | /* Cache successfully created. */
|
---|
7351 | pDisk->pCache = pCache;
|
---|
7352 | }
|
---|
7353 | else
|
---|
7354 | {
|
---|
7355 | /* Error detected, but image opened. Close and delete image. */
|
---|
7356 | rc2 = pCache->Backend->pfnClose(pCache->pBackendData, true);
|
---|
7357 | AssertRC(rc2);
|
---|
7358 | pCache->pBackendData = NULL;
|
---|
7359 | }
|
---|
7360 | } while (0);
|
---|
7361 |
|
---|
7362 | if (RT_UNLIKELY(fLockWrite))
|
---|
7363 | {
|
---|
7364 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7365 | AssertRC(rc2);
|
---|
7366 | }
|
---|
7367 | else if (RT_UNLIKELY(fLockRead))
|
---|
7368 | {
|
---|
7369 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7370 | AssertRC(rc2);
|
---|
7371 | }
|
---|
7372 |
|
---|
7373 | if (RT_FAILURE(rc))
|
---|
7374 | {
|
---|
7375 | if (pCache)
|
---|
7376 | {
|
---|
7377 | if (pCache->pszFilename)
|
---|
7378 | RTStrFree(pCache->pszFilename);
|
---|
7379 | RTMemFree(pCache);
|
---|
7380 | }
|
---|
7381 | }
|
---|
7382 |
|
---|
7383 | if (RT_SUCCESS(rc) && pIfProgress && pIfProgress->pfnProgress)
|
---|
7384 | pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
|
---|
7385 |
|
---|
7386 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
7387 | return rc;
|
---|
7388 | }
|
---|
7389 |
|
---|
7390 | /**
|
---|
7391 | * Merges two images (not necessarily with direct parent/child relationship).
|
---|
7392 | * As a side effect the source image and potentially the other images which
|
---|
7393 | * are also merged to the destination are deleted from both the disk and the
|
---|
7394 | * images in the HDD container.
|
---|
7395 | *
|
---|
7396 | * @returns VBox status code.
|
---|
7397 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
7398 | * @param pDisk Pointer to HDD container.
|
---|
7399 | * @param nImageFrom Name of the image file to merge from.
|
---|
7400 | * @param nImageTo Name of the image file to merge to.
|
---|
7401 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
7402 | */
|
---|
7403 | VBOXDDU_DECL(int) VDMerge(PVBOXHDD pDisk, unsigned nImageFrom,
|
---|
7404 | unsigned nImageTo, PVDINTERFACE pVDIfsOperation)
|
---|
7405 | {
|
---|
7406 | int rc = VINF_SUCCESS;
|
---|
7407 | int rc2;
|
---|
7408 | bool fLockWrite = false, fLockRead = false;
|
---|
7409 | void *pvBuf = NULL;
|
---|
7410 |
|
---|
7411 | LogFlowFunc(("pDisk=%#p nImageFrom=%u nImageTo=%u pVDIfsOperation=%#p\n",
|
---|
7412 | pDisk, nImageFrom, nImageTo, pVDIfsOperation));
|
---|
7413 |
|
---|
7414 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
7415 |
|
---|
7416 | do
|
---|
7417 | {
|
---|
7418 | /* sanity check */
|
---|
7419 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7420 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7421 |
|
---|
7422 | /* For simplicity reasons lock for writing as the image reopen below
|
---|
7423 | * might need it. After all the reopen is usually needed. */
|
---|
7424 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7425 | AssertRC(rc2);
|
---|
7426 | fLockWrite = true;
|
---|
7427 | PVDIMAGE pImageFrom = vdGetImageByNumber(pDisk, nImageFrom);
|
---|
7428 | PVDIMAGE pImageTo = vdGetImageByNumber(pDisk, nImageTo);
|
---|
7429 | if (!pImageFrom || !pImageTo)
|
---|
7430 | {
|
---|
7431 | rc = VERR_VD_IMAGE_NOT_FOUND;
|
---|
7432 | break;
|
---|
7433 | }
|
---|
7434 | AssertBreakStmt(pImageFrom != pImageTo, rc = VERR_INVALID_PARAMETER);
|
---|
7435 |
|
---|
7436 | /* Make sure destination image is writable. */
|
---|
7437 | unsigned uOpenFlags = pImageTo->Backend->pfnGetOpenFlags(pImageTo->pBackendData);
|
---|
7438 | if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
7439 | {
|
---|
7440 | /*
|
---|
7441 | * Clear skip consistency checks because the image is made writable now and
|
---|
7442 | * skipping consistency checks is only possible for readonly images.
|
---|
7443 | */
|
---|
7444 | uOpenFlags &= ~(VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS);
|
---|
7445 | rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pBackendData,
|
---|
7446 | uOpenFlags);
|
---|
7447 | if (RT_FAILURE(rc))
|
---|
7448 | break;
|
---|
7449 | }
|
---|
7450 |
|
---|
7451 | /* Get size of destination image. */
|
---|
7452 | uint64_t cbSize = pImageTo->Backend->pfnGetSize(pImageTo->pBackendData);
|
---|
7453 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7454 | AssertRC(rc2);
|
---|
7455 | fLockWrite = false;
|
---|
7456 |
|
---|
7457 | /* Allocate tmp buffer. */
|
---|
7458 | pvBuf = RTMemTmpAlloc(VD_MERGE_BUFFER_SIZE);
|
---|
7459 | if (!pvBuf)
|
---|
7460 | {
|
---|
7461 | rc = VERR_NO_MEMORY;
|
---|
7462 | break;
|
---|
7463 | }
|
---|
7464 |
|
---|
7465 | /* Merging is done directly on the images itself. This potentially
|
---|
7466 | * causes trouble if the disk is full in the middle of operation. */
|
---|
7467 | if (nImageFrom < nImageTo)
|
---|
7468 | {
|
---|
7469 | /* Merge parent state into child. This means writing all not
|
---|
7470 | * allocated blocks in the destination image which are allocated in
|
---|
7471 | * the images to be merged. */
|
---|
7472 | uint64_t uOffset = 0;
|
---|
7473 | uint64_t cbRemaining = cbSize;
|
---|
7474 | do
|
---|
7475 | {
|
---|
7476 | size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
|
---|
7477 | RTSGSEG SegmentBuf;
|
---|
7478 | RTSGBUF SgBuf;
|
---|
7479 | VDIOCTX IoCtx;
|
---|
7480 |
|
---|
7481 | SegmentBuf.pvSeg = pvBuf;
|
---|
7482 | SegmentBuf.cbSeg = VD_MERGE_BUFFER_SIZE;
|
---|
7483 | RTSgBufInit(&SgBuf, &SegmentBuf, 1);
|
---|
7484 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_READ, 0, 0, NULL,
|
---|
7485 | &SgBuf, NULL, NULL, VDIOCTX_FLAGS_SYNC);
|
---|
7486 |
|
---|
7487 | /* Need to hold the write lock during a read-write operation. */
|
---|
7488 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7489 | AssertRC(rc2);
|
---|
7490 | fLockWrite = true;
|
---|
7491 |
|
---|
7492 | rc = pImageTo->Backend->pfnRead(pImageTo->pBackendData,
|
---|
7493 | uOffset, cbThisRead,
|
---|
7494 | &IoCtx, &cbThisRead);
|
---|
7495 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
7496 | {
|
---|
7497 | /* Search for image with allocated block. Do not attempt to
|
---|
7498 | * read more than the previous reads marked as valid.
|
---|
7499 | * Otherwise this would return stale data when different
|
---|
7500 | * block sizes are used for the images. */
|
---|
7501 | for (PVDIMAGE pCurrImage = pImageTo->pPrev;
|
---|
7502 | pCurrImage != NULL && pCurrImage != pImageFrom->pPrev && rc == VERR_VD_BLOCK_FREE;
|
---|
7503 | pCurrImage = pCurrImage->pPrev)
|
---|
7504 | {
|
---|
7505 | rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
|
---|
7506 | uOffset, cbThisRead,
|
---|
7507 | &IoCtx, &cbThisRead);
|
---|
7508 | }
|
---|
7509 |
|
---|
7510 | if (rc != VERR_VD_BLOCK_FREE)
|
---|
7511 | {
|
---|
7512 | if (RT_FAILURE(rc))
|
---|
7513 | break;
|
---|
7514 | /* Updating the cache is required because this might be a live merge. */
|
---|
7515 | rc = vdWriteHelperEx(pDisk, pImageTo, pImageFrom->pPrev,
|
---|
7516 | uOffset, pvBuf, cbThisRead,
|
---|
7517 | VDIOCTX_FLAGS_READ_UPDATE_CACHE, 0);
|
---|
7518 | if (RT_FAILURE(rc))
|
---|
7519 | break;
|
---|
7520 | }
|
---|
7521 | else
|
---|
7522 | rc = VINF_SUCCESS;
|
---|
7523 | }
|
---|
7524 | else if (RT_FAILURE(rc))
|
---|
7525 | break;
|
---|
7526 |
|
---|
7527 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7528 | AssertRC(rc2);
|
---|
7529 | fLockWrite = false;
|
---|
7530 |
|
---|
7531 | uOffset += cbThisRead;
|
---|
7532 | cbRemaining -= cbThisRead;
|
---|
7533 |
|
---|
7534 | if (pIfProgress && pIfProgress->pfnProgress)
|
---|
7535 | {
|
---|
7536 | /** @todo r=klaus: this can update the progress to the same
|
---|
7537 | * percentage over and over again if the image format makes
|
---|
7538 | * relatively small increments. */
|
---|
7539 | rc = pIfProgress->pfnProgress(pIfProgress->Core.pvUser,
|
---|
7540 | uOffset * 99 / cbSize);
|
---|
7541 | if (RT_FAILURE(rc))
|
---|
7542 | break;
|
---|
7543 | }
|
---|
7544 | } while (uOffset < cbSize);
|
---|
7545 | }
|
---|
7546 | else
|
---|
7547 | {
|
---|
7548 | /*
|
---|
7549 | * We may need to update the parent uuid of the child coming after
|
---|
7550 | * the last image to be merged. We have to reopen it read/write.
|
---|
7551 | *
|
---|
7552 | * This is done before we do the actual merge to prevent an
|
---|
7553 | * inconsistent chain if the mode change fails for some reason.
|
---|
7554 | */
|
---|
7555 | if (pImageFrom->pNext)
|
---|
7556 | {
|
---|
7557 | PVDIMAGE pImageChild = pImageFrom->pNext;
|
---|
7558 |
|
---|
7559 | /* Take the write lock. */
|
---|
7560 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7561 | AssertRC(rc2);
|
---|
7562 | fLockWrite = true;
|
---|
7563 |
|
---|
7564 | /* We need to open the image in read/write mode. */
|
---|
7565 | uOpenFlags = pImageChild->Backend->pfnGetOpenFlags(pImageChild->pBackendData);
|
---|
7566 |
|
---|
7567 | if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
7568 | {
|
---|
7569 | uOpenFlags &= ~VD_OPEN_FLAGS_READONLY;
|
---|
7570 | rc = pImageChild->Backend->pfnSetOpenFlags(pImageChild->pBackendData,
|
---|
7571 | uOpenFlags);
|
---|
7572 | if (RT_FAILURE(rc))
|
---|
7573 | break;
|
---|
7574 | }
|
---|
7575 |
|
---|
7576 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7577 | AssertRC(rc2);
|
---|
7578 | fLockWrite = false;
|
---|
7579 | }
|
---|
7580 |
|
---|
7581 | /* If the merge is from the last image we have to relay all writes
|
---|
7582 | * to the merge destination as well, so that concurrent writes
|
---|
7583 | * (in case of a live merge) are handled correctly. */
|
---|
7584 | if (!pImageFrom->pNext)
|
---|
7585 | {
|
---|
7586 | /* Take the write lock. */
|
---|
7587 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7588 | AssertRC(rc2);
|
---|
7589 | fLockWrite = true;
|
---|
7590 |
|
---|
7591 | pDisk->pImageRelay = pImageTo;
|
---|
7592 |
|
---|
7593 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7594 | AssertRC(rc2);
|
---|
7595 | fLockWrite = false;
|
---|
7596 | }
|
---|
7597 |
|
---|
7598 | /* Merge child state into parent. This means writing all blocks
|
---|
7599 | * which are allocated in the image up to the source image to the
|
---|
7600 | * destination image. */
|
---|
7601 | uint64_t uOffset = 0;
|
---|
7602 | uint64_t cbRemaining = cbSize;
|
---|
7603 | do
|
---|
7604 | {
|
---|
7605 | size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
|
---|
7606 | RTSGSEG SegmentBuf;
|
---|
7607 | RTSGBUF SgBuf;
|
---|
7608 | VDIOCTX IoCtx;
|
---|
7609 |
|
---|
7610 | rc = VERR_VD_BLOCK_FREE;
|
---|
7611 |
|
---|
7612 | SegmentBuf.pvSeg = pvBuf;
|
---|
7613 | SegmentBuf.cbSeg = VD_MERGE_BUFFER_SIZE;
|
---|
7614 | RTSgBufInit(&SgBuf, &SegmentBuf, 1);
|
---|
7615 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_READ, 0, 0, NULL,
|
---|
7616 | &SgBuf, NULL, NULL, VDIOCTX_FLAGS_SYNC);
|
---|
7617 |
|
---|
7618 | /* Need to hold the write lock during a read-write operation. */
|
---|
7619 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7620 | AssertRC(rc2);
|
---|
7621 | fLockWrite = true;
|
---|
7622 |
|
---|
7623 | /* Search for image with allocated block. Do not attempt to
|
---|
7624 | * read more than the previous reads marked as valid. Otherwise
|
---|
7625 | * this would return stale data when different block sizes are
|
---|
7626 | * used for the images. */
|
---|
7627 | for (PVDIMAGE pCurrImage = pImageFrom;
|
---|
7628 | pCurrImage != NULL && pCurrImage != pImageTo && rc == VERR_VD_BLOCK_FREE;
|
---|
7629 | pCurrImage = pCurrImage->pPrev)
|
---|
7630 | {
|
---|
7631 | rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
|
---|
7632 | uOffset, cbThisRead,
|
---|
7633 | &IoCtx, &cbThisRead);
|
---|
7634 | }
|
---|
7635 |
|
---|
7636 | if (rc != VERR_VD_BLOCK_FREE)
|
---|
7637 | {
|
---|
7638 | if (RT_FAILURE(rc))
|
---|
7639 | break;
|
---|
7640 | rc = vdWriteHelper(pDisk, pImageTo, uOffset, pvBuf,
|
---|
7641 | cbThisRead, VDIOCTX_FLAGS_READ_UPDATE_CACHE);
|
---|
7642 | if (RT_FAILURE(rc))
|
---|
7643 | break;
|
---|
7644 | }
|
---|
7645 | else
|
---|
7646 | rc = VINF_SUCCESS;
|
---|
7647 |
|
---|
7648 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7649 | AssertRC(rc2);
|
---|
7650 | fLockWrite = false;
|
---|
7651 |
|
---|
7652 | uOffset += cbThisRead;
|
---|
7653 | cbRemaining -= cbThisRead;
|
---|
7654 |
|
---|
7655 | if (pIfProgress && pIfProgress->pfnProgress)
|
---|
7656 | {
|
---|
7657 | /** @todo r=klaus: this can update the progress to the same
|
---|
7658 | * percentage over and over again if the image format makes
|
---|
7659 | * relatively small increments. */
|
---|
7660 | rc = pIfProgress->pfnProgress(pIfProgress->Core.pvUser,
|
---|
7661 | uOffset * 99 / cbSize);
|
---|
7662 | if (RT_FAILURE(rc))
|
---|
7663 | break;
|
---|
7664 | }
|
---|
7665 | } while (uOffset < cbSize);
|
---|
7666 |
|
---|
7667 | /* In case we set up a "write proxy" image above we must clear
|
---|
7668 | * this again now to prevent stray writes. Failure or not. */
|
---|
7669 | if (!pImageFrom->pNext)
|
---|
7670 | {
|
---|
7671 | /* Take the write lock. */
|
---|
7672 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7673 | AssertRC(rc2);
|
---|
7674 | fLockWrite = true;
|
---|
7675 |
|
---|
7676 | pDisk->pImageRelay = NULL;
|
---|
7677 |
|
---|
7678 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7679 | AssertRC(rc2);
|
---|
7680 | fLockWrite = false;
|
---|
7681 | }
|
---|
7682 | }
|
---|
7683 |
|
---|
7684 | /*
|
---|
7685 | * Leave in case of an error to avoid corrupted data in the image chain
|
---|
7686 | * (includes cancelling the operation by the user).
|
---|
7687 | */
|
---|
7688 | if (RT_FAILURE(rc))
|
---|
7689 | break;
|
---|
7690 |
|
---|
7691 | /* Need to hold the write lock while finishing the merge. */
|
---|
7692 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7693 | AssertRC(rc2);
|
---|
7694 | fLockWrite = true;
|
---|
7695 |
|
---|
7696 | /* Update parent UUID so that image chain is consistent.
|
---|
7697 | * The two attempts work around the problem that some backends
|
---|
7698 | * (e.g. iSCSI) do not support UUIDs, so we exploit the fact that
|
---|
7699 | * so far there can only be one such image in the chain. */
|
---|
7700 | /** @todo needs a better long-term solution, passing the UUID
|
---|
7701 | * knowledge from the caller or some such */
|
---|
7702 | RTUUID Uuid;
|
---|
7703 | PVDIMAGE pImageChild = NULL;
|
---|
7704 | if (nImageFrom < nImageTo)
|
---|
7705 | {
|
---|
7706 | if (pImageFrom->pPrev)
|
---|
7707 | {
|
---|
7708 | /* plan A: ask the parent itself for its UUID */
|
---|
7709 | rc = pImageFrom->pPrev->Backend->pfnGetUuid(pImageFrom->pPrev->pBackendData,
|
---|
7710 | &Uuid);
|
---|
7711 | if (RT_FAILURE(rc))
|
---|
7712 | {
|
---|
7713 | /* plan B: ask the child of the parent for parent UUID */
|
---|
7714 | rc = pImageFrom->Backend->pfnGetParentUuid(pImageFrom->pBackendData,
|
---|
7715 | &Uuid);
|
---|
7716 | }
|
---|
7717 | AssertRC(rc);
|
---|
7718 | }
|
---|
7719 | else
|
---|
7720 | RTUuidClear(&Uuid);
|
---|
7721 | rc = pImageTo->Backend->pfnSetParentUuid(pImageTo->pBackendData,
|
---|
7722 | &Uuid);
|
---|
7723 | AssertRC(rc);
|
---|
7724 | }
|
---|
7725 | else
|
---|
7726 | {
|
---|
7727 | /* Update the parent uuid of the child of the last merged image. */
|
---|
7728 | if (pImageFrom->pNext)
|
---|
7729 | {
|
---|
7730 | /* plan A: ask the parent itself for its UUID */
|
---|
7731 | rc = pImageTo->Backend->pfnGetUuid(pImageTo->pBackendData,
|
---|
7732 | &Uuid);
|
---|
7733 | if (RT_FAILURE(rc))
|
---|
7734 | {
|
---|
7735 | /* plan B: ask the child of the parent for parent UUID */
|
---|
7736 | rc = pImageTo->pNext->Backend->pfnGetParentUuid(pImageTo->pNext->pBackendData,
|
---|
7737 | &Uuid);
|
---|
7738 | }
|
---|
7739 | AssertRC(rc);
|
---|
7740 |
|
---|
7741 | rc = pImageFrom->Backend->pfnSetParentUuid(pImageFrom->pNext->pBackendData,
|
---|
7742 | &Uuid);
|
---|
7743 | AssertRC(rc);
|
---|
7744 |
|
---|
7745 | pImageChild = pImageFrom->pNext;
|
---|
7746 | }
|
---|
7747 | }
|
---|
7748 |
|
---|
7749 | /* Delete the no longer needed images. */
|
---|
7750 | PVDIMAGE pImg = pImageFrom, pTmp;
|
---|
7751 | while (pImg != pImageTo)
|
---|
7752 | {
|
---|
7753 | if (nImageFrom < nImageTo)
|
---|
7754 | pTmp = pImg->pNext;
|
---|
7755 | else
|
---|
7756 | pTmp = pImg->pPrev;
|
---|
7757 | vdRemoveImageFromList(pDisk, pImg);
|
---|
7758 | pImg->Backend->pfnClose(pImg->pBackendData, true);
|
---|
7759 | RTMemFree(pImg->pszFilename);
|
---|
7760 | RTMemFree(pImg);
|
---|
7761 | pImg = pTmp;
|
---|
7762 | }
|
---|
7763 |
|
---|
7764 | /* Make sure destination image is back to read only if necessary. */
|
---|
7765 | if (pImageTo != pDisk->pLast)
|
---|
7766 | {
|
---|
7767 | uOpenFlags = pImageTo->Backend->pfnGetOpenFlags(pImageTo->pBackendData);
|
---|
7768 | uOpenFlags |= VD_OPEN_FLAGS_READONLY;
|
---|
7769 | rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pBackendData,
|
---|
7770 | uOpenFlags);
|
---|
7771 | if (RT_FAILURE(rc))
|
---|
7772 | break;
|
---|
7773 | }
|
---|
7774 |
|
---|
7775 | /*
|
---|
7776 | * Make sure the child is readonly
|
---|
7777 | * for the child -> parent merge direction
|
---|
7778 | * if necessary.
|
---|
7779 | */
|
---|
7780 | if ( nImageFrom > nImageTo
|
---|
7781 | && pImageChild
|
---|
7782 | && pImageChild != pDisk->pLast)
|
---|
7783 | {
|
---|
7784 | uOpenFlags = pImageChild->Backend->pfnGetOpenFlags(pImageChild->pBackendData);
|
---|
7785 | uOpenFlags |= VD_OPEN_FLAGS_READONLY;
|
---|
7786 | rc = pImageChild->Backend->pfnSetOpenFlags(pImageChild->pBackendData,
|
---|
7787 | uOpenFlags);
|
---|
7788 | if (RT_FAILURE(rc))
|
---|
7789 | break;
|
---|
7790 | }
|
---|
7791 | } while (0);
|
---|
7792 |
|
---|
7793 | if (RT_UNLIKELY(fLockWrite))
|
---|
7794 | {
|
---|
7795 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7796 | AssertRC(rc2);
|
---|
7797 | }
|
---|
7798 | else if (RT_UNLIKELY(fLockRead))
|
---|
7799 | {
|
---|
7800 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7801 | AssertRC(rc2);
|
---|
7802 | }
|
---|
7803 |
|
---|
7804 | if (pvBuf)
|
---|
7805 | RTMemTmpFree(pvBuf);
|
---|
7806 |
|
---|
7807 | if (RT_SUCCESS(rc) && pIfProgress && pIfProgress->pfnProgress)
|
---|
7808 | pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
|
---|
7809 |
|
---|
7810 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
7811 | return rc;
|
---|
7812 | }
|
---|
7813 |
|
---|
7814 | /**
|
---|
7815 | * Copies an image from one HDD container to another - extended version.
|
---|
7816 | * The copy is opened in the target HDD container.
|
---|
7817 | * It is possible to convert between different image formats, because the
|
---|
7818 | * backend for the destination may be different from the source.
|
---|
7819 | * If both the source and destination reference the same HDD container,
|
---|
7820 | * then the image is moved (by copying/deleting or renaming) to the new location.
|
---|
7821 | * The source container is unchanged if the move operation fails, otherwise
|
---|
7822 | * the image at the new location is opened in the same way as the old one was.
|
---|
7823 | *
|
---|
7824 | * @note The read/write accesses across disks are not synchronized, just the
|
---|
7825 | * accesses to each disk. Once there is a use case which requires a defined
|
---|
7826 | * read/write behavior in this situation this needs to be extended.
|
---|
7827 | *
|
---|
7828 | * @return VBox status code.
|
---|
7829 | * @return VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
7830 | * @param pDiskFrom Pointer to source HDD container.
|
---|
7831 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
7832 | * @param pDiskTo Pointer to destination HDD container.
|
---|
7833 | * @param pszBackend Name of the image file backend to use (may be NULL to use the same as the source, case insensitive).
|
---|
7834 | * @param pszFilename New name of the image (may be NULL to specify that the
|
---|
7835 | * copy destination is the destination container, or
|
---|
7836 | * if pDiskFrom == pDiskTo, i.e. when moving).
|
---|
7837 | * @param fMoveByRename If true, attempt to perform a move by renaming (if successful the new size is ignored).
|
---|
7838 | * @param cbSize New image size (0 means leave unchanged).
|
---|
7839 | * @param nImageSameFrom todo
|
---|
7840 | * @param nImageSameTo todo
|
---|
7841 | * @param uImageFlags Flags specifying special destination image features.
|
---|
7842 | * @param pDstUuid New UUID of the destination image. If NULL, a new UUID is created.
|
---|
7843 | * This parameter is used if and only if a true copy is created.
|
---|
7844 | * In all rename/move cases or copy to existing image cases the modification UUIDs are copied over.
|
---|
7845 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
7846 | * Only used if the destination image is created.
|
---|
7847 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
7848 | * @param pDstVDIfsImage Pointer to the per-image VD interface list, for the
|
---|
7849 | * destination image.
|
---|
7850 | * @param pDstVDIfsOperation Pointer to the per-operation VD interface list,
|
---|
7851 | * for the destination operation.
|
---|
7852 | */
|
---|
7853 | VBOXDDU_DECL(int) VDCopyEx(PVBOXHDD pDiskFrom, unsigned nImage, PVBOXHDD pDiskTo,
|
---|
7854 | const char *pszBackend, const char *pszFilename,
|
---|
7855 | bool fMoveByRename, uint64_t cbSize,
|
---|
7856 | unsigned nImageFromSame, unsigned nImageToSame,
|
---|
7857 | unsigned uImageFlags, PCRTUUID pDstUuid,
|
---|
7858 | unsigned uOpenFlags, PVDINTERFACE pVDIfsOperation,
|
---|
7859 | PVDINTERFACE pDstVDIfsImage,
|
---|
7860 | PVDINTERFACE pDstVDIfsOperation)
|
---|
7861 | {
|
---|
7862 | int rc = VINF_SUCCESS;
|
---|
7863 | int rc2;
|
---|
7864 | bool fLockReadFrom = false, fLockWriteFrom = false, fLockWriteTo = false;
|
---|
7865 | PVDIMAGE pImageTo = NULL;
|
---|
7866 |
|
---|
7867 | 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",
|
---|
7868 | pDiskFrom, nImage, pDiskTo, pszBackend, pszFilename, fMoveByRename, cbSize, nImageFromSame, nImageToSame, uImageFlags, pDstUuid, uOpenFlags, pVDIfsOperation, pDstVDIfsImage, pDstVDIfsOperation));
|
---|
7869 |
|
---|
7870 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
7871 | PVDINTERFACEPROGRESS pDstIfProgress = VDIfProgressGet(pDstVDIfsOperation);
|
---|
7872 |
|
---|
7873 | do {
|
---|
7874 | /* Check arguments. */
|
---|
7875 | AssertMsgBreakStmt(VALID_PTR(pDiskFrom), ("pDiskFrom=%#p\n", pDiskFrom),
|
---|
7876 | rc = VERR_INVALID_PARAMETER);
|
---|
7877 | AssertMsg(pDiskFrom->u32Signature == VBOXHDDDISK_SIGNATURE,
|
---|
7878 | ("u32Signature=%08x\n", pDiskFrom->u32Signature));
|
---|
7879 |
|
---|
7880 | rc2 = vdThreadStartRead(pDiskFrom);
|
---|
7881 | AssertRC(rc2);
|
---|
7882 | fLockReadFrom = true;
|
---|
7883 | PVDIMAGE pImageFrom = vdGetImageByNumber(pDiskFrom, nImage);
|
---|
7884 | AssertPtrBreakStmt(pImageFrom, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
7885 | AssertMsgBreakStmt(VALID_PTR(pDiskTo), ("pDiskTo=%#p\n", pDiskTo),
|
---|
7886 | rc = VERR_INVALID_PARAMETER);
|
---|
7887 | AssertMsg(pDiskTo->u32Signature == VBOXHDDDISK_SIGNATURE,
|
---|
7888 | ("u32Signature=%08x\n", pDiskTo->u32Signature));
|
---|
7889 | AssertMsgBreakStmt( (nImageFromSame < nImage || nImageFromSame == VD_IMAGE_CONTENT_UNKNOWN)
|
---|
7890 | && (nImageToSame < pDiskTo->cImages || nImageToSame == VD_IMAGE_CONTENT_UNKNOWN)
|
---|
7891 | && ( (nImageFromSame == VD_IMAGE_CONTENT_UNKNOWN && nImageToSame == VD_IMAGE_CONTENT_UNKNOWN)
|
---|
7892 | || (nImageFromSame != VD_IMAGE_CONTENT_UNKNOWN && nImageToSame != VD_IMAGE_CONTENT_UNKNOWN)),
|
---|
7893 | ("nImageFromSame=%u nImageToSame=%u\n", nImageFromSame, nImageToSame),
|
---|
7894 | rc = VERR_INVALID_PARAMETER);
|
---|
7895 |
|
---|
7896 | /* Move the image. */
|
---|
7897 | if (pDiskFrom == pDiskTo)
|
---|
7898 | {
|
---|
7899 | /* Rename only works when backends are the same, are file based
|
---|
7900 | * and the rename method is implemented. */
|
---|
7901 | if ( fMoveByRename
|
---|
7902 | && !RTStrICmp(pszBackend, pImageFrom->Backend->pszBackendName)
|
---|
7903 | && pImageFrom->Backend->uBackendCaps & VD_CAP_FILE
|
---|
7904 | && pImageFrom->Backend->pfnRename)
|
---|
7905 | {
|
---|
7906 | rc2 = vdThreadFinishRead(pDiskFrom);
|
---|
7907 | AssertRC(rc2);
|
---|
7908 | fLockReadFrom = false;
|
---|
7909 |
|
---|
7910 | rc2 = vdThreadStartWrite(pDiskFrom);
|
---|
7911 | AssertRC(rc2);
|
---|
7912 | fLockWriteFrom = true;
|
---|
7913 | rc = pImageFrom->Backend->pfnRename(pImageFrom->pBackendData, pszFilename ? pszFilename : pImageFrom->pszFilename);
|
---|
7914 | break;
|
---|
7915 | }
|
---|
7916 |
|
---|
7917 | /** @todo Moving (including shrinking/growing) of the image is
|
---|
7918 | * requested, but the rename attempt failed or it wasn't possible.
|
---|
7919 | * Must now copy image to temp location. */
|
---|
7920 | AssertReleaseMsgFailed(("VDCopy: moving by copy/delete not implemented\n"));
|
---|
7921 | }
|
---|
7922 |
|
---|
7923 | /* pszFilename is allowed to be NULL, as this indicates copy to the existing image. */
|
---|
7924 | AssertMsgBreakStmt(pszFilename == NULL || (VALID_PTR(pszFilename) && *pszFilename),
|
---|
7925 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
7926 | rc = VERR_INVALID_PARAMETER);
|
---|
7927 |
|
---|
7928 | uint64_t cbSizeFrom;
|
---|
7929 | cbSizeFrom = pImageFrom->Backend->pfnGetSize(pImageFrom->pBackendData);
|
---|
7930 | if (cbSizeFrom == 0)
|
---|
7931 | {
|
---|
7932 | rc = VERR_VD_VALUE_NOT_FOUND;
|
---|
7933 | break;
|
---|
7934 | }
|
---|
7935 |
|
---|
7936 | VDGEOMETRY PCHSGeometryFrom = {0, 0, 0};
|
---|
7937 | VDGEOMETRY LCHSGeometryFrom = {0, 0, 0};
|
---|
7938 | pImageFrom->Backend->pfnGetPCHSGeometry(pImageFrom->pBackendData, &PCHSGeometryFrom);
|
---|
7939 | pImageFrom->Backend->pfnGetLCHSGeometry(pImageFrom->pBackendData, &LCHSGeometryFrom);
|
---|
7940 |
|
---|
7941 | RTUUID ImageUuid, ImageModificationUuid;
|
---|
7942 | if (pDiskFrom != pDiskTo)
|
---|
7943 | {
|
---|
7944 | if (pDstUuid)
|
---|
7945 | ImageUuid = *pDstUuid;
|
---|
7946 | else
|
---|
7947 | RTUuidCreate(&ImageUuid);
|
---|
7948 | }
|
---|
7949 | else
|
---|
7950 | {
|
---|
7951 | rc = pImageFrom->Backend->pfnGetUuid(pImageFrom->pBackendData, &ImageUuid);
|
---|
7952 | if (RT_FAILURE(rc))
|
---|
7953 | RTUuidCreate(&ImageUuid);
|
---|
7954 | }
|
---|
7955 | rc = pImageFrom->Backend->pfnGetModificationUuid(pImageFrom->pBackendData, &ImageModificationUuid);
|
---|
7956 | if (RT_FAILURE(rc))
|
---|
7957 | RTUuidClear(&ImageModificationUuid);
|
---|
7958 |
|
---|
7959 | char szComment[1024];
|
---|
7960 | rc = pImageFrom->Backend->pfnGetComment(pImageFrom->pBackendData, szComment, sizeof(szComment));
|
---|
7961 | if (RT_FAILURE(rc))
|
---|
7962 | szComment[0] = '\0';
|
---|
7963 | else
|
---|
7964 | szComment[sizeof(szComment) - 1] = '\0';
|
---|
7965 |
|
---|
7966 | rc2 = vdThreadFinishRead(pDiskFrom);
|
---|
7967 | AssertRC(rc2);
|
---|
7968 | fLockReadFrom = false;
|
---|
7969 |
|
---|
7970 | rc2 = vdThreadStartRead(pDiskTo);
|
---|
7971 | AssertRC(rc2);
|
---|
7972 | unsigned cImagesTo = pDiskTo->cImages;
|
---|
7973 | rc2 = vdThreadFinishRead(pDiskTo);
|
---|
7974 | AssertRC(rc2);
|
---|
7975 |
|
---|
7976 | if (pszFilename)
|
---|
7977 | {
|
---|
7978 | if (cbSize == 0)
|
---|
7979 | cbSize = cbSizeFrom;
|
---|
7980 |
|
---|
7981 | /* Create destination image with the properties of source image. */
|
---|
7982 | /** @todo replace the VDCreateDiff/VDCreateBase calls by direct
|
---|
7983 | * calls to the backend. Unifies the code and reduces the API
|
---|
7984 | * dependencies. Would also make the synchronization explicit. */
|
---|
7985 | if (cImagesTo > 0)
|
---|
7986 | {
|
---|
7987 | rc = VDCreateDiff(pDiskTo, pszBackend, pszFilename,
|
---|
7988 | uImageFlags, szComment, &ImageUuid,
|
---|
7989 | NULL /* pParentUuid */,
|
---|
7990 | uOpenFlags & ~VD_OPEN_FLAGS_READONLY,
|
---|
7991 | pDstVDIfsImage, NULL);
|
---|
7992 |
|
---|
7993 | rc2 = vdThreadStartWrite(pDiskTo);
|
---|
7994 | AssertRC(rc2);
|
---|
7995 | fLockWriteTo = true;
|
---|
7996 | } else {
|
---|
7997 | /** @todo hack to force creation of a fixed image for
|
---|
7998 | * the RAW backend, which can't handle anything else. */
|
---|
7999 | if (!RTStrICmp(pszBackend, "RAW"))
|
---|
8000 | uImageFlags |= VD_IMAGE_FLAGS_FIXED;
|
---|
8001 |
|
---|
8002 | vdFixupPCHSGeometry(&PCHSGeometryFrom, cbSize);
|
---|
8003 | vdFixupLCHSGeometry(&LCHSGeometryFrom, cbSize);
|
---|
8004 |
|
---|
8005 | rc = VDCreateBase(pDiskTo, pszBackend, pszFilename, cbSize,
|
---|
8006 | uImageFlags, szComment,
|
---|
8007 | &PCHSGeometryFrom, &LCHSGeometryFrom,
|
---|
8008 | NULL, uOpenFlags & ~VD_OPEN_FLAGS_READONLY,
|
---|
8009 | pDstVDIfsImage, NULL);
|
---|
8010 |
|
---|
8011 | rc2 = vdThreadStartWrite(pDiskTo);
|
---|
8012 | AssertRC(rc2);
|
---|
8013 | fLockWriteTo = true;
|
---|
8014 |
|
---|
8015 | if (RT_SUCCESS(rc) && !RTUuidIsNull(&ImageUuid))
|
---|
8016 | pDiskTo->pLast->Backend->pfnSetUuid(pDiskTo->pLast->pBackendData, &ImageUuid);
|
---|
8017 | }
|
---|
8018 | if (RT_FAILURE(rc))
|
---|
8019 | break;
|
---|
8020 |
|
---|
8021 | pImageTo = pDiskTo->pLast;
|
---|
8022 | AssertPtrBreakStmt(pImageTo, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
8023 |
|
---|
8024 | cbSize = RT_MIN(cbSize, cbSizeFrom);
|
---|
8025 | }
|
---|
8026 | else
|
---|
8027 | {
|
---|
8028 | pImageTo = pDiskTo->pLast;
|
---|
8029 | AssertPtrBreakStmt(pImageTo, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
8030 |
|
---|
8031 | uint64_t cbSizeTo;
|
---|
8032 | cbSizeTo = pImageTo->Backend->pfnGetSize(pImageTo->pBackendData);
|
---|
8033 | if (cbSizeTo == 0)
|
---|
8034 | {
|
---|
8035 | rc = VERR_VD_VALUE_NOT_FOUND;
|
---|
8036 | break;
|
---|
8037 | }
|
---|
8038 |
|
---|
8039 | if (cbSize == 0)
|
---|
8040 | cbSize = RT_MIN(cbSizeFrom, cbSizeTo);
|
---|
8041 |
|
---|
8042 | vdFixupPCHSGeometry(&PCHSGeometryFrom, cbSize);
|
---|
8043 | vdFixupLCHSGeometry(&LCHSGeometryFrom, cbSize);
|
---|
8044 |
|
---|
8045 | /* Update the geometry in the destination image. */
|
---|
8046 | pImageTo->Backend->pfnSetPCHSGeometry(pImageTo->pBackendData, &PCHSGeometryFrom);
|
---|
8047 | pImageTo->Backend->pfnSetLCHSGeometry(pImageTo->pBackendData, &LCHSGeometryFrom);
|
---|
8048 | }
|
---|
8049 |
|
---|
8050 | rc2 = vdThreadFinishWrite(pDiskTo);
|
---|
8051 | AssertRC(rc2);
|
---|
8052 | fLockWriteTo = false;
|
---|
8053 |
|
---|
8054 | /* Whether we can take the optimized copy path (false) or not.
|
---|
8055 | * Don't optimize if the image existed or if it is a child image. */
|
---|
8056 | bool fSuppressRedundantIo = ( !(pszFilename == NULL || cImagesTo > 0)
|
---|
8057 | || (nImageToSame != VD_IMAGE_CONTENT_UNKNOWN));
|
---|
8058 | unsigned cImagesFromReadBack, cImagesToReadBack;
|
---|
8059 |
|
---|
8060 | if (nImageFromSame == VD_IMAGE_CONTENT_UNKNOWN)
|
---|
8061 | cImagesFromReadBack = 0;
|
---|
8062 | else
|
---|
8063 | {
|
---|
8064 | if (nImage == VD_LAST_IMAGE)
|
---|
8065 | cImagesFromReadBack = pDiskFrom->cImages - nImageFromSame - 1;
|
---|
8066 | else
|
---|
8067 | cImagesFromReadBack = nImage - nImageFromSame;
|
---|
8068 | }
|
---|
8069 |
|
---|
8070 | if (nImageToSame == VD_IMAGE_CONTENT_UNKNOWN)
|
---|
8071 | cImagesToReadBack = 0;
|
---|
8072 | else
|
---|
8073 | cImagesToReadBack = pDiskTo->cImages - nImageToSame - 1;
|
---|
8074 |
|
---|
8075 | /* Copy the data. */
|
---|
8076 | rc = vdCopyHelper(pDiskFrom, pImageFrom, pDiskTo, cbSize,
|
---|
8077 | cImagesFromReadBack, cImagesToReadBack,
|
---|
8078 | fSuppressRedundantIo, pIfProgress, pDstIfProgress);
|
---|
8079 |
|
---|
8080 | if (RT_SUCCESS(rc))
|
---|
8081 | {
|
---|
8082 | rc2 = vdThreadStartWrite(pDiskTo);
|
---|
8083 | AssertRC(rc2);
|
---|
8084 | fLockWriteTo = true;
|
---|
8085 |
|
---|
8086 | /* Only set modification UUID if it is non-null, since the source
|
---|
8087 | * backend might not provide a valid modification UUID. */
|
---|
8088 | if (!RTUuidIsNull(&ImageModificationUuid))
|
---|
8089 | pImageTo->Backend->pfnSetModificationUuid(pImageTo->pBackendData, &ImageModificationUuid);
|
---|
8090 |
|
---|
8091 | /* Set the requested open flags if they differ from the value
|
---|
8092 | * required for creating the image and copying the contents. */
|
---|
8093 | if ( pImageTo && pszFilename
|
---|
8094 | && uOpenFlags != (uOpenFlags & ~VD_OPEN_FLAGS_READONLY))
|
---|
8095 | rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pBackendData,
|
---|
8096 | uOpenFlags);
|
---|
8097 | }
|
---|
8098 | } while (0);
|
---|
8099 |
|
---|
8100 | if (RT_FAILURE(rc) && pImageTo && pszFilename)
|
---|
8101 | {
|
---|
8102 | /* Take the write lock only if it is not taken. Not worth making the
|
---|
8103 | * above code even more complicated. */
|
---|
8104 | if (RT_UNLIKELY(!fLockWriteTo))
|
---|
8105 | {
|
---|
8106 | rc2 = vdThreadStartWrite(pDiskTo);
|
---|
8107 | AssertRC(rc2);
|
---|
8108 | fLockWriteTo = true;
|
---|
8109 | }
|
---|
8110 | /* Error detected, but new image created. Remove image from list. */
|
---|
8111 | vdRemoveImageFromList(pDiskTo, pImageTo);
|
---|
8112 |
|
---|
8113 | /* Close and delete image. */
|
---|
8114 | rc2 = pImageTo->Backend->pfnClose(pImageTo->pBackendData, true);
|
---|
8115 | AssertRC(rc2);
|
---|
8116 | pImageTo->pBackendData = NULL;
|
---|
8117 |
|
---|
8118 | /* Free remaining resources. */
|
---|
8119 | if (pImageTo->pszFilename)
|
---|
8120 | RTStrFree(pImageTo->pszFilename);
|
---|
8121 |
|
---|
8122 | RTMemFree(pImageTo);
|
---|
8123 | }
|
---|
8124 |
|
---|
8125 | if (RT_UNLIKELY(fLockWriteTo))
|
---|
8126 | {
|
---|
8127 | rc2 = vdThreadFinishWrite(pDiskTo);
|
---|
8128 | AssertRC(rc2);
|
---|
8129 | }
|
---|
8130 | if (RT_UNLIKELY(fLockWriteFrom))
|
---|
8131 | {
|
---|
8132 | rc2 = vdThreadFinishWrite(pDiskFrom);
|
---|
8133 | AssertRC(rc2);
|
---|
8134 | }
|
---|
8135 | else if (RT_UNLIKELY(fLockReadFrom))
|
---|
8136 | {
|
---|
8137 | rc2 = vdThreadFinishRead(pDiskFrom);
|
---|
8138 | AssertRC(rc2);
|
---|
8139 | }
|
---|
8140 |
|
---|
8141 | if (RT_SUCCESS(rc))
|
---|
8142 | {
|
---|
8143 | if (pIfProgress && pIfProgress->pfnProgress)
|
---|
8144 | pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
|
---|
8145 | if (pDstIfProgress && pDstIfProgress->pfnProgress)
|
---|
8146 | pDstIfProgress->pfnProgress(pDstIfProgress->Core.pvUser, 100);
|
---|
8147 | }
|
---|
8148 |
|
---|
8149 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8150 | return rc;
|
---|
8151 | }
|
---|
8152 |
|
---|
8153 | /**
|
---|
8154 | * Copies an image from one HDD container to another.
|
---|
8155 | * The copy is opened in the target HDD container.
|
---|
8156 | * It is possible to convert between different image formats, because the
|
---|
8157 | * backend for the destination may be different from the source.
|
---|
8158 | * If both the source and destination reference the same HDD container,
|
---|
8159 | * then the image is moved (by copying/deleting or renaming) to the new location.
|
---|
8160 | * The source container is unchanged if the move operation fails, otherwise
|
---|
8161 | * the image at the new location is opened in the same way as the old one was.
|
---|
8162 | *
|
---|
8163 | * @returns VBox status code.
|
---|
8164 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
8165 | * @param pDiskFrom Pointer to source HDD container.
|
---|
8166 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
8167 | * @param pDiskTo Pointer to destination HDD container.
|
---|
8168 | * @param pszBackend Name of the image file backend to use.
|
---|
8169 | * @param pszFilename New name of the image (may be NULL if pDiskFrom == pDiskTo).
|
---|
8170 | * @param fMoveByRename If true, attempt to perform a move by renaming (if successful the new size is ignored).
|
---|
8171 | * @param cbSize New image size (0 means leave unchanged).
|
---|
8172 | * @param uImageFlags Flags specifying special destination image features.
|
---|
8173 | * @param pDstUuid New UUID of the destination image. If NULL, a new UUID is created.
|
---|
8174 | * This parameter is used if and only if a true copy is created.
|
---|
8175 | * In all rename/move cases the UUIDs are copied over.
|
---|
8176 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
8177 | * Only used if the destination image is created.
|
---|
8178 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
8179 | * @param pDstVDIfsImage Pointer to the per-image VD interface list, for the
|
---|
8180 | * destination image.
|
---|
8181 | * @param pDstVDIfsOperation Pointer to the per-image VD interface list,
|
---|
8182 | * for the destination image.
|
---|
8183 | */
|
---|
8184 | VBOXDDU_DECL(int) VDCopy(PVBOXHDD pDiskFrom, unsigned nImage, PVBOXHDD pDiskTo,
|
---|
8185 | const char *pszBackend, const char *pszFilename,
|
---|
8186 | bool fMoveByRename, uint64_t cbSize,
|
---|
8187 | unsigned uImageFlags, PCRTUUID pDstUuid,
|
---|
8188 | unsigned uOpenFlags, PVDINTERFACE pVDIfsOperation,
|
---|
8189 | PVDINTERFACE pDstVDIfsImage,
|
---|
8190 | PVDINTERFACE pDstVDIfsOperation)
|
---|
8191 | {
|
---|
8192 | return VDCopyEx(pDiskFrom, nImage, pDiskTo, pszBackend, pszFilename, fMoveByRename,
|
---|
8193 | cbSize, VD_IMAGE_CONTENT_UNKNOWN, VD_IMAGE_CONTENT_UNKNOWN,
|
---|
8194 | uImageFlags, pDstUuid, uOpenFlags, pVDIfsOperation,
|
---|
8195 | pDstVDIfsImage, pDstVDIfsOperation);
|
---|
8196 | }
|
---|
8197 |
|
---|
8198 | /**
|
---|
8199 | * Optimizes the storage consumption of an image. Typically the unused blocks
|
---|
8200 | * have to be wiped with zeroes to achieve a substantial reduced storage use.
|
---|
8201 | * Another optimization done is reordering the image blocks, which can provide
|
---|
8202 | * a significant performance boost, as reads and writes tend to use less random
|
---|
8203 | * file offsets.
|
---|
8204 | *
|
---|
8205 | * @return VBox status code.
|
---|
8206 | * @return VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
8207 | * @return VERR_VD_IMAGE_READ_ONLY if image is not writable.
|
---|
8208 | * @return VERR_NOT_SUPPORTED if this kind of image can be compacted, but
|
---|
8209 | * the code for this isn't implemented yet.
|
---|
8210 | * @param pDisk Pointer to HDD container.
|
---|
8211 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
8212 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
8213 | */
|
---|
8214 | VBOXDDU_DECL(int) VDCompact(PVBOXHDD pDisk, unsigned nImage,
|
---|
8215 | PVDINTERFACE pVDIfsOperation)
|
---|
8216 | {
|
---|
8217 | int rc = VINF_SUCCESS;
|
---|
8218 | int rc2;
|
---|
8219 | bool fLockRead = false, fLockWrite = false;
|
---|
8220 | void *pvBuf = NULL;
|
---|
8221 | void *pvTmp = NULL;
|
---|
8222 |
|
---|
8223 | LogFlowFunc(("pDisk=%#p nImage=%u pVDIfsOperation=%#p\n",
|
---|
8224 | pDisk, nImage, pVDIfsOperation));
|
---|
8225 |
|
---|
8226 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
8227 |
|
---|
8228 | do {
|
---|
8229 | /* Check arguments. */
|
---|
8230 | AssertMsgBreakStmt(VALID_PTR(pDisk), ("pDisk=%#p\n", pDisk),
|
---|
8231 | rc = VERR_INVALID_PARAMETER);
|
---|
8232 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE,
|
---|
8233 | ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8234 |
|
---|
8235 | rc2 = vdThreadStartRead(pDisk);
|
---|
8236 | AssertRC(rc2);
|
---|
8237 | fLockRead = true;
|
---|
8238 |
|
---|
8239 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
8240 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
8241 |
|
---|
8242 | /* If there is no compact callback for not file based backends then
|
---|
8243 | * the backend doesn't need compaction. No need to make much fuss about
|
---|
8244 | * this. For file based ones signal this as not yet supported. */
|
---|
8245 | if (!pImage->Backend->pfnCompact)
|
---|
8246 | {
|
---|
8247 | if (pImage->Backend->uBackendCaps & VD_CAP_FILE)
|
---|
8248 | rc = VERR_NOT_SUPPORTED;
|
---|
8249 | else
|
---|
8250 | rc = VINF_SUCCESS;
|
---|
8251 | break;
|
---|
8252 | }
|
---|
8253 |
|
---|
8254 | /* Insert interface for reading parent state into per-operation list,
|
---|
8255 | * if there is a parent image. */
|
---|
8256 | VDINTERFACEPARENTSTATE VDIfParent;
|
---|
8257 | VDPARENTSTATEDESC ParentUser;
|
---|
8258 | if (pImage->pPrev)
|
---|
8259 | {
|
---|
8260 | VDIfParent.pfnParentRead = vdParentRead;
|
---|
8261 | ParentUser.pDisk = pDisk;
|
---|
8262 | ParentUser.pImage = pImage->pPrev;
|
---|
8263 | rc = VDInterfaceAdd(&VDIfParent.Core, "VDCompact_ParentState", VDINTERFACETYPE_PARENTSTATE,
|
---|
8264 | &ParentUser, sizeof(VDINTERFACEPARENTSTATE), &pVDIfsOperation);
|
---|
8265 | AssertRC(rc);
|
---|
8266 | }
|
---|
8267 |
|
---|
8268 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8269 | AssertRC(rc2);
|
---|
8270 | fLockRead = false;
|
---|
8271 |
|
---|
8272 | rc2 = vdThreadStartWrite(pDisk);
|
---|
8273 | AssertRC(rc2);
|
---|
8274 | fLockWrite = true;
|
---|
8275 |
|
---|
8276 | rc = pImage->Backend->pfnCompact(pImage->pBackendData,
|
---|
8277 | 0, 99,
|
---|
8278 | pDisk->pVDIfsDisk,
|
---|
8279 | pImage->pVDIfsImage,
|
---|
8280 | pVDIfsOperation);
|
---|
8281 | } while (0);
|
---|
8282 |
|
---|
8283 | if (RT_UNLIKELY(fLockWrite))
|
---|
8284 | {
|
---|
8285 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
8286 | AssertRC(rc2);
|
---|
8287 | }
|
---|
8288 | else if (RT_UNLIKELY(fLockRead))
|
---|
8289 | {
|
---|
8290 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8291 | AssertRC(rc2);
|
---|
8292 | }
|
---|
8293 |
|
---|
8294 | if (pvBuf)
|
---|
8295 | RTMemTmpFree(pvBuf);
|
---|
8296 | if (pvTmp)
|
---|
8297 | RTMemTmpFree(pvTmp);
|
---|
8298 |
|
---|
8299 | if (RT_SUCCESS(rc))
|
---|
8300 | {
|
---|
8301 | if (pIfProgress && pIfProgress->pfnProgress)
|
---|
8302 | pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
|
---|
8303 | }
|
---|
8304 |
|
---|
8305 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8306 | return rc;
|
---|
8307 | }
|
---|
8308 |
|
---|
8309 | /**
|
---|
8310 | * Resizes the given disk image to the given size.
|
---|
8311 | *
|
---|
8312 | * @return VBox status
|
---|
8313 | * @return VERR_VD_IMAGE_READ_ONLY if image is not writable.
|
---|
8314 | * @return VERR_NOT_SUPPORTED if this kind of image can be compacted, but
|
---|
8315 | *
|
---|
8316 | * @param pDisk Pointer to the HDD container.
|
---|
8317 | * @param cbSize New size of the image.
|
---|
8318 | * @param pPCHSGeometry Pointer to the new physical disk geometry <= (16383,16,63). Not NULL.
|
---|
8319 | * @param pLCHSGeometry Pointer to the new logical disk geometry <= (x,255,63). Not NULL.
|
---|
8320 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
8321 | */
|
---|
8322 | VBOXDDU_DECL(int) VDResize(PVBOXHDD pDisk, uint64_t cbSize,
|
---|
8323 | PCVDGEOMETRY pPCHSGeometry,
|
---|
8324 | PCVDGEOMETRY pLCHSGeometry,
|
---|
8325 | PVDINTERFACE pVDIfsOperation)
|
---|
8326 | {
|
---|
8327 | /** @todo r=klaus resizing was designed to be part of VDCopy, so having a separate function is not desirable. */
|
---|
8328 | int rc = VINF_SUCCESS;
|
---|
8329 | int rc2;
|
---|
8330 | bool fLockRead = false, fLockWrite = false;
|
---|
8331 |
|
---|
8332 | LogFlowFunc(("pDisk=%#p cbSize=%llu pVDIfsOperation=%#p\n",
|
---|
8333 | pDisk, cbSize, pVDIfsOperation));
|
---|
8334 |
|
---|
8335 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
8336 |
|
---|
8337 | do {
|
---|
8338 | /* Check arguments. */
|
---|
8339 | AssertMsgBreakStmt(VALID_PTR(pDisk), ("pDisk=%#p\n", pDisk),
|
---|
8340 | rc = VERR_INVALID_PARAMETER);
|
---|
8341 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE,
|
---|
8342 | ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8343 |
|
---|
8344 | rc2 = vdThreadStartRead(pDisk);
|
---|
8345 | AssertRC(rc2);
|
---|
8346 | fLockRead = true;
|
---|
8347 |
|
---|
8348 | /* Must have at least one image in the chain, will resize last. */
|
---|
8349 | AssertMsgBreakStmt(pDisk->cImages >= 1, ("cImages=%u\n", pDisk->cImages),
|
---|
8350 | rc = VERR_NOT_SUPPORTED);
|
---|
8351 |
|
---|
8352 | PVDIMAGE pImage = pDisk->pLast;
|
---|
8353 |
|
---|
8354 | /* If there is no compact callback for not file based backends then
|
---|
8355 | * the backend doesn't need compaction. No need to make much fuss about
|
---|
8356 | * this. For file based ones signal this as not yet supported. */
|
---|
8357 | if (!pImage->Backend->pfnResize)
|
---|
8358 | {
|
---|
8359 | if (pImage->Backend->uBackendCaps & VD_CAP_FILE)
|
---|
8360 | rc = VERR_NOT_SUPPORTED;
|
---|
8361 | else
|
---|
8362 | rc = VINF_SUCCESS;
|
---|
8363 | break;
|
---|
8364 | }
|
---|
8365 |
|
---|
8366 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8367 | AssertRC(rc2);
|
---|
8368 | fLockRead = false;
|
---|
8369 |
|
---|
8370 | rc2 = vdThreadStartWrite(pDisk);
|
---|
8371 | AssertRC(rc2);
|
---|
8372 | fLockWrite = true;
|
---|
8373 |
|
---|
8374 | VDGEOMETRY PCHSGeometryOld;
|
---|
8375 | VDGEOMETRY LCHSGeometryOld;
|
---|
8376 | PCVDGEOMETRY pPCHSGeometryNew;
|
---|
8377 | PCVDGEOMETRY pLCHSGeometryNew;
|
---|
8378 |
|
---|
8379 | if (pPCHSGeometry->cCylinders == 0)
|
---|
8380 | {
|
---|
8381 | /* Auto-detect marker, calculate new value ourself. */
|
---|
8382 | rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData, &PCHSGeometryOld);
|
---|
8383 | if (RT_SUCCESS(rc) && (PCHSGeometryOld.cCylinders != 0))
|
---|
8384 | PCHSGeometryOld.cCylinders = RT_MIN(cbSize / 512 / PCHSGeometryOld.cHeads / PCHSGeometryOld.cSectors, 16383);
|
---|
8385 | else if (rc == VERR_VD_GEOMETRY_NOT_SET)
|
---|
8386 | rc = VINF_SUCCESS;
|
---|
8387 |
|
---|
8388 | pPCHSGeometryNew = &PCHSGeometryOld;
|
---|
8389 | }
|
---|
8390 | else
|
---|
8391 | pPCHSGeometryNew = pPCHSGeometry;
|
---|
8392 |
|
---|
8393 | if (pLCHSGeometry->cCylinders == 0)
|
---|
8394 | {
|
---|
8395 | /* Auto-detect marker, calculate new value ourself. */
|
---|
8396 | rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData, &LCHSGeometryOld);
|
---|
8397 | if (RT_SUCCESS(rc) && (LCHSGeometryOld.cCylinders != 0))
|
---|
8398 | LCHSGeometryOld.cCylinders = cbSize / 512 / LCHSGeometryOld.cHeads / LCHSGeometryOld.cSectors;
|
---|
8399 | else if (rc == VERR_VD_GEOMETRY_NOT_SET)
|
---|
8400 | rc = VINF_SUCCESS;
|
---|
8401 |
|
---|
8402 | pLCHSGeometryNew = &LCHSGeometryOld;
|
---|
8403 | }
|
---|
8404 | else
|
---|
8405 | pLCHSGeometryNew = pLCHSGeometry;
|
---|
8406 |
|
---|
8407 | if (RT_SUCCESS(rc))
|
---|
8408 | rc = pImage->Backend->pfnResize(pImage->pBackendData,
|
---|
8409 | cbSize,
|
---|
8410 | pPCHSGeometryNew,
|
---|
8411 | pLCHSGeometryNew,
|
---|
8412 | 0, 99,
|
---|
8413 | pDisk->pVDIfsDisk,
|
---|
8414 | pImage->pVDIfsImage,
|
---|
8415 | pVDIfsOperation);
|
---|
8416 | } while (0);
|
---|
8417 |
|
---|
8418 | if (RT_UNLIKELY(fLockWrite))
|
---|
8419 | {
|
---|
8420 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
8421 | AssertRC(rc2);
|
---|
8422 | }
|
---|
8423 | else if (RT_UNLIKELY(fLockRead))
|
---|
8424 | {
|
---|
8425 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8426 | AssertRC(rc2);
|
---|
8427 | }
|
---|
8428 |
|
---|
8429 | if (RT_SUCCESS(rc))
|
---|
8430 | {
|
---|
8431 | if (pIfProgress && pIfProgress->pfnProgress)
|
---|
8432 | pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
|
---|
8433 |
|
---|
8434 | pDisk->cbSize = cbSize;
|
---|
8435 | }
|
---|
8436 |
|
---|
8437 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8438 | return rc;
|
---|
8439 | }
|
---|
8440 |
|
---|
8441 | /**
|
---|
8442 | * Closes the last opened image file in HDD container.
|
---|
8443 | * If previous image file was opened in read-only mode (the normal case) and
|
---|
8444 | * the last opened image is in read-write mode then the previous image will be
|
---|
8445 | * reopened in read/write mode.
|
---|
8446 | *
|
---|
8447 | * @returns VBox status code.
|
---|
8448 | * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
|
---|
8449 | * @param pDisk Pointer to HDD container.
|
---|
8450 | * @param fDelete If true, delete the image from the host disk.
|
---|
8451 | */
|
---|
8452 | VBOXDDU_DECL(int) VDClose(PVBOXHDD pDisk, bool fDelete)
|
---|
8453 | {
|
---|
8454 | int rc = VINF_SUCCESS;
|
---|
8455 | int rc2;
|
---|
8456 | bool fLockWrite = false;
|
---|
8457 |
|
---|
8458 | LogFlowFunc(("pDisk=%#p fDelete=%d\n", pDisk, fDelete));
|
---|
8459 | do
|
---|
8460 | {
|
---|
8461 | /* sanity check */
|
---|
8462 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8463 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8464 |
|
---|
8465 | /* Not worth splitting this up into a read lock phase and write
|
---|
8466 | * lock phase, as closing an image is a relatively fast operation
|
---|
8467 | * dominated by the part which needs the write lock. */
|
---|
8468 | rc2 = vdThreadStartWrite(pDisk);
|
---|
8469 | AssertRC(rc2);
|
---|
8470 | fLockWrite = true;
|
---|
8471 |
|
---|
8472 | PVDIMAGE pImage = pDisk->pLast;
|
---|
8473 | if (!pImage)
|
---|
8474 | {
|
---|
8475 | rc = VERR_VD_NOT_OPENED;
|
---|
8476 | break;
|
---|
8477 | }
|
---|
8478 |
|
---|
8479 | /* Destroy the current discard state first which might still have pending blocks. */
|
---|
8480 | rc = vdDiscardStateDestroy(pDisk);
|
---|
8481 | if (RT_FAILURE(rc))
|
---|
8482 | break;
|
---|
8483 |
|
---|
8484 | unsigned uOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
|
---|
8485 | /* Remove image from list of opened images. */
|
---|
8486 | vdRemoveImageFromList(pDisk, pImage);
|
---|
8487 | /* Close (and optionally delete) image. */
|
---|
8488 | rc = pImage->Backend->pfnClose(pImage->pBackendData, fDelete);
|
---|
8489 | /* Free remaining resources related to the image. */
|
---|
8490 | RTStrFree(pImage->pszFilename);
|
---|
8491 | RTMemFree(pImage);
|
---|
8492 |
|
---|
8493 | pImage = pDisk->pLast;
|
---|
8494 | if (!pImage)
|
---|
8495 | break;
|
---|
8496 |
|
---|
8497 | /* If disk was previously in read/write mode, make sure it will stay
|
---|
8498 | * like this (if possible) after closing this image. Set the open flags
|
---|
8499 | * accordingly. */
|
---|
8500 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
8501 | {
|
---|
8502 | uOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
|
---|
8503 | uOpenFlags &= ~ VD_OPEN_FLAGS_READONLY;
|
---|
8504 | rc = pImage->Backend->pfnSetOpenFlags(pImage->pBackendData, uOpenFlags);
|
---|
8505 | }
|
---|
8506 |
|
---|
8507 | /* Cache disk information. */
|
---|
8508 | pDisk->cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
|
---|
8509 |
|
---|
8510 | /* Cache PCHS geometry. */
|
---|
8511 | rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
8512 | &pDisk->PCHSGeometry);
|
---|
8513 | if (RT_FAILURE(rc2))
|
---|
8514 | {
|
---|
8515 | pDisk->PCHSGeometry.cCylinders = 0;
|
---|
8516 | pDisk->PCHSGeometry.cHeads = 0;
|
---|
8517 | pDisk->PCHSGeometry.cSectors = 0;
|
---|
8518 | }
|
---|
8519 | else
|
---|
8520 | {
|
---|
8521 | /* Make sure the PCHS geometry is properly clipped. */
|
---|
8522 | pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
|
---|
8523 | pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
|
---|
8524 | pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
|
---|
8525 | }
|
---|
8526 |
|
---|
8527 | /* Cache LCHS geometry. */
|
---|
8528 | rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
8529 | &pDisk->LCHSGeometry);
|
---|
8530 | if (RT_FAILURE(rc2))
|
---|
8531 | {
|
---|
8532 | pDisk->LCHSGeometry.cCylinders = 0;
|
---|
8533 | pDisk->LCHSGeometry.cHeads = 0;
|
---|
8534 | pDisk->LCHSGeometry.cSectors = 0;
|
---|
8535 | }
|
---|
8536 | else
|
---|
8537 | {
|
---|
8538 | /* Make sure the LCHS geometry is properly clipped. */
|
---|
8539 | pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
|
---|
8540 | pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
|
---|
8541 | }
|
---|
8542 | } while (0);
|
---|
8543 |
|
---|
8544 | if (RT_UNLIKELY(fLockWrite))
|
---|
8545 | {
|
---|
8546 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
8547 | AssertRC(rc2);
|
---|
8548 | }
|
---|
8549 |
|
---|
8550 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8551 | return rc;
|
---|
8552 | }
|
---|
8553 |
|
---|
8554 | /**
|
---|
8555 | * Closes the currently opened cache image file in HDD container.
|
---|
8556 | *
|
---|
8557 | * @return VBox status code.
|
---|
8558 | * @return VERR_VD_NOT_OPENED if no cache is opened in HDD container.
|
---|
8559 | * @param pDisk Pointer to HDD container.
|
---|
8560 | * @param fDelete If true, delete the image from the host disk.
|
---|
8561 | */
|
---|
8562 | VBOXDDU_DECL(int) VDCacheClose(PVBOXHDD pDisk, bool fDelete)
|
---|
8563 | {
|
---|
8564 | int rc = VINF_SUCCESS;
|
---|
8565 | int rc2;
|
---|
8566 | bool fLockWrite = false;
|
---|
8567 | PVDCACHE pCache = NULL;
|
---|
8568 |
|
---|
8569 | LogFlowFunc(("pDisk=%#p fDelete=%d\n", pDisk, fDelete));
|
---|
8570 |
|
---|
8571 | do
|
---|
8572 | {
|
---|
8573 | /* sanity check */
|
---|
8574 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8575 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8576 |
|
---|
8577 | rc2 = vdThreadStartWrite(pDisk);
|
---|
8578 | AssertRC(rc2);
|
---|
8579 | fLockWrite = true;
|
---|
8580 |
|
---|
8581 | AssertPtrBreakStmt(pDisk->pCache, rc = VERR_VD_CACHE_NOT_FOUND);
|
---|
8582 |
|
---|
8583 | pCache = pDisk->pCache;
|
---|
8584 | pDisk->pCache = NULL;
|
---|
8585 |
|
---|
8586 | pCache->Backend->pfnClose(pCache->pBackendData, fDelete);
|
---|
8587 | if (pCache->pszFilename)
|
---|
8588 | RTStrFree(pCache->pszFilename);
|
---|
8589 | RTMemFree(pCache);
|
---|
8590 | } while (0);
|
---|
8591 |
|
---|
8592 | if (RT_LIKELY(fLockWrite))
|
---|
8593 | {
|
---|
8594 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
8595 | AssertRC(rc2);
|
---|
8596 | }
|
---|
8597 |
|
---|
8598 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8599 | return rc;
|
---|
8600 | }
|
---|
8601 |
|
---|
8602 | /**
|
---|
8603 | * Removes the last added filter in the HDD container.
|
---|
8604 | *
|
---|
8605 | * @return VBox status code.
|
---|
8606 | * @retval VERR_VD_NOT_OPENED if no filter is present for the disk.
|
---|
8607 | * @param pDisk Pointer to HDD container.
|
---|
8608 | */
|
---|
8609 | VBOXDDU_DECL(int) VDFilterRemove(PVBOXHDD pDisk)
|
---|
8610 | {
|
---|
8611 | int rc = VINF_SUCCESS;
|
---|
8612 | int rc2;
|
---|
8613 | bool fLockWrite = false;
|
---|
8614 | PVDFILTER pFilter = NULL;
|
---|
8615 |
|
---|
8616 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
8617 |
|
---|
8618 | do
|
---|
8619 | {
|
---|
8620 | /* sanity check */
|
---|
8621 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8622 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8623 |
|
---|
8624 | rc2 = vdThreadStartWrite(pDisk);
|
---|
8625 | AssertRC(rc2);
|
---|
8626 | fLockWrite = true;
|
---|
8627 |
|
---|
8628 | AssertPtrBreakStmt(pDisk->pFilterHead, rc = VERR_VD_NOT_OPENED);
|
---|
8629 |
|
---|
8630 | pFilter = pDisk->pFilterTail;
|
---|
8631 | vdRemoveFilterFromList(pDisk, pFilter);
|
---|
8632 |
|
---|
8633 | pFilter->pBackend->pfnDestroy(pFilter->pvBackendData);
|
---|
8634 | RTMemFree(pFilter);
|
---|
8635 | } while (0);
|
---|
8636 |
|
---|
8637 | if (RT_LIKELY(fLockWrite))
|
---|
8638 | {
|
---|
8639 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
8640 | AssertRC(rc2);
|
---|
8641 | }
|
---|
8642 |
|
---|
8643 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8644 | return rc;
|
---|
8645 | }
|
---|
8646 |
|
---|
8647 | /**
|
---|
8648 | * Closes all opened image files in HDD container.
|
---|
8649 | *
|
---|
8650 | * @returns VBox status code.
|
---|
8651 | * @param pDisk Pointer to HDD container.
|
---|
8652 | */
|
---|
8653 | VBOXDDU_DECL(int) VDCloseAll(PVBOXHDD pDisk)
|
---|
8654 | {
|
---|
8655 | int rc = VINF_SUCCESS;
|
---|
8656 | int rc2;
|
---|
8657 | bool fLockWrite = false;
|
---|
8658 |
|
---|
8659 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
8660 | do
|
---|
8661 | {
|
---|
8662 | /* sanity check */
|
---|
8663 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8664 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8665 |
|
---|
8666 | /* Lock the entire operation. */
|
---|
8667 | rc2 = vdThreadStartWrite(pDisk);
|
---|
8668 | AssertRC(rc2);
|
---|
8669 | fLockWrite = true;
|
---|
8670 |
|
---|
8671 | PVDCACHE pCache = pDisk->pCache;
|
---|
8672 | if (pCache)
|
---|
8673 | {
|
---|
8674 | rc2 = pCache->Backend->pfnClose(pCache->pBackendData, false);
|
---|
8675 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
8676 | rc = rc2;
|
---|
8677 |
|
---|
8678 | if (pCache->pszFilename)
|
---|
8679 | RTStrFree(pCache->pszFilename);
|
---|
8680 | RTMemFree(pCache);
|
---|
8681 | }
|
---|
8682 |
|
---|
8683 | PVDIMAGE pImage = pDisk->pLast;
|
---|
8684 | while (VALID_PTR(pImage))
|
---|
8685 | {
|
---|
8686 | PVDIMAGE pPrev = pImage->pPrev;
|
---|
8687 | /* Remove image from list of opened images. */
|
---|
8688 | vdRemoveImageFromList(pDisk, pImage);
|
---|
8689 | /* Close image. */
|
---|
8690 | rc2 = pImage->Backend->pfnClose(pImage->pBackendData, false);
|
---|
8691 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
8692 | rc = rc2;
|
---|
8693 | /* Free remaining resources related to the image. */
|
---|
8694 | RTStrFree(pImage->pszFilename);
|
---|
8695 | RTMemFree(pImage);
|
---|
8696 | pImage = pPrev;
|
---|
8697 | }
|
---|
8698 | Assert(!VALID_PTR(pDisk->pLast));
|
---|
8699 | } while (0);
|
---|
8700 |
|
---|
8701 | if (RT_UNLIKELY(fLockWrite))
|
---|
8702 | {
|
---|
8703 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
8704 | AssertRC(rc2);
|
---|
8705 | }
|
---|
8706 |
|
---|
8707 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8708 | return rc;
|
---|
8709 | }
|
---|
8710 |
|
---|
8711 | /**
|
---|
8712 | * Removes all filters of the given HDD container.
|
---|
8713 | *
|
---|
8714 | * @return VBox status code.
|
---|
8715 | * @param pDisk Pointer to HDD container.
|
---|
8716 | */
|
---|
8717 | VBOXDDU_DECL(int) VDFilterRemoveAll(PVBOXHDD pDisk)
|
---|
8718 | {
|
---|
8719 | int rc = VINF_SUCCESS;
|
---|
8720 | int rc2;
|
---|
8721 | bool fLockWrite = false;
|
---|
8722 |
|
---|
8723 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
8724 | do
|
---|
8725 | {
|
---|
8726 | /* sanity check */
|
---|
8727 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8728 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8729 |
|
---|
8730 | /* Lock the entire operation. */
|
---|
8731 | rc2 = vdThreadStartWrite(pDisk);
|
---|
8732 | AssertRC(rc2);
|
---|
8733 | fLockWrite = true;
|
---|
8734 |
|
---|
8735 | PVDFILTER pFilter = pDisk->pFilterTail;
|
---|
8736 | while (VALID_PTR(pFilter))
|
---|
8737 | {
|
---|
8738 | PVDFILTER pPrev = pFilter->pPrev;
|
---|
8739 | vdRemoveFilterFromList(pDisk, pFilter);
|
---|
8740 |
|
---|
8741 | rc2 = pFilter->pBackend->pfnDestroy(pFilter->pvBackendData);
|
---|
8742 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
8743 | rc = rc2;
|
---|
8744 | /* Free remaining resources related to the image. */
|
---|
8745 | RTMemFree(pFilter);
|
---|
8746 | pFilter = pPrev;
|
---|
8747 | }
|
---|
8748 | Assert(!VALID_PTR(pDisk->pFilterTail));
|
---|
8749 | } while (0);
|
---|
8750 |
|
---|
8751 | if (RT_UNLIKELY(fLockWrite))
|
---|
8752 | {
|
---|
8753 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
8754 | AssertRC(rc2);
|
---|
8755 | }
|
---|
8756 |
|
---|
8757 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8758 | return rc;
|
---|
8759 | }
|
---|
8760 |
|
---|
8761 | /**
|
---|
8762 | * Read data from virtual HDD.
|
---|
8763 | *
|
---|
8764 | * @returns VBox status code.
|
---|
8765 | * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
|
---|
8766 | * @param pDisk Pointer to HDD container.
|
---|
8767 | * @param uOffset Offset of first reading byte from start of disk.
|
---|
8768 | * @param pvBuf Pointer to buffer for reading data.
|
---|
8769 | * @param cbRead Number of bytes to read.
|
---|
8770 | */
|
---|
8771 | VBOXDDU_DECL(int) VDRead(PVBOXHDD pDisk, uint64_t uOffset, void *pvBuf,
|
---|
8772 | size_t cbRead)
|
---|
8773 | {
|
---|
8774 | int rc = VINF_SUCCESS;
|
---|
8775 | int rc2;
|
---|
8776 | bool fLockRead = false;
|
---|
8777 |
|
---|
8778 | LogFlowFunc(("pDisk=%#p uOffset=%llu pvBuf=%p cbRead=%zu\n",
|
---|
8779 | pDisk, uOffset, pvBuf, cbRead));
|
---|
8780 | do
|
---|
8781 | {
|
---|
8782 | /* sanity check */
|
---|
8783 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8784 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8785 |
|
---|
8786 | /* Check arguments. */
|
---|
8787 | AssertMsgBreakStmt(VALID_PTR(pvBuf),
|
---|
8788 | ("pvBuf=%#p\n", pvBuf),
|
---|
8789 | rc = VERR_INVALID_PARAMETER);
|
---|
8790 | AssertMsgBreakStmt(cbRead,
|
---|
8791 | ("cbRead=%zu\n", cbRead),
|
---|
8792 | rc = VERR_INVALID_PARAMETER);
|
---|
8793 |
|
---|
8794 | rc2 = vdThreadStartRead(pDisk);
|
---|
8795 | AssertRC(rc2);
|
---|
8796 | fLockRead = true;
|
---|
8797 |
|
---|
8798 | PVDIMAGE pImage = pDisk->pLast;
|
---|
8799 | AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
|
---|
8800 |
|
---|
8801 | if (uOffset + cbRead > pDisk->cbSize)
|
---|
8802 | {
|
---|
8803 | /* Floppy images might be smaller than the standard expected by
|
---|
8804 | the floppy controller code. So, we won't fail here. */
|
---|
8805 | AssertMsgBreakStmt(pDisk->enmType == VDTYPE_FLOPPY,
|
---|
8806 | ("uOffset=%llu cbRead=%zu pDisk->cbSize=%llu\n",
|
---|
8807 | uOffset, cbRead, pDisk->cbSize),
|
---|
8808 | rc = VERR_EOF);
|
---|
8809 | memset(pvBuf, 0xf6, cbRead); /* f6h = format.com filler byte */
|
---|
8810 | if (uOffset >= pDisk->cbSize)
|
---|
8811 | break;
|
---|
8812 | cbRead = pDisk->cbSize - uOffset;
|
---|
8813 | }
|
---|
8814 |
|
---|
8815 | rc = vdReadHelper(pDisk, pImage, uOffset, pvBuf, cbRead,
|
---|
8816 | true /* fUpdateCache */);
|
---|
8817 | } while (0);
|
---|
8818 |
|
---|
8819 | if (RT_UNLIKELY(fLockRead))
|
---|
8820 | {
|
---|
8821 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8822 | AssertRC(rc2);
|
---|
8823 | }
|
---|
8824 |
|
---|
8825 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8826 | return rc;
|
---|
8827 | }
|
---|
8828 |
|
---|
8829 | /**
|
---|
8830 | * Write data to virtual HDD.
|
---|
8831 | *
|
---|
8832 | * @returns VBox status code.
|
---|
8833 | * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
|
---|
8834 | * @param pDisk Pointer to HDD container.
|
---|
8835 | * @param uOffset Offset of the first byte being
|
---|
8836 | * written from start of disk.
|
---|
8837 | * @param pvBuf Pointer to buffer for writing data.
|
---|
8838 | * @param cbWrite Number of bytes to write.
|
---|
8839 | */
|
---|
8840 | VBOXDDU_DECL(int) VDWrite(PVBOXHDD pDisk, uint64_t uOffset, const void *pvBuf,
|
---|
8841 | size_t cbWrite)
|
---|
8842 | {
|
---|
8843 | int rc = VINF_SUCCESS;
|
---|
8844 | int rc2;
|
---|
8845 | bool fLockWrite = false;
|
---|
8846 |
|
---|
8847 | LogFlowFunc(("pDisk=%#p uOffset=%llu pvBuf=%p cbWrite=%zu\n",
|
---|
8848 | pDisk, uOffset, pvBuf, cbWrite));
|
---|
8849 | do
|
---|
8850 | {
|
---|
8851 | /* sanity check */
|
---|
8852 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8853 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8854 |
|
---|
8855 | /* Check arguments. */
|
---|
8856 | AssertMsgBreakStmt(VALID_PTR(pvBuf),
|
---|
8857 | ("pvBuf=%#p\n", pvBuf),
|
---|
8858 | rc = VERR_INVALID_PARAMETER);
|
---|
8859 | AssertMsgBreakStmt(cbWrite,
|
---|
8860 | ("cbWrite=%zu\n", cbWrite),
|
---|
8861 | rc = VERR_INVALID_PARAMETER);
|
---|
8862 |
|
---|
8863 | rc2 = vdThreadStartWrite(pDisk);
|
---|
8864 | AssertRC(rc2);
|
---|
8865 | fLockWrite = true;
|
---|
8866 |
|
---|
8867 | AssertMsgBreakStmt(uOffset + cbWrite <= pDisk->cbSize,
|
---|
8868 | ("uOffset=%llu cbWrite=%zu pDisk->cbSize=%llu\n",
|
---|
8869 | uOffset, cbWrite, pDisk->cbSize),
|
---|
8870 | rc = VERR_INVALID_PARAMETER);
|
---|
8871 |
|
---|
8872 | PVDIMAGE pImage = pDisk->pLast;
|
---|
8873 | AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
|
---|
8874 |
|
---|
8875 | vdSetModifiedFlag(pDisk);
|
---|
8876 | rc = vdWriteHelper(pDisk, pImage, uOffset, pvBuf, cbWrite,
|
---|
8877 | VDIOCTX_FLAGS_READ_UPDATE_CACHE);
|
---|
8878 | if (RT_FAILURE(rc))
|
---|
8879 | break;
|
---|
8880 |
|
---|
8881 | /* If there is a merge (in the direction towards a parent) running
|
---|
8882 | * concurrently then we have to also "relay" the write to this parent,
|
---|
8883 | * as the merge position might be already past the position where
|
---|
8884 | * this write is going. The "context" of the write can come from the
|
---|
8885 | * natural chain, since merging either already did or will take care
|
---|
8886 | * of the "other" content which is might be needed to fill the block
|
---|
8887 | * to a full allocation size. The cache doesn't need to be touched
|
---|
8888 | * as this write is covered by the previous one. */
|
---|
8889 | if (RT_UNLIKELY(pDisk->pImageRelay))
|
---|
8890 | rc = vdWriteHelper(pDisk, pDisk->pImageRelay, uOffset,
|
---|
8891 | pvBuf, cbWrite, VDIOCTX_FLAGS_DEFAULT);
|
---|
8892 | } while (0);
|
---|
8893 |
|
---|
8894 | if (RT_UNLIKELY(fLockWrite))
|
---|
8895 | {
|
---|
8896 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
8897 | AssertRC(rc2);
|
---|
8898 | }
|
---|
8899 |
|
---|
8900 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8901 | return rc;
|
---|
8902 | }
|
---|
8903 |
|
---|
8904 | /**
|
---|
8905 | * Make sure the on disk representation of a virtual HDD is up to date.
|
---|
8906 | *
|
---|
8907 | * @returns VBox status code.
|
---|
8908 | * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
|
---|
8909 | * @param pDisk Pointer to HDD container.
|
---|
8910 | */
|
---|
8911 | VBOXDDU_DECL(int) VDFlush(PVBOXHDD pDisk)
|
---|
8912 | {
|
---|
8913 | int rc = VINF_SUCCESS;
|
---|
8914 | int rc2;
|
---|
8915 | bool fLockWrite = false;
|
---|
8916 |
|
---|
8917 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
8918 | do
|
---|
8919 | {
|
---|
8920 | /* sanity check */
|
---|
8921 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8922 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8923 |
|
---|
8924 | rc2 = vdThreadStartWrite(pDisk);
|
---|
8925 | AssertRC(rc2);
|
---|
8926 | fLockWrite = true;
|
---|
8927 |
|
---|
8928 | PVDIMAGE pImage = pDisk->pLast;
|
---|
8929 | AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
|
---|
8930 |
|
---|
8931 | VDIOCTX IoCtx;
|
---|
8932 | RTSEMEVENT hEventComplete = NIL_RTSEMEVENT;
|
---|
8933 |
|
---|
8934 | rc = RTSemEventCreate(&hEventComplete);
|
---|
8935 | if (RT_FAILURE(rc))
|
---|
8936 | break;
|
---|
8937 |
|
---|
8938 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_FLUSH, 0, 0, pImage, NULL,
|
---|
8939 | NULL, vdFlushHelperAsync, VDIOCTX_FLAGS_SYNC | VDIOCTX_FLAGS_DONT_FREE);
|
---|
8940 |
|
---|
8941 | IoCtx.Type.Root.pfnComplete = vdIoCtxSyncComplete;
|
---|
8942 | IoCtx.Type.Root.pvUser1 = pDisk;
|
---|
8943 | IoCtx.Type.Root.pvUser2 = hEventComplete;
|
---|
8944 | rc = vdIoCtxProcessSync(&IoCtx, hEventComplete);
|
---|
8945 |
|
---|
8946 | RTSemEventDestroy(hEventComplete);
|
---|
8947 | } while (0);
|
---|
8948 |
|
---|
8949 | if (RT_UNLIKELY(fLockWrite))
|
---|
8950 | {
|
---|
8951 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
8952 | AssertRC(rc2);
|
---|
8953 | }
|
---|
8954 |
|
---|
8955 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8956 | return rc;
|
---|
8957 | }
|
---|
8958 |
|
---|
8959 | /**
|
---|
8960 | * Get number of opened images in HDD container.
|
---|
8961 | *
|
---|
8962 | * @returns Number of opened images for HDD container. 0 if no images have been opened.
|
---|
8963 | * @param pDisk Pointer to HDD container.
|
---|
8964 | */
|
---|
8965 | VBOXDDU_DECL(unsigned) VDGetCount(PVBOXHDD pDisk)
|
---|
8966 | {
|
---|
8967 | unsigned cImages;
|
---|
8968 | int rc2;
|
---|
8969 | bool fLockRead = false;
|
---|
8970 |
|
---|
8971 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
8972 | do
|
---|
8973 | {
|
---|
8974 | /* sanity check */
|
---|
8975 | AssertPtrBreakStmt(pDisk, cImages = 0);
|
---|
8976 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8977 |
|
---|
8978 | rc2 = vdThreadStartRead(pDisk);
|
---|
8979 | AssertRC(rc2);
|
---|
8980 | fLockRead = true;
|
---|
8981 |
|
---|
8982 | cImages = pDisk->cImages;
|
---|
8983 | } while (0);
|
---|
8984 |
|
---|
8985 | if (RT_UNLIKELY(fLockRead))
|
---|
8986 | {
|
---|
8987 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8988 | AssertRC(rc2);
|
---|
8989 | }
|
---|
8990 |
|
---|
8991 | LogFlowFunc(("returns %u\n", cImages));
|
---|
8992 | return cImages;
|
---|
8993 | }
|
---|
8994 |
|
---|
8995 | /**
|
---|
8996 | * Get read/write mode of HDD container.
|
---|
8997 | *
|
---|
8998 | * @returns Virtual disk ReadOnly status.
|
---|
8999 | * @returns true if no image is opened in HDD container.
|
---|
9000 | * @param pDisk Pointer to HDD container.
|
---|
9001 | */
|
---|
9002 | VBOXDDU_DECL(bool) VDIsReadOnly(PVBOXHDD pDisk)
|
---|
9003 | {
|
---|
9004 | bool fReadOnly;
|
---|
9005 | int rc2;
|
---|
9006 | bool fLockRead = false;
|
---|
9007 |
|
---|
9008 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
9009 | do
|
---|
9010 | {
|
---|
9011 | /* sanity check */
|
---|
9012 | AssertPtrBreakStmt(pDisk, fReadOnly = false);
|
---|
9013 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9014 |
|
---|
9015 | rc2 = vdThreadStartRead(pDisk);
|
---|
9016 | AssertRC(rc2);
|
---|
9017 | fLockRead = true;
|
---|
9018 |
|
---|
9019 | PVDIMAGE pImage = pDisk->pLast;
|
---|
9020 | AssertPtrBreakStmt(pImage, fReadOnly = true);
|
---|
9021 |
|
---|
9022 | unsigned uOpenFlags;
|
---|
9023 | uOpenFlags = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pBackendData);
|
---|
9024 | fReadOnly = !!(uOpenFlags & VD_OPEN_FLAGS_READONLY);
|
---|
9025 | } while (0);
|
---|
9026 |
|
---|
9027 | if (RT_UNLIKELY(fLockRead))
|
---|
9028 | {
|
---|
9029 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9030 | AssertRC(rc2);
|
---|
9031 | }
|
---|
9032 |
|
---|
9033 | LogFlowFunc(("returns %d\n", fReadOnly));
|
---|
9034 | return fReadOnly;
|
---|
9035 | }
|
---|
9036 |
|
---|
9037 | /**
|
---|
9038 | * Get sector size of an image in HDD container.
|
---|
9039 | *
|
---|
9040 | * @return Virtual disk sector size in bytes.
|
---|
9041 | * @return 0 if image with specified number was not opened.
|
---|
9042 | * @param pDisk Pointer to HDD container.
|
---|
9043 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9044 | */
|
---|
9045 | VBOXDDU_DECL(uint32_t) VDGetSectorSize(PVBOXHDD pDisk, unsigned nImage)
|
---|
9046 | {
|
---|
9047 | uint64_t cbSector;
|
---|
9048 | int rc2;
|
---|
9049 | bool fLockRead = false;
|
---|
9050 |
|
---|
9051 | LogFlowFunc(("pDisk=%#p nImage=%u\n", pDisk, nImage));
|
---|
9052 | do
|
---|
9053 | {
|
---|
9054 | /* sanity check */
|
---|
9055 | AssertPtrBreakStmt(pDisk, cbSector = 0);
|
---|
9056 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9057 |
|
---|
9058 | rc2 = vdThreadStartRead(pDisk);
|
---|
9059 | AssertRC(rc2);
|
---|
9060 | fLockRead = true;
|
---|
9061 |
|
---|
9062 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9063 | AssertPtrBreakStmt(pImage, cbSector = 0);
|
---|
9064 | cbSector = pImage->Backend->pfnGetSectorSize(pImage->pBackendData);
|
---|
9065 | } while (0);
|
---|
9066 |
|
---|
9067 | if (RT_UNLIKELY(fLockRead))
|
---|
9068 | {
|
---|
9069 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9070 | AssertRC(rc2);
|
---|
9071 | }
|
---|
9072 |
|
---|
9073 | LogFlowFunc(("returns %u\n", cbSector));
|
---|
9074 | return cbSector;
|
---|
9075 | }
|
---|
9076 |
|
---|
9077 | /**
|
---|
9078 | * Get total capacity of an image in HDD container.
|
---|
9079 | *
|
---|
9080 | * @returns Virtual disk size in bytes.
|
---|
9081 | * @returns 0 if no image with specified number was not opened.
|
---|
9082 | * @param pDisk Pointer to HDD container.
|
---|
9083 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9084 | */
|
---|
9085 | VBOXDDU_DECL(uint64_t) VDGetSize(PVBOXHDD pDisk, unsigned nImage)
|
---|
9086 | {
|
---|
9087 | uint64_t cbSize;
|
---|
9088 | int rc2;
|
---|
9089 | bool fLockRead = false;
|
---|
9090 |
|
---|
9091 | LogFlowFunc(("pDisk=%#p nImage=%u\n", pDisk, nImage));
|
---|
9092 | do
|
---|
9093 | {
|
---|
9094 | /* sanity check */
|
---|
9095 | AssertPtrBreakStmt(pDisk, cbSize = 0);
|
---|
9096 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9097 |
|
---|
9098 | rc2 = vdThreadStartRead(pDisk);
|
---|
9099 | AssertRC(rc2);
|
---|
9100 | fLockRead = true;
|
---|
9101 |
|
---|
9102 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9103 | AssertPtrBreakStmt(pImage, cbSize = 0);
|
---|
9104 | cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
|
---|
9105 | } while (0);
|
---|
9106 |
|
---|
9107 | if (RT_UNLIKELY(fLockRead))
|
---|
9108 | {
|
---|
9109 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9110 | AssertRC(rc2);
|
---|
9111 | }
|
---|
9112 |
|
---|
9113 | LogFlowFunc(("returns %llu\n", cbSize));
|
---|
9114 | return cbSize;
|
---|
9115 | }
|
---|
9116 |
|
---|
9117 | /**
|
---|
9118 | * Get total file size of an image in HDD container.
|
---|
9119 | *
|
---|
9120 | * @returns Virtual disk size in bytes.
|
---|
9121 | * @returns 0 if no image is opened in HDD container.
|
---|
9122 | * @param pDisk Pointer to HDD container.
|
---|
9123 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9124 | */
|
---|
9125 | VBOXDDU_DECL(uint64_t) VDGetFileSize(PVBOXHDD pDisk, unsigned nImage)
|
---|
9126 | {
|
---|
9127 | uint64_t cbSize;
|
---|
9128 | int rc2;
|
---|
9129 | bool fLockRead = false;
|
---|
9130 |
|
---|
9131 | LogFlowFunc(("pDisk=%#p nImage=%u\n", pDisk, nImage));
|
---|
9132 | do
|
---|
9133 | {
|
---|
9134 | /* sanity check */
|
---|
9135 | AssertPtrBreakStmt(pDisk, cbSize = 0);
|
---|
9136 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9137 |
|
---|
9138 | rc2 = vdThreadStartRead(pDisk);
|
---|
9139 | AssertRC(rc2);
|
---|
9140 | fLockRead = true;
|
---|
9141 |
|
---|
9142 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9143 | AssertPtrBreakStmt(pImage, cbSize = 0);
|
---|
9144 | cbSize = pImage->Backend->pfnGetFileSize(pImage->pBackendData);
|
---|
9145 | } while (0);
|
---|
9146 |
|
---|
9147 | if (RT_UNLIKELY(fLockRead))
|
---|
9148 | {
|
---|
9149 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9150 | AssertRC(rc2);
|
---|
9151 | }
|
---|
9152 |
|
---|
9153 | LogFlowFunc(("returns %llu\n", cbSize));
|
---|
9154 | return cbSize;
|
---|
9155 | }
|
---|
9156 |
|
---|
9157 | /**
|
---|
9158 | * Get virtual disk PCHS geometry stored in HDD container.
|
---|
9159 | *
|
---|
9160 | * @returns VBox status code.
|
---|
9161 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9162 | * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
|
---|
9163 | * @param pDisk Pointer to HDD container.
|
---|
9164 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9165 | * @param pPCHSGeometry Where to store PCHS geometry. Not NULL.
|
---|
9166 | */
|
---|
9167 | VBOXDDU_DECL(int) VDGetPCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
|
---|
9168 | PVDGEOMETRY pPCHSGeometry)
|
---|
9169 | {
|
---|
9170 | int rc = VINF_SUCCESS;
|
---|
9171 | int rc2;
|
---|
9172 | bool fLockRead = false;
|
---|
9173 |
|
---|
9174 | LogFlowFunc(("pDisk=%#p nImage=%u pPCHSGeometry=%#p\n",
|
---|
9175 | pDisk, nImage, pPCHSGeometry));
|
---|
9176 | do
|
---|
9177 | {
|
---|
9178 | /* sanity check */
|
---|
9179 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9180 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9181 |
|
---|
9182 | /* Check arguments. */
|
---|
9183 | AssertMsgBreakStmt(VALID_PTR(pPCHSGeometry),
|
---|
9184 | ("pPCHSGeometry=%#p\n", pPCHSGeometry),
|
---|
9185 | rc = VERR_INVALID_PARAMETER);
|
---|
9186 |
|
---|
9187 | rc2 = vdThreadStartRead(pDisk);
|
---|
9188 | AssertRC(rc2);
|
---|
9189 | fLockRead = true;
|
---|
9190 |
|
---|
9191 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9192 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9193 |
|
---|
9194 | if (pImage == pDisk->pLast)
|
---|
9195 | {
|
---|
9196 | /* Use cached information if possible. */
|
---|
9197 | if (pDisk->PCHSGeometry.cCylinders != 0)
|
---|
9198 | *pPCHSGeometry = pDisk->PCHSGeometry;
|
---|
9199 | else
|
---|
9200 | rc = VERR_VD_GEOMETRY_NOT_SET;
|
---|
9201 | }
|
---|
9202 | else
|
---|
9203 | rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
9204 | pPCHSGeometry);
|
---|
9205 | } while (0);
|
---|
9206 |
|
---|
9207 | if (RT_UNLIKELY(fLockRead))
|
---|
9208 | {
|
---|
9209 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9210 | AssertRC(rc2);
|
---|
9211 | }
|
---|
9212 |
|
---|
9213 | LogFlowFunc(("%Rrc (PCHS=%u/%u/%u)\n", rc,
|
---|
9214 | pDisk->PCHSGeometry.cCylinders, pDisk->PCHSGeometry.cHeads,
|
---|
9215 | pDisk->PCHSGeometry.cSectors));
|
---|
9216 | return rc;
|
---|
9217 | }
|
---|
9218 |
|
---|
9219 | /**
|
---|
9220 | * Store virtual disk PCHS geometry in HDD container.
|
---|
9221 | *
|
---|
9222 | * Note that in case of unrecoverable error all images in HDD container will be closed.
|
---|
9223 | *
|
---|
9224 | * @returns VBox status code.
|
---|
9225 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9226 | * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
|
---|
9227 | * @param pDisk Pointer to HDD container.
|
---|
9228 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9229 | * @param pPCHSGeometry Where to load PCHS geometry from. Not NULL.
|
---|
9230 | */
|
---|
9231 | VBOXDDU_DECL(int) VDSetPCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
|
---|
9232 | PCVDGEOMETRY pPCHSGeometry)
|
---|
9233 | {
|
---|
9234 | int rc = VINF_SUCCESS;
|
---|
9235 | int rc2;
|
---|
9236 | bool fLockWrite = false;
|
---|
9237 |
|
---|
9238 | LogFlowFunc(("pDisk=%#p nImage=%u pPCHSGeometry=%#p PCHS=%u/%u/%u\n",
|
---|
9239 | pDisk, nImage, pPCHSGeometry, pPCHSGeometry->cCylinders,
|
---|
9240 | pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
|
---|
9241 | do
|
---|
9242 | {
|
---|
9243 | /* sanity check */
|
---|
9244 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9245 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9246 |
|
---|
9247 | /* Check arguments. */
|
---|
9248 | AssertMsgBreakStmt( VALID_PTR(pPCHSGeometry)
|
---|
9249 | && pPCHSGeometry->cHeads <= 16
|
---|
9250 | && pPCHSGeometry->cSectors <= 63,
|
---|
9251 | ("pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pPCHSGeometry,
|
---|
9252 | pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
|
---|
9253 | pPCHSGeometry->cSectors),
|
---|
9254 | rc = VERR_INVALID_PARAMETER);
|
---|
9255 |
|
---|
9256 | rc2 = vdThreadStartWrite(pDisk);
|
---|
9257 | AssertRC(rc2);
|
---|
9258 | fLockWrite = true;
|
---|
9259 |
|
---|
9260 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9261 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9262 |
|
---|
9263 | if (pImage == pDisk->pLast)
|
---|
9264 | {
|
---|
9265 | if ( pPCHSGeometry->cCylinders != pDisk->PCHSGeometry.cCylinders
|
---|
9266 | || pPCHSGeometry->cHeads != pDisk->PCHSGeometry.cHeads
|
---|
9267 | || pPCHSGeometry->cSectors != pDisk->PCHSGeometry.cSectors)
|
---|
9268 | {
|
---|
9269 | /* Only update geometry if it is changed. Avoids similar checks
|
---|
9270 | * in every backend. Most of the time the new geometry is set
|
---|
9271 | * to the previous values, so no need to go through the hassle
|
---|
9272 | * of updating an image which could be opened in read-only mode
|
---|
9273 | * right now. */
|
---|
9274 | rc = pImage->Backend->pfnSetPCHSGeometry(pImage->pBackendData,
|
---|
9275 | pPCHSGeometry);
|
---|
9276 |
|
---|
9277 | /* Cache new geometry values in any case. */
|
---|
9278 | rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
9279 | &pDisk->PCHSGeometry);
|
---|
9280 | if (RT_FAILURE(rc2))
|
---|
9281 | {
|
---|
9282 | pDisk->PCHSGeometry.cCylinders = 0;
|
---|
9283 | pDisk->PCHSGeometry.cHeads = 0;
|
---|
9284 | pDisk->PCHSGeometry.cSectors = 0;
|
---|
9285 | }
|
---|
9286 | else
|
---|
9287 | {
|
---|
9288 | /* Make sure the CHS geometry is properly clipped. */
|
---|
9289 | pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 255);
|
---|
9290 | pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
|
---|
9291 | }
|
---|
9292 | }
|
---|
9293 | }
|
---|
9294 | else
|
---|
9295 | {
|
---|
9296 | VDGEOMETRY PCHS;
|
---|
9297 | rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
9298 | &PCHS);
|
---|
9299 | if ( RT_FAILURE(rc)
|
---|
9300 | || pPCHSGeometry->cCylinders != PCHS.cCylinders
|
---|
9301 | || pPCHSGeometry->cHeads != PCHS.cHeads
|
---|
9302 | || pPCHSGeometry->cSectors != PCHS.cSectors)
|
---|
9303 | {
|
---|
9304 | /* Only update geometry if it is changed. Avoids similar checks
|
---|
9305 | * in every backend. Most of the time the new geometry is set
|
---|
9306 | * to the previous values, so no need to go through the hassle
|
---|
9307 | * of updating an image which could be opened in read-only mode
|
---|
9308 | * right now. */
|
---|
9309 | rc = pImage->Backend->pfnSetPCHSGeometry(pImage->pBackendData,
|
---|
9310 | pPCHSGeometry);
|
---|
9311 | }
|
---|
9312 | }
|
---|
9313 | } while (0);
|
---|
9314 |
|
---|
9315 | if (RT_UNLIKELY(fLockWrite))
|
---|
9316 | {
|
---|
9317 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
9318 | AssertRC(rc2);
|
---|
9319 | }
|
---|
9320 |
|
---|
9321 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9322 | return rc;
|
---|
9323 | }
|
---|
9324 |
|
---|
9325 | /**
|
---|
9326 | * Get virtual disk LCHS geometry stored in HDD container.
|
---|
9327 | *
|
---|
9328 | * @returns VBox status code.
|
---|
9329 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9330 | * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
|
---|
9331 | * @param pDisk Pointer to HDD container.
|
---|
9332 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9333 | * @param pLCHSGeometry Where to store LCHS geometry. Not NULL.
|
---|
9334 | */
|
---|
9335 | VBOXDDU_DECL(int) VDGetLCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
|
---|
9336 | PVDGEOMETRY pLCHSGeometry)
|
---|
9337 | {
|
---|
9338 | int rc = VINF_SUCCESS;
|
---|
9339 | int rc2;
|
---|
9340 | bool fLockRead = false;
|
---|
9341 |
|
---|
9342 | LogFlowFunc(("pDisk=%#p nImage=%u pLCHSGeometry=%#p\n",
|
---|
9343 | pDisk, nImage, pLCHSGeometry));
|
---|
9344 | do
|
---|
9345 | {
|
---|
9346 | /* sanity check */
|
---|
9347 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9348 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9349 |
|
---|
9350 | /* Check arguments. */
|
---|
9351 | AssertMsgBreakStmt(VALID_PTR(pLCHSGeometry),
|
---|
9352 | ("pLCHSGeometry=%#p\n", pLCHSGeometry),
|
---|
9353 | rc = VERR_INVALID_PARAMETER);
|
---|
9354 |
|
---|
9355 | rc2 = vdThreadStartRead(pDisk);
|
---|
9356 | AssertRC(rc2);
|
---|
9357 | fLockRead = true;
|
---|
9358 |
|
---|
9359 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9360 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9361 |
|
---|
9362 | if (pImage == pDisk->pLast)
|
---|
9363 | {
|
---|
9364 | /* Use cached information if possible. */
|
---|
9365 | if (pDisk->LCHSGeometry.cCylinders != 0)
|
---|
9366 | *pLCHSGeometry = pDisk->LCHSGeometry;
|
---|
9367 | else
|
---|
9368 | rc = VERR_VD_GEOMETRY_NOT_SET;
|
---|
9369 | }
|
---|
9370 | else
|
---|
9371 | rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
9372 | pLCHSGeometry);
|
---|
9373 | } while (0);
|
---|
9374 |
|
---|
9375 | if (RT_UNLIKELY(fLockRead))
|
---|
9376 | {
|
---|
9377 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9378 | AssertRC(rc2);
|
---|
9379 | }
|
---|
9380 |
|
---|
9381 | LogFlowFunc((": %Rrc (LCHS=%u/%u/%u)\n", rc,
|
---|
9382 | pDisk->LCHSGeometry.cCylinders, pDisk->LCHSGeometry.cHeads,
|
---|
9383 | pDisk->LCHSGeometry.cSectors));
|
---|
9384 | return rc;
|
---|
9385 | }
|
---|
9386 |
|
---|
9387 | /**
|
---|
9388 | * Store virtual disk LCHS geometry in HDD container.
|
---|
9389 | *
|
---|
9390 | * Note that in case of unrecoverable error all images in HDD container will be closed.
|
---|
9391 | *
|
---|
9392 | * @returns VBox status code.
|
---|
9393 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9394 | * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
|
---|
9395 | * @param pDisk Pointer to HDD container.
|
---|
9396 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9397 | * @param pLCHSGeometry Where to load LCHS geometry from. Not NULL.
|
---|
9398 | */
|
---|
9399 | VBOXDDU_DECL(int) VDSetLCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
|
---|
9400 | PCVDGEOMETRY pLCHSGeometry)
|
---|
9401 | {
|
---|
9402 | int rc = VINF_SUCCESS;
|
---|
9403 | int rc2;
|
---|
9404 | bool fLockWrite = false;
|
---|
9405 |
|
---|
9406 | LogFlowFunc(("pDisk=%#p nImage=%u pLCHSGeometry=%#p LCHS=%u/%u/%u\n",
|
---|
9407 | pDisk, nImage, pLCHSGeometry, pLCHSGeometry->cCylinders,
|
---|
9408 | pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
|
---|
9409 | do
|
---|
9410 | {
|
---|
9411 | /* sanity check */
|
---|
9412 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9413 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9414 |
|
---|
9415 | /* Check arguments. */
|
---|
9416 | AssertMsgBreakStmt( VALID_PTR(pLCHSGeometry)
|
---|
9417 | && pLCHSGeometry->cHeads <= 255
|
---|
9418 | && pLCHSGeometry->cSectors <= 63,
|
---|
9419 | ("pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pLCHSGeometry,
|
---|
9420 | pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads,
|
---|
9421 | pLCHSGeometry->cSectors),
|
---|
9422 | rc = VERR_INVALID_PARAMETER);
|
---|
9423 |
|
---|
9424 | rc2 = vdThreadStartWrite(pDisk);
|
---|
9425 | AssertRC(rc2);
|
---|
9426 | fLockWrite = true;
|
---|
9427 |
|
---|
9428 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9429 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9430 |
|
---|
9431 | if (pImage == pDisk->pLast)
|
---|
9432 | {
|
---|
9433 | if ( pLCHSGeometry->cCylinders != pDisk->LCHSGeometry.cCylinders
|
---|
9434 | || pLCHSGeometry->cHeads != pDisk->LCHSGeometry.cHeads
|
---|
9435 | || pLCHSGeometry->cSectors != pDisk->LCHSGeometry.cSectors)
|
---|
9436 | {
|
---|
9437 | /* Only update geometry if it is changed. Avoids similar checks
|
---|
9438 | * in every backend. Most of the time the new geometry is set
|
---|
9439 | * to the previous values, so no need to go through the hassle
|
---|
9440 | * of updating an image which could be opened in read-only mode
|
---|
9441 | * right now. */
|
---|
9442 | rc = pImage->Backend->pfnSetLCHSGeometry(pImage->pBackendData,
|
---|
9443 | pLCHSGeometry);
|
---|
9444 |
|
---|
9445 | /* Cache new geometry values in any case. */
|
---|
9446 | rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
9447 | &pDisk->LCHSGeometry);
|
---|
9448 | if (RT_FAILURE(rc2))
|
---|
9449 | {
|
---|
9450 | pDisk->LCHSGeometry.cCylinders = 0;
|
---|
9451 | pDisk->LCHSGeometry.cHeads = 0;
|
---|
9452 | pDisk->LCHSGeometry.cSectors = 0;
|
---|
9453 | }
|
---|
9454 | else
|
---|
9455 | {
|
---|
9456 | /* Make sure the CHS geometry is properly clipped. */
|
---|
9457 | pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
|
---|
9458 | pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
|
---|
9459 | }
|
---|
9460 | }
|
---|
9461 | }
|
---|
9462 | else
|
---|
9463 | {
|
---|
9464 | VDGEOMETRY LCHS;
|
---|
9465 | rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
9466 | &LCHS);
|
---|
9467 | if ( RT_FAILURE(rc)
|
---|
9468 | || pLCHSGeometry->cCylinders != LCHS.cCylinders
|
---|
9469 | || pLCHSGeometry->cHeads != LCHS.cHeads
|
---|
9470 | || pLCHSGeometry->cSectors != LCHS.cSectors)
|
---|
9471 | {
|
---|
9472 | /* Only update geometry if it is changed. Avoids similar checks
|
---|
9473 | * in every backend. Most of the time the new geometry is set
|
---|
9474 | * to the previous values, so no need to go through the hassle
|
---|
9475 | * of updating an image which could be opened in read-only mode
|
---|
9476 | * right now. */
|
---|
9477 | rc = pImage->Backend->pfnSetLCHSGeometry(pImage->pBackendData,
|
---|
9478 | pLCHSGeometry);
|
---|
9479 | }
|
---|
9480 | }
|
---|
9481 | } while (0);
|
---|
9482 |
|
---|
9483 | if (RT_UNLIKELY(fLockWrite))
|
---|
9484 | {
|
---|
9485 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
9486 | AssertRC(rc2);
|
---|
9487 | }
|
---|
9488 |
|
---|
9489 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9490 | return rc;
|
---|
9491 | }
|
---|
9492 |
|
---|
9493 | /**
|
---|
9494 | * Get version of image in HDD container.
|
---|
9495 | *
|
---|
9496 | * @returns VBox status code.
|
---|
9497 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9498 | * @param pDisk Pointer to HDD container.
|
---|
9499 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9500 | * @param puVersion Where to store the image version.
|
---|
9501 | */
|
---|
9502 | VBOXDDU_DECL(int) VDGetVersion(PVBOXHDD pDisk, unsigned nImage,
|
---|
9503 | unsigned *puVersion)
|
---|
9504 | {
|
---|
9505 | int rc = VINF_SUCCESS;
|
---|
9506 | int rc2;
|
---|
9507 | bool fLockRead = false;
|
---|
9508 |
|
---|
9509 | LogFlowFunc(("pDisk=%#p nImage=%u puVersion=%#p\n",
|
---|
9510 | pDisk, nImage, puVersion));
|
---|
9511 | do
|
---|
9512 | {
|
---|
9513 | /* sanity check */
|
---|
9514 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9515 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9516 |
|
---|
9517 | /* Check arguments. */
|
---|
9518 | AssertMsgBreakStmt(VALID_PTR(puVersion),
|
---|
9519 | ("puVersion=%#p\n", puVersion),
|
---|
9520 | rc = VERR_INVALID_PARAMETER);
|
---|
9521 |
|
---|
9522 | rc2 = vdThreadStartRead(pDisk);
|
---|
9523 | AssertRC(rc2);
|
---|
9524 | fLockRead = true;
|
---|
9525 |
|
---|
9526 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9527 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9528 |
|
---|
9529 | *puVersion = pImage->Backend->pfnGetVersion(pImage->pBackendData);
|
---|
9530 | } while (0);
|
---|
9531 |
|
---|
9532 | if (RT_UNLIKELY(fLockRead))
|
---|
9533 | {
|
---|
9534 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9535 | AssertRC(rc2);
|
---|
9536 | }
|
---|
9537 |
|
---|
9538 | LogFlowFunc(("returns %Rrc uVersion=%#x\n", rc, *puVersion));
|
---|
9539 | return rc;
|
---|
9540 | }
|
---|
9541 |
|
---|
9542 | /**
|
---|
9543 | * List the capabilities of image backend in HDD container.
|
---|
9544 | *
|
---|
9545 | * @returns VBox status code.
|
---|
9546 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9547 | * @param pDisk Pointer to the HDD container.
|
---|
9548 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9549 | * @param pbackendInfo Where to store the backend information.
|
---|
9550 | */
|
---|
9551 | VBOXDDU_DECL(int) VDBackendInfoSingle(PVBOXHDD pDisk, unsigned nImage,
|
---|
9552 | PVDBACKENDINFO pBackendInfo)
|
---|
9553 | {
|
---|
9554 | int rc = VINF_SUCCESS;
|
---|
9555 | int rc2;
|
---|
9556 | bool fLockRead = false;
|
---|
9557 |
|
---|
9558 | LogFlowFunc(("pDisk=%#p nImage=%u pBackendInfo=%#p\n",
|
---|
9559 | pDisk, nImage, pBackendInfo));
|
---|
9560 | do
|
---|
9561 | {
|
---|
9562 | /* sanity check */
|
---|
9563 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9564 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9565 |
|
---|
9566 | /* Check arguments. */
|
---|
9567 | AssertMsgBreakStmt(VALID_PTR(pBackendInfo),
|
---|
9568 | ("pBackendInfo=%#p\n", pBackendInfo),
|
---|
9569 | rc = VERR_INVALID_PARAMETER);
|
---|
9570 |
|
---|
9571 | rc2 = vdThreadStartRead(pDisk);
|
---|
9572 | AssertRC(rc2);
|
---|
9573 | fLockRead = true;
|
---|
9574 |
|
---|
9575 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9576 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9577 |
|
---|
9578 | pBackendInfo->pszBackend = pImage->Backend->pszBackendName;
|
---|
9579 | pBackendInfo->uBackendCaps = pImage->Backend->uBackendCaps;
|
---|
9580 | pBackendInfo->paFileExtensions = pImage->Backend->paFileExtensions;
|
---|
9581 | pBackendInfo->paConfigInfo = pImage->Backend->paConfigInfo;
|
---|
9582 | } while (0);
|
---|
9583 |
|
---|
9584 | if (RT_UNLIKELY(fLockRead))
|
---|
9585 | {
|
---|
9586 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9587 | AssertRC(rc2);
|
---|
9588 | }
|
---|
9589 |
|
---|
9590 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9591 | return rc;
|
---|
9592 | }
|
---|
9593 |
|
---|
9594 | /**
|
---|
9595 | * Get flags of image in HDD container.
|
---|
9596 | *
|
---|
9597 | * @returns VBox status code.
|
---|
9598 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9599 | * @param pDisk Pointer to HDD container.
|
---|
9600 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9601 | * @param puImageFlags Where to store the image flags.
|
---|
9602 | */
|
---|
9603 | VBOXDDU_DECL(int) VDGetImageFlags(PVBOXHDD pDisk, unsigned nImage,
|
---|
9604 | unsigned *puImageFlags)
|
---|
9605 | {
|
---|
9606 | int rc = VINF_SUCCESS;
|
---|
9607 | int rc2;
|
---|
9608 | bool fLockRead = false;
|
---|
9609 |
|
---|
9610 | LogFlowFunc(("pDisk=%#p nImage=%u puImageFlags=%#p\n",
|
---|
9611 | pDisk, nImage, puImageFlags));
|
---|
9612 | do
|
---|
9613 | {
|
---|
9614 | /* sanity check */
|
---|
9615 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9616 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9617 |
|
---|
9618 | /* Check arguments. */
|
---|
9619 | AssertMsgBreakStmt(VALID_PTR(puImageFlags),
|
---|
9620 | ("puImageFlags=%#p\n", puImageFlags),
|
---|
9621 | rc = VERR_INVALID_PARAMETER);
|
---|
9622 |
|
---|
9623 | rc2 = vdThreadStartRead(pDisk);
|
---|
9624 | AssertRC(rc2);
|
---|
9625 | fLockRead = true;
|
---|
9626 |
|
---|
9627 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9628 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9629 |
|
---|
9630 | *puImageFlags = pImage->uImageFlags;
|
---|
9631 | } while (0);
|
---|
9632 |
|
---|
9633 | if (RT_UNLIKELY(fLockRead))
|
---|
9634 | {
|
---|
9635 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9636 | AssertRC(rc2);
|
---|
9637 | }
|
---|
9638 |
|
---|
9639 | LogFlowFunc(("returns %Rrc uImageFlags=%#x\n", rc, *puImageFlags));
|
---|
9640 | return rc;
|
---|
9641 | }
|
---|
9642 |
|
---|
9643 | /**
|
---|
9644 | * Get open flags of image in HDD container.
|
---|
9645 | *
|
---|
9646 | * @returns VBox status code.
|
---|
9647 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9648 | * @param pDisk Pointer to HDD container.
|
---|
9649 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9650 | * @param puOpenFlags Where to store the image open flags.
|
---|
9651 | */
|
---|
9652 | VBOXDDU_DECL(int) VDGetOpenFlags(PVBOXHDD pDisk, unsigned nImage,
|
---|
9653 | unsigned *puOpenFlags)
|
---|
9654 | {
|
---|
9655 | int rc = VINF_SUCCESS;
|
---|
9656 | int rc2;
|
---|
9657 | bool fLockRead = false;
|
---|
9658 |
|
---|
9659 | LogFlowFunc(("pDisk=%#p nImage=%u puOpenFlags=%#p\n",
|
---|
9660 | pDisk, nImage, puOpenFlags));
|
---|
9661 | do
|
---|
9662 | {
|
---|
9663 | /* sanity check */
|
---|
9664 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9665 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9666 |
|
---|
9667 | /* Check arguments. */
|
---|
9668 | AssertMsgBreakStmt(VALID_PTR(puOpenFlags),
|
---|
9669 | ("puOpenFlags=%#p\n", puOpenFlags),
|
---|
9670 | rc = VERR_INVALID_PARAMETER);
|
---|
9671 |
|
---|
9672 | rc2 = vdThreadStartRead(pDisk);
|
---|
9673 | AssertRC(rc2);
|
---|
9674 | fLockRead = true;
|
---|
9675 |
|
---|
9676 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9677 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9678 |
|
---|
9679 | *puOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
|
---|
9680 | } while (0);
|
---|
9681 |
|
---|
9682 | if (RT_UNLIKELY(fLockRead))
|
---|
9683 | {
|
---|
9684 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9685 | AssertRC(rc2);
|
---|
9686 | }
|
---|
9687 |
|
---|
9688 | LogFlowFunc(("returns %Rrc uOpenFlags=%#x\n", rc, *puOpenFlags));
|
---|
9689 | return rc;
|
---|
9690 | }
|
---|
9691 |
|
---|
9692 | /**
|
---|
9693 | * Set open flags of image in HDD container.
|
---|
9694 | * This operation may cause file locking changes and/or files being reopened.
|
---|
9695 | * Note that in case of unrecoverable error all images in HDD container will be closed.
|
---|
9696 | *
|
---|
9697 | * @returns VBox status code.
|
---|
9698 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9699 | * @param pDisk Pointer to HDD container.
|
---|
9700 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9701 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
9702 | */
|
---|
9703 | VBOXDDU_DECL(int) VDSetOpenFlags(PVBOXHDD pDisk, unsigned nImage,
|
---|
9704 | unsigned uOpenFlags)
|
---|
9705 | {
|
---|
9706 | int rc;
|
---|
9707 | int rc2;
|
---|
9708 | bool fLockWrite = false;
|
---|
9709 |
|
---|
9710 | LogFlowFunc(("pDisk=%#p uOpenFlags=%#u\n", pDisk, uOpenFlags));
|
---|
9711 | do
|
---|
9712 | {
|
---|
9713 | /* sanity check */
|
---|
9714 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9715 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9716 |
|
---|
9717 | /* Check arguments. */
|
---|
9718 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
9719 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
9720 | rc = VERR_INVALID_PARAMETER);
|
---|
9721 |
|
---|
9722 | rc2 = vdThreadStartWrite(pDisk);
|
---|
9723 | AssertRC(rc2);
|
---|
9724 | fLockWrite = true;
|
---|
9725 |
|
---|
9726 | /* Destroy any discard state because the image might be changed to readonly mode. */
|
---|
9727 | rc = vdDiscardStateDestroy(pDisk);
|
---|
9728 | if (RT_FAILURE(rc))
|
---|
9729 | break;
|
---|
9730 |
|
---|
9731 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9732 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9733 |
|
---|
9734 | rc = pImage->Backend->pfnSetOpenFlags(pImage->pBackendData,
|
---|
9735 | uOpenFlags & ~(VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_IGNORE_FLUSH | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS));
|
---|
9736 | if (RT_SUCCESS(rc))
|
---|
9737 | pImage->uOpenFlags = uOpenFlags & (VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_DISCARD | VD_OPEN_FLAGS_IGNORE_FLUSH | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS);
|
---|
9738 | } while (0);
|
---|
9739 |
|
---|
9740 | if (RT_UNLIKELY(fLockWrite))
|
---|
9741 | {
|
---|
9742 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
9743 | AssertRC(rc2);
|
---|
9744 | }
|
---|
9745 |
|
---|
9746 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9747 | return rc;
|
---|
9748 | }
|
---|
9749 |
|
---|
9750 | /**
|
---|
9751 | * Get base filename of image in HDD container. Some image formats use
|
---|
9752 | * other filenames as well, so don't use this for anything but informational
|
---|
9753 | * purposes.
|
---|
9754 | *
|
---|
9755 | * @returns VBox status code.
|
---|
9756 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9757 | * @returns VERR_BUFFER_OVERFLOW if pszFilename buffer too small to hold filename.
|
---|
9758 | * @param pDisk Pointer to HDD container.
|
---|
9759 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9760 | * @param pszFilename Where to store the image file name.
|
---|
9761 | * @param cbFilename Size of buffer pszFilename points to.
|
---|
9762 | */
|
---|
9763 | VBOXDDU_DECL(int) VDGetFilename(PVBOXHDD pDisk, unsigned nImage,
|
---|
9764 | char *pszFilename, unsigned cbFilename)
|
---|
9765 | {
|
---|
9766 | int rc;
|
---|
9767 | int rc2;
|
---|
9768 | bool fLockRead = false;
|
---|
9769 |
|
---|
9770 | LogFlowFunc(("pDisk=%#p nImage=%u pszFilename=%#p cbFilename=%u\n",
|
---|
9771 | pDisk, nImage, pszFilename, cbFilename));
|
---|
9772 | do
|
---|
9773 | {
|
---|
9774 | /* sanity check */
|
---|
9775 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9776 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9777 |
|
---|
9778 | /* Check arguments. */
|
---|
9779 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
9780 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
9781 | rc = VERR_INVALID_PARAMETER);
|
---|
9782 | AssertMsgBreakStmt(cbFilename,
|
---|
9783 | ("cbFilename=%u\n", cbFilename),
|
---|
9784 | rc = VERR_INVALID_PARAMETER);
|
---|
9785 |
|
---|
9786 | rc2 = vdThreadStartRead(pDisk);
|
---|
9787 | AssertRC(rc2);
|
---|
9788 | fLockRead = true;
|
---|
9789 |
|
---|
9790 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9791 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9792 |
|
---|
9793 | size_t cb = strlen(pImage->pszFilename);
|
---|
9794 | if (cb <= cbFilename)
|
---|
9795 | {
|
---|
9796 | strcpy(pszFilename, pImage->pszFilename);
|
---|
9797 | rc = VINF_SUCCESS;
|
---|
9798 | }
|
---|
9799 | else
|
---|
9800 | {
|
---|
9801 | strncpy(pszFilename, pImage->pszFilename, cbFilename - 1);
|
---|
9802 | pszFilename[cbFilename - 1] = '\0';
|
---|
9803 | rc = VERR_BUFFER_OVERFLOW;
|
---|
9804 | }
|
---|
9805 | } while (0);
|
---|
9806 |
|
---|
9807 | if (RT_UNLIKELY(fLockRead))
|
---|
9808 | {
|
---|
9809 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9810 | AssertRC(rc2);
|
---|
9811 | }
|
---|
9812 |
|
---|
9813 | LogFlowFunc(("returns %Rrc, pszFilename=\"%s\"\n", rc, pszFilename));
|
---|
9814 | return rc;
|
---|
9815 | }
|
---|
9816 |
|
---|
9817 | /**
|
---|
9818 | * Get the comment line of image in HDD container.
|
---|
9819 | *
|
---|
9820 | * @returns VBox status code.
|
---|
9821 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9822 | * @returns VERR_BUFFER_OVERFLOW if pszComment buffer too small to hold comment text.
|
---|
9823 | * @param pDisk Pointer to HDD container.
|
---|
9824 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9825 | * @param pszComment Where to store the comment string of image. NULL is ok.
|
---|
9826 | * @param cbComment The size of pszComment buffer. 0 is ok.
|
---|
9827 | */
|
---|
9828 | VBOXDDU_DECL(int) VDGetComment(PVBOXHDD pDisk, unsigned nImage,
|
---|
9829 | char *pszComment, unsigned cbComment)
|
---|
9830 | {
|
---|
9831 | int rc;
|
---|
9832 | int rc2;
|
---|
9833 | bool fLockRead = false;
|
---|
9834 |
|
---|
9835 | LogFlowFunc(("pDisk=%#p nImage=%u pszComment=%#p cbComment=%u\n",
|
---|
9836 | pDisk, nImage, pszComment, cbComment));
|
---|
9837 | do
|
---|
9838 | {
|
---|
9839 | /* sanity check */
|
---|
9840 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9841 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9842 |
|
---|
9843 | /* Check arguments. */
|
---|
9844 | AssertMsgBreakStmt(VALID_PTR(pszComment),
|
---|
9845 | ("pszComment=%#p \"%s\"\n", pszComment, pszComment),
|
---|
9846 | rc = VERR_INVALID_PARAMETER);
|
---|
9847 | AssertMsgBreakStmt(cbComment,
|
---|
9848 | ("cbComment=%u\n", cbComment),
|
---|
9849 | rc = VERR_INVALID_PARAMETER);
|
---|
9850 |
|
---|
9851 | rc2 = vdThreadStartRead(pDisk);
|
---|
9852 | AssertRC(rc2);
|
---|
9853 | fLockRead = true;
|
---|
9854 |
|
---|
9855 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9856 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9857 |
|
---|
9858 | rc = pImage->Backend->pfnGetComment(pImage->pBackendData, pszComment,
|
---|
9859 | cbComment);
|
---|
9860 | } while (0);
|
---|
9861 |
|
---|
9862 | if (RT_UNLIKELY(fLockRead))
|
---|
9863 | {
|
---|
9864 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9865 | AssertRC(rc2);
|
---|
9866 | }
|
---|
9867 |
|
---|
9868 | LogFlowFunc(("returns %Rrc, pszComment=\"%s\"\n", rc, pszComment));
|
---|
9869 | return rc;
|
---|
9870 | }
|
---|
9871 |
|
---|
9872 | /**
|
---|
9873 | * Changes the comment line of image in HDD container.
|
---|
9874 | *
|
---|
9875 | * @returns VBox status code.
|
---|
9876 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9877 | * @param pDisk Pointer to HDD container.
|
---|
9878 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9879 | * @param pszComment New comment string (UTF-8). NULL is allowed to reset the comment.
|
---|
9880 | */
|
---|
9881 | VBOXDDU_DECL(int) VDSetComment(PVBOXHDD pDisk, unsigned nImage,
|
---|
9882 | const char *pszComment)
|
---|
9883 | {
|
---|
9884 | int rc;
|
---|
9885 | int rc2;
|
---|
9886 | bool fLockWrite = false;
|
---|
9887 |
|
---|
9888 | LogFlowFunc(("pDisk=%#p nImage=%u pszComment=%#p \"%s\"\n",
|
---|
9889 | pDisk, nImage, pszComment, pszComment));
|
---|
9890 | do
|
---|
9891 | {
|
---|
9892 | /* sanity check */
|
---|
9893 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9894 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9895 |
|
---|
9896 | /* Check arguments. */
|
---|
9897 | AssertMsgBreakStmt(VALID_PTR(pszComment) || pszComment == NULL,
|
---|
9898 | ("pszComment=%#p \"%s\"\n", pszComment, pszComment),
|
---|
9899 | rc = VERR_INVALID_PARAMETER);
|
---|
9900 |
|
---|
9901 | rc2 = vdThreadStartWrite(pDisk);
|
---|
9902 | AssertRC(rc2);
|
---|
9903 | fLockWrite = true;
|
---|
9904 |
|
---|
9905 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9906 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9907 |
|
---|
9908 | rc = pImage->Backend->pfnSetComment(pImage->pBackendData, pszComment);
|
---|
9909 | } while (0);
|
---|
9910 |
|
---|
9911 | if (RT_UNLIKELY(fLockWrite))
|
---|
9912 | {
|
---|
9913 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
9914 | AssertRC(rc2);
|
---|
9915 | }
|
---|
9916 |
|
---|
9917 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9918 | return rc;
|
---|
9919 | }
|
---|
9920 |
|
---|
9921 |
|
---|
9922 | /**
|
---|
9923 | * Get UUID of image in HDD container.
|
---|
9924 | *
|
---|
9925 | * @returns VBox status code.
|
---|
9926 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9927 | * @param pDisk Pointer to HDD container.
|
---|
9928 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9929 | * @param pUuid Where to store the image creation UUID.
|
---|
9930 | */
|
---|
9931 | VBOXDDU_DECL(int) VDGetUuid(PVBOXHDD pDisk, unsigned nImage, PRTUUID pUuid)
|
---|
9932 | {
|
---|
9933 | int rc;
|
---|
9934 | int rc2;
|
---|
9935 | bool fLockRead = false;
|
---|
9936 |
|
---|
9937 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
|
---|
9938 | do
|
---|
9939 | {
|
---|
9940 | /* sanity check */
|
---|
9941 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9942 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9943 |
|
---|
9944 | /* Check arguments. */
|
---|
9945 | AssertMsgBreakStmt(VALID_PTR(pUuid),
|
---|
9946 | ("pUuid=%#p\n", pUuid),
|
---|
9947 | rc = VERR_INVALID_PARAMETER);
|
---|
9948 |
|
---|
9949 | rc2 = vdThreadStartRead(pDisk);
|
---|
9950 | AssertRC(rc2);
|
---|
9951 | fLockRead = true;
|
---|
9952 |
|
---|
9953 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9954 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9955 |
|
---|
9956 | rc = pImage->Backend->pfnGetUuid(pImage->pBackendData, pUuid);
|
---|
9957 | } while (0);
|
---|
9958 |
|
---|
9959 | if (RT_UNLIKELY(fLockRead))
|
---|
9960 | {
|
---|
9961 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9962 | AssertRC(rc2);
|
---|
9963 | }
|
---|
9964 |
|
---|
9965 | LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
|
---|
9966 | return rc;
|
---|
9967 | }
|
---|
9968 |
|
---|
9969 | /**
|
---|
9970 | * Set the image's UUID. Should not be used by normal applications.
|
---|
9971 | *
|
---|
9972 | * @returns VBox status code.
|
---|
9973 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9974 | * @param pDisk Pointer to HDD container.
|
---|
9975 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9976 | * @param pUuid New UUID of the image. If NULL, a new UUID is created.
|
---|
9977 | */
|
---|
9978 | VBOXDDU_DECL(int) VDSetUuid(PVBOXHDD pDisk, unsigned nImage, PCRTUUID pUuid)
|
---|
9979 | {
|
---|
9980 | int rc;
|
---|
9981 | int rc2;
|
---|
9982 | bool fLockWrite = false;
|
---|
9983 |
|
---|
9984 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
|
---|
9985 | pDisk, nImage, pUuid, pUuid));
|
---|
9986 | do
|
---|
9987 | {
|
---|
9988 | /* sanity check */
|
---|
9989 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9990 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9991 |
|
---|
9992 | AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
|
---|
9993 | ("pUuid=%#p\n", pUuid),
|
---|
9994 | rc = VERR_INVALID_PARAMETER);
|
---|
9995 |
|
---|
9996 | rc2 = vdThreadStartWrite(pDisk);
|
---|
9997 | AssertRC(rc2);
|
---|
9998 | fLockWrite = true;
|
---|
9999 |
|
---|
10000 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
10001 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
10002 |
|
---|
10003 | RTUUID Uuid;
|
---|
10004 | if (!pUuid)
|
---|
10005 | {
|
---|
10006 | RTUuidCreate(&Uuid);
|
---|
10007 | pUuid = &Uuid;
|
---|
10008 | }
|
---|
10009 | rc = pImage->Backend->pfnSetUuid(pImage->pBackendData, pUuid);
|
---|
10010 | } while (0);
|
---|
10011 |
|
---|
10012 | if (RT_UNLIKELY(fLockWrite))
|
---|
10013 | {
|
---|
10014 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
10015 | AssertRC(rc2);
|
---|
10016 | }
|
---|
10017 |
|
---|
10018 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
10019 | return rc;
|
---|
10020 | }
|
---|
10021 |
|
---|
10022 | /**
|
---|
10023 | * Get last modification UUID of image in HDD container.
|
---|
10024 | *
|
---|
10025 | * @returns VBox status code.
|
---|
10026 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
10027 | * @param pDisk Pointer to HDD container.
|
---|
10028 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
10029 | * @param pUuid Where to store the image modification UUID.
|
---|
10030 | */
|
---|
10031 | VBOXDDU_DECL(int) VDGetModificationUuid(PVBOXHDD pDisk, unsigned nImage, PRTUUID pUuid)
|
---|
10032 | {
|
---|
10033 | int rc = VINF_SUCCESS;
|
---|
10034 | int rc2;
|
---|
10035 | bool fLockRead = false;
|
---|
10036 |
|
---|
10037 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
|
---|
10038 | do
|
---|
10039 | {
|
---|
10040 | /* sanity check */
|
---|
10041 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10042 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10043 |
|
---|
10044 | /* Check arguments. */
|
---|
10045 | AssertMsgBreakStmt(VALID_PTR(pUuid),
|
---|
10046 | ("pUuid=%#p\n", pUuid),
|
---|
10047 | rc = VERR_INVALID_PARAMETER);
|
---|
10048 |
|
---|
10049 | rc2 = vdThreadStartRead(pDisk);
|
---|
10050 | AssertRC(rc2);
|
---|
10051 | fLockRead = true;
|
---|
10052 |
|
---|
10053 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
10054 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
10055 |
|
---|
10056 | rc = pImage->Backend->pfnGetModificationUuid(pImage->pBackendData,
|
---|
10057 | pUuid);
|
---|
10058 | } while (0);
|
---|
10059 |
|
---|
10060 | if (RT_UNLIKELY(fLockRead))
|
---|
10061 | {
|
---|
10062 | rc2 = vdThreadFinishRead(pDisk);
|
---|
10063 | AssertRC(rc2);
|
---|
10064 | }
|
---|
10065 |
|
---|
10066 | LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
|
---|
10067 | return rc;
|
---|
10068 | }
|
---|
10069 |
|
---|
10070 | /**
|
---|
10071 | * Set the image's last modification UUID. Should not be used by normal applications.
|
---|
10072 | *
|
---|
10073 | * @returns VBox status code.
|
---|
10074 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
10075 | * @param pDisk Pointer to HDD container.
|
---|
10076 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
10077 | * @param pUuid New modification UUID of the image. If NULL, a new UUID is created.
|
---|
10078 | */
|
---|
10079 | VBOXDDU_DECL(int) VDSetModificationUuid(PVBOXHDD pDisk, unsigned nImage, PCRTUUID pUuid)
|
---|
10080 | {
|
---|
10081 | int rc;
|
---|
10082 | int rc2;
|
---|
10083 | bool fLockWrite = false;
|
---|
10084 |
|
---|
10085 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
|
---|
10086 | pDisk, nImage, pUuid, pUuid));
|
---|
10087 | do
|
---|
10088 | {
|
---|
10089 | /* sanity check */
|
---|
10090 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10091 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10092 |
|
---|
10093 | /* Check arguments. */
|
---|
10094 | AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
|
---|
10095 | ("pUuid=%#p\n", pUuid),
|
---|
10096 | rc = VERR_INVALID_PARAMETER);
|
---|
10097 |
|
---|
10098 | rc2 = vdThreadStartWrite(pDisk);
|
---|
10099 | AssertRC(rc2);
|
---|
10100 | fLockWrite = true;
|
---|
10101 |
|
---|
10102 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
10103 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
10104 |
|
---|
10105 | RTUUID Uuid;
|
---|
10106 | if (!pUuid)
|
---|
10107 | {
|
---|
10108 | RTUuidCreate(&Uuid);
|
---|
10109 | pUuid = &Uuid;
|
---|
10110 | }
|
---|
10111 | rc = pImage->Backend->pfnSetModificationUuid(pImage->pBackendData,
|
---|
10112 | pUuid);
|
---|
10113 | } while (0);
|
---|
10114 |
|
---|
10115 | if (RT_UNLIKELY(fLockWrite))
|
---|
10116 | {
|
---|
10117 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
10118 | AssertRC(rc2);
|
---|
10119 | }
|
---|
10120 |
|
---|
10121 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
10122 | return rc;
|
---|
10123 | }
|
---|
10124 |
|
---|
10125 | /**
|
---|
10126 | * Get parent UUID of image in HDD container.
|
---|
10127 | *
|
---|
10128 | * @returns VBox status code.
|
---|
10129 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
10130 | * @param pDisk Pointer to HDD container.
|
---|
10131 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
10132 | * @param pUuid Where to store the parent image UUID.
|
---|
10133 | */
|
---|
10134 | VBOXDDU_DECL(int) VDGetParentUuid(PVBOXHDD pDisk, unsigned nImage,
|
---|
10135 | PRTUUID pUuid)
|
---|
10136 | {
|
---|
10137 | int rc = VINF_SUCCESS;
|
---|
10138 | int rc2;
|
---|
10139 | bool fLockRead = false;
|
---|
10140 |
|
---|
10141 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
|
---|
10142 | do
|
---|
10143 | {
|
---|
10144 | /* sanity check */
|
---|
10145 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10146 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10147 |
|
---|
10148 | /* Check arguments. */
|
---|
10149 | AssertMsgBreakStmt(VALID_PTR(pUuid),
|
---|
10150 | ("pUuid=%#p\n", pUuid),
|
---|
10151 | rc = VERR_INVALID_PARAMETER);
|
---|
10152 |
|
---|
10153 | rc2 = vdThreadStartRead(pDisk);
|
---|
10154 | AssertRC(rc2);
|
---|
10155 | fLockRead = true;
|
---|
10156 |
|
---|
10157 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
10158 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
10159 |
|
---|
10160 | rc = pImage->Backend->pfnGetParentUuid(pImage->pBackendData, pUuid);
|
---|
10161 | } while (0);
|
---|
10162 |
|
---|
10163 | if (RT_UNLIKELY(fLockRead))
|
---|
10164 | {
|
---|
10165 | rc2 = vdThreadFinishRead(pDisk);
|
---|
10166 | AssertRC(rc2);
|
---|
10167 | }
|
---|
10168 |
|
---|
10169 | LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
|
---|
10170 | return rc;
|
---|
10171 | }
|
---|
10172 |
|
---|
10173 | /**
|
---|
10174 | * Set the image's parent UUID. Should not be used by normal applications.
|
---|
10175 | *
|
---|
10176 | * @returns VBox status code.
|
---|
10177 | * @param pDisk Pointer to HDD container.
|
---|
10178 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
10179 | * @param pUuid New parent UUID of the image. If NULL, a new UUID is created.
|
---|
10180 | */
|
---|
10181 | VBOXDDU_DECL(int) VDSetParentUuid(PVBOXHDD pDisk, unsigned nImage,
|
---|
10182 | PCRTUUID pUuid)
|
---|
10183 | {
|
---|
10184 | int rc;
|
---|
10185 | int rc2;
|
---|
10186 | bool fLockWrite = false;
|
---|
10187 |
|
---|
10188 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
|
---|
10189 | pDisk, nImage, pUuid, pUuid));
|
---|
10190 | do
|
---|
10191 | {
|
---|
10192 | /* sanity check */
|
---|
10193 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10194 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10195 |
|
---|
10196 | /* Check arguments. */
|
---|
10197 | AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
|
---|
10198 | ("pUuid=%#p\n", pUuid),
|
---|
10199 | rc = VERR_INVALID_PARAMETER);
|
---|
10200 |
|
---|
10201 | rc2 = vdThreadStartWrite(pDisk);
|
---|
10202 | AssertRC(rc2);
|
---|
10203 | fLockWrite = true;
|
---|
10204 |
|
---|
10205 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
10206 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
10207 |
|
---|
10208 | RTUUID Uuid;
|
---|
10209 | if (!pUuid)
|
---|
10210 | {
|
---|
10211 | RTUuidCreate(&Uuid);
|
---|
10212 | pUuid = &Uuid;
|
---|
10213 | }
|
---|
10214 | rc = pImage->Backend->pfnSetParentUuid(pImage->pBackendData, pUuid);
|
---|
10215 | } while (0);
|
---|
10216 |
|
---|
10217 | if (RT_UNLIKELY(fLockWrite))
|
---|
10218 | {
|
---|
10219 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
10220 | AssertRC(rc2);
|
---|
10221 | }
|
---|
10222 |
|
---|
10223 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
10224 | return rc;
|
---|
10225 | }
|
---|
10226 |
|
---|
10227 |
|
---|
10228 | /**
|
---|
10229 | * Debug helper - dumps all opened images in HDD container into the log file.
|
---|
10230 | *
|
---|
10231 | * @param pDisk Pointer to HDD container.
|
---|
10232 | */
|
---|
10233 | VBOXDDU_DECL(void) VDDumpImages(PVBOXHDD pDisk)
|
---|
10234 | {
|
---|
10235 | int rc2;
|
---|
10236 | bool fLockRead = false;
|
---|
10237 |
|
---|
10238 | do
|
---|
10239 | {
|
---|
10240 | /* sanity check */
|
---|
10241 | AssertPtrBreak(pDisk);
|
---|
10242 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10243 |
|
---|
10244 | if (!pDisk->pInterfaceError || !VALID_PTR(pDisk->pInterfaceError->pfnMessage))
|
---|
10245 | pDisk->pInterfaceError->pfnMessage = vdLogMessage;
|
---|
10246 |
|
---|
10247 | rc2 = vdThreadStartRead(pDisk);
|
---|
10248 | AssertRC(rc2);
|
---|
10249 | fLockRead = true;
|
---|
10250 |
|
---|
10251 | vdMessageWrapper(pDisk, "--- Dumping VD Disk, Images=%u\n", pDisk->cImages);
|
---|
10252 | for (PVDIMAGE pImage = pDisk->pBase; pImage; pImage = pImage->pNext)
|
---|
10253 | {
|
---|
10254 | vdMessageWrapper(pDisk, "Dumping VD image \"%s\" (Backend=%s)\n",
|
---|
10255 | pImage->pszFilename, pImage->Backend->pszBackendName);
|
---|
10256 | pImage->Backend->pfnDump(pImage->pBackendData);
|
---|
10257 | }
|
---|
10258 | } while (0);
|
---|
10259 |
|
---|
10260 | if (RT_UNLIKELY(fLockRead))
|
---|
10261 | {
|
---|
10262 | rc2 = vdThreadFinishRead(pDisk);
|
---|
10263 | AssertRC(rc2);
|
---|
10264 | }
|
---|
10265 | }
|
---|
10266 |
|
---|
10267 |
|
---|
10268 | VBOXDDU_DECL(int) VDDiscardRanges(PVBOXHDD pDisk, PCRTRANGE paRanges, unsigned cRanges)
|
---|
10269 | {
|
---|
10270 | int rc;
|
---|
10271 | int rc2;
|
---|
10272 | bool fLockWrite = false;
|
---|
10273 |
|
---|
10274 | LogFlowFunc(("pDisk=%#p paRanges=%#p cRanges=%u\n",
|
---|
10275 | pDisk, paRanges, cRanges));
|
---|
10276 | do
|
---|
10277 | {
|
---|
10278 | /* sanity check */
|
---|
10279 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10280 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10281 |
|
---|
10282 | /* Check arguments. */
|
---|
10283 | AssertMsgBreakStmt(cRanges,
|
---|
10284 | ("cRanges=%u\n", cRanges),
|
---|
10285 | rc = VERR_INVALID_PARAMETER);
|
---|
10286 | AssertMsgBreakStmt(VALID_PTR(paRanges),
|
---|
10287 | ("paRanges=%#p\n", paRanges),
|
---|
10288 | rc = VERR_INVALID_PARAMETER);
|
---|
10289 |
|
---|
10290 | rc2 = vdThreadStartWrite(pDisk);
|
---|
10291 | AssertRC(rc2);
|
---|
10292 | fLockWrite = true;
|
---|
10293 |
|
---|
10294 | AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
|
---|
10295 |
|
---|
10296 | AssertMsgBreakStmt(pDisk->pLast->uOpenFlags & VD_OPEN_FLAGS_DISCARD,
|
---|
10297 | ("Discarding not supported\n"),
|
---|
10298 | rc = VERR_NOT_SUPPORTED);
|
---|
10299 |
|
---|
10300 | VDIOCTX IoCtx;
|
---|
10301 | RTSEMEVENT hEventComplete = NIL_RTSEMEVENT;
|
---|
10302 |
|
---|
10303 | rc = RTSemEventCreate(&hEventComplete);
|
---|
10304 | if (RT_FAILURE(rc))
|
---|
10305 | break;
|
---|
10306 |
|
---|
10307 | vdIoCtxDiscardInit(&IoCtx, pDisk, paRanges, cRanges,
|
---|
10308 | vdIoCtxSyncComplete, pDisk, hEventComplete, NULL,
|
---|
10309 | vdDiscardHelperAsync, VDIOCTX_FLAGS_SYNC | VDIOCTX_FLAGS_DONT_FREE);
|
---|
10310 | rc = vdIoCtxProcessSync(&IoCtx, hEventComplete);
|
---|
10311 |
|
---|
10312 | RTSemEventDestroy(hEventComplete);
|
---|
10313 | } while (0);
|
---|
10314 |
|
---|
10315 | if (RT_UNLIKELY(fLockWrite))
|
---|
10316 | {
|
---|
10317 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
10318 | AssertRC(rc2);
|
---|
10319 | }
|
---|
10320 |
|
---|
10321 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
10322 | return rc;
|
---|
10323 | }
|
---|
10324 |
|
---|
10325 |
|
---|
10326 | VBOXDDU_DECL(int) VDAsyncRead(PVBOXHDD pDisk, uint64_t uOffset, size_t cbRead,
|
---|
10327 | PCRTSGBUF pcSgBuf,
|
---|
10328 | PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
10329 | void *pvUser1, void *pvUser2)
|
---|
10330 | {
|
---|
10331 | int rc = VERR_VD_BLOCK_FREE;
|
---|
10332 | int rc2;
|
---|
10333 | bool fLockRead = false;
|
---|
10334 | PVDIOCTX pIoCtx = NULL;
|
---|
10335 |
|
---|
10336 | LogFlowFunc(("pDisk=%#p uOffset=%llu pcSgBuf=%#p cbRead=%zu pvUser1=%#p pvUser2=%#p\n",
|
---|
10337 | pDisk, uOffset, pcSgBuf, cbRead, pvUser1, pvUser2));
|
---|
10338 |
|
---|
10339 | do
|
---|
10340 | {
|
---|
10341 | /* sanity check */
|
---|
10342 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10343 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10344 |
|
---|
10345 | /* Check arguments. */
|
---|
10346 | AssertMsgBreakStmt(cbRead,
|
---|
10347 | ("cbRead=%zu\n", cbRead),
|
---|
10348 | rc = VERR_INVALID_PARAMETER);
|
---|
10349 | AssertMsgBreakStmt(VALID_PTR(pcSgBuf),
|
---|
10350 | ("pcSgBuf=%#p\n", pcSgBuf),
|
---|
10351 | rc = VERR_INVALID_PARAMETER);
|
---|
10352 |
|
---|
10353 | rc2 = vdThreadStartRead(pDisk);
|
---|
10354 | AssertRC(rc2);
|
---|
10355 | fLockRead = true;
|
---|
10356 |
|
---|
10357 | AssertMsgBreakStmt(uOffset + cbRead <= pDisk->cbSize,
|
---|
10358 | ("uOffset=%llu cbRead=%zu pDisk->cbSize=%llu\n",
|
---|
10359 | uOffset, cbRead, pDisk->cbSize),
|
---|
10360 | rc = VERR_INVALID_PARAMETER);
|
---|
10361 | AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
|
---|
10362 |
|
---|
10363 | pIoCtx = vdIoCtxRootAlloc(pDisk, VDIOCTXTXDIR_READ, uOffset,
|
---|
10364 | cbRead, pDisk->pLast, pcSgBuf,
|
---|
10365 | pfnComplete, pvUser1, pvUser2,
|
---|
10366 | NULL, vdReadHelperAsync,
|
---|
10367 | VDIOCTX_FLAGS_ZERO_FREE_BLOCKS);
|
---|
10368 | if (!pIoCtx)
|
---|
10369 | {
|
---|
10370 | rc = VERR_NO_MEMORY;
|
---|
10371 | break;
|
---|
10372 | }
|
---|
10373 |
|
---|
10374 | rc = vdIoCtxProcessTryLockDefer(pIoCtx);
|
---|
10375 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
10376 | {
|
---|
10377 | if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
|
---|
10378 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
10379 | else
|
---|
10380 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
|
---|
10381 | }
|
---|
10382 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
|
---|
10383 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
10384 |
|
---|
10385 | } while (0);
|
---|
10386 |
|
---|
10387 | if (RT_UNLIKELY(fLockRead) && ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
10388 | || rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
10389 | {
|
---|
10390 | rc2 = vdThreadFinishRead(pDisk);
|
---|
10391 | AssertRC(rc2);
|
---|
10392 | }
|
---|
10393 |
|
---|
10394 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
10395 | return rc;
|
---|
10396 | }
|
---|
10397 |
|
---|
10398 |
|
---|
10399 | VBOXDDU_DECL(int) VDAsyncWrite(PVBOXHDD pDisk, uint64_t uOffset, size_t cbWrite,
|
---|
10400 | PCRTSGBUF pcSgBuf,
|
---|
10401 | PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
10402 | void *pvUser1, void *pvUser2)
|
---|
10403 | {
|
---|
10404 | int rc;
|
---|
10405 | int rc2;
|
---|
10406 | bool fLockWrite = false;
|
---|
10407 | PVDIOCTX pIoCtx = NULL;
|
---|
10408 |
|
---|
10409 | LogFlowFunc(("pDisk=%#p uOffset=%llu cSgBuf=%#p cbWrite=%zu pvUser1=%#p pvUser2=%#p\n",
|
---|
10410 | pDisk, uOffset, pcSgBuf, cbWrite, pvUser1, pvUser2));
|
---|
10411 | do
|
---|
10412 | {
|
---|
10413 | /* sanity check */
|
---|
10414 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10415 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10416 |
|
---|
10417 | /* Check arguments. */
|
---|
10418 | AssertMsgBreakStmt(cbWrite,
|
---|
10419 | ("cbWrite=%zu\n", cbWrite),
|
---|
10420 | rc = VERR_INVALID_PARAMETER);
|
---|
10421 | AssertMsgBreakStmt(VALID_PTR(pcSgBuf),
|
---|
10422 | ("pcSgBuf=%#p\n", pcSgBuf),
|
---|
10423 | rc = VERR_INVALID_PARAMETER);
|
---|
10424 |
|
---|
10425 | rc2 = vdThreadStartWrite(pDisk);
|
---|
10426 | AssertRC(rc2);
|
---|
10427 | fLockWrite = true;
|
---|
10428 |
|
---|
10429 | AssertMsgBreakStmt(uOffset + cbWrite <= pDisk->cbSize,
|
---|
10430 | ("uOffset=%llu cbWrite=%zu pDisk->cbSize=%llu\n",
|
---|
10431 | uOffset, cbWrite, pDisk->cbSize),
|
---|
10432 | rc = VERR_INVALID_PARAMETER);
|
---|
10433 | AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
|
---|
10434 |
|
---|
10435 | pIoCtx = vdIoCtxRootAlloc(pDisk, VDIOCTXTXDIR_WRITE, uOffset,
|
---|
10436 | cbWrite, pDisk->pLast, pcSgBuf,
|
---|
10437 | pfnComplete, pvUser1, pvUser2,
|
---|
10438 | NULL, vdWriteHelperAsync,
|
---|
10439 | VDIOCTX_FLAGS_DEFAULT);
|
---|
10440 | if (!pIoCtx)
|
---|
10441 | {
|
---|
10442 | rc = VERR_NO_MEMORY;
|
---|
10443 | break;
|
---|
10444 | }
|
---|
10445 |
|
---|
10446 | rc = vdIoCtxProcessTryLockDefer(pIoCtx);
|
---|
10447 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
10448 | {
|
---|
10449 | if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
|
---|
10450 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
10451 | else
|
---|
10452 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
|
---|
10453 | }
|
---|
10454 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
|
---|
10455 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
10456 | } while (0);
|
---|
10457 |
|
---|
10458 | if (RT_UNLIKELY(fLockWrite) && ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
10459 | || rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
10460 | {
|
---|
10461 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
10462 | AssertRC(rc2);
|
---|
10463 | }
|
---|
10464 |
|
---|
10465 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
10466 | return rc;
|
---|
10467 | }
|
---|
10468 |
|
---|
10469 |
|
---|
10470 | VBOXDDU_DECL(int) VDAsyncFlush(PVBOXHDD pDisk, PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
10471 | void *pvUser1, void *pvUser2)
|
---|
10472 | {
|
---|
10473 | int rc;
|
---|
10474 | int rc2;
|
---|
10475 | bool fLockWrite = false;
|
---|
10476 | PVDIOCTX pIoCtx = NULL;
|
---|
10477 |
|
---|
10478 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
10479 |
|
---|
10480 | do
|
---|
10481 | {
|
---|
10482 | /* sanity check */
|
---|
10483 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10484 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10485 |
|
---|
10486 | rc2 = vdThreadStartWrite(pDisk);
|
---|
10487 | AssertRC(rc2);
|
---|
10488 | fLockWrite = true;
|
---|
10489 |
|
---|
10490 | AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
|
---|
10491 |
|
---|
10492 | pIoCtx = vdIoCtxRootAlloc(pDisk, VDIOCTXTXDIR_FLUSH, 0,
|
---|
10493 | 0, pDisk->pLast, NULL,
|
---|
10494 | pfnComplete, pvUser1, pvUser2,
|
---|
10495 | NULL, vdFlushHelperAsync,
|
---|
10496 | VDIOCTX_FLAGS_DEFAULT);
|
---|
10497 | if (!pIoCtx)
|
---|
10498 | {
|
---|
10499 | rc = VERR_NO_MEMORY;
|
---|
10500 | break;
|
---|
10501 | }
|
---|
10502 |
|
---|
10503 | rc = vdIoCtxProcessTryLockDefer(pIoCtx);
|
---|
10504 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
10505 | {
|
---|
10506 | if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
|
---|
10507 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
10508 | else
|
---|
10509 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
|
---|
10510 | }
|
---|
10511 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
|
---|
10512 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
10513 | } while (0);
|
---|
10514 |
|
---|
10515 | if (RT_UNLIKELY(fLockWrite) && ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
10516 | || rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
10517 | {
|
---|
10518 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
10519 | AssertRC(rc2);
|
---|
10520 | }
|
---|
10521 |
|
---|
10522 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
10523 | return rc;
|
---|
10524 | }
|
---|
10525 |
|
---|
10526 | VBOXDDU_DECL(int) VDAsyncDiscardRanges(PVBOXHDD pDisk, PCRTRANGE paRanges, unsigned cRanges,
|
---|
10527 | PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
10528 | void *pvUser1, void *pvUser2)
|
---|
10529 | {
|
---|
10530 | int rc;
|
---|
10531 | int rc2;
|
---|
10532 | bool fLockWrite = false;
|
---|
10533 | PVDIOCTX pIoCtx = NULL;
|
---|
10534 |
|
---|
10535 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
10536 |
|
---|
10537 | do
|
---|
10538 | {
|
---|
10539 | /* sanity check */
|
---|
10540 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
10541 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
10542 |
|
---|
10543 | rc2 = vdThreadStartWrite(pDisk);
|
---|
10544 | AssertRC(rc2);
|
---|
10545 | fLockWrite = true;
|
---|
10546 |
|
---|
10547 | AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
|
---|
10548 |
|
---|
10549 | pIoCtx = vdIoCtxDiscardAlloc(pDisk, paRanges, cRanges,
|
---|
10550 | pfnComplete, pvUser1, pvUser2, NULL,
|
---|
10551 | vdDiscardHelperAsync,
|
---|
10552 | VDIOCTX_FLAGS_DEFAULT);
|
---|
10553 | if (!pIoCtx)
|
---|
10554 | {
|
---|
10555 | rc = VERR_NO_MEMORY;
|
---|
10556 | break;
|
---|
10557 | }
|
---|
10558 |
|
---|
10559 | rc = vdIoCtxProcessTryLockDefer(pIoCtx);
|
---|
10560 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
10561 | {
|
---|
10562 | if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
|
---|
10563 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
10564 | else
|
---|
10565 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
|
---|
10566 | }
|
---|
10567 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
|
---|
10568 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
10569 | } while (0);
|
---|
10570 |
|
---|
10571 | if (RT_UNLIKELY(fLockWrite) && ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
10572 | || rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
10573 | {
|
---|
10574 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
10575 | AssertRC(rc2);
|
---|
10576 | }
|
---|
10577 |
|
---|
10578 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
10579 | return rc;
|
---|
10580 | }
|
---|
10581 |
|
---|
10582 | VBOXDDU_DECL(int) VDRepair(PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
|
---|
10583 | const char *pszFilename, const char *pszBackend,
|
---|
10584 | uint32_t fFlags)
|
---|
10585 | {
|
---|
10586 | int rc = VERR_NOT_SUPPORTED;
|
---|
10587 | PCVBOXHDDBACKEND pBackend = NULL;
|
---|
10588 | VDINTERFACEIOINT VDIfIoInt;
|
---|
10589 | VDINTERFACEIO VDIfIoFallback;
|
---|
10590 | PVDINTERFACEIO pInterfaceIo;
|
---|
10591 |
|
---|
10592 | LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
|
---|
10593 | /* Check arguments. */
|
---|
10594 | AssertMsgReturn(VALID_PTR(pszFilename) && *pszFilename,
|
---|
10595 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
10596 | VERR_INVALID_PARAMETER);
|
---|
10597 | AssertMsgReturn(VALID_PTR(pszBackend),
|
---|
10598 | ("pszBackend=%#p\n", pszBackend),
|
---|
10599 | VERR_INVALID_PARAMETER);
|
---|
10600 | AssertMsgReturn((fFlags & ~VD_REPAIR_FLAGS_MASK) == 0,
|
---|
10601 | ("fFlags=%#x\n", fFlags),
|
---|
10602 | VERR_INVALID_PARAMETER);
|
---|
10603 |
|
---|
10604 | pInterfaceIo = VDIfIoGet(pVDIfsImage);
|
---|
10605 | if (!pInterfaceIo)
|
---|
10606 | {
|
---|
10607 | /*
|
---|
10608 | * Caller doesn't provide an I/O interface, create our own using the
|
---|
10609 | * native file API.
|
---|
10610 | */
|
---|
10611 | vdIfIoFallbackCallbacksSetup(&VDIfIoFallback);
|
---|
10612 | pInterfaceIo = &VDIfIoFallback;
|
---|
10613 | }
|
---|
10614 |
|
---|
10615 | /* Set up the internal I/O interface. */
|
---|
10616 | AssertReturn(!VDIfIoIntGet(pVDIfsImage), VERR_INVALID_PARAMETER);
|
---|
10617 | VDIfIoInt.pfnOpen = vdIOIntOpenLimited;
|
---|
10618 | VDIfIoInt.pfnClose = vdIOIntCloseLimited;
|
---|
10619 | VDIfIoInt.pfnDelete = vdIOIntDeleteLimited;
|
---|
10620 | VDIfIoInt.pfnMove = vdIOIntMoveLimited;
|
---|
10621 | VDIfIoInt.pfnGetFreeSpace = vdIOIntGetFreeSpaceLimited;
|
---|
10622 | VDIfIoInt.pfnGetModificationTime = vdIOIntGetModificationTimeLimited;
|
---|
10623 | VDIfIoInt.pfnGetSize = vdIOIntGetSizeLimited;
|
---|
10624 | VDIfIoInt.pfnSetSize = vdIOIntSetSizeLimited;
|
---|
10625 | VDIfIoInt.pfnReadUser = vdIOIntReadUserLimited;
|
---|
10626 | VDIfIoInt.pfnWriteUser = vdIOIntWriteUserLimited;
|
---|
10627 | VDIfIoInt.pfnReadMeta = vdIOIntReadMetaLimited;
|
---|
10628 | VDIfIoInt.pfnWriteMeta = vdIOIntWriteMetaLimited;
|
---|
10629 | VDIfIoInt.pfnFlush = vdIOIntFlushLimited;
|
---|
10630 | rc = VDInterfaceAdd(&VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
10631 | pInterfaceIo, sizeof(VDINTERFACEIOINT), &pVDIfsImage);
|
---|
10632 | AssertRC(rc);
|
---|
10633 |
|
---|
10634 | rc = vdFindBackend(pszBackend, &pBackend);
|
---|
10635 | if (RT_SUCCESS(rc))
|
---|
10636 | {
|
---|
10637 | if (pBackend->pfnRepair)
|
---|
10638 | rc = pBackend->pfnRepair(pszFilename, pVDIfsDisk, pVDIfsImage, fFlags);
|
---|
10639 | else
|
---|
10640 | rc = VERR_VD_IMAGE_REPAIR_NOT_SUPPORTED;
|
---|
10641 | }
|
---|
10642 |
|
---|
10643 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
10644 | return rc;
|
---|
10645 | }
|
---|
10646 |
|
---|