VirtualBox

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

Last change on this file since 63788 was 63788, checked in by vboxsync, 8 years ago

Storage: Remove unused/obsolete VDFormats directory (can be resurrected later if required again)

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