VirtualBox

source: vbox/trunk/src/VBox/Storage/Parallels.cpp@ 37739

Last change on this file since 37739 was 37739, checked in by vboxsync, 13 years ago

Parallels: Fix possible data corruption

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 46.0 KB
Line 
1/* $Id: Parallels.cpp 37739 2011-07-03 20:42:12Z vboxsync $ */
2/** @file
3 *
4 * Parallels hdd disk image, core code.
5 */
6
7/*
8 * Copyright (C) 2006-2010 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19#define LOG_GROUP LOG_GROUP_VD_PARALLELS
20#include <VBox/vd-plugin.h>
21#include <VBox/err.h>
22
23#include <VBox/log.h>
24#include <iprt/assert.h>
25#include <iprt/mem.h>
26#include <iprt/uuid.h>
27#include <iprt/path.h>
28#include <iprt/string.h>
29
30#define PARALLELS_HEADER_MAGIC "WithoutFreeSpace"
31#define PARALLELS_DISK_VERSION 2
32
33/** The header of the parallels disk. */
34#pragma pack(1)
35typedef struct ParallelsHeader
36{
37 /** The magic header to identify a parallels hdd image. */
38 char HeaderIdentifier[16];
39 /** The version of the disk image. */
40 uint32_t uVersion;
41 /** The number of heads the hdd has. */
42 uint32_t cHeads;
43 /** Number of cylinders. */
44 uint32_t cCylinders;
45 /** Number of sectors per track. */
46 uint32_t cSectorsPerTrack;
47 /** Number of entries in the allocation bitmap. */
48 uint32_t cEntriesInAllocationBitmap;
49 /** Total number of sectors. */
50 uint32_t cSectors;
51 /** Padding. */
52 char Padding[24];
53} ParallelsHeader;
54#pragma pack()
55
56/**
57 * Parallels image structure.
58 */
59typedef struct PARALLELSIMAGE
60{
61 /** Image file name. */
62 const char *pszFilename;
63 /** Opaque storage handle. */
64 PVDIOSTORAGE pStorage;
65
66 /** I/O interface. */
67 PVDINTERFACE pInterfaceIO;
68 /** I/O interface callbacks. */
69 PVDINTERFACEIOINT pInterfaceIOCallbacks;
70
71 /** Pointer to the per-disk VD interface list. */
72 PVDINTERFACE pVDIfsDisk;
73 /** Pointer to the per-image VD interface list. */
74 PVDINTERFACE pVDIfsImage;
75 /** Error interface. */
76 PVDINTERFACE pInterfaceError;
77 /** Error interface callbacks. */
78 PVDINTERFACEERROR pInterfaceErrorCallbacks;
79
80 /** Open flags passed by VBoxHDD layer. */
81 unsigned uOpenFlags;
82 /** Image flags defined during creation or determined during open. */
83 unsigned uImageFlags;
84 /** Total size of the image. */
85 uint64_t cbSize;
86
87 /** Physical geometry of this image. */
88 VDGEOMETRY PCHSGeometry;
89 /** Logical geometry of this image. */
90 VDGEOMETRY LCHSGeometry;
91
92 /** Pointer to the allocation bitmap. */
93 uint32_t *pAllocationBitmap;
94 /** Entries in the allocation bitmap. */
95 uint64_t cAllocationBitmapEntries;
96 /** Flag whether the allocation bitmap was changed. */
97 bool fAllocationBitmapChanged;
98 /** Current file size. */
99 uint64_t cbFileCurrent;
100} PARALLELSIMAGE, *PPARALLELSIMAGE;
101
102/*******************************************************************************
103* Static Variables *
104*******************************************************************************/
105
106/** NULL-terminated array of supported file extensions. */
107static const VDFILEEXTENSION s_aParallelsFileExtensions[] =
108{
109 {"hdd", VDTYPE_HDD},
110 {NULL, VDTYPE_INVALID}
111};
112
113/***************************************************
114 * Internal functions *
115 **************************************************/
116
117/**
118 * Internal: signal an error to the frontend.
119 */
120DECLINLINE(int) parallelsError(PPARALLELSIMAGE pImage, int rc, RT_SRC_POS_DECL,
121 const char *pszFormat, ...)
122{
123 va_list va;
124 va_start(va, pszFormat);
125 if (pImage->pInterfaceError && pImage->pInterfaceErrorCallbacks)
126 pImage->pInterfaceErrorCallbacks->pfnError(pImage->pInterfaceError->pvUser, rc, RT_SRC_POS_ARGS,
127 pszFormat, va);
128 va_end(va);
129 return rc;
130}
131
132/**
133 * Internal: signal an informational message to the frontend.
134 */
135DECLINLINE(int) parallelsMessage(PPARALLELSIMAGE pImage, const char *pszFormat, ...)
136{
137 int rc = VINF_SUCCESS;
138 va_list va;
139 va_start(va, pszFormat);
140 if (pImage->pInterfaceError && pImage->pInterfaceErrorCallbacks)
141 rc = pImage->pInterfaceErrorCallbacks->pfnMessage(pImage->pInterfaceError->pvUser,
142 pszFormat, va);
143 va_end(va);
144 return rc;
145}
146
147
148DECLINLINE(int) parallelsFileOpen(PPARALLELSIMAGE pImage, const char *pszFilename,
149 uint32_t fOpen)
150{
151 return pImage->pInterfaceIOCallbacks->pfnOpen(pImage->pInterfaceIO->pvUser,
152 pszFilename, fOpen,
153 &pImage->pStorage);
154}
155
156DECLINLINE(int) parallelsFileClose(PPARALLELSIMAGE pImage)
157{
158 return pImage->pInterfaceIOCallbacks->pfnClose(pImage->pInterfaceIO->pvUser,
159 pImage->pStorage);
160}
161
162DECLINLINE(int) parallelsFileDelete(PPARALLELSIMAGE pImage, const char *pszFilename)
163{
164 return pImage->pInterfaceIOCallbacks->pfnDelete(pImage->pInterfaceIO->pvUser,
165 pszFilename);
166}
167
168DECLINLINE(int) parallelsFileMove(PPARALLELSIMAGE pImage, const char *pszSrc,
169 const char *pszDst, unsigned fMove)
170{
171 return pImage->pInterfaceIOCallbacks->pfnMove(pImage->pInterfaceIO->pvUser,
172 pszSrc, pszDst, fMove);
173}
174
175DECLINLINE(int) parallelsFileGetSize(PPARALLELSIMAGE pImage, uint64_t *pcbSize)
176{
177 return pImage->pInterfaceIOCallbacks->pfnGetSize(pImage->pInterfaceIO->pvUser,
178 pImage->pStorage, pcbSize);
179}
180
181DECLINLINE(int) parallelsFileSetSize(PPARALLELSIMAGE pImage, uint64_t cbSize)
182{
183 return pImage->pInterfaceIOCallbacks->pfnSetSize(pImage->pInterfaceIO->pvUser,
184 pImage->pStorage, cbSize);
185}
186
187DECLINLINE(int) parallelsFileWriteSync(PPARALLELSIMAGE pImage, uint64_t uOffset,
188 const void *pvBuffer, size_t cbBuffer,
189 size_t *pcbWritten)
190{
191 return pImage->pInterfaceIOCallbacks->pfnWriteSync(pImage->pInterfaceIO->pvUser,
192 pImage->pStorage, uOffset,
193 pvBuffer, cbBuffer, pcbWritten);
194}
195
196DECLINLINE(int) parallelsFileReadSync(PPARALLELSIMAGE pImage, uint64_t uOffset,
197 void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
198{
199 return pImage->pInterfaceIOCallbacks->pfnReadSync(pImage->pInterfaceIO->pvUser,
200 pImage->pStorage, uOffset,
201 pvBuffer, cbBuffer, pcbRead);
202}
203
204DECLINLINE(int) parallelsFileFlushSync(PPARALLELSIMAGE pImage)
205{
206 return pImage->pInterfaceIOCallbacks->pfnFlushSync(pImage->pInterfaceIO->pvUser,
207 pImage->pStorage);
208}
209
210DECLINLINE(int) parallelsFileReadUserAsync(PPARALLELSIMAGE pImage, uint64_t uOffset,
211 PVDIOCTX pIoCtx, size_t cbRead)
212{
213 return pImage->pInterfaceIOCallbacks->pfnReadUserAsync(pImage->pInterfaceIO->pvUser,
214 pImage->pStorage,
215 uOffset, pIoCtx,
216 cbRead);
217}
218
219DECLINLINE(int) parallelsFileWriteUserAsync(PPARALLELSIMAGE pImage, uint64_t uOffset,
220 PVDIOCTX pIoCtx, size_t cbWrite,
221 PFNVDXFERCOMPLETED pfnComplete,
222 void *pvCompleteUser)
223{
224 return pImage->pInterfaceIOCallbacks->pfnWriteUserAsync(pImage->pInterfaceIO->pvUser,
225 pImage->pStorage,
226 uOffset, pIoCtx,
227 cbWrite,
228 pfnComplete,
229 pvCompleteUser);
230}
231
232DECLINLINE(int) parallelsFileWriteMetaAsync(PPARALLELSIMAGE pImage, uint64_t uOffset,
233 void *pvBuffer, size_t cbBuffer,
234 PVDIOCTX pIoCtx,
235 PFNVDXFERCOMPLETED pfnComplete,
236 void *pvCompleteUser)
237{
238 return pImage->pInterfaceIOCallbacks->pfnWriteMetaAsync(pImage->pInterfaceIO->pvUser,
239 pImage->pStorage,
240 uOffset, pvBuffer,
241 cbBuffer, pIoCtx,
242 pfnComplete,
243 pvCompleteUser);
244}
245
246DECLINLINE(int) parallelsFileFlushAsync(PPARALLELSIMAGE pImage, PVDIOCTX pIoCtx,
247 PFNVDXFERCOMPLETED pfnComplete,
248 void *pvCompleteUser)
249{
250 return pImage->pInterfaceIOCallbacks->pfnFlushAsync(pImage->pInterfaceIO->pvUser,
251 pImage->pStorage,
252 pIoCtx, pfnComplete,
253 pvCompleteUser);
254}
255
256
257/**
258 * Internal. Flush image data to disk.
259 */
260static int parallelsFlushImage(PPARALLELSIMAGE pImage)
261{
262 int rc = VINF_SUCCESS;
263
264 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
265 return VINF_SUCCESS;
266
267 if ( !(pImage->uImageFlags & VD_IMAGE_FLAGS_FIXED)
268 && (pImage->fAllocationBitmapChanged))
269 {
270 pImage->fAllocationBitmapChanged = false;
271 /* Write the allocation bitmap to the file. */
272 rc = parallelsFileWriteSync(pImage, sizeof(ParallelsHeader),
273 pImage->pAllocationBitmap,
274 pImage->cAllocationBitmapEntries * sizeof(uint32_t),
275 NULL);
276 if (RT_FAILURE(rc))
277 return rc;
278 }
279
280 /* Flush file. */
281 rc = parallelsFileFlushSync(pImage);
282
283 LogFlowFunc(("returns %Rrc\n", rc));
284 return rc;
285}
286
287/**
288 * Internal. Free all allocated space for representing an image except pImage,
289 * and optionally delete the image from disk.
290 */
291static int parallelsFreeImage(PPARALLELSIMAGE pImage, bool fDelete)
292{
293 int rc = VINF_SUCCESS;
294
295 /* Freeing a never allocated image (e.g. because the open failed) is
296 * not signalled as an error. After all nothing bad happens. */
297 if (pImage)
298 {
299 if (pImage->pStorage)
300 {
301 /* No point updating the file that is deleted anyway. */
302 if (!fDelete)
303 parallelsFlushImage(pImage);
304
305 parallelsFileClose(pImage);
306 pImage->pStorage = NULL;
307 }
308
309 if (pImage->pAllocationBitmap)
310 {
311 RTMemFree(pImage->pAllocationBitmap);
312 pImage->pAllocationBitmap = NULL;
313 }
314
315 if (fDelete && pImage->pszFilename)
316 parallelsFileDelete(pImage, pImage->pszFilename);
317 }
318
319 return rc;
320}
321
322static int parallelsOpenImage(PPARALLELSIMAGE pImage, unsigned uOpenFlags)
323{
324 int rc = VINF_SUCCESS;
325 ParallelsHeader parallelsHeader;
326
327 /* Try to get error interface. */
328 pImage->pInterfaceError = VDInterfaceGet(pImage->pVDIfsDisk, VDINTERFACETYPE_ERROR);
329 if (pImage->pInterfaceError)
330 pImage->pInterfaceErrorCallbacks = VDGetInterfaceError(pImage->pInterfaceError);
331
332 /* Get I/O interface. */
333 pImage->pInterfaceIO = VDInterfaceGet(pImage->pVDIfsImage, VDINTERFACETYPE_IOINT);
334 AssertPtrReturn(pImage->pInterfaceIO, VERR_INVALID_PARAMETER);
335 pImage->pInterfaceIOCallbacks = VDGetInterfaceIOInt(pImage->pInterfaceIO);
336 AssertPtrReturn(pImage->pInterfaceIOCallbacks, VERR_INVALID_PARAMETER);
337
338 rc = parallelsFileOpen(pImage, pImage->pszFilename,
339 VDOpenFlagsToFileOpenFlags(uOpenFlags,
340 false /* fCreate */));
341 if (RT_FAILURE(rc))
342 goto out;
343
344 rc = parallelsFileGetSize(pImage, &pImage->cbFileCurrent);
345 if (RT_FAILURE(rc))
346 goto out;
347 AssertMsg(pImage->cbFileCurrent % 512 == 0, ("File size is not a multiple of 512\n"));
348
349 rc = parallelsFileReadSync(pImage, 0, &parallelsHeader, sizeof(parallelsHeader), NULL);
350 if (RT_FAILURE(rc))
351 goto out;
352
353 if (memcmp(parallelsHeader.HeaderIdentifier, PARALLELS_HEADER_MAGIC, 16))
354 {
355 /* Check if the file has hdd as extension. It is a fixed size raw image then. */
356 char *pszExtension = RTPathExt(pImage->pszFilename);
357 if (strcmp(pszExtension, ".hdd"))
358 {
359 rc = VERR_VD_PARALLELS_INVALID_HEADER;
360 goto out;
361 }
362
363 /* This is a fixed size image. */
364 pImage->uImageFlags |= VD_IMAGE_FLAGS_FIXED;
365 pImage->cbSize = pImage->cbFileCurrent;
366
367 pImage->PCHSGeometry.cHeads = 16;
368 pImage->PCHSGeometry.cSectors = 63;
369 uint64_t cCylinders = pImage->cbSize / (512 * pImage->PCHSGeometry.cSectors * pImage->PCHSGeometry.cHeads);
370 pImage->PCHSGeometry.cCylinders = (uint32_t)cCylinders;
371 }
372 else
373 {
374 if (parallelsHeader.uVersion != PARALLELS_DISK_VERSION)
375 {
376 rc = VERR_NOT_SUPPORTED;
377 goto out;
378 }
379
380 if (parallelsHeader.cEntriesInAllocationBitmap > (1 << 30))
381 {
382 rc = VERR_NOT_SUPPORTED;
383 goto out;
384 }
385
386 Log(("cSectors=%u\n", parallelsHeader.cSectors));
387 pImage->cbSize = ((uint64_t)parallelsHeader.cSectors) * 512;
388 pImage->uImageFlags = VD_IMAGE_FLAGS_NONE;
389 pImage->cAllocationBitmapEntries = parallelsHeader.cEntriesInAllocationBitmap;
390 pImage->pAllocationBitmap = (uint32_t *)RTMemAllocZ((uint32_t)pImage->cAllocationBitmapEntries * sizeof(uint32_t));
391 if (!pImage->pAllocationBitmap)
392 {
393 rc = VERR_NO_MEMORY;
394 goto out;
395 }
396
397 rc = parallelsFileReadSync(pImage, sizeof(ParallelsHeader),
398 pImage->pAllocationBitmap,
399 pImage->cAllocationBitmapEntries * sizeof(uint32_t),
400 NULL);
401 if (RT_FAILURE(rc))
402 goto out;
403
404 pImage->PCHSGeometry.cCylinders = parallelsHeader.cCylinders;
405 pImage->PCHSGeometry.cHeads = parallelsHeader.cHeads;
406 pImage->PCHSGeometry.cSectors = parallelsHeader.cSectorsPerTrack;
407 }
408
409out:
410 LogFlowFunc(("returns %Rrc\n", rc));
411 return rc;
412}
413
414
415/** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
416static int parallelsCheckIfValid(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
417 PVDINTERFACE pVDIfsImage, VDTYPE *penmType)
418{
419 int rc;
420 PVDIOSTORAGE pStorage;
421 ParallelsHeader parallelsHeader;
422
423 /* Get I/O interface. */
424 PVDINTERFACE pInterfaceIO = VDInterfaceGet(pVDIfsImage, VDINTERFACETYPE_IOINT);
425 AssertPtrReturn(pInterfaceIO, VERR_INVALID_PARAMETER);
426 PVDINTERFACEIOINT pInterfaceIOCallbacks = VDGetInterfaceIOInt(pInterfaceIO);
427 AssertPtrReturn(pInterfaceIOCallbacks, VERR_INVALID_PARAMETER);
428
429 rc = pInterfaceIOCallbacks->pfnOpen(pInterfaceIO->pvUser, pszFilename,
430 VDOpenFlagsToFileOpenFlags(VD_OPEN_FLAGS_READONLY,
431 false /* fCreate */),
432 &pStorage);
433 if (RT_FAILURE(rc))
434 return rc;
435
436 rc = pInterfaceIOCallbacks->pfnReadSync(pInterfaceIO->pvUser, pStorage,
437 0, &parallelsHeader,
438 sizeof(ParallelsHeader), NULL);
439 if (RT_SUCCESS(rc))
440 {
441 if ( !memcmp(parallelsHeader.HeaderIdentifier, PARALLELS_HEADER_MAGIC, 16)
442 && (parallelsHeader.uVersion == PARALLELS_DISK_VERSION))
443 rc = VINF_SUCCESS;
444 else
445 {
446 /*
447 * The image may be an fixed size image.
448 * Unfortunately fixed sized parallels images
449 * are just raw files hence no magic header to
450 * check for.
451 * The code succeeds if the file is a multiple
452 * of 512 and if the file extensions is *.hdd
453 */
454 uint64_t cbFile;
455 char *pszExtension;
456
457 rc = pInterfaceIOCallbacks->pfnGetSize(pInterfaceIO->pvUser, pStorage,
458 &cbFile);
459 if (RT_FAILURE(rc) || ((cbFile % 512) != 0))
460 {
461 pInterfaceIOCallbacks->pfnClose(pInterfaceIO->pvUser, pStorage);
462 return VERR_VD_PARALLELS_INVALID_HEADER;
463 }
464
465 pszExtension = RTPathExt(pszFilename);
466 if (!pszExtension || strcmp(pszExtension, ".hdd"))
467 rc = VERR_VD_PARALLELS_INVALID_HEADER;
468 else
469 rc = VINF_SUCCESS;
470 }
471 }
472
473 if (RT_SUCCESS(rc))
474 *penmType = VDTYPE_HDD;
475
476 pInterfaceIOCallbacks->pfnClose(pInterfaceIO->pvUser, pStorage);
477 return rc;
478}
479
480/** @copydoc VBOXHDDBACKEND::pfnOpen */
481static int parallelsOpen(const char *pszFilename, unsigned uOpenFlags,
482 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
483 VDTYPE enmType, void **ppBackendData)
484{
485 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, ppBackendData));
486 int rc;
487 PPARALLELSIMAGE pImage;
488
489 /* Check open flags. All valid flags are supported. */
490 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
491 {
492 rc = VERR_INVALID_PARAMETER;
493 goto out;
494 }
495
496 /* Check remaining arguments. */
497 if ( !VALID_PTR(pszFilename)
498 || !*pszFilename)
499 {
500 rc = VERR_INVALID_PARAMETER;
501 goto out;
502 }
503
504 /** @todo r=klaus why this duplicate check, async is not claimed... */
505 if (uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO)
506 {
507 rc = VERR_NOT_SUPPORTED;
508 goto out;
509 }
510
511 pImage = (PPARALLELSIMAGE)RTMemAllocZ(sizeof(PARALLELSIMAGE));
512 if (!pImage)
513 {
514 rc = VERR_NO_MEMORY;
515 goto out;
516 }
517
518 pImage->pszFilename = pszFilename;
519 pImage->pStorage = NULL;
520 pImage->pVDIfsDisk = pVDIfsDisk;
521 pImage->pVDIfsImage = pVDIfsImage;
522 pImage->fAllocationBitmapChanged = false;
523
524 rc = parallelsOpenImage(pImage, uOpenFlags);
525 if (RT_SUCCESS(rc))
526 *ppBackendData = pImage;
527 else
528 RTMemFree(pImage);
529
530out:
531 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
532 return rc;
533}
534
535/** @copydoc VBOXHDDBACKEND::pfnCreate */
536static int parallelsCreate(const char *pszFilename, uint64_t cbSize,
537 unsigned uImageFlags, const char *pszComment,
538 PCVDGEOMETRY pPCHSGeometry,
539 PCVDGEOMETRY pLCHSGeometry, PCRTUUID pUuid,
540 unsigned uOpenFlags, unsigned uPercentStart,
541 unsigned uPercentSpan, PVDINTERFACE pVDIfsDisk,
542 PVDINTERFACE pVDIfsImage,
543 PVDINTERFACE pVDIfsOperation, void **ppBackendData)
544{
545 LogFlowFunc(("pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" pPCHSGeometry=%#p pLCHSGeometry=%#p Uuid=%RTuuid uOpenFlags=%#x uPercentStart=%u uPercentSpan=%u pVDIfsDisk=%#p pVDIfsImage=%#p pVDIfsOperation=%#p ppBackendData=%#p", pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, ppBackendData));
546 int rc = VERR_NOT_IMPLEMENTED;
547
548 LogFlowFunc(("returns %Rrc\n", rc));
549 return rc;
550}
551
552/** @copydoc VBOXHDDBACKEND::pfnRename */
553static int parallelsRename(void *pBackendData, const char *pszFilename)
554{
555 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
556 int rc = VINF_SUCCESS;
557 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
558
559 /* Check arguments. */
560 if ( !pImage
561 || !pszFilename
562 || !*pszFilename)
563 {
564 rc = VERR_INVALID_PARAMETER;
565 goto out;
566 }
567
568 /* Close the image. */
569 rc = parallelsFreeImage(pImage, false);
570 if (RT_FAILURE(rc))
571 goto out;
572
573 /* Rename the file. */
574 rc = parallelsFileMove(pImage, pImage->pszFilename, pszFilename, 0);
575 if (RT_FAILURE(rc))
576 {
577 /* The move failed, try to reopen the original image. */
578 int rc2 = parallelsOpenImage(pImage, pImage->uOpenFlags);
579 if (RT_FAILURE(rc2))
580 rc = rc2;
581
582 goto out;
583 }
584
585 /* Update pImage with the new information. */
586 pImage->pszFilename = pszFilename;
587
588 /* Open the old image with new name. */
589 rc = parallelsOpenImage(pImage, pImage->uOpenFlags);
590 if (RT_FAILURE(rc))
591 goto out;
592
593out:
594 LogFlowFunc(("returns %Rrc\n", rc));
595 return rc;
596}
597
598/** @copydoc VBOXHDDBACKEND::pfnClose */
599static int parallelsClose(void *pBackendData, bool fDelete)
600{
601 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
602 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
603 int rc;
604
605 rc = parallelsFreeImage(pImage, fDelete);
606 RTMemFree(pImage);
607
608 LogFlowFunc(("returns %Rrc\n", rc));
609 return rc;
610}
611
612/** @copydoc VBOXHDDBACKEND::pfnRead */
613static int parallelsRead(void *pBackendData, uint64_t uOffset, void *pvBuf,
614 size_t cbBuf, size_t *pcbActuallyRead)
615{
616 LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbBuf=%zu pcbActuallyRead=%#p\n", pBackendData, uOffset, pvBuf, cbBuf, pcbActuallyRead));
617 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
618 int rc = VINF_SUCCESS;
619 uint64_t uSector;
620 uint64_t uOffsetInFile;
621 uint32_t iIndexInAllocationTable;
622
623 AssertPtr(pImage);
624 Assert(uOffset % 512 == 0);
625 Assert(cbBuf % 512 == 0);
626
627 if (pImage->uImageFlags & VD_IMAGE_FLAGS_FIXED)
628 {
629 rc = parallelsFileReadSync(pImage, uOffset, pvBuf, cbBuf, NULL);
630 }
631 else
632 {
633 /* Calculate offset in the real file. */
634 uSector = uOffset / 512;
635 /* One chunk in the file is always one track big. */
636 iIndexInAllocationTable = (uint32_t)(uSector / pImage->PCHSGeometry.cSectors);
637 uSector = uSector % pImage->PCHSGeometry.cSectors;
638
639 cbBuf = RT_MIN(cbBuf, (pImage->PCHSGeometry.cSectors - uSector)*512);
640
641 if (pImage->pAllocationBitmap[iIndexInAllocationTable] == 0)
642 {
643 rc = VERR_VD_BLOCK_FREE;
644 }
645 else
646 {
647 uOffsetInFile = (pImage->pAllocationBitmap[iIndexInAllocationTable] + uSector) * 512;
648 rc = parallelsFileReadSync(pImage, uOffsetInFile, pvBuf, cbBuf, NULL);
649 }
650 }
651
652 if ( RT_SUCCESS(rc)
653 || rc == VERR_VD_BLOCK_FREE)
654 {
655 if (pcbActuallyRead)
656 *pcbActuallyRead = cbBuf;
657
658 Log2(("parallelsRead: off=%#llx pvBuf=%p cbBuf=%d\n"
659 "%.*Rhxd\n",
660 uOffset, pvBuf, cbBuf, cbBuf, pvBuf));
661 }
662
663 LogFlowFunc(("returns %Rrc\n", rc));
664 return rc;
665}
666
667/** @copydoc VBOXHDDBACKEND::pfnWrite */
668static int parallelsWrite(void *pBackendData, uint64_t uOffset, const void *pvBuf,
669 size_t cbBuf, size_t *pcbWriteProcess,
670 size_t *pcbPreRead, size_t *pcbPostRead, unsigned fWrite)
671{
672 LogFlowFunc(("pBackendData=%#p uOffset=%llu pvBuf=%#p cbBuf=%zu pcbWriteProcess=%#p\n", pBackendData, uOffset, pvBuf, cbBuf, pcbWriteProcess));
673 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
674 int rc = VINF_SUCCESS;
675 uint64_t uSector;
676 uint64_t uOffsetInFile;
677 uint32_t iIndexInAllocationTable;
678
679 AssertPtr(pImage);
680 Assert(uOffset % 512 == 0);
681 Assert(cbBuf % 512 == 0);
682
683 if (pImage->uImageFlags & VD_IMAGE_FLAGS_FIXED)
684 {
685 rc = parallelsFileWriteSync(pImage, uOffset, pvBuf, cbBuf, NULL);
686 }
687 else
688 {
689 /** Calculate offset in the real file. */
690 uSector = uOffset / 512;
691 /** One chunk in the file is always one track big. */
692 iIndexInAllocationTable = (uint32_t)(uSector / pImage->PCHSGeometry.cSectors);
693 uSector = uSector % pImage->PCHSGeometry.cSectors;
694
695 cbBuf = RT_MIN(cbBuf, (pImage->PCHSGeometry.cSectors - uSector)*512);
696
697 if (pImage->pAllocationBitmap[iIndexInAllocationTable] == 0)
698 {
699 /* Allocate new chunk in the file. */
700 AssertMsg(pImage->cbFileCurrent % 512 == 0, ("File size is not a multiple of 512\n"));
701 pImage->pAllocationBitmap[iIndexInAllocationTable] = (uint32_t)(pImage->cbFileCurrent / 512);
702 pImage->cbFileCurrent += pImage->PCHSGeometry.cSectors * 512;
703 pImage->fAllocationBitmapChanged = true;
704
705 uint8_t *pNewBlock = (uint8_t *)RTMemAllocZ(pImage->PCHSGeometry.cSectors * 512);
706
707 if (!pNewBlock)
708 {
709 rc = VERR_NO_MEMORY;
710 goto out;
711 }
712
713 uOffsetInFile = (uint64_t)pImage->pAllocationBitmap[iIndexInAllocationTable] * 512;
714 memcpy(pNewBlock + (uOffset - ((uint64_t)iIndexInAllocationTable * pImage->PCHSGeometry.cSectors * 512)),
715 pvBuf, cbBuf);
716
717 /*
718 * Write the new block at the current end of the file.
719 */
720 rc = parallelsFileWriteSync(pImage, uOffsetInFile, pNewBlock,
721 pImage->PCHSGeometry.cSectors * 512,
722 NULL);
723
724 RTMemFree(pNewBlock);
725 }
726 else
727 {
728 uOffsetInFile = (pImage->pAllocationBitmap[iIndexInAllocationTable] + uSector) * 512;
729 rc = parallelsFileWriteSync(pImage, uOffsetInFile, pvBuf, cbBuf, NULL);
730 }
731 }
732
733 if (pcbWriteProcess)
734 *pcbWriteProcess = cbBuf;
735
736 /* Stay on the safe side. Do not run the risk of confusing the higher
737 * level, as that can be pretty lethal to image consistency. */
738 *pcbPreRead = 0;
739 *pcbPostRead = 0;
740
741out:
742 LogFlowFunc(("returns %Rrc\n", rc));
743 return rc;
744}
745
746/** @copydoc VBOXHDDBACKEND::pfnFlush */
747static int parallelsFlush(void *pBackendData)
748{
749 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
750 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
751 int rc;
752
753 AssertPtr(pImage);
754
755 rc = parallelsFlushImage(pImage);
756
757 LogFlowFunc(("returns %Rrc\n", rc));
758 return rc;
759}
760
761/** @copydoc VBOXHDDBACKEND::pfnGetVersion */
762static unsigned parallelsGetVersion(void *pBackendData)
763{
764 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
765 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
766
767 AssertPtr(pImage);
768
769 if (pImage)
770 return PARALLELS_DISK_VERSION;
771 else
772 return 0;
773}
774
775/** @copydoc VBOXHDDBACKEND::pfnGetSize */
776static uint64_t parallelsGetSize(void *pBackendData)
777{
778 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
779 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
780 uint64_t cb = 0;
781
782 AssertPtr(pImage);
783
784 if (pImage && pImage->pStorage)
785 cb = pImage->cbSize;
786
787 LogFlowFunc(("returns %llu\n", cb));
788 return cb;
789}
790
791/** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
792static uint64_t parallelsGetFileSize(void *pBackendData)
793{
794 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
795 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
796 uint64_t cb = 0;
797
798 AssertPtr(pImage);
799
800 if (pImage && pImage->pStorage)
801 cb = pImage->cbFileCurrent;
802
803 LogFlowFunc(("returns %lld\n", cb));
804 return cb;
805}
806
807/** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
808static int parallelsGetPCHSGeometry(void *pBackendData,
809 PVDGEOMETRY pPCHSGeometry)
810{
811 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
812 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
813 int rc;
814
815 AssertPtr(pImage);
816
817 if (pImage)
818 {
819 if (pImage->PCHSGeometry.cCylinders)
820 {
821 *pPCHSGeometry = pImage->PCHSGeometry;
822 rc = VINF_SUCCESS;
823 }
824 else
825 rc = VERR_VD_GEOMETRY_NOT_SET;
826 }
827 else
828 rc = VERR_VD_NOT_OPENED;
829
830 LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
831 return rc;
832}
833
834/** @copydoc VBOXHDDBACKEND::pfnSetPCHSGeometry */
835static int parallelsSetPCHSGeometry(void *pBackendData,
836 PCVDGEOMETRY pPCHSGeometry)
837{
838 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
839 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
840 int rc;
841
842 AssertPtr(pImage);
843
844 if (pImage)
845 {
846 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
847 {
848 rc = VERR_VD_IMAGE_READ_ONLY;
849 goto out;
850 }
851
852 pImage->PCHSGeometry = *pPCHSGeometry;
853 rc = VINF_SUCCESS;
854 }
855 else
856 rc = VERR_VD_NOT_OPENED;
857
858out:
859 LogFlowFunc(("returns %Rrc\n", rc));
860 return rc;
861}
862
863/** @copydoc VBOXHDDBACKEND::pfnGetLCHSGeometry */
864static int parallelsGetLCHSGeometry(void *pBackendData,
865 PVDGEOMETRY pLCHSGeometry)
866{
867 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
868 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
869 int rc;
870
871 AssertPtr(pImage);
872
873 if (pImage)
874 {
875 if (pImage->LCHSGeometry.cCylinders)
876 {
877 *pLCHSGeometry = pImage->LCHSGeometry;
878 rc = VINF_SUCCESS;
879 }
880 else
881 rc = VERR_VD_GEOMETRY_NOT_SET;
882 }
883 else
884 rc = VERR_VD_NOT_OPENED;
885
886 LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
887 return rc;
888}
889
890/** @copydoc VBOXHDDBACKEND::pfnSetLCHSGeometry */
891static int parallelsSetLCHSGeometry(void *pBackendData,
892 PCVDGEOMETRY pLCHSGeometry)
893{
894 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
895 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
896 int rc;
897
898 AssertPtr(pImage);
899
900 if (pImage)
901 {
902 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
903 {
904 rc = VERR_VD_IMAGE_READ_ONLY;
905 goto out;
906 }
907
908 pImage->LCHSGeometry = *pLCHSGeometry;
909 rc = VINF_SUCCESS;
910 }
911 else
912 rc = VERR_VD_NOT_OPENED;
913
914out:
915 LogFlowFunc(("returns %Rrc\n", rc));
916 return rc;
917}
918
919/** @copydoc VBOXHDDBACKEND::pfnGetImageFlags */
920static unsigned parallelsGetImageFlags(void *pBackendData)
921{
922 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
923 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
924 unsigned uImageFlags;
925
926 AssertPtr(pImage);
927
928 if (pImage)
929 uImageFlags = pImage->uImageFlags;
930 else
931 uImageFlags = 0;
932
933 LogFlowFunc(("returns %#x\n", uImageFlags));
934 return uImageFlags;
935}
936
937/** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
938static unsigned parallelsGetOpenFlags(void *pBackendData)
939{
940 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
941 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
942 unsigned uOpenFlags;
943
944 AssertPtr(pImage);
945
946 if (pImage)
947 uOpenFlags = pImage->uOpenFlags;
948 else
949 uOpenFlags = 0;
950
951 LogFlowFunc(("returns %#x\n", uOpenFlags));
952 return uOpenFlags;
953}
954
955/** @copydoc VBOXHDDBACKEND::pfnSetOpenFlags */
956static int parallelsSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
957{
958 LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
959 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
960 int rc;
961
962 /* Image must be opened and the new flags must be valid. */
963 /** @todo r=klaus add VD_OPEN_FLAGS_ASYNC_IO when async io has been tested */
964 if (!pImage || (uOpenFlags & ~(VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_SHAREABLE | VD_OPEN_FLAGS_SEQUENTIAL)))
965 {
966 rc = VERR_INVALID_PARAMETER;
967 goto out;
968 }
969
970 /* Implement this operation via reopening the image. */
971 parallelsFreeImage(pImage, false);
972 rc = parallelsOpenImage(pImage, uOpenFlags);
973
974out:
975 LogFlowFunc(("returns %Rrc\n", rc));
976 return rc;
977}
978
979/** @copydoc VBOXHDDBACKEND::pfnGetComment */
980static int parallelsGetComment(void *pBackendData, char *pszComment,
981 size_t cbComment)
982{
983 LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
984 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
985 int rc;
986
987 AssertPtr(pImage);
988
989 if (pImage)
990 rc = VERR_NOT_SUPPORTED;
991 else
992 rc = VERR_VD_NOT_OPENED;
993
994 LogFlowFunc(("returns %Rrc comment='%s'\n", rc, pszComment));
995 return rc;
996}
997
998/** @copydoc VBOXHDDBACKEND::pfnSetComment */
999static int parallelsSetComment(void *pBackendData, const char *pszComment)
1000{
1001 LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
1002 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
1003 int rc;
1004
1005 AssertPtr(pImage);
1006
1007 if (pImage)
1008 {
1009 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1010 rc = VERR_VD_IMAGE_READ_ONLY;
1011 else
1012 rc = VERR_NOT_SUPPORTED;
1013 }
1014 else
1015 rc = VERR_VD_NOT_OPENED;
1016
1017 LogFlowFunc(("returns %Rrc\n", rc));
1018 return rc;
1019}
1020
1021/** @copydoc VBOXHDDBACKEND::pfnGetUuid */
1022static int parallelsGetUuid(void *pBackendData, PRTUUID pUuid)
1023{
1024 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1025 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
1026 int rc;
1027
1028 AssertPtr(pImage);
1029
1030 if (pImage)
1031 rc = VERR_NOT_SUPPORTED;
1032 else
1033 rc = VERR_VD_NOT_OPENED;
1034
1035 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
1036 return rc;
1037}
1038
1039/** @copydoc VBOXHDDBACKEND::pfnSetUuid */
1040static int parallelsSetUuid(void *pBackendData, PCRTUUID pUuid)
1041{
1042 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
1043 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
1044 int rc;
1045
1046 AssertPtr(pImage);
1047
1048 if (pImage)
1049 {
1050 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1051 rc = VERR_NOT_SUPPORTED;
1052 else
1053 rc = VERR_VD_IMAGE_READ_ONLY;
1054 }
1055 else
1056 rc = VERR_VD_NOT_OPENED;
1057
1058 LogFlowFunc(("returns %Rrc\n", rc));
1059 return rc;
1060}
1061
1062/** @copydoc VBOXHDDBACKEND::pfnGetModificationUuid */
1063static int parallelsGetModificationUuid(void *pBackendData, PRTUUID pUuid)
1064{
1065 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1066 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
1067 int rc;
1068
1069 AssertPtr(pImage);
1070
1071 if (pImage)
1072 rc = VERR_NOT_SUPPORTED;
1073 else
1074 rc = VERR_VD_NOT_OPENED;
1075
1076 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
1077 return rc;
1078}
1079
1080/** @copydoc VBOXHDDBACKEND::pfnSetModificationUuid */
1081static int parallelsSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
1082{
1083 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
1084 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
1085 int rc;
1086
1087 AssertPtr(pImage);
1088
1089 if (pImage)
1090 {
1091 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1092 rc = VERR_NOT_SUPPORTED;
1093 else
1094 rc = VERR_VD_IMAGE_READ_ONLY;
1095 }
1096 else
1097 rc = VERR_VD_NOT_OPENED;
1098
1099 LogFlowFunc(("returns %Rrc\n", rc));
1100 return rc;
1101}
1102
1103/** @copydoc VBOXHDDBACKEND::pfnGetParentUuid */
1104static int parallelsGetParentUuid(void *pBackendData, PRTUUID pUuid)
1105{
1106 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1107 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
1108 int rc;
1109
1110 AssertPtr(pImage);
1111
1112 if (pImage)
1113 rc = VERR_NOT_SUPPORTED;
1114 else
1115 rc = VERR_VD_NOT_OPENED;
1116
1117 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
1118 return rc;
1119}
1120
1121/** @copydoc VBOXHDDBACKEND::pfnSetParentUuid */
1122static int parallelsSetParentUuid(void *pBackendData, PCRTUUID pUuid)
1123{
1124 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
1125 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
1126 int rc;
1127
1128 AssertPtr(pImage);
1129
1130 if (pImage)
1131 {
1132 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1133 rc = VERR_NOT_SUPPORTED;
1134 else
1135 rc = VERR_VD_IMAGE_READ_ONLY;
1136 }
1137 else
1138 rc = VERR_VD_NOT_OPENED;
1139
1140 LogFlowFunc(("returns %Rrc\n", rc));
1141 return rc;
1142}
1143
1144/** @copydoc VBOXHDDBACKEND::pfnGetParentModificationUuid */
1145static int parallelsGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
1146{
1147 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1148 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
1149 int rc;
1150
1151 AssertPtr(pImage);
1152
1153 if (pImage)
1154 rc = VERR_NOT_SUPPORTED;
1155 else
1156 rc = VERR_VD_NOT_OPENED;
1157
1158 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
1159 return rc;
1160}
1161
1162/** @copydoc VBOXHDDBACKEND::pfnSetParentModificationUuid */
1163static int parallelsSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
1164{
1165 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
1166 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
1167 int rc;
1168
1169 AssertPtr(pImage);
1170
1171 if (pImage)
1172 {
1173 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1174 rc = VERR_NOT_SUPPORTED;
1175 else
1176 rc = VERR_VD_IMAGE_READ_ONLY;
1177 }
1178 else
1179 rc = VERR_VD_NOT_OPENED;
1180
1181 LogFlowFunc(("returns %Rrc\n", rc));
1182 return rc;
1183}
1184
1185/** @copydoc VBOXHDDBACKEND::pfnDump */
1186static void parallelsDump(void *pBackendData)
1187{
1188 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
1189
1190 AssertPtr(pImage);
1191 if (pImage)
1192 {
1193 parallelsMessage(pImage, "Header: Geometry PCHS=%u/%u/%u LCHS=%u/%u/%u\n",
1194 pImage->PCHSGeometry.cCylinders, pImage->PCHSGeometry.cHeads, pImage->PCHSGeometry.cSectors,
1195 pImage->LCHSGeometry.cCylinders, pImage->LCHSGeometry.cHeads, pImage->LCHSGeometry.cSectors);
1196 }
1197}
1198
1199/** @copydoc VBOXHDDBACKEND::pfnAsyncRead */
1200static int parallelsAsyncRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
1201 PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
1202{
1203 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
1204 pBackendData, uOffset, pIoCtx, cbToRead, pcbActuallyRead));
1205 int rc = VINF_SUCCESS;
1206 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
1207 uint64_t uSector;
1208 uint64_t uOffsetInFile;
1209 uint32_t iIndexInAllocationTable;
1210
1211 AssertPtr(pImage);
1212 Assert(uOffset % 512 == 0);
1213 Assert(cbToRead % 512 == 0);
1214
1215 if (pImage->uImageFlags & VD_IMAGE_FLAGS_FIXED)
1216 {
1217 rc = parallelsFileReadUserAsync(pImage, uOffset, pIoCtx, cbToRead);
1218 }
1219 else
1220 {
1221 /* Calculate offset in the real file. */
1222 uSector = uOffset / 512;
1223 /* One chunk in the file is always one track big. */
1224 iIndexInAllocationTable = (uint32_t)(uSector / pImage->PCHSGeometry.cSectors);
1225 uSector = uSector % pImage->PCHSGeometry.cSectors;
1226
1227 cbToRead = RT_MIN(cbToRead, (pImage->PCHSGeometry.cSectors - uSector)*512);
1228
1229 if (pImage->pAllocationBitmap[iIndexInAllocationTable] == 0)
1230 {
1231 rc = VERR_VD_BLOCK_FREE;
1232 }
1233 else
1234 {
1235 uOffsetInFile = (pImage->pAllocationBitmap[iIndexInAllocationTable] + uSector) * 512;
1236 rc = parallelsFileReadUserAsync(pImage, uOffsetInFile, pIoCtx, cbToRead);
1237 }
1238 }
1239
1240 *pcbActuallyRead = cbToRead;
1241
1242 LogFlowFunc(("returns %Rrc\n", rc));
1243 return rc;
1244}
1245
1246/** @copydoc VBOXHDDBACKEND::pfnAsyncWrite */
1247static int parallelsAsyncWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
1248 PVDIOCTX pIoCtx,
1249 size_t *pcbWriteProcess, size_t *pcbPreRead,
1250 size_t *pcbPostRead, unsigned fWrite)
1251{
1252 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p\n",
1253 pBackendData, uOffset, pIoCtx, cbToWrite, pcbWriteProcess));
1254 int rc = VINF_SUCCESS;
1255 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
1256 uint64_t uSector;
1257 uint64_t uOffsetInFile;
1258 uint32_t iIndexInAllocationTable;
1259
1260 AssertPtr(pImage);
1261 Assert(uOffset % 512 == 0);
1262 Assert(cbToWrite % 512 == 0);
1263
1264 if (pImage->uImageFlags & VD_IMAGE_FLAGS_FIXED)
1265 {
1266 rc = parallelsFileWriteUserAsync(pImage, uOffset, pIoCtx, cbToWrite, NULL, NULL);
1267 }
1268 else
1269 {
1270 /* Calculate offset in the real file. */
1271 uSector = uOffset / 512;
1272 /* One chunk in the file is always one track big. */
1273 iIndexInAllocationTable = (uint32_t)(uSector / pImage->PCHSGeometry.cSectors);
1274 uSector = uSector % pImage->PCHSGeometry.cSectors;
1275
1276 cbToWrite = RT_MIN(cbToWrite, (pImage->PCHSGeometry.cSectors - uSector)*512);
1277
1278 if (pImage->pAllocationBitmap[iIndexInAllocationTable] == 0)
1279 {
1280 if (fWrite & VD_WRITE_NO_ALLOC)
1281 {
1282 *pcbPreRead = uSector * 512;
1283 *pcbPostRead = pImage->PCHSGeometry.cSectors * 512 - cbToWrite - *pcbPreRead;
1284
1285 if (pcbWriteProcess)
1286 *pcbWriteProcess = cbToWrite;
1287 return VERR_VD_BLOCK_FREE;
1288 }
1289
1290 /* Allocate new chunk in the file. */
1291 Assert(uSector == 0);
1292 AssertMsg(pImage->cbFileCurrent % 512 == 0, ("File size is not a multiple of 512\n"));
1293 pImage->pAllocationBitmap[iIndexInAllocationTable] = (uint32_t)(pImage->cbFileCurrent / 512);
1294 pImage->cbFileCurrent += pImage->PCHSGeometry.cSectors * 512;
1295 pImage->fAllocationBitmapChanged = true;
1296 uOffsetInFile = (uint64_t)pImage->pAllocationBitmap[iIndexInAllocationTable] * 512;
1297
1298 /*
1299 * Write the new block at the current end of the file.
1300 */
1301 rc = parallelsFileWriteUserAsync(pImage, uOffsetInFile, pIoCtx, cbToWrite, NULL, NULL);
1302 if (RT_SUCCESS(rc) || (rc == VERR_VD_ASYNC_IO_IN_PROGRESS))
1303 {
1304 /* Write the changed allocation bitmap entry. */
1305 /** @todo: Error handling. */
1306 rc = parallelsFileWriteMetaAsync(pImage,
1307 sizeof(ParallelsHeader) + iIndexInAllocationTable * sizeof(uint32_t),
1308 &pImage->pAllocationBitmap[iIndexInAllocationTable],
1309 sizeof(uint32_t), pIoCtx,
1310 NULL, NULL);
1311 }
1312 }
1313 else
1314 {
1315 uOffsetInFile = (pImage->pAllocationBitmap[iIndexInAllocationTable] + uSector) * 512;
1316 rc = parallelsFileWriteUserAsync(pImage, uOffsetInFile, pIoCtx, cbToWrite, NULL, NULL);
1317 }
1318 }
1319
1320 if (pcbWriteProcess)
1321 *pcbWriteProcess = cbToWrite;
1322
1323 LogFlowFunc(("returns %Rrc\n", rc));
1324 return rc;
1325}
1326
1327/** @copydoc VBOXHDDBACKEND::pfnAsyncFlush */
1328static int parallelsAsyncFlush(void *pBackendData, PVDIOCTX pIoCtx)
1329{
1330 int rc = VINF_SUCCESS;
1331 PPARALLELSIMAGE pImage = (PPARALLELSIMAGE)pBackendData;
1332
1333 LogFlowFunc(("pImage=#%p\n", pImage));
1334
1335 /* Flush the file, everything is up to date already. */
1336 rc = parallelsFileFlushAsync(pImage, pIoCtx, NULL, NULL);
1337
1338 LogFlowFunc(("returns %Rrc\n", rc));
1339 return rc;
1340}
1341
1342
1343VBOXHDDBACKEND g_ParallelsBackend =
1344{
1345 /* pszBackendName */
1346 "Parallels",
1347 /* cbSize */
1348 sizeof(VBOXHDDBACKEND),
1349 /* uBackendCaps */
1350 VD_CAP_FILE | VD_CAP_ASYNC | VD_CAP_VFS,
1351 /* paFileExtensions */
1352 s_aParallelsFileExtensions,
1353 /* paConfigInfo */
1354 NULL,
1355 /* hPlugin */
1356 NIL_RTLDRMOD,
1357 /* pfnCheckIfValid */
1358 parallelsCheckIfValid,
1359 /* pfnOpen */
1360 parallelsOpen,
1361 /* pfnCreate */
1362 parallelsCreate,
1363 /* pfnRename */
1364 parallelsRename,
1365 /* pfnClose */
1366 parallelsClose,
1367 /* pfnRead */
1368 parallelsRead,
1369 /* pfnWrite */
1370 parallelsWrite,
1371 /* pfnFlush */
1372 parallelsFlush,
1373 /* pfnGetVersion */
1374 parallelsGetVersion,
1375 /* pfnGetSize */
1376 parallelsGetSize,
1377 /* pfnGetFileSize */
1378 parallelsGetFileSize,
1379 /* pfnGetPCHSGeometry */
1380 parallelsGetPCHSGeometry,
1381 /* pfnSetPCHSGeometry */
1382 parallelsSetPCHSGeometry,
1383 /* pfnGetLCHSGeometry */
1384 parallelsGetLCHSGeometry,
1385 /* pfnSetLCHSGeometry */
1386 parallelsSetLCHSGeometry,
1387 /* pfnGetImageFlags */
1388 parallelsGetImageFlags,
1389 /* pfnGetOpenFlags */
1390 parallelsGetOpenFlags,
1391 /* pfnSetOpenFlags */
1392 parallelsSetOpenFlags,
1393 /* pfnGetComment */
1394 parallelsGetComment,
1395 /* pfnSetComment */
1396 parallelsSetComment,
1397 /* pfnGetUuid */
1398 parallelsGetUuid,
1399 /* pfnSetUuid */
1400 parallelsSetUuid,
1401 /* pfnGetModificationUuid */
1402 parallelsGetModificationUuid,
1403 /* pfnSetModificationUuid */
1404 parallelsSetModificationUuid,
1405 /* pfnGetParentUuid */
1406 parallelsGetParentUuid,
1407 /* pfnSetParentUuid */
1408 parallelsSetParentUuid,
1409 /* pfnGetParentModificationUuid */
1410 parallelsGetParentModificationUuid,
1411 /* pfnSetParentModificationUuid */
1412 parallelsSetParentModificationUuid,
1413 /* pfnDump */
1414 parallelsDump,
1415 /* pfnGetTimeStamp */
1416 NULL,
1417 /* pfnGetParentTimeStamp */
1418 NULL,
1419 /* pfnSetParentTimeStamp */
1420 NULL,
1421 /* pfnGetParentFilename */
1422 NULL,
1423 /* pfnSetParentFilename */
1424 NULL,
1425 /* pfnAsyncRead */
1426 parallelsAsyncRead,
1427 /* pfnAsyncWrite */
1428 parallelsAsyncWrite,
1429 /* pfnAsyncFlush */
1430 parallelsAsyncFlush,
1431 /* pfnComposeLocation */
1432 genericFileComposeLocation,
1433 /* pfnComposeName */
1434 genericFileComposeName,
1435 /* pfnCompact */
1436 NULL,
1437 /* pfnResize */
1438 NULL
1439};
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