VirtualBox

source: vbox/trunk/src/VBox/Storage/VCICache.cpp@ 58591

Last change on this file since 58591 was 57388, checked in by vboxsync, 9 years ago

DECLCALLBACK

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 65.1 KB
Line 
1/* $Id: VCICache.cpp 57388 2015-08-17 14:20:55Z vboxsync $ */
2/** @file
3 * VCICacheCore - VirtualBox Cache Image, Core Code.
4 */
5
6/*
7 * Copyright (C) 2006-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/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_VD_RAW /** @todo logging group */
23#include <VBox/vd-cache-backend.h>
24#include <VBox/err.h>
25
26#include <VBox/log.h>
27#include <iprt/assert.h>
28#include <iprt/alloc.h>
29#include <iprt/file.h>
30#include <iprt/asm.h>
31
32#include "VDBackends.h"
33
34/*******************************************************************************
35* On disk data structures *
36*******************************************************************************/
37
38/** @note All structures which are written to the disk are written in camel case
39 * and packed. */
40
41/** Block size used internally, because we cache sectors the smallest unit we
42 * have to care about is 512 bytes. */
43#define VCI_BLOCK_SIZE 512
44
45/** Convert block number/size to byte offset/size. */
46#define VCI_BLOCK2BYTE(u) ((uint64_t)(u) << 9)
47
48/** Convert byte offset/size to block number/size. */
49#define VCI_BYTE2BLOCK(u) ((u) >> 9)
50
51/**
52 * The VCI header - at the beginning of the file.
53 *
54 * All entries a stored in little endian order.
55 */
56#pragma pack(1)
57typedef struct VciHdr
58{
59 /** The signature to identify a cache image. */
60 uint32_t u32Signature;
61 /** Version of the layout of metadata in the cache. */
62 uint32_t u32Version;
63 /** Maximum size of the cache file in blocks.
64 * This includes all metadata. */
65 uint64_t cBlocksCache;
66 /** Flag indicating whether the cache was closed cleanly. */
67 uint8_t fUncleanShutdown;
68 /** Cache type. */
69 uint32_t u32CacheType;
70 /** Offset of the B+-Tree root in the image in blocks. */
71 uint64_t offTreeRoot;
72 /** Offset of the block allocation bitmap in blocks. */
73 uint64_t offBlkMap;
74 /** Size of the block allocation bitmap in blocks. */
75 uint32_t cBlkMap;
76 /** UUID of the image. */
77 RTUUID uuidImage;
78 /** Modification UUID for the cache. */
79 RTUUID uuidModification;
80 /** Reserved for future use. */
81 uint8_t abReserved[951];
82} VciHdr, *PVciHdr;
83#pragma pack()
84AssertCompileSize(VciHdr, 2 * VCI_BLOCK_SIZE);
85
86/** VCI signature to identify a valid image. */
87#define VCI_HDR_SIGNATURE UINT32_C(0x00494356) /* \0ICV */
88/** Current version we support. */
89#define VCI_HDR_VERSION UINT32_C(0x00000001)
90
91/** Value for an unclean cache shutdown. */
92#define VCI_HDR_UNCLEAN_SHUTDOWN UINT8_C(0x01)
93/** Value for a clean cache shutdown. */
94#define VCI_HDR_CLEAN_SHUTDOWN UINT8_C(0x00)
95
96/** Cache type: Dynamic image growing to the maximum value. */
97#define VCI_HDR_CACHE_TYPE_DYNAMIC UINT32_C(0x00000001)
98/** Cache type: Fixed image, space is preallocated. */
99#define VCI_HDR_CACHE_TYPE_FIXED UINT32_C(0x00000002)
100
101/**
102 * On disk representation of an extent describing a range of cached data.
103 *
104 * All entries a stored in little endian order.
105 */
106#pragma pack(1)
107typedef struct VciCacheExtent
108{
109 /** Block address of the previous extent in the LRU list. */
110 uint64_t u64ExtentPrev;
111 /** Block address of the next extent in the LRU list. */
112 uint64_t u64ExtentNext;
113 /** Flags (for compression, encryption etc.) - currently unused and should be always 0. */
114 uint8_t u8Flags;
115 /** Reserved */
116 uint8_t u8Reserved;
117 /** First block of cached data the extent represents. */
118 uint64_t u64BlockOffset;
119 /** Number of blocks the extent represents. */
120 uint32_t u32Blocks;
121 /** First block in the image where the data is stored. */
122 uint64_t u64BlockAddr;
123} VciCacheExtent, *PVciCacheExtent;
124#pragma pack()
125AssertCompileSize(VciCacheExtent, 38);
126
127/**
128 * On disk representation of an internal node.
129 *
130 * All entries a stored in little endian order.
131 */
132#pragma pack(1)
133typedef struct VciTreeNodeInternal
134{
135 /** First block of cached data the internal node represents. */
136 uint64_t u64BlockOffset;
137 /** Number of blocks the internal node represents. */
138 uint32_t u32Blocks;
139 /** Block address in the image where the next node in the tree is stored. */
140 uint64_t u64ChildAddr;
141} VciTreeNodeInternal, *PVciTreeNodeInternal;
142#pragma pack()
143AssertCompileSize(VciTreeNodeInternal, 20);
144
145/**
146 * On-disk representation of a node in the B+-Tree.
147 *
148 * All entries a stored in little endian order.
149 */
150#pragma pack(1)
151typedef struct VciTreeNode
152{
153 /** Type of the node (root, internal, leaf). */
154 uint8_t u8Type;
155 /** Data in the node. */
156 uint8_t au8Data[4095];
157} VciTreeNode, *PVciTreeNode;
158#pragma pack()
159AssertCompileSize(VciTreeNode, 8 * VCI_BLOCK_SIZE);
160
161/** Node type: Internal node containing links to other nodes (VciTreeNodeInternal). */
162#define VCI_TREE_NODE_TYPE_INTERNAL UINT8_C(0x01)
163/** Node type: Leaf of the tree (VciCacheExtent). */
164#define VCI_TREE_NODE_TYPE_LEAF UINT8_C(0x02)
165
166/** Number of cache extents described by one node. */
167#define VCI_TREE_EXTENTS_PER_NODE ((sizeof(VciTreeNode)-1) / sizeof(VciCacheExtent))
168/** Number of internal nodes managed by one tree node. */
169#define VCI_TREE_INTERNAL_NODES_PER_NODE ((sizeof(VciTreeNode)-1) / sizeof(VciTreeNodeInternal))
170
171/**
172 * VCI block bitmap header.
173 *
174 * All entries a stored in little endian order.
175 */
176#pragma pack(1)
177typedef struct VciBlkMap
178{
179 /** Magic of the block bitmap. */
180 uint32_t u32Magic;
181 /** Version of the block bitmap. */
182 uint32_t u32Version;
183 /** Number of blocks this block map manages. */
184 uint64_t cBlocks;
185 /** Number of free blocks. */
186 uint64_t cBlocksFree;
187 /** Number of blocks allocated for metadata. */
188 uint64_t cBlocksAllocMeta;
189 /** Number of blocks allocated for actual cached data. */
190 uint64_t cBlocksAllocData;
191 /** Reserved for future use. */
192 uint8_t au8Reserved[472];
193} VciBlkMap, *PVciBlkMap;
194#pragma pack()
195AssertCompileSize(VciBlkMap, VCI_BLOCK_SIZE);
196
197/** The magic which identifies a block map. */
198#define VCI_BLKMAP_MAGIC UINT32_C(0x4b4c4256) /* KLBV */
199/** Current version. */
200#define VCI_BLKMAP_VERSION UINT32_C(0x00000001)
201
202/** Block bitmap entry */
203typedef uint8_t VciBlkMapEnt;
204
205
206/*********************************************************************************************************************************
207* Constants And Macros, Structures and Typedefs *
208*********************************************************************************************************************************/
209
210/**
211 * Block range descriptor.
212 */
213typedef struct VCIBLKRANGEDESC
214{
215 /** Previous entry in the list. */
216 struct VCIBLKRANGEDESC *pPrev;
217 /** Next entry in the list. */
218 struct VCIBLKRANGEDESC *pNext;
219 /** Start address of the range. */
220 uint64_t offAddrStart;
221 /** Number of blocks in the range. */
222 uint64_t cBlocks;
223 /** Flag whether the range is free or allocated. */
224 bool fFree;
225} VCIBLKRANGEDESC, *PVCIBLKRANGEDESC;
226
227/**
228 * Block map for the cache image - in memory structure.
229 */
230typedef struct VCIBLKMAP
231{
232 /** Number of blocks the map manages. */
233 uint64_t cBlocks;
234 /** Number of blocks allocated for metadata. */
235 uint64_t cBlocksAllocMeta;
236 /** Number of blocks allocated for actual cached data. */
237 uint64_t cBlocksAllocData;
238 /** Number of free blocks. */
239 uint64_t cBlocksFree;
240
241 /** Pointer to the head of the block range list. */
242 PVCIBLKRANGEDESC pRangesHead;
243 /** Pointer to the tail of the block range list. */
244 PVCIBLKRANGEDESC pRangesTail;
245
246} VCIBLKMAP;
247/** Pointer to a block map. */
248typedef VCIBLKMAP *PVCIBLKMAP;
249
250/**
251 * B+-Tree node header.
252 */
253typedef struct VCITREENODE
254{
255 /** Type of the node (VCI_TREE_NODE_TYPE_*). */
256 uint8_t u8Type;
257 /** Block address where the node is stored. */
258 uint64_t u64BlockAddr;
259 /** Pointer to the parent. */
260 struct VCITREENODE *pParent;
261} VCITREENODE, *PVCITREENODE;
262
263/**
264 * B+-Tree node pointer.
265 */
266typedef struct VCITREENODEPTR
267{
268 /** Flag whether the node is in memory or still on the disk. */
269 bool fInMemory;
270 /** Type dependent data. */
271 union
272 {
273 /** Pointer to a in memory node. */
274 PVCITREENODE pNode;
275 /** Start block address of the node. */
276 uint64_t offAddrBlockNode;
277 } u;
278} VCITREENODEPTR, *PVCITREENODEPTR;
279
280/**
281 * Internal node.
282 */
283typedef struct VCINODEINTERNAL
284{
285 /** First block of cached data the internal node represents. */
286 uint64_t u64BlockOffset;
287 /** Number of blocks the internal node represents. */
288 uint32_t u32Blocks;
289 /** Pointer to the child node. */
290 VCITREENODEPTR PtrChild;
291} VCINODEINTERNAL, *PVCINODEINTERNAL;
292
293/**
294 * A in memory internal B+-tree node.
295 */
296typedef struct VCITREENODEINT
297{
298 /** Node core. */
299 VCITREENODE Core;
300 /** Number of used nodes. */
301 unsigned cUsedNodes;
302 /** Array of internal nodes. */
303 VCINODEINTERNAL aIntNodes[VCI_TREE_INTERNAL_NODES_PER_NODE];
304} VCITREENODEINT, *PVCITREENODEINT;
305
306/**
307 * A in memory cache extent.
308 */
309typedef struct VCICACHEEXTENT
310{
311 /** First block of cached data the extent represents. */
312 uint64_t u64BlockOffset;
313 /** Number of blocks the extent represents. */
314 uint32_t u32Blocks;
315 /** First block in the image where the data is stored. */
316 uint64_t u64BlockAddr;
317} VCICACHEEXTENT, *PVCICACHEEXTENT;
318
319/**
320 * A in memory leaf B+-tree node.
321 */
322typedef struct VCITREENODELEAF
323{
324 /** Node core. */
325 VCITREENODE Core;
326 /** Next leaf node in the list. */
327 struct VCITREENODELEAF *pNext;
328 /** Number of used nodes. */
329 unsigned cUsedNodes;
330 /** The extents in the node. */
331 VCICACHEEXTENT aExtents[VCI_TREE_EXTENTS_PER_NODE];
332} VCITREENODELEAF, *PVCITREENODELEAF;
333
334/**
335 * VCI image data structure.
336 */
337typedef struct VCICACHE
338{
339 /** Image name. */
340 const char *pszFilename;
341 /** Storage handle. */
342 PVDIOSTORAGE pStorage;
343
344 /** Pointer to the per-disk VD interface list. */
345 PVDINTERFACE pVDIfsDisk;
346 /** Pointer to the per-image VD interface list. */
347 PVDINTERFACE pVDIfsImage;
348 /** Error interface. */
349 PVDINTERFACEERROR pIfError;
350 /** I/O interface. */
351 PVDINTERFACEIOINT pIfIo;
352
353 /** Open flags passed by VBoxHD layer. */
354 unsigned uOpenFlags;
355 /** Image flags defined during creation or determined during open. */
356 unsigned uImageFlags;
357 /** Total size of the image. */
358 uint64_t cbSize;
359
360 /** Offset of the B+-Tree in the image in bytes. */
361 uint64_t offTreeRoot;
362 /** Pointer to the root node of the B+-Tree. */
363 PVCITREENODE pRoot;
364 /** Offset to the block allocation bitmap in bytes. */
365 uint64_t offBlksBitmap;
366 /** Block map. */
367 PVCIBLKMAP pBlkMap;
368} VCICACHE, *PVCICACHE;
369
370/** No block free in bitmap error code. */
371#define VERR_VCI_NO_BLOCKS_FREE (-65536)
372
373/** Flags for the block map allocator. */
374#define VCIBLKMAP_ALLOC_DATA 0
375#define VCIBLKMAP_ALLOC_META RT_BIT(0)
376#define VCIBLKMAP_ALLOC_MASK 0x1
377
378
379/*********************************************************************************************************************************
380* Static Variables *
381*********************************************************************************************************************************/
382
383/** NULL-terminated array of supported file extensions. */
384static const char *const s_apszVciFileExtensions[] =
385{
386 "vci",
387 NULL
388};
389
390
391/*********************************************************************************************************************************
392* Internal Functions *
393*********************************************************************************************************************************/
394
395/**
396 * Internal. Flush image data to disk.
397 */
398static int vciFlushImage(PVCICACHE pCache)
399{
400 int rc = VINF_SUCCESS;
401
402 if ( pCache->pStorage
403 && !(pCache->uOpenFlags & VD_OPEN_FLAGS_READONLY))
404 {
405 rc = vdIfIoIntFileFlushSync(pCache->pIfIo, pCache->pStorage);
406 }
407
408 return rc;
409}
410
411/**
412 * Internal. Free all allocated space for representing an image except pCache,
413 * and optionally delete the image from disk.
414 */
415static int vciFreeImage(PVCICACHE pCache, bool fDelete)
416{
417 int rc = VINF_SUCCESS;
418
419 /* Freeing a never allocated image (e.g. because the open failed) is
420 * not signalled as an error. After all nothing bad happens. */
421 if (pCache)
422 {
423 if (pCache->pStorage)
424 {
425 /* No point updating the file that is deleted anyway. */
426 if (!fDelete)
427 vciFlushImage(pCache);
428
429 vdIfIoIntFileClose(pCache->pIfIo, pCache->pStorage);
430 pCache->pStorage = NULL;
431 }
432
433 if (fDelete && pCache->pszFilename)
434 vdIfIoIntFileDelete(pCache->pIfIo, pCache->pszFilename);
435 }
436
437 LogFlowFunc(("returns %Rrc\n", rc));
438 return rc;
439}
440
441/**
442 * Creates a new block map which can manage the given number of blocks.
443 *
444 * The size of the bitmap is aligned to the VCI block size.
445 *
446 * @returns VBox status code.
447 * @param cBlocks The number of blocks the bitmap can manage.
448 * @param ppBlkMap Where to store the pointer to the block bitmap.
449 * @param pcbBlkMap Where to store the size of the block bitmap in blocks
450 * needed on the disk.
451 */
452static int vciBlkMapCreate(uint64_t cBlocks, PVCIBLKMAP *ppBlkMap, uint32_t *pcBlkMap)
453{
454 int rc = VINF_SUCCESS;
455 uint32_t cbBlkMap = RT_ALIGN_Z(cBlocks / sizeof(VciBlkMapEnt) / 8, VCI_BLOCK_SIZE);
456 PVCIBLKMAP pBlkMap = (PVCIBLKMAP)RTMemAllocZ(sizeof(VCIBLKMAP));
457 PVCIBLKRANGEDESC pFree = (PVCIBLKRANGEDESC)RTMemAllocZ(sizeof(VCIBLKRANGEDESC));
458
459 LogFlowFunc(("cBlocks=%u ppBlkMap=%#p pcBlkMap=%#p\n", cBlocks, ppBlkMap, pcBlkMap));
460
461 if (pBlkMap && pFree)
462 {
463 pBlkMap->cBlocks = cBlocks;
464 pBlkMap->cBlocksAllocMeta = 0;
465 pBlkMap->cBlocksAllocData = 0;
466 pBlkMap->cBlocksFree = cBlocks;
467
468 pFree->pPrev = NULL;
469 pFree->pNext = NULL;
470 pFree->offAddrStart = 0;
471 pFree->cBlocks = cBlocks;
472 pFree->fFree = true;
473
474 pBlkMap->pRangesHead = pFree;
475 pBlkMap->pRangesTail = pFree;
476
477 Assert(!((cbBlkMap + sizeof(VciBlkMap)) % VCI_BLOCK_SIZE));
478 *ppBlkMap = pBlkMap;
479 *pcBlkMap = VCI_BYTE2BLOCK(cbBlkMap + sizeof(VciBlkMap));
480 }
481 else
482 {
483 if (pBlkMap)
484 RTMemFree(pBlkMap);
485 if (pFree)
486 RTMemFree(pFree);
487
488 rc = VERR_NO_MEMORY;
489 }
490
491 LogFlowFunc(("returns rc=%Rrc cBlkMap=%u\n", rc, *pcBlkMap));
492 return rc;
493}
494
495/**
496 * Frees a block map.
497 *
498 * @returns nothing.
499 * @param pBlkMap The block bitmap to destroy.
500 */
501static void vciBlkMapDestroy(PVCIBLKMAP pBlkMap)
502{
503 LogFlowFunc(("pBlkMap=%#p\n", pBlkMap));
504
505 PVCIBLKRANGEDESC pRangeCur = pBlkMap->pRangesHead;
506
507 while (pRangeCur)
508 {
509 PVCIBLKRANGEDESC pTmp = pRangeCur;
510
511 RTMemFree(pTmp);
512
513 pRangeCur = pRangeCur->pNext;
514 }
515
516 RTMemFree(pBlkMap);
517
518 LogFlowFunc(("returns\n"));
519}
520
521/**
522 * Loads the block map from the specified medium and creates all necessary
523 * in memory structures to manage used and free blocks.
524 *
525 * @returns VBox status code.
526 * @param pStorage Storage handle to read the block bitmap from.
527 * @param offBlkMap Start of the block bitmap in blocks.
528 * @param cBlkMap Size of the block bitmap on the disk in blocks.
529 * @param ppBlkMap Where to store the block bitmap on success.
530 */
531static int vciBlkMapLoad(PVCICACHE pStorage, uint64_t offBlkMap, uint32_t cBlkMap, PVCIBLKMAP *ppBlkMap)
532{
533 int rc = VINF_SUCCESS;
534 VciBlkMap BlkMap;
535
536 LogFlowFunc(("pStorage=%#p offBlkMap=%llu cBlkMap=%u ppBlkMap=%#p\n",
537 pStorage, offBlkMap, cBlkMap, ppBlkMap));
538
539 if (cBlkMap >= VCI_BYTE2BLOCK(sizeof(VciBlkMap)))
540 {
541 cBlkMap -= VCI_BYTE2BLOCK(sizeof(VciBlkMap));
542
543 rc = vdIfIoIntFileReadSync(pStorage->pIfIo, pStorage->pStorage, offBlkMap,
544 &BlkMap, VCI_BYTE2BLOCK(sizeof(VciBlkMap)));
545 if (RT_SUCCESS(rc))
546 {
547 offBlkMap += VCI_BYTE2BLOCK(sizeof(VciBlkMap));
548
549 BlkMap.u32Magic = RT_LE2H_U32(BlkMap.u32Magic);
550 BlkMap.u32Version = RT_LE2H_U32(BlkMap.u32Version);
551 BlkMap.cBlocks = RT_LE2H_U32(BlkMap.cBlocks);
552 BlkMap.cBlocksFree = RT_LE2H_U32(BlkMap.cBlocksFree);
553 BlkMap.cBlocksAllocMeta = RT_LE2H_U32(BlkMap.cBlocksAllocMeta);
554 BlkMap.cBlocksAllocData = RT_LE2H_U32(BlkMap.cBlocksAllocData);
555
556 if ( BlkMap.u32Magic == VCI_BLKMAP_MAGIC
557 && BlkMap.u32Version == VCI_BLKMAP_VERSION
558 && BlkMap.cBlocks == BlkMap.cBlocksFree + BlkMap.cBlocksAllocMeta + BlkMap.cBlocksAllocData
559 && VCI_BYTE2BLOCK(BlkMap.cBlocks / 8) == cBlkMap)
560 {
561 PVCIBLKMAP pBlkMap = (PVCIBLKMAP)RTMemAllocZ(sizeof(VCIBLKMAP));
562 if (pBlkMap)
563 {
564 pBlkMap->cBlocks = BlkMap.cBlocks;
565 pBlkMap->cBlocksFree = BlkMap.cBlocksFree;
566 pBlkMap->cBlocksAllocMeta = BlkMap.cBlocksAllocMeta;
567 pBlkMap->cBlocksAllocData = BlkMap.cBlocksAllocData;
568
569 /* Load the bitmap and construct the range list. */
570 uint32_t cBlocksFree = 0;
571 uint32_t cBlocksAllocated = 0;
572 PVCIBLKRANGEDESC pRangeCur = (PVCIBLKRANGEDESC)RTMemAllocZ(sizeof(VCIBLKRANGEDESC));
573
574 if (pRangeCur)
575 {
576 uint8_t abBitmapBuffer[16 * _1K];
577 uint32_t cBlocksRead = 0;
578 uint64_t cBlocksLeft = VCI_BYTE2BLOCK(pBlkMap->cBlocks / 8);
579
580 cBlocksRead = RT_MIN(VCI_BYTE2BLOCK(sizeof(abBitmapBuffer)), cBlocksLeft);
581 rc = vdIfIoIntFileReadSync(pStorage->pIfIo, pStorage->pStorage,
582 offBlkMap, abBitmapBuffer,
583 cBlocksRead);
584
585 if (RT_SUCCESS(rc))
586 {
587 pRangeCur->fFree = !(abBitmapBuffer[0] & 0x01);
588 pRangeCur->offAddrStart = 0;
589 pRangeCur->cBlocks = 0;
590 pRangeCur->pNext = NULL;
591 pRangeCur->pPrev = NULL;
592 pBlkMap->pRangesHead = pRangeCur;
593 pBlkMap->pRangesTail = pRangeCur;
594 }
595 else
596 RTMemFree(pRangeCur);
597
598 while ( RT_SUCCESS(rc)
599 && cBlocksLeft)
600 {
601 int iBit = 0;
602 uint32_t cBits = VCI_BLOCK2BYTE(cBlocksRead) * 8;
603 uint32_t iBitPrev = 0xffffffff;
604
605 while (cBits)
606 {
607 if (pRangeCur->fFree)
608 {
609 /* Check for the first set bit. */
610 iBit = ASMBitNextSet(abBitmapBuffer, cBits, iBitPrev);
611 }
612 else
613 {
614 /* Check for the first free bit. */
615 iBit = ASMBitNextClear(abBitmapBuffer, cBits, iBitPrev);
616 }
617
618 if (iBit == -1)
619 {
620 /* No change. */
621 pRangeCur->cBlocks += cBits;
622 cBits = 0;
623 }
624 else
625 {
626 Assert((uint32_t)iBit < cBits);
627 pRangeCur->cBlocks += iBit;
628
629 /* Create a new range descriptor. */
630 PVCIBLKRANGEDESC pRangeNew = (PVCIBLKRANGEDESC)RTMemAllocZ(sizeof(VCIBLKRANGEDESC));
631 if (!pRangeNew)
632 {
633 rc = VERR_NO_MEMORY;
634 break;
635 }
636
637 pRangeNew->fFree = !pRangeCur->fFree;
638 pRangeNew->offAddrStart = pRangeCur->offAddrStart + pRangeCur->cBlocks;
639 pRangeNew->cBlocks = 0;
640 pRangeNew->pPrev = pRangeCur;
641 pRangeCur->pNext = pRangeNew;
642 pBlkMap->pRangesTail = pRangeNew;
643 pRangeCur = pRangeNew;
644 cBits -= iBit;
645 iBitPrev = iBit;
646 }
647 }
648
649 cBlocksLeft -= cBlocksRead;
650 offBlkMap += cBlocksRead;
651
652 if ( RT_SUCCESS(rc)
653 && cBlocksLeft)
654 {
655 /* Read next chunk. */
656 cBlocksRead = RT_MIN(VCI_BYTE2BLOCK(sizeof(abBitmapBuffer)), cBlocksLeft);
657 rc = vdIfIoIntFileReadSync(pStorage->pIfIo, pStorage->pStorage,
658 offBlkMap, abBitmapBuffer, cBlocksRead);
659 }
660 }
661 }
662 else
663 rc = VERR_NO_MEMORY;
664
665 if (RT_SUCCESS(rc))
666 {
667 *ppBlkMap = pBlkMap;
668 LogFlowFunc(("return success\n"));
669 return VINF_SUCCESS;
670 }
671 else
672 RTMemFree(pBlkMap);
673 }
674 else
675 rc = VERR_NO_MEMORY;
676 }
677 else
678 rc = VERR_VD_GEN_INVALID_HEADER;
679 }
680 else
681 rc = VERR_VD_GEN_INVALID_HEADER;
682 }
683 else
684 rc = VERR_VD_GEN_INVALID_HEADER;
685
686 LogFlowFunc(("returns rc=%Rrc\n", rc));
687 return rc;
688}
689
690/**
691 * Saves the block map in the cache image. All necessary on disk structures
692 * are written.
693 *
694 * @returns VBox status code.
695 * @param pBlkMap The block bitmap to save.
696 * @param pStorage Where the block bitmap should be written to.
697 * @param offBlkMap Start of the block bitmap in blocks.
698 * @param cBlkMap Size of the block bitmap on the disk in blocks.
699 */
700static int vciBlkMapSave(PVCIBLKMAP pBlkMap, PVCICACHE pStorage, uint64_t offBlkMap, uint32_t cBlkMap)
701{
702 int rc = VINF_SUCCESS;
703 VciBlkMap BlkMap;
704
705 LogFlowFunc(("pBlkMap=%#p pStorage=%#p offBlkMap=%llu cBlkMap=%u\n",
706 pBlkMap, pStorage, offBlkMap, cBlkMap));
707
708 /* Make sure the number of blocks allocated for us match our expectations. */
709 if (VCI_BYTE2BLOCK(pBlkMap->cBlocks / 8) + VCI_BYTE2BLOCK(sizeof(VciBlkMap)) == cBlkMap)
710 {
711 /* Setup the header */
712 memset(&BlkMap, 0, sizeof(VciBlkMap));
713
714 BlkMap.u32Magic = RT_H2LE_U32(VCI_BLKMAP_MAGIC);
715 BlkMap.u32Version = RT_H2LE_U32(VCI_BLKMAP_VERSION);
716 BlkMap.cBlocks = RT_H2LE_U32(pBlkMap->cBlocks);
717 BlkMap.cBlocksFree = RT_H2LE_U32(pBlkMap->cBlocksFree);
718 BlkMap.cBlocksAllocMeta = RT_H2LE_U32(pBlkMap->cBlocksAllocMeta);
719 BlkMap.cBlocksAllocData = RT_H2LE_U32(pBlkMap->cBlocksAllocData);
720
721 rc = vdIfIoIntFileWriteSync(pStorage->pIfIo, pStorage->pStorage, offBlkMap,
722 &BlkMap, VCI_BYTE2BLOCK(sizeof(VciBlkMap)));
723 if (RT_SUCCESS(rc))
724 {
725 uint8_t abBitmapBuffer[16*_1K];
726 unsigned iBit = 0;
727 PVCIBLKRANGEDESC pCur = pBlkMap->pRangesHead;
728
729 offBlkMap += VCI_BYTE2BLOCK(sizeof(VciBlkMap));
730
731 /* Write the descriptor ranges. */
732 while (pCur)
733 {
734 uint64_t cBlocks = pCur->cBlocks;
735
736 while (cBlocks)
737 {
738 uint64_t cBlocksMax = RT_MIN(cBlocks, sizeof(abBitmapBuffer) * 8 - iBit);
739
740 if (pCur->fFree)
741 ASMBitClearRange(abBitmapBuffer, iBit, iBit + cBlocksMax);
742 else
743 ASMBitSetRange(abBitmapBuffer, iBit, iBit + cBlocksMax);
744
745 iBit += cBlocksMax;
746 cBlocks -= cBlocksMax;
747
748 if (iBit == sizeof(abBitmapBuffer) * 8)
749 {
750 /* Buffer is full, write to file and reset. */
751 rc = vdIfIoIntFileWriteSync(pStorage->pIfIo, pStorage->pStorage,
752 offBlkMap, abBitmapBuffer,
753 VCI_BYTE2BLOCK(sizeof(abBitmapBuffer)));
754 if (RT_FAILURE(rc))
755 break;
756
757 offBlkMap += VCI_BYTE2BLOCK(sizeof(abBitmapBuffer));
758 iBit = 0;
759 }
760 }
761
762 pCur = pCur->pNext;
763 }
764
765 Assert(iBit % 8 == 0);
766
767 if (RT_SUCCESS(rc) && iBit)
768 rc = vdIfIoIntFileWriteSync(pStorage->pIfIo, pStorage->pStorage,
769 offBlkMap, abBitmapBuffer, VCI_BYTE2BLOCK(iBit / 8));
770 }
771 }
772 else
773 rc = VERR_INTERNAL_ERROR; /* @todo Better error code. */
774
775 LogFlowFunc(("returns rc=%Rrc\n", rc));
776 return rc;
777}
778
779/**
780 * Finds the range block describing the given block address.
781 *
782 * @returns Pointer to the block range descriptor or NULL if none could be found.
783 * @param pBlkMap The block bitmap to search on.
784 * @param offBlockAddr The block address to search for.
785 */
786static PVCIBLKRANGEDESC vciBlkMapFindByBlock(PVCIBLKMAP pBlkMap, uint64_t offBlockAddr)
787{
788 PVCIBLKRANGEDESC pBlk = pBlkMap->pRangesHead;
789
790 while ( pBlk
791 && pBlk->offAddrStart < offBlockAddr)
792 pBlk = pBlk->pNext;
793
794 return pBlk;
795}
796
797/**
798 * Allocates the given number of blocks in the bitmap and returns the start block address.
799 *
800 * @returns VBox status code.
801 * @param pBlkMap The block bitmap to allocate the blocks from.
802 * @param cBlocks How many blocks to allocate.
803 * @param fFlags Allocation flags, comgination of VCIBLKMAP_ALLOC_*.
804 * @param poffBlockAddr Where to store the start address of the allocated region.
805 */
806static int vciBlkMapAllocate(PVCIBLKMAP pBlkMap, uint32_t cBlocks, uint32_t fFlags,
807 uint64_t *poffBlockAddr)
808{
809 PVCIBLKRANGEDESC pBestFit = NULL;
810 PVCIBLKRANGEDESC pCur = NULL;
811 int rc = VINF_SUCCESS;
812
813 LogFlowFunc(("pBlkMap=%#p cBlocks=%u poffBlockAddr=%#p\n",
814 pBlkMap, cBlocks, poffBlockAddr));
815
816 pCur = pBlkMap->pRangesHead;
817
818 while (pCur)
819 {
820 if ( pCur->fFree
821 && pCur->cBlocks >= cBlocks)
822 {
823 if ( !pBestFit
824 || pCur->cBlocks < pBestFit->cBlocks)
825 {
826 pBestFit = pCur;
827 /* Stop searching if the size is matching exactly. */
828 if (pBestFit->cBlocks == cBlocks)
829 break;
830 }
831 }
832 pCur = pCur->pNext;
833 }
834
835 Assert(!pBestFit || pBestFit->fFree);
836
837 if (pBestFit)
838 {
839 pBestFit->fFree = false;
840
841 if (pBestFit->cBlocks > cBlocks)
842 {
843 /* Create a new free block. */
844 PVCIBLKRANGEDESC pFree = (PVCIBLKRANGEDESC)RTMemAllocZ(sizeof(VCIBLKRANGEDESC));
845
846 if (pFree)
847 {
848 pFree->fFree = true;
849 pFree->cBlocks = pBestFit->cBlocks - cBlocks;
850 pBestFit->cBlocks -= pFree->cBlocks;
851 pFree->offAddrStart = pBestFit->offAddrStart + cBlocks;
852
853 /* Link into the list. */
854 pFree->pNext = pBestFit->pNext;
855 pBestFit->pNext = pFree;
856 pFree->pPrev = pBestFit;
857 if (!pFree->pNext)
858 pBlkMap->pRangesTail = pFree;
859
860 *poffBlockAddr = pBestFit->offAddrStart;
861 }
862 else
863 {
864 rc = VERR_NO_MEMORY;
865 pBestFit->fFree = true;
866 }
867 }
868 }
869 else
870 rc = VERR_VCI_NO_BLOCKS_FREE;
871
872 if (RT_SUCCESS(rc))
873 {
874 if ((fFlags & VCIBLKMAP_ALLOC_MASK) == VCIBLKMAP_ALLOC_DATA)
875 pBlkMap->cBlocksAllocMeta += cBlocks;
876 else
877 pBlkMap->cBlocksAllocData += cBlocks;
878
879 pBlkMap->cBlocksFree -= cBlocks;
880 }
881
882 LogFlowFunc(("returns rc=%Rrc offBlockAddr=%llu\n", rc, *poffBlockAddr));
883 return rc;
884}
885
886/**
887 * Try to extend the space of an already allocated block.
888 *
889 * @returns VBox status code.
890 * @param pBlkMap The block bitmap to allocate the blocks from.
891 * @param cBlocksNew How many blocks the extended block should have.
892 * @param offBlockAddrOld The start address of the block to reallocate.
893 * @param poffBlockAddr Where to store the start address of the allocated region.
894 */
895static int vciBlkMapRealloc(PVCIBLKMAP pBlkMap, uint32_t cBlocksNew, uint64_t offBlockAddrOld,
896 uint64_t *poffBlockAddr)
897{
898 int rc = VINF_SUCCESS;
899
900 LogFlowFunc(("pBlkMap=%#p cBlocksNew=%u offBlockAddrOld=%llu poffBlockAddr=%#p\n",
901 pBlkMap, cBlocksNew, offBlockAddrOld, poffBlockAddr));
902
903 AssertMsgFailed(("Implement\n"));
904
905 LogFlowFunc(("returns rc=%Rrc offBlockAddr=%llu\n", rc, *poffBlockAddr));
906 return rc;
907}
908
909/**
910 * Frees a range of blocks.
911 *
912 * @returns nothing.
913 * @param pBlkMap The block bitmap.
914 * @param offBlockAddr Address of the first block to free.
915 * @param cBlocks How many blocks to free.
916 * @param fFlags Allocation flags, comgination of VCIBLKMAP_ALLOC_*.
917 */
918static void vciBlkMapFree(PVCIBLKMAP pBlkMap, uint64_t offBlockAddr, uint32_t cBlocks,
919 uint32_t fFlags)
920{
921 PVCIBLKRANGEDESC pBlk;
922
923 LogFlowFunc(("pBlkMap=%#p offBlockAddr=%llu cBlocks=%u\n",
924 pBlkMap, offBlockAddr, cBlocks));
925
926 while (cBlocks)
927 {
928 pBlk = vciBlkMapFindByBlock(pBlkMap, offBlockAddr);
929 AssertPtr(pBlk);
930
931 /* Easy case, the whole block is freed. */
932 if ( pBlk->offAddrStart == offBlockAddr
933 && pBlk->cBlocks <= cBlocks)
934 {
935 pBlk->fFree = true;
936 cBlocks -= pBlk->cBlocks;
937 offBlockAddr += pBlk->cBlocks;
938
939 /* Check if it is possible to merge free blocks. */
940 if ( pBlk->pPrev
941 && pBlk->pPrev->fFree)
942 {
943 PVCIBLKRANGEDESC pBlkPrev = pBlk->pPrev;
944
945 Assert(pBlkPrev->offAddrStart + pBlkPrev->cBlocks == pBlk->offAddrStart);
946 pBlkPrev->cBlocks += pBlk->cBlocks;
947 pBlkPrev->pNext = pBlk->pNext;
948 if (pBlk->pNext)
949 pBlk->pNext->pPrev = pBlkPrev;
950 else
951 pBlkMap->pRangesTail = pBlkPrev;
952
953 RTMemFree(pBlk);
954 pBlk = pBlkPrev;
955 }
956
957 /* Now the one to the right. */
958 if ( pBlk->pNext
959 && pBlk->pNext->fFree)
960 {
961 PVCIBLKRANGEDESC pBlkNext = pBlk->pNext;
962
963 Assert(pBlk->offAddrStart + pBlk->cBlocks == pBlkNext->offAddrStart);
964 pBlk->cBlocks += pBlkNext->cBlocks;
965 pBlk->pNext = pBlkNext->pNext;
966 if (pBlkNext->pNext)
967 pBlkNext->pNext->pPrev = pBlk;
968 else
969 pBlkMap->pRangesTail = pBlk;
970
971 RTMemFree(pBlkNext);
972 }
973 }
974 else
975 {
976 /* The block is intersecting. */
977 AssertMsgFailed(("TODO\n"));
978 }
979 }
980
981 if ((fFlags & VCIBLKMAP_ALLOC_MASK) == VCIBLKMAP_ALLOC_DATA)
982 pBlkMap->cBlocksAllocMeta -= cBlocks;
983 else
984 pBlkMap->cBlocksAllocData -= cBlocks;
985
986 pBlkMap->cBlocksFree += cBlocks;
987
988 LogFlowFunc(("returns\n"));
989}
990
991/**
992 * Converts a tree node from the image to the in memory structure.
993 *
994 * @returns Pointer to the in memory tree node.
995 * @param offBlockAddrNode Block address of the node.
996 * @param pNodeImage Pointer to the image representation of the node.
997 */
998static PVCITREENODE vciTreeNodeImage2Host(uint64_t offBlockAddrNode, PVciTreeNode pNodeImage)
999{
1000 PVCITREENODE pNode = NULL;
1001
1002 if (pNodeImage->u8Type == VCI_TREE_NODE_TYPE_LEAF)
1003 {
1004 PVCITREENODELEAF pLeaf = (PVCITREENODELEAF)RTMemAllocZ(sizeof(VCITREENODELEAF));
1005
1006 if (pLeaf)
1007 {
1008 PVciCacheExtent pExtent = (PVciCacheExtent)&pNodeImage->au8Data[0];
1009
1010 pLeaf->Core.u8Type = VCI_TREE_NODE_TYPE_LEAF;
1011
1012 for (unsigned idx = 0; idx < RT_ELEMENTS(pLeaf->aExtents); idx++)
1013 {
1014 pLeaf->aExtents[idx].u64BlockOffset = RT_LE2H_U64(pExtent->u64BlockOffset);
1015 pLeaf->aExtents[idx].u32Blocks = RT_LE2H_U32(pExtent->u32Blocks);
1016 pLeaf->aExtents[idx].u64BlockAddr = RT_LE2H_U64(pExtent->u64BlockAddr);
1017 pExtent++;
1018
1019 if ( pLeaf->aExtents[idx].u32Blocks
1020 && pLeaf->aExtents[idx].u64BlockAddr)
1021 pLeaf->cUsedNodes++;
1022 }
1023
1024 pNode = &pLeaf->Core;
1025 }
1026 }
1027 else if (pNodeImage->u8Type == VCI_TREE_NODE_TYPE_INTERNAL)
1028 {
1029 PVCITREENODEINT pInt = (PVCITREENODEINT)RTMemAllocZ(sizeof(VCITREENODEINT));
1030
1031 if (pInt)
1032 {
1033 PVciTreeNodeInternal pIntImage = (PVciTreeNodeInternal)&pNodeImage->au8Data[0];
1034
1035 pInt->Core.u8Type = VCI_TREE_NODE_TYPE_INTERNAL;
1036
1037 for (unsigned idx = 0; idx < RT_ELEMENTS(pInt->aIntNodes); idx++)
1038 {
1039 pInt->aIntNodes[idx].u64BlockOffset = RT_LE2H_U64(pIntImage->u64BlockOffset);
1040 pInt->aIntNodes[idx].u32Blocks = RT_LE2H_U32(pIntImage->u32Blocks);
1041 pInt->aIntNodes[idx].PtrChild.fInMemory = false;
1042 pInt->aIntNodes[idx].PtrChild.u.offAddrBlockNode = RT_LE2H_U64(pIntImage->u64ChildAddr);
1043 pIntImage++;
1044
1045 if ( pInt->aIntNodes[idx].u32Blocks
1046 && pInt->aIntNodes[idx].PtrChild.u.offAddrBlockNode)
1047 pInt->cUsedNodes++;
1048 }
1049
1050 pNode = &pInt->Core;
1051 }
1052 }
1053 else
1054 AssertMsgFailed(("Invalid node type %d\n", pNodeImage->u8Type));
1055
1056 if (pNode)
1057 pNode->u64BlockAddr = offBlockAddrNode;
1058
1059 return pNode;
1060}
1061
1062/**
1063 * Looks up the cache extent for the given virtual block address.
1064 *
1065 * @returns Pointer to the cache extent or NULL if none could be found.
1066 * @param pCache The cache image instance.
1067 * @param offBlockOffset The block offset to search for.
1068 * @param ppNextBestFit Where to store the pointer to the next best fit
1069 * cache extent above offBlockOffset if existing. - Optional
1070 * This is always filled if possible even if the function returns NULL.
1071 */
1072static PVCICACHEEXTENT vciCacheExtentLookup(PVCICACHE pCache, uint64_t offBlockOffset,
1073 PVCICACHEEXTENT *ppNextBestFit)
1074{
1075 int rc = VINF_SUCCESS;
1076 PVCICACHEEXTENT pExtent = NULL;
1077 PVCITREENODE pNodeCur = pCache->pRoot;
1078
1079 while ( RT_SUCCESS(rc)
1080 && pNodeCur
1081 && pNodeCur->u8Type != VCI_TREE_NODE_TYPE_LEAF)
1082 {
1083 PVCITREENODEINT pNodeInt = (PVCITREENODEINT)pNodeCur;
1084
1085 Assert(pNodeCur->u8Type == VCI_TREE_NODE_TYPE_INTERNAL);
1086
1087 /* Search for the correct internal node. */
1088 unsigned idxMin = 0;
1089 unsigned idxMax = pNodeInt->cUsedNodes;
1090 unsigned idxCur = pNodeInt->cUsedNodes / 2;
1091
1092 while (idxMin < idxMax)
1093 {
1094 PVCINODEINTERNAL pInt = &pNodeInt->aIntNodes[idxCur];
1095
1096 /* Determine the search direction. */
1097 if (offBlockOffset < pInt->u64BlockOffset)
1098 {
1099 /* Search left from the current extent. */
1100 idxMax = idxCur;
1101 }
1102 else if (offBlockOffset >= pInt->u64BlockOffset + pInt->u32Blocks)
1103 {
1104 /* Search right from the current extent. */
1105 idxMin = idxCur;
1106 }
1107 else
1108 {
1109 /* The block lies in the node, stop searching. */
1110 if (pInt->PtrChild.fInMemory)
1111 pNodeCur = pInt->PtrChild.u.pNode;
1112 else
1113 {
1114 PVCITREENODE pNodeNew;
1115 VciTreeNode NodeTree;
1116
1117 /* Read from disk and add to the tree. */
1118 rc = vdIfIoIntFileReadSync(pCache->pIfIo, pCache->pStorage,
1119 VCI_BLOCK2BYTE(pInt->PtrChild.u.offAddrBlockNode),
1120 &NodeTree, sizeof(NodeTree));
1121 AssertRC(rc);
1122
1123 pNodeNew = vciTreeNodeImage2Host(pInt->PtrChild.u.offAddrBlockNode, &NodeTree);
1124 if (pNodeNew)
1125 {
1126 /* Link to the parent. */
1127 pInt->PtrChild.fInMemory = true;
1128 pInt->PtrChild.u.pNode = pNodeNew;
1129 pNodeNew->pParent = pNodeCur;
1130 pNodeCur = pNodeNew;
1131 }
1132 else
1133 rc = VERR_NO_MEMORY;
1134 }
1135 break;
1136 }
1137
1138 idxCur = idxMin + (idxMax - idxMin) / 2;
1139 }
1140 }
1141
1142 if ( RT_SUCCESS(rc)
1143 && pNodeCur)
1144 {
1145 PVCITREENODELEAF pLeaf = (PVCITREENODELEAF)pNodeCur;
1146 Assert(pNodeCur->u8Type == VCI_TREE_NODE_TYPE_LEAF);
1147
1148 /* Search the range. */
1149 unsigned idxMin = 0;
1150 unsigned idxMax = pLeaf->cUsedNodes;
1151 unsigned idxCur = pLeaf->cUsedNodes / 2;
1152
1153 while (idxMin < idxMax)
1154 {
1155 PVCICACHEEXTENT pExtentCur = &pLeaf->aExtents[idxCur];
1156
1157 /* Determine the search direction. */
1158 if (offBlockOffset < pExtentCur->u64BlockOffset)
1159 {
1160 /* Search left from the current extent. */
1161 idxMax = idxCur;
1162 }
1163 else if (offBlockOffset >= pExtentCur->u64BlockOffset + pExtentCur->u32Blocks)
1164 {
1165 /* Search right from the current extent. */
1166 idxMin = idxCur;
1167 }
1168 else
1169 {
1170 /* We found the extent, stop searching. */
1171 pExtent = pExtentCur;
1172 break;
1173 }
1174
1175 idxCur = idxMin + (idxMax - idxMin) / 2;
1176 }
1177
1178 /* Get the next best fit extent if it exists. */
1179 if (ppNextBestFit)
1180 {
1181 if (idxCur < pLeaf->cUsedNodes - 1)
1182 *ppNextBestFit = &pLeaf->aExtents[idxCur + 1];
1183 else
1184 {
1185 /*
1186 * Go up the tree and find the best extent
1187 * in the leftmost tree of the child subtree to the right.
1188 */
1189 PVCITREENODEINT pInt = (PVCITREENODEINT)pLeaf->Core.pParent;
1190
1191 while (pInt)
1192 {
1193
1194 }
1195 }
1196 }
1197 }
1198
1199 return pExtent;
1200}
1201
1202/**
1203 * Internal: Open an image, constructing all necessary data structures.
1204 */
1205static int vciOpenImage(PVCICACHE pCache, unsigned uOpenFlags)
1206{
1207 VciHdr Hdr;
1208 uint64_t cbFile;
1209 int rc;
1210
1211 pCache->uOpenFlags = uOpenFlags;
1212
1213 pCache->pIfError = VDIfErrorGet(pCache->pVDIfsDisk);
1214 pCache->pIfIo = VDIfIoIntGet(pCache->pVDIfsImage);
1215 AssertPtrReturn(pCache->pIfIo, VERR_INVALID_PARAMETER);
1216
1217 /*
1218 * Open the image.
1219 */
1220 rc = vdIfIoIntFileOpen(pCache->pIfIo, pCache->pszFilename,
1221 VDOpenFlagsToFileOpenFlags(uOpenFlags,
1222 false /* fCreate */),
1223 &pCache->pStorage);
1224 if (RT_FAILURE(rc))
1225 {
1226 /* Do NOT signal an appropriate error here, as the VD layer has the
1227 * choice of retrying the open if it failed. */
1228 goto out;
1229 }
1230
1231 rc = vdIfIoIntFileGetSize(pCache->pIfIo, pCache->pStorage, &cbFile);
1232 if (RT_FAILURE(rc) || cbFile < sizeof(VciHdr))
1233 {
1234 rc = VERR_VD_GEN_INVALID_HEADER;
1235 goto out;
1236 }
1237
1238 rc = vdIfIoIntFileReadSync(pCache->pIfIo, pCache->pStorage, 0, &Hdr,
1239 VCI_BYTE2BLOCK(sizeof(Hdr)));
1240 if (RT_FAILURE(rc))
1241 {
1242 rc = VERR_VD_GEN_INVALID_HEADER;
1243 goto out;
1244 }
1245
1246 Hdr.u32Signature = RT_LE2H_U32(Hdr.u32Signature);
1247 Hdr.u32Version = RT_LE2H_U32(Hdr.u32Version);
1248 Hdr.cBlocksCache = RT_LE2H_U64(Hdr.cBlocksCache);
1249 Hdr.u32CacheType = RT_LE2H_U32(Hdr.u32CacheType);
1250 Hdr.offTreeRoot = RT_LE2H_U64(Hdr.offTreeRoot);
1251 Hdr.offBlkMap = RT_LE2H_U64(Hdr.offBlkMap);
1252 Hdr.cBlkMap = RT_LE2H_U32(Hdr.cBlkMap);
1253
1254 if ( Hdr.u32Signature == VCI_HDR_SIGNATURE
1255 && Hdr.u32Version == VCI_HDR_VERSION)
1256 {
1257 pCache->offTreeRoot = Hdr.offTreeRoot;
1258 pCache->offBlksBitmap = Hdr.offBlkMap;
1259
1260 /* Load the block map. */
1261 rc = vciBlkMapLoad(pCache, pCache->offBlksBitmap, Hdr.cBlkMap, &pCache->pBlkMap);
1262 if (RT_SUCCESS(rc))
1263 {
1264 /* Load the first tree node. */
1265 VciTreeNode RootNode;
1266
1267 rc = vdIfIoIntFileReadSync(pCache->pIfIo, pCache->pStorage,
1268 pCache->offTreeRoot, &RootNode,
1269 VCI_BYTE2BLOCK(sizeof(VciTreeNode)));
1270 if (RT_SUCCESS(rc))
1271 {
1272 pCache->pRoot = vciTreeNodeImage2Host(pCache->offTreeRoot, &RootNode);
1273 if (!pCache->pRoot)
1274 rc = VERR_NO_MEMORY;
1275 }
1276 }
1277 }
1278 else
1279 rc = VERR_VD_GEN_INVALID_HEADER;
1280
1281out:
1282 if (RT_FAILURE(rc))
1283 vciFreeImage(pCache, false);
1284 return rc;
1285}
1286
1287/**
1288 * Internal: Create a vci image.
1289 */
1290static int vciCreateImage(PVCICACHE pCache, uint64_t cbSize,
1291 unsigned uImageFlags, const char *pszComment,
1292 unsigned uOpenFlags, PFNVDPROGRESS pfnProgress,
1293 void *pvUser, unsigned uPercentStart,
1294 unsigned uPercentSpan)
1295{
1296 VciHdr Hdr;
1297 VciTreeNode NodeRoot;
1298 int rc;
1299 uint64_t cBlocks = cbSize / VCI_BLOCK_SIZE; /* Size of the cache in blocks. */
1300
1301 pCache->uImageFlags = uImageFlags;
1302 pCache->uOpenFlags = uOpenFlags & ~VD_OPEN_FLAGS_READONLY;
1303
1304 pCache->pIfError = VDIfErrorGet(pCache->pVDIfsDisk);
1305 pCache->pIfIo = VDIfIoIntGet(pCache->pVDIfsImage);
1306 AssertPtrReturn(pCache->pIfIo, VERR_INVALID_PARAMETER);
1307
1308 if (uImageFlags & VD_IMAGE_FLAGS_DIFF)
1309 {
1310 rc = vdIfError(pCache->pIfError, VERR_VD_RAW_INVALID_TYPE, RT_SRC_POS, N_("VCI: cannot create diff image '%s'"), pCache->pszFilename);
1311 return rc;
1312 }
1313
1314 do
1315 {
1316 /* Create image file. */
1317 rc = vdIfIoIntFileOpen(pCache->pIfIo, pCache->pszFilename,
1318 VDOpenFlagsToFileOpenFlags(uOpenFlags & ~VD_OPEN_FLAGS_READONLY,
1319 true /* fCreate */),
1320 &pCache->pStorage);
1321 if (RT_FAILURE(rc))
1322 {
1323 rc = vdIfError(pCache->pIfError, rc, RT_SRC_POS, N_("VCI: cannot create image '%s'"), pCache->pszFilename);
1324 break;
1325 }
1326
1327 /* Allocate block bitmap. */
1328 uint32_t cBlkMap = 0;
1329 rc = vciBlkMapCreate(cBlocks, &pCache->pBlkMap, &cBlkMap);
1330 if (RT_FAILURE(rc))
1331 {
1332 rc = vdIfError(pCache->pIfError, rc, RT_SRC_POS, N_("VCI: cannot create block bitmap '%s'"), pCache->pszFilename);
1333 break;
1334 }
1335
1336 /*
1337 * Allocate space for the header in the block bitmap.
1338 * Because the block map is empty the header has to start at block 0
1339 */
1340 uint64_t offHdr = 0;
1341 rc = vciBlkMapAllocate(pCache->pBlkMap, VCI_BYTE2BLOCK(sizeof(VciHdr)), VCIBLKMAP_ALLOC_META, &offHdr);
1342 if (RT_FAILURE(rc))
1343 {
1344 rc = vdIfError(pCache->pIfError, rc, RT_SRC_POS, N_("VCI: cannot allocate space for header in block bitmap '%s'"), pCache->pszFilename);
1345 break;
1346 }
1347
1348 Assert(offHdr == 0);
1349
1350 /*
1351 * Allocate space for the block map itself.
1352 */
1353 uint64_t offBlkMap = 0;
1354 rc = vciBlkMapAllocate(pCache->pBlkMap, cBlkMap, VCIBLKMAP_ALLOC_META, &offBlkMap);
1355 if (RT_FAILURE(rc))
1356 {
1357 rc = vdIfError(pCache->pIfError, rc, RT_SRC_POS, N_("VCI: cannot allocate space for block map in block map '%s'"), pCache->pszFilename);
1358 break;
1359 }
1360
1361 /*
1362 * Allocate space for the tree root node.
1363 */
1364 uint64_t offTreeRoot = 0;
1365 rc = vciBlkMapAllocate(pCache->pBlkMap, VCI_BYTE2BLOCK(sizeof(VciTreeNode)), VCIBLKMAP_ALLOC_META, &offTreeRoot);
1366 if (RT_FAILURE(rc))
1367 {
1368 rc = vdIfError(pCache->pIfError, rc, RT_SRC_POS, N_("VCI: cannot allocate space for block map in block map '%s'"), pCache->pszFilename);
1369 break;
1370 }
1371
1372 /*
1373 * Allocate the in memory root node.
1374 */
1375 pCache->pRoot = (PVCITREENODE)RTMemAllocZ(sizeof(VCITREENODELEAF));
1376 if (!pCache->pRoot)
1377 {
1378 rc = vdIfError(pCache->pIfError, rc, RT_SRC_POS, N_("VCI: cannot allocate B+-Tree root pointer '%s'"), pCache->pszFilename);
1379 break;
1380 }
1381
1382 pCache->pRoot->u8Type = VCI_TREE_NODE_TYPE_LEAF;
1383 /* Rest remains 0 as the tree is still empty. */
1384
1385 /*
1386 * Now that we are here we have all the basic structures and know where to place them in the image.
1387 * It's time to write it now.
1388 */
1389
1390 /* Setup the header. */
1391 memset(&Hdr, 0, sizeof(VciHdr));
1392 Hdr.u32Signature = RT_H2LE_U32(VCI_HDR_SIGNATURE);
1393 Hdr.u32Version = RT_H2LE_U32(VCI_HDR_VERSION);
1394 Hdr.cBlocksCache = RT_H2LE_U64(cBlocks);
1395 Hdr.fUncleanShutdown = VCI_HDR_UNCLEAN_SHUTDOWN;
1396 Hdr.u32CacheType = uImageFlags & VD_IMAGE_FLAGS_FIXED
1397 ? RT_H2LE_U32(VCI_HDR_CACHE_TYPE_FIXED)
1398 : RT_H2LE_U32(VCI_HDR_CACHE_TYPE_DYNAMIC);
1399 Hdr.offTreeRoot = RT_H2LE_U64(offTreeRoot);
1400 Hdr.offBlkMap = RT_H2LE_U64(offBlkMap);
1401 Hdr.cBlkMap = RT_H2LE_U32(cBlkMap);
1402
1403 rc = vdIfIoIntFileWriteSync(pCache->pIfIo, pCache->pStorage, offHdr, &Hdr,
1404 VCI_BYTE2BLOCK(sizeof(VciHdr)));
1405 if (RT_FAILURE(rc))
1406 {
1407 rc = vdIfError(pCache->pIfError, rc, RT_SRC_POS, N_("VCI: cannot write header '%s'"), pCache->pszFilename);
1408 break;
1409 }
1410
1411 rc = vciBlkMapSave(pCache->pBlkMap, pCache, offBlkMap, cBlkMap);
1412 if (RT_FAILURE(rc))
1413 {
1414 rc = vdIfError(pCache->pIfError, rc, RT_SRC_POS, N_("VCI: cannot write block map '%s'"), pCache->pszFilename);
1415 break;
1416 }
1417
1418 /* Setup the root tree. */
1419 memset(&NodeRoot, 0, sizeof(VciTreeNode));
1420 NodeRoot.u8Type = RT_H2LE_U32(VCI_TREE_NODE_TYPE_LEAF);
1421
1422 rc = vdIfIoIntFileWriteSync(pCache->pIfIo, pCache->pStorage, offTreeRoot,
1423 &NodeRoot, VCI_BYTE2BLOCK(sizeof(VciTreeNode)));
1424 if (RT_FAILURE(rc))
1425 {
1426 rc = vdIfError(pCache->pIfError, rc, RT_SRC_POS, N_("VCI: cannot write root node '%s'"), pCache->pszFilename);
1427 break;
1428 }
1429
1430 rc = vciFlushImage(pCache);
1431 if (RT_FAILURE(rc))
1432 {
1433 rc = vdIfError(pCache->pIfError, rc, RT_SRC_POS, N_("VCI: cannot flush '%s'"), pCache->pszFilename);
1434 break;
1435 }
1436
1437 pCache->cbSize = cbSize;
1438
1439 } while (0);
1440
1441 if (RT_SUCCESS(rc) && pfnProgress)
1442 pfnProgress(pvUser, uPercentStart + uPercentSpan);
1443
1444 if (RT_FAILURE(rc))
1445 vciFreeImage(pCache, rc != VERR_ALREADY_EXISTS);
1446 return rc;
1447}
1448
1449/** @copydoc VDCACHEBACKEND::pfnProbe */
1450static DECLCALLBACK(int) vciProbe(const char *pszFilename, PVDINTERFACE pVDIfsCache,
1451 PVDINTERFACE pVDIfsImage)
1452{
1453 VciHdr Hdr;
1454 PVDIOSTORAGE pStorage = NULL;
1455 uint64_t cbFile;
1456 int rc = VINF_SUCCESS;
1457
1458 LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
1459
1460 PVDINTERFACEIOINT pIfIo = VDIfIoIntGet(pVDIfsImage);
1461 AssertPtrReturn(pIfIo, VERR_INVALID_PARAMETER);
1462
1463 rc = vdIfIoIntFileOpen(pIfIo, pszFilename,
1464 VDOpenFlagsToFileOpenFlags(VD_OPEN_FLAGS_READONLY,
1465 false /* fCreate */),
1466 &pStorage);
1467 if (RT_FAILURE(rc))
1468 goto out;
1469
1470 rc = vdIfIoIntFileGetSize(pIfIo, pStorage, &cbFile);
1471 if (RT_FAILURE(rc) || cbFile < sizeof(VciHdr))
1472 {
1473 rc = VERR_VD_GEN_INVALID_HEADER;
1474 goto out;
1475 }
1476
1477 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, 0, &Hdr, sizeof(Hdr));
1478 if (RT_FAILURE(rc))
1479 {
1480 rc = VERR_VD_GEN_INVALID_HEADER;
1481 goto out;
1482 }
1483
1484 Hdr.u32Signature = RT_LE2H_U32(Hdr.u32Signature);
1485 Hdr.u32Version = RT_LE2H_U32(Hdr.u32Version);
1486 Hdr.cBlocksCache = RT_LE2H_U64(Hdr.cBlocksCache);
1487 Hdr.u32CacheType = RT_LE2H_U32(Hdr.u32CacheType);
1488 Hdr.offTreeRoot = RT_LE2H_U64(Hdr.offTreeRoot);
1489 Hdr.offBlkMap = RT_LE2H_U64(Hdr.offBlkMap);
1490 Hdr.cBlkMap = RT_LE2H_U32(Hdr.cBlkMap);
1491
1492 if ( Hdr.u32Signature == VCI_HDR_SIGNATURE
1493 && Hdr.u32Version == VCI_HDR_VERSION)
1494 rc = VINF_SUCCESS;
1495 else
1496 rc = VERR_VD_GEN_INVALID_HEADER;
1497
1498out:
1499 if (pStorage)
1500 vdIfIoIntFileClose(pIfIo, pStorage);
1501
1502 LogFlowFunc(("returns %Rrc\n", rc));
1503 return rc;
1504}
1505
1506/** @copydoc VDCACHEBACKEND::pfnOpen */
1507static DECLCALLBACK(int) vciOpen(const char *pszFilename, unsigned uOpenFlags,
1508 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1509 void **ppBackendData)
1510{
1511 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, ppBackendData));
1512 int rc;
1513 PVCICACHE pCache;
1514
1515 /* Check open flags. All valid flags are supported. */
1516 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
1517 {
1518 rc = VERR_INVALID_PARAMETER;
1519 goto out;
1520 }
1521
1522 /* Check remaining arguments. */
1523 if ( !VALID_PTR(pszFilename)
1524 || !*pszFilename)
1525 {
1526 rc = VERR_INVALID_PARAMETER;
1527 goto out;
1528 }
1529
1530
1531 pCache = (PVCICACHE)RTMemAllocZ(sizeof(VCICACHE));
1532 if (!pCache)
1533 {
1534 rc = VERR_NO_MEMORY;
1535 goto out;
1536 }
1537 pCache->pszFilename = pszFilename;
1538 pCache->pStorage = NULL;
1539 pCache->pVDIfsDisk = pVDIfsDisk;
1540 pCache->pVDIfsImage = pVDIfsImage;
1541
1542 rc = vciOpenImage(pCache, uOpenFlags);
1543 if (RT_SUCCESS(rc))
1544 *ppBackendData = pCache;
1545 else
1546 RTMemFree(pCache);
1547
1548out:
1549 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1550 return rc;
1551}
1552
1553/** @copydoc VDCACHEBACKEND::pfnCreate */
1554static DECLCALLBACK(int) vciCreate(const char *pszFilename, uint64_t cbSize,
1555 unsigned uImageFlags, const char *pszComment,
1556 PCRTUUID pUuid, unsigned uOpenFlags,
1557 unsigned uPercentStart, unsigned uPercentSpan,
1558 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1559 PVDINTERFACE pVDIfsOperation, void **ppBackendData)
1560{
1561 LogFlowFunc(("pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" Uuid=%RTuuid uOpenFlags=%#x uPercentStart=%u uPercentSpan=%u pVDIfsDisk=%#p pVDIfsImage=%#p pVDIfsOperation=%#p ppBackendData=%#p",
1562 pszFilename, cbSize, uImageFlags, pszComment, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, ppBackendData));
1563 int rc;
1564 PVCICACHE pCache;
1565
1566 PFNVDPROGRESS pfnProgress = NULL;
1567 void *pvUser = NULL;
1568 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
1569 if (pIfProgress)
1570 {
1571 pfnProgress = pIfProgress->pfnProgress;
1572 pvUser = pIfProgress->Core.pvUser;
1573 }
1574
1575 /* Check open flags. All valid flags are supported. */
1576 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
1577 {
1578 rc = VERR_INVALID_PARAMETER;
1579 goto out;
1580 }
1581
1582 /* Check remaining arguments. */
1583 if ( !VALID_PTR(pszFilename)
1584 || !*pszFilename)
1585 {
1586 rc = VERR_INVALID_PARAMETER;
1587 goto out;
1588 }
1589
1590 pCache = (PVCICACHE)RTMemAllocZ(sizeof(VCICACHE));
1591 if (!pCache)
1592 {
1593 rc = VERR_NO_MEMORY;
1594 goto out;
1595 }
1596 pCache->pszFilename = pszFilename;
1597 pCache->pStorage = NULL;
1598 pCache->pVDIfsDisk = pVDIfsDisk;
1599 pCache->pVDIfsImage = pVDIfsImage;
1600
1601 rc = vciCreateImage(pCache, cbSize, uImageFlags, pszComment, uOpenFlags,
1602 pfnProgress, pvUser, uPercentStart, uPercentSpan);
1603 if (RT_SUCCESS(rc))
1604 {
1605 /* So far the image is opened in read/write mode. Make sure the
1606 * image is opened in read-only mode if the caller requested that. */
1607 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
1608 {
1609 vciFreeImage(pCache, false);
1610 rc = vciOpenImage(pCache, uOpenFlags);
1611 if (RT_FAILURE(rc))
1612 {
1613 RTMemFree(pCache);
1614 goto out;
1615 }
1616 }
1617 *ppBackendData = pCache;
1618 }
1619 else
1620 RTMemFree(pCache);
1621
1622out:
1623 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1624 return rc;
1625}
1626
1627/** @copydoc VDCACHEBACKEND::pfnClose */
1628static DECLCALLBACK(int) vciClose(void *pBackendData, bool fDelete)
1629{
1630 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
1631 PVCICACHE pCache = (PVCICACHE)pBackendData;
1632 int rc;
1633
1634 rc = vciFreeImage(pCache, fDelete);
1635 RTMemFree(pCache);
1636
1637 LogFlowFunc(("returns %Rrc\n", rc));
1638 return rc;
1639}
1640
1641/** @copydoc VDCACHEBACKEND::pfnRead */
1642static DECLCALLBACK(int) vciRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
1643 PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
1644{
1645 LogFlowFunc(("pBackendData=%#p uOffset=%llu cbToRead=%zu pIoCtx=%#p pcbActuallyRead=%#p\n",
1646 pBackendData, uOffset, cbToRead, pIoCtx, pcbActuallyRead));
1647 PVCICACHE pCache = (PVCICACHE)pBackendData;
1648 int rc = VINF_SUCCESS;
1649 PVCICACHEEXTENT pExtent;
1650 uint64_t cBlocksToRead = VCI_BYTE2BLOCK(cbToRead);
1651 uint64_t offBlockAddr = VCI_BYTE2BLOCK(uOffset);
1652
1653 AssertPtr(pCache);
1654 Assert(uOffset % 512 == 0);
1655 Assert(cbToRead % 512 == 0);
1656
1657 pExtent = vciCacheExtentLookup(pCache, offBlockAddr, NULL);
1658 if (pExtent)
1659 {
1660 uint64_t offRead = offBlockAddr - pExtent->u64BlockOffset;
1661 cBlocksToRead = RT_MIN(cBlocksToRead, pExtent->u32Blocks - offRead);
1662
1663 rc = vdIfIoIntFileReadUser(pCache->pIfIo, pCache->pStorage,
1664 pExtent->u64BlockAddr + offRead,
1665 pIoCtx, cBlocksToRead);
1666 }
1667 else
1668 {
1669 /** @todo Best fit to check whether we have cached data later and set
1670 * pcbActuallyRead accordingly. */
1671 rc = VERR_VD_BLOCK_FREE;
1672 }
1673
1674 if (pcbActuallyRead)
1675 *pcbActuallyRead = VCI_BLOCK2BYTE(cBlocksToRead);
1676
1677 LogFlowFunc(("returns %Rrc\n", rc));
1678 return rc;
1679}
1680
1681/** @copydoc VDCACHEBACKEND::pfnWrite */
1682static DECLCALLBACK(int) vciWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
1683 PVDIOCTX pIoCtx, size_t *pcbWriteProcess)
1684{
1685 LogFlowFunc(("pBackendData=%#p uOffset=%llu cbToWrite=%zu pIoCtx=%#p pcbWriteProcess=%#p\n",
1686 pBackendData, uOffset, cbToWrite, pIoCtx, pcbWriteProcess));
1687 PVCICACHE pCache = (PVCICACHE)pBackendData;
1688 int rc = VINF_SUCCESS;
1689 uint64_t cBlocksToWrite = VCI_BYTE2BLOCK(cbToWrite);
1690 uint64_t offBlockAddr = VCI_BYTE2BLOCK(uOffset);
1691
1692 AssertPtr(pCache);
1693 Assert(uOffset % 512 == 0);
1694 Assert(cbToWrite % 512 == 0);
1695
1696 while (cBlocksToWrite)
1697 {
1698
1699 }
1700
1701 *pcbWriteProcess = cbToWrite; /** @todo: Implement. */
1702
1703 LogFlowFunc(("returns %Rrc\n", rc));
1704 return rc;
1705}
1706
1707/** @copydoc VDCACHEBACKEND::pfnFlush */
1708static DECLCALLBACK(int) vciFlush(void *pBackendData, PVDIOCTX pIoCtx)
1709{
1710 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1711 PVCICACHE pCache = (PVCICACHE)pBackendData;
1712 int rc = VINF_SUCCESS;
1713
1714 rc = vciFlushImage(pCache);
1715 LogFlowFunc(("returns %Rrc\n", rc));
1716 return rc;
1717}
1718
1719/** @copydoc VDCACHEBACKEND::pfnGetVersion */
1720static DECLCALLBACK(unsigned) vciGetVersion(void *pBackendData)
1721{
1722 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1723 PVCICACHE pCache = (PVCICACHE)pBackendData;
1724
1725 AssertPtr(pCache);
1726
1727 if (pCache)
1728 return 1;
1729 else
1730 return 0;
1731}
1732
1733/** @copydoc VDCACHEBACKEND::pfnGetSize */
1734static DECLCALLBACK(uint64_t) vciGetSize(void *pBackendData)
1735{
1736 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1737 PVCICACHE pCache = (PVCICACHE)pBackendData;
1738 uint64_t cb = 0;
1739
1740 AssertPtr(pCache);
1741
1742 if (pCache && pCache->pStorage)
1743 cb = pCache->cbSize;
1744
1745 LogFlowFunc(("returns %llu\n", cb));
1746 return cb;
1747}
1748
1749/** @copydoc VDCACHEBACKEND::pfnGetFileSize */
1750static DECLCALLBACK(uint64_t) vciGetFileSize(void *pBackendData)
1751{
1752 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1753 PVCICACHE pCache = (PVCICACHE)pBackendData;
1754 uint64_t cb = 0;
1755
1756 AssertPtr(pCache);
1757
1758 if (pCache)
1759 {
1760 uint64_t cbFile;
1761 if (pCache->pStorage)
1762 {
1763 int rc = vdIfIoIntFileGetSize(pCache->pIfIo, pCache->pStorage, &cbFile);
1764 if (RT_SUCCESS(rc))
1765 cb = cbFile;
1766 }
1767 }
1768
1769 LogFlowFunc(("returns %lld\n", cb));
1770 return cb;
1771}
1772
1773/** @copydoc VDCACHEBACKEND::pfnGetImageFlags */
1774static DECLCALLBACK(unsigned) vciGetImageFlags(void *pBackendData)
1775{
1776 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1777 PVCICACHE pCache = (PVCICACHE)pBackendData;
1778 unsigned uImageFlags;
1779
1780 AssertPtr(pCache);
1781
1782 if (pCache)
1783 uImageFlags = pCache->uImageFlags;
1784 else
1785 uImageFlags = 0;
1786
1787 LogFlowFunc(("returns %#x\n", uImageFlags));
1788 return uImageFlags;
1789}
1790
1791/** @copydoc VDCACHEBACKEND::pfnGetOpenFlags */
1792static DECLCALLBACK(unsigned) vciGetOpenFlags(void *pBackendData)
1793{
1794 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
1795 PVCICACHE pCache = (PVCICACHE)pBackendData;
1796 unsigned uOpenFlags;
1797
1798 AssertPtr(pCache);
1799
1800 if (pCache)
1801 uOpenFlags = pCache->uOpenFlags;
1802 else
1803 uOpenFlags = 0;
1804
1805 LogFlowFunc(("returns %#x\n", uOpenFlags));
1806 return uOpenFlags;
1807}
1808
1809/** @copydoc VDCACHEBACKEND::pfnSetOpenFlags */
1810static DECLCALLBACK(int) vciSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
1811{
1812 LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
1813 PVCICACHE pCache = (PVCICACHE)pBackendData;
1814 int rc;
1815
1816 /* Image must be opened and the new flags must be valid. Just readonly and
1817 * info flags are supported. */
1818 if (!pCache || (uOpenFlags & ~(VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO)))
1819 {
1820 rc = VERR_INVALID_PARAMETER;
1821 goto out;
1822 }
1823
1824 /* Implement this operation via reopening the image. */
1825 rc = vciFreeImage(pCache, false);
1826 if (RT_FAILURE(rc))
1827 goto out;
1828 rc = vciOpenImage(pCache, uOpenFlags);
1829
1830out:
1831 LogFlowFunc(("returns %Rrc\n", rc));
1832 return rc;
1833}
1834
1835/** @copydoc VDCACHEBACKEND::pfnGetComment */
1836static DECLCALLBACK(int) vciGetComment(void *pBackendData, char *pszComment,
1837 size_t cbComment)
1838{
1839 LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
1840 PVCICACHE pCache = (PVCICACHE)pBackendData;
1841 int rc;
1842
1843 AssertPtr(pCache);
1844
1845 if (pCache)
1846 rc = VERR_NOT_SUPPORTED;
1847 else
1848 rc = VERR_VD_NOT_OPENED;
1849
1850 LogFlowFunc(("returns %Rrc comment='%s'\n", rc, pszComment));
1851 return rc;
1852}
1853
1854/** @copydoc VDCACHEBACKEND::pfnSetComment */
1855static DECLCALLBACK(int) vciSetComment(void *pBackendData, const char *pszComment)
1856{
1857 LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
1858 PVCICACHE pCache = (PVCICACHE)pBackendData;
1859 int rc;
1860
1861 AssertPtr(pCache);
1862
1863 if (pCache)
1864 {
1865 if (pCache->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1866 rc = VERR_VD_IMAGE_READ_ONLY;
1867 else
1868 rc = VERR_NOT_SUPPORTED;
1869 }
1870 else
1871 rc = VERR_VD_NOT_OPENED;
1872
1873 LogFlowFunc(("returns %Rrc\n", rc));
1874 return rc;
1875}
1876
1877/** @copydoc VDCACHEBACKEND::pfnGetUuid */
1878static DECLCALLBACK(int) vciGetUuid(void *pBackendData, PRTUUID pUuid)
1879{
1880 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1881 PVCICACHE pCache = (PVCICACHE)pBackendData;
1882 int rc;
1883
1884 AssertPtr(pCache);
1885
1886 if (pCache)
1887 rc = VERR_NOT_SUPPORTED;
1888 else
1889 rc = VERR_VD_NOT_OPENED;
1890
1891 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
1892 return rc;
1893}
1894
1895/** @copydoc VDCACHEBACKEND::pfnSetUuid */
1896static DECLCALLBACK(int) vciSetUuid(void *pBackendData, PCRTUUID pUuid)
1897{
1898 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
1899 PVCICACHE pCache = (PVCICACHE)pBackendData;
1900 int rc;
1901
1902 LogFlowFunc(("%RTuuid\n", pUuid));
1903 AssertPtr(pCache);
1904
1905 if (pCache)
1906 {
1907 if (!(pCache->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1908 rc = VERR_NOT_SUPPORTED;
1909 else
1910 rc = VERR_VD_IMAGE_READ_ONLY;
1911 }
1912 else
1913 rc = VERR_VD_NOT_OPENED;
1914
1915 LogFlowFunc(("returns %Rrc\n", rc));
1916 return rc;
1917}
1918
1919/** @copydoc VDCACHEBACKEND::pfnGetModificationUuid */
1920static DECLCALLBACK(int) vciGetModificationUuid(void *pBackendData, PRTUUID pUuid)
1921{
1922 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1923 PVCICACHE pCache = (PVCICACHE)pBackendData;
1924 int rc;
1925
1926 AssertPtr(pCache);
1927
1928 if (pCache)
1929 rc = VERR_NOT_SUPPORTED;
1930 else
1931 rc = VERR_VD_NOT_OPENED;
1932
1933 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
1934 return rc;
1935}
1936
1937/** @copydoc VDCACHEBACKEND::pfnSetModificationUuid */
1938static DECLCALLBACK(int) vciSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
1939{
1940 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
1941 PVCICACHE pCache = (PVCICACHE)pBackendData;
1942 int rc;
1943
1944 AssertPtr(pCache);
1945
1946 if (pCache)
1947 {
1948 if (!(pCache->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1949 rc = VERR_NOT_SUPPORTED;
1950 else
1951 rc = VERR_VD_IMAGE_READ_ONLY;
1952 }
1953 else
1954 rc = VERR_VD_NOT_OPENED;
1955
1956 LogFlowFunc(("returns %Rrc\n", rc));
1957 return rc;
1958}
1959
1960/** @copydoc VDCACHEBACKEND::pfnDump */
1961static DECLCALLBACK(void) vciDump(void *pBackendData)
1962{
1963 NOREF(pBackendData);
1964}
1965
1966
1967const VDCACHEBACKEND g_VciCacheBackend =
1968{
1969 /* pszBackendName */
1970 "vci",
1971 /* cbSize */
1972 sizeof(VDCACHEBACKEND),
1973 /* uBackendCaps */
1974 VD_CAP_CREATE_FIXED | VD_CAP_CREATE_DYNAMIC | VD_CAP_FILE | VD_CAP_VFS,
1975 /* papszFileExtensions */
1976 s_apszVciFileExtensions,
1977 /* paConfigInfo */
1978 NULL,
1979 /* pfnProbe */
1980 vciProbe,
1981 /* pfnOpen */
1982 vciOpen,
1983 /* pfnCreate */
1984 vciCreate,
1985 /* pfnClose */
1986 vciClose,
1987 /* pfnRead */
1988 vciRead,
1989 /* pfnWrite */
1990 vciWrite,
1991 /* pfnFlush */
1992 vciFlush,
1993 /* pfnDiscard */
1994 NULL,
1995 /* pfnGetVersion */
1996 vciGetVersion,
1997 /* pfnGetSize */
1998 vciGetSize,
1999 /* pfnGetFileSize */
2000 vciGetFileSize,
2001 /* pfnGetImageFlags */
2002 vciGetImageFlags,
2003 /* pfnGetOpenFlags */
2004 vciGetOpenFlags,
2005 /* pfnSetOpenFlags */
2006 vciSetOpenFlags,
2007 /* pfnGetComment */
2008 vciGetComment,
2009 /* pfnSetComment */
2010 vciSetComment,
2011 /* pfnGetUuid */
2012 vciGetUuid,
2013 /* pfnSetUuid */
2014 vciSetUuid,
2015 /* pfnGetModificationUuid */
2016 vciGetModificationUuid,
2017 /* pfnSetModificationUuid */
2018 vciSetModificationUuid,
2019 /* pfnDump */
2020 vciDump,
2021 /* pfnComposeLocation */
2022 NULL,
2023 /* pfnComposeName */
2024 NULL
2025};
2026
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