VirtualBox

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

Last change on this file since 44794 was 44529, checked in by vboxsync, 12 years ago

header (C) fixes

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