VirtualBox

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

Last change on this file since 72168 was 69967, checked in by vboxsync, 7 years ago

IPRT/dvm: Don't leak volume related stuff.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 21.8 KB
Line 
1/* $Id: dvm.cpp 69967 2017-12-07 10:38:48Z vboxsync $ */
2/** @file
3 * IPRT Disk Volume Management API (DVM) - generic code.
4 */
5
6/*
7 * Copyright (C) 2011-2017 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*********************************************************************************************************************************/
96extern RTDVMFMTOPS g_rtDvmFmtMbr;
97extern RTDVMFMTOPS g_rtDvmFmtGpt;
98extern RTDVMFMTOPS g_rtDvmFmtBsdLbl;
99
100/**
101 * Supported volume formats.
102 */
103static PCRTDVMFMTOPS const g_aDvmFmts[] =
104{
105 &g_rtDvmFmtMbr,
106 &g_rtDvmFmtGpt,
107 &g_rtDvmFmtBsdLbl
108};
109
110/**
111 * Descriptions of the volume types.
112 *
113 * This is indexed by RTDVMVOLTYPE.
114 */
115static const char * const g_apszDvmVolTypes[] =
116{
117 "Invalid",
118 "Unknown",
119 "NTFS",
120 "FAT12",
121 "FAT16",
122 "FAT32",
123 "Linux swap",
124 "Linux native",
125 "Linux LVM",
126 "Linux SoftRaid",
127 "FreeBSD",
128 "NetBSD",
129 "OpenBSD",
130 "Mac OS X HFS or HFS+",
131 "Solaris"
132};
133AssertCompile(RT_ELEMENTS(g_apszDvmVolTypes) == RTDVMVOLTYPE_END);
134
135
136/**
137 * Creates a new volume.
138 *
139 * @returns IPRT status code.
140 * @param pThis The DVM map instance.
141 * @param hVolFmt The format specific volume handle.
142 * @param phVol Where to store the generic volume handle on success.
143 */
144static int rtDvmVolumeCreate(PRTDVMINTERNAL pThis, RTDVMVOLUMEFMT hVolFmt, PRTDVMVOLUME phVol)
145{
146 PRTDVMVOLUMEINTERNAL pVol = (PRTDVMVOLUMEINTERNAL)RTMemAllocZ(sizeof(RTDVMVOLUMEINTERNAL));
147 if (pVol)
148 {
149 pVol->u32Magic = RTDVMVOLUME_MAGIC;
150 pVol->cRefs = 0;
151 pVol->pVolMgr = pThis;
152 pVol->hVolFmt = hVolFmt;
153
154 *phVol = pVol;
155 return VINF_SUCCESS;
156 }
157 return VERR_NO_MEMORY;
158}
159
160/**
161 * Destroys a volume handle.
162 *
163 * @param pThis The volume manager instance.
164 * @param pVol The volume to destroy.
165 */
166static void rtDvmVolumeDestroy(PRTDVMINTERNAL pThis, PRTDVMVOLUMEINTERNAL pVol)
167{
168 AssertPtr(pThis);
169 AssertPtr(pThis->pDvmFmtOps);
170 Assert(pVol->pVolMgr == pThis);
171
172 /* Close the volume. */
173 pThis->pDvmFmtOps->pfnVolumeClose(pVol->hVolFmt);
174
175 pVol->u32Magic = RTDVMVOLUME_MAGIC_DEAD;
176 pVol->pVolMgr = NULL;
177 pVol->hVolFmt = NIL_RTDVMVOLUMEFMT;
178 RTMemFree(pVol);
179}
180
181
182RTDECL(int) RTDvmCreate(PRTDVM phVolMgr, RTVFSFILE hVfsFile, uint32_t cbSector, uint32_t fFlags)
183{
184 AssertMsgReturn(!(fFlags & ~DVM_FLAGS_VALID_MASK), ("Invalid flags given %#x\n", fFlags), VERR_INVALID_FLAGS);
185 uint32_t cRefs = RTVfsFileRetain(hVfsFile);
186 AssertReturn(cRefs != UINT32_MAX, VERR_INVALID_HANDLE);
187
188 uint64_t cbDisk;
189 int rc = RTVfsFileGetSize(hVfsFile, &cbDisk);
190 if (RT_SUCCESS(rc))
191 {
192 PRTDVMINTERNAL pThis = (PRTDVMINTERNAL)RTMemAllocZ(sizeof(RTDVMINTERNAL));
193 if (pThis)
194 {
195 pThis->u32Magic = RTDVM_MAGIC;
196 pThis->DvmDisk.cbDisk = cbDisk;
197 pThis->DvmDisk.cbSector = cbSector;
198 pThis->DvmDisk.hVfsFile = hVfsFile;
199
200 pThis->pDvmFmtOps = NULL;
201 pThis->hVolMgrFmt = NIL_RTDVMFMT;
202 pThis->fFlags = fFlags;
203 pThis->cRefs = 1;
204 RTListInit(&pThis->VolumeList);
205
206 *phVolMgr = pThis;
207 return VINF_SUCCESS;
208 }
209 rc = VERR_NO_MEMORY;
210 }
211 RTVfsFileRelease(hVfsFile);
212 return rc;
213}
214
215
216RTDECL(uint32_t) RTDvmRetain(RTDVM hVolMgr)
217{
218 PRTDVMINTERNAL pThis = hVolMgr;
219 AssertPtrReturn(pThis, UINT32_MAX);
220 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, UINT32_MAX);
221
222 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
223 AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p\n", cRefs, pThis));
224 return cRefs;
225}
226
227/**
228 * Destroys a volume manager handle.
229 *
230 * @param pThis The volume manager to destroy.
231 */
232static void rtDvmDestroy(PRTDVMINTERNAL pThis)
233{
234 pThis->u32Magic = RTDVM_MAGIC_DEAD;
235
236 if (pThis->hVolMgrFmt != NIL_RTDVMFMT)
237 {
238 AssertPtr(pThis->pDvmFmtOps);
239
240 /* */
241 PRTDVMVOLUMEINTERNAL pItNext, pIt;
242 RTListForEachSafe(&pThis->VolumeList, pIt, pItNext, RTDVMVOLUMEINTERNAL, VolumeNode)
243 {
244 RTListNodeRemove(&pIt->VolumeNode);
245 rtDvmVolumeDestroy(pThis, pIt);
246 }
247
248 /* Let the backend do it's own cleanup first. */
249 pThis->pDvmFmtOps->pfnClose(pThis->hVolMgrFmt);
250 pThis->hVolMgrFmt = NIL_RTDVMFMT;
251 pThis->pDvmFmtOps = NULL;
252 }
253
254 pThis->DvmDisk.cbDisk = 0;
255 pThis->DvmDisk.cbSector = 0;
256 if (pThis->DvmDisk.hVfsFile != NIL_RTVFSFILE)
257 {
258 RTVfsFileRelease(pThis->DvmDisk.hVfsFile);
259 pThis->DvmDisk.hVfsFile = NIL_RTVFSFILE;
260 }
261
262 RTMemFree(pThis);
263}
264
265RTDECL(uint32_t) RTDvmRelease(RTDVM hVolMgr)
266{
267 PRTDVMINTERNAL pThis = hVolMgr;
268 if (pThis == NIL_RTDVM)
269 return 0;
270 AssertPtrReturn(pThis, UINT32_MAX);
271 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, UINT32_MAX);
272
273 uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
274 AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pThis));
275 if (cRefs == 0)
276 rtDvmDestroy(pThis);
277 return cRefs;
278}
279
280RTDECL(int) RTDvmMapOpen(RTDVM hVolMgr)
281{
282 PRTDVMINTERNAL pThis = hVolMgr;
283 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
284 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, VERR_INVALID_HANDLE);
285 AssertReturn(pThis->hVolMgrFmt == NIL_RTDVMFMT, VERR_WRONG_ORDER);
286
287 Assert(!pThis->pDvmFmtOps);
288
289 /*
290 * Let each format backend have a go at the disk, pick the one which scores the highest.
291 */
292 int rc = VINF_SUCCESS;
293 uint32_t uScoreMax = RTDVM_MATCH_SCORE_UNSUPPORTED;
294 PCRTDVMFMTOPS pDvmFmtOpsMatch = NULL;
295 for (unsigned i = 0; i < RT_ELEMENTS(g_aDvmFmts); i++)
296 {
297 uint32_t uScore = 0;
298 PCRTDVMFMTOPS pDvmFmtOps = g_aDvmFmts[i];
299
300 rc = pDvmFmtOps->pfnProbe(&pThis->DvmDisk, &uScore);
301 if (RT_SUCCESS(rc))
302 {
303 if (uScore > uScoreMax)
304 {
305 pDvmFmtOpsMatch = pDvmFmtOps;
306 uScoreMax = uScore;
307 }
308 }
309 else
310 return rc;
311 }
312 if (uScoreMax > RTDVM_MATCH_SCORE_UNSUPPORTED)
313 {
314 AssertPtr(pDvmFmtOpsMatch);
315
316 /*
317 * Open the format.
318 */
319 rc = pDvmFmtOpsMatch->pfnOpen(&pThis->DvmDisk, &pThis->hVolMgrFmt);
320 if (RT_SUCCESS(rc))
321 {
322 pThis->pDvmFmtOps = pDvmFmtOpsMatch;
323
324 /*
325 * Construct volume list (we're done if none).
326 */
327 uint32_t cVols = pThis->pDvmFmtOps->pfnGetValidVolumes(pThis->hVolMgrFmt);
328 if (cVols == 0)
329 return VINF_SUCCESS;
330
331 /* First volume. */
332 RTDVMVOLUMEFMT hVolFmt = NIL_RTDVMVOLUMEFMT;
333 rc = pThis->pDvmFmtOps->pfnQueryFirstVolume(pThis->hVolMgrFmt, &hVolFmt);
334 if (RT_SUCCESS(rc))
335 {
336 for (;;)
337 {
338 PRTDVMVOLUMEINTERNAL pVol = NULL;
339 rc = rtDvmVolumeCreate(pThis, hVolFmt, &pVol);
340 if (RT_FAILURE(rc))
341 {
342 pThis->pDvmFmtOps->pfnVolumeClose(hVolFmt);
343 break;
344 }
345 RTListAppend(&pThis->VolumeList, &pVol->VolumeNode);
346
347 /* Done?*/
348 cVols--;
349 if (cVols < 1)
350 return VINF_SUCCESS;
351
352 /* Next volume. */
353 rc = pThis->pDvmFmtOps->pfnQueryNextVolume(pThis->hVolMgrFmt, pVol->hVolFmt, &hVolFmt);
354 if (RT_FAILURE(rc))
355 break;
356 }
357
358 /* Bail out. */
359 PRTDVMVOLUMEINTERNAL pItNext, pIt;
360 RTListForEachSafe(&pThis->VolumeList, pIt, pItNext, RTDVMVOLUMEINTERNAL, VolumeNode)
361 {
362 RTListNodeRemove(&pIt->VolumeNode);
363 rtDvmVolumeDestroy(pThis, pIt);
364 }
365 }
366
367 /** @todo shouldn't we close the format too here? */
368 }
369 }
370 else
371 rc = VERR_NOT_SUPPORTED;
372 return rc;
373}
374
375RTDECL(int) RTDvmMapInitialize(RTDVM hVolMgr, const char *pszFmt)
376{
377 PRTDVMINTERNAL pThis = hVolMgr;
378 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
379 AssertPtrReturn(pszFmt, VERR_INVALID_POINTER);
380 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, VERR_INVALID_HANDLE);
381 AssertReturn(pThis->hVolMgrFmt == NIL_RTDVMFMT, VERR_WRONG_ORDER);
382
383 for (unsigned i = 0; i < RT_ELEMENTS(g_aDvmFmts); i++)
384 {
385 PCRTDVMFMTOPS pDvmFmtOps = g_aDvmFmts[i];
386 if (!RTStrCmp(pDvmFmtOps->pszFmt, pszFmt))
387 {
388 int rc = pDvmFmtOps->pfnInitialize(&pThis->DvmDisk, &pThis->hVolMgrFmt);
389 if (RT_SUCCESS(rc))
390 pThis->pDvmFmtOps = pDvmFmtOps;
391 return rc;
392 }
393 }
394 return VERR_NOT_SUPPORTED;
395}
396
397RTDECL(const char *) RTDvmMapGetFormatName(RTDVM hVolMgr)
398{
399 PRTDVMINTERNAL pThis = hVolMgr;
400 AssertPtrReturn(pThis, NULL);
401 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, NULL);
402 AssertReturn(pThis->hVolMgrFmt != NIL_RTDVMFMT, NULL);
403
404 return pThis->pDvmFmtOps->pszFmt;
405}
406
407RTDECL(RTDVMFORMATTYPE) RTDvmMapGetFormatType(RTDVM hVolMgr)
408{
409 PRTDVMINTERNAL pThis = hVolMgr;
410 AssertPtrReturn(pThis, RTDVMFORMATTYPE_INVALID);
411 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, RTDVMFORMATTYPE_INVALID);
412 AssertReturn(pThis->hVolMgrFmt != NIL_RTDVMFMT, RTDVMFORMATTYPE_INVALID);
413
414 return pThis->pDvmFmtOps->enmFormat;
415}
416
417RTDECL(uint32_t) RTDvmMapGetValidVolumes(RTDVM hVolMgr)
418{
419 PRTDVMINTERNAL pThis = hVolMgr;
420 AssertPtrReturn(pThis, UINT32_MAX);
421 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, UINT32_MAX);
422 AssertReturn(pThis->hVolMgrFmt != NIL_RTDVMFMT, UINT32_MAX);
423
424 return pThis->pDvmFmtOps->pfnGetValidVolumes(pThis->hVolMgrFmt);
425}
426
427RTDECL(uint32_t) RTDvmMapGetMaxVolumes(RTDVM hVolMgr)
428{
429 PRTDVMINTERNAL pThis = hVolMgr;
430 AssertPtrReturn(pThis, UINT32_MAX);
431 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, UINT32_MAX);
432 AssertReturn(pThis->hVolMgrFmt != NIL_RTDVMFMT, UINT32_MAX);
433
434 return pThis->pDvmFmtOps->pfnGetMaxVolumes(pThis->hVolMgrFmt);
435}
436
437RTDECL(int) RTDvmMapQueryFirstVolume(RTDVM hVolMgr, PRTDVMVOLUME phVol)
438{
439 PRTDVMINTERNAL pThis = hVolMgr;
440 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
441 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, VERR_INVALID_HANDLE);
442 AssertReturn(pThis->hVolMgrFmt != NIL_RTDVMFMT, VERR_INVALID_HANDLE);
443 AssertPtrReturn(phVol, VERR_INVALID_POINTER);
444
445 int rc = VERR_DVM_MAP_EMPTY;
446 PRTDVMVOLUMEINTERNAL pVol = RTListGetFirst(&pThis->VolumeList, RTDVMVOLUMEINTERNAL, VolumeNode);
447 if (pVol)
448 {
449 rc = VINF_SUCCESS;
450 RTDvmVolumeRetain(pVol);
451 *phVol = pVol;
452 }
453
454 return rc;
455}
456
457RTDECL(int) RTDvmMapQueryNextVolume(RTDVM hVolMgr, RTDVMVOLUME hVol, PRTDVMVOLUME phVolNext)
458{
459 PRTDVMINTERNAL pThis = hVolMgr;
460 PRTDVMVOLUMEINTERNAL pVol = hVol;
461 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
462 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, VERR_INVALID_HANDLE);
463 AssertReturn(pThis->hVolMgrFmt != NIL_RTDVMFMT, VERR_INVALID_HANDLE);
464 AssertPtrReturn(pVol, VERR_INVALID_HANDLE);
465 AssertReturn(pVol->u32Magic == RTDVMVOLUME_MAGIC, VERR_INVALID_HANDLE);
466 AssertPtrReturn(phVolNext, VERR_INVALID_POINTER);
467
468 int rc = VERR_DVM_MAP_NO_VOLUME;
469 PRTDVMVOLUMEINTERNAL pVolNext = RTListGetNext(&pThis->VolumeList, pVol, RTDVMVOLUMEINTERNAL, VolumeNode);
470 if (pVolNext)
471 {
472 rc = VINF_SUCCESS;
473 RTDvmVolumeRetain(pVolNext);
474 *phVolNext = pVolNext;
475 }
476
477 return rc;
478}
479
480RTDECL(int) RTDvmMapQueryBlockStatus(RTDVM hVolMgr, uint64_t off, uint64_t cb, bool *pfAllocated)
481{
482 PRTDVMINTERNAL pThis = hVolMgr;
483
484 /*
485 * Input validation.
486 */
487 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
488 AssertPtrReturn(pfAllocated, VERR_INVALID_POINTER);
489 AssertReturn(pThis->u32Magic == RTDVM_MAGIC, VERR_INVALID_HANDLE);
490 AssertReturn(pThis->hVolMgrFmt != NIL_RTDVMFMT, VERR_WRONG_ORDER);
491 AssertMsgReturn( off <= pThis->DvmDisk.cbDisk
492 || cb <= pThis->DvmDisk.cbDisk
493 || off + cb <= pThis->DvmDisk.cbDisk,
494 ("off=%#RX64 cb=%#RX64 cbDisk=%#RX64\n", off, cb, pThis->DvmDisk.cbDisk),
495 VERR_OUT_OF_RANGE);
496
497 /*
498 * Check whether the range is inuse by the volume manager metadata first.
499 */
500 int rc = pThis->pDvmFmtOps->pfnQueryRangeUse(pThis->hVolMgrFmt, off, cb, pfAllocated);
501 if (RT_FAILURE(rc) || *pfAllocated)
502 return rc;
503
504 /*
505 * Not used by volume manager metadata, so work thru the specified range one
506 * volume / void (free space) at a time. All must be unallocated for us to
507 * reach the end, we return immediately if any portion is allocated.
508 */
509 while (cb > 0)
510 {
511 /*
512 * Search through all volumes.
513 *
514 * It is not possible to get all start sectors and sizes of all volumes
515 * here because volumes can be scattered around the disk for certain formats.
516 * Linux LVM is one example, it extents of logical volumes don't need to be
517 * contiguous on the medium.
518 */
519 bool fVolFound = false;
520 PRTDVMVOLUMEINTERNAL pVol;
521 RTListForEach(&pThis->VolumeList, pVol, RTDVMVOLUMEINTERNAL, VolumeNode)
522 {
523 uint64_t cbIntersect;
524 uint64_t offVol;
525 bool fIntersect = pThis->pDvmFmtOps->pfnVolumeIsRangeIntersecting(pVol->hVolFmt, off, cb, &offVol, &cbIntersect);
526 if (fIntersect)
527 {
528 fVolFound = true;
529 if (pVol->pfnQueryBlockStatus)
530 {
531 bool fVolAllocated = true;
532 rc = pVol->pfnQueryBlockStatus(pVol->pvUser, offVol, cbIntersect, &fVolAllocated);
533 if (RT_FAILURE(rc) || fVolAllocated)
534 {
535 *pfAllocated = true;
536 return rc;
537 }
538 }
539 else if (!(pThis->fFlags & DVM_FLAGS_NO_STATUS_CALLBACK_MARK_AS_UNUSED))
540 {
541 *pfAllocated = true;
542 return VINF_SUCCESS;
543 }
544 /* else, flag is set, continue. */
545
546 cb -= cbIntersect;
547 off += cbIntersect;
548 break;
549 }
550 }
551
552 if (!fVolFound)
553 {
554 if (pThis->fFlags & DVM_FLAGS_UNUSED_SPACE_MARK_AS_USED)
555 {
556 *pfAllocated = true;
557 return VINF_SUCCESS;
558 }
559
560 cb -= pThis->DvmDisk.cbSector;
561 off += pThis->DvmDisk.cbSector;
562 }
563 }
564
565 *pfAllocated = false;
566 return rc;
567}
568
569RTDECL(uint32_t) RTDvmVolumeRetain(RTDVMVOLUME hVol)
570{
571 PRTDVMVOLUMEINTERNAL pThis = hVol;
572 AssertPtrReturn(pThis, UINT32_MAX);
573 AssertReturn(pThis->u32Magic == RTDVMVOLUME_MAGIC, UINT32_MAX);
574
575 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
576 AssertMsg(cRefs >= 1 && cRefs < _1M, ("%#x %p\n", cRefs, pThis));
577 if (cRefs == 1)
578 RTDvmRetain(pThis->pVolMgr);
579 return cRefs;
580}
581
582RTDECL(uint32_t) RTDvmVolumeRelease(RTDVMVOLUME hVol)
583{
584 PRTDVMVOLUMEINTERNAL pThis = hVol;
585 if (pThis == NIL_RTDVMVOLUME)
586 return 0;
587 AssertPtrReturn(pThis, UINT32_MAX);
588 AssertReturn(pThis->u32Magic == RTDVMVOLUME_MAGIC, UINT32_MAX);
589
590 uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
591 AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pThis));
592 if (cRefs == 0)
593 {
594 /* Release the volume manager. */
595 pThis->pfnQueryBlockStatus = NULL;
596 RTDvmRelease(pThis->pVolMgr);
597 }
598 return cRefs;
599}
600
601RTDECL(void) RTDvmVolumeSetQueryBlockStatusCallback(RTDVMVOLUME hVol,
602 PFNDVMVOLUMEQUERYBLOCKSTATUS pfnQueryBlockStatus,
603 void *pvUser)
604{
605 PRTDVMVOLUMEINTERNAL pThis = hVol;
606 AssertPtrReturnVoid(pThis);
607 AssertReturnVoid(pThis->u32Magic == RTDVMVOLUME_MAGIC);
608
609 pThis->pfnQueryBlockStatus = pfnQueryBlockStatus;
610 pThis->pvUser = pvUser;
611}
612
613RTDECL(uint64_t) RTDvmVolumeGetSize(RTDVMVOLUME hVol)
614{
615 PRTDVMVOLUMEINTERNAL pThis = hVol;
616 AssertPtrReturn(pThis, 0);
617 AssertReturn(pThis->u32Magic == RTDVMVOLUME_MAGIC, 0);
618
619 return pThis->pVolMgr->pDvmFmtOps->pfnVolumeGetSize(pThis->hVolFmt);
620}
621
622RTDECL(int) RTDvmVolumeQueryName(RTDVMVOLUME hVol, char **ppszVolName)
623{
624 PRTDVMVOLUMEINTERNAL pThis = hVol;
625 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
626 AssertReturn(pThis->u32Magic == RTDVMVOLUME_MAGIC, VERR_INVALID_HANDLE);
627 AssertReturn(ppszVolName, VERR_INVALID_POINTER);
628
629 return pThis->pVolMgr->pDvmFmtOps->pfnVolumeQueryName(pThis->hVolFmt, ppszVolName);
630}
631
632RTDECL(RTDVMVOLTYPE) RTDvmVolumeGetType(RTDVMVOLUME hVol)
633{
634 PRTDVMVOLUMEINTERNAL pThis = hVol;
635 AssertPtrReturn(pThis, RTDVMVOLTYPE_INVALID);
636 AssertReturn(pThis->u32Magic == RTDVMVOLUME_MAGIC, RTDVMVOLTYPE_INVALID);
637
638 return pThis->pVolMgr->pDvmFmtOps->pfnVolumeGetType(pThis->hVolFmt);
639}
640
641RTDECL(uint64_t) RTDvmVolumeGetFlags(RTDVMVOLUME hVol)
642{
643 PRTDVMVOLUMEINTERNAL pThis = hVol;
644 AssertPtrReturn(pThis, UINT64_MAX);
645 AssertReturn(pThis->u32Magic == RTDVMVOLUME_MAGIC, UINT64_MAX);
646
647 return pThis->pVolMgr->pDvmFmtOps->pfnVolumeGetFlags(pThis->hVolFmt);
648}
649
650RTDECL(int) RTDvmVolumeRead(RTDVMVOLUME hVol, uint64_t off, void *pvBuf, size_t cbRead)
651{
652 PRTDVMVOLUMEINTERNAL pThis = hVol;
653 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
654 AssertReturn(pThis->u32Magic == RTDVMVOLUME_MAGIC, VERR_INVALID_HANDLE);
655 AssertReturn(pvBuf, VERR_INVALID_POINTER);
656 AssertReturn(cbRead > 0, VERR_INVALID_PARAMETER);
657
658 return pThis->pVolMgr->pDvmFmtOps->pfnVolumeRead(pThis->hVolFmt, off, pvBuf, cbRead);
659}
660
661RTDECL(int) RTDvmVolumeWrite(RTDVMVOLUME hVol, uint64_t off, const void *pvBuf, size_t cbWrite)
662{
663 PRTDVMVOLUMEINTERNAL pThis = hVol;
664 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
665 AssertReturn(pThis->u32Magic == RTDVMVOLUME_MAGIC, VERR_INVALID_HANDLE);
666 AssertReturn(pvBuf, VERR_INVALID_POINTER);
667 AssertReturn(cbWrite > 0, VERR_INVALID_PARAMETER);
668
669 return pThis->pVolMgr->pDvmFmtOps->pfnVolumeWrite(pThis->hVolFmt, off, pvBuf, cbWrite);
670}
671
672RTDECL(const char *) RTDvmVolumeTypeGetDescr(RTDVMVOLTYPE enmVolType)
673{
674 AssertReturn(enmVolType >= RTDVMVOLTYPE_INVALID && enmVolType < RTDVMVOLTYPE_END, NULL);
675
676 return g_apszDvmVolTypes[enmVolType];
677}
678
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