VirtualBox

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

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

(C) 2016

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