VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/dvm/dvm.cpp@ 91560

Last change on this file since 91560 was 88948, checked in by vboxsync, 4 years ago

IPRT/dvm: Add RT_OS_NETBSD to the list of "not windows". bugref:8547.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 30.4 KB
Line 
1/* $Id: dvm.cpp 88948 2021-05-08 23:00:38Z vboxsync $ */
2/** @file
3 * IPRT Disk Volume Management API (DVM) - generic code.
4 */
5
6/*
7 * Copyright (C) 2011-2020 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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/types.h>
32#include <iprt/assert.h>
33#include <iprt/mem.h>
34#include <iprt/dvm.h>
35#include <iprt/err.h>
36#include <iprt/asm.h>
37#include <iprt/string.h>
38#include <iprt/list.h>
39#include "internal/dvm.h"
40
41
42/*********************************************************************************************************************************
43* Structures and Typedefs *
44*********************************************************************************************************************************/
45
46/**
47 * The internal volume manager structure.
48 */
49typedef struct RTDVMINTERNAL
50{
51 /** The DVM magic (RTDVM_MAGIC). */
52 uint32_t u32Magic;
53 /** The disk descriptor. */
54 RTDVMDISK DvmDisk;
55 /** Pointer to the backend operations table after a successful probe. */
56 PCRTDVMFMTOPS pDvmFmtOps;
57 /** The format specific volume manager data. */
58 RTDVMFMT hVolMgrFmt;
59 /** Flags passed on manager creation. */
60 uint32_t fFlags;
61 /** Reference counter. */
62 uint32_t volatile cRefs;
63 /** List of recognised volumes (RTDVMVOLUMEINTERNAL). */
64 RTLISTANCHOR VolumeList;
65} RTDVMINTERNAL;
66/** Pointer to an internal volume manager. */
67typedef RTDVMINTERNAL *PRTDVMINTERNAL;
68
69/**
70 * The internal volume structure.
71 */
72typedef struct RTDVMVOLUMEINTERNAL
73{
74 /** The DVM volume magic (RTDVMVOLUME_MAGIC). */
75 uint32_t u32Magic;
76 /** Node for the volume list. */
77 RTLISTNODE VolumeNode;
78 /** Pointer to the owning volume manager. */
79 PRTDVMINTERNAL pVolMgr;
80 /** Format specific volume data. */
81 RTDVMVOLUMEFMT hVolFmt;
82 /** Set block status.callback */
83 PFNDVMVOLUMEQUERYBLOCKSTATUS pfnQueryBlockStatus;
84 /** Opaque user data. */
85 void *pvUser;
86 /** Reference counter. */
87 uint32_t volatile cRefs;
88} RTDVMVOLUMEINTERNAL;
89/** Pointer to an internal volume. */
90typedef RTDVMVOLUMEINTERNAL *PRTDVMVOLUMEINTERNAL;
91
92
93/*********************************************************************************************************************************
94* Global variables *
95*********************************************************************************************************************************/
96/**
97 * Supported volume formats.
98 */
99static PCRTDVMFMTOPS const g_aDvmFmts[] =
100{
101 &g_rtDvmFmtMbr,
102 &g_rtDvmFmtGpt,
103 &g_rtDvmFmtBsdLbl
104};
105
106/**
107 * Descriptions of the volume types.
108 *
109 * This is indexed by RTDVMVOLTYPE.
110 */
111static const char * const g_apszDvmVolTypes[] =
112{
113 "Invalid",
114 "Unknown",
115 "NTFS",
116 "FAT12",
117 "FAT16",
118 "FAT32",
119
120 "EFI system partition",
121
122 "Mac OS X HFS or HFS+",
123 "Mac OS X APFS",
124
125 "Linux swap",
126 "Linux native",
127 "Linux LVM",
128 "Linux SoftRaid",
129
130 "FreeBSD",
131 "NetBSD",
132 "OpenBSD",
133 "Solaris",
134
135 "Basic data partition",
136 "Microsoft reserved partition",
137 "Windows LDM metadata",
138 "Windows LDM data",
139 "Windows recovery partition",
140 "Windows storage spaces",
141
142 "IBM GPFS",
143
144 "OS/2",
145};
146AssertCompile(RT_ELEMENTS(g_apszDvmVolTypes) == RTDVMVOLTYPE_END);
147
148
149/**
150 * Read from the disk at the given offset, neither the offset nor the size is
151 * necessary sector aligned.
152 *
153 * @returns IPRT status code.
154 * @param pDisk The disk descriptor to read from.
155 * @param off Start offset.
156 * @param pvBuf Destination buffer.
157 * @param cbRead How much to read.
158 */
159DECLHIDDEN(int) rtDvmDiskReadUnaligned(PCRTDVMDISK pDisk, uint64_t off, void *pvBuf, size_t cbRead)
160{
161 size_t const cbSector = (size_t)pDisk->cbSector;
162 size_t const offDelta = off % cbSector;
163 size_t const cbDelta = cbRead % cbSector;
164 if (!cbDelta && !offDelta)
165 return rtDvmDiskRead(pDisk, off, pvBuf, cbRead);
166
167 int rc;
168 size_t cbExtra = offDelta + (cbDelta ? cbSector - cbDelta: 0);
169 uint8_t *pbTmpBuf = (uint8_t *)RTMemTmpAlloc(cbRead + cbExtra);
170 if (pbTmpBuf)
171 {
172 rc = rtDvmDiskRead(pDisk, off - offDelta, pbTmpBuf, cbRead + cbExtra);
173 if (RT_SUCCESS(rc))
174 memcpy(pvBuf, &pbTmpBuf[offDelta], cbRead);
175 else
176 RT_BZERO(pvBuf, cbRead);
177 RTMemTmpFree(pbTmpBuf);
178 }
179 else
180 rc = VERR_NO_TMP_MEMORY;
181 return rc;
182}
183
184
185/**
186 * Creates a new volume.
187 *
188 * @returns IPRT status code.
189 * @param pThis The DVM map instance.
190 * @param hVolFmt The format specific volume handle.
191 * @param phVol Where to store the generic volume handle on success.
192 */
193static int rtDvmVolumeCreate(PRTDVMINTERNAL pThis, RTDVMVOLUMEFMT hVolFmt, PRTDVMVOLUME phVol)
194{
195 PRTDVMVOLUMEINTERNAL pVol = (PRTDVMVOLUMEINTERNAL)RTMemAllocZ(sizeof(RTDVMVOLUMEINTERNAL));
196 if (pVol)
197 {
198 pVol->u32Magic = RTDVMVOLUME_MAGIC;
199 pVol->cRefs = 0;
200 pVol->pVolMgr = pThis;
201 pVol->hVolFmt = hVolFmt;
202
203 *phVol = pVol;
204 return VINF_SUCCESS;
205 }
206 return VERR_NO_MEMORY;
207}
208
209/**
210 * Destroys a volume handle.
211 *
212 * @param pThis The volume manager instance.
213 * @param pVol The volume to destroy.
214 */
215static void rtDvmVolumeDestroy(PRTDVMINTERNAL pThis, PRTDVMVOLUMEINTERNAL pVol)
216{
217 AssertPtr(pThis);
218 AssertPtr(pThis->pDvmFmtOps);
219 Assert(pVol->pVolMgr == pThis);
220
221 /* Close the volume. */
222 pThis->pDvmFmtOps->pfnVolumeClose(pVol->hVolFmt);
223
224 pVol->u32Magic = RTDVMVOLUME_MAGIC_DEAD;
225 pVol->pVolMgr = NULL;
226 pVol->hVolFmt = NIL_RTDVMVOLUMEFMT;
227 RTMemFree(pVol);
228}
229
230
231RTDECL(int) RTDvmCreate(PRTDVM phVolMgr, RTVFSFILE hVfsFile, uint32_t cbSector, uint32_t fFlags)
232{
233 AssertMsgReturn(!(fFlags & ~DVM_FLAGS_VALID_MASK), ("Invalid flags given %#x\n", fFlags), VERR_INVALID_FLAGS);
234 uint32_t cRefs = RTVfsFileRetain(hVfsFile);
235 AssertReturn(cRefs != UINT32_MAX, VERR_INVALID_HANDLE);
236
237 uint64_t cbDisk;
238 int rc = RTVfsFileQuerySize(hVfsFile, &cbDisk);
239 if (RT_SUCCESS(rc))
240 {
241 PRTDVMINTERNAL pThis = (PRTDVMINTERNAL)RTMemAllocZ(sizeof(RTDVMINTERNAL));
242 if (pThis)
243 {
244 pThis->u32Magic = RTDVM_MAGIC;
245 pThis->DvmDisk.cbDisk = cbDisk;
246 pThis->DvmDisk.cbSector = cbSector;
247 pThis->DvmDisk.hVfsFile = hVfsFile;
248
249 pThis->pDvmFmtOps = NULL;
250 pThis->hVolMgrFmt = NIL_RTDVMFMT;
251 pThis->fFlags = fFlags;
252 pThis->cRefs = 1;
253 RTListInit(&pThis->VolumeList);
254
255 *phVolMgr = pThis;
256 return VINF_SUCCESS;
257 }
258 rc = VERR_NO_MEMORY;
259 }
260 RTVfsFileRelease(hVfsFile);
261 return rc;
262}
263
264
265RTDECL(uint32_t) RTDvmRetain(RTDVM hVolMgr)
266{
267 PRTDVMINTERNAL pThis = hVolMgr;
268 AssertPtrReturn(pThis, UINT32_MAX);
269 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, UINT32_MAX);
270
271 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
272 AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p\n", cRefs, pThis));
273 return cRefs;
274}
275
276/**
277 * Destroys a volume manager handle.
278 *
279 * @param pThis The volume manager to destroy.
280 */
281static void rtDvmDestroy(PRTDVMINTERNAL pThis)
282{
283 pThis->u32Magic = RTDVM_MAGIC_DEAD;
284
285 if (pThis->hVolMgrFmt != NIL_RTDVMFMT)
286 {
287 AssertPtr(pThis->pDvmFmtOps);
288
289 /* */
290 PRTDVMVOLUMEINTERNAL pItNext, pIt;
291 RTListForEachSafe(&pThis->VolumeList, pIt, pItNext, RTDVMVOLUMEINTERNAL, VolumeNode)
292 {
293 RTListNodeRemove(&pIt->VolumeNode);
294 rtDvmVolumeDestroy(pThis, pIt);
295 }
296
297 /* Let the backend do it's own cleanup first. */
298 pThis->pDvmFmtOps->pfnClose(pThis->hVolMgrFmt);
299 pThis->hVolMgrFmt = NIL_RTDVMFMT;
300 pThis->pDvmFmtOps = NULL;
301 }
302
303 pThis->DvmDisk.cbDisk = 0;
304 pThis->DvmDisk.cbSector = 0;
305 if (pThis->DvmDisk.hVfsFile != NIL_RTVFSFILE)
306 {
307 RTVfsFileRelease(pThis->DvmDisk.hVfsFile);
308 pThis->DvmDisk.hVfsFile = NIL_RTVFSFILE;
309 }
310
311 RTMemFree(pThis);
312}
313
314RTDECL(uint32_t) RTDvmRelease(RTDVM hVolMgr)
315{
316 PRTDVMINTERNAL pThis = hVolMgr;
317 if (pThis == NIL_RTDVM)
318 return 0;
319 AssertPtrReturn(pThis, UINT32_MAX);
320 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, UINT32_MAX);
321
322 uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
323 AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pThis));
324 if (cRefs == 0)
325 rtDvmDestroy(pThis);
326 return cRefs;
327}
328
329RTDECL(int) RTDvmMapOpen(RTDVM hVolMgr)
330{
331 PRTDVMINTERNAL pThis = hVolMgr;
332 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
333 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, VERR_INVALID_HANDLE);
334 AssertReturn(pThis->hVolMgrFmt == NIL_RTDVMFMT, VERR_WRONG_ORDER);
335
336 Assert(!pThis->pDvmFmtOps);
337
338 /*
339 * Let each format backend have a go at the disk, pick the one which scores the highest.
340 */
341 int rc = VINF_SUCCESS;
342 uint32_t uScoreMax = RTDVM_MATCH_SCORE_UNSUPPORTED;
343 PCRTDVMFMTOPS pDvmFmtOpsMatch = NULL;
344 for (unsigned i = 0; i < RT_ELEMENTS(g_aDvmFmts); i++)
345 {
346 uint32_t uScore = 0;
347 PCRTDVMFMTOPS pDvmFmtOps = g_aDvmFmts[i];
348
349 rc = pDvmFmtOps->pfnProbe(&pThis->DvmDisk, &uScore);
350 if (RT_SUCCESS(rc))
351 {
352 if (uScore > uScoreMax)
353 {
354 pDvmFmtOpsMatch = pDvmFmtOps;
355 uScoreMax = uScore;
356 }
357 }
358 else
359 return rc;
360 }
361 if (uScoreMax > RTDVM_MATCH_SCORE_UNSUPPORTED)
362 {
363 AssertPtr(pDvmFmtOpsMatch);
364
365 /*
366 * Open the format.
367 */
368 rc = pDvmFmtOpsMatch->pfnOpen(&pThis->DvmDisk, &pThis->hVolMgrFmt);
369 if (RT_SUCCESS(rc))
370 {
371 pThis->pDvmFmtOps = pDvmFmtOpsMatch;
372
373 /*
374 * Construct volume list (we're done if none).
375 */
376 uint32_t cVols = pThis->pDvmFmtOps->pfnGetValidVolumes(pThis->hVolMgrFmt);
377 if (cVols == 0)
378 return VINF_SUCCESS;
379
380 /* First volume. */
381 RTDVMVOLUMEFMT hVolFmt = NIL_RTDVMVOLUMEFMT;
382 rc = pThis->pDvmFmtOps->pfnQueryFirstVolume(pThis->hVolMgrFmt, &hVolFmt);
383 if (RT_SUCCESS(rc))
384 {
385 for (;;)
386 {
387 PRTDVMVOLUMEINTERNAL pVol = NULL;
388 rc = rtDvmVolumeCreate(pThis, hVolFmt, &pVol);
389 if (RT_FAILURE(rc))
390 {
391 pThis->pDvmFmtOps->pfnVolumeClose(hVolFmt);
392 break;
393 }
394 RTListAppend(&pThis->VolumeList, &pVol->VolumeNode);
395
396 /* Done?*/
397 cVols--;
398 if (cVols < 1)
399 return VINF_SUCCESS;
400
401 /* Next volume. */
402 rc = pThis->pDvmFmtOps->pfnQueryNextVolume(pThis->hVolMgrFmt, pVol->hVolFmt, &hVolFmt);
403 if (RT_FAILURE(rc))
404 break;
405 }
406
407 /* Bail out. */
408 PRTDVMVOLUMEINTERNAL pItNext, pIt;
409 RTListForEachSafe(&pThis->VolumeList, pIt, pItNext, RTDVMVOLUMEINTERNAL, VolumeNode)
410 {
411 RTListNodeRemove(&pIt->VolumeNode);
412 rtDvmVolumeDestroy(pThis, pIt);
413 }
414 }
415
416 pDvmFmtOpsMatch->pfnClose(pThis->hVolMgrFmt);
417 }
418 }
419 else
420 rc = VERR_NOT_SUPPORTED;
421 return rc;
422}
423
424RTDECL(int) RTDvmMapInitialize(RTDVM hVolMgr, const char *pszFmt)
425{
426 PRTDVMINTERNAL pThis = hVolMgr;
427 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
428 AssertPtrReturn(pszFmt, VERR_INVALID_POINTER);
429 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, VERR_INVALID_HANDLE);
430 AssertReturn(pThis->hVolMgrFmt == NIL_RTDVMFMT, VERR_WRONG_ORDER);
431
432 for (unsigned i = 0; i < RT_ELEMENTS(g_aDvmFmts); i++)
433 {
434 PCRTDVMFMTOPS pDvmFmtOps = g_aDvmFmts[i];
435 if (!RTStrCmp(pDvmFmtOps->pszFmt, pszFmt))
436 {
437 int rc = pDvmFmtOps->pfnInitialize(&pThis->DvmDisk, &pThis->hVolMgrFmt);
438 if (RT_SUCCESS(rc))
439 pThis->pDvmFmtOps = pDvmFmtOps;
440 return rc;
441 }
442 }
443 return VERR_NOT_SUPPORTED;
444}
445
446RTDECL(const char *) RTDvmMapGetFormatName(RTDVM hVolMgr)
447{
448 PRTDVMINTERNAL pThis = hVolMgr;
449 AssertPtrReturn(pThis, NULL);
450 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, NULL);
451 AssertReturn(pThis->hVolMgrFmt != NIL_RTDVMFMT, NULL);
452
453 return pThis->pDvmFmtOps->pszFmt;
454}
455
456RTDECL(RTDVMFORMATTYPE) RTDvmMapGetFormatType(RTDVM hVolMgr)
457{
458 PRTDVMINTERNAL pThis = hVolMgr;
459 AssertPtrReturn(pThis, RTDVMFORMATTYPE_INVALID);
460 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, RTDVMFORMATTYPE_INVALID);
461 AssertReturn(pThis->hVolMgrFmt != NIL_RTDVMFMT, RTDVMFORMATTYPE_INVALID);
462
463 return pThis->pDvmFmtOps->enmFormat;
464}
465
466RTDECL(int) RTDvmMapQueryDiskUuid(RTDVM hVolMgr, PRTUUID pUuid)
467{
468 PRTDVMINTERNAL pThis = hVolMgr;
469 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
470 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, VERR_INVALID_HANDLE);
471 AssertReturn(pThis->hVolMgrFmt != NIL_RTDVMFMT, VERR_INVALID_HANDLE);
472 AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
473
474 if (pThis->pDvmFmtOps->pfnQueryDiskUuid)
475 return pThis->pDvmFmtOps->pfnQueryDiskUuid(pThis->hVolMgrFmt, pUuid);
476 return VERR_NOT_SUPPORTED;
477}
478
479RTDECL(uint32_t) RTDvmMapGetValidVolumes(RTDVM hVolMgr)
480{
481 PRTDVMINTERNAL pThis = hVolMgr;
482 AssertPtrReturn(pThis, UINT32_MAX);
483 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, UINT32_MAX);
484 AssertReturn(pThis->hVolMgrFmt != NIL_RTDVMFMT, UINT32_MAX);
485
486 return pThis->pDvmFmtOps->pfnGetValidVolumes(pThis->hVolMgrFmt);
487}
488
489RTDECL(uint32_t) RTDvmMapGetMaxVolumes(RTDVM hVolMgr)
490{
491 PRTDVMINTERNAL pThis = hVolMgr;
492 AssertPtrReturn(pThis, UINT32_MAX);
493 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, UINT32_MAX);
494 AssertReturn(pThis->hVolMgrFmt != NIL_RTDVMFMT, UINT32_MAX);
495
496 return pThis->pDvmFmtOps->pfnGetMaxVolumes(pThis->hVolMgrFmt);
497}
498
499RTDECL(int) RTDvmMapQueryFirstVolume(RTDVM hVolMgr, PRTDVMVOLUME phVol)
500{
501 PRTDVMINTERNAL pThis = hVolMgr;
502 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
503 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, VERR_INVALID_HANDLE);
504 AssertReturn(pThis->hVolMgrFmt != NIL_RTDVMFMT, VERR_INVALID_HANDLE);
505 AssertPtrReturn(phVol, VERR_INVALID_POINTER);
506
507 int rc = VERR_DVM_MAP_EMPTY;
508 PRTDVMVOLUMEINTERNAL pVol = RTListGetFirst(&pThis->VolumeList, RTDVMVOLUMEINTERNAL, VolumeNode);
509 if (pVol)
510 {
511 rc = VINF_SUCCESS;
512 RTDvmVolumeRetain(pVol);
513 *phVol = pVol;
514 }
515
516 return rc;
517}
518
519RTDECL(int) RTDvmMapQueryNextVolume(RTDVM hVolMgr, RTDVMVOLUME hVol, PRTDVMVOLUME phVolNext)
520{
521 PRTDVMINTERNAL pThis = hVolMgr;
522 PRTDVMVOLUMEINTERNAL pVol = hVol;
523 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
524 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, VERR_INVALID_HANDLE);
525 AssertReturn(pThis->hVolMgrFmt != NIL_RTDVMFMT, VERR_INVALID_HANDLE);
526 AssertPtrReturn(pVol, VERR_INVALID_HANDLE);
527 AssertReturn(pVol->u32Magic == RTDVMVOLUME_MAGIC, VERR_INVALID_HANDLE);
528 AssertPtrReturn(phVolNext, VERR_INVALID_POINTER);
529
530 int rc = VERR_DVM_MAP_NO_VOLUME;
531 PRTDVMVOLUMEINTERNAL pVolNext = RTListGetNext(&pThis->VolumeList, pVol, RTDVMVOLUMEINTERNAL, VolumeNode);
532 if (pVolNext)
533 {
534 rc = VINF_SUCCESS;
535 RTDvmVolumeRetain(pVolNext);
536 *phVolNext = pVolNext;
537 }
538
539 return rc;
540}
541
542RTDECL(int) RTDvmMapQueryBlockStatus(RTDVM hVolMgr, uint64_t off, uint64_t cb, bool *pfAllocated)
543{
544 PRTDVMINTERNAL pThis = hVolMgr;
545
546 /*
547 * Input validation.
548 */
549 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
550 AssertPtrReturn(pfAllocated, VERR_INVALID_POINTER);
551 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, VERR_INVALID_HANDLE);
552 AssertReturn(pThis->hVolMgrFmt != NIL_RTDVMFMT, VERR_WRONG_ORDER);
553 AssertMsgReturn( off <= pThis->DvmDisk.cbDisk
554 || cb <= pThis->DvmDisk.cbDisk
555 || off + cb <= pThis->DvmDisk.cbDisk,
556 ("off=%#RX64 cb=%#RX64 cbDisk=%#RX64\n", off, cb, pThis->DvmDisk.cbDisk),
557 VERR_OUT_OF_RANGE);
558
559 /*
560 * Check whether the range is inuse by the volume manager metadata first.
561 */
562 int rc = pThis->pDvmFmtOps->pfnQueryRangeUse(pThis->hVolMgrFmt, off, cb, pfAllocated);
563 if (RT_FAILURE(rc) || *pfAllocated)
564 return rc;
565
566 /*
567 * Not used by volume manager metadata, so work thru the specified range one
568 * volume / void (free space) at a time. All must be unallocated for us to
569 * reach the end, we return immediately if any portion is allocated.
570 */
571 while (cb > 0)
572 {
573 /*
574 * Search through all volumes.
575 *
576 * It is not possible to get all start sectors and sizes of all volumes
577 * here because volumes can be scattered around the disk for certain formats.
578 * Linux LVM is one example, it extents of logical volumes don't need to be
579 * contiguous on the medium.
580 */
581 bool fVolFound = false;
582 PRTDVMVOLUMEINTERNAL pVol;
583 RTListForEach(&pThis->VolumeList, pVol, RTDVMVOLUMEINTERNAL, VolumeNode)
584 {
585 uint64_t cbIntersect;
586 uint64_t offVol;
587 bool fIntersect = pThis->pDvmFmtOps->pfnVolumeIsRangeIntersecting(pVol->hVolFmt, off, cb, &offVol, &cbIntersect);
588 if (fIntersect)
589 {
590 fVolFound = true;
591 if (pVol->pfnQueryBlockStatus)
592 {
593 bool fVolAllocated = true;
594 rc = pVol->pfnQueryBlockStatus(pVol->pvUser, offVol, cbIntersect, &fVolAllocated);
595 if (RT_FAILURE(rc) || fVolAllocated)
596 {
597 *pfAllocated = true;
598 return rc;
599 }
600 }
601 else if (!(pThis->fFlags & DVM_FLAGS_NO_STATUS_CALLBACK_MARK_AS_UNUSED))
602 {
603 *pfAllocated = true;
604 return VINF_SUCCESS;
605 }
606 /* else, flag is set, continue. */
607
608 cb -= cbIntersect;
609 off += cbIntersect;
610 break;
611 }
612 }
613
614 if (!fVolFound)
615 {
616 if (pThis->fFlags & DVM_FLAGS_UNUSED_SPACE_MARK_AS_USED)
617 {
618 *pfAllocated = true;
619 return VINF_SUCCESS;
620 }
621
622 cb -= pThis->DvmDisk.cbSector;
623 off += pThis->DvmDisk.cbSector;
624 }
625 }
626
627 *pfAllocated = false;
628 return rc;
629}
630
631RTDECL(int) RTDvmMapQueryTableLocations(RTDVM hVolMgr, uint32_t fFlags,
632 PRTDVMTABLELOCATION paLocations, size_t cLocations, size_t *pcActual)
633{
634 PRTDVMINTERNAL pThis = hVolMgr;
635
636 /*
637 * Input validation.
638 */
639 if (cLocations)
640 {
641 AssertPtrReturn(paLocations, VERR_INVALID_POINTER);
642 if (pcActual)
643 {
644 AssertPtrReturn(pcActual, VERR_INVALID_POINTER);
645 *pcActual = 0;
646 }
647 }
648 else
649 {
650 AssertPtrReturn(pcActual, VERR_INVALID_POINTER);
651 *pcActual = 0;
652 }
653 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
654 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, VERR_INVALID_HANDLE);
655 AssertReturn(!(fFlags & ~RTDVMMAPQTABLOC_F_VALID_MASK), VERR_INVALID_FLAGS);
656
657 /*
658 * Pass it down to the format backend.
659 */
660 return pThis->pDvmFmtOps->pfnQueryTableLocations(pThis->hVolMgrFmt, fFlags, paLocations, cLocations, pcActual);
661}
662
663RTDECL(uint32_t) RTDvmVolumeRetain(RTDVMVOLUME hVol)
664{
665 PRTDVMVOLUMEINTERNAL pThis = hVol;
666 AssertPtrReturn(pThis, UINT32_MAX);
667 AssertReturn(pThis->u32Magic == RTDVMVOLUME_MAGIC, UINT32_MAX);
668
669 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
670 AssertMsg(cRefs >= 1 && cRefs < _1M, ("%#x %p\n", cRefs, pThis));
671 if (cRefs == 1)
672 RTDvmRetain(pThis->pVolMgr);
673 return cRefs;
674}
675
676RTDECL(uint32_t) RTDvmVolumeRelease(RTDVMVOLUME hVol)
677{
678 PRTDVMVOLUMEINTERNAL pThis = hVol;
679 if (pThis == NIL_RTDVMVOLUME)
680 return 0;
681 AssertPtrReturn(pThis, UINT32_MAX);
682 AssertReturn(pThis->u32Magic == RTDVMVOLUME_MAGIC, UINT32_MAX);
683
684 uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
685 AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pThis));
686 if (cRefs == 0)
687 {
688 /* Release the volume manager. */
689 pThis->pfnQueryBlockStatus = NULL;
690 RTDvmRelease(pThis->pVolMgr);
691 }
692 return cRefs;
693}
694
695RTDECL(void) RTDvmVolumeSetQueryBlockStatusCallback(RTDVMVOLUME hVol,
696 PFNDVMVOLUMEQUERYBLOCKSTATUS pfnQueryBlockStatus,
697 void *pvUser)
698{
699 PRTDVMVOLUMEINTERNAL pThis = hVol;
700 AssertPtrReturnVoid(pThis);
701 AssertReturnVoid(pThis->u32Magic == RTDVMVOLUME_MAGIC);
702
703 pThis->pfnQueryBlockStatus = pfnQueryBlockStatus;
704 pThis->pvUser = pvUser;
705}
706
707RTDECL(uint64_t) RTDvmVolumeGetSize(RTDVMVOLUME hVol)
708{
709 PRTDVMVOLUMEINTERNAL pThis = hVol;
710 AssertPtrReturn(pThis, 0);
711 AssertReturn(pThis->u32Magic == RTDVMVOLUME_MAGIC, 0);
712
713 return pThis->pVolMgr->pDvmFmtOps->pfnVolumeGetSize(pThis->hVolFmt);
714}
715
716RTDECL(int) RTDvmVolumeQueryName(RTDVMVOLUME hVol, char **ppszVolName)
717{
718 PRTDVMVOLUMEINTERNAL pThis = hVol;
719 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
720 AssertReturn(pThis->u32Magic == RTDVMVOLUME_MAGIC, VERR_INVALID_HANDLE);
721 AssertReturn(ppszVolName, VERR_INVALID_POINTER);
722
723 return pThis->pVolMgr->pDvmFmtOps->pfnVolumeQueryName(pThis->hVolFmt, ppszVolName);
724}
725
726RTDECL(RTDVMVOLTYPE) RTDvmVolumeGetType(RTDVMVOLUME hVol)
727{
728 PRTDVMVOLUMEINTERNAL pThis = hVol;
729 AssertPtrReturn(pThis, RTDVMVOLTYPE_INVALID);
730 AssertReturn(pThis->u32Magic == RTDVMVOLUME_MAGIC, RTDVMVOLTYPE_INVALID);
731
732 return pThis->pVolMgr->pDvmFmtOps->pfnVolumeGetType(pThis->hVolFmt);
733}
734
735RTDECL(uint64_t) RTDvmVolumeGetFlags(RTDVMVOLUME hVol)
736{
737 PRTDVMVOLUMEINTERNAL pThis = hVol;
738 AssertPtrReturn(pThis, UINT64_MAX);
739 AssertReturn(pThis->u32Magic == RTDVMVOLUME_MAGIC, UINT64_MAX);
740
741 return pThis->pVolMgr->pDvmFmtOps->pfnVolumeGetFlags(pThis->hVolFmt);
742}
743
744RTDECL(int) RTDvmVolumeQueryRange(RTDVMVOLUME hVol, uint64_t *poffStart, uint64_t *poffLast)
745{
746 PRTDVMVOLUMEINTERNAL pThis = hVol;
747 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
748 AssertReturn(pThis->u32Magic == RTDVMVOLUME_MAGIC, VERR_INVALID_HANDLE);
749 AssertPtrReturn(poffStart, VERR_INVALID_POINTER);
750 AssertPtrReturn(poffLast, VERR_INVALID_POINTER);
751
752 return pThis->pVolMgr->pDvmFmtOps->pfnVolumeQueryRange(pThis->hVolFmt, poffStart, poffLast);
753}
754
755RTDECL(int) RTDvmVolumeQueryTableLocation(RTDVMVOLUME hVol, uint64_t *poffTable, uint64_t *pcbTable)
756{
757 PRTDVMVOLUMEINTERNAL pThis = hVol;
758 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
759 AssertReturn(pThis->u32Magic == RTDVMVOLUME_MAGIC, VERR_INVALID_HANDLE);
760 AssertPtrReturn(poffTable, VERR_INVALID_POINTER);
761 AssertPtrReturn(pcbTable, VERR_INVALID_POINTER);
762
763 return pThis->pVolMgr->pDvmFmtOps->pfnVolumeQueryTableLocation(pThis->hVolFmt, poffTable, pcbTable);
764}
765
766RTDECL(uint32_t) RTDvmVolumeGetIndex(RTDVMVOLUME hVol, RTDVMVOLIDX enmIndex)
767{
768 PRTDVMVOLUMEINTERNAL pThis = hVol;
769 AssertPtrReturn(pThis, UINT32_MAX);
770 AssertReturn(pThis->u32Magic == RTDVMVOLUME_MAGIC, UINT32_MAX);
771 AssertReturn(enmIndex > RTDVMVOLIDX_INVALID && enmIndex < RTDVMVOLIDX_END, UINT32_MAX);
772
773 if (enmIndex == RTDVMVOLIDX_HOST)
774 {
775#ifdef RT_OS_WINDOWS
776 enmIndex = RTDVMVOLIDX_USER_VISIBLE;
777#elif defined(RT_OS_LINUX) \
778 || defined(RT_OS_FREEBSD) \
779 || defined(RT_OS_NETBSD) \
780 || defined(RT_OS_SOLARIS) \
781 || defined(RT_OS_DARWIN) \
782 || defined(RT_OS_OS2) /*whatever*/
783/* Darwing and freebsd matches the linux algo. Solaris matches linux algo partially, at least, in the part we use. */
784 enmIndex = RTDVMVOLIDX_LINUX;
785#else
786# error "PORTME"
787#endif
788 }
789
790 return pThis->pVolMgr->pDvmFmtOps->pfnVolumeGetIndex(pThis->hVolFmt, enmIndex);
791}
792
793/**
794 * Helper for RTDvmVolumeQueryProp.
795 */
796static void rtDvmReturnInteger(void *pvDst, size_t cbDst, PRTUINT64U pSrc, size_t cbSrc)
797{
798 /* Read the source: */
799 uint64_t uSrc;
800 switch (cbSrc)
801 {
802 case sizeof(uint8_t): uSrc = (uint8_t)pSrc->Words.w0; break;
803 case sizeof(uint16_t): uSrc = pSrc->Words.w0; break;
804 case sizeof(uint32_t): uSrc = pSrc->s.Lo; break;
805 default: AssertFailed(); RT_FALL_THROUGH();
806 case sizeof(uint64_t): uSrc = pSrc->u; break;
807 }
808
809 /* Write the destination: */
810 switch (cbDst)
811 {
812 default: AssertFailed(); RT_FALL_THROUGH();
813 case sizeof(uint8_t): *(uint8_t *)pvDst = (uint8_t)uSrc; break;
814 case sizeof(uint16_t): *(uint16_t *)pvDst = (uint16_t)uSrc; break;
815 case sizeof(uint32_t): *(uint32_t *)pvDst = (uint32_t)uSrc; break;
816 case sizeof(uint64_t): *(uint64_t *)pvDst = uSrc; break;
817 }
818}
819
820RTDECL(int) RTDvmVolumeQueryProp(RTDVMVOLUME hVol, RTDVMVOLPROP enmProperty, void *pvBuf, size_t cbBuf, size_t *pcbBuf)
821{
822 PRTDVMVOLUMEINTERNAL pThis = hVol;
823 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
824 AssertReturn(pThis->u32Magic == RTDVMVOLUME_MAGIC, VERR_INVALID_HANDLE);
825 size_t cbBufFallback = 0;
826 if (pcbBuf == NULL)
827 pcbBuf = &cbBufFallback;
828 AssertReturnStmt(enmProperty > RTDVMVOLPROP_INVALID && enmProperty < RTDVMVOLPROP_END, *pcbBuf = 0, VERR_INVALID_FUNCTION);
829
830 switch (enmProperty)
831 {
832 /* 8, 16, 32 or 64 bit sized integers: */
833 case RTDVMVOLPROP_MBR_FIRST_HEAD:
834 case RTDVMVOLPROP_MBR_FIRST_SECTOR:
835 case RTDVMVOLPROP_MBR_LAST_HEAD:
836 case RTDVMVOLPROP_MBR_LAST_SECTOR:
837 case RTDVMVOLPROP_MBR_TYPE:
838 {
839 *pcbBuf = sizeof(uint8_t);
840 AssertReturn( cbBuf == sizeof(uint8_t)
841 || cbBuf == sizeof(uint16_t)
842 || cbBuf == sizeof(uint32_t)
843 || cbBuf == sizeof(uint64_t), VERR_INVALID_PARAMETER);
844 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
845
846 RTUINT64U Union64 = {0};
847 int rc = pThis->pVolMgr->pDvmFmtOps->pfnVolumeQueryProp(pThis->hVolFmt, enmProperty, &Union64, cbBuf, pcbBuf);
848 rtDvmReturnInteger(pvBuf, cbBuf, &Union64, *pcbBuf);
849 return rc;
850 }
851
852 /* 16, 32 or 64 bit sized integers: */
853 case RTDVMVOLPROP_MBR_FIRST_CYLINDER:
854 case RTDVMVOLPROP_MBR_LAST_CYLINDER:
855 {
856 *pcbBuf = sizeof(uint16_t);
857 AssertReturn( cbBuf == sizeof(uint16_t)
858 || cbBuf == sizeof(uint32_t)
859 || cbBuf == sizeof(uint64_t), VERR_INVALID_PARAMETER);
860 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
861
862 RTUINT64U Union64 = {0};
863 int rc = pThis->pVolMgr->pDvmFmtOps->pfnVolumeQueryProp(pThis->hVolFmt, enmProperty, &Union64, cbBuf, pcbBuf);
864 rtDvmReturnInteger(pvBuf, cbBuf, &Union64, *pcbBuf);
865 return rc;
866 }
867
868 /* RTUUIDs: */
869 case RTDVMVOLPROP_GPT_TYPE:
870 case RTDVMVOLPROP_GPT_UUID:
871 {
872 *pcbBuf = sizeof(RTUUID);
873 AssertReturn(cbBuf == sizeof(RTUUID), VERR_INVALID_PARAMETER);
874 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
875
876 RTUUID Uuid = RTUUID_INITIALIZE_NULL;
877 int rc = pThis->pVolMgr->pDvmFmtOps->pfnVolumeQueryProp(pThis->hVolFmt, enmProperty, &Uuid, sizeof(RTUUID), pcbBuf);
878 memcpy(pvBuf, &Uuid, sizeof(Uuid));
879 return rc;
880 }
881
882 case RTDVMVOLPROP_INVALID:
883 case RTDVMVOLPROP_END:
884 case RTDVMVOLPROP_32BIT_HACK:
885 break;
886 /* No default case! */
887 }
888 AssertFailed();
889 return VERR_NOT_SUPPORTED;
890}
891
892RTDECL(uint64_t) RTDvmVolumeGetPropU64(RTDVMVOLUME hVol, RTDVMVOLPROP enmProperty, uint64_t uDefault)
893{
894 uint64_t uValue = uDefault;
895 int rc = RTDvmVolumeQueryProp(hVol, enmProperty, &uValue, sizeof(uValue), NULL);
896 if (RT_SUCCESS(rc))
897 return uValue;
898 AssertMsg(rc == VERR_NOT_SUPPORTED || rc == VERR_NOT_FOUND, ("%Rrc enmProperty=%d\n", rc, enmProperty));
899 return uDefault;
900}
901
902RTDECL(int) RTDvmVolumeRead(RTDVMVOLUME hVol, uint64_t off, void *pvBuf, size_t cbRead)
903{
904 PRTDVMVOLUMEINTERNAL pThis = hVol;
905 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
906 AssertReturn(pThis->u32Magic == RTDVMVOLUME_MAGIC, VERR_INVALID_HANDLE);
907 AssertReturn(pvBuf, VERR_INVALID_POINTER);
908 AssertReturn(cbRead > 0, VERR_INVALID_PARAMETER);
909
910 return pThis->pVolMgr->pDvmFmtOps->pfnVolumeRead(pThis->hVolFmt, off, pvBuf, cbRead);
911}
912
913RTDECL(int) RTDvmVolumeWrite(RTDVMVOLUME hVol, uint64_t off, const void *pvBuf, size_t cbWrite)
914{
915 PRTDVMVOLUMEINTERNAL pThis = hVol;
916 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
917 AssertReturn(pThis->u32Magic == RTDVMVOLUME_MAGIC, VERR_INVALID_HANDLE);
918 AssertReturn(pvBuf, VERR_INVALID_POINTER);
919 AssertReturn(cbWrite > 0, VERR_INVALID_PARAMETER);
920
921 return pThis->pVolMgr->pDvmFmtOps->pfnVolumeWrite(pThis->hVolFmt, off, pvBuf, cbWrite);
922}
923
924RTDECL(const char *) RTDvmVolumeTypeGetDescr(RTDVMVOLTYPE enmVolType)
925{
926 AssertReturn(enmVolType >= RTDVMVOLTYPE_INVALID && enmVolType < RTDVMVOLTYPE_END, NULL);
927
928 return g_apszDvmVolTypes[enmVolType];
929}
930
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