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