VirtualBox

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

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

Storage/QED: Check that the filename size for the backing file is in bounds if the feature flag is set

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 90.2 KB
Line 
1/* $Id: QED.cpp 63636 2016-08-25 14:09:21Z 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#if 0 /* unused */
569/**
570 * Fetches the L2 from the given offset trying the LRU cache first and
571 * reading it from the image after a cache miss.
572 *
573 * @returns VBox status code.
574 * @param pImage Image instance data.
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 qedL2TblCacheFetch(PQEDIMAGE pImage, uint64_t offL2Tbl, PQEDL2CACHEENTRY *ppL2Entry)
579{
580 int rc = VINF_SUCCESS;
581
582 LogFlowFunc(("pImage=%#p offL2Tbl=%llu ppL2Entry=%#p\n", pImage, offL2Tbl, ppL2Entry));
583
584 /* Try to fetch the L2 table from the cache first. */
585 PQEDL2CACHEENTRY pL2Entry = qedL2TblCacheRetain(pImage, offL2Tbl);
586 if (!pL2Entry)
587 {
588 LogFlowFunc(("Reading L2 table from image\n"));
589 pL2Entry = qedL2TblCacheEntryAlloc(pImage);
590
591 if (pL2Entry)
592 {
593 /* Read from the image. */
594 pL2Entry->offL2Tbl = offL2Tbl;
595 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, offL2Tbl,
596 pL2Entry->paL2Tbl, pImage->cbTable);
597 if (RT_SUCCESS(rc))
598 {
599#if defined(RT_BIG_ENDIAN)
600 qedTableConvertToHostEndianess(pL2Entry->paL2Tbl, pImage->cTableEntries);
601#endif
602 qedL2TblCacheEntryInsert(pImage, pL2Entry);
603 }
604 else
605 {
606 qedL2TblCacheEntryRelease(pL2Entry);
607 qedL2TblCacheEntryFree(pImage, pL2Entry);
608 }
609 }
610 else
611 rc = VERR_NO_MEMORY;
612 }
613
614 if (RT_SUCCESS(rc))
615 *ppL2Entry = pL2Entry;
616
617 LogFlowFunc(("returns rc=%Rrc\n", rc));
618 return rc;
619}
620#endif
621
622/**
623 * Fetches the L2 from the given offset trying the LRU cache first and
624 * reading it from the image after a cache miss - version for async I/O.
625 *
626 * @returns VBox status code.
627 * @param pImage Image instance data.
628 * @param pIoCtx The I/O context.
629 * @param offL2Tbl The offset of the L2 table in the image.
630 * @param ppL2Entry Where to store the L2 table on success.
631 */
632static int qedL2TblCacheFetchAsync(PQEDIMAGE pImage, PVDIOCTX pIoCtx,
633 uint64_t offL2Tbl, PQEDL2CACHEENTRY *ppL2Entry)
634{
635 int rc = VINF_SUCCESS;
636
637 /* Try to fetch the L2 table from the cache first. */
638 PQEDL2CACHEENTRY pL2Entry = qedL2TblCacheRetain(pImage, offL2Tbl);
639 if (!pL2Entry)
640 {
641 pL2Entry = qedL2TblCacheEntryAlloc(pImage);
642
643 if (pL2Entry)
644 {
645 /* Read from the image. */
646 PVDMETAXFER pMetaXfer;
647
648 pL2Entry->offL2Tbl = offL2Tbl;
649 rc = vdIfIoIntFileReadMeta(pImage->pIfIo, pImage->pStorage,
650 offL2Tbl, pL2Entry->paL2Tbl,
651 pImage->cbTable, pIoCtx,
652 &pMetaXfer, NULL, NULL);
653 if (RT_SUCCESS(rc))
654 {
655 vdIfIoIntMetaXferRelease(pImage->pIfIo, pMetaXfer);
656#if defined(RT_BIG_ENDIAN)
657 qedTableConvertToHostEndianess(pL2Entry->paL2Tbl, pImage->cTableEntries);
658#endif
659 qedL2TblCacheEntryInsert(pImage, pL2Entry);
660 }
661 else
662 {
663 qedL2TblCacheEntryRelease(pL2Entry);
664 qedL2TblCacheEntryFree(pImage, pL2Entry);
665 }
666 }
667 else
668 rc = VERR_NO_MEMORY;
669 }
670
671 if (RT_SUCCESS(rc))
672 *ppL2Entry = pL2Entry;
673
674 return rc;
675}
676
677/**
678 * Return power of 2 or 0 if num error.
679 *
680 * @returns The power of 2 or 0 if the given number is not a power of 2.
681 * @param u32 The number.
682 */
683static uint32_t qedGetPowerOfTwo(uint32_t u32)
684{
685 if (u32 == 0)
686 return 0;
687 uint32_t uPower2 = 0;
688 while ((u32 & 1) == 0)
689 {
690 u32 >>= 1;
691 uPower2++;
692 }
693 return u32 == 1 ? uPower2 : 0;
694}
695
696/**
697 * Sets the L1, L2 and offset bitmasks and L1 and L2 bit shift members.
698 *
699 * @returns nothing.
700 * @param pImage The image instance data.
701 */
702static void qedTableMasksInit(PQEDIMAGE pImage)
703{
704 uint32_t cClusterBits, cTableBits;
705
706 cClusterBits = qedGetPowerOfTwo(pImage->cbCluster);
707 cTableBits = qedGetPowerOfTwo(pImage->cTableEntries);
708
709 Assert(cClusterBits + 2 * cTableBits <= 64);
710
711 pImage->fOffsetMask = ((uint64_t)pImage->cbCluster - 1);
712 pImage->fL2Mask = ((uint64_t)pImage->cTableEntries - 1) << cClusterBits;
713 pImage->cL2Shift = cClusterBits;
714 pImage->fL1Mask = ((uint64_t)pImage->cTableEntries - 1) << (cClusterBits + cTableBits);
715 pImage->cL1Shift = cClusterBits + cTableBits;
716}
717
718/**
719 * Converts a given logical offset into the
720 *
721 * @returns nothing.
722 * @param pImage The image instance data.
723 * @param off The logical offset to convert.
724 * @param pidxL1 Where to store the index in the L1 table on success.
725 * @param pidxL2 Where to store the index in the L2 table on success.
726 * @param poffCluster Where to store the offset in the cluster on success.
727 */
728DECLINLINE(void) qedConvertLogicalOffset(PQEDIMAGE pImage, uint64_t off, uint32_t *pidxL1,
729 uint32_t *pidxL2, uint32_t *poffCluster)
730{
731 AssertPtr(pidxL1);
732 AssertPtr(pidxL2);
733 AssertPtr(poffCluster);
734
735 *poffCluster = off & pImage->fOffsetMask;
736 *pidxL1 = (off & pImage->fL1Mask) >> pImage->cL1Shift;
737 *pidxL2 = (off & pImage->fL2Mask) >> pImage->cL2Shift;
738}
739
740/**
741 * Converts Cluster size to a byte size.
742 *
743 * @returns Number of bytes derived from the given number of clusters.
744 * @param pImage The image instance data.
745 * @param cClusters The clusters to convert.
746 */
747DECLINLINE(uint64_t) qedCluster2Byte(PQEDIMAGE pImage, uint64_t cClusters)
748{
749 return cClusters * pImage->cbCluster;
750}
751
752/**
753 * Converts number of bytes to cluster size rounding to the next cluster.
754 *
755 * @returns Number of bytes derived from the given number of clusters.
756 * @param pImage The image instance data.
757 * @param cb Number of bytes to convert.
758 */
759DECLINLINE(uint64_t) qedByte2Cluster(PQEDIMAGE pImage, uint64_t cb)
760{
761 return cb / pImage->cbCluster + (cb % pImage->cbCluster ? 1 : 0);
762}
763
764/**
765 * Allocates a new cluster in the image.
766 *
767 * @returns The start offset of the new cluster in the image.
768 * @param pImage The image instance data.
769 * @param cCLusters Number of clusters to allocate.
770 */
771DECLINLINE(uint64_t) qedClusterAllocate(PQEDIMAGE pImage, uint32_t cClusters)
772{
773 uint64_t offCluster;
774
775 offCluster = pImage->cbImage;
776 pImage->cbImage += cClusters*pImage->cbCluster;
777
778 return offCluster;
779}
780
781/**
782 * Returns the real image offset for a given cluster or an error if the cluster is not
783 * yet allocated.
784 *
785 * @returns VBox status code.
786 * VERR_VD_BLOCK_FREE if the cluster is not yet allocated.
787 * @param pImage The image instance data.
788 * @param pIoCtx The I/O context.
789 * @param idxL1 The L1 index.
790 * @param idxL2 The L2 index.
791 * @param offCluster Offset inside the cluster.
792 * @param poffImage Where to store the image offset on success;
793 */
794static int qedConvertToImageOffset(PQEDIMAGE pImage, PVDIOCTX pIoCtx,
795 uint32_t idxL1, uint32_t idxL2,
796 uint32_t offCluster, uint64_t *poffImage)
797{
798 int rc = VERR_VD_BLOCK_FREE;
799
800 AssertReturn(idxL1 < pImage->cTableEntries, VERR_INVALID_PARAMETER);
801 AssertReturn(idxL2 < pImage->cTableEntries, VERR_INVALID_PARAMETER);
802
803 if (pImage->paL1Table[idxL1])
804 {
805 PQEDL2CACHEENTRY pL2Entry;
806
807 rc = qedL2TblCacheFetchAsync(pImage, pIoCtx, pImage->paL1Table[idxL1],
808 &pL2Entry);
809 if (RT_SUCCESS(rc))
810 {
811 /* Get real file offset. */
812 if (pL2Entry->paL2Tbl[idxL2])
813 *poffImage = pL2Entry->paL2Tbl[idxL2] + offCluster;
814 else
815 rc = VERR_VD_BLOCK_FREE;
816
817 qedL2TblCacheEntryRelease(pL2Entry);
818 }
819 }
820
821 return rc;
822}
823
824
825/**
826 * Internal. Flush image data to disk.
827 */
828static int qedFlushImage(PQEDIMAGE pImage)
829{
830 int rc = VINF_SUCCESS;
831
832 if ( pImage->pStorage
833 && !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
834 {
835 QedHeader Header;
836
837 Assert(!(pImage->cbTable % pImage->cbCluster));
838#if defined(RT_BIG_ENDIAN)
839 uint64_t *paL1TblImg = (uint64_t *)RTMemAllocZ(pImage->cbTable);
840 if (paL1TblImg)
841 {
842 qedTableConvertFromHostEndianess(paL1TblImg, pImage->paL1Table,
843 pImage->cTableEntries);
844 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
845 pImage->offL1Table, paL1TblImg,
846 pImage->cbTable);
847 RTMemFree(paL1TblImg);
848 }
849 else
850 rc = VERR_NO_MEMORY;
851#else
852 /* Write L1 table directly. */
853 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, pImage->offL1Table,
854 pImage->paL1Table, pImage->cbTable);
855#endif
856 if (RT_SUCCESS(rc))
857 {
858 /* Write header. */
859 qedHdrConvertFromHostEndianess(pImage, &Header);
860 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, 0, &Header,
861 sizeof(Header));
862 if (RT_SUCCESS(rc))
863 rc = vdIfIoIntFileFlushSync(pImage->pIfIo, pImage->pStorage);
864 }
865 }
866
867 return rc;
868}
869
870/**
871 * Flush image data to disk - version for async I/O.
872 *
873 * @returns VBox status code.
874 * @param pImage The image instance data.
875 * @param pIoCtx The I/o context
876 */
877static int qedFlushImageAsync(PQEDIMAGE pImage, PVDIOCTX pIoCtx)
878{
879 int rc = VINF_SUCCESS;
880
881 if ( pImage->pStorage
882 && !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
883 {
884 QedHeader Header;
885
886 Assert(!(pImage->cbTable % pImage->cbCluster));
887#if defined(RT_BIG_ENDIAN)
888 uint64_t *paL1TblImg = (uint64_t *)RTMemAllocZ(pImage->cbTable);
889 if (paL1TblImg)
890 {
891 qedTableConvertFromHostEndianess(paL1TblImg, pImage->paL1Table,
892 pImage->cTableEntries);
893 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
894 pImage->offL1Table, paL1TblImg,
895 pImage->cbTable, pIoCtx, NULL, NULL);
896 RTMemFree(paL1TblImg);
897 }
898 else
899 rc = VERR_NO_MEMORY;
900#else
901 /* Write L1 table directly. */
902 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
903 pImage->offL1Table, pImage->paL1Table,
904 pImage->cbTable, pIoCtx, NULL, NULL);
905#endif
906 if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
907 {
908 /* Write header. */
909 qedHdrConvertFromHostEndianess(pImage, &Header);
910 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
911 0, &Header, sizeof(Header),
912 pIoCtx, NULL, NULL);
913 if (RT_SUCCESS(rc) || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
914 rc = vdIfIoIntFileFlush(pImage->pIfIo, pImage->pStorage,
915 pIoCtx, NULL, NULL);
916 }
917 }
918
919 return rc;
920}
921
922/**
923 * Checks whether the given cluster offset is valid.
924 *
925 * @returns Whether the given cluster offset is valid.
926 * @param offCluster The table offset to check.
927 * @param cbFile The real file size of the image.
928 * @param cbCluster The cluster size in bytes.
929 */
930DECLINLINE(bool) qedIsClusterOffsetValid(uint64_t offCluster, uint64_t cbFile, size_t cbCluster)
931{
932 return (offCluster <= cbFile - cbCluster)
933 && !(offCluster & (cbCluster - 1));
934}
935
936/**
937 * Checks whether the given table offset is valid.
938 *
939 * @returns Whether the given table offset is valid.
940 * @param offTbl The table offset to check.
941 * @param cbFile The real file size of the image.
942 * @param cbTable The table size in bytes.
943 * @param cbCluster The cluster size in bytes.
944 */
945DECLINLINE(bool) qedIsTblOffsetValid(uint64_t offTbl, uint64_t cbFile, size_t cbTable, size_t cbCluster)
946{
947 return (offTbl <= cbFile - cbTable)
948 && !(offTbl & (cbCluster - 1));
949}
950
951/**
952 * Sets the specified range in the cluster bitmap checking whether any of the clusters is already
953 * used before.
954 *
955 * @returns Whether the range was clear and is set now.
956 * @param pvClusterBitmap The cluster bitmap to use.
957 * @param offClusterStart The first cluster to check and set.
958 * @param offClusterEnd The first cluster to not check and set anymore.
959 */
960static bool qedClusterBitmapCheckAndSet(void *pvClusterBitmap, uint32_t offClusterStart, uint32_t offClusterEnd)
961{
962 for (uint32_t offCluster = offClusterStart; offCluster < offClusterEnd; offCluster++)
963 if (ASMBitTest(pvClusterBitmap, offCluster))
964 return false;
965
966 ASMBitSetRange(pvClusterBitmap, offClusterStart, offClusterEnd);
967 return true;
968}
969
970/**
971 * Checks the given image for consistency, usually called when the
972 * QED_FEATURE_NEED_CHECK bit is set.
973 *
974 * @returns VBox status code.
975 * @retval VINF_SUCCESS when the image can be accessed.
976 * @param pImage The image instance data.
977 * @param pHeader The header to use for checking.
978 *
979 * @note It is not required that the image state is fully initialized Only
980 * The I/O interface and storage handle need to be valid.
981 * @note The header must be converted to the host CPU endian format already
982 * and should be validated already.
983 */
984static int qedCheckImage(PQEDIMAGE pImage, PQedHeader pHeader)
985{
986 uint64_t cbFile;
987 uint32_t cbTable;
988 uint32_t cTableEntries;
989 uint64_t *paL1Tbl = NULL;
990 uint64_t *paL2Tbl = NULL;
991 void *pvClusterBitmap = NULL;
992 uint32_t offClusterStart;
993 int rc = VINF_SUCCESS;
994
995 pImage->cbCluster = pHeader->u32ClusterSize;
996 cbTable = pHeader->u32TableSize * pHeader->u32ClusterSize;
997 cTableEntries = cbTable / sizeof(uint64_t);
998
999 do
1000 {
1001 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
1002 if (RT_FAILURE(rc))
1003 {
1004 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1005 N_("Qed: Querying the file size of image '%s' failed"),
1006 pImage->pszFilename);
1007 break;
1008 }
1009
1010 /* Allocate L1 table. */
1011 paL1Tbl = (uint64_t *)RTMemAllocZ(cbTable);
1012 if (!paL1Tbl)
1013 {
1014 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
1015 N_("Qed: Allocating memory for the L1 table for image '%s' failed"),
1016 pImage->pszFilename);
1017 break;
1018 }
1019
1020 paL2Tbl = (uint64_t *)RTMemAllocZ(cbTable);
1021 if (!paL2Tbl)
1022 {
1023 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
1024 N_("Qed: Allocating memory for the L2 table for image '%s' failed"),
1025 pImage->pszFilename);
1026 break;
1027 }
1028
1029 pvClusterBitmap = RTMemAllocZ(cbFile / pHeader->u32ClusterSize / 8);
1030 if (!pvClusterBitmap)
1031 {
1032 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
1033 N_("Qed: Allocating memory for the cluster bitmap for image '%s' failed"),
1034 pImage->pszFilename);
1035 break;
1036 }
1037
1038 /* Validate L1 table offset. */
1039 if (!qedIsTblOffsetValid(pHeader->u64OffL1Table, cbFile, cbTable, pHeader->u32ClusterSize))
1040 {
1041 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1042 N_("Qed: L1 table offset of image '%s' is corrupt (%llu)"),
1043 pImage->pszFilename, pHeader->u64OffL1Table);
1044 break;
1045 }
1046
1047 /* Read L1 table. */
1048 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
1049 pHeader->u64OffL1Table, paL1Tbl, cbTable);
1050 if (RT_FAILURE(rc))
1051 {
1052 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1053 N_("Qed: Reading the L1 table from image '%s' failed"),
1054 pImage->pszFilename);
1055 break;
1056 }
1057
1058 /* Mark the L1 table in cluster bitmap. */
1059 ASMBitSet(pvClusterBitmap, 0); /* Header is always in cluster 0. */
1060 offClusterStart = qedByte2Cluster(pImage, pHeader->u64OffL1Table);
1061 bool fSet = qedClusterBitmapCheckAndSet(pvClusterBitmap, offClusterStart, offClusterStart + pHeader->u32TableSize);
1062 Assert(fSet);
1063
1064 /* Scan the L1 and L2 tables for invalid entries. */
1065 qedTableConvertToHostEndianess(paL1Tbl, cTableEntries);
1066
1067 for (unsigned iL1 = 0; iL1 < cTableEntries; iL1++)
1068 {
1069 if (!paL1Tbl[iL1])
1070 continue; /* Skip unallocated clusters. */
1071
1072 if (!qedIsTblOffsetValid(paL1Tbl[iL1], cbFile, cbTable, pHeader->u32ClusterSize))
1073 {
1074 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1075 N_("Qed: Entry %d of the L1 table from image '%s' is invalid (%llu)"),
1076 iL1, pImage->pszFilename, paL1Tbl[iL1]);
1077 break;
1078 }
1079
1080 /* Now check that the clusters are not allocated already. */
1081 offClusterStart = qedByte2Cluster(pImage, paL1Tbl[iL1]);
1082 fSet = qedClusterBitmapCheckAndSet(pvClusterBitmap, offClusterStart, offClusterStart + pHeader->u32TableSize);
1083 if (!fSet)
1084 {
1085 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1086 N_("Qed: Entry %d of the L1 table from image '%s' points to a already used cluster (%llu)"),
1087 iL1, pImage->pszFilename, paL1Tbl[iL1]);
1088 break;
1089 }
1090
1091 /* Read the linked L2 table and check it. */
1092 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
1093 paL1Tbl[iL1], paL2Tbl, cbTable);
1094 if (RT_FAILURE(rc))
1095 {
1096 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1097 N_("Qed: Reading the L2 table from image '%s' failed"),
1098 pImage->pszFilename);
1099 break;
1100 }
1101
1102 /* Check all L2 entries. */
1103 for (unsigned iL2 = 0; iL2 < cTableEntries; iL2++)
1104 {
1105 if (paL2Tbl[iL2])
1106 continue; /* Skip unallocated clusters. */
1107
1108 if (!qedIsClusterOffsetValid(paL2Tbl[iL2], cbFile, pHeader->u32ClusterSize))
1109 {
1110 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1111 N_("Qed: Entry %d of the L2 table from image '%s' is invalid (%llu)"),
1112 iL2, pImage->pszFilename, paL2Tbl[iL2]);
1113 break;
1114 }
1115
1116 /* Now check that the clusters are not allocated already. */
1117 offClusterStart = qedByte2Cluster(pImage, paL2Tbl[iL2]);
1118 fSet = qedClusterBitmapCheckAndSet(pvClusterBitmap, offClusterStart, offClusterStart + 1);
1119 if (!fSet)
1120 {
1121 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1122 N_("Qed: Entry %d of the L2 table from image '%s' points to a already used cluster (%llu)"),
1123 iL2, pImage->pszFilename, paL2Tbl[iL2]);
1124 break;
1125 }
1126 }
1127 }
1128 } while(0);
1129
1130 if (paL1Tbl)
1131 RTMemFree(paL1Tbl);
1132 if (paL2Tbl)
1133 RTMemFree(paL2Tbl);
1134 if (pvClusterBitmap)
1135 RTMemFree(pvClusterBitmap);
1136
1137 return rc;
1138}
1139
1140/**
1141 * Internal. Free all allocated space for representing an image except pImage,
1142 * and optionally delete the image from disk.
1143 */
1144static int qedFreeImage(PQEDIMAGE pImage, bool fDelete)
1145{
1146 int rc = VINF_SUCCESS;
1147
1148 /* Freeing a never allocated image (e.g. because the open failed) is
1149 * not signalled as an error. After all nothing bad happens. */
1150 if (pImage)
1151 {
1152 if (pImage->pStorage)
1153 {
1154 /* No point updating the file that is deleted anyway. */
1155 if (!fDelete)
1156 qedFlushImage(pImage);
1157
1158 rc = vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
1159 pImage->pStorage = NULL;
1160 }
1161
1162 if (pImage->paL1Table)
1163 RTMemFree(pImage->paL1Table);
1164
1165 if (pImage->pszBackingFilename)
1166 {
1167 RTMemFree(pImage->pszBackingFilename);
1168 pImage->pszBackingFilename = NULL;
1169 }
1170
1171 qedL2TblCacheDestroy(pImage);
1172
1173 if (fDelete && pImage->pszFilename)
1174 vdIfIoIntFileDelete(pImage->pIfIo, pImage->pszFilename);
1175 }
1176
1177 LogFlowFunc(("returns %Rrc\n", rc));
1178 return rc;
1179}
1180
1181/**
1182 * Internal: Open an image, constructing all necessary data structures.
1183 */
1184static int qedOpenImage(PQEDIMAGE pImage, unsigned uOpenFlags)
1185{
1186 int rc;
1187
1188 pImage->uOpenFlags = uOpenFlags;
1189
1190 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
1191 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
1192 AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
1193
1194 /*
1195 * Create the L2 cache before opening the image so we can call qedFreeImage()
1196 * even if opening the image file fails.
1197 */
1198 rc = qedL2TblCacheCreate(pImage);
1199 if (RT_FAILURE(rc))
1200 {
1201 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1202 N_("Qed: Creating the L2 table cache for image '%s' failed"),
1203 pImage->pszFilename);
1204
1205 goto out;
1206 }
1207
1208 /*
1209 * Open the image.
1210 */
1211 rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename,
1212 VDOpenFlagsToFileOpenFlags(uOpenFlags,
1213 false /* fCreate */),
1214 &pImage->pStorage);
1215 if (RT_FAILURE(rc))
1216 {
1217 /* Do NOT signal an appropriate error here, as the VD layer has the
1218 * choice of retrying the open if it failed. */
1219 goto out;
1220 }
1221
1222 uint64_t cbFile;
1223 QedHeader Header;
1224 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
1225 if (RT_FAILURE(rc))
1226 goto out;
1227 if (cbFile > sizeof(Header))
1228 {
1229 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, 0, &Header, sizeof(Header));
1230 if ( RT_SUCCESS(rc)
1231 && qedHdrConvertToHostEndianess(&Header))
1232 {
1233 if ( !(Header.u64FeatureFlags & ~QED_FEATURE_MASK)
1234 && !(Header.u64FeatureFlags & QED_FEATURE_BACKING_FILE_NO_PROBE))
1235 {
1236 if (Header.u64FeatureFlags & QED_FEATURE_NEED_CHECK)
1237 {
1238 /* Image needs checking. */
1239 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
1240 rc = qedCheckImage(pImage, &Header);
1241 else
1242 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1243 N_("Qed: Image '%s' needs checking but is opened readonly"),
1244 pImage->pszFilename);
1245 }
1246
1247 if ( RT_SUCCESS(rc)
1248 && (Header.u64FeatureFlags & QED_FEATURE_BACKING_FILE))
1249 {
1250 /* Load backing filename from image. */
1251 pImage->pszBackingFilename = (char *)RTMemAllocZ(Header.u32BackingFilenameSize + 1); /* +1 for \0 terminator. */
1252 if (pImage->pszBackingFilename)
1253 {
1254 pImage->cbBackingFilename = Header.u32BackingFilenameSize;
1255 pImage->offBackingFilename = Header.u32OffBackingFilename;
1256 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
1257 Header.u32OffBackingFilename, pImage->pszBackingFilename,
1258 Header.u32BackingFilenameSize);
1259 }
1260 else
1261 rc = VERR_NO_MEMORY;
1262 }
1263
1264 if (RT_SUCCESS(rc))
1265 {
1266 pImage->cbImage = cbFile;
1267 pImage->cbCluster = Header.u32ClusterSize;
1268 pImage->cbTable = Header.u32TableSize * pImage->cbCluster;
1269 pImage->cTableEntries = pImage->cbTable / sizeof(uint64_t);
1270 pImage->offL1Table = Header.u64OffL1Table;
1271 pImage->cbSize = Header.u64Size;
1272 qedTableMasksInit(pImage);
1273
1274 /* Allocate L1 table. */
1275 pImage->paL1Table = (uint64_t *)RTMemAllocZ(pImage->cbTable);
1276 if (pImage->paL1Table)
1277 {
1278 /* Read from the image. */
1279 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
1280 pImage->offL1Table, pImage->paL1Table,
1281 pImage->cbTable);
1282 if (RT_SUCCESS(rc))
1283 {
1284 qedTableConvertToHostEndianess(pImage->paL1Table, pImage->cTableEntries);
1285
1286 /* If the consistency check succeeded, clear the flag by flushing the image. */
1287 if (Header.u64FeatureFlags & QED_FEATURE_NEED_CHECK)
1288 rc = qedFlushImage(pImage);
1289 }
1290 else
1291 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1292 N_("Qed: Reading the L1 table for image '%s' failed"),
1293 pImage->pszFilename);
1294 }
1295 else
1296 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
1297 N_("Qed: Out of memory allocating L1 table for image '%s'"),
1298 pImage->pszFilename);
1299 }
1300 }
1301 else
1302 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1303 N_("Qed: The image '%s' makes use of unsupported features"),
1304 pImage->pszFilename);
1305 }
1306 else if (RT_SUCCESS(rc))
1307 rc = VERR_VD_GEN_INVALID_HEADER;
1308 }
1309 else
1310 rc = VERR_VD_GEN_INVALID_HEADER;
1311
1312out:
1313 if (RT_FAILURE(rc))
1314 qedFreeImage(pImage, false);
1315 return rc;
1316}
1317
1318/**
1319 * Internal: Create a qed image.
1320 */
1321static int qedCreateImage(PQEDIMAGE pImage, uint64_t cbSize,
1322 unsigned uImageFlags, const char *pszComment,
1323 PCVDGEOMETRY pPCHSGeometry,
1324 PCVDGEOMETRY pLCHSGeometry, unsigned uOpenFlags,
1325 PFNVDPROGRESS pfnProgress, void *pvUser,
1326 unsigned uPercentStart, unsigned uPercentSpan)
1327{
1328 RT_NOREF1(pszComment);
1329 int rc;
1330 int32_t fOpen;
1331
1332 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
1333 {
1334 rc = vdIfError(pImage->pIfError, VERR_VD_INVALID_TYPE, RT_SRC_POS, N_("Qed: cannot create fixed image '%s'"), pImage->pszFilename);
1335 goto out;
1336 }
1337
1338 pImage->uOpenFlags = uOpenFlags & ~VD_OPEN_FLAGS_READONLY;
1339 pImage->uImageFlags = uImageFlags;
1340 pImage->PCHSGeometry = *pPCHSGeometry;
1341 pImage->LCHSGeometry = *pLCHSGeometry;
1342
1343 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
1344 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
1345 AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
1346
1347 /* Create image file. */
1348 fOpen = VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags, true /* fCreate */);
1349 rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename, fOpen, &pImage->pStorage);
1350 if (RT_FAILURE(rc))
1351 {
1352 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("Qed: cannot create image '%s'"), pImage->pszFilename);
1353 goto out;
1354 }
1355
1356 /* Init image state. */
1357 pImage->cbSize = cbSize;
1358 pImage->cbCluster = QED_CLUSTER_SIZE_DEFAULT;
1359 pImage->cbTable = qedCluster2Byte(pImage, QED_TABLE_SIZE_DEFAULT);
1360 pImage->cTableEntries = pImage->cbTable / sizeof(uint64_t);
1361 pImage->offL1Table = qedCluster2Byte(pImage, 1); /* Cluster 0 is the header. */
1362 pImage->cbImage = (1 * pImage->cbCluster) + pImage->cbTable; /* Header + L1 table size. */
1363 pImage->cbBackingFilename = 0;
1364 pImage->offBackingFilename = 0;
1365 qedTableMasksInit(pImage);
1366
1367 /* Init L1 table. */
1368 pImage->paL1Table = (uint64_t *)RTMemAllocZ(pImage->cbTable);
1369 if (!pImage->paL1Table)
1370 {
1371 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS, N_("Qed: cannot allocate memory for L1 table of image '%s'"),
1372 pImage->pszFilename);
1373 goto out;
1374 }
1375
1376 rc = qedL2TblCacheCreate(pImage);
1377 if (RT_FAILURE(rc))
1378 {
1379 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("Qed: Failed to create L2 cache for image '%s'"),
1380 pImage->pszFilename);
1381 goto out;
1382 }
1383
1384 if (RT_SUCCESS(rc) && pfnProgress)
1385 pfnProgress(pvUser, uPercentStart + uPercentSpan * 98 / 100);
1386
1387 rc = qedFlushImage(pImage);
1388
1389out:
1390 if (RT_SUCCESS(rc) && pfnProgress)
1391 pfnProgress(pvUser, uPercentStart + uPercentSpan);
1392
1393 if (RT_FAILURE(rc))
1394 qedFreeImage(pImage, rc != VERR_ALREADY_EXISTS);
1395 return rc;
1396}
1397
1398/**
1399 * Rollback anything done during async cluster allocation.
1400 *
1401 * @returns VBox status code.
1402 * @param pImage The image instance data.
1403 * @param pIoCtx The I/O context.
1404 * @param pClusterAlloc The cluster allocation to rollback.
1405 */
1406static int qedAsyncClusterAllocRollback(PQEDIMAGE pImage, PVDIOCTX pIoCtx, PQEDCLUSTERASYNCALLOC pClusterAlloc)
1407{
1408 RT_NOREF1(pIoCtx);
1409 int rc = VINF_SUCCESS;
1410
1411 switch (pClusterAlloc->enmAllocState)
1412 {
1413 case QEDCLUSTERASYNCALLOCSTATE_L2_ALLOC:
1414 case QEDCLUSTERASYNCALLOCSTATE_L2_LINK:
1415 {
1416 /* Assumption right now is that the L1 table is not modified if the link fails. */
1417 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, pClusterAlloc->cbImageOld);
1418 qedL2TblCacheEntryRelease(pClusterAlloc->pL2Entry); /* Release L2 cache entry. */
1419 qedL2TblCacheEntryFree(pImage, pClusterAlloc->pL2Entry); /* Free it, it is not in the cache yet. */
1420 break;
1421 }
1422 case QEDCLUSTERASYNCALLOCSTATE_USER_ALLOC:
1423 case QEDCLUSTERASYNCALLOCSTATE_USER_LINK:
1424 {
1425 /* Assumption right now is that the L2 table is not modified if the link fails. */
1426 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, pClusterAlloc->cbImageOld);
1427 qedL2TblCacheEntryRelease(pClusterAlloc->pL2Entry); /* Release L2 cache entry. */
1428 break;
1429 }
1430 default:
1431 AssertMsgFailed(("Invalid cluster allocation state %d\n", pClusterAlloc->enmAllocState));
1432 rc = VERR_INVALID_STATE;
1433 }
1434
1435 RTMemFree(pClusterAlloc);
1436 return rc;
1437}
1438
1439/**
1440 * Updates the state of the async cluster allocation.
1441 *
1442 * @returns VBox status code.
1443 * @param pBackendData The opaque backend data.
1444 * @param pIoCtx I/O context associated with this request.
1445 * @param pvUser Opaque user data passed during a read/write request.
1446 * @param rcReq Status code for the completed request.
1447 */
1448static DECLCALLBACK(int) qedAsyncClusterAllocUpdate(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
1449{
1450 int rc = VINF_SUCCESS;
1451 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
1452 PQEDCLUSTERASYNCALLOC pClusterAlloc = (PQEDCLUSTERASYNCALLOC)pvUser;
1453
1454 if (RT_FAILURE(rcReq))
1455 return qedAsyncClusterAllocRollback(pImage, pIoCtx, pClusterAlloc);
1456
1457 AssertPtr(pClusterAlloc->pL2Entry);
1458
1459 switch (pClusterAlloc->enmAllocState)
1460 {
1461 case QEDCLUSTERASYNCALLOCSTATE_L2_ALLOC:
1462 {
1463 uint64_t offUpdateLe = RT_H2LE_U64(pClusterAlloc->pL2Entry->offL2Tbl);
1464
1465 /* Update the link in the on disk L1 table now. */
1466 pClusterAlloc->enmAllocState = QEDCLUSTERASYNCALLOCSTATE_L2_LINK;
1467 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
1468 pImage->offL1Table + pClusterAlloc->idxL1*sizeof(uint64_t),
1469 &offUpdateLe, sizeof(uint64_t), pIoCtx,
1470 qedAsyncClusterAllocUpdate, pClusterAlloc);
1471 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1472 break;
1473 else if (RT_FAILURE(rc))
1474 {
1475 /* Rollback. */
1476 qedAsyncClusterAllocRollback(pImage, pIoCtx, pClusterAlloc);
1477 break;
1478 }
1479 /* Success, fall through. */
1480 }
1481 case QEDCLUSTERASYNCALLOCSTATE_L2_LINK:
1482 {
1483 /* L2 link updated in L1 , save L2 entry in cache and allocate new user data cluster. */
1484 uint64_t offData = qedClusterAllocate(pImage, 1);
1485
1486 /* Update the link in the in memory L1 table now. */
1487 pImage->paL1Table[pClusterAlloc->idxL1] = pClusterAlloc->pL2Entry->offL2Tbl;
1488 qedL2TblCacheEntryInsert(pImage, pClusterAlloc->pL2Entry);
1489
1490 pClusterAlloc->enmAllocState = QEDCLUSTERASYNCALLOCSTATE_USER_ALLOC;
1491 pClusterAlloc->cbImageOld = offData;
1492 pClusterAlloc->offClusterNew = offData;
1493
1494 /* Write data. */
1495 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
1496 offData, pIoCtx, pClusterAlloc->cbToWrite,
1497 qedAsyncClusterAllocUpdate, pClusterAlloc);
1498 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1499 break;
1500 else if (RT_FAILURE(rc))
1501 {
1502 qedAsyncClusterAllocRollback(pImage, pIoCtx, pClusterAlloc);
1503 RTMemFree(pClusterAlloc);
1504 break;
1505 }
1506 }
1507 case QEDCLUSTERASYNCALLOCSTATE_USER_ALLOC:
1508 {
1509 uint64_t offUpdateLe = RT_H2LE_U64(pClusterAlloc->offClusterNew);
1510
1511 pClusterAlloc->enmAllocState = QEDCLUSTERASYNCALLOCSTATE_USER_LINK;
1512
1513 /* Link L2 table and update it. */
1514 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
1515 pImage->paL1Table[pClusterAlloc->idxL1] + pClusterAlloc->idxL2*sizeof(uint64_t),
1516 &offUpdateLe, sizeof(uint64_t), pIoCtx,
1517 qedAsyncClusterAllocUpdate, pClusterAlloc);
1518 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1519 break;
1520 else if (RT_FAILURE(rc))
1521 {
1522 qedAsyncClusterAllocRollback(pImage, pIoCtx, pClusterAlloc);
1523 RTMemFree(pClusterAlloc);
1524 break;
1525 }
1526 }
1527 case QEDCLUSTERASYNCALLOCSTATE_USER_LINK:
1528 {
1529 /* Everything done without errors, signal completion. */
1530 pClusterAlloc->pL2Entry->paL2Tbl[pClusterAlloc->idxL2] = pClusterAlloc->offClusterNew;
1531 qedL2TblCacheEntryRelease(pClusterAlloc->pL2Entry);
1532 RTMemFree(pClusterAlloc);
1533 rc = VINF_SUCCESS;
1534 break;
1535 }
1536 default:
1537 AssertMsgFailed(("Invalid async cluster allocation state %d\n",
1538 pClusterAlloc->enmAllocState));
1539 }
1540
1541 return rc;
1542}
1543
1544/** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
1545static DECLCALLBACK(int) qedCheckIfValid(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
1546 PVDINTERFACE pVDIfsImage, VDTYPE *penmType)
1547{
1548 RT_NOREF1(pVDIfsDisk);
1549 LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p\n", pszFilename, pVDIfsDisk, pVDIfsImage));
1550 PVDIOSTORAGE pStorage = NULL;
1551 uint64_t cbFile;
1552 int rc = VINF_SUCCESS;
1553
1554 /* Get I/O interface. */
1555 PVDINTERFACEIOINT pIfIo = VDIfIoIntGet(pVDIfsImage);
1556 AssertPtrReturn(pIfIo, VERR_INVALID_PARAMETER);
1557
1558 if ( !VALID_PTR(pszFilename)
1559 || !*pszFilename)
1560 {
1561 rc = VERR_INVALID_PARAMETER;
1562 goto out;
1563 }
1564
1565 /*
1566 * Open the file and read the footer.
1567 */
1568 rc = vdIfIoIntFileOpen(pIfIo, pszFilename,
1569 VDOpenFlagsToFileOpenFlags(VD_OPEN_FLAGS_READONLY,
1570 false /* fCreate */),
1571 &pStorage);
1572 if (RT_SUCCESS(rc))
1573 {
1574 rc = vdIfIoIntFileGetSize(pIfIo, pStorage, &cbFile);
1575 if ( RT_SUCCESS(rc)
1576 && cbFile > sizeof(QedHeader))
1577 {
1578 QedHeader Header;
1579
1580 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, 0, &Header, sizeof(Header));
1581 if ( RT_SUCCESS(rc)
1582 && qedHdrConvertToHostEndianess(&Header))
1583 {
1584 *penmType = VDTYPE_HDD;
1585 rc = VINF_SUCCESS;
1586 }
1587 else
1588 rc = VERR_VD_GEN_INVALID_HEADER;
1589 }
1590 else
1591 rc = VERR_VD_GEN_INVALID_HEADER;
1592 }
1593
1594 if (pStorage)
1595 vdIfIoIntFileClose(pIfIo, pStorage);
1596
1597out:
1598 LogFlowFunc(("returns %Rrc\n", rc));
1599 return rc;
1600}
1601
1602/** @copydoc VBOXHDDBACKEND::pfnOpen */
1603static DECLCALLBACK(int) qedOpen(const char *pszFilename, unsigned uOpenFlags,
1604 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1605 VDTYPE enmType, void **ppBackendData)
1606{
1607 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p enmType=%u ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, enmType, ppBackendData));
1608 int rc;
1609 PQEDIMAGE pImage;
1610
1611 NOREF(enmType); /**< @todo r=klaus make use of the type info. */
1612
1613 /* Check open flags. All valid flags are supported. */
1614 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
1615 {
1616 rc = VERR_INVALID_PARAMETER;
1617 goto out;
1618 }
1619
1620 /* Check remaining arguments. */
1621 if ( !VALID_PTR(pszFilename)
1622 || !*pszFilename)
1623 {
1624 rc = VERR_INVALID_PARAMETER;
1625 goto out;
1626 }
1627
1628
1629 pImage = (PQEDIMAGE)RTMemAllocZ(sizeof(QEDIMAGE));
1630 if (!pImage)
1631 {
1632 rc = VERR_NO_MEMORY;
1633 goto out;
1634 }
1635 pImage->pszFilename = pszFilename;
1636 pImage->pStorage = NULL;
1637 pImage->pVDIfsDisk = pVDIfsDisk;
1638 pImage->pVDIfsImage = pVDIfsImage;
1639
1640 rc = qedOpenImage(pImage, uOpenFlags);
1641 if (RT_SUCCESS(rc))
1642 *ppBackendData = pImage;
1643 else
1644 RTMemFree(pImage);
1645
1646out:
1647 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1648 return rc;
1649}
1650
1651/** @copydoc VBOXHDDBACKEND::pfnCreate */
1652static DECLCALLBACK(int) qedCreate(const char *pszFilename, uint64_t cbSize,
1653 unsigned uImageFlags, const char *pszComment,
1654 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
1655 PCRTUUID pUuid, unsigned uOpenFlags,
1656 unsigned uPercentStart, unsigned uPercentSpan,
1657 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1658 PVDINTERFACE pVDIfsOperation, VDTYPE enmType,
1659 void **ppBackendData)
1660{
1661 RT_NOREF1(pUuid);
1662 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",
1663 pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData));
1664 int rc;
1665 PQEDIMAGE pImage;
1666
1667 PFNVDPROGRESS pfnProgress = NULL;
1668 void *pvUser = NULL;
1669 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
1670 if (pIfProgress)
1671 {
1672 pfnProgress = pIfProgress->pfnProgress;
1673 pvUser = pIfProgress->Core.pvUser;
1674 }
1675
1676 /* Check the VD container type. */
1677 if (enmType != VDTYPE_HDD)
1678 {
1679 rc = VERR_VD_INVALID_TYPE;
1680 goto out;
1681 }
1682
1683 /* Check open flags. All valid flags are supported. */
1684 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
1685 {
1686 rc = VERR_INVALID_PARAMETER;
1687 goto out;
1688 }
1689
1690 /* Check remaining arguments. */
1691 if ( !VALID_PTR(pszFilename)
1692 || !*pszFilename
1693 || !VALID_PTR(pPCHSGeometry)
1694 || !VALID_PTR(pLCHSGeometry))
1695 {
1696 rc = VERR_INVALID_PARAMETER;
1697 goto out;
1698 }
1699
1700 pImage = (PQEDIMAGE)RTMemAllocZ(sizeof(QEDIMAGE));
1701 if (!pImage)
1702 {
1703 rc = VERR_NO_MEMORY;
1704 goto out;
1705 }
1706 pImage->pszFilename = pszFilename;
1707 pImage->pStorage = NULL;
1708 pImage->pVDIfsDisk = pVDIfsDisk;
1709 pImage->pVDIfsImage = pVDIfsImage;
1710
1711 rc = qedCreateImage(pImage, cbSize, uImageFlags, pszComment,
1712 pPCHSGeometry, pLCHSGeometry, uOpenFlags,
1713 pfnProgress, pvUser, uPercentStart, uPercentSpan);
1714 if (RT_SUCCESS(rc))
1715 {
1716 /* So far the image is opened in read/write mode. Make sure the
1717 * image is opened in read-only mode if the caller requested that. */
1718 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
1719 {
1720 qedFreeImage(pImage, false);
1721 rc = qedOpenImage(pImage, uOpenFlags);
1722 if (RT_FAILURE(rc))
1723 {
1724 RTMemFree(pImage);
1725 goto out;
1726 }
1727 }
1728 *ppBackendData = pImage;
1729 }
1730 else
1731 RTMemFree(pImage);
1732
1733out:
1734 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1735 return rc;
1736}
1737
1738/** @copydoc VBOXHDDBACKEND::pfnRename */
1739static DECLCALLBACK(int) qedRename(void *pBackendData, const char *pszFilename)
1740{
1741 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
1742 int rc = VINF_SUCCESS;
1743 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
1744
1745 /* Check arguments. */
1746 if ( !pImage
1747 || !pszFilename
1748 || !*pszFilename)
1749 {
1750 rc = VERR_INVALID_PARAMETER;
1751 goto out;
1752 }
1753
1754 /* Close the image. */
1755 rc = qedFreeImage(pImage, false);
1756 if (RT_FAILURE(rc))
1757 goto out;
1758
1759 /* Rename the file. */
1760 rc = vdIfIoIntFileMove(pImage->pIfIo, pImage->pszFilename, pszFilename, 0);
1761 if (RT_FAILURE(rc))
1762 {
1763 /* The move failed, try to reopen the original image. */
1764 int rc2 = qedOpenImage(pImage, pImage->uOpenFlags);
1765 if (RT_FAILURE(rc2))
1766 rc = rc2;
1767
1768 goto out;
1769 }
1770
1771 /* Update pImage with the new information. */
1772 pImage->pszFilename = pszFilename;
1773
1774 /* Open the old image with new name. */
1775 rc = qedOpenImage(pImage, pImage->uOpenFlags);
1776 if (RT_FAILURE(rc))
1777 goto out;
1778
1779out:
1780 LogFlowFunc(("returns %Rrc\n", rc));
1781 return rc;
1782}
1783
1784/** @copydoc VBOXHDDBACKEND::pfnClose */
1785static DECLCALLBACK(int) qedClose(void *pBackendData, bool fDelete)
1786{
1787 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
1788 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
1789 int rc;
1790
1791 rc = qedFreeImage(pImage, fDelete);
1792 RTMemFree(pImage);
1793
1794 LogFlowFunc(("returns %Rrc\n", rc));
1795 return rc;
1796}
1797
1798static DECLCALLBACK(int) qedRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
1799 PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
1800{
1801 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
1802 pBackendData, uOffset, pIoCtx, cbToRead, pcbActuallyRead));
1803 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
1804 uint32_t offCluster = 0;
1805 uint32_t idxL1 = 0;
1806 uint32_t idxL2 = 0;
1807 uint64_t offFile = 0;
1808 int rc;
1809
1810 AssertPtr(pImage);
1811 Assert(uOffset % 512 == 0);
1812 Assert(cbToRead % 512 == 0);
1813
1814 if (!VALID_PTR(pIoCtx) || !cbToRead)
1815 {
1816 rc = VERR_INVALID_PARAMETER;
1817 goto out;
1818 }
1819
1820 if ( uOffset + cbToRead > pImage->cbSize
1821 || cbToRead == 0)
1822 {
1823 rc = VERR_INVALID_PARAMETER;
1824 goto out;
1825 }
1826
1827 qedConvertLogicalOffset(pImage, uOffset, &idxL1, &idxL2, &offCluster);
1828
1829 /* Clip read size to remain in the cluster. */
1830 cbToRead = RT_MIN(cbToRead, pImage->cbCluster - offCluster);
1831
1832 /* Get offset in image. */
1833 rc = qedConvertToImageOffset(pImage, pIoCtx, idxL1, idxL2, offCluster, &offFile);
1834 if (RT_SUCCESS(rc))
1835 rc = vdIfIoIntFileReadUser(pImage->pIfIo, pImage->pStorage, offFile,
1836 pIoCtx, cbToRead);
1837
1838 if ( ( RT_SUCCESS(rc)
1839 || rc == VERR_VD_BLOCK_FREE
1840 || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1841 && pcbActuallyRead)
1842 *pcbActuallyRead = cbToRead;
1843
1844out:
1845 LogFlowFunc(("returns %Rrc\n", rc));
1846 return rc;
1847}
1848
1849static DECLCALLBACK(int) qedWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
1850 PVDIOCTX pIoCtx, size_t *pcbWriteProcess, size_t *pcbPreRead,
1851 size_t *pcbPostRead, unsigned fWrite)
1852{
1853 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
1854 pBackendData, uOffset, pIoCtx, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
1855 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
1856 uint32_t offCluster = 0;
1857 uint32_t idxL1 = 0;
1858 uint32_t idxL2 = 0;
1859 uint64_t offImage = 0;
1860 int rc = VINF_SUCCESS;
1861
1862 AssertPtr(pImage);
1863 Assert(!(uOffset % 512));
1864 Assert(!(cbToWrite % 512));
1865
1866 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1867 {
1868 rc = VERR_VD_IMAGE_READ_ONLY;
1869 goto out;
1870 }
1871
1872 if (!VALID_PTR(pIoCtx) || !cbToWrite)
1873 {
1874 rc = VERR_INVALID_PARAMETER;
1875 goto out;
1876 }
1877
1878 if ( uOffset + cbToWrite > pImage->cbSize
1879 || cbToWrite == 0)
1880 {
1881 rc = VERR_INVALID_PARAMETER;
1882 goto out;
1883 }
1884
1885 /* Convert offset to L1, L2 index and cluster offset. */
1886 qedConvertLogicalOffset(pImage, uOffset, &idxL1, &idxL2, &offCluster);
1887
1888 /* Clip write size to remain in the cluster. */
1889 cbToWrite = RT_MIN(cbToWrite, pImage->cbCluster - offCluster);
1890 Assert(!(cbToWrite % 512));
1891
1892 /* Get offset in image. */
1893 rc = qedConvertToImageOffset(pImage, pIoCtx, idxL1, idxL2, offCluster, &offImage);
1894 if (RT_SUCCESS(rc))
1895 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
1896 offImage, pIoCtx, cbToWrite, NULL, NULL);
1897 else if (rc == VERR_VD_BLOCK_FREE)
1898 {
1899 if ( cbToWrite == pImage->cbCluster
1900 && !(fWrite & VD_WRITE_NO_ALLOC))
1901 {
1902 PQEDL2CACHEENTRY pL2Entry = NULL;
1903
1904 /* Full cluster write to previously unallocated cluster.
1905 * Allocate cluster and write data. */
1906 Assert(!offCluster);
1907
1908 do
1909 {
1910 /* Check if we have to allocate a new cluster for L2 tables. */
1911 if (!pImage->paL1Table[idxL1])
1912 {
1913 uint64_t offL2Tbl;
1914 PQEDCLUSTERASYNCALLOC pL2ClusterAlloc = NULL;
1915
1916 /* Allocate new async cluster allocation state. */
1917 pL2ClusterAlloc = (PQEDCLUSTERASYNCALLOC)RTMemAllocZ(sizeof(QEDCLUSTERASYNCALLOC));
1918 if (RT_UNLIKELY(!pL2ClusterAlloc))
1919 {
1920 rc = VERR_NO_MEMORY;
1921 break;
1922 }
1923
1924 pL2Entry = qedL2TblCacheEntryAlloc(pImage);
1925 if (!pL2Entry)
1926 {
1927 rc = VERR_NO_MEMORY;
1928 RTMemFree(pL2ClusterAlloc);
1929 break;
1930 }
1931
1932 offL2Tbl = qedClusterAllocate(pImage, qedByte2Cluster(pImage, pImage->cbTable));
1933 pL2Entry->offL2Tbl = offL2Tbl;
1934 memset(pL2Entry->paL2Tbl, 0, pImage->cbTable);
1935
1936 pL2ClusterAlloc->enmAllocState = QEDCLUSTERASYNCALLOCSTATE_L2_ALLOC;
1937 pL2ClusterAlloc->cbImageOld = offL2Tbl;
1938 pL2ClusterAlloc->offClusterNew = offL2Tbl;
1939 pL2ClusterAlloc->idxL1 = idxL1;
1940 pL2ClusterAlloc->idxL2 = idxL2;
1941 pL2ClusterAlloc->cbToWrite = cbToWrite;
1942 pL2ClusterAlloc->pL2Entry = pL2Entry;
1943
1944 /*
1945 * Write the L2 table first and link to the L1 table afterwards.
1946 * If something unexpected happens the worst case which can happen
1947 * is a leak of some clusters.
1948 */
1949 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
1950 offL2Tbl, pL2Entry->paL2Tbl, pImage->cbTable, pIoCtx,
1951 qedAsyncClusterAllocUpdate, pL2ClusterAlloc);
1952 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1953 break;
1954 else if (RT_FAILURE(rc))
1955 {
1956 RTMemFree(pL2ClusterAlloc);
1957 qedL2TblCacheEntryFree(pImage, pL2Entry);
1958 break;
1959 }
1960
1961 rc = qedAsyncClusterAllocUpdate(pImage, pIoCtx, pL2ClusterAlloc, rc);
1962 }
1963 else
1964 {
1965 rc = qedL2TblCacheFetchAsync(pImage, pIoCtx, pImage->paL1Table[idxL1],
1966 &pL2Entry);
1967
1968 if (RT_SUCCESS(rc))
1969 {
1970 PQEDCLUSTERASYNCALLOC pDataClusterAlloc = NULL;
1971
1972 /* Allocate new async cluster allocation state. */
1973 pDataClusterAlloc = (PQEDCLUSTERASYNCALLOC)RTMemAllocZ(sizeof(QEDCLUSTERASYNCALLOC));
1974 if (RT_UNLIKELY(!pDataClusterAlloc))
1975 {
1976 rc = VERR_NO_MEMORY;
1977 break;
1978 }
1979
1980 /* Allocate new cluster for the data. */
1981 uint64_t offData = qedClusterAllocate(pImage, 1);
1982
1983 pDataClusterAlloc->enmAllocState = QEDCLUSTERASYNCALLOCSTATE_USER_ALLOC;
1984 pDataClusterAlloc->cbImageOld = offData;
1985 pDataClusterAlloc->offClusterNew = offData;
1986 pDataClusterAlloc->idxL1 = idxL1;
1987 pDataClusterAlloc->idxL2 = idxL2;
1988 pDataClusterAlloc->cbToWrite = cbToWrite;
1989 pDataClusterAlloc->pL2Entry = pL2Entry;
1990
1991 /* Write data. */
1992 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
1993 offData, pIoCtx, cbToWrite,
1994 qedAsyncClusterAllocUpdate, pDataClusterAlloc);
1995 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1996 break;
1997 else if (RT_FAILURE(rc))
1998 {
1999 RTMemFree(pDataClusterAlloc);
2000 break;
2001 }
2002
2003 rc = qedAsyncClusterAllocUpdate(pImage, pIoCtx, pDataClusterAlloc, rc);
2004 }
2005 }
2006
2007 } while (0);
2008
2009 *pcbPreRead = 0;
2010 *pcbPostRead = 0;
2011 }
2012 else
2013 {
2014 /* Trying to do a partial write to an unallocated cluster. Don't do
2015 * anything except letting the upper layer know what to do. */
2016 *pcbPreRead = offCluster;
2017 *pcbPostRead = pImage->cbCluster - cbToWrite - *pcbPreRead;
2018 }
2019 }
2020
2021 if (pcbWriteProcess)
2022 *pcbWriteProcess = cbToWrite;
2023
2024
2025out:
2026 LogFlowFunc(("returns %Rrc\n", rc));
2027 return rc;
2028}
2029
2030static DECLCALLBACK(int) qedFlush(void *pBackendData, PVDIOCTX pIoCtx)
2031{
2032 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2033 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2034 int rc = VINF_SUCCESS;
2035
2036 Assert(pImage);
2037
2038 if (VALID_PTR(pIoCtx))
2039 rc = qedFlushImageAsync(pImage, pIoCtx);
2040 else
2041 rc = VERR_INVALID_PARAMETER;
2042
2043 LogFlowFunc(("returns %Rrc\n", rc));
2044 return rc;
2045}
2046
2047/** @copydoc VBOXHDDBACKEND::pfnGetVersion */
2048static DECLCALLBACK(unsigned) qedGetVersion(void *pBackendData)
2049{
2050 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2051 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2052
2053 AssertPtr(pImage);
2054
2055 if (pImage)
2056 return 1;
2057 else
2058 return 0;
2059}
2060
2061/** @copydoc VBOXHDDBACKEND::pfnGetSectorSize */
2062static DECLCALLBACK(uint32_t) qedGetSectorSize(void *pBackendData)
2063{
2064 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2065 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2066 uint32_t cb = 0;
2067
2068 AssertPtr(pImage);
2069
2070 if (pImage && pImage->pStorage)
2071 cb = 512;
2072
2073 LogFlowFunc(("returns %u\n", cb));
2074 return cb;
2075}
2076
2077/** @copydoc VBOXHDDBACKEND::pfnGetSize */
2078static DECLCALLBACK(uint64_t) qedGetSize(void *pBackendData)
2079{
2080 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2081 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2082 uint64_t cb = 0;
2083
2084 AssertPtr(pImage);
2085
2086 if (pImage && pImage->pStorage)
2087 cb = pImage->cbSize;
2088
2089 LogFlowFunc(("returns %llu\n", cb));
2090 return cb;
2091}
2092
2093/** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
2094static DECLCALLBACK(uint64_t) qedGetFileSize(void *pBackendData)
2095{
2096 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2097 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2098 uint64_t cb = 0;
2099
2100 AssertPtr(pImage);
2101
2102 if (pImage)
2103 {
2104 uint64_t cbFile;
2105 if (pImage->pStorage)
2106 {
2107 int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
2108 if (RT_SUCCESS(rc))
2109 cb += cbFile;
2110 }
2111 }
2112
2113 LogFlowFunc(("returns %lld\n", cb));
2114 return cb;
2115}
2116
2117/** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
2118static DECLCALLBACK(int) qedGetPCHSGeometry(void *pBackendData,
2119 PVDGEOMETRY pPCHSGeometry)
2120{
2121 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
2122 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2123 int rc;
2124
2125 AssertPtr(pImage);
2126
2127 if (pImage)
2128 {
2129 if (pImage->PCHSGeometry.cCylinders)
2130 {
2131 *pPCHSGeometry = pImage->PCHSGeometry;
2132 rc = VINF_SUCCESS;
2133 }
2134 else
2135 rc = VERR_VD_GEOMETRY_NOT_SET;
2136 }
2137 else
2138 rc = VERR_VD_NOT_OPENED;
2139
2140 LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2141 return rc;
2142}
2143
2144/** @copydoc VBOXHDDBACKEND::pfnSetPCHSGeometry */
2145static DECLCALLBACK(int) qedSetPCHSGeometry(void *pBackendData,
2146 PCVDGEOMETRY pPCHSGeometry)
2147{
2148 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2149 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2150 int rc;
2151
2152 AssertPtr(pImage);
2153
2154 if (pImage)
2155 {
2156 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2157 {
2158 rc = VERR_VD_IMAGE_READ_ONLY;
2159 goto out;
2160 }
2161
2162 pImage->PCHSGeometry = *pPCHSGeometry;
2163 rc = VINF_SUCCESS;
2164 }
2165 else
2166 rc = VERR_VD_NOT_OPENED;
2167
2168out:
2169 LogFlowFunc(("returns %Rrc\n", rc));
2170 return rc;
2171}
2172
2173/** @copydoc VBOXHDDBACKEND::pfnGetLCHSGeometry */
2174static DECLCALLBACK(int) qedGetLCHSGeometry(void *pBackendData,
2175 PVDGEOMETRY pLCHSGeometry)
2176{
2177 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
2178 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2179 int rc;
2180
2181 AssertPtr(pImage);
2182
2183 if (pImage)
2184 {
2185 if (pImage->LCHSGeometry.cCylinders)
2186 {
2187 *pLCHSGeometry = pImage->LCHSGeometry;
2188 rc = VINF_SUCCESS;
2189 }
2190 else
2191 rc = VERR_VD_GEOMETRY_NOT_SET;
2192 }
2193 else
2194 rc = VERR_VD_NOT_OPENED;
2195
2196 LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2197 return rc;
2198}
2199
2200/** @copydoc VBOXHDDBACKEND::pfnSetLCHSGeometry */
2201static DECLCALLBACK(int) qedSetLCHSGeometry(void *pBackendData,
2202 PCVDGEOMETRY pLCHSGeometry)
2203{
2204 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2205 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2206 int rc;
2207
2208 AssertPtr(pImage);
2209
2210 if (pImage)
2211 {
2212 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2213 {
2214 rc = VERR_VD_IMAGE_READ_ONLY;
2215 goto out;
2216 }
2217
2218 pImage->LCHSGeometry = *pLCHSGeometry;
2219 rc = VINF_SUCCESS;
2220 }
2221 else
2222 rc = VERR_VD_NOT_OPENED;
2223
2224out:
2225 LogFlowFunc(("returns %Rrc\n", rc));
2226 return rc;
2227}
2228
2229/** @copydoc VBOXHDDBACKEND::pfnGetImageFlags */
2230static DECLCALLBACK(unsigned) qedGetImageFlags(void *pBackendData)
2231{
2232 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2233 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2234 unsigned uImageFlags;
2235
2236 AssertPtr(pImage);
2237
2238 if (pImage)
2239 uImageFlags = pImage->uImageFlags;
2240 else
2241 uImageFlags = 0;
2242
2243 LogFlowFunc(("returns %#x\n", uImageFlags));
2244 return uImageFlags;
2245}
2246
2247/** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
2248static DECLCALLBACK(unsigned) qedGetOpenFlags(void *pBackendData)
2249{
2250 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2251 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2252 unsigned uOpenFlags;
2253
2254 AssertPtr(pImage);
2255
2256 if (pImage)
2257 uOpenFlags = pImage->uOpenFlags;
2258 else
2259 uOpenFlags = 0;
2260
2261 LogFlowFunc(("returns %#x\n", uOpenFlags));
2262 return uOpenFlags;
2263}
2264
2265/** @copydoc VBOXHDDBACKEND::pfnSetOpenFlags */
2266static DECLCALLBACK(int) qedSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
2267{
2268 LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
2269 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2270 int rc;
2271
2272 /* Image must be opened and the new flags must be valid. */
2273 if (!pImage || (uOpenFlags & ~( VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO
2274 | VD_OPEN_FLAGS_ASYNC_IO | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)))
2275 {
2276 rc = VERR_INVALID_PARAMETER;
2277 goto out;
2278 }
2279
2280 /* Implement this operation via reopening the image. */
2281 rc = qedFreeImage(pImage, false);
2282 if (RT_FAILURE(rc))
2283 goto out;
2284 rc = qedOpenImage(pImage, uOpenFlags);
2285
2286out:
2287 LogFlowFunc(("returns %Rrc\n", rc));
2288 return rc;
2289}
2290
2291/** @copydoc VBOXHDDBACKEND::pfnGetComment */
2292static DECLCALLBACK(int) qedGetComment(void *pBackendData, char *pszComment,
2293 size_t cbComment)
2294{
2295 RT_NOREF2(pszComment, cbComment);
2296 LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
2297 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2298 int rc;
2299
2300 AssertPtr(pImage);
2301
2302 if (pImage)
2303 rc = VERR_NOT_SUPPORTED;
2304 else
2305 rc = VERR_VD_NOT_OPENED;
2306
2307 LogFlowFunc(("returns %Rrc comment='%s'\n", rc, pszComment));
2308 return rc;
2309}
2310
2311/** @copydoc VBOXHDDBACKEND::pfnSetComment */
2312static DECLCALLBACK(int) qedSetComment(void *pBackendData, const char *pszComment)
2313{
2314 RT_NOREF1(pszComment);
2315 LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
2316 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2317 int rc;
2318
2319 AssertPtr(pImage);
2320
2321 if (pImage)
2322 {
2323 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2324 rc = VERR_VD_IMAGE_READ_ONLY;
2325 else
2326 rc = VERR_NOT_SUPPORTED;
2327 }
2328 else
2329 rc = VERR_VD_NOT_OPENED;
2330
2331 LogFlowFunc(("returns %Rrc\n", rc));
2332 return rc;
2333}
2334
2335/** @copydoc VBOXHDDBACKEND::pfnGetUuid */
2336static DECLCALLBACK(int) qedGetUuid(void *pBackendData, PRTUUID pUuid)
2337{
2338 RT_NOREF1(pUuid);
2339 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2340 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2341 int rc;
2342
2343 AssertPtr(pImage);
2344
2345 if (pImage)
2346 rc = VERR_NOT_SUPPORTED;
2347 else
2348 rc = VERR_VD_NOT_OPENED;
2349
2350 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2351 return rc;
2352}
2353
2354/** @copydoc VBOXHDDBACKEND::pfnSetUuid */
2355static DECLCALLBACK(int) qedSetUuid(void *pBackendData, PCRTUUID pUuid)
2356{
2357 RT_NOREF1(pUuid);
2358 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2359 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2360 int rc;
2361
2362 LogFlowFunc(("%RTuuid\n", pUuid));
2363 AssertPtr(pImage);
2364
2365 if (pImage)
2366 {
2367 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2368 rc = VERR_NOT_SUPPORTED;
2369 else
2370 rc = VERR_VD_IMAGE_READ_ONLY;
2371 }
2372 else
2373 rc = VERR_VD_NOT_OPENED;
2374
2375 LogFlowFunc(("returns %Rrc\n", rc));
2376 return rc;
2377}
2378
2379/** @copydoc VBOXHDDBACKEND::pfnGetModificationUuid */
2380static DECLCALLBACK(int) qedGetModificationUuid(void *pBackendData, PRTUUID pUuid)
2381{
2382 RT_NOREF1(pUuid);
2383 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2384 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2385 int rc;
2386
2387 AssertPtr(pImage);
2388
2389 if (pImage)
2390 rc = VERR_NOT_SUPPORTED;
2391 else
2392 rc = VERR_VD_NOT_OPENED;
2393
2394 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2395 return rc;
2396}
2397
2398/** @copydoc VBOXHDDBACKEND::pfnSetModificationUuid */
2399static DECLCALLBACK(int) qedSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
2400{
2401 RT_NOREF1(pUuid);
2402 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2403 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2404 int rc;
2405
2406 AssertPtr(pImage);
2407
2408 if (pImage)
2409 {
2410 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2411 rc = VERR_NOT_SUPPORTED;
2412 else
2413 rc = VERR_VD_IMAGE_READ_ONLY;
2414 }
2415 else
2416 rc = VERR_VD_NOT_OPENED;
2417
2418 LogFlowFunc(("returns %Rrc\n", rc));
2419 return rc;
2420}
2421
2422/** @copydoc VBOXHDDBACKEND::pfnGetParentUuid */
2423static DECLCALLBACK(int) qedGetParentUuid(void *pBackendData, PRTUUID pUuid)
2424{
2425 RT_NOREF1(pUuid);
2426 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2427 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2428 int rc;
2429
2430 AssertPtr(pImage);
2431
2432 if (pImage)
2433 rc = VERR_NOT_SUPPORTED;
2434 else
2435 rc = VERR_VD_NOT_OPENED;
2436
2437 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2438 return rc;
2439}
2440
2441/** @copydoc VBOXHDDBACKEND::pfnSetParentUuid */
2442static DECLCALLBACK(int) qedSetParentUuid(void *pBackendData, PCRTUUID pUuid)
2443{
2444 RT_NOREF1(pUuid);
2445 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2446 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2447 int rc;
2448
2449 AssertPtr(pImage);
2450
2451 if (pImage)
2452 {
2453 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2454 rc = VERR_NOT_SUPPORTED;
2455 else
2456 rc = VERR_VD_IMAGE_READ_ONLY;
2457 }
2458 else
2459 rc = VERR_VD_NOT_OPENED;
2460
2461 LogFlowFunc(("returns %Rrc\n", rc));
2462 return rc;
2463}
2464
2465/** @copydoc VBOXHDDBACKEND::pfnGetParentModificationUuid */
2466static DECLCALLBACK(int) qedGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
2467{
2468 RT_NOREF1(pUuid);
2469 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2470 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2471 int rc;
2472
2473 AssertPtr(pImage);
2474
2475 if (pImage)
2476 rc = VERR_NOT_SUPPORTED;
2477 else
2478 rc = VERR_VD_NOT_OPENED;
2479
2480 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2481 return rc;
2482}
2483
2484/** @copydoc VBOXHDDBACKEND::pfnSetParentModificationUuid */
2485static DECLCALLBACK(int) qedSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
2486{
2487 RT_NOREF1(pUuid);
2488 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2489 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2490 int rc;
2491
2492 AssertPtr(pImage);
2493
2494 if (pImage)
2495 {
2496 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2497 rc = VERR_NOT_SUPPORTED;
2498 else
2499 rc = VERR_VD_IMAGE_READ_ONLY;
2500 }
2501 else
2502 rc = VERR_VD_NOT_OPENED;
2503
2504 LogFlowFunc(("returns %Rrc\n", rc));
2505 return rc;
2506}
2507
2508/** @copydoc VBOXHDDBACKEND::pfnDump */
2509static DECLCALLBACK(void) qedDump(void *pBackendData)
2510{
2511 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2512
2513 AssertPtr(pImage);
2514 if (pImage)
2515 {
2516 vdIfErrorMessage(pImage->pIfError, "Header: Geometry PCHS=%u/%u/%u LCHS=%u/%u/%u cbSector=%llu\n",
2517 pImage->PCHSGeometry.cCylinders, pImage->PCHSGeometry.cHeads, pImage->PCHSGeometry.cSectors,
2518 pImage->LCHSGeometry.cCylinders, pImage->LCHSGeometry.cHeads, pImage->LCHSGeometry.cSectors,
2519 pImage->cbSize / 512);
2520 }
2521}
2522
2523/** @copydoc VBOXHDDBACKEND::pfnGetParentFilename */
2524static DECLCALLBACK(int) qedGetParentFilename(void *pBackendData, char **ppszParentFilename)
2525{
2526 int rc = VINF_SUCCESS;
2527 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2528
2529 AssertPtr(pImage);
2530 if (pImage)
2531 if (pImage->pszBackingFilename)
2532 *ppszParentFilename = RTStrDup(pImage->pszBackingFilename);
2533 else
2534 rc = VERR_NOT_SUPPORTED;
2535 else
2536 rc = VERR_VD_NOT_OPENED;
2537
2538 LogFlowFunc(("returns %Rrc\n", rc));
2539 return rc;
2540}
2541
2542/** @copydoc VBOXHDDBACKEND::pfnSetParentFilename */
2543static DECLCALLBACK(int) qedSetParentFilename(void *pBackendData, const char *pszParentFilename)
2544{
2545 int rc = VINF_SUCCESS;
2546 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2547
2548 AssertPtr(pImage);
2549 if (pImage)
2550 {
2551 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2552 rc = VERR_VD_IMAGE_READ_ONLY;
2553 else if ( pImage->pszBackingFilename
2554 && (strlen(pszParentFilename) > pImage->cbBackingFilename))
2555 rc = VERR_NOT_SUPPORTED; /* The new filename is longer than the old one. */
2556 else
2557 {
2558 if (pImage->pszBackingFilename)
2559 RTStrFree(pImage->pszBackingFilename);
2560 pImage->pszBackingFilename = RTStrDup(pszParentFilename);
2561 if (!pImage->pszBackingFilename)
2562 rc = VERR_NO_MEMORY;
2563 else
2564 {
2565 if (!pImage->offBackingFilename)
2566 {
2567 /* Allocate new cluster. */
2568 uint64_t offData = qedClusterAllocate(pImage, 1);
2569
2570 Assert((offData & UINT32_MAX) == offData);
2571 pImage->offBackingFilename = (uint32_t)offData;
2572 pImage->cbBackingFilename = (uint32_t)strlen(pszParentFilename);
2573 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage,
2574 offData + pImage->cbCluster);
2575 }
2576
2577 if (RT_SUCCESS(rc))
2578 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
2579 pImage->offBackingFilename,
2580 pImage->pszBackingFilename,
2581 strlen(pImage->pszBackingFilename));
2582 }
2583 }
2584 }
2585 else
2586 rc = VERR_VD_NOT_OPENED;
2587
2588 LogFlowFunc(("returns %Rrc\n", rc));
2589 return rc;
2590}
2591
2592/** @copydoc VBOXHDDBACKEND::pfnResize */
2593static DECLCALLBACK(int) qedResize(void *pBackendData, uint64_t cbSize,
2594 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
2595 unsigned uPercentStart, unsigned uPercentSpan,
2596 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
2597 PVDINTERFACE pVDIfsOperation)
2598{
2599 RT_NOREF7(pPCHSGeometry, pLCHSGeometry, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation);
2600 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2601 int rc = VINF_SUCCESS;
2602
2603 /* Making the image smaller is not supported at the moment. */
2604 if (cbSize < pImage->cbSize)
2605 rc = VERR_NOT_SUPPORTED;
2606 else if (cbSize > pImage->cbSize)
2607 {
2608 /*
2609 * It is enough to just update the size field in the header to complete
2610 * growing. With the default cluster and table sizes the image can be expanded
2611 * to 64TB without overflowing the L1 and L2 tables making block relocation
2612 * superfluous.
2613 * @todo: The rare case where block relocation is still required (non default
2614 * table and/or cluster size or images with more than 64TB) is not
2615 * implemented yet and resizing such an image will fail with an error.
2616 */
2617 if (qedByte2Cluster(pImage, pImage->cbTable)*pImage->cTableEntries*pImage->cTableEntries*pImage->cbCluster < cbSize)
2618 rc = vdIfError(pImage->pIfError, VERR_BUFFER_OVERFLOW, RT_SRC_POS,
2619 N_("Qed: Resizing the image '%s' is not supported because it would overflow the L1 and L2 table\n"),
2620 pImage->pszFilename);
2621 else
2622 {
2623 uint64_t cbSizeOld = pImage->cbSize;
2624
2625 pImage->cbSize = cbSize;
2626 rc = qedFlushImage(pImage);
2627 if (RT_FAILURE(rc))
2628 {
2629 pImage->cbSize = cbSizeOld; /* Restore */
2630
2631 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("Qed: Resizing the image '%s' failed\n"),
2632 pImage->pszFilename);
2633 }
2634 }
2635 }
2636 /* Same size doesn't change the image at all. */
2637
2638 LogFlowFunc(("returns %Rrc\n", rc));
2639 return rc;
2640}
2641
2642
2643const VBOXHDDBACKEND g_QedBackend =
2644{
2645 /* pszBackendName */
2646 "QED",
2647 /* cbSize */
2648 sizeof(VBOXHDDBACKEND),
2649 /* uBackendCaps */
2650 VD_CAP_FILE | VD_CAP_VFS | VD_CAP_CREATE_DYNAMIC | VD_CAP_DIFF | VD_CAP_ASYNC,
2651 /* paFileExtensions */
2652 s_aQedFileExtensions,
2653 /* paConfigInfo */
2654 NULL,
2655 /* pfnCheckIfValid */
2656 qedCheckIfValid,
2657 /* pfnOpen */
2658 qedOpen,
2659 /* pfnCreate */
2660 qedCreate,
2661 /* pfnRename */
2662 qedRename,
2663 /* pfnClose */
2664 qedClose,
2665 /* pfnRead */
2666 qedRead,
2667 /* pfnWrite */
2668 qedWrite,
2669 /* pfnFlush */
2670 qedFlush,
2671 /* pfnDiscard */
2672 NULL,
2673 /* pfnGetVersion */
2674 qedGetVersion,
2675 /* pfnGetSectorSize */
2676 qedGetSectorSize,
2677 /* pfnGetSize */
2678 qedGetSize,
2679 /* pfnGetFileSize */
2680 qedGetFileSize,
2681 /* pfnGetPCHSGeometry */
2682 qedGetPCHSGeometry,
2683 /* pfnSetPCHSGeometry */
2684 qedSetPCHSGeometry,
2685 /* pfnGetLCHSGeometry */
2686 qedGetLCHSGeometry,
2687 /* pfnSetLCHSGeometry */
2688 qedSetLCHSGeometry,
2689 /* pfnGetImageFlags */
2690 qedGetImageFlags,
2691 /* pfnGetOpenFlags */
2692 qedGetOpenFlags,
2693 /* pfnSetOpenFlags */
2694 qedSetOpenFlags,
2695 /* pfnGetComment */
2696 qedGetComment,
2697 /* pfnSetComment */
2698 qedSetComment,
2699 /* pfnGetUuid */
2700 qedGetUuid,
2701 /* pfnSetUuid */
2702 qedSetUuid,
2703 /* pfnGetModificationUuid */
2704 qedGetModificationUuid,
2705 /* pfnSetModificationUuid */
2706 qedSetModificationUuid,
2707 /* pfnGetParentUuid */
2708 qedGetParentUuid,
2709 /* pfnSetParentUuid */
2710 qedSetParentUuid,
2711 /* pfnGetParentModificationUuid */
2712 qedGetParentModificationUuid,
2713 /* pfnSetParentModificationUuid */
2714 qedSetParentModificationUuid,
2715 /* pfnDump */
2716 qedDump,
2717 /* pfnGetTimestamp */
2718 NULL,
2719 /* pfnGetParentTimestamp */
2720 NULL,
2721 /* pfnSetParentTimestamp */
2722 NULL,
2723 /* pfnGetParentFilename */
2724 qedGetParentFilename,
2725 /* pfnSetParentFilename */
2726 qedSetParentFilename,
2727 /* pfnComposeLocation */
2728 genericFileComposeLocation,
2729 /* pfnComposeName */
2730 genericFileComposeName,
2731 /* pfnCompact */
2732 NULL,
2733 /* pfnResize */
2734 qedResize,
2735 /* pfnRepair */
2736 NULL,
2737 /* pfnTraverseMetadata */
2738 NULL
2739};
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