VirtualBox

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

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

Storage/QED: Apply the fix as done to QCOW as they share the same issue (see r112215)

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