VirtualBox

source: vbox/trunk/src/VBox/Storage/QED.cpp@ 69405

Last change on this file since 69405 was 69046, checked in by vboxsync, 7 years ago

Global: replace fall-through comments with RT_FALL_THRU().
bugref:8192: gcc warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 86.4 KB
Line 
1/* $Id: QED.cpp 69046 2017-10-11 16:11:23Z vboxsync $ */
2/** @file
3 * QED - QED Disk image.
4 */
5
6/*
7 * Copyright (C) 2011-2016 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_VD_QED
23#include <VBox/vd-plugin.h>
24#include <VBox/err.h>
25
26#include <VBox/log.h>
27#include <iprt/asm.h>
28#include <iprt/assert.h>
29#include <iprt/string.h>
30#include <iprt/alloc.h>
31#include <iprt/path.h>
32#include <iprt/list.h>
33
34#include "VDBackends.h"
35#include "VDBackendsInline.h"
36
37/**
38 * The QED backend implements support for the qemu enhanced disk format (short QED)
39 * The specification for the format is available under http://wiki.qemu.org/Features/QED/Specification
40 *
41 * Missing things to implement:
42 * - compaction
43 * - resizing which requires block relocation (very rare case)
44 */
45
46
47/*********************************************************************************************************************************
48* Structures in a QED image, little endian *
49*********************************************************************************************************************************/
50
51#pragma pack(1)
52typedef struct QedHeader
53{
54 /** Magic value. */
55 uint32_t u32Magic;
56 /** Cluster size in bytes. */
57 uint32_t u32ClusterSize;
58 /** Size of L1 and L2 tables in clusters. */
59 uint32_t u32TableSize;
60 /** size of this header structure in clusters. */
61 uint32_t u32HeaderSize;
62 /** Features used for the image. */
63 uint64_t u64FeatureFlags;
64 /** Compatibility features used for the image. */
65 uint64_t u64CompatFeatureFlags;
66 /** Self resetting feature bits. */
67 uint64_t u64AutoresetFeatureFlags;
68 /** Offset of the L1 table in bytes. */
69 uint64_t u64OffL1Table;
70 /** Logical image size as seen by the guest. */
71 uint64_t u64Size;
72 /** Offset of the backing filename in bytes. */
73 uint32_t u32OffBackingFilename;
74 /** Size of the backing filename. */
75 uint32_t u32BackingFilenameSize;
76} QedHeader;
77#pragma pack()
78/** Pointer to a on disk QED header. */
79typedef QedHeader *PQedHeader;
80
81/** QED magic value. */
82#define QED_MAGIC UINT32_C(0x00444551) /* QED\0 */
83/** Cluster size minimum. */
84#define QED_CLUSTER_SIZE_MIN RT_BIT(12)
85/** Cluster size maximum. */
86#define QED_CLUSTER_SIZE_MAX RT_BIT(26)
87/** L1 and L2 Table size minimum. */
88#define QED_TABLE_SIZE_MIN 1
89/** L1 and L2 Table size maximum. */
90#define QED_TABLE_SIZE_MAX 16
91
92/** QED default cluster size when creating an image. */
93#define QED_CLUSTER_SIZE_DEFAULT (64 * _1K)
94/** The default table size in clusters. */
95#define QED_TABLE_SIZE_DEFAULT 4
96
97/** Feature flags.
98 * @{
99 */
100/** Image uses a backing file to provide data for unallocated clusters. */
101#define QED_FEATURE_BACKING_FILE RT_BIT_64(0)
102/** Image needs checking before use. */
103#define QED_FEATURE_NEED_CHECK RT_BIT_64(1)
104/** Don't probe for format of the backing file, treat as raw image. */
105#define QED_FEATURE_BACKING_FILE_NO_PROBE RT_BIT_64(2)
106/** Mask of valid features. */
107#define QED_FEATURE_MASK (QED_FEATURE_BACKING_FILE | QED_FEATURE_NEED_CHECK | QED_FEATURE_BACKING_FILE_NO_PROBE)
108/** @} */
109
110/** Compatibility feature flags.
111 * @{
112 */
113/** Mask of valid compatibility features. */
114#define QED_COMPAT_FEATURE_MASK (0)
115/** @} */
116
117/** Autoreset feature flags.
118 * @{
119 */
120/** Mask of valid autoreset features. */
121#define QED_AUTORESET_FEATURE_MASK (0)
122/** @} */
123
124
125/*********************************************************************************************************************************
126* Constants And Macros, Structures and Typedefs *
127*********************************************************************************************************************************/
128
129/**
130 * QED L2 cache entry.
131 */
132typedef struct QEDL2CACHEENTRY
133{
134 /** List node for the search list. */
135 RTLISTNODE NodeSearch;
136 /** List node for the LRU list. */
137 RTLISTNODE NodeLru;
138 /** Reference counter. */
139 uint32_t cRefs;
140 /** The offset of the L2 table, used as search key. */
141 uint64_t offL2Tbl;
142 /** Pointer to the cached L2 table. */
143 uint64_t *paL2Tbl;
144} QEDL2CACHEENTRY, *PQEDL2CACHEENTRY;
145
146/** Maximum amount of memory the cache is allowed to use. */
147#define QED_L2_CACHE_MEMORY_MAX (2*_1M)
148
149/**
150 * QED image data structure.
151 */
152typedef struct QEDIMAGE
153{
154 /** Image name. */
155 const char *pszFilename;
156 /** Storage handle. */
157 PVDIOSTORAGE pStorage;
158
159 /** Pointer to the per-disk VD interface list. */
160 PVDINTERFACE pVDIfsDisk;
161 /** Pointer to the per-image VD interface list. */
162 PVDINTERFACE pVDIfsImage;
163 /** Error interface. */
164 PVDINTERFACEERROR pIfError;
165 /** I/O interface. */
166 PVDINTERFACEIOINT pIfIo;
167
168 /** Open flags passed by VBoxHD layer. */
169 unsigned uOpenFlags;
170 /** Image flags defined during creation or determined during open. */
171 unsigned uImageFlags;
172 /** Total size of the image. */
173 uint64_t cbSize;
174 /** Physical geometry of this image. */
175 VDGEOMETRY PCHSGeometry;
176 /** Logical geometry of this image. */
177 VDGEOMETRY LCHSGeometry;
178
179 /** Filename of the backing file if any. */
180 char *pszBackingFilename;
181 /** Offset of the filename in the image. */
182 uint32_t offBackingFilename;
183 /** Size of the backing filename excluding \0. */
184 uint32_t cbBackingFilename;
185
186 /** Size of the image, multiple of clusters. */
187 uint64_t cbImage;
188 /** Cluster size in bytes. */
189 uint32_t cbCluster;
190 /** Number of entries in the L1 and L2 table. */
191 uint32_t cTableEntries;
192 /** Size of an L1 or L2 table rounded to the next cluster size. */
193 uint32_t cbTable;
194 /** Pointer to the L1 table. */
195 uint64_t *paL1Table;
196 /** Offset of the L1 table. */
197 uint64_t offL1Table;
198
199 /** Offset mask for a cluster. */
200 uint64_t fOffsetMask;
201 /** L1 table mask to get the L1 index. */
202 uint64_t fL1Mask;
203 /** Number of bits to shift to get the L1 index. */
204 uint32_t cL1Shift;
205 /** L2 table mask to get the L2 index. */
206 uint64_t fL2Mask;
207 /** Number of bits to shift to get the L2 index. */
208 uint32_t cL2Shift;
209
210 /** Pointer to the L2 table we are currently allocating
211 * (can be only one at a time). */
212 PQEDL2CACHEENTRY pL2TblAlloc;
213
214 /** Memory occupied by the L2 table cache. */
215 size_t cbL2Cache;
216 /** The sorted L2 entry list used for searching. */
217 RTLISTNODE ListSearch;
218 /** The LRU L2 entry list used for eviction. */
219 RTLISTNODE ListLru;
220 /** The static region list. */
221 VDREGIONLIST RegionList;
222} QEDIMAGE, *PQEDIMAGE;
223
224/**
225 * State of the async cluster allocation.
226 */
227typedef enum QEDCLUSTERASYNCALLOCSTATE
228{
229 /** Invalid. */
230 QEDCLUSTERASYNCALLOCSTATE_INVALID = 0,
231 /** L2 table allocation. */
232 QEDCLUSTERASYNCALLOCSTATE_L2_ALLOC,
233 /** Link L2 table into L1. */
234 QEDCLUSTERASYNCALLOCSTATE_L2_LINK,
235 /** Allocate user data cluster. */
236 QEDCLUSTERASYNCALLOCSTATE_USER_ALLOC,
237 /** Link user data cluster. */
238 QEDCLUSTERASYNCALLOCSTATE_USER_LINK,
239 /** 32bit blowup. */
240 QEDCLUSTERASYNCALLOCSTATE_32BIT_HACK = 0x7fffffff
241} QEDCLUSTERASYNCALLOCSTATE, *PQEDCLUSTERASYNCALLOCSTATE;
242
243/**
244 * Data needed to track async cluster allocation.
245 */
246typedef struct QEDCLUSTERASYNCALLOC
247{
248 /** The state of the cluster allocation. */
249 QEDCLUSTERASYNCALLOCSTATE enmAllocState;
250 /** Old image size to rollback in case of an error. */
251 uint64_t cbImageOld;
252 /** L1 index to link if any. */
253 uint32_t idxL1;
254 /** L2 index to link, required in any case. */
255 uint32_t idxL2;
256 /** Start offset of the allocated cluster. */
257 uint64_t offClusterNew;
258 /** L2 cache entry if a L2 table is allocated. */
259 PQEDL2CACHEENTRY pL2Entry;
260 /** Number of bytes to write. */
261 size_t cbToWrite;
262} QEDCLUSTERASYNCALLOC, *PQEDCLUSTERASYNCALLOC;
263
264
265/*********************************************************************************************************************************
266* Static Variables *
267*********************************************************************************************************************************/
268
269/** NULL-terminated array of supported file extensions. */
270static const VDFILEEXTENSION s_aQedFileExtensions[] =
271{
272 {"qed", VDTYPE_HDD},
273 {NULL, VDTYPE_INVALID}
274};
275
276
277/*********************************************************************************************************************************
278* Internal Functions *
279*********************************************************************************************************************************/
280
281/**
282 * Converts the image header to the host endianess and performs basic checks.
283 *
284 * @returns Whether the given header is valid or not.
285 * @param pHeader Pointer to the header to convert.
286 */
287static bool qedHdrConvertToHostEndianess(PQedHeader pHeader)
288{
289 pHeader->u32Magic = RT_LE2H_U32(pHeader->u32Magic);
290 pHeader->u32ClusterSize = RT_LE2H_U32(pHeader->u32ClusterSize);
291 pHeader->u32TableSize = RT_LE2H_U32(pHeader->u32TableSize);
292 pHeader->u32HeaderSize = RT_LE2H_U32(pHeader->u32HeaderSize);
293 pHeader->u64FeatureFlags = RT_LE2H_U64(pHeader->u64FeatureFlags);
294 pHeader->u64CompatFeatureFlags = RT_LE2H_U64(pHeader->u64CompatFeatureFlags);
295 pHeader->u64AutoresetFeatureFlags = RT_LE2H_U64(pHeader->u64AutoresetFeatureFlags);
296 pHeader->u64OffL1Table = RT_LE2H_U64(pHeader->u64OffL1Table);
297 pHeader->u64Size = RT_LE2H_U64(pHeader->u64Size);
298 pHeader->u32OffBackingFilename = RT_LE2H_U32(pHeader->u32OffBackingFilename);
299 pHeader->u32BackingFilenameSize = RT_LE2H_U32(pHeader->u32BackingFilenameSize);
300
301 if (RT_UNLIKELY(pHeader->u32Magic != QED_MAGIC))
302 return false;
303 if (RT_UNLIKELY( pHeader->u32ClusterSize < QED_CLUSTER_SIZE_MIN
304 || pHeader->u32ClusterSize > QED_CLUSTER_SIZE_MAX))
305 return false;
306 if (RT_UNLIKELY( pHeader->u32TableSize < QED_TABLE_SIZE_MIN
307 || pHeader->u32TableSize > QED_TABLE_SIZE_MAX))
308 return false;
309 if (RT_UNLIKELY(pHeader->u64Size % 512 != 0))
310 return false;
311 if (RT_UNLIKELY( pHeader->u64FeatureFlags & QED_FEATURE_BACKING_FILE
312 && ( pHeader->u32BackingFilenameSize == 0
313 || pHeader->u32BackingFilenameSize == UINT32_MAX)))
314 return false;
315
316 return true;
317}
318
319/**
320 * Creates a QED header from the given image state.
321 *
322 * @returns nothing.
323 * @param pImage Image instance data.
324 * @param pHeader Pointer to the header to convert.
325 */
326static void qedHdrConvertFromHostEndianess(PQEDIMAGE pImage, PQedHeader pHeader)
327{
328 pHeader->u32Magic = RT_H2LE_U32(QED_MAGIC);
329 pHeader->u32ClusterSize = RT_H2LE_U32(pImage->cbCluster);
330 pHeader->u32TableSize = RT_H2LE_U32(pImage->cbTable / pImage->cbCluster);
331 pHeader->u32HeaderSize = RT_H2LE_U32(1);
332 pHeader->u64FeatureFlags = RT_H2LE_U64(pImage->pszBackingFilename ? QED_FEATURE_BACKING_FILE : UINT64_C(0));
333 pHeader->u64CompatFeatureFlags = RT_H2LE_U64(UINT64_C(0));
334 pHeader->u64AutoresetFeatureFlags = RT_H2LE_U64(UINT64_C(0));
335 pHeader->u64OffL1Table = RT_H2LE_U64(pImage->offL1Table);
336 pHeader->u64Size = RT_H2LE_U64(pImage->cbSize);
337 pHeader->u32OffBackingFilename = RT_H2LE_U32(pImage->offBackingFilename);
338 pHeader->u32BackingFilenameSize = RT_H2LE_U32(pImage->cbBackingFilename);
339}
340
341/**
342 * Convert table entries from little endian to host endianess.
343 *
344 * @returns nothing.
345 * @param paTbl Pointer to the table.
346 * @param cEntries Number of entries in the table.
347 */
348static void qedTableConvertToHostEndianess(uint64_t *paTbl, uint32_t cEntries)
349{
350 while(cEntries-- > 0)
351 {
352 *paTbl = RT_LE2H_U64(*paTbl);
353 paTbl++;
354 }
355}
356
357#if defined(RT_BIG_ENDIAN)
358/**
359 * Convert table entries from host to little endian format.
360 *
361 * @returns nothing.
362 * @param paTblImg Pointer to the table which will store the little endian table.
363 * @param paTbl The source table to convert.
364 * @param cEntries Number of entries in the table.
365 */
366static void qedTableConvertFromHostEndianess(uint64_t *paTblImg, uint64_t *paTbl,
367 uint32_t cEntries)
368{
369 while(cEntries-- > 0)
370 {
371 *paTblImg = RT_H2LE_U64(*paTbl);
372 paTbl++;
373 paTblImg++;
374 }
375}
376#endif
377
378/**
379 * Creates the L2 table cache.
380 *
381 * @returns VBox status code.
382 * @param pImage The image instance data.
383 */
384static int qedL2TblCacheCreate(PQEDIMAGE pImage)
385{
386 pImage->cbL2Cache = 0;
387 RTListInit(&pImage->ListSearch);
388 RTListInit(&pImage->ListLru);
389
390 return VINF_SUCCESS;
391}
392
393/**
394 * Destroys the L2 table cache.
395 *
396 * @returns nothing.
397 * @param pImage The image instance data.
398 */
399static void qedL2TblCacheDestroy(PQEDIMAGE pImage)
400{
401 PQEDL2CACHEENTRY pL2Entry;
402 PQEDL2CACHEENTRY pL2Next;
403 RTListForEachSafe(&pImage->ListSearch, pL2Entry, pL2Next, QEDL2CACHEENTRY, NodeSearch)
404 {
405 Assert(!pL2Entry->cRefs);
406
407 RTListNodeRemove(&pL2Entry->NodeSearch);
408 RTMemPageFree(pL2Entry->paL2Tbl, pImage->cbTable);
409 RTMemFree(pL2Entry);
410 }
411
412 pImage->cbL2Cache = 0;
413 RTListInit(&pImage->ListSearch);
414 RTListInit(&pImage->ListLru);
415}
416
417/**
418 * Returns the L2 table matching the given offset or NULL if none could be found.
419 *
420 * @returns Pointer to the L2 table cache entry or NULL.
421 * @param pImage The image instance data.
422 * @param offL2Tbl Offset of the L2 table to search for.
423 */
424static PQEDL2CACHEENTRY qedL2TblCacheRetain(PQEDIMAGE pImage, uint64_t offL2Tbl)
425{
426 if ( pImage->pL2TblAlloc
427 && pImage->pL2TblAlloc->offL2Tbl == offL2Tbl)
428 {
429 pImage->pL2TblAlloc->cRefs++;
430 return pImage->pL2TblAlloc;
431 }
432
433 PQEDL2CACHEENTRY pL2Entry;
434 RTListForEach(&pImage->ListSearch, pL2Entry, QEDL2CACHEENTRY, NodeSearch)
435 {
436 if (pL2Entry->offL2Tbl == offL2Tbl)
437 break;
438 }
439
440 if (!RTListNodeIsDummy(&pImage->ListSearch, pL2Entry, QEDL2CACHEENTRY, NodeSearch))
441 {
442 /* Update LRU list. */
443 RTListNodeRemove(&pL2Entry->NodeLru);
444 RTListPrepend(&pImage->ListLru, &pL2Entry->NodeLru);
445 pL2Entry->cRefs++;
446 return pL2Entry;
447 }
448 else
449 return NULL;
450}
451
452/**
453 * Releases a L2 table cache entry.
454 *
455 * @returns nothing.
456 * @param pL2Entry The L2 cache entry.
457 */
458static void qedL2TblCacheEntryRelease(PQEDL2CACHEENTRY pL2Entry)
459{
460 Assert(pL2Entry->cRefs > 0);
461 pL2Entry->cRefs--;
462}
463
464/**
465 * Allocates a new L2 table from the cache evicting old entries if required.
466 *
467 * @returns Pointer to the L2 cache entry or NULL.
468 * @param pImage The image instance data.
469 */
470static PQEDL2CACHEENTRY qedL2TblCacheEntryAlloc(PQEDIMAGE pImage)
471{
472 PQEDL2CACHEENTRY pL2Entry = NULL;
473
474 if (pImage->cbL2Cache + pImage->cbTable <= QED_L2_CACHE_MEMORY_MAX)
475 {
476 /* Add a new entry. */
477 pL2Entry = (PQEDL2CACHEENTRY)RTMemAllocZ(sizeof(QEDL2CACHEENTRY));
478 if (pL2Entry)
479 {
480 pL2Entry->paL2Tbl = (uint64_t *)RTMemPageAllocZ(pImage->cbTable);
481 if (RT_UNLIKELY(!pL2Entry->paL2Tbl))
482 {
483 RTMemFree(pL2Entry);
484 pL2Entry = NULL;
485 }
486 else
487 {
488 pL2Entry->cRefs = 1;
489 pImage->cbL2Cache += pImage->cbTable;
490 }
491 }
492 }
493 else
494 {
495 /* Evict the last not in use entry and use it */
496 Assert(!RTListIsEmpty(&pImage->ListLru));
497
498 RTListForEachReverse(&pImage->ListLru, pL2Entry, QEDL2CACHEENTRY, NodeLru)
499 {
500 if (!pL2Entry->cRefs)
501 break;
502 }
503
504 if (!RTListNodeIsDummy(&pImage->ListSearch, pL2Entry, QEDL2CACHEENTRY, NodeSearch))
505 {
506 RTListNodeRemove(&pL2Entry->NodeSearch);
507 RTListNodeRemove(&pL2Entry->NodeLru);
508 pL2Entry->offL2Tbl = 0;
509 pL2Entry->cRefs = 1;
510 }
511 else
512 pL2Entry = NULL;
513 }
514
515 return pL2Entry;
516}
517
518/**
519 * Frees a L2 table cache entry.
520 *
521 * @returns nothing.
522 * @param pImage The image instance data.
523 * @param pL2Entry The L2 cache entry to free.
524 */
525static void qedL2TblCacheEntryFree(PQEDIMAGE pImage, PQEDL2CACHEENTRY pL2Entry)
526{
527 Assert(!pL2Entry->cRefs);
528 RTMemPageFree(pL2Entry->paL2Tbl, pImage->cbTable);
529 RTMemFree(pL2Entry);
530
531 pImage->cbL2Cache -= pImage->cbTable;
532}
533
534/**
535 * Inserts an entry in the L2 table cache.
536 *
537 * @returns nothing.
538 * @param pImage The image instance data.
539 * @param pL2Entry The L2 cache entry to insert.
540 */
541static void qedL2TblCacheEntryInsert(PQEDIMAGE pImage, PQEDL2CACHEENTRY pL2Entry)
542{
543 Assert(pL2Entry->offL2Tbl > 0);
544
545 /* Insert at the top of the LRU list. */
546 RTListPrepend(&pImage->ListLru, &pL2Entry->NodeLru);
547
548 if (RTListIsEmpty(&pImage->ListSearch))
549 {
550 RTListAppend(&pImage->ListSearch, &pL2Entry->NodeSearch);
551 }
552 else
553 {
554 /* Insert into search list. */
555 PQEDL2CACHEENTRY pIt;
556 pIt = RTListGetFirst(&pImage->ListSearch, QEDL2CACHEENTRY, NodeSearch);
557 if (pIt->offL2Tbl > pL2Entry->offL2Tbl)
558 RTListPrepend(&pImage->ListSearch, &pL2Entry->NodeSearch);
559 else
560 {
561 bool fInserted = false;
562
563 RTListForEach(&pImage->ListSearch, pIt, QEDL2CACHEENTRY, NodeSearch)
564 {
565 Assert(pIt->offL2Tbl != pL2Entry->offL2Tbl);
566 if (pIt->offL2Tbl < pL2Entry->offL2Tbl)
567 {
568 RTListNodeInsertAfter(&pIt->NodeSearch, &pL2Entry->NodeSearch);
569 fInserted = true;
570 break;
571 }
572 }
573 Assert(fInserted);
574 }
575 }
576}
577
578/**
579 * Fetches the L2 from the given offset trying the LRU cache first and
580 * reading it from the image after a cache miss - version for async I/O.
581 *
582 * @returns VBox status code.
583 * @param pImage Image instance data.
584 * @param pIoCtx The I/O context.
585 * @param offL2Tbl The offset of the L2 table in the image.
586 * @param ppL2Entry Where to store the L2 table on success.
587 */
588static int qedL2TblCacheFetchAsync(PQEDIMAGE pImage, PVDIOCTX pIoCtx,
589 uint64_t offL2Tbl, PQEDL2CACHEENTRY *ppL2Entry)
590{
591 int rc = VINF_SUCCESS;
592
593 /* Try to fetch the L2 table from the cache first. */
594 PQEDL2CACHEENTRY pL2Entry = qedL2TblCacheRetain(pImage, offL2Tbl);
595 if (!pL2Entry)
596 {
597 pL2Entry = qedL2TblCacheEntryAlloc(pImage);
598
599 if (pL2Entry)
600 {
601 /* Read from the image. */
602 PVDMETAXFER pMetaXfer;
603
604 pL2Entry->offL2Tbl = offL2Tbl;
605 rc = vdIfIoIntFileReadMeta(pImage->pIfIo, pImage->pStorage,
606 offL2Tbl, pL2Entry->paL2Tbl,
607 pImage->cbTable, pIoCtx,
608 &pMetaXfer, NULL, NULL);
609 if (RT_SUCCESS(rc))
610 {
611 vdIfIoIntMetaXferRelease(pImage->pIfIo, pMetaXfer);
612#if defined(RT_BIG_ENDIAN)
613 qedTableConvertToHostEndianess(pL2Entry->paL2Tbl, pImage->cTableEntries);
614#endif
615 qedL2TblCacheEntryInsert(pImage, pL2Entry);
616 }
617 else
618 {
619 qedL2TblCacheEntryRelease(pL2Entry);
620 qedL2TblCacheEntryFree(pImage, pL2Entry);
621 }
622 }
623 else
624 rc = VERR_NO_MEMORY;
625 }
626
627 if (RT_SUCCESS(rc))
628 *ppL2Entry = pL2Entry;
629
630 return rc;
631}
632
633/**
634 * Return power of 2 or 0 if num error.
635 *
636 * @returns The power of 2 or 0 if the given number is not a power of 2.
637 * @param u32 The number.
638 */
639static uint32_t qedGetPowerOfTwo(uint32_t u32)
640{
641 if (u32 == 0)
642 return 0;
643 uint32_t uPower2 = 0;
644 while ((u32 & 1) == 0)
645 {
646 u32 >>= 1;
647 uPower2++;
648 }
649 return u32 == 1 ? uPower2 : 0;
650}
651
652/**
653 * Sets the L1, L2 and offset bitmasks and L1 and L2 bit shift members.
654 *
655 * @returns nothing.
656 * @param pImage The image instance data.
657 */
658static void qedTableMasksInit(PQEDIMAGE pImage)
659{
660 uint32_t cClusterBits, cTableBits;
661
662 cClusterBits = qedGetPowerOfTwo(pImage->cbCluster);
663 cTableBits = qedGetPowerOfTwo(pImage->cTableEntries);
664
665 Assert(cClusterBits + 2 * cTableBits <= 64);
666
667 pImage->fOffsetMask = ((uint64_t)pImage->cbCluster - 1);
668 pImage->fL2Mask = ((uint64_t)pImage->cTableEntries - 1) << cClusterBits;
669 pImage->cL2Shift = cClusterBits;
670 pImage->fL1Mask = ((uint64_t)pImage->cTableEntries - 1) << (cClusterBits + cTableBits);
671 pImage->cL1Shift = cClusterBits + cTableBits;
672}
673
674/**
675 * Converts a given logical offset into the
676 *
677 * @returns nothing.
678 * @param pImage The image instance data.
679 * @param off The logical offset to convert.
680 * @param pidxL1 Where to store the index in the L1 table on success.
681 * @param pidxL2 Where to store the index in the L2 table on success.
682 * @param poffCluster Where to store the offset in the cluster on success.
683 */
684DECLINLINE(void) qedConvertLogicalOffset(PQEDIMAGE pImage, uint64_t off, uint32_t *pidxL1,
685 uint32_t *pidxL2, uint32_t *poffCluster)
686{
687 AssertPtr(pidxL1);
688 AssertPtr(pidxL2);
689 AssertPtr(poffCluster);
690
691 *poffCluster = off & pImage->fOffsetMask;
692 *pidxL1 = (off & pImage->fL1Mask) >> pImage->cL1Shift;
693 *pidxL2 = (off & pImage->fL2Mask) >> pImage->cL2Shift;
694}
695
696/**
697 * Converts Cluster size to a byte size.
698 *
699 * @returns Number of bytes derived from the given number of clusters.
700 * @param pImage The image instance data.
701 * @param cClusters The clusters to convert.
702 */
703DECLINLINE(uint64_t) qedCluster2Byte(PQEDIMAGE pImage, uint64_t cClusters)
704{
705 return cClusters * pImage->cbCluster;
706}
707
708/**
709 * Converts number of bytes to cluster size rounding to the next cluster.
710 *
711 * @returns Number of bytes derived from the given number of clusters.
712 * @param pImage The image instance data.
713 * @param cb Number of bytes to convert.
714 */
715DECLINLINE(uint64_t) qedByte2Cluster(PQEDIMAGE pImage, uint64_t cb)
716{
717 return cb / pImage->cbCluster + (cb % pImage->cbCluster ? 1 : 0);
718}
719
720/**
721 * Allocates a new cluster in the image.
722 *
723 * @returns The start offset of the new cluster in the image.
724 * @param pImage The image instance data.
725 * @param cClusters Number of clusters to allocate.
726 */
727DECLINLINE(uint64_t) qedClusterAllocate(PQEDIMAGE pImage, uint32_t cClusters)
728{
729 uint64_t offCluster;
730
731 offCluster = pImage->cbImage;
732 pImage->cbImage += cClusters*pImage->cbCluster;
733
734 return offCluster;
735}
736
737/**
738 * Returns the real image offset for a given cluster or an error if the cluster is not
739 * yet allocated.
740 *
741 * @returns VBox status code.
742 * VERR_VD_BLOCK_FREE if the cluster is not yet allocated.
743 * @param pImage The image instance data.
744 * @param pIoCtx The I/O context.
745 * @param idxL1 The L1 index.
746 * @param idxL2 The L2 index.
747 * @param offCluster Offset inside the cluster.
748 * @param poffImage Where to store the image offset on success;
749 */
750static int qedConvertToImageOffset(PQEDIMAGE pImage, PVDIOCTX pIoCtx,
751 uint32_t idxL1, uint32_t idxL2,
752 uint32_t offCluster, uint64_t *poffImage)
753{
754 int rc = VERR_VD_BLOCK_FREE;
755
756 AssertReturn(idxL1 < pImage->cTableEntries, VERR_INVALID_PARAMETER);
757 AssertReturn(idxL2 < pImage->cTableEntries, VERR_INVALID_PARAMETER);
758
759 if (pImage->paL1Table[idxL1])
760 {
761 PQEDL2CACHEENTRY pL2Entry;
762
763 rc = qedL2TblCacheFetchAsync(pImage, pIoCtx, pImage->paL1Table[idxL1],
764 &pL2Entry);
765 if (RT_SUCCESS(rc))
766 {
767 /* Get real file offset. */
768 if (pL2Entry->paL2Tbl[idxL2])
769 *poffImage = pL2Entry->paL2Tbl[idxL2] + offCluster;
770 else
771 rc = VERR_VD_BLOCK_FREE;
772
773 qedL2TblCacheEntryRelease(pL2Entry);
774 }
775 }
776
777 return rc;
778}
779
780/**
781 * Write the given table to image converting to the image endianess if required.
782 *
783 * @returns VBox status code.
784 * @param pImage The image instance data.
785 * @param pIoCtx The I/O context.
786 * @param offTbl The offset the table should be written to.
787 * @param paTbl The table to write.
788 * @param pfnComplete Callback called when the write completes.
789 * @param pvUser Opaque user data to pass in the completion callback.
790 */
791static int qedTblWrite(PQEDIMAGE pImage, PVDIOCTX pIoCtx, uint64_t offTbl, uint64_t *paTbl,
792 PFNVDXFERCOMPLETED pfnComplete, void *pvUser)
793{
794 int rc = VINF_SUCCESS;
795
796#if defined(RT_BIG_ENDIAN)
797 uint64_t *paTblImg = (uint64_t *)RTMemAllocZ(pImage->cbTable);
798 if (paTblImg)
799 {
800 qedTableConvertFromHostEndianess(paTblImg, paTbl,
801 pImage->cTableEntries);
802 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
803 offTbl, paTblImg, pImage->cbTable,
804 pIoCtx, pfnComplete, pvUser);
805 RTMemFree(paTblImg);
806 }
807 else
808 rc = VERR_NO_MEMORY;
809#else
810 /* Write table directly. */
811 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
812 offTbl, paTbl, pImage->cbTable, pIoCtx,
813 pfnComplete, pvUser);
814#endif
815
816 return rc;
817}
818
819/**
820 * Internal. Flush image data to disk.
821 */
822static int qedFlushImage(PQEDIMAGE pImage)
823{
824 int rc = VINF_SUCCESS;
825
826 if ( pImage->pStorage
827 && !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
828 {
829 QedHeader Header;
830
831 Assert(!(pImage->cbTable % pImage->cbCluster));
832#if defined(RT_BIG_ENDIAN)
833 uint64_t *paL1TblImg = (uint64_t *)RTMemAllocZ(pImage->cbTable);
834 if (paL1TblImg)
835 {
836 qedTableConvertFromHostEndianess(paL1TblImg, pImage->paL1Table,
837 pImage->cTableEntries);
838 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
839 pImage->offL1Table, paL1TblImg,
840 pImage->cbTable);
841 RTMemFree(paL1TblImg);
842 }
843 else
844 rc = VERR_NO_MEMORY;
845#else
846 /* Write L1 table directly. */
847 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, pImage->offL1Table,
848 pImage->paL1Table, pImage->cbTable);
849#endif
850 if (RT_SUCCESS(rc))
851 {
852 /* Write header. */
853 qedHdrConvertFromHostEndianess(pImage, &Header);
854 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, 0, &Header,
855 sizeof(Header));
856 if (RT_SUCCESS(rc))
857 rc = vdIfIoIntFileFlushSync(pImage->pIfIo, pImage->pStorage);
858 }
859 }
860
861 return rc;
862}
863
864/**
865 * Checks whether the given cluster offset is valid.
866 *
867 * @returns Whether the given cluster offset is valid.
868 * @param offCluster The table offset to check.
869 * @param cbFile The real file size of the image.
870 * @param cbCluster The cluster size in bytes.
871 */
872DECLINLINE(bool) qedIsClusterOffsetValid(uint64_t offCluster, uint64_t cbFile, size_t cbCluster)
873{
874 return (offCluster <= cbFile - cbCluster)
875 && !(offCluster & (cbCluster - 1));
876}
877
878/**
879 * Checks whether the given table offset is valid.
880 *
881 * @returns Whether the given table offset is valid.
882 * @param offTbl The table offset to check.
883 * @param cbFile The real file size of the image.
884 * @param cbTable The table size in bytes.
885 * @param cbCluster The cluster size in bytes.
886 */
887DECLINLINE(bool) qedIsTblOffsetValid(uint64_t offTbl, uint64_t cbFile, size_t cbTable, size_t cbCluster)
888{
889 return (offTbl <= cbFile - cbTable)
890 && !(offTbl & (cbCluster - 1));
891}
892
893/**
894 * Sets the specified range in the cluster bitmap checking whether any of the clusters is already
895 * used before.
896 *
897 * @returns Whether the range was clear and is set now.
898 * @param pvClusterBitmap The cluster bitmap to use.
899 * @param offClusterStart The first cluster to check and set.
900 * @param offClusterEnd The first cluster to not check and set anymore.
901 */
902static bool qedClusterBitmapCheckAndSet(void *pvClusterBitmap, uint32_t offClusterStart, uint32_t offClusterEnd)
903{
904 for (uint32_t offCluster = offClusterStart; offCluster < offClusterEnd; offCluster++)
905 if (ASMBitTest(pvClusterBitmap, offCluster))
906 return false;
907
908 ASMBitSetRange(pvClusterBitmap, offClusterStart, offClusterEnd);
909 return true;
910}
911
912/**
913 * Checks the given image for consistency, usually called when the
914 * QED_FEATURE_NEED_CHECK bit is set.
915 *
916 * @returns VBox status code.
917 * @retval VINF_SUCCESS when the image can be accessed.
918 * @param pImage The image instance data.
919 * @param pHeader The header to use for checking.
920 *
921 * @note It is not required that the image state is fully initialized Only
922 * The I/O interface and storage handle need to be valid.
923 * @note The header must be converted to the host CPU endian format already
924 * and should be validated already.
925 */
926static int qedCheckImage(PQEDIMAGE pImage, PQedHeader pHeader)
927{
928 uint64_t cbFile;
929 uint32_t cbTable;
930 uint32_t cTableEntries;
931 uint64_t *paL1Tbl = NULL;
932 uint64_t *paL2Tbl = NULL;
933 void *pvClusterBitmap = NULL;
934 uint32_t offClusterStart;
935 int rc = VINF_SUCCESS;
936
937 pImage->cbCluster = pHeader->u32ClusterSize;
938 cbTable = pHeader->u32TableSize * pHeader->u32ClusterSize;
939 cTableEntries = cbTable / sizeof(uint64_t);
940
941 do
942 {
943 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
944 if (RT_FAILURE(rc))
945 {
946 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
947 N_("Qed: Querying the file size of image '%s' failed"),
948 pImage->pszFilename);
949 break;
950 }
951
952 /* Allocate L1 table. */
953 paL1Tbl = (uint64_t *)RTMemAllocZ(cbTable);
954 if (!paL1Tbl)
955 {
956 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
957 N_("Qed: Allocating memory for the L1 table for image '%s' failed"),
958 pImage->pszFilename);
959 break;
960 }
961
962 paL2Tbl = (uint64_t *)RTMemAllocZ(cbTable);
963 if (!paL2Tbl)
964 {
965 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
966 N_("Qed: Allocating memory for the L2 table for image '%s' failed"),
967 pImage->pszFilename);
968 break;
969 }
970
971 pvClusterBitmap = RTMemAllocZ(cbFile / pHeader->u32ClusterSize / 8);
972 if (!pvClusterBitmap)
973 {
974 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
975 N_("Qed: Allocating memory for the cluster bitmap for image '%s' failed"),
976 pImage->pszFilename);
977 break;
978 }
979
980 /* Validate L1 table offset. */
981 if (!qedIsTblOffsetValid(pHeader->u64OffL1Table, cbFile, cbTable, pHeader->u32ClusterSize))
982 {
983 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
984 N_("Qed: L1 table offset of image '%s' is corrupt (%llu)"),
985 pImage->pszFilename, pHeader->u64OffL1Table);
986 break;
987 }
988
989 /* Read L1 table. */
990 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
991 pHeader->u64OffL1Table, paL1Tbl, cbTable);
992 if (RT_FAILURE(rc))
993 {
994 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
995 N_("Qed: Reading the L1 table from image '%s' failed"),
996 pImage->pszFilename);
997 break;
998 }
999
1000 /* Mark the L1 table in cluster bitmap. */
1001 ASMBitSet(pvClusterBitmap, 0); /* Header is always in cluster 0. */
1002 offClusterStart = qedByte2Cluster(pImage, pHeader->u64OffL1Table);
1003 bool fSet = qedClusterBitmapCheckAndSet(pvClusterBitmap, offClusterStart, offClusterStart + pHeader->u32TableSize);
1004 Assert(fSet);
1005
1006 /* Scan the L1 and L2 tables for invalid entries. */
1007 qedTableConvertToHostEndianess(paL1Tbl, cTableEntries);
1008
1009 for (unsigned iL1 = 0; iL1 < cTableEntries; iL1++)
1010 {
1011 if (!paL1Tbl[iL1])
1012 continue; /* Skip unallocated clusters. */
1013
1014 if (!qedIsTblOffsetValid(paL1Tbl[iL1], cbFile, cbTable, pHeader->u32ClusterSize))
1015 {
1016 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1017 N_("Qed: Entry %d of the L1 table from image '%s' is invalid (%llu)"),
1018 iL1, pImage->pszFilename, paL1Tbl[iL1]);
1019 break;
1020 }
1021
1022 /* Now check that the clusters are not allocated already. */
1023 offClusterStart = qedByte2Cluster(pImage, paL1Tbl[iL1]);
1024 fSet = qedClusterBitmapCheckAndSet(pvClusterBitmap, offClusterStart, offClusterStart + pHeader->u32TableSize);
1025 if (!fSet)
1026 {
1027 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1028 N_("Qed: Entry %d of the L1 table from image '%s' points to a already used cluster (%llu)"),
1029 iL1, pImage->pszFilename, paL1Tbl[iL1]);
1030 break;
1031 }
1032
1033 /* Read the linked L2 table and check it. */
1034 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
1035 paL1Tbl[iL1], paL2Tbl, cbTable);
1036 if (RT_FAILURE(rc))
1037 {
1038 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1039 N_("Qed: Reading the L2 table from image '%s' failed"),
1040 pImage->pszFilename);
1041 break;
1042 }
1043
1044 /* Check all L2 entries. */
1045 for (unsigned iL2 = 0; iL2 < cTableEntries; iL2++)
1046 {
1047 if (paL2Tbl[iL2])
1048 continue; /* Skip unallocated clusters. */
1049
1050 if (!qedIsClusterOffsetValid(paL2Tbl[iL2], cbFile, pHeader->u32ClusterSize))
1051 {
1052 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1053 N_("Qed: Entry %d of the L2 table from image '%s' is invalid (%llu)"),
1054 iL2, pImage->pszFilename, paL2Tbl[iL2]);
1055 break;
1056 }
1057
1058 /* Now check that the clusters are not allocated already. */
1059 offClusterStart = qedByte2Cluster(pImage, paL2Tbl[iL2]);
1060 fSet = qedClusterBitmapCheckAndSet(pvClusterBitmap, offClusterStart, offClusterStart + 1);
1061 if (!fSet)
1062 {
1063 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1064 N_("Qed: Entry %d of the L2 table from image '%s' points to a already used cluster (%llu)"),
1065 iL2, pImage->pszFilename, paL2Tbl[iL2]);
1066 break;
1067 }
1068 }
1069 }
1070 } while(0);
1071
1072 if (paL1Tbl)
1073 RTMemFree(paL1Tbl);
1074 if (paL2Tbl)
1075 RTMemFree(paL2Tbl);
1076 if (pvClusterBitmap)
1077 RTMemFree(pvClusterBitmap);
1078
1079 return rc;
1080}
1081
1082/**
1083 * Internal. Free all allocated space for representing an image except pImage,
1084 * and optionally delete the image from disk.
1085 */
1086static int qedFreeImage(PQEDIMAGE pImage, bool fDelete)
1087{
1088 int rc = VINF_SUCCESS;
1089
1090 /* Freeing a never allocated image (e.g. because the open failed) is
1091 * not signalled as an error. After all nothing bad happens. */
1092 if (pImage)
1093 {
1094 if (pImage->pStorage)
1095 {
1096 /* No point updating the file that is deleted anyway. */
1097 if (!fDelete)
1098 qedFlushImage(pImage);
1099
1100 rc = vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
1101 pImage->pStorage = NULL;
1102 }
1103
1104 if (pImage->paL1Table)
1105 RTMemFree(pImage->paL1Table);
1106
1107 if (pImage->pszBackingFilename)
1108 {
1109 RTMemFree(pImage->pszBackingFilename);
1110 pImage->pszBackingFilename = NULL;
1111 }
1112
1113 qedL2TblCacheDestroy(pImage);
1114
1115 if (fDelete && pImage->pszFilename)
1116 vdIfIoIntFileDelete(pImage->pIfIo, pImage->pszFilename);
1117 }
1118
1119 LogFlowFunc(("returns %Rrc\n", rc));
1120 return rc;
1121}
1122
1123/**
1124 * Internal: Open an image, constructing all necessary data structures.
1125 */
1126static int qedOpenImage(PQEDIMAGE pImage, unsigned uOpenFlags)
1127{
1128 pImage->uOpenFlags = uOpenFlags;
1129
1130 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
1131 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
1132 AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
1133
1134 /*
1135 * Create the L2 cache before opening the image so we can call qedFreeImage()
1136 * even if opening the image file fails.
1137 */
1138 int rc = qedL2TblCacheCreate(pImage);
1139 if (RT_SUCCESS(rc))
1140 {
1141 /* Open the image. */
1142 rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename,
1143 VDOpenFlagsToFileOpenFlags(uOpenFlags,
1144 false /* fCreate */),
1145 &pImage->pStorage);
1146 if (RT_SUCCESS(rc))
1147 {
1148 uint64_t cbFile;
1149 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
1150 if ( RT_SUCCESS(rc)
1151 && cbFile > sizeof(QedHeader))
1152 {
1153 QedHeader Header;
1154
1155 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, 0, &Header, sizeof(Header));
1156 if ( RT_SUCCESS(rc)
1157 && qedHdrConvertToHostEndianess(&Header))
1158 {
1159 if ( !(Header.u64FeatureFlags & ~QED_FEATURE_MASK)
1160 && !(Header.u64FeatureFlags & QED_FEATURE_BACKING_FILE_NO_PROBE))
1161 {
1162 if (Header.u64FeatureFlags & QED_FEATURE_NEED_CHECK)
1163 {
1164 /* Image needs checking. */
1165 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
1166 rc = qedCheckImage(pImage, &Header);
1167 else
1168 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1169 N_("Qed: Image '%s' needs checking but is opened readonly"),
1170 pImage->pszFilename);
1171 }
1172
1173 if ( RT_SUCCESS(rc)
1174 && (Header.u64FeatureFlags & QED_FEATURE_BACKING_FILE))
1175 {
1176 /* Load backing filename from image. */
1177 pImage->pszBackingFilename = (char *)RTMemAllocZ(Header.u32BackingFilenameSize + 1); /* +1 for \0 terminator. */
1178 if (pImage->pszBackingFilename)
1179 {
1180 pImage->cbBackingFilename = Header.u32BackingFilenameSize;
1181 pImage->offBackingFilename = Header.u32OffBackingFilename;
1182 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
1183 Header.u32OffBackingFilename, pImage->pszBackingFilename,
1184 Header.u32BackingFilenameSize);
1185 }
1186 else
1187 rc = VERR_NO_MEMORY;
1188 }
1189
1190 if (RT_SUCCESS(rc))
1191 {
1192 pImage->cbImage = cbFile;
1193 pImage->cbCluster = Header.u32ClusterSize;
1194 pImage->cbTable = Header.u32TableSize * pImage->cbCluster;
1195 pImage->cTableEntries = pImage->cbTable / sizeof(uint64_t);
1196 pImage->offL1Table = Header.u64OffL1Table;
1197 pImage->cbSize = Header.u64Size;
1198 qedTableMasksInit(pImage);
1199
1200 /* Allocate L1 table. */
1201 pImage->paL1Table = (uint64_t *)RTMemAllocZ(pImage->cbTable);
1202 if (pImage->paL1Table)
1203 {
1204 /* Read from the image. */
1205 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
1206 pImage->offL1Table, pImage->paL1Table,
1207 pImage->cbTable);
1208 if (RT_SUCCESS(rc))
1209 {
1210 qedTableConvertToHostEndianess(pImage->paL1Table, pImage->cTableEntries);
1211
1212 /* If the consistency check succeeded, clear the flag by flushing the image. */
1213 if (Header.u64FeatureFlags & QED_FEATURE_NEED_CHECK)
1214 rc = qedFlushImage(pImage);
1215 }
1216 else
1217 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1218 N_("Qed: Reading the L1 table for image '%s' failed"),
1219 pImage->pszFilename);
1220 }
1221 else
1222 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
1223 N_("Qed: Out of memory allocating L1 table for image '%s'"),
1224 pImage->pszFilename);
1225 }
1226 }
1227 else
1228 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1229 N_("Qed: The image '%s' makes use of unsupported features"),
1230 pImage->pszFilename);
1231 }
1232 else if (RT_SUCCESS(rc))
1233 rc = VERR_VD_GEN_INVALID_HEADER;
1234 }
1235 else if (RT_SUCCESS(rc))
1236 rc = VERR_VD_GEN_INVALID_HEADER;
1237 }
1238 /* else: Do NOT signal an appropriate error here, as the VD layer has the
1239 * choice of retrying the open if it failed. */
1240 }
1241 else
1242 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1243 N_("Qed: Creating the L2 table cache for image '%s' failed"),
1244 pImage->pszFilename);
1245
1246 if (RT_SUCCESS(rc))
1247 {
1248 PVDREGIONDESC pRegion = &pImage->RegionList.aRegions[0];
1249 pImage->RegionList.fFlags = 0;
1250 pImage->RegionList.cRegions = 1;
1251
1252 pRegion->offRegion = 0; /* Disk start. */
1253 pRegion->cbBlock = 512;
1254 pRegion->enmDataForm = VDREGIONDATAFORM_RAW;
1255 pRegion->enmMetadataForm = VDREGIONMETADATAFORM_NONE;
1256 pRegion->cbData = 512;
1257 pRegion->cbMetadata = 0;
1258 pRegion->cRegionBlocksOrBytes = pImage->cbSize;
1259 }
1260 else
1261 qedFreeImage(pImage, false);
1262 return rc;
1263}
1264
1265/**
1266 * Internal: Create a qed image.
1267 */
1268static int qedCreateImage(PQEDIMAGE pImage, uint64_t cbSize,
1269 unsigned uImageFlags, const char *pszComment,
1270 PCVDGEOMETRY pPCHSGeometry,
1271 PCVDGEOMETRY pLCHSGeometry, unsigned uOpenFlags,
1272 PVDINTERFACEPROGRESS pIfProgress,
1273 unsigned uPercentStart, unsigned uPercentSpan)
1274{
1275 RT_NOREF1(pszComment);
1276 int rc;
1277
1278 if (!(uImageFlags & VD_IMAGE_FLAGS_FIXED))
1279 {
1280 rc = qedL2TblCacheCreate(pImage);
1281 if (RT_SUCCESS(rc))
1282 {
1283 pImage->uOpenFlags = uOpenFlags & ~VD_OPEN_FLAGS_READONLY;
1284 pImage->uImageFlags = uImageFlags;
1285 pImage->PCHSGeometry = *pPCHSGeometry;
1286 pImage->LCHSGeometry = *pLCHSGeometry;
1287
1288 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
1289 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
1290 AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
1291
1292 /* Create image file. */
1293 uint32_t fOpen = VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags, true /* fCreate */);
1294 rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename, fOpen, &pImage->pStorage);
1295 if (RT_SUCCESS(rc))
1296 {
1297 /* Init image state. */
1298 pImage->cbSize = cbSize;
1299 pImage->cbCluster = QED_CLUSTER_SIZE_DEFAULT;
1300 pImage->cbTable = qedCluster2Byte(pImage, QED_TABLE_SIZE_DEFAULT);
1301 pImage->cTableEntries = pImage->cbTable / sizeof(uint64_t);
1302 pImage->offL1Table = qedCluster2Byte(pImage, 1); /* Cluster 0 is the header. */
1303 pImage->cbImage = (1 * pImage->cbCluster) + pImage->cbTable; /* Header + L1 table size. */
1304 pImage->cbBackingFilename = 0;
1305 pImage->offBackingFilename = 0;
1306 qedTableMasksInit(pImage);
1307
1308 /* Init L1 table. */
1309 pImage->paL1Table = (uint64_t *)RTMemAllocZ(pImage->cbTable);
1310 if (RT_LIKELY(pImage->paL1Table))
1311 {
1312 vdIfProgress(pIfProgress, uPercentStart + uPercentSpan * 98 / 100);
1313 rc = qedFlushImage(pImage);
1314 }
1315 else
1316 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS, N_("Qed: cannot allocate memory for L1 table of image '%s'"),
1317 pImage->pszFilename);
1318 }
1319 else
1320 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("Qed: cannot create image '%s'"), pImage->pszFilename);
1321 }
1322 else
1323 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("Qed: Failed to create L2 cache for image '%s'"),
1324 pImage->pszFilename);
1325 }
1326 else
1327 rc = vdIfError(pImage->pIfError, VERR_VD_INVALID_TYPE, RT_SRC_POS, N_("Qed: cannot create fixed image '%s'"), pImage->pszFilename);
1328
1329 if (RT_SUCCESS(rc))
1330 {
1331 PVDREGIONDESC pRegion = &pImage->RegionList.aRegions[0];
1332 pImage->RegionList.fFlags = 0;
1333 pImage->RegionList.cRegions = 1;
1334
1335 pRegion->offRegion = 0; /* Disk start. */
1336 pRegion->cbBlock = 512;
1337 pRegion->enmDataForm = VDREGIONDATAFORM_RAW;
1338 pRegion->enmMetadataForm = VDREGIONMETADATAFORM_NONE;
1339 pRegion->cbData = 512;
1340 pRegion->cbMetadata = 0;
1341 pRegion->cRegionBlocksOrBytes = pImage->cbSize;
1342
1343 vdIfProgress(pIfProgress, uPercentStart + uPercentSpan);
1344 }
1345 else
1346 qedFreeImage(pImage, rc != VERR_ALREADY_EXISTS);
1347
1348 return rc;
1349}
1350
1351/**
1352 * Rollback anything done during async cluster allocation.
1353 *
1354 * @returns VBox status code.
1355 * @param pImage The image instance data.
1356 * @param pIoCtx The I/O context.
1357 * @param pClusterAlloc The cluster allocation to rollback.
1358 */
1359static int qedAsyncClusterAllocRollback(PQEDIMAGE pImage, PVDIOCTX pIoCtx, PQEDCLUSTERASYNCALLOC pClusterAlloc)
1360{
1361 RT_NOREF1(pIoCtx);
1362 int rc = VINF_SUCCESS;
1363
1364 switch (pClusterAlloc->enmAllocState)
1365 {
1366 case QEDCLUSTERASYNCALLOCSTATE_L2_ALLOC:
1367 case QEDCLUSTERASYNCALLOCSTATE_L2_LINK:
1368 {
1369 /* Revert the L1 table entry */
1370 pImage->paL1Table[pClusterAlloc->idxL1] = 0;
1371 pImage->pL2TblAlloc = NULL;
1372
1373 /* Assumption right now is that the L1 table is not modified on storage if the link fails. */
1374 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, pClusterAlloc->cbImageOld);
1375 qedL2TblCacheEntryRelease(pClusterAlloc->pL2Entry); /* Release L2 cache entry. */
1376 Assert(!pClusterAlloc->pL2Entry->cRefs);
1377 qedL2TblCacheEntryFree(pImage, pClusterAlloc->pL2Entry); /* Free it, it is not in the cache yet. */
1378 break;
1379 }
1380 case QEDCLUSTERASYNCALLOCSTATE_USER_ALLOC:
1381 case QEDCLUSTERASYNCALLOCSTATE_USER_LINK:
1382 {
1383 /* Assumption right now is that the L2 table is not modified if the link fails. */
1384 pClusterAlloc->pL2Entry->paL2Tbl[pClusterAlloc->idxL2] = 0;
1385 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, pClusterAlloc->cbImageOld);
1386 qedL2TblCacheEntryRelease(pClusterAlloc->pL2Entry); /* Release L2 cache entry. */
1387 break;
1388 }
1389 default:
1390 AssertMsgFailed(("Invalid cluster allocation state %d\n", pClusterAlloc->enmAllocState));
1391 rc = VERR_INVALID_STATE;
1392 }
1393
1394 RTMemFree(pClusterAlloc);
1395 return rc;
1396}
1397
1398/**
1399 * Updates the state of the async cluster allocation.
1400 *
1401 * @returns VBox status code.
1402 * @param pBackendData The opaque backend data.
1403 * @param pIoCtx I/O context associated with this request.
1404 * @param pvUser Opaque user data passed during a read/write request.
1405 * @param rcReq Status code for the completed request.
1406 */
1407static DECLCALLBACK(int) qedAsyncClusterAllocUpdate(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
1408{
1409 int rc = VINF_SUCCESS;
1410 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
1411 PQEDCLUSTERASYNCALLOC pClusterAlloc = (PQEDCLUSTERASYNCALLOC)pvUser;
1412
1413 if (RT_FAILURE(rcReq))
1414 return qedAsyncClusterAllocRollback(pImage, pIoCtx, pClusterAlloc);
1415
1416 AssertPtr(pClusterAlloc->pL2Entry);
1417
1418 switch (pClusterAlloc->enmAllocState)
1419 {
1420 case QEDCLUSTERASYNCALLOCSTATE_L2_ALLOC:
1421 {
1422 /* Update the link in the in memory L1 table now. */
1423 pImage->paL1Table[pClusterAlloc->idxL1] = pClusterAlloc->pL2Entry->offL2Tbl;
1424
1425 /* Update the link in the on disk L1 table now. */
1426 pClusterAlloc->enmAllocState = QEDCLUSTERASYNCALLOCSTATE_L2_LINK;
1427 rc = qedTblWrite(pImage, pIoCtx, pImage->offL1Table, pImage->paL1Table,
1428 qedAsyncClusterAllocUpdate, pClusterAlloc);
1429 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1430 break;
1431 else if (RT_FAILURE(rc))
1432 {
1433 /* Rollback. */
1434 qedAsyncClusterAllocRollback(pImage, pIoCtx, pClusterAlloc);
1435 break;
1436 }
1437 }
1438 RT_FALL_THRU();
1439 case QEDCLUSTERASYNCALLOCSTATE_L2_LINK:
1440 {
1441 /* L2 link updated in L1 , save L2 entry in cache and allocate new user data cluster. */
1442 uint64_t offData = qedClusterAllocate(pImage, 1);
1443
1444 pImage->pL2TblAlloc = NULL;
1445 qedL2TblCacheEntryInsert(pImage, pClusterAlloc->pL2Entry);
1446
1447 pClusterAlloc->enmAllocState = QEDCLUSTERASYNCALLOCSTATE_USER_ALLOC;
1448 pClusterAlloc->cbImageOld = offData;
1449 pClusterAlloc->offClusterNew = offData;
1450
1451 /* Write data. */
1452 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
1453 offData, pIoCtx, pClusterAlloc->cbToWrite,
1454 qedAsyncClusterAllocUpdate, pClusterAlloc);
1455 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1456 break;
1457 else if (RT_FAILURE(rc))
1458 {
1459 qedAsyncClusterAllocRollback(pImage, pIoCtx, pClusterAlloc);
1460 RTMemFree(pClusterAlloc);
1461 break;
1462 }
1463 }
1464 RT_FALL_THRU();
1465 case QEDCLUSTERASYNCALLOCSTATE_USER_ALLOC:
1466 {
1467 pClusterAlloc->enmAllocState = QEDCLUSTERASYNCALLOCSTATE_USER_LINK;
1468 pClusterAlloc->pL2Entry->paL2Tbl[pClusterAlloc->idxL2] = pClusterAlloc->offClusterNew;
1469
1470 /* Link L2 table and update it. */
1471 rc = qedTblWrite(pImage, pIoCtx, pImage->paL1Table[pClusterAlloc->idxL1],
1472 pClusterAlloc->pL2Entry->paL2Tbl,
1473 qedAsyncClusterAllocUpdate, pClusterAlloc);
1474 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1475 break;
1476 else if (RT_FAILURE(rc))
1477 {
1478 qedAsyncClusterAllocRollback(pImage, pIoCtx, pClusterAlloc);
1479 RTMemFree(pClusterAlloc);
1480 break;
1481 }
1482 }
1483 RT_FALL_THRU();
1484 case QEDCLUSTERASYNCALLOCSTATE_USER_LINK:
1485 {
1486 /* Everything done without errors, signal completion. */
1487 qedL2TblCacheEntryRelease(pClusterAlloc->pL2Entry);
1488 RTMemFree(pClusterAlloc);
1489 rc = VINF_SUCCESS;
1490 break;
1491 }
1492 default:
1493 AssertMsgFailed(("Invalid async cluster allocation state %d\n",
1494 pClusterAlloc->enmAllocState));
1495 }
1496
1497 return rc;
1498}
1499
1500/** @copydoc VDIMAGEBACKEND::pfnProbe */
1501static DECLCALLBACK(int) qedProbe(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
1502 PVDINTERFACE pVDIfsImage, VDTYPE *penmType)
1503{
1504 RT_NOREF1(pVDIfsDisk);
1505 LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p\n", pszFilename, pVDIfsDisk, pVDIfsImage));
1506 PVDIOSTORAGE pStorage = NULL;
1507 int rc = VINF_SUCCESS;
1508
1509 /* Get I/O interface. */
1510 PVDINTERFACEIOINT pIfIo = VDIfIoIntGet(pVDIfsImage);
1511 AssertPtrReturn(pIfIo, VERR_INVALID_PARAMETER);
1512 AssertReturn((VALID_PTR(pszFilename) && *pszFilename), VERR_INVALID_PARAMETER);
1513
1514 /*
1515 * Open the file and read the footer.
1516 */
1517 rc = vdIfIoIntFileOpen(pIfIo, pszFilename,
1518 VDOpenFlagsToFileOpenFlags(VD_OPEN_FLAGS_READONLY,
1519 false /* fCreate */),
1520 &pStorage);
1521 if (RT_SUCCESS(rc))
1522 {
1523 uint64_t cbFile;
1524
1525 rc = vdIfIoIntFileGetSize(pIfIo, pStorage, &cbFile);
1526 if ( RT_SUCCESS(rc)
1527 && cbFile > sizeof(QedHeader))
1528 {
1529 QedHeader Header;
1530
1531 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, 0, &Header, sizeof(Header));
1532 if ( RT_SUCCESS(rc)
1533 && qedHdrConvertToHostEndianess(&Header))
1534 *penmType = VDTYPE_HDD;
1535 else
1536 rc = VERR_VD_GEN_INVALID_HEADER;
1537 }
1538 else
1539 rc = VERR_VD_GEN_INVALID_HEADER;
1540 }
1541
1542 if (pStorage)
1543 vdIfIoIntFileClose(pIfIo, pStorage);
1544
1545 LogFlowFunc(("returns %Rrc\n", rc));
1546 return rc;
1547}
1548
1549/** @copydoc VDIMAGEBACKEND::pfnOpen */
1550static DECLCALLBACK(int) qedOpen(const char *pszFilename, unsigned uOpenFlags,
1551 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1552 VDTYPE enmType, void **ppBackendData)
1553{
1554 RT_NOREF1(enmType); /**< @todo r=klaus make use of the type info. */
1555
1556 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p enmType=%u ppBackendData=%#p\n",
1557 pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, enmType, ppBackendData));
1558 int rc;
1559
1560 /* Check open flags. All valid flags are supported. */
1561 AssertReturn(!(uOpenFlags & ~VD_OPEN_FLAGS_MASK), VERR_INVALID_PARAMETER);
1562 AssertReturn((VALID_PTR(pszFilename) && *pszFilename), VERR_INVALID_PARAMETER);
1563
1564 PQEDIMAGE pImage = (PQEDIMAGE)RTMemAllocZ(RT_UOFFSETOF(QEDIMAGE, RegionList.aRegions[1]));
1565 if (RT_LIKELY(pImage))
1566 {
1567 pImage->pszFilename = pszFilename;
1568 pImage->pStorage = NULL;
1569 pImage->pVDIfsDisk = pVDIfsDisk;
1570 pImage->pVDIfsImage = pVDIfsImage;
1571
1572 rc = qedOpenImage(pImage, uOpenFlags);
1573 if (RT_SUCCESS(rc))
1574 *ppBackendData = pImage;
1575 else
1576 RTMemFree(pImage);
1577 }
1578 else
1579 rc = VERR_NO_MEMORY;
1580
1581 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1582 return rc;
1583}
1584
1585/** @copydoc VDIMAGEBACKEND::pfnCreate */
1586static DECLCALLBACK(int) qedCreate(const char *pszFilename, uint64_t cbSize,
1587 unsigned uImageFlags, const char *pszComment,
1588 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
1589 PCRTUUID pUuid, unsigned uOpenFlags,
1590 unsigned uPercentStart, unsigned uPercentSpan,
1591 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1592 PVDINTERFACE pVDIfsOperation, VDTYPE enmType,
1593 void **ppBackendData)
1594{
1595 RT_NOREF1(pUuid);
1596 LogFlowFunc(("pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" pPCHSGeometry=%#p pLCHSGeometry=%#p Uuid=%RTuuid uOpenFlags=%#x uPercentStart=%u uPercentSpan=%u pVDIfsDisk=%#p pVDIfsImage=%#p pVDIfsOperation=%#p enmType=%d ppBackendData=%#p",
1597 pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData));
1598 int rc;
1599
1600 /* Check the VD container type. */
1601 if (enmType != VDTYPE_HDD)
1602 return VERR_VD_INVALID_TYPE;
1603
1604 /* Check open flags. All valid flags are supported. */
1605 AssertReturn(!(uOpenFlags & ~VD_OPEN_FLAGS_MASK), VERR_INVALID_PARAMETER);
1606 AssertReturn( VALID_PTR(pszFilename)
1607 && *pszFilename
1608 && VALID_PTR(pPCHSGeometry)
1609 && VALID_PTR(pLCHSGeometry), VERR_INVALID_PARAMETER);
1610
1611 PQEDIMAGE pImage = (PQEDIMAGE)RTMemAllocZ(RT_UOFFSETOF(QEDIMAGE, RegionList.aRegions[1]));
1612 if (RT_LIKELY(pImage))
1613 {
1614 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
1615
1616 pImage->pszFilename = pszFilename;
1617 pImage->pStorage = NULL;
1618 pImage->pVDIfsDisk = pVDIfsDisk;
1619 pImage->pVDIfsImage = pVDIfsImage;
1620
1621 rc = qedCreateImage(pImage, cbSize, uImageFlags, pszComment,
1622 pPCHSGeometry, pLCHSGeometry, uOpenFlags,
1623 pIfProgress, uPercentStart, uPercentSpan);
1624 if (RT_SUCCESS(rc))
1625 {
1626 /* So far the image is opened in read/write mode. Make sure the
1627 * image is opened in read-only mode if the caller requested that. */
1628 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
1629 {
1630 qedFreeImage(pImage, false);
1631 rc = qedOpenImage(pImage, uOpenFlags);
1632 }
1633
1634 if (RT_SUCCESS(rc))
1635 *ppBackendData = pImage;
1636 }
1637
1638 if (RT_FAILURE(rc))
1639 RTMemFree(pImage);
1640 }
1641 else
1642 rc = VERR_NO_MEMORY;
1643
1644 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1645 return rc;
1646}
1647
1648/** @copydoc VDIMAGEBACKEND::pfnRename */
1649static DECLCALLBACK(int) qedRename(void *pBackendData, const char *pszFilename)
1650{
1651 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
1652 int rc = VINF_SUCCESS;
1653 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
1654
1655 /* Check arguments. */
1656 AssertReturn((pImage && pszFilename && *pszFilename), VERR_INVALID_PARAMETER);
1657
1658 /* Close the image. */
1659 rc = qedFreeImage(pImage, false);
1660 if (RT_SUCCESS(rc))
1661 {
1662 /* Rename the file. */
1663 rc = vdIfIoIntFileMove(pImage->pIfIo, pImage->pszFilename, pszFilename, 0);
1664 if (RT_SUCCESS(rc))
1665 {
1666 /* Update pImage with the new information. */
1667 pImage->pszFilename = pszFilename;
1668
1669 /* Open the old image with new name. */
1670 rc = qedOpenImage(pImage, pImage->uOpenFlags);
1671 }
1672 else
1673 {
1674 /* The move failed, try to reopen the original image. */
1675 int rc2 = qedOpenImage(pImage, pImage->uOpenFlags);
1676 if (RT_FAILURE(rc2))
1677 rc = rc2;
1678 }
1679 }
1680
1681 LogFlowFunc(("returns %Rrc\n", rc));
1682 return rc;
1683}
1684
1685/** @copydoc VDIMAGEBACKEND::pfnClose */
1686static DECLCALLBACK(int) qedClose(void *pBackendData, bool fDelete)
1687{
1688 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
1689 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
1690
1691 int rc = qedFreeImage(pImage, fDelete);
1692 RTMemFree(pImage);
1693
1694 LogFlowFunc(("returns %Rrc\n", rc));
1695 return rc;
1696}
1697
1698/** @copydoc VDIMAGEBACKEND::pfnRead */
1699static DECLCALLBACK(int) qedRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
1700 PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
1701{
1702 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
1703 pBackendData, uOffset, pIoCtx, cbToRead, pcbActuallyRead));
1704 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
1705 uint32_t offCluster = 0;
1706 uint32_t idxL1 = 0;
1707 uint32_t idxL2 = 0;
1708 uint64_t offFile = 0;
1709
1710 AssertPtr(pImage);
1711 Assert(uOffset % 512 == 0);
1712 Assert(cbToRead % 512 == 0);
1713 AssertReturn((VALID_PTR(pIoCtx) && cbToRead), VERR_INVALID_PARAMETER);
1714 AssertReturn(uOffset + cbToRead <= pImage->cbSize, VERR_INVALID_PARAMETER);
1715
1716 qedConvertLogicalOffset(pImage, uOffset, &idxL1, &idxL2, &offCluster);
1717
1718 /* Clip read size to remain in the cluster. */
1719 cbToRead = RT_MIN(cbToRead, pImage->cbCluster - offCluster);
1720
1721 /* Get offset in image. */
1722 int rc = qedConvertToImageOffset(pImage, pIoCtx, idxL1, idxL2, offCluster, &offFile);
1723 if (RT_SUCCESS(rc))
1724 rc = vdIfIoIntFileReadUser(pImage->pIfIo, pImage->pStorage, offFile,
1725 pIoCtx, cbToRead);
1726
1727 if ( ( RT_SUCCESS(rc)
1728 || rc == VERR_VD_BLOCK_FREE
1729 || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1730 && pcbActuallyRead)
1731 *pcbActuallyRead = cbToRead;
1732
1733 LogFlowFunc(("returns %Rrc\n", rc));
1734 return rc;
1735}
1736
1737/** @copydoc VDIMAGEBACKEND::pfnWrite */
1738static DECLCALLBACK(int) qedWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
1739 PVDIOCTX pIoCtx, size_t *pcbWriteProcess, size_t *pcbPreRead,
1740 size_t *pcbPostRead, unsigned fWrite)
1741{
1742 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
1743 pBackendData, uOffset, pIoCtx, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
1744 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
1745 uint32_t offCluster = 0;
1746 uint32_t idxL1 = 0;
1747 uint32_t idxL2 = 0;
1748 uint64_t offImage = 0;
1749 int rc = VINF_SUCCESS;
1750
1751 AssertPtr(pImage);
1752 Assert(!(uOffset % 512));
1753 Assert(!(cbToWrite % 512));
1754 AssertReturn((VALID_PTR(pIoCtx) && cbToWrite), VERR_INVALID_PARAMETER);
1755 AssertReturn(uOffset + cbToWrite <= pImage->cbSize, VERR_INVALID_PARAMETER);
1756
1757 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1758 {
1759 /* Convert offset to L1, L2 index and cluster offset. */
1760 qedConvertLogicalOffset(pImage, uOffset, &idxL1, &idxL2, &offCluster);
1761
1762 /* Clip write size to remain in the cluster. */
1763 cbToWrite = RT_MIN(cbToWrite, pImage->cbCluster - offCluster);
1764 Assert(!(cbToWrite % 512));
1765
1766 /* Get offset in image. */
1767 rc = qedConvertToImageOffset(pImage, pIoCtx, idxL1, idxL2, offCluster, &offImage);
1768 if (RT_SUCCESS(rc))
1769 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
1770 offImage, pIoCtx, cbToWrite, NULL, NULL);
1771 else if (rc == VERR_VD_BLOCK_FREE)
1772 {
1773 if ( cbToWrite == pImage->cbCluster
1774 && !(fWrite & VD_WRITE_NO_ALLOC))
1775 {
1776 PQEDL2CACHEENTRY pL2Entry = NULL;
1777
1778 /* Full cluster write to previously unallocated cluster.
1779 * Allocate cluster and write data. */
1780 Assert(!offCluster);
1781
1782 do
1783 {
1784 /* Check if we have to allocate a new cluster for L2 tables. */
1785 if (!pImage->paL1Table[idxL1])
1786 {
1787 uint64_t offL2Tbl;
1788 PQEDCLUSTERASYNCALLOC pL2ClusterAlloc = NULL;
1789
1790 /* Allocate new async cluster allocation state. */
1791 pL2ClusterAlloc = (PQEDCLUSTERASYNCALLOC)RTMemAllocZ(sizeof(QEDCLUSTERASYNCALLOC));
1792 if (RT_UNLIKELY(!pL2ClusterAlloc))
1793 {
1794 rc = VERR_NO_MEMORY;
1795 break;
1796 }
1797
1798 pL2Entry = qedL2TblCacheEntryAlloc(pImage);
1799 if (!pL2Entry)
1800 {
1801 rc = VERR_NO_MEMORY;
1802 RTMemFree(pL2ClusterAlloc);
1803 break;
1804 }
1805
1806 offL2Tbl = qedClusterAllocate(pImage, qedByte2Cluster(pImage, pImage->cbTable));
1807 pL2Entry->offL2Tbl = offL2Tbl;
1808 memset(pL2Entry->paL2Tbl, 0, pImage->cbTable);
1809
1810 pL2ClusterAlloc->enmAllocState = QEDCLUSTERASYNCALLOCSTATE_L2_ALLOC;
1811 pL2ClusterAlloc->cbImageOld = offL2Tbl;
1812 pL2ClusterAlloc->offClusterNew = offL2Tbl;
1813 pL2ClusterAlloc->idxL1 = idxL1;
1814 pL2ClusterAlloc->idxL2 = idxL2;
1815 pL2ClusterAlloc->cbToWrite = cbToWrite;
1816 pL2ClusterAlloc->pL2Entry = pL2Entry;
1817
1818 pImage->pL2TblAlloc = pL2Entry;
1819
1820 LogFlowFunc(("Allocating new L2 table at cluster offset %llu\n", offL2Tbl));
1821
1822 /*
1823 * Write the L2 table first and link to the L1 table afterwards.
1824 * If something unexpected happens the worst case which can happen
1825 * is a leak of some clusters.
1826 */
1827 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
1828 offL2Tbl, pL2Entry->paL2Tbl, pImage->cbTable, pIoCtx,
1829 qedAsyncClusterAllocUpdate, pL2ClusterAlloc);
1830 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1831 break;
1832 else if (RT_FAILURE(rc))
1833 {
1834 RTMemFree(pL2ClusterAlloc);
1835 qedL2TblCacheEntryFree(pImage, pL2Entry);
1836 break;
1837 }
1838
1839 rc = qedAsyncClusterAllocUpdate(pImage, pIoCtx, pL2ClusterAlloc, rc);
1840 }
1841 else
1842 {
1843 LogFlowFunc(("Fetching L2 table at cluster offset %llu\n", pImage->paL1Table[idxL1]));
1844
1845 rc = qedL2TblCacheFetchAsync(pImage, pIoCtx, pImage->paL1Table[idxL1],
1846 &pL2Entry);
1847
1848 if (RT_SUCCESS(rc))
1849 {
1850 PQEDCLUSTERASYNCALLOC pDataClusterAlloc = NULL;
1851
1852 /* Allocate new async cluster allocation state. */
1853 pDataClusterAlloc = (PQEDCLUSTERASYNCALLOC)RTMemAllocZ(sizeof(QEDCLUSTERASYNCALLOC));
1854 if (RT_UNLIKELY(!pDataClusterAlloc))
1855 {
1856 rc = VERR_NO_MEMORY;
1857 break;
1858 }
1859
1860 /* Allocate new cluster for the data. */
1861 uint64_t offData = qedClusterAllocate(pImage, 1);
1862
1863 pDataClusterAlloc->enmAllocState = QEDCLUSTERASYNCALLOCSTATE_USER_ALLOC;
1864 pDataClusterAlloc->cbImageOld = offData;
1865 pDataClusterAlloc->offClusterNew = offData;
1866 pDataClusterAlloc->idxL1 = idxL1;
1867 pDataClusterAlloc->idxL2 = idxL2;
1868 pDataClusterAlloc->cbToWrite = cbToWrite;
1869 pDataClusterAlloc->pL2Entry = pL2Entry;
1870
1871 /* Write data. */
1872 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
1873 offData, pIoCtx, cbToWrite,
1874 qedAsyncClusterAllocUpdate, pDataClusterAlloc);
1875 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1876 break;
1877 else if (RT_FAILURE(rc))
1878 {
1879 RTMemFree(pDataClusterAlloc);
1880 break;
1881 }
1882
1883 rc = qedAsyncClusterAllocUpdate(pImage, pIoCtx, pDataClusterAlloc, rc);
1884 }
1885 }
1886
1887 } while (0);
1888
1889 *pcbPreRead = 0;
1890 *pcbPostRead = 0;
1891 }
1892 else
1893 {
1894 /* Trying to do a partial write to an unallocated cluster. Don't do
1895 * anything except letting the upper layer know what to do. */
1896 *pcbPreRead = offCluster;
1897 *pcbPostRead = pImage->cbCluster - cbToWrite - *pcbPreRead;
1898 }
1899 }
1900
1901 if (pcbWriteProcess)
1902 *pcbWriteProcess = cbToWrite;
1903 }
1904 else
1905 rc = VERR_VD_IMAGE_READ_ONLY;
1906
1907 LogFlowFunc(("returns %Rrc\n", rc));
1908 return rc;
1909}
1910
1911/** @copydoc VDIMAGEBACKEND::pfnFlush */
1912static DECLCALLBACK(int) qedFlush(void *pBackendData, PVDIOCTX pIoCtx)
1913{
1914 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1915 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
1916 int rc = VINF_SUCCESS;
1917
1918 AssertPtr(pImage);
1919 AssertPtrReturn(pIoCtx, VERR_INVALID_PARAMETER);
1920
1921 if ( pImage->pStorage
1922 && !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1923 {
1924 QedHeader Header;
1925
1926 Assert(!(pImage->cbTable % pImage->cbCluster));
1927 rc = qedTblWrite(pImage, pIoCtx, pImage->offL1Table, pImage->paL1Table,
1928 NULL, NULL);
1929 if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1930 {
1931 /* Write header. */
1932 qedHdrConvertFromHostEndianess(pImage, &Header);
1933 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
1934 0, &Header, sizeof(Header),
1935 pIoCtx, NULL, NULL);
1936 if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1937 rc = vdIfIoIntFileFlush(pImage->pIfIo, pImage->pStorage,
1938 pIoCtx, NULL, NULL);
1939 }
1940 }
1941
1942 LogFlowFunc(("returns %Rrc\n", rc));
1943 return rc;
1944}
1945
1946/** @copydoc VDIMAGEBACKEND::pfnGetVersion */
1947static DECLCALLBACK(unsigned) qedGetVersion(void *pBackendData)
1948{
1949 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1950 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
1951
1952 AssertPtrReturn(pImage, 0);
1953
1954 return 1;
1955}
1956
1957/** @copydoc VDIMAGEBACKEND::pfnGetFileSize */
1958static DECLCALLBACK(uint64_t) qedGetFileSize(void *pBackendData)
1959{
1960 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1961 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
1962 uint64_t cb = 0;
1963
1964 AssertPtrReturn(pImage, 0);
1965
1966 uint64_t cbFile;
1967 if (pImage->pStorage)
1968 {
1969 int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
1970 if (RT_SUCCESS(rc))
1971 cb += cbFile;
1972 }
1973
1974 LogFlowFunc(("returns %lld\n", cb));
1975 return cb;
1976}
1977
1978/** @copydoc VDIMAGEBACKEND::pfnGetPCHSGeometry */
1979static DECLCALLBACK(int) qedGetPCHSGeometry(void *pBackendData,
1980 PVDGEOMETRY pPCHSGeometry)
1981{
1982 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
1983 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
1984 int rc = VINF_SUCCESS;
1985
1986 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
1987
1988 if (pImage->PCHSGeometry.cCylinders)
1989 *pPCHSGeometry = pImage->PCHSGeometry;
1990 else
1991 rc = VERR_VD_GEOMETRY_NOT_SET;
1992
1993 LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
1994 return rc;
1995}
1996
1997/** @copydoc VDIMAGEBACKEND::pfnSetPCHSGeometry */
1998static DECLCALLBACK(int) qedSetPCHSGeometry(void *pBackendData,
1999 PCVDGEOMETRY pPCHSGeometry)
2000{
2001 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n",
2002 pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2003 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2004 int rc = VINF_SUCCESS;
2005
2006 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
2007
2008 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2009 rc = VERR_VD_IMAGE_READ_ONLY;
2010 else
2011 pImage->PCHSGeometry = *pPCHSGeometry;
2012
2013 LogFlowFunc(("returns %Rrc\n", rc));
2014 return rc;
2015}
2016
2017/** @copydoc VDIMAGEBACKEND::pfnGetLCHSGeometry */
2018static DECLCALLBACK(int) qedGetLCHSGeometry(void *pBackendData, PVDGEOMETRY pLCHSGeometry)
2019{
2020 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
2021 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2022 int rc = VINF_SUCCESS;
2023
2024 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
2025
2026 if (pImage->LCHSGeometry.cCylinders)
2027 *pLCHSGeometry = pImage->LCHSGeometry;
2028 else
2029 rc = VERR_VD_GEOMETRY_NOT_SET;
2030
2031 LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders,
2032 pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2033 return rc;
2034}
2035
2036/** @copydoc VDIMAGEBACKEND::pfnSetLCHSGeometry */
2037static DECLCALLBACK(int) qedSetLCHSGeometry(void *pBackendData, PCVDGEOMETRY pLCHSGeometry)
2038{
2039 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData,
2040 pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2041 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2042 int rc = VINF_SUCCESS;
2043
2044 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
2045
2046 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2047 rc = VERR_VD_IMAGE_READ_ONLY;
2048 else
2049 pImage->LCHSGeometry = *pLCHSGeometry;
2050
2051 LogFlowFunc(("returns %Rrc\n", rc));
2052 return rc;
2053}
2054
2055/** @copydoc VDIMAGEBACKEND::pfnQueryRegions */
2056static DECLCALLBACK(int) qedQueryRegions(void *pBackendData, PCVDREGIONLIST *ppRegionList)
2057{
2058 LogFlowFunc(("pBackendData=%#p ppRegionList=%#p\n", pBackendData, ppRegionList));
2059 PQEDIMAGE pThis = (PQEDIMAGE)pBackendData;
2060
2061 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2062
2063 *ppRegionList = &pThis->RegionList;
2064 LogFlowFunc(("returns %Rrc\n", VINF_SUCCESS));
2065 return VINF_SUCCESS;
2066}
2067
2068/** @copydoc VDIMAGEBACKEND::pfnRegionListRelease */
2069static DECLCALLBACK(void) qedRegionListRelease(void *pBackendData, PCVDREGIONLIST pRegionList)
2070{
2071 RT_NOREF1(pRegionList);
2072 LogFlowFunc(("pBackendData=%#p pRegionList=%#p\n", pBackendData, pRegionList));
2073 PQEDIMAGE pThis = (PQEDIMAGE)pBackendData;
2074 AssertPtr(pThis); RT_NOREF(pThis);
2075
2076 /* Nothing to do here. */
2077}
2078
2079/** @copydoc VDIMAGEBACKEND::pfnGetImageFlags */
2080static DECLCALLBACK(unsigned) qedGetImageFlags(void *pBackendData)
2081{
2082 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2083 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2084
2085 AssertPtrReturn(pImage, 0);
2086
2087 LogFlowFunc(("returns %#x\n", pImage->uImageFlags));
2088 return pImage->uImageFlags;
2089}
2090
2091/** @copydoc VDIMAGEBACKEND::pfnGetOpenFlags */
2092static DECLCALLBACK(unsigned) qedGetOpenFlags(void *pBackendData)
2093{
2094 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2095 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2096
2097 AssertPtrReturn(pImage, 0);
2098
2099 LogFlowFunc(("returns %#x\n", pImage->uOpenFlags));
2100 return pImage->uOpenFlags;
2101}
2102
2103/** @copydoc VDIMAGEBACKEND::pfnSetOpenFlags */
2104static DECLCALLBACK(int) qedSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
2105{
2106 LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
2107 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2108 int rc = VINF_SUCCESS;
2109
2110 /* Image must be opened and the new flags must be valid. */
2111 if (!pImage || (uOpenFlags & ~( VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO
2112 | VD_OPEN_FLAGS_ASYNC_IO | VD_OPEN_FLAGS_SHAREABLE
2113 | VD_OPEN_FLAGS_SEQUENTIAL | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)))
2114 rc = VERR_INVALID_PARAMETER;
2115 else
2116 {
2117 /* Implement this operation via reopening the image. */
2118 rc = qedFreeImage(pImage, false);
2119 if (RT_SUCCESS(rc))
2120 rc = qedOpenImage(pImage, uOpenFlags);
2121 }
2122
2123 LogFlowFunc(("returns %Rrc\n", rc));
2124 return rc;
2125}
2126
2127/** @copydoc VDIMAGEBACKEND::pfnGetComment */
2128VD_BACKEND_CALLBACK_GET_COMMENT_DEF_NOT_SUPPORTED(qedGetComment);
2129
2130/** @copydoc VDIMAGEBACKEND::pfnSetComment */
2131VD_BACKEND_CALLBACK_SET_COMMENT_DEF_NOT_SUPPORTED(qedSetComment, PQEDIMAGE);
2132
2133/** @copydoc VDIMAGEBACKEND::pfnGetUuid */
2134VD_BACKEND_CALLBACK_GET_UUID_DEF_NOT_SUPPORTED(qedGetUuid);
2135
2136/** @copydoc VDIMAGEBACKEND::pfnSetUuid */
2137VD_BACKEND_CALLBACK_SET_UUID_DEF_NOT_SUPPORTED(qedSetUuid, PQEDIMAGE);
2138
2139/** @copydoc VDIMAGEBACKEND::pfnGetModificationUuid */
2140VD_BACKEND_CALLBACK_GET_UUID_DEF_NOT_SUPPORTED(qedGetModificationUuid);
2141
2142/** @copydoc VDIMAGEBACKEND::pfnSetModificationUuid */
2143VD_BACKEND_CALLBACK_SET_UUID_DEF_NOT_SUPPORTED(qedSetModificationUuid, PQEDIMAGE);
2144
2145/** @copydoc VDIMAGEBACKEND::pfnGetParentUuid */
2146VD_BACKEND_CALLBACK_GET_UUID_DEF_NOT_SUPPORTED(qedGetParentUuid);
2147
2148/** @copydoc VDIMAGEBACKEND::pfnSetParentUuid */
2149VD_BACKEND_CALLBACK_SET_UUID_DEF_NOT_SUPPORTED(qedSetParentUuid, PQEDIMAGE);
2150
2151/** @copydoc VDIMAGEBACKEND::pfnGetParentModificationUuid */
2152VD_BACKEND_CALLBACK_GET_UUID_DEF_NOT_SUPPORTED(qedGetParentModificationUuid);
2153
2154/** @copydoc VDIMAGEBACKEND::pfnSetParentModificationUuid */
2155VD_BACKEND_CALLBACK_SET_UUID_DEF_NOT_SUPPORTED(qedSetParentModificationUuid, PQEDIMAGE);
2156
2157/** @copydoc VDIMAGEBACKEND::pfnDump */
2158static DECLCALLBACK(void) qedDump(void *pBackendData)
2159{
2160 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2161
2162 AssertPtrReturnVoid(pImage);
2163 vdIfErrorMessage(pImage->pIfError, "Header: Geometry PCHS=%u/%u/%u LCHS=%u/%u/%u cbSector=%llu\n",
2164 pImage->PCHSGeometry.cCylinders, pImage->PCHSGeometry.cHeads, pImage->PCHSGeometry.cSectors,
2165 pImage->LCHSGeometry.cCylinders, pImage->LCHSGeometry.cHeads, pImage->LCHSGeometry.cSectors,
2166 pImage->cbSize / 512);
2167}
2168
2169/** @copydoc VDIMAGEBACKEND::pfnGetParentFilename */
2170static DECLCALLBACK(int) qedGetParentFilename(void *pBackendData, char **ppszParentFilename)
2171{
2172 int rc = VINF_SUCCESS;
2173 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2174
2175 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
2176
2177 if (pImage->pszBackingFilename)
2178 *ppszParentFilename = RTStrDup(pImage->pszBackingFilename);
2179 else
2180 rc = VERR_NOT_SUPPORTED;
2181
2182 LogFlowFunc(("returns %Rrc\n", rc));
2183 return rc;
2184}
2185
2186/** @copydoc VDIMAGEBACKEND::pfnSetParentFilename */
2187static DECLCALLBACK(int) qedSetParentFilename(void *pBackendData, const char *pszParentFilename)
2188{
2189 int rc = VINF_SUCCESS;
2190 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2191
2192 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
2193
2194 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2195 rc = VERR_VD_IMAGE_READ_ONLY;
2196 else if ( pImage->pszBackingFilename
2197 && (strlen(pszParentFilename) > pImage->cbBackingFilename))
2198 rc = VERR_NOT_SUPPORTED; /* The new filename is longer than the old one. */
2199 else
2200 {
2201 if (pImage->pszBackingFilename)
2202 RTStrFree(pImage->pszBackingFilename);
2203 pImage->pszBackingFilename = RTStrDup(pszParentFilename);
2204 if (!pImage->pszBackingFilename)
2205 rc = VERR_NO_MEMORY;
2206 else
2207 {
2208 if (!pImage->offBackingFilename)
2209 {
2210 /* Allocate new cluster. */
2211 uint64_t offData = qedClusterAllocate(pImage, 1);
2212
2213 Assert((offData & UINT32_MAX) == offData);
2214 pImage->offBackingFilename = (uint32_t)offData;
2215 pImage->cbBackingFilename = (uint32_t)strlen(pszParentFilename);
2216 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage,
2217 offData + pImage->cbCluster);
2218 }
2219
2220 if (RT_SUCCESS(rc))
2221 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
2222 pImage->offBackingFilename,
2223 pImage->pszBackingFilename,
2224 strlen(pImage->pszBackingFilename));
2225 }
2226 }
2227
2228 LogFlowFunc(("returns %Rrc\n", rc));
2229 return rc;
2230}
2231
2232/** @copydoc VDIMAGEBACKEND::pfnResize */
2233static DECLCALLBACK(int) qedResize(void *pBackendData, uint64_t cbSize,
2234 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
2235 unsigned uPercentStart, unsigned uPercentSpan,
2236 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
2237 PVDINTERFACE pVDIfsOperation)
2238{
2239 RT_NOREF7(pPCHSGeometry, pLCHSGeometry, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation);
2240 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2241 int rc = VINF_SUCCESS;
2242
2243 /* Making the image smaller is not supported at the moment. */
2244 if (cbSize < pImage->cbSize)
2245 rc = VERR_NOT_SUPPORTED;
2246 else if (cbSize > pImage->cbSize)
2247 {
2248 /*
2249 * It is enough to just update the size field in the header to complete
2250 * growing. With the default cluster and table sizes the image can be expanded
2251 * to 64TB without overflowing the L1 and L2 tables making block relocation
2252 * superfluous.
2253 * @todo: The rare case where block relocation is still required (non default
2254 * table and/or cluster size or images with more than 64TB) is not
2255 * implemented yet and resizing such an image will fail with an error.
2256 */
2257 if (qedByte2Cluster(pImage, pImage->cbTable)*pImage->cTableEntries*pImage->cTableEntries*pImage->cbCluster < cbSize)
2258 rc = vdIfError(pImage->pIfError, VERR_BUFFER_OVERFLOW, RT_SRC_POS,
2259 N_("Qed: Resizing the image '%s' is not supported because it would overflow the L1 and L2 table\n"),
2260 pImage->pszFilename);
2261 else
2262 {
2263 uint64_t cbSizeOld = pImage->cbSize;
2264
2265 pImage->cbSize = cbSize;
2266 rc = qedFlushImage(pImage);
2267 if (RT_FAILURE(rc))
2268 {
2269 pImage->cbSize = cbSizeOld; /* Restore */
2270
2271 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("Qed: Resizing the image '%s' failed\n"),
2272 pImage->pszFilename);
2273 }
2274 }
2275 }
2276 /* Same size doesn't change the image at all. */
2277
2278 LogFlowFunc(("returns %Rrc\n", rc));
2279 return rc;
2280}
2281
2282
2283const VDIMAGEBACKEND g_QedBackend =
2284{
2285 /* u32Version */
2286 VD_IMGBACKEND_VERSION,
2287 /* pszBackendName */
2288 "QED",
2289 /* uBackendCaps */
2290 VD_CAP_FILE | VD_CAP_VFS | VD_CAP_CREATE_DYNAMIC | VD_CAP_DIFF | VD_CAP_ASYNC,
2291 /* paFileExtensions */
2292 s_aQedFileExtensions,
2293 /* paConfigInfo */
2294 NULL,
2295 /* pfnProbe */
2296 qedProbe,
2297 /* pfnOpen */
2298 qedOpen,
2299 /* pfnCreate */
2300 qedCreate,
2301 /* pfnRename */
2302 qedRename,
2303 /* pfnClose */
2304 qedClose,
2305 /* pfnRead */
2306 qedRead,
2307 /* pfnWrite */
2308 qedWrite,
2309 /* pfnFlush */
2310 qedFlush,
2311 /* pfnDiscard */
2312 NULL,
2313 /* pfnGetVersion */
2314 qedGetVersion,
2315 /* pfnGetFileSize */
2316 qedGetFileSize,
2317 /* pfnGetPCHSGeometry */
2318 qedGetPCHSGeometry,
2319 /* pfnSetPCHSGeometry */
2320 qedSetPCHSGeometry,
2321 /* pfnGetLCHSGeometry */
2322 qedGetLCHSGeometry,
2323 /* pfnSetLCHSGeometry */
2324 qedSetLCHSGeometry,
2325 /* pfnQueryRegions */
2326 qedQueryRegions,
2327 /* pfnRegionListRelease */
2328 qedRegionListRelease,
2329 /* pfnGetImageFlags */
2330 qedGetImageFlags,
2331 /* pfnGetOpenFlags */
2332 qedGetOpenFlags,
2333 /* pfnSetOpenFlags */
2334 qedSetOpenFlags,
2335 /* pfnGetComment */
2336 qedGetComment,
2337 /* pfnSetComment */
2338 qedSetComment,
2339 /* pfnGetUuid */
2340 qedGetUuid,
2341 /* pfnSetUuid */
2342 qedSetUuid,
2343 /* pfnGetModificationUuid */
2344 qedGetModificationUuid,
2345 /* pfnSetModificationUuid */
2346 qedSetModificationUuid,
2347 /* pfnGetParentUuid */
2348 qedGetParentUuid,
2349 /* pfnSetParentUuid */
2350 qedSetParentUuid,
2351 /* pfnGetParentModificationUuid */
2352 qedGetParentModificationUuid,
2353 /* pfnSetParentModificationUuid */
2354 qedSetParentModificationUuid,
2355 /* pfnDump */
2356 qedDump,
2357 /* pfnGetTimestamp */
2358 NULL,
2359 /* pfnGetParentTimestamp */
2360 NULL,
2361 /* pfnSetParentTimestamp */
2362 NULL,
2363 /* pfnGetParentFilename */
2364 qedGetParentFilename,
2365 /* pfnSetParentFilename */
2366 qedSetParentFilename,
2367 /* pfnComposeLocation */
2368 genericFileComposeLocation,
2369 /* pfnComposeName */
2370 genericFileComposeName,
2371 /* pfnCompact */
2372 NULL,
2373 /* pfnResize */
2374 qedResize,
2375 /* pfnRepair */
2376 NULL,
2377 /* pfnTraverseMetadata */
2378 NULL,
2379 /* u32Version */
2380 VD_IMGBACKEND_VERSION
2381};
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette