1 | /* $Id: VD.cpp 44799 2013-02-21 22:46:14Z 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 | #include <VBox/vd-cache-plugin.h>
|
---|
45 |
|
---|
46 | /** Disable dynamic backends on non x86 architectures. This feature
|
---|
47 | * requires the SUPR3 library which is not available there.
|
---|
48 | */
|
---|
49 | #if !defined(VBOX_HDD_NO_DYNAMIC_BACKENDS) && !defined(RT_ARCH_X86) && !defined(RT_ARCH_AMD64)
|
---|
50 | # define VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | #define VBOXHDDDISK_SIGNATURE 0x6f0e2a7d
|
---|
54 |
|
---|
55 | /** Buffer size used for merging images. */
|
---|
56 | #define VD_MERGE_BUFFER_SIZE (16 * _1M)
|
---|
57 |
|
---|
58 | /** Maximum number of segments in one I/O task. */
|
---|
59 | #define VD_IO_TASK_SEGMENTS_MAX 64
|
---|
60 |
|
---|
61 | /** Threshold after not recently used blocks are removed from the list. */
|
---|
62 | #define VD_DISCARD_REMOVE_THRESHOLD (10 * _1M) /** @todo: experiment */
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * VD async I/O interface storage descriptor.
|
---|
66 | */
|
---|
67 | typedef struct VDIIOFALLBACKSTORAGE
|
---|
68 | {
|
---|
69 | /** File handle. */
|
---|
70 | RTFILE File;
|
---|
71 | /** Completion callback. */
|
---|
72 | PFNVDCOMPLETED pfnCompleted;
|
---|
73 | /** Thread for async access. */
|
---|
74 | RTTHREAD ThreadAsync;
|
---|
75 | } VDIIOFALLBACKSTORAGE, *PVDIIOFALLBACKSTORAGE;
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Structure containing everything I/O related
|
---|
79 | * for the image and cache descriptors.
|
---|
80 | */
|
---|
81 | typedef struct VDIO
|
---|
82 | {
|
---|
83 | /** I/O interface to the upper layer. */
|
---|
84 | PVDINTERFACEIO pInterfaceIo;
|
---|
85 |
|
---|
86 | /** Per image internal I/O interface. */
|
---|
87 | VDINTERFACEIOINT VDIfIoInt;
|
---|
88 |
|
---|
89 | /** Fallback I/O interface, only used if the caller doesn't provide it. */
|
---|
90 | VDINTERFACEIO VDIfIo;
|
---|
91 |
|
---|
92 | /** Opaque backend data. */
|
---|
93 | void *pBackendData;
|
---|
94 | /** Disk this image is part of */
|
---|
95 | PVBOXHDD pDisk;
|
---|
96 | /** Flag whether to ignore flush requests. */
|
---|
97 | bool fIgnoreFlush;
|
---|
98 | } VDIO, *PVDIO;
|
---|
99 |
|
---|
100 | /** Forward declaration of an I/O task */
|
---|
101 | typedef struct VDIOTASK *PVDIOTASK;
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * VBox HDD Container image descriptor.
|
---|
105 | */
|
---|
106 | typedef struct VDIMAGE
|
---|
107 | {
|
---|
108 | /** Link to parent image descriptor, if any. */
|
---|
109 | struct VDIMAGE *pPrev;
|
---|
110 | /** Link to child image descriptor, if any. */
|
---|
111 | struct VDIMAGE *pNext;
|
---|
112 | /** Container base filename. (UTF-8) */
|
---|
113 | char *pszFilename;
|
---|
114 | /** Data managed by the backend which keeps the actual info. */
|
---|
115 | void *pBackendData;
|
---|
116 | /** Cached sanitized image flags. */
|
---|
117 | unsigned uImageFlags;
|
---|
118 | /** Image open flags (only those handled generically in this code and which
|
---|
119 | * the backends will never ever see). */
|
---|
120 | unsigned uOpenFlags;
|
---|
121 |
|
---|
122 | /** Function pointers for the various backend methods. */
|
---|
123 | PCVBOXHDDBACKEND Backend;
|
---|
124 | /** Pointer to list of VD interfaces, per-image. */
|
---|
125 | PVDINTERFACE pVDIfsImage;
|
---|
126 | /** I/O related things. */
|
---|
127 | VDIO VDIo;
|
---|
128 | } VDIMAGE, *PVDIMAGE;
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * uModified bit flags.
|
---|
132 | */
|
---|
133 | #define VD_IMAGE_MODIFIED_FLAG RT_BIT(0)
|
---|
134 | #define VD_IMAGE_MODIFIED_FIRST RT_BIT(1)
|
---|
135 | #define VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE RT_BIT(2)
|
---|
136 |
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * VBox HDD Cache image descriptor.
|
---|
140 | */
|
---|
141 | typedef struct VDCACHE
|
---|
142 | {
|
---|
143 | /** Cache base filename. (UTF-8) */
|
---|
144 | char *pszFilename;
|
---|
145 | /** Data managed by the backend which keeps the actual info. */
|
---|
146 | void *pBackendData;
|
---|
147 | /** Cached sanitized image flags. */
|
---|
148 | unsigned uImageFlags;
|
---|
149 | /** Image open flags (only those handled generically in this code and which
|
---|
150 | * the backends will never ever see). */
|
---|
151 | unsigned uOpenFlags;
|
---|
152 |
|
---|
153 | /** Function pointers for the various backend methods. */
|
---|
154 | PCVDCACHEBACKEND Backend;
|
---|
155 |
|
---|
156 | /** Pointer to list of VD interfaces, per-cache. */
|
---|
157 | PVDINTERFACE pVDIfsCache;
|
---|
158 | /** I/O related things. */
|
---|
159 | VDIO VDIo;
|
---|
160 | } VDCACHE, *PVDCACHE;
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * A block waiting for a discard.
|
---|
164 | */
|
---|
165 | typedef struct VDDISCARDBLOCK
|
---|
166 | {
|
---|
167 | /** AVL core. */
|
---|
168 | AVLRU64NODECORE Core;
|
---|
169 | /** LRU list node. */
|
---|
170 | RTLISTNODE NodeLru;
|
---|
171 | /** Number of bytes to discard. */
|
---|
172 | size_t cbDiscard;
|
---|
173 | /** Bitmap of allocated sectors. */
|
---|
174 | void *pbmAllocated;
|
---|
175 | } VDDISCARDBLOCK, *PVDDISCARDBLOCK;
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * VD discard state.
|
---|
179 | */
|
---|
180 | typedef struct VDDISCARDSTATE
|
---|
181 | {
|
---|
182 | /** Number of bytes waiting for a discard. */
|
---|
183 | size_t cbDiscarding;
|
---|
184 | /** AVL tree with blocks waiting for a discard.
|
---|
185 | * The uOffset + cbDiscard range is the search key. */
|
---|
186 | PAVLRU64TREE pTreeBlocks;
|
---|
187 | /** LRU list of the least frequently discarded blocks.
|
---|
188 | * If there are to many blocks waiting the least frequently used
|
---|
189 | * will be removed and the range will be set to 0.
|
---|
190 | */
|
---|
191 | RTLISTNODE ListLru;
|
---|
192 | } VDDISCARDSTATE, *PVDDISCARDSTATE;
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * VBox HDD Container main structure, private part.
|
---|
196 | */
|
---|
197 | struct VBOXHDD
|
---|
198 | {
|
---|
199 | /** Structure signature (VBOXHDDDISK_SIGNATURE). */
|
---|
200 | uint32_t u32Signature;
|
---|
201 |
|
---|
202 | /** Image type. */
|
---|
203 | VDTYPE enmType;
|
---|
204 |
|
---|
205 | /** Number of opened images. */
|
---|
206 | unsigned cImages;
|
---|
207 |
|
---|
208 | /** Base image. */
|
---|
209 | PVDIMAGE pBase;
|
---|
210 |
|
---|
211 | /** Last opened image in the chain.
|
---|
212 | * The same as pBase if only one image is used. */
|
---|
213 | PVDIMAGE pLast;
|
---|
214 |
|
---|
215 | /** If a merge to one of the parents is running this may be non-NULL
|
---|
216 | * to indicate to what image the writes should be additionally relayed. */
|
---|
217 | PVDIMAGE pImageRelay;
|
---|
218 |
|
---|
219 | /** Flags representing the modification state. */
|
---|
220 | unsigned uModified;
|
---|
221 |
|
---|
222 | /** Cached size of this disk. */
|
---|
223 | uint64_t cbSize;
|
---|
224 | /** Cached PCHS geometry for this disk. */
|
---|
225 | VDGEOMETRY PCHSGeometry;
|
---|
226 | /** Cached LCHS geometry for this disk. */
|
---|
227 | VDGEOMETRY LCHSGeometry;
|
---|
228 |
|
---|
229 | /** Pointer to list of VD interfaces, per-disk. */
|
---|
230 | PVDINTERFACE pVDIfsDisk;
|
---|
231 | /** Pointer to the common interface structure for error reporting. */
|
---|
232 | PVDINTERFACEERROR pInterfaceError;
|
---|
233 | /** Pointer to the optional thread synchronization callbacks. */
|
---|
234 | PVDINTERFACETHREADSYNC pInterfaceThreadSync;
|
---|
235 |
|
---|
236 | /** Memory cache for I/O contexts */
|
---|
237 | RTMEMCACHE hMemCacheIoCtx;
|
---|
238 | /** Memory cache for I/O tasks. */
|
---|
239 | RTMEMCACHE hMemCacheIoTask;
|
---|
240 | /** An I/O context is currently using the disk structures
|
---|
241 | * Every I/O context must be placed on one of the lists below. */
|
---|
242 | volatile bool fLocked;
|
---|
243 | /** Head of pending I/O tasks waiting for completion - LIFO order. */
|
---|
244 | volatile PVDIOTASK pIoTasksPendingHead;
|
---|
245 | /** Head of newly queued I/O contexts - LIFO order. */
|
---|
246 | volatile PVDIOCTX pIoCtxHead;
|
---|
247 | /** Head of halted I/O contexts which are given back to generic
|
---|
248 | * disk framework by the backend. - LIFO order. */
|
---|
249 | volatile PVDIOCTX pIoCtxHaltedHead;
|
---|
250 |
|
---|
251 | /** Head of blocked I/O contexts, processed only
|
---|
252 | * after pIoCtxLockOwner was freed - LIFO order. */
|
---|
253 | volatile PVDIOCTX pIoCtxBlockedHead;
|
---|
254 | /** I/O context which locked the disk for a growing write or flush request.
|
---|
255 | * Other flush or growing write requests need to wait until
|
---|
256 | * the current one completes. - NIL_VDIOCTX if unlocked. */
|
---|
257 | volatile PVDIOCTX pIoCtxLockOwner;
|
---|
258 |
|
---|
259 | /** Pointer to the L2 disk cache if any. */
|
---|
260 | PVDCACHE pCache;
|
---|
261 | /** Pointer to the discard state if any. */
|
---|
262 | PVDDISCARDSTATE pDiscard;
|
---|
263 |
|
---|
264 | /** Event semaphore for synchronous I/O. */
|
---|
265 | RTSEMEVENT hEventSemSyncIo;
|
---|
266 | /** Status code of the last synchronous I/O request. */
|
---|
267 | int rcSync;
|
---|
268 | };
|
---|
269 |
|
---|
270 | # define VD_IS_LOCKED(a_pDisk) \
|
---|
271 | do \
|
---|
272 | { \
|
---|
273 | AssertMsg(a_pDisk->fLocked, \
|
---|
274 | ("Lock not held\n"));\
|
---|
275 | } while(0)
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * VBox parent read descriptor, used internally for compaction.
|
---|
279 | */
|
---|
280 | typedef struct VDPARENTSTATEDESC
|
---|
281 | {
|
---|
282 | /** Pointer to disk descriptor. */
|
---|
283 | PVBOXHDD pDisk;
|
---|
284 | /** Pointer to image descriptor. */
|
---|
285 | PVDIMAGE pImage;
|
---|
286 | } VDPARENTSTATEDESC, *PVDPARENTSTATEDESC;
|
---|
287 |
|
---|
288 | /**
|
---|
289 | * Transfer direction.
|
---|
290 | */
|
---|
291 | typedef enum VDIOCTXTXDIR
|
---|
292 | {
|
---|
293 | /** Read */
|
---|
294 | VDIOCTXTXDIR_READ = 0,
|
---|
295 | /** Write */
|
---|
296 | VDIOCTXTXDIR_WRITE,
|
---|
297 | /** Flush */
|
---|
298 | VDIOCTXTXDIR_FLUSH,
|
---|
299 | /** Discard */
|
---|
300 | VDIOCTXTXDIR_DISCARD,
|
---|
301 | /** 32bit hack */
|
---|
302 | VDIOCTXTXDIR_32BIT_HACK = 0x7fffffff
|
---|
303 | } VDIOCTXTXDIR, *PVDIOCTXTXDIR;
|
---|
304 |
|
---|
305 | /** Transfer function */
|
---|
306 | typedef DECLCALLBACK(int) FNVDIOCTXTRANSFER (PVDIOCTX pIoCtx);
|
---|
307 | /** Pointer to a transfer function. */
|
---|
308 | typedef FNVDIOCTXTRANSFER *PFNVDIOCTXTRANSFER;
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * I/O context
|
---|
312 | */
|
---|
313 | typedef struct VDIOCTX
|
---|
314 | {
|
---|
315 | /** Pointer to the next I/O context. */
|
---|
316 | struct VDIOCTX * volatile pIoCtxNext;
|
---|
317 | /** Disk this is request is for. */
|
---|
318 | PVBOXHDD pDisk;
|
---|
319 | /** Return code. */
|
---|
320 | int rcReq;
|
---|
321 | /** Various flags for the I/O context. */
|
---|
322 | uint32_t fFlags;
|
---|
323 | /** Number of data transfers currently pending. */
|
---|
324 | volatile uint32_t cDataTransfersPending;
|
---|
325 | /** How many meta data transfers are pending. */
|
---|
326 | volatile uint32_t cMetaTransfersPending;
|
---|
327 | /** Flag whether the request finished */
|
---|
328 | volatile bool fComplete;
|
---|
329 | /** Temporary allocated memory which is freed
|
---|
330 | * when the context completes. */
|
---|
331 | void *pvAllocation;
|
---|
332 | /** Transfer function. */
|
---|
333 | PFNVDIOCTXTRANSFER pfnIoCtxTransfer;
|
---|
334 | /** Next transfer part after the current one completed. */
|
---|
335 | PFNVDIOCTXTRANSFER pfnIoCtxTransferNext;
|
---|
336 | /** Transfer direction */
|
---|
337 | VDIOCTXTXDIR enmTxDir;
|
---|
338 | /** Request type dependent data. */
|
---|
339 | union
|
---|
340 | {
|
---|
341 | /** I/O request (read/write). */
|
---|
342 | struct
|
---|
343 | {
|
---|
344 | /** Number of bytes left until this context completes. */
|
---|
345 | volatile uint32_t cbTransferLeft;
|
---|
346 | /** Current offset */
|
---|
347 | volatile uint64_t uOffset;
|
---|
348 | /** Number of bytes to transfer */
|
---|
349 | volatile size_t cbTransfer;
|
---|
350 | /** Current image in the chain. */
|
---|
351 | PVDIMAGE pImageCur;
|
---|
352 | /** Start image to read from. pImageCur is reset to this
|
---|
353 | * value after it reached the first image in the chain. */
|
---|
354 | PVDIMAGE pImageStart;
|
---|
355 | /** S/G buffer */
|
---|
356 | RTSGBUF SgBuf;
|
---|
357 | } Io;
|
---|
358 | /** Discard requests. */
|
---|
359 | struct
|
---|
360 | {
|
---|
361 | /** Pointer to the range descriptor array. */
|
---|
362 | PCRTRANGE paRanges;
|
---|
363 | /** Number of ranges in the array. */
|
---|
364 | unsigned cRanges;
|
---|
365 | /** Range descriptor index which is processed. */
|
---|
366 | unsigned idxRange;
|
---|
367 | /** Start offset to discard currently. */
|
---|
368 | uint64_t offCur;
|
---|
369 | /** How many bytes left to discard in the current range. */
|
---|
370 | size_t cbDiscardLeft;
|
---|
371 | /** How many bytes to discard in the current block (<= cbDiscardLeft). */
|
---|
372 | size_t cbThisDiscard;
|
---|
373 | /** Discard block handled currently. */
|
---|
374 | PVDDISCARDBLOCK pBlock;
|
---|
375 | } Discard;
|
---|
376 | } Req;
|
---|
377 | /** Parent I/O context if any. Sets the type of the context (root/child) */
|
---|
378 | PVDIOCTX pIoCtxParent;
|
---|
379 | /** Type dependent data (root/child) */
|
---|
380 | union
|
---|
381 | {
|
---|
382 | /** Root data */
|
---|
383 | struct
|
---|
384 | {
|
---|
385 | /** Completion callback */
|
---|
386 | PFNVDASYNCTRANSFERCOMPLETE pfnComplete;
|
---|
387 | /** User argument 1 passed on completion. */
|
---|
388 | void *pvUser1;
|
---|
389 | /** User argument 2 passed on completion. */
|
---|
390 | void *pvUser2;
|
---|
391 | } Root;
|
---|
392 | /** Child data */
|
---|
393 | struct
|
---|
394 | {
|
---|
395 | /** Saved start offset */
|
---|
396 | uint64_t uOffsetSaved;
|
---|
397 | /** Saved transfer size */
|
---|
398 | size_t cbTransferLeftSaved;
|
---|
399 | /** Number of bytes transferred from the parent if this context completes. */
|
---|
400 | size_t cbTransferParent;
|
---|
401 | /** Number of bytes to pre read */
|
---|
402 | size_t cbPreRead;
|
---|
403 | /** Number of bytes to post read. */
|
---|
404 | size_t cbPostRead;
|
---|
405 | /** Number of bytes to write left in the parent. */
|
---|
406 | size_t cbWriteParent;
|
---|
407 | /** Write type dependent data. */
|
---|
408 | union
|
---|
409 | {
|
---|
410 | /** Optimized */
|
---|
411 | struct
|
---|
412 | {
|
---|
413 | /** Bytes to fill to satisfy the block size. Not part of the virtual disk. */
|
---|
414 | size_t cbFill;
|
---|
415 | /** Bytes to copy instead of reading from the parent */
|
---|
416 | size_t cbWriteCopy;
|
---|
417 | /** Bytes to read from the image. */
|
---|
418 | size_t cbReadImage;
|
---|
419 | } Optimized;
|
---|
420 | } Write;
|
---|
421 | } Child;
|
---|
422 | } Type;
|
---|
423 | } VDIOCTX;
|
---|
424 |
|
---|
425 | /** Default flags for an I/O context, i.e. unblocked and async. */
|
---|
426 | #define VDIOCTX_FLAGS_DEFAULT (0)
|
---|
427 | /** Flag whether the context is blocked. */
|
---|
428 | #define VDIOCTX_FLAGS_BLOCKED RT_BIT_32(0)
|
---|
429 | /** Flag whether the I/O context is using synchronous I/O. */
|
---|
430 | #define VDIOCTX_FLAGS_SYNC RT_BIT_32(1)
|
---|
431 |
|
---|
432 | /** NIL I/O context pointer value. */
|
---|
433 | #define NIL_VDIOCTX ((PVDIOCTX)0)
|
---|
434 |
|
---|
435 | /**
|
---|
436 | * List node for deferred I/O contexts.
|
---|
437 | */
|
---|
438 | typedef struct VDIOCTXDEFERRED
|
---|
439 | {
|
---|
440 | /** Node in the list of deferred requests.
|
---|
441 | * A request can be deferred if the image is growing
|
---|
442 | * and the request accesses the same range or if
|
---|
443 | * the backend needs to read or write metadata from the disk
|
---|
444 | * before it can continue. */
|
---|
445 | RTLISTNODE NodeDeferred;
|
---|
446 | /** I/O context this entry points to. */
|
---|
447 | PVDIOCTX pIoCtx;
|
---|
448 | } VDIOCTXDEFERRED, *PVDIOCTXDEFERRED;
|
---|
449 |
|
---|
450 | /**
|
---|
451 | * I/O task.
|
---|
452 | */
|
---|
453 | typedef struct VDIOTASK
|
---|
454 | {
|
---|
455 | /** Next I/O task waiting in the list. */
|
---|
456 | struct VDIOTASK * volatile pNext;
|
---|
457 | /** Storage this task belongs to. */
|
---|
458 | PVDIOSTORAGE pIoStorage;
|
---|
459 | /** Optional completion callback. */
|
---|
460 | PFNVDXFERCOMPLETED pfnComplete;
|
---|
461 | /** Opaque user data. */
|
---|
462 | void *pvUser;
|
---|
463 | /** Completion status code for the task. */
|
---|
464 | int rcReq;
|
---|
465 | /** Flag whether this is a meta data transfer. */
|
---|
466 | bool fMeta;
|
---|
467 | /** Type dependent data. */
|
---|
468 | union
|
---|
469 | {
|
---|
470 | /** User data transfer. */
|
---|
471 | struct
|
---|
472 | {
|
---|
473 | /** Number of bytes this task transferred. */
|
---|
474 | uint32_t cbTransfer;
|
---|
475 | /** Pointer to the I/O context the task belongs. */
|
---|
476 | PVDIOCTX pIoCtx;
|
---|
477 | } User;
|
---|
478 | /** Meta data transfer. */
|
---|
479 | struct
|
---|
480 | {
|
---|
481 | /** Meta transfer this task is for. */
|
---|
482 | PVDMETAXFER pMetaXfer;
|
---|
483 | } Meta;
|
---|
484 | } Type;
|
---|
485 | } VDIOTASK;
|
---|
486 |
|
---|
487 | /**
|
---|
488 | * Storage handle.
|
---|
489 | */
|
---|
490 | typedef struct VDIOSTORAGE
|
---|
491 | {
|
---|
492 | /** Image I/O state this storage handle belongs to. */
|
---|
493 | PVDIO pVDIo;
|
---|
494 | /** AVL tree for pending async metadata transfers. */
|
---|
495 | PAVLRFOFFTREE pTreeMetaXfers;
|
---|
496 | /** Storage handle */
|
---|
497 | void *pStorage;
|
---|
498 | } VDIOSTORAGE;
|
---|
499 |
|
---|
500 | /**
|
---|
501 | * Metadata transfer.
|
---|
502 | *
|
---|
503 | * @note This entry can't be freed if either the list is not empty or
|
---|
504 | * the reference counter is not 0.
|
---|
505 | * The assumption is that the backends don't need to read huge amounts of
|
---|
506 | * metadata to complete a transfer so the additional memory overhead should
|
---|
507 | * be relatively small.
|
---|
508 | */
|
---|
509 | typedef struct VDMETAXFER
|
---|
510 | {
|
---|
511 | /** AVL core for fast search (the file offset is the key) */
|
---|
512 | AVLRFOFFNODECORE Core;
|
---|
513 | /** I/O storage for this transfer. */
|
---|
514 | PVDIOSTORAGE pIoStorage;
|
---|
515 | /** Flags. */
|
---|
516 | uint32_t fFlags;
|
---|
517 | /** List of I/O contexts waiting for this metadata transfer to complete. */
|
---|
518 | RTLISTNODE ListIoCtxWaiting;
|
---|
519 | /** Number of references to this entry. */
|
---|
520 | unsigned cRefs;
|
---|
521 | /** Size of the data stored with this entry. */
|
---|
522 | size_t cbMeta;
|
---|
523 | /** Data stored - variable size. */
|
---|
524 | uint8_t abData[1];
|
---|
525 | } VDMETAXFER;
|
---|
526 |
|
---|
527 | /**
|
---|
528 | * The transfer direction for the metadata.
|
---|
529 | */
|
---|
530 | #define VDMETAXFER_TXDIR_MASK 0x3
|
---|
531 | #define VDMETAXFER_TXDIR_NONE 0x0
|
---|
532 | #define VDMETAXFER_TXDIR_WRITE 0x1
|
---|
533 | #define VDMETAXFER_TXDIR_READ 0x2
|
---|
534 | #define VDMETAXFER_TXDIR_FLUSH 0x3
|
---|
535 | #define VDMETAXFER_TXDIR_GET(flags) ((flags) & VDMETAXFER_TXDIR_MASK)
|
---|
536 | #define VDMETAXFER_TXDIR_SET(flags, dir) ((flags) = (flags & ~VDMETAXFER_TXDIR_MASK) | (dir))
|
---|
537 |
|
---|
538 | extern VBOXHDDBACKEND g_RawBackend;
|
---|
539 | extern VBOXHDDBACKEND g_VmdkBackend;
|
---|
540 | extern VBOXHDDBACKEND g_VDIBackend;
|
---|
541 | extern VBOXHDDBACKEND g_VhdBackend;
|
---|
542 | extern VBOXHDDBACKEND g_ParallelsBackend;
|
---|
543 | extern VBOXHDDBACKEND g_DmgBackend;
|
---|
544 | extern VBOXHDDBACKEND g_ISCSIBackend;
|
---|
545 | extern VBOXHDDBACKEND g_QedBackend;
|
---|
546 | extern VBOXHDDBACKEND g_QCowBackend;
|
---|
547 | extern VBOXHDDBACKEND g_VhdxBackend;
|
---|
548 |
|
---|
549 | static unsigned g_cBackends = 0;
|
---|
550 | static PVBOXHDDBACKEND *g_apBackends = NULL;
|
---|
551 | static PVBOXHDDBACKEND aStaticBackends[] =
|
---|
552 | {
|
---|
553 | &g_VmdkBackend,
|
---|
554 | &g_VDIBackend,
|
---|
555 | &g_VhdBackend,
|
---|
556 | &g_ParallelsBackend,
|
---|
557 | &g_DmgBackend,
|
---|
558 | &g_QedBackend,
|
---|
559 | &g_QCowBackend,
|
---|
560 | &g_VhdxBackend,
|
---|
561 | &g_RawBackend,
|
---|
562 | &g_ISCSIBackend
|
---|
563 | };
|
---|
564 |
|
---|
565 | /**
|
---|
566 | * Supported backends for the disk cache.
|
---|
567 | */
|
---|
568 | extern VDCACHEBACKEND g_VciCacheBackend;
|
---|
569 |
|
---|
570 | static unsigned g_cCacheBackends = 0;
|
---|
571 | static PVDCACHEBACKEND *g_apCacheBackends = NULL;
|
---|
572 | static PVDCACHEBACKEND aStaticCacheBackends[] =
|
---|
573 | {
|
---|
574 | &g_VciCacheBackend
|
---|
575 | };
|
---|
576 |
|
---|
577 | /** Forward declaration of the async discard helper. */
|
---|
578 | static int vdDiscardHelperAsync(PVDIOCTX pIoCtx);
|
---|
579 | static void vdDiskProcessBlockedIoCtx(PVBOXHDD pDisk);
|
---|
580 | static int vdDiskUnlock(PVBOXHDD pDisk, PVDIOCTX pIoCtxRc);
|
---|
581 |
|
---|
582 | /**
|
---|
583 | * internal: add several backends.
|
---|
584 | */
|
---|
585 | static int vdAddBackends(PVBOXHDDBACKEND *ppBackends, unsigned cBackends)
|
---|
586 | {
|
---|
587 | PVBOXHDDBACKEND *pTmp = (PVBOXHDDBACKEND*)RTMemRealloc(g_apBackends,
|
---|
588 | (g_cBackends + cBackends) * sizeof(PVBOXHDDBACKEND));
|
---|
589 | if (RT_UNLIKELY(!pTmp))
|
---|
590 | return VERR_NO_MEMORY;
|
---|
591 | g_apBackends = pTmp;
|
---|
592 | memcpy(&g_apBackends[g_cBackends], ppBackends, cBackends * sizeof(PVBOXHDDBACKEND));
|
---|
593 | g_cBackends += cBackends;
|
---|
594 | return VINF_SUCCESS;
|
---|
595 | }
|
---|
596 |
|
---|
597 | /**
|
---|
598 | * internal: add single backend.
|
---|
599 | */
|
---|
600 | DECLINLINE(int) vdAddBackend(PVBOXHDDBACKEND pBackend)
|
---|
601 | {
|
---|
602 | return vdAddBackends(&pBackend, 1);
|
---|
603 | }
|
---|
604 |
|
---|
605 | /**
|
---|
606 | * internal: add several cache backends.
|
---|
607 | */
|
---|
608 | static int vdAddCacheBackends(PVDCACHEBACKEND *ppBackends, unsigned cBackends)
|
---|
609 | {
|
---|
610 | PVDCACHEBACKEND *pTmp = (PVDCACHEBACKEND*)RTMemRealloc(g_apCacheBackends,
|
---|
611 | (g_cCacheBackends + cBackends) * sizeof(PVDCACHEBACKEND));
|
---|
612 | if (RT_UNLIKELY(!pTmp))
|
---|
613 | return VERR_NO_MEMORY;
|
---|
614 | g_apCacheBackends = pTmp;
|
---|
615 | memcpy(&g_apCacheBackends[g_cCacheBackends], ppBackends, cBackends * sizeof(PVDCACHEBACKEND));
|
---|
616 | g_cCacheBackends += cBackends;
|
---|
617 | return VINF_SUCCESS;
|
---|
618 | }
|
---|
619 |
|
---|
620 | /**
|
---|
621 | * internal: add single cache backend.
|
---|
622 | */
|
---|
623 | DECLINLINE(int) vdAddCacheBackend(PVDCACHEBACKEND pBackend)
|
---|
624 | {
|
---|
625 | return vdAddCacheBackends(&pBackend, 1);
|
---|
626 | }
|
---|
627 |
|
---|
628 | /**
|
---|
629 | * internal: issue error message.
|
---|
630 | */
|
---|
631 | static int vdError(PVBOXHDD pDisk, int rc, RT_SRC_POS_DECL,
|
---|
632 | const char *pszFormat, ...)
|
---|
633 | {
|
---|
634 | va_list va;
|
---|
635 | va_start(va, pszFormat);
|
---|
636 | if (pDisk->pInterfaceError)
|
---|
637 | pDisk->pInterfaceError->pfnError(pDisk->pInterfaceError->Core.pvUser, rc, RT_SRC_POS_ARGS, pszFormat, va);
|
---|
638 | va_end(va);
|
---|
639 | return rc;
|
---|
640 | }
|
---|
641 |
|
---|
642 | /**
|
---|
643 | * internal: thread synchronization, start read.
|
---|
644 | */
|
---|
645 | DECLINLINE(int) vdThreadStartRead(PVBOXHDD pDisk)
|
---|
646 | {
|
---|
647 | int rc = VINF_SUCCESS;
|
---|
648 | if (RT_UNLIKELY(pDisk->pInterfaceThreadSync))
|
---|
649 | rc = pDisk->pInterfaceThreadSync->pfnStartRead(pDisk->pInterfaceThreadSync->Core.pvUser);
|
---|
650 | return rc;
|
---|
651 | }
|
---|
652 |
|
---|
653 | /**
|
---|
654 | * internal: thread synchronization, finish read.
|
---|
655 | */
|
---|
656 | DECLINLINE(int) vdThreadFinishRead(PVBOXHDD pDisk)
|
---|
657 | {
|
---|
658 | int rc = VINF_SUCCESS;
|
---|
659 | if (RT_UNLIKELY(pDisk->pInterfaceThreadSync))
|
---|
660 | rc = pDisk->pInterfaceThreadSync->pfnFinishRead(pDisk->pInterfaceThreadSync->Core.pvUser);
|
---|
661 | return rc;
|
---|
662 | }
|
---|
663 |
|
---|
664 | /**
|
---|
665 | * internal: thread synchronization, start write.
|
---|
666 | */
|
---|
667 | DECLINLINE(int) vdThreadStartWrite(PVBOXHDD pDisk)
|
---|
668 | {
|
---|
669 | int rc = VINF_SUCCESS;
|
---|
670 | if (RT_UNLIKELY(pDisk->pInterfaceThreadSync))
|
---|
671 | rc = pDisk->pInterfaceThreadSync->pfnStartWrite(pDisk->pInterfaceThreadSync->Core.pvUser);
|
---|
672 | return rc;
|
---|
673 | }
|
---|
674 |
|
---|
675 | /**
|
---|
676 | * internal: thread synchronization, finish write.
|
---|
677 | */
|
---|
678 | DECLINLINE(int) vdThreadFinishWrite(PVBOXHDD pDisk)
|
---|
679 | {
|
---|
680 | int rc = VINF_SUCCESS;
|
---|
681 | if (RT_UNLIKELY(pDisk->pInterfaceThreadSync))
|
---|
682 | rc = pDisk->pInterfaceThreadSync->pfnFinishWrite(pDisk->pInterfaceThreadSync->Core.pvUser);
|
---|
683 | return rc;
|
---|
684 | }
|
---|
685 |
|
---|
686 | /**
|
---|
687 | * internal: find image format backend.
|
---|
688 | */
|
---|
689 | static int vdFindBackend(const char *pszBackend, PCVBOXHDDBACKEND *ppBackend)
|
---|
690 | {
|
---|
691 | int rc = VINF_SUCCESS;
|
---|
692 | PCVBOXHDDBACKEND pBackend = NULL;
|
---|
693 |
|
---|
694 | if (!g_apBackends)
|
---|
695 | VDInit();
|
---|
696 |
|
---|
697 | for (unsigned i = 0; i < g_cBackends; i++)
|
---|
698 | {
|
---|
699 | if (!RTStrICmp(pszBackend, g_apBackends[i]->pszBackendName))
|
---|
700 | {
|
---|
701 | pBackend = g_apBackends[i];
|
---|
702 | break;
|
---|
703 | }
|
---|
704 | }
|
---|
705 | *ppBackend = pBackend;
|
---|
706 | return rc;
|
---|
707 | }
|
---|
708 |
|
---|
709 | /**
|
---|
710 | * internal: find cache format backend.
|
---|
711 | */
|
---|
712 | static int vdFindCacheBackend(const char *pszBackend, PCVDCACHEBACKEND *ppBackend)
|
---|
713 | {
|
---|
714 | int rc = VINF_SUCCESS;
|
---|
715 | PCVDCACHEBACKEND pBackend = NULL;
|
---|
716 |
|
---|
717 | if (!g_apCacheBackends)
|
---|
718 | VDInit();
|
---|
719 |
|
---|
720 | for (unsigned i = 0; i < g_cCacheBackends; i++)
|
---|
721 | {
|
---|
722 | if (!RTStrICmp(pszBackend, g_apCacheBackends[i]->pszBackendName))
|
---|
723 | {
|
---|
724 | pBackend = g_apCacheBackends[i];
|
---|
725 | break;
|
---|
726 | }
|
---|
727 | }
|
---|
728 | *ppBackend = pBackend;
|
---|
729 | return rc;
|
---|
730 | }
|
---|
731 |
|
---|
732 | /**
|
---|
733 | * internal: add image structure to the end of images list.
|
---|
734 | */
|
---|
735 | static void vdAddImageToList(PVBOXHDD pDisk, PVDIMAGE pImage)
|
---|
736 | {
|
---|
737 | pImage->pPrev = NULL;
|
---|
738 | pImage->pNext = NULL;
|
---|
739 |
|
---|
740 | if (pDisk->pBase)
|
---|
741 | {
|
---|
742 | Assert(pDisk->cImages > 0);
|
---|
743 | pImage->pPrev = pDisk->pLast;
|
---|
744 | pDisk->pLast->pNext = pImage;
|
---|
745 | pDisk->pLast = pImage;
|
---|
746 | }
|
---|
747 | else
|
---|
748 | {
|
---|
749 | Assert(pDisk->cImages == 0);
|
---|
750 | pDisk->pBase = pImage;
|
---|
751 | pDisk->pLast = pImage;
|
---|
752 | }
|
---|
753 |
|
---|
754 | pDisk->cImages++;
|
---|
755 | }
|
---|
756 |
|
---|
757 | /**
|
---|
758 | * internal: remove image structure from the images list.
|
---|
759 | */
|
---|
760 | static void vdRemoveImageFromList(PVBOXHDD pDisk, PVDIMAGE pImage)
|
---|
761 | {
|
---|
762 | Assert(pDisk->cImages > 0);
|
---|
763 |
|
---|
764 | if (pImage->pPrev)
|
---|
765 | pImage->pPrev->pNext = pImage->pNext;
|
---|
766 | else
|
---|
767 | pDisk->pBase = pImage->pNext;
|
---|
768 |
|
---|
769 | if (pImage->pNext)
|
---|
770 | pImage->pNext->pPrev = pImage->pPrev;
|
---|
771 | else
|
---|
772 | pDisk->pLast = pImage->pPrev;
|
---|
773 |
|
---|
774 | pImage->pPrev = NULL;
|
---|
775 | pImage->pNext = NULL;
|
---|
776 |
|
---|
777 | pDisk->cImages--;
|
---|
778 | }
|
---|
779 |
|
---|
780 | /**
|
---|
781 | * internal: find image by index into the images list.
|
---|
782 | */
|
---|
783 | static PVDIMAGE vdGetImageByNumber(PVBOXHDD pDisk, unsigned nImage)
|
---|
784 | {
|
---|
785 | PVDIMAGE pImage = pDisk->pBase;
|
---|
786 | if (nImage == VD_LAST_IMAGE)
|
---|
787 | return pDisk->pLast;
|
---|
788 | while (pImage && nImage)
|
---|
789 | {
|
---|
790 | pImage = pImage->pNext;
|
---|
791 | nImage--;
|
---|
792 | }
|
---|
793 | return pImage;
|
---|
794 | }
|
---|
795 |
|
---|
796 | /**
|
---|
797 | * Initialize the structure members of a given I/O context.
|
---|
798 | */
|
---|
799 | DECLINLINE(void) vdIoCtxInit(PVDIOCTX pIoCtx, PVBOXHDD pDisk, VDIOCTXTXDIR enmTxDir,
|
---|
800 | uint64_t uOffset, size_t cbTransfer, PVDIMAGE pImageStart,
|
---|
801 | PCRTSGBUF pcSgBuf, void *pvAllocation,
|
---|
802 | PFNVDIOCTXTRANSFER pfnIoCtxTransfer, uint32_t fFlags)
|
---|
803 | {
|
---|
804 | pIoCtx->pDisk = pDisk;
|
---|
805 | pIoCtx->enmTxDir = enmTxDir;
|
---|
806 | pIoCtx->Req.Io.cbTransferLeft = cbTransfer;
|
---|
807 | pIoCtx->Req.Io.uOffset = uOffset;
|
---|
808 | pIoCtx->Req.Io.cbTransfer = cbTransfer;
|
---|
809 | pIoCtx->Req.Io.pImageStart = pImageStart;
|
---|
810 | pIoCtx->Req.Io.pImageCur = pImageStart;
|
---|
811 | pIoCtx->cDataTransfersPending = 0;
|
---|
812 | pIoCtx->cMetaTransfersPending = 0;
|
---|
813 | pIoCtx->fComplete = false;
|
---|
814 | pIoCtx->fFlags = fFlags;
|
---|
815 | pIoCtx->pvAllocation = pvAllocation;
|
---|
816 | pIoCtx->pfnIoCtxTransfer = pfnIoCtxTransfer;
|
---|
817 | pIoCtx->pfnIoCtxTransferNext = NULL;
|
---|
818 | pIoCtx->rcReq = VINF_SUCCESS;
|
---|
819 |
|
---|
820 | /* There is no S/G list for a flush request. */
|
---|
821 | if ( enmTxDir != VDIOCTXTXDIR_FLUSH
|
---|
822 | && enmTxDir != VDIOCTXTXDIR_DISCARD)
|
---|
823 | RTSgBufClone(&pIoCtx->Req.Io.SgBuf, pcSgBuf);
|
---|
824 | else
|
---|
825 | memset(&pIoCtx->Req.Io.SgBuf, 0, sizeof(RTSGBUF));
|
---|
826 | }
|
---|
827 |
|
---|
828 | /**
|
---|
829 | * Internal: Tries to read the desired range from the given cache.
|
---|
830 | *
|
---|
831 | * @returns VBox status code.
|
---|
832 | * @retval VERR_VD_BLOCK_FREE if the block is not in the cache.
|
---|
833 | * pcbRead will be set to the number of bytes not in the cache.
|
---|
834 | * Everything thereafter might be in the cache.
|
---|
835 | * @param pCache The cache to read from.
|
---|
836 | * @param uOffset Offset of the virtual disk to read.
|
---|
837 | * @param cbRead How much to read.
|
---|
838 | * @param pIoCtx The I/O context to read into.
|
---|
839 | * @param pcbRead Where to store the number of bytes actually read.
|
---|
840 | * On success this indicates the number of bytes read from the cache.
|
---|
841 | * If VERR_VD_BLOCK_FREE is returned this gives the number of bytes
|
---|
842 | * which are not in the cache.
|
---|
843 | * In both cases everything beyond this value
|
---|
844 | * might or might not be in the cache.
|
---|
845 | */
|
---|
846 | static int vdCacheReadHelper(PVDCACHE pCache, uint64_t uOffset,
|
---|
847 | PVDIOCTX pIoCtx, size_t cbRead, size_t *pcbRead)
|
---|
848 | {
|
---|
849 | int rc = VINF_SUCCESS;
|
---|
850 |
|
---|
851 | LogFlowFunc(("pCache=%#p uOffset=%llu pIoCtx=%p cbRead=%zu pcbRead=%#p\n",
|
---|
852 | pCache, uOffset, pIoCtx, cbRead, pcbRead));
|
---|
853 |
|
---|
854 | AssertPtr(pCache);
|
---|
855 | AssertPtr(pcbRead);
|
---|
856 |
|
---|
857 | rc = pCache->Backend->pfnRead(pCache->pBackendData, uOffset, cbRead,
|
---|
858 | pIoCtx, pcbRead);
|
---|
859 |
|
---|
860 | LogFlowFunc(("returns rc=%Rrc pcbRead=%zu\n", rc, *pcbRead));
|
---|
861 | return rc;
|
---|
862 | }
|
---|
863 |
|
---|
864 | /**
|
---|
865 | * Internal: Writes data for the given block into the cache.
|
---|
866 | *
|
---|
867 | * @returns VBox status code.
|
---|
868 | * @param pCache The cache to write to.
|
---|
869 | * @param uOffset Offset of the virtual disk to write to the cache.
|
---|
870 | * @param cbWrite How much to write.
|
---|
871 | * @param pIoCtx The I/O context to ẃrite from.
|
---|
872 | * @param pcbWritten How much data could be written, optional.
|
---|
873 | */
|
---|
874 | static int vdCacheWriteHelper(PVDCACHE pCache, uint64_t uOffset, size_t cbWrite,
|
---|
875 | PVDIOCTX pIoCtx, size_t *pcbWritten)
|
---|
876 | {
|
---|
877 | int rc = VINF_SUCCESS;
|
---|
878 |
|
---|
879 | LogFlowFunc(("pCache=%#p uOffset=%llu pIoCtx=%p cbWrite=%zu pcbWritten=%#p\n",
|
---|
880 | pCache, uOffset, pIoCtx, cbWrite, pcbWritten));
|
---|
881 |
|
---|
882 | AssertPtr(pCache);
|
---|
883 | AssertPtr(pIoCtx);
|
---|
884 | Assert(cbWrite > 0);
|
---|
885 |
|
---|
886 | if (pcbWritten)
|
---|
887 | rc = pCache->Backend->pfnWrite(pCache->pBackendData, uOffset, cbWrite,
|
---|
888 | pIoCtx, pcbWritten);
|
---|
889 | else
|
---|
890 | {
|
---|
891 | size_t cbWritten = 0;
|
---|
892 |
|
---|
893 | do
|
---|
894 | {
|
---|
895 | rc = pCache->Backend->pfnWrite(pCache->pBackendData, uOffset, cbWrite,
|
---|
896 | pIoCtx, &cbWritten);
|
---|
897 | uOffset += cbWritten;
|
---|
898 | cbWrite -= cbWritten;
|
---|
899 | } while ( cbWrite
|
---|
900 | && ( RT_SUCCESS(rc)
|
---|
901 | || rc == VERR_VD_ASYNC_IO_IN_PROGRESS));
|
---|
902 | }
|
---|
903 |
|
---|
904 | LogFlowFunc(("returns rc=%Rrc pcbWritten=%zu\n",
|
---|
905 | rc, pcbWritten ? *pcbWritten : cbWrite));
|
---|
906 | return rc;
|
---|
907 | }
|
---|
908 |
|
---|
909 | /**
|
---|
910 | * Internal: Reads a given amount of data from the image chain of the disk.
|
---|
911 | **/
|
---|
912 | static int vdDiskReadHelper(PVBOXHDD pDisk, PVDIMAGE pImage, PVDIMAGE pImageParentOverride,
|
---|
913 | uint64_t uOffset, void *pvBuf, size_t cbRead, size_t *pcbThisRead)
|
---|
914 | {
|
---|
915 | int rc = VINF_SUCCESS;
|
---|
916 | size_t cbThisRead = cbRead;
|
---|
917 | RTSGSEG SegmentBuf;
|
---|
918 | RTSGBUF SgBuf;
|
---|
919 | VDIOCTX IoCtx;
|
---|
920 |
|
---|
921 | AssertPtr(pcbThisRead);
|
---|
922 |
|
---|
923 | *pcbThisRead = 0;
|
---|
924 |
|
---|
925 | SegmentBuf.pvSeg = pvBuf;
|
---|
926 | SegmentBuf.cbSeg = VD_MERGE_BUFFER_SIZE;
|
---|
927 | RTSgBufInit(&SgBuf, &SegmentBuf, 1);
|
---|
928 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_READ, 0, 0, NULL,
|
---|
929 | &SgBuf, NULL, NULL, VDIOCTX_FLAGS_SYNC);
|
---|
930 |
|
---|
931 | /*
|
---|
932 | * Try to read from the given image.
|
---|
933 | * If the block is not allocated read from override chain if present.
|
---|
934 | */
|
---|
935 | rc = pImage->Backend->pfnRead(pImage->pBackendData,
|
---|
936 | uOffset, cbThisRead, &IoCtx,
|
---|
937 | &cbThisRead);
|
---|
938 |
|
---|
939 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
940 | {
|
---|
941 | for (PVDIMAGE pCurrImage = pImageParentOverride ? pImageParentOverride : pImage->pPrev;
|
---|
942 | pCurrImage != NULL && rc == VERR_VD_BLOCK_FREE;
|
---|
943 | pCurrImage = pCurrImage->pPrev)
|
---|
944 | {
|
---|
945 | rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
|
---|
946 | uOffset, cbThisRead, &IoCtx,
|
---|
947 | &cbThisRead);
|
---|
948 | }
|
---|
949 | }
|
---|
950 |
|
---|
951 | if (RT_SUCCESS(rc) || rc == VERR_VD_BLOCK_FREE)
|
---|
952 | *pcbThisRead = cbThisRead;
|
---|
953 |
|
---|
954 | return rc;
|
---|
955 | }
|
---|
956 |
|
---|
957 | /**
|
---|
958 | * Extended version of vdReadHelper(), implementing certain optimizations
|
---|
959 | * for image cloning.
|
---|
960 | *
|
---|
961 | * @returns VBox status code.
|
---|
962 | * @param pDisk The disk to read from.
|
---|
963 | * @param pImage The image to start reading from.
|
---|
964 | * @param pImageParentOverride The parent image to read from
|
---|
965 | * if the starting image returns a free block.
|
---|
966 | * If NULL is passed the real parent of the image
|
---|
967 | * in the chain is used.
|
---|
968 | * @param uOffset Offset in the disk to start reading from.
|
---|
969 | * @param pvBuf Where to store the read data.
|
---|
970 | * @param cbRead How much to read.
|
---|
971 | * @param fZeroFreeBlocks Flag whether free blocks should be zeroed.
|
---|
972 | * If false and no image has data for sepcified
|
---|
973 | * range VERR_VD_BLOCK_FREE is returned.
|
---|
974 | * Note that unallocated blocks are still zeroed
|
---|
975 | * if at least one image has valid data for a part
|
---|
976 | * of the range.
|
---|
977 | * @param fUpdateCache Flag whether to update the attached cache if
|
---|
978 | * available.
|
---|
979 | * @param cImagesRead Number of images in the chain to read until
|
---|
980 | * the read is cut off. A value of 0 disables the cut off.
|
---|
981 | */
|
---|
982 | static int vdReadHelperEx(PVBOXHDD pDisk, PVDIMAGE pImage, PVDIMAGE pImageParentOverride,
|
---|
983 | uint64_t uOffset, void *pvBuf, size_t cbRead,
|
---|
984 | bool fZeroFreeBlocks, bool fUpdateCache, unsigned cImagesRead)
|
---|
985 | {
|
---|
986 | int rc = VINF_SUCCESS;
|
---|
987 | size_t cbThisRead;
|
---|
988 | bool fAllFree = true;
|
---|
989 | size_t cbBufClear = 0;
|
---|
990 |
|
---|
991 | /* Loop until all read. */
|
---|
992 | do
|
---|
993 | {
|
---|
994 | /* Search for image with allocated block. Do not attempt to read more
|
---|
995 | * than the previous reads marked as valid. Otherwise this would return
|
---|
996 | * stale data when different block sizes are used for the images. */
|
---|
997 | cbThisRead = cbRead;
|
---|
998 |
|
---|
999 | if ( pDisk->pCache
|
---|
1000 | && !pImageParentOverride)
|
---|
1001 | {
|
---|
1002 | #if 0 /** @todo: Will go soon when the sync and async read helper versions are merged. */
|
---|
1003 | rc = vdCacheReadHelper(pDisk->pCache, uOffset, pvBuf,
|
---|
1004 | cbThisRead, &cbThisRead);
|
---|
1005 | #endif
|
---|
1006 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
1007 | {
|
---|
1008 | rc = vdDiskReadHelper(pDisk, pImage, NULL, uOffset, pvBuf, cbThisRead,
|
---|
1009 | &cbThisRead);
|
---|
1010 |
|
---|
1011 | /* If the read was successful, write the data back into the cache. */
|
---|
1012 | if ( RT_SUCCESS(rc)
|
---|
1013 | && fUpdateCache)
|
---|
1014 | {
|
---|
1015 | #if 0 /** @todo: Will go soon when the sync and async read helper versions are merged. */
|
---|
1016 | rc = vdCacheWriteHelper(pDisk->pCache, uOffset, pvBuf,
|
---|
1017 | cbThisRead, NULL);
|
---|
1018 | #endif
|
---|
1019 | }
|
---|
1020 | }
|
---|
1021 | }
|
---|
1022 | else
|
---|
1023 | {
|
---|
1024 | RTSGSEG SegmentBuf;
|
---|
1025 | RTSGBUF SgBuf;
|
---|
1026 | VDIOCTX IoCtx;
|
---|
1027 |
|
---|
1028 | SegmentBuf.pvSeg = pvBuf;
|
---|
1029 | SegmentBuf.cbSeg = cbThisRead;
|
---|
1030 | RTSgBufInit(&SgBuf, &SegmentBuf, 1);
|
---|
1031 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_READ, 0, 0, NULL,
|
---|
1032 | &SgBuf, NULL, NULL, VDIOCTX_FLAGS_SYNC);
|
---|
1033 |
|
---|
1034 | /*
|
---|
1035 | * Try to read from the given image.
|
---|
1036 | * If the block is not allocated read from override chain if present.
|
---|
1037 | */
|
---|
1038 | rc = pImage->Backend->pfnRead(pImage->pBackendData,
|
---|
1039 | uOffset, cbThisRead, &IoCtx,
|
---|
1040 | &cbThisRead);
|
---|
1041 |
|
---|
1042 | if ( rc == VERR_VD_BLOCK_FREE
|
---|
1043 | && cImagesRead != 1)
|
---|
1044 | {
|
---|
1045 | unsigned cImagesToProcess = cImagesRead;
|
---|
1046 |
|
---|
1047 | for (PVDIMAGE pCurrImage = pImageParentOverride ? pImageParentOverride : pImage->pPrev;
|
---|
1048 | pCurrImage != NULL && rc == VERR_VD_BLOCK_FREE;
|
---|
1049 | pCurrImage = pCurrImage->pPrev)
|
---|
1050 | {
|
---|
1051 | rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
|
---|
1052 | uOffset, cbThisRead,
|
---|
1053 | &IoCtx, &cbThisRead);
|
---|
1054 | if (cImagesToProcess == 1)
|
---|
1055 | break;
|
---|
1056 | else if (cImagesToProcess > 0)
|
---|
1057 | cImagesToProcess--;
|
---|
1058 | }
|
---|
1059 | }
|
---|
1060 | }
|
---|
1061 |
|
---|
1062 | /* No image in the chain contains the data for the block. */
|
---|
1063 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
1064 | {
|
---|
1065 | /* Fill the free space with 0 if we are told to do so
|
---|
1066 | * or a previous read returned valid data. */
|
---|
1067 | if (fZeroFreeBlocks || !fAllFree)
|
---|
1068 | memset(pvBuf, '\0', cbThisRead);
|
---|
1069 | else
|
---|
1070 | cbBufClear += cbThisRead;
|
---|
1071 |
|
---|
1072 | if (pImage->uOpenFlags & VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS)
|
---|
1073 | rc = VINF_VD_NEW_ZEROED_BLOCK;
|
---|
1074 | else
|
---|
1075 | rc = VINF_SUCCESS;
|
---|
1076 | }
|
---|
1077 | else if (RT_SUCCESS(rc))
|
---|
1078 | {
|
---|
1079 | /* First not free block, fill the space before with 0. */
|
---|
1080 | if (!fZeroFreeBlocks)
|
---|
1081 | {
|
---|
1082 | memset((char *)pvBuf - cbBufClear, '\0', cbBufClear);
|
---|
1083 | cbBufClear = 0;
|
---|
1084 | fAllFree = false;
|
---|
1085 | }
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | cbRead -= cbThisRead;
|
---|
1089 | uOffset += cbThisRead;
|
---|
1090 | pvBuf = (char *)pvBuf + cbThisRead;
|
---|
1091 | } while (cbRead != 0 && RT_SUCCESS(rc));
|
---|
1092 |
|
---|
1093 | return (!fZeroFreeBlocks && fAllFree) ? VERR_VD_BLOCK_FREE : rc;
|
---|
1094 | }
|
---|
1095 |
|
---|
1096 | /**
|
---|
1097 | * internal: read the specified amount of data in whatever blocks the backend
|
---|
1098 | * will give us.
|
---|
1099 | */
|
---|
1100 | static int vdReadHelper(PVBOXHDD pDisk, PVDIMAGE pImage, uint64_t uOffset,
|
---|
1101 | void *pvBuf, size_t cbRead, bool fUpdateCache)
|
---|
1102 | {
|
---|
1103 | return vdReadHelperEx(pDisk, pImage, NULL, uOffset, pvBuf, cbRead,
|
---|
1104 | true /* fZeroFreeBlocks */, fUpdateCache, 0);
|
---|
1105 | }
|
---|
1106 |
|
---|
1107 | /**
|
---|
1108 | * Creates a new empty discard state.
|
---|
1109 | *
|
---|
1110 | * @returns Pointer to the new discard state or NULL if out of memory.
|
---|
1111 | */
|
---|
1112 | static PVDDISCARDSTATE vdDiscardStateCreate(void)
|
---|
1113 | {
|
---|
1114 | PVDDISCARDSTATE pDiscard = (PVDDISCARDSTATE)RTMemAllocZ(sizeof(VDDISCARDSTATE));
|
---|
1115 |
|
---|
1116 | if (pDiscard)
|
---|
1117 | {
|
---|
1118 | RTListInit(&pDiscard->ListLru);
|
---|
1119 | pDiscard->pTreeBlocks = (PAVLRU64TREE)RTMemAllocZ(sizeof(AVLRU64TREE));
|
---|
1120 | if (!pDiscard->pTreeBlocks)
|
---|
1121 | {
|
---|
1122 | RTMemFree(pDiscard);
|
---|
1123 | pDiscard = NULL;
|
---|
1124 | }
|
---|
1125 | }
|
---|
1126 |
|
---|
1127 | return pDiscard;
|
---|
1128 | }
|
---|
1129 |
|
---|
1130 | /**
|
---|
1131 | * Removes the least recently used blocks from the waiting list until
|
---|
1132 | * the new value is reached.
|
---|
1133 | *
|
---|
1134 | * @returns VBox status code.
|
---|
1135 | * @param pDisk VD disk container.
|
---|
1136 | * @param pDiscard The discard state.
|
---|
1137 | * @param cbDiscardingNew How many bytes should be waiting on success.
|
---|
1138 | * The number of bytes waiting can be less.
|
---|
1139 | */
|
---|
1140 | static int vdDiscardRemoveBlocks(PVBOXHDD pDisk, PVDDISCARDSTATE pDiscard, size_t cbDiscardingNew)
|
---|
1141 | {
|
---|
1142 | int rc = VINF_SUCCESS;
|
---|
1143 |
|
---|
1144 | LogFlowFunc(("pDisk=%#p pDiscard=%#p cbDiscardingNew=%zu\n",
|
---|
1145 | pDisk, pDiscard, cbDiscardingNew));
|
---|
1146 |
|
---|
1147 | while (pDiscard->cbDiscarding > cbDiscardingNew)
|
---|
1148 | {
|
---|
1149 | PVDDISCARDBLOCK pBlock = RTListGetLast(&pDiscard->ListLru, VDDISCARDBLOCK, NodeLru);
|
---|
1150 |
|
---|
1151 | Assert(!RTListIsEmpty(&pDiscard->ListLru));
|
---|
1152 |
|
---|
1153 | /* Go over the allocation bitmap and mark all discarded sectors as unused. */
|
---|
1154 | uint64_t offStart = pBlock->Core.Key;
|
---|
1155 | uint32_t idxStart = 0;
|
---|
1156 | size_t cbLeft = pBlock->cbDiscard;
|
---|
1157 | bool fAllocated = ASMBitTest(pBlock->pbmAllocated, idxStart);
|
---|
1158 | uint32_t cSectors = pBlock->cbDiscard / 512;
|
---|
1159 |
|
---|
1160 | while (cbLeft > 0)
|
---|
1161 | {
|
---|
1162 | int32_t idxEnd;
|
---|
1163 | size_t cbThis = cbLeft;
|
---|
1164 |
|
---|
1165 | if (fAllocated)
|
---|
1166 | {
|
---|
1167 | /* Check for the first unallocated bit. */
|
---|
1168 | idxEnd = ASMBitNextClear(pBlock->pbmAllocated, cSectors, idxStart);
|
---|
1169 | if (idxEnd != -1)
|
---|
1170 | {
|
---|
1171 | cbThis = (idxEnd - idxStart) * 512;
|
---|
1172 | fAllocated = false;
|
---|
1173 | }
|
---|
1174 | }
|
---|
1175 | else
|
---|
1176 | {
|
---|
1177 | /* Mark as unused and check for the first set bit. */
|
---|
1178 | idxEnd = ASMBitNextSet(pBlock->pbmAllocated, cSectors, idxStart);
|
---|
1179 | if (idxEnd != -1)
|
---|
1180 | cbThis = (idxEnd - idxStart) * 512;
|
---|
1181 |
|
---|
1182 |
|
---|
1183 | VDIOCTX IoCtx;
|
---|
1184 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_DISCARD, 0, 0, NULL,
|
---|
1185 | NULL, NULL, NULL, VDIOCTX_FLAGS_SYNC);
|
---|
1186 | rc = pDisk->pLast->Backend->pfnDiscard(pDisk->pLast->pBackendData,
|
---|
1187 | &IoCtx, offStart, cbThis, NULL,
|
---|
1188 | NULL, &cbThis, NULL,
|
---|
1189 | VD_DISCARD_MARK_UNUSED);
|
---|
1190 | if (RT_FAILURE(rc))
|
---|
1191 | break;
|
---|
1192 |
|
---|
1193 | fAllocated = true;
|
---|
1194 | }
|
---|
1195 |
|
---|
1196 | idxStart = idxEnd;
|
---|
1197 | offStart += cbThis;
|
---|
1198 | cbLeft -= cbThis;
|
---|
1199 | }
|
---|
1200 |
|
---|
1201 | if (RT_FAILURE(rc))
|
---|
1202 | break;
|
---|
1203 |
|
---|
1204 | PVDDISCARDBLOCK pBlockRemove = (PVDDISCARDBLOCK)RTAvlrU64RangeRemove(pDiscard->pTreeBlocks, pBlock->Core.Key);
|
---|
1205 | Assert(pBlockRemove == pBlock);
|
---|
1206 | RTListNodeRemove(&pBlock->NodeLru);
|
---|
1207 |
|
---|
1208 | pDiscard->cbDiscarding -= pBlock->cbDiscard;
|
---|
1209 | RTMemFree(pBlock->pbmAllocated);
|
---|
1210 | RTMemFree(pBlock);
|
---|
1211 | }
|
---|
1212 |
|
---|
1213 | Assert(RT_FAILURE(rc) || pDiscard->cbDiscarding <= cbDiscardingNew);
|
---|
1214 |
|
---|
1215 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
1216 | return rc;
|
---|
1217 | }
|
---|
1218 |
|
---|
1219 | /**
|
---|
1220 | * Destroys the current discard state, writing any waiting blocks to the image.
|
---|
1221 | *
|
---|
1222 | * @returns VBox status code.
|
---|
1223 | * @param pDisk VD disk container.
|
---|
1224 | */
|
---|
1225 | static int vdDiscardStateDestroy(PVBOXHDD pDisk)
|
---|
1226 | {
|
---|
1227 | int rc = VINF_SUCCESS;
|
---|
1228 |
|
---|
1229 | if (pDisk->pDiscard)
|
---|
1230 | {
|
---|
1231 | rc = vdDiscardRemoveBlocks(pDisk, pDisk->pDiscard, 0 /* Remove all blocks. */);
|
---|
1232 | AssertRC(rc);
|
---|
1233 | RTMemFree(pDisk->pDiscard->pTreeBlocks);
|
---|
1234 | RTMemFree(pDisk->pDiscard);
|
---|
1235 | pDisk->pDiscard = NULL;
|
---|
1236 | }
|
---|
1237 |
|
---|
1238 | return rc;
|
---|
1239 | }
|
---|
1240 |
|
---|
1241 | /**
|
---|
1242 | * Marks the given range as allocated in the image.
|
---|
1243 | * Required if there are discards in progress and a write to a block which can get discarded
|
---|
1244 | * is written to.
|
---|
1245 | *
|
---|
1246 | * @returns VBox status code.
|
---|
1247 | * @param pDisk VD container data.
|
---|
1248 | * @param uOffset First byte to mark as allocated.
|
---|
1249 | * @param cbRange Number of bytes to mark as allocated.
|
---|
1250 | */
|
---|
1251 | static int vdDiscardSetRangeAllocated(PVBOXHDD pDisk, uint64_t uOffset, size_t cbRange)
|
---|
1252 | {
|
---|
1253 | PVDDISCARDSTATE pDiscard = pDisk->pDiscard;
|
---|
1254 | int rc = VINF_SUCCESS;
|
---|
1255 |
|
---|
1256 | if (pDiscard)
|
---|
1257 | {
|
---|
1258 | do
|
---|
1259 | {
|
---|
1260 | size_t cbThisRange = cbRange;
|
---|
1261 | PVDDISCARDBLOCK pBlock = (PVDDISCARDBLOCK)RTAvlrU64RangeGet(pDiscard->pTreeBlocks, uOffset);
|
---|
1262 |
|
---|
1263 | if (pBlock)
|
---|
1264 | {
|
---|
1265 | int32_t idxStart, idxEnd;
|
---|
1266 |
|
---|
1267 | Assert(!(cbThisRange % 512));
|
---|
1268 | Assert(!((uOffset - pBlock->Core.Key) % 512));
|
---|
1269 |
|
---|
1270 | cbThisRange = RT_MIN(cbThisRange, pBlock->Core.KeyLast - uOffset + 1);
|
---|
1271 |
|
---|
1272 | idxStart = (uOffset - pBlock->Core.Key) / 512;
|
---|
1273 | idxEnd = idxStart + (cbThisRange / 512);
|
---|
1274 | ASMBitSetRange(pBlock->pbmAllocated, idxStart, idxEnd);
|
---|
1275 | }
|
---|
1276 | else
|
---|
1277 | {
|
---|
1278 | pBlock = (PVDDISCARDBLOCK)RTAvlrU64GetBestFit(pDiscard->pTreeBlocks, uOffset, true);
|
---|
1279 | if (pBlock)
|
---|
1280 | cbThisRange = RT_MIN(cbThisRange, pBlock->Core.Key - uOffset);
|
---|
1281 | }
|
---|
1282 |
|
---|
1283 | Assert(cbRange >= cbThisRange);
|
---|
1284 |
|
---|
1285 | uOffset += cbThisRange;
|
---|
1286 | cbRange -= cbThisRange;
|
---|
1287 | } while (cbRange != 0);
|
---|
1288 | }
|
---|
1289 |
|
---|
1290 | return rc;
|
---|
1291 | }
|
---|
1292 |
|
---|
1293 | DECLINLINE(PVDIOCTX) vdIoCtxAlloc(PVBOXHDD pDisk, VDIOCTXTXDIR enmTxDir,
|
---|
1294 | uint64_t uOffset, size_t cbTransfer,
|
---|
1295 | PVDIMAGE pImageStart,PCRTSGBUF pcSgBuf,
|
---|
1296 | void *pvAllocation, PFNVDIOCTXTRANSFER pfnIoCtxTransfer,
|
---|
1297 | uint32_t fFlags)
|
---|
1298 | {
|
---|
1299 | PVDIOCTX pIoCtx = NULL;
|
---|
1300 |
|
---|
1301 | pIoCtx = (PVDIOCTX)RTMemCacheAlloc(pDisk->hMemCacheIoCtx);
|
---|
1302 | if (RT_LIKELY(pIoCtx))
|
---|
1303 | {
|
---|
1304 | vdIoCtxInit(pIoCtx, pDisk, enmTxDir, uOffset, cbTransfer, pImageStart,
|
---|
1305 | pcSgBuf, pvAllocation, pfnIoCtxTransfer, fFlags);
|
---|
1306 | }
|
---|
1307 |
|
---|
1308 | return pIoCtx;
|
---|
1309 | }
|
---|
1310 |
|
---|
1311 | DECLINLINE(PVDIOCTX) vdIoCtxRootAlloc(PVBOXHDD pDisk, VDIOCTXTXDIR enmTxDir,
|
---|
1312 | uint64_t uOffset, size_t cbTransfer,
|
---|
1313 | PVDIMAGE pImageStart, PCRTSGBUF pcSgBuf,
|
---|
1314 | PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
1315 | void *pvUser1, void *pvUser2,
|
---|
1316 | void *pvAllocation,
|
---|
1317 | PFNVDIOCTXTRANSFER pfnIoCtxTransfer,
|
---|
1318 | uint32_t fFlags)
|
---|
1319 | {
|
---|
1320 | PVDIOCTX pIoCtx = vdIoCtxAlloc(pDisk, enmTxDir, uOffset, cbTransfer, pImageStart,
|
---|
1321 | pcSgBuf, pvAllocation, pfnIoCtxTransfer, fFlags);
|
---|
1322 |
|
---|
1323 | if (RT_LIKELY(pIoCtx))
|
---|
1324 | {
|
---|
1325 | pIoCtx->pIoCtxParent = NULL;
|
---|
1326 | pIoCtx->Type.Root.pfnComplete = pfnComplete;
|
---|
1327 | pIoCtx->Type.Root.pvUser1 = pvUser1;
|
---|
1328 | pIoCtx->Type.Root.pvUser2 = pvUser2;
|
---|
1329 | }
|
---|
1330 |
|
---|
1331 | LogFlow(("Allocated root I/O context %#p\n", pIoCtx));
|
---|
1332 | return pIoCtx;
|
---|
1333 | }
|
---|
1334 |
|
---|
1335 | DECLINLINE(PVDIOCTX) vdIoCtxDiscardAlloc(PVBOXHDD pDisk, PCRTRANGE paRanges,
|
---|
1336 | unsigned cRanges,
|
---|
1337 | PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
1338 | void *pvUser1, void *pvUser2,
|
---|
1339 | void *pvAllocation,
|
---|
1340 | PFNVDIOCTXTRANSFER pfnIoCtxTransfer,
|
---|
1341 | uint32_t fFlags)
|
---|
1342 | {
|
---|
1343 | PVDIOCTX pIoCtx = NULL;
|
---|
1344 |
|
---|
1345 | pIoCtx = (PVDIOCTX)RTMemCacheAlloc(pDisk->hMemCacheIoCtx);
|
---|
1346 | if (RT_LIKELY(pIoCtx))
|
---|
1347 | {
|
---|
1348 | pIoCtx->pIoCtxNext = NULL;
|
---|
1349 | pIoCtx->pDisk = pDisk;
|
---|
1350 | pIoCtx->enmTxDir = VDIOCTXTXDIR_DISCARD;
|
---|
1351 | pIoCtx->cDataTransfersPending = 0;
|
---|
1352 | pIoCtx->cMetaTransfersPending = 0;
|
---|
1353 | pIoCtx->fComplete = false;
|
---|
1354 | pIoCtx->fFlags = fFlags;
|
---|
1355 | pIoCtx->pvAllocation = pvAllocation;
|
---|
1356 | pIoCtx->pfnIoCtxTransfer = pfnIoCtxTransfer;
|
---|
1357 | pIoCtx->pfnIoCtxTransferNext = NULL;
|
---|
1358 | pIoCtx->rcReq = VINF_SUCCESS;
|
---|
1359 | pIoCtx->Req.Discard.paRanges = paRanges;
|
---|
1360 | pIoCtx->Req.Discard.cRanges = cRanges;
|
---|
1361 | pIoCtx->Req.Discard.idxRange = 0;
|
---|
1362 | pIoCtx->Req.Discard.cbDiscardLeft = 0;
|
---|
1363 | pIoCtx->Req.Discard.offCur = 0;
|
---|
1364 | pIoCtx->Req.Discard.cbThisDiscard = 0;
|
---|
1365 |
|
---|
1366 | pIoCtx->pIoCtxParent = NULL;
|
---|
1367 | pIoCtx->Type.Root.pfnComplete = pfnComplete;
|
---|
1368 | pIoCtx->Type.Root.pvUser1 = pvUser1;
|
---|
1369 | pIoCtx->Type.Root.pvUser2 = pvUser2;
|
---|
1370 | }
|
---|
1371 |
|
---|
1372 | LogFlow(("Allocated discard I/O context %#p\n", pIoCtx));
|
---|
1373 | return pIoCtx;
|
---|
1374 | }
|
---|
1375 |
|
---|
1376 | DECLINLINE(PVDIOCTX) vdIoCtxChildAlloc(PVBOXHDD pDisk, VDIOCTXTXDIR enmTxDir,
|
---|
1377 | uint64_t uOffset, size_t cbTransfer,
|
---|
1378 | PVDIMAGE pImageStart, PCRTSGBUF pcSgBuf,
|
---|
1379 | PVDIOCTX pIoCtxParent, size_t cbTransferParent,
|
---|
1380 | size_t cbWriteParent, void *pvAllocation,
|
---|
1381 | PFNVDIOCTXTRANSFER pfnIoCtxTransfer)
|
---|
1382 | {
|
---|
1383 | PVDIOCTX pIoCtx = vdIoCtxAlloc(pDisk, enmTxDir, uOffset, cbTransfer, pImageStart,
|
---|
1384 | pcSgBuf, pvAllocation, pfnIoCtxTransfer, 0);
|
---|
1385 |
|
---|
1386 | AssertPtr(pIoCtxParent);
|
---|
1387 | Assert(!pIoCtxParent->pIoCtxParent);
|
---|
1388 |
|
---|
1389 | if (RT_LIKELY(pIoCtx))
|
---|
1390 | {
|
---|
1391 | pIoCtx->pIoCtxParent = pIoCtxParent;
|
---|
1392 | pIoCtx->Type.Child.uOffsetSaved = uOffset;
|
---|
1393 | pIoCtx->Type.Child.cbTransferLeftSaved = cbTransfer;
|
---|
1394 | pIoCtx->Type.Child.cbTransferParent = cbTransferParent;
|
---|
1395 | pIoCtx->Type.Child.cbWriteParent = cbWriteParent;
|
---|
1396 | }
|
---|
1397 |
|
---|
1398 | LogFlow(("Allocated child I/O context %#p\n", pIoCtx));
|
---|
1399 | return pIoCtx;
|
---|
1400 | }
|
---|
1401 |
|
---|
1402 | DECLINLINE(PVDIOTASK) vdIoTaskUserAlloc(PVDIOSTORAGE pIoStorage, PFNVDXFERCOMPLETED pfnComplete, void *pvUser, PVDIOCTX pIoCtx, uint32_t cbTransfer)
|
---|
1403 | {
|
---|
1404 | PVDIOTASK pIoTask = NULL;
|
---|
1405 |
|
---|
1406 | pIoTask = (PVDIOTASK)RTMemCacheAlloc(pIoStorage->pVDIo->pDisk->hMemCacheIoTask);
|
---|
1407 | if (pIoTask)
|
---|
1408 | {
|
---|
1409 | pIoTask->pIoStorage = pIoStorage;
|
---|
1410 | pIoTask->pfnComplete = pfnComplete;
|
---|
1411 | pIoTask->pvUser = pvUser;
|
---|
1412 | pIoTask->fMeta = false;
|
---|
1413 | pIoTask->Type.User.cbTransfer = cbTransfer;
|
---|
1414 | pIoTask->Type.User.pIoCtx = pIoCtx;
|
---|
1415 | }
|
---|
1416 |
|
---|
1417 | return pIoTask;
|
---|
1418 | }
|
---|
1419 |
|
---|
1420 | DECLINLINE(PVDIOTASK) vdIoTaskMetaAlloc(PVDIOSTORAGE pIoStorage, PFNVDXFERCOMPLETED pfnComplete, void *pvUser, PVDMETAXFER pMetaXfer)
|
---|
1421 | {
|
---|
1422 | PVDIOTASK pIoTask = NULL;
|
---|
1423 |
|
---|
1424 | pIoTask = (PVDIOTASK)RTMemCacheAlloc(pIoStorage->pVDIo->pDisk->hMemCacheIoTask);
|
---|
1425 | if (pIoTask)
|
---|
1426 | {
|
---|
1427 | pIoTask->pIoStorage = pIoStorage;
|
---|
1428 | pIoTask->pfnComplete = pfnComplete;
|
---|
1429 | pIoTask->pvUser = pvUser;
|
---|
1430 | pIoTask->fMeta = true;
|
---|
1431 | pIoTask->Type.Meta.pMetaXfer = pMetaXfer;
|
---|
1432 | }
|
---|
1433 |
|
---|
1434 | return pIoTask;
|
---|
1435 | }
|
---|
1436 |
|
---|
1437 | DECLINLINE(void) vdIoCtxFree(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
|
---|
1438 | {
|
---|
1439 | LogFlow(("Freeing I/O context %#p\n", pIoCtx));
|
---|
1440 | if (pIoCtx->pvAllocation)
|
---|
1441 | RTMemFree(pIoCtx->pvAllocation);
|
---|
1442 | #ifdef DEBUG
|
---|
1443 | memset(pIoCtx, 0xff, sizeof(VDIOCTX));
|
---|
1444 | #endif
|
---|
1445 | RTMemCacheFree(pDisk->hMemCacheIoCtx, pIoCtx);
|
---|
1446 | }
|
---|
1447 |
|
---|
1448 | DECLINLINE(void) vdIoTaskFree(PVBOXHDD pDisk, PVDIOTASK pIoTask)
|
---|
1449 | {
|
---|
1450 | RTMemCacheFree(pDisk->hMemCacheIoTask, pIoTask);
|
---|
1451 | }
|
---|
1452 |
|
---|
1453 | DECLINLINE(void) vdIoCtxChildReset(PVDIOCTX pIoCtx)
|
---|
1454 | {
|
---|
1455 | AssertPtr(pIoCtx->pIoCtxParent);
|
---|
1456 |
|
---|
1457 | RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
|
---|
1458 | pIoCtx->Req.Io.uOffset = pIoCtx->Type.Child.uOffsetSaved;
|
---|
1459 | pIoCtx->Req.Io.cbTransferLeft = pIoCtx->Type.Child.cbTransferLeftSaved;
|
---|
1460 | }
|
---|
1461 |
|
---|
1462 | DECLINLINE(PVDMETAXFER) vdMetaXferAlloc(PVDIOSTORAGE pIoStorage, uint64_t uOffset, size_t cb)
|
---|
1463 | {
|
---|
1464 | PVDMETAXFER pMetaXfer = (PVDMETAXFER)RTMemAlloc(RT_OFFSETOF(VDMETAXFER, abData[cb]));
|
---|
1465 |
|
---|
1466 | if (RT_LIKELY(pMetaXfer))
|
---|
1467 | {
|
---|
1468 | pMetaXfer->Core.Key = uOffset;
|
---|
1469 | pMetaXfer->Core.KeyLast = uOffset + cb - 1;
|
---|
1470 | pMetaXfer->fFlags = VDMETAXFER_TXDIR_NONE;
|
---|
1471 | pMetaXfer->cbMeta = cb;
|
---|
1472 | pMetaXfer->pIoStorage = pIoStorage;
|
---|
1473 | pMetaXfer->cRefs = 0;
|
---|
1474 | RTListInit(&pMetaXfer->ListIoCtxWaiting);
|
---|
1475 | }
|
---|
1476 | return pMetaXfer;
|
---|
1477 | }
|
---|
1478 |
|
---|
1479 | DECLINLINE(void) vdIoCtxAddToWaitingList(volatile PVDIOCTX *ppList, PVDIOCTX pIoCtx)
|
---|
1480 | {
|
---|
1481 | /* Put it on the waiting list. */
|
---|
1482 | PVDIOCTX pNext = ASMAtomicUoReadPtrT(ppList, PVDIOCTX);
|
---|
1483 | PVDIOCTX pHeadOld;
|
---|
1484 | pIoCtx->pIoCtxNext = pNext;
|
---|
1485 | while (!ASMAtomicCmpXchgExPtr(ppList, pIoCtx, pNext, &pHeadOld))
|
---|
1486 | {
|
---|
1487 | pNext = pHeadOld;
|
---|
1488 | Assert(pNext != pIoCtx);
|
---|
1489 | pIoCtx->pIoCtxNext = pNext;
|
---|
1490 | ASMNopPause();
|
---|
1491 | }
|
---|
1492 | }
|
---|
1493 |
|
---|
1494 | DECLINLINE(void) vdIoCtxDefer(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
|
---|
1495 | {
|
---|
1496 | LogFlowFunc(("Deferring write pIoCtx=%#p\n", pIoCtx));
|
---|
1497 |
|
---|
1498 | Assert(!pIoCtx->pIoCtxParent && !(pIoCtx->fFlags & VDIOCTX_FLAGS_BLOCKED));
|
---|
1499 | pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
|
---|
1500 | vdIoCtxAddToWaitingList(&pDisk->pIoCtxBlockedHead, pIoCtx);
|
---|
1501 | }
|
---|
1502 |
|
---|
1503 | static size_t vdIoCtxCopy(PVDIOCTX pIoCtxDst, PVDIOCTX pIoCtxSrc, size_t cbData)
|
---|
1504 | {
|
---|
1505 | return RTSgBufCopy(&pIoCtxDst->Req.Io.SgBuf, &pIoCtxSrc->Req.Io.SgBuf, cbData);
|
---|
1506 | }
|
---|
1507 |
|
---|
1508 | static int vdIoCtxCmp(PVDIOCTX pIoCtx1, PVDIOCTX pIoCtx2, size_t cbData)
|
---|
1509 | {
|
---|
1510 | return RTSgBufCmp(&pIoCtx1->Req.Io.SgBuf, &pIoCtx2->Req.Io.SgBuf, cbData);
|
---|
1511 | }
|
---|
1512 |
|
---|
1513 | static size_t vdIoCtxCopyTo(PVDIOCTX pIoCtx, const uint8_t *pbData, size_t cbData)
|
---|
1514 | {
|
---|
1515 | return RTSgBufCopyFromBuf(&pIoCtx->Req.Io.SgBuf, pbData, cbData);
|
---|
1516 | }
|
---|
1517 |
|
---|
1518 | static size_t vdIoCtxCopyFrom(PVDIOCTX pIoCtx, uint8_t *pbData, size_t cbData)
|
---|
1519 | {
|
---|
1520 | return RTSgBufCopyToBuf(&pIoCtx->Req.Io.SgBuf, pbData, cbData);
|
---|
1521 | }
|
---|
1522 |
|
---|
1523 | static size_t vdIoCtxSet(PVDIOCTX pIoCtx, uint8_t ch, size_t cbData)
|
---|
1524 | {
|
---|
1525 | return RTSgBufSet(&pIoCtx->Req.Io.SgBuf, ch, cbData);
|
---|
1526 | }
|
---|
1527 |
|
---|
1528 | /**
|
---|
1529 | * Process the I/O context, core method which assumes that the I/O context
|
---|
1530 | * acquired the lock.
|
---|
1531 | *
|
---|
1532 | * @returns VBox status code.
|
---|
1533 | * @param pIoCtx I/O context to process.
|
---|
1534 | */
|
---|
1535 | static int vdIoCtxProcessLocked(PVDIOCTX pIoCtx)
|
---|
1536 | {
|
---|
1537 | int rc = VINF_SUCCESS;
|
---|
1538 |
|
---|
1539 | VD_IS_LOCKED(pIoCtx->pDisk);
|
---|
1540 |
|
---|
1541 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
1542 |
|
---|
1543 | if ( !pIoCtx->cMetaTransfersPending
|
---|
1544 | && !pIoCtx->cDataTransfersPending
|
---|
1545 | && !pIoCtx->pfnIoCtxTransfer)
|
---|
1546 | {
|
---|
1547 | rc = VINF_VD_ASYNC_IO_FINISHED;
|
---|
1548 | goto out;
|
---|
1549 | }
|
---|
1550 |
|
---|
1551 | /*
|
---|
1552 | * We complete the I/O context in case of an error
|
---|
1553 | * if there is no I/O task pending.
|
---|
1554 | */
|
---|
1555 | if ( RT_FAILURE(pIoCtx->rcReq)
|
---|
1556 | && !pIoCtx->cMetaTransfersPending
|
---|
1557 | && !pIoCtx->cDataTransfersPending)
|
---|
1558 | {
|
---|
1559 | rc = VINF_VD_ASYNC_IO_FINISHED;
|
---|
1560 | goto out;
|
---|
1561 | }
|
---|
1562 |
|
---|
1563 | /* Don't change anything if there is a metadata transfer pending or we are blocked. */
|
---|
1564 | if ( pIoCtx->cMetaTransfersPending
|
---|
1565 | || (pIoCtx->fFlags & VDIOCTX_FLAGS_BLOCKED))
|
---|
1566 | {
|
---|
1567 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1568 | goto out;
|
---|
1569 | }
|
---|
1570 |
|
---|
1571 | if (pIoCtx->pfnIoCtxTransfer)
|
---|
1572 | {
|
---|
1573 | /* Call the transfer function advancing to the next while there is no error. */
|
---|
1574 | while ( pIoCtx->pfnIoCtxTransfer
|
---|
1575 | && !pIoCtx->cMetaTransfersPending
|
---|
1576 | && RT_SUCCESS(rc))
|
---|
1577 | {
|
---|
1578 | LogFlowFunc(("calling transfer function %#p\n", pIoCtx->pfnIoCtxTransfer));
|
---|
1579 | rc = pIoCtx->pfnIoCtxTransfer(pIoCtx);
|
---|
1580 |
|
---|
1581 | /* Advance to the next part of the transfer if the current one succeeded. */
|
---|
1582 | if (RT_SUCCESS(rc))
|
---|
1583 | {
|
---|
1584 | pIoCtx->pfnIoCtxTransfer = pIoCtx->pfnIoCtxTransferNext;
|
---|
1585 | pIoCtx->pfnIoCtxTransferNext = NULL;
|
---|
1586 | }
|
---|
1587 | }
|
---|
1588 | }
|
---|
1589 |
|
---|
1590 | if ( RT_SUCCESS(rc)
|
---|
1591 | && !pIoCtx->cMetaTransfersPending
|
---|
1592 | && !pIoCtx->cDataTransfersPending)
|
---|
1593 | rc = VINF_VD_ASYNC_IO_FINISHED;
|
---|
1594 | else if ( RT_SUCCESS(rc)
|
---|
1595 | || rc == VERR_VD_NOT_ENOUGH_METADATA
|
---|
1596 | || rc == VERR_VD_IOCTX_HALT)
|
---|
1597 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1598 | else if (RT_FAILURE(rc) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
1599 | {
|
---|
1600 | ASMAtomicCmpXchgS32(&pIoCtx->rcReq, rc, VINF_SUCCESS);
|
---|
1601 | /*
|
---|
1602 | * The I/O context completed if we have an error and there is no data
|
---|
1603 | * or meta data transfer pending.
|
---|
1604 | */
|
---|
1605 | if ( !pIoCtx->cMetaTransfersPending
|
---|
1606 | && !pIoCtx->cDataTransfersPending)
|
---|
1607 | rc = VINF_VD_ASYNC_IO_FINISHED;
|
---|
1608 | else
|
---|
1609 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1610 | }
|
---|
1611 |
|
---|
1612 | out:
|
---|
1613 | LogFlowFunc(("pIoCtx=%#p rc=%Rrc cDataTransfersPending=%u cMetaTransfersPending=%u fComplete=%RTbool\n",
|
---|
1614 | pIoCtx, rc, pIoCtx->cDataTransfersPending, pIoCtx->cMetaTransfersPending,
|
---|
1615 | pIoCtx->fComplete));
|
---|
1616 |
|
---|
1617 | return rc;
|
---|
1618 | }
|
---|
1619 |
|
---|
1620 | /**
|
---|
1621 | * Processes the list of waiting I/O contexts.
|
---|
1622 | *
|
---|
1623 | * @returns VBox status code.
|
---|
1624 | * @param pDisk The disk structure.
|
---|
1625 | * @param pIoCtxRc An I/O context handle which waits on the list. When processed
|
---|
1626 | * The status code is returned. NULL if there is no I/O context
|
---|
1627 | * to return the status code for.
|
---|
1628 | */
|
---|
1629 | static int vdDiskProcessWaitingIoCtx(PVBOXHDD pDisk, PVDIOCTX pIoCtxRc)
|
---|
1630 | {
|
---|
1631 | int rc = VINF_SUCCESS;
|
---|
1632 |
|
---|
1633 | LogFlowFunc(("pDisk=%#p pIoCtxRc=%#p\n", pDisk, pIoCtxRc));
|
---|
1634 |
|
---|
1635 | VD_IS_LOCKED(pDisk);
|
---|
1636 |
|
---|
1637 | /* Get the waiting list and process it in FIFO order. */
|
---|
1638 | PVDIOCTX pIoCtxHead = ASMAtomicXchgPtrT(&pDisk->pIoCtxHead, NULL, PVDIOCTX);
|
---|
1639 |
|
---|
1640 | /* Reverse it. */
|
---|
1641 | PVDIOCTX pCur = pIoCtxHead;
|
---|
1642 | pIoCtxHead = NULL;
|
---|
1643 | while (pCur)
|
---|
1644 | {
|
---|
1645 | PVDIOCTX pInsert = pCur;
|
---|
1646 | pCur = pCur->pIoCtxNext;
|
---|
1647 | pInsert->pIoCtxNext = pIoCtxHead;
|
---|
1648 | pIoCtxHead = pInsert;
|
---|
1649 | }
|
---|
1650 |
|
---|
1651 | /* Process now. */
|
---|
1652 | pCur = pIoCtxHead;
|
---|
1653 | while (pCur)
|
---|
1654 | {
|
---|
1655 | int rcTmp;
|
---|
1656 | PVDIOCTX pTmp = pCur;
|
---|
1657 |
|
---|
1658 | pCur = pCur->pIoCtxNext;
|
---|
1659 | pTmp->pIoCtxNext = NULL;
|
---|
1660 |
|
---|
1661 | rcTmp = vdIoCtxProcessLocked(pTmp);
|
---|
1662 | if (pTmp == pIoCtxRc)
|
---|
1663 | {
|
---|
1664 | /* The given I/O context was processed, pass the return code to the caller. */
|
---|
1665 | rc = rcTmp;
|
---|
1666 | }
|
---|
1667 | else if ( rcTmp == VINF_VD_ASYNC_IO_FINISHED
|
---|
1668 | && ASMAtomicCmpXchgBool(&pTmp->fComplete, true, false))
|
---|
1669 | {
|
---|
1670 | LogFlowFunc(("Waiting I/O context completed pTmp=%#p\n", pTmp));
|
---|
1671 | vdThreadFinishWrite(pDisk);
|
---|
1672 | pTmp->Type.Root.pfnComplete(pTmp->Type.Root.pvUser1,
|
---|
1673 | pTmp->Type.Root.pvUser2,
|
---|
1674 | pTmp->rcReq);
|
---|
1675 | vdIoCtxFree(pDisk, pTmp);
|
---|
1676 | }
|
---|
1677 | }
|
---|
1678 |
|
---|
1679 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
1680 | return rc;
|
---|
1681 | }
|
---|
1682 |
|
---|
1683 | /**
|
---|
1684 | * Processes the list of blocked I/O contexts.
|
---|
1685 | *
|
---|
1686 | * @returns nothing.
|
---|
1687 | * @param pDisk The disk structure.
|
---|
1688 | */
|
---|
1689 | static void vdDiskProcessBlockedIoCtx(PVBOXHDD pDisk)
|
---|
1690 | {
|
---|
1691 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
1692 |
|
---|
1693 | VD_IS_LOCKED(pDisk);
|
---|
1694 |
|
---|
1695 | /* Get the waiting list and process it in FIFO order. */
|
---|
1696 | PVDIOCTX pIoCtxHead = ASMAtomicXchgPtrT(&pDisk->pIoCtxBlockedHead, NULL, PVDIOCTX);
|
---|
1697 |
|
---|
1698 | /* Reverse it. */
|
---|
1699 | PVDIOCTX pCur = pIoCtxHead;
|
---|
1700 | pIoCtxHead = NULL;
|
---|
1701 | while (pCur)
|
---|
1702 | {
|
---|
1703 | PVDIOCTX pInsert = pCur;
|
---|
1704 | pCur = pCur->pIoCtxNext;
|
---|
1705 | pInsert->pIoCtxNext = pIoCtxHead;
|
---|
1706 | pIoCtxHead = pInsert;
|
---|
1707 | }
|
---|
1708 |
|
---|
1709 | /* Process now. */
|
---|
1710 | pCur = pIoCtxHead;
|
---|
1711 | while (pCur)
|
---|
1712 | {
|
---|
1713 | int rc;
|
---|
1714 | PVDIOCTX pTmp = pCur;
|
---|
1715 |
|
---|
1716 | pCur = pCur->pIoCtxNext;
|
---|
1717 | pTmp->pIoCtxNext = NULL;
|
---|
1718 |
|
---|
1719 | Assert(!pTmp->pIoCtxParent);
|
---|
1720 | Assert(pTmp->fFlags & VDIOCTX_FLAGS_BLOCKED);
|
---|
1721 | pTmp->fFlags &= ~VDIOCTX_FLAGS_BLOCKED;
|
---|
1722 |
|
---|
1723 | rc = vdIoCtxProcessLocked(pTmp);
|
---|
1724 | if ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
1725 | && ASMAtomicCmpXchgBool(&pTmp->fComplete, true, false))
|
---|
1726 | {
|
---|
1727 | LogFlowFunc(("Waiting I/O context completed pTmp=%#p\n", pTmp));
|
---|
1728 | vdThreadFinishWrite(pDisk);
|
---|
1729 | pTmp->Type.Root.pfnComplete(pTmp->Type.Root.pvUser1,
|
---|
1730 | pTmp->Type.Root.pvUser2,
|
---|
1731 | pTmp->rcReq);
|
---|
1732 | vdIoCtxFree(pDisk, pTmp);
|
---|
1733 | }
|
---|
1734 | }
|
---|
1735 |
|
---|
1736 | LogFlowFunc(("returns\n"));
|
---|
1737 | }
|
---|
1738 |
|
---|
1739 | /**
|
---|
1740 | * Processes the I/O context trying to lock the criticial section.
|
---|
1741 | * The context is deferred if the critical section is busy.
|
---|
1742 | *
|
---|
1743 | * @returns VBox status code.
|
---|
1744 | * @param pIoCtx The I/O context to process.
|
---|
1745 | */
|
---|
1746 | static int vdIoCtxProcessTryLockDefer(PVDIOCTX pIoCtx)
|
---|
1747 | {
|
---|
1748 | int rc = VINF_SUCCESS;
|
---|
1749 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
1750 |
|
---|
1751 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
1752 |
|
---|
1753 | /* Put it on the waiting list first. */
|
---|
1754 | vdIoCtxAddToWaitingList(&pDisk->pIoCtxHead, pIoCtx);
|
---|
1755 |
|
---|
1756 | if (ASMAtomicCmpXchgBool(&pDisk->fLocked, true, false))
|
---|
1757 | {
|
---|
1758 | /* Leave it again, the context will be processed just before leaving the lock. */
|
---|
1759 | LogFlowFunc(("Successfully acquired the lock\n"));
|
---|
1760 | rc = vdDiskUnlock(pDisk, pIoCtx);
|
---|
1761 | }
|
---|
1762 | else
|
---|
1763 | {
|
---|
1764 | LogFlowFunc(("Lock is held\n"));
|
---|
1765 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1766 | }
|
---|
1767 |
|
---|
1768 | return rc;
|
---|
1769 | }
|
---|
1770 |
|
---|
1771 | /**
|
---|
1772 | * Process the I/O context in a synchronous manner, waiting
|
---|
1773 | * for it to complete.
|
---|
1774 | *
|
---|
1775 | * @returns VBox status code of the completed request.
|
---|
1776 | * @param pIoCtx The sync I/O context.
|
---|
1777 | */
|
---|
1778 | static int vdIoCtxProcessSync(PVDIOCTX pIoCtx)
|
---|
1779 | {
|
---|
1780 | int rc = VINF_SUCCESS;
|
---|
1781 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
1782 |
|
---|
1783 | LogFlowFunc(("pIoCtx=%p\n", pIoCtx));
|
---|
1784 |
|
---|
1785 | AssertMsg(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC,
|
---|
1786 | ("I/O context is not marked as synchronous\n"));
|
---|
1787 |
|
---|
1788 | rc = vdIoCtxProcessTryLockDefer(pIoCtx);
|
---|
1789 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
1790 | rc = VINF_SUCCESS;
|
---|
1791 |
|
---|
1792 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
1793 | {
|
---|
1794 | rc = RTSemEventWait(pDisk->hEventSemSyncIo, RT_INDEFINITE_WAIT);
|
---|
1795 | AssertRC(rc);
|
---|
1796 |
|
---|
1797 | rc = pDisk->rcSync;
|
---|
1798 | }
|
---|
1799 | else /* Success or error. */
|
---|
1800 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
1801 |
|
---|
1802 | return rc;
|
---|
1803 | }
|
---|
1804 |
|
---|
1805 | DECLINLINE(bool) vdIoCtxIsDiskLockOwner(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
|
---|
1806 | {
|
---|
1807 | return pDisk->pIoCtxLockOwner == pIoCtx;
|
---|
1808 | }
|
---|
1809 |
|
---|
1810 | static int vdIoCtxLockDisk(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
|
---|
1811 | {
|
---|
1812 | int rc = VINF_SUCCESS;
|
---|
1813 |
|
---|
1814 | VD_IS_LOCKED(pDisk);
|
---|
1815 |
|
---|
1816 | LogFlowFunc(("pDisk=%#p pIoCtx=%#p\n", pDisk, pIoCtx));
|
---|
1817 |
|
---|
1818 | if (!ASMAtomicCmpXchgPtr(&pDisk->pIoCtxLockOwner, pIoCtx, NIL_VDIOCTX))
|
---|
1819 | {
|
---|
1820 | Assert(pDisk->pIoCtxLockOwner != pIoCtx); /* No nesting allowed. */
|
---|
1821 | vdIoCtxDefer(pDisk, pIoCtx);
|
---|
1822 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
1823 | }
|
---|
1824 |
|
---|
1825 | LogFlowFunc(("returns -> %Rrc\n", rc));
|
---|
1826 | return rc;
|
---|
1827 | }
|
---|
1828 |
|
---|
1829 | static void vdIoCtxUnlockDisk(PVBOXHDD pDisk, PVDIOCTX pIoCtx, bool fProcessBlockedReqs)
|
---|
1830 | {
|
---|
1831 | LogFlowFunc(("pDisk=%#p pIoCtx=%#p fProcessBlockedReqs=%RTbool\n",
|
---|
1832 | pDisk, pIoCtx, fProcessBlockedReqs));
|
---|
1833 |
|
---|
1834 | VD_IS_LOCKED(pDisk);
|
---|
1835 |
|
---|
1836 | LogFlow(("Unlocking disk lock owner is %#p\n", pDisk->pIoCtxLockOwner));
|
---|
1837 | Assert(pDisk->pIoCtxLockOwner == pIoCtx);
|
---|
1838 | ASMAtomicXchgPtrT(&pDisk->pIoCtxLockOwner, NIL_VDIOCTX, PVDIOCTX);
|
---|
1839 |
|
---|
1840 | if (fProcessBlockedReqs)
|
---|
1841 | {
|
---|
1842 | /* Process any blocked writes if the current request didn't caused another growing. */
|
---|
1843 | vdDiskProcessBlockedIoCtx(pDisk);
|
---|
1844 | }
|
---|
1845 |
|
---|
1846 | LogFlowFunc(("returns\n"));
|
---|
1847 | }
|
---|
1848 |
|
---|
1849 | /**
|
---|
1850 | * internal: read the specified amount of data in whatever blocks the backend
|
---|
1851 | * will give us - async version.
|
---|
1852 | */
|
---|
1853 | static int vdReadHelperAsync(PVDIOCTX pIoCtx)
|
---|
1854 | {
|
---|
1855 | int rc;
|
---|
1856 | size_t cbToRead = pIoCtx->Req.Io.cbTransfer;
|
---|
1857 | uint64_t uOffset = pIoCtx->Req.Io.uOffset;
|
---|
1858 | PVDIMAGE pCurrImage = pIoCtx->Req.Io.pImageCur;;
|
---|
1859 | size_t cbThisRead;
|
---|
1860 |
|
---|
1861 | /* Loop until all reads started or we have a backend which needs to read metadata. */
|
---|
1862 | do
|
---|
1863 | {
|
---|
1864 | /* Search for image with allocated block. Do not attempt to read more
|
---|
1865 | * than the previous reads marked as valid. Otherwise this would return
|
---|
1866 | * stale data when different block sizes are used for the images. */
|
---|
1867 | cbThisRead = cbToRead;
|
---|
1868 |
|
---|
1869 | /*
|
---|
1870 | * Try to read from the given image.
|
---|
1871 | * If the block is not allocated read from override chain if present.
|
---|
1872 | */
|
---|
1873 | rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
|
---|
1874 | uOffset, cbThisRead,
|
---|
1875 | pIoCtx, &cbThisRead);
|
---|
1876 |
|
---|
1877 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
1878 | {
|
---|
1879 | while ( pCurrImage->pPrev != NULL
|
---|
1880 | && rc == VERR_VD_BLOCK_FREE)
|
---|
1881 | {
|
---|
1882 | pCurrImage = pCurrImage->pPrev;
|
---|
1883 | rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
|
---|
1884 | uOffset, cbThisRead,
|
---|
1885 | pIoCtx, &cbThisRead);
|
---|
1886 | }
|
---|
1887 | }
|
---|
1888 |
|
---|
1889 | /* The task state will be updated on success already, don't do it here!. */
|
---|
1890 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
1891 | {
|
---|
1892 | /* No image in the chain contains the data for the block. */
|
---|
1893 | vdIoCtxSet(pIoCtx, '\0', cbThisRead);
|
---|
1894 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, cbThisRead);
|
---|
1895 | rc = VINF_SUCCESS;
|
---|
1896 | }
|
---|
1897 | else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
1898 | rc = VINF_SUCCESS;
|
---|
1899 | else if (rc == VERR_VD_IOCTX_HALT)
|
---|
1900 | {
|
---|
1901 | uOffset += cbThisRead;
|
---|
1902 | cbToRead -= cbThisRead;
|
---|
1903 | pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
|
---|
1904 | }
|
---|
1905 |
|
---|
1906 | if (RT_FAILURE(rc))
|
---|
1907 | break;
|
---|
1908 |
|
---|
1909 | cbToRead -= cbThisRead;
|
---|
1910 | uOffset += cbThisRead;
|
---|
1911 | pCurrImage = pIoCtx->Req.Io.pImageStart; /* Start with the highest image in the chain. */
|
---|
1912 | } while (cbToRead != 0 && RT_SUCCESS(rc));
|
---|
1913 |
|
---|
1914 | if ( rc == VERR_VD_NOT_ENOUGH_METADATA
|
---|
1915 | || rc == VERR_VD_IOCTX_HALT)
|
---|
1916 | {
|
---|
1917 | /* Save the current state. */
|
---|
1918 | pIoCtx->Req.Io.uOffset = uOffset;
|
---|
1919 | pIoCtx->Req.Io.cbTransfer = cbToRead;
|
---|
1920 | pIoCtx->Req.Io.pImageCur = pCurrImage ? pCurrImage : pIoCtx->Req.Io.pImageStart;
|
---|
1921 | }
|
---|
1922 |
|
---|
1923 | return rc;
|
---|
1924 | }
|
---|
1925 |
|
---|
1926 | /**
|
---|
1927 | * internal: parent image read wrapper for compacting.
|
---|
1928 | */
|
---|
1929 | static int vdParentRead(void *pvUser, uint64_t uOffset, void *pvBuf,
|
---|
1930 | size_t cbRead)
|
---|
1931 | {
|
---|
1932 | PVDPARENTSTATEDESC pParentState = (PVDPARENTSTATEDESC)pvUser;
|
---|
1933 | return vdReadHelper(pParentState->pDisk, pParentState->pImage, uOffset,
|
---|
1934 | pvBuf, cbRead, false /* fUpdateCache */);
|
---|
1935 | }
|
---|
1936 |
|
---|
1937 | /**
|
---|
1938 | * internal: mark the disk as not modified.
|
---|
1939 | */
|
---|
1940 | static void vdResetModifiedFlag(PVBOXHDD pDisk)
|
---|
1941 | {
|
---|
1942 | if (pDisk->uModified & VD_IMAGE_MODIFIED_FLAG)
|
---|
1943 | {
|
---|
1944 | /* generate new last-modified uuid */
|
---|
1945 | if (!(pDisk->uModified & VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
|
---|
1946 | {
|
---|
1947 | RTUUID Uuid;
|
---|
1948 |
|
---|
1949 | RTUuidCreate(&Uuid);
|
---|
1950 | pDisk->pLast->Backend->pfnSetModificationUuid(pDisk->pLast->pBackendData,
|
---|
1951 | &Uuid);
|
---|
1952 |
|
---|
1953 | if (pDisk->pCache)
|
---|
1954 | pDisk->pCache->Backend->pfnSetModificationUuid(pDisk->pCache->pBackendData,
|
---|
1955 | &Uuid);
|
---|
1956 | }
|
---|
1957 |
|
---|
1958 | pDisk->uModified &= ~VD_IMAGE_MODIFIED_FLAG;
|
---|
1959 | }
|
---|
1960 | }
|
---|
1961 |
|
---|
1962 | /**
|
---|
1963 | * internal: mark the disk as modified.
|
---|
1964 | */
|
---|
1965 | static void vdSetModifiedFlag(PVBOXHDD pDisk)
|
---|
1966 | {
|
---|
1967 | pDisk->uModified |= VD_IMAGE_MODIFIED_FLAG;
|
---|
1968 | if (pDisk->uModified & VD_IMAGE_MODIFIED_FIRST)
|
---|
1969 | {
|
---|
1970 | pDisk->uModified &= ~VD_IMAGE_MODIFIED_FIRST;
|
---|
1971 |
|
---|
1972 | /* First modify, so create a UUID and ensure it's written to disk. */
|
---|
1973 | vdResetModifiedFlag(pDisk);
|
---|
1974 |
|
---|
1975 | if (!(pDisk->uModified & VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
|
---|
1976 | {
|
---|
1977 | VDIOCTX IoCtx;
|
---|
1978 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_FLUSH, 0, 0, NULL,
|
---|
1979 | NULL, NULL, NULL, VDIOCTX_FLAGS_SYNC);
|
---|
1980 | pDisk->pLast->Backend->pfnFlush(pDisk->pLast->pBackendData, &IoCtx);
|
---|
1981 | }
|
---|
1982 | }
|
---|
1983 | }
|
---|
1984 |
|
---|
1985 | /**
|
---|
1986 | * internal: write a complete block (only used for diff images), taking the
|
---|
1987 | * remaining data from parent images. This implementation does not optimize
|
---|
1988 | * anything (except that it tries to read only that portions from parent
|
---|
1989 | * images that are really needed).
|
---|
1990 | */
|
---|
1991 | static int vdWriteHelperStandard(PVBOXHDD pDisk, PVDIMAGE pImage,
|
---|
1992 | PVDIMAGE pImageParentOverride,
|
---|
1993 | uint64_t uOffset, size_t cbWrite,
|
---|
1994 | size_t cbThisWrite, size_t cbPreRead,
|
---|
1995 | size_t cbPostRead, const void *pvBuf,
|
---|
1996 | void *pvTmp)
|
---|
1997 | {
|
---|
1998 | int rc = VINF_SUCCESS;
|
---|
1999 |
|
---|
2000 | LogFlowFunc(("pDisk=%p pImage=%p pImageParentOverride=%p uOffset=%llu cbWrite=%zu\n",
|
---|
2001 | pDisk, pImage, pImageParentOverride, uOffset, cbWrite));
|
---|
2002 |
|
---|
2003 | /* Read the data that goes before the write to fill the block. */
|
---|
2004 | if (cbPreRead)
|
---|
2005 | {
|
---|
2006 | /*
|
---|
2007 | * Updating the cache doesn't make sense here because
|
---|
2008 | * this will be done after the complete block was written.
|
---|
2009 | */
|
---|
2010 | rc = vdReadHelperEx(pDisk, pImage, pImageParentOverride,
|
---|
2011 | uOffset - cbPreRead, pvTmp, cbPreRead,
|
---|
2012 | true /* fZeroFreeBlocks*/,
|
---|
2013 | false /* fUpdateCache */, 0);
|
---|
2014 | if (RT_FAILURE(rc))
|
---|
2015 | return rc;
|
---|
2016 | }
|
---|
2017 |
|
---|
2018 | /* Copy the data to the right place in the buffer. */
|
---|
2019 | memcpy((char *)pvTmp + cbPreRead, pvBuf, cbThisWrite);
|
---|
2020 |
|
---|
2021 | /* Read the data that goes after the write to fill the block. */
|
---|
2022 | if (cbPostRead)
|
---|
2023 | {
|
---|
2024 | /* If we have data to be written, use that instead of reading
|
---|
2025 | * data from the image. */
|
---|
2026 | size_t cbWriteCopy;
|
---|
2027 | if (cbWrite > cbThisWrite)
|
---|
2028 | cbWriteCopy = RT_MIN(cbWrite - cbThisWrite, cbPostRead);
|
---|
2029 | else
|
---|
2030 | cbWriteCopy = 0;
|
---|
2031 | /* Figure out how much we cannot read from the image, because
|
---|
2032 | * the last block to write might exceed the nominal size of the
|
---|
2033 | * image for technical reasons. */
|
---|
2034 | size_t cbFill;
|
---|
2035 | if (uOffset + cbThisWrite + cbPostRead > pDisk->cbSize)
|
---|
2036 | cbFill = uOffset + cbThisWrite + cbPostRead - pDisk->cbSize;
|
---|
2037 | else
|
---|
2038 | cbFill = 0;
|
---|
2039 | /* The rest must be read from the image. */
|
---|
2040 | size_t cbReadImage = cbPostRead - cbWriteCopy - cbFill;
|
---|
2041 |
|
---|
2042 | /* Now assemble the remaining data. */
|
---|
2043 | if (cbWriteCopy)
|
---|
2044 | memcpy((char *)pvTmp + cbPreRead + cbThisWrite,
|
---|
2045 | (char *)pvBuf + cbThisWrite, cbWriteCopy);
|
---|
2046 | if (cbReadImage)
|
---|
2047 | rc = vdReadHelperEx(pDisk, pImage, pImageParentOverride,
|
---|
2048 | uOffset + cbThisWrite + cbWriteCopy,
|
---|
2049 | (char *)pvTmp + cbPreRead + cbThisWrite + cbWriteCopy,
|
---|
2050 | cbReadImage, true /* fZeroFreeBlocks */,
|
---|
2051 | false /* fUpdateCache */, 0);
|
---|
2052 | if (RT_FAILURE(rc))
|
---|
2053 | return rc;
|
---|
2054 | /* Zero out the remainder of this block. Will never be visible, as this
|
---|
2055 | * is beyond the limit of the image. */
|
---|
2056 | if (cbFill)
|
---|
2057 | memset((char *)pvTmp + cbPreRead + cbThisWrite + cbWriteCopy + cbReadImage,
|
---|
2058 | '\0', cbFill);
|
---|
2059 | }
|
---|
2060 |
|
---|
2061 | /* Write the full block to the virtual disk. */
|
---|
2062 | RTSGSEG SegmentBuf;
|
---|
2063 | RTSGBUF SgBuf;
|
---|
2064 | VDIOCTX IoCtx;
|
---|
2065 |
|
---|
2066 | SegmentBuf.pvSeg = pvTmp;
|
---|
2067 | SegmentBuf.cbSeg = cbPreRead + cbThisWrite + cbPostRead;
|
---|
2068 | RTSgBufInit(&SgBuf, &SegmentBuf, 1);
|
---|
2069 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_WRITE, 0, 0, NULL,
|
---|
2070 | &SgBuf, NULL, NULL, VDIOCTX_FLAGS_SYNC);
|
---|
2071 | rc = pImage->Backend->pfnWrite(pImage->pBackendData, uOffset - cbPreRead,
|
---|
2072 | cbPreRead + cbThisWrite + cbPostRead,
|
---|
2073 | &IoCtx, NULL, &cbPreRead, &cbPostRead, 0);
|
---|
2074 | Assert(rc != VERR_VD_BLOCK_FREE);
|
---|
2075 | Assert(cbPreRead == 0);
|
---|
2076 | Assert(cbPostRead == 0);
|
---|
2077 |
|
---|
2078 | return rc;
|
---|
2079 | }
|
---|
2080 |
|
---|
2081 | /**
|
---|
2082 | * internal: write a complete block (only used for diff images), taking the
|
---|
2083 | * remaining data from parent images. This implementation optimizes out writes
|
---|
2084 | * that do not change the data relative to the state as of the parent images.
|
---|
2085 | * All backends which support differential/growing images support this.
|
---|
2086 | */
|
---|
2087 | static int vdWriteHelperOptimized(PVBOXHDD pDisk, PVDIMAGE pImage,
|
---|
2088 | PVDIMAGE pImageParentOverride,
|
---|
2089 | uint64_t uOffset, size_t cbWrite,
|
---|
2090 | size_t cbThisWrite, size_t cbPreRead,
|
---|
2091 | size_t cbPostRead, const void *pvBuf,
|
---|
2092 | void *pvTmp, unsigned cImagesRead)
|
---|
2093 | {
|
---|
2094 | size_t cbFill = 0;
|
---|
2095 | size_t cbWriteCopy = 0;
|
---|
2096 | size_t cbReadImage = 0;
|
---|
2097 | int rc;
|
---|
2098 |
|
---|
2099 | LogFlowFunc(("pDisk=%p pImage=%p pImageParentOverride=%p uOffset=%llu cbWrite=%zu\n",
|
---|
2100 | pDisk, pImage, pImageParentOverride, uOffset, cbWrite));
|
---|
2101 |
|
---|
2102 | if (cbPostRead)
|
---|
2103 | {
|
---|
2104 | /* Figure out how much we cannot read from the image, because
|
---|
2105 | * the last block to write might exceed the nominal size of the
|
---|
2106 | * image for technical reasons. */
|
---|
2107 | if (uOffset + cbThisWrite + cbPostRead > pDisk->cbSize)
|
---|
2108 | cbFill = uOffset + cbThisWrite + cbPostRead - pDisk->cbSize;
|
---|
2109 |
|
---|
2110 | /* If we have data to be written, use that instead of reading
|
---|
2111 | * data from the image. */
|
---|
2112 | if (cbWrite > cbThisWrite)
|
---|
2113 | cbWriteCopy = RT_MIN(cbWrite - cbThisWrite, cbPostRead);
|
---|
2114 |
|
---|
2115 | /* The rest must be read from the image. */
|
---|
2116 | cbReadImage = cbPostRead - cbWriteCopy - cbFill;
|
---|
2117 | }
|
---|
2118 |
|
---|
2119 | /* Read the entire data of the block so that we can compare whether it will
|
---|
2120 | * be modified by the write or not. */
|
---|
2121 | rc = vdReadHelperEx(pDisk, pImage, pImageParentOverride, uOffset - cbPreRead, pvTmp,
|
---|
2122 | cbPreRead + cbThisWrite + cbPostRead - cbFill,
|
---|
2123 | true /* fZeroFreeBlocks */, false /* fUpdateCache */,
|
---|
2124 | cImagesRead);
|
---|
2125 | if (RT_FAILURE(rc))
|
---|
2126 | return rc;
|
---|
2127 |
|
---|
2128 | /* Check if the write would modify anything in this block. */
|
---|
2129 | if ( !memcmp((char *)pvTmp + cbPreRead, pvBuf, cbThisWrite)
|
---|
2130 | && (!cbWriteCopy || !memcmp((char *)pvTmp + cbPreRead + cbThisWrite,
|
---|
2131 | (char *)pvBuf + cbThisWrite, cbWriteCopy)))
|
---|
2132 | {
|
---|
2133 | /* Block is completely unchanged, so no need to write anything. */
|
---|
2134 | return VINF_SUCCESS;
|
---|
2135 | }
|
---|
2136 |
|
---|
2137 | /* Copy the data to the right place in the buffer. */
|
---|
2138 | memcpy((char *)pvTmp + cbPreRead, pvBuf, cbThisWrite);
|
---|
2139 |
|
---|
2140 | /* Handle the data that goes after the write to fill the block. */
|
---|
2141 | if (cbPostRead)
|
---|
2142 | {
|
---|
2143 | /* Now assemble the remaining data. */
|
---|
2144 | if (cbWriteCopy)
|
---|
2145 | memcpy((char *)pvTmp + cbPreRead + cbThisWrite,
|
---|
2146 | (char *)pvBuf + cbThisWrite, cbWriteCopy);
|
---|
2147 | /* Zero out the remainder of this block. Will never be visible, as this
|
---|
2148 | * is beyond the limit of the image. */
|
---|
2149 | if (cbFill)
|
---|
2150 | memset((char *)pvTmp + cbPreRead + cbThisWrite + cbWriteCopy + cbReadImage,
|
---|
2151 | '\0', cbFill);
|
---|
2152 | }
|
---|
2153 |
|
---|
2154 | /* Write the full block to the virtual disk. */
|
---|
2155 | RTSGSEG SegmentBuf;
|
---|
2156 | RTSGBUF SgBuf;
|
---|
2157 | VDIOCTX IoCtx;
|
---|
2158 |
|
---|
2159 | SegmentBuf.pvSeg = pvTmp;
|
---|
2160 | SegmentBuf.cbSeg = cbPreRead + cbThisWrite + cbPostRead;
|
---|
2161 | RTSgBufInit(&SgBuf, &SegmentBuf, 1);
|
---|
2162 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_WRITE, 0, 0, NULL,
|
---|
2163 | &SgBuf, NULL, NULL, VDIOCTX_FLAGS_SYNC);
|
---|
2164 | rc = pImage->Backend->pfnWrite(pImage->pBackendData, uOffset - cbPreRead,
|
---|
2165 | cbPreRead + cbThisWrite + cbPostRead,
|
---|
2166 | &IoCtx, NULL, &cbPreRead, &cbPostRead, 0);
|
---|
2167 | Assert(rc != VERR_VD_BLOCK_FREE);
|
---|
2168 | Assert(cbPreRead == 0);
|
---|
2169 | Assert(cbPostRead == 0);
|
---|
2170 |
|
---|
2171 | return rc;
|
---|
2172 | }
|
---|
2173 |
|
---|
2174 | /**
|
---|
2175 | * internal: write buffer to the image, taking care of block boundaries and
|
---|
2176 | * write optimizations.
|
---|
2177 | */
|
---|
2178 | static int vdWriteHelperEx(PVBOXHDD pDisk, PVDIMAGE pImage,
|
---|
2179 | PVDIMAGE pImageParentOverride, uint64_t uOffset,
|
---|
2180 | const void *pvBuf, size_t cbWrite,
|
---|
2181 | bool fUpdateCache, unsigned cImagesRead)
|
---|
2182 | {
|
---|
2183 | int rc;
|
---|
2184 | unsigned fWrite;
|
---|
2185 | size_t cbThisWrite;
|
---|
2186 | size_t cbPreRead, cbPostRead;
|
---|
2187 | uint64_t uOffsetCur = uOffset;
|
---|
2188 | size_t cbWriteCur = cbWrite;
|
---|
2189 | const void *pcvBufCur = pvBuf;
|
---|
2190 | RTSGSEG SegmentBuf;
|
---|
2191 | RTSGBUF SgBuf;
|
---|
2192 | VDIOCTX IoCtx;
|
---|
2193 |
|
---|
2194 | /* Loop until all written. */
|
---|
2195 | do
|
---|
2196 | {
|
---|
2197 | /* Try to write the possibly partial block to the last opened image.
|
---|
2198 | * This works when the block is already allocated in this image or
|
---|
2199 | * if it is a full-block write (and allocation isn't suppressed below).
|
---|
2200 | * For image formats which don't support zero blocks, it's beneficial
|
---|
2201 | * to avoid unnecessarily allocating unchanged blocks. This prevents
|
---|
2202 | * unwanted expanding of images. VMDK is an example. */
|
---|
2203 | cbThisWrite = cbWriteCur;
|
---|
2204 | fWrite = (pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME)
|
---|
2205 | ? 0 : VD_WRITE_NO_ALLOC;
|
---|
2206 |
|
---|
2207 | SegmentBuf.pvSeg = (void *)pcvBufCur;
|
---|
2208 | SegmentBuf.cbSeg = cbWrite;
|
---|
2209 | RTSgBufInit(&SgBuf, &SegmentBuf, 1);
|
---|
2210 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_WRITE, 0, 0, NULL,
|
---|
2211 | &SgBuf, NULL, NULL, VDIOCTX_FLAGS_SYNC);
|
---|
2212 | rc = pImage->Backend->pfnWrite(pImage->pBackendData, uOffsetCur, cbThisWrite,
|
---|
2213 | &IoCtx, &cbThisWrite, &cbPreRead,
|
---|
2214 | &cbPostRead, fWrite);
|
---|
2215 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
2216 | {
|
---|
2217 | void *pvTmp = RTMemTmpAlloc(cbPreRead + cbThisWrite + cbPostRead);
|
---|
2218 | AssertBreakStmt(VALID_PTR(pvTmp), rc = VERR_NO_MEMORY);
|
---|
2219 |
|
---|
2220 | if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME))
|
---|
2221 | {
|
---|
2222 | /* Optimized write, suppress writing to a so far unallocated
|
---|
2223 | * block if the data is in fact not changed. */
|
---|
2224 | rc = vdWriteHelperOptimized(pDisk, pImage, pImageParentOverride,
|
---|
2225 | uOffsetCur, cbWriteCur,
|
---|
2226 | cbThisWrite, cbPreRead, cbPostRead,
|
---|
2227 | pcvBufCur, pvTmp, cImagesRead);
|
---|
2228 | }
|
---|
2229 | else
|
---|
2230 | {
|
---|
2231 | /* Normal write, not optimized in any way. The block will
|
---|
2232 | * be written no matter what. This will usually (unless the
|
---|
2233 | * backend has some further optimization enabled) cause the
|
---|
2234 | * block to be allocated. */
|
---|
2235 | rc = vdWriteHelperStandard(pDisk, pImage, pImageParentOverride,
|
---|
2236 | uOffsetCur, cbWriteCur,
|
---|
2237 | cbThisWrite, cbPreRead, cbPostRead,
|
---|
2238 | pcvBufCur, pvTmp);
|
---|
2239 | }
|
---|
2240 | RTMemTmpFree(pvTmp);
|
---|
2241 | if (RT_FAILURE(rc))
|
---|
2242 | break;
|
---|
2243 | }
|
---|
2244 |
|
---|
2245 | cbWriteCur -= cbThisWrite;
|
---|
2246 | uOffsetCur += cbThisWrite;
|
---|
2247 | pcvBufCur = (char *)pcvBufCur + cbThisWrite;
|
---|
2248 | } while (cbWriteCur != 0 && RT_SUCCESS(rc));
|
---|
2249 |
|
---|
2250 | #if 0 /** @todo: Soon removed when sync and async version of the write helper are merged. */
|
---|
2251 | /* Update the cache on success */
|
---|
2252 | if ( RT_SUCCESS(rc)
|
---|
2253 | && pDisk->pCache
|
---|
2254 | && fUpdateCache)
|
---|
2255 | rc = vdCacheWriteHelper(pDisk->pCache, uOffset, pvBuf, cbWrite, NULL);
|
---|
2256 |
|
---|
2257 | if (RT_SUCCESS(rc))
|
---|
2258 | rc = vdDiscardSetRangeAllocated(pDisk, uOffset, cbWrite);
|
---|
2259 | #endif
|
---|
2260 |
|
---|
2261 | return rc;
|
---|
2262 | }
|
---|
2263 |
|
---|
2264 | /**
|
---|
2265 | * internal: write buffer to the image, taking care of block boundaries and
|
---|
2266 | * write optimizations.
|
---|
2267 | */
|
---|
2268 | static int vdWriteHelper(PVBOXHDD pDisk, PVDIMAGE pImage, uint64_t uOffset,
|
---|
2269 | const void *pvBuf, size_t cbWrite, bool fUpdateCache)
|
---|
2270 | {
|
---|
2271 | return vdWriteHelperEx(pDisk, pImage, NULL, uOffset, pvBuf, cbWrite,
|
---|
2272 | fUpdateCache, 0);
|
---|
2273 | }
|
---|
2274 |
|
---|
2275 | /**
|
---|
2276 | * Internal: Copies the content of one disk to another one applying optimizations
|
---|
2277 | * to speed up the copy process if possible.
|
---|
2278 | */
|
---|
2279 | static int vdCopyHelper(PVBOXHDD pDiskFrom, PVDIMAGE pImageFrom, PVBOXHDD pDiskTo,
|
---|
2280 | uint64_t cbSize, unsigned cImagesFromRead, unsigned cImagesToRead,
|
---|
2281 | bool fSuppressRedundantIo, PVDINTERFACEPROGRESS pIfProgress,
|
---|
2282 | PVDINTERFACEPROGRESS pDstIfProgress)
|
---|
2283 | {
|
---|
2284 | int rc = VINF_SUCCESS;
|
---|
2285 | int rc2;
|
---|
2286 | uint64_t uOffset = 0;
|
---|
2287 | uint64_t cbRemaining = cbSize;
|
---|
2288 | void *pvBuf = NULL;
|
---|
2289 | bool fLockReadFrom = false;
|
---|
2290 | bool fLockWriteTo = false;
|
---|
2291 | bool fBlockwiseCopy = fSuppressRedundantIo || (cImagesFromRead > 0);
|
---|
2292 | unsigned uProgressOld = 0;
|
---|
2293 |
|
---|
2294 | LogFlowFunc(("pDiskFrom=%#p pImageFrom=%#p pDiskTo=%#p cbSize=%llu cImagesFromRead=%u cImagesToRead=%u fSuppressRedundantIo=%RTbool pIfProgress=%#p pDstIfProgress=%#p\n",
|
---|
2295 | pDiskFrom, pImageFrom, pDiskTo, cbSize, cImagesFromRead, cImagesToRead, fSuppressRedundantIo, pDstIfProgress, pDstIfProgress));
|
---|
2296 |
|
---|
2297 | /* Allocate tmp buffer. */
|
---|
2298 | pvBuf = RTMemTmpAlloc(VD_MERGE_BUFFER_SIZE);
|
---|
2299 | if (!pvBuf)
|
---|
2300 | return rc;
|
---|
2301 |
|
---|
2302 | do
|
---|
2303 | {
|
---|
2304 | size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
|
---|
2305 |
|
---|
2306 | /* Note that we don't attempt to synchronize cross-disk accesses.
|
---|
2307 | * It wouldn't be very difficult to do, just the lock order would
|
---|
2308 | * need to be defined somehow to prevent deadlocks. Postpone such
|
---|
2309 | * magic as there is no use case for this. */
|
---|
2310 |
|
---|
2311 | rc2 = vdThreadStartRead(pDiskFrom);
|
---|
2312 | AssertRC(rc2);
|
---|
2313 | fLockReadFrom = true;
|
---|
2314 |
|
---|
2315 | if (fBlockwiseCopy)
|
---|
2316 | {
|
---|
2317 | RTSGSEG SegmentBuf;
|
---|
2318 | RTSGBUF SgBuf;
|
---|
2319 | VDIOCTX IoCtx;
|
---|
2320 |
|
---|
2321 | SegmentBuf.pvSeg = pvBuf;
|
---|
2322 | SegmentBuf.cbSeg = VD_MERGE_BUFFER_SIZE;
|
---|
2323 | RTSgBufInit(&SgBuf, &SegmentBuf, 1);
|
---|
2324 | vdIoCtxInit(&IoCtx, pDiskFrom, VDIOCTXTXDIR_READ, 0, 0, NULL,
|
---|
2325 | &SgBuf, NULL, NULL, VDIOCTX_FLAGS_SYNC);
|
---|
2326 |
|
---|
2327 | /* Read the source data. */
|
---|
2328 | rc = pImageFrom->Backend->pfnRead(pImageFrom->pBackendData,
|
---|
2329 | uOffset, cbThisRead, &IoCtx,
|
---|
2330 | &cbThisRead);
|
---|
2331 |
|
---|
2332 | if ( rc == VERR_VD_BLOCK_FREE
|
---|
2333 | && cImagesFromRead != 1)
|
---|
2334 | {
|
---|
2335 | unsigned cImagesToProcess = cImagesFromRead;
|
---|
2336 |
|
---|
2337 | for (PVDIMAGE pCurrImage = pImageFrom->pPrev;
|
---|
2338 | pCurrImage != NULL && rc == VERR_VD_BLOCK_FREE;
|
---|
2339 | pCurrImage = pCurrImage->pPrev)
|
---|
2340 | {
|
---|
2341 | rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
|
---|
2342 | uOffset, cbThisRead,
|
---|
2343 | &IoCtx, &cbThisRead);
|
---|
2344 | if (cImagesToProcess == 1)
|
---|
2345 | break;
|
---|
2346 | else if (cImagesToProcess > 0)
|
---|
2347 | cImagesToProcess--;
|
---|
2348 | }
|
---|
2349 | }
|
---|
2350 | }
|
---|
2351 | else
|
---|
2352 | rc = vdReadHelper(pDiskFrom, pImageFrom, uOffset, pvBuf, cbThisRead,
|
---|
2353 | false /* fUpdateCache */);
|
---|
2354 |
|
---|
2355 | if (RT_FAILURE(rc) && rc != VERR_VD_BLOCK_FREE)
|
---|
2356 | break;
|
---|
2357 |
|
---|
2358 | rc2 = vdThreadFinishRead(pDiskFrom);
|
---|
2359 | AssertRC(rc2);
|
---|
2360 | fLockReadFrom = false;
|
---|
2361 |
|
---|
2362 | if (rc != VERR_VD_BLOCK_FREE)
|
---|
2363 | {
|
---|
2364 | rc2 = vdThreadStartWrite(pDiskTo);
|
---|
2365 | AssertRC(rc2);
|
---|
2366 | fLockWriteTo = true;
|
---|
2367 |
|
---|
2368 | /* Only do collapsed I/O if we are copying the data blockwise. */
|
---|
2369 | rc = vdWriteHelperEx(pDiskTo, pDiskTo->pLast, NULL, uOffset, pvBuf,
|
---|
2370 | cbThisRead, false /* fUpdateCache */,
|
---|
2371 | fBlockwiseCopy ? cImagesToRead : 0);
|
---|
2372 | if (RT_FAILURE(rc))
|
---|
2373 | break;
|
---|
2374 |
|
---|
2375 | rc2 = vdThreadFinishWrite(pDiskTo);
|
---|
2376 | AssertRC(rc2);
|
---|
2377 | fLockWriteTo = false;
|
---|
2378 | }
|
---|
2379 | else /* Don't propagate the error to the outside */
|
---|
2380 | rc = VINF_SUCCESS;
|
---|
2381 |
|
---|
2382 | uOffset += cbThisRead;
|
---|
2383 | cbRemaining -= cbThisRead;
|
---|
2384 |
|
---|
2385 | unsigned uProgressNew = uOffset * 99 / cbSize;
|
---|
2386 | if (uProgressNew != uProgressOld)
|
---|
2387 | {
|
---|
2388 | uProgressOld = uProgressNew;
|
---|
2389 |
|
---|
2390 | if (pIfProgress && pIfProgress->pfnProgress)
|
---|
2391 | {
|
---|
2392 | rc = pIfProgress->pfnProgress(pIfProgress->Core.pvUser,
|
---|
2393 | uProgressOld);
|
---|
2394 | if (RT_FAILURE(rc))
|
---|
2395 | break;
|
---|
2396 | }
|
---|
2397 | if (pDstIfProgress && pDstIfProgress->pfnProgress)
|
---|
2398 | {
|
---|
2399 | rc = pDstIfProgress->pfnProgress(pDstIfProgress->Core.pvUser,
|
---|
2400 | uProgressOld);
|
---|
2401 | if (RT_FAILURE(rc))
|
---|
2402 | break;
|
---|
2403 | }
|
---|
2404 | }
|
---|
2405 | } while (uOffset < cbSize);
|
---|
2406 |
|
---|
2407 | RTMemFree(pvBuf);
|
---|
2408 |
|
---|
2409 | if (fLockReadFrom)
|
---|
2410 | {
|
---|
2411 | rc2 = vdThreadFinishRead(pDiskFrom);
|
---|
2412 | AssertRC(rc2);
|
---|
2413 | }
|
---|
2414 |
|
---|
2415 | if (fLockWriteTo)
|
---|
2416 | {
|
---|
2417 | rc2 = vdThreadFinishWrite(pDiskTo);
|
---|
2418 | AssertRC(rc2);
|
---|
2419 | }
|
---|
2420 |
|
---|
2421 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
2422 | return rc;
|
---|
2423 | }
|
---|
2424 |
|
---|
2425 | /**
|
---|
2426 | * Flush helper async version.
|
---|
2427 | */
|
---|
2428 | static int vdSetModifiedHelperAsync(PVDIOCTX pIoCtx)
|
---|
2429 | {
|
---|
2430 | int rc = VINF_SUCCESS;
|
---|
2431 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
2432 | PVDIMAGE pImage = pIoCtx->Req.Io.pImageCur;
|
---|
2433 |
|
---|
2434 | rc = pImage->Backend->pfnFlush(pImage->pBackendData, pIoCtx);
|
---|
2435 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
2436 | rc = VINF_SUCCESS;
|
---|
2437 |
|
---|
2438 | return rc;
|
---|
2439 | }
|
---|
2440 |
|
---|
2441 | /**
|
---|
2442 | * internal: mark the disk as modified - async version.
|
---|
2443 | */
|
---|
2444 | static int vdSetModifiedFlagAsync(PVBOXHDD pDisk, PVDIOCTX pIoCtx)
|
---|
2445 | {
|
---|
2446 | int rc = VINF_SUCCESS;
|
---|
2447 |
|
---|
2448 | VD_IS_LOCKED(pDisk);
|
---|
2449 |
|
---|
2450 | pDisk->uModified |= VD_IMAGE_MODIFIED_FLAG;
|
---|
2451 | if (pDisk->uModified & VD_IMAGE_MODIFIED_FIRST)
|
---|
2452 | {
|
---|
2453 | rc = vdIoCtxLockDisk(pDisk, pIoCtx);
|
---|
2454 | if (RT_SUCCESS(rc))
|
---|
2455 | {
|
---|
2456 | pDisk->uModified &= ~VD_IMAGE_MODIFIED_FIRST;
|
---|
2457 |
|
---|
2458 | /* First modify, so create a UUID and ensure it's written to disk. */
|
---|
2459 | vdResetModifiedFlag(pDisk);
|
---|
2460 |
|
---|
2461 | if (!(pDisk->uModified & VD_IMAGE_MODIFIED_DISABLE_UUID_UPDATE))
|
---|
2462 | {
|
---|
2463 | PVDIOCTX pIoCtxFlush = vdIoCtxChildAlloc(pDisk, VDIOCTXTXDIR_FLUSH,
|
---|
2464 | 0, 0, pDisk->pLast,
|
---|
2465 | NULL, pIoCtx, 0, 0, NULL,
|
---|
2466 | vdSetModifiedHelperAsync);
|
---|
2467 |
|
---|
2468 | if (pIoCtxFlush)
|
---|
2469 | {
|
---|
2470 | rc = vdIoCtxProcessLocked(pIoCtxFlush);
|
---|
2471 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
2472 | {
|
---|
2473 | vdIoCtxUnlockDisk(pDisk, pIoCtx, false /* fProcessDeferredReqs */);
|
---|
2474 | vdIoCtxFree(pDisk, pIoCtxFlush);
|
---|
2475 | }
|
---|
2476 | else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
2477 | {
|
---|
2478 | ASMAtomicIncU32(&pIoCtx->cDataTransfersPending);
|
---|
2479 | pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
|
---|
2480 | }
|
---|
2481 | else /* Another error */
|
---|
2482 | vdIoCtxFree(pDisk, pIoCtxFlush);
|
---|
2483 | }
|
---|
2484 | else
|
---|
2485 | rc = VERR_NO_MEMORY;
|
---|
2486 | }
|
---|
2487 | }
|
---|
2488 | }
|
---|
2489 |
|
---|
2490 | return rc;
|
---|
2491 | }
|
---|
2492 |
|
---|
2493 | /**
|
---|
2494 | * internal: write a complete block (only used for diff images), taking the
|
---|
2495 | * remaining data from parent images. This implementation does not optimize
|
---|
2496 | * anything (except that it tries to read only that portions from parent
|
---|
2497 | * images that are really needed) - async version.
|
---|
2498 | */
|
---|
2499 | static int vdWriteHelperStandardAsync(PVDIOCTX pIoCtx)
|
---|
2500 | {
|
---|
2501 | int rc = VINF_SUCCESS;
|
---|
2502 |
|
---|
2503 | #if 0
|
---|
2504 |
|
---|
2505 | /* Read the data that goes before the write to fill the block. */
|
---|
2506 | if (cbPreRead)
|
---|
2507 | {
|
---|
2508 | rc = vdReadHelperAsync(pIoCtxDst);
|
---|
2509 | if (RT_FAILURE(rc))
|
---|
2510 | return rc;
|
---|
2511 | }
|
---|
2512 |
|
---|
2513 | /* Copy the data to the right place in the buffer. */
|
---|
2514 | vdIoCtxCopy(pIoCtxDst, pIoCtxSrc, cbThisWrite);
|
---|
2515 |
|
---|
2516 | /* Read the data that goes after the write to fill the block. */
|
---|
2517 | if (cbPostRead)
|
---|
2518 | {
|
---|
2519 | /* If we have data to be written, use that instead of reading
|
---|
2520 | * data from the image. */
|
---|
2521 | size_t cbWriteCopy;
|
---|
2522 | if (cbWrite > cbThisWrite)
|
---|
2523 | cbWriteCopy = RT_MIN(cbWrite - cbThisWrite, cbPostRead);
|
---|
2524 | else
|
---|
2525 | cbWriteCopy = 0;
|
---|
2526 | /* Figure out how much we cannot read from the image, because
|
---|
2527 | * the last block to write might exceed the nominal size of the
|
---|
2528 | * image for technical reasons. */
|
---|
2529 | size_t cbFill;
|
---|
2530 | if (uOffset + cbThisWrite + cbPostRead > pDisk->cbSize)
|
---|
2531 | cbFill = uOffset + cbThisWrite + cbPostRead - pDisk->cbSize;
|
---|
2532 | else
|
---|
2533 | cbFill = 0;
|
---|
2534 | /* The rest must be read from the image. */
|
---|
2535 | size_t cbReadImage = cbPostRead - cbWriteCopy - cbFill;
|
---|
2536 |
|
---|
2537 | /* Now assemble the remaining data. */
|
---|
2538 | if (cbWriteCopy)
|
---|
2539 | {
|
---|
2540 | vdIoCtxCopy(pIoCtxDst, pIoCtxSrc, cbWriteCopy);
|
---|
2541 | ASMAtomicSubU32(&pIoCtxDst->cbTransferLeft, cbWriteCopy);
|
---|
2542 | }
|
---|
2543 |
|
---|
2544 | if (cbReadImage)
|
---|
2545 | rc = vdReadHelperAsync(pDisk, pImage, pImageParentOverride, pIoCtxDst,
|
---|
2546 | uOffset + cbThisWrite + cbWriteCopy,
|
---|
2547 | cbReadImage);
|
---|
2548 | if (RT_FAILURE(rc))
|
---|
2549 | return rc;
|
---|
2550 | /* Zero out the remainder of this block. Will never be visible, as this
|
---|
2551 | * is beyond the limit of the image. */
|
---|
2552 | if (cbFill)
|
---|
2553 | {
|
---|
2554 | vdIoCtxSet(pIoCtxDst, '\0', cbFill);
|
---|
2555 | ASMAtomicSubU32(&pIoCtxDst->cbTransferLeft, cbFill);
|
---|
2556 | }
|
---|
2557 | }
|
---|
2558 |
|
---|
2559 | if ( !pIoCtxDst->cbTransferLeft
|
---|
2560 | && !pIoCtxDst->cMetaTransfersPending
|
---|
2561 | && ASMAtomicCmpXchgBool(&pIoCtxDst->fComplete, true, false))
|
---|
2562 | {
|
---|
2563 | /* Write the full block to the virtual disk. */
|
---|
2564 | vdIoCtxChildReset(pIoCtxDst);
|
---|
2565 | rc = pImage->Backend->pfnWrite(pImage->pBackendData,
|
---|
2566 | uOffset - cbPreRead,
|
---|
2567 | cbPreRead + cbThisWrite + cbPostRead,
|
---|
2568 | pIoCtxDst,
|
---|
2569 | NULL, &cbPreRead, &cbPostRead, 0);
|
---|
2570 | Assert(rc != VERR_VD_BLOCK_FREE);
|
---|
2571 | Assert(cbPreRead == 0);
|
---|
2572 | Assert(cbPostRead == 0);
|
---|
2573 | }
|
---|
2574 | else
|
---|
2575 | {
|
---|
2576 | LogFlow(("cbTransferLeft=%u cMetaTransfersPending=%u fComplete=%RTbool\n",
|
---|
2577 | pIoCtxDst->cbTransferLeft, pIoCtxDst->cMetaTransfersPending,
|
---|
2578 | pIoCtxDst->fComplete));
|
---|
2579 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
2580 | }
|
---|
2581 |
|
---|
2582 | return rc;
|
---|
2583 | #endif
|
---|
2584 | return VERR_NOT_IMPLEMENTED;
|
---|
2585 | }
|
---|
2586 |
|
---|
2587 | static int vdWriteHelperOptimizedCommitAsync(PVDIOCTX pIoCtx)
|
---|
2588 | {
|
---|
2589 | int rc = VINF_SUCCESS;
|
---|
2590 | PVDIMAGE pImage = pIoCtx->Req.Io.pImageStart;
|
---|
2591 | size_t cbPreRead = pIoCtx->Type.Child.cbPreRead;
|
---|
2592 | size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
|
---|
2593 | size_t cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
|
---|
2594 |
|
---|
2595 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
2596 | rc = pImage->Backend->pfnWrite(pImage->pBackendData,
|
---|
2597 | pIoCtx->Req.Io.uOffset - cbPreRead,
|
---|
2598 | cbPreRead + cbThisWrite + cbPostRead,
|
---|
2599 | pIoCtx, NULL, &cbPreRead, &cbPostRead, 0);
|
---|
2600 | Assert(rc != VERR_VD_BLOCK_FREE);
|
---|
2601 | Assert(rc == VERR_VD_NOT_ENOUGH_METADATA || cbPreRead == 0);
|
---|
2602 | Assert(rc == VERR_VD_NOT_ENOUGH_METADATA || cbPostRead == 0);
|
---|
2603 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
2604 | rc = VINF_SUCCESS;
|
---|
2605 | else if (rc == VERR_VD_IOCTX_HALT)
|
---|
2606 | {
|
---|
2607 | pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
|
---|
2608 | rc = VINF_SUCCESS;
|
---|
2609 | }
|
---|
2610 |
|
---|
2611 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
2612 | return rc;
|
---|
2613 | }
|
---|
2614 |
|
---|
2615 | static int vdWriteHelperOptimizedCmpAndWriteAsync(PVDIOCTX pIoCtx)
|
---|
2616 | {
|
---|
2617 | int rc = VINF_SUCCESS;
|
---|
2618 | PVDIMAGE pImage = pIoCtx->Req.Io.pImageCur;
|
---|
2619 | size_t cbThisWrite = 0;
|
---|
2620 | size_t cbPreRead = pIoCtx->Type.Child.cbPreRead;
|
---|
2621 | size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
|
---|
2622 | size_t cbWriteCopy = pIoCtx->Type.Child.Write.Optimized.cbWriteCopy;
|
---|
2623 | size_t cbFill = pIoCtx->Type.Child.Write.Optimized.cbFill;
|
---|
2624 | size_t cbReadImage = pIoCtx->Type.Child.Write.Optimized.cbReadImage;
|
---|
2625 | PVDIOCTX pIoCtxParent = pIoCtx->pIoCtxParent;
|
---|
2626 |
|
---|
2627 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
2628 |
|
---|
2629 | AssertPtr(pIoCtxParent);
|
---|
2630 | Assert(!pIoCtxParent->pIoCtxParent);
|
---|
2631 | Assert(!pIoCtx->Req.Io.cbTransferLeft && !pIoCtx->cMetaTransfersPending);
|
---|
2632 |
|
---|
2633 | vdIoCtxChildReset(pIoCtx);
|
---|
2634 | cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
|
---|
2635 | RTSgBufAdvance(&pIoCtx->Req.Io.SgBuf, cbPreRead);
|
---|
2636 |
|
---|
2637 | /* Check if the write would modify anything in this block. */
|
---|
2638 | if (!RTSgBufCmp(&pIoCtx->Req.Io.SgBuf, &pIoCtxParent->Req.Io.SgBuf, cbThisWrite))
|
---|
2639 | {
|
---|
2640 | RTSGBUF SgBufSrcTmp;
|
---|
2641 |
|
---|
2642 | RTSgBufClone(&SgBufSrcTmp, &pIoCtxParent->Req.Io.SgBuf);
|
---|
2643 | RTSgBufAdvance(&SgBufSrcTmp, cbThisWrite);
|
---|
2644 | RTSgBufAdvance(&pIoCtx->Req.Io.SgBuf, cbThisWrite);
|
---|
2645 |
|
---|
2646 | if (!cbWriteCopy || !RTSgBufCmp(&pIoCtx->Req.Io.SgBuf, &SgBufSrcTmp, cbWriteCopy))
|
---|
2647 | {
|
---|
2648 | /* Block is completely unchanged, so no need to write anything. */
|
---|
2649 | LogFlowFunc(("Block didn't changed\n"));
|
---|
2650 | ASMAtomicWriteU32(&pIoCtx->Req.Io.cbTransferLeft, 0);
|
---|
2651 | RTSgBufAdvance(&pIoCtxParent->Req.Io.SgBuf, cbThisWrite);
|
---|
2652 | return VINF_VD_ASYNC_IO_FINISHED;
|
---|
2653 | }
|
---|
2654 | }
|
---|
2655 |
|
---|
2656 | /* Copy the data to the right place in the buffer. */
|
---|
2657 | RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
|
---|
2658 | RTSgBufAdvance(&pIoCtx->Req.Io.SgBuf, cbPreRead);
|
---|
2659 | vdIoCtxCopy(pIoCtx, pIoCtxParent, cbThisWrite);
|
---|
2660 |
|
---|
2661 | /* Handle the data that goes after the write to fill the block. */
|
---|
2662 | if (cbPostRead)
|
---|
2663 | {
|
---|
2664 | /* Now assemble the remaining data. */
|
---|
2665 | if (cbWriteCopy)
|
---|
2666 | {
|
---|
2667 | /*
|
---|
2668 | * The S/G buffer of the parent needs to be cloned because
|
---|
2669 | * it is not allowed to modify the state.
|
---|
2670 | */
|
---|
2671 | RTSGBUF SgBufParentTmp;
|
---|
2672 |
|
---|
2673 | RTSgBufClone(&SgBufParentTmp, &pIoCtxParent->Req.Io.SgBuf);
|
---|
2674 | RTSgBufCopy(&pIoCtx->Req.Io.SgBuf, &SgBufParentTmp, cbWriteCopy);
|
---|
2675 | }
|
---|
2676 |
|
---|
2677 | /* Zero out the remainder of this block. Will never be visible, as this
|
---|
2678 | * is beyond the limit of the image. */
|
---|
2679 | if (cbFill)
|
---|
2680 | {
|
---|
2681 | RTSgBufAdvance(&pIoCtx->Req.Io.SgBuf, cbReadImage);
|
---|
2682 | vdIoCtxSet(pIoCtx, '\0', cbFill);
|
---|
2683 | }
|
---|
2684 | }
|
---|
2685 |
|
---|
2686 | /* Write the full block to the virtual disk. */
|
---|
2687 | RTSgBufReset(&pIoCtx->Req.Io.SgBuf);
|
---|
2688 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperOptimizedCommitAsync;
|
---|
2689 |
|
---|
2690 | return rc;
|
---|
2691 | }
|
---|
2692 |
|
---|
2693 | static int vdWriteHelperOptimizedPreReadAsync(PVDIOCTX pIoCtx)
|
---|
2694 | {
|
---|
2695 | int rc = VINF_SUCCESS;
|
---|
2696 |
|
---|
2697 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
2698 |
|
---|
2699 | if (pIoCtx->Req.Io.cbTransferLeft)
|
---|
2700 | rc = vdReadHelperAsync(pIoCtx);
|
---|
2701 |
|
---|
2702 | if ( RT_SUCCESS(rc)
|
---|
2703 | && ( pIoCtx->Req.Io.cbTransferLeft
|
---|
2704 | || pIoCtx->cMetaTransfersPending))
|
---|
2705 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
2706 | else
|
---|
2707 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperOptimizedCmpAndWriteAsync;
|
---|
2708 |
|
---|
2709 | return rc;
|
---|
2710 | }
|
---|
2711 |
|
---|
2712 | /**
|
---|
2713 | * internal: write a complete block (only used for diff images), taking the
|
---|
2714 | * remaining data from parent images. This implementation optimizes out writes
|
---|
2715 | * that do not change the data relative to the state as of the parent images.
|
---|
2716 | * All backends which support differential/growing images support this - async version.
|
---|
2717 | */
|
---|
2718 | static int vdWriteHelperOptimizedAsync(PVDIOCTX pIoCtx)
|
---|
2719 | {
|
---|
2720 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
2721 | uint64_t uOffset = pIoCtx->Type.Child.uOffsetSaved;
|
---|
2722 | size_t cbThisWrite = pIoCtx->Type.Child.cbTransferParent;
|
---|
2723 | size_t cbPreRead = pIoCtx->Type.Child.cbPreRead;
|
---|
2724 | size_t cbPostRead = pIoCtx->Type.Child.cbPostRead;
|
---|
2725 | size_t cbWrite = pIoCtx->Type.Child.cbWriteParent;
|
---|
2726 | size_t cbFill = 0;
|
---|
2727 | size_t cbWriteCopy = 0;
|
---|
2728 | size_t cbReadImage = 0;
|
---|
2729 |
|
---|
2730 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
2731 |
|
---|
2732 | AssertPtr(pIoCtx->pIoCtxParent);
|
---|
2733 | Assert(!pIoCtx->pIoCtxParent->pIoCtxParent);
|
---|
2734 |
|
---|
2735 | if (cbPostRead)
|
---|
2736 | {
|
---|
2737 | /* Figure out how much we cannot read from the image, because
|
---|
2738 | * the last block to write might exceed the nominal size of the
|
---|
2739 | * image for technical reasons. */
|
---|
2740 | if (uOffset + cbThisWrite + cbPostRead > pDisk->cbSize)
|
---|
2741 | cbFill = uOffset + cbThisWrite + cbPostRead - pDisk->cbSize;
|
---|
2742 |
|
---|
2743 | /* If we have data to be written, use that instead of reading
|
---|
2744 | * data from the image. */
|
---|
2745 | if (cbWrite > cbThisWrite)
|
---|
2746 | cbWriteCopy = RT_MIN(cbWrite - cbThisWrite, cbPostRead);
|
---|
2747 |
|
---|
2748 | /* The rest must be read from the image. */
|
---|
2749 | cbReadImage = cbPostRead - cbWriteCopy - cbFill;
|
---|
2750 | }
|
---|
2751 |
|
---|
2752 | pIoCtx->Type.Child.Write.Optimized.cbFill = cbFill;
|
---|
2753 | pIoCtx->Type.Child.Write.Optimized.cbWriteCopy = cbWriteCopy;
|
---|
2754 | pIoCtx->Type.Child.Write.Optimized.cbReadImage = cbReadImage;
|
---|
2755 |
|
---|
2756 | /* Read the entire data of the block so that we can compare whether it will
|
---|
2757 | * be modified by the write or not. */
|
---|
2758 | pIoCtx->Req.Io.cbTransferLeft = cbPreRead + cbThisWrite + cbPostRead - cbFill;
|
---|
2759 | pIoCtx->Req.Io.cbTransfer = pIoCtx->Req.Io.cbTransferLeft;
|
---|
2760 | pIoCtx->Req.Io.uOffset -= cbPreRead;
|
---|
2761 |
|
---|
2762 | /* Next step */
|
---|
2763 | pIoCtx->pfnIoCtxTransferNext = vdWriteHelperOptimizedPreReadAsync;
|
---|
2764 | return VINF_SUCCESS;
|
---|
2765 | }
|
---|
2766 |
|
---|
2767 | /**
|
---|
2768 | * internal: write buffer to the image, taking care of block boundaries and
|
---|
2769 | * write optimizations - async version.
|
---|
2770 | */
|
---|
2771 | static int vdWriteHelperAsync(PVDIOCTX pIoCtx)
|
---|
2772 | {
|
---|
2773 | int rc;
|
---|
2774 | size_t cbWrite = pIoCtx->Req.Io.cbTransfer;
|
---|
2775 | uint64_t uOffset = pIoCtx->Req.Io.uOffset;
|
---|
2776 | PVDIMAGE pImage = pIoCtx->Req.Io.pImageCur;
|
---|
2777 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
2778 | unsigned fWrite;
|
---|
2779 | size_t cbThisWrite;
|
---|
2780 | size_t cbPreRead, cbPostRead;
|
---|
2781 |
|
---|
2782 | rc = vdSetModifiedFlagAsync(pDisk, pIoCtx);
|
---|
2783 | if (RT_FAILURE(rc)) /* Includes I/O in progress. */
|
---|
2784 | return rc;
|
---|
2785 |
|
---|
2786 | rc = vdDiscardSetRangeAllocated(pDisk, uOffset, cbWrite);
|
---|
2787 | if (RT_FAILURE(rc))
|
---|
2788 | return rc;
|
---|
2789 |
|
---|
2790 | /* Loop until all written. */
|
---|
2791 | do
|
---|
2792 | {
|
---|
2793 | /* Try to write the possibly partial block to the last opened image.
|
---|
2794 | * This works when the block is already allocated in this image or
|
---|
2795 | * if it is a full-block write (and allocation isn't suppressed below).
|
---|
2796 | * For image formats which don't support zero blocks, it's beneficial
|
---|
2797 | * to avoid unnecessarily allocating unchanged blocks. This prevents
|
---|
2798 | * unwanted expanding of images. VMDK is an example. */
|
---|
2799 | cbThisWrite = cbWrite;
|
---|
2800 | fWrite = (pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME)
|
---|
2801 | ? 0 : VD_WRITE_NO_ALLOC;
|
---|
2802 | rc = pImage->Backend->pfnWrite(pImage->pBackendData, uOffset,
|
---|
2803 | cbThisWrite, pIoCtx,
|
---|
2804 | &cbThisWrite, &cbPreRead,
|
---|
2805 | &cbPostRead, fWrite);
|
---|
2806 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
2807 | {
|
---|
2808 | /* Lock the disk .*/
|
---|
2809 | rc = vdIoCtxLockDisk(pDisk, pIoCtx);
|
---|
2810 | if (RT_SUCCESS(rc))
|
---|
2811 | {
|
---|
2812 | /*
|
---|
2813 | * Allocate segment and buffer in one go.
|
---|
2814 | * A bit hackish but avoids the need to allocate memory twice.
|
---|
2815 | */
|
---|
2816 | PRTSGBUF pTmp = (PRTSGBUF)RTMemAlloc(cbPreRead + cbThisWrite + cbPostRead + sizeof(RTSGSEG) + sizeof(RTSGBUF));
|
---|
2817 | AssertBreakStmt(VALID_PTR(pTmp), rc = VERR_NO_MEMORY);
|
---|
2818 | PRTSGSEG pSeg = (PRTSGSEG)(pTmp + 1);
|
---|
2819 |
|
---|
2820 | pSeg->pvSeg = pSeg + 1;
|
---|
2821 | pSeg->cbSeg = cbPreRead + cbThisWrite + cbPostRead;
|
---|
2822 | RTSgBufInit(pTmp, pSeg, 1);
|
---|
2823 |
|
---|
2824 | PVDIOCTX pIoCtxWrite = vdIoCtxChildAlloc(pDisk, VDIOCTXTXDIR_WRITE,
|
---|
2825 | uOffset, pSeg->cbSeg, pImage,
|
---|
2826 | pTmp,
|
---|
2827 | pIoCtx, cbThisWrite,
|
---|
2828 | cbWrite,
|
---|
2829 | pTmp,
|
---|
2830 | (pImage->uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME)
|
---|
2831 | ? vdWriteHelperStandardAsync
|
---|
2832 | : vdWriteHelperOptimizedAsync);
|
---|
2833 | if (!VALID_PTR(pIoCtxWrite))
|
---|
2834 | {
|
---|
2835 | RTMemTmpFree(pTmp);
|
---|
2836 | rc = VERR_NO_MEMORY;
|
---|
2837 | break;
|
---|
2838 | }
|
---|
2839 |
|
---|
2840 | LogFlowFunc(("Disk is growing because of pIoCtx=%#p pIoCtxWrite=%#p\n",
|
---|
2841 | pIoCtx, pIoCtxWrite));
|
---|
2842 |
|
---|
2843 | pIoCtxWrite->Type.Child.cbPreRead = cbPreRead;
|
---|
2844 | pIoCtxWrite->Type.Child.cbPostRead = cbPostRead;
|
---|
2845 |
|
---|
2846 | /* Process the write request */
|
---|
2847 | rc = vdIoCtxProcessLocked(pIoCtxWrite);
|
---|
2848 |
|
---|
2849 | if (RT_FAILURE(rc) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
2850 | {
|
---|
2851 | vdIoCtxFree(pDisk, pIoCtxWrite);
|
---|
2852 | break;
|
---|
2853 | }
|
---|
2854 | else if ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
2855 | && ASMAtomicCmpXchgBool(&pIoCtxWrite->fComplete, true, false))
|
---|
2856 | {
|
---|
2857 | LogFlow(("Child write request completed\n"));
|
---|
2858 | Assert(pIoCtx->Req.Io.cbTransferLeft >= cbThisWrite);
|
---|
2859 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, cbThisWrite);
|
---|
2860 | vdIoCtxUnlockDisk(pDisk, pIoCtx, false /* fProcessDeferredReqs*/ );
|
---|
2861 | vdIoCtxFree(pDisk, pIoCtxWrite);
|
---|
2862 |
|
---|
2863 | rc = VINF_SUCCESS;
|
---|
2864 | }
|
---|
2865 | else
|
---|
2866 | {
|
---|
2867 | LogFlow(("Child write pending\n"));
|
---|
2868 | ASMAtomicIncU32(&pIoCtx->cDataTransfersPending);
|
---|
2869 | pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
|
---|
2870 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
2871 | cbWrite -= cbThisWrite;
|
---|
2872 | uOffset += cbThisWrite;
|
---|
2873 | break;
|
---|
2874 | }
|
---|
2875 | }
|
---|
2876 | else
|
---|
2877 | {
|
---|
2878 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
|
---|
2879 | break;
|
---|
2880 | }
|
---|
2881 | }
|
---|
2882 |
|
---|
2883 | if (rc == VERR_VD_IOCTX_HALT)
|
---|
2884 | {
|
---|
2885 | cbWrite -= cbThisWrite;
|
---|
2886 | uOffset += cbThisWrite;
|
---|
2887 | pIoCtx->fFlags |= VDIOCTX_FLAGS_BLOCKED;
|
---|
2888 | break;
|
---|
2889 | }
|
---|
2890 | else if (rc == VERR_VD_NOT_ENOUGH_METADATA)
|
---|
2891 | break;
|
---|
2892 |
|
---|
2893 | cbWrite -= cbThisWrite;
|
---|
2894 | uOffset += cbThisWrite;
|
---|
2895 | } while (cbWrite != 0 && (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS));
|
---|
2896 |
|
---|
2897 | if ( rc == VERR_VD_ASYNC_IO_IN_PROGRESS
|
---|
2898 | || rc == VERR_VD_NOT_ENOUGH_METADATA
|
---|
2899 | || rc == VERR_VD_IOCTX_HALT)
|
---|
2900 | {
|
---|
2901 | /*
|
---|
2902 | * Tell the caller that we don't need to go back here because all
|
---|
2903 | * writes are initiated.
|
---|
2904 | */
|
---|
2905 | if ( !cbWrite
|
---|
2906 | && rc != VERR_VD_IOCTX_HALT)
|
---|
2907 | rc = VINF_SUCCESS;
|
---|
2908 |
|
---|
2909 | pIoCtx->Req.Io.uOffset = uOffset;
|
---|
2910 | pIoCtx->Req.Io.cbTransfer = cbWrite;
|
---|
2911 | }
|
---|
2912 |
|
---|
2913 | return rc;
|
---|
2914 | }
|
---|
2915 |
|
---|
2916 | /**
|
---|
2917 | * Flush helper async version.
|
---|
2918 | */
|
---|
2919 | static int vdFlushHelperAsync(PVDIOCTX pIoCtx)
|
---|
2920 | {
|
---|
2921 | int rc = VINF_SUCCESS;
|
---|
2922 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
2923 | PVDIMAGE pImage = pIoCtx->Req.Io.pImageCur;
|
---|
2924 |
|
---|
2925 | rc = vdIoCtxLockDisk(pDisk, pIoCtx);
|
---|
2926 | if (RT_SUCCESS(rc))
|
---|
2927 | {
|
---|
2928 | vdResetModifiedFlag(pDisk);
|
---|
2929 | rc = pImage->Backend->pfnFlush(pImage->pBackendData, pIoCtx);
|
---|
2930 | if ( ( RT_SUCCESS(rc)
|
---|
2931 | || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
2932 | && pDisk->pCache)
|
---|
2933 | {
|
---|
2934 | rc = pDisk->pCache->Backend->pfnFlush(pDisk->pCache->pBackendData, pIoCtx);
|
---|
2935 | if ( RT_SUCCESS(rc)
|
---|
2936 | || rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
2937 | vdIoCtxUnlockDisk(pDisk, pIoCtx, true /* fProcessBlockedReqs */);
|
---|
2938 | else
|
---|
2939 | rc = VINF_SUCCESS;
|
---|
2940 | }
|
---|
2941 | else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
2942 | rc = VINF_SUCCESS;
|
---|
2943 | else /* Some other error. */
|
---|
2944 | vdIoCtxUnlockDisk(pDisk, pIoCtx, true /* fProcessBlockedReqs */);
|
---|
2945 | }
|
---|
2946 |
|
---|
2947 | return rc;
|
---|
2948 | }
|
---|
2949 |
|
---|
2950 | /**
|
---|
2951 | * Async discard helper - discards a whole block which is recorded in the block
|
---|
2952 | * tree.
|
---|
2953 | *
|
---|
2954 | * @returns VBox status code.
|
---|
2955 | * @param pIoCtx The I/O context to operate on.
|
---|
2956 | */
|
---|
2957 | static int vdDiscardWholeBlockAsync(PVDIOCTX pIoCtx)
|
---|
2958 | {
|
---|
2959 | int rc = VINF_SUCCESS;
|
---|
2960 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
2961 | PVDDISCARDSTATE pDiscard = pDisk->pDiscard;
|
---|
2962 | PVDDISCARDBLOCK pBlock = pIoCtx->Req.Discard.pBlock;
|
---|
2963 | size_t cbPreAllocated, cbPostAllocated, cbActuallyDiscarded;
|
---|
2964 |
|
---|
2965 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
2966 |
|
---|
2967 | AssertPtr(pBlock);
|
---|
2968 |
|
---|
2969 | rc = pDisk->pLast->Backend->pfnDiscard(pDisk->pLast->pBackendData, pIoCtx,
|
---|
2970 | pBlock->Core.Key, pBlock->cbDiscard,
|
---|
2971 | &cbPreAllocated, &cbPostAllocated,
|
---|
2972 | &cbActuallyDiscarded, NULL, 0);
|
---|
2973 | Assert(rc != VERR_VD_DISCARD_ALIGNMENT_NOT_MET);
|
---|
2974 | Assert(!cbPreAllocated);
|
---|
2975 | Assert(!cbPostAllocated);
|
---|
2976 | Assert(cbActuallyDiscarded == pBlock->cbDiscard || RT_FAILURE(rc));
|
---|
2977 |
|
---|
2978 | /* Remove the block on success. */
|
---|
2979 | if ( RT_SUCCESS(rc)
|
---|
2980 | || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
2981 | {
|
---|
2982 | PVDDISCARDBLOCK pBlockRemove = (PVDDISCARDBLOCK)RTAvlrU64RangeRemove(pDiscard->pTreeBlocks, pBlock->Core.Key);
|
---|
2983 | Assert(pBlockRemove == pBlock);
|
---|
2984 |
|
---|
2985 | pDiscard->cbDiscarding -= pBlock->cbDiscard;
|
---|
2986 | RTListNodeRemove(&pBlock->NodeLru);
|
---|
2987 | RTMemFree(pBlock->pbmAllocated);
|
---|
2988 | RTMemFree(pBlock);
|
---|
2989 | pIoCtx->Req.Discard.pBlock = NULL;/* Safety precaution. */
|
---|
2990 | pIoCtx->pfnIoCtxTransferNext = vdDiscardHelperAsync; /* Next part. */
|
---|
2991 | rc = VINF_SUCCESS;
|
---|
2992 | }
|
---|
2993 |
|
---|
2994 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
2995 | return rc;
|
---|
2996 | }
|
---|
2997 |
|
---|
2998 | /**
|
---|
2999 | * Removes the least recently used blocks from the waiting list until
|
---|
3000 | * the new value is reached - version for async I/O.
|
---|
3001 | *
|
---|
3002 | * @returns VBox status code.
|
---|
3003 | * @param pDisk VD disk container.
|
---|
3004 | * @param pDiscard The discard state.
|
---|
3005 | * @param cbDiscardingNew How many bytes should be waiting on success.
|
---|
3006 | * The number of bytes waiting can be less.
|
---|
3007 | */
|
---|
3008 | static int vdDiscardRemoveBlocksAsync(PVBOXHDD pDisk, PVDIOCTX pIoCtx, size_t cbDiscardingNew)
|
---|
3009 | {
|
---|
3010 | int rc = VINF_SUCCESS;
|
---|
3011 | PVDDISCARDSTATE pDiscard = pDisk->pDiscard;
|
---|
3012 |
|
---|
3013 | LogFlowFunc(("pDisk=%#p pDiscard=%#p cbDiscardingNew=%zu\n",
|
---|
3014 | pDisk, pDiscard, cbDiscardingNew));
|
---|
3015 |
|
---|
3016 | while (pDiscard->cbDiscarding > cbDiscardingNew)
|
---|
3017 | {
|
---|
3018 | PVDDISCARDBLOCK pBlock = RTListGetLast(&pDiscard->ListLru, VDDISCARDBLOCK, NodeLru);
|
---|
3019 |
|
---|
3020 | Assert(!RTListIsEmpty(&pDiscard->ListLru));
|
---|
3021 |
|
---|
3022 | /* Go over the allocation bitmap and mark all discarded sectors as unused. */
|
---|
3023 | uint64_t offStart = pBlock->Core.Key;
|
---|
3024 | uint32_t idxStart = 0;
|
---|
3025 | size_t cbLeft = pBlock->cbDiscard;
|
---|
3026 | bool fAllocated = ASMBitTest(pBlock->pbmAllocated, idxStart);
|
---|
3027 | uint32_t cSectors = pBlock->cbDiscard / 512;
|
---|
3028 |
|
---|
3029 | while (cbLeft > 0)
|
---|
3030 | {
|
---|
3031 | int32_t idxEnd;
|
---|
3032 | size_t cbThis = cbLeft;
|
---|
3033 |
|
---|
3034 | if (fAllocated)
|
---|
3035 | {
|
---|
3036 | /* Check for the first unallocated bit. */
|
---|
3037 | idxEnd = ASMBitNextClear(pBlock->pbmAllocated, cSectors, idxStart);
|
---|
3038 | if (idxEnd != -1)
|
---|
3039 | {
|
---|
3040 | cbThis = (idxEnd - idxStart) * 512;
|
---|
3041 | fAllocated = false;
|
---|
3042 | }
|
---|
3043 | }
|
---|
3044 | else
|
---|
3045 | {
|
---|
3046 | /* Mark as unused and check for the first set bit. */
|
---|
3047 | idxEnd = ASMBitNextSet(pBlock->pbmAllocated, cSectors, idxStart);
|
---|
3048 | if (idxEnd != -1)
|
---|
3049 | cbThis = (idxEnd - idxStart) * 512;
|
---|
3050 |
|
---|
3051 | rc = pDisk->pLast->Backend->pfnDiscard(pDisk->pLast->pBackendData, pIoCtx,
|
---|
3052 | offStart, cbThis, NULL, NULL, &cbThis,
|
---|
3053 | NULL, VD_DISCARD_MARK_UNUSED);
|
---|
3054 | if ( RT_FAILURE(rc)
|
---|
3055 | && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
3056 | break;
|
---|
3057 |
|
---|
3058 | fAllocated = true;
|
---|
3059 | }
|
---|
3060 |
|
---|
3061 | idxStart = idxEnd;
|
---|
3062 | offStart += cbThis;
|
---|
3063 | cbLeft -= cbThis;
|
---|
3064 | }
|
---|
3065 |
|
---|
3066 | if ( RT_FAILURE(rc)
|
---|
3067 | && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
3068 | break;
|
---|
3069 |
|
---|
3070 | PVDDISCARDBLOCK pBlockRemove = (PVDDISCARDBLOCK)RTAvlrU64RangeRemove(pDiscard->pTreeBlocks, pBlock->Core.Key);
|
---|
3071 | Assert(pBlockRemove == pBlock);
|
---|
3072 | RTListNodeRemove(&pBlock->NodeLru);
|
---|
3073 |
|
---|
3074 | pDiscard->cbDiscarding -= pBlock->cbDiscard;
|
---|
3075 | RTMemFree(pBlock->pbmAllocated);
|
---|
3076 | RTMemFree(pBlock);
|
---|
3077 | }
|
---|
3078 |
|
---|
3079 | if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
3080 | rc = VINF_SUCCESS;
|
---|
3081 |
|
---|
3082 | Assert(RT_FAILURE(rc) || pDiscard->cbDiscarding <= cbDiscardingNew);
|
---|
3083 |
|
---|
3084 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
3085 | return rc;
|
---|
3086 | }
|
---|
3087 |
|
---|
3088 | /**
|
---|
3089 | * Async discard helper - discards the current range if there is no matching
|
---|
3090 | * block in the tree.
|
---|
3091 | *
|
---|
3092 | * @returns VBox status code.
|
---|
3093 | * @param pIoCtx The I/O context to operate on.
|
---|
3094 | */
|
---|
3095 | static int vdDiscardCurrentRangeAsync(PVDIOCTX pIoCtx)
|
---|
3096 | {
|
---|
3097 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
3098 | PVDDISCARDSTATE pDiscard = pDisk->pDiscard;
|
---|
3099 | uint64_t offStart = pIoCtx->Req.Discard.offCur;
|
---|
3100 | size_t cbThisDiscard = pIoCtx->Req.Discard.cbThisDiscard;
|
---|
3101 | void *pbmAllocated = NULL;
|
---|
3102 | size_t cbPreAllocated, cbPostAllocated;
|
---|
3103 | int rc = VINF_SUCCESS;
|
---|
3104 |
|
---|
3105 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
3106 |
|
---|
3107 | /* No block found, try to discard using the backend first. */
|
---|
3108 | rc = pDisk->pLast->Backend->pfnDiscard(pDisk->pLast->pBackendData, pIoCtx,
|
---|
3109 | offStart, cbThisDiscard, &cbPreAllocated,
|
---|
3110 | &cbPostAllocated, &cbThisDiscard,
|
---|
3111 | &pbmAllocated, 0);
|
---|
3112 | if (rc == VERR_VD_DISCARD_ALIGNMENT_NOT_MET)
|
---|
3113 | {
|
---|
3114 | /* Create new discard block. */
|
---|
3115 | PVDDISCARDBLOCK pBlock = (PVDDISCARDBLOCK)RTMemAllocZ(sizeof(VDDISCARDBLOCK));
|
---|
3116 | if (pBlock)
|
---|
3117 | {
|
---|
3118 | pBlock->Core.Key = offStart - cbPreAllocated;
|
---|
3119 | pBlock->Core.KeyLast = offStart + cbThisDiscard + cbPostAllocated - 1;
|
---|
3120 | pBlock->cbDiscard = cbPreAllocated + cbThisDiscard + cbPostAllocated;
|
---|
3121 | pBlock->pbmAllocated = pbmAllocated;
|
---|
3122 | bool fInserted = RTAvlrU64Insert(pDiscard->pTreeBlocks, &pBlock->Core);
|
---|
3123 | Assert(fInserted);
|
---|
3124 |
|
---|
3125 | RTListPrepend(&pDiscard->ListLru, &pBlock->NodeLru);
|
---|
3126 | pDiscard->cbDiscarding += pBlock->cbDiscard;
|
---|
3127 |
|
---|
3128 | Assert(pIoCtx->Req.Discard.cbDiscardLeft >= cbThisDiscard);
|
---|
3129 | pIoCtx->Req.Discard.cbDiscardLeft -= cbThisDiscard;
|
---|
3130 | pIoCtx->Req.Discard.offCur += cbThisDiscard;
|
---|
3131 | pIoCtx->Req.Discard.cbThisDiscard = cbThisDiscard;
|
---|
3132 |
|
---|
3133 | if (pDiscard->cbDiscarding > VD_DISCARD_REMOVE_THRESHOLD)
|
---|
3134 | rc = vdDiscardRemoveBlocksAsync(pDisk, pIoCtx, VD_DISCARD_REMOVE_THRESHOLD);
|
---|
3135 | else
|
---|
3136 | rc = VINF_SUCCESS;
|
---|
3137 |
|
---|
3138 | if (RT_SUCCESS(rc))
|
---|
3139 | pIoCtx->pfnIoCtxTransferNext = vdDiscardHelperAsync; /* Next part. */
|
---|
3140 | }
|
---|
3141 | else
|
---|
3142 | {
|
---|
3143 | RTMemFree(pbmAllocated);
|
---|
3144 | rc = VERR_NO_MEMORY;
|
---|
3145 | }
|
---|
3146 | }
|
---|
3147 | else if ( RT_SUCCESS(rc)
|
---|
3148 | || rc == VERR_VD_ASYNC_IO_IN_PROGRESS) /* Save state and andvance to next range. */
|
---|
3149 | {
|
---|
3150 | Assert(pIoCtx->Req.Discard.cbDiscardLeft >= cbThisDiscard);
|
---|
3151 | pIoCtx->Req.Discard.cbDiscardLeft -= cbThisDiscard;
|
---|
3152 | pIoCtx->Req.Discard.offCur += cbThisDiscard;
|
---|
3153 | pIoCtx->Req.Discard.cbThisDiscard = cbThisDiscard;
|
---|
3154 | pIoCtx->pfnIoCtxTransferNext = vdDiscardHelperAsync;
|
---|
3155 | rc = VINF_SUCCESS;
|
---|
3156 | }
|
---|
3157 |
|
---|
3158 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
3159 | return rc;
|
---|
3160 | }
|
---|
3161 |
|
---|
3162 | /**
|
---|
3163 | * Async discard helper - entry point.
|
---|
3164 | *
|
---|
3165 | * @returns VBox status code.
|
---|
3166 | * @param pIoCtx The I/O context to operate on.
|
---|
3167 | */
|
---|
3168 | static int vdDiscardHelperAsync(PVDIOCTX pIoCtx)
|
---|
3169 | {
|
---|
3170 | int rc = VINF_SUCCESS;
|
---|
3171 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
3172 | PCRTRANGE paRanges = pIoCtx->Req.Discard.paRanges;
|
---|
3173 | unsigned cRanges = pIoCtx->Req.Discard.cRanges;
|
---|
3174 | PVDDISCARDSTATE pDiscard = pDisk->pDiscard;
|
---|
3175 |
|
---|
3176 | LogFlowFunc(("pIoCtx=%#p\n", pIoCtx));
|
---|
3177 |
|
---|
3178 | /* Check if the I/O context processed all ranges. */
|
---|
3179 | if ( pIoCtx->Req.Discard.idxRange == cRanges
|
---|
3180 | && !pIoCtx->Req.Discard.cbDiscardLeft)
|
---|
3181 | {
|
---|
3182 | LogFlowFunc(("All ranges discarded, completing\n"));
|
---|
3183 | vdIoCtxUnlockDisk(pDisk, pIoCtx, true /* fProcessDeferredReqs*/);
|
---|
3184 | return VINF_SUCCESS;
|
---|
3185 | }
|
---|
3186 |
|
---|
3187 | if (pDisk->pIoCtxLockOwner != pIoCtx)
|
---|
3188 | rc = vdIoCtxLockDisk(pDisk, pIoCtx);
|
---|
3189 |
|
---|
3190 | if (RT_SUCCESS(rc))
|
---|
3191 | {
|
---|
3192 | uint64_t offStart = pIoCtx->Req.Discard.offCur;
|
---|
3193 | size_t cbDiscardLeft = pIoCtx->Req.Discard.cbDiscardLeft;
|
---|
3194 | size_t cbThisDiscard;
|
---|
3195 |
|
---|
3196 | if (RT_UNLIKELY(!pDiscard))
|
---|
3197 | {
|
---|
3198 | pDiscard = vdDiscardStateCreate();
|
---|
3199 | if (!pDiscard)
|
---|
3200 | return VERR_NO_MEMORY;
|
---|
3201 |
|
---|
3202 | pDisk->pDiscard = pDiscard;
|
---|
3203 | }
|
---|
3204 |
|
---|
3205 | if (!pIoCtx->Req.Discard.cbDiscardLeft)
|
---|
3206 | {
|
---|
3207 | offStart = paRanges[pIoCtx->Req.Discard.idxRange].offStart;
|
---|
3208 | cbDiscardLeft = paRanges[pIoCtx->Req.Discard.idxRange].cbRange;
|
---|
3209 | LogFlowFunc(("New range descriptor loaded (%u) offStart=%llu cbDiscard=%zu\n",
|
---|
3210 | pIoCtx->Req.Discard.idxRange, offStart, cbDiscardLeft));
|
---|
3211 | pIoCtx->Req.Discard.idxRange++;
|
---|
3212 | }
|
---|
3213 |
|
---|
3214 | /* Look for a matching block in the AVL tree first. */
|
---|
3215 | PVDDISCARDBLOCK pBlock = (PVDDISCARDBLOCK)RTAvlrU64GetBestFit(pDiscard->pTreeBlocks, offStart, false);
|
---|
3216 | if (!pBlock || pBlock->Core.KeyLast < offStart)
|
---|
3217 | {
|
---|
3218 | PVDDISCARDBLOCK pBlockAbove = (PVDDISCARDBLOCK)RTAvlrU64GetBestFit(pDiscard->pTreeBlocks, offStart, true);
|
---|
3219 |
|
---|
3220 | /* Clip range to remain in the current block. */
|
---|
3221 | if (pBlockAbove)
|
---|
3222 | cbThisDiscard = RT_MIN(cbDiscardLeft, pBlockAbove->Core.KeyLast - offStart + 1);
|
---|
3223 | else
|
---|
3224 | cbThisDiscard = cbDiscardLeft;
|
---|
3225 |
|
---|
3226 | Assert(!(cbThisDiscard % 512));
|
---|
3227 | pIoCtx->Req.Discard.pBlock = NULL;
|
---|
3228 | pIoCtx->pfnIoCtxTransferNext = vdDiscardCurrentRangeAsync;
|
---|
3229 | }
|
---|
3230 | else
|
---|
3231 | {
|
---|
3232 | /* Range lies partly in the block, update allocation bitmap. */
|
---|
3233 | int32_t idxStart, idxEnd;
|
---|
3234 |
|
---|
3235 | cbThisDiscard = RT_MIN(cbDiscardLeft, pBlock->Core.KeyLast - offStart + 1);
|
---|
3236 |
|
---|
3237 | AssertPtr(pBlock);
|
---|
3238 |
|
---|
3239 | Assert(!(cbThisDiscard % 512));
|
---|
3240 | Assert(!((offStart - pBlock->Core.Key) % 512));
|
---|
3241 |
|
---|
3242 | idxStart = (offStart - pBlock->Core.Key) / 512;
|
---|
3243 | idxEnd = idxStart + (cbThisDiscard / 512);
|
---|
3244 |
|
---|
3245 | ASMBitClearRange(pBlock->pbmAllocated, idxStart, idxEnd);
|
---|
3246 |
|
---|
3247 | cbDiscardLeft -= cbThisDiscard;
|
---|
3248 | offStart += cbThisDiscard;
|
---|
3249 |
|
---|
3250 | /* Call the backend to discard the block if it is completely unallocated now. */
|
---|
3251 | if (ASMBitFirstSet((volatile void *)pBlock->pbmAllocated, pBlock->cbDiscard / 512) == -1)
|
---|
3252 | {
|
---|
3253 | pIoCtx->Req.Discard.pBlock = pBlock;
|
---|
3254 | pIoCtx->pfnIoCtxTransferNext = vdDiscardWholeBlockAsync;
|
---|
3255 | rc = VINF_SUCCESS;
|
---|
3256 | }
|
---|
3257 | else
|
---|
3258 | {
|
---|
3259 | RTListNodeRemove(&pBlock->NodeLru);
|
---|
3260 | RTListPrepend(&pDiscard->ListLru, &pBlock->NodeLru);
|
---|
3261 |
|
---|
3262 | /* Start with next range. */
|
---|
3263 | pIoCtx->pfnIoCtxTransferNext = vdDiscardHelperAsync;
|
---|
3264 | rc = VINF_SUCCESS;
|
---|
3265 | }
|
---|
3266 | }
|
---|
3267 |
|
---|
3268 | /* Save state in the context. */
|
---|
3269 | pIoCtx->Req.Discard.offCur = offStart;
|
---|
3270 | pIoCtx->Req.Discard.cbDiscardLeft = cbDiscardLeft;
|
---|
3271 | pIoCtx->Req.Discard.cbThisDiscard = cbThisDiscard;
|
---|
3272 | }
|
---|
3273 |
|
---|
3274 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
3275 | return rc;
|
---|
3276 | }
|
---|
3277 |
|
---|
3278 | /**
|
---|
3279 | * internal: scans plugin directory and loads the backends have been found.
|
---|
3280 | */
|
---|
3281 | static int vdLoadDynamicBackends()
|
---|
3282 | {
|
---|
3283 | #ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
3284 | int rc = VINF_SUCCESS;
|
---|
3285 | PRTDIR pPluginDir = NULL;
|
---|
3286 |
|
---|
3287 | /* Enumerate plugin backends. */
|
---|
3288 | char szPath[RTPATH_MAX];
|
---|
3289 | rc = RTPathAppPrivateArch(szPath, sizeof(szPath));
|
---|
3290 | if (RT_FAILURE(rc))
|
---|
3291 | return rc;
|
---|
3292 |
|
---|
3293 | /* To get all entries with VBoxHDD as prefix. */
|
---|
3294 | char *pszPluginFilter = RTPathJoinA(szPath, VBOX_HDDFORMAT_PLUGIN_PREFIX "*");
|
---|
3295 | if (!pszPluginFilter)
|
---|
3296 | return VERR_NO_STR_MEMORY;
|
---|
3297 |
|
---|
3298 | PRTDIRENTRYEX pPluginDirEntry = NULL;
|
---|
3299 | size_t cbPluginDirEntry = sizeof(RTDIRENTRYEX);
|
---|
3300 | /* The plugins are in the same directory as the other shared libs. */
|
---|
3301 | rc = RTDirOpenFiltered(&pPluginDir, pszPluginFilter, RTDIRFILTER_WINNT, 0);
|
---|
3302 | if (RT_FAILURE(rc))
|
---|
3303 | {
|
---|
3304 | /* On Windows the above immediately signals that there are no
|
---|
3305 | * files matching, while on other platforms enumerating the
|
---|
3306 | * files below fails. Either way: no plugins. */
|
---|
3307 | goto out;
|
---|
3308 | }
|
---|
3309 |
|
---|
3310 | pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(sizeof(RTDIRENTRYEX));
|
---|
3311 | if (!pPluginDirEntry)
|
---|
3312 | {
|
---|
3313 | rc = VERR_NO_MEMORY;
|
---|
3314 | goto out;
|
---|
3315 | }
|
---|
3316 |
|
---|
3317 | while ((rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK)) != VERR_NO_MORE_FILES)
|
---|
3318 | {
|
---|
3319 | RTLDRMOD hPlugin = NIL_RTLDRMOD;
|
---|
3320 | PFNVBOXHDDFORMATLOAD pfnHDDFormatLoad = NULL;
|
---|
3321 | PVBOXHDDBACKEND pBackend = NULL;
|
---|
3322 | char *pszPluginPath = NULL;
|
---|
3323 |
|
---|
3324 | if (rc == VERR_BUFFER_OVERFLOW)
|
---|
3325 | {
|
---|
3326 | /* allocate new buffer. */
|
---|
3327 | RTMemFree(pPluginDirEntry);
|
---|
3328 | pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(cbPluginDirEntry);
|
---|
3329 | if (!pPluginDirEntry)
|
---|
3330 | {
|
---|
3331 | rc = VERR_NO_MEMORY;
|
---|
3332 | break;
|
---|
3333 | }
|
---|
3334 | /* Retry. */
|
---|
3335 | rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
|
---|
3336 | if (RT_FAILURE(rc))
|
---|
3337 | break;
|
---|
3338 | }
|
---|
3339 | else if (RT_FAILURE(rc))
|
---|
3340 | break;
|
---|
3341 |
|
---|
3342 | /* We got the new entry. */
|
---|
3343 | if (!RTFS_IS_FILE(pPluginDirEntry->Info.Attr.fMode))
|
---|
3344 | continue;
|
---|
3345 |
|
---|
3346 | /* Prepend the path to the libraries. */
|
---|
3347 | pszPluginPath = RTPathJoinA(szPath, pPluginDirEntry->szName);
|
---|
3348 | if (!pszPluginPath)
|
---|
3349 | {
|
---|
3350 | rc = VERR_NO_STR_MEMORY;
|
---|
3351 | break;
|
---|
3352 | }
|
---|
3353 |
|
---|
3354 | rc = SUPR3HardenedLdrLoadPlugIn(pszPluginPath, &hPlugin, NULL);
|
---|
3355 | if (RT_SUCCESS(rc))
|
---|
3356 | {
|
---|
3357 | rc = RTLdrGetSymbol(hPlugin, VBOX_HDDFORMAT_LOAD_NAME, (void**)&pfnHDDFormatLoad);
|
---|
3358 | if (RT_FAILURE(rc) || !pfnHDDFormatLoad)
|
---|
3359 | {
|
---|
3360 | LogFunc(("error resolving the entry point %s in plugin %s, rc=%Rrc, pfnHDDFormat=%#p\n", VBOX_HDDFORMAT_LOAD_NAME, pPluginDirEntry->szName, rc, pfnHDDFormatLoad));
|
---|
3361 | if (RT_SUCCESS(rc))
|
---|
3362 | rc = VERR_SYMBOL_NOT_FOUND;
|
---|
3363 | }
|
---|
3364 |
|
---|
3365 | if (RT_SUCCESS(rc))
|
---|
3366 | {
|
---|
3367 | /* Get the function table. */
|
---|
3368 | rc = pfnHDDFormatLoad(&pBackend);
|
---|
3369 | if (RT_SUCCESS(rc) && pBackend->cbSize == sizeof(VBOXHDDBACKEND))
|
---|
3370 | {
|
---|
3371 | pBackend->hPlugin = hPlugin;
|
---|
3372 | vdAddBackend(pBackend);
|
---|
3373 | }
|
---|
3374 | else
|
---|
3375 | LogFunc(("ignored plugin '%s': pBackend->cbSize=%d rc=%Rrc\n", pszPluginPath, pBackend->cbSize, rc));
|
---|
3376 | }
|
---|
3377 | else
|
---|
3378 | LogFunc(("ignored plugin '%s': rc=%Rrc\n", pszPluginPath, rc));
|
---|
3379 |
|
---|
3380 | if (RT_FAILURE(rc))
|
---|
3381 | RTLdrClose(hPlugin);
|
---|
3382 | }
|
---|
3383 | RTStrFree(pszPluginPath);
|
---|
3384 | }
|
---|
3385 | out:
|
---|
3386 | if (rc == VERR_NO_MORE_FILES)
|
---|
3387 | rc = VINF_SUCCESS;
|
---|
3388 | RTStrFree(pszPluginFilter);
|
---|
3389 | if (pPluginDirEntry)
|
---|
3390 | RTMemFree(pPluginDirEntry);
|
---|
3391 | if (pPluginDir)
|
---|
3392 | RTDirClose(pPluginDir);
|
---|
3393 | return rc;
|
---|
3394 | #else
|
---|
3395 | return VINF_SUCCESS;
|
---|
3396 | #endif
|
---|
3397 | }
|
---|
3398 |
|
---|
3399 | /**
|
---|
3400 | * internal: scans plugin directory and loads the cache backends have been found.
|
---|
3401 | */
|
---|
3402 | static int vdLoadDynamicCacheBackends()
|
---|
3403 | {
|
---|
3404 | #ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
3405 | int rc = VINF_SUCCESS;
|
---|
3406 | PRTDIR pPluginDir = NULL;
|
---|
3407 |
|
---|
3408 | /* Enumerate plugin backends. */
|
---|
3409 | char szPath[RTPATH_MAX];
|
---|
3410 | rc = RTPathAppPrivateArch(szPath, sizeof(szPath));
|
---|
3411 | if (RT_FAILURE(rc))
|
---|
3412 | return rc;
|
---|
3413 |
|
---|
3414 | /* To get all entries with VBoxHDD as prefix. */
|
---|
3415 | char *pszPluginFilter = RTPathJoinA(szPath, VD_CACHEFORMAT_PLUGIN_PREFIX "*");
|
---|
3416 | if (!pszPluginFilter)
|
---|
3417 | {
|
---|
3418 | rc = VERR_NO_STR_MEMORY;
|
---|
3419 | return rc;
|
---|
3420 | }
|
---|
3421 |
|
---|
3422 | PRTDIRENTRYEX pPluginDirEntry = NULL;
|
---|
3423 | size_t cbPluginDirEntry = sizeof(RTDIRENTRYEX);
|
---|
3424 | /* The plugins are in the same directory as the other shared libs. */
|
---|
3425 | rc = RTDirOpenFiltered(&pPluginDir, pszPluginFilter, RTDIRFILTER_WINNT, 0);
|
---|
3426 | if (RT_FAILURE(rc))
|
---|
3427 | {
|
---|
3428 | /* On Windows the above immediately signals that there are no
|
---|
3429 | * files matching, while on other platforms enumerating the
|
---|
3430 | * files below fails. Either way: no plugins. */
|
---|
3431 | goto out;
|
---|
3432 | }
|
---|
3433 |
|
---|
3434 | pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(sizeof(RTDIRENTRYEX));
|
---|
3435 | if (!pPluginDirEntry)
|
---|
3436 | {
|
---|
3437 | rc = VERR_NO_MEMORY;
|
---|
3438 | goto out;
|
---|
3439 | }
|
---|
3440 |
|
---|
3441 | while ((rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK)) != VERR_NO_MORE_FILES)
|
---|
3442 | {
|
---|
3443 | RTLDRMOD hPlugin = NIL_RTLDRMOD;
|
---|
3444 | PFNVDCACHEFORMATLOAD pfnVDCacheLoad = NULL;
|
---|
3445 | PVDCACHEBACKEND pBackend = NULL;
|
---|
3446 | char *pszPluginPath = NULL;
|
---|
3447 |
|
---|
3448 | if (rc == VERR_BUFFER_OVERFLOW)
|
---|
3449 | {
|
---|
3450 | /* allocate new buffer. */
|
---|
3451 | RTMemFree(pPluginDirEntry);
|
---|
3452 | pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(cbPluginDirEntry);
|
---|
3453 | if (!pPluginDirEntry)
|
---|
3454 | {
|
---|
3455 | rc = VERR_NO_MEMORY;
|
---|
3456 | break;
|
---|
3457 | }
|
---|
3458 | /* Retry. */
|
---|
3459 | rc = RTDirReadEx(pPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
|
---|
3460 | if (RT_FAILURE(rc))
|
---|
3461 | break;
|
---|
3462 | }
|
---|
3463 | else if (RT_FAILURE(rc))
|
---|
3464 | break;
|
---|
3465 |
|
---|
3466 | /* We got the new entry. */
|
---|
3467 | if (!RTFS_IS_FILE(pPluginDirEntry->Info.Attr.fMode))
|
---|
3468 | continue;
|
---|
3469 |
|
---|
3470 | /* Prepend the path to the libraries. */
|
---|
3471 | pszPluginPath = RTPathJoinA(szPath, pPluginDirEntry->szName);
|
---|
3472 | if (!pszPluginPath)
|
---|
3473 | {
|
---|
3474 | rc = VERR_NO_STR_MEMORY;
|
---|
3475 | break;
|
---|
3476 | }
|
---|
3477 |
|
---|
3478 | rc = SUPR3HardenedLdrLoadPlugIn(pszPluginPath, &hPlugin, NULL);
|
---|
3479 | if (RT_SUCCESS(rc))
|
---|
3480 | {
|
---|
3481 | rc = RTLdrGetSymbol(hPlugin, VD_CACHEFORMAT_LOAD_NAME, (void**)&pfnVDCacheLoad);
|
---|
3482 | if (RT_FAILURE(rc) || !pfnVDCacheLoad)
|
---|
3483 | {
|
---|
3484 | LogFunc(("error resolving the entry point %s in plugin %s, rc=%Rrc, pfnVDCacheLoad=%#p\n",
|
---|
3485 | VD_CACHEFORMAT_LOAD_NAME, pPluginDirEntry->szName, rc, pfnVDCacheLoad));
|
---|
3486 | if (RT_SUCCESS(rc))
|
---|
3487 | rc = VERR_SYMBOL_NOT_FOUND;
|
---|
3488 | }
|
---|
3489 |
|
---|
3490 | if (RT_SUCCESS(rc))
|
---|
3491 | {
|
---|
3492 | /* Get the function table. */
|
---|
3493 | rc = pfnVDCacheLoad(&pBackend);
|
---|
3494 | if (RT_SUCCESS(rc) && pBackend->cbSize == sizeof(VDCACHEBACKEND))
|
---|
3495 | {
|
---|
3496 | pBackend->hPlugin = hPlugin;
|
---|
3497 | vdAddCacheBackend(pBackend);
|
---|
3498 | }
|
---|
3499 | else
|
---|
3500 | LogFunc(("ignored plugin '%s': pBackend->cbSize=%d rc=%Rrc\n", pszPluginPath, pBackend->cbSize, rc));
|
---|
3501 | }
|
---|
3502 | else
|
---|
3503 | LogFunc(("ignored plugin '%s': rc=%Rrc\n", pszPluginPath, rc));
|
---|
3504 |
|
---|
3505 | if (RT_FAILURE(rc))
|
---|
3506 | RTLdrClose(hPlugin);
|
---|
3507 | }
|
---|
3508 | RTStrFree(pszPluginPath);
|
---|
3509 | }
|
---|
3510 | out:
|
---|
3511 | if (rc == VERR_NO_MORE_FILES)
|
---|
3512 | rc = VINF_SUCCESS;
|
---|
3513 | RTStrFree(pszPluginFilter);
|
---|
3514 | if (pPluginDirEntry)
|
---|
3515 | RTMemFree(pPluginDirEntry);
|
---|
3516 | if (pPluginDir)
|
---|
3517 | RTDirClose(pPluginDir);
|
---|
3518 | return rc;
|
---|
3519 | #else
|
---|
3520 | return VINF_SUCCESS;
|
---|
3521 | #endif
|
---|
3522 | }
|
---|
3523 |
|
---|
3524 | /**
|
---|
3525 | * VD async I/O interface open callback.
|
---|
3526 | */
|
---|
3527 | static int vdIOOpenFallback(void *pvUser, const char *pszLocation,
|
---|
3528 | uint32_t fOpen, PFNVDCOMPLETED pfnCompleted,
|
---|
3529 | void **ppStorage)
|
---|
3530 | {
|
---|
3531 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)RTMemAllocZ(sizeof(VDIIOFALLBACKSTORAGE));
|
---|
3532 |
|
---|
3533 | if (!pStorage)
|
---|
3534 | return VERR_NO_MEMORY;
|
---|
3535 |
|
---|
3536 | pStorage->pfnCompleted = pfnCompleted;
|
---|
3537 |
|
---|
3538 | /* Open the file. */
|
---|
3539 | int rc = RTFileOpen(&pStorage->File, pszLocation, fOpen);
|
---|
3540 | if (RT_SUCCESS(rc))
|
---|
3541 | {
|
---|
3542 | *ppStorage = pStorage;
|
---|
3543 | return VINF_SUCCESS;
|
---|
3544 | }
|
---|
3545 |
|
---|
3546 | RTMemFree(pStorage);
|
---|
3547 | return rc;
|
---|
3548 | }
|
---|
3549 |
|
---|
3550 | /**
|
---|
3551 | * VD async I/O interface close callback.
|
---|
3552 | */
|
---|
3553 | static int vdIOCloseFallback(void *pvUser, void *pvStorage)
|
---|
3554 | {
|
---|
3555 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
3556 |
|
---|
3557 | RTFileClose(pStorage->File);
|
---|
3558 | RTMemFree(pStorage);
|
---|
3559 | return VINF_SUCCESS;
|
---|
3560 | }
|
---|
3561 |
|
---|
3562 | static int vdIODeleteFallback(void *pvUser, const char *pcszFilename)
|
---|
3563 | {
|
---|
3564 | return RTFileDelete(pcszFilename);
|
---|
3565 | }
|
---|
3566 |
|
---|
3567 | static int vdIOMoveFallback(void *pvUser, const char *pcszSrc, const char *pcszDst, unsigned fMove)
|
---|
3568 | {
|
---|
3569 | return RTFileMove(pcszSrc, pcszDst, fMove);
|
---|
3570 | }
|
---|
3571 |
|
---|
3572 | static int vdIOGetFreeSpaceFallback(void *pvUser, const char *pcszFilename, int64_t *pcbFreeSpace)
|
---|
3573 | {
|
---|
3574 | return RTFsQuerySizes(pcszFilename, NULL, pcbFreeSpace, NULL, NULL);
|
---|
3575 | }
|
---|
3576 |
|
---|
3577 | static int vdIOGetModificationTimeFallback(void *pvUser, const char *pcszFilename, PRTTIMESPEC pModificationTime)
|
---|
3578 | {
|
---|
3579 | RTFSOBJINFO info;
|
---|
3580 | int rc = RTPathQueryInfo(pcszFilename, &info, RTFSOBJATTRADD_NOTHING);
|
---|
3581 | if (RT_SUCCESS(rc))
|
---|
3582 | *pModificationTime = info.ModificationTime;
|
---|
3583 | return rc;
|
---|
3584 | }
|
---|
3585 |
|
---|
3586 | /**
|
---|
3587 | * VD async I/O interface callback for retrieving the file size.
|
---|
3588 | */
|
---|
3589 | static int vdIOGetSizeFallback(void *pvUser, void *pvStorage, uint64_t *pcbSize)
|
---|
3590 | {
|
---|
3591 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
3592 |
|
---|
3593 | return RTFileGetSize(pStorage->File, pcbSize);
|
---|
3594 | }
|
---|
3595 |
|
---|
3596 | /**
|
---|
3597 | * VD async I/O interface callback for setting the file size.
|
---|
3598 | */
|
---|
3599 | static int vdIOSetSizeFallback(void *pvUser, void *pvStorage, uint64_t cbSize)
|
---|
3600 | {
|
---|
3601 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
3602 |
|
---|
3603 | return RTFileSetSize(pStorage->File, cbSize);
|
---|
3604 | }
|
---|
3605 |
|
---|
3606 | /**
|
---|
3607 | * VD async I/O interface callback for a synchronous write to the file.
|
---|
3608 | */
|
---|
3609 | static int vdIOWriteSyncFallback(void *pvUser, void *pvStorage, uint64_t uOffset,
|
---|
3610 | const void *pvBuf, size_t cbWrite, size_t *pcbWritten)
|
---|
3611 | {
|
---|
3612 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
3613 |
|
---|
3614 | return RTFileWriteAt(pStorage->File, uOffset, pvBuf, cbWrite, pcbWritten);
|
---|
3615 | }
|
---|
3616 |
|
---|
3617 | /**
|
---|
3618 | * VD async I/O interface callback for a synchronous read from the file.
|
---|
3619 | */
|
---|
3620 | static int vdIOReadSyncFallback(void *pvUser, void *pvStorage, uint64_t uOffset,
|
---|
3621 | void *pvBuf, size_t cbRead, size_t *pcbRead)
|
---|
3622 | {
|
---|
3623 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
3624 |
|
---|
3625 | return RTFileReadAt(pStorage->File, uOffset, pvBuf, cbRead, pcbRead);
|
---|
3626 | }
|
---|
3627 |
|
---|
3628 | /**
|
---|
3629 | * VD async I/O interface callback for a synchronous flush of the file data.
|
---|
3630 | */
|
---|
3631 | static int vdIOFlushSyncFallback(void *pvUser, void *pvStorage)
|
---|
3632 | {
|
---|
3633 | PVDIIOFALLBACKSTORAGE pStorage = (PVDIIOFALLBACKSTORAGE)pvStorage;
|
---|
3634 |
|
---|
3635 | return RTFileFlush(pStorage->File);
|
---|
3636 | }
|
---|
3637 |
|
---|
3638 | /**
|
---|
3639 | * VD async I/O interface callback for a asynchronous read from the file.
|
---|
3640 | */
|
---|
3641 | static int vdIOReadAsyncFallback(void *pvUser, void *pStorage, uint64_t uOffset,
|
---|
3642 | PCRTSGSEG paSegments, size_t cSegments,
|
---|
3643 | size_t cbRead, void *pvCompletion,
|
---|
3644 | void **ppTask)
|
---|
3645 | {
|
---|
3646 | return VERR_NOT_IMPLEMENTED;
|
---|
3647 | }
|
---|
3648 |
|
---|
3649 | /**
|
---|
3650 | * VD async I/O interface callback for a asynchronous write to the file.
|
---|
3651 | */
|
---|
3652 | static int vdIOWriteAsyncFallback(void *pvUser, void *pStorage, uint64_t uOffset,
|
---|
3653 | PCRTSGSEG paSegments, size_t cSegments,
|
---|
3654 | size_t cbWrite, void *pvCompletion,
|
---|
3655 | void **ppTask)
|
---|
3656 | {
|
---|
3657 | return VERR_NOT_IMPLEMENTED;
|
---|
3658 | }
|
---|
3659 |
|
---|
3660 | /**
|
---|
3661 | * VD async I/O interface callback for a asynchronous flush of the file data.
|
---|
3662 | */
|
---|
3663 | static int vdIOFlushAsyncFallback(void *pvUser, void *pStorage,
|
---|
3664 | void *pvCompletion, void **ppTask)
|
---|
3665 | {
|
---|
3666 | return VERR_NOT_IMPLEMENTED;
|
---|
3667 | }
|
---|
3668 |
|
---|
3669 | /**
|
---|
3670 | * Internal - Continues an I/O context after
|
---|
3671 | * it was halted because of an active transfer.
|
---|
3672 | */
|
---|
3673 | static int vdIoCtxContinue(PVDIOCTX pIoCtx, int rcReq)
|
---|
3674 | {
|
---|
3675 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
3676 | int rc = VINF_SUCCESS;
|
---|
3677 |
|
---|
3678 | VD_IS_LOCKED(pDisk);
|
---|
3679 |
|
---|
3680 | if (RT_FAILURE(rcReq))
|
---|
3681 | ASMAtomicCmpXchgS32(&pIoCtx->rcReq, rcReq, VINF_SUCCESS);
|
---|
3682 |
|
---|
3683 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_BLOCKED))
|
---|
3684 | {
|
---|
3685 | /* Continue the transfer */
|
---|
3686 | rc = vdIoCtxProcessLocked(pIoCtx);
|
---|
3687 |
|
---|
3688 | if ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
3689 | && ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
|
---|
3690 | {
|
---|
3691 | LogFlowFunc(("I/O context completed pIoCtx=%#p\n", pIoCtx));
|
---|
3692 | if (pIoCtx->pIoCtxParent)
|
---|
3693 | {
|
---|
3694 | PVDIOCTX pIoCtxParent = pIoCtx->pIoCtxParent;
|
---|
3695 |
|
---|
3696 | Assert(!pIoCtxParent->pIoCtxParent);
|
---|
3697 | if (RT_FAILURE(pIoCtx->rcReq))
|
---|
3698 | ASMAtomicCmpXchgS32(&pIoCtxParent->rcReq, pIoCtx->rcReq, VINF_SUCCESS);
|
---|
3699 |
|
---|
3700 | ASMAtomicDecU32(&pIoCtxParent->cDataTransfersPending);
|
---|
3701 |
|
---|
3702 | if (pIoCtx->enmTxDir == VDIOCTXTXDIR_WRITE)
|
---|
3703 | {
|
---|
3704 | LogFlowFunc(("I/O context transferred %u bytes for the parent pIoCtxParent=%p\n",
|
---|
3705 | pIoCtx->Type.Child.cbTransferParent, pIoCtxParent));
|
---|
3706 |
|
---|
3707 | /* Update the parent state. */
|
---|
3708 | Assert(pIoCtxParent->Req.Io.cbTransferLeft >= pIoCtx->Type.Child.cbTransferParent);
|
---|
3709 | ASMAtomicSubU32(&pIoCtxParent->Req.Io.cbTransferLeft, pIoCtx->Type.Child.cbTransferParent);
|
---|
3710 | }
|
---|
3711 | else
|
---|
3712 | Assert(pIoCtx->enmTxDir == VDIOCTXTXDIR_FLUSH);
|
---|
3713 |
|
---|
3714 | /*
|
---|
3715 | * A completed child write means that we finished growing the image.
|
---|
3716 | * We have to process any pending writes now.
|
---|
3717 | */
|
---|
3718 | vdIoCtxUnlockDisk(pDisk, pIoCtxParent, false /* fProcessDeferredReqs */);
|
---|
3719 |
|
---|
3720 | /* Unblock the parent */
|
---|
3721 | pIoCtxParent->fFlags &= ~VDIOCTX_FLAGS_BLOCKED;
|
---|
3722 |
|
---|
3723 | rc = vdIoCtxProcessLocked(pIoCtxParent);
|
---|
3724 |
|
---|
3725 | if ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
3726 | && ASMAtomicCmpXchgBool(&pIoCtxParent->fComplete, true, false))
|
---|
3727 | {
|
---|
3728 | LogFlowFunc(("Parent I/O context completed pIoCtxParent=%#p rcReq=%Rrc\n", pIoCtxParent, pIoCtxParent->rcReq));
|
---|
3729 | pIoCtxParent->Type.Root.pfnComplete(pIoCtxParent->Type.Root.pvUser1,
|
---|
3730 | pIoCtxParent->Type.Root.pvUser2,
|
---|
3731 | pIoCtxParent->rcReq);
|
---|
3732 | vdThreadFinishWrite(pDisk);
|
---|
3733 | vdIoCtxFree(pDisk, pIoCtxParent);
|
---|
3734 | vdDiskProcessBlockedIoCtx(pDisk);
|
---|
3735 | }
|
---|
3736 | else if (!vdIoCtxIsDiskLockOwner(pDisk, pIoCtx))
|
---|
3737 | {
|
---|
3738 | /* Process any pending writes if the current request didn't caused another growing. */
|
---|
3739 | vdDiskProcessBlockedIoCtx(pDisk);
|
---|
3740 | }
|
---|
3741 | }
|
---|
3742 | else
|
---|
3743 | {
|
---|
3744 | if (pIoCtx->enmTxDir == VDIOCTXTXDIR_FLUSH)
|
---|
3745 | {
|
---|
3746 | vdIoCtxUnlockDisk(pDisk, pIoCtx, true /* fProcessDerredReqs */);
|
---|
3747 | vdThreadFinishWrite(pDisk);
|
---|
3748 | }
|
---|
3749 | else if ( pIoCtx->enmTxDir == VDIOCTXTXDIR_WRITE
|
---|
3750 | || pIoCtx->enmTxDir == VDIOCTXTXDIR_DISCARD)
|
---|
3751 | vdThreadFinishWrite(pDisk);
|
---|
3752 | else
|
---|
3753 | {
|
---|
3754 | Assert(pIoCtx->enmTxDir == VDIOCTXTXDIR_READ);
|
---|
3755 | vdThreadFinishRead(pDisk);
|
---|
3756 | }
|
---|
3757 |
|
---|
3758 | LogFlowFunc(("I/O context completed pIoCtx=%#p rcReq=%Rrc\n", pIoCtx, pIoCtx->rcReq));
|
---|
3759 | pIoCtx->Type.Root.pfnComplete(pIoCtx->Type.Root.pvUser1,
|
---|
3760 | pIoCtx->Type.Root.pvUser2,
|
---|
3761 | pIoCtx->rcReq);
|
---|
3762 | }
|
---|
3763 |
|
---|
3764 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
3765 | }
|
---|
3766 | }
|
---|
3767 |
|
---|
3768 | return VINF_SUCCESS;
|
---|
3769 | }
|
---|
3770 |
|
---|
3771 | /**
|
---|
3772 | * Internal - Called when user transfer completed.
|
---|
3773 | */
|
---|
3774 | static int vdUserXferCompleted(PVDIOSTORAGE pIoStorage, PVDIOCTX pIoCtx,
|
---|
3775 | PFNVDXFERCOMPLETED pfnComplete, void *pvUser,
|
---|
3776 | size_t cbTransfer, int rcReq)
|
---|
3777 | {
|
---|
3778 | int rc = VINF_SUCCESS;
|
---|
3779 | bool fIoCtxContinue = true;
|
---|
3780 | PVBOXHDD pDisk = pIoCtx->pDisk;
|
---|
3781 |
|
---|
3782 | LogFlowFunc(("pIoStorage=%#p pIoCtx=%#p pfnComplete=%#p pvUser=%#p cbTransfer=%zu rcReq=%Rrc\n",
|
---|
3783 | pIoStorage, pIoCtx, pfnComplete, pvUser, cbTransfer, rcReq));
|
---|
3784 |
|
---|
3785 | VD_IS_LOCKED(pDisk);
|
---|
3786 |
|
---|
3787 | Assert(pIoCtx->Req.Io.cbTransferLeft >= cbTransfer);
|
---|
3788 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, cbTransfer);
|
---|
3789 | ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
|
---|
3790 |
|
---|
3791 | if (pfnComplete)
|
---|
3792 | rc = pfnComplete(pIoStorage->pVDIo->pBackendData, pIoCtx, pvUser, rcReq);
|
---|
3793 |
|
---|
3794 | if (RT_SUCCESS(rc))
|
---|
3795 | rc = vdIoCtxContinue(pIoCtx, rcReq);
|
---|
3796 | else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
3797 | rc = VINF_SUCCESS;
|
---|
3798 |
|
---|
3799 | return rc;
|
---|
3800 | }
|
---|
3801 |
|
---|
3802 | /**
|
---|
3803 | * Internal - Called when a meta transfer completed.
|
---|
3804 | */
|
---|
3805 | static int vdMetaXferCompleted(PVDIOSTORAGE pIoStorage, PFNVDXFERCOMPLETED pfnComplete, void *pvUser,
|
---|
3806 | PVDMETAXFER pMetaXfer, int rcReq)
|
---|
3807 | {
|
---|
3808 | PVBOXHDD pDisk = pIoStorage->pVDIo->pDisk;
|
---|
3809 | RTLISTNODE ListIoCtxWaiting;
|
---|
3810 | bool fFlush;
|
---|
3811 |
|
---|
3812 | LogFlowFunc(("pIoStorage=%#p pfnComplete=%#p pvUser=%#p pMetaXfer=%#p rcReq=%Rrc\n",
|
---|
3813 | pIoStorage, pfnComplete, pvUser, pMetaXfer, rcReq));
|
---|
3814 |
|
---|
3815 | VD_IS_LOCKED(pDisk);
|
---|
3816 |
|
---|
3817 | fFlush = VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_FLUSH;
|
---|
3818 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
|
---|
3819 |
|
---|
3820 | if (!fFlush)
|
---|
3821 | {
|
---|
3822 | RTListMove(&ListIoCtxWaiting, &pMetaXfer->ListIoCtxWaiting);
|
---|
3823 |
|
---|
3824 | if (RT_FAILURE(rcReq))
|
---|
3825 | {
|
---|
3826 | /* Remove from the AVL tree. */
|
---|
3827 | LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
|
---|
3828 | bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
|
---|
3829 | Assert(fRemoved);
|
---|
3830 | RTMemFree(pMetaXfer);
|
---|
3831 | }
|
---|
3832 | else
|
---|
3833 | {
|
---|
3834 | /* Increase the reference counter to make sure it doesn't go away before the last context is processed. */
|
---|
3835 | pMetaXfer->cRefs++;
|
---|
3836 | }
|
---|
3837 | }
|
---|
3838 | else
|
---|
3839 | RTListMove(&ListIoCtxWaiting, &pMetaXfer->ListIoCtxWaiting);
|
---|
3840 |
|
---|
3841 | /* Go through the waiting list and continue the I/O contexts. */
|
---|
3842 | while (!RTListIsEmpty(&ListIoCtxWaiting))
|
---|
3843 | {
|
---|
3844 | int rc = VINF_SUCCESS;
|
---|
3845 | bool fContinue = true;
|
---|
3846 | PVDIOCTXDEFERRED pDeferred = RTListGetFirst(&ListIoCtxWaiting, VDIOCTXDEFERRED, NodeDeferred);
|
---|
3847 | PVDIOCTX pIoCtx = pDeferred->pIoCtx;
|
---|
3848 | RTListNodeRemove(&pDeferred->NodeDeferred);
|
---|
3849 |
|
---|
3850 | RTMemFree(pDeferred);
|
---|
3851 | ASMAtomicDecU32(&pIoCtx->cMetaTransfersPending);
|
---|
3852 |
|
---|
3853 | if (pfnComplete)
|
---|
3854 | rc = pfnComplete(pIoStorage->pVDIo->pBackendData, pIoCtx, pvUser, rcReq);
|
---|
3855 |
|
---|
3856 | LogFlow(("Completion callback for I/O context %#p returned %Rrc\n", pIoCtx, rc));
|
---|
3857 |
|
---|
3858 | if (RT_SUCCESS(rc))
|
---|
3859 | {
|
---|
3860 | rc = vdIoCtxContinue(pIoCtx, rcReq);
|
---|
3861 | AssertRC(rc);
|
---|
3862 | }
|
---|
3863 | else
|
---|
3864 | Assert(rc == VERR_VD_ASYNC_IO_IN_PROGRESS);
|
---|
3865 | }
|
---|
3866 |
|
---|
3867 | /* Remove if not used anymore. */
|
---|
3868 | if (RT_SUCCESS(rcReq) && !fFlush)
|
---|
3869 | {
|
---|
3870 | pMetaXfer->cRefs--;
|
---|
3871 | if (!pMetaXfer->cRefs && RTListIsEmpty(&pMetaXfer->ListIoCtxWaiting))
|
---|
3872 | {
|
---|
3873 | /* Remove from the AVL tree. */
|
---|
3874 | LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
|
---|
3875 | bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
|
---|
3876 | Assert(fRemoved);
|
---|
3877 | RTMemFree(pMetaXfer);
|
---|
3878 | }
|
---|
3879 | }
|
---|
3880 | else if (fFlush)
|
---|
3881 | RTMemFree(pMetaXfer);
|
---|
3882 |
|
---|
3883 | return VINF_SUCCESS;
|
---|
3884 | }
|
---|
3885 |
|
---|
3886 | /**
|
---|
3887 | * Processes a list of waiting I/O tasks. The disk lock must be held by caller.
|
---|
3888 | *
|
---|
3889 | * @returns nothing.
|
---|
3890 | * @param pDisk The disk to process the list for.
|
---|
3891 | */
|
---|
3892 | static void vdIoTaskProcessWaitingList(PVBOXHDD pDisk)
|
---|
3893 | {
|
---|
3894 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
3895 |
|
---|
3896 | VD_IS_LOCKED(pDisk);
|
---|
3897 |
|
---|
3898 | PVDIOTASK pHead = ASMAtomicXchgPtrT(&pDisk->pIoTasksPendingHead, NULL, PVDIOTASK);
|
---|
3899 |
|
---|
3900 | /* Reverse order. */
|
---|
3901 | PVDIOTASK pCur = pHead;
|
---|
3902 | pHead = NULL;
|
---|
3903 | while (pCur)
|
---|
3904 | {
|
---|
3905 | PVDIOTASK pInsert = pCur;
|
---|
3906 | pCur = pCur->pNext;
|
---|
3907 | pInsert->pNext = pHead;
|
---|
3908 | pHead = pInsert;
|
---|
3909 | }
|
---|
3910 |
|
---|
3911 | while (pHead)
|
---|
3912 | {
|
---|
3913 | PVDIOSTORAGE pIoStorage = pHead->pIoStorage;
|
---|
3914 |
|
---|
3915 | if (!pHead->fMeta)
|
---|
3916 | vdUserXferCompleted(pIoStorage, pHead->Type.User.pIoCtx,
|
---|
3917 | pHead->pfnComplete, pHead->pvUser,
|
---|
3918 | pHead->Type.User.cbTransfer, pHead->rcReq);
|
---|
3919 | else
|
---|
3920 | vdMetaXferCompleted(pIoStorage, pHead->pfnComplete, pHead->pvUser,
|
---|
3921 | pHead->Type.Meta.pMetaXfer, pHead->rcReq);
|
---|
3922 |
|
---|
3923 | pCur = pHead;
|
---|
3924 | pHead = pHead->pNext;
|
---|
3925 | vdIoTaskFree(pDisk, pCur);
|
---|
3926 | }
|
---|
3927 | }
|
---|
3928 |
|
---|
3929 | /**
|
---|
3930 | * Process any I/O context on the halted list.
|
---|
3931 | *
|
---|
3932 | * @returns nothing.
|
---|
3933 | * @param pDisk The disk.
|
---|
3934 | */
|
---|
3935 | static void vdIoCtxProcessHaltedList(PVBOXHDD pDisk)
|
---|
3936 | {
|
---|
3937 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
3938 |
|
---|
3939 | VD_IS_LOCKED(pDisk);
|
---|
3940 |
|
---|
3941 | /* Get the waiting list and process it in FIFO order. */
|
---|
3942 | PVDIOCTX pIoCtxHead = ASMAtomicXchgPtrT(&pDisk->pIoCtxHaltedHead, NULL, PVDIOCTX);
|
---|
3943 |
|
---|
3944 | /* Reverse it. */
|
---|
3945 | PVDIOCTX pCur = pIoCtxHead;
|
---|
3946 | pIoCtxHead = NULL;
|
---|
3947 | while (pCur)
|
---|
3948 | {
|
---|
3949 | PVDIOCTX pInsert = pCur;
|
---|
3950 | pCur = pCur->pIoCtxNext;
|
---|
3951 | pInsert->pIoCtxNext = pIoCtxHead;
|
---|
3952 | pIoCtxHead = pInsert;
|
---|
3953 | }
|
---|
3954 |
|
---|
3955 | /* Process now. */
|
---|
3956 | pCur = pIoCtxHead;
|
---|
3957 | while (pCur)
|
---|
3958 | {
|
---|
3959 | PVDIOCTX pTmp = pCur;
|
---|
3960 |
|
---|
3961 | pCur = pCur->pIoCtxNext;
|
---|
3962 | pTmp->pIoCtxNext = NULL;
|
---|
3963 |
|
---|
3964 | /* Continue */
|
---|
3965 | pTmp->fFlags &= ~VDIOCTX_FLAGS_BLOCKED;
|
---|
3966 | vdIoCtxContinue(pTmp, pTmp->rcReq);
|
---|
3967 | }
|
---|
3968 | }
|
---|
3969 |
|
---|
3970 | /**
|
---|
3971 | * Unlock the disk and process pending tasks.
|
---|
3972 | *
|
---|
3973 | * @returns VBox status code.
|
---|
3974 | * @param pDisk The disk to unlock.
|
---|
3975 | */
|
---|
3976 | static int vdDiskUnlock(PVBOXHDD pDisk, PVDIOCTX pIoCtxRc)
|
---|
3977 | {
|
---|
3978 | int rc = VINF_SUCCESS;
|
---|
3979 |
|
---|
3980 | VD_IS_LOCKED(pDisk);
|
---|
3981 |
|
---|
3982 | /*
|
---|
3983 | * Process the list of waiting I/O tasks first
|
---|
3984 | * because they might complete I/O contexts.
|
---|
3985 | * Same for the list of halted I/O contexts.
|
---|
3986 | * Afterwards comes the list of new I/O contexts.
|
---|
3987 | */
|
---|
3988 | vdIoTaskProcessWaitingList(pDisk);
|
---|
3989 | vdIoCtxProcessHaltedList(pDisk);
|
---|
3990 | rc = vdDiskProcessWaitingIoCtx(pDisk, pIoCtxRc);
|
---|
3991 | ASMAtomicXchgBool(&pDisk->fLocked, false);
|
---|
3992 |
|
---|
3993 | /*
|
---|
3994 | * Need to check for new I/O tasks and waiting I/O contexts now
|
---|
3995 | * again as other threads might added them while we processed
|
---|
3996 | * previous lists.
|
---|
3997 | */
|
---|
3998 | while ( ASMAtomicUoReadPtrT(&pDisk->pIoCtxHead, PVDIOCTX) != NULL
|
---|
3999 | || ASMAtomicUoReadPtrT(&pDisk->pIoTasksPendingHead, PVDIOTASK) != NULL
|
---|
4000 | || ASMAtomicUoReadPtrT(&pDisk->pIoCtxHaltedHead, PVDIOCTX) != NULL)
|
---|
4001 | {
|
---|
4002 | /* Try lock disk again. */
|
---|
4003 | if (ASMAtomicCmpXchgBool(&pDisk->fLocked, true, false))
|
---|
4004 | {
|
---|
4005 | vdIoTaskProcessWaitingList(pDisk);
|
---|
4006 | vdIoCtxProcessHaltedList(pDisk);
|
---|
4007 | vdDiskProcessWaitingIoCtx(pDisk, NULL);
|
---|
4008 | ASMAtomicXchgBool(&pDisk->fLocked, false);
|
---|
4009 | }
|
---|
4010 | else /* Let the other thread everything when he unlocks the disk. */
|
---|
4011 | break;
|
---|
4012 | }
|
---|
4013 |
|
---|
4014 | return rc;
|
---|
4015 | }
|
---|
4016 |
|
---|
4017 | /**
|
---|
4018 | * Try to lock the disk to complete pressing of the I/O task.
|
---|
4019 | * The completion is deferred if the disk is locked already.
|
---|
4020 | *
|
---|
4021 | * @returns nothing.
|
---|
4022 | * @param pIoTask The I/O task to complete.
|
---|
4023 | */
|
---|
4024 | static void vdXferTryLockDiskDeferIoTask(PVDIOTASK pIoTask)
|
---|
4025 | {
|
---|
4026 | PVDIOSTORAGE pIoStorage = pIoTask->pIoStorage;
|
---|
4027 | PVBOXHDD pDisk = pIoStorage->pVDIo->pDisk;
|
---|
4028 |
|
---|
4029 | /* Put it on the waiting list. */
|
---|
4030 | PVDIOTASK pNext = ASMAtomicUoReadPtrT(&pDisk->pIoTasksPendingHead, PVDIOTASK);
|
---|
4031 | PVDIOTASK pHeadOld;
|
---|
4032 | pIoTask->pNext = pNext;
|
---|
4033 | while (!ASMAtomicCmpXchgExPtr(&pDisk->pIoTasksPendingHead, pIoTask, pNext, &pHeadOld))
|
---|
4034 | {
|
---|
4035 | pNext = pHeadOld;
|
---|
4036 | Assert(pNext != pIoTask);
|
---|
4037 | pIoTask->pNext = pNext;
|
---|
4038 | ASMNopPause();
|
---|
4039 | }
|
---|
4040 |
|
---|
4041 | if (ASMAtomicCmpXchgBool(&pDisk->fLocked, true, false))
|
---|
4042 | {
|
---|
4043 | /* Release disk lock, it will take care of processing all lists. */
|
---|
4044 | vdDiskUnlock(pDisk, NULL);
|
---|
4045 | }
|
---|
4046 | }
|
---|
4047 |
|
---|
4048 | static int vdIOIntReqCompleted(void *pvUser, int rcReq)
|
---|
4049 | {
|
---|
4050 | PVDIOTASK pIoTask = (PVDIOTASK)pvUser;
|
---|
4051 |
|
---|
4052 | LogFlowFunc(("Task completed pIoTask=%#p\n", pIoTask));
|
---|
4053 |
|
---|
4054 | pIoTask->rcReq = rcReq;
|
---|
4055 | vdXferTryLockDiskDeferIoTask(pIoTask);
|
---|
4056 | return VINF_SUCCESS;
|
---|
4057 | }
|
---|
4058 |
|
---|
4059 | /**
|
---|
4060 | * VD I/O interface callback for opening a file.
|
---|
4061 | */
|
---|
4062 | static int vdIOIntOpen(void *pvUser, const char *pszLocation,
|
---|
4063 | unsigned uOpenFlags, PPVDIOSTORAGE ppIoStorage)
|
---|
4064 | {
|
---|
4065 | int rc = VINF_SUCCESS;
|
---|
4066 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4067 | PVDIOSTORAGE pIoStorage = (PVDIOSTORAGE)RTMemAllocZ(sizeof(VDIOSTORAGE));
|
---|
4068 |
|
---|
4069 | if (!pIoStorage)
|
---|
4070 | return VERR_NO_MEMORY;
|
---|
4071 |
|
---|
4072 | /* Create the AVl tree. */
|
---|
4073 | pIoStorage->pTreeMetaXfers = (PAVLRFOFFTREE)RTMemAllocZ(sizeof(AVLRFOFFTREE));
|
---|
4074 | if (pIoStorage->pTreeMetaXfers)
|
---|
4075 | {
|
---|
4076 | rc = pVDIo->pInterfaceIo->pfnOpen(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4077 | pszLocation, uOpenFlags,
|
---|
4078 | vdIOIntReqCompleted,
|
---|
4079 | &pIoStorage->pStorage);
|
---|
4080 | if (RT_SUCCESS(rc))
|
---|
4081 | {
|
---|
4082 | pIoStorage->pVDIo = pVDIo;
|
---|
4083 | *ppIoStorage = pIoStorage;
|
---|
4084 | return VINF_SUCCESS;
|
---|
4085 | }
|
---|
4086 |
|
---|
4087 | RTMemFree(pIoStorage->pTreeMetaXfers);
|
---|
4088 | }
|
---|
4089 | else
|
---|
4090 | rc = VERR_NO_MEMORY;
|
---|
4091 |
|
---|
4092 | RTMemFree(pIoStorage);
|
---|
4093 | return rc;
|
---|
4094 | }
|
---|
4095 |
|
---|
4096 | static int vdIOIntTreeMetaXferDestroy(PAVLRFOFFNODECORE pNode, void *pvUser)
|
---|
4097 | {
|
---|
4098 | AssertMsgFailed(("Tree should be empty at this point!\n"));
|
---|
4099 | return VINF_SUCCESS;
|
---|
4100 | }
|
---|
4101 |
|
---|
4102 | static int vdIOIntClose(void *pvUser, PVDIOSTORAGE pIoStorage)
|
---|
4103 | {
|
---|
4104 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4105 |
|
---|
4106 | int rc = pVDIo->pInterfaceIo->pfnClose(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4107 | pIoStorage->pStorage);
|
---|
4108 | AssertRC(rc);
|
---|
4109 |
|
---|
4110 | RTAvlrFileOffsetDestroy(pIoStorage->pTreeMetaXfers, vdIOIntTreeMetaXferDestroy, NULL);
|
---|
4111 | RTMemFree(pIoStorage->pTreeMetaXfers);
|
---|
4112 | RTMemFree(pIoStorage);
|
---|
4113 | return VINF_SUCCESS;
|
---|
4114 | }
|
---|
4115 |
|
---|
4116 | static int vdIOIntDelete(void *pvUser, const char *pcszFilename)
|
---|
4117 | {
|
---|
4118 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4119 | return pVDIo->pInterfaceIo->pfnDelete(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4120 | pcszFilename);
|
---|
4121 | }
|
---|
4122 |
|
---|
4123 | static int vdIOIntMove(void *pvUser, const char *pcszSrc, const char *pcszDst,
|
---|
4124 | unsigned fMove)
|
---|
4125 | {
|
---|
4126 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4127 | return pVDIo->pInterfaceIo->pfnMove(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4128 | pcszSrc, pcszDst, fMove);
|
---|
4129 | }
|
---|
4130 |
|
---|
4131 | static int vdIOIntGetFreeSpace(void *pvUser, const char *pcszFilename,
|
---|
4132 | int64_t *pcbFreeSpace)
|
---|
4133 | {
|
---|
4134 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4135 | return pVDIo->pInterfaceIo->pfnGetFreeSpace(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4136 | pcszFilename, pcbFreeSpace);
|
---|
4137 | }
|
---|
4138 |
|
---|
4139 | static int vdIOIntGetModificationTime(void *pvUser, const char *pcszFilename,
|
---|
4140 | PRTTIMESPEC pModificationTime)
|
---|
4141 | {
|
---|
4142 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4143 | return pVDIo->pInterfaceIo->pfnGetModificationTime(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4144 | pcszFilename, pModificationTime);
|
---|
4145 | }
|
---|
4146 |
|
---|
4147 | static int vdIOIntGetSize(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
4148 | uint64_t *pcbSize)
|
---|
4149 | {
|
---|
4150 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4151 | return pVDIo->pInterfaceIo->pfnGetSize(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4152 | pIoStorage->pStorage, pcbSize);
|
---|
4153 | }
|
---|
4154 |
|
---|
4155 | static int vdIOIntSetSize(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
4156 | uint64_t cbSize)
|
---|
4157 | {
|
---|
4158 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4159 | return pVDIo->pInterfaceIo->pfnSetSize(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4160 | pIoStorage->pStorage, cbSize);
|
---|
4161 | }
|
---|
4162 |
|
---|
4163 | static int vdIOIntReadUser(void *pvUser, PVDIOSTORAGE pIoStorage, uint64_t uOffset,
|
---|
4164 | PVDIOCTX pIoCtx, size_t cbRead)
|
---|
4165 | {
|
---|
4166 | int rc = VINF_SUCCESS;
|
---|
4167 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4168 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
4169 |
|
---|
4170 | LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pIoCtx=%#p cbRead=%u\n",
|
---|
4171 | pvUser, pIoStorage, uOffset, pIoCtx, cbRead));
|
---|
4172 |
|
---|
4173 | /** @todo: Enable check for sync I/O later. */
|
---|
4174 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
4175 | VD_IS_LOCKED(pDisk);
|
---|
4176 |
|
---|
4177 | Assert(cbRead > 0);
|
---|
4178 |
|
---|
4179 | if (pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC)
|
---|
4180 | {
|
---|
4181 | RTSGSEG Seg;
|
---|
4182 | unsigned cSegments = 1;
|
---|
4183 | size_t cbTaskRead = 0;
|
---|
4184 |
|
---|
4185 | /* Synchronous I/O contexts only have one buffer segment. */
|
---|
4186 | AssertMsgReturn(pIoCtx->Req.Io.SgBuf.cSegs == 1,
|
---|
4187 | ("Invalid number of buffer segments for synchronous I/O context"),
|
---|
4188 | VERR_INVALID_PARAMETER);
|
---|
4189 |
|
---|
4190 | cbTaskRead = RTSgBufSegArrayCreate(&pIoCtx->Req.Io.SgBuf, &Seg, &cSegments, cbRead);
|
---|
4191 | Assert(cbRead == cbTaskRead);
|
---|
4192 | Assert(cSegments == 1);
|
---|
4193 | rc = pVDIo->pInterfaceIo->pfnReadSync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4194 | pIoStorage->pStorage, uOffset,
|
---|
4195 | Seg.pvSeg, cbRead, NULL);
|
---|
4196 | }
|
---|
4197 | else
|
---|
4198 | {
|
---|
4199 | /* Build the S/G array and spawn a new I/O task */
|
---|
4200 | while (cbRead)
|
---|
4201 | {
|
---|
4202 | RTSGSEG aSeg[VD_IO_TASK_SEGMENTS_MAX];
|
---|
4203 | unsigned cSegments = VD_IO_TASK_SEGMENTS_MAX;
|
---|
4204 | size_t cbTaskRead = 0;
|
---|
4205 |
|
---|
4206 | cbTaskRead = RTSgBufSegArrayCreate(&pIoCtx->Req.Io.SgBuf, aSeg, &cSegments, cbRead);
|
---|
4207 |
|
---|
4208 | Assert(cSegments > 0);
|
---|
4209 | Assert(cbTaskRead > 0);
|
---|
4210 | AssertMsg(cbTaskRead <= cbRead, ("Invalid number of bytes to read\n"));
|
---|
4211 |
|
---|
4212 | LogFlow(("Reading %u bytes into %u segments\n", cbTaskRead, cSegments));
|
---|
4213 |
|
---|
4214 | #ifdef RT_STRICT
|
---|
4215 | for (unsigned i = 0; i < cSegments; i++)
|
---|
4216 | AssertMsg(aSeg[i].pvSeg && !(aSeg[i].cbSeg % 512),
|
---|
4217 | ("Segment %u is invalid\n", i));
|
---|
4218 | #endif
|
---|
4219 |
|
---|
4220 | PVDIOTASK pIoTask = vdIoTaskUserAlloc(pIoStorage, NULL, NULL, pIoCtx, cbTaskRead);
|
---|
4221 |
|
---|
4222 | if (!pIoTask)
|
---|
4223 | return VERR_NO_MEMORY;
|
---|
4224 |
|
---|
4225 | ASMAtomicIncU32(&pIoCtx->cDataTransfersPending);
|
---|
4226 |
|
---|
4227 | void *pvTask;
|
---|
4228 | rc = pVDIo->pInterfaceIo->pfnReadAsync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4229 | pIoStorage->pStorage, uOffset,
|
---|
4230 | aSeg, cSegments, cbTaskRead, pIoTask,
|
---|
4231 | &pvTask);
|
---|
4232 | if (RT_SUCCESS(rc))
|
---|
4233 | {
|
---|
4234 | AssertMsg(cbTaskRead <= pIoCtx->Req.Io.cbTransferLeft, ("Impossible!\n"));
|
---|
4235 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, cbTaskRead);
|
---|
4236 | ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
|
---|
4237 | vdIoTaskFree(pDisk, pIoTask);
|
---|
4238 | }
|
---|
4239 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
4240 | {
|
---|
4241 | ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
|
---|
4242 | vdIoTaskFree(pDisk, pIoTask);
|
---|
4243 | break;
|
---|
4244 | }
|
---|
4245 |
|
---|
4246 | uOffset += cbTaskRead;
|
---|
4247 | cbRead -= cbTaskRead;
|
---|
4248 | }
|
---|
4249 | }
|
---|
4250 |
|
---|
4251 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
4252 | return rc;
|
---|
4253 | }
|
---|
4254 |
|
---|
4255 | static int vdIOIntWriteUser(void *pvUser, PVDIOSTORAGE pIoStorage, uint64_t uOffset,
|
---|
4256 | PVDIOCTX pIoCtx, size_t cbWrite, PFNVDXFERCOMPLETED pfnComplete,
|
---|
4257 | void *pvCompleteUser)
|
---|
4258 | {
|
---|
4259 | int rc = VINF_SUCCESS;
|
---|
4260 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4261 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
4262 |
|
---|
4263 | LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pIoCtx=%#p cbWrite=%u\n",
|
---|
4264 | pvUser, pIoStorage, uOffset, pIoCtx, cbWrite));
|
---|
4265 |
|
---|
4266 | /** @todo: Enable check for sync I/O later. */
|
---|
4267 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
4268 | VD_IS_LOCKED(pDisk);
|
---|
4269 |
|
---|
4270 | Assert(cbWrite > 0);
|
---|
4271 |
|
---|
4272 | if (pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC)
|
---|
4273 | {
|
---|
4274 | RTSGSEG Seg;
|
---|
4275 | unsigned cSegments = 1;
|
---|
4276 | size_t cbTaskWrite = 0;
|
---|
4277 |
|
---|
4278 | /* Synchronous I/O contexts only have one buffer segment. */
|
---|
4279 | AssertMsgReturn(pIoCtx->Req.Io.SgBuf.cSegs == 1,
|
---|
4280 | ("Invalid number of buffer segments for synchronous I/O context"),
|
---|
4281 | VERR_INVALID_PARAMETER);
|
---|
4282 |
|
---|
4283 | cbTaskWrite = RTSgBufSegArrayCreate(&pIoCtx->Req.Io.SgBuf, &Seg, &cSegments, cbWrite);
|
---|
4284 | Assert(cbWrite == cbTaskWrite);
|
---|
4285 | Assert(cSegments == 1);
|
---|
4286 | rc = pVDIo->pInterfaceIo->pfnWriteSync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4287 | pIoStorage->pStorage, uOffset,
|
---|
4288 | Seg.pvSeg, cbWrite, NULL);
|
---|
4289 | }
|
---|
4290 | else
|
---|
4291 | {
|
---|
4292 | /* Build the S/G array and spawn a new I/O task */
|
---|
4293 | while (cbWrite)
|
---|
4294 | {
|
---|
4295 | RTSGSEG aSeg[VD_IO_TASK_SEGMENTS_MAX];
|
---|
4296 | unsigned cSegments = VD_IO_TASK_SEGMENTS_MAX;
|
---|
4297 | size_t cbTaskWrite = 0;
|
---|
4298 |
|
---|
4299 | cbTaskWrite = RTSgBufSegArrayCreate(&pIoCtx->Req.Io.SgBuf, aSeg, &cSegments, cbWrite);
|
---|
4300 |
|
---|
4301 | Assert(cSegments > 0);
|
---|
4302 | Assert(cbTaskWrite > 0);
|
---|
4303 | AssertMsg(cbTaskWrite <= cbWrite, ("Invalid number of bytes to write\n"));
|
---|
4304 |
|
---|
4305 | LogFlow(("Writing %u bytes from %u segments\n", cbTaskWrite, cSegments));
|
---|
4306 |
|
---|
4307 | #ifdef DEBUG
|
---|
4308 | for (unsigned i = 0; i < cSegments; i++)
|
---|
4309 | AssertMsg(aSeg[i].pvSeg && !(aSeg[i].cbSeg % 512),
|
---|
4310 | ("Segment %u is invalid\n", i));
|
---|
4311 | #endif
|
---|
4312 |
|
---|
4313 | PVDIOTASK pIoTask = vdIoTaskUserAlloc(pIoStorage, pfnComplete, pvCompleteUser, pIoCtx, cbTaskWrite);
|
---|
4314 |
|
---|
4315 | if (!pIoTask)
|
---|
4316 | return VERR_NO_MEMORY;
|
---|
4317 |
|
---|
4318 | ASMAtomicIncU32(&pIoCtx->cDataTransfersPending);
|
---|
4319 |
|
---|
4320 | void *pvTask;
|
---|
4321 | rc = pVDIo->pInterfaceIo->pfnWriteAsync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4322 | pIoStorage->pStorage,
|
---|
4323 | uOffset, aSeg, cSegments,
|
---|
4324 | cbTaskWrite, pIoTask, &pvTask);
|
---|
4325 | if (RT_SUCCESS(rc))
|
---|
4326 | {
|
---|
4327 | AssertMsg(cbTaskWrite <= pIoCtx->Req.Io.cbTransferLeft, ("Impossible!\n"));
|
---|
4328 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, cbTaskWrite);
|
---|
4329 | ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
|
---|
4330 | vdIoTaskFree(pDisk, pIoTask);
|
---|
4331 | }
|
---|
4332 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
4333 | {
|
---|
4334 | ASMAtomicDecU32(&pIoCtx->cDataTransfersPending);
|
---|
4335 | vdIoTaskFree(pDisk, pIoTask);
|
---|
4336 | break;
|
---|
4337 | }
|
---|
4338 |
|
---|
4339 | uOffset += cbTaskWrite;
|
---|
4340 | cbWrite -= cbTaskWrite;
|
---|
4341 | }
|
---|
4342 | }
|
---|
4343 |
|
---|
4344 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
4345 | return rc;
|
---|
4346 | }
|
---|
4347 |
|
---|
4348 | static int vdIOIntReadMeta(void *pvUser, PVDIOSTORAGE pIoStorage, uint64_t uOffset,
|
---|
4349 | void *pvBuf, size_t cbRead, PVDIOCTX pIoCtx,
|
---|
4350 | PPVDMETAXFER ppMetaXfer, PFNVDXFERCOMPLETED pfnComplete,
|
---|
4351 | void *pvCompleteUser)
|
---|
4352 | {
|
---|
4353 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4354 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
4355 | int rc = VINF_SUCCESS;
|
---|
4356 | RTSGSEG Seg;
|
---|
4357 | PVDIOTASK pIoTask;
|
---|
4358 | PVDMETAXFER pMetaXfer = NULL;
|
---|
4359 | void *pvTask = NULL;
|
---|
4360 |
|
---|
4361 | LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pvBuf=%#p cbRead=%u\n",
|
---|
4362 | pvUser, pIoStorage, uOffset, pvBuf, cbRead));
|
---|
4363 |
|
---|
4364 | AssertMsgReturn( pIoCtx
|
---|
4365 | || (!ppMetaXfer && !pfnComplete && !pvCompleteUser),
|
---|
4366 | ("A synchronous metadata read is requested but the parameters are wrong\n"),
|
---|
4367 | VERR_INVALID_POINTER);
|
---|
4368 |
|
---|
4369 | /** @todo: Enable check for sync I/O later. */
|
---|
4370 | if ( pIoCtx
|
---|
4371 | && !(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
4372 | VD_IS_LOCKED(pDisk);
|
---|
4373 |
|
---|
4374 | if ( !pIoCtx
|
---|
4375 | || pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC)
|
---|
4376 | {
|
---|
4377 | /* Handle synchronous metadata I/O. */
|
---|
4378 | /** @todo: Integrate with metadata transfers below. */
|
---|
4379 | rc = pVDIo->pInterfaceIo->pfnReadSync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4380 | pIoStorage->pStorage, uOffset,
|
---|
4381 | pvBuf, cbRead, NULL);
|
---|
4382 | if (ppMetaXfer)
|
---|
4383 | *ppMetaXfer = NULL;
|
---|
4384 | }
|
---|
4385 | else
|
---|
4386 | {
|
---|
4387 | pMetaXfer = (PVDMETAXFER)RTAvlrFileOffsetGet(pIoStorage->pTreeMetaXfers, uOffset);
|
---|
4388 | if (!pMetaXfer)
|
---|
4389 | {
|
---|
4390 | #ifdef RT_STRICT
|
---|
4391 | pMetaXfer = (PVDMETAXFER)RTAvlrFileOffsetGetBestFit(pIoStorage->pTreeMetaXfers, uOffset, false /* fAbove */);
|
---|
4392 | AssertMsg(!pMetaXfer || (pMetaXfer->Core.Key + (RTFOFF)pMetaXfer->cbMeta <= (RTFOFF)uOffset),
|
---|
4393 | ("Overlapping meta transfers!\n"));
|
---|
4394 | #endif
|
---|
4395 |
|
---|
4396 | /* Allocate a new meta transfer. */
|
---|
4397 | pMetaXfer = vdMetaXferAlloc(pIoStorage, uOffset, cbRead);
|
---|
4398 | if (!pMetaXfer)
|
---|
4399 | return VERR_NO_MEMORY;
|
---|
4400 |
|
---|
4401 | pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvCompleteUser, pMetaXfer);
|
---|
4402 | if (!pIoTask)
|
---|
4403 | {
|
---|
4404 | RTMemFree(pMetaXfer);
|
---|
4405 | return VERR_NO_MEMORY;
|
---|
4406 | }
|
---|
4407 |
|
---|
4408 | Seg.cbSeg = cbRead;
|
---|
4409 | Seg.pvSeg = pMetaXfer->abData;
|
---|
4410 |
|
---|
4411 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_READ);
|
---|
4412 | rc = pVDIo->pInterfaceIo->pfnReadAsync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4413 | pIoStorage->pStorage,
|
---|
4414 | uOffset, &Seg, 1,
|
---|
4415 | cbRead, pIoTask, &pvTask);
|
---|
4416 |
|
---|
4417 | if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
4418 | {
|
---|
4419 | bool fInserted = RTAvlrFileOffsetInsert(pIoStorage->pTreeMetaXfers, &pMetaXfer->Core);
|
---|
4420 | Assert(fInserted);
|
---|
4421 | }
|
---|
4422 | else
|
---|
4423 | RTMemFree(pMetaXfer);
|
---|
4424 |
|
---|
4425 | if (RT_SUCCESS(rc))
|
---|
4426 | {
|
---|
4427 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
|
---|
4428 | vdIoTaskFree(pDisk, pIoTask);
|
---|
4429 | }
|
---|
4430 | else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS && !pfnComplete)
|
---|
4431 | rc = VERR_VD_NOT_ENOUGH_METADATA;
|
---|
4432 | }
|
---|
4433 |
|
---|
4434 | Assert(VALID_PTR(pMetaXfer) || RT_FAILURE(rc));
|
---|
4435 |
|
---|
4436 | if (RT_SUCCESS(rc) || rc == VERR_VD_NOT_ENOUGH_METADATA || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
4437 | {
|
---|
4438 | /* If it is pending add the request to the list. */
|
---|
4439 | if (VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_READ)
|
---|
4440 | {
|
---|
4441 | PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
|
---|
4442 | AssertPtr(pDeferred);
|
---|
4443 |
|
---|
4444 | RTListInit(&pDeferred->NodeDeferred);
|
---|
4445 | pDeferred->pIoCtx = pIoCtx;
|
---|
4446 |
|
---|
4447 | ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
|
---|
4448 | RTListAppend(&pMetaXfer->ListIoCtxWaiting, &pDeferred->NodeDeferred);
|
---|
4449 | rc = VERR_VD_NOT_ENOUGH_METADATA;
|
---|
4450 | }
|
---|
4451 | else
|
---|
4452 | {
|
---|
4453 | /* Transfer the data. */
|
---|
4454 | pMetaXfer->cRefs++;
|
---|
4455 | Assert(pMetaXfer->cbMeta >= cbRead);
|
---|
4456 | Assert(pMetaXfer->Core.Key == (RTFOFF)uOffset);
|
---|
4457 | memcpy(pvBuf, pMetaXfer->abData, cbRead);
|
---|
4458 | *ppMetaXfer = pMetaXfer;
|
---|
4459 | }
|
---|
4460 | }
|
---|
4461 | }
|
---|
4462 |
|
---|
4463 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
4464 | return rc;
|
---|
4465 | }
|
---|
4466 |
|
---|
4467 | static int vdIOIntWriteMeta(void *pvUser, PVDIOSTORAGE pIoStorage, uint64_t uOffset,
|
---|
4468 | const void *pvBuf, size_t cbWrite, PVDIOCTX pIoCtx,
|
---|
4469 | PFNVDXFERCOMPLETED pfnComplete, void *pvCompleteUser)
|
---|
4470 | {
|
---|
4471 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4472 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
4473 | int rc = VINF_SUCCESS;
|
---|
4474 | RTSGSEG Seg;
|
---|
4475 | PVDIOTASK pIoTask;
|
---|
4476 | PVDMETAXFER pMetaXfer = NULL;
|
---|
4477 | bool fInTree = false;
|
---|
4478 | void *pvTask = NULL;
|
---|
4479 |
|
---|
4480 | LogFlowFunc(("pvUser=%#p pIoStorage=%#p uOffset=%llu pvBuf=%#p cbWrite=%u\n",
|
---|
4481 | pvUser, pIoStorage, uOffset, pvBuf, cbWrite));
|
---|
4482 |
|
---|
4483 | AssertMsgReturn( pIoCtx
|
---|
4484 | || (!pfnComplete && !pvCompleteUser),
|
---|
4485 | ("A synchronous metadata write is requested but the parameters are wrong\n"),
|
---|
4486 | VERR_INVALID_POINTER);
|
---|
4487 |
|
---|
4488 | /** @todo: Enable check for sync I/O later. */
|
---|
4489 | if ( pIoCtx
|
---|
4490 | && !(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
4491 | VD_IS_LOCKED(pDisk);
|
---|
4492 |
|
---|
4493 | if ( !pIoCtx
|
---|
4494 | || pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC)
|
---|
4495 | {
|
---|
4496 | /* Handle synchronous metadata I/O. */
|
---|
4497 | /** @todo: Integrate with metadata transfers below. */
|
---|
4498 | rc = pVDIo->pInterfaceIo->pfnWriteSync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4499 | pIoStorage->pStorage, uOffset,
|
---|
4500 | pvBuf, cbWrite, NULL);
|
---|
4501 | }
|
---|
4502 | else
|
---|
4503 | {
|
---|
4504 | pMetaXfer = (PVDMETAXFER)RTAvlrFileOffsetGet(pIoStorage->pTreeMetaXfers, uOffset);
|
---|
4505 | if (!pMetaXfer)
|
---|
4506 | {
|
---|
4507 | /* Allocate a new meta transfer. */
|
---|
4508 | pMetaXfer = vdMetaXferAlloc(pIoStorage, uOffset, cbWrite);
|
---|
4509 | if (!pMetaXfer)
|
---|
4510 | return VERR_NO_MEMORY;
|
---|
4511 | }
|
---|
4512 | else
|
---|
4513 | {
|
---|
4514 | Assert(pMetaXfer->cbMeta >= cbWrite);
|
---|
4515 | Assert(pMetaXfer->Core.Key == (RTFOFF)uOffset);
|
---|
4516 | fInTree = true;
|
---|
4517 | }
|
---|
4518 |
|
---|
4519 | Assert(VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE);
|
---|
4520 |
|
---|
4521 | pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvCompleteUser, pMetaXfer);
|
---|
4522 | if (!pIoTask)
|
---|
4523 | {
|
---|
4524 | RTMemFree(pMetaXfer);
|
---|
4525 | return VERR_NO_MEMORY;
|
---|
4526 | }
|
---|
4527 |
|
---|
4528 | memcpy(pMetaXfer->abData, pvBuf, cbWrite);
|
---|
4529 | Seg.cbSeg = cbWrite;
|
---|
4530 | Seg.pvSeg = pMetaXfer->abData;
|
---|
4531 |
|
---|
4532 | ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
|
---|
4533 |
|
---|
4534 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_WRITE);
|
---|
4535 | rc = pVDIo->pInterfaceIo->pfnWriteAsync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4536 | pIoStorage->pStorage,
|
---|
4537 | uOffset, &Seg, 1, cbWrite, pIoTask,
|
---|
4538 | &pvTask);
|
---|
4539 | if (RT_SUCCESS(rc))
|
---|
4540 | {
|
---|
4541 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
|
---|
4542 | ASMAtomicDecU32(&pIoCtx->cMetaTransfersPending);
|
---|
4543 | vdIoTaskFree(pDisk, pIoTask);
|
---|
4544 | if (fInTree && !pMetaXfer->cRefs)
|
---|
4545 | {
|
---|
4546 | LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
|
---|
4547 | bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
|
---|
4548 | AssertMsg(fRemoved, ("Metadata transfer wasn't removed\n"));
|
---|
4549 | RTMemFree(pMetaXfer);
|
---|
4550 | pMetaXfer = NULL;
|
---|
4551 | }
|
---|
4552 | }
|
---|
4553 | else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
4554 | {
|
---|
4555 | PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
|
---|
4556 | AssertPtr(pDeferred);
|
---|
4557 |
|
---|
4558 | RTListInit(&pDeferred->NodeDeferred);
|
---|
4559 | pDeferred->pIoCtx = pIoCtx;
|
---|
4560 |
|
---|
4561 | if (!fInTree)
|
---|
4562 | {
|
---|
4563 | bool fInserted = RTAvlrFileOffsetInsert(pIoStorage->pTreeMetaXfers, &pMetaXfer->Core);
|
---|
4564 | Assert(fInserted);
|
---|
4565 | }
|
---|
4566 |
|
---|
4567 | RTListAppend(&pMetaXfer->ListIoCtxWaiting, &pDeferred->NodeDeferred);
|
---|
4568 | }
|
---|
4569 | else
|
---|
4570 | {
|
---|
4571 | RTMemFree(pMetaXfer);
|
---|
4572 | pMetaXfer = NULL;
|
---|
4573 | }
|
---|
4574 | }
|
---|
4575 |
|
---|
4576 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
4577 | return rc;
|
---|
4578 | }
|
---|
4579 |
|
---|
4580 | static void vdIOIntMetaXferRelease(void *pvUser, PVDMETAXFER pMetaXfer)
|
---|
4581 | {
|
---|
4582 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4583 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
4584 | PVDIOSTORAGE pIoStorage;
|
---|
4585 |
|
---|
4586 | /*
|
---|
4587 | * It is possible that we get called with a NULL metadata xfer handle
|
---|
4588 | * for synchronous I/O. Just exit.
|
---|
4589 | */
|
---|
4590 | if (!pMetaXfer)
|
---|
4591 | return;
|
---|
4592 |
|
---|
4593 | pIoStorage = pMetaXfer->pIoStorage;
|
---|
4594 |
|
---|
4595 | VD_IS_LOCKED(pDisk);
|
---|
4596 |
|
---|
4597 | Assert( VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE
|
---|
4598 | || VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_WRITE);
|
---|
4599 | Assert(pMetaXfer->cRefs > 0);
|
---|
4600 |
|
---|
4601 | pMetaXfer->cRefs--;
|
---|
4602 | if ( !pMetaXfer->cRefs
|
---|
4603 | && RTListIsEmpty(&pMetaXfer->ListIoCtxWaiting)
|
---|
4604 | && VDMETAXFER_TXDIR_GET(pMetaXfer->fFlags) == VDMETAXFER_TXDIR_NONE)
|
---|
4605 | {
|
---|
4606 | /* Free the meta data entry. */
|
---|
4607 | LogFlow(("Removing meta xfer=%#p\n", pMetaXfer));
|
---|
4608 | bool fRemoved = RTAvlrFileOffsetRemove(pIoStorage->pTreeMetaXfers, pMetaXfer->Core.Key) != NULL;
|
---|
4609 | AssertMsg(fRemoved, ("Metadata transfer wasn't removed\n"));
|
---|
4610 |
|
---|
4611 | RTMemFree(pMetaXfer);
|
---|
4612 | }
|
---|
4613 | }
|
---|
4614 |
|
---|
4615 | static int vdIOIntFlush(void *pvUser, PVDIOSTORAGE pIoStorage, PVDIOCTX pIoCtx,
|
---|
4616 | PFNVDXFERCOMPLETED pfnComplete, void *pvCompleteUser)
|
---|
4617 | {
|
---|
4618 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4619 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
4620 | int rc = VINF_SUCCESS;
|
---|
4621 | PVDIOTASK pIoTask;
|
---|
4622 | PVDMETAXFER pMetaXfer = NULL;
|
---|
4623 | void *pvTask = NULL;
|
---|
4624 |
|
---|
4625 | LogFlowFunc(("pvUser=%#p pIoStorage=%#p pIoCtx=%#p\n",
|
---|
4626 | pvUser, pIoStorage, pIoCtx));
|
---|
4627 |
|
---|
4628 | AssertMsgReturn( pIoCtx
|
---|
4629 | || (!pfnComplete && !pvCompleteUser),
|
---|
4630 | ("A synchronous metadata write is requested but the parameters are wrong\n"),
|
---|
4631 | VERR_INVALID_POINTER);
|
---|
4632 |
|
---|
4633 | /** @todo: Enable check for sync I/O later. */
|
---|
4634 | if ( pIoCtx
|
---|
4635 | && !(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
4636 | VD_IS_LOCKED(pDisk);
|
---|
4637 |
|
---|
4638 | if (pVDIo->fIgnoreFlush)
|
---|
4639 | return VINF_SUCCESS;
|
---|
4640 |
|
---|
4641 | if ( !pIoCtx
|
---|
4642 | || pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC)
|
---|
4643 | {
|
---|
4644 | /* Handle synchronous flushes. */
|
---|
4645 | /** @todo: Integrate with metadata transfers below. */
|
---|
4646 | rc = pVDIo->pInterfaceIo->pfnFlushSync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4647 | pIoStorage->pStorage);
|
---|
4648 | }
|
---|
4649 | else
|
---|
4650 | {
|
---|
4651 | /* Allocate a new meta transfer. */
|
---|
4652 | pMetaXfer = vdMetaXferAlloc(pIoStorage, 0, 0);
|
---|
4653 | if (!pMetaXfer)
|
---|
4654 | return VERR_NO_MEMORY;
|
---|
4655 |
|
---|
4656 | pIoTask = vdIoTaskMetaAlloc(pIoStorage, pfnComplete, pvUser, pMetaXfer);
|
---|
4657 | if (!pIoTask)
|
---|
4658 | {
|
---|
4659 | RTMemFree(pMetaXfer);
|
---|
4660 | return VERR_NO_MEMORY;
|
---|
4661 | }
|
---|
4662 |
|
---|
4663 | ASMAtomicIncU32(&pIoCtx->cMetaTransfersPending);
|
---|
4664 |
|
---|
4665 | PVDIOCTXDEFERRED pDeferred = (PVDIOCTXDEFERRED)RTMemAllocZ(sizeof(VDIOCTXDEFERRED));
|
---|
4666 | AssertPtr(pDeferred);
|
---|
4667 |
|
---|
4668 | RTListInit(&pDeferred->NodeDeferred);
|
---|
4669 | pDeferred->pIoCtx = pIoCtx;
|
---|
4670 |
|
---|
4671 | RTListAppend(&pMetaXfer->ListIoCtxWaiting, &pDeferred->NodeDeferred);
|
---|
4672 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_FLUSH);
|
---|
4673 | rc = pVDIo->pInterfaceIo->pfnFlushAsync(pVDIo->pInterfaceIo->Core.pvUser,
|
---|
4674 | pIoStorage->pStorage,
|
---|
4675 | pIoTask, &pvTask);
|
---|
4676 | if (RT_SUCCESS(rc))
|
---|
4677 | {
|
---|
4678 | VDMETAXFER_TXDIR_SET(pMetaXfer->fFlags, VDMETAXFER_TXDIR_NONE);
|
---|
4679 | ASMAtomicDecU32(&pIoCtx->cMetaTransfersPending);
|
---|
4680 | vdIoTaskFree(pDisk, pIoTask);
|
---|
4681 | RTMemFree(pDeferred);
|
---|
4682 | RTMemFree(pMetaXfer);
|
---|
4683 | }
|
---|
4684 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
4685 | RTMemFree(pMetaXfer);
|
---|
4686 | }
|
---|
4687 |
|
---|
4688 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
4689 | return rc;
|
---|
4690 | }
|
---|
4691 |
|
---|
4692 | static size_t vdIOIntIoCtxCopyTo(void *pvUser, PVDIOCTX pIoCtx,
|
---|
4693 | const void *pvBuf, size_t cbBuf)
|
---|
4694 | {
|
---|
4695 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4696 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
4697 | size_t cbCopied = 0;
|
---|
4698 |
|
---|
4699 | /** @todo: Enable check for sync I/O later. */
|
---|
4700 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
4701 | VD_IS_LOCKED(pDisk);
|
---|
4702 |
|
---|
4703 | cbCopied = vdIoCtxCopyTo(pIoCtx, (uint8_t *)pvBuf, cbBuf);
|
---|
4704 | Assert(cbCopied == cbBuf);
|
---|
4705 |
|
---|
4706 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, cbCopied);
|
---|
4707 |
|
---|
4708 | return cbCopied;
|
---|
4709 | }
|
---|
4710 |
|
---|
4711 | static size_t vdIOIntIoCtxCopyFrom(void *pvUser, PVDIOCTX pIoCtx,
|
---|
4712 | void *pvBuf, size_t cbBuf)
|
---|
4713 | {
|
---|
4714 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4715 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
4716 | size_t cbCopied = 0;
|
---|
4717 |
|
---|
4718 | /** @todo: Enable check for sync I/O later. */
|
---|
4719 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
4720 | VD_IS_LOCKED(pDisk);
|
---|
4721 |
|
---|
4722 | cbCopied = vdIoCtxCopyFrom(pIoCtx, (uint8_t *)pvBuf, cbBuf);
|
---|
4723 | Assert(cbCopied == cbBuf);
|
---|
4724 |
|
---|
4725 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, cbCopied);
|
---|
4726 |
|
---|
4727 | return cbCopied;
|
---|
4728 | }
|
---|
4729 |
|
---|
4730 | static size_t vdIOIntIoCtxSet(void *pvUser, PVDIOCTX pIoCtx, int ch, size_t cb)
|
---|
4731 | {
|
---|
4732 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4733 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
4734 | size_t cbSet = 0;
|
---|
4735 |
|
---|
4736 | /** @todo: Enable check for sync I/O later. */
|
---|
4737 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
4738 | VD_IS_LOCKED(pDisk);
|
---|
4739 |
|
---|
4740 | cbSet = vdIoCtxSet(pIoCtx, ch, cb);
|
---|
4741 | Assert(cbSet == cb);
|
---|
4742 |
|
---|
4743 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, cbSet);
|
---|
4744 |
|
---|
4745 | return cbSet;
|
---|
4746 | }
|
---|
4747 |
|
---|
4748 | static size_t vdIOIntIoCtxSegArrayCreate(void *pvUser, PVDIOCTX pIoCtx,
|
---|
4749 | PRTSGSEG paSeg, unsigned *pcSeg,
|
---|
4750 | size_t cbData)
|
---|
4751 | {
|
---|
4752 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4753 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
4754 | size_t cbCreated = 0;
|
---|
4755 |
|
---|
4756 | /** @todo: Enable check for sync I/O later. */
|
---|
4757 | if (!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC))
|
---|
4758 | VD_IS_LOCKED(pDisk);
|
---|
4759 |
|
---|
4760 | cbCreated = RTSgBufSegArrayCreate(&pIoCtx->Req.Io.SgBuf, paSeg, pcSeg, cbData);
|
---|
4761 | Assert(!paSeg || cbData == cbCreated);
|
---|
4762 |
|
---|
4763 | return cbCreated;
|
---|
4764 | }
|
---|
4765 |
|
---|
4766 | static void vdIOIntIoCtxCompleted(void *pvUser, PVDIOCTX pIoCtx, int rcReq,
|
---|
4767 | size_t cbCompleted)
|
---|
4768 | {
|
---|
4769 | PVDIO pVDIo = (PVDIO)pvUser;
|
---|
4770 | PVBOXHDD pDisk = pVDIo->pDisk;
|
---|
4771 |
|
---|
4772 | /*
|
---|
4773 | * Grab the disk critical section to avoid races with other threads which
|
---|
4774 | * might still modify the I/O context.
|
---|
4775 | * Example is that iSCSI is doing an asynchronous write but calls us already
|
---|
4776 | * while the other thread is still hanging in vdWriteHelperAsync and couldn't update
|
---|
4777 | * the blocked state yet.
|
---|
4778 | * It can overwrite the state to true before we call vdIoCtxContinue and the
|
---|
4779 | * the request would hang indefinite.
|
---|
4780 | */
|
---|
4781 | pIoCtx->rcReq = rcReq;
|
---|
4782 | ASMAtomicSubU32(&pIoCtx->Req.Io.cbTransferLeft, cbCompleted);
|
---|
4783 |
|
---|
4784 | /* Clear the pointer to next transfer function in case we have nothing to transfer anymore.
|
---|
4785 | * @todo: Find a better way to prevent vdIoCtxContinue from calling the read/write helper again. */
|
---|
4786 | if (!pIoCtx->Req.Io.cbTransferLeft)
|
---|
4787 | pIoCtx->pfnIoCtxTransfer = NULL;
|
---|
4788 |
|
---|
4789 | vdIoCtxAddToWaitingList(&pDisk->pIoCtxHaltedHead, pIoCtx);
|
---|
4790 | if (ASMAtomicCmpXchgBool(&pDisk->fLocked, true, false))
|
---|
4791 | {
|
---|
4792 | /* Immediately drop the lock again, it will take of processing the list. */
|
---|
4793 | vdDiskUnlock(pDisk, NULL);
|
---|
4794 | }
|
---|
4795 | }
|
---|
4796 |
|
---|
4797 | static DECLCALLBACK(bool) vdIOIntIoCtxIsSynchronous(void *pvUser, PVDIOCTX pIoCtx)
|
---|
4798 | {
|
---|
4799 | NOREF(pvUser);
|
---|
4800 | return !!(pIoCtx->fFlags & VDIOCTX_FLAGS_SYNC);
|
---|
4801 | }
|
---|
4802 |
|
---|
4803 | static DECLCALLBACK(bool) vdIOIntIoCtxIsZero(void *pvUser, PVDIOCTX pIoCtx, size_t cbCheck,
|
---|
4804 | bool fAdvance)
|
---|
4805 | {
|
---|
4806 | NOREF(pvUser);
|
---|
4807 |
|
---|
4808 | bool fIsZero = RTSgBufIsZero(&pIoCtx->Req.Io.SgBuf, cbCheck);
|
---|
4809 | if (fIsZero && fAdvance)
|
---|
4810 | RTSgBufAdvance(&pIoCtx->Req.Io.SgBuf, cbCheck);
|
---|
4811 |
|
---|
4812 | return fIsZero;
|
---|
4813 | }
|
---|
4814 |
|
---|
4815 | /**
|
---|
4816 | * VD I/O interface callback for opening a file (limited version for VDGetFormat).
|
---|
4817 | */
|
---|
4818 | static int vdIOIntOpenLimited(void *pvUser, const char *pszLocation,
|
---|
4819 | uint32_t fOpen, PPVDIOSTORAGE ppIoStorage)
|
---|
4820 | {
|
---|
4821 | int rc = VINF_SUCCESS;
|
---|
4822 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
4823 | PVDIOSTORAGE pIoStorage = (PVDIOSTORAGE)RTMemAllocZ(sizeof(VDIOSTORAGE));
|
---|
4824 |
|
---|
4825 | if (!pIoStorage)
|
---|
4826 | return VERR_NO_MEMORY;
|
---|
4827 |
|
---|
4828 | rc = pInterfaceIo->pfnOpen(NULL, pszLocation, fOpen, NULL, &pIoStorage->pStorage);
|
---|
4829 | if (RT_SUCCESS(rc))
|
---|
4830 | *ppIoStorage = pIoStorage;
|
---|
4831 | else
|
---|
4832 | RTMemFree(pIoStorage);
|
---|
4833 |
|
---|
4834 | return rc;
|
---|
4835 | }
|
---|
4836 |
|
---|
4837 | static int vdIOIntCloseLimited(void *pvUser, PVDIOSTORAGE pIoStorage)
|
---|
4838 | {
|
---|
4839 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
4840 | int rc = pInterfaceIo->pfnClose(NULL, pIoStorage->pStorage);
|
---|
4841 | AssertRC(rc);
|
---|
4842 |
|
---|
4843 | RTMemFree(pIoStorage);
|
---|
4844 | return VINF_SUCCESS;
|
---|
4845 | }
|
---|
4846 |
|
---|
4847 | static int vdIOIntDeleteLimited(void *pvUser, const char *pcszFilename)
|
---|
4848 | {
|
---|
4849 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
4850 | return pInterfaceIo->pfnDelete(NULL, pcszFilename);
|
---|
4851 | }
|
---|
4852 |
|
---|
4853 | static int vdIOIntMoveLimited(void *pvUser, const char *pcszSrc,
|
---|
4854 | const char *pcszDst, unsigned fMove)
|
---|
4855 | {
|
---|
4856 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
4857 | return pInterfaceIo->pfnMove(NULL, pcszSrc, pcszDst, fMove);
|
---|
4858 | }
|
---|
4859 |
|
---|
4860 | static int vdIOIntGetFreeSpaceLimited(void *pvUser, const char *pcszFilename,
|
---|
4861 | int64_t *pcbFreeSpace)
|
---|
4862 | {
|
---|
4863 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
4864 | return pInterfaceIo->pfnGetFreeSpace(NULL, pcszFilename, pcbFreeSpace);
|
---|
4865 | }
|
---|
4866 |
|
---|
4867 | static int vdIOIntGetModificationTimeLimited(void *pvUser,
|
---|
4868 | const char *pcszFilename,
|
---|
4869 | PRTTIMESPEC pModificationTime)
|
---|
4870 | {
|
---|
4871 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
4872 | return pInterfaceIo->pfnGetModificationTime(NULL, pcszFilename, pModificationTime);
|
---|
4873 | }
|
---|
4874 |
|
---|
4875 | static int vdIOIntGetSizeLimited(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
4876 | uint64_t *pcbSize)
|
---|
4877 | {
|
---|
4878 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
4879 | return pInterfaceIo->pfnGetSize(NULL, pIoStorage->pStorage, pcbSize);
|
---|
4880 | }
|
---|
4881 |
|
---|
4882 | static int vdIOIntSetSizeLimited(void *pvUser, PVDIOSTORAGE pIoStorage,
|
---|
4883 | uint64_t cbSize)
|
---|
4884 | {
|
---|
4885 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
4886 | return pInterfaceIo->pfnSetSize(NULL, pIoStorage->pStorage, cbSize);
|
---|
4887 | }
|
---|
4888 |
|
---|
4889 | static int vdIOIntWriteUserLimited(void *pvUser, PVDIOSTORAGE pStorage,
|
---|
4890 | uint64_t uOffset, PVDIOCTX pIoCtx,
|
---|
4891 | size_t cbWrite,
|
---|
4892 | PFNVDXFERCOMPLETED pfnComplete,
|
---|
4893 | void *pvCompleteUser)
|
---|
4894 | {
|
---|
4895 | NOREF(pvUser);
|
---|
4896 | NOREF(pStorage);
|
---|
4897 | NOREF(uOffset);
|
---|
4898 | NOREF(pIoCtx);
|
---|
4899 | NOREF(cbWrite);
|
---|
4900 | NOREF(pfnComplete);
|
---|
4901 | NOREF(pvCompleteUser);
|
---|
4902 | AssertMsgFailedReturn(("This needs to be implemented when called\n"), VERR_NOT_IMPLEMENTED);
|
---|
4903 | }
|
---|
4904 |
|
---|
4905 | static int vdIOIntReadUserLimited(void *pvUser, PVDIOSTORAGE pStorage,
|
---|
4906 | uint64_t uOffset, PVDIOCTX pIoCtx,
|
---|
4907 | size_t cbRead)
|
---|
4908 | {
|
---|
4909 | NOREF(pvUser);
|
---|
4910 | NOREF(pStorage);
|
---|
4911 | NOREF(uOffset);
|
---|
4912 | NOREF(pIoCtx);
|
---|
4913 | NOREF(cbRead);
|
---|
4914 | AssertMsgFailedReturn(("This needs to be implemented when called\n"), VERR_NOT_IMPLEMENTED);
|
---|
4915 | }
|
---|
4916 |
|
---|
4917 | static int vdIOIntWriteMetaLimited(void *pvUser, PVDIOSTORAGE pStorage,
|
---|
4918 | uint64_t uOffset, const void *pvBuffer,
|
---|
4919 | size_t cbBuffer, PVDIOCTX pIoCtx,
|
---|
4920 | PFNVDXFERCOMPLETED pfnComplete,
|
---|
4921 | void *pvCompleteUser)
|
---|
4922 | {
|
---|
4923 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
4924 |
|
---|
4925 | AssertMsgReturn(!pIoCtx && !pfnComplete && !pvCompleteUser,
|
---|
4926 | ("Async I/O not implemented for the limited interface"),
|
---|
4927 | VERR_NOT_SUPPORTED);
|
---|
4928 |
|
---|
4929 | return pInterfaceIo->pfnWriteSync(NULL, pStorage->pStorage, uOffset, pvBuffer, cbBuffer, NULL);
|
---|
4930 | }
|
---|
4931 |
|
---|
4932 | static int vdIOIntReadMetaLimited(void *pvUser, PVDIOSTORAGE pStorage,
|
---|
4933 | uint64_t uOffset, void *pvBuffer,
|
---|
4934 | size_t cbBuffer, PVDIOCTX pIoCtx,
|
---|
4935 | PPVDMETAXFER ppMetaXfer,
|
---|
4936 | PFNVDXFERCOMPLETED pfnComplete,
|
---|
4937 | void *pvCompleteUser)
|
---|
4938 | {
|
---|
4939 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
4940 |
|
---|
4941 | AssertMsgReturn(!pIoCtx && !ppMetaXfer && !pfnComplete && !pvCompleteUser,
|
---|
4942 | ("Async I/O not implemented for the limited interface"),
|
---|
4943 | VERR_NOT_SUPPORTED);
|
---|
4944 |
|
---|
4945 | return pInterfaceIo->pfnReadSync(NULL, pStorage->pStorage, uOffset, pvBuffer, cbBuffer, NULL);
|
---|
4946 | }
|
---|
4947 |
|
---|
4948 | static int vdIOIntMetaXferReleaseLimited(void *pvUser, PVDMETAXFER pMetaXfer)
|
---|
4949 | {
|
---|
4950 | /* This is a NOP in this case. */
|
---|
4951 | NOREF(pvUser);
|
---|
4952 | NOREF(pMetaXfer);
|
---|
4953 | return VINF_SUCCESS;
|
---|
4954 | }
|
---|
4955 |
|
---|
4956 | static int vdIOIntFlushLimited(void *pvUser, PVDIOSTORAGE pStorage,
|
---|
4957 | PVDIOCTX pIoCtx,
|
---|
4958 | PFNVDXFERCOMPLETED pfnComplete,
|
---|
4959 | void *pvCompleteUser)
|
---|
4960 | {
|
---|
4961 | PVDINTERFACEIO pInterfaceIo = (PVDINTERFACEIO)pvUser;
|
---|
4962 |
|
---|
4963 | AssertMsgReturn(!pIoCtx && !pfnComplete && !pvCompleteUser,
|
---|
4964 | ("Async I/O not implemented for the limited interface"),
|
---|
4965 | VERR_NOT_SUPPORTED);
|
---|
4966 |
|
---|
4967 | return pInterfaceIo->pfnFlushSync(NULL, pStorage->pStorage);
|
---|
4968 | }
|
---|
4969 |
|
---|
4970 | /**
|
---|
4971 | * internal: send output to the log (unconditionally).
|
---|
4972 | */
|
---|
4973 | int vdLogMessage(void *pvUser, const char *pszFormat, va_list args)
|
---|
4974 | {
|
---|
4975 | NOREF(pvUser);
|
---|
4976 | RTLogPrintfV(pszFormat, args);
|
---|
4977 | return VINF_SUCCESS;
|
---|
4978 | }
|
---|
4979 |
|
---|
4980 | DECLINLINE(int) vdMessageWrapper(PVBOXHDD pDisk, const char *pszFormat, ...)
|
---|
4981 | {
|
---|
4982 | va_list va;
|
---|
4983 | va_start(va, pszFormat);
|
---|
4984 | int rc = pDisk->pInterfaceError->pfnMessage(pDisk->pInterfaceError->Core.pvUser,
|
---|
4985 | pszFormat, va);
|
---|
4986 | va_end(va);
|
---|
4987 | return rc;
|
---|
4988 | }
|
---|
4989 |
|
---|
4990 |
|
---|
4991 | /**
|
---|
4992 | * internal: adjust PCHS geometry
|
---|
4993 | */
|
---|
4994 | static void vdFixupPCHSGeometry(PVDGEOMETRY pPCHS, uint64_t cbSize)
|
---|
4995 | {
|
---|
4996 | /* Fix broken PCHS geometry. Can happen for two reasons: either the backend
|
---|
4997 | * mixes up PCHS and LCHS, or the application used to create the source
|
---|
4998 | * image has put garbage in it. Additionally, if the PCHS geometry covers
|
---|
4999 | * more than the image size, set it back to the default. */
|
---|
5000 | if ( pPCHS->cHeads > 16
|
---|
5001 | || pPCHS->cSectors > 63
|
---|
5002 | || pPCHS->cCylinders == 0
|
---|
5003 | || (uint64_t)pPCHS->cHeads * pPCHS->cSectors * pPCHS->cCylinders * 512 > cbSize)
|
---|
5004 | {
|
---|
5005 | Assert(!(RT_MIN(cbSize / 512 / 16 / 63, 16383) - (uint32_t)RT_MIN(cbSize / 512 / 16 / 63, 16383)));
|
---|
5006 | pPCHS->cCylinders = (uint32_t)RT_MIN(cbSize / 512 / 16 / 63, 16383);
|
---|
5007 | pPCHS->cHeads = 16;
|
---|
5008 | pPCHS->cSectors = 63;
|
---|
5009 | }
|
---|
5010 | }
|
---|
5011 |
|
---|
5012 | /**
|
---|
5013 | * internal: adjust PCHS geometry
|
---|
5014 | */
|
---|
5015 | static void vdFixupLCHSGeometry(PVDGEOMETRY pLCHS, uint64_t cbSize)
|
---|
5016 | {
|
---|
5017 | /* Fix broken LCHS geometry. Can happen for two reasons: either the backend
|
---|
5018 | * mixes up PCHS and LCHS, or the application used to create the source
|
---|
5019 | * image has put garbage in it. The fix in this case is to clear the LCHS
|
---|
5020 | * geometry to trigger autodetection when it is used next. If the geometry
|
---|
5021 | * already says "please autodetect" (cylinders=0) keep it. */
|
---|
5022 | if ( ( pLCHS->cHeads > 255
|
---|
5023 | || pLCHS->cHeads == 0
|
---|
5024 | || pLCHS->cSectors > 63
|
---|
5025 | || pLCHS->cSectors == 0)
|
---|
5026 | && pLCHS->cCylinders != 0)
|
---|
5027 | {
|
---|
5028 | pLCHS->cCylinders = 0;
|
---|
5029 | pLCHS->cHeads = 0;
|
---|
5030 | pLCHS->cSectors = 0;
|
---|
5031 | }
|
---|
5032 | /* Always recompute the number of cylinders stored in the LCHS
|
---|
5033 | * geometry if it isn't set to "autotedetect" at the moment.
|
---|
5034 | * This is very useful if the destination image size is
|
---|
5035 | * larger or smaller than the source image size. Do not modify
|
---|
5036 | * the number of heads and sectors. Windows guests hate it. */
|
---|
5037 | if ( pLCHS->cCylinders != 0
|
---|
5038 | && pLCHS->cHeads != 0 /* paranoia */
|
---|
5039 | && pLCHS->cSectors != 0 /* paranoia */)
|
---|
5040 | {
|
---|
5041 | Assert(!(RT_MIN(cbSize / 512 / pLCHS->cHeads / pLCHS->cSectors, 1024) - (uint32_t)RT_MIN(cbSize / 512 / pLCHS->cHeads / pLCHS->cSectors, 1024)));
|
---|
5042 | pLCHS->cCylinders = (uint32_t)RT_MIN(cbSize / 512 / pLCHS->cHeads / pLCHS->cSectors, 1024);
|
---|
5043 | }
|
---|
5044 | }
|
---|
5045 |
|
---|
5046 | /**
|
---|
5047 | * Sets the I/O callbacks of the given interface to the fallback methods
|
---|
5048 | *
|
---|
5049 | * @returns nothing.
|
---|
5050 | * @param pIfIo The I/O interface to setup.
|
---|
5051 | */
|
---|
5052 | static void vdIfIoFallbackCallbacksSetup(PVDINTERFACEIO pIfIo)
|
---|
5053 | {
|
---|
5054 | pIfIo->pfnOpen = vdIOOpenFallback;
|
---|
5055 | pIfIo->pfnClose = vdIOCloseFallback;
|
---|
5056 | pIfIo->pfnDelete = vdIODeleteFallback;
|
---|
5057 | pIfIo->pfnMove = vdIOMoveFallback;
|
---|
5058 | pIfIo->pfnGetFreeSpace = vdIOGetFreeSpaceFallback;
|
---|
5059 | pIfIo->pfnGetModificationTime = vdIOGetModificationTimeFallback;
|
---|
5060 | pIfIo->pfnGetSize = vdIOGetSizeFallback;
|
---|
5061 | pIfIo->pfnSetSize = vdIOSetSizeFallback;
|
---|
5062 | pIfIo->pfnReadSync = vdIOReadSyncFallback;
|
---|
5063 | pIfIo->pfnWriteSync = vdIOWriteSyncFallback;
|
---|
5064 | pIfIo->pfnFlushSync = vdIOFlushSyncFallback;
|
---|
5065 | pIfIo->pfnReadAsync = vdIOReadAsyncFallback;
|
---|
5066 | pIfIo->pfnWriteAsync = vdIOWriteAsyncFallback;
|
---|
5067 | pIfIo->pfnFlushAsync = vdIOFlushAsyncFallback;
|
---|
5068 | }
|
---|
5069 |
|
---|
5070 | /**
|
---|
5071 | * Sets the internal I/O callbacks of the given interface.
|
---|
5072 | *
|
---|
5073 | * @returns nothing.
|
---|
5074 | * @param pIfIoInt The internal I/O interface to setup.
|
---|
5075 | */
|
---|
5076 | static void vdIfIoIntCallbacksSetup(PVDINTERFACEIOINT pIfIoInt)
|
---|
5077 | {
|
---|
5078 | pIfIoInt->pfnOpen = vdIOIntOpen;
|
---|
5079 | pIfIoInt->pfnClose = vdIOIntClose;
|
---|
5080 | pIfIoInt->pfnDelete = vdIOIntDelete;
|
---|
5081 | pIfIoInt->pfnMove = vdIOIntMove;
|
---|
5082 | pIfIoInt->pfnGetFreeSpace = vdIOIntGetFreeSpace;
|
---|
5083 | pIfIoInt->pfnGetModificationTime = vdIOIntGetModificationTime;
|
---|
5084 | pIfIoInt->pfnGetSize = vdIOIntGetSize;
|
---|
5085 | pIfIoInt->pfnSetSize = vdIOIntSetSize;
|
---|
5086 | pIfIoInt->pfnReadUser = vdIOIntReadUser;
|
---|
5087 | pIfIoInt->pfnWriteUser = vdIOIntWriteUser;
|
---|
5088 | pIfIoInt->pfnReadMeta = vdIOIntReadMeta;
|
---|
5089 | pIfIoInt->pfnWriteMeta = vdIOIntWriteMeta;
|
---|
5090 | pIfIoInt->pfnMetaXferRelease = vdIOIntMetaXferRelease;
|
---|
5091 | pIfIoInt->pfnFlush = vdIOIntFlush;
|
---|
5092 | pIfIoInt->pfnIoCtxCopyFrom = vdIOIntIoCtxCopyFrom;
|
---|
5093 | pIfIoInt->pfnIoCtxCopyTo = vdIOIntIoCtxCopyTo;
|
---|
5094 | pIfIoInt->pfnIoCtxSet = vdIOIntIoCtxSet;
|
---|
5095 | pIfIoInt->pfnIoCtxSegArrayCreate = vdIOIntIoCtxSegArrayCreate;
|
---|
5096 | pIfIoInt->pfnIoCtxCompleted = vdIOIntIoCtxCompleted;
|
---|
5097 | pIfIoInt->pfnIoCtxIsSynchronous = vdIOIntIoCtxIsSynchronous;
|
---|
5098 | pIfIoInt->pfnIoCtxIsZero = vdIOIntIoCtxIsZero;
|
---|
5099 | }
|
---|
5100 |
|
---|
5101 | /**
|
---|
5102 | * Internally used completion handler for synchronous I/O contexts.
|
---|
5103 | */
|
---|
5104 | static DECLCALLBACK(void) vdIoCtxSyncComplete(void *pvUser1, void *pvUser2, int rcReq)
|
---|
5105 | {
|
---|
5106 | PVBOXHDD pDisk = (PVBOXHDD)pvUser1;
|
---|
5107 |
|
---|
5108 | pDisk->rcSync = rcReq;
|
---|
5109 | RTSemEventSignal(pDisk->hEventSemSyncIo);
|
---|
5110 | }
|
---|
5111 |
|
---|
5112 | /**
|
---|
5113 | * Initializes HDD backends.
|
---|
5114 | *
|
---|
5115 | * @returns VBox status code.
|
---|
5116 | */
|
---|
5117 | VBOXDDU_DECL(int) VDInit(void)
|
---|
5118 | {
|
---|
5119 | int rc = vdAddBackends(aStaticBackends, RT_ELEMENTS(aStaticBackends));
|
---|
5120 | if (RT_SUCCESS(rc))
|
---|
5121 | {
|
---|
5122 | rc = vdAddCacheBackends(aStaticCacheBackends, RT_ELEMENTS(aStaticCacheBackends));
|
---|
5123 | if (RT_SUCCESS(rc))
|
---|
5124 | {
|
---|
5125 | rc = vdLoadDynamicBackends();
|
---|
5126 | if (RT_SUCCESS(rc))
|
---|
5127 | rc = vdLoadDynamicCacheBackends();
|
---|
5128 | }
|
---|
5129 | }
|
---|
5130 | LogRel(("VDInit finished\n"));
|
---|
5131 | return rc;
|
---|
5132 | }
|
---|
5133 |
|
---|
5134 | /**
|
---|
5135 | * Destroys loaded HDD backends.
|
---|
5136 | *
|
---|
5137 | * @returns VBox status code.
|
---|
5138 | */
|
---|
5139 | VBOXDDU_DECL(int) VDShutdown(void)
|
---|
5140 | {
|
---|
5141 | PVBOXHDDBACKEND *pBackends = g_apBackends;
|
---|
5142 | PVDCACHEBACKEND *pCacheBackends = g_apCacheBackends;
|
---|
5143 | unsigned cBackends = g_cBackends;
|
---|
5144 |
|
---|
5145 | if (!pBackends)
|
---|
5146 | return VERR_INTERNAL_ERROR;
|
---|
5147 |
|
---|
5148 | g_cBackends = 0;
|
---|
5149 | g_apBackends = NULL;
|
---|
5150 |
|
---|
5151 | #ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
5152 | for (unsigned i = 0; i < cBackends; i++)
|
---|
5153 | if (pBackends[i]->hPlugin != NIL_RTLDRMOD)
|
---|
5154 | RTLdrClose(pBackends[i]->hPlugin);
|
---|
5155 | #endif
|
---|
5156 |
|
---|
5157 | /* Clear the supported cache backends. */
|
---|
5158 | cBackends = g_cCacheBackends;
|
---|
5159 | g_cCacheBackends = 0;
|
---|
5160 | g_apCacheBackends = NULL;
|
---|
5161 |
|
---|
5162 | #ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
|
---|
5163 | for (unsigned i = 0; i < cBackends; i++)
|
---|
5164 | if (pCacheBackends[i]->hPlugin != NIL_RTLDRMOD)
|
---|
5165 | RTLdrClose(pCacheBackends[i]->hPlugin);
|
---|
5166 | #endif
|
---|
5167 |
|
---|
5168 | if (pCacheBackends)
|
---|
5169 | RTMemFree(pCacheBackends);
|
---|
5170 | RTMemFree(pBackends);
|
---|
5171 | return VINF_SUCCESS;
|
---|
5172 | }
|
---|
5173 |
|
---|
5174 |
|
---|
5175 | /**
|
---|
5176 | * Lists all HDD backends and their capabilities in a caller-provided buffer.
|
---|
5177 | *
|
---|
5178 | * @returns VBox status code.
|
---|
5179 | * VERR_BUFFER_OVERFLOW if not enough space is passed.
|
---|
5180 | * @param cEntriesAlloc Number of list entries available.
|
---|
5181 | * @param pEntries Pointer to array for the entries.
|
---|
5182 | * @param pcEntriesUsed Number of entries returned.
|
---|
5183 | */
|
---|
5184 | VBOXDDU_DECL(int) VDBackendInfo(unsigned cEntriesAlloc, PVDBACKENDINFO pEntries,
|
---|
5185 | unsigned *pcEntriesUsed)
|
---|
5186 | {
|
---|
5187 | int rc = VINF_SUCCESS;
|
---|
5188 | PRTDIR pPluginDir = NULL;
|
---|
5189 | unsigned cEntries = 0;
|
---|
5190 |
|
---|
5191 | LogFlowFunc(("cEntriesAlloc=%u pEntries=%#p pcEntriesUsed=%#p\n", cEntriesAlloc, pEntries, pcEntriesUsed));
|
---|
5192 | /* Check arguments. */
|
---|
5193 | AssertMsgReturn(cEntriesAlloc,
|
---|
5194 | ("cEntriesAlloc=%u\n", cEntriesAlloc),
|
---|
5195 | VERR_INVALID_PARAMETER);
|
---|
5196 | AssertMsgReturn(VALID_PTR(pEntries),
|
---|
5197 | ("pEntries=%#p\n", pEntries),
|
---|
5198 | VERR_INVALID_PARAMETER);
|
---|
5199 | AssertMsgReturn(VALID_PTR(pcEntriesUsed),
|
---|
5200 | ("pcEntriesUsed=%#p\n", pcEntriesUsed),
|
---|
5201 | VERR_INVALID_PARAMETER);
|
---|
5202 | if (!g_apBackends)
|
---|
5203 | VDInit();
|
---|
5204 |
|
---|
5205 | if (cEntriesAlloc < g_cBackends)
|
---|
5206 | {
|
---|
5207 | *pcEntriesUsed = g_cBackends;
|
---|
5208 | return VERR_BUFFER_OVERFLOW;
|
---|
5209 | }
|
---|
5210 |
|
---|
5211 | for (unsigned i = 0; i < g_cBackends; i++)
|
---|
5212 | {
|
---|
5213 | pEntries[i].pszBackend = g_apBackends[i]->pszBackendName;
|
---|
5214 | pEntries[i].uBackendCaps = g_apBackends[i]->uBackendCaps;
|
---|
5215 | pEntries[i].paFileExtensions = g_apBackends[i]->paFileExtensions;
|
---|
5216 | pEntries[i].paConfigInfo = g_apBackends[i]->paConfigInfo;
|
---|
5217 | pEntries[i].pfnComposeLocation = g_apBackends[i]->pfnComposeLocation;
|
---|
5218 | pEntries[i].pfnComposeName = g_apBackends[i]->pfnComposeName;
|
---|
5219 | }
|
---|
5220 |
|
---|
5221 | LogFlowFunc(("returns %Rrc *pcEntriesUsed=%u\n", rc, cEntries));
|
---|
5222 | *pcEntriesUsed = g_cBackends;
|
---|
5223 | return rc;
|
---|
5224 | }
|
---|
5225 |
|
---|
5226 | /**
|
---|
5227 | * Lists the capabilities of a backend identified by its name.
|
---|
5228 | *
|
---|
5229 | * @returns VBox status code.
|
---|
5230 | * @param pszBackend The backend name.
|
---|
5231 | * @param pEntries Pointer to an entry.
|
---|
5232 | */
|
---|
5233 | VBOXDDU_DECL(int) VDBackendInfoOne(const char *pszBackend, PVDBACKENDINFO pEntry)
|
---|
5234 | {
|
---|
5235 | LogFlowFunc(("pszBackend=%#p pEntry=%#p\n", pszBackend, pEntry));
|
---|
5236 | /* Check arguments. */
|
---|
5237 | AssertMsgReturn(VALID_PTR(pszBackend),
|
---|
5238 | ("pszBackend=%#p\n", pszBackend),
|
---|
5239 | VERR_INVALID_PARAMETER);
|
---|
5240 | AssertMsgReturn(VALID_PTR(pEntry),
|
---|
5241 | ("pEntry=%#p\n", pEntry),
|
---|
5242 | VERR_INVALID_PARAMETER);
|
---|
5243 | if (!g_apBackends)
|
---|
5244 | VDInit();
|
---|
5245 |
|
---|
5246 | /* Go through loaded backends. */
|
---|
5247 | for (unsigned i = 0; i < g_cBackends; i++)
|
---|
5248 | {
|
---|
5249 | if (!RTStrICmp(pszBackend, g_apBackends[i]->pszBackendName))
|
---|
5250 | {
|
---|
5251 | pEntry->pszBackend = g_apBackends[i]->pszBackendName;
|
---|
5252 | pEntry->uBackendCaps = g_apBackends[i]->uBackendCaps;
|
---|
5253 | pEntry->paFileExtensions = g_apBackends[i]->paFileExtensions;
|
---|
5254 | pEntry->paConfigInfo = g_apBackends[i]->paConfigInfo;
|
---|
5255 | return VINF_SUCCESS;
|
---|
5256 | }
|
---|
5257 | }
|
---|
5258 |
|
---|
5259 | return VERR_NOT_FOUND;
|
---|
5260 | }
|
---|
5261 |
|
---|
5262 | /**
|
---|
5263 | * Allocates and initializes an empty HDD container.
|
---|
5264 | * No image files are opened.
|
---|
5265 | *
|
---|
5266 | * @returns VBox status code.
|
---|
5267 | * @param pVDIfsDisk Pointer to the per-disk VD interface list.
|
---|
5268 | * @param enmType Type of the image container.
|
---|
5269 | * @param ppDisk Where to store the reference to HDD container.
|
---|
5270 | */
|
---|
5271 | VBOXDDU_DECL(int) VDCreate(PVDINTERFACE pVDIfsDisk, VDTYPE enmType, PVBOXHDD *ppDisk)
|
---|
5272 | {
|
---|
5273 | int rc = VINF_SUCCESS;
|
---|
5274 | PVBOXHDD pDisk = NULL;
|
---|
5275 |
|
---|
5276 | LogFlowFunc(("pVDIfsDisk=%#p\n", pVDIfsDisk));
|
---|
5277 | do
|
---|
5278 | {
|
---|
5279 | /* Check arguments. */
|
---|
5280 | AssertMsgBreakStmt(VALID_PTR(ppDisk),
|
---|
5281 | ("ppDisk=%#p\n", ppDisk),
|
---|
5282 | rc = VERR_INVALID_PARAMETER);
|
---|
5283 |
|
---|
5284 | pDisk = (PVBOXHDD)RTMemAllocZ(sizeof(VBOXHDD));
|
---|
5285 | if (pDisk)
|
---|
5286 | {
|
---|
5287 | pDisk->u32Signature = VBOXHDDDISK_SIGNATURE;
|
---|
5288 | pDisk->enmType = enmType;
|
---|
5289 | pDisk->cImages = 0;
|
---|
5290 | pDisk->pBase = NULL;
|
---|
5291 | pDisk->pLast = NULL;
|
---|
5292 | pDisk->cbSize = 0;
|
---|
5293 | pDisk->PCHSGeometry.cCylinders = 0;
|
---|
5294 | pDisk->PCHSGeometry.cHeads = 0;
|
---|
5295 | pDisk->PCHSGeometry.cSectors = 0;
|
---|
5296 | pDisk->LCHSGeometry.cCylinders = 0;
|
---|
5297 | pDisk->LCHSGeometry.cHeads = 0;
|
---|
5298 | pDisk->LCHSGeometry.cSectors = 0;
|
---|
5299 | pDisk->pVDIfsDisk = pVDIfsDisk;
|
---|
5300 | pDisk->pInterfaceError = NULL;
|
---|
5301 | pDisk->pInterfaceThreadSync = NULL;
|
---|
5302 | pDisk->pIoCtxLockOwner = NULL;
|
---|
5303 | pDisk->pIoCtxHead = NULL;
|
---|
5304 | pDisk->fLocked = false;
|
---|
5305 | pDisk->hEventSemSyncIo = NIL_RTSEMEVENT;
|
---|
5306 | pDisk->hMemCacheIoCtx = NIL_RTMEMCACHE;
|
---|
5307 | pDisk->hMemCacheIoTask = NIL_RTMEMCACHE;
|
---|
5308 |
|
---|
5309 | rc = RTSemEventCreate(&pDisk->hEventSemSyncIo);
|
---|
5310 | if (RT_FAILURE(rc))
|
---|
5311 | break;
|
---|
5312 |
|
---|
5313 | /* Create the I/O ctx cache */
|
---|
5314 | rc = RTMemCacheCreate(&pDisk->hMemCacheIoCtx, sizeof(VDIOCTX), 0, UINT32_MAX,
|
---|
5315 | NULL, NULL, NULL, 0);
|
---|
5316 | if (RT_FAILURE(rc))
|
---|
5317 | break;
|
---|
5318 |
|
---|
5319 | /* Create the I/O task cache */
|
---|
5320 | rc = RTMemCacheCreate(&pDisk->hMemCacheIoTask, sizeof(VDIOTASK), 0, UINT32_MAX,
|
---|
5321 | NULL, NULL, NULL, 0);
|
---|
5322 | if (RT_FAILURE(rc))
|
---|
5323 | break;
|
---|
5324 |
|
---|
5325 | pDisk->pInterfaceError = VDIfErrorGet(pVDIfsDisk);
|
---|
5326 | pDisk->pInterfaceThreadSync = VDIfThreadSyncGet(pVDIfsDisk);
|
---|
5327 |
|
---|
5328 | *ppDisk = pDisk;
|
---|
5329 | }
|
---|
5330 | else
|
---|
5331 | {
|
---|
5332 | rc = VERR_NO_MEMORY;
|
---|
5333 | break;
|
---|
5334 | }
|
---|
5335 | } while (0);
|
---|
5336 |
|
---|
5337 | if ( RT_FAILURE(rc)
|
---|
5338 | && pDisk)
|
---|
5339 | {
|
---|
5340 | if (pDisk->hEventSemSyncIo != NIL_RTSEMEVENT)
|
---|
5341 | RTSemEventDestroy(pDisk->hEventSemSyncIo);
|
---|
5342 | if (pDisk->hMemCacheIoCtx != NIL_RTMEMCACHE)
|
---|
5343 | RTMemCacheDestroy(pDisk->hMemCacheIoCtx);
|
---|
5344 | if (pDisk->hMemCacheIoTask != NIL_RTMEMCACHE)
|
---|
5345 | RTMemCacheDestroy(pDisk->hMemCacheIoTask);
|
---|
5346 | }
|
---|
5347 |
|
---|
5348 | LogFlowFunc(("returns %Rrc (pDisk=%#p)\n", rc, pDisk));
|
---|
5349 | return rc;
|
---|
5350 | }
|
---|
5351 |
|
---|
5352 | /**
|
---|
5353 | * Destroys HDD container.
|
---|
5354 | * If container has opened image files they will be closed.
|
---|
5355 | *
|
---|
5356 | * @returns VBox status code.
|
---|
5357 | * @param pDisk Pointer to HDD container.
|
---|
5358 | */
|
---|
5359 | VBOXDDU_DECL(int) VDDestroy(PVBOXHDD pDisk)
|
---|
5360 | {
|
---|
5361 | int rc = VINF_SUCCESS;
|
---|
5362 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
5363 | do
|
---|
5364 | {
|
---|
5365 | /* sanity check */
|
---|
5366 | AssertPtrBreak(pDisk);
|
---|
5367 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
5368 | Assert(!pDisk->fLocked);
|
---|
5369 |
|
---|
5370 | rc = VDCloseAll(pDisk);
|
---|
5371 | RTMemCacheDestroy(pDisk->hMemCacheIoCtx);
|
---|
5372 | RTMemCacheDestroy(pDisk->hMemCacheIoTask);
|
---|
5373 | RTSemEventDestroy(pDisk->hEventSemSyncIo);
|
---|
5374 | RTMemFree(pDisk);
|
---|
5375 | } while (0);
|
---|
5376 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
5377 | return rc;
|
---|
5378 | }
|
---|
5379 |
|
---|
5380 | /**
|
---|
5381 | * Try to get the backend name which can use this image.
|
---|
5382 | *
|
---|
5383 | * @returns VBox status code.
|
---|
5384 | * VINF_SUCCESS if a plugin was found.
|
---|
5385 | * ppszFormat contains the string which can be used as backend name.
|
---|
5386 | * VERR_NOT_SUPPORTED if no backend was found.
|
---|
5387 | * @param pVDIfsDisk Pointer to the per-disk VD interface list.
|
---|
5388 | * @param pVDIfsImage Pointer to the per-image VD interface list.
|
---|
5389 | * @param pszFilename Name of the image file for which the backend is queried.
|
---|
5390 | * @param ppszFormat Receives pointer of the UTF-8 string which contains the format name.
|
---|
5391 | * The returned pointer must be freed using RTStrFree().
|
---|
5392 | */
|
---|
5393 | VBOXDDU_DECL(int) VDGetFormat(PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
|
---|
5394 | const char *pszFilename, char **ppszFormat, VDTYPE *penmType)
|
---|
5395 | {
|
---|
5396 | int rc = VERR_NOT_SUPPORTED;
|
---|
5397 | VDINTERFACEIOINT VDIfIoInt;
|
---|
5398 | VDINTERFACEIO VDIfIoFallback;
|
---|
5399 | PVDINTERFACEIO pInterfaceIo;
|
---|
5400 |
|
---|
5401 | LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
|
---|
5402 | /* Check arguments. */
|
---|
5403 | AssertMsgReturn(VALID_PTR(pszFilename) && *pszFilename,
|
---|
5404 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
5405 | VERR_INVALID_PARAMETER);
|
---|
5406 | AssertMsgReturn(VALID_PTR(ppszFormat),
|
---|
5407 | ("ppszFormat=%#p\n", ppszFormat),
|
---|
5408 | VERR_INVALID_PARAMETER);
|
---|
5409 | AssertMsgReturn(VALID_PTR(penmType),
|
---|
5410 | ("penmType=%#p\n", penmType),
|
---|
5411 | VERR_INVALID_PARAMETER);
|
---|
5412 |
|
---|
5413 | if (!g_apBackends)
|
---|
5414 | VDInit();
|
---|
5415 |
|
---|
5416 | pInterfaceIo = VDIfIoGet(pVDIfsImage);
|
---|
5417 | if (!pInterfaceIo)
|
---|
5418 | {
|
---|
5419 | /*
|
---|
5420 | * Caller doesn't provide an I/O interface, create our own using the
|
---|
5421 | * native file API.
|
---|
5422 | */
|
---|
5423 | vdIfIoFallbackCallbacksSetup(&VDIfIoFallback);
|
---|
5424 | pInterfaceIo = &VDIfIoFallback;
|
---|
5425 | }
|
---|
5426 |
|
---|
5427 | /* Set up the internal I/O interface. */
|
---|
5428 | AssertReturn(!VDIfIoIntGet(pVDIfsImage), VERR_INVALID_PARAMETER);
|
---|
5429 | VDIfIoInt.pfnOpen = vdIOIntOpenLimited;
|
---|
5430 | VDIfIoInt.pfnClose = vdIOIntCloseLimited;
|
---|
5431 | VDIfIoInt.pfnDelete = vdIOIntDeleteLimited;
|
---|
5432 | VDIfIoInt.pfnMove = vdIOIntMoveLimited;
|
---|
5433 | VDIfIoInt.pfnGetFreeSpace = vdIOIntGetFreeSpaceLimited;
|
---|
5434 | VDIfIoInt.pfnGetModificationTime = vdIOIntGetModificationTimeLimited;
|
---|
5435 | VDIfIoInt.pfnGetSize = vdIOIntGetSizeLimited;
|
---|
5436 | VDIfIoInt.pfnSetSize = vdIOIntSetSizeLimited;
|
---|
5437 | VDIfIoInt.pfnReadUser = vdIOIntReadUserLimited;
|
---|
5438 | VDIfIoInt.pfnWriteUser = vdIOIntWriteUserLimited;
|
---|
5439 | VDIfIoInt.pfnReadMeta = vdIOIntReadMetaLimited;
|
---|
5440 | VDIfIoInt.pfnWriteMeta = vdIOIntWriteMetaLimited;
|
---|
5441 | VDIfIoInt.pfnFlush = vdIOIntFlushLimited;
|
---|
5442 | rc = VDInterfaceAdd(&VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
5443 | pInterfaceIo, sizeof(VDINTERFACEIOINT), &pVDIfsImage);
|
---|
5444 | AssertRC(rc);
|
---|
5445 |
|
---|
5446 | /* Find the backend supporting this file format. */
|
---|
5447 | for (unsigned i = 0; i < g_cBackends; i++)
|
---|
5448 | {
|
---|
5449 | if (g_apBackends[i]->pfnCheckIfValid)
|
---|
5450 | {
|
---|
5451 | rc = g_apBackends[i]->pfnCheckIfValid(pszFilename, pVDIfsDisk,
|
---|
5452 | pVDIfsImage, penmType);
|
---|
5453 | if ( RT_SUCCESS(rc)
|
---|
5454 | /* The correct backend has been found, but there is a small
|
---|
5455 | * incompatibility so that the file cannot be used. Stop here
|
---|
5456 | * and signal success - the actual open will of course fail,
|
---|
5457 | * but that will create a really sensible error message. */
|
---|
5458 | || ( rc != VERR_VD_GEN_INVALID_HEADER
|
---|
5459 | && rc != VERR_VD_VDI_INVALID_HEADER
|
---|
5460 | && rc != VERR_VD_VMDK_INVALID_HEADER
|
---|
5461 | && rc != VERR_VD_ISCSI_INVALID_HEADER
|
---|
5462 | && rc != VERR_VD_VHD_INVALID_HEADER
|
---|
5463 | && rc != VERR_VD_RAW_INVALID_HEADER
|
---|
5464 | && rc != VERR_VD_PARALLELS_INVALID_HEADER
|
---|
5465 | && rc != VERR_VD_DMG_INVALID_HEADER))
|
---|
5466 | {
|
---|
5467 | /* Copy the name into the new string. */
|
---|
5468 | char *pszFormat = RTStrDup(g_apBackends[i]->pszBackendName);
|
---|
5469 | if (!pszFormat)
|
---|
5470 | {
|
---|
5471 | rc = VERR_NO_MEMORY;
|
---|
5472 | break;
|
---|
5473 | }
|
---|
5474 | *ppszFormat = pszFormat;
|
---|
5475 | /* Do not consider the typical file access errors as success,
|
---|
5476 | * which allows the caller to deal with such issues. */
|
---|
5477 | if ( rc != VERR_ACCESS_DENIED
|
---|
5478 | && rc != VERR_PATH_NOT_FOUND
|
---|
5479 | && rc != VERR_FILE_NOT_FOUND)
|
---|
5480 | rc = VINF_SUCCESS;
|
---|
5481 | break;
|
---|
5482 | }
|
---|
5483 | rc = VERR_NOT_SUPPORTED;
|
---|
5484 | }
|
---|
5485 | }
|
---|
5486 |
|
---|
5487 | /* Try the cache backends. */
|
---|
5488 | if (rc == VERR_NOT_SUPPORTED)
|
---|
5489 | {
|
---|
5490 | for (unsigned i = 0; i < g_cCacheBackends; i++)
|
---|
5491 | {
|
---|
5492 | if (g_apCacheBackends[i]->pfnProbe)
|
---|
5493 | {
|
---|
5494 | rc = g_apCacheBackends[i]->pfnProbe(pszFilename, pVDIfsDisk,
|
---|
5495 | pVDIfsImage);
|
---|
5496 | if ( RT_SUCCESS(rc)
|
---|
5497 | || (rc != VERR_VD_GEN_INVALID_HEADER))
|
---|
5498 | {
|
---|
5499 | /* Copy the name into the new string. */
|
---|
5500 | char *pszFormat = RTStrDup(g_apBackends[i]->pszBackendName);
|
---|
5501 | if (!pszFormat)
|
---|
5502 | {
|
---|
5503 | rc = VERR_NO_MEMORY;
|
---|
5504 | break;
|
---|
5505 | }
|
---|
5506 | *ppszFormat = pszFormat;
|
---|
5507 | rc = VINF_SUCCESS;
|
---|
5508 | break;
|
---|
5509 | }
|
---|
5510 | rc = VERR_NOT_SUPPORTED;
|
---|
5511 | }
|
---|
5512 | }
|
---|
5513 | }
|
---|
5514 |
|
---|
5515 | LogFlowFunc(("returns %Rrc *ppszFormat=\"%s\"\n", rc, *ppszFormat));
|
---|
5516 | return rc;
|
---|
5517 | }
|
---|
5518 |
|
---|
5519 | /**
|
---|
5520 | * Opens an image file.
|
---|
5521 | *
|
---|
5522 | * The first opened image file in HDD container must have a base image type,
|
---|
5523 | * others (next opened images) must be a differencing or undo images.
|
---|
5524 | * Linkage is checked for differencing image to be in consistence with the previously opened image.
|
---|
5525 | * When another differencing image is opened and the last image was opened in read/write access
|
---|
5526 | * mode, then the last image is reopened in read-only with deny write sharing mode. This allows
|
---|
5527 | * other processes to use images in read-only mode too.
|
---|
5528 | *
|
---|
5529 | * Note that the image is opened in read-only mode if a read/write open is not possible.
|
---|
5530 | * Use VDIsReadOnly to check open mode.
|
---|
5531 | *
|
---|
5532 | * @returns VBox status code.
|
---|
5533 | * @param pDisk Pointer to HDD container.
|
---|
5534 | * @param pszBackend Name of the image file backend to use.
|
---|
5535 | * @param pszFilename Name of the image file to open.
|
---|
5536 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
5537 | * @param pVDIfsImage Pointer to the per-image VD interface list.
|
---|
5538 | */
|
---|
5539 | VBOXDDU_DECL(int) VDOpen(PVBOXHDD pDisk, const char *pszBackend,
|
---|
5540 | const char *pszFilename, unsigned uOpenFlags,
|
---|
5541 | PVDINTERFACE pVDIfsImage)
|
---|
5542 | {
|
---|
5543 | int rc = VINF_SUCCESS;
|
---|
5544 | int rc2;
|
---|
5545 | bool fLockWrite = false;
|
---|
5546 | PVDIMAGE pImage = NULL;
|
---|
5547 |
|
---|
5548 | LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uOpenFlags=%#x, pVDIfsImage=%#p\n",
|
---|
5549 | pDisk, pszBackend, pszFilename, uOpenFlags, pVDIfsImage));
|
---|
5550 |
|
---|
5551 | do
|
---|
5552 | {
|
---|
5553 | /* sanity check */
|
---|
5554 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
5555 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
5556 |
|
---|
5557 | /* Check arguments. */
|
---|
5558 | AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
|
---|
5559 | ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
|
---|
5560 | rc = VERR_INVALID_PARAMETER);
|
---|
5561 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
5562 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
5563 | rc = VERR_INVALID_PARAMETER);
|
---|
5564 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
5565 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
5566 | rc = VERR_INVALID_PARAMETER);
|
---|
5567 | AssertMsgBreakStmt( !(uOpenFlags & VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)
|
---|
5568 | || (uOpenFlags & VD_OPEN_FLAGS_READONLY),
|
---|
5569 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
5570 | rc = VERR_INVALID_PARAMETER);
|
---|
5571 |
|
---|
5572 | /*
|
---|
5573 | * Destroy the current discard state first which might still have pending blocks
|
---|
5574 | * for the currently opened image which will be switched to readonly mode.
|
---|
5575 | */
|
---|
5576 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
5577 | rc2 = vdThreadStartWrite(pDisk);
|
---|
5578 | AssertRC(rc2);
|
---|
5579 | fLockWrite = true;
|
---|
5580 | rc = vdDiscardStateDestroy(pDisk);
|
---|
5581 | if (RT_FAILURE(rc))
|
---|
5582 | break;
|
---|
5583 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
5584 | AssertRC(rc2);
|
---|
5585 | fLockWrite = false;
|
---|
5586 |
|
---|
5587 | /* Set up image descriptor. */
|
---|
5588 | pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
|
---|
5589 | if (!pImage)
|
---|
5590 | {
|
---|
5591 | rc = VERR_NO_MEMORY;
|
---|
5592 | break;
|
---|
5593 | }
|
---|
5594 | pImage->pszFilename = RTStrDup(pszFilename);
|
---|
5595 | if (!pImage->pszFilename)
|
---|
5596 | {
|
---|
5597 | rc = VERR_NO_MEMORY;
|
---|
5598 | break;
|
---|
5599 | }
|
---|
5600 |
|
---|
5601 | pImage->VDIo.pDisk = pDisk;
|
---|
5602 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
5603 |
|
---|
5604 | rc = vdFindBackend(pszBackend, &pImage->Backend);
|
---|
5605 | if (RT_FAILURE(rc))
|
---|
5606 | break;
|
---|
5607 | if (!pImage->Backend)
|
---|
5608 | {
|
---|
5609 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
5610 | N_("VD: unknown backend name '%s'"), pszBackend);
|
---|
5611 | break;
|
---|
5612 | }
|
---|
5613 |
|
---|
5614 | /*
|
---|
5615 | * Fail if the backend can't do async I/O but the
|
---|
5616 | * flag is set.
|
---|
5617 | */
|
---|
5618 | if ( !(pImage->Backend->uBackendCaps & VD_CAP_ASYNC)
|
---|
5619 | && (uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO))
|
---|
5620 | {
|
---|
5621 | rc = vdError(pDisk, VERR_NOT_SUPPORTED, RT_SRC_POS,
|
---|
5622 | N_("VD: Backend '%s' does not support async I/O"), pszBackend);
|
---|
5623 | break;
|
---|
5624 | }
|
---|
5625 |
|
---|
5626 | /*
|
---|
5627 | * Fail if the backend doesn't support the discard operation but the
|
---|
5628 | * flag is set.
|
---|
5629 | */
|
---|
5630 | if ( !(pImage->Backend->uBackendCaps & VD_CAP_DISCARD)
|
---|
5631 | && (uOpenFlags & VD_OPEN_FLAGS_DISCARD))
|
---|
5632 | {
|
---|
5633 | rc = vdError(pDisk, VERR_VD_DISCARD_NOT_SUPPORTED, RT_SRC_POS,
|
---|
5634 | N_("VD: Backend '%s' does not support discard"), pszBackend);
|
---|
5635 | break;
|
---|
5636 | }
|
---|
5637 |
|
---|
5638 | /* Set up the I/O interface. */
|
---|
5639 | pImage->VDIo.pInterfaceIo = VDIfIoGet(pVDIfsImage);
|
---|
5640 | if (!pImage->VDIo.pInterfaceIo)
|
---|
5641 | {
|
---|
5642 | vdIfIoFallbackCallbacksSetup(&pImage->VDIo.VDIfIo);
|
---|
5643 | rc = VDInterfaceAdd(&pImage->VDIo.VDIfIo.Core, "VD_IO", VDINTERFACETYPE_IO,
|
---|
5644 | pDisk, sizeof(VDINTERFACEIO), &pVDIfsImage);
|
---|
5645 | pImage->VDIo.pInterfaceIo = &pImage->VDIo.VDIfIo;
|
---|
5646 | }
|
---|
5647 |
|
---|
5648 | /* Set up the internal I/O interface. */
|
---|
5649 | AssertBreakStmt(!VDIfIoIntGet(pVDIfsImage), rc = VERR_INVALID_PARAMETER);
|
---|
5650 | vdIfIoIntCallbacksSetup(&pImage->VDIo.VDIfIoInt);
|
---|
5651 | rc = VDInterfaceAdd(&pImage->VDIo.VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
5652 | &pImage->VDIo, sizeof(VDINTERFACEIOINT), &pImage->pVDIfsImage);
|
---|
5653 | AssertRC(rc);
|
---|
5654 |
|
---|
5655 | pImage->uOpenFlags = uOpenFlags & (VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_DISCARD | VD_OPEN_FLAGS_IGNORE_FLUSH | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS);
|
---|
5656 | pImage->VDIo.fIgnoreFlush = (uOpenFlags & VD_OPEN_FLAGS_IGNORE_FLUSH) != 0;
|
---|
5657 | rc = pImage->Backend->pfnOpen(pImage->pszFilename,
|
---|
5658 | uOpenFlags & ~(VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_IGNORE_FLUSH | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS),
|
---|
5659 | pDisk->pVDIfsDisk,
|
---|
5660 | pImage->pVDIfsImage,
|
---|
5661 | pDisk->enmType,
|
---|
5662 | &pImage->pBackendData);
|
---|
5663 | /*
|
---|
5664 | * If the image is corrupted and there is a repair method try to repair it
|
---|
5665 | * first if it was openend in read-write mode and open again afterwards.
|
---|
5666 | */
|
---|
5667 | if ( RT_UNLIKELY(rc == VERR_VD_IMAGE_CORRUPTED)
|
---|
5668 | && pImage->Backend->pfnRepair)
|
---|
5669 | {
|
---|
5670 | rc = pImage->Backend->pfnRepair(pszFilename, pDisk->pVDIfsDisk, pImage->pVDIfsImage, 0 /* fFlags */);
|
---|
5671 | if (RT_SUCCESS(rc))
|
---|
5672 | rc = pImage->Backend->pfnOpen(pImage->pszFilename,
|
---|
5673 | uOpenFlags & ~(VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_IGNORE_FLUSH | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS),
|
---|
5674 | pDisk->pVDIfsDisk,
|
---|
5675 | pImage->pVDIfsImage,
|
---|
5676 | pDisk->enmType,
|
---|
5677 | &pImage->pBackendData);
|
---|
5678 | else
|
---|
5679 | {
|
---|
5680 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
5681 | N_("VD: error %Rrc repairing corrupted image file '%s'"), rc, pszFilename);
|
---|
5682 | break;
|
---|
5683 | }
|
---|
5684 | }
|
---|
5685 | else if (RT_UNLIKELY(rc == VERR_VD_IMAGE_CORRUPTED))
|
---|
5686 | {
|
---|
5687 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
5688 | N_("VD: Image file '%s' is corrupted and can't be opened"), pszFilename);
|
---|
5689 | break;
|
---|
5690 | }
|
---|
5691 |
|
---|
5692 | /* If the open in read-write mode failed, retry in read-only mode. */
|
---|
5693 | if (RT_FAILURE(rc))
|
---|
5694 | {
|
---|
5695 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
5696 | && ( rc == VERR_ACCESS_DENIED
|
---|
5697 | || rc == VERR_PERMISSION_DENIED
|
---|
5698 | || rc == VERR_WRITE_PROTECT
|
---|
5699 | || rc == VERR_SHARING_VIOLATION
|
---|
5700 | || rc == VERR_FILE_LOCK_FAILED))
|
---|
5701 | rc = pImage->Backend->pfnOpen(pImage->pszFilename,
|
---|
5702 | (uOpenFlags & ~(VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS))
|
---|
5703 | | VD_OPEN_FLAGS_READONLY,
|
---|
5704 | pDisk->pVDIfsDisk,
|
---|
5705 | pImage->pVDIfsImage,
|
---|
5706 | pDisk->enmType,
|
---|
5707 | &pImage->pBackendData);
|
---|
5708 | if (RT_FAILURE(rc))
|
---|
5709 | {
|
---|
5710 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
5711 | N_("VD: error %Rrc opening image file '%s'"), rc, pszFilename);
|
---|
5712 | break;
|
---|
5713 | }
|
---|
5714 | }
|
---|
5715 |
|
---|
5716 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
5717 | rc2 = vdThreadStartWrite(pDisk);
|
---|
5718 | AssertRC(rc2);
|
---|
5719 | fLockWrite = true;
|
---|
5720 |
|
---|
5721 | pImage->VDIo.pBackendData = pImage->pBackendData;
|
---|
5722 |
|
---|
5723 | /* Check image type. As the image itself has only partial knowledge
|
---|
5724 | * whether it's a base image or not, this info is derived here. The
|
---|
5725 | * base image can be fixed or normal, all others must be normal or
|
---|
5726 | * diff images. Some image formats don't distinguish between normal
|
---|
5727 | * and diff images, so this must be corrected here. */
|
---|
5728 | unsigned uImageFlags;
|
---|
5729 | uImageFlags = pImage->Backend->pfnGetImageFlags(pImage->pBackendData);
|
---|
5730 | if (RT_FAILURE(rc))
|
---|
5731 | uImageFlags = VD_IMAGE_FLAGS_NONE;
|
---|
5732 | if ( RT_SUCCESS(rc)
|
---|
5733 | && !(uOpenFlags & VD_OPEN_FLAGS_INFO))
|
---|
5734 | {
|
---|
5735 | if ( pDisk->cImages == 0
|
---|
5736 | && (uImageFlags & VD_IMAGE_FLAGS_DIFF))
|
---|
5737 | {
|
---|
5738 | rc = VERR_VD_INVALID_TYPE;
|
---|
5739 | break;
|
---|
5740 | }
|
---|
5741 | else if (pDisk->cImages != 0)
|
---|
5742 | {
|
---|
5743 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
5744 | {
|
---|
5745 | rc = VERR_VD_INVALID_TYPE;
|
---|
5746 | break;
|
---|
5747 | }
|
---|
5748 | else
|
---|
5749 | uImageFlags |= VD_IMAGE_FLAGS_DIFF;
|
---|
5750 | }
|
---|
5751 | }
|
---|
5752 |
|
---|
5753 | /* Ensure we always get correct diff information, even if the backend
|
---|
5754 | * doesn't actually have a stored flag for this. It must not return
|
---|
5755 | * bogus information for the parent UUID if it is not a diff image. */
|
---|
5756 | RTUUID parentUuid;
|
---|
5757 | RTUuidClear(&parentUuid);
|
---|
5758 | rc2 = pImage->Backend->pfnGetParentUuid(pImage->pBackendData, &parentUuid);
|
---|
5759 | if (RT_SUCCESS(rc2) && !RTUuidIsNull(&parentUuid))
|
---|
5760 | uImageFlags |= VD_IMAGE_FLAGS_DIFF;
|
---|
5761 |
|
---|
5762 | pImage->uImageFlags = uImageFlags;
|
---|
5763 |
|
---|
5764 | /* Force sane optimization settings. It's not worth avoiding writes
|
---|
5765 | * to fixed size images. The overhead would have almost no payback. */
|
---|
5766 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
5767 | pImage->uOpenFlags |= VD_OPEN_FLAGS_HONOR_SAME;
|
---|
5768 |
|
---|
5769 | /** @todo optionally check UUIDs */
|
---|
5770 |
|
---|
5771 | /* Cache disk information. */
|
---|
5772 | pDisk->cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
|
---|
5773 |
|
---|
5774 | /* Cache PCHS geometry. */
|
---|
5775 | rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
5776 | &pDisk->PCHSGeometry);
|
---|
5777 | if (RT_FAILURE(rc2))
|
---|
5778 | {
|
---|
5779 | pDisk->PCHSGeometry.cCylinders = 0;
|
---|
5780 | pDisk->PCHSGeometry.cHeads = 0;
|
---|
5781 | pDisk->PCHSGeometry.cSectors = 0;
|
---|
5782 | }
|
---|
5783 | else
|
---|
5784 | {
|
---|
5785 | /* Make sure the PCHS geometry is properly clipped. */
|
---|
5786 | pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
|
---|
5787 | pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
|
---|
5788 | pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
|
---|
5789 | }
|
---|
5790 |
|
---|
5791 | /* Cache LCHS geometry. */
|
---|
5792 | rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
5793 | &pDisk->LCHSGeometry);
|
---|
5794 | if (RT_FAILURE(rc2))
|
---|
5795 | {
|
---|
5796 | pDisk->LCHSGeometry.cCylinders = 0;
|
---|
5797 | pDisk->LCHSGeometry.cHeads = 0;
|
---|
5798 | pDisk->LCHSGeometry.cSectors = 0;
|
---|
5799 | }
|
---|
5800 | else
|
---|
5801 | {
|
---|
5802 | /* Make sure the LCHS geometry is properly clipped. */
|
---|
5803 | pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
|
---|
5804 | pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
|
---|
5805 | }
|
---|
5806 |
|
---|
5807 | if (pDisk->cImages != 0)
|
---|
5808 | {
|
---|
5809 | /* Switch previous image to read-only mode. */
|
---|
5810 | unsigned uOpenFlagsPrevImg;
|
---|
5811 | uOpenFlagsPrevImg = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pBackendData);
|
---|
5812 | if (!(uOpenFlagsPrevImg & VD_OPEN_FLAGS_READONLY))
|
---|
5813 | {
|
---|
5814 | uOpenFlagsPrevImg |= VD_OPEN_FLAGS_READONLY;
|
---|
5815 | rc = pDisk->pLast->Backend->pfnSetOpenFlags(pDisk->pLast->pBackendData, uOpenFlagsPrevImg);
|
---|
5816 | }
|
---|
5817 | }
|
---|
5818 |
|
---|
5819 | if (RT_SUCCESS(rc))
|
---|
5820 | {
|
---|
5821 | /* Image successfully opened, make it the last image. */
|
---|
5822 | vdAddImageToList(pDisk, pImage);
|
---|
5823 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
5824 | pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
|
---|
5825 | }
|
---|
5826 | else
|
---|
5827 | {
|
---|
5828 | /* Error detected, but image opened. Close image. */
|
---|
5829 | rc2 = pImage->Backend->pfnClose(pImage->pBackendData, false);
|
---|
5830 | AssertRC(rc2);
|
---|
5831 | pImage->pBackendData = NULL;
|
---|
5832 | }
|
---|
5833 | } while (0);
|
---|
5834 |
|
---|
5835 | if (RT_UNLIKELY(fLockWrite))
|
---|
5836 | {
|
---|
5837 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
5838 | AssertRC(rc2);
|
---|
5839 | }
|
---|
5840 |
|
---|
5841 | if (RT_FAILURE(rc))
|
---|
5842 | {
|
---|
5843 | if (pImage)
|
---|
5844 | {
|
---|
5845 | if (pImage->pszFilename)
|
---|
5846 | RTStrFree(pImage->pszFilename);
|
---|
5847 | RTMemFree(pImage);
|
---|
5848 | }
|
---|
5849 | }
|
---|
5850 |
|
---|
5851 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
5852 | return rc;
|
---|
5853 | }
|
---|
5854 |
|
---|
5855 | /**
|
---|
5856 | * Opens a cache image.
|
---|
5857 | *
|
---|
5858 | * @return VBox status code.
|
---|
5859 | * @param pDisk Pointer to the HDD container which should use the cache image.
|
---|
5860 | * @param pszBackend Name of the cache file backend to use (case insensitive).
|
---|
5861 | * @param pszFilename Name of the cache image to open.
|
---|
5862 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
5863 | * @param pVDIfsCache Pointer to the per-cache VD interface list.
|
---|
5864 | */
|
---|
5865 | VBOXDDU_DECL(int) VDCacheOpen(PVBOXHDD pDisk, const char *pszBackend,
|
---|
5866 | const char *pszFilename, unsigned uOpenFlags,
|
---|
5867 | PVDINTERFACE pVDIfsCache)
|
---|
5868 | {
|
---|
5869 | int rc = VINF_SUCCESS;
|
---|
5870 | int rc2;
|
---|
5871 | bool fLockWrite = false;
|
---|
5872 | PVDCACHE pCache = NULL;
|
---|
5873 |
|
---|
5874 | LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uOpenFlags=%#x, pVDIfsCache=%#p\n",
|
---|
5875 | pDisk, pszBackend, pszFilename, uOpenFlags, pVDIfsCache));
|
---|
5876 |
|
---|
5877 | do
|
---|
5878 | {
|
---|
5879 | /* sanity check */
|
---|
5880 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
5881 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
5882 |
|
---|
5883 | /* Check arguments. */
|
---|
5884 | AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
|
---|
5885 | ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
|
---|
5886 | rc = VERR_INVALID_PARAMETER);
|
---|
5887 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
5888 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
5889 | rc = VERR_INVALID_PARAMETER);
|
---|
5890 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
5891 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
5892 | rc = VERR_INVALID_PARAMETER);
|
---|
5893 |
|
---|
5894 | /* Set up image descriptor. */
|
---|
5895 | pCache = (PVDCACHE)RTMemAllocZ(sizeof(VDCACHE));
|
---|
5896 | if (!pCache)
|
---|
5897 | {
|
---|
5898 | rc = VERR_NO_MEMORY;
|
---|
5899 | break;
|
---|
5900 | }
|
---|
5901 | pCache->pszFilename = RTStrDup(pszFilename);
|
---|
5902 | if (!pCache->pszFilename)
|
---|
5903 | {
|
---|
5904 | rc = VERR_NO_MEMORY;
|
---|
5905 | break;
|
---|
5906 | }
|
---|
5907 |
|
---|
5908 | pCache->VDIo.pDisk = pDisk;
|
---|
5909 | pCache->pVDIfsCache = pVDIfsCache;
|
---|
5910 |
|
---|
5911 | rc = vdFindCacheBackend(pszBackend, &pCache->Backend);
|
---|
5912 | if (RT_FAILURE(rc))
|
---|
5913 | break;
|
---|
5914 | if (!pCache->Backend)
|
---|
5915 | {
|
---|
5916 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
5917 | N_("VD: unknown backend name '%s'"), pszBackend);
|
---|
5918 | break;
|
---|
5919 | }
|
---|
5920 |
|
---|
5921 | /* Set up the I/O interface. */
|
---|
5922 | pCache->VDIo.pInterfaceIo = VDIfIoGet(pVDIfsCache);
|
---|
5923 | if (!pCache->VDIo.pInterfaceIo)
|
---|
5924 | {
|
---|
5925 | vdIfIoFallbackCallbacksSetup(&pCache->VDIo.VDIfIo);
|
---|
5926 | rc = VDInterfaceAdd(&pCache->VDIo.VDIfIo.Core, "VD_IO", VDINTERFACETYPE_IO,
|
---|
5927 | pDisk, sizeof(VDINTERFACEIO), &pVDIfsCache);
|
---|
5928 | pCache->VDIo.pInterfaceIo = &pCache->VDIo.VDIfIo;
|
---|
5929 | }
|
---|
5930 |
|
---|
5931 | /* Set up the internal I/O interface. */
|
---|
5932 | AssertBreakStmt(!VDIfIoIntGet(pVDIfsCache), rc = VERR_INVALID_PARAMETER);
|
---|
5933 | vdIfIoIntCallbacksSetup(&pCache->VDIo.VDIfIoInt);
|
---|
5934 | rc = VDInterfaceAdd(&pCache->VDIo.VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
5935 | &pCache->VDIo, sizeof(VDINTERFACEIOINT), &pCache->pVDIfsCache);
|
---|
5936 | AssertRC(rc);
|
---|
5937 |
|
---|
5938 | pCache->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
|
---|
5939 | rc = pCache->Backend->pfnOpen(pCache->pszFilename,
|
---|
5940 | uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
|
---|
5941 | pDisk->pVDIfsDisk,
|
---|
5942 | pCache->pVDIfsCache,
|
---|
5943 | &pCache->pBackendData);
|
---|
5944 | /* If the open in read-write mode failed, retry in read-only mode. */
|
---|
5945 | if (RT_FAILURE(rc))
|
---|
5946 | {
|
---|
5947 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
5948 | && ( rc == VERR_ACCESS_DENIED
|
---|
5949 | || rc == VERR_PERMISSION_DENIED
|
---|
5950 | || rc == VERR_WRITE_PROTECT
|
---|
5951 | || rc == VERR_SHARING_VIOLATION
|
---|
5952 | || rc == VERR_FILE_LOCK_FAILED))
|
---|
5953 | rc = pCache->Backend->pfnOpen(pCache->pszFilename,
|
---|
5954 | (uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME)
|
---|
5955 | | VD_OPEN_FLAGS_READONLY,
|
---|
5956 | pDisk->pVDIfsDisk,
|
---|
5957 | pCache->pVDIfsCache,
|
---|
5958 | &pCache->pBackendData);
|
---|
5959 | if (RT_FAILURE(rc))
|
---|
5960 | {
|
---|
5961 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
5962 | N_("VD: error %Rrc opening image file '%s'"), rc, pszFilename);
|
---|
5963 | break;
|
---|
5964 | }
|
---|
5965 | }
|
---|
5966 |
|
---|
5967 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
5968 | rc2 = vdThreadStartWrite(pDisk);
|
---|
5969 | AssertRC(rc2);
|
---|
5970 | fLockWrite = true;
|
---|
5971 |
|
---|
5972 | /*
|
---|
5973 | * Check that the modification UUID of the cache and last image
|
---|
5974 | * match. If not the image was modified in-between without the cache.
|
---|
5975 | * The cache might contain stale data.
|
---|
5976 | */
|
---|
5977 | RTUUID UuidImage, UuidCache;
|
---|
5978 |
|
---|
5979 | rc = pCache->Backend->pfnGetModificationUuid(pCache->pBackendData,
|
---|
5980 | &UuidCache);
|
---|
5981 | if (RT_SUCCESS(rc))
|
---|
5982 | {
|
---|
5983 | rc = pDisk->pLast->Backend->pfnGetModificationUuid(pDisk->pLast->pBackendData,
|
---|
5984 | &UuidImage);
|
---|
5985 | if (RT_SUCCESS(rc))
|
---|
5986 | {
|
---|
5987 | if (RTUuidCompare(&UuidImage, &UuidCache))
|
---|
5988 | rc = VERR_VD_CACHE_NOT_UP_TO_DATE;
|
---|
5989 | }
|
---|
5990 | }
|
---|
5991 |
|
---|
5992 | /*
|
---|
5993 | * We assume that the user knows what he is doing if one of the images
|
---|
5994 | * doesn't support the modification uuid.
|
---|
5995 | */
|
---|
5996 | if (rc == VERR_NOT_SUPPORTED)
|
---|
5997 | rc = VINF_SUCCESS;
|
---|
5998 |
|
---|
5999 | if (RT_SUCCESS(rc))
|
---|
6000 | {
|
---|
6001 | /* Cache successfully opened, make it the current one. */
|
---|
6002 | if (!pDisk->pCache)
|
---|
6003 | pDisk->pCache = pCache;
|
---|
6004 | else
|
---|
6005 | rc = VERR_VD_CACHE_ALREADY_EXISTS;
|
---|
6006 | }
|
---|
6007 |
|
---|
6008 | if (RT_FAILURE(rc))
|
---|
6009 | {
|
---|
6010 | /* Error detected, but image opened. Close image. */
|
---|
6011 | rc2 = pCache->Backend->pfnClose(pCache->pBackendData, false);
|
---|
6012 | AssertRC(rc2);
|
---|
6013 | pCache->pBackendData = NULL;
|
---|
6014 | }
|
---|
6015 | } while (0);
|
---|
6016 |
|
---|
6017 | if (RT_UNLIKELY(fLockWrite))
|
---|
6018 | {
|
---|
6019 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6020 | AssertRC(rc2);
|
---|
6021 | }
|
---|
6022 |
|
---|
6023 | if (RT_FAILURE(rc))
|
---|
6024 | {
|
---|
6025 | if (pCache)
|
---|
6026 | {
|
---|
6027 | if (pCache->pszFilename)
|
---|
6028 | RTStrFree(pCache->pszFilename);
|
---|
6029 | RTMemFree(pCache);
|
---|
6030 | }
|
---|
6031 | }
|
---|
6032 |
|
---|
6033 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6034 | return rc;
|
---|
6035 | }
|
---|
6036 |
|
---|
6037 | /**
|
---|
6038 | * Creates and opens a new base image file.
|
---|
6039 | *
|
---|
6040 | * @returns VBox status code.
|
---|
6041 | * @param pDisk Pointer to HDD container.
|
---|
6042 | * @param pszBackend Name of the image file backend to use.
|
---|
6043 | * @param pszFilename Name of the image file to create.
|
---|
6044 | * @param cbSize Image size in bytes.
|
---|
6045 | * @param uImageFlags Flags specifying special image features.
|
---|
6046 | * @param pszComment Pointer to image comment. NULL is ok.
|
---|
6047 | * @param pPCHSGeometry Pointer to physical disk geometry <= (16383,16,63). Not NULL.
|
---|
6048 | * @param pLCHSGeometry Pointer to logical disk geometry <= (x,255,63). Not NULL.
|
---|
6049 | * @param pUuid New UUID of the image. If NULL, a new UUID is created.
|
---|
6050 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
6051 | * @param pVDIfsImage Pointer to the per-image VD interface list.
|
---|
6052 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
6053 | */
|
---|
6054 | VBOXDDU_DECL(int) VDCreateBase(PVBOXHDD pDisk, const char *pszBackend,
|
---|
6055 | const char *pszFilename, uint64_t cbSize,
|
---|
6056 | unsigned uImageFlags, const char *pszComment,
|
---|
6057 | PCVDGEOMETRY pPCHSGeometry,
|
---|
6058 | PCVDGEOMETRY pLCHSGeometry,
|
---|
6059 | PCRTUUID pUuid, unsigned uOpenFlags,
|
---|
6060 | PVDINTERFACE pVDIfsImage,
|
---|
6061 | PVDINTERFACE pVDIfsOperation)
|
---|
6062 | {
|
---|
6063 | int rc = VINF_SUCCESS;
|
---|
6064 | int rc2;
|
---|
6065 | bool fLockWrite = false, fLockRead = false;
|
---|
6066 | PVDIMAGE pImage = NULL;
|
---|
6067 | RTUUID uuid;
|
---|
6068 |
|
---|
6069 | 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",
|
---|
6070 | pDisk, pszBackend, pszFilename, cbSize, uImageFlags, pszComment,
|
---|
6071 | pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
|
---|
6072 | pPCHSGeometry->cSectors, pLCHSGeometry->cCylinders,
|
---|
6073 | pLCHSGeometry->cHeads, pLCHSGeometry->cSectors, pUuid,
|
---|
6074 | uOpenFlags, pVDIfsImage, pVDIfsOperation));
|
---|
6075 |
|
---|
6076 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
6077 |
|
---|
6078 | do
|
---|
6079 | {
|
---|
6080 | /* sanity check */
|
---|
6081 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
6082 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6083 |
|
---|
6084 | /* Check arguments. */
|
---|
6085 | AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
|
---|
6086 | ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
|
---|
6087 | rc = VERR_INVALID_PARAMETER);
|
---|
6088 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
6089 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
6090 | rc = VERR_INVALID_PARAMETER);
|
---|
6091 | AssertMsgBreakStmt(cbSize,
|
---|
6092 | ("cbSize=%llu\n", cbSize),
|
---|
6093 | rc = VERR_INVALID_PARAMETER);
|
---|
6094 | AssertMsgBreakStmt( ((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0)
|
---|
6095 | || ((uImageFlags & (VD_IMAGE_FLAGS_FIXED | VD_IMAGE_FLAGS_DIFF)) != VD_IMAGE_FLAGS_FIXED),
|
---|
6096 | ("uImageFlags=%#x\n", uImageFlags),
|
---|
6097 | rc = VERR_INVALID_PARAMETER);
|
---|
6098 | /* The PCHS geometry fields may be 0 to leave it for later. */
|
---|
6099 | AssertMsgBreakStmt( VALID_PTR(pPCHSGeometry)
|
---|
6100 | && pPCHSGeometry->cHeads <= 16
|
---|
6101 | && pPCHSGeometry->cSectors <= 63,
|
---|
6102 | ("pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pPCHSGeometry,
|
---|
6103 | pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
|
---|
6104 | pPCHSGeometry->cSectors),
|
---|
6105 | rc = VERR_INVALID_PARAMETER);
|
---|
6106 | /* The LCHS geometry fields may be 0 to leave it to later autodetection. */
|
---|
6107 | AssertMsgBreakStmt( VALID_PTR(pLCHSGeometry)
|
---|
6108 | && pLCHSGeometry->cHeads <= 255
|
---|
6109 | && pLCHSGeometry->cSectors <= 63,
|
---|
6110 | ("pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pLCHSGeometry,
|
---|
6111 | pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads,
|
---|
6112 | pLCHSGeometry->cSectors),
|
---|
6113 | rc = VERR_INVALID_PARAMETER);
|
---|
6114 | /* The UUID may be NULL. */
|
---|
6115 | AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
|
---|
6116 | ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
|
---|
6117 | rc = VERR_INVALID_PARAMETER);
|
---|
6118 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
6119 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
6120 | rc = VERR_INVALID_PARAMETER);
|
---|
6121 |
|
---|
6122 | /* Check state. Needs a temporary read lock. Holding the write lock
|
---|
6123 | * all the time would be blocking other activities for too long. */
|
---|
6124 | rc2 = vdThreadStartRead(pDisk);
|
---|
6125 | AssertRC(rc2);
|
---|
6126 | fLockRead = true;
|
---|
6127 | AssertMsgBreakStmt(pDisk->cImages == 0,
|
---|
6128 | ("Create base image cannot be done with other images open\n"),
|
---|
6129 | rc = VERR_VD_INVALID_STATE);
|
---|
6130 | rc2 = vdThreadFinishRead(pDisk);
|
---|
6131 | AssertRC(rc2);
|
---|
6132 | fLockRead = false;
|
---|
6133 |
|
---|
6134 | /* Set up image descriptor. */
|
---|
6135 | pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
|
---|
6136 | if (!pImage)
|
---|
6137 | {
|
---|
6138 | rc = VERR_NO_MEMORY;
|
---|
6139 | break;
|
---|
6140 | }
|
---|
6141 | pImage->pszFilename = RTStrDup(pszFilename);
|
---|
6142 | if (!pImage->pszFilename)
|
---|
6143 | {
|
---|
6144 | rc = VERR_NO_MEMORY;
|
---|
6145 | break;
|
---|
6146 | }
|
---|
6147 | pImage->VDIo.pDisk = pDisk;
|
---|
6148 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
6149 |
|
---|
6150 | /* Set up the I/O interface. */
|
---|
6151 | pImage->VDIo.pInterfaceIo = VDIfIoGet(pVDIfsImage);
|
---|
6152 | if (!pImage->VDIo.pInterfaceIo)
|
---|
6153 | {
|
---|
6154 | vdIfIoFallbackCallbacksSetup(&pImage->VDIo.VDIfIo);
|
---|
6155 | rc = VDInterfaceAdd(&pImage->VDIo.VDIfIo.Core, "VD_IO", VDINTERFACETYPE_IO,
|
---|
6156 | pDisk, sizeof(VDINTERFACEIO), &pVDIfsImage);
|
---|
6157 | pImage->VDIo.pInterfaceIo = &pImage->VDIo.VDIfIo;
|
---|
6158 | }
|
---|
6159 |
|
---|
6160 | /* Set up the internal I/O interface. */
|
---|
6161 | AssertBreakStmt(!VDIfIoIntGet(pVDIfsImage), rc = VERR_INVALID_PARAMETER);
|
---|
6162 | vdIfIoIntCallbacksSetup(&pImage->VDIo.VDIfIoInt);
|
---|
6163 | rc = VDInterfaceAdd(&pImage->VDIo.VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
6164 | &pImage->VDIo, sizeof(VDINTERFACEIOINT), &pImage->pVDIfsImage);
|
---|
6165 | AssertRC(rc);
|
---|
6166 |
|
---|
6167 | rc = vdFindBackend(pszBackend, &pImage->Backend);
|
---|
6168 | if (RT_FAILURE(rc))
|
---|
6169 | break;
|
---|
6170 | if (!pImage->Backend)
|
---|
6171 | {
|
---|
6172 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
6173 | N_("VD: unknown backend name '%s'"), pszBackend);
|
---|
6174 | break;
|
---|
6175 | }
|
---|
6176 | if (!(pImage->Backend->uBackendCaps & ( VD_CAP_CREATE_FIXED
|
---|
6177 | | VD_CAP_CREATE_DYNAMIC)))
|
---|
6178 | {
|
---|
6179 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
6180 | N_("VD: backend '%s' cannot create base images"), pszBackend);
|
---|
6181 | break;
|
---|
6182 | }
|
---|
6183 |
|
---|
6184 | /* Create UUID if the caller didn't specify one. */
|
---|
6185 | if (!pUuid)
|
---|
6186 | {
|
---|
6187 | rc = RTUuidCreate(&uuid);
|
---|
6188 | if (RT_FAILURE(rc))
|
---|
6189 | {
|
---|
6190 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
6191 | N_("VD: cannot generate UUID for image '%s'"),
|
---|
6192 | pszFilename);
|
---|
6193 | break;
|
---|
6194 | }
|
---|
6195 | pUuid = &uuid;
|
---|
6196 | }
|
---|
6197 |
|
---|
6198 | pImage->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
|
---|
6199 | uImageFlags &= ~VD_IMAGE_FLAGS_DIFF;
|
---|
6200 | pImage->VDIo.fIgnoreFlush = (uOpenFlags & VD_OPEN_FLAGS_IGNORE_FLUSH) != 0;
|
---|
6201 | rc = pImage->Backend->pfnCreate(pImage->pszFilename, cbSize,
|
---|
6202 | uImageFlags, pszComment, pPCHSGeometry,
|
---|
6203 | pLCHSGeometry, pUuid,
|
---|
6204 | uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
|
---|
6205 | 0, 99,
|
---|
6206 | pDisk->pVDIfsDisk,
|
---|
6207 | pImage->pVDIfsImage,
|
---|
6208 | pVDIfsOperation,
|
---|
6209 | &pImage->pBackendData);
|
---|
6210 |
|
---|
6211 | if (RT_SUCCESS(rc))
|
---|
6212 | {
|
---|
6213 | pImage->VDIo.pBackendData = pImage->pBackendData;
|
---|
6214 | pImage->uImageFlags = uImageFlags;
|
---|
6215 |
|
---|
6216 | /* Force sane optimization settings. It's not worth avoiding writes
|
---|
6217 | * to fixed size images. The overhead would have almost no payback. */
|
---|
6218 | if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
|
---|
6219 | pImage->uOpenFlags |= VD_OPEN_FLAGS_HONOR_SAME;
|
---|
6220 |
|
---|
6221 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
6222 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6223 | AssertRC(rc2);
|
---|
6224 | fLockWrite = true;
|
---|
6225 |
|
---|
6226 | /** @todo optionally check UUIDs */
|
---|
6227 |
|
---|
6228 | /* Re-check state, as the lock wasn't held and another image
|
---|
6229 | * creation call could have been done by another thread. */
|
---|
6230 | AssertMsgStmt(pDisk->cImages == 0,
|
---|
6231 | ("Create base image cannot be done with other images open\n"),
|
---|
6232 | rc = VERR_VD_INVALID_STATE);
|
---|
6233 | }
|
---|
6234 |
|
---|
6235 | if (RT_SUCCESS(rc))
|
---|
6236 | {
|
---|
6237 | /* Cache disk information. */
|
---|
6238 | pDisk->cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
|
---|
6239 |
|
---|
6240 | /* Cache PCHS geometry. */
|
---|
6241 | rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
6242 | &pDisk->PCHSGeometry);
|
---|
6243 | if (RT_FAILURE(rc2))
|
---|
6244 | {
|
---|
6245 | pDisk->PCHSGeometry.cCylinders = 0;
|
---|
6246 | pDisk->PCHSGeometry.cHeads = 0;
|
---|
6247 | pDisk->PCHSGeometry.cSectors = 0;
|
---|
6248 | }
|
---|
6249 | else
|
---|
6250 | {
|
---|
6251 | /* Make sure the CHS geometry is properly clipped. */
|
---|
6252 | pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
|
---|
6253 | pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
|
---|
6254 | pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
|
---|
6255 | }
|
---|
6256 |
|
---|
6257 | /* Cache LCHS geometry. */
|
---|
6258 | rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
6259 | &pDisk->LCHSGeometry);
|
---|
6260 | if (RT_FAILURE(rc2))
|
---|
6261 | {
|
---|
6262 | pDisk->LCHSGeometry.cCylinders = 0;
|
---|
6263 | pDisk->LCHSGeometry.cHeads = 0;
|
---|
6264 | pDisk->LCHSGeometry.cSectors = 0;
|
---|
6265 | }
|
---|
6266 | else
|
---|
6267 | {
|
---|
6268 | /* Make sure the CHS geometry is properly clipped. */
|
---|
6269 | pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
|
---|
6270 | pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
|
---|
6271 | }
|
---|
6272 |
|
---|
6273 | /* Image successfully opened, make it the last image. */
|
---|
6274 | vdAddImageToList(pDisk, pImage);
|
---|
6275 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
6276 | pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
|
---|
6277 | }
|
---|
6278 | else
|
---|
6279 | {
|
---|
6280 | /* Error detected, image may or may not be opened. Close and delete
|
---|
6281 | * image if it was opened. */
|
---|
6282 | if (pImage->pBackendData)
|
---|
6283 | {
|
---|
6284 | rc2 = pImage->Backend->pfnClose(pImage->pBackendData, true);
|
---|
6285 | AssertRC(rc2);
|
---|
6286 | pImage->pBackendData = NULL;
|
---|
6287 | }
|
---|
6288 | }
|
---|
6289 | } while (0);
|
---|
6290 |
|
---|
6291 | if (RT_UNLIKELY(fLockWrite))
|
---|
6292 | {
|
---|
6293 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6294 | AssertRC(rc2);
|
---|
6295 | }
|
---|
6296 | else if (RT_UNLIKELY(fLockRead))
|
---|
6297 | {
|
---|
6298 | rc2 = vdThreadFinishRead(pDisk);
|
---|
6299 | AssertRC(rc2);
|
---|
6300 | }
|
---|
6301 |
|
---|
6302 | if (RT_FAILURE(rc))
|
---|
6303 | {
|
---|
6304 | if (pImage)
|
---|
6305 | {
|
---|
6306 | if (pImage->pszFilename)
|
---|
6307 | RTStrFree(pImage->pszFilename);
|
---|
6308 | RTMemFree(pImage);
|
---|
6309 | }
|
---|
6310 | }
|
---|
6311 |
|
---|
6312 | if (RT_SUCCESS(rc) && pIfProgress && pIfProgress->pfnProgress)
|
---|
6313 | pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
|
---|
6314 |
|
---|
6315 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6316 | return rc;
|
---|
6317 | }
|
---|
6318 |
|
---|
6319 | /**
|
---|
6320 | * Creates and opens a new differencing image file in HDD container.
|
---|
6321 | * See comments for VDOpen function about differencing images.
|
---|
6322 | *
|
---|
6323 | * @returns VBox status code.
|
---|
6324 | * @param pDisk Pointer to HDD container.
|
---|
6325 | * @param pszBackend Name of the image file backend to use.
|
---|
6326 | * @param pszFilename Name of the differencing image file to create.
|
---|
6327 | * @param uImageFlags Flags specifying special image features.
|
---|
6328 | * @param pszComment Pointer to image comment. NULL is ok.
|
---|
6329 | * @param pUuid New UUID of the image. If NULL, a new UUID is created.
|
---|
6330 | * @param pParentUuid New parent UUID of the image. If NULL, the UUID is queried automatically.
|
---|
6331 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
6332 | * @param pVDIfsImage Pointer to the per-image VD interface list.
|
---|
6333 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
6334 | */
|
---|
6335 | VBOXDDU_DECL(int) VDCreateDiff(PVBOXHDD pDisk, const char *pszBackend,
|
---|
6336 | const char *pszFilename, unsigned uImageFlags,
|
---|
6337 | const char *pszComment, PCRTUUID pUuid,
|
---|
6338 | PCRTUUID pParentUuid, unsigned uOpenFlags,
|
---|
6339 | PVDINTERFACE pVDIfsImage,
|
---|
6340 | PVDINTERFACE pVDIfsOperation)
|
---|
6341 | {
|
---|
6342 | int rc = VINF_SUCCESS;
|
---|
6343 | int rc2;
|
---|
6344 | bool fLockWrite = false, fLockRead = false;
|
---|
6345 | PVDIMAGE pImage = NULL;
|
---|
6346 | RTUUID uuid;
|
---|
6347 |
|
---|
6348 | LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" uImageFlags=%#x pszComment=\"%s\" Uuid=%RTuuid uOpenFlags=%#x pVDIfsImage=%#p pVDIfsOperation=%#p\n",
|
---|
6349 | pDisk, pszBackend, pszFilename, uImageFlags, pszComment, pUuid, uOpenFlags, pVDIfsImage, pVDIfsOperation));
|
---|
6350 |
|
---|
6351 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
6352 |
|
---|
6353 | do
|
---|
6354 | {
|
---|
6355 | /* sanity check */
|
---|
6356 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
6357 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6358 |
|
---|
6359 | /* Check arguments. */
|
---|
6360 | AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
|
---|
6361 | ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
|
---|
6362 | rc = VERR_INVALID_PARAMETER);
|
---|
6363 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
6364 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
6365 | rc = VERR_INVALID_PARAMETER);
|
---|
6366 | AssertMsgBreakStmt((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0,
|
---|
6367 | ("uImageFlags=%#x\n", uImageFlags),
|
---|
6368 | rc = VERR_INVALID_PARAMETER);
|
---|
6369 | /* The UUID may be NULL. */
|
---|
6370 | AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
|
---|
6371 | ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
|
---|
6372 | rc = VERR_INVALID_PARAMETER);
|
---|
6373 | /* The parent UUID may be NULL. */
|
---|
6374 | AssertMsgBreakStmt(pParentUuid == NULL || VALID_PTR(pParentUuid),
|
---|
6375 | ("pParentUuid=%#p ParentUUID=%RTuuid\n", pParentUuid, pParentUuid),
|
---|
6376 | rc = VERR_INVALID_PARAMETER);
|
---|
6377 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
6378 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
6379 | rc = VERR_INVALID_PARAMETER);
|
---|
6380 |
|
---|
6381 | /* Check state. Needs a temporary read lock. Holding the write lock
|
---|
6382 | * all the time would be blocking other activities for too long. */
|
---|
6383 | rc2 = vdThreadStartRead(pDisk);
|
---|
6384 | AssertRC(rc2);
|
---|
6385 | fLockRead = true;
|
---|
6386 | AssertMsgBreakStmt(pDisk->cImages != 0,
|
---|
6387 | ("Create diff image cannot be done without other images open\n"),
|
---|
6388 | rc = VERR_VD_INVALID_STATE);
|
---|
6389 | rc2 = vdThreadFinishRead(pDisk);
|
---|
6390 | AssertRC(rc2);
|
---|
6391 | fLockRead = false;
|
---|
6392 |
|
---|
6393 | /*
|
---|
6394 | * Destroy the current discard state first which might still have pending blocks
|
---|
6395 | * for the currently opened image which will be switched to readonly mode.
|
---|
6396 | */
|
---|
6397 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
6398 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6399 | AssertRC(rc2);
|
---|
6400 | fLockWrite = true;
|
---|
6401 | rc = vdDiscardStateDestroy(pDisk);
|
---|
6402 | if (RT_FAILURE(rc))
|
---|
6403 | break;
|
---|
6404 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6405 | AssertRC(rc2);
|
---|
6406 | fLockWrite = false;
|
---|
6407 |
|
---|
6408 | /* Set up image descriptor. */
|
---|
6409 | pImage = (PVDIMAGE)RTMemAllocZ(sizeof(VDIMAGE));
|
---|
6410 | if (!pImage)
|
---|
6411 | {
|
---|
6412 | rc = VERR_NO_MEMORY;
|
---|
6413 | break;
|
---|
6414 | }
|
---|
6415 | pImage->pszFilename = RTStrDup(pszFilename);
|
---|
6416 | if (!pImage->pszFilename)
|
---|
6417 | {
|
---|
6418 | rc = VERR_NO_MEMORY;
|
---|
6419 | break;
|
---|
6420 | }
|
---|
6421 |
|
---|
6422 | rc = vdFindBackend(pszBackend, &pImage->Backend);
|
---|
6423 | if (RT_FAILURE(rc))
|
---|
6424 | break;
|
---|
6425 | if (!pImage->Backend)
|
---|
6426 | {
|
---|
6427 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
6428 | N_("VD: unknown backend name '%s'"), pszBackend);
|
---|
6429 | break;
|
---|
6430 | }
|
---|
6431 | if ( !(pImage->Backend->uBackendCaps & VD_CAP_DIFF)
|
---|
6432 | || !(pImage->Backend->uBackendCaps & ( VD_CAP_CREATE_FIXED
|
---|
6433 | | VD_CAP_CREATE_DYNAMIC)))
|
---|
6434 | {
|
---|
6435 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
6436 | N_("VD: backend '%s' cannot create diff images"), pszBackend);
|
---|
6437 | break;
|
---|
6438 | }
|
---|
6439 |
|
---|
6440 | pImage->VDIo.pDisk = pDisk;
|
---|
6441 | pImage->pVDIfsImage = pVDIfsImage;
|
---|
6442 |
|
---|
6443 | /* Set up the I/O interface. */
|
---|
6444 | pImage->VDIo.pInterfaceIo = VDIfIoGet(pVDIfsImage);
|
---|
6445 | if (!pImage->VDIo.pInterfaceIo)
|
---|
6446 | {
|
---|
6447 | vdIfIoFallbackCallbacksSetup(&pImage->VDIo.VDIfIo);
|
---|
6448 | rc = VDInterfaceAdd(&pImage->VDIo.VDIfIo.Core, "VD_IO", VDINTERFACETYPE_IO,
|
---|
6449 | pDisk, sizeof(VDINTERFACEIO), &pVDIfsImage);
|
---|
6450 | pImage->VDIo.pInterfaceIo = &pImage->VDIo.VDIfIo;
|
---|
6451 | }
|
---|
6452 |
|
---|
6453 | /* Set up the internal I/O interface. */
|
---|
6454 | AssertBreakStmt(!VDIfIoIntGet(pVDIfsImage), rc = VERR_INVALID_PARAMETER);
|
---|
6455 | vdIfIoIntCallbacksSetup(&pImage->VDIo.VDIfIoInt);
|
---|
6456 | rc = VDInterfaceAdd(&pImage->VDIo.VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
6457 | &pImage->VDIo, sizeof(VDINTERFACEIOINT), &pImage->pVDIfsImage);
|
---|
6458 | AssertRC(rc);
|
---|
6459 |
|
---|
6460 | /* Create UUID if the caller didn't specify one. */
|
---|
6461 | if (!pUuid)
|
---|
6462 | {
|
---|
6463 | rc = RTUuidCreate(&uuid);
|
---|
6464 | if (RT_FAILURE(rc))
|
---|
6465 | {
|
---|
6466 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
6467 | N_("VD: cannot generate UUID for image '%s'"),
|
---|
6468 | pszFilename);
|
---|
6469 | break;
|
---|
6470 | }
|
---|
6471 | pUuid = &uuid;
|
---|
6472 | }
|
---|
6473 |
|
---|
6474 | pImage->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
|
---|
6475 | pImage->VDIo.fIgnoreFlush = (uOpenFlags & VD_OPEN_FLAGS_IGNORE_FLUSH) != 0;
|
---|
6476 | uImageFlags |= VD_IMAGE_FLAGS_DIFF;
|
---|
6477 | rc = pImage->Backend->pfnCreate(pImage->pszFilename, pDisk->cbSize,
|
---|
6478 | uImageFlags | VD_IMAGE_FLAGS_DIFF,
|
---|
6479 | pszComment, &pDisk->PCHSGeometry,
|
---|
6480 | &pDisk->LCHSGeometry, pUuid,
|
---|
6481 | uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
|
---|
6482 | 0, 99,
|
---|
6483 | pDisk->pVDIfsDisk,
|
---|
6484 | pImage->pVDIfsImage,
|
---|
6485 | pVDIfsOperation,
|
---|
6486 | &pImage->pBackendData);
|
---|
6487 |
|
---|
6488 | if (RT_SUCCESS(rc))
|
---|
6489 | {
|
---|
6490 | pImage->VDIo.pBackendData = pImage->pBackendData;
|
---|
6491 | pImage->uImageFlags = uImageFlags;
|
---|
6492 |
|
---|
6493 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
6494 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6495 | AssertRC(rc2);
|
---|
6496 | fLockWrite = true;
|
---|
6497 |
|
---|
6498 | /* Switch previous image to read-only mode. */
|
---|
6499 | unsigned uOpenFlagsPrevImg;
|
---|
6500 | uOpenFlagsPrevImg = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pBackendData);
|
---|
6501 | if (!(uOpenFlagsPrevImg & VD_OPEN_FLAGS_READONLY))
|
---|
6502 | {
|
---|
6503 | uOpenFlagsPrevImg |= VD_OPEN_FLAGS_READONLY;
|
---|
6504 | rc = pDisk->pLast->Backend->pfnSetOpenFlags(pDisk->pLast->pBackendData, uOpenFlagsPrevImg);
|
---|
6505 | }
|
---|
6506 |
|
---|
6507 | /** @todo optionally check UUIDs */
|
---|
6508 |
|
---|
6509 | /* Re-check state, as the lock wasn't held and another image
|
---|
6510 | * creation call could have been done by another thread. */
|
---|
6511 | AssertMsgStmt(pDisk->cImages != 0,
|
---|
6512 | ("Create diff image cannot be done without other images open\n"),
|
---|
6513 | rc = VERR_VD_INVALID_STATE);
|
---|
6514 | }
|
---|
6515 |
|
---|
6516 | if (RT_SUCCESS(rc))
|
---|
6517 | {
|
---|
6518 | RTUUID Uuid;
|
---|
6519 | RTTIMESPEC ts;
|
---|
6520 |
|
---|
6521 | if (pParentUuid && !RTUuidIsNull(pParentUuid))
|
---|
6522 | {
|
---|
6523 | Uuid = *pParentUuid;
|
---|
6524 | pImage->Backend->pfnSetParentUuid(pImage->pBackendData, &Uuid);
|
---|
6525 | }
|
---|
6526 | else
|
---|
6527 | {
|
---|
6528 | rc2 = pDisk->pLast->Backend->pfnGetUuid(pDisk->pLast->pBackendData,
|
---|
6529 | &Uuid);
|
---|
6530 | if (RT_SUCCESS(rc2))
|
---|
6531 | pImage->Backend->pfnSetParentUuid(pImage->pBackendData, &Uuid);
|
---|
6532 | }
|
---|
6533 | rc2 = pDisk->pLast->Backend->pfnGetModificationUuid(pDisk->pLast->pBackendData,
|
---|
6534 | &Uuid);
|
---|
6535 | if (RT_SUCCESS(rc2))
|
---|
6536 | pImage->Backend->pfnSetParentModificationUuid(pImage->pBackendData,
|
---|
6537 | &Uuid);
|
---|
6538 | if (pDisk->pLast->Backend->pfnGetTimeStamp)
|
---|
6539 | rc2 = pDisk->pLast->Backend->pfnGetTimeStamp(pDisk->pLast->pBackendData,
|
---|
6540 | &ts);
|
---|
6541 | else
|
---|
6542 | rc2 = VERR_NOT_IMPLEMENTED;
|
---|
6543 | if (RT_SUCCESS(rc2) && pImage->Backend->pfnSetParentTimeStamp)
|
---|
6544 | pImage->Backend->pfnSetParentTimeStamp(pImage->pBackendData, &ts);
|
---|
6545 |
|
---|
6546 | if (pImage->Backend->pfnSetParentFilename)
|
---|
6547 | rc2 = pImage->Backend->pfnSetParentFilename(pImage->pBackendData, pDisk->pLast->pszFilename);
|
---|
6548 | }
|
---|
6549 |
|
---|
6550 | if (RT_SUCCESS(rc))
|
---|
6551 | {
|
---|
6552 | /* Image successfully opened, make it the last image. */
|
---|
6553 | vdAddImageToList(pDisk, pImage);
|
---|
6554 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
6555 | pDisk->uModified = VD_IMAGE_MODIFIED_FIRST;
|
---|
6556 | }
|
---|
6557 | else
|
---|
6558 | {
|
---|
6559 | /* Error detected, but image opened. Close and delete image. */
|
---|
6560 | rc2 = pImage->Backend->pfnClose(pImage->pBackendData, true);
|
---|
6561 | AssertRC(rc2);
|
---|
6562 | pImage->pBackendData = NULL;
|
---|
6563 | }
|
---|
6564 | } while (0);
|
---|
6565 |
|
---|
6566 | if (RT_UNLIKELY(fLockWrite))
|
---|
6567 | {
|
---|
6568 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6569 | AssertRC(rc2);
|
---|
6570 | }
|
---|
6571 | else if (RT_UNLIKELY(fLockRead))
|
---|
6572 | {
|
---|
6573 | rc2 = vdThreadFinishRead(pDisk);
|
---|
6574 | AssertRC(rc2);
|
---|
6575 | }
|
---|
6576 |
|
---|
6577 | if (RT_FAILURE(rc))
|
---|
6578 | {
|
---|
6579 | if (pImage)
|
---|
6580 | {
|
---|
6581 | if (pImage->pszFilename)
|
---|
6582 | RTStrFree(pImage->pszFilename);
|
---|
6583 | RTMemFree(pImage);
|
---|
6584 | }
|
---|
6585 | }
|
---|
6586 |
|
---|
6587 | if (RT_SUCCESS(rc) && pIfProgress && pIfProgress->pfnProgress)
|
---|
6588 | pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
|
---|
6589 |
|
---|
6590 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6591 | return rc;
|
---|
6592 | }
|
---|
6593 |
|
---|
6594 |
|
---|
6595 | /**
|
---|
6596 | * Creates and opens new cache image file in HDD container.
|
---|
6597 | *
|
---|
6598 | * @return VBox status code.
|
---|
6599 | * @param pDisk Name of the cache file backend to use (case insensitive).
|
---|
6600 | * @param pszFilename Name of the differencing cache file to create.
|
---|
6601 | * @param cbSize Maximum size of the cache.
|
---|
6602 | * @param uImageFlags Flags specifying special cache features.
|
---|
6603 | * @param pszComment Pointer to image comment. NULL is ok.
|
---|
6604 | * @param pUuid New UUID of the image. If NULL, a new UUID is created.
|
---|
6605 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
6606 | * @param pVDIfsCache Pointer to the per-cache VD interface list.
|
---|
6607 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
6608 | */
|
---|
6609 | VBOXDDU_DECL(int) VDCreateCache(PVBOXHDD pDisk, const char *pszBackend,
|
---|
6610 | const char *pszFilename, uint64_t cbSize,
|
---|
6611 | unsigned uImageFlags, const char *pszComment,
|
---|
6612 | PCRTUUID pUuid, unsigned uOpenFlags,
|
---|
6613 | PVDINTERFACE pVDIfsCache, PVDINTERFACE pVDIfsOperation)
|
---|
6614 | {
|
---|
6615 | int rc = VINF_SUCCESS;
|
---|
6616 | int rc2;
|
---|
6617 | bool fLockWrite = false, fLockRead = false;
|
---|
6618 | PVDCACHE pCache = NULL;
|
---|
6619 | RTUUID uuid;
|
---|
6620 |
|
---|
6621 | LogFlowFunc(("pDisk=%#p pszBackend=\"%s\" pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" Uuid=%RTuuid uOpenFlags=%#x pVDIfsImage=%#p pVDIfsOperation=%#p\n",
|
---|
6622 | pDisk, pszBackend, pszFilename, cbSize, uImageFlags, pszComment, pUuid, uOpenFlags, pVDIfsCache, pVDIfsOperation));
|
---|
6623 |
|
---|
6624 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
6625 |
|
---|
6626 | do
|
---|
6627 | {
|
---|
6628 | /* sanity check */
|
---|
6629 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
6630 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6631 |
|
---|
6632 | /* Check arguments. */
|
---|
6633 | AssertMsgBreakStmt(VALID_PTR(pszBackend) && *pszBackend,
|
---|
6634 | ("pszBackend=%#p \"%s\"\n", pszBackend, pszBackend),
|
---|
6635 | rc = VERR_INVALID_PARAMETER);
|
---|
6636 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
6637 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
6638 | rc = VERR_INVALID_PARAMETER);
|
---|
6639 | AssertMsgBreakStmt(cbSize,
|
---|
6640 | ("cbSize=%llu\n", cbSize),
|
---|
6641 | rc = VERR_INVALID_PARAMETER);
|
---|
6642 | AssertMsgBreakStmt((uImageFlags & ~VD_IMAGE_FLAGS_MASK) == 0,
|
---|
6643 | ("uImageFlags=%#x\n", uImageFlags),
|
---|
6644 | rc = VERR_INVALID_PARAMETER);
|
---|
6645 | /* The UUID may be NULL. */
|
---|
6646 | AssertMsgBreakStmt(pUuid == NULL || VALID_PTR(pUuid),
|
---|
6647 | ("pUuid=%#p UUID=%RTuuid\n", pUuid, pUuid),
|
---|
6648 | rc = VERR_INVALID_PARAMETER);
|
---|
6649 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
6650 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
6651 | rc = VERR_INVALID_PARAMETER);
|
---|
6652 |
|
---|
6653 | /* Check state. Needs a temporary read lock. Holding the write lock
|
---|
6654 | * all the time would be blocking other activities for too long. */
|
---|
6655 | rc2 = vdThreadStartRead(pDisk);
|
---|
6656 | AssertRC(rc2);
|
---|
6657 | fLockRead = true;
|
---|
6658 | AssertMsgBreakStmt(!pDisk->pCache,
|
---|
6659 | ("Create cache image cannot be done with a cache already attached\n"),
|
---|
6660 | rc = VERR_VD_CACHE_ALREADY_EXISTS);
|
---|
6661 | rc2 = vdThreadFinishRead(pDisk);
|
---|
6662 | AssertRC(rc2);
|
---|
6663 | fLockRead = false;
|
---|
6664 |
|
---|
6665 | /* Set up image descriptor. */
|
---|
6666 | pCache = (PVDCACHE)RTMemAllocZ(sizeof(VDCACHE));
|
---|
6667 | if (!pCache)
|
---|
6668 | {
|
---|
6669 | rc = VERR_NO_MEMORY;
|
---|
6670 | break;
|
---|
6671 | }
|
---|
6672 | pCache->pszFilename = RTStrDup(pszFilename);
|
---|
6673 | if (!pCache->pszFilename)
|
---|
6674 | {
|
---|
6675 | rc = VERR_NO_MEMORY;
|
---|
6676 | break;
|
---|
6677 | }
|
---|
6678 |
|
---|
6679 | rc = vdFindCacheBackend(pszBackend, &pCache->Backend);
|
---|
6680 | if (RT_FAILURE(rc))
|
---|
6681 | break;
|
---|
6682 | if (!pCache->Backend)
|
---|
6683 | {
|
---|
6684 | rc = vdError(pDisk, VERR_INVALID_PARAMETER, RT_SRC_POS,
|
---|
6685 | N_("VD: unknown backend name '%s'"), pszBackend);
|
---|
6686 | break;
|
---|
6687 | }
|
---|
6688 |
|
---|
6689 | pCache->VDIo.pDisk = pDisk;
|
---|
6690 | pCache->pVDIfsCache = pVDIfsCache;
|
---|
6691 |
|
---|
6692 | /* Set up the I/O interface. */
|
---|
6693 | pCache->VDIo.pInterfaceIo = VDIfIoGet(pVDIfsCache);
|
---|
6694 | if (!pCache->VDIo.pInterfaceIo)
|
---|
6695 | {
|
---|
6696 | vdIfIoFallbackCallbacksSetup(&pCache->VDIo.VDIfIo);
|
---|
6697 | rc = VDInterfaceAdd(&pCache->VDIo.VDIfIo.Core, "VD_IO", VDINTERFACETYPE_IO,
|
---|
6698 | pDisk, sizeof(VDINTERFACEIO), &pVDIfsCache);
|
---|
6699 | pCache->VDIo.pInterfaceIo = &pCache->VDIo.VDIfIo;
|
---|
6700 | }
|
---|
6701 |
|
---|
6702 | /* Set up the internal I/O interface. */
|
---|
6703 | AssertBreakStmt(!VDIfIoIntGet(pVDIfsCache), rc = VERR_INVALID_PARAMETER);
|
---|
6704 | vdIfIoIntCallbacksSetup(&pCache->VDIo.VDIfIoInt);
|
---|
6705 | rc = VDInterfaceAdd(&pCache->VDIo.VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
6706 | &pCache->VDIo, sizeof(VDINTERFACEIOINT), &pCache->pVDIfsCache);
|
---|
6707 | AssertRC(rc);
|
---|
6708 |
|
---|
6709 | /* Create UUID if the caller didn't specify one. */
|
---|
6710 | if (!pUuid)
|
---|
6711 | {
|
---|
6712 | rc = RTUuidCreate(&uuid);
|
---|
6713 | if (RT_FAILURE(rc))
|
---|
6714 | {
|
---|
6715 | rc = vdError(pDisk, rc, RT_SRC_POS,
|
---|
6716 | N_("VD: cannot generate UUID for image '%s'"),
|
---|
6717 | pszFilename);
|
---|
6718 | break;
|
---|
6719 | }
|
---|
6720 | pUuid = &uuid;
|
---|
6721 | }
|
---|
6722 |
|
---|
6723 | pCache->uOpenFlags = uOpenFlags & VD_OPEN_FLAGS_HONOR_SAME;
|
---|
6724 | pCache->VDIo.fIgnoreFlush = (uOpenFlags & VD_OPEN_FLAGS_IGNORE_FLUSH) != 0;
|
---|
6725 | rc = pCache->Backend->pfnCreate(pCache->pszFilename, cbSize,
|
---|
6726 | uImageFlags,
|
---|
6727 | pszComment, pUuid,
|
---|
6728 | uOpenFlags & ~VD_OPEN_FLAGS_HONOR_SAME,
|
---|
6729 | 0, 99,
|
---|
6730 | pDisk->pVDIfsDisk,
|
---|
6731 | pCache->pVDIfsCache,
|
---|
6732 | pVDIfsOperation,
|
---|
6733 | &pCache->pBackendData);
|
---|
6734 |
|
---|
6735 | if (RT_SUCCESS(rc))
|
---|
6736 | {
|
---|
6737 | /* Lock disk for writing, as we modify pDisk information below. */
|
---|
6738 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6739 | AssertRC(rc2);
|
---|
6740 | fLockWrite = true;
|
---|
6741 |
|
---|
6742 | pCache->VDIo.pBackendData = pCache->pBackendData;
|
---|
6743 |
|
---|
6744 | /* Re-check state, as the lock wasn't held and another image
|
---|
6745 | * creation call could have been done by another thread. */
|
---|
6746 | AssertMsgStmt(!pDisk->pCache,
|
---|
6747 | ("Create cache image cannot be done with another cache open\n"),
|
---|
6748 | rc = VERR_VD_CACHE_ALREADY_EXISTS);
|
---|
6749 | }
|
---|
6750 |
|
---|
6751 | if ( RT_SUCCESS(rc)
|
---|
6752 | && pDisk->pLast)
|
---|
6753 | {
|
---|
6754 | RTUUID UuidModification;
|
---|
6755 |
|
---|
6756 | /* Set same modification Uuid as the last image. */
|
---|
6757 | rc = pDisk->pLast->Backend->pfnGetModificationUuid(pDisk->pLast->pBackendData,
|
---|
6758 | &UuidModification);
|
---|
6759 | if (RT_SUCCESS(rc))
|
---|
6760 | {
|
---|
6761 | rc = pCache->Backend->pfnSetModificationUuid(pCache->pBackendData,
|
---|
6762 | &UuidModification);
|
---|
6763 | }
|
---|
6764 |
|
---|
6765 | if (rc == VERR_NOT_SUPPORTED)
|
---|
6766 | rc = VINF_SUCCESS;
|
---|
6767 | }
|
---|
6768 |
|
---|
6769 | if (RT_SUCCESS(rc))
|
---|
6770 | {
|
---|
6771 | /* Cache successfully created. */
|
---|
6772 | pDisk->pCache = pCache;
|
---|
6773 | }
|
---|
6774 | else
|
---|
6775 | {
|
---|
6776 | /* Error detected, but image opened. Close and delete image. */
|
---|
6777 | rc2 = pCache->Backend->pfnClose(pCache->pBackendData, true);
|
---|
6778 | AssertRC(rc2);
|
---|
6779 | pCache->pBackendData = NULL;
|
---|
6780 | }
|
---|
6781 | } while (0);
|
---|
6782 |
|
---|
6783 | if (RT_UNLIKELY(fLockWrite))
|
---|
6784 | {
|
---|
6785 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6786 | AssertRC(rc2);
|
---|
6787 | }
|
---|
6788 | else if (RT_UNLIKELY(fLockRead))
|
---|
6789 | {
|
---|
6790 | rc2 = vdThreadFinishRead(pDisk);
|
---|
6791 | AssertRC(rc2);
|
---|
6792 | }
|
---|
6793 |
|
---|
6794 | if (RT_FAILURE(rc))
|
---|
6795 | {
|
---|
6796 | if (pCache)
|
---|
6797 | {
|
---|
6798 | if (pCache->pszFilename)
|
---|
6799 | RTStrFree(pCache->pszFilename);
|
---|
6800 | RTMemFree(pCache);
|
---|
6801 | }
|
---|
6802 | }
|
---|
6803 |
|
---|
6804 | if (RT_SUCCESS(rc) && pIfProgress && pIfProgress->pfnProgress)
|
---|
6805 | pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
|
---|
6806 |
|
---|
6807 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
6808 | return rc;
|
---|
6809 | }
|
---|
6810 |
|
---|
6811 | /**
|
---|
6812 | * Merges two images (not necessarily with direct parent/child relationship).
|
---|
6813 | * As a side effect the source image and potentially the other images which
|
---|
6814 | * are also merged to the destination are deleted from both the disk and the
|
---|
6815 | * images in the HDD container.
|
---|
6816 | *
|
---|
6817 | * @returns VBox status code.
|
---|
6818 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
6819 | * @param pDisk Pointer to HDD container.
|
---|
6820 | * @param nImageFrom Name of the image file to merge from.
|
---|
6821 | * @param nImageTo Name of the image file to merge to.
|
---|
6822 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
6823 | */
|
---|
6824 | VBOXDDU_DECL(int) VDMerge(PVBOXHDD pDisk, unsigned nImageFrom,
|
---|
6825 | unsigned nImageTo, PVDINTERFACE pVDIfsOperation)
|
---|
6826 | {
|
---|
6827 | int rc = VINF_SUCCESS;
|
---|
6828 | int rc2;
|
---|
6829 | bool fLockWrite = false, fLockRead = false;
|
---|
6830 | void *pvBuf = NULL;
|
---|
6831 |
|
---|
6832 | LogFlowFunc(("pDisk=%#p nImageFrom=%u nImageTo=%u pVDIfsOperation=%#p\n",
|
---|
6833 | pDisk, nImageFrom, nImageTo, pVDIfsOperation));
|
---|
6834 |
|
---|
6835 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
6836 |
|
---|
6837 | do
|
---|
6838 | {
|
---|
6839 | /* sanity check */
|
---|
6840 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
6841 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
6842 |
|
---|
6843 | /* For simplicity reasons lock for writing as the image reopen below
|
---|
6844 | * might need it. After all the reopen is usually needed. */
|
---|
6845 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6846 | AssertRC(rc2);
|
---|
6847 | fLockWrite = true;
|
---|
6848 | PVDIMAGE pImageFrom = vdGetImageByNumber(pDisk, nImageFrom);
|
---|
6849 | PVDIMAGE pImageTo = vdGetImageByNumber(pDisk, nImageTo);
|
---|
6850 | if (!pImageFrom || !pImageTo)
|
---|
6851 | {
|
---|
6852 | rc = VERR_VD_IMAGE_NOT_FOUND;
|
---|
6853 | break;
|
---|
6854 | }
|
---|
6855 | AssertBreakStmt(pImageFrom != pImageTo, rc = VERR_INVALID_PARAMETER);
|
---|
6856 |
|
---|
6857 | /* Make sure destination image is writable. */
|
---|
6858 | unsigned uOpenFlags = pImageTo->Backend->pfnGetOpenFlags(pImageTo->pBackendData);
|
---|
6859 | if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
6860 | {
|
---|
6861 | /*
|
---|
6862 | * Clear skip consistency checks because the image is made writable now and
|
---|
6863 | * skipping consistency checks is only possible for readonly images.
|
---|
6864 | */
|
---|
6865 | uOpenFlags &= ~(VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS);
|
---|
6866 | rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pBackendData,
|
---|
6867 | uOpenFlags);
|
---|
6868 | if (RT_FAILURE(rc))
|
---|
6869 | break;
|
---|
6870 | }
|
---|
6871 |
|
---|
6872 | /* Get size of destination image. */
|
---|
6873 | uint64_t cbSize = pImageTo->Backend->pfnGetSize(pImageTo->pBackendData);
|
---|
6874 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6875 | AssertRC(rc2);
|
---|
6876 | fLockWrite = false;
|
---|
6877 |
|
---|
6878 | /* Allocate tmp buffer. */
|
---|
6879 | pvBuf = RTMemTmpAlloc(VD_MERGE_BUFFER_SIZE);
|
---|
6880 | if (!pvBuf)
|
---|
6881 | {
|
---|
6882 | rc = VERR_NO_MEMORY;
|
---|
6883 | break;
|
---|
6884 | }
|
---|
6885 |
|
---|
6886 | /* Merging is done directly on the images itself. This potentially
|
---|
6887 | * causes trouble if the disk is full in the middle of operation. */
|
---|
6888 | if (nImageFrom < nImageTo)
|
---|
6889 | {
|
---|
6890 | /* Merge parent state into child. This means writing all not
|
---|
6891 | * allocated blocks in the destination image which are allocated in
|
---|
6892 | * the images to be merged. */
|
---|
6893 | uint64_t uOffset = 0;
|
---|
6894 | uint64_t cbRemaining = cbSize;
|
---|
6895 | do
|
---|
6896 | {
|
---|
6897 | size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
|
---|
6898 | RTSGSEG SegmentBuf;
|
---|
6899 | RTSGBUF SgBuf;
|
---|
6900 | VDIOCTX IoCtx;
|
---|
6901 |
|
---|
6902 | SegmentBuf.pvSeg = pvBuf;
|
---|
6903 | SegmentBuf.cbSeg = VD_MERGE_BUFFER_SIZE;
|
---|
6904 | RTSgBufInit(&SgBuf, &SegmentBuf, 1);
|
---|
6905 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_READ, 0, 0, NULL,
|
---|
6906 | &SgBuf, NULL, NULL, VDIOCTX_FLAGS_SYNC);
|
---|
6907 |
|
---|
6908 | /* Need to hold the write lock during a read-write operation. */
|
---|
6909 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6910 | AssertRC(rc2);
|
---|
6911 | fLockWrite = true;
|
---|
6912 |
|
---|
6913 | rc = pImageTo->Backend->pfnRead(pImageTo->pBackendData,
|
---|
6914 | uOffset, cbThisRead,
|
---|
6915 | &IoCtx, &cbThisRead);
|
---|
6916 | if (rc == VERR_VD_BLOCK_FREE)
|
---|
6917 | {
|
---|
6918 | /* Search for image with allocated block. Do not attempt to
|
---|
6919 | * read more than the previous reads marked as valid.
|
---|
6920 | * Otherwise this would return stale data when different
|
---|
6921 | * block sizes are used for the images. */
|
---|
6922 | for (PVDIMAGE pCurrImage = pImageTo->pPrev;
|
---|
6923 | pCurrImage != NULL && pCurrImage != pImageFrom->pPrev && rc == VERR_VD_BLOCK_FREE;
|
---|
6924 | pCurrImage = pCurrImage->pPrev)
|
---|
6925 | {
|
---|
6926 | rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
|
---|
6927 | uOffset, cbThisRead,
|
---|
6928 | &IoCtx, &cbThisRead);
|
---|
6929 | }
|
---|
6930 |
|
---|
6931 | if (rc != VERR_VD_BLOCK_FREE)
|
---|
6932 | {
|
---|
6933 | if (RT_FAILURE(rc))
|
---|
6934 | break;
|
---|
6935 | /* Updating the cache is required because this might be a live merge. */
|
---|
6936 | rc = vdWriteHelperEx(pDisk, pImageTo, pImageFrom->pPrev,
|
---|
6937 | uOffset, pvBuf, cbThisRead,
|
---|
6938 | true /* fUpdateCache */, 0);
|
---|
6939 | if (RT_FAILURE(rc))
|
---|
6940 | break;
|
---|
6941 | }
|
---|
6942 | else
|
---|
6943 | rc = VINF_SUCCESS;
|
---|
6944 | }
|
---|
6945 | else if (RT_FAILURE(rc))
|
---|
6946 | break;
|
---|
6947 |
|
---|
6948 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6949 | AssertRC(rc2);
|
---|
6950 | fLockWrite = false;
|
---|
6951 |
|
---|
6952 | uOffset += cbThisRead;
|
---|
6953 | cbRemaining -= cbThisRead;
|
---|
6954 |
|
---|
6955 | if (pIfProgress && pIfProgress->pfnProgress)
|
---|
6956 | {
|
---|
6957 | /** @todo r=klaus: this can update the progress to the same
|
---|
6958 | * percentage over and over again if the image format makes
|
---|
6959 | * relatively small increments. */
|
---|
6960 | rc = pIfProgress->pfnProgress(pIfProgress->Core.pvUser,
|
---|
6961 | uOffset * 99 / cbSize);
|
---|
6962 | if (RT_FAILURE(rc))
|
---|
6963 | break;
|
---|
6964 | }
|
---|
6965 | } while (uOffset < cbSize);
|
---|
6966 | }
|
---|
6967 | else
|
---|
6968 | {
|
---|
6969 | /*
|
---|
6970 | * We may need to update the parent uuid of the child coming after
|
---|
6971 | * the last image to be merged. We have to reopen it read/write.
|
---|
6972 | *
|
---|
6973 | * This is done before we do the actual merge to prevent an
|
---|
6974 | * inconsistent chain if the mode change fails for some reason.
|
---|
6975 | */
|
---|
6976 | if (pImageFrom->pNext)
|
---|
6977 | {
|
---|
6978 | PVDIMAGE pImageChild = pImageFrom->pNext;
|
---|
6979 |
|
---|
6980 | /* Take the write lock. */
|
---|
6981 | rc2 = vdThreadStartWrite(pDisk);
|
---|
6982 | AssertRC(rc2);
|
---|
6983 | fLockWrite = true;
|
---|
6984 |
|
---|
6985 | /* We need to open the image in read/write mode. */
|
---|
6986 | uOpenFlags = pImageChild->Backend->pfnGetOpenFlags(pImageChild->pBackendData);
|
---|
6987 |
|
---|
6988 | if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
|
---|
6989 | {
|
---|
6990 | uOpenFlags &= ~VD_OPEN_FLAGS_READONLY;
|
---|
6991 | rc = pImageChild->Backend->pfnSetOpenFlags(pImageChild->pBackendData,
|
---|
6992 | uOpenFlags);
|
---|
6993 | if (RT_FAILURE(rc))
|
---|
6994 | break;
|
---|
6995 | }
|
---|
6996 |
|
---|
6997 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
6998 | AssertRC(rc2);
|
---|
6999 | fLockWrite = false;
|
---|
7000 | }
|
---|
7001 |
|
---|
7002 | /* If the merge is from the last image we have to relay all writes
|
---|
7003 | * to the merge destination as well, so that concurrent writes
|
---|
7004 | * (in case of a live merge) are handled correctly. */
|
---|
7005 | if (!pImageFrom->pNext)
|
---|
7006 | {
|
---|
7007 | /* Take the write lock. */
|
---|
7008 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7009 | AssertRC(rc2);
|
---|
7010 | fLockWrite = true;
|
---|
7011 |
|
---|
7012 | pDisk->pImageRelay = pImageTo;
|
---|
7013 |
|
---|
7014 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7015 | AssertRC(rc2);
|
---|
7016 | fLockWrite = false;
|
---|
7017 | }
|
---|
7018 |
|
---|
7019 | /* Merge child state into parent. This means writing all blocks
|
---|
7020 | * which are allocated in the image up to the source image to the
|
---|
7021 | * destination image. */
|
---|
7022 | uint64_t uOffset = 0;
|
---|
7023 | uint64_t cbRemaining = cbSize;
|
---|
7024 | do
|
---|
7025 | {
|
---|
7026 | size_t cbThisRead = RT_MIN(VD_MERGE_BUFFER_SIZE, cbRemaining);
|
---|
7027 | RTSGSEG SegmentBuf;
|
---|
7028 | RTSGBUF SgBuf;
|
---|
7029 | VDIOCTX IoCtx;
|
---|
7030 |
|
---|
7031 | rc = VERR_VD_BLOCK_FREE;
|
---|
7032 |
|
---|
7033 | SegmentBuf.pvSeg = pvBuf;
|
---|
7034 | SegmentBuf.cbSeg = VD_MERGE_BUFFER_SIZE;
|
---|
7035 | RTSgBufInit(&SgBuf, &SegmentBuf, 1);
|
---|
7036 | vdIoCtxInit(&IoCtx, pDisk, VDIOCTXTXDIR_READ, 0, 0, NULL,
|
---|
7037 | &SgBuf, NULL, NULL, VDIOCTX_FLAGS_SYNC);
|
---|
7038 |
|
---|
7039 | /* Need to hold the write lock during a read-write operation. */
|
---|
7040 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7041 | AssertRC(rc2);
|
---|
7042 | fLockWrite = true;
|
---|
7043 |
|
---|
7044 | /* Search for image with allocated block. Do not attempt to
|
---|
7045 | * read more than the previous reads marked as valid. Otherwise
|
---|
7046 | * this would return stale data when different block sizes are
|
---|
7047 | * used for the images. */
|
---|
7048 | for (PVDIMAGE pCurrImage = pImageFrom;
|
---|
7049 | pCurrImage != NULL && pCurrImage != pImageTo && rc == VERR_VD_BLOCK_FREE;
|
---|
7050 | pCurrImage = pCurrImage->pPrev)
|
---|
7051 | {
|
---|
7052 | rc = pCurrImage->Backend->pfnRead(pCurrImage->pBackendData,
|
---|
7053 | uOffset, cbThisRead,
|
---|
7054 | &IoCtx, &cbThisRead);
|
---|
7055 | }
|
---|
7056 |
|
---|
7057 | if (rc != VERR_VD_BLOCK_FREE)
|
---|
7058 | {
|
---|
7059 | if (RT_FAILURE(rc))
|
---|
7060 | break;
|
---|
7061 | rc = vdWriteHelper(pDisk, pImageTo, uOffset, pvBuf,
|
---|
7062 | cbThisRead, true /* fUpdateCache */);
|
---|
7063 | if (RT_FAILURE(rc))
|
---|
7064 | break;
|
---|
7065 | }
|
---|
7066 | else
|
---|
7067 | rc = VINF_SUCCESS;
|
---|
7068 |
|
---|
7069 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7070 | AssertRC(rc2);
|
---|
7071 | fLockWrite = false;
|
---|
7072 |
|
---|
7073 | uOffset += cbThisRead;
|
---|
7074 | cbRemaining -= cbThisRead;
|
---|
7075 |
|
---|
7076 | if (pIfProgress && pIfProgress->pfnProgress)
|
---|
7077 | {
|
---|
7078 | /** @todo r=klaus: this can update the progress to the same
|
---|
7079 | * percentage over and over again if the image format makes
|
---|
7080 | * relatively small increments. */
|
---|
7081 | rc = pIfProgress->pfnProgress(pIfProgress->Core.pvUser,
|
---|
7082 | uOffset * 99 / cbSize);
|
---|
7083 | if (RT_FAILURE(rc))
|
---|
7084 | break;
|
---|
7085 | }
|
---|
7086 | } while (uOffset < cbSize);
|
---|
7087 |
|
---|
7088 | /* In case we set up a "write proxy" image above we must clear
|
---|
7089 | * this again now to prevent stray writes. Failure or not. */
|
---|
7090 | if (!pImageFrom->pNext)
|
---|
7091 | {
|
---|
7092 | /* Take the write lock. */
|
---|
7093 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7094 | AssertRC(rc2);
|
---|
7095 | fLockWrite = true;
|
---|
7096 |
|
---|
7097 | pDisk->pImageRelay = NULL;
|
---|
7098 |
|
---|
7099 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7100 | AssertRC(rc2);
|
---|
7101 | fLockWrite = false;
|
---|
7102 | }
|
---|
7103 | }
|
---|
7104 |
|
---|
7105 | /*
|
---|
7106 | * Leave in case of an error to avoid corrupted data in the image chain
|
---|
7107 | * (includes cancelling the operation by the user).
|
---|
7108 | */
|
---|
7109 | if (RT_FAILURE(rc))
|
---|
7110 | break;
|
---|
7111 |
|
---|
7112 | /* Need to hold the write lock while finishing the merge. */
|
---|
7113 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7114 | AssertRC(rc2);
|
---|
7115 | fLockWrite = true;
|
---|
7116 |
|
---|
7117 | /* Update parent UUID so that image chain is consistent.
|
---|
7118 | * The two attempts work around the problem that some backends
|
---|
7119 | * (e.g. iSCSI) do not support UUIDs, so we exploit the fact that
|
---|
7120 | * so far there can only be one such image in the chain. */
|
---|
7121 | /** @todo needs a better long-term solution, passing the UUID
|
---|
7122 | * knowledge from the caller or some such */
|
---|
7123 | RTUUID Uuid;
|
---|
7124 | PVDIMAGE pImageChild = NULL;
|
---|
7125 | if (nImageFrom < nImageTo)
|
---|
7126 | {
|
---|
7127 | if (pImageFrom->pPrev)
|
---|
7128 | {
|
---|
7129 | /* plan A: ask the parent itself for its UUID */
|
---|
7130 | rc = pImageFrom->pPrev->Backend->pfnGetUuid(pImageFrom->pPrev->pBackendData,
|
---|
7131 | &Uuid);
|
---|
7132 | if (RT_FAILURE(rc))
|
---|
7133 | {
|
---|
7134 | /* plan B: ask the child of the parent for parent UUID */
|
---|
7135 | rc = pImageFrom->Backend->pfnGetParentUuid(pImageFrom->pBackendData,
|
---|
7136 | &Uuid);
|
---|
7137 | }
|
---|
7138 | AssertRC(rc);
|
---|
7139 | }
|
---|
7140 | else
|
---|
7141 | RTUuidClear(&Uuid);
|
---|
7142 | rc = pImageTo->Backend->pfnSetParentUuid(pImageTo->pBackendData,
|
---|
7143 | &Uuid);
|
---|
7144 | AssertRC(rc);
|
---|
7145 | }
|
---|
7146 | else
|
---|
7147 | {
|
---|
7148 | /* Update the parent uuid of the child of the last merged image. */
|
---|
7149 | if (pImageFrom->pNext)
|
---|
7150 | {
|
---|
7151 | /* plan A: ask the parent itself for its UUID */
|
---|
7152 | rc = pImageTo->Backend->pfnGetUuid(pImageTo->pBackendData,
|
---|
7153 | &Uuid);
|
---|
7154 | if (RT_FAILURE(rc))
|
---|
7155 | {
|
---|
7156 | /* plan B: ask the child of the parent for parent UUID */
|
---|
7157 | rc = pImageTo->pNext->Backend->pfnGetParentUuid(pImageTo->pNext->pBackendData,
|
---|
7158 | &Uuid);
|
---|
7159 | }
|
---|
7160 | AssertRC(rc);
|
---|
7161 |
|
---|
7162 | rc = pImageFrom->Backend->pfnSetParentUuid(pImageFrom->pNext->pBackendData,
|
---|
7163 | &Uuid);
|
---|
7164 | AssertRC(rc);
|
---|
7165 |
|
---|
7166 | pImageChild = pImageFrom->pNext;
|
---|
7167 | }
|
---|
7168 | }
|
---|
7169 |
|
---|
7170 | /* Delete the no longer needed images. */
|
---|
7171 | PVDIMAGE pImg = pImageFrom, pTmp;
|
---|
7172 | while (pImg != pImageTo)
|
---|
7173 | {
|
---|
7174 | if (nImageFrom < nImageTo)
|
---|
7175 | pTmp = pImg->pNext;
|
---|
7176 | else
|
---|
7177 | pTmp = pImg->pPrev;
|
---|
7178 | vdRemoveImageFromList(pDisk, pImg);
|
---|
7179 | pImg->Backend->pfnClose(pImg->pBackendData, true);
|
---|
7180 | RTMemFree(pImg->pszFilename);
|
---|
7181 | RTMemFree(pImg);
|
---|
7182 | pImg = pTmp;
|
---|
7183 | }
|
---|
7184 |
|
---|
7185 | /* Make sure destination image is back to read only if necessary. */
|
---|
7186 | if (pImageTo != pDisk->pLast)
|
---|
7187 | {
|
---|
7188 | uOpenFlags = pImageTo->Backend->pfnGetOpenFlags(pImageTo->pBackendData);
|
---|
7189 | uOpenFlags |= VD_OPEN_FLAGS_READONLY;
|
---|
7190 | rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pBackendData,
|
---|
7191 | uOpenFlags);
|
---|
7192 | if (RT_FAILURE(rc))
|
---|
7193 | break;
|
---|
7194 | }
|
---|
7195 |
|
---|
7196 | /*
|
---|
7197 | * Make sure the child is readonly
|
---|
7198 | * for the child -> parent merge direction
|
---|
7199 | * if necessary.
|
---|
7200 | */
|
---|
7201 | if ( nImageFrom > nImageTo
|
---|
7202 | && pImageChild
|
---|
7203 | && pImageChild != pDisk->pLast)
|
---|
7204 | {
|
---|
7205 | uOpenFlags = pImageChild->Backend->pfnGetOpenFlags(pImageChild->pBackendData);
|
---|
7206 | uOpenFlags |= VD_OPEN_FLAGS_READONLY;
|
---|
7207 | rc = pImageChild->Backend->pfnSetOpenFlags(pImageChild->pBackendData,
|
---|
7208 | uOpenFlags);
|
---|
7209 | if (RT_FAILURE(rc))
|
---|
7210 | break;
|
---|
7211 | }
|
---|
7212 | } while (0);
|
---|
7213 |
|
---|
7214 | if (RT_UNLIKELY(fLockWrite))
|
---|
7215 | {
|
---|
7216 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7217 | AssertRC(rc2);
|
---|
7218 | }
|
---|
7219 | else if (RT_UNLIKELY(fLockRead))
|
---|
7220 | {
|
---|
7221 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7222 | AssertRC(rc2);
|
---|
7223 | }
|
---|
7224 |
|
---|
7225 | if (pvBuf)
|
---|
7226 | RTMemTmpFree(pvBuf);
|
---|
7227 |
|
---|
7228 | if (RT_SUCCESS(rc) && pIfProgress && pIfProgress->pfnProgress)
|
---|
7229 | pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
|
---|
7230 |
|
---|
7231 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
7232 | return rc;
|
---|
7233 | }
|
---|
7234 |
|
---|
7235 | /**
|
---|
7236 | * Copies an image from one HDD container to another - extended version.
|
---|
7237 | * The copy is opened in the target HDD container.
|
---|
7238 | * It is possible to convert between different image formats, because the
|
---|
7239 | * backend for the destination may be different from the source.
|
---|
7240 | * If both the source and destination reference the same HDD container,
|
---|
7241 | * then the image is moved (by copying/deleting or renaming) to the new location.
|
---|
7242 | * The source container is unchanged if the move operation fails, otherwise
|
---|
7243 | * the image at the new location is opened in the same way as the old one was.
|
---|
7244 | *
|
---|
7245 | * @note The read/write accesses across disks are not synchronized, just the
|
---|
7246 | * accesses to each disk. Once there is a use case which requires a defined
|
---|
7247 | * read/write behavior in this situation this needs to be extended.
|
---|
7248 | *
|
---|
7249 | * @return VBox status code.
|
---|
7250 | * @return VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
7251 | * @param pDiskFrom Pointer to source HDD container.
|
---|
7252 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
7253 | * @param pDiskTo Pointer to destination HDD container.
|
---|
7254 | * @param pszBackend Name of the image file backend to use (may be NULL to use the same as the source, case insensitive).
|
---|
7255 | * @param pszFilename New name of the image (may be NULL to specify that the
|
---|
7256 | * copy destination is the destination container, or
|
---|
7257 | * if pDiskFrom == pDiskTo, i.e. when moving).
|
---|
7258 | * @param fMoveByRename If true, attempt to perform a move by renaming (if successful the new size is ignored).
|
---|
7259 | * @param cbSize New image size (0 means leave unchanged).
|
---|
7260 | * @param nImageSameFrom todo
|
---|
7261 | * @param nImageSameTo todo
|
---|
7262 | * @param uImageFlags Flags specifying special destination image features.
|
---|
7263 | * @param pDstUuid New UUID of the destination image. If NULL, a new UUID is created.
|
---|
7264 | * This parameter is used if and only if a true copy is created.
|
---|
7265 | * In all rename/move cases or copy to existing image cases the modification UUIDs are copied over.
|
---|
7266 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
7267 | * Only used if the destination image is created.
|
---|
7268 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
7269 | * @param pDstVDIfsImage Pointer to the per-image VD interface list, for the
|
---|
7270 | * destination image.
|
---|
7271 | * @param pDstVDIfsOperation Pointer to the per-operation VD interface list,
|
---|
7272 | * for the destination operation.
|
---|
7273 | */
|
---|
7274 | VBOXDDU_DECL(int) VDCopyEx(PVBOXHDD pDiskFrom, unsigned nImage, PVBOXHDD pDiskTo,
|
---|
7275 | const char *pszBackend, const char *pszFilename,
|
---|
7276 | bool fMoveByRename, uint64_t cbSize,
|
---|
7277 | unsigned nImageFromSame, unsigned nImageToSame,
|
---|
7278 | unsigned uImageFlags, PCRTUUID pDstUuid,
|
---|
7279 | unsigned uOpenFlags, PVDINTERFACE pVDIfsOperation,
|
---|
7280 | PVDINTERFACE pDstVDIfsImage,
|
---|
7281 | PVDINTERFACE pDstVDIfsOperation)
|
---|
7282 | {
|
---|
7283 | int rc = VINF_SUCCESS;
|
---|
7284 | int rc2;
|
---|
7285 | bool fLockReadFrom = false, fLockWriteFrom = false, fLockWriteTo = false;
|
---|
7286 | PVDIMAGE pImageTo = NULL;
|
---|
7287 |
|
---|
7288 | 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",
|
---|
7289 | pDiskFrom, nImage, pDiskTo, pszBackend, pszFilename, fMoveByRename, cbSize, nImageFromSame, nImageToSame, uImageFlags, pDstUuid, uOpenFlags, pVDIfsOperation, pDstVDIfsImage, pDstVDIfsOperation));
|
---|
7290 |
|
---|
7291 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
7292 | PVDINTERFACEPROGRESS pDstIfProgress = VDIfProgressGet(pDstVDIfsOperation);
|
---|
7293 |
|
---|
7294 | do {
|
---|
7295 | /* Check arguments. */
|
---|
7296 | AssertMsgBreakStmt(VALID_PTR(pDiskFrom), ("pDiskFrom=%#p\n", pDiskFrom),
|
---|
7297 | rc = VERR_INVALID_PARAMETER);
|
---|
7298 | AssertMsg(pDiskFrom->u32Signature == VBOXHDDDISK_SIGNATURE,
|
---|
7299 | ("u32Signature=%08x\n", pDiskFrom->u32Signature));
|
---|
7300 |
|
---|
7301 | rc2 = vdThreadStartRead(pDiskFrom);
|
---|
7302 | AssertRC(rc2);
|
---|
7303 | fLockReadFrom = true;
|
---|
7304 | PVDIMAGE pImageFrom = vdGetImageByNumber(pDiskFrom, nImage);
|
---|
7305 | AssertPtrBreakStmt(pImageFrom, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
7306 | AssertMsgBreakStmt(VALID_PTR(pDiskTo), ("pDiskTo=%#p\n", pDiskTo),
|
---|
7307 | rc = VERR_INVALID_PARAMETER);
|
---|
7308 | AssertMsg(pDiskTo->u32Signature == VBOXHDDDISK_SIGNATURE,
|
---|
7309 | ("u32Signature=%08x\n", pDiskTo->u32Signature));
|
---|
7310 | AssertMsgBreakStmt( (nImageFromSame < nImage || nImageFromSame == VD_IMAGE_CONTENT_UNKNOWN)
|
---|
7311 | && (nImageToSame < pDiskTo->cImages || nImageToSame == VD_IMAGE_CONTENT_UNKNOWN)
|
---|
7312 | && ( (nImageFromSame == VD_IMAGE_CONTENT_UNKNOWN && nImageToSame == VD_IMAGE_CONTENT_UNKNOWN)
|
---|
7313 | || (nImageFromSame != VD_IMAGE_CONTENT_UNKNOWN && nImageToSame != VD_IMAGE_CONTENT_UNKNOWN)),
|
---|
7314 | ("nImageFromSame=%u nImageToSame=%u\n", nImageFromSame, nImageToSame),
|
---|
7315 | rc = VERR_INVALID_PARAMETER);
|
---|
7316 |
|
---|
7317 | /* Move the image. */
|
---|
7318 | if (pDiskFrom == pDiskTo)
|
---|
7319 | {
|
---|
7320 | /* Rename only works when backends are the same, are file based
|
---|
7321 | * and the rename method is implemented. */
|
---|
7322 | if ( fMoveByRename
|
---|
7323 | && !RTStrICmp(pszBackend, pImageFrom->Backend->pszBackendName)
|
---|
7324 | && pImageFrom->Backend->uBackendCaps & VD_CAP_FILE
|
---|
7325 | && pImageFrom->Backend->pfnRename)
|
---|
7326 | {
|
---|
7327 | rc2 = vdThreadFinishRead(pDiskFrom);
|
---|
7328 | AssertRC(rc2);
|
---|
7329 | fLockReadFrom = false;
|
---|
7330 |
|
---|
7331 | rc2 = vdThreadStartWrite(pDiskFrom);
|
---|
7332 | AssertRC(rc2);
|
---|
7333 | fLockWriteFrom = true;
|
---|
7334 | rc = pImageFrom->Backend->pfnRename(pImageFrom->pBackendData, pszFilename ? pszFilename : pImageFrom->pszFilename);
|
---|
7335 | break;
|
---|
7336 | }
|
---|
7337 |
|
---|
7338 | /** @todo Moving (including shrinking/growing) of the image is
|
---|
7339 | * requested, but the rename attempt failed or it wasn't possible.
|
---|
7340 | * Must now copy image to temp location. */
|
---|
7341 | AssertReleaseMsgFailed(("VDCopy: moving by copy/delete not implemented\n"));
|
---|
7342 | }
|
---|
7343 |
|
---|
7344 | /* pszFilename is allowed to be NULL, as this indicates copy to the existing image. */
|
---|
7345 | AssertMsgBreakStmt(pszFilename == NULL || (VALID_PTR(pszFilename) && *pszFilename),
|
---|
7346 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
7347 | rc = VERR_INVALID_PARAMETER);
|
---|
7348 |
|
---|
7349 | uint64_t cbSizeFrom;
|
---|
7350 | cbSizeFrom = pImageFrom->Backend->pfnGetSize(pImageFrom->pBackendData);
|
---|
7351 | if (cbSizeFrom == 0)
|
---|
7352 | {
|
---|
7353 | rc = VERR_VD_VALUE_NOT_FOUND;
|
---|
7354 | break;
|
---|
7355 | }
|
---|
7356 |
|
---|
7357 | VDGEOMETRY PCHSGeometryFrom = {0, 0, 0};
|
---|
7358 | VDGEOMETRY LCHSGeometryFrom = {0, 0, 0};
|
---|
7359 | pImageFrom->Backend->pfnGetPCHSGeometry(pImageFrom->pBackendData, &PCHSGeometryFrom);
|
---|
7360 | pImageFrom->Backend->pfnGetLCHSGeometry(pImageFrom->pBackendData, &LCHSGeometryFrom);
|
---|
7361 |
|
---|
7362 | RTUUID ImageUuid, ImageModificationUuid;
|
---|
7363 | if (pDiskFrom != pDiskTo)
|
---|
7364 | {
|
---|
7365 | if (pDstUuid)
|
---|
7366 | ImageUuid = *pDstUuid;
|
---|
7367 | else
|
---|
7368 | RTUuidCreate(&ImageUuid);
|
---|
7369 | }
|
---|
7370 | else
|
---|
7371 | {
|
---|
7372 | rc = pImageFrom->Backend->pfnGetUuid(pImageFrom->pBackendData, &ImageUuid);
|
---|
7373 | if (RT_FAILURE(rc))
|
---|
7374 | RTUuidCreate(&ImageUuid);
|
---|
7375 | }
|
---|
7376 | rc = pImageFrom->Backend->pfnGetModificationUuid(pImageFrom->pBackendData, &ImageModificationUuid);
|
---|
7377 | if (RT_FAILURE(rc))
|
---|
7378 | RTUuidClear(&ImageModificationUuid);
|
---|
7379 |
|
---|
7380 | char szComment[1024];
|
---|
7381 | rc = pImageFrom->Backend->pfnGetComment(pImageFrom->pBackendData, szComment, sizeof(szComment));
|
---|
7382 | if (RT_FAILURE(rc))
|
---|
7383 | szComment[0] = '\0';
|
---|
7384 | else
|
---|
7385 | szComment[sizeof(szComment) - 1] = '\0';
|
---|
7386 |
|
---|
7387 | rc2 = vdThreadFinishRead(pDiskFrom);
|
---|
7388 | AssertRC(rc2);
|
---|
7389 | fLockReadFrom = false;
|
---|
7390 |
|
---|
7391 | rc2 = vdThreadStartRead(pDiskTo);
|
---|
7392 | AssertRC(rc2);
|
---|
7393 | unsigned cImagesTo = pDiskTo->cImages;
|
---|
7394 | rc2 = vdThreadFinishRead(pDiskTo);
|
---|
7395 | AssertRC(rc2);
|
---|
7396 |
|
---|
7397 | if (pszFilename)
|
---|
7398 | {
|
---|
7399 | if (cbSize == 0)
|
---|
7400 | cbSize = cbSizeFrom;
|
---|
7401 |
|
---|
7402 | /* Create destination image with the properties of source image. */
|
---|
7403 | /** @todo replace the VDCreateDiff/VDCreateBase calls by direct
|
---|
7404 | * calls to the backend. Unifies the code and reduces the API
|
---|
7405 | * dependencies. Would also make the synchronization explicit. */
|
---|
7406 | if (cImagesTo > 0)
|
---|
7407 | {
|
---|
7408 | rc = VDCreateDiff(pDiskTo, pszBackend, pszFilename,
|
---|
7409 | uImageFlags, szComment, &ImageUuid,
|
---|
7410 | NULL /* pParentUuid */,
|
---|
7411 | uOpenFlags & ~VD_OPEN_FLAGS_READONLY,
|
---|
7412 | pDstVDIfsImage, NULL);
|
---|
7413 |
|
---|
7414 | rc2 = vdThreadStartWrite(pDiskTo);
|
---|
7415 | AssertRC(rc2);
|
---|
7416 | fLockWriteTo = true;
|
---|
7417 | } else {
|
---|
7418 | /** @todo hack to force creation of a fixed image for
|
---|
7419 | * the RAW backend, which can't handle anything else. */
|
---|
7420 | if (!RTStrICmp(pszBackend, "RAW"))
|
---|
7421 | uImageFlags |= VD_IMAGE_FLAGS_FIXED;
|
---|
7422 |
|
---|
7423 | vdFixupPCHSGeometry(&PCHSGeometryFrom, cbSize);
|
---|
7424 | vdFixupLCHSGeometry(&LCHSGeometryFrom, cbSize);
|
---|
7425 |
|
---|
7426 | rc = VDCreateBase(pDiskTo, pszBackend, pszFilename, cbSize,
|
---|
7427 | uImageFlags, szComment,
|
---|
7428 | &PCHSGeometryFrom, &LCHSGeometryFrom,
|
---|
7429 | NULL, uOpenFlags & ~VD_OPEN_FLAGS_READONLY,
|
---|
7430 | pDstVDIfsImage, NULL);
|
---|
7431 |
|
---|
7432 | rc2 = vdThreadStartWrite(pDiskTo);
|
---|
7433 | AssertRC(rc2);
|
---|
7434 | fLockWriteTo = true;
|
---|
7435 |
|
---|
7436 | if (RT_SUCCESS(rc) && !RTUuidIsNull(&ImageUuid))
|
---|
7437 | pDiskTo->pLast->Backend->pfnSetUuid(pDiskTo->pLast->pBackendData, &ImageUuid);
|
---|
7438 | }
|
---|
7439 | if (RT_FAILURE(rc))
|
---|
7440 | break;
|
---|
7441 |
|
---|
7442 | pImageTo = pDiskTo->pLast;
|
---|
7443 | AssertPtrBreakStmt(pImageTo, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
7444 |
|
---|
7445 | cbSize = RT_MIN(cbSize, cbSizeFrom);
|
---|
7446 | }
|
---|
7447 | else
|
---|
7448 | {
|
---|
7449 | pImageTo = pDiskTo->pLast;
|
---|
7450 | AssertPtrBreakStmt(pImageTo, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
7451 |
|
---|
7452 | uint64_t cbSizeTo;
|
---|
7453 | cbSizeTo = pImageTo->Backend->pfnGetSize(pImageTo->pBackendData);
|
---|
7454 | if (cbSizeTo == 0)
|
---|
7455 | {
|
---|
7456 | rc = VERR_VD_VALUE_NOT_FOUND;
|
---|
7457 | break;
|
---|
7458 | }
|
---|
7459 |
|
---|
7460 | if (cbSize == 0)
|
---|
7461 | cbSize = RT_MIN(cbSizeFrom, cbSizeTo);
|
---|
7462 |
|
---|
7463 | vdFixupPCHSGeometry(&PCHSGeometryFrom, cbSize);
|
---|
7464 | vdFixupLCHSGeometry(&LCHSGeometryFrom, cbSize);
|
---|
7465 |
|
---|
7466 | /* Update the geometry in the destination image. */
|
---|
7467 | pImageTo->Backend->pfnSetPCHSGeometry(pImageTo->pBackendData, &PCHSGeometryFrom);
|
---|
7468 | pImageTo->Backend->pfnSetLCHSGeometry(pImageTo->pBackendData, &LCHSGeometryFrom);
|
---|
7469 | }
|
---|
7470 |
|
---|
7471 | rc2 = vdThreadFinishWrite(pDiskTo);
|
---|
7472 | AssertRC(rc2);
|
---|
7473 | fLockWriteTo = false;
|
---|
7474 |
|
---|
7475 | /* Whether we can take the optimized copy path (false) or not.
|
---|
7476 | * Don't optimize if the image existed or if it is a child image. */
|
---|
7477 | bool fSuppressRedundantIo = ( !(pszFilename == NULL || cImagesTo > 0)
|
---|
7478 | || (nImageToSame != VD_IMAGE_CONTENT_UNKNOWN));
|
---|
7479 | unsigned cImagesFromReadBack, cImagesToReadBack;
|
---|
7480 |
|
---|
7481 | if (nImageFromSame == VD_IMAGE_CONTENT_UNKNOWN)
|
---|
7482 | cImagesFromReadBack = 0;
|
---|
7483 | else
|
---|
7484 | {
|
---|
7485 | if (nImage == VD_LAST_IMAGE)
|
---|
7486 | cImagesFromReadBack = pDiskFrom->cImages - nImageFromSame - 1;
|
---|
7487 | else
|
---|
7488 | cImagesFromReadBack = nImage - nImageFromSame;
|
---|
7489 | }
|
---|
7490 |
|
---|
7491 | if (nImageToSame == VD_IMAGE_CONTENT_UNKNOWN)
|
---|
7492 | cImagesToReadBack = 0;
|
---|
7493 | else
|
---|
7494 | cImagesToReadBack = pDiskTo->cImages - nImageToSame - 1;
|
---|
7495 |
|
---|
7496 | /* Copy the data. */
|
---|
7497 | rc = vdCopyHelper(pDiskFrom, pImageFrom, pDiskTo, cbSize,
|
---|
7498 | cImagesFromReadBack, cImagesToReadBack,
|
---|
7499 | fSuppressRedundantIo, pIfProgress, pDstIfProgress);
|
---|
7500 |
|
---|
7501 | if (RT_SUCCESS(rc))
|
---|
7502 | {
|
---|
7503 | rc2 = vdThreadStartWrite(pDiskTo);
|
---|
7504 | AssertRC(rc2);
|
---|
7505 | fLockWriteTo = true;
|
---|
7506 |
|
---|
7507 | /* Only set modification UUID if it is non-null, since the source
|
---|
7508 | * backend might not provide a valid modification UUID. */
|
---|
7509 | if (!RTUuidIsNull(&ImageModificationUuid))
|
---|
7510 | pImageTo->Backend->pfnSetModificationUuid(pImageTo->pBackendData, &ImageModificationUuid);
|
---|
7511 |
|
---|
7512 | /* Set the requested open flags if they differ from the value
|
---|
7513 | * required for creating the image and copying the contents. */
|
---|
7514 | if ( pImageTo && pszFilename
|
---|
7515 | && uOpenFlags != (uOpenFlags & ~VD_OPEN_FLAGS_READONLY))
|
---|
7516 | rc = pImageTo->Backend->pfnSetOpenFlags(pImageTo->pBackendData,
|
---|
7517 | uOpenFlags);
|
---|
7518 | }
|
---|
7519 | } while (0);
|
---|
7520 |
|
---|
7521 | if (RT_FAILURE(rc) && pImageTo && pszFilename)
|
---|
7522 | {
|
---|
7523 | /* Take the write lock only if it is not taken. Not worth making the
|
---|
7524 | * above code even more complicated. */
|
---|
7525 | if (RT_UNLIKELY(!fLockWriteTo))
|
---|
7526 | {
|
---|
7527 | rc2 = vdThreadStartWrite(pDiskTo);
|
---|
7528 | AssertRC(rc2);
|
---|
7529 | fLockWriteTo = true;
|
---|
7530 | }
|
---|
7531 | /* Error detected, but new image created. Remove image from list. */
|
---|
7532 | vdRemoveImageFromList(pDiskTo, pImageTo);
|
---|
7533 |
|
---|
7534 | /* Close and delete image. */
|
---|
7535 | rc2 = pImageTo->Backend->pfnClose(pImageTo->pBackendData, true);
|
---|
7536 | AssertRC(rc2);
|
---|
7537 | pImageTo->pBackendData = NULL;
|
---|
7538 |
|
---|
7539 | /* Free remaining resources. */
|
---|
7540 | if (pImageTo->pszFilename)
|
---|
7541 | RTStrFree(pImageTo->pszFilename);
|
---|
7542 |
|
---|
7543 | RTMemFree(pImageTo);
|
---|
7544 | }
|
---|
7545 |
|
---|
7546 | if (RT_UNLIKELY(fLockWriteTo))
|
---|
7547 | {
|
---|
7548 | rc2 = vdThreadFinishWrite(pDiskTo);
|
---|
7549 | AssertRC(rc2);
|
---|
7550 | }
|
---|
7551 | if (RT_UNLIKELY(fLockWriteFrom))
|
---|
7552 | {
|
---|
7553 | rc2 = vdThreadFinishWrite(pDiskFrom);
|
---|
7554 | AssertRC(rc2);
|
---|
7555 | }
|
---|
7556 | else if (RT_UNLIKELY(fLockReadFrom))
|
---|
7557 | {
|
---|
7558 | rc2 = vdThreadFinishRead(pDiskFrom);
|
---|
7559 | AssertRC(rc2);
|
---|
7560 | }
|
---|
7561 |
|
---|
7562 | if (RT_SUCCESS(rc))
|
---|
7563 | {
|
---|
7564 | if (pIfProgress && pIfProgress->pfnProgress)
|
---|
7565 | pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
|
---|
7566 | if (pDstIfProgress && pDstIfProgress->pfnProgress)
|
---|
7567 | pDstIfProgress->pfnProgress(pDstIfProgress->Core.pvUser, 100);
|
---|
7568 | }
|
---|
7569 |
|
---|
7570 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
7571 | return rc;
|
---|
7572 | }
|
---|
7573 |
|
---|
7574 | /**
|
---|
7575 | * Copies an image from one HDD container to another.
|
---|
7576 | * The copy is opened in the target HDD container.
|
---|
7577 | * It is possible to convert between different image formats, because the
|
---|
7578 | * backend for the destination may be different from the source.
|
---|
7579 | * If both the source and destination reference the same HDD container,
|
---|
7580 | * then the image is moved (by copying/deleting or renaming) to the new location.
|
---|
7581 | * The source container is unchanged if the move operation fails, otherwise
|
---|
7582 | * the image at the new location is opened in the same way as the old one was.
|
---|
7583 | *
|
---|
7584 | * @returns VBox status code.
|
---|
7585 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
7586 | * @param pDiskFrom Pointer to source HDD container.
|
---|
7587 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
7588 | * @param pDiskTo Pointer to destination HDD container.
|
---|
7589 | * @param pszBackend Name of the image file backend to use.
|
---|
7590 | * @param pszFilename New name of the image (may be NULL if pDiskFrom == pDiskTo).
|
---|
7591 | * @param fMoveByRename If true, attempt to perform a move by renaming (if successful the new size is ignored).
|
---|
7592 | * @param cbSize New image size (0 means leave unchanged).
|
---|
7593 | * @param uImageFlags Flags specifying special destination image features.
|
---|
7594 | * @param pDstUuid New UUID of the destination image. If NULL, a new UUID is created.
|
---|
7595 | * This parameter is used if and only if a true copy is created.
|
---|
7596 | * In all rename/move cases the UUIDs are copied over.
|
---|
7597 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
7598 | * Only used if the destination image is created.
|
---|
7599 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
7600 | * @param pDstVDIfsImage Pointer to the per-image VD interface list, for the
|
---|
7601 | * destination image.
|
---|
7602 | * @param pDstVDIfsOperation Pointer to the per-image VD interface list,
|
---|
7603 | * for the destination image.
|
---|
7604 | */
|
---|
7605 | VBOXDDU_DECL(int) VDCopy(PVBOXHDD pDiskFrom, unsigned nImage, PVBOXHDD pDiskTo,
|
---|
7606 | const char *pszBackend, const char *pszFilename,
|
---|
7607 | bool fMoveByRename, uint64_t cbSize,
|
---|
7608 | unsigned uImageFlags, PCRTUUID pDstUuid,
|
---|
7609 | unsigned uOpenFlags, PVDINTERFACE pVDIfsOperation,
|
---|
7610 | PVDINTERFACE pDstVDIfsImage,
|
---|
7611 | PVDINTERFACE pDstVDIfsOperation)
|
---|
7612 | {
|
---|
7613 | return VDCopyEx(pDiskFrom, nImage, pDiskTo, pszBackend, pszFilename, fMoveByRename,
|
---|
7614 | cbSize, VD_IMAGE_CONTENT_UNKNOWN, VD_IMAGE_CONTENT_UNKNOWN,
|
---|
7615 | uImageFlags, pDstUuid, uOpenFlags, pVDIfsOperation,
|
---|
7616 | pDstVDIfsImage, pDstVDIfsOperation);
|
---|
7617 | }
|
---|
7618 |
|
---|
7619 | /**
|
---|
7620 | * Optimizes the storage consumption of an image. Typically the unused blocks
|
---|
7621 | * have to be wiped with zeroes to achieve a substantial reduced storage use.
|
---|
7622 | * Another optimization done is reordering the image blocks, which can provide
|
---|
7623 | * a significant performance boost, as reads and writes tend to use less random
|
---|
7624 | * file offsets.
|
---|
7625 | *
|
---|
7626 | * @return VBox status code.
|
---|
7627 | * @return VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
7628 | * @return VERR_VD_IMAGE_READ_ONLY if image is not writable.
|
---|
7629 | * @return VERR_NOT_SUPPORTED if this kind of image can be compacted, but
|
---|
7630 | * the code for this isn't implemented yet.
|
---|
7631 | * @param pDisk Pointer to HDD container.
|
---|
7632 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
7633 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
7634 | */
|
---|
7635 | VBOXDDU_DECL(int) VDCompact(PVBOXHDD pDisk, unsigned nImage,
|
---|
7636 | PVDINTERFACE pVDIfsOperation)
|
---|
7637 | {
|
---|
7638 | int rc = VINF_SUCCESS;
|
---|
7639 | int rc2;
|
---|
7640 | bool fLockRead = false, fLockWrite = false;
|
---|
7641 | void *pvBuf = NULL;
|
---|
7642 | void *pvTmp = NULL;
|
---|
7643 |
|
---|
7644 | LogFlowFunc(("pDisk=%#p nImage=%u pVDIfsOperation=%#p\n",
|
---|
7645 | pDisk, nImage, pVDIfsOperation));
|
---|
7646 |
|
---|
7647 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
7648 |
|
---|
7649 | do {
|
---|
7650 | /* Check arguments. */
|
---|
7651 | AssertMsgBreakStmt(VALID_PTR(pDisk), ("pDisk=%#p\n", pDisk),
|
---|
7652 | rc = VERR_INVALID_PARAMETER);
|
---|
7653 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE,
|
---|
7654 | ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7655 |
|
---|
7656 | rc2 = vdThreadStartRead(pDisk);
|
---|
7657 | AssertRC(rc2);
|
---|
7658 | fLockRead = true;
|
---|
7659 |
|
---|
7660 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
7661 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
7662 |
|
---|
7663 | /* If there is no compact callback for not file based backends then
|
---|
7664 | * the backend doesn't need compaction. No need to make much fuss about
|
---|
7665 | * this. For file based ones signal this as not yet supported. */
|
---|
7666 | if (!pImage->Backend->pfnCompact)
|
---|
7667 | {
|
---|
7668 | if (pImage->Backend->uBackendCaps & VD_CAP_FILE)
|
---|
7669 | rc = VERR_NOT_SUPPORTED;
|
---|
7670 | else
|
---|
7671 | rc = VINF_SUCCESS;
|
---|
7672 | break;
|
---|
7673 | }
|
---|
7674 |
|
---|
7675 | /* Insert interface for reading parent state into per-operation list,
|
---|
7676 | * if there is a parent image. */
|
---|
7677 | VDINTERFACEPARENTSTATE VDIfParent;
|
---|
7678 | VDPARENTSTATEDESC ParentUser;
|
---|
7679 | if (pImage->pPrev)
|
---|
7680 | {
|
---|
7681 | VDIfParent.pfnParentRead = vdParentRead;
|
---|
7682 | ParentUser.pDisk = pDisk;
|
---|
7683 | ParentUser.pImage = pImage->pPrev;
|
---|
7684 | rc = VDInterfaceAdd(&VDIfParent.Core, "VDCompact_ParentState", VDINTERFACETYPE_PARENTSTATE,
|
---|
7685 | &ParentUser, sizeof(VDINTERFACEPARENTSTATE), &pVDIfsOperation);
|
---|
7686 | AssertRC(rc);
|
---|
7687 | }
|
---|
7688 |
|
---|
7689 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7690 | AssertRC(rc2);
|
---|
7691 | fLockRead = false;
|
---|
7692 |
|
---|
7693 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7694 | AssertRC(rc2);
|
---|
7695 | fLockWrite = true;
|
---|
7696 |
|
---|
7697 | rc = pImage->Backend->pfnCompact(pImage->pBackendData,
|
---|
7698 | 0, 99,
|
---|
7699 | pDisk->pVDIfsDisk,
|
---|
7700 | pImage->pVDIfsImage,
|
---|
7701 | pVDIfsOperation);
|
---|
7702 | } while (0);
|
---|
7703 |
|
---|
7704 | if (RT_UNLIKELY(fLockWrite))
|
---|
7705 | {
|
---|
7706 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7707 | AssertRC(rc2);
|
---|
7708 | }
|
---|
7709 | else if (RT_UNLIKELY(fLockRead))
|
---|
7710 | {
|
---|
7711 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7712 | AssertRC(rc2);
|
---|
7713 | }
|
---|
7714 |
|
---|
7715 | if (pvBuf)
|
---|
7716 | RTMemTmpFree(pvBuf);
|
---|
7717 | if (pvTmp)
|
---|
7718 | RTMemTmpFree(pvTmp);
|
---|
7719 |
|
---|
7720 | if (RT_SUCCESS(rc))
|
---|
7721 | {
|
---|
7722 | if (pIfProgress && pIfProgress->pfnProgress)
|
---|
7723 | pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
|
---|
7724 | }
|
---|
7725 |
|
---|
7726 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
7727 | return rc;
|
---|
7728 | }
|
---|
7729 |
|
---|
7730 | /**
|
---|
7731 | * Resizes the given disk image to the given size.
|
---|
7732 | *
|
---|
7733 | * @return VBox status
|
---|
7734 | * @return VERR_VD_IMAGE_READ_ONLY if image is not writable.
|
---|
7735 | * @return VERR_NOT_SUPPORTED if this kind of image can be compacted, but
|
---|
7736 | *
|
---|
7737 | * @param pDisk Pointer to the HDD container.
|
---|
7738 | * @param cbSize New size of the image.
|
---|
7739 | * @param pPCHSGeometry Pointer to the new physical disk geometry <= (16383,16,63). Not NULL.
|
---|
7740 | * @param pLCHSGeometry Pointer to the new logical disk geometry <= (x,255,63). Not NULL.
|
---|
7741 | * @param pVDIfsOperation Pointer to the per-operation VD interface list.
|
---|
7742 | */
|
---|
7743 | VBOXDDU_DECL(int) VDResize(PVBOXHDD pDisk, uint64_t cbSize,
|
---|
7744 | PCVDGEOMETRY pPCHSGeometry,
|
---|
7745 | PCVDGEOMETRY pLCHSGeometry,
|
---|
7746 | PVDINTERFACE pVDIfsOperation)
|
---|
7747 | {
|
---|
7748 | /** @todo r=klaus resizing was designed to be part of VDCopy, so having a separate function is not desirable. */
|
---|
7749 | int rc = VINF_SUCCESS;
|
---|
7750 | int rc2;
|
---|
7751 | bool fLockRead = false, fLockWrite = false;
|
---|
7752 |
|
---|
7753 | LogFlowFunc(("pDisk=%#p cbSize=%llu pVDIfsOperation=%#p\n",
|
---|
7754 | pDisk, cbSize, pVDIfsOperation));
|
---|
7755 |
|
---|
7756 | PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
|
---|
7757 |
|
---|
7758 | do {
|
---|
7759 | /* Check arguments. */
|
---|
7760 | AssertMsgBreakStmt(VALID_PTR(pDisk), ("pDisk=%#p\n", pDisk),
|
---|
7761 | rc = VERR_INVALID_PARAMETER);
|
---|
7762 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE,
|
---|
7763 | ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7764 |
|
---|
7765 | rc2 = vdThreadStartRead(pDisk);
|
---|
7766 | AssertRC(rc2);
|
---|
7767 | fLockRead = true;
|
---|
7768 |
|
---|
7769 | /* Must have at least one image in the chain, will resize last. */
|
---|
7770 | AssertMsgBreakStmt(pDisk->cImages >= 1, ("cImages=%u\n", pDisk->cImages),
|
---|
7771 | rc = VERR_NOT_SUPPORTED);
|
---|
7772 |
|
---|
7773 | PVDIMAGE pImage = pDisk->pLast;
|
---|
7774 |
|
---|
7775 | /* If there is no compact callback for not file based backends then
|
---|
7776 | * the backend doesn't need compaction. No need to make much fuss about
|
---|
7777 | * this. For file based ones signal this as not yet supported. */
|
---|
7778 | if (!pImage->Backend->pfnResize)
|
---|
7779 | {
|
---|
7780 | if (pImage->Backend->uBackendCaps & VD_CAP_FILE)
|
---|
7781 | rc = VERR_NOT_SUPPORTED;
|
---|
7782 | else
|
---|
7783 | rc = VINF_SUCCESS;
|
---|
7784 | break;
|
---|
7785 | }
|
---|
7786 |
|
---|
7787 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7788 | AssertRC(rc2);
|
---|
7789 | fLockRead = false;
|
---|
7790 |
|
---|
7791 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7792 | AssertRC(rc2);
|
---|
7793 | fLockWrite = true;
|
---|
7794 |
|
---|
7795 | VDGEOMETRY PCHSGeometryOld;
|
---|
7796 | VDGEOMETRY LCHSGeometryOld;
|
---|
7797 | PCVDGEOMETRY pPCHSGeometryNew;
|
---|
7798 | PCVDGEOMETRY pLCHSGeometryNew;
|
---|
7799 |
|
---|
7800 | if (pPCHSGeometry->cCylinders == 0)
|
---|
7801 | {
|
---|
7802 | /* Auto-detect marker, calculate new value ourself. */
|
---|
7803 | rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData, &PCHSGeometryOld);
|
---|
7804 | if (RT_SUCCESS(rc) && (PCHSGeometryOld.cCylinders != 0))
|
---|
7805 | PCHSGeometryOld.cCylinders = RT_MIN(cbSize / 512 / PCHSGeometryOld.cHeads / PCHSGeometryOld.cSectors, 16383);
|
---|
7806 | else if (rc == VERR_VD_GEOMETRY_NOT_SET)
|
---|
7807 | rc = VINF_SUCCESS;
|
---|
7808 |
|
---|
7809 | pPCHSGeometryNew = &PCHSGeometryOld;
|
---|
7810 | }
|
---|
7811 | else
|
---|
7812 | pPCHSGeometryNew = pPCHSGeometry;
|
---|
7813 |
|
---|
7814 | if (pLCHSGeometry->cCylinders == 0)
|
---|
7815 | {
|
---|
7816 | /* Auto-detect marker, calculate new value ourself. */
|
---|
7817 | rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData, &LCHSGeometryOld);
|
---|
7818 | if (RT_SUCCESS(rc) && (LCHSGeometryOld.cCylinders != 0))
|
---|
7819 | LCHSGeometryOld.cCylinders = cbSize / 512 / LCHSGeometryOld.cHeads / LCHSGeometryOld.cSectors;
|
---|
7820 | else if (rc == VERR_VD_GEOMETRY_NOT_SET)
|
---|
7821 | rc = VINF_SUCCESS;
|
---|
7822 |
|
---|
7823 | pLCHSGeometryNew = &LCHSGeometryOld;
|
---|
7824 | }
|
---|
7825 | else
|
---|
7826 | pLCHSGeometryNew = pLCHSGeometry;
|
---|
7827 |
|
---|
7828 | if (RT_SUCCESS(rc))
|
---|
7829 | rc = pImage->Backend->pfnResize(pImage->pBackendData,
|
---|
7830 | cbSize,
|
---|
7831 | pPCHSGeometryNew,
|
---|
7832 | pLCHSGeometryNew,
|
---|
7833 | 0, 99,
|
---|
7834 | pDisk->pVDIfsDisk,
|
---|
7835 | pImage->pVDIfsImage,
|
---|
7836 | pVDIfsOperation);
|
---|
7837 | } while (0);
|
---|
7838 |
|
---|
7839 | if (RT_UNLIKELY(fLockWrite))
|
---|
7840 | {
|
---|
7841 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7842 | AssertRC(rc2);
|
---|
7843 | }
|
---|
7844 | else if (RT_UNLIKELY(fLockRead))
|
---|
7845 | {
|
---|
7846 | rc2 = vdThreadFinishRead(pDisk);
|
---|
7847 | AssertRC(rc2);
|
---|
7848 | }
|
---|
7849 |
|
---|
7850 | if (RT_SUCCESS(rc))
|
---|
7851 | {
|
---|
7852 | if (pIfProgress && pIfProgress->pfnProgress)
|
---|
7853 | pIfProgress->pfnProgress(pIfProgress->Core.pvUser, 100);
|
---|
7854 |
|
---|
7855 | pDisk->cbSize = cbSize;
|
---|
7856 | }
|
---|
7857 |
|
---|
7858 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
7859 | return rc;
|
---|
7860 | }
|
---|
7861 |
|
---|
7862 | /**
|
---|
7863 | * Closes the last opened image file in HDD container.
|
---|
7864 | * If previous image file was opened in read-only mode (the normal case) and
|
---|
7865 | * the last opened image is in read-write mode then the previous image will be
|
---|
7866 | * reopened in read/write mode.
|
---|
7867 | *
|
---|
7868 | * @returns VBox status code.
|
---|
7869 | * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
|
---|
7870 | * @param pDisk Pointer to HDD container.
|
---|
7871 | * @param fDelete If true, delete the image from the host disk.
|
---|
7872 | */
|
---|
7873 | VBOXDDU_DECL(int) VDClose(PVBOXHDD pDisk, bool fDelete)
|
---|
7874 | {
|
---|
7875 | int rc = VINF_SUCCESS;
|
---|
7876 | int rc2;
|
---|
7877 | bool fLockWrite = false;
|
---|
7878 |
|
---|
7879 | LogFlowFunc(("pDisk=%#p fDelete=%d\n", pDisk, fDelete));
|
---|
7880 | do
|
---|
7881 | {
|
---|
7882 | /* sanity check */
|
---|
7883 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7884 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7885 |
|
---|
7886 | /* Not worth splitting this up into a read lock phase and write
|
---|
7887 | * lock phase, as closing an image is a relatively fast operation
|
---|
7888 | * dominated by the part which needs the write lock. */
|
---|
7889 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7890 | AssertRC(rc2);
|
---|
7891 | fLockWrite = true;
|
---|
7892 |
|
---|
7893 | PVDIMAGE pImage = pDisk->pLast;
|
---|
7894 | if (!pImage)
|
---|
7895 | {
|
---|
7896 | rc = VERR_VD_NOT_OPENED;
|
---|
7897 | break;
|
---|
7898 | }
|
---|
7899 |
|
---|
7900 | /* Destroy the current discard state first which might still have pending blocks. */
|
---|
7901 | rc = vdDiscardStateDestroy(pDisk);
|
---|
7902 | if (RT_FAILURE(rc))
|
---|
7903 | break;
|
---|
7904 |
|
---|
7905 | unsigned uOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
|
---|
7906 | /* Remove image from list of opened images. */
|
---|
7907 | vdRemoveImageFromList(pDisk, pImage);
|
---|
7908 | /* Close (and optionally delete) image. */
|
---|
7909 | rc = pImage->Backend->pfnClose(pImage->pBackendData, fDelete);
|
---|
7910 | /* Free remaining resources related to the image. */
|
---|
7911 | RTStrFree(pImage->pszFilename);
|
---|
7912 | RTMemFree(pImage);
|
---|
7913 |
|
---|
7914 | pImage = pDisk->pLast;
|
---|
7915 | if (!pImage)
|
---|
7916 | break;
|
---|
7917 |
|
---|
7918 | /* If disk was previously in read/write mode, make sure it will stay
|
---|
7919 | * like this (if possible) after closing this image. Set the open flags
|
---|
7920 | * accordingly. */
|
---|
7921 | if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
|
---|
7922 | {
|
---|
7923 | uOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
|
---|
7924 | uOpenFlags &= ~ VD_OPEN_FLAGS_READONLY;
|
---|
7925 | rc = pImage->Backend->pfnSetOpenFlags(pImage->pBackendData, uOpenFlags);
|
---|
7926 | }
|
---|
7927 |
|
---|
7928 | /* Cache disk information. */
|
---|
7929 | pDisk->cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
|
---|
7930 |
|
---|
7931 | /* Cache PCHS geometry. */
|
---|
7932 | rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
7933 | &pDisk->PCHSGeometry);
|
---|
7934 | if (RT_FAILURE(rc2))
|
---|
7935 | {
|
---|
7936 | pDisk->PCHSGeometry.cCylinders = 0;
|
---|
7937 | pDisk->PCHSGeometry.cHeads = 0;
|
---|
7938 | pDisk->PCHSGeometry.cSectors = 0;
|
---|
7939 | }
|
---|
7940 | else
|
---|
7941 | {
|
---|
7942 | /* Make sure the PCHS geometry is properly clipped. */
|
---|
7943 | pDisk->PCHSGeometry.cCylinders = RT_MIN(pDisk->PCHSGeometry.cCylinders, 16383);
|
---|
7944 | pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 16);
|
---|
7945 | pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
|
---|
7946 | }
|
---|
7947 |
|
---|
7948 | /* Cache LCHS geometry. */
|
---|
7949 | rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
7950 | &pDisk->LCHSGeometry);
|
---|
7951 | if (RT_FAILURE(rc2))
|
---|
7952 | {
|
---|
7953 | pDisk->LCHSGeometry.cCylinders = 0;
|
---|
7954 | pDisk->LCHSGeometry.cHeads = 0;
|
---|
7955 | pDisk->LCHSGeometry.cSectors = 0;
|
---|
7956 | }
|
---|
7957 | else
|
---|
7958 | {
|
---|
7959 | /* Make sure the LCHS geometry is properly clipped. */
|
---|
7960 | pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
|
---|
7961 | pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
|
---|
7962 | }
|
---|
7963 | } while (0);
|
---|
7964 |
|
---|
7965 | if (RT_UNLIKELY(fLockWrite))
|
---|
7966 | {
|
---|
7967 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
7968 | AssertRC(rc2);
|
---|
7969 | }
|
---|
7970 |
|
---|
7971 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
7972 | return rc;
|
---|
7973 | }
|
---|
7974 |
|
---|
7975 | /**
|
---|
7976 | * Closes the currently opened cache image file in HDD container.
|
---|
7977 | *
|
---|
7978 | * @return VBox status code.
|
---|
7979 | * @return VERR_VD_NOT_OPENED if no cache is opened in HDD container.
|
---|
7980 | * @param pDisk Pointer to HDD container.
|
---|
7981 | * @param fDelete If true, delete the image from the host disk.
|
---|
7982 | */
|
---|
7983 | VBOXDDU_DECL(int) VDCacheClose(PVBOXHDD pDisk, bool fDelete)
|
---|
7984 | {
|
---|
7985 | int rc = VINF_SUCCESS;
|
---|
7986 | int rc2;
|
---|
7987 | bool fLockWrite = false;
|
---|
7988 | PVDCACHE pCache = NULL;
|
---|
7989 |
|
---|
7990 | LogFlowFunc(("pDisk=%#p fDelete=%d\n", pDisk, fDelete));
|
---|
7991 |
|
---|
7992 | do
|
---|
7993 | {
|
---|
7994 | /* sanity check */
|
---|
7995 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
7996 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
7997 |
|
---|
7998 | rc2 = vdThreadStartWrite(pDisk);
|
---|
7999 | AssertRC(rc2);
|
---|
8000 | fLockWrite = true;
|
---|
8001 |
|
---|
8002 | AssertPtrBreakStmt(pDisk->pCache, rc = VERR_VD_CACHE_NOT_FOUND);
|
---|
8003 |
|
---|
8004 | pCache = pDisk->pCache;
|
---|
8005 | pDisk->pCache = NULL;
|
---|
8006 |
|
---|
8007 | pCache->Backend->pfnClose(pCache->pBackendData, fDelete);
|
---|
8008 | if (pCache->pszFilename)
|
---|
8009 | RTStrFree(pCache->pszFilename);
|
---|
8010 | RTMemFree(pCache);
|
---|
8011 | } while (0);
|
---|
8012 |
|
---|
8013 | if (RT_LIKELY(fLockWrite))
|
---|
8014 | {
|
---|
8015 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
8016 | AssertRC(rc2);
|
---|
8017 | }
|
---|
8018 |
|
---|
8019 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8020 | return rc;
|
---|
8021 | }
|
---|
8022 |
|
---|
8023 | /**
|
---|
8024 | * Closes all opened image files in HDD container.
|
---|
8025 | *
|
---|
8026 | * @returns VBox status code.
|
---|
8027 | * @param pDisk Pointer to HDD container.
|
---|
8028 | */
|
---|
8029 | VBOXDDU_DECL(int) VDCloseAll(PVBOXHDD pDisk)
|
---|
8030 | {
|
---|
8031 | int rc = VINF_SUCCESS;
|
---|
8032 | int rc2;
|
---|
8033 | bool fLockWrite = false;
|
---|
8034 |
|
---|
8035 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
8036 | do
|
---|
8037 | {
|
---|
8038 | /* sanity check */
|
---|
8039 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8040 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8041 |
|
---|
8042 | /* Lock the entire operation. */
|
---|
8043 | rc2 = vdThreadStartWrite(pDisk);
|
---|
8044 | AssertRC(rc2);
|
---|
8045 | fLockWrite = true;
|
---|
8046 |
|
---|
8047 | PVDCACHE pCache = pDisk->pCache;
|
---|
8048 | if (pCache)
|
---|
8049 | {
|
---|
8050 | rc2 = pCache->Backend->pfnClose(pCache->pBackendData, false);
|
---|
8051 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
8052 | rc = rc2;
|
---|
8053 |
|
---|
8054 | if (pCache->pszFilename)
|
---|
8055 | RTStrFree(pCache->pszFilename);
|
---|
8056 | RTMemFree(pCache);
|
---|
8057 | }
|
---|
8058 |
|
---|
8059 | PVDIMAGE pImage = pDisk->pLast;
|
---|
8060 | while (VALID_PTR(pImage))
|
---|
8061 | {
|
---|
8062 | PVDIMAGE pPrev = pImage->pPrev;
|
---|
8063 | /* Remove image from list of opened images. */
|
---|
8064 | vdRemoveImageFromList(pDisk, pImage);
|
---|
8065 | /* Close image. */
|
---|
8066 | rc2 = pImage->Backend->pfnClose(pImage->pBackendData, false);
|
---|
8067 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
8068 | rc = rc2;
|
---|
8069 | /* Free remaining resources related to the image. */
|
---|
8070 | RTStrFree(pImage->pszFilename);
|
---|
8071 | RTMemFree(pImage);
|
---|
8072 | pImage = pPrev;
|
---|
8073 | }
|
---|
8074 | Assert(!VALID_PTR(pDisk->pLast));
|
---|
8075 | } while (0);
|
---|
8076 |
|
---|
8077 | if (RT_UNLIKELY(fLockWrite))
|
---|
8078 | {
|
---|
8079 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
8080 | AssertRC(rc2);
|
---|
8081 | }
|
---|
8082 |
|
---|
8083 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8084 | return rc;
|
---|
8085 | }
|
---|
8086 |
|
---|
8087 | /**
|
---|
8088 | * Read data from virtual HDD.
|
---|
8089 | *
|
---|
8090 | * @returns VBox status code.
|
---|
8091 | * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
|
---|
8092 | * @param pDisk Pointer to HDD container.
|
---|
8093 | * @param uOffset Offset of first reading byte from start of disk.
|
---|
8094 | * @param pvBuf Pointer to buffer for reading data.
|
---|
8095 | * @param cbRead Number of bytes to read.
|
---|
8096 | */
|
---|
8097 | VBOXDDU_DECL(int) VDRead(PVBOXHDD pDisk, uint64_t uOffset, void *pvBuf,
|
---|
8098 | size_t cbRead)
|
---|
8099 | {
|
---|
8100 | int rc = VINF_SUCCESS;
|
---|
8101 | int rc2;
|
---|
8102 | bool fLockRead = false;
|
---|
8103 |
|
---|
8104 | LogFlowFunc(("pDisk=%#p uOffset=%llu pvBuf=%p cbRead=%zu\n",
|
---|
8105 | pDisk, uOffset, pvBuf, cbRead));
|
---|
8106 | do
|
---|
8107 | {
|
---|
8108 | /* sanity check */
|
---|
8109 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8110 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8111 |
|
---|
8112 | /* Check arguments. */
|
---|
8113 | AssertMsgBreakStmt(VALID_PTR(pvBuf),
|
---|
8114 | ("pvBuf=%#p\n", pvBuf),
|
---|
8115 | rc = VERR_INVALID_PARAMETER);
|
---|
8116 | AssertMsgBreakStmt(cbRead,
|
---|
8117 | ("cbRead=%zu\n", cbRead),
|
---|
8118 | rc = VERR_INVALID_PARAMETER);
|
---|
8119 |
|
---|
8120 | rc2 = vdThreadStartRead(pDisk);
|
---|
8121 | AssertRC(rc2);
|
---|
8122 | fLockRead = true;
|
---|
8123 |
|
---|
8124 | AssertMsgBreakStmt(uOffset + cbRead <= pDisk->cbSize,
|
---|
8125 | ("uOffset=%llu cbRead=%zu pDisk->cbSize=%llu\n",
|
---|
8126 | uOffset, cbRead, pDisk->cbSize),
|
---|
8127 | rc = VERR_INVALID_PARAMETER);
|
---|
8128 |
|
---|
8129 | PVDIMAGE pImage = pDisk->pLast;
|
---|
8130 | AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
|
---|
8131 |
|
---|
8132 | rc = vdReadHelper(pDisk, pImage, uOffset, pvBuf, cbRead,
|
---|
8133 | true /* fUpdateCache */);
|
---|
8134 | } while (0);
|
---|
8135 |
|
---|
8136 | if (RT_UNLIKELY(fLockRead))
|
---|
8137 | {
|
---|
8138 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8139 | AssertRC(rc2);
|
---|
8140 | }
|
---|
8141 |
|
---|
8142 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8143 | return rc;
|
---|
8144 | }
|
---|
8145 |
|
---|
8146 | /**
|
---|
8147 | * Write data to virtual HDD.
|
---|
8148 | *
|
---|
8149 | * @returns VBox status code.
|
---|
8150 | * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
|
---|
8151 | * @param pDisk Pointer to HDD container.
|
---|
8152 | * @param uOffset Offset of the first byte being
|
---|
8153 | * written from start of disk.
|
---|
8154 | * @param pvBuf Pointer to buffer for writing data.
|
---|
8155 | * @param cbWrite Number of bytes to write.
|
---|
8156 | */
|
---|
8157 | VBOXDDU_DECL(int) VDWrite(PVBOXHDD pDisk, uint64_t uOffset, const void *pvBuf,
|
---|
8158 | size_t cbWrite)
|
---|
8159 | {
|
---|
8160 | int rc = VINF_SUCCESS;
|
---|
8161 | int rc2;
|
---|
8162 | bool fLockWrite = false;
|
---|
8163 |
|
---|
8164 | LogFlowFunc(("pDisk=%#p uOffset=%llu pvBuf=%p cbWrite=%zu\n",
|
---|
8165 | pDisk, uOffset, pvBuf, cbWrite));
|
---|
8166 | do
|
---|
8167 | {
|
---|
8168 | /* sanity check */
|
---|
8169 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8170 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8171 |
|
---|
8172 | /* Check arguments. */
|
---|
8173 | AssertMsgBreakStmt(VALID_PTR(pvBuf),
|
---|
8174 | ("pvBuf=%#p\n", pvBuf),
|
---|
8175 | rc = VERR_INVALID_PARAMETER);
|
---|
8176 | AssertMsgBreakStmt(cbWrite,
|
---|
8177 | ("cbWrite=%zu\n", cbWrite),
|
---|
8178 | rc = VERR_INVALID_PARAMETER);
|
---|
8179 |
|
---|
8180 | rc2 = vdThreadStartWrite(pDisk);
|
---|
8181 | AssertRC(rc2);
|
---|
8182 | fLockWrite = true;
|
---|
8183 |
|
---|
8184 | AssertMsgBreakStmt(uOffset + cbWrite <= pDisk->cbSize,
|
---|
8185 | ("uOffset=%llu cbWrite=%zu pDisk->cbSize=%llu\n",
|
---|
8186 | uOffset, cbWrite, pDisk->cbSize),
|
---|
8187 | rc = VERR_INVALID_PARAMETER);
|
---|
8188 |
|
---|
8189 | PVDIMAGE pImage = pDisk->pLast;
|
---|
8190 | AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
|
---|
8191 |
|
---|
8192 | vdSetModifiedFlag(pDisk);
|
---|
8193 | rc = vdWriteHelper(pDisk, pImage, uOffset, pvBuf, cbWrite,
|
---|
8194 | true /* fUpdateCache */);
|
---|
8195 | if (RT_FAILURE(rc))
|
---|
8196 | break;
|
---|
8197 |
|
---|
8198 | /* If there is a merge (in the direction towards a parent) running
|
---|
8199 | * concurrently then we have to also "relay" the write to this parent,
|
---|
8200 | * as the merge position might be already past the position where
|
---|
8201 | * this write is going. The "context" of the write can come from the
|
---|
8202 | * natural chain, since merging either already did or will take care
|
---|
8203 | * of the "other" content which is might be needed to fill the block
|
---|
8204 | * to a full allocation size. The cache doesn't need to be touched
|
---|
8205 | * as this write is covered by the previous one. */
|
---|
8206 | if (RT_UNLIKELY(pDisk->pImageRelay))
|
---|
8207 | rc = vdWriteHelper(pDisk, pDisk->pImageRelay, uOffset,
|
---|
8208 | pvBuf, cbWrite, false /* fUpdateCache */);
|
---|
8209 | } while (0);
|
---|
8210 |
|
---|
8211 | if (RT_UNLIKELY(fLockWrite))
|
---|
8212 | {
|
---|
8213 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
8214 | AssertRC(rc2);
|
---|
8215 | }
|
---|
8216 |
|
---|
8217 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8218 | return rc;
|
---|
8219 | }
|
---|
8220 |
|
---|
8221 | /**
|
---|
8222 | * Make sure the on disk representation of a virtual HDD is up to date.
|
---|
8223 | *
|
---|
8224 | * @returns VBox status code.
|
---|
8225 | * @returns VERR_VD_NOT_OPENED if no image is opened in HDD container.
|
---|
8226 | * @param pDisk Pointer to HDD container.
|
---|
8227 | */
|
---|
8228 | VBOXDDU_DECL(int) VDFlush(PVBOXHDD pDisk)
|
---|
8229 | {
|
---|
8230 | int rc = VINF_SUCCESS;
|
---|
8231 | int rc2;
|
---|
8232 | bool fLockWrite = false;
|
---|
8233 |
|
---|
8234 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
8235 | do
|
---|
8236 | {
|
---|
8237 | /* sanity check */
|
---|
8238 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8239 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8240 |
|
---|
8241 | rc2 = vdThreadStartWrite(pDisk);
|
---|
8242 | AssertRC(rc2);
|
---|
8243 | fLockWrite = true;
|
---|
8244 |
|
---|
8245 | PVDIMAGE pImage = pDisk->pLast;
|
---|
8246 | AssertPtrBreakStmt(pImage, rc = VERR_VD_NOT_OPENED);
|
---|
8247 |
|
---|
8248 | PVDIOCTX pIoCtx = vdIoCtxRootAlloc(pDisk, VDIOCTXTXDIR_FLUSH, 0,
|
---|
8249 | 0, pDisk->pLast, NULL,
|
---|
8250 | vdIoCtxSyncComplete, pDisk, NULL,
|
---|
8251 | NULL, vdFlushHelperAsync,
|
---|
8252 | VDIOCTX_FLAGS_SYNC);
|
---|
8253 |
|
---|
8254 | if (!pIoCtx)
|
---|
8255 | {
|
---|
8256 | rc = VERR_NO_MEMORY;
|
---|
8257 | break;
|
---|
8258 | }
|
---|
8259 |
|
---|
8260 | rc = vdIoCtxProcessSync(pIoCtx);
|
---|
8261 | } while (0);
|
---|
8262 |
|
---|
8263 | if (RT_UNLIKELY(fLockWrite))
|
---|
8264 | {
|
---|
8265 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
8266 | AssertRC(rc2);
|
---|
8267 | }
|
---|
8268 |
|
---|
8269 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8270 | return rc;
|
---|
8271 | }
|
---|
8272 |
|
---|
8273 | /**
|
---|
8274 | * Get number of opened images in HDD container.
|
---|
8275 | *
|
---|
8276 | * @returns Number of opened images for HDD container. 0 if no images have been opened.
|
---|
8277 | * @param pDisk Pointer to HDD container.
|
---|
8278 | */
|
---|
8279 | VBOXDDU_DECL(unsigned) VDGetCount(PVBOXHDD pDisk)
|
---|
8280 | {
|
---|
8281 | unsigned cImages;
|
---|
8282 | int rc2;
|
---|
8283 | bool fLockRead = false;
|
---|
8284 |
|
---|
8285 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
8286 | do
|
---|
8287 | {
|
---|
8288 | /* sanity check */
|
---|
8289 | AssertPtrBreakStmt(pDisk, cImages = 0);
|
---|
8290 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8291 |
|
---|
8292 | rc2 = vdThreadStartRead(pDisk);
|
---|
8293 | AssertRC(rc2);
|
---|
8294 | fLockRead = true;
|
---|
8295 |
|
---|
8296 | cImages = pDisk->cImages;
|
---|
8297 | } while (0);
|
---|
8298 |
|
---|
8299 | if (RT_UNLIKELY(fLockRead))
|
---|
8300 | {
|
---|
8301 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8302 | AssertRC(rc2);
|
---|
8303 | }
|
---|
8304 |
|
---|
8305 | LogFlowFunc(("returns %u\n", cImages));
|
---|
8306 | return cImages;
|
---|
8307 | }
|
---|
8308 |
|
---|
8309 | /**
|
---|
8310 | * Get read/write mode of HDD container.
|
---|
8311 | *
|
---|
8312 | * @returns Virtual disk ReadOnly status.
|
---|
8313 | * @returns true if no image is opened in HDD container.
|
---|
8314 | * @param pDisk Pointer to HDD container.
|
---|
8315 | */
|
---|
8316 | VBOXDDU_DECL(bool) VDIsReadOnly(PVBOXHDD pDisk)
|
---|
8317 | {
|
---|
8318 | bool fReadOnly;
|
---|
8319 | int rc2;
|
---|
8320 | bool fLockRead = false;
|
---|
8321 |
|
---|
8322 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
8323 | do
|
---|
8324 | {
|
---|
8325 | /* sanity check */
|
---|
8326 | AssertPtrBreakStmt(pDisk, fReadOnly = false);
|
---|
8327 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8328 |
|
---|
8329 | rc2 = vdThreadStartRead(pDisk);
|
---|
8330 | AssertRC(rc2);
|
---|
8331 | fLockRead = true;
|
---|
8332 |
|
---|
8333 | PVDIMAGE pImage = pDisk->pLast;
|
---|
8334 | AssertPtrBreakStmt(pImage, fReadOnly = true);
|
---|
8335 |
|
---|
8336 | unsigned uOpenFlags;
|
---|
8337 | uOpenFlags = pDisk->pLast->Backend->pfnGetOpenFlags(pDisk->pLast->pBackendData);
|
---|
8338 | fReadOnly = !!(uOpenFlags & VD_OPEN_FLAGS_READONLY);
|
---|
8339 | } while (0);
|
---|
8340 |
|
---|
8341 | if (RT_UNLIKELY(fLockRead))
|
---|
8342 | {
|
---|
8343 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8344 | AssertRC(rc2);
|
---|
8345 | }
|
---|
8346 |
|
---|
8347 | LogFlowFunc(("returns %d\n", fReadOnly));
|
---|
8348 | return fReadOnly;
|
---|
8349 | }
|
---|
8350 |
|
---|
8351 | /**
|
---|
8352 | * Get total capacity of an image in HDD container.
|
---|
8353 | *
|
---|
8354 | * @returns Virtual disk size in bytes.
|
---|
8355 | * @returns 0 if no image with specified number was not opened.
|
---|
8356 | * @param pDisk Pointer to HDD container.
|
---|
8357 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
8358 | */
|
---|
8359 | VBOXDDU_DECL(uint64_t) VDGetSize(PVBOXHDD pDisk, unsigned nImage)
|
---|
8360 | {
|
---|
8361 | uint64_t cbSize;
|
---|
8362 | int rc2;
|
---|
8363 | bool fLockRead = false;
|
---|
8364 |
|
---|
8365 | LogFlowFunc(("pDisk=%#p nImage=%u\n", pDisk, nImage));
|
---|
8366 | do
|
---|
8367 | {
|
---|
8368 | /* sanity check */
|
---|
8369 | AssertPtrBreakStmt(pDisk, cbSize = 0);
|
---|
8370 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8371 |
|
---|
8372 | rc2 = vdThreadStartRead(pDisk);
|
---|
8373 | AssertRC(rc2);
|
---|
8374 | fLockRead = true;
|
---|
8375 |
|
---|
8376 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
8377 | AssertPtrBreakStmt(pImage, cbSize = 0);
|
---|
8378 | cbSize = pImage->Backend->pfnGetSize(pImage->pBackendData);
|
---|
8379 | } while (0);
|
---|
8380 |
|
---|
8381 | if (RT_UNLIKELY(fLockRead))
|
---|
8382 | {
|
---|
8383 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8384 | AssertRC(rc2);
|
---|
8385 | }
|
---|
8386 |
|
---|
8387 | LogFlowFunc(("returns %llu\n", cbSize));
|
---|
8388 | return cbSize;
|
---|
8389 | }
|
---|
8390 |
|
---|
8391 | /**
|
---|
8392 | * Get total file size of an image in HDD container.
|
---|
8393 | *
|
---|
8394 | * @returns Virtual disk size in bytes.
|
---|
8395 | * @returns 0 if no image is opened in HDD container.
|
---|
8396 | * @param pDisk Pointer to HDD container.
|
---|
8397 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
8398 | */
|
---|
8399 | VBOXDDU_DECL(uint64_t) VDGetFileSize(PVBOXHDD pDisk, unsigned nImage)
|
---|
8400 | {
|
---|
8401 | uint64_t cbSize;
|
---|
8402 | int rc2;
|
---|
8403 | bool fLockRead = false;
|
---|
8404 |
|
---|
8405 | LogFlowFunc(("pDisk=%#p nImage=%u\n", pDisk, nImage));
|
---|
8406 | do
|
---|
8407 | {
|
---|
8408 | /* sanity check */
|
---|
8409 | AssertPtrBreakStmt(pDisk, cbSize = 0);
|
---|
8410 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8411 |
|
---|
8412 | rc2 = vdThreadStartRead(pDisk);
|
---|
8413 | AssertRC(rc2);
|
---|
8414 | fLockRead = true;
|
---|
8415 |
|
---|
8416 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
8417 | AssertPtrBreakStmt(pImage, cbSize = 0);
|
---|
8418 | cbSize = pImage->Backend->pfnGetFileSize(pImage->pBackendData);
|
---|
8419 | } while (0);
|
---|
8420 |
|
---|
8421 | if (RT_UNLIKELY(fLockRead))
|
---|
8422 | {
|
---|
8423 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8424 | AssertRC(rc2);
|
---|
8425 | }
|
---|
8426 |
|
---|
8427 | LogFlowFunc(("returns %llu\n", cbSize));
|
---|
8428 | return cbSize;
|
---|
8429 | }
|
---|
8430 |
|
---|
8431 | /**
|
---|
8432 | * Get virtual disk PCHS geometry stored in HDD container.
|
---|
8433 | *
|
---|
8434 | * @returns VBox status code.
|
---|
8435 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
8436 | * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
|
---|
8437 | * @param pDisk Pointer to HDD container.
|
---|
8438 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
8439 | * @param pPCHSGeometry Where to store PCHS geometry. Not NULL.
|
---|
8440 | */
|
---|
8441 | VBOXDDU_DECL(int) VDGetPCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
|
---|
8442 | PVDGEOMETRY pPCHSGeometry)
|
---|
8443 | {
|
---|
8444 | int rc = VINF_SUCCESS;
|
---|
8445 | int rc2;
|
---|
8446 | bool fLockRead = false;
|
---|
8447 |
|
---|
8448 | LogFlowFunc(("pDisk=%#p nImage=%u pPCHSGeometry=%#p\n",
|
---|
8449 | pDisk, nImage, pPCHSGeometry));
|
---|
8450 | do
|
---|
8451 | {
|
---|
8452 | /* sanity check */
|
---|
8453 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8454 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8455 |
|
---|
8456 | /* Check arguments. */
|
---|
8457 | AssertMsgBreakStmt(VALID_PTR(pPCHSGeometry),
|
---|
8458 | ("pPCHSGeometry=%#p\n", pPCHSGeometry),
|
---|
8459 | rc = VERR_INVALID_PARAMETER);
|
---|
8460 |
|
---|
8461 | rc2 = vdThreadStartRead(pDisk);
|
---|
8462 | AssertRC(rc2);
|
---|
8463 | fLockRead = true;
|
---|
8464 |
|
---|
8465 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
8466 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
8467 |
|
---|
8468 | if (pImage == pDisk->pLast)
|
---|
8469 | {
|
---|
8470 | /* Use cached information if possible. */
|
---|
8471 | if (pDisk->PCHSGeometry.cCylinders != 0)
|
---|
8472 | *pPCHSGeometry = pDisk->PCHSGeometry;
|
---|
8473 | else
|
---|
8474 | rc = VERR_VD_GEOMETRY_NOT_SET;
|
---|
8475 | }
|
---|
8476 | else
|
---|
8477 | rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
8478 | pPCHSGeometry);
|
---|
8479 | } while (0);
|
---|
8480 |
|
---|
8481 | if (RT_UNLIKELY(fLockRead))
|
---|
8482 | {
|
---|
8483 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8484 | AssertRC(rc2);
|
---|
8485 | }
|
---|
8486 |
|
---|
8487 | LogFlowFunc(("%Rrc (PCHS=%u/%u/%u)\n", rc,
|
---|
8488 | pDisk->PCHSGeometry.cCylinders, pDisk->PCHSGeometry.cHeads,
|
---|
8489 | pDisk->PCHSGeometry.cSectors));
|
---|
8490 | return rc;
|
---|
8491 | }
|
---|
8492 |
|
---|
8493 | /**
|
---|
8494 | * Store virtual disk PCHS geometry in HDD container.
|
---|
8495 | *
|
---|
8496 | * Note that in case of unrecoverable error all images in HDD container will be closed.
|
---|
8497 | *
|
---|
8498 | * @returns VBox status code.
|
---|
8499 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
8500 | * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
|
---|
8501 | * @param pDisk Pointer to HDD container.
|
---|
8502 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
8503 | * @param pPCHSGeometry Where to load PCHS geometry from. Not NULL.
|
---|
8504 | */
|
---|
8505 | VBOXDDU_DECL(int) VDSetPCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
|
---|
8506 | PCVDGEOMETRY pPCHSGeometry)
|
---|
8507 | {
|
---|
8508 | int rc = VINF_SUCCESS;
|
---|
8509 | int rc2;
|
---|
8510 | bool fLockWrite = false;
|
---|
8511 |
|
---|
8512 | LogFlowFunc(("pDisk=%#p nImage=%u pPCHSGeometry=%#p PCHS=%u/%u/%u\n",
|
---|
8513 | pDisk, nImage, pPCHSGeometry, pPCHSGeometry->cCylinders,
|
---|
8514 | pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
|
---|
8515 | do
|
---|
8516 | {
|
---|
8517 | /* sanity check */
|
---|
8518 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8519 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8520 |
|
---|
8521 | /* Check arguments. */
|
---|
8522 | AssertMsgBreakStmt( VALID_PTR(pPCHSGeometry)
|
---|
8523 | && pPCHSGeometry->cHeads <= 16
|
---|
8524 | && pPCHSGeometry->cSectors <= 63,
|
---|
8525 | ("pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pPCHSGeometry,
|
---|
8526 | pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads,
|
---|
8527 | pPCHSGeometry->cSectors),
|
---|
8528 | rc = VERR_INVALID_PARAMETER);
|
---|
8529 |
|
---|
8530 | rc2 = vdThreadStartWrite(pDisk);
|
---|
8531 | AssertRC(rc2);
|
---|
8532 | fLockWrite = true;
|
---|
8533 |
|
---|
8534 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
8535 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
8536 |
|
---|
8537 | if (pImage == pDisk->pLast)
|
---|
8538 | {
|
---|
8539 | if ( pPCHSGeometry->cCylinders != pDisk->PCHSGeometry.cCylinders
|
---|
8540 | || pPCHSGeometry->cHeads != pDisk->PCHSGeometry.cHeads
|
---|
8541 | || pPCHSGeometry->cSectors != pDisk->PCHSGeometry.cSectors)
|
---|
8542 | {
|
---|
8543 | /* Only update geometry if it is changed. Avoids similar checks
|
---|
8544 | * in every backend. Most of the time the new geometry is set
|
---|
8545 | * to the previous values, so no need to go through the hassle
|
---|
8546 | * of updating an image which could be opened in read-only mode
|
---|
8547 | * right now. */
|
---|
8548 | rc = pImage->Backend->pfnSetPCHSGeometry(pImage->pBackendData,
|
---|
8549 | pPCHSGeometry);
|
---|
8550 |
|
---|
8551 | /* Cache new geometry values in any case. */
|
---|
8552 | rc2 = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
8553 | &pDisk->PCHSGeometry);
|
---|
8554 | if (RT_FAILURE(rc2))
|
---|
8555 | {
|
---|
8556 | pDisk->PCHSGeometry.cCylinders = 0;
|
---|
8557 | pDisk->PCHSGeometry.cHeads = 0;
|
---|
8558 | pDisk->PCHSGeometry.cSectors = 0;
|
---|
8559 | }
|
---|
8560 | else
|
---|
8561 | {
|
---|
8562 | /* Make sure the CHS geometry is properly clipped. */
|
---|
8563 | pDisk->PCHSGeometry.cHeads = RT_MIN(pDisk->PCHSGeometry.cHeads, 255);
|
---|
8564 | pDisk->PCHSGeometry.cSectors = RT_MIN(pDisk->PCHSGeometry.cSectors, 63);
|
---|
8565 | }
|
---|
8566 | }
|
---|
8567 | }
|
---|
8568 | else
|
---|
8569 | {
|
---|
8570 | VDGEOMETRY PCHS;
|
---|
8571 | rc = pImage->Backend->pfnGetPCHSGeometry(pImage->pBackendData,
|
---|
8572 | &PCHS);
|
---|
8573 | if ( RT_FAILURE(rc)
|
---|
8574 | || pPCHSGeometry->cCylinders != PCHS.cCylinders
|
---|
8575 | || pPCHSGeometry->cHeads != PCHS.cHeads
|
---|
8576 | || pPCHSGeometry->cSectors != PCHS.cSectors)
|
---|
8577 | {
|
---|
8578 | /* Only update geometry if it is changed. Avoids similar checks
|
---|
8579 | * in every backend. Most of the time the new geometry is set
|
---|
8580 | * to the previous values, so no need to go through the hassle
|
---|
8581 | * of updating an image which could be opened in read-only mode
|
---|
8582 | * right now. */
|
---|
8583 | rc = pImage->Backend->pfnSetPCHSGeometry(pImage->pBackendData,
|
---|
8584 | pPCHSGeometry);
|
---|
8585 | }
|
---|
8586 | }
|
---|
8587 | } while (0);
|
---|
8588 |
|
---|
8589 | if (RT_UNLIKELY(fLockWrite))
|
---|
8590 | {
|
---|
8591 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
8592 | AssertRC(rc2);
|
---|
8593 | }
|
---|
8594 |
|
---|
8595 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8596 | return rc;
|
---|
8597 | }
|
---|
8598 |
|
---|
8599 | /**
|
---|
8600 | * Get virtual disk LCHS geometry stored in HDD container.
|
---|
8601 | *
|
---|
8602 | * @returns VBox status code.
|
---|
8603 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
8604 | * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
|
---|
8605 | * @param pDisk Pointer to HDD container.
|
---|
8606 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
8607 | * @param pLCHSGeometry Where to store LCHS geometry. Not NULL.
|
---|
8608 | */
|
---|
8609 | VBOXDDU_DECL(int) VDGetLCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
|
---|
8610 | PVDGEOMETRY pLCHSGeometry)
|
---|
8611 | {
|
---|
8612 | int rc = VINF_SUCCESS;
|
---|
8613 | int rc2;
|
---|
8614 | bool fLockRead = false;
|
---|
8615 |
|
---|
8616 | LogFlowFunc(("pDisk=%#p nImage=%u pLCHSGeometry=%#p\n",
|
---|
8617 | pDisk, nImage, pLCHSGeometry));
|
---|
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 | /* Check arguments. */
|
---|
8625 | AssertMsgBreakStmt(VALID_PTR(pLCHSGeometry),
|
---|
8626 | ("pLCHSGeometry=%#p\n", pLCHSGeometry),
|
---|
8627 | rc = VERR_INVALID_PARAMETER);
|
---|
8628 |
|
---|
8629 | rc2 = vdThreadStartRead(pDisk);
|
---|
8630 | AssertRC(rc2);
|
---|
8631 | fLockRead = true;
|
---|
8632 |
|
---|
8633 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
8634 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
8635 |
|
---|
8636 | if (pImage == pDisk->pLast)
|
---|
8637 | {
|
---|
8638 | /* Use cached information if possible. */
|
---|
8639 | if (pDisk->LCHSGeometry.cCylinders != 0)
|
---|
8640 | *pLCHSGeometry = pDisk->LCHSGeometry;
|
---|
8641 | else
|
---|
8642 | rc = VERR_VD_GEOMETRY_NOT_SET;
|
---|
8643 | }
|
---|
8644 | else
|
---|
8645 | rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
8646 | pLCHSGeometry);
|
---|
8647 | } while (0);
|
---|
8648 |
|
---|
8649 | if (RT_UNLIKELY(fLockRead))
|
---|
8650 | {
|
---|
8651 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8652 | AssertRC(rc2);
|
---|
8653 | }
|
---|
8654 |
|
---|
8655 | LogFlowFunc((": %Rrc (LCHS=%u/%u/%u)\n", rc,
|
---|
8656 | pDisk->LCHSGeometry.cCylinders, pDisk->LCHSGeometry.cHeads,
|
---|
8657 | pDisk->LCHSGeometry.cSectors));
|
---|
8658 | return rc;
|
---|
8659 | }
|
---|
8660 |
|
---|
8661 | /**
|
---|
8662 | * Store virtual disk LCHS geometry in HDD container.
|
---|
8663 | *
|
---|
8664 | * Note that in case of unrecoverable error all images in HDD container will be closed.
|
---|
8665 | *
|
---|
8666 | * @returns VBox status code.
|
---|
8667 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
8668 | * @returns VERR_VD_GEOMETRY_NOT_SET if no geometry present in the HDD container.
|
---|
8669 | * @param pDisk Pointer to HDD container.
|
---|
8670 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
8671 | * @param pLCHSGeometry Where to load LCHS geometry from. Not NULL.
|
---|
8672 | */
|
---|
8673 | VBOXDDU_DECL(int) VDSetLCHSGeometry(PVBOXHDD pDisk, unsigned nImage,
|
---|
8674 | PCVDGEOMETRY pLCHSGeometry)
|
---|
8675 | {
|
---|
8676 | int rc = VINF_SUCCESS;
|
---|
8677 | int rc2;
|
---|
8678 | bool fLockWrite = false;
|
---|
8679 |
|
---|
8680 | LogFlowFunc(("pDisk=%#p nImage=%u pLCHSGeometry=%#p LCHS=%u/%u/%u\n",
|
---|
8681 | pDisk, nImage, pLCHSGeometry, pLCHSGeometry->cCylinders,
|
---|
8682 | pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
|
---|
8683 | do
|
---|
8684 | {
|
---|
8685 | /* sanity check */
|
---|
8686 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8687 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8688 |
|
---|
8689 | /* Check arguments. */
|
---|
8690 | AssertMsgBreakStmt( VALID_PTR(pLCHSGeometry)
|
---|
8691 | && pLCHSGeometry->cHeads <= 255
|
---|
8692 | && pLCHSGeometry->cSectors <= 63,
|
---|
8693 | ("pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pLCHSGeometry,
|
---|
8694 | pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads,
|
---|
8695 | pLCHSGeometry->cSectors),
|
---|
8696 | rc = VERR_INVALID_PARAMETER);
|
---|
8697 |
|
---|
8698 | rc2 = vdThreadStartWrite(pDisk);
|
---|
8699 | AssertRC(rc2);
|
---|
8700 | fLockWrite = true;
|
---|
8701 |
|
---|
8702 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
8703 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
8704 |
|
---|
8705 | if (pImage == pDisk->pLast)
|
---|
8706 | {
|
---|
8707 | if ( pLCHSGeometry->cCylinders != pDisk->LCHSGeometry.cCylinders
|
---|
8708 | || pLCHSGeometry->cHeads != pDisk->LCHSGeometry.cHeads
|
---|
8709 | || pLCHSGeometry->cSectors != pDisk->LCHSGeometry.cSectors)
|
---|
8710 | {
|
---|
8711 | /* Only update geometry if it is changed. Avoids similar checks
|
---|
8712 | * in every backend. Most of the time the new geometry is set
|
---|
8713 | * to the previous values, so no need to go through the hassle
|
---|
8714 | * of updating an image which could be opened in read-only mode
|
---|
8715 | * right now. */
|
---|
8716 | rc = pImage->Backend->pfnSetLCHSGeometry(pImage->pBackendData,
|
---|
8717 | pLCHSGeometry);
|
---|
8718 |
|
---|
8719 | /* Cache new geometry values in any case. */
|
---|
8720 | rc2 = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
8721 | &pDisk->LCHSGeometry);
|
---|
8722 | if (RT_FAILURE(rc2))
|
---|
8723 | {
|
---|
8724 | pDisk->LCHSGeometry.cCylinders = 0;
|
---|
8725 | pDisk->LCHSGeometry.cHeads = 0;
|
---|
8726 | pDisk->LCHSGeometry.cSectors = 0;
|
---|
8727 | }
|
---|
8728 | else
|
---|
8729 | {
|
---|
8730 | /* Make sure the CHS geometry is properly clipped. */
|
---|
8731 | pDisk->LCHSGeometry.cHeads = RT_MIN(pDisk->LCHSGeometry.cHeads, 255);
|
---|
8732 | pDisk->LCHSGeometry.cSectors = RT_MIN(pDisk->LCHSGeometry.cSectors, 63);
|
---|
8733 | }
|
---|
8734 | }
|
---|
8735 | }
|
---|
8736 | else
|
---|
8737 | {
|
---|
8738 | VDGEOMETRY LCHS;
|
---|
8739 | rc = pImage->Backend->pfnGetLCHSGeometry(pImage->pBackendData,
|
---|
8740 | &LCHS);
|
---|
8741 | if ( RT_FAILURE(rc)
|
---|
8742 | || pLCHSGeometry->cCylinders != LCHS.cCylinders
|
---|
8743 | || pLCHSGeometry->cHeads != LCHS.cHeads
|
---|
8744 | || pLCHSGeometry->cSectors != LCHS.cSectors)
|
---|
8745 | {
|
---|
8746 | /* Only update geometry if it is changed. Avoids similar checks
|
---|
8747 | * in every backend. Most of the time the new geometry is set
|
---|
8748 | * to the previous values, so no need to go through the hassle
|
---|
8749 | * of updating an image which could be opened in read-only mode
|
---|
8750 | * right now. */
|
---|
8751 | rc = pImage->Backend->pfnSetLCHSGeometry(pImage->pBackendData,
|
---|
8752 | pLCHSGeometry);
|
---|
8753 | }
|
---|
8754 | }
|
---|
8755 | } while (0);
|
---|
8756 |
|
---|
8757 | if (RT_UNLIKELY(fLockWrite))
|
---|
8758 | {
|
---|
8759 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
8760 | AssertRC(rc2);
|
---|
8761 | }
|
---|
8762 |
|
---|
8763 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8764 | return rc;
|
---|
8765 | }
|
---|
8766 |
|
---|
8767 | /**
|
---|
8768 | * Get version of image in HDD container.
|
---|
8769 | *
|
---|
8770 | * @returns VBox status code.
|
---|
8771 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
8772 | * @param pDisk Pointer to HDD container.
|
---|
8773 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
8774 | * @param puVersion Where to store the image version.
|
---|
8775 | */
|
---|
8776 | VBOXDDU_DECL(int) VDGetVersion(PVBOXHDD pDisk, unsigned nImage,
|
---|
8777 | unsigned *puVersion)
|
---|
8778 | {
|
---|
8779 | int rc = VINF_SUCCESS;
|
---|
8780 | int rc2;
|
---|
8781 | bool fLockRead = false;
|
---|
8782 |
|
---|
8783 | LogFlowFunc(("pDisk=%#p nImage=%u puVersion=%#p\n",
|
---|
8784 | pDisk, nImage, puVersion));
|
---|
8785 | do
|
---|
8786 | {
|
---|
8787 | /* sanity check */
|
---|
8788 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8789 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8790 |
|
---|
8791 | /* Check arguments. */
|
---|
8792 | AssertMsgBreakStmt(VALID_PTR(puVersion),
|
---|
8793 | ("puVersion=%#p\n", puVersion),
|
---|
8794 | rc = VERR_INVALID_PARAMETER);
|
---|
8795 |
|
---|
8796 | rc2 = vdThreadStartRead(pDisk);
|
---|
8797 | AssertRC(rc2);
|
---|
8798 | fLockRead = true;
|
---|
8799 |
|
---|
8800 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
8801 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
8802 |
|
---|
8803 | *puVersion = pImage->Backend->pfnGetVersion(pImage->pBackendData);
|
---|
8804 | } while (0);
|
---|
8805 |
|
---|
8806 | if (RT_UNLIKELY(fLockRead))
|
---|
8807 | {
|
---|
8808 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8809 | AssertRC(rc2);
|
---|
8810 | }
|
---|
8811 |
|
---|
8812 | LogFlowFunc(("returns %Rrc uVersion=%#x\n", rc, *puVersion));
|
---|
8813 | return rc;
|
---|
8814 | }
|
---|
8815 |
|
---|
8816 | /**
|
---|
8817 | * List the capabilities of image backend in HDD container.
|
---|
8818 | *
|
---|
8819 | * @returns VBox status code.
|
---|
8820 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
8821 | * @param pDisk Pointer to the HDD container.
|
---|
8822 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
8823 | * @param pbackendInfo Where to store the backend information.
|
---|
8824 | */
|
---|
8825 | VBOXDDU_DECL(int) VDBackendInfoSingle(PVBOXHDD pDisk, unsigned nImage,
|
---|
8826 | PVDBACKENDINFO pBackendInfo)
|
---|
8827 | {
|
---|
8828 | int rc = VINF_SUCCESS;
|
---|
8829 | int rc2;
|
---|
8830 | bool fLockRead = false;
|
---|
8831 |
|
---|
8832 | LogFlowFunc(("pDisk=%#p nImage=%u pBackendInfo=%#p\n",
|
---|
8833 | pDisk, nImage, pBackendInfo));
|
---|
8834 | do
|
---|
8835 | {
|
---|
8836 | /* sanity check */
|
---|
8837 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8838 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8839 |
|
---|
8840 | /* Check arguments. */
|
---|
8841 | AssertMsgBreakStmt(VALID_PTR(pBackendInfo),
|
---|
8842 | ("pBackendInfo=%#p\n", pBackendInfo),
|
---|
8843 | rc = VERR_INVALID_PARAMETER);
|
---|
8844 |
|
---|
8845 | rc2 = vdThreadStartRead(pDisk);
|
---|
8846 | AssertRC(rc2);
|
---|
8847 | fLockRead = true;
|
---|
8848 |
|
---|
8849 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
8850 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
8851 |
|
---|
8852 | pBackendInfo->pszBackend = pImage->Backend->pszBackendName;
|
---|
8853 | pBackendInfo->uBackendCaps = pImage->Backend->uBackendCaps;
|
---|
8854 | pBackendInfo->paFileExtensions = pImage->Backend->paFileExtensions;
|
---|
8855 | pBackendInfo->paConfigInfo = pImage->Backend->paConfigInfo;
|
---|
8856 | } while (0);
|
---|
8857 |
|
---|
8858 | if (RT_UNLIKELY(fLockRead))
|
---|
8859 | {
|
---|
8860 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8861 | AssertRC(rc2);
|
---|
8862 | }
|
---|
8863 |
|
---|
8864 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
8865 | return rc;
|
---|
8866 | }
|
---|
8867 |
|
---|
8868 | /**
|
---|
8869 | * Get flags of image in HDD container.
|
---|
8870 | *
|
---|
8871 | * @returns VBox status code.
|
---|
8872 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
8873 | * @param pDisk Pointer to HDD container.
|
---|
8874 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
8875 | * @param puImageFlags Where to store the image flags.
|
---|
8876 | */
|
---|
8877 | VBOXDDU_DECL(int) VDGetImageFlags(PVBOXHDD pDisk, unsigned nImage,
|
---|
8878 | unsigned *puImageFlags)
|
---|
8879 | {
|
---|
8880 | int rc = VINF_SUCCESS;
|
---|
8881 | int rc2;
|
---|
8882 | bool fLockRead = false;
|
---|
8883 |
|
---|
8884 | LogFlowFunc(("pDisk=%#p nImage=%u puImageFlags=%#p\n",
|
---|
8885 | pDisk, nImage, puImageFlags));
|
---|
8886 | do
|
---|
8887 | {
|
---|
8888 | /* sanity check */
|
---|
8889 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8890 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8891 |
|
---|
8892 | /* Check arguments. */
|
---|
8893 | AssertMsgBreakStmt(VALID_PTR(puImageFlags),
|
---|
8894 | ("puImageFlags=%#p\n", puImageFlags),
|
---|
8895 | rc = VERR_INVALID_PARAMETER);
|
---|
8896 |
|
---|
8897 | rc2 = vdThreadStartRead(pDisk);
|
---|
8898 | AssertRC(rc2);
|
---|
8899 | fLockRead = true;
|
---|
8900 |
|
---|
8901 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
8902 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
8903 |
|
---|
8904 | *puImageFlags = pImage->uImageFlags;
|
---|
8905 | } while (0);
|
---|
8906 |
|
---|
8907 | if (RT_UNLIKELY(fLockRead))
|
---|
8908 | {
|
---|
8909 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8910 | AssertRC(rc2);
|
---|
8911 | }
|
---|
8912 |
|
---|
8913 | LogFlowFunc(("returns %Rrc uImageFlags=%#x\n", rc, *puImageFlags));
|
---|
8914 | return rc;
|
---|
8915 | }
|
---|
8916 |
|
---|
8917 | /**
|
---|
8918 | * Get open flags of image in HDD container.
|
---|
8919 | *
|
---|
8920 | * @returns VBox status code.
|
---|
8921 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
8922 | * @param pDisk Pointer to HDD container.
|
---|
8923 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
8924 | * @param puOpenFlags Where to store the image open flags.
|
---|
8925 | */
|
---|
8926 | VBOXDDU_DECL(int) VDGetOpenFlags(PVBOXHDD pDisk, unsigned nImage,
|
---|
8927 | unsigned *puOpenFlags)
|
---|
8928 | {
|
---|
8929 | int rc = VINF_SUCCESS;
|
---|
8930 | int rc2;
|
---|
8931 | bool fLockRead = false;
|
---|
8932 |
|
---|
8933 | LogFlowFunc(("pDisk=%#p nImage=%u puOpenFlags=%#p\n",
|
---|
8934 | pDisk, nImage, puOpenFlags));
|
---|
8935 | do
|
---|
8936 | {
|
---|
8937 | /* sanity check */
|
---|
8938 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8939 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8940 |
|
---|
8941 | /* Check arguments. */
|
---|
8942 | AssertMsgBreakStmt(VALID_PTR(puOpenFlags),
|
---|
8943 | ("puOpenFlags=%#p\n", puOpenFlags),
|
---|
8944 | rc = VERR_INVALID_PARAMETER);
|
---|
8945 |
|
---|
8946 | rc2 = vdThreadStartRead(pDisk);
|
---|
8947 | AssertRC(rc2);
|
---|
8948 | fLockRead = true;
|
---|
8949 |
|
---|
8950 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
8951 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
8952 |
|
---|
8953 | *puOpenFlags = pImage->Backend->pfnGetOpenFlags(pImage->pBackendData);
|
---|
8954 | } while (0);
|
---|
8955 |
|
---|
8956 | if (RT_UNLIKELY(fLockRead))
|
---|
8957 | {
|
---|
8958 | rc2 = vdThreadFinishRead(pDisk);
|
---|
8959 | AssertRC(rc2);
|
---|
8960 | }
|
---|
8961 |
|
---|
8962 | LogFlowFunc(("returns %Rrc uOpenFlags=%#x\n", rc, *puOpenFlags));
|
---|
8963 | return rc;
|
---|
8964 | }
|
---|
8965 |
|
---|
8966 | /**
|
---|
8967 | * Set open flags of image in HDD container.
|
---|
8968 | * This operation may cause file locking changes and/or files being reopened.
|
---|
8969 | * Note that in case of unrecoverable error all images in HDD container will be closed.
|
---|
8970 | *
|
---|
8971 | * @returns VBox status code.
|
---|
8972 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
8973 | * @param pDisk Pointer to HDD container.
|
---|
8974 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
8975 | * @param uOpenFlags Image file open mode, see VD_OPEN_FLAGS_* constants.
|
---|
8976 | */
|
---|
8977 | VBOXDDU_DECL(int) VDSetOpenFlags(PVBOXHDD pDisk, unsigned nImage,
|
---|
8978 | unsigned uOpenFlags)
|
---|
8979 | {
|
---|
8980 | int rc;
|
---|
8981 | int rc2;
|
---|
8982 | bool fLockWrite = false;
|
---|
8983 |
|
---|
8984 | LogFlowFunc(("pDisk=%#p uOpenFlags=%#u\n", pDisk, uOpenFlags));
|
---|
8985 | do
|
---|
8986 | {
|
---|
8987 | /* sanity check */
|
---|
8988 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
8989 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
8990 |
|
---|
8991 | /* Check arguments. */
|
---|
8992 | AssertMsgBreakStmt((uOpenFlags & ~VD_OPEN_FLAGS_MASK) == 0,
|
---|
8993 | ("uOpenFlags=%#x\n", uOpenFlags),
|
---|
8994 | rc = VERR_INVALID_PARAMETER);
|
---|
8995 |
|
---|
8996 | rc2 = vdThreadStartWrite(pDisk);
|
---|
8997 | AssertRC(rc2);
|
---|
8998 | fLockWrite = true;
|
---|
8999 |
|
---|
9000 | /* Destroy any discard state because the image might be changed to readonly mode. */
|
---|
9001 | rc = vdDiscardStateDestroy(pDisk);
|
---|
9002 | if (RT_FAILURE(rc))
|
---|
9003 | break;
|
---|
9004 |
|
---|
9005 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9006 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9007 |
|
---|
9008 | rc = pImage->Backend->pfnSetOpenFlags(pImage->pBackendData,
|
---|
9009 | uOpenFlags & ~(VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_IGNORE_FLUSH | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS));
|
---|
9010 | if (RT_SUCCESS(rc))
|
---|
9011 | pImage->uOpenFlags = uOpenFlags & (VD_OPEN_FLAGS_HONOR_SAME | VD_OPEN_FLAGS_DISCARD | VD_OPEN_FLAGS_IGNORE_FLUSH | VD_OPEN_FLAGS_INFORM_ABOUT_ZERO_BLOCKS);
|
---|
9012 | } while (0);
|
---|
9013 |
|
---|
9014 | if (RT_UNLIKELY(fLockWrite))
|
---|
9015 | {
|
---|
9016 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
9017 | AssertRC(rc2);
|
---|
9018 | }
|
---|
9019 |
|
---|
9020 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9021 | return rc;
|
---|
9022 | }
|
---|
9023 |
|
---|
9024 | /**
|
---|
9025 | * Get base filename of image in HDD container. Some image formats use
|
---|
9026 | * other filenames as well, so don't use this for anything but informational
|
---|
9027 | * purposes.
|
---|
9028 | *
|
---|
9029 | * @returns VBox status code.
|
---|
9030 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9031 | * @returns VERR_BUFFER_OVERFLOW if pszFilename buffer too small to hold filename.
|
---|
9032 | * @param pDisk Pointer to HDD container.
|
---|
9033 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9034 | * @param pszFilename Where to store the image file name.
|
---|
9035 | * @param cbFilename Size of buffer pszFilename points to.
|
---|
9036 | */
|
---|
9037 | VBOXDDU_DECL(int) VDGetFilename(PVBOXHDD pDisk, unsigned nImage,
|
---|
9038 | char *pszFilename, unsigned cbFilename)
|
---|
9039 | {
|
---|
9040 | int rc;
|
---|
9041 | int rc2;
|
---|
9042 | bool fLockRead = false;
|
---|
9043 |
|
---|
9044 | LogFlowFunc(("pDisk=%#p nImage=%u pszFilename=%#p cbFilename=%u\n",
|
---|
9045 | pDisk, nImage, pszFilename, cbFilename));
|
---|
9046 | do
|
---|
9047 | {
|
---|
9048 | /* sanity check */
|
---|
9049 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9050 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9051 |
|
---|
9052 | /* Check arguments. */
|
---|
9053 | AssertMsgBreakStmt(VALID_PTR(pszFilename) && *pszFilename,
|
---|
9054 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
9055 | rc = VERR_INVALID_PARAMETER);
|
---|
9056 | AssertMsgBreakStmt(cbFilename,
|
---|
9057 | ("cbFilename=%u\n", cbFilename),
|
---|
9058 | rc = VERR_INVALID_PARAMETER);
|
---|
9059 |
|
---|
9060 | rc2 = vdThreadStartRead(pDisk);
|
---|
9061 | AssertRC(rc2);
|
---|
9062 | fLockRead = true;
|
---|
9063 |
|
---|
9064 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9065 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9066 |
|
---|
9067 | size_t cb = strlen(pImage->pszFilename);
|
---|
9068 | if (cb <= cbFilename)
|
---|
9069 | {
|
---|
9070 | strcpy(pszFilename, pImage->pszFilename);
|
---|
9071 | rc = VINF_SUCCESS;
|
---|
9072 | }
|
---|
9073 | else
|
---|
9074 | {
|
---|
9075 | strncpy(pszFilename, pImage->pszFilename, cbFilename - 1);
|
---|
9076 | pszFilename[cbFilename - 1] = '\0';
|
---|
9077 | rc = VERR_BUFFER_OVERFLOW;
|
---|
9078 | }
|
---|
9079 | } while (0);
|
---|
9080 |
|
---|
9081 | if (RT_UNLIKELY(fLockRead))
|
---|
9082 | {
|
---|
9083 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9084 | AssertRC(rc2);
|
---|
9085 | }
|
---|
9086 |
|
---|
9087 | LogFlowFunc(("returns %Rrc, pszFilename=\"%s\"\n", rc, pszFilename));
|
---|
9088 | return rc;
|
---|
9089 | }
|
---|
9090 |
|
---|
9091 | /**
|
---|
9092 | * Get the comment line of image in HDD container.
|
---|
9093 | *
|
---|
9094 | * @returns VBox status code.
|
---|
9095 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9096 | * @returns VERR_BUFFER_OVERFLOW if pszComment buffer too small to hold comment text.
|
---|
9097 | * @param pDisk Pointer to HDD container.
|
---|
9098 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9099 | * @param pszComment Where to store the comment string of image. NULL is ok.
|
---|
9100 | * @param cbComment The size of pszComment buffer. 0 is ok.
|
---|
9101 | */
|
---|
9102 | VBOXDDU_DECL(int) VDGetComment(PVBOXHDD pDisk, unsigned nImage,
|
---|
9103 | char *pszComment, unsigned cbComment)
|
---|
9104 | {
|
---|
9105 | int rc;
|
---|
9106 | int rc2;
|
---|
9107 | bool fLockRead = false;
|
---|
9108 |
|
---|
9109 | LogFlowFunc(("pDisk=%#p nImage=%u pszComment=%#p cbComment=%u\n",
|
---|
9110 | pDisk, nImage, pszComment, cbComment));
|
---|
9111 | do
|
---|
9112 | {
|
---|
9113 | /* sanity check */
|
---|
9114 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9115 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9116 |
|
---|
9117 | /* Check arguments. */
|
---|
9118 | AssertMsgBreakStmt(VALID_PTR(pszComment),
|
---|
9119 | ("pszComment=%#p \"%s\"\n", pszComment, pszComment),
|
---|
9120 | rc = VERR_INVALID_PARAMETER);
|
---|
9121 | AssertMsgBreakStmt(cbComment,
|
---|
9122 | ("cbComment=%u\n", cbComment),
|
---|
9123 | rc = VERR_INVALID_PARAMETER);
|
---|
9124 |
|
---|
9125 | rc2 = vdThreadStartRead(pDisk);
|
---|
9126 | AssertRC(rc2);
|
---|
9127 | fLockRead = true;
|
---|
9128 |
|
---|
9129 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9130 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9131 |
|
---|
9132 | rc = pImage->Backend->pfnGetComment(pImage->pBackendData, pszComment,
|
---|
9133 | cbComment);
|
---|
9134 | } while (0);
|
---|
9135 |
|
---|
9136 | if (RT_UNLIKELY(fLockRead))
|
---|
9137 | {
|
---|
9138 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9139 | AssertRC(rc2);
|
---|
9140 | }
|
---|
9141 |
|
---|
9142 | LogFlowFunc(("returns %Rrc, pszComment=\"%s\"\n", rc, pszComment));
|
---|
9143 | return rc;
|
---|
9144 | }
|
---|
9145 |
|
---|
9146 | /**
|
---|
9147 | * Changes the comment line of image in HDD container.
|
---|
9148 | *
|
---|
9149 | * @returns VBox status code.
|
---|
9150 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9151 | * @param pDisk Pointer to HDD container.
|
---|
9152 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9153 | * @param pszComment New comment string (UTF-8). NULL is allowed to reset the comment.
|
---|
9154 | */
|
---|
9155 | VBOXDDU_DECL(int) VDSetComment(PVBOXHDD pDisk, unsigned nImage,
|
---|
9156 | const char *pszComment)
|
---|
9157 | {
|
---|
9158 | int rc;
|
---|
9159 | int rc2;
|
---|
9160 | bool fLockWrite = false;
|
---|
9161 |
|
---|
9162 | LogFlowFunc(("pDisk=%#p nImage=%u pszComment=%#p \"%s\"\n",
|
---|
9163 | pDisk, nImage, pszComment, pszComment));
|
---|
9164 | do
|
---|
9165 | {
|
---|
9166 | /* sanity check */
|
---|
9167 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9168 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9169 |
|
---|
9170 | /* Check arguments. */
|
---|
9171 | AssertMsgBreakStmt(VALID_PTR(pszComment) || pszComment == NULL,
|
---|
9172 | ("pszComment=%#p \"%s\"\n", pszComment, pszComment),
|
---|
9173 | rc = VERR_INVALID_PARAMETER);
|
---|
9174 |
|
---|
9175 | rc2 = vdThreadStartWrite(pDisk);
|
---|
9176 | AssertRC(rc2);
|
---|
9177 | fLockWrite = true;
|
---|
9178 |
|
---|
9179 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9180 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9181 |
|
---|
9182 | rc = pImage->Backend->pfnSetComment(pImage->pBackendData, pszComment);
|
---|
9183 | } while (0);
|
---|
9184 |
|
---|
9185 | if (RT_UNLIKELY(fLockWrite))
|
---|
9186 | {
|
---|
9187 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
9188 | AssertRC(rc2);
|
---|
9189 | }
|
---|
9190 |
|
---|
9191 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9192 | return rc;
|
---|
9193 | }
|
---|
9194 |
|
---|
9195 |
|
---|
9196 | /**
|
---|
9197 | * Get UUID of image in HDD container.
|
---|
9198 | *
|
---|
9199 | * @returns VBox status code.
|
---|
9200 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9201 | * @param pDisk Pointer to HDD container.
|
---|
9202 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9203 | * @param pUuid Where to store the image creation UUID.
|
---|
9204 | */
|
---|
9205 | VBOXDDU_DECL(int) VDGetUuid(PVBOXHDD pDisk, unsigned nImage, PRTUUID pUuid)
|
---|
9206 | {
|
---|
9207 | int rc;
|
---|
9208 | int rc2;
|
---|
9209 | bool fLockRead = false;
|
---|
9210 |
|
---|
9211 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
|
---|
9212 | do
|
---|
9213 | {
|
---|
9214 | /* sanity check */
|
---|
9215 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9216 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9217 |
|
---|
9218 | /* Check arguments. */
|
---|
9219 | AssertMsgBreakStmt(VALID_PTR(pUuid),
|
---|
9220 | ("pUuid=%#p\n", pUuid),
|
---|
9221 | rc = VERR_INVALID_PARAMETER);
|
---|
9222 |
|
---|
9223 | rc2 = vdThreadStartRead(pDisk);
|
---|
9224 | AssertRC(rc2);
|
---|
9225 | fLockRead = true;
|
---|
9226 |
|
---|
9227 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9228 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9229 |
|
---|
9230 | rc = pImage->Backend->pfnGetUuid(pImage->pBackendData, pUuid);
|
---|
9231 | } while (0);
|
---|
9232 |
|
---|
9233 | if (RT_UNLIKELY(fLockRead))
|
---|
9234 | {
|
---|
9235 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9236 | AssertRC(rc2);
|
---|
9237 | }
|
---|
9238 |
|
---|
9239 | LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
|
---|
9240 | return rc;
|
---|
9241 | }
|
---|
9242 |
|
---|
9243 | /**
|
---|
9244 | * Set the image's UUID. Should not be used by normal applications.
|
---|
9245 | *
|
---|
9246 | * @returns VBox status code.
|
---|
9247 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9248 | * @param pDisk Pointer to HDD container.
|
---|
9249 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9250 | * @param pUuid New UUID of the image. If NULL, a new UUID is created.
|
---|
9251 | */
|
---|
9252 | VBOXDDU_DECL(int) VDSetUuid(PVBOXHDD pDisk, unsigned nImage, PCRTUUID pUuid)
|
---|
9253 | {
|
---|
9254 | int rc;
|
---|
9255 | int rc2;
|
---|
9256 | bool fLockWrite = false;
|
---|
9257 |
|
---|
9258 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
|
---|
9259 | pDisk, nImage, pUuid, pUuid));
|
---|
9260 | do
|
---|
9261 | {
|
---|
9262 | /* sanity check */
|
---|
9263 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9264 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9265 |
|
---|
9266 | AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
|
---|
9267 | ("pUuid=%#p\n", pUuid),
|
---|
9268 | rc = VERR_INVALID_PARAMETER);
|
---|
9269 |
|
---|
9270 | rc2 = vdThreadStartWrite(pDisk);
|
---|
9271 | AssertRC(rc2);
|
---|
9272 | fLockWrite = true;
|
---|
9273 |
|
---|
9274 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9275 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9276 |
|
---|
9277 | RTUUID Uuid;
|
---|
9278 | if (!pUuid)
|
---|
9279 | {
|
---|
9280 | RTUuidCreate(&Uuid);
|
---|
9281 | pUuid = &Uuid;
|
---|
9282 | }
|
---|
9283 | rc = pImage->Backend->pfnSetUuid(pImage->pBackendData, pUuid);
|
---|
9284 | } while (0);
|
---|
9285 |
|
---|
9286 | if (RT_UNLIKELY(fLockWrite))
|
---|
9287 | {
|
---|
9288 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
9289 | AssertRC(rc2);
|
---|
9290 | }
|
---|
9291 |
|
---|
9292 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9293 | return rc;
|
---|
9294 | }
|
---|
9295 |
|
---|
9296 | /**
|
---|
9297 | * Get last modification UUID of image in HDD container.
|
---|
9298 | *
|
---|
9299 | * @returns VBox status code.
|
---|
9300 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9301 | * @param pDisk Pointer to HDD container.
|
---|
9302 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9303 | * @param pUuid Where to store the image modification UUID.
|
---|
9304 | */
|
---|
9305 | VBOXDDU_DECL(int) VDGetModificationUuid(PVBOXHDD pDisk, unsigned nImage, PRTUUID pUuid)
|
---|
9306 | {
|
---|
9307 | int rc = VINF_SUCCESS;
|
---|
9308 | int rc2;
|
---|
9309 | bool fLockRead = false;
|
---|
9310 |
|
---|
9311 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
|
---|
9312 | do
|
---|
9313 | {
|
---|
9314 | /* sanity check */
|
---|
9315 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9316 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9317 |
|
---|
9318 | /* Check arguments. */
|
---|
9319 | AssertMsgBreakStmt(VALID_PTR(pUuid),
|
---|
9320 | ("pUuid=%#p\n", pUuid),
|
---|
9321 | rc = VERR_INVALID_PARAMETER);
|
---|
9322 |
|
---|
9323 | rc2 = vdThreadStartRead(pDisk);
|
---|
9324 | AssertRC(rc2);
|
---|
9325 | fLockRead = true;
|
---|
9326 |
|
---|
9327 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9328 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9329 |
|
---|
9330 | rc = pImage->Backend->pfnGetModificationUuid(pImage->pBackendData,
|
---|
9331 | pUuid);
|
---|
9332 | } while (0);
|
---|
9333 |
|
---|
9334 | if (RT_UNLIKELY(fLockRead))
|
---|
9335 | {
|
---|
9336 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9337 | AssertRC(rc2);
|
---|
9338 | }
|
---|
9339 |
|
---|
9340 | LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
|
---|
9341 | return rc;
|
---|
9342 | }
|
---|
9343 |
|
---|
9344 | /**
|
---|
9345 | * Set the image's last modification UUID. Should not be used by normal applications.
|
---|
9346 | *
|
---|
9347 | * @returns VBox status code.
|
---|
9348 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9349 | * @param pDisk Pointer to HDD container.
|
---|
9350 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9351 | * @param pUuid New modification UUID of the image. If NULL, a new UUID is created.
|
---|
9352 | */
|
---|
9353 | VBOXDDU_DECL(int) VDSetModificationUuid(PVBOXHDD pDisk, unsigned nImage, PCRTUUID pUuid)
|
---|
9354 | {
|
---|
9355 | int rc;
|
---|
9356 | int rc2;
|
---|
9357 | bool fLockWrite = false;
|
---|
9358 |
|
---|
9359 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
|
---|
9360 | pDisk, nImage, pUuid, pUuid));
|
---|
9361 | do
|
---|
9362 | {
|
---|
9363 | /* sanity check */
|
---|
9364 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9365 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9366 |
|
---|
9367 | /* Check arguments. */
|
---|
9368 | AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
|
---|
9369 | ("pUuid=%#p\n", pUuid),
|
---|
9370 | rc = VERR_INVALID_PARAMETER);
|
---|
9371 |
|
---|
9372 | rc2 = vdThreadStartWrite(pDisk);
|
---|
9373 | AssertRC(rc2);
|
---|
9374 | fLockWrite = true;
|
---|
9375 |
|
---|
9376 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9377 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9378 |
|
---|
9379 | RTUUID Uuid;
|
---|
9380 | if (!pUuid)
|
---|
9381 | {
|
---|
9382 | RTUuidCreate(&Uuid);
|
---|
9383 | pUuid = &Uuid;
|
---|
9384 | }
|
---|
9385 | rc = pImage->Backend->pfnSetModificationUuid(pImage->pBackendData,
|
---|
9386 | pUuid);
|
---|
9387 | } while (0);
|
---|
9388 |
|
---|
9389 | if (RT_UNLIKELY(fLockWrite))
|
---|
9390 | {
|
---|
9391 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
9392 | AssertRC(rc2);
|
---|
9393 | }
|
---|
9394 |
|
---|
9395 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9396 | return rc;
|
---|
9397 | }
|
---|
9398 |
|
---|
9399 | /**
|
---|
9400 | * Get parent UUID of image in HDD container.
|
---|
9401 | *
|
---|
9402 | * @returns VBox status code.
|
---|
9403 | * @returns VERR_VD_IMAGE_NOT_FOUND if image with specified number was not opened.
|
---|
9404 | * @param pDisk Pointer to HDD container.
|
---|
9405 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9406 | * @param pUuid Where to store the parent image UUID.
|
---|
9407 | */
|
---|
9408 | VBOXDDU_DECL(int) VDGetParentUuid(PVBOXHDD pDisk, unsigned nImage,
|
---|
9409 | PRTUUID pUuid)
|
---|
9410 | {
|
---|
9411 | int rc = VINF_SUCCESS;
|
---|
9412 | int rc2;
|
---|
9413 | bool fLockRead = false;
|
---|
9414 |
|
---|
9415 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p\n", pDisk, nImage, pUuid));
|
---|
9416 | do
|
---|
9417 | {
|
---|
9418 | /* sanity check */
|
---|
9419 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9420 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9421 |
|
---|
9422 | /* Check arguments. */
|
---|
9423 | AssertMsgBreakStmt(VALID_PTR(pUuid),
|
---|
9424 | ("pUuid=%#p\n", pUuid),
|
---|
9425 | rc = VERR_INVALID_PARAMETER);
|
---|
9426 |
|
---|
9427 | rc2 = vdThreadStartRead(pDisk);
|
---|
9428 | AssertRC(rc2);
|
---|
9429 | fLockRead = true;
|
---|
9430 |
|
---|
9431 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9432 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9433 |
|
---|
9434 | rc = pImage->Backend->pfnGetParentUuid(pImage->pBackendData, pUuid);
|
---|
9435 | } while (0);
|
---|
9436 |
|
---|
9437 | if (RT_UNLIKELY(fLockRead))
|
---|
9438 | {
|
---|
9439 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9440 | AssertRC(rc2);
|
---|
9441 | }
|
---|
9442 |
|
---|
9443 | LogFlowFunc(("returns %Rrc, Uuid={%RTuuid}\n", rc, pUuid));
|
---|
9444 | return rc;
|
---|
9445 | }
|
---|
9446 |
|
---|
9447 | /**
|
---|
9448 | * Set the image's parent UUID. Should not be used by normal applications.
|
---|
9449 | *
|
---|
9450 | * @returns VBox status code.
|
---|
9451 | * @param pDisk Pointer to HDD container.
|
---|
9452 | * @param nImage Image number, counts from 0. 0 is always base image of container.
|
---|
9453 | * @param pUuid New parent UUID of the image. If NULL, a new UUID is created.
|
---|
9454 | */
|
---|
9455 | VBOXDDU_DECL(int) VDSetParentUuid(PVBOXHDD pDisk, unsigned nImage,
|
---|
9456 | PCRTUUID pUuid)
|
---|
9457 | {
|
---|
9458 | int rc;
|
---|
9459 | int rc2;
|
---|
9460 | bool fLockWrite = false;
|
---|
9461 |
|
---|
9462 | LogFlowFunc(("pDisk=%#p nImage=%u pUuid=%#p {%RTuuid}\n",
|
---|
9463 | pDisk, nImage, pUuid, pUuid));
|
---|
9464 | do
|
---|
9465 | {
|
---|
9466 | /* sanity check */
|
---|
9467 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9468 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9469 |
|
---|
9470 | /* Check arguments. */
|
---|
9471 | AssertMsgBreakStmt(VALID_PTR(pUuid) || pUuid == NULL,
|
---|
9472 | ("pUuid=%#p\n", pUuid),
|
---|
9473 | rc = VERR_INVALID_PARAMETER);
|
---|
9474 |
|
---|
9475 | rc2 = vdThreadStartWrite(pDisk);
|
---|
9476 | AssertRC(rc2);
|
---|
9477 | fLockWrite = true;
|
---|
9478 |
|
---|
9479 | PVDIMAGE pImage = vdGetImageByNumber(pDisk, nImage);
|
---|
9480 | AssertPtrBreakStmt(pImage, rc = VERR_VD_IMAGE_NOT_FOUND);
|
---|
9481 |
|
---|
9482 | RTUUID Uuid;
|
---|
9483 | if (!pUuid)
|
---|
9484 | {
|
---|
9485 | RTUuidCreate(&Uuid);
|
---|
9486 | pUuid = &Uuid;
|
---|
9487 | }
|
---|
9488 | rc = pImage->Backend->pfnSetParentUuid(pImage->pBackendData, pUuid);
|
---|
9489 | } while (0);
|
---|
9490 |
|
---|
9491 | if (RT_UNLIKELY(fLockWrite))
|
---|
9492 | {
|
---|
9493 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
9494 | AssertRC(rc2);
|
---|
9495 | }
|
---|
9496 |
|
---|
9497 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9498 | return rc;
|
---|
9499 | }
|
---|
9500 |
|
---|
9501 |
|
---|
9502 | /**
|
---|
9503 | * Debug helper - dumps all opened images in HDD container into the log file.
|
---|
9504 | *
|
---|
9505 | * @param pDisk Pointer to HDD container.
|
---|
9506 | */
|
---|
9507 | VBOXDDU_DECL(void) VDDumpImages(PVBOXHDD pDisk)
|
---|
9508 | {
|
---|
9509 | int rc2;
|
---|
9510 | bool fLockRead = false;
|
---|
9511 |
|
---|
9512 | do
|
---|
9513 | {
|
---|
9514 | /* sanity check */
|
---|
9515 | AssertPtrBreak(pDisk);
|
---|
9516 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9517 |
|
---|
9518 | if (!pDisk->pInterfaceError || !VALID_PTR(pDisk->pInterfaceError->pfnMessage))
|
---|
9519 | pDisk->pInterfaceError->pfnMessage = vdLogMessage;
|
---|
9520 |
|
---|
9521 | rc2 = vdThreadStartRead(pDisk);
|
---|
9522 | AssertRC(rc2);
|
---|
9523 | fLockRead = true;
|
---|
9524 |
|
---|
9525 | vdMessageWrapper(pDisk, "--- Dumping VD Disk, Images=%u\n", pDisk->cImages);
|
---|
9526 | for (PVDIMAGE pImage = pDisk->pBase; pImage; pImage = pImage->pNext)
|
---|
9527 | {
|
---|
9528 | vdMessageWrapper(pDisk, "Dumping VD image \"%s\" (Backend=%s)\n",
|
---|
9529 | pImage->pszFilename, pImage->Backend->pszBackendName);
|
---|
9530 | pImage->Backend->pfnDump(pImage->pBackendData);
|
---|
9531 | }
|
---|
9532 | } while (0);
|
---|
9533 |
|
---|
9534 | if (RT_UNLIKELY(fLockRead))
|
---|
9535 | {
|
---|
9536 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9537 | AssertRC(rc2);
|
---|
9538 | }
|
---|
9539 | }
|
---|
9540 |
|
---|
9541 |
|
---|
9542 | VBOXDDU_DECL(int) VDDiscardRanges(PVBOXHDD pDisk, PCRTRANGE paRanges, unsigned cRanges)
|
---|
9543 | {
|
---|
9544 | int rc;
|
---|
9545 | int rc2;
|
---|
9546 | bool fLockWrite = false;
|
---|
9547 |
|
---|
9548 | LogFlowFunc(("pDisk=%#p paRanges=%#p cRanges=%u\n",
|
---|
9549 | pDisk, paRanges, cRanges));
|
---|
9550 | do
|
---|
9551 | {
|
---|
9552 | /* sanity check */
|
---|
9553 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9554 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9555 |
|
---|
9556 | /* Check arguments. */
|
---|
9557 | AssertMsgBreakStmt(cRanges,
|
---|
9558 | ("cRanges=%u\n", cRanges),
|
---|
9559 | rc = VERR_INVALID_PARAMETER);
|
---|
9560 | AssertMsgBreakStmt(VALID_PTR(paRanges),
|
---|
9561 | ("paRanges=%#p\n", paRanges),
|
---|
9562 | rc = VERR_INVALID_PARAMETER);
|
---|
9563 |
|
---|
9564 | rc2 = vdThreadStartWrite(pDisk);
|
---|
9565 | AssertRC(rc2);
|
---|
9566 | fLockWrite = true;
|
---|
9567 |
|
---|
9568 | AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
|
---|
9569 |
|
---|
9570 | AssertMsgBreakStmt(pDisk->pLast->uOpenFlags & VD_OPEN_FLAGS_DISCARD,
|
---|
9571 | ("Discarding not supported\n"),
|
---|
9572 | rc = VERR_NOT_SUPPORTED);
|
---|
9573 |
|
---|
9574 | PVDIOCTX pIoCtx = vdIoCtxDiscardAlloc(pDisk, paRanges, cRanges,
|
---|
9575 | vdIoCtxSyncComplete, pDisk, NULL, NULL,
|
---|
9576 | vdDiscardHelperAsync,
|
---|
9577 | VDIOCTX_FLAGS_SYNC);
|
---|
9578 | if (!pIoCtx)
|
---|
9579 | {
|
---|
9580 | rc = VERR_NO_MEMORY;
|
---|
9581 | break;
|
---|
9582 | }
|
---|
9583 |
|
---|
9584 | rc = vdIoCtxProcessSync(pIoCtx);
|
---|
9585 | } while (0);
|
---|
9586 |
|
---|
9587 | if (RT_UNLIKELY(fLockWrite))
|
---|
9588 | {
|
---|
9589 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
9590 | AssertRC(rc2);
|
---|
9591 | }
|
---|
9592 |
|
---|
9593 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9594 | return rc;
|
---|
9595 | }
|
---|
9596 |
|
---|
9597 |
|
---|
9598 | VBOXDDU_DECL(int) VDAsyncRead(PVBOXHDD pDisk, uint64_t uOffset, size_t cbRead,
|
---|
9599 | PCRTSGBUF pcSgBuf,
|
---|
9600 | PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
9601 | void *pvUser1, void *pvUser2)
|
---|
9602 | {
|
---|
9603 | int rc = VERR_VD_BLOCK_FREE;
|
---|
9604 | int rc2;
|
---|
9605 | bool fLockRead = false;
|
---|
9606 | PVDIOCTX pIoCtx = NULL;
|
---|
9607 |
|
---|
9608 | LogFlowFunc(("pDisk=%#p uOffset=%llu pcSgBuf=%#p cbRead=%zu pvUser1=%#p pvUser2=%#p\n",
|
---|
9609 | pDisk, uOffset, pcSgBuf, cbRead, pvUser1, pvUser2));
|
---|
9610 |
|
---|
9611 | do
|
---|
9612 | {
|
---|
9613 | /* sanity check */
|
---|
9614 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9615 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9616 |
|
---|
9617 | /* Check arguments. */
|
---|
9618 | AssertMsgBreakStmt(cbRead,
|
---|
9619 | ("cbRead=%zu\n", cbRead),
|
---|
9620 | rc = VERR_INVALID_PARAMETER);
|
---|
9621 | AssertMsgBreakStmt(VALID_PTR(pcSgBuf),
|
---|
9622 | ("pcSgBuf=%#p\n", pcSgBuf),
|
---|
9623 | rc = VERR_INVALID_PARAMETER);
|
---|
9624 |
|
---|
9625 | rc2 = vdThreadStartRead(pDisk);
|
---|
9626 | AssertRC(rc2);
|
---|
9627 | fLockRead = true;
|
---|
9628 |
|
---|
9629 | AssertMsgBreakStmt(uOffset + cbRead <= pDisk->cbSize,
|
---|
9630 | ("uOffset=%llu cbRead=%zu pDisk->cbSize=%llu\n",
|
---|
9631 | uOffset, cbRead, pDisk->cbSize),
|
---|
9632 | rc = VERR_INVALID_PARAMETER);
|
---|
9633 | AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
|
---|
9634 |
|
---|
9635 | pIoCtx = vdIoCtxRootAlloc(pDisk, VDIOCTXTXDIR_READ, uOffset,
|
---|
9636 | cbRead, pDisk->pLast, pcSgBuf,
|
---|
9637 | pfnComplete, pvUser1, pvUser2,
|
---|
9638 | NULL, vdReadHelperAsync,
|
---|
9639 | VDIOCTX_FLAGS_DEFAULT);
|
---|
9640 | if (!pIoCtx)
|
---|
9641 | {
|
---|
9642 | rc = VERR_NO_MEMORY;
|
---|
9643 | break;
|
---|
9644 | }
|
---|
9645 |
|
---|
9646 | rc = vdIoCtxProcessTryLockDefer(pIoCtx);
|
---|
9647 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
9648 | {
|
---|
9649 | if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
|
---|
9650 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
9651 | else
|
---|
9652 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
|
---|
9653 | }
|
---|
9654 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
|
---|
9655 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
9656 |
|
---|
9657 | } while (0);
|
---|
9658 |
|
---|
9659 | if (RT_UNLIKELY(fLockRead) && ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
9660 | || rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
9661 | {
|
---|
9662 | rc2 = vdThreadFinishRead(pDisk);
|
---|
9663 | AssertRC(rc2);
|
---|
9664 | }
|
---|
9665 |
|
---|
9666 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9667 | return rc;
|
---|
9668 | }
|
---|
9669 |
|
---|
9670 |
|
---|
9671 | VBOXDDU_DECL(int) VDAsyncWrite(PVBOXHDD pDisk, uint64_t uOffset, size_t cbWrite,
|
---|
9672 | PCRTSGBUF pcSgBuf,
|
---|
9673 | PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
9674 | void *pvUser1, void *pvUser2)
|
---|
9675 | {
|
---|
9676 | int rc;
|
---|
9677 | int rc2;
|
---|
9678 | bool fLockWrite = false;
|
---|
9679 | PVDIOCTX pIoCtx = NULL;
|
---|
9680 |
|
---|
9681 | LogFlowFunc(("pDisk=%#p uOffset=%llu cSgBuf=%#p cbWrite=%zu pvUser1=%#p pvUser2=%#p\n",
|
---|
9682 | pDisk, uOffset, pcSgBuf, cbWrite, pvUser1, pvUser2));
|
---|
9683 | do
|
---|
9684 | {
|
---|
9685 | /* sanity check */
|
---|
9686 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9687 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9688 |
|
---|
9689 | /* Check arguments. */
|
---|
9690 | AssertMsgBreakStmt(cbWrite,
|
---|
9691 | ("cbWrite=%zu\n", cbWrite),
|
---|
9692 | rc = VERR_INVALID_PARAMETER);
|
---|
9693 | AssertMsgBreakStmt(VALID_PTR(pcSgBuf),
|
---|
9694 | ("pcSgBuf=%#p\n", pcSgBuf),
|
---|
9695 | rc = VERR_INVALID_PARAMETER);
|
---|
9696 |
|
---|
9697 | rc2 = vdThreadStartWrite(pDisk);
|
---|
9698 | AssertRC(rc2);
|
---|
9699 | fLockWrite = true;
|
---|
9700 |
|
---|
9701 | AssertMsgBreakStmt(uOffset + cbWrite <= pDisk->cbSize,
|
---|
9702 | ("uOffset=%llu cbWrite=%zu pDisk->cbSize=%llu\n",
|
---|
9703 | uOffset, cbWrite, pDisk->cbSize),
|
---|
9704 | rc = VERR_INVALID_PARAMETER);
|
---|
9705 | AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
|
---|
9706 |
|
---|
9707 | pIoCtx = vdIoCtxRootAlloc(pDisk, VDIOCTXTXDIR_WRITE, uOffset,
|
---|
9708 | cbWrite, pDisk->pLast, pcSgBuf,
|
---|
9709 | pfnComplete, pvUser1, pvUser2,
|
---|
9710 | NULL, vdWriteHelperAsync,
|
---|
9711 | VDIOCTX_FLAGS_DEFAULT);
|
---|
9712 | if (!pIoCtx)
|
---|
9713 | {
|
---|
9714 | rc = VERR_NO_MEMORY;
|
---|
9715 | break;
|
---|
9716 | }
|
---|
9717 |
|
---|
9718 | rc = vdIoCtxProcessTryLockDefer(pIoCtx);
|
---|
9719 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
9720 | {
|
---|
9721 | if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
|
---|
9722 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
9723 | else
|
---|
9724 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
|
---|
9725 | }
|
---|
9726 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
|
---|
9727 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
9728 | } while (0);
|
---|
9729 |
|
---|
9730 | if (RT_UNLIKELY(fLockWrite) && ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
9731 | || rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
9732 | {
|
---|
9733 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
9734 | AssertRC(rc2);
|
---|
9735 | }
|
---|
9736 |
|
---|
9737 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9738 | return rc;
|
---|
9739 | }
|
---|
9740 |
|
---|
9741 |
|
---|
9742 | VBOXDDU_DECL(int) VDAsyncFlush(PVBOXHDD pDisk, PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
9743 | void *pvUser1, void *pvUser2)
|
---|
9744 | {
|
---|
9745 | int rc;
|
---|
9746 | int rc2;
|
---|
9747 | bool fLockWrite = false;
|
---|
9748 | PVDIOCTX pIoCtx = NULL;
|
---|
9749 |
|
---|
9750 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
9751 |
|
---|
9752 | do
|
---|
9753 | {
|
---|
9754 | /* sanity check */
|
---|
9755 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9756 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9757 |
|
---|
9758 | rc2 = vdThreadStartWrite(pDisk);
|
---|
9759 | AssertRC(rc2);
|
---|
9760 | fLockWrite = true;
|
---|
9761 |
|
---|
9762 | AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
|
---|
9763 |
|
---|
9764 | pIoCtx = vdIoCtxRootAlloc(pDisk, VDIOCTXTXDIR_FLUSH, 0,
|
---|
9765 | 0, pDisk->pLast, NULL,
|
---|
9766 | pfnComplete, pvUser1, pvUser2,
|
---|
9767 | NULL, vdFlushHelperAsync,
|
---|
9768 | VDIOCTX_FLAGS_DEFAULT);
|
---|
9769 | if (!pIoCtx)
|
---|
9770 | {
|
---|
9771 | rc = VERR_NO_MEMORY;
|
---|
9772 | break;
|
---|
9773 | }
|
---|
9774 |
|
---|
9775 | rc = vdIoCtxProcessTryLockDefer(pIoCtx);
|
---|
9776 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
9777 | {
|
---|
9778 | if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
|
---|
9779 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
9780 | else
|
---|
9781 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
|
---|
9782 | }
|
---|
9783 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
|
---|
9784 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
9785 | } while (0);
|
---|
9786 |
|
---|
9787 | if (RT_UNLIKELY(fLockWrite) && ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
9788 | || rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
9789 | {
|
---|
9790 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
9791 | AssertRC(rc2);
|
---|
9792 | }
|
---|
9793 |
|
---|
9794 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9795 | return rc;
|
---|
9796 | }
|
---|
9797 |
|
---|
9798 | VBOXDDU_DECL(int) VDAsyncDiscardRanges(PVBOXHDD pDisk, PCRTRANGE paRanges, unsigned cRanges,
|
---|
9799 | PFNVDASYNCTRANSFERCOMPLETE pfnComplete,
|
---|
9800 | void *pvUser1, void *pvUser2)
|
---|
9801 | {
|
---|
9802 | int rc;
|
---|
9803 | int rc2;
|
---|
9804 | bool fLockWrite = false;
|
---|
9805 | PVDIOCTX pIoCtx = NULL;
|
---|
9806 |
|
---|
9807 | LogFlowFunc(("pDisk=%#p\n", pDisk));
|
---|
9808 |
|
---|
9809 | do
|
---|
9810 | {
|
---|
9811 | /* sanity check */
|
---|
9812 | AssertPtrBreakStmt(pDisk, rc = VERR_INVALID_PARAMETER);
|
---|
9813 | AssertMsg(pDisk->u32Signature == VBOXHDDDISK_SIGNATURE, ("u32Signature=%08x\n", pDisk->u32Signature));
|
---|
9814 |
|
---|
9815 | rc2 = vdThreadStartWrite(pDisk);
|
---|
9816 | AssertRC(rc2);
|
---|
9817 | fLockWrite = true;
|
---|
9818 |
|
---|
9819 | AssertPtrBreakStmt(pDisk->pLast, rc = VERR_VD_NOT_OPENED);
|
---|
9820 |
|
---|
9821 | pIoCtx = vdIoCtxDiscardAlloc(pDisk, paRanges, cRanges,
|
---|
9822 | pfnComplete, pvUser1, pvUser2, NULL,
|
---|
9823 | vdDiscardHelperAsync,
|
---|
9824 | VDIOCTX_FLAGS_DEFAULT);
|
---|
9825 | if (!pIoCtx)
|
---|
9826 | {
|
---|
9827 | rc = VERR_NO_MEMORY;
|
---|
9828 | break;
|
---|
9829 | }
|
---|
9830 |
|
---|
9831 | rc = vdIoCtxProcessTryLockDefer(pIoCtx);
|
---|
9832 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
9833 | {
|
---|
9834 | if (ASMAtomicCmpXchgBool(&pIoCtx->fComplete, true, false))
|
---|
9835 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
9836 | else
|
---|
9837 | rc = VERR_VD_ASYNC_IO_IN_PROGRESS; /* Let the other handler complete the request. */
|
---|
9838 | }
|
---|
9839 | else if (rc != VERR_VD_ASYNC_IO_IN_PROGRESS) /* Another error */
|
---|
9840 | vdIoCtxFree(pDisk, pIoCtx);
|
---|
9841 | } while (0);
|
---|
9842 |
|
---|
9843 | if (RT_UNLIKELY(fLockWrite) && ( rc == VINF_VD_ASYNC_IO_FINISHED
|
---|
9844 | || rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
|
---|
9845 | {
|
---|
9846 | rc2 = vdThreadFinishWrite(pDisk);
|
---|
9847 | AssertRC(rc2);
|
---|
9848 | }
|
---|
9849 |
|
---|
9850 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9851 | return rc;
|
---|
9852 | }
|
---|
9853 |
|
---|
9854 | VBOXDDU_DECL(int) VDRepair(PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
|
---|
9855 | const char *pszFilename, const char *pszBackend,
|
---|
9856 | uint32_t fFlags)
|
---|
9857 | {
|
---|
9858 | int rc = VERR_NOT_SUPPORTED;
|
---|
9859 | PCVBOXHDDBACKEND pBackend = NULL;
|
---|
9860 | VDINTERFACEIOINT VDIfIoInt;
|
---|
9861 | VDINTERFACEIO VDIfIoFallback;
|
---|
9862 | PVDINTERFACEIO pInterfaceIo;
|
---|
9863 |
|
---|
9864 | LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
|
---|
9865 | /* Check arguments. */
|
---|
9866 | AssertMsgReturn(VALID_PTR(pszFilename) && *pszFilename,
|
---|
9867 | ("pszFilename=%#p \"%s\"\n", pszFilename, pszFilename),
|
---|
9868 | VERR_INVALID_PARAMETER);
|
---|
9869 | AssertMsgReturn(VALID_PTR(pszBackend),
|
---|
9870 | ("pszBackend=%#p\n", pszBackend),
|
---|
9871 | VERR_INVALID_PARAMETER);
|
---|
9872 | AssertMsgReturn((fFlags & ~VD_REPAIR_FLAGS_MASK) == 0,
|
---|
9873 | ("fFlags=%#x\n", fFlags),
|
---|
9874 | VERR_INVALID_PARAMETER);
|
---|
9875 |
|
---|
9876 | pInterfaceIo = VDIfIoGet(pVDIfsImage);
|
---|
9877 | if (!pInterfaceIo)
|
---|
9878 | {
|
---|
9879 | /*
|
---|
9880 | * Caller doesn't provide an I/O interface, create our own using the
|
---|
9881 | * native file API.
|
---|
9882 | */
|
---|
9883 | vdIfIoFallbackCallbacksSetup(&VDIfIoFallback);
|
---|
9884 | pInterfaceIo = &VDIfIoFallback;
|
---|
9885 | }
|
---|
9886 |
|
---|
9887 | /* Set up the internal I/O interface. */
|
---|
9888 | AssertReturn(!VDIfIoIntGet(pVDIfsImage), VERR_INVALID_PARAMETER);
|
---|
9889 | VDIfIoInt.pfnOpen = vdIOIntOpenLimited;
|
---|
9890 | VDIfIoInt.pfnClose = vdIOIntCloseLimited;
|
---|
9891 | VDIfIoInt.pfnDelete = vdIOIntDeleteLimited;
|
---|
9892 | VDIfIoInt.pfnMove = vdIOIntMoveLimited;
|
---|
9893 | VDIfIoInt.pfnGetFreeSpace = vdIOIntGetFreeSpaceLimited;
|
---|
9894 | VDIfIoInt.pfnGetModificationTime = vdIOIntGetModificationTimeLimited;
|
---|
9895 | VDIfIoInt.pfnGetSize = vdIOIntGetSizeLimited;
|
---|
9896 | VDIfIoInt.pfnSetSize = vdIOIntSetSizeLimited;
|
---|
9897 | VDIfIoInt.pfnReadUser = vdIOIntReadUserLimited;
|
---|
9898 | VDIfIoInt.pfnWriteUser = vdIOIntWriteUserLimited;
|
---|
9899 | VDIfIoInt.pfnReadMeta = vdIOIntReadMetaLimited;
|
---|
9900 | VDIfIoInt.pfnWriteMeta = vdIOIntWriteMetaLimited;
|
---|
9901 | VDIfIoInt.pfnFlush = vdIOIntFlushLimited;
|
---|
9902 | rc = VDInterfaceAdd(&VDIfIoInt.Core, "VD_IOINT", VDINTERFACETYPE_IOINT,
|
---|
9903 | pInterfaceIo, sizeof(VDINTERFACEIOINT), &pVDIfsImage);
|
---|
9904 | AssertRC(rc);
|
---|
9905 |
|
---|
9906 | rc = vdFindBackend(pszBackend, &pBackend);
|
---|
9907 | if (RT_SUCCESS(rc))
|
---|
9908 | {
|
---|
9909 | if (pBackend->pfnRepair)
|
---|
9910 | rc = pBackend->pfnRepair(pszFilename, pVDIfsDisk, pVDIfsImage, fFlags);
|
---|
9911 | else
|
---|
9912 | rc = VERR_VD_IMAGE_REPAIR_NOT_SUPPORTED;
|
---|
9913 | }
|
---|
9914 |
|
---|
9915 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
9916 | return rc;
|
---|
9917 | }
|
---|
9918 |
|
---|