VirtualBox

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

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

Storage/QED,QCOW: Always write the complete L1 and L2 table when allocating a new cluster instead of just the updated entry

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