VirtualBox

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

Last change on this file since 55180 was 54430, checked in by vboxsync, 10 years ago

Storage/VD: make use of the image type (hdd/dvd/floppy) for sanity checking when creating disk images

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 88.1 KB
Line 
1/* $Id: QED.cpp 54430 2015-02-24 10:43:16Z vboxsync $ */
2/** @file
3 * QED - QED Disk image.
4 */
5
6/*
7 * Copyright (C) 2011-2015 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);
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 * Create the L2 cache before opening the image so we can call qedFreeImage()
1184 * even if opening the image file fails.
1185 */
1186 rc = qedL2TblCacheCreate(pImage);
1187 if (RT_FAILURE(rc))
1188 {
1189 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1190 N_("Qed: Creating the L2 table cache for image '%s' failed"),
1191 pImage->pszFilename);
1192
1193 goto out;
1194 }
1195
1196 /*
1197 * Open the image.
1198 */
1199 rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename,
1200 VDOpenFlagsToFileOpenFlags(uOpenFlags,
1201 false /* fCreate */),
1202 &pImage->pStorage);
1203 if (RT_FAILURE(rc))
1204 {
1205 /* Do NOT signal an appropriate error here, as the VD layer has the
1206 * choice of retrying the open if it failed. */
1207 goto out;
1208 }
1209
1210 uint64_t cbFile;
1211 QedHeader Header;
1212 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
1213 if (RT_FAILURE(rc))
1214 goto out;
1215 if (cbFile > sizeof(Header))
1216 {
1217 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, 0, &Header, sizeof(Header));
1218 if ( RT_SUCCESS(rc)
1219 && qedHdrConvertToHostEndianess(&Header))
1220 {
1221 if ( !(Header.u64FeatureFlags & ~QED_FEATURE_MASK)
1222 && !(Header.u64FeatureFlags & QED_FEATURE_BACKING_FILE_NO_PROBE))
1223 {
1224 if (Header.u64FeatureFlags & QED_FEATURE_NEED_CHECK)
1225 {
1226 /* Image needs checking. */
1227 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
1228 rc = qedCheckImage(pImage, &Header);
1229 else
1230 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1231 N_("Qed: Image '%s' needs checking but is opened readonly"),
1232 pImage->pszFilename);
1233 }
1234
1235 if ( RT_SUCCESS(rc)
1236 && (Header.u64FeatureFlags & QED_FEATURE_BACKING_FILE))
1237 {
1238 /* Load backing filename from image. */
1239 pImage->pszBackingFilename = (char *)RTMemAllocZ(Header.u32BackingFilenameSize + 1); /* +1 for \0 terminator. */
1240 if (pImage->pszBackingFilename)
1241 {
1242 pImage->cbBackingFilename = Header.u32BackingFilenameSize;
1243 pImage->offBackingFilename = Header.u32OffBackingFilename;
1244 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
1245 Header.u32OffBackingFilename, pImage->pszBackingFilename,
1246 Header.u32BackingFilenameSize);
1247 }
1248 else
1249 rc = VERR_NO_MEMORY;
1250 }
1251
1252 if (RT_SUCCESS(rc))
1253 {
1254 pImage->cbImage = cbFile;
1255 pImage->cbCluster = Header.u32ClusterSize;
1256 pImage->cbTable = Header.u32TableSize * pImage->cbCluster;
1257 pImage->cTableEntries = pImage->cbTable / sizeof(uint64_t);
1258 pImage->offL1Table = Header.u64OffL1Table;
1259 pImage->cbSize = Header.u64Size;
1260 qedTableMasksInit(pImage);
1261
1262 /* Allocate L1 table. */
1263 pImage->paL1Table = (uint64_t *)RTMemAllocZ(pImage->cbTable);
1264 if (pImage->paL1Table)
1265 {
1266 /* Read from the image. */
1267 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage,
1268 pImage->offL1Table, pImage->paL1Table,
1269 pImage->cbTable);
1270 if (RT_SUCCESS(rc))
1271 {
1272 qedTableConvertToHostEndianess(pImage->paL1Table, pImage->cTableEntries);
1273
1274 /* If the consistency check succeeded, clear the flag by flushing the image. */
1275 if (Header.u64FeatureFlags & QED_FEATURE_NEED_CHECK)
1276 rc = qedFlushImage(pImage);
1277 }
1278 else
1279 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1280 N_("Qed: Reading the L1 table for image '%s' failed"),
1281 pImage->pszFilename);
1282 }
1283 else
1284 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
1285 N_("Qed: Out of memory allocating L1 table for image '%s'"),
1286 pImage->pszFilename);
1287 }
1288 }
1289 else
1290 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1291 N_("Qed: The image '%s' makes use of unsupported features"),
1292 pImage->pszFilename);
1293 }
1294 else if (RT_SUCCESS(rc))
1295 rc = VERR_VD_GEN_INVALID_HEADER;
1296 }
1297 else
1298 rc = VERR_VD_GEN_INVALID_HEADER;
1299
1300out:
1301 if (RT_FAILURE(rc))
1302 qedFreeImage(pImage, false);
1303 return rc;
1304}
1305
1306/**
1307 * Internal: Create a qed image.
1308 */
1309static int qedCreateImage(PQEDIMAGE pImage, uint64_t cbSize,
1310 unsigned uImageFlags, const char *pszComment,
1311 PCVDGEOMETRY pPCHSGeometry,
1312 PCVDGEOMETRY pLCHSGeometry, unsigned uOpenFlags,
1313 PFNVDPROGRESS pfnProgress, void *pvUser,
1314 unsigned uPercentStart, unsigned uPercentSpan)
1315{
1316 int rc;
1317 int32_t fOpen;
1318
1319 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
1320 {
1321 rc = vdIfError(pImage->pIfError, VERR_VD_INVALID_TYPE, RT_SRC_POS, N_("Qed: cannot create fixed image '%s'"), pImage->pszFilename);
1322 goto out;
1323 }
1324
1325 pImage->uOpenFlags = uOpenFlags & ~VD_OPEN_FLAGS_READONLY;
1326 pImage->uImageFlags = uImageFlags;
1327 pImage->PCHSGeometry = *pPCHSGeometry;
1328 pImage->LCHSGeometry = *pLCHSGeometry;
1329
1330 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
1331 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
1332 AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
1333
1334 /* Create image file. */
1335 fOpen = VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags, true /* fCreate */);
1336 rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename, fOpen, &pImage->pStorage);
1337 if (RT_FAILURE(rc))
1338 {
1339 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("Qed: cannot create image '%s'"), pImage->pszFilename);
1340 goto out;
1341 }
1342
1343 /* Init image state. */
1344 pImage->cbSize = cbSize;
1345 pImage->cbCluster = QED_CLUSTER_SIZE_DEFAULT;
1346 pImage->cbTable = qedCluster2Byte(pImage, QED_TABLE_SIZE_DEFAULT);
1347 pImage->cTableEntries = pImage->cbTable / sizeof(uint64_t);
1348 pImage->offL1Table = qedCluster2Byte(pImage, 1); /* Cluster 0 is the header. */
1349 pImage->cbImage = (1 * pImage->cbCluster) + pImage->cbTable; /* Header + L1 table size. */
1350 pImage->cbBackingFilename = 0;
1351 pImage->offBackingFilename = 0;
1352 qedTableMasksInit(pImage);
1353
1354 /* Init L1 table. */
1355 pImage->paL1Table = (uint64_t *)RTMemAllocZ(pImage->cbTable);
1356 if (!pImage->paL1Table)
1357 {
1358 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS, N_("Qed: cannot allocate memory for L1 table of image '%s'"),
1359 pImage->pszFilename);
1360 goto out;
1361 }
1362
1363 rc = qedL2TblCacheCreate(pImage);
1364 if (RT_FAILURE(rc))
1365 {
1366 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("Qed: Failed to create L2 cache for image '%s'"),
1367 pImage->pszFilename);
1368 goto out;
1369 }
1370
1371 if (RT_SUCCESS(rc) && pfnProgress)
1372 pfnProgress(pvUser, uPercentStart + uPercentSpan * 98 / 100);
1373
1374 rc = qedFlushImage(pImage);
1375
1376out:
1377 if (RT_SUCCESS(rc) && pfnProgress)
1378 pfnProgress(pvUser, uPercentStart + uPercentSpan);
1379
1380 if (RT_FAILURE(rc))
1381 qedFreeImage(pImage, rc != VERR_ALREADY_EXISTS);
1382 return rc;
1383}
1384
1385/**
1386 * Rollback anything done during async cluster allocation.
1387 *
1388 * @returns VBox status code.
1389 * @param pImage The image instance data.
1390 * @param pIoCtx The I/O context.
1391 * @param pClusterAlloc The cluster allocation to rollback.
1392 */
1393static int qedAsyncClusterAllocRollback(PQEDIMAGE pImage, PVDIOCTX pIoCtx, PQEDCLUSTERASYNCALLOC pClusterAlloc)
1394{
1395 int rc = VINF_SUCCESS;
1396
1397 switch (pClusterAlloc->enmAllocState)
1398 {
1399 case QEDCLUSTERASYNCALLOCSTATE_L2_ALLOC:
1400 case QEDCLUSTERASYNCALLOCSTATE_L2_LINK:
1401 {
1402 /* Assumption right now is that the L1 table is not modified if the link fails. */
1403 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, pClusterAlloc->cbImageOld);
1404 qedL2TblCacheEntryRelease(pClusterAlloc->pL2Entry); /* Release L2 cache entry. */
1405 qedL2TblCacheEntryFree(pImage, pClusterAlloc->pL2Entry); /* Free it, it is not in the cache yet. */
1406 break;
1407 }
1408 case QEDCLUSTERASYNCALLOCSTATE_USER_ALLOC:
1409 case QEDCLUSTERASYNCALLOCSTATE_USER_LINK:
1410 {
1411 /* Assumption right now is that the L2 table is not modified if the link fails. */
1412 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, pClusterAlloc->cbImageOld);
1413 qedL2TblCacheEntryRelease(pClusterAlloc->pL2Entry); /* Release L2 cache entry. */
1414 break;
1415 }
1416 default:
1417 AssertMsgFailed(("Invalid cluster allocation state %d\n", pClusterAlloc->enmAllocState));
1418 rc = VERR_INVALID_STATE;
1419 }
1420
1421 RTMemFree(pClusterAlloc);
1422 return rc;
1423}
1424
1425/**
1426 * Updates the state of the async cluster allocation.
1427 *
1428 * @returns VBox status code.
1429 * @param pBackendData The opaque backend data.
1430 * @param pIoCtx I/O context associated with this request.
1431 * @param pvUser Opaque user data passed during a read/write request.
1432 * @param rcReq Status code for the completed request.
1433 */
1434static DECLCALLBACK(int) qedAsyncClusterAllocUpdate(void *pBackendData, PVDIOCTX pIoCtx, void *pvUser, int rcReq)
1435{
1436 int rc = VINF_SUCCESS;
1437 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
1438 PQEDCLUSTERASYNCALLOC pClusterAlloc = (PQEDCLUSTERASYNCALLOC)pvUser;
1439
1440 if (RT_FAILURE(rcReq))
1441 return qedAsyncClusterAllocRollback(pImage, pIoCtx, pClusterAlloc);
1442
1443 AssertPtr(pClusterAlloc->pL2Entry);
1444
1445 switch (pClusterAlloc->enmAllocState)
1446 {
1447 case QEDCLUSTERASYNCALLOCSTATE_L2_ALLOC:
1448 {
1449 uint64_t offUpdateLe = RT_H2LE_U64(pClusterAlloc->pL2Entry->offL2Tbl);
1450
1451 /* Update the link in the on disk L1 table now. */
1452 pClusterAlloc->enmAllocState = QEDCLUSTERASYNCALLOCSTATE_L2_LINK;
1453 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
1454 pImage->offL1Table + pClusterAlloc->idxL1*sizeof(uint64_t),
1455 &offUpdateLe, sizeof(uint64_t), pIoCtx,
1456 qedAsyncClusterAllocUpdate, pClusterAlloc);
1457 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1458 break;
1459 else if (RT_FAILURE(rc))
1460 {
1461 /* Rollback. */
1462 qedAsyncClusterAllocRollback(pImage, pIoCtx, pClusterAlloc);
1463 break;
1464 }
1465 /* Success, fall through. */
1466 }
1467 case QEDCLUSTERASYNCALLOCSTATE_L2_LINK:
1468 {
1469 /* L2 link updated in L1 , save L2 entry in cache and allocate new user data cluster. */
1470 uint64_t offData = qedClusterAllocate(pImage, 1);
1471
1472 /* Update the link in the in memory L1 table now. */
1473 pImage->paL1Table[pClusterAlloc->idxL1] = pClusterAlloc->pL2Entry->offL2Tbl;
1474 qedL2TblCacheEntryInsert(pImage, pClusterAlloc->pL2Entry);
1475
1476 pClusterAlloc->enmAllocState = QEDCLUSTERASYNCALLOCSTATE_USER_ALLOC;
1477 pClusterAlloc->cbImageOld = offData;
1478 pClusterAlloc->offClusterNew = offData;
1479
1480 /* Write data. */
1481 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
1482 offData, pIoCtx, pClusterAlloc->cbToWrite,
1483 qedAsyncClusterAllocUpdate, pClusterAlloc);
1484 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1485 break;
1486 else if (RT_FAILURE(rc))
1487 {
1488 qedAsyncClusterAllocRollback(pImage, pIoCtx, pClusterAlloc);
1489 RTMemFree(pClusterAlloc);
1490 break;
1491 }
1492 }
1493 case QEDCLUSTERASYNCALLOCSTATE_USER_ALLOC:
1494 {
1495 uint64_t offUpdateLe = RT_H2LE_U64(pClusterAlloc->offClusterNew);
1496
1497 pClusterAlloc->enmAllocState = QEDCLUSTERASYNCALLOCSTATE_USER_LINK;
1498
1499 /* Link L2 table and update it. */
1500 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
1501 pImage->paL1Table[pClusterAlloc->idxL1] + pClusterAlloc->idxL2*sizeof(uint64_t),
1502 &offUpdateLe, sizeof(uint64_t), pIoCtx,
1503 qedAsyncClusterAllocUpdate, pClusterAlloc);
1504 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1505 break;
1506 else if (RT_FAILURE(rc))
1507 {
1508 qedAsyncClusterAllocRollback(pImage, pIoCtx, pClusterAlloc);
1509 RTMemFree(pClusterAlloc);
1510 break;
1511 }
1512 }
1513 case QEDCLUSTERASYNCALLOCSTATE_USER_LINK:
1514 {
1515 /* Everything done without errors, signal completion. */
1516 pClusterAlloc->pL2Entry->paL2Tbl[pClusterAlloc->idxL2] = pClusterAlloc->offClusterNew;
1517 qedL2TblCacheEntryRelease(pClusterAlloc->pL2Entry);
1518 RTMemFree(pClusterAlloc);
1519 rc = VINF_SUCCESS;
1520 break;
1521 }
1522 default:
1523 AssertMsgFailed(("Invalid async cluster allocation state %d\n",
1524 pClusterAlloc->enmAllocState));
1525 }
1526
1527 return rc;
1528}
1529
1530/** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
1531static int qedCheckIfValid(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
1532 PVDINTERFACE pVDIfsImage, VDTYPE *penmType)
1533{
1534 LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p\n", pszFilename, pVDIfsDisk, pVDIfsImage));
1535 PVDIOSTORAGE pStorage = NULL;
1536 uint64_t cbFile;
1537 int rc = VINF_SUCCESS;
1538
1539 /* Get I/O interface. */
1540 PVDINTERFACEIOINT pIfIo = VDIfIoIntGet(pVDIfsImage);
1541 AssertPtrReturn(pIfIo, VERR_INVALID_PARAMETER);
1542
1543 if ( !VALID_PTR(pszFilename)
1544 || !*pszFilename)
1545 {
1546 rc = VERR_INVALID_PARAMETER;
1547 goto out;
1548 }
1549
1550 /*
1551 * Open the file and read the footer.
1552 */
1553 rc = vdIfIoIntFileOpen(pIfIo, pszFilename,
1554 VDOpenFlagsToFileOpenFlags(VD_OPEN_FLAGS_READONLY,
1555 false /* fCreate */),
1556 &pStorage);
1557 if (RT_SUCCESS(rc))
1558 rc = vdIfIoIntFileGetSize(pIfIo, pStorage, &cbFile);
1559
1560 if ( RT_SUCCESS(rc)
1561 && cbFile > sizeof(QedHeader))
1562 {
1563 QedHeader Header;
1564
1565 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, 0, &Header, sizeof(Header));
1566 if ( RT_SUCCESS(rc)
1567 && qedHdrConvertToHostEndianess(&Header))
1568 {
1569 *penmType = VDTYPE_HDD;
1570 rc = VINF_SUCCESS;
1571 }
1572 else
1573 rc = VERR_VD_GEN_INVALID_HEADER;
1574 }
1575 else
1576 rc = VERR_VD_GEN_INVALID_HEADER;
1577
1578 if (pStorage)
1579 vdIfIoIntFileClose(pIfIo, pStorage);
1580
1581out:
1582 LogFlowFunc(("returns %Rrc\n", rc));
1583 return rc;
1584}
1585
1586/** @copydoc VBOXHDDBACKEND::pfnOpen */
1587static int qedOpen(const char *pszFilename, unsigned uOpenFlags,
1588 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1589 VDTYPE enmType, void **ppBackendData)
1590{
1591 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p enmType=%u ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, enmType, ppBackendData));
1592 int rc;
1593 PQEDIMAGE pImage;
1594
1595 NOREF(enmType); /**< @todo r=klaus make use of the type info. */
1596
1597 /* Check open flags. All valid flags are supported. */
1598 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
1599 {
1600 rc = VERR_INVALID_PARAMETER;
1601 goto out;
1602 }
1603
1604 /* Check remaining arguments. */
1605 if ( !VALID_PTR(pszFilename)
1606 || !*pszFilename)
1607 {
1608 rc = VERR_INVALID_PARAMETER;
1609 goto out;
1610 }
1611
1612
1613 pImage = (PQEDIMAGE)RTMemAllocZ(sizeof(QEDIMAGE));
1614 if (!pImage)
1615 {
1616 rc = VERR_NO_MEMORY;
1617 goto out;
1618 }
1619 pImage->pszFilename = pszFilename;
1620 pImage->pStorage = NULL;
1621 pImage->pVDIfsDisk = pVDIfsDisk;
1622 pImage->pVDIfsImage = pVDIfsImage;
1623
1624 rc = qedOpenImage(pImage, uOpenFlags);
1625 if (RT_SUCCESS(rc))
1626 *ppBackendData = pImage;
1627 else
1628 RTMemFree(pImage);
1629
1630out:
1631 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1632 return rc;
1633}
1634
1635/** @copydoc VBOXHDDBACKEND::pfnCreate */
1636static int qedCreate(const char *pszFilename, uint64_t cbSize,
1637 unsigned uImageFlags, const char *pszComment,
1638 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
1639 PCRTUUID pUuid, unsigned uOpenFlags,
1640 unsigned uPercentStart, unsigned uPercentSpan,
1641 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1642 PVDINTERFACE pVDIfsOperation, VDTYPE enmType,
1643 void **ppBackendData)
1644{
1645 LogFlowFunc(("pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" pPCHSGeometry=%#p pLCHSGeometry=%#p Uuid=%RTuuid uOpenFlags=%#x uPercentStart=%u uPercentSpan=%u pVDIfsDisk=%#p pVDIfsImage=%#p pVDIfsOperation=%#p enmType, ppBackendData=%#p",
1646 pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData));
1647 int rc;
1648 PQEDIMAGE pImage;
1649
1650 PFNVDPROGRESS pfnProgress = NULL;
1651 void *pvUser = NULL;
1652 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
1653 if (pIfProgress)
1654 {
1655 pfnProgress = pIfProgress->pfnProgress;
1656 pvUser = pIfProgress->Core.pvUser;
1657 }
1658
1659 /* Check the VD container type. */
1660 if (enmType != VDTYPE_HDD)
1661 {
1662 rc = VERR_VD_INVALID_TYPE;
1663 goto out;
1664 }
1665
1666 /* Check open flags. All valid flags are supported. */
1667 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
1668 {
1669 rc = VERR_INVALID_PARAMETER;
1670 goto out;
1671 }
1672
1673 /* Check remaining arguments. */
1674 if ( !VALID_PTR(pszFilename)
1675 || !*pszFilename
1676 || !VALID_PTR(pPCHSGeometry)
1677 || !VALID_PTR(pLCHSGeometry))
1678 {
1679 rc = VERR_INVALID_PARAMETER;
1680 goto out;
1681 }
1682
1683 pImage = (PQEDIMAGE)RTMemAllocZ(sizeof(QEDIMAGE));
1684 if (!pImage)
1685 {
1686 rc = VERR_NO_MEMORY;
1687 goto out;
1688 }
1689 pImage->pszFilename = pszFilename;
1690 pImage->pStorage = NULL;
1691 pImage->pVDIfsDisk = pVDIfsDisk;
1692 pImage->pVDIfsImage = pVDIfsImage;
1693
1694 rc = qedCreateImage(pImage, cbSize, uImageFlags, pszComment,
1695 pPCHSGeometry, pLCHSGeometry, uOpenFlags,
1696 pfnProgress, pvUser, uPercentStart, uPercentSpan);
1697 if (RT_SUCCESS(rc))
1698 {
1699 /* So far the image is opened in read/write mode. Make sure the
1700 * image is opened in read-only mode if the caller requested that. */
1701 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
1702 {
1703 qedFreeImage(pImage, false);
1704 rc = qedOpenImage(pImage, uOpenFlags);
1705 if (RT_FAILURE(rc))
1706 {
1707 RTMemFree(pImage);
1708 goto out;
1709 }
1710 }
1711 *ppBackendData = pImage;
1712 }
1713 else
1714 RTMemFree(pImage);
1715
1716out:
1717 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1718 return rc;
1719}
1720
1721/** @copydoc VBOXHDDBACKEND::pfnRename */
1722static int qedRename(void *pBackendData, const char *pszFilename)
1723{
1724 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
1725 int rc = VINF_SUCCESS;
1726 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
1727
1728 /* Check arguments. */
1729 if ( !pImage
1730 || !pszFilename
1731 || !*pszFilename)
1732 {
1733 rc = VERR_INVALID_PARAMETER;
1734 goto out;
1735 }
1736
1737 /* Close the image. */
1738 rc = qedFreeImage(pImage, false);
1739 if (RT_FAILURE(rc))
1740 goto out;
1741
1742 /* Rename the file. */
1743 rc = vdIfIoIntFileMove(pImage->pIfIo, pImage->pszFilename, pszFilename, 0);
1744 if (RT_FAILURE(rc))
1745 {
1746 /* The move failed, try to reopen the original image. */
1747 int rc2 = qedOpenImage(pImage, pImage->uOpenFlags);
1748 if (RT_FAILURE(rc2))
1749 rc = rc2;
1750
1751 goto out;
1752 }
1753
1754 /* Update pImage with the new information. */
1755 pImage->pszFilename = pszFilename;
1756
1757 /* Open the old image with new name. */
1758 rc = qedOpenImage(pImage, pImage->uOpenFlags);
1759 if (RT_FAILURE(rc))
1760 goto out;
1761
1762out:
1763 LogFlowFunc(("returns %Rrc\n", rc));
1764 return rc;
1765}
1766
1767/** @copydoc VBOXHDDBACKEND::pfnClose */
1768static int qedClose(void *pBackendData, bool fDelete)
1769{
1770 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
1771 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
1772 int rc;
1773
1774 rc = qedFreeImage(pImage, fDelete);
1775 RTMemFree(pImage);
1776
1777 LogFlowFunc(("returns %Rrc\n", rc));
1778 return rc;
1779}
1780
1781static int qedRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
1782 PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
1783{
1784 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
1785 pBackendData, uOffset, pIoCtx, cbToRead, pcbActuallyRead));
1786 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
1787 uint32_t offCluster = 0;
1788 uint32_t idxL1 = 0;
1789 uint32_t idxL2 = 0;
1790 uint64_t offFile = 0;
1791 int rc;
1792
1793 AssertPtr(pImage);
1794 Assert(uOffset % 512 == 0);
1795 Assert(cbToRead % 512 == 0);
1796
1797 if (!VALID_PTR(pIoCtx) || !cbToRead)
1798 {
1799 rc = VERR_INVALID_PARAMETER;
1800 goto out;
1801 }
1802
1803 if ( uOffset + cbToRead > pImage->cbSize
1804 || cbToRead == 0)
1805 {
1806 rc = VERR_INVALID_PARAMETER;
1807 goto out;
1808 }
1809
1810 qedConvertLogicalOffset(pImage, uOffset, &idxL1, &idxL2, &offCluster);
1811
1812 /* Clip read size to remain in the cluster. */
1813 cbToRead = RT_MIN(cbToRead, pImage->cbCluster - offCluster);
1814
1815 /* Get offset in image. */
1816 rc = qedConvertToImageOffset(pImage, pIoCtx, idxL1, idxL2, offCluster, &offFile);
1817 if (RT_SUCCESS(rc))
1818 rc = vdIfIoIntFileReadUser(pImage->pIfIo, pImage->pStorage, offFile,
1819 pIoCtx, cbToRead);
1820
1821 if ( ( RT_SUCCESS(rc)
1822 || rc == VERR_VD_BLOCK_FREE
1823 || rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1824 && pcbActuallyRead)
1825 *pcbActuallyRead = cbToRead;
1826
1827out:
1828 LogFlowFunc(("returns %Rrc\n", rc));
1829 return rc;
1830}
1831
1832static int qedWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
1833 PVDIOCTX pIoCtx, size_t *pcbWriteProcess, size_t *pcbPreRead,
1834 size_t *pcbPostRead, unsigned fWrite)
1835{
1836 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
1837 pBackendData, uOffset, pIoCtx, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
1838 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
1839 uint32_t offCluster = 0;
1840 uint32_t idxL1 = 0;
1841 uint32_t idxL2 = 0;
1842 uint64_t offImage = 0;
1843 int rc = VINF_SUCCESS;
1844
1845 AssertPtr(pImage);
1846 Assert(!(uOffset % 512));
1847 Assert(!(cbToWrite % 512));
1848
1849 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1850 {
1851 rc = VERR_VD_IMAGE_READ_ONLY;
1852 goto out;
1853 }
1854
1855 if (!VALID_PTR(pIoCtx) || !cbToWrite)
1856 {
1857 rc = VERR_INVALID_PARAMETER;
1858 goto out;
1859 }
1860
1861 if ( uOffset + cbToWrite > pImage->cbSize
1862 || cbToWrite == 0)
1863 {
1864 rc = VERR_INVALID_PARAMETER;
1865 goto out;
1866 }
1867
1868 /* Convert offset to L1, L2 index and cluster offset. */
1869 qedConvertLogicalOffset(pImage, uOffset, &idxL1, &idxL2, &offCluster);
1870
1871 /* Clip write size to remain in the cluster. */
1872 cbToWrite = RT_MIN(cbToWrite, pImage->cbCluster - offCluster);
1873 Assert(!(cbToWrite % 512));
1874
1875 /* Get offset in image. */
1876 rc = qedConvertToImageOffset(pImage, pIoCtx, idxL1, idxL2, offCluster, &offImage);
1877 if (RT_SUCCESS(rc))
1878 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
1879 offImage, pIoCtx, cbToWrite, NULL, NULL);
1880 else if (rc == VERR_VD_BLOCK_FREE)
1881 {
1882 if ( cbToWrite == pImage->cbCluster
1883 && !(fWrite & VD_WRITE_NO_ALLOC))
1884 {
1885 PQEDL2CACHEENTRY pL2Entry = NULL;
1886
1887 /* Full cluster write to previously unallocated cluster.
1888 * Allocate cluster and write data. */
1889 Assert(!offCluster);
1890
1891 do
1892 {
1893 uint64_t idxUpdateLe = 0;
1894
1895 /* Check if we have to allocate a new cluster for L2 tables. */
1896 if (!pImage->paL1Table[idxL1])
1897 {
1898 uint64_t offL2Tbl;
1899 PQEDCLUSTERASYNCALLOC pL2ClusterAlloc = NULL;
1900
1901 /* Allocate new async cluster allocation state. */
1902 pL2ClusterAlloc = (PQEDCLUSTERASYNCALLOC)RTMemAllocZ(sizeof(QEDCLUSTERASYNCALLOC));
1903 if (RT_UNLIKELY(!pL2ClusterAlloc))
1904 {
1905 rc = VERR_NO_MEMORY;
1906 break;
1907 }
1908
1909 pL2Entry = qedL2TblCacheEntryAlloc(pImage);
1910 if (!pL2Entry)
1911 {
1912 rc = VERR_NO_MEMORY;
1913 RTMemFree(pL2ClusterAlloc);
1914 break;
1915 }
1916
1917 offL2Tbl = qedClusterAllocate(pImage, qedByte2Cluster(pImage, pImage->cbTable));
1918 pL2Entry->offL2Tbl = offL2Tbl;
1919 memset(pL2Entry->paL2Tbl, 0, pImage->cbTable);
1920
1921 pL2ClusterAlloc->enmAllocState = QEDCLUSTERASYNCALLOCSTATE_L2_ALLOC;
1922 pL2ClusterAlloc->cbImageOld = offL2Tbl;
1923 pL2ClusterAlloc->offClusterNew = offL2Tbl;
1924 pL2ClusterAlloc->idxL1 = idxL1;
1925 pL2ClusterAlloc->idxL2 = idxL2;
1926 pL2ClusterAlloc->cbToWrite = cbToWrite;
1927 pL2ClusterAlloc->pL2Entry = pL2Entry;
1928
1929 /*
1930 * Write the L2 table first and link to the L1 table afterwards.
1931 * If something unexpected happens the worst case which can happen
1932 * is a leak of some clusters.
1933 */
1934 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pImage->pStorage,
1935 offL2Tbl, pL2Entry->paL2Tbl, pImage->cbTable, pIoCtx,
1936 qedAsyncClusterAllocUpdate, pL2ClusterAlloc);
1937 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1938 break;
1939 else if (RT_FAILURE(rc))
1940 {
1941 RTMemFree(pL2ClusterAlloc);
1942 qedL2TblCacheEntryFree(pImage, pL2Entry);
1943 break;
1944 }
1945
1946 rc = qedAsyncClusterAllocUpdate(pImage, pIoCtx, pL2ClusterAlloc, rc);
1947 }
1948 else
1949 {
1950 rc = qedL2TblCacheFetchAsync(pImage, pIoCtx, pImage->paL1Table[idxL1],
1951 &pL2Entry);
1952
1953 if (RT_SUCCESS(rc))
1954 {
1955 PQEDCLUSTERASYNCALLOC pDataClusterAlloc = NULL;
1956
1957 /* Allocate new async cluster allocation state. */
1958 pDataClusterAlloc = (PQEDCLUSTERASYNCALLOC)RTMemAllocZ(sizeof(QEDCLUSTERASYNCALLOC));
1959 if (RT_UNLIKELY(!pDataClusterAlloc))
1960 {
1961 rc = VERR_NO_MEMORY;
1962 break;
1963 }
1964
1965 /* Allocate new cluster for the data. */
1966 uint64_t offData = qedClusterAllocate(pImage, 1);
1967
1968 pDataClusterAlloc->enmAllocState = QEDCLUSTERASYNCALLOCSTATE_USER_ALLOC;
1969 pDataClusterAlloc->cbImageOld = offData;
1970 pDataClusterAlloc->offClusterNew = offData;
1971 pDataClusterAlloc->idxL1 = idxL1;
1972 pDataClusterAlloc->idxL2 = idxL2;
1973 pDataClusterAlloc->cbToWrite = cbToWrite;
1974 pDataClusterAlloc->pL2Entry = pL2Entry;
1975
1976 /* Write data. */
1977 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage,
1978 offData, pIoCtx, cbToWrite,
1979 qedAsyncClusterAllocUpdate, pDataClusterAlloc);
1980 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1981 break;
1982 else if (RT_FAILURE(rc))
1983 {
1984 RTMemFree(pDataClusterAlloc);
1985 break;
1986 }
1987
1988 rc = qedAsyncClusterAllocUpdate(pImage, pIoCtx, pDataClusterAlloc, rc);
1989 }
1990 }
1991
1992 } while (0);
1993
1994 *pcbPreRead = 0;
1995 *pcbPostRead = 0;
1996 }
1997 else
1998 {
1999 /* Trying to do a partial write to an unallocated cluster. Don't do
2000 * anything except letting the upper layer know what to do. */
2001 *pcbPreRead = offCluster;
2002 *pcbPostRead = pImage->cbCluster - cbToWrite - *pcbPreRead;
2003 }
2004 }
2005
2006 if (pcbWriteProcess)
2007 *pcbWriteProcess = cbToWrite;
2008
2009
2010out:
2011 LogFlowFunc(("returns %Rrc\n", rc));
2012 return rc;
2013}
2014
2015static int qedFlush(void *pBackendData, PVDIOCTX pIoCtx)
2016{
2017 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2018 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2019 int rc = VINF_SUCCESS;
2020
2021 Assert(pImage);
2022
2023 if (VALID_PTR(pIoCtx))
2024 rc = qedFlushImageAsync(pImage, pIoCtx);
2025 else
2026 rc = VERR_INVALID_PARAMETER;
2027
2028 LogFlowFunc(("returns %Rrc\n", rc));
2029 return rc;
2030}
2031
2032/** @copydoc VBOXHDDBACKEND::pfnGetVersion */
2033static unsigned qedGetVersion(void *pBackendData)
2034{
2035 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2036 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2037
2038 AssertPtr(pImage);
2039
2040 if (pImage)
2041 return 1;
2042 else
2043 return 0;
2044}
2045
2046/** @copydoc VBOXHDDBACKEND::pfnGetSectorSize */
2047static uint32_t qedGetSectorSize(void *pBackendData)
2048{
2049 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2050 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2051 uint32_t cb = 0;
2052
2053 AssertPtr(pImage);
2054
2055 if (pImage && pImage->pStorage)
2056 cb = 512;
2057
2058 LogFlowFunc(("returns %u\n", cb));
2059 return cb;
2060}
2061
2062/** @copydoc VBOXHDDBACKEND::pfnGetSize */
2063static uint64_t qedGetSize(void *pBackendData)
2064{
2065 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2066 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2067 uint64_t cb = 0;
2068
2069 AssertPtr(pImage);
2070
2071 if (pImage && pImage->pStorage)
2072 cb = pImage->cbSize;
2073
2074 LogFlowFunc(("returns %llu\n", cb));
2075 return cb;
2076}
2077
2078/** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
2079static uint64_t qedGetFileSize(void *pBackendData)
2080{
2081 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2082 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2083 uint64_t cb = 0;
2084
2085 AssertPtr(pImage);
2086
2087 if (pImage)
2088 {
2089 uint64_t cbFile;
2090 if (pImage->pStorage)
2091 {
2092 int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
2093 if (RT_SUCCESS(rc))
2094 cb += cbFile;
2095 }
2096 }
2097
2098 LogFlowFunc(("returns %lld\n", cb));
2099 return cb;
2100}
2101
2102/** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
2103static int qedGetPCHSGeometry(void *pBackendData,
2104 PVDGEOMETRY pPCHSGeometry)
2105{
2106 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
2107 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2108 int rc;
2109
2110 AssertPtr(pImage);
2111
2112 if (pImage)
2113 {
2114 if (pImage->PCHSGeometry.cCylinders)
2115 {
2116 *pPCHSGeometry = pImage->PCHSGeometry;
2117 rc = VINF_SUCCESS;
2118 }
2119 else
2120 rc = VERR_VD_GEOMETRY_NOT_SET;
2121 }
2122 else
2123 rc = VERR_VD_NOT_OPENED;
2124
2125 LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2126 return rc;
2127}
2128
2129/** @copydoc VBOXHDDBACKEND::pfnSetPCHSGeometry */
2130static int qedSetPCHSGeometry(void *pBackendData,
2131 PCVDGEOMETRY pPCHSGeometry)
2132{
2133 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2134 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2135 int rc;
2136
2137 AssertPtr(pImage);
2138
2139 if (pImage)
2140 {
2141 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2142 {
2143 rc = VERR_VD_IMAGE_READ_ONLY;
2144 goto out;
2145 }
2146
2147 pImage->PCHSGeometry = *pPCHSGeometry;
2148 rc = VINF_SUCCESS;
2149 }
2150 else
2151 rc = VERR_VD_NOT_OPENED;
2152
2153out:
2154 LogFlowFunc(("returns %Rrc\n", rc));
2155 return rc;
2156}
2157
2158/** @copydoc VBOXHDDBACKEND::pfnGetLCHSGeometry */
2159static int qedGetLCHSGeometry(void *pBackendData,
2160 PVDGEOMETRY pLCHSGeometry)
2161{
2162 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
2163 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2164 int rc;
2165
2166 AssertPtr(pImage);
2167
2168 if (pImage)
2169 {
2170 if (pImage->LCHSGeometry.cCylinders)
2171 {
2172 *pLCHSGeometry = pImage->LCHSGeometry;
2173 rc = VINF_SUCCESS;
2174 }
2175 else
2176 rc = VERR_VD_GEOMETRY_NOT_SET;
2177 }
2178 else
2179 rc = VERR_VD_NOT_OPENED;
2180
2181 LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2182 return rc;
2183}
2184
2185/** @copydoc VBOXHDDBACKEND::pfnSetLCHSGeometry */
2186static int qedSetLCHSGeometry(void *pBackendData,
2187 PCVDGEOMETRY pLCHSGeometry)
2188{
2189 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2190 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2191 int rc;
2192
2193 AssertPtr(pImage);
2194
2195 if (pImage)
2196 {
2197 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2198 {
2199 rc = VERR_VD_IMAGE_READ_ONLY;
2200 goto out;
2201 }
2202
2203 pImage->LCHSGeometry = *pLCHSGeometry;
2204 rc = VINF_SUCCESS;
2205 }
2206 else
2207 rc = VERR_VD_NOT_OPENED;
2208
2209out:
2210 LogFlowFunc(("returns %Rrc\n", rc));
2211 return rc;
2212}
2213
2214/** @copydoc VBOXHDDBACKEND::pfnGetImageFlags */
2215static unsigned qedGetImageFlags(void *pBackendData)
2216{
2217 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2218 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2219 unsigned uImageFlags;
2220
2221 AssertPtr(pImage);
2222
2223 if (pImage)
2224 uImageFlags = pImage->uImageFlags;
2225 else
2226 uImageFlags = 0;
2227
2228 LogFlowFunc(("returns %#x\n", uImageFlags));
2229 return uImageFlags;
2230}
2231
2232/** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
2233static unsigned qedGetOpenFlags(void *pBackendData)
2234{
2235 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2236 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2237 unsigned uOpenFlags;
2238
2239 AssertPtr(pImage);
2240
2241 if (pImage)
2242 uOpenFlags = pImage->uOpenFlags;
2243 else
2244 uOpenFlags = 0;
2245
2246 LogFlowFunc(("returns %#x\n", uOpenFlags));
2247 return uOpenFlags;
2248}
2249
2250/** @copydoc VBOXHDDBACKEND::pfnSetOpenFlags */
2251static int qedSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
2252{
2253 LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
2254 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2255 int rc;
2256
2257 /* Image must be opened and the new flags must be valid. */
2258 if (!pImage || (uOpenFlags & ~( VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO
2259 | VD_OPEN_FLAGS_ASYNC_IO | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)))
2260 {
2261 rc = VERR_INVALID_PARAMETER;
2262 goto out;
2263 }
2264
2265 /* Implement this operation via reopening the image. */
2266 rc = qedFreeImage(pImage, false);
2267 if (RT_FAILURE(rc))
2268 goto out;
2269 rc = qedOpenImage(pImage, uOpenFlags);
2270
2271out:
2272 LogFlowFunc(("returns %Rrc\n", rc));
2273 return rc;
2274}
2275
2276/** @copydoc VBOXHDDBACKEND::pfnGetComment */
2277static int qedGetComment(void *pBackendData, char *pszComment,
2278 size_t cbComment)
2279{
2280 LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
2281 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2282 int rc;
2283
2284 AssertPtr(pImage);
2285
2286 if (pImage)
2287 rc = VERR_NOT_SUPPORTED;
2288 else
2289 rc = VERR_VD_NOT_OPENED;
2290
2291 LogFlowFunc(("returns %Rrc comment='%s'\n", rc, pszComment));
2292 return rc;
2293}
2294
2295/** @copydoc VBOXHDDBACKEND::pfnSetComment */
2296static int qedSetComment(void *pBackendData, const char *pszComment)
2297{
2298 LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
2299 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2300 int rc;
2301
2302 AssertPtr(pImage);
2303
2304 if (pImage)
2305 {
2306 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2307 rc = VERR_VD_IMAGE_READ_ONLY;
2308 else
2309 rc = VERR_NOT_SUPPORTED;
2310 }
2311 else
2312 rc = VERR_VD_NOT_OPENED;
2313
2314 LogFlowFunc(("returns %Rrc\n", rc));
2315 return rc;
2316}
2317
2318/** @copydoc VBOXHDDBACKEND::pfnGetUuid */
2319static int qedGetUuid(void *pBackendData, PRTUUID pUuid)
2320{
2321 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2322 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2323 int rc;
2324
2325 AssertPtr(pImage);
2326
2327 if (pImage)
2328 rc = VERR_NOT_SUPPORTED;
2329 else
2330 rc = VERR_VD_NOT_OPENED;
2331
2332 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2333 return rc;
2334}
2335
2336/** @copydoc VBOXHDDBACKEND::pfnSetUuid */
2337static int qedSetUuid(void *pBackendData, PCRTUUID pUuid)
2338{
2339 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2340 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2341 int rc;
2342
2343 LogFlowFunc(("%RTuuid\n", pUuid));
2344 AssertPtr(pImage);
2345
2346 if (pImage)
2347 {
2348 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2349 rc = VERR_NOT_SUPPORTED;
2350 else
2351 rc = VERR_VD_IMAGE_READ_ONLY;
2352 }
2353 else
2354 rc = VERR_VD_NOT_OPENED;
2355
2356 LogFlowFunc(("returns %Rrc\n", rc));
2357 return rc;
2358}
2359
2360/** @copydoc VBOXHDDBACKEND::pfnGetModificationUuid */
2361static int qedGetModificationUuid(void *pBackendData, PRTUUID pUuid)
2362{
2363 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2364 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2365 int rc;
2366
2367 AssertPtr(pImage);
2368
2369 if (pImage)
2370 rc = VERR_NOT_SUPPORTED;
2371 else
2372 rc = VERR_VD_NOT_OPENED;
2373
2374 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2375 return rc;
2376}
2377
2378/** @copydoc VBOXHDDBACKEND::pfnSetModificationUuid */
2379static int qedSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
2380{
2381 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2382 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2383 int rc;
2384
2385 AssertPtr(pImage);
2386
2387 if (pImage)
2388 {
2389 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2390 rc = VERR_NOT_SUPPORTED;
2391 else
2392 rc = VERR_VD_IMAGE_READ_ONLY;
2393 }
2394 else
2395 rc = VERR_VD_NOT_OPENED;
2396
2397 LogFlowFunc(("returns %Rrc\n", rc));
2398 return rc;
2399}
2400
2401/** @copydoc VBOXHDDBACKEND::pfnGetParentUuid */
2402static int qedGetParentUuid(void *pBackendData, PRTUUID pUuid)
2403{
2404 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2405 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2406 int rc;
2407
2408 AssertPtr(pImage);
2409
2410 if (pImage)
2411 rc = VERR_NOT_SUPPORTED;
2412 else
2413 rc = VERR_VD_NOT_OPENED;
2414
2415 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2416 return rc;
2417}
2418
2419/** @copydoc VBOXHDDBACKEND::pfnSetParentUuid */
2420static int qedSetParentUuid(void *pBackendData, PCRTUUID pUuid)
2421{
2422 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2423 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2424 int rc;
2425
2426 AssertPtr(pImage);
2427
2428 if (pImage)
2429 {
2430 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2431 rc = VERR_NOT_SUPPORTED;
2432 else
2433 rc = VERR_VD_IMAGE_READ_ONLY;
2434 }
2435 else
2436 rc = VERR_VD_NOT_OPENED;
2437
2438 LogFlowFunc(("returns %Rrc\n", rc));
2439 return rc;
2440}
2441
2442/** @copydoc VBOXHDDBACKEND::pfnGetParentModificationUuid */
2443static int qedGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
2444{
2445 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2446 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2447 int rc;
2448
2449 AssertPtr(pImage);
2450
2451 if (pImage)
2452 rc = VERR_NOT_SUPPORTED;
2453 else
2454 rc = VERR_VD_NOT_OPENED;
2455
2456 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2457 return rc;
2458}
2459
2460/** @copydoc VBOXHDDBACKEND::pfnSetParentModificationUuid */
2461static int qedSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
2462{
2463 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2464 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2465 int rc;
2466
2467 AssertPtr(pImage);
2468
2469 if (pImage)
2470 {
2471 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2472 rc = VERR_NOT_SUPPORTED;
2473 else
2474 rc = VERR_VD_IMAGE_READ_ONLY;
2475 }
2476 else
2477 rc = VERR_VD_NOT_OPENED;
2478
2479 LogFlowFunc(("returns %Rrc\n", rc));
2480 return rc;
2481}
2482
2483/** @copydoc VBOXHDDBACKEND::pfnDump */
2484static void qedDump(void *pBackendData)
2485{
2486 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2487
2488 AssertPtr(pImage);
2489 if (pImage)
2490 {
2491 vdIfErrorMessage(pImage->pIfError, "Header: Geometry PCHS=%u/%u/%u LCHS=%u/%u/%u cbSector=%llu\n",
2492 pImage->PCHSGeometry.cCylinders, pImage->PCHSGeometry.cHeads, pImage->PCHSGeometry.cSectors,
2493 pImage->LCHSGeometry.cCylinders, pImage->LCHSGeometry.cHeads, pImage->LCHSGeometry.cSectors,
2494 pImage->cbSize / 512);
2495 }
2496}
2497
2498/** @copydoc VBOXHDDBACKEND::pfnGetParentFilename */
2499static int qedGetParentFilename(void *pBackendData, char **ppszParentFilename)
2500{
2501 int rc = VINF_SUCCESS;
2502 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2503
2504 AssertPtr(pImage);
2505 if (pImage)
2506 if (pImage->pszBackingFilename)
2507 *ppszParentFilename = RTStrDup(pImage->pszBackingFilename);
2508 else
2509 rc = VERR_NOT_SUPPORTED;
2510 else
2511 rc = VERR_VD_NOT_OPENED;
2512
2513 LogFlowFunc(("returns %Rrc\n", rc));
2514 return rc;
2515}
2516
2517/** @copydoc VBOXHDDBACKEND::pfnSetParentFilename */
2518static int qedSetParentFilename(void *pBackendData, const char *pszParentFilename)
2519{
2520 int rc = VINF_SUCCESS;
2521 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2522
2523 AssertPtr(pImage);
2524 if (pImage)
2525 {
2526 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2527 rc = VERR_VD_IMAGE_READ_ONLY;
2528 else if ( pImage->pszBackingFilename
2529 && (strlen(pszParentFilename) > pImage->cbBackingFilename))
2530 rc = VERR_NOT_SUPPORTED; /* The new filename is longer than the old one. */
2531 else
2532 {
2533 if (pImage->pszBackingFilename)
2534 RTStrFree(pImage->pszBackingFilename);
2535 pImage->pszBackingFilename = RTStrDup(pszParentFilename);
2536 if (!pImage->pszBackingFilename)
2537 rc = VERR_NO_MEMORY;
2538 else
2539 {
2540 if (!pImage->offBackingFilename)
2541 {
2542 /* Allocate new cluster. */
2543 uint64_t offData = qedClusterAllocate(pImage, 1);
2544
2545 Assert((offData & UINT32_MAX) == offData);
2546 pImage->offBackingFilename = (uint32_t)offData;
2547 pImage->cbBackingFilename = (uint32_t)strlen(pszParentFilename);
2548 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage,
2549 offData + pImage->cbCluster);
2550 }
2551
2552 if (RT_SUCCESS(rc))
2553 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
2554 pImage->offBackingFilename,
2555 pImage->pszBackingFilename,
2556 strlen(pImage->pszBackingFilename));
2557 }
2558 }
2559 }
2560 else
2561 rc = VERR_VD_NOT_OPENED;
2562
2563 LogFlowFunc(("returns %Rrc\n", rc));
2564 return rc;
2565}
2566
2567/** @copydoc VBOXHDDBACKEND::pfnResize */
2568static int qedResize(void *pBackendData, uint64_t cbSize,
2569 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
2570 unsigned uPercentStart, unsigned uPercentSpan,
2571 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
2572 PVDINTERFACE pVDIfsOperation)
2573{
2574 PQEDIMAGE pImage = (PQEDIMAGE)pBackendData;
2575 int rc = VINF_SUCCESS;
2576
2577 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
2578
2579 /* Making the image smaller is not supported at the moment. */
2580 if (cbSize < pImage->cbSize)
2581 rc = VERR_NOT_SUPPORTED;
2582 else if (cbSize > pImage->cbSize)
2583 {
2584 /*
2585 * It is enough to just update the size field in the header to complete
2586 * growing. With the default cluster and table sizes the image can be expanded
2587 * to 64TB without overflowing the L1 and L2 tables making block relocation
2588 * superfluous.
2589 * @todo: The rare case where block relocation is still required (non default
2590 * table and/or cluster size or images with more than 64TB) is not
2591 * implemented yet and resizing such an image will fail with an error.
2592 */
2593 if (qedByte2Cluster(pImage, pImage->cbTable)*pImage->cTableEntries*pImage->cTableEntries*pImage->cbCluster < cbSize)
2594 rc = vdIfError(pImage->pIfError, VERR_BUFFER_OVERFLOW, RT_SRC_POS,
2595 N_("Qed: Resizing the image '%s' is not supported because it would overflow the L1 and L2 table\n"),
2596 pImage->pszFilename);
2597 else
2598 {
2599 uint64_t cbSizeOld = pImage->cbSize;
2600
2601 pImage->cbSize = cbSize;
2602 rc = qedFlushImage(pImage);
2603 if (RT_FAILURE(rc))
2604 {
2605 pImage->cbSize = cbSizeOld; /* Restore */
2606
2607 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("Qed: Resizing the image '%s' failed\n"),
2608 pImage->pszFilename);
2609 }
2610 }
2611 }
2612 /* Same size doesn't change the image at all. */
2613
2614 LogFlowFunc(("returns %Rrc\n", rc));
2615 return rc;
2616}
2617
2618
2619const VBOXHDDBACKEND g_QedBackend =
2620{
2621 /* pszBackendName */
2622 "QED",
2623 /* cbSize */
2624 sizeof(VBOXHDDBACKEND),
2625 /* uBackendCaps */
2626 VD_CAP_FILE | VD_CAP_VFS | VD_CAP_CREATE_DYNAMIC | VD_CAP_DIFF | VD_CAP_ASYNC,
2627 /* paFileExtensions */
2628 s_aQedFileExtensions,
2629 /* paConfigInfo */
2630 NULL,
2631 /* pfnCheckIfValid */
2632 qedCheckIfValid,
2633 /* pfnOpen */
2634 qedOpen,
2635 /* pfnCreate */
2636 qedCreate,
2637 /* pfnRename */
2638 qedRename,
2639 /* pfnClose */
2640 qedClose,
2641 /* pfnRead */
2642 qedRead,
2643 /* pfnWrite */
2644 qedWrite,
2645 /* pfnFlush */
2646 qedFlush,
2647 /* pfnDiscard */
2648 NULL,
2649 /* pfnGetVersion */
2650 qedGetVersion,
2651 /* pfnGetSectorSize */
2652 qedGetSectorSize,
2653 /* pfnGetSize */
2654 qedGetSize,
2655 /* pfnGetFileSize */
2656 qedGetFileSize,
2657 /* pfnGetPCHSGeometry */
2658 qedGetPCHSGeometry,
2659 /* pfnSetPCHSGeometry */
2660 qedSetPCHSGeometry,
2661 /* pfnGetLCHSGeometry */
2662 qedGetLCHSGeometry,
2663 /* pfnSetLCHSGeometry */
2664 qedSetLCHSGeometry,
2665 /* pfnGetImageFlags */
2666 qedGetImageFlags,
2667 /* pfnGetOpenFlags */
2668 qedGetOpenFlags,
2669 /* pfnSetOpenFlags */
2670 qedSetOpenFlags,
2671 /* pfnGetComment */
2672 qedGetComment,
2673 /* pfnSetComment */
2674 qedSetComment,
2675 /* pfnGetUuid */
2676 qedGetUuid,
2677 /* pfnSetUuid */
2678 qedSetUuid,
2679 /* pfnGetModificationUuid */
2680 qedGetModificationUuid,
2681 /* pfnSetModificationUuid */
2682 qedSetModificationUuid,
2683 /* pfnGetParentUuid */
2684 qedGetParentUuid,
2685 /* pfnSetParentUuid */
2686 qedSetParentUuid,
2687 /* pfnGetParentModificationUuid */
2688 qedGetParentModificationUuid,
2689 /* pfnSetParentModificationUuid */
2690 qedSetParentModificationUuid,
2691 /* pfnDump */
2692 qedDump,
2693 /* pfnGetTimeStamp */
2694 NULL,
2695 /* pfnGetParentTimeStamp */
2696 NULL,
2697 /* pfnSetParentTimeStamp */
2698 NULL,
2699 /* pfnGetParentFilename */
2700 qedGetParentFilename,
2701 /* pfnSetParentFilename */
2702 qedSetParentFilename,
2703 /* pfnComposeLocation */
2704 genericFileComposeLocation,
2705 /* pfnComposeName */
2706 genericFileComposeName,
2707 /* pfnCompact */
2708 NULL,
2709 /* pfnResize */
2710 qedResize,
2711 /* pfnRepair */
2712 NULL,
2713 /* pfnTraverseMetadata */
2714 NULL
2715};
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