VirtualBox

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

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

Storage/VD: Convert all backends to use the region list callbacks, remove the pfnGetSize and pfnGetSectorSize callbacks because they are covered by the region lists

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