VirtualBox

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

Last change on this file since 51287 was 50988, checked in by vboxsync, 11 years ago

Storage/VD: Cleanup VD plugin handling. One shared object can now support an arbitrary number of image backends instead of just one like before

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