VirtualBox

source: vbox/trunk/src/VBox/Storage/RAW.cpp@ 57388

Last change on this file since 57388 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: 36.6 KB
Line 
1/* $Id: RAW.cpp 57388 2015-08-17 14:20:55Z vboxsync $ */
2/** @file
3 * RawHDDCore - Raw Disk image, Core Code.
4 */
5
6/*
7 * Copyright (C) 2006-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_VD_RAW
23#include <VBox/vd-plugin.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/path.h>
30
31#include "VDBackends.h"
32
33
34/*********************************************************************************************************************************
35* Constants And Macros, Structures and Typedefs *
36*********************************************************************************************************************************/
37
38/**
39 * Raw image data structure.
40 */
41typedef struct RAWIMAGE
42{
43 /** Image name. */
44 const char *pszFilename;
45 /** Storage handle. */
46 PVDIOSTORAGE pStorage;
47
48 /** Pointer to the per-disk VD interface list. */
49 PVDINTERFACE pVDIfsDisk;
50 /** Pointer to the per-image VD interface list. */
51 PVDINTERFACE pVDIfsImage;
52 /** Error interface. */
53 PVDINTERFACEERROR pIfError;
54 /** I/O interface. */
55 PVDINTERFACEIOINT pIfIo;
56
57 /** Open flags passed by VBoxHD layer. */
58 unsigned uOpenFlags;
59 /** Image flags defined during creation or determined during open. */
60 unsigned uImageFlags;
61 /** Total size of the image. */
62 uint64_t cbSize;
63 /** Position in the image (only truly used for sequential access). */
64 uint64_t offAccess;
65 /** Flag if this is a newly created image. */
66 bool fCreate;
67 /** Physical geometry of this image. */
68 VDGEOMETRY PCHSGeometry;
69 /** Logical geometry of this image. */
70 VDGEOMETRY LCHSGeometry;
71 /** Sector size of the image. */
72 uint32_t cbSector;
73} RAWIMAGE, *PRAWIMAGE;
74
75
76/** Size of write operations when filling an image with zeroes. */
77#define RAW_FILL_SIZE (128 * _1K)
78
79/** The maximum reasonable size of a floppy image (big format 2.88MB medium). */
80#define RAW_MAX_FLOPPY_IMG_SIZE (512 * 82 * 48 * 2)
81
82
83/*********************************************************************************************************************************
84* Static Variables *
85*********************************************************************************************************************************/
86
87/** NULL-terminated array of supported file extensions. */
88static const VDFILEEXTENSION s_aRawFileExtensions[] =
89{
90 {"iso", VDTYPE_DVD},
91 {"cdr", VDTYPE_DVD},
92 {"img", VDTYPE_FLOPPY},
93 {"ima", VDTYPE_FLOPPY},
94 {"dsk", VDTYPE_FLOPPY},
95 {"flp", VDTYPE_FLOPPY},
96 {"vfd", VDTYPE_FLOPPY},
97 {NULL, VDTYPE_INVALID}
98};
99
100
101/*********************************************************************************************************************************
102* Internal Functions *
103*********************************************************************************************************************************/
104
105/**
106 * Internal. Flush image data to disk.
107 */
108static int rawFlushImage(PRAWIMAGE pImage)
109{
110 int rc = VINF_SUCCESS;
111
112 if ( pImage->pStorage
113 && !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
114 rc = vdIfIoIntFileFlushSync(pImage->pIfIo, pImage->pStorage);
115
116 return rc;
117}
118
119/**
120 * Internal. Free all allocated space for representing an image except pImage,
121 * and optionally delete the image from disk.
122 */
123static int rawFreeImage(PRAWIMAGE pImage, bool fDelete)
124{
125 int rc = VINF_SUCCESS;
126
127 /* Freeing a never allocated image (e.g. because the open failed) is
128 * not signalled as an error. After all nothing bad happens. */
129 if (pImage)
130 {
131 if (pImage->pStorage)
132 {
133 /* No point updating the file that is deleted anyway. */
134 if (!fDelete)
135 {
136 /* For newly created images in sequential mode fill it to
137 * the nominal size. */
138 if ( pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL
139 && !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
140 && pImage->fCreate)
141 {
142 /* Fill rest of image with zeroes, a must for sequential
143 * images to reach the nominal size. */
144 uint64_t uOff;
145 void *pvBuf = RTMemTmpAllocZ(RAW_FILL_SIZE);
146 if (!pvBuf)
147 goto out;
148
149 uOff = pImage->offAccess;
150 /* Write data to all image blocks. */
151 while (uOff < pImage->cbSize)
152 {
153 unsigned cbChunk = (unsigned)RT_MIN(pImage->cbSize,
154 RAW_FILL_SIZE);
155
156 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage,
157 uOff, pvBuf, cbChunk);
158 if (RT_FAILURE(rc))
159 goto out;
160
161 uOff += cbChunk;
162 }
163out:
164 if (pvBuf)
165 RTMemTmpFree(pvBuf);
166 }
167 rawFlushImage(pImage);
168 }
169
170 rc = vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
171 pImage->pStorage = NULL;
172 }
173
174 if (fDelete && pImage->pszFilename)
175 vdIfIoIntFileDelete(pImage->pIfIo, pImage->pszFilename);
176 }
177
178 LogFlowFunc(("returns %Rrc\n", rc));
179 return rc;
180}
181
182/**
183 * Internal: Open an image, constructing all necessary data structures.
184 */
185static int rawOpenImage(PRAWIMAGE pImage, unsigned uOpenFlags)
186{
187 int rc;
188
189 pImage->uOpenFlags = uOpenFlags;
190 pImage->fCreate = false;
191
192 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
193 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
194 AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
195
196 /*
197 * Open the image.
198 */
199 rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename,
200 VDOpenFlagsToFileOpenFlags(uOpenFlags,
201 false /* fCreate */),
202 &pImage->pStorage);
203 if (RT_FAILURE(rc))
204 {
205 /* Do NOT signal an appropriate error here, as the VD layer has the
206 * choice of retrying the open if it failed. */
207 goto out;
208 }
209
210 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &pImage->cbSize);
211 if (RT_FAILURE(rc))
212 goto out;
213 if (pImage->cbSize % 512)
214 {
215 rc = VERR_VD_RAW_SIZE_MODULO_512;
216 goto out;
217 }
218 pImage->uImageFlags |= VD_IMAGE_FLAGS_FIXED;
219
220out:
221 if (RT_FAILURE(rc))
222 rawFreeImage(pImage, false);
223 return rc;
224}
225
226/**
227 * Internal: Create a raw image.
228 */
229static int rawCreateImage(PRAWIMAGE pImage, uint64_t cbSize,
230 unsigned uImageFlags, const char *pszComment,
231 PCVDGEOMETRY pPCHSGeometry,
232 PCVDGEOMETRY pLCHSGeometry, unsigned uOpenFlags,
233 PFNVDPROGRESS pfnProgress, void *pvUser,
234 unsigned uPercentStart, unsigned uPercentSpan)
235{
236 int rc;
237 RTFOFF cbFree = 0;
238 uint64_t uOff;
239 void *pvBuf = NULL;
240 int32_t fOpen;
241
242 uImageFlags |= VD_IMAGE_FLAGS_FIXED;
243
244 pImage->uOpenFlags = uOpenFlags & ~VD_OPEN_FLAGS_READONLY;
245
246 pImage->uImageFlags = uImageFlags;
247 pImage->fCreate = true;
248 pImage->PCHSGeometry = *pPCHSGeometry;
249 pImage->LCHSGeometry = *pLCHSGeometry;
250
251 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
252 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
253 AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
254
255 if (uImageFlags & VD_IMAGE_FLAGS_DIFF)
256 {
257 rc = vdIfError(pImage->pIfError, VERR_VD_RAW_INVALID_TYPE, RT_SRC_POS, N_("Raw: cannot create diff image '%s'"), pImage->pszFilename);
258 goto out;
259 }
260
261 /* Create image file. */
262 fOpen = VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags, true /* fCreate */);
263 if (uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL)
264 fOpen &= ~RTFILE_O_READ;
265 rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename, fOpen, &pImage->pStorage);
266 if (RT_FAILURE(rc))
267 {
268 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("Raw: cannot create image '%s'"), pImage->pszFilename);
269 goto out;
270 }
271
272 if (!(uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL))
273 {
274 /* Check the free space on the disk and leave early if there is not
275 * sufficient space available. */
276 rc = vdIfIoIntFileGetFreeSpace(pImage->pIfIo, pImage->pszFilename, &cbFree);
277 if (RT_SUCCESS(rc) /* ignore errors */ && ((uint64_t)cbFree < cbSize))
278 {
279 rc = vdIfError(pImage->pIfError, VERR_DISK_FULL, RT_SRC_POS, N_("Raw: disk would overflow creating image '%s'"), pImage->pszFilename);
280 goto out;
281 }
282
283 /* Allocate & commit whole file if fixed image, it must be more
284 * effective than expanding file by write operations. */
285 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pImage->pStorage, cbSize);
286 if (RT_FAILURE(rc))
287 {
288 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("Raw: setting image size failed for '%s'"), pImage->pszFilename);
289 goto out;
290 }
291
292 /* Fill image with zeroes. We do this for every fixed-size image since
293 * on some systems (for example Windows Vista), it takes ages to write
294 * a block near the end of a sparse file and the guest could complain
295 * about an ATA timeout. */
296 pvBuf = RTMemTmpAllocZ(RAW_FILL_SIZE);
297 if (!pvBuf)
298 {
299 rc = VERR_NO_MEMORY;
300 goto out;
301 }
302
303 uOff = 0;
304 /* Write data to all image blocks. */
305 while (uOff < cbSize)
306 {
307 unsigned cbChunk = (unsigned)RT_MIN(cbSize - uOff, RAW_FILL_SIZE);
308
309 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pImage->pStorage, uOff,
310 pvBuf, cbChunk);
311 if (RT_FAILURE(rc))
312 {
313 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("Raw: writing block failed for '%s'"), pImage->pszFilename);
314 goto out;
315 }
316
317 uOff += cbChunk;
318
319 if (pfnProgress)
320 {
321 rc = pfnProgress(pvUser,
322 uPercentStart + uOff * uPercentSpan * 98 / (cbSize * 100));
323 if (RT_FAILURE(rc))
324 goto out;
325 }
326 }
327 }
328
329 if (RT_SUCCESS(rc) && pfnProgress)
330 pfnProgress(pvUser, uPercentStart + uPercentSpan * 98 / 100);
331
332 pImage->cbSize = cbSize;
333
334 rc = rawFlushImage(pImage);
335
336out:
337 if (pvBuf)
338 RTMemTmpFree(pvBuf);
339
340 if (RT_SUCCESS(rc) && pfnProgress)
341 pfnProgress(pvUser, uPercentStart + uPercentSpan);
342
343 if (RT_FAILURE(rc))
344 rawFreeImage(pImage, rc != VERR_ALREADY_EXISTS);
345 return rc;
346}
347
348
349/** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
350static DECLCALLBACK(int) rawCheckIfValid(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
351 PVDINTERFACE pVDIfsImage, VDTYPE *penmType)
352{
353 LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p\n", pszFilename, pVDIfsDisk, pVDIfsImage));
354 PVDIOSTORAGE pStorage = NULL;
355 uint64_t cbFile;
356 int rc = VINF_SUCCESS;
357 char *pszSuffix = NULL;
358
359 PVDINTERFACEIOINT pIfIo = VDIfIoIntGet(pVDIfsImage);
360 AssertPtrReturn(pIfIo, VERR_INVALID_PARAMETER);
361
362 if ( !VALID_PTR(pszFilename)
363 || !*pszFilename)
364 {
365 rc = VERR_INVALID_PARAMETER;
366 goto out;
367 }
368
369 pszSuffix = RTPathSuffix(pszFilename);
370
371 /*
372 * Open the file and read the footer.
373 */
374 rc = vdIfIoIntFileOpen(pIfIo, pszFilename,
375 VDOpenFlagsToFileOpenFlags(VD_OPEN_FLAGS_READONLY,
376 false /* fCreate */),
377 &pStorage);
378 if (RT_SUCCESS(rc))
379 rc = vdIfIoIntFileGetSize(pIfIo, pStorage, &cbFile);
380
381 /* Try to guess the image type based on the extension. */
382 if ( RT_SUCCESS(rc)
383 && pszSuffix)
384 {
385 if ( !RTStrICmp(pszSuffix, ".iso")
386 || !RTStrICmp(pszSuffix, ".cdr")) /* DVD images. */
387 {
388 /* Note that there are ISO images smaller than 1 MB; it is impossible to distinguish
389 * between raw floppy and CD images based on their size (and cannot be reliably done
390 * based on contents, either).
391 */
392 if (cbFile % 2048)
393 rc = VERR_VD_RAW_SIZE_MODULO_2048;
394 else if (cbFile <= 32768)
395 rc = VERR_VD_RAW_SIZE_OPTICAL_TOO_SMALL;
396 else
397 {
398 *penmType = VDTYPE_DVD;
399 rc = VINF_SUCCESS;
400 }
401 }
402 else if ( !RTStrICmp(pszSuffix, ".img")
403 || !RTStrICmp(pszSuffix, ".ima")
404 || !RTStrICmp(pszSuffix, ".dsk")
405 || !RTStrICmp(pszSuffix, ".flp")
406 || !RTStrICmp(pszSuffix, ".vfd")) /* Floppy images */
407 {
408 if (cbFile % 512)
409 rc = VERR_VD_RAW_SIZE_MODULO_512;
410 else if (cbFile > RAW_MAX_FLOPPY_IMG_SIZE)
411 rc = VERR_VD_RAW_SIZE_FLOPPY_TOO_BIG;
412 else
413 {
414 *penmType = VDTYPE_FLOPPY;
415 rc = VINF_SUCCESS;
416 }
417 }
418 else
419 rc = VERR_VD_RAW_INVALID_HEADER;
420 }
421 else
422 rc = VERR_VD_RAW_INVALID_HEADER;
423
424 if (pStorage)
425 vdIfIoIntFileClose(pIfIo, pStorage);
426
427out:
428 LogFlowFunc(("returns %Rrc\n", rc));
429 return rc;
430}
431
432/** @copydoc VBOXHDDBACKEND::pfnOpen */
433static DECLCALLBACK(int) rawOpen(const char *pszFilename, unsigned uOpenFlags,
434 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
435 VDTYPE enmType, void **ppBackendData)
436{
437 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p enmType=%u ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, enmType, ppBackendData));
438 int rc;
439 PRAWIMAGE pImage;
440
441 NOREF(enmType); /**< @todo r=klaus make use of the type info. */
442
443 /* Check open flags. All valid flags are supported. */
444 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
445 {
446 rc = VERR_INVALID_PARAMETER;
447 goto out;
448 }
449
450 /* Check remaining arguments. */
451 if ( !VALID_PTR(pszFilename)
452 || !*pszFilename)
453 {
454 rc = VERR_INVALID_PARAMETER;
455 goto out;
456 }
457
458
459 pImage = (PRAWIMAGE)RTMemAllocZ(sizeof(RAWIMAGE));
460 if (!pImage)
461 {
462 rc = VERR_NO_MEMORY;
463 goto out;
464 }
465 pImage->pszFilename = pszFilename;
466 pImage->pStorage = NULL;
467 pImage->pVDIfsDisk = pVDIfsDisk;
468 pImage->pVDIfsImage = pVDIfsImage;
469
470 rc = rawOpenImage(pImage, uOpenFlags);
471 if (RT_SUCCESS(rc))
472 {
473 if (enmType == VDTYPE_DVD)
474 pImage->cbSector = 2048;
475 else
476 pImage->cbSector = 512;
477 *ppBackendData = pImage;
478 }
479 else
480 RTMemFree(pImage);
481
482out:
483 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
484 return rc;
485}
486
487/** @copydoc VBOXHDDBACKEND::pfnCreate */
488static DECLCALLBACK(int) rawCreate(const char *pszFilename, uint64_t cbSize,
489 unsigned uImageFlags, const char *pszComment,
490 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
491 PCRTUUID pUuid, unsigned uOpenFlags,
492 unsigned uPercentStart, unsigned uPercentSpan,
493 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
494 PVDINTERFACE pVDIfsOperation, VDTYPE enmType,
495 void **ppBackendData)
496{
497 LogFlowFunc(("pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" pPCHSGeometry=%#p pLCHSGeometry=%#p Uuid=%RTuuid uOpenFlags=%#x uPercentStart=%u uPercentSpan=%u pVDIfsDisk=%#p pVDIfsImage=%#p pVDIfsOperation=%#p enmType=%u ppBackendData=%#p",
498 pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData));
499 int rc;
500 PRAWIMAGE pImage;
501
502 PFNVDPROGRESS pfnProgress = NULL;
503 void *pvUser = NULL;
504 PVDINTERFACEPROGRESS pIfProgress = VDIfProgressGet(pVDIfsOperation);
505 if (pIfProgress)
506 {
507 pfnProgress = pIfProgress->pfnProgress;
508 pvUser = pIfProgress->Core.pvUser;
509 }
510
511 /* Check the VD container type. Yes, hard disk must be allowed, otherwise
512 * various tools using this backend for hard disk images will fail. */
513 if (enmType != VDTYPE_HDD && enmType != VDTYPE_DVD && enmType != VDTYPE_FLOPPY)
514 {
515 rc = VERR_VD_INVALID_TYPE;
516 goto out;
517 }
518
519 /* Check open flags. All valid flags are supported. */
520 if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
521 {
522 rc = VERR_INVALID_PARAMETER;
523 goto out;
524 }
525
526 /* Check remaining arguments. */
527 if ( !VALID_PTR(pszFilename)
528 || !*pszFilename
529 || !VALID_PTR(pPCHSGeometry)
530 || !VALID_PTR(pLCHSGeometry))
531 {
532 rc = VERR_INVALID_PARAMETER;
533 goto out;
534 }
535
536 pImage = (PRAWIMAGE)RTMemAllocZ(sizeof(RAWIMAGE));
537 if (!pImage)
538 {
539 rc = VERR_NO_MEMORY;
540 goto out;
541 }
542 pImage->pszFilename = pszFilename;
543 pImage->pStorage = NULL;
544 pImage->pVDIfsDisk = pVDIfsDisk;
545 pImage->pVDIfsImage = pVDIfsImage;
546
547 rc = rawCreateImage(pImage, cbSize, uImageFlags, pszComment,
548 pPCHSGeometry, pLCHSGeometry, uOpenFlags,
549 pfnProgress, pvUser, uPercentStart, uPercentSpan);
550 if (RT_SUCCESS(rc))
551 {
552 /* So far the image is opened in read/write mode. Make sure the
553 * image is opened in read-only mode if the caller requested that. */
554 if (uOpenFlags & VD_OPEN_FLAGS_READONLY)
555 {
556 rawFreeImage(pImage, false);
557 rc = rawOpenImage(pImage, uOpenFlags);
558 if (RT_FAILURE(rc))
559 {
560 RTMemFree(pImage);
561 goto out;
562 }
563 }
564 *ppBackendData = pImage;
565 }
566 else
567 RTMemFree(pImage);
568
569out:
570 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
571 return rc;
572}
573
574/** @copydoc VBOXHDDBACKEND::pfnRename */
575static DECLCALLBACK(int) rawRename(void *pBackendData, const char *pszFilename)
576{
577 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
578 int rc = VINF_SUCCESS;
579 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
580
581 /* Check arguments. */
582 if ( !pImage
583 || !pszFilename
584 || !*pszFilename)
585 {
586 rc = VERR_INVALID_PARAMETER;
587 goto out;
588 }
589
590 /* Close the image. */
591 rc = rawFreeImage(pImage, false);
592 if (RT_FAILURE(rc))
593 goto out;
594
595 /* Rename the file. */
596 rc = vdIfIoIntFileMove(pImage->pIfIo, pImage->pszFilename, pszFilename, 0);
597 if (RT_FAILURE(rc))
598 {
599 /* The move failed, try to reopen the original image. */
600 int rc2 = rawOpenImage(pImage, pImage->uOpenFlags);
601 if (RT_FAILURE(rc2))
602 rc = rc2;
603
604 goto out;
605 }
606
607 /* Update pImage with the new information. */
608 pImage->pszFilename = pszFilename;
609
610 /* Open the old image with new name. */
611 rc = rawOpenImage(pImage, pImage->uOpenFlags);
612 if (RT_FAILURE(rc))
613 goto out;
614
615out:
616 LogFlowFunc(("returns %Rrc\n", rc));
617 return rc;
618}
619
620/** @copydoc VBOXHDDBACKEND::pfnClose */
621static DECLCALLBACK(int) rawClose(void *pBackendData, bool fDelete)
622{
623 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
624 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
625 int rc;
626
627 rc = rawFreeImage(pImage, fDelete);
628 RTMemFree(pImage);
629
630 LogFlowFunc(("returns %Rrc\n", rc));
631 return rc;
632}
633
634/** @copydoc VBOXHDDBACKEND::pfnRead */
635static DECLCALLBACK(int) rawRead(void *pBackendData, uint64_t uOffset, size_t cbRead,
636 PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
637{
638 int rc = VINF_SUCCESS;
639 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
640
641 rc = vdIfIoIntFileReadUser(pImage->pIfIo, pImage->pStorage, uOffset,
642 pIoCtx, cbRead);
643 if (RT_SUCCESS(rc))
644 *pcbActuallyRead = cbRead;
645
646 return rc;
647}
648
649/** @copydoc VBOXHDDBACKEND::pfnWrite */
650static DECLCALLBACK(int) rawWrite(void *pBackendData, uint64_t uOffset, size_t cbWrite,
651 PVDIOCTX pIoCtx, size_t *pcbWriteProcess, size_t *pcbPreRead,
652 size_t *pcbPostRead, unsigned fWrite)
653{
654 int rc = VINF_SUCCESS;
655 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
656
657 rc = vdIfIoIntFileWriteUser(pImage->pIfIo, pImage->pStorage, uOffset,
658 pIoCtx, cbWrite, NULL, NULL);
659 if (RT_SUCCESS(rc))
660 {
661 *pcbWriteProcess = cbWrite;
662 *pcbPostRead = 0;
663 *pcbPreRead = 0;
664 }
665
666 return rc;
667}
668
669/** @copydoc VBOXHDDBACKEND::pfnFlush */
670static DECLCALLBACK(int) rawFlush(void *pBackendData, PVDIOCTX pIoCtx)
671{
672 int rc = VINF_SUCCESS;
673 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
674
675 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
676 rc = vdIfIoIntFileFlush(pImage->pIfIo, pImage->pStorage, pIoCtx,
677 NULL, NULL);
678
679 return rc;
680}
681
682/** @copydoc VBOXHDDBACKEND::pfnGetVersion */
683static DECLCALLBACK(unsigned) rawGetVersion(void *pBackendData)
684{
685 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
686 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
687
688 AssertPtr(pImage);
689
690 if (pImage)
691 return 1;
692 else
693 return 0;
694}
695
696/** @copydoc VBOXHDDBACKEND::pfnGetSize */
697static DECLCALLBACK(uint32_t) rawGetSectorSize(void *pBackendData)
698{
699 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
700 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
701 uint32_t cb = 0;
702
703 AssertPtr(pImage);
704
705 if (pImage && pImage->pStorage)
706 cb = pImage->cbSector;
707
708 LogFlowFunc(("returns %u\n", cb));
709 return cb;
710}
711
712/** @copydoc VBOXHDDBACKEND::pfnGetSize */
713static DECLCALLBACK(uint64_t) rawGetSize(void *pBackendData)
714{
715 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
716 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
717 uint64_t cb = 0;
718
719 AssertPtr(pImage);
720
721 if (pImage && pImage->pStorage)
722 cb = pImage->cbSize;
723
724 LogFlowFunc(("returns %llu\n", cb));
725 return cb;
726}
727
728/** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
729static DECLCALLBACK(uint64_t) rawGetFileSize(void *pBackendData)
730{
731 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
732 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
733 uint64_t cb = 0;
734
735 AssertPtr(pImage);
736
737 if (pImage)
738 {
739 uint64_t cbFile;
740 if (pImage->pStorage)
741 {
742 int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
743 if (RT_SUCCESS(rc))
744 cb += cbFile;
745 }
746 }
747
748 LogFlowFunc(("returns %lld\n", cb));
749 return cb;
750}
751
752/** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
753static DECLCALLBACK(int) rawGetPCHSGeometry(void *pBackendData,
754 PVDGEOMETRY pPCHSGeometry)
755{
756 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
757 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
758 int rc;
759
760 AssertPtr(pImage);
761
762 if (pImage)
763 {
764 if (pImage->PCHSGeometry.cCylinders)
765 {
766 *pPCHSGeometry = pImage->PCHSGeometry;
767 rc = VINF_SUCCESS;
768 }
769 else
770 rc = VERR_VD_GEOMETRY_NOT_SET;
771 }
772 else
773 rc = VERR_VD_NOT_OPENED;
774
775 LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
776 return rc;
777}
778
779/** @copydoc VBOXHDDBACKEND::pfnSetPCHSGeometry */
780static DECLCALLBACK(int) rawSetPCHSGeometry(void *pBackendData,
781 PCVDGEOMETRY pPCHSGeometry)
782{
783 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
784 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
785 int rc;
786
787 AssertPtr(pImage);
788
789 if (pImage)
790 {
791 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
792 {
793 rc = VERR_VD_IMAGE_READ_ONLY;
794 goto out;
795 }
796
797 pImage->PCHSGeometry = *pPCHSGeometry;
798 rc = VINF_SUCCESS;
799 }
800 else
801 rc = VERR_VD_NOT_OPENED;
802
803out:
804 LogFlowFunc(("returns %Rrc\n", rc));
805 return rc;
806}
807
808/** @copydoc VBOXHDDBACKEND::pfnGetLCHSGeometry */
809static DECLCALLBACK(int) rawGetLCHSGeometry(void *pBackendData,
810 PVDGEOMETRY pLCHSGeometry)
811{
812 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
813 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
814 int rc;
815
816 AssertPtr(pImage);
817
818 if (pImage)
819 {
820 if (pImage->LCHSGeometry.cCylinders)
821 {
822 *pLCHSGeometry = pImage->LCHSGeometry;
823 rc = VINF_SUCCESS;
824 }
825 else
826 rc = VERR_VD_GEOMETRY_NOT_SET;
827 }
828 else
829 rc = VERR_VD_NOT_OPENED;
830
831 LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
832 return rc;
833}
834
835/** @copydoc VBOXHDDBACKEND::pfnSetLCHSGeometry */
836static DECLCALLBACK(int) rawSetLCHSGeometry(void *pBackendData,
837 PCVDGEOMETRY pLCHSGeometry)
838{
839 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
840 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
841 int rc;
842
843 AssertPtr(pImage);
844
845 if (pImage)
846 {
847 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
848 {
849 rc = VERR_VD_IMAGE_READ_ONLY;
850 goto out;
851 }
852
853 pImage->LCHSGeometry = *pLCHSGeometry;
854 rc = VINF_SUCCESS;
855 }
856 else
857 rc = VERR_VD_NOT_OPENED;
858
859out:
860 LogFlowFunc(("returns %Rrc\n", rc));
861 return rc;
862}
863
864/** @copydoc VBOXHDDBACKEND::pfnGetImageFlags */
865static DECLCALLBACK(unsigned) rawGetImageFlags(void *pBackendData)
866{
867 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
868 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
869 unsigned uImageFlags;
870
871 AssertPtr(pImage);
872
873 if (pImage)
874 uImageFlags = pImage->uImageFlags;
875 else
876 uImageFlags = 0;
877
878 LogFlowFunc(("returns %#x\n", uImageFlags));
879 return uImageFlags;
880}
881
882/** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
883static DECLCALLBACK(unsigned) rawGetOpenFlags(void *pBackendData)
884{
885 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
886 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
887 unsigned uOpenFlags;
888
889 AssertPtr(pImage);
890
891 if (pImage)
892 uOpenFlags = pImage->uOpenFlags;
893 else
894 uOpenFlags = 0;
895
896 LogFlowFunc(("returns %#x\n", uOpenFlags));
897 return uOpenFlags;
898}
899
900/** @copydoc VBOXHDDBACKEND::pfnSetOpenFlags */
901static DECLCALLBACK(int) rawSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
902{
903 LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
904 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
905 int rc;
906
907 /* Image must be opened and the new flags must be valid. */
908 if (!pImage || (uOpenFlags & ~( VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO
909 | VD_OPEN_FLAGS_ASYNC_IO | VD_OPEN_FLAGS_SHAREABLE
910 | VD_OPEN_FLAGS_SEQUENTIAL | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)))
911 {
912 rc = VERR_INVALID_PARAMETER;
913 goto out;
914 }
915
916 /* Implement this operation via reopening the image. */
917 rc = rawFreeImage(pImage, false);
918 if (RT_FAILURE(rc))
919 goto out;
920 rc = rawOpenImage(pImage, uOpenFlags);
921
922out:
923 LogFlowFunc(("returns %Rrc\n", rc));
924 return rc;
925}
926
927/** @copydoc VBOXHDDBACKEND::pfnGetComment */
928static DECLCALLBACK(int) rawGetComment(void *pBackendData, char *pszComment,
929 size_t cbComment)
930{
931 LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
932 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
933 int rc;
934
935 AssertPtr(pImage);
936
937 if (pImage)
938 rc = VERR_NOT_SUPPORTED;
939 else
940 rc = VERR_VD_NOT_OPENED;
941
942 LogFlowFunc(("returns %Rrc comment='%s'\n", rc, pszComment));
943 return rc;
944}
945
946/** @copydoc VBOXHDDBACKEND::pfnSetComment */
947static DECLCALLBACK(int) rawSetComment(void *pBackendData, const char *pszComment)
948{
949 LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
950 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
951 int rc;
952
953 AssertPtr(pImage);
954
955 if (pImage)
956 {
957 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
958 rc = VERR_VD_IMAGE_READ_ONLY;
959 else
960 rc = VERR_NOT_SUPPORTED;
961 }
962 else
963 rc = VERR_VD_NOT_OPENED;
964
965 LogFlowFunc(("returns %Rrc\n", rc));
966 return rc;
967}
968
969/** @copydoc VBOXHDDBACKEND::pfnGetUuid */
970static DECLCALLBACK(int) rawGetUuid(void *pBackendData, PRTUUID pUuid)
971{
972 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
973 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
974 int rc;
975
976 AssertPtr(pImage);
977
978 if (pImage)
979 rc = VERR_NOT_SUPPORTED;
980 else
981 rc = VERR_VD_NOT_OPENED;
982
983 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
984 return rc;
985}
986
987/** @copydoc VBOXHDDBACKEND::pfnSetUuid */
988static DECLCALLBACK(int) rawSetUuid(void *pBackendData, PCRTUUID pUuid)
989{
990 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
991 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
992 int rc;
993
994 LogFlowFunc(("%RTuuid\n", pUuid));
995 AssertPtr(pImage);
996
997 if (pImage)
998 {
999 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1000 rc = VERR_NOT_SUPPORTED;
1001 else
1002 rc = VERR_VD_IMAGE_READ_ONLY;
1003 }
1004 else
1005 rc = VERR_VD_NOT_OPENED;
1006
1007 LogFlowFunc(("returns %Rrc\n", rc));
1008 return rc;
1009}
1010
1011/** @copydoc VBOXHDDBACKEND::pfnGetModificationUuid */
1012static DECLCALLBACK(int) rawGetModificationUuid(void *pBackendData, PRTUUID pUuid)
1013{
1014 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1015 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
1016 int rc;
1017
1018 AssertPtr(pImage);
1019
1020 if (pImage)
1021 rc = VERR_NOT_SUPPORTED;
1022 else
1023 rc = VERR_VD_NOT_OPENED;
1024
1025 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
1026 return rc;
1027}
1028
1029/** @copydoc VBOXHDDBACKEND::pfnSetModificationUuid */
1030static DECLCALLBACK(int) rawSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
1031{
1032 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
1033 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
1034 int rc;
1035
1036 AssertPtr(pImage);
1037
1038 if (pImage)
1039 {
1040 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1041 rc = VERR_NOT_SUPPORTED;
1042 else
1043 rc = VERR_VD_IMAGE_READ_ONLY;
1044 }
1045 else
1046 rc = VERR_VD_NOT_OPENED;
1047
1048 LogFlowFunc(("returns %Rrc\n", rc));
1049 return rc;
1050}
1051
1052/** @copydoc VBOXHDDBACKEND::pfnGetParentUuid */
1053static DECLCALLBACK(int) rawGetParentUuid(void *pBackendData, PRTUUID pUuid)
1054{
1055 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1056 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
1057 int rc;
1058
1059 AssertPtr(pImage);
1060
1061 if (pImage)
1062 rc = VERR_NOT_SUPPORTED;
1063 else
1064 rc = VERR_VD_NOT_OPENED;
1065
1066 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
1067 return rc;
1068}
1069
1070/** @copydoc VBOXHDDBACKEND::pfnSetParentUuid */
1071static DECLCALLBACK(int) rawSetParentUuid(void *pBackendData, PCRTUUID pUuid)
1072{
1073 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
1074 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
1075 int rc;
1076
1077 AssertPtr(pImage);
1078
1079 if (pImage)
1080 {
1081 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1082 rc = VERR_NOT_SUPPORTED;
1083 else
1084 rc = VERR_VD_IMAGE_READ_ONLY;
1085 }
1086 else
1087 rc = VERR_VD_NOT_OPENED;
1088
1089 LogFlowFunc(("returns %Rrc\n", rc));
1090 return rc;
1091}
1092
1093/** @copydoc VBOXHDDBACKEND::pfnGetParentModificationUuid */
1094static DECLCALLBACK(int) rawGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
1095{
1096 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
1097 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
1098 int rc;
1099
1100 AssertPtr(pImage);
1101
1102 if (pImage)
1103 rc = VERR_NOT_SUPPORTED;
1104 else
1105 rc = VERR_VD_NOT_OPENED;
1106
1107 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
1108 return rc;
1109}
1110
1111/** @copydoc VBOXHDDBACKEND::pfnSetParentModificationUuid */
1112static DECLCALLBACK(int) rawSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
1113{
1114 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
1115 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
1116 int rc;
1117
1118 AssertPtr(pImage);
1119
1120 if (pImage)
1121 {
1122 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
1123 rc = VERR_NOT_SUPPORTED;
1124 else
1125 rc = VERR_VD_IMAGE_READ_ONLY;
1126 }
1127 else
1128 rc = VERR_VD_NOT_OPENED;
1129
1130 LogFlowFunc(("returns %Rrc\n", rc));
1131 return rc;
1132}
1133
1134/** @copydoc VBOXHDDBACKEND::pfnDump */
1135static DECLCALLBACK(void) rawDump(void *pBackendData)
1136{
1137 PRAWIMAGE pImage = (PRAWIMAGE)pBackendData;
1138
1139 AssertPtr(pImage);
1140 if (pImage)
1141 {
1142 vdIfErrorMessage(pImage->pIfError, "Header: Geometry PCHS=%u/%u/%u LCHS=%u/%u/%u cbSector=%llu\n",
1143 pImage->PCHSGeometry.cCylinders, pImage->PCHSGeometry.cHeads, pImage->PCHSGeometry.cSectors,
1144 pImage->LCHSGeometry.cCylinders, pImage->LCHSGeometry.cHeads, pImage->LCHSGeometry.cSectors,
1145 pImage->cbSize / 512);
1146 }
1147}
1148
1149
1150
1151const VBOXHDDBACKEND g_RawBackend =
1152{
1153 /* pszBackendName */
1154 "RAW",
1155 /* cbSize */
1156 sizeof(VBOXHDDBACKEND),
1157 /* uBackendCaps */
1158 VD_CAP_CREATE_FIXED | VD_CAP_FILE | VD_CAP_ASYNC | VD_CAP_VFS,
1159 /* paFileExtensions */
1160 s_aRawFileExtensions,
1161 /* paConfigInfo */
1162 NULL,
1163 /* pfnCheckIfValid */
1164 rawCheckIfValid,
1165 /* pfnOpen */
1166 rawOpen,
1167 /* pfnCreate */
1168 rawCreate,
1169 /* pfnRename */
1170 rawRename,
1171 /* pfnClose */
1172 rawClose,
1173 /* pfnRead */
1174 rawRead,
1175 /* pfnWrite */
1176 rawWrite,
1177 /* pfnFlush */
1178 rawFlush,
1179 /* pfnDiscard */
1180 NULL,
1181 /* pfnGetVersion */
1182 rawGetVersion,
1183 /* pfnGetSectorSize */
1184 rawGetSectorSize,
1185 /* pfnGetSize */
1186 rawGetSize,
1187 /* pfnGetFileSize */
1188 rawGetFileSize,
1189 /* pfnGetPCHSGeometry */
1190 rawGetPCHSGeometry,
1191 /* pfnSetPCHSGeometry */
1192 rawSetPCHSGeometry,
1193 /* pfnGetLCHSGeometry */
1194 rawGetLCHSGeometry,
1195 /* pfnSetLCHSGeometry */
1196 rawSetLCHSGeometry,
1197 /* pfnGetImageFlags */
1198 rawGetImageFlags,
1199 /* pfnGetOpenFlags */
1200 rawGetOpenFlags,
1201 /* pfnSetOpenFlags */
1202 rawSetOpenFlags,
1203 /* pfnGetComment */
1204 rawGetComment,
1205 /* pfnSetComment */
1206 rawSetComment,
1207 /* pfnGetUuid */
1208 rawGetUuid,
1209 /* pfnSetUuid */
1210 rawSetUuid,
1211 /* pfnGetModificationUuid */
1212 rawGetModificationUuid,
1213 /* pfnSetModificationUuid */
1214 rawSetModificationUuid,
1215 /* pfnGetParentUuid */
1216 rawGetParentUuid,
1217 /* pfnSetParentUuid */
1218 rawSetParentUuid,
1219 /* pfnGetParentModificationUuid */
1220 rawGetParentModificationUuid,
1221 /* pfnSetParentModificationUuid */
1222 rawSetParentModificationUuid,
1223 /* pfnDump */
1224 rawDump,
1225 /* pfnGetTimeStamp */
1226 NULL,
1227 /* pfnGetParentTimeStamp */
1228 NULL,
1229 /* pfnSetParentTimeStamp */
1230 NULL,
1231 /* pfnGetParentFilename */
1232 NULL,
1233 /* pfnSetParentFilename */
1234 NULL,
1235 /* pfnComposeLocation */
1236 genericFileComposeLocation,
1237 /* pfnComposeName */
1238 genericFileComposeName,
1239 /* pfnCompact */
1240 NULL,
1241 /* pfnResize */
1242 NULL,
1243 /* pfnRepair */
1244 NULL,
1245 /* pfnTraverseMetadata */
1246 NULL
1247};
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