VirtualBox

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

Last change on this file since 48950 was 48851, checked in by vboxsync, 11 years ago

Storage: Addressing 64-bit windows warnings.

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