1 | /* $Id: MediumImpl.cpp 97864 2022-12-23 21:25:48Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox COM class implementation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #define LOG_GROUP LOG_GROUP_MAIN_MEDIUM
|
---|
29 | #include "MediumImpl.h"
|
---|
30 | #include "MediumIOImpl.h"
|
---|
31 | #include "TokenImpl.h"
|
---|
32 | #include "ProgressImpl.h"
|
---|
33 | #include "SystemPropertiesImpl.h"
|
---|
34 | #include "VirtualBoxImpl.h"
|
---|
35 | #include "ExtPackManagerImpl.h"
|
---|
36 |
|
---|
37 | #include "AutoCaller.h"
|
---|
38 | #include "Global.h"
|
---|
39 | #include "LoggingNew.h"
|
---|
40 | #include "ThreadTask.h"
|
---|
41 | #include "VBox/com/MultiResult.h"
|
---|
42 | #include "VBox/com/ErrorInfo.h"
|
---|
43 |
|
---|
44 | #include <VBox/err.h>
|
---|
45 | #include <VBox/settings.h>
|
---|
46 |
|
---|
47 | #include <iprt/param.h>
|
---|
48 | #include <iprt/path.h>
|
---|
49 | #include <iprt/file.h>
|
---|
50 | #include <iprt/cpp/utils.h>
|
---|
51 | #include <iprt/memsafer.h>
|
---|
52 | #include <iprt/base64.h>
|
---|
53 | #include <iprt/vfs.h>
|
---|
54 | #include <iprt/fsvfs.h>
|
---|
55 |
|
---|
56 | #include <VBox/vd.h>
|
---|
57 |
|
---|
58 | #include <algorithm>
|
---|
59 | #include <list>
|
---|
60 | #include <set>
|
---|
61 | #include <map>
|
---|
62 |
|
---|
63 |
|
---|
64 | typedef std::list<Guid> GuidList;
|
---|
65 |
|
---|
66 |
|
---|
67 | #ifdef VBOX_WITH_EXTPACK
|
---|
68 | static const char g_szVDPlugin[] = "VDPluginCrypt";
|
---|
69 | #endif
|
---|
70 |
|
---|
71 |
|
---|
72 | ////////////////////////////////////////////////////////////////////////////////
|
---|
73 | //
|
---|
74 | // Medium data definition
|
---|
75 | //
|
---|
76 | ////////////////////////////////////////////////////////////////////////////////
|
---|
77 | #if __cplusplus < 201700 && RT_GNUC_PREREQ(11,0) /* gcc/libstdc++ 12.1.1 errors out here because unary_function is deprecated */
|
---|
78 | # pragma GCC diagnostic push
|
---|
79 | # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
---|
80 | #endif
|
---|
81 |
|
---|
82 | struct SnapshotRef
|
---|
83 | {
|
---|
84 | /** Equality predicate for stdc++. */
|
---|
85 | struct EqualsTo
|
---|
86 | #if __cplusplus < 201700 /* deprecated in C++11, removed in C++17. */
|
---|
87 | : public std::unary_function <SnapshotRef, bool>
|
---|
88 | #endif
|
---|
89 | {
|
---|
90 | explicit EqualsTo(const Guid &aSnapshotId) : snapshotId(aSnapshotId) {}
|
---|
91 |
|
---|
92 | #if __cplusplus < 201700 /* deprecated in C++11, removed in C++17. */
|
---|
93 | bool operator()(const argument_type &aThat) const
|
---|
94 | #else
|
---|
95 | bool operator()(const SnapshotRef &aThat) const
|
---|
96 | #endif
|
---|
97 | {
|
---|
98 | return aThat.snapshotId == snapshotId;
|
---|
99 | }
|
---|
100 |
|
---|
101 | const Guid snapshotId;
|
---|
102 | };
|
---|
103 |
|
---|
104 | SnapshotRef(const Guid &aSnapshotId,
|
---|
105 | const int &aRefCnt = 1)
|
---|
106 | : snapshotId(aSnapshotId),
|
---|
107 | iRefCnt(aRefCnt) {}
|
---|
108 |
|
---|
109 | Guid snapshotId;
|
---|
110 | /*
|
---|
111 | * The number of attachments of the medium in the same snapshot.
|
---|
112 | * Used for MediumType_Readonly. It is always equal to 1 for other types.
|
---|
113 | * Usual int is used because any changes in the BackRef are guarded by
|
---|
114 | * AutoWriteLock.
|
---|
115 | */
|
---|
116 | int iRefCnt;
|
---|
117 | };
|
---|
118 |
|
---|
119 | /** Describes how a machine refers to this medium. */
|
---|
120 | struct BackRef
|
---|
121 | {
|
---|
122 | /** Equality predicate for stdc++. */
|
---|
123 | struct EqualsTo
|
---|
124 | #if __cplusplus < 201700 /* deprecated in C++11, removed in C++17. */
|
---|
125 | : public std::unary_function <BackRef, bool>
|
---|
126 | #endif
|
---|
127 | {
|
---|
128 | explicit EqualsTo(const Guid &aMachineId) : machineId(aMachineId) {}
|
---|
129 |
|
---|
130 | #if __cplusplus < 201700 /* deprecated in C++11, removed in C++17. */
|
---|
131 | bool operator()(const argument_type &aThat) const
|
---|
132 | #else
|
---|
133 | bool operator()(const BackRef &aThat) const
|
---|
134 | #endif
|
---|
135 | {
|
---|
136 | return aThat.machineId == machineId;
|
---|
137 | }
|
---|
138 |
|
---|
139 | const Guid machineId;
|
---|
140 | };
|
---|
141 |
|
---|
142 | BackRef(const Guid &aMachineId,
|
---|
143 | const Guid &aSnapshotId = Guid::Empty)
|
---|
144 | : machineId(aMachineId),
|
---|
145 | iRefCnt(1),
|
---|
146 | fInCurState(aSnapshotId.isZero())
|
---|
147 | {
|
---|
148 | if (aSnapshotId.isValid() && !aSnapshotId.isZero())
|
---|
149 | llSnapshotIds.push_back(SnapshotRef(aSnapshotId));
|
---|
150 | }
|
---|
151 |
|
---|
152 | Guid machineId;
|
---|
153 | /*
|
---|
154 | * The number of attachments of the medium in the same machine.
|
---|
155 | * Used for MediumType_Readonly. It is always equal to 1 for other types.
|
---|
156 | * Usual int is used because any changes in the BackRef are guarded by
|
---|
157 | * AutoWriteLock.
|
---|
158 | */
|
---|
159 | int iRefCnt;
|
---|
160 | bool fInCurState : 1;
|
---|
161 | std::list<SnapshotRef> llSnapshotIds;
|
---|
162 | };
|
---|
163 |
|
---|
164 | typedef std::list<BackRef> BackRefList;
|
---|
165 |
|
---|
166 | #if __cplusplus < 201700 && RT_GNUC_PREREQ(11,0)
|
---|
167 | # pragma GCC diagnostic pop
|
---|
168 | #endif
|
---|
169 |
|
---|
170 |
|
---|
171 | struct Medium::Data
|
---|
172 | {
|
---|
173 | Data()
|
---|
174 | : pVirtualBox(NULL),
|
---|
175 | state(MediumState_NotCreated),
|
---|
176 | variant(MediumVariant_Standard),
|
---|
177 | size(0),
|
---|
178 | readers(0),
|
---|
179 | preLockState(MediumState_NotCreated),
|
---|
180 | queryInfoSem(LOCKCLASS_MEDIUMQUERY),
|
---|
181 | queryInfoRunning(false),
|
---|
182 | type(MediumType_Normal),
|
---|
183 | devType(DeviceType_HardDisk),
|
---|
184 | logicalSize(0),
|
---|
185 | hddOpenMode(OpenReadWrite),
|
---|
186 | autoReset(false),
|
---|
187 | hostDrive(false),
|
---|
188 | implicit(false),
|
---|
189 | fClosing(false),
|
---|
190 | uOpenFlagsDef(VD_OPEN_FLAGS_IGNORE_FLUSH),
|
---|
191 | numCreateDiffTasks(0),
|
---|
192 | vdDiskIfaces(NULL),
|
---|
193 | vdImageIfaces(NULL),
|
---|
194 | fMoveThisMedium(false)
|
---|
195 | { }
|
---|
196 |
|
---|
197 | /** weak VirtualBox parent */
|
---|
198 | VirtualBox * const pVirtualBox;
|
---|
199 |
|
---|
200 | // pParent and llChildren are protected by VirtualBox::i_getMediaTreeLockHandle()
|
---|
201 | ComObjPtr<Medium> pParent;
|
---|
202 | MediaList llChildren; // to add a child, just call push_back; to remove
|
---|
203 | // a child, call child->deparent() which does a lookup
|
---|
204 |
|
---|
205 | GuidList llRegistryIDs; // media registries in which this medium is listed
|
---|
206 |
|
---|
207 | const Guid id;
|
---|
208 | Utf8Str strDescription;
|
---|
209 | MediumState_T state;
|
---|
210 | MediumVariant_T variant;
|
---|
211 | Utf8Str strLocationFull;
|
---|
212 | uint64_t size;
|
---|
213 | Utf8Str strLastAccessError;
|
---|
214 |
|
---|
215 | BackRefList backRefs;
|
---|
216 |
|
---|
217 | size_t readers;
|
---|
218 | MediumState_T preLockState;
|
---|
219 |
|
---|
220 | /** Special synchronization for operations which must wait for
|
---|
221 | * Medium::i_queryInfo in another thread to complete. Using a SemRW is
|
---|
222 | * not quite ideal, but at least it is subject to the lock validator,
|
---|
223 | * unlike the SemEventMulti which we had here for many years. Catching
|
---|
224 | * possible deadlocks is more important than a tiny bit of efficiency. */
|
---|
225 | RWLockHandle queryInfoSem;
|
---|
226 | bool queryInfoRunning : 1;
|
---|
227 |
|
---|
228 | const Utf8Str strFormat;
|
---|
229 | ComObjPtr<MediumFormat> formatObj;
|
---|
230 |
|
---|
231 | MediumType_T type;
|
---|
232 | DeviceType_T devType;
|
---|
233 | uint64_t logicalSize;
|
---|
234 |
|
---|
235 | HDDOpenMode hddOpenMode;
|
---|
236 |
|
---|
237 | bool autoReset : 1;
|
---|
238 |
|
---|
239 | /** New UUID to be set on the next Medium::i_queryInfo call. */
|
---|
240 | const Guid uuidImage;
|
---|
241 | /** New parent UUID to be set on the next Medium::i_queryInfo call. */
|
---|
242 | const Guid uuidParentImage;
|
---|
243 |
|
---|
244 | /** @todo r=bird: the boolean bitfields are pointless if they're not grouped! */
|
---|
245 | bool hostDrive : 1;
|
---|
246 |
|
---|
247 | settings::StringsMap mapProperties;
|
---|
248 |
|
---|
249 | bool implicit : 1;
|
---|
250 | /** Flag whether the medium is in the process of being closed. */
|
---|
251 | bool fClosing: 1;
|
---|
252 |
|
---|
253 | /** Default flags passed to VDOpen(). */
|
---|
254 | unsigned uOpenFlagsDef;
|
---|
255 |
|
---|
256 | uint32_t numCreateDiffTasks;
|
---|
257 |
|
---|
258 | Utf8Str vdError; /*< Error remembered by the VD error callback. */
|
---|
259 |
|
---|
260 | VDINTERFACEERROR vdIfError;
|
---|
261 |
|
---|
262 | VDINTERFACECONFIG vdIfConfig;
|
---|
263 |
|
---|
264 | /** The handle to the default VD TCP/IP interface. */
|
---|
265 | VDIFINST hTcpNetInst;
|
---|
266 |
|
---|
267 | PVDINTERFACE vdDiskIfaces;
|
---|
268 | PVDINTERFACE vdImageIfaces;
|
---|
269 |
|
---|
270 | /** Flag if the medium is going to move to a new
|
---|
271 | * location. */
|
---|
272 | bool fMoveThisMedium;
|
---|
273 | /** new location path */
|
---|
274 | Utf8Str strNewLocationFull;
|
---|
275 | };
|
---|
276 |
|
---|
277 | typedef struct VDSOCKETINT
|
---|
278 | {
|
---|
279 | /** Socket handle. */
|
---|
280 | RTSOCKET hSocket;
|
---|
281 | } VDSOCKETINT, *PVDSOCKETINT;
|
---|
282 |
|
---|
283 | ////////////////////////////////////////////////////////////////////////////////
|
---|
284 | //
|
---|
285 | // Globals
|
---|
286 | //
|
---|
287 | ////////////////////////////////////////////////////////////////////////////////
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * Medium::Task class for asynchronous operations.
|
---|
291 | *
|
---|
292 | * @note Instances of this class must be created using new() because the
|
---|
293 | * task thread function will delete them when the task is complete.
|
---|
294 | *
|
---|
295 | * @note The constructor of this class adds a caller on the managed Medium
|
---|
296 | * object which is automatically released upon destruction.
|
---|
297 | */
|
---|
298 | class Medium::Task : public ThreadTask
|
---|
299 | {
|
---|
300 | public:
|
---|
301 | Task(Medium *aMedium, Progress *aProgress, bool fNotifyAboutChanges = true)
|
---|
302 | : ThreadTask("Medium::Task"),
|
---|
303 | mVDOperationIfaces(NULL),
|
---|
304 | mMedium(aMedium),
|
---|
305 | mMediumCaller(aMedium),
|
---|
306 | mProgress(aProgress),
|
---|
307 | mVirtualBoxCaller(NULL),
|
---|
308 | mNotifyAboutChanges(fNotifyAboutChanges)
|
---|
309 | {
|
---|
310 | AssertReturnVoidStmt(aMedium, mRC = E_FAIL);
|
---|
311 | mRC = mMediumCaller.rc();
|
---|
312 | if (FAILED(mRC))
|
---|
313 | return;
|
---|
314 |
|
---|
315 | /* Get strong VirtualBox reference, see below. */
|
---|
316 | VirtualBox *pVirtualBox = aMedium->m->pVirtualBox;
|
---|
317 | mVirtualBox = pVirtualBox;
|
---|
318 | mVirtualBoxCaller.attach(pVirtualBox);
|
---|
319 | mRC = mVirtualBoxCaller.rc();
|
---|
320 | if (FAILED(mRC))
|
---|
321 | return;
|
---|
322 |
|
---|
323 | /* Set up a per-operation progress interface, can be used freely (for
|
---|
324 | * binary operations you can use it either on the source or target). */
|
---|
325 | if (mProgress)
|
---|
326 | {
|
---|
327 | mVDIfProgress.pfnProgress = aProgress->i_vdProgressCallback;
|
---|
328 | int vrc = VDInterfaceAdd(&mVDIfProgress.Core,
|
---|
329 | "Medium::Task::vdInterfaceProgress",
|
---|
330 | VDINTERFACETYPE_PROGRESS,
|
---|
331 | mProgress,
|
---|
332 | sizeof(mVDIfProgress),
|
---|
333 | &mVDOperationIfaces);
|
---|
334 | AssertRC(vrc);
|
---|
335 | if (RT_FAILURE(vrc))
|
---|
336 | mRC = E_FAIL;
|
---|
337 | }
|
---|
338 | }
|
---|
339 |
|
---|
340 | // Make all destructors virtual. Just in case.
|
---|
341 | virtual ~Task()
|
---|
342 | {
|
---|
343 | /* send the notification of completion.*/
|
---|
344 | if ( isAsync()
|
---|
345 | && !mProgress.isNull())
|
---|
346 | mProgress->i_notifyComplete(mRC);
|
---|
347 | }
|
---|
348 |
|
---|
349 | HRESULT rc() const { return mRC; }
|
---|
350 | bool isOk() const { return SUCCEEDED(rc()); }
|
---|
351 | bool NotifyAboutChanges() const { return mNotifyAboutChanges; }
|
---|
352 |
|
---|
353 | const ComPtr<Progress>& GetProgressObject() const {return mProgress;}
|
---|
354 |
|
---|
355 | /**
|
---|
356 | * Runs Medium::Task::executeTask() on the current thread
|
---|
357 | * instead of creating a new one.
|
---|
358 | */
|
---|
359 | HRESULT runNow()
|
---|
360 | {
|
---|
361 | LogFlowFuncEnter();
|
---|
362 |
|
---|
363 | mRC = executeTask();
|
---|
364 |
|
---|
365 | LogFlowFunc(("rc=%Rhrc\n", mRC));
|
---|
366 | LogFlowFuncLeave();
|
---|
367 | return mRC;
|
---|
368 | }
|
---|
369 |
|
---|
370 | /**
|
---|
371 | * Implementation code for the "create base" task.
|
---|
372 | * Used as function for execution from a standalone thread.
|
---|
373 | */
|
---|
374 | void handler()
|
---|
375 | {
|
---|
376 | LogFlowFuncEnter();
|
---|
377 | try
|
---|
378 | {
|
---|
379 | mRC = executeTask(); /* (destructor picks up mRC, see above) */
|
---|
380 | LogFlowFunc(("rc=%Rhrc\n", mRC));
|
---|
381 | }
|
---|
382 | catch (...)
|
---|
383 | {
|
---|
384 | LogRel(("Some exception in the function Medium::Task:handler()\n"));
|
---|
385 | }
|
---|
386 |
|
---|
387 | LogFlowFuncLeave();
|
---|
388 | }
|
---|
389 |
|
---|
390 | PVDINTERFACE mVDOperationIfaces;
|
---|
391 |
|
---|
392 | const ComObjPtr<Medium> mMedium;
|
---|
393 | AutoCaller mMediumCaller;
|
---|
394 |
|
---|
395 | protected:
|
---|
396 | HRESULT mRC;
|
---|
397 |
|
---|
398 | private:
|
---|
399 | virtual HRESULT executeTask() = 0;
|
---|
400 |
|
---|
401 | const ComObjPtr<Progress> mProgress;
|
---|
402 |
|
---|
403 | VDINTERFACEPROGRESS mVDIfProgress;
|
---|
404 |
|
---|
405 | /* Must have a strong VirtualBox reference during a task otherwise the
|
---|
406 | * reference count might drop to 0 while a task is still running. This
|
---|
407 | * would result in weird behavior, including deadlocks due to uninit and
|
---|
408 | * locking order issues. The deadlock often is not detectable because the
|
---|
409 | * uninit uses event semaphores which sabotages deadlock detection. */
|
---|
410 | ComObjPtr<VirtualBox> mVirtualBox;
|
---|
411 | AutoCaller mVirtualBoxCaller;
|
---|
412 | bool mNotifyAboutChanges;
|
---|
413 | };
|
---|
414 |
|
---|
415 | class Medium::CreateBaseTask : public Medium::Task
|
---|
416 | {
|
---|
417 | public:
|
---|
418 | CreateBaseTask(Medium *aMedium,
|
---|
419 | Progress *aProgress,
|
---|
420 | uint64_t aSize,
|
---|
421 | MediumVariant_T aVariant,
|
---|
422 | bool fNotifyAboutChanges = true)
|
---|
423 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
424 | mSize(aSize),
|
---|
425 | mVariant(aVariant)
|
---|
426 | {
|
---|
427 | m_strTaskName = "createBase";
|
---|
428 | }
|
---|
429 |
|
---|
430 | uint64_t mSize;
|
---|
431 | MediumVariant_T mVariant;
|
---|
432 |
|
---|
433 | private:
|
---|
434 | HRESULT executeTask()
|
---|
435 | {
|
---|
436 | return mMedium->i_taskCreateBaseHandler(*this);
|
---|
437 | }
|
---|
438 | };
|
---|
439 |
|
---|
440 | class Medium::CreateDiffTask : public Medium::Task
|
---|
441 | {
|
---|
442 | public:
|
---|
443 | CreateDiffTask(Medium *aMedium,
|
---|
444 | Progress *aProgress,
|
---|
445 | Medium *aTarget,
|
---|
446 | MediumVariant_T aVariant,
|
---|
447 | MediumLockList *aMediumLockList,
|
---|
448 | bool fKeepMediumLockList = false,
|
---|
449 | bool fNotifyAboutChanges = true)
|
---|
450 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
451 | mpMediumLockList(aMediumLockList),
|
---|
452 | mTarget(aTarget),
|
---|
453 | mVariant(aVariant),
|
---|
454 | mTargetCaller(aTarget),
|
---|
455 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
456 | {
|
---|
457 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
458 | mRC = mTargetCaller.rc();
|
---|
459 | if (FAILED(mRC))
|
---|
460 | return;
|
---|
461 | m_strTaskName = "createDiff";
|
---|
462 | }
|
---|
463 |
|
---|
464 | ~CreateDiffTask()
|
---|
465 | {
|
---|
466 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
467 | delete mpMediumLockList;
|
---|
468 | }
|
---|
469 |
|
---|
470 | MediumLockList *mpMediumLockList;
|
---|
471 |
|
---|
472 | const ComObjPtr<Medium> mTarget;
|
---|
473 | MediumVariant_T mVariant;
|
---|
474 |
|
---|
475 | private:
|
---|
476 | HRESULT executeTask()
|
---|
477 | {
|
---|
478 | return mMedium->i_taskCreateDiffHandler(*this);
|
---|
479 | }
|
---|
480 |
|
---|
481 | AutoCaller mTargetCaller;
|
---|
482 | bool mfKeepMediumLockList;
|
---|
483 | };
|
---|
484 |
|
---|
485 | class Medium::CloneTask : public Medium::Task
|
---|
486 | {
|
---|
487 | public:
|
---|
488 | CloneTask(Medium *aMedium,
|
---|
489 | Progress *aProgress,
|
---|
490 | Medium *aTarget,
|
---|
491 | MediumVariant_T aVariant,
|
---|
492 | Medium *aParent,
|
---|
493 | uint32_t idxSrcImageSame,
|
---|
494 | uint32_t idxDstImageSame,
|
---|
495 | MediumLockList *aSourceMediumLockList,
|
---|
496 | MediumLockList *aTargetMediumLockList,
|
---|
497 | bool fKeepSourceMediumLockList = false,
|
---|
498 | bool fKeepTargetMediumLockList = false,
|
---|
499 | bool fNotifyAboutChanges = true,
|
---|
500 | LONG64 aTargetLogicalSize = 0)
|
---|
501 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
502 | mTarget(aTarget),
|
---|
503 | mParent(aParent),
|
---|
504 | mTargetLogicalSize(aTargetLogicalSize),
|
---|
505 | mpSourceMediumLockList(aSourceMediumLockList),
|
---|
506 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
507 | mVariant(aVariant),
|
---|
508 | midxSrcImageSame(idxSrcImageSame),
|
---|
509 | midxDstImageSame(idxDstImageSame),
|
---|
510 | mTargetCaller(aTarget),
|
---|
511 | mParentCaller(aParent),
|
---|
512 | mfKeepSourceMediumLockList(fKeepSourceMediumLockList),
|
---|
513 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
514 |
|
---|
515 | {
|
---|
516 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
517 | mRC = mTargetCaller.rc();
|
---|
518 | if (FAILED(mRC))
|
---|
519 | return;
|
---|
520 | /* aParent may be NULL */
|
---|
521 | mRC = mParentCaller.rc();
|
---|
522 | if (FAILED(mRC))
|
---|
523 | return;
|
---|
524 | AssertReturnVoidStmt(aSourceMediumLockList != NULL, mRC = E_FAIL);
|
---|
525 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
526 | m_strTaskName = "createClone";
|
---|
527 | }
|
---|
528 |
|
---|
529 | ~CloneTask()
|
---|
530 | {
|
---|
531 | if (!mfKeepSourceMediumLockList && mpSourceMediumLockList)
|
---|
532 | delete mpSourceMediumLockList;
|
---|
533 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
534 | delete mpTargetMediumLockList;
|
---|
535 | }
|
---|
536 |
|
---|
537 | const ComObjPtr<Medium> mTarget;
|
---|
538 | const ComObjPtr<Medium> mParent;
|
---|
539 | LONG64 mTargetLogicalSize;
|
---|
540 | MediumLockList *mpSourceMediumLockList;
|
---|
541 | MediumLockList *mpTargetMediumLockList;
|
---|
542 | MediumVariant_T mVariant;
|
---|
543 | uint32_t midxSrcImageSame;
|
---|
544 | uint32_t midxDstImageSame;
|
---|
545 |
|
---|
546 | private:
|
---|
547 | HRESULT executeTask()
|
---|
548 | {
|
---|
549 | return mMedium->i_taskCloneHandler(*this);
|
---|
550 | }
|
---|
551 |
|
---|
552 | AutoCaller mTargetCaller;
|
---|
553 | AutoCaller mParentCaller;
|
---|
554 | bool mfKeepSourceMediumLockList;
|
---|
555 | bool mfKeepTargetMediumLockList;
|
---|
556 | };
|
---|
557 |
|
---|
558 | class Medium::MoveTask : public Medium::Task
|
---|
559 | {
|
---|
560 | public:
|
---|
561 | MoveTask(Medium *aMedium,
|
---|
562 | Progress *aProgress,
|
---|
563 | MediumVariant_T aVariant,
|
---|
564 | MediumLockList *aMediumLockList,
|
---|
565 | bool fKeepMediumLockList = false,
|
---|
566 | bool fNotifyAboutChanges = true)
|
---|
567 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
568 | mpMediumLockList(aMediumLockList),
|
---|
569 | mVariant(aVariant),
|
---|
570 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
571 | {
|
---|
572 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
573 | m_strTaskName = "createMove";
|
---|
574 | }
|
---|
575 |
|
---|
576 | ~MoveTask()
|
---|
577 | {
|
---|
578 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
579 | delete mpMediumLockList;
|
---|
580 | }
|
---|
581 |
|
---|
582 | MediumLockList *mpMediumLockList;
|
---|
583 | MediumVariant_T mVariant;
|
---|
584 |
|
---|
585 | private:
|
---|
586 | HRESULT executeTask()
|
---|
587 | {
|
---|
588 | return mMedium->i_taskMoveHandler(*this);
|
---|
589 | }
|
---|
590 |
|
---|
591 | bool mfKeepMediumLockList;
|
---|
592 | };
|
---|
593 |
|
---|
594 | class Medium::CompactTask : public Medium::Task
|
---|
595 | {
|
---|
596 | public:
|
---|
597 | CompactTask(Medium *aMedium,
|
---|
598 | Progress *aProgress,
|
---|
599 | MediumLockList *aMediumLockList,
|
---|
600 | bool fKeepMediumLockList = false,
|
---|
601 | bool fNotifyAboutChanges = true)
|
---|
602 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
603 | mpMediumLockList(aMediumLockList),
|
---|
604 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
605 | {
|
---|
606 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
607 | m_strTaskName = "createCompact";
|
---|
608 | }
|
---|
609 |
|
---|
610 | ~CompactTask()
|
---|
611 | {
|
---|
612 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
613 | delete mpMediumLockList;
|
---|
614 | }
|
---|
615 |
|
---|
616 | MediumLockList *mpMediumLockList;
|
---|
617 |
|
---|
618 | private:
|
---|
619 | HRESULT executeTask()
|
---|
620 | {
|
---|
621 | return mMedium->i_taskCompactHandler(*this);
|
---|
622 | }
|
---|
623 |
|
---|
624 | bool mfKeepMediumLockList;
|
---|
625 | };
|
---|
626 |
|
---|
627 | class Medium::ResizeTask : public Medium::Task
|
---|
628 | {
|
---|
629 | public:
|
---|
630 | ResizeTask(Medium *aMedium,
|
---|
631 | uint64_t aSize,
|
---|
632 | Progress *aProgress,
|
---|
633 | MediumLockList *aMediumLockList,
|
---|
634 | bool fKeepMediumLockList = false,
|
---|
635 | bool fNotifyAboutChanges = true)
|
---|
636 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
637 | mSize(aSize),
|
---|
638 | mpMediumLockList(aMediumLockList),
|
---|
639 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
640 | {
|
---|
641 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
642 | m_strTaskName = "createResize";
|
---|
643 | }
|
---|
644 |
|
---|
645 | ~ResizeTask()
|
---|
646 | {
|
---|
647 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
648 | delete mpMediumLockList;
|
---|
649 | }
|
---|
650 |
|
---|
651 | uint64_t mSize;
|
---|
652 | MediumLockList *mpMediumLockList;
|
---|
653 |
|
---|
654 | private:
|
---|
655 | HRESULT executeTask()
|
---|
656 | {
|
---|
657 | return mMedium->i_taskResizeHandler(*this);
|
---|
658 | }
|
---|
659 |
|
---|
660 | bool mfKeepMediumLockList;
|
---|
661 | };
|
---|
662 |
|
---|
663 | class Medium::ResetTask : public Medium::Task
|
---|
664 | {
|
---|
665 | public:
|
---|
666 | ResetTask(Medium *aMedium,
|
---|
667 | Progress *aProgress,
|
---|
668 | MediumLockList *aMediumLockList,
|
---|
669 | bool fKeepMediumLockList = false,
|
---|
670 | bool fNotifyAboutChanges = true)
|
---|
671 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
672 | mpMediumLockList(aMediumLockList),
|
---|
673 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
674 | {
|
---|
675 | m_strTaskName = "createReset";
|
---|
676 | }
|
---|
677 |
|
---|
678 | ~ResetTask()
|
---|
679 | {
|
---|
680 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
681 | delete mpMediumLockList;
|
---|
682 | }
|
---|
683 |
|
---|
684 | MediumLockList *mpMediumLockList;
|
---|
685 |
|
---|
686 | private:
|
---|
687 | HRESULT executeTask()
|
---|
688 | {
|
---|
689 | return mMedium->i_taskResetHandler(*this);
|
---|
690 | }
|
---|
691 |
|
---|
692 | bool mfKeepMediumLockList;
|
---|
693 | };
|
---|
694 |
|
---|
695 | class Medium::DeleteTask : public Medium::Task
|
---|
696 | {
|
---|
697 | public:
|
---|
698 | DeleteTask(Medium *aMedium,
|
---|
699 | Progress *aProgress,
|
---|
700 | MediumLockList *aMediumLockList,
|
---|
701 | bool fKeepMediumLockList = false,
|
---|
702 | bool fNotifyAboutChanges = true)
|
---|
703 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
704 | mpMediumLockList(aMediumLockList),
|
---|
705 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
706 | {
|
---|
707 | m_strTaskName = "createDelete";
|
---|
708 | }
|
---|
709 |
|
---|
710 | ~DeleteTask()
|
---|
711 | {
|
---|
712 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
713 | delete mpMediumLockList;
|
---|
714 | }
|
---|
715 |
|
---|
716 | MediumLockList *mpMediumLockList;
|
---|
717 |
|
---|
718 | private:
|
---|
719 | HRESULT executeTask()
|
---|
720 | {
|
---|
721 | return mMedium->i_taskDeleteHandler(*this);
|
---|
722 | }
|
---|
723 |
|
---|
724 | bool mfKeepMediumLockList;
|
---|
725 | };
|
---|
726 |
|
---|
727 | class Medium::MergeTask : public Medium::Task
|
---|
728 | {
|
---|
729 | public:
|
---|
730 | MergeTask(Medium *aMedium,
|
---|
731 | Medium *aTarget,
|
---|
732 | bool fMergeForward,
|
---|
733 | Medium *aParentForTarget,
|
---|
734 | MediumLockList *aChildrenToReparent,
|
---|
735 | Progress *aProgress,
|
---|
736 | MediumLockList *aMediumLockList,
|
---|
737 | bool fKeepMediumLockList = false,
|
---|
738 | bool fNotifyAboutChanges = true)
|
---|
739 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
740 | mTarget(aTarget),
|
---|
741 | mfMergeForward(fMergeForward),
|
---|
742 | mParentForTarget(aParentForTarget),
|
---|
743 | mpChildrenToReparent(aChildrenToReparent),
|
---|
744 | mpMediumLockList(aMediumLockList),
|
---|
745 | mTargetCaller(aTarget),
|
---|
746 | mParentForTargetCaller(aParentForTarget),
|
---|
747 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
748 | {
|
---|
749 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
750 | m_strTaskName = "createMerge";
|
---|
751 | }
|
---|
752 |
|
---|
753 | ~MergeTask()
|
---|
754 | {
|
---|
755 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
756 | delete mpMediumLockList;
|
---|
757 | if (mpChildrenToReparent)
|
---|
758 | delete mpChildrenToReparent;
|
---|
759 | }
|
---|
760 |
|
---|
761 | const ComObjPtr<Medium> mTarget;
|
---|
762 | bool mfMergeForward;
|
---|
763 | /* When mpChildrenToReparent is null then mParentForTarget is non-null and
|
---|
764 | * vice versa. In other words: they are used in different cases. */
|
---|
765 | const ComObjPtr<Medium> mParentForTarget;
|
---|
766 | MediumLockList *mpChildrenToReparent;
|
---|
767 | MediumLockList *mpMediumLockList;
|
---|
768 |
|
---|
769 | private:
|
---|
770 | HRESULT executeTask()
|
---|
771 | {
|
---|
772 | return mMedium->i_taskMergeHandler(*this);
|
---|
773 | }
|
---|
774 |
|
---|
775 | AutoCaller mTargetCaller;
|
---|
776 | AutoCaller mParentForTargetCaller;
|
---|
777 | bool mfKeepMediumLockList;
|
---|
778 | };
|
---|
779 |
|
---|
780 | class Medium::ImportTask : public Medium::Task
|
---|
781 | {
|
---|
782 | public:
|
---|
783 | ImportTask(Medium *aMedium,
|
---|
784 | Progress *aProgress,
|
---|
785 | const char *aFilename,
|
---|
786 | MediumFormat *aFormat,
|
---|
787 | MediumVariant_T aVariant,
|
---|
788 | RTVFSIOSTREAM aVfsIosSrc,
|
---|
789 | Medium *aParent,
|
---|
790 | MediumLockList *aTargetMediumLockList,
|
---|
791 | bool fKeepTargetMediumLockList = false,
|
---|
792 | bool fNotifyAboutChanges = true)
|
---|
793 | : Medium::Task(aMedium, aProgress, fNotifyAboutChanges),
|
---|
794 | mFilename(aFilename),
|
---|
795 | mFormat(aFormat),
|
---|
796 | mVariant(aVariant),
|
---|
797 | mParent(aParent),
|
---|
798 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
799 | mpVfsIoIf(NULL),
|
---|
800 | mParentCaller(aParent),
|
---|
801 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
802 | {
|
---|
803 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
804 | /* aParent may be NULL */
|
---|
805 | mRC = mParentCaller.rc();
|
---|
806 | if (FAILED(mRC))
|
---|
807 | return;
|
---|
808 |
|
---|
809 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
810 |
|
---|
811 | int vrc = VDIfCreateFromVfsStream(aVfsIosSrc, RTFILE_O_READ, &mpVfsIoIf);
|
---|
812 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
813 |
|
---|
814 | vrc = VDInterfaceAdd(&mpVfsIoIf->Core, "Medium::ImportTaskVfsIos",
|
---|
815 | VDINTERFACETYPE_IO, mpVfsIoIf,
|
---|
816 | sizeof(VDINTERFACEIO), &mVDImageIfaces);
|
---|
817 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
818 | m_strTaskName = "createImport";
|
---|
819 | }
|
---|
820 |
|
---|
821 | ~ImportTask()
|
---|
822 | {
|
---|
823 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
824 | delete mpTargetMediumLockList;
|
---|
825 | if (mpVfsIoIf)
|
---|
826 | {
|
---|
827 | VDIfDestroyFromVfsStream(mpVfsIoIf);
|
---|
828 | mpVfsIoIf = NULL;
|
---|
829 | }
|
---|
830 | }
|
---|
831 |
|
---|
832 | Utf8Str mFilename;
|
---|
833 | ComObjPtr<MediumFormat> mFormat;
|
---|
834 | MediumVariant_T mVariant;
|
---|
835 | const ComObjPtr<Medium> mParent;
|
---|
836 | MediumLockList *mpTargetMediumLockList;
|
---|
837 | PVDINTERFACE mVDImageIfaces;
|
---|
838 | PVDINTERFACEIO mpVfsIoIf; /**< Pointer to the VFS I/O stream to VD I/O interface wrapper. */
|
---|
839 |
|
---|
840 | private:
|
---|
841 | HRESULT executeTask()
|
---|
842 | {
|
---|
843 | return mMedium->i_taskImportHandler(*this);
|
---|
844 | }
|
---|
845 |
|
---|
846 | AutoCaller mParentCaller;
|
---|
847 | bool mfKeepTargetMediumLockList;
|
---|
848 | };
|
---|
849 |
|
---|
850 | class Medium::EncryptTask : public Medium::Task
|
---|
851 | {
|
---|
852 | public:
|
---|
853 | EncryptTask(Medium *aMedium,
|
---|
854 | const com::Utf8Str &strNewPassword,
|
---|
855 | const com::Utf8Str &strCurrentPassword,
|
---|
856 | const com::Utf8Str &strCipher,
|
---|
857 | const com::Utf8Str &strNewPasswordId,
|
---|
858 | Progress *aProgress,
|
---|
859 | MediumLockList *aMediumLockList)
|
---|
860 | : Medium::Task(aMedium, aProgress, false),
|
---|
861 | mstrNewPassword(strNewPassword),
|
---|
862 | mstrCurrentPassword(strCurrentPassword),
|
---|
863 | mstrCipher(strCipher),
|
---|
864 | mstrNewPasswordId(strNewPasswordId),
|
---|
865 | mpMediumLockList(aMediumLockList)
|
---|
866 | {
|
---|
867 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
868 | /* aParent may be NULL */
|
---|
869 | mRC = mParentCaller.rc();
|
---|
870 | if (FAILED(mRC))
|
---|
871 | return;
|
---|
872 |
|
---|
873 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
874 | m_strTaskName = "createEncrypt";
|
---|
875 | }
|
---|
876 |
|
---|
877 | ~EncryptTask()
|
---|
878 | {
|
---|
879 | if (mstrNewPassword.length())
|
---|
880 | RTMemWipeThoroughly(mstrNewPassword.mutableRaw(), mstrNewPassword.length(), 10 /* cPasses */);
|
---|
881 | if (mstrCurrentPassword.length())
|
---|
882 | RTMemWipeThoroughly(mstrCurrentPassword.mutableRaw(), mstrCurrentPassword.length(), 10 /* cPasses */);
|
---|
883 |
|
---|
884 | /* Keep any errors which might be set when deleting the lock list. */
|
---|
885 | ErrorInfoKeeper eik;
|
---|
886 | delete mpMediumLockList;
|
---|
887 | }
|
---|
888 |
|
---|
889 | Utf8Str mstrNewPassword;
|
---|
890 | Utf8Str mstrCurrentPassword;
|
---|
891 | Utf8Str mstrCipher;
|
---|
892 | Utf8Str mstrNewPasswordId;
|
---|
893 | MediumLockList *mpMediumLockList;
|
---|
894 | PVDINTERFACE mVDImageIfaces;
|
---|
895 |
|
---|
896 | private:
|
---|
897 | HRESULT executeTask()
|
---|
898 | {
|
---|
899 | return mMedium->i_taskEncryptHandler(*this);
|
---|
900 | }
|
---|
901 |
|
---|
902 | AutoCaller mParentCaller;
|
---|
903 | };
|
---|
904 |
|
---|
905 |
|
---|
906 |
|
---|
907 | /**
|
---|
908 | * Converts the Medium device type to the VD type.
|
---|
909 | */
|
---|
910 | static const char *getVDTypeName(VDTYPE enmType)
|
---|
911 | {
|
---|
912 | switch (enmType)
|
---|
913 | {
|
---|
914 | case VDTYPE_HDD: return "HDD";
|
---|
915 | case VDTYPE_OPTICAL_DISC: return "DVD";
|
---|
916 | case VDTYPE_FLOPPY: return "floppy";
|
---|
917 | case VDTYPE_INVALID: return "invalid";
|
---|
918 | default:
|
---|
919 | AssertFailedReturn("unknown");
|
---|
920 | }
|
---|
921 | }
|
---|
922 |
|
---|
923 | /**
|
---|
924 | * Converts the Medium device type to the VD type.
|
---|
925 | */
|
---|
926 | static const char *getDeviceTypeName(DeviceType_T enmType)
|
---|
927 | {
|
---|
928 | switch (enmType)
|
---|
929 | {
|
---|
930 | case DeviceType_HardDisk: return "HDD";
|
---|
931 | case DeviceType_DVD: return "DVD";
|
---|
932 | case DeviceType_Floppy: return "floppy";
|
---|
933 | case DeviceType_Null: return "null";
|
---|
934 | case DeviceType_Network: return "network";
|
---|
935 | case DeviceType_USB: return "USB";
|
---|
936 | case DeviceType_SharedFolder: return "shared folder";
|
---|
937 | case DeviceType_Graphics3D: return "graphics 3d";
|
---|
938 | default:
|
---|
939 | AssertFailedReturn("unknown");
|
---|
940 | }
|
---|
941 | }
|
---|
942 |
|
---|
943 |
|
---|
944 |
|
---|
945 | ////////////////////////////////////////////////////////////////////////////////
|
---|
946 | //
|
---|
947 | // Medium constructor / destructor
|
---|
948 | //
|
---|
949 | ////////////////////////////////////////////////////////////////////////////////
|
---|
950 |
|
---|
951 | DEFINE_EMPTY_CTOR_DTOR(Medium)
|
---|
952 |
|
---|
953 | HRESULT Medium::FinalConstruct()
|
---|
954 | {
|
---|
955 | m = new Data;
|
---|
956 |
|
---|
957 | /* Initialize the callbacks of the VD error interface */
|
---|
958 | m->vdIfError.pfnError = i_vdErrorCall;
|
---|
959 | m->vdIfError.pfnMessage = NULL;
|
---|
960 |
|
---|
961 | /* Initialize the callbacks of the VD config interface */
|
---|
962 | m->vdIfConfig.pfnAreKeysValid = i_vdConfigAreKeysValid;
|
---|
963 | m->vdIfConfig.pfnQuerySize = i_vdConfigQuerySize;
|
---|
964 | m->vdIfConfig.pfnQuery = i_vdConfigQuery;
|
---|
965 | m->vdIfConfig.pfnUpdate = i_vdConfigUpdate;
|
---|
966 | m->vdIfConfig.pfnQueryBytes = NULL;
|
---|
967 |
|
---|
968 | /* Initialize the per-disk interface chain (could be done more globally,
|
---|
969 | * but it's not wasting much time or space so it's not worth it). */
|
---|
970 | int vrc;
|
---|
971 | vrc = VDInterfaceAdd(&m->vdIfError.Core,
|
---|
972 | "Medium::vdInterfaceError",
|
---|
973 | VDINTERFACETYPE_ERROR, this,
|
---|
974 | sizeof(VDINTERFACEERROR), &m->vdDiskIfaces);
|
---|
975 | AssertRCReturn(vrc, E_FAIL);
|
---|
976 |
|
---|
977 | /* Initialize the per-image interface chain */
|
---|
978 | vrc = VDInterfaceAdd(&m->vdIfConfig.Core,
|
---|
979 | "Medium::vdInterfaceConfig",
|
---|
980 | VDINTERFACETYPE_CONFIG, this,
|
---|
981 | sizeof(VDINTERFACECONFIG), &m->vdImageIfaces);
|
---|
982 | AssertRCReturn(vrc, E_FAIL);
|
---|
983 |
|
---|
984 | /* Initialize the callbacks of the VD TCP interface (we always use the host
|
---|
985 | * IP stack for now) */
|
---|
986 | vrc = VDIfTcpNetInstDefaultCreate(&m->hTcpNetInst, &m->vdImageIfaces);
|
---|
987 | AssertRCReturn(vrc, E_FAIL);
|
---|
988 |
|
---|
989 | return BaseFinalConstruct();
|
---|
990 | }
|
---|
991 |
|
---|
992 | void Medium::FinalRelease()
|
---|
993 | {
|
---|
994 | uninit();
|
---|
995 |
|
---|
996 | VDIfTcpNetInstDefaultDestroy(m->hTcpNetInst);
|
---|
997 | delete m;
|
---|
998 |
|
---|
999 | BaseFinalRelease();
|
---|
1000 | }
|
---|
1001 |
|
---|
1002 | /**
|
---|
1003 | * Initializes an empty hard disk object without creating or opening an associated
|
---|
1004 | * storage unit.
|
---|
1005 | *
|
---|
1006 | * This gets called by VirtualBox::CreateMedium() in which case uuidMachineRegistry
|
---|
1007 | * is empty since starting with VirtualBox 4.0, we no longer add opened media to a
|
---|
1008 | * registry automatically (this is deferred until the medium is attached to a machine).
|
---|
1009 | *
|
---|
1010 | * This also gets called when VirtualBox creates diff images; in this case uuidMachineRegistry
|
---|
1011 | * is set to the registry of the parent image to make sure they all end up in the same
|
---|
1012 | * file.
|
---|
1013 | *
|
---|
1014 | * For hard disks that don't have the MediumFormatCapabilities_CreateFixed or
|
---|
1015 | * MediumFormatCapabilities_CreateDynamic capability (and therefore cannot be created or deleted
|
---|
1016 | * with the means of VirtualBox) the associated storage unit is assumed to be
|
---|
1017 | * ready for use so the state of the hard disk object will be set to Created.
|
---|
1018 | *
|
---|
1019 | * @param aVirtualBox VirtualBox object.
|
---|
1020 | * @param aFormat
|
---|
1021 | * @param aLocation Storage unit location.
|
---|
1022 | * @param uuidMachineRegistry The registry to which this medium should be added
|
---|
1023 | * (global registry UUID or machine UUID or empty if none).
|
---|
1024 | * @param aDeviceType Device Type.
|
---|
1025 | */
|
---|
1026 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1027 | const Utf8Str &aFormat,
|
---|
1028 | const Utf8Str &aLocation,
|
---|
1029 | const Guid &uuidMachineRegistry,
|
---|
1030 | const DeviceType_T aDeviceType)
|
---|
1031 | {
|
---|
1032 | AssertReturn(aVirtualBox != NULL, E_FAIL);
|
---|
1033 | AssertReturn(!aFormat.isEmpty(), E_FAIL);
|
---|
1034 |
|
---|
1035 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1036 | AutoInitSpan autoInitSpan(this);
|
---|
1037 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1038 |
|
---|
1039 | HRESULT rc = S_OK;
|
---|
1040 |
|
---|
1041 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1042 |
|
---|
1043 | if (uuidMachineRegistry.isValid() && !uuidMachineRegistry.isZero())
|
---|
1044 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
1045 |
|
---|
1046 | /* no storage yet */
|
---|
1047 | m->state = MediumState_NotCreated;
|
---|
1048 |
|
---|
1049 | /* cannot be a host drive */
|
---|
1050 | m->hostDrive = false;
|
---|
1051 |
|
---|
1052 | m->devType = aDeviceType;
|
---|
1053 |
|
---|
1054 | /* No storage unit is created yet, no need to call Medium::i_queryInfo */
|
---|
1055 |
|
---|
1056 | rc = i_setFormat(aFormat);
|
---|
1057 | if (FAILED(rc)) return rc;
|
---|
1058 |
|
---|
1059 | rc = i_setLocation(aLocation);
|
---|
1060 | if (FAILED(rc)) return rc;
|
---|
1061 |
|
---|
1062 | if (!(m->formatObj->i_getCapabilities() & ( MediumFormatCapabilities_CreateFixed
|
---|
1063 | | MediumFormatCapabilities_CreateDynamic))
|
---|
1064 | )
|
---|
1065 | {
|
---|
1066 | /* Storage for mediums of this format can neither be explicitly
|
---|
1067 | * created by VirtualBox nor deleted, so we place the medium to
|
---|
1068 | * Inaccessible state here and also add it to the registry. The
|
---|
1069 | * state means that one has to use RefreshState() to update the
|
---|
1070 | * medium format specific fields. */
|
---|
1071 | m->state = MediumState_Inaccessible;
|
---|
1072 | // create new UUID
|
---|
1073 | unconst(m->id).create();
|
---|
1074 |
|
---|
1075 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1076 | ComObjPtr<Medium> pMedium;
|
---|
1077 |
|
---|
1078 | /*
|
---|
1079 | * Check whether the UUID is taken already and create a new one
|
---|
1080 | * if required.
|
---|
1081 | * Try this only a limited amount of times in case the PRNG is broken
|
---|
1082 | * in some way to prevent an endless loop.
|
---|
1083 | */
|
---|
1084 | for (unsigned i = 0; i < 5; i++)
|
---|
1085 | {
|
---|
1086 | bool fInUse;
|
---|
1087 |
|
---|
1088 | fInUse = m->pVirtualBox->i_isMediaUuidInUse(m->id, aDeviceType);
|
---|
1089 | if (fInUse)
|
---|
1090 | {
|
---|
1091 | // create new UUID
|
---|
1092 | unconst(m->id).create();
|
---|
1093 | }
|
---|
1094 | else
|
---|
1095 | break;
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | rc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
1099 | Assert(this == pMedium || FAILED(rc));
|
---|
1100 | }
|
---|
1101 |
|
---|
1102 | /* Confirm a successful initialization when it's the case */
|
---|
1103 | if (SUCCEEDED(rc))
|
---|
1104 | autoInitSpan.setSucceeded();
|
---|
1105 |
|
---|
1106 | return rc;
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | /**
|
---|
1110 | * Initializes the medium object by opening the storage unit at the specified
|
---|
1111 | * location. The enOpenMode parameter defines whether the medium will be opened
|
---|
1112 | * read/write or read-only.
|
---|
1113 | *
|
---|
1114 | * This gets called by VirtualBox::OpenMedium() and also by
|
---|
1115 | * Machine::AttachDevice() and createImplicitDiffs() when new diff
|
---|
1116 | * images are created.
|
---|
1117 | *
|
---|
1118 | * There is no registry for this case since starting with VirtualBox 4.0, we
|
---|
1119 | * no longer add opened media to a registry automatically (this is deferred
|
---|
1120 | * until the medium is attached to a machine).
|
---|
1121 | *
|
---|
1122 | * For hard disks, the UUID, format and the parent of this medium will be
|
---|
1123 | * determined when reading the medium storage unit. For DVD and floppy images,
|
---|
1124 | * which have no UUIDs in their storage units, new UUIDs are created.
|
---|
1125 | * If the detected or set parent is not known to VirtualBox, then this method
|
---|
1126 | * will fail.
|
---|
1127 | *
|
---|
1128 | * @param aVirtualBox VirtualBox object.
|
---|
1129 | * @param aLocation Storage unit location.
|
---|
1130 | * @param enOpenMode Whether to open the medium read/write or read-only.
|
---|
1131 | * @param fForceNewUuid Whether a new UUID should be set to avoid duplicates.
|
---|
1132 | * @param aDeviceType Device type of medium.
|
---|
1133 | */
|
---|
1134 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1135 | const Utf8Str &aLocation,
|
---|
1136 | HDDOpenMode enOpenMode,
|
---|
1137 | bool fForceNewUuid,
|
---|
1138 | DeviceType_T aDeviceType)
|
---|
1139 | {
|
---|
1140 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1141 | AssertReturn(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1142 |
|
---|
1143 | HRESULT rc = S_OK;
|
---|
1144 |
|
---|
1145 | {
|
---|
1146 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1147 | AutoInitSpan autoInitSpan(this);
|
---|
1148 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1149 |
|
---|
1150 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1151 |
|
---|
1152 | /* there must be a storage unit */
|
---|
1153 | m->state = MediumState_Created;
|
---|
1154 |
|
---|
1155 | /* remember device type for correct unregistering later */
|
---|
1156 | m->devType = aDeviceType;
|
---|
1157 |
|
---|
1158 | /* cannot be a host drive */
|
---|
1159 | m->hostDrive = false;
|
---|
1160 |
|
---|
1161 | /* remember the open mode (defaults to ReadWrite) */
|
---|
1162 | m->hddOpenMode = enOpenMode;
|
---|
1163 |
|
---|
1164 | if (aDeviceType == DeviceType_DVD)
|
---|
1165 | m->type = MediumType_Readonly;
|
---|
1166 | else if (aDeviceType == DeviceType_Floppy)
|
---|
1167 | m->type = MediumType_Writethrough;
|
---|
1168 |
|
---|
1169 | rc = i_setLocation(aLocation);
|
---|
1170 | if (FAILED(rc)) return rc;
|
---|
1171 |
|
---|
1172 | /* get all the information about the medium from the storage unit */
|
---|
1173 | if (fForceNewUuid)
|
---|
1174 | unconst(m->uuidImage).create();
|
---|
1175 |
|
---|
1176 | m->state = MediumState_Inaccessible;
|
---|
1177 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
1178 |
|
---|
1179 | /* Confirm a successful initialization before the call to i_queryInfo.
|
---|
1180 | * Otherwise we can end up with a AutoCaller deadlock because the
|
---|
1181 | * medium becomes visible but is not marked as initialized. Causes
|
---|
1182 | * locking trouble (e.g. trying to save media registries) which is
|
---|
1183 | * hard to solve. */
|
---|
1184 | autoInitSpan.setSucceeded();
|
---|
1185 | }
|
---|
1186 |
|
---|
1187 | /* we're normal code from now on, no longer init */
|
---|
1188 | AutoCaller autoCaller(this);
|
---|
1189 | if (FAILED(autoCaller.rc()))
|
---|
1190 | return autoCaller.rc();
|
---|
1191 |
|
---|
1192 | /* need to call i_queryInfo immediately to correctly place the medium in
|
---|
1193 | * the respective media tree and update other information such as uuid */
|
---|
1194 | rc = i_queryInfo(fForceNewUuid /* fSetImageId */, false /* fSetParentId */,
|
---|
1195 | autoCaller);
|
---|
1196 | if (SUCCEEDED(rc))
|
---|
1197 | {
|
---|
1198 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1199 |
|
---|
1200 | /* if the storage unit is not accessible, it's not acceptable for the
|
---|
1201 | * newly opened media so convert this into an error */
|
---|
1202 | if (m->state == MediumState_Inaccessible)
|
---|
1203 | {
|
---|
1204 | Assert(!m->strLastAccessError.isEmpty());
|
---|
1205 | rc = setError(E_FAIL, "%s", m->strLastAccessError.c_str());
|
---|
1206 | alock.release();
|
---|
1207 | autoCaller.release();
|
---|
1208 | uninit();
|
---|
1209 | }
|
---|
1210 | else
|
---|
1211 | {
|
---|
1212 | AssertStmt(!m->id.isZero(),
|
---|
1213 | alock.release(); autoCaller.release(); uninit(); return E_FAIL);
|
---|
1214 |
|
---|
1215 | /* storage format must be detected by Medium::i_queryInfo if the
|
---|
1216 | * medium is accessible */
|
---|
1217 | AssertStmt(!m->strFormat.isEmpty(),
|
---|
1218 | alock.release(); autoCaller.release(); uninit(); return E_FAIL);
|
---|
1219 | }
|
---|
1220 | }
|
---|
1221 | else
|
---|
1222 | {
|
---|
1223 | /* opening this image failed, mark the object as dead */
|
---|
1224 | autoCaller.release();
|
---|
1225 | uninit();
|
---|
1226 | }
|
---|
1227 |
|
---|
1228 | return rc;
|
---|
1229 | }
|
---|
1230 |
|
---|
1231 | /**
|
---|
1232 | * Initializes the medium object by loading its data from the given settings
|
---|
1233 | * node. The medium will always be opened read/write.
|
---|
1234 | *
|
---|
1235 | * In this case, since we're loading from a registry, uuidMachineRegistry is
|
---|
1236 | * always set: it's either the global registry UUID or a machine UUID when
|
---|
1237 | * loading from a per-machine registry.
|
---|
1238 | *
|
---|
1239 | * @param aParent Parent medium disk or NULL for a root (base) medium.
|
---|
1240 | * @param aDeviceType Device type of the medium.
|
---|
1241 | * @param uuidMachineRegistry The registry to which this medium should be
|
---|
1242 | * added (global registry UUID or machine UUID).
|
---|
1243 | * @param data Configuration settings.
|
---|
1244 | * @param strMachineFolder The machine folder with which to resolve relative paths;
|
---|
1245 | * if empty, then we use the VirtualBox home directory
|
---|
1246 | *
|
---|
1247 | * @note Locks the medium tree for writing.
|
---|
1248 | */
|
---|
1249 | HRESULT Medium::initOne(Medium *aParent,
|
---|
1250 | DeviceType_T aDeviceType,
|
---|
1251 | const Guid &uuidMachineRegistry,
|
---|
1252 | const Utf8Str &strMachineFolder,
|
---|
1253 | const settings::Medium &data)
|
---|
1254 | {
|
---|
1255 | HRESULT rc;
|
---|
1256 |
|
---|
1257 | if (uuidMachineRegistry.isValid() && !uuidMachineRegistry.isZero())
|
---|
1258 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
1259 |
|
---|
1260 | /* register with VirtualBox/parent early, since uninit() will
|
---|
1261 | * unconditionally unregister on failure */
|
---|
1262 | if (aParent)
|
---|
1263 | {
|
---|
1264 | // differencing medium: add to parent
|
---|
1265 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1266 | // no need to check maximum depth as settings reading did it
|
---|
1267 | i_setParent(aParent);
|
---|
1268 | }
|
---|
1269 |
|
---|
1270 | /* see below why we don't call Medium::i_queryInfo (and therefore treat
|
---|
1271 | * the medium as inaccessible for now */
|
---|
1272 | m->state = MediumState_Inaccessible;
|
---|
1273 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
1274 |
|
---|
1275 | /* required */
|
---|
1276 | unconst(m->id) = data.uuid;
|
---|
1277 |
|
---|
1278 | /* assume not a host drive */
|
---|
1279 | m->hostDrive = false;
|
---|
1280 |
|
---|
1281 | /* optional */
|
---|
1282 | m->strDescription = data.strDescription;
|
---|
1283 |
|
---|
1284 | /* required */
|
---|
1285 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1286 | {
|
---|
1287 | AssertReturn(!data.strFormat.isEmpty(), E_FAIL);
|
---|
1288 | rc = i_setFormat(data.strFormat);
|
---|
1289 | if (FAILED(rc)) return rc;
|
---|
1290 | }
|
---|
1291 | else
|
---|
1292 | {
|
---|
1293 | /// @todo handle host drive settings here as well?
|
---|
1294 | if (!data.strFormat.isEmpty())
|
---|
1295 | rc = i_setFormat(data.strFormat);
|
---|
1296 | else
|
---|
1297 | rc = i_setFormat("RAW");
|
---|
1298 | if (FAILED(rc)) return rc;
|
---|
1299 | }
|
---|
1300 |
|
---|
1301 | /* optional, only for diffs, default is false; we can only auto-reset
|
---|
1302 | * diff media so they must have a parent */
|
---|
1303 | if (aParent != NULL)
|
---|
1304 | m->autoReset = data.fAutoReset;
|
---|
1305 | else
|
---|
1306 | m->autoReset = false;
|
---|
1307 |
|
---|
1308 | /* properties (after setting the format as it populates the map). Note that
|
---|
1309 | * if some properties are not supported but present in the settings file,
|
---|
1310 | * they will still be read and accessible (for possible backward
|
---|
1311 | * compatibility; we can also clean them up from the XML upon next
|
---|
1312 | * XML format version change if we wish) */
|
---|
1313 | for (settings::StringsMap::const_iterator it = data.properties.begin();
|
---|
1314 | it != data.properties.end();
|
---|
1315 | ++it)
|
---|
1316 | {
|
---|
1317 | const Utf8Str &name = it->first;
|
---|
1318 | const Utf8Str &value = it->second;
|
---|
1319 | m->mapProperties[name] = value;
|
---|
1320 | }
|
---|
1321 |
|
---|
1322 | /* try to decrypt an optional iSCSI initiator secret */
|
---|
1323 | settings::StringsMap::const_iterator itCph = data.properties.find("InitiatorSecretEncrypted");
|
---|
1324 | if ( itCph != data.properties.end()
|
---|
1325 | && !itCph->second.isEmpty())
|
---|
1326 | {
|
---|
1327 | Utf8Str strPlaintext;
|
---|
1328 | int vrc = m->pVirtualBox->i_decryptSetting(&strPlaintext, itCph->second);
|
---|
1329 | if (RT_SUCCESS(vrc))
|
---|
1330 | m->mapProperties["InitiatorSecret"] = strPlaintext;
|
---|
1331 | }
|
---|
1332 |
|
---|
1333 | Utf8Str strFull;
|
---|
1334 | if (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
1335 | {
|
---|
1336 | // compose full path of the medium, if it's not fully qualified...
|
---|
1337 | // slightly convoluted logic here. If the caller has given us a
|
---|
1338 | // machine folder, then a relative path will be relative to that:
|
---|
1339 | if ( !strMachineFolder.isEmpty()
|
---|
1340 | && !RTPathStartsWithRoot(data.strLocation.c_str())
|
---|
1341 | )
|
---|
1342 | {
|
---|
1343 | strFull = strMachineFolder;
|
---|
1344 | strFull += RTPATH_SLASH;
|
---|
1345 | strFull += data.strLocation;
|
---|
1346 | }
|
---|
1347 | else
|
---|
1348 | {
|
---|
1349 | // Otherwise use the old VirtualBox "make absolute path" logic:
|
---|
1350 | int vrc = m->pVirtualBox->i_calculateFullPath(data.strLocation, strFull);
|
---|
1351 | if (RT_FAILURE(vrc))
|
---|
1352 | return Global::vboxStatusCodeToCOM(vrc);
|
---|
1353 | }
|
---|
1354 | }
|
---|
1355 | else
|
---|
1356 | strFull = data.strLocation;
|
---|
1357 |
|
---|
1358 | rc = i_setLocation(strFull);
|
---|
1359 | if (FAILED(rc)) return rc;
|
---|
1360 |
|
---|
1361 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1362 | {
|
---|
1363 | /* type is only for base hard disks */
|
---|
1364 | if (m->pParent.isNull())
|
---|
1365 | m->type = data.hdType;
|
---|
1366 | }
|
---|
1367 | else if (aDeviceType == DeviceType_DVD)
|
---|
1368 | m->type = MediumType_Readonly;
|
---|
1369 | else
|
---|
1370 | m->type = MediumType_Writethrough;
|
---|
1371 |
|
---|
1372 | /* remember device type for correct unregistering later */
|
---|
1373 | m->devType = aDeviceType;
|
---|
1374 |
|
---|
1375 | LogFlowThisFunc(("m->strLocationFull='%s', m->strFormat=%s, m->id={%RTuuid}\n",
|
---|
1376 | m->strLocationFull.c_str(), m->strFormat.c_str(), m->id.raw()));
|
---|
1377 |
|
---|
1378 | return S_OK;
|
---|
1379 | }
|
---|
1380 |
|
---|
1381 | /**
|
---|
1382 | * Initializes and registers the medium object and its children by loading its
|
---|
1383 | * data from the given settings node. The medium will always be opened
|
---|
1384 | * read/write.
|
---|
1385 | *
|
---|
1386 | * @todo r=bird: What's that stuff about 'always be opened read/write'?
|
---|
1387 | *
|
---|
1388 | * In this case, since we're loading from a registry, uuidMachineRegistry is
|
---|
1389 | * always set: it's either the global registry UUID or a machine UUID when
|
---|
1390 | * loading from a per-machine registry.
|
---|
1391 | *
|
---|
1392 | * The only caller is currently VirtualBox::initMedia().
|
---|
1393 | *
|
---|
1394 | * @param aVirtualBox VirtualBox object.
|
---|
1395 | * @param aDeviceType Device type of the medium.
|
---|
1396 | * @param uuidMachineRegistry The registry to which this medium should be added
|
---|
1397 | * (global registry UUID or machine UUID).
|
---|
1398 | * @param strMachineFolder The machine folder with which to resolve relative
|
---|
1399 | * paths; if empty, then we use the VirtualBox home directory
|
---|
1400 | * @param data Configuration settings.
|
---|
1401 | * @param mediaTreeLock Autolock.
|
---|
1402 | * @param uIdsForNotify List to be updated with newly registered media.
|
---|
1403 | *
|
---|
1404 | * @note Assumes that the medium tree lock is held for writing. May release
|
---|
1405 | * and lock it again. At the end it is always held.
|
---|
1406 | */
|
---|
1407 | /* static */
|
---|
1408 | HRESULT Medium::initFromSettings(VirtualBox *aVirtualBox,
|
---|
1409 | DeviceType_T aDeviceType,
|
---|
1410 | const Guid &uuidMachineRegistry,
|
---|
1411 | const Utf8Str &strMachineFolder,
|
---|
1412 | const settings::Medium &data,
|
---|
1413 | AutoWriteLock &mediaTreeLock,
|
---|
1414 | std::list<std::pair<Guid, DeviceType_T> > &uIdsForNotify)
|
---|
1415 | {
|
---|
1416 | Assert(aVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
1417 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1418 |
|
---|
1419 | HRESULT rc = S_OK;
|
---|
1420 |
|
---|
1421 | MediaList llMediaTocleanup;
|
---|
1422 |
|
---|
1423 | std::list<const settings::Medium *> llSettingsTodo;
|
---|
1424 | llSettingsTodo.push_back(&data);
|
---|
1425 | MediaList llParentsTodo;
|
---|
1426 | llParentsTodo.push_back(NULL);
|
---|
1427 |
|
---|
1428 | while (!llSettingsTodo.empty())
|
---|
1429 | {
|
---|
1430 | const settings::Medium *current = llSettingsTodo.front();
|
---|
1431 | llSettingsTodo.pop_front();
|
---|
1432 | ComObjPtr<Medium> pParent = llParentsTodo.front();
|
---|
1433 | llParentsTodo.pop_front();
|
---|
1434 |
|
---|
1435 | bool fReleasedMediaTreeLock = false;
|
---|
1436 | ComObjPtr<Medium> pMedium;
|
---|
1437 | rc = pMedium.createObject();
|
---|
1438 | if (FAILED(rc))
|
---|
1439 | break;
|
---|
1440 | ComObjPtr<Medium> pActualMedium(pMedium);
|
---|
1441 |
|
---|
1442 | {
|
---|
1443 | AutoInitSpan autoInitSpan(pMedium);
|
---|
1444 | AssertBreakStmt(autoInitSpan.isOk(), rc = E_FAIL);
|
---|
1445 |
|
---|
1446 | unconst(pMedium->m->pVirtualBox) = aVirtualBox;
|
---|
1447 | rc = pMedium->initOne(pParent, aDeviceType, uuidMachineRegistry, strMachineFolder, *current);
|
---|
1448 | if (FAILED(rc))
|
---|
1449 | break;
|
---|
1450 | rc = aVirtualBox->i_registerMedium(pActualMedium, &pActualMedium, mediaTreeLock, true /*fCalledFromMediumInit*/);
|
---|
1451 | if (FAILED(rc))
|
---|
1452 | break;
|
---|
1453 |
|
---|
1454 | if (pActualMedium == pMedium)
|
---|
1455 | {
|
---|
1456 | /* It is a truly new medium, remember details for cleanup. */
|
---|
1457 | autoInitSpan.setSucceeded();
|
---|
1458 | llMediaTocleanup.push_front(pMedium);
|
---|
1459 | }
|
---|
1460 | else
|
---|
1461 | {
|
---|
1462 | /* Since the newly created medium was replaced by an already
|
---|
1463 | * known one when merging medium trees, we can immediately mark
|
---|
1464 | * it as failed. */
|
---|
1465 | autoInitSpan.setFailed();
|
---|
1466 | mediaTreeLock.release();
|
---|
1467 | fReleasedMediaTreeLock = true;
|
---|
1468 | }
|
---|
1469 | }
|
---|
1470 | if (fReleasedMediaTreeLock)
|
---|
1471 | {
|
---|
1472 | /* With the InitSpan out of the way it's safe to let the refcount
|
---|
1473 | * drop to 0 without causing uninit trouble. */
|
---|
1474 | pMedium.setNull();
|
---|
1475 | mediaTreeLock.acquire();
|
---|
1476 | }
|
---|
1477 |
|
---|
1478 | /* create all children */
|
---|
1479 | std::list<settings::Medium>::const_iterator itBegin = current->llChildren.begin();
|
---|
1480 | std::list<settings::Medium>::const_iterator itEnd = current->llChildren.end();
|
---|
1481 | for (std::list<settings::Medium>::const_iterator it = itBegin; it != itEnd; ++it)
|
---|
1482 | {
|
---|
1483 | llSettingsTodo.push_back(&*it);
|
---|
1484 | llParentsTodo.push_back(pActualMedium);
|
---|
1485 | }
|
---|
1486 | }
|
---|
1487 |
|
---|
1488 | if (SUCCEEDED(rc))
|
---|
1489 | {
|
---|
1490 | /* Check for consistency. */
|
---|
1491 | Assert(llSettingsTodo.size() == 0);
|
---|
1492 | Assert(llParentsTodo.size() == 0);
|
---|
1493 | /* Create the list of notifications, parent first. */
|
---|
1494 | MediaList::const_reverse_iterator itBegin = llMediaTocleanup.rbegin();
|
---|
1495 | MediaList::const_reverse_iterator itEnd = llMediaTocleanup.rend();
|
---|
1496 | for (MediaList::const_reverse_iterator it = itBegin; it != itEnd; --it)
|
---|
1497 | {
|
---|
1498 | ComObjPtr<Medium> pMedium = *it;
|
---|
1499 | AutoCaller mediumCaller(pMedium);
|
---|
1500 | if (FAILED(mediumCaller.rc())) continue;
|
---|
1501 | const Guid &id = pMedium->i_getId();
|
---|
1502 | uIdsForNotify.push_back(std::pair<Guid, DeviceType_T>(id, aDeviceType));
|
---|
1503 | }
|
---|
1504 | }
|
---|
1505 | else
|
---|
1506 | {
|
---|
1507 | /* Forget state of the settings processing. */
|
---|
1508 | llSettingsTodo.clear();
|
---|
1509 | llParentsTodo.clear();
|
---|
1510 | /* Unregister all accumulated medium objects in the right order (last
|
---|
1511 | * created to first created, avoiding config leftovers). */
|
---|
1512 | MediaList::const_iterator itBegin = llMediaTocleanup.begin();
|
---|
1513 | MediaList::const_iterator itEnd = llMediaTocleanup.end();
|
---|
1514 | for (MediaList::const_iterator it = itBegin; it != itEnd; ++it)
|
---|
1515 | {
|
---|
1516 | ComObjPtr<Medium> pMedium = *it;
|
---|
1517 | pMedium->i_unregisterWithVirtualBox();
|
---|
1518 | }
|
---|
1519 | /* Forget the only references to all newly created medium objects,
|
---|
1520 | * triggering freeing (uninit happened in unregistering above). */
|
---|
1521 | mediaTreeLock.release();
|
---|
1522 | llMediaTocleanup.clear();
|
---|
1523 | mediaTreeLock.acquire();
|
---|
1524 | }
|
---|
1525 |
|
---|
1526 | return rc;
|
---|
1527 | }
|
---|
1528 |
|
---|
1529 | /**
|
---|
1530 | * Initializes the medium object by providing the host drive information.
|
---|
1531 | * Not used for anything but the host floppy/host DVD case.
|
---|
1532 | *
|
---|
1533 | * There is no registry for this case.
|
---|
1534 | *
|
---|
1535 | * @param aVirtualBox VirtualBox object.
|
---|
1536 | * @param aDeviceType Device type of the medium.
|
---|
1537 | * @param aLocation Location of the host drive.
|
---|
1538 | * @param aDescription Comment for this host drive.
|
---|
1539 | *
|
---|
1540 | * @note Locks VirtualBox lock for writing.
|
---|
1541 | */
|
---|
1542 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1543 | DeviceType_T aDeviceType,
|
---|
1544 | const Utf8Str &aLocation,
|
---|
1545 | const Utf8Str &aDescription /* = Utf8Str::Empty */)
|
---|
1546 | {
|
---|
1547 | ComAssertRet(aDeviceType == DeviceType_DVD || aDeviceType == DeviceType_Floppy, E_INVALIDARG);
|
---|
1548 | ComAssertRet(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1549 |
|
---|
1550 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1551 | AutoInitSpan autoInitSpan(this);
|
---|
1552 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1553 |
|
---|
1554 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1555 |
|
---|
1556 | // We do not store host drives in VirtualBox.xml or anywhere else, so if we want
|
---|
1557 | // host drives to be identifiable by UUID and not give the drive a different UUID
|
---|
1558 | // every time VirtualBox starts, we need to fake a reproducible UUID here:
|
---|
1559 | RTUUID uuid;
|
---|
1560 | RTUuidClear(&uuid);
|
---|
1561 | if (aDeviceType == DeviceType_DVD)
|
---|
1562 | memcpy(&uuid.au8[0], "DVD", 3);
|
---|
1563 | else
|
---|
1564 | memcpy(&uuid.au8[0], "FD", 2);
|
---|
1565 | /* use device name, adjusted to the end of uuid, shortened if necessary */
|
---|
1566 | size_t lenLocation = aLocation.length();
|
---|
1567 | if (lenLocation > 12)
|
---|
1568 | memcpy(&uuid.au8[4], aLocation.c_str() + (lenLocation - 12), 12);
|
---|
1569 | else
|
---|
1570 | memcpy(&uuid.au8[4 + 12 - lenLocation], aLocation.c_str(), lenLocation);
|
---|
1571 | unconst(m->id) = uuid;
|
---|
1572 |
|
---|
1573 | if (aDeviceType == DeviceType_DVD)
|
---|
1574 | m->type = MediumType_Readonly;
|
---|
1575 | else
|
---|
1576 | m->type = MediumType_Writethrough;
|
---|
1577 | m->devType = aDeviceType;
|
---|
1578 | m->state = MediumState_Created;
|
---|
1579 | m->hostDrive = true;
|
---|
1580 | HRESULT rc = i_setFormat("RAW");
|
---|
1581 | if (FAILED(rc)) return rc;
|
---|
1582 | rc = i_setLocation(aLocation);
|
---|
1583 | if (FAILED(rc)) return rc;
|
---|
1584 | m->strDescription = aDescription;
|
---|
1585 |
|
---|
1586 | autoInitSpan.setSucceeded();
|
---|
1587 | return S_OK;
|
---|
1588 | }
|
---|
1589 |
|
---|
1590 | /**
|
---|
1591 | * Uninitializes the instance.
|
---|
1592 | *
|
---|
1593 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
1594 | *
|
---|
1595 | * @note All children of this medium get uninitialized, too, in a stack
|
---|
1596 | * friendly manner.
|
---|
1597 | */
|
---|
1598 | void Medium::uninit()
|
---|
1599 | {
|
---|
1600 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1601 | * the pVirtualBox reference, and in this case we don't need to continue.
|
---|
1602 | * Normally this would be handled through the AutoUninitSpan magic, however
|
---|
1603 | * this cannot be done at this point as the media tree must be locked
|
---|
1604 | * before reaching the AutoUninitSpan, otherwise deadlocks can happen.
|
---|
1605 | *
|
---|
1606 | * NOTE: The tree lock is higher priority than the medium caller and medium
|
---|
1607 | * object locks, i.e. the medium caller may have to be released and be
|
---|
1608 | * re-acquired in the right place later. See Medium::getParent() for sample
|
---|
1609 | * code how to do this safely. */
|
---|
1610 | VirtualBox *pVirtualBox = m->pVirtualBox;
|
---|
1611 | if (!pVirtualBox)
|
---|
1612 | return;
|
---|
1613 |
|
---|
1614 | /* Caller must not hold the object (checked below) or media tree lock. */
|
---|
1615 | Assert(!pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
1616 |
|
---|
1617 | AutoWriteLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1618 |
|
---|
1619 | /* Must use a list without refcounting help since "this" might already have
|
---|
1620 | * reached 0, and then the refcount must not be increased again since it
|
---|
1621 | * would otherwise trigger a double free. For all other list entries this
|
---|
1622 | * needs manual refcount updating, to make sure the refcount for children
|
---|
1623 | * does not drop to 0 too early. */
|
---|
1624 | std::list<Medium *> llMediaTodo;
|
---|
1625 | llMediaTodo.push_back(this);
|
---|
1626 |
|
---|
1627 | while (!llMediaTodo.empty())
|
---|
1628 | {
|
---|
1629 | Medium *pMedium = llMediaTodo.front();
|
---|
1630 | llMediaTodo.pop_front();
|
---|
1631 |
|
---|
1632 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
1633 | AutoUninitSpan autoUninitSpan(pMedium);
|
---|
1634 | if (autoUninitSpan.uninitDone())
|
---|
1635 | {
|
---|
1636 | if (pMedium != this)
|
---|
1637 | pMedium->Release();
|
---|
1638 | continue;
|
---|
1639 | }
|
---|
1640 |
|
---|
1641 | Assert(!pMedium->isWriteLockOnCurrentThread());
|
---|
1642 | #ifdef DEBUG
|
---|
1643 | if (!pMedium->m->backRefs.empty())
|
---|
1644 | pMedium->i_dumpBackRefs();
|
---|
1645 | #endif
|
---|
1646 | Assert(pMedium->m->backRefs.empty());
|
---|
1647 |
|
---|
1648 | pMedium->m->formatObj.setNull();
|
---|
1649 |
|
---|
1650 | if (pMedium->m->state == MediumState_Deleting)
|
---|
1651 | {
|
---|
1652 | /* This medium has been already deleted (directly or as part of a
|
---|
1653 | * merge). Reparenting has already been done. */
|
---|
1654 | Assert(pMedium->m->pParent.isNull());
|
---|
1655 | Assert(pMedium->m->llChildren.empty());
|
---|
1656 | if (pMedium != this)
|
---|
1657 | pMedium->Release();
|
---|
1658 | continue;
|
---|
1659 | }
|
---|
1660 |
|
---|
1661 | //Assert(!pMedium->m->pParent);
|
---|
1662 | /** @todo r=klaus Should not be necessary, since the caller should be
|
---|
1663 | * doing the deparenting. No time right now to test everything. */
|
---|
1664 | if (pMedium == this && pMedium->m->pParent)
|
---|
1665 | pMedium->i_deparent();
|
---|
1666 |
|
---|
1667 | /* Process all children */
|
---|
1668 | MediaList::const_iterator itBegin = pMedium->m->llChildren.begin();
|
---|
1669 | MediaList::const_iterator itEnd = pMedium->m->llChildren.end();
|
---|
1670 | for (MediaList::const_iterator it = itBegin; it != itEnd; ++it)
|
---|
1671 | {
|
---|
1672 | Medium *pChild = *it;
|
---|
1673 | pChild->m->pParent.setNull();
|
---|
1674 | pChild->AddRef();
|
---|
1675 | llMediaTodo.push_back(pChild);
|
---|
1676 | }
|
---|
1677 |
|
---|
1678 | /* Children information obsolete, will be processed anyway. */
|
---|
1679 | pMedium->m->llChildren.clear();
|
---|
1680 |
|
---|
1681 | unconst(pMedium->m->pVirtualBox) = NULL;
|
---|
1682 |
|
---|
1683 | if (pMedium != this)
|
---|
1684 | pMedium->Release();
|
---|
1685 |
|
---|
1686 | autoUninitSpan.setSucceeded();
|
---|
1687 | }
|
---|
1688 | }
|
---|
1689 |
|
---|
1690 | /**
|
---|
1691 | * Internal helper that removes "this" from the list of children of its
|
---|
1692 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1693 | *
|
---|
1694 | * The caller must hold the medium tree lock!
|
---|
1695 | */
|
---|
1696 | void Medium::i_deparent()
|
---|
1697 | {
|
---|
1698 | MediaList &llParent = m->pParent->m->llChildren;
|
---|
1699 | for (MediaList::iterator it = llParent.begin();
|
---|
1700 | it != llParent.end();
|
---|
1701 | ++it)
|
---|
1702 | {
|
---|
1703 | Medium *pParentsChild = *it;
|
---|
1704 | if (this == pParentsChild)
|
---|
1705 | {
|
---|
1706 | llParent.erase(it);
|
---|
1707 | break;
|
---|
1708 | }
|
---|
1709 | }
|
---|
1710 | m->pParent.setNull();
|
---|
1711 | }
|
---|
1712 |
|
---|
1713 | /**
|
---|
1714 | * Internal helper that removes "this" from the list of children of its
|
---|
1715 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1716 | *
|
---|
1717 | * The caller must hold the medium tree lock!
|
---|
1718 | */
|
---|
1719 | void Medium::i_setParent(const ComObjPtr<Medium> &pParent)
|
---|
1720 | {
|
---|
1721 | m->pParent = pParent;
|
---|
1722 | if (pParent)
|
---|
1723 | pParent->m->llChildren.push_back(this);
|
---|
1724 | }
|
---|
1725 |
|
---|
1726 |
|
---|
1727 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1728 | //
|
---|
1729 | // IMedium public methods
|
---|
1730 | //
|
---|
1731 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1732 |
|
---|
1733 | HRESULT Medium::getId(com::Guid &aId)
|
---|
1734 | {
|
---|
1735 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1736 |
|
---|
1737 | aId = m->id;
|
---|
1738 |
|
---|
1739 | return S_OK;
|
---|
1740 | }
|
---|
1741 |
|
---|
1742 | HRESULT Medium::getDescription(AutoCaller &autoCaller, com::Utf8Str &aDescription)
|
---|
1743 | {
|
---|
1744 | NOREF(autoCaller);
|
---|
1745 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1746 |
|
---|
1747 | aDescription = m->strDescription;
|
---|
1748 |
|
---|
1749 | return S_OK;
|
---|
1750 | }
|
---|
1751 |
|
---|
1752 | HRESULT Medium::setDescription(AutoCaller &autoCaller, const com::Utf8Str &aDescription)
|
---|
1753 | {
|
---|
1754 | /// @todo update m->strDescription and save the global registry (and local
|
---|
1755 | /// registries of portable VMs referring to this medium), this will also
|
---|
1756 | /// require to add the mRegistered flag to data
|
---|
1757 |
|
---|
1758 | HRESULT rc = S_OK;
|
---|
1759 |
|
---|
1760 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
1761 |
|
---|
1762 | try
|
---|
1763 | {
|
---|
1764 | autoCaller.release();
|
---|
1765 |
|
---|
1766 | // to avoid redundant locking, which just takes a time, just call required functions.
|
---|
1767 | // the error will be just stored and will be reported after locks will be acquired again
|
---|
1768 |
|
---|
1769 | const char *pszError = NULL;
|
---|
1770 |
|
---|
1771 |
|
---|
1772 | /* Build the lock list. */
|
---|
1773 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
1774 | this /* pToLockWrite */,
|
---|
1775 | true /* fMediumLockWriteAll */,
|
---|
1776 | NULL,
|
---|
1777 | *pMediumLockList);
|
---|
1778 | if (FAILED(rc))
|
---|
1779 | {
|
---|
1780 | pszError = tr("Failed to create medium lock list for '%s'");
|
---|
1781 | }
|
---|
1782 | else
|
---|
1783 | {
|
---|
1784 | rc = pMediumLockList->Lock();
|
---|
1785 | if (FAILED(rc))
|
---|
1786 | pszError = tr("Failed to lock media '%s'");
|
---|
1787 | }
|
---|
1788 |
|
---|
1789 | // locking: we need the tree lock first because we access parent pointers
|
---|
1790 | // and we need to write-lock the media involved
|
---|
1791 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1792 |
|
---|
1793 | autoCaller.add();
|
---|
1794 | AssertComRCThrowRC(autoCaller.rc());
|
---|
1795 |
|
---|
1796 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1797 |
|
---|
1798 | if (FAILED(rc))
|
---|
1799 | throw setError(rc, pszError, i_getLocationFull().c_str());
|
---|
1800 |
|
---|
1801 | /* Set a new description */
|
---|
1802 | m->strDescription = aDescription;
|
---|
1803 |
|
---|
1804 | // save the settings
|
---|
1805 | alock.release();
|
---|
1806 | autoCaller.release();
|
---|
1807 | treeLock.release();
|
---|
1808 | i_markRegistriesModified();
|
---|
1809 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
1810 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
1811 | }
|
---|
1812 | catch (HRESULT aRC) { rc = aRC; }
|
---|
1813 |
|
---|
1814 | delete pMediumLockList;
|
---|
1815 |
|
---|
1816 | return rc;
|
---|
1817 | }
|
---|
1818 |
|
---|
1819 | HRESULT Medium::getState(MediumState_T *aState)
|
---|
1820 | {
|
---|
1821 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1822 | *aState = m->state;
|
---|
1823 |
|
---|
1824 | return S_OK;
|
---|
1825 | }
|
---|
1826 |
|
---|
1827 | HRESULT Medium::getVariant(std::vector<MediumVariant_T> &aVariant)
|
---|
1828 | {
|
---|
1829 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1830 |
|
---|
1831 | const size_t cBits = sizeof(MediumVariant_T) * 8;
|
---|
1832 | aVariant.resize(cBits);
|
---|
1833 | for (size_t i = 0; i < cBits; ++i)
|
---|
1834 | aVariant[i] = (MediumVariant_T)(m->variant & RT_BIT(i));
|
---|
1835 |
|
---|
1836 | return S_OK;
|
---|
1837 | }
|
---|
1838 |
|
---|
1839 | HRESULT Medium::getLocation(com::Utf8Str &aLocation)
|
---|
1840 | {
|
---|
1841 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1842 |
|
---|
1843 | aLocation = m->strLocationFull;
|
---|
1844 |
|
---|
1845 | return S_OK;
|
---|
1846 | }
|
---|
1847 |
|
---|
1848 | HRESULT Medium::getName(com::Utf8Str &aName)
|
---|
1849 | {
|
---|
1850 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1851 |
|
---|
1852 | aName = i_getName();
|
---|
1853 |
|
---|
1854 | return S_OK;
|
---|
1855 | }
|
---|
1856 |
|
---|
1857 | HRESULT Medium::getDeviceType(DeviceType_T *aDeviceType)
|
---|
1858 | {
|
---|
1859 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1860 |
|
---|
1861 | *aDeviceType = m->devType;
|
---|
1862 |
|
---|
1863 | return S_OK;
|
---|
1864 | }
|
---|
1865 |
|
---|
1866 | HRESULT Medium::getHostDrive(BOOL *aHostDrive)
|
---|
1867 | {
|
---|
1868 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1869 |
|
---|
1870 | *aHostDrive = m->hostDrive;
|
---|
1871 |
|
---|
1872 | return S_OK;
|
---|
1873 | }
|
---|
1874 |
|
---|
1875 | HRESULT Medium::getSize(LONG64 *aSize)
|
---|
1876 | {
|
---|
1877 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1878 |
|
---|
1879 | *aSize = (LONG64)m->size;
|
---|
1880 |
|
---|
1881 | return S_OK;
|
---|
1882 | }
|
---|
1883 |
|
---|
1884 | HRESULT Medium::getFormat(com::Utf8Str &aFormat)
|
---|
1885 | {
|
---|
1886 | /* no need to lock, m->strFormat is const */
|
---|
1887 |
|
---|
1888 | aFormat = m->strFormat;
|
---|
1889 | return S_OK;
|
---|
1890 | }
|
---|
1891 |
|
---|
1892 | HRESULT Medium::getMediumFormat(ComPtr<IMediumFormat> &aMediumFormat)
|
---|
1893 | {
|
---|
1894 | /* no need to lock, m->formatObj is const */
|
---|
1895 | m->formatObj.queryInterfaceTo(aMediumFormat.asOutParam());
|
---|
1896 |
|
---|
1897 | return S_OK;
|
---|
1898 | }
|
---|
1899 |
|
---|
1900 | HRESULT Medium::getType(AutoCaller &autoCaller, MediumType_T *aType)
|
---|
1901 | {
|
---|
1902 | NOREF(autoCaller);
|
---|
1903 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1904 |
|
---|
1905 | *aType = m->type;
|
---|
1906 |
|
---|
1907 | return S_OK;
|
---|
1908 | }
|
---|
1909 |
|
---|
1910 | HRESULT Medium::setType(AutoCaller &autoCaller, MediumType_T aType)
|
---|
1911 | {
|
---|
1912 | autoCaller.release();
|
---|
1913 |
|
---|
1914 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1915 | * the pVirtualBox reference, see #uninit(). */
|
---|
1916 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1917 |
|
---|
1918 | // we access m->pParent
|
---|
1919 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
1920 |
|
---|
1921 | autoCaller.add();
|
---|
1922 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1923 |
|
---|
1924 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1925 |
|
---|
1926 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
1927 | while (m->queryInfoRunning)
|
---|
1928 | {
|
---|
1929 | mlock.release();
|
---|
1930 | autoCaller.release();
|
---|
1931 | treeLock.release();
|
---|
1932 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs
|
---|
1933 | * this lock and thus we would run into a deadlock here. */
|
---|
1934 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
1935 | /* must not hold the object lock now */
|
---|
1936 | Assert(!isWriteLockOnCurrentThread());
|
---|
1937 | {
|
---|
1938 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
1939 | }
|
---|
1940 | treeLock.acquire();
|
---|
1941 | autoCaller.add();
|
---|
1942 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1943 | mlock.acquire();
|
---|
1944 | }
|
---|
1945 |
|
---|
1946 | switch (m->state)
|
---|
1947 | {
|
---|
1948 | case MediumState_Created:
|
---|
1949 | case MediumState_Inaccessible:
|
---|
1950 | break;
|
---|
1951 | default:
|
---|
1952 | return i_setStateError();
|
---|
1953 | }
|
---|
1954 |
|
---|
1955 | if (m->type == aType)
|
---|
1956 | {
|
---|
1957 | /* Nothing to do */
|
---|
1958 | return S_OK;
|
---|
1959 | }
|
---|
1960 |
|
---|
1961 | DeviceType_T devType = i_getDeviceType();
|
---|
1962 | // DVD media can only be readonly.
|
---|
1963 | if (devType == DeviceType_DVD && aType != MediumType_Readonly)
|
---|
1964 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1965 | tr("Cannot change the type of DVD medium '%s'"),
|
---|
1966 | m->strLocationFull.c_str());
|
---|
1967 | // Floppy media can only be writethrough or readonly.
|
---|
1968 | if ( devType == DeviceType_Floppy
|
---|
1969 | && aType != MediumType_Writethrough
|
---|
1970 | && aType != MediumType_Readonly)
|
---|
1971 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1972 | tr("Cannot change the type of floppy medium '%s'"),
|
---|
1973 | m->strLocationFull.c_str());
|
---|
1974 |
|
---|
1975 | /* cannot change the type of a differencing medium */
|
---|
1976 | if (m->pParent)
|
---|
1977 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1978 | tr("Cannot change the type of medium '%s' because it is a differencing medium"),
|
---|
1979 | m->strLocationFull.c_str());
|
---|
1980 |
|
---|
1981 | /* Cannot change the type of a medium being in use by more than one VM.
|
---|
1982 | * If the change is to Immutable or MultiAttach then it must not be
|
---|
1983 | * directly attached to any VM, otherwise the assumptions about indirect
|
---|
1984 | * attachment elsewhere are violated and the VM becomes inaccessible.
|
---|
1985 | * Attaching an immutable medium triggers the diff creation, and this is
|
---|
1986 | * vital for the correct operation. */
|
---|
1987 | if ( m->backRefs.size() > 1
|
---|
1988 | || ( ( aType == MediumType_Immutable
|
---|
1989 | || aType == MediumType_MultiAttach)
|
---|
1990 | && m->backRefs.size() > 0))
|
---|
1991 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1992 | tr("Cannot change the type of medium '%s' because it is attached to %d virtual machines",
|
---|
1993 | "", m->backRefs.size()),
|
---|
1994 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
1995 |
|
---|
1996 | switch (aType)
|
---|
1997 | {
|
---|
1998 | case MediumType_Normal:
|
---|
1999 | case MediumType_Immutable:
|
---|
2000 | case MediumType_MultiAttach:
|
---|
2001 | {
|
---|
2002 | /* normal can be easily converted to immutable and vice versa even
|
---|
2003 | * if they have children as long as they are not attached to any
|
---|
2004 | * machine themselves */
|
---|
2005 | break;
|
---|
2006 | }
|
---|
2007 | case MediumType_Writethrough:
|
---|
2008 | case MediumType_Shareable:
|
---|
2009 | case MediumType_Readonly:
|
---|
2010 | {
|
---|
2011 | /* cannot change to writethrough, shareable or readonly
|
---|
2012 | * if there are children */
|
---|
2013 | if (i_getChildren().size() != 0)
|
---|
2014 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
2015 | tr("Cannot change type for medium '%s' since it has %d child media", "", i_getChildren().size()),
|
---|
2016 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
2017 | if (aType == MediumType_Shareable)
|
---|
2018 | {
|
---|
2019 | MediumVariant_T variant = i_getVariant();
|
---|
2020 | if (!(variant & MediumVariant_Fixed))
|
---|
2021 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2022 | tr("Cannot change type for medium '%s' to 'Shareable' since it is a dynamic medium storage unit"),
|
---|
2023 | m->strLocationFull.c_str());
|
---|
2024 | }
|
---|
2025 | else if (aType == MediumType_Readonly && devType == DeviceType_HardDisk)
|
---|
2026 | {
|
---|
2027 | // Readonly hard disks are not allowed, this medium type is reserved for
|
---|
2028 | // DVDs and floppy images at the moment. Later we might allow readonly hard
|
---|
2029 | // disks, but that's extremely unusual and many guest OSes will have trouble.
|
---|
2030 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2031 | tr("Cannot change type for medium '%s' to 'Readonly' since it is a hard disk"),
|
---|
2032 | m->strLocationFull.c_str());
|
---|
2033 | }
|
---|
2034 | break;
|
---|
2035 | }
|
---|
2036 | default:
|
---|
2037 | AssertFailedReturn(E_FAIL);
|
---|
2038 | }
|
---|
2039 |
|
---|
2040 | if (aType == MediumType_MultiAttach)
|
---|
2041 | {
|
---|
2042 | // This type is new with VirtualBox 4.0 and therefore requires settings
|
---|
2043 | // version 1.11 in the settings backend. Unfortunately it is not enough to do
|
---|
2044 | // the usual routine in MachineConfigFile::bumpSettingsVersionIfNeeded() for
|
---|
2045 | // two reasons: The medium type is a property of the media registry tree, which
|
---|
2046 | // can reside in the global config file (for pre-4.0 media); we would therefore
|
---|
2047 | // possibly need to bump the global config version. We don't want to do that though
|
---|
2048 | // because that might make downgrading to pre-4.0 impossible.
|
---|
2049 | // As a result, we can only use these two new types if the medium is NOT in the
|
---|
2050 | // global registry:
|
---|
2051 | const Guid &uuidGlobalRegistry = m->pVirtualBox->i_getGlobalRegistryId();
|
---|
2052 | if (i_isInRegistry(uuidGlobalRegistry))
|
---|
2053 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2054 | tr("Cannot change type for medium '%s': the media type 'MultiAttach' can only be used "
|
---|
2055 | "on media registered with a machine that was created with VirtualBox 4.0 or later"),
|
---|
2056 | m->strLocationFull.c_str());
|
---|
2057 | }
|
---|
2058 |
|
---|
2059 | m->type = aType;
|
---|
2060 |
|
---|
2061 | // save the settings
|
---|
2062 | mlock.release();
|
---|
2063 | autoCaller.release();
|
---|
2064 | treeLock.release();
|
---|
2065 | i_markRegistriesModified();
|
---|
2066 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2067 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
2068 |
|
---|
2069 | return S_OK;
|
---|
2070 | }
|
---|
2071 |
|
---|
2072 | HRESULT Medium::getAllowedTypes(std::vector<MediumType_T> &aAllowedTypes)
|
---|
2073 | {
|
---|
2074 | NOREF(aAllowedTypes);
|
---|
2075 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2076 |
|
---|
2077 | ReturnComNotImplemented();
|
---|
2078 | }
|
---|
2079 |
|
---|
2080 | HRESULT Medium::getParent(AutoCaller &autoCaller, ComPtr<IMedium> &aParent)
|
---|
2081 | {
|
---|
2082 | autoCaller.release();
|
---|
2083 |
|
---|
2084 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
2085 | * the pVirtualBox reference, see #uninit(). */
|
---|
2086 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2087 |
|
---|
2088 | /* we access m->pParent */
|
---|
2089 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
2090 |
|
---|
2091 | autoCaller.add();
|
---|
2092 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2093 |
|
---|
2094 | m->pParent.queryInterfaceTo(aParent.asOutParam());
|
---|
2095 |
|
---|
2096 | return S_OK;
|
---|
2097 | }
|
---|
2098 |
|
---|
2099 | HRESULT Medium::getChildren(AutoCaller &autoCaller, std::vector<ComPtr<IMedium> > &aChildren)
|
---|
2100 | {
|
---|
2101 | autoCaller.release();
|
---|
2102 |
|
---|
2103 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
2104 | * the pVirtualBox reference, see #uninit(). */
|
---|
2105 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2106 |
|
---|
2107 | /* we access children */
|
---|
2108 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
2109 |
|
---|
2110 | autoCaller.add();
|
---|
2111 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2112 |
|
---|
2113 | MediaList children(this->i_getChildren());
|
---|
2114 | aChildren.resize(children.size());
|
---|
2115 | size_t i = 0;
|
---|
2116 | for (MediaList::const_iterator it = children.begin(); it != children.end(); ++it, ++i)
|
---|
2117 | (*it).queryInterfaceTo(aChildren[i].asOutParam());
|
---|
2118 | return S_OK;
|
---|
2119 | }
|
---|
2120 |
|
---|
2121 | HRESULT Medium::getBase(AutoCaller &autoCaller, ComPtr<IMedium> &aBase)
|
---|
2122 | {
|
---|
2123 | autoCaller.release();
|
---|
2124 |
|
---|
2125 | /* i_getBase() will do callers/locking */
|
---|
2126 | i_getBase().queryInterfaceTo(aBase.asOutParam());
|
---|
2127 |
|
---|
2128 | return S_OK;
|
---|
2129 | }
|
---|
2130 |
|
---|
2131 | HRESULT Medium::getReadOnly(AutoCaller &autoCaller, BOOL *aReadOnly)
|
---|
2132 | {
|
---|
2133 | autoCaller.release();
|
---|
2134 |
|
---|
2135 | /* isReadOnly() will do locking */
|
---|
2136 | *aReadOnly = i_isReadOnly();
|
---|
2137 |
|
---|
2138 | return S_OK;
|
---|
2139 | }
|
---|
2140 |
|
---|
2141 | HRESULT Medium::getLogicalSize(LONG64 *aLogicalSize)
|
---|
2142 | {
|
---|
2143 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2144 |
|
---|
2145 | *aLogicalSize = (LONG64)m->logicalSize;
|
---|
2146 |
|
---|
2147 | return S_OK;
|
---|
2148 | }
|
---|
2149 |
|
---|
2150 | HRESULT Medium::getAutoReset(BOOL *aAutoReset)
|
---|
2151 | {
|
---|
2152 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2153 |
|
---|
2154 | if (m->pParent.isNull())
|
---|
2155 | *aAutoReset = FALSE;
|
---|
2156 | else
|
---|
2157 | *aAutoReset = m->autoReset;
|
---|
2158 |
|
---|
2159 | return S_OK;
|
---|
2160 | }
|
---|
2161 |
|
---|
2162 | HRESULT Medium::setAutoReset(BOOL aAutoReset)
|
---|
2163 | {
|
---|
2164 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2165 |
|
---|
2166 | if (m->pParent.isNull())
|
---|
2167 | return setError(VBOX_E_NOT_SUPPORTED,
|
---|
2168 | tr("Medium '%s' is not differencing"),
|
---|
2169 | m->strLocationFull.c_str());
|
---|
2170 |
|
---|
2171 | if (m->autoReset != !!aAutoReset)
|
---|
2172 | {
|
---|
2173 | m->autoReset = !!aAutoReset;
|
---|
2174 |
|
---|
2175 | // save the settings
|
---|
2176 | mlock.release();
|
---|
2177 | i_markRegistriesModified();
|
---|
2178 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2179 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
2180 | }
|
---|
2181 |
|
---|
2182 | return S_OK;
|
---|
2183 | }
|
---|
2184 |
|
---|
2185 | HRESULT Medium::getLastAccessError(com::Utf8Str &aLastAccessError)
|
---|
2186 | {
|
---|
2187 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2188 |
|
---|
2189 | aLastAccessError = m->strLastAccessError;
|
---|
2190 |
|
---|
2191 | return S_OK;
|
---|
2192 | }
|
---|
2193 |
|
---|
2194 | HRESULT Medium::getMachineIds(std::vector<com::Guid> &aMachineIds)
|
---|
2195 | {
|
---|
2196 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2197 |
|
---|
2198 | if (m->backRefs.size() != 0)
|
---|
2199 | {
|
---|
2200 | BackRefList brlist(m->backRefs);
|
---|
2201 | aMachineIds.resize(brlist.size());
|
---|
2202 | size_t i = 0;
|
---|
2203 | for (BackRefList::const_iterator it = brlist.begin(); it != brlist.end(); ++it, ++i)
|
---|
2204 | aMachineIds[i] = it->machineId;
|
---|
2205 | }
|
---|
2206 |
|
---|
2207 | return S_OK;
|
---|
2208 | }
|
---|
2209 |
|
---|
2210 | HRESULT Medium::setIds(AutoCaller &autoCaller,
|
---|
2211 | BOOL aSetImageId,
|
---|
2212 | const com::Guid &aImageId,
|
---|
2213 | BOOL aSetParentId,
|
---|
2214 | const com::Guid &aParentId)
|
---|
2215 | {
|
---|
2216 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2217 |
|
---|
2218 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2219 | if (m->queryInfoRunning)
|
---|
2220 | {
|
---|
2221 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2222 | * lock and thus we would run into a deadlock here. */
|
---|
2223 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2224 | while (m->queryInfoRunning)
|
---|
2225 | {
|
---|
2226 | alock.release();
|
---|
2227 | /* must not hold the object lock now */
|
---|
2228 | Assert(!isWriteLockOnCurrentThread());
|
---|
2229 | {
|
---|
2230 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2231 | }
|
---|
2232 | alock.acquire();
|
---|
2233 | }
|
---|
2234 | }
|
---|
2235 |
|
---|
2236 | switch (m->state)
|
---|
2237 | {
|
---|
2238 | case MediumState_Created:
|
---|
2239 | break;
|
---|
2240 | default:
|
---|
2241 | return i_setStateError();
|
---|
2242 | }
|
---|
2243 |
|
---|
2244 | Guid imageId, parentId;
|
---|
2245 | if (aSetImageId)
|
---|
2246 | {
|
---|
2247 | if (aImageId.isZero())
|
---|
2248 | imageId.create();
|
---|
2249 | else
|
---|
2250 | {
|
---|
2251 | imageId = aImageId;
|
---|
2252 | if (!imageId.isValid())
|
---|
2253 | return setError(E_INVALIDARG, tr("Argument %s is invalid"), "aImageId");
|
---|
2254 | }
|
---|
2255 | }
|
---|
2256 | if (aSetParentId)
|
---|
2257 | {
|
---|
2258 | if (aParentId.isZero())
|
---|
2259 | parentId.create();
|
---|
2260 | else
|
---|
2261 | parentId = aParentId;
|
---|
2262 | }
|
---|
2263 |
|
---|
2264 | const Guid uPrevImage = m->uuidImage;
|
---|
2265 | unconst(m->uuidImage) = imageId;
|
---|
2266 | ComObjPtr<Medium> pPrevParent = i_getParent();
|
---|
2267 | unconst(m->uuidParentImage) = parentId;
|
---|
2268 |
|
---|
2269 | // must not hold any locks before calling Medium::i_queryInfo
|
---|
2270 | alock.release();
|
---|
2271 |
|
---|
2272 | HRESULT rc = i_queryInfo(!!aSetImageId /* fSetImageId */,
|
---|
2273 | !!aSetParentId /* fSetParentId */,
|
---|
2274 | autoCaller);
|
---|
2275 |
|
---|
2276 | AutoReadLock arlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2277 | const Guid uCurrImage = m->uuidImage;
|
---|
2278 | ComObjPtr<Medium> pCurrParent = i_getParent();
|
---|
2279 | arlock.release();
|
---|
2280 |
|
---|
2281 | if (SUCCEEDED(rc))
|
---|
2282 | {
|
---|
2283 | if (uCurrImage != uPrevImage)
|
---|
2284 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
2285 | if (pPrevParent != pCurrParent)
|
---|
2286 | {
|
---|
2287 | if (pPrevParent)
|
---|
2288 | m->pVirtualBox->i_onMediumConfigChanged(pPrevParent);
|
---|
2289 | if (pCurrParent)
|
---|
2290 | m->pVirtualBox->i_onMediumConfigChanged(pCurrParent);
|
---|
2291 | }
|
---|
2292 | }
|
---|
2293 |
|
---|
2294 | return rc;
|
---|
2295 | }
|
---|
2296 |
|
---|
2297 | HRESULT Medium::refreshState(AutoCaller &autoCaller, MediumState_T *aState)
|
---|
2298 | {
|
---|
2299 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2300 |
|
---|
2301 | HRESULT rc = S_OK;
|
---|
2302 |
|
---|
2303 | switch (m->state)
|
---|
2304 | {
|
---|
2305 | case MediumState_Created:
|
---|
2306 | case MediumState_Inaccessible:
|
---|
2307 | case MediumState_LockedRead:
|
---|
2308 | {
|
---|
2309 | // must not hold any locks before calling Medium::i_queryInfo
|
---|
2310 | alock.release();
|
---|
2311 |
|
---|
2312 | rc = i_queryInfo(false /* fSetImageId */, false /* fSetParentId */,
|
---|
2313 | autoCaller);
|
---|
2314 |
|
---|
2315 | alock.acquire();
|
---|
2316 | break;
|
---|
2317 | }
|
---|
2318 | default:
|
---|
2319 | break;
|
---|
2320 | }
|
---|
2321 |
|
---|
2322 | *aState = m->state;
|
---|
2323 |
|
---|
2324 | return rc;
|
---|
2325 | }
|
---|
2326 |
|
---|
2327 | HRESULT Medium::getSnapshotIds(const com::Guid &aMachineId,
|
---|
2328 | std::vector<com::Guid> &aSnapshotIds)
|
---|
2329 | {
|
---|
2330 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2331 |
|
---|
2332 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
2333 | it != m->backRefs.end(); ++it)
|
---|
2334 | {
|
---|
2335 | if (it->machineId == aMachineId)
|
---|
2336 | {
|
---|
2337 | size_t size = it->llSnapshotIds.size();
|
---|
2338 |
|
---|
2339 | /* if the medium is attached to the machine in the current state, we
|
---|
2340 | * return its ID as the first element of the array */
|
---|
2341 | if (it->fInCurState)
|
---|
2342 | ++size;
|
---|
2343 |
|
---|
2344 | if (size > 0)
|
---|
2345 | {
|
---|
2346 | aSnapshotIds.resize(size);
|
---|
2347 |
|
---|
2348 | size_t j = 0;
|
---|
2349 | if (it->fInCurState)
|
---|
2350 | aSnapshotIds[j++] = it->machineId.toUtf16();
|
---|
2351 |
|
---|
2352 | for(std::list<SnapshotRef>::const_iterator jt = it->llSnapshotIds.begin(); jt != it->llSnapshotIds.end(); ++jt, ++j)
|
---|
2353 | aSnapshotIds[j] = jt->snapshotId;
|
---|
2354 | }
|
---|
2355 |
|
---|
2356 | break;
|
---|
2357 | }
|
---|
2358 | }
|
---|
2359 |
|
---|
2360 | return S_OK;
|
---|
2361 | }
|
---|
2362 |
|
---|
2363 | HRESULT Medium::lockRead(ComPtr<IToken> &aToken)
|
---|
2364 | {
|
---|
2365 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2366 |
|
---|
2367 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2368 | if (m->queryInfoRunning)
|
---|
2369 | {
|
---|
2370 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2371 | * lock and thus we would run into a deadlock here. */
|
---|
2372 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2373 | while (m->queryInfoRunning)
|
---|
2374 | {
|
---|
2375 | alock.release();
|
---|
2376 | /* must not hold the object lock now */
|
---|
2377 | Assert(!isWriteLockOnCurrentThread());
|
---|
2378 | {
|
---|
2379 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2380 | }
|
---|
2381 | alock.acquire();
|
---|
2382 | }
|
---|
2383 | }
|
---|
2384 |
|
---|
2385 | HRESULT rc = S_OK;
|
---|
2386 |
|
---|
2387 | switch (m->state)
|
---|
2388 | {
|
---|
2389 | case MediumState_Created:
|
---|
2390 | case MediumState_Inaccessible:
|
---|
2391 | case MediumState_LockedRead:
|
---|
2392 | {
|
---|
2393 | ++m->readers;
|
---|
2394 |
|
---|
2395 | ComAssertMsgBreak(m->readers != 0, (tr("Counter overflow")), rc = E_FAIL);
|
---|
2396 |
|
---|
2397 | /* Remember pre-lock state */
|
---|
2398 | if (m->state != MediumState_LockedRead)
|
---|
2399 | m->preLockState = m->state;
|
---|
2400 |
|
---|
2401 | LogFlowThisFunc(("Okay - prev state=%d readers=%d\n", m->state, m->readers));
|
---|
2402 | m->state = MediumState_LockedRead;
|
---|
2403 |
|
---|
2404 | ComObjPtr<MediumLockToken> pToken;
|
---|
2405 | rc = pToken.createObject();
|
---|
2406 | if (SUCCEEDED(rc))
|
---|
2407 | rc = pToken->init(this, false /* fWrite */);
|
---|
2408 | if (FAILED(rc))
|
---|
2409 | {
|
---|
2410 | --m->readers;
|
---|
2411 | if (m->readers == 0)
|
---|
2412 | m->state = m->preLockState;
|
---|
2413 | return rc;
|
---|
2414 | }
|
---|
2415 |
|
---|
2416 | pToken.queryInterfaceTo(aToken.asOutParam());
|
---|
2417 | break;
|
---|
2418 | }
|
---|
2419 | default:
|
---|
2420 | {
|
---|
2421 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2422 | rc = i_setStateError();
|
---|
2423 | break;
|
---|
2424 | }
|
---|
2425 | }
|
---|
2426 |
|
---|
2427 | return rc;
|
---|
2428 | }
|
---|
2429 |
|
---|
2430 | /**
|
---|
2431 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2432 | * in-process calls).
|
---|
2433 | */
|
---|
2434 | HRESULT Medium::i_unlockRead(MediumState_T *aState)
|
---|
2435 | {
|
---|
2436 | AutoCaller autoCaller(this);
|
---|
2437 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2438 |
|
---|
2439 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2440 |
|
---|
2441 | HRESULT rc = S_OK;
|
---|
2442 |
|
---|
2443 | switch (m->state)
|
---|
2444 | {
|
---|
2445 | case MediumState_LockedRead:
|
---|
2446 | {
|
---|
2447 | ComAssertMsgBreak(m->readers != 0, (tr("Counter underflow")), rc = E_FAIL);
|
---|
2448 | --m->readers;
|
---|
2449 |
|
---|
2450 | /* Reset the state after the last reader */
|
---|
2451 | if (m->readers == 0)
|
---|
2452 | {
|
---|
2453 | m->state = m->preLockState;
|
---|
2454 | /* There are cases where we inject the deleting state into
|
---|
2455 | * a medium locked for reading. Make sure #unmarkForDeletion()
|
---|
2456 | * gets the right state afterwards. */
|
---|
2457 | if (m->preLockState == MediumState_Deleting)
|
---|
2458 | m->preLockState = MediumState_Created;
|
---|
2459 | }
|
---|
2460 |
|
---|
2461 | LogFlowThisFunc(("new state=%d\n", m->state));
|
---|
2462 | break;
|
---|
2463 | }
|
---|
2464 | default:
|
---|
2465 | {
|
---|
2466 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2467 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2468 | tr("Medium '%s' is not locked for reading"),
|
---|
2469 | m->strLocationFull.c_str());
|
---|
2470 | break;
|
---|
2471 | }
|
---|
2472 | }
|
---|
2473 |
|
---|
2474 | /* return the current state after */
|
---|
2475 | if (aState)
|
---|
2476 | *aState = m->state;
|
---|
2477 |
|
---|
2478 | return rc;
|
---|
2479 | }
|
---|
2480 | HRESULT Medium::lockWrite(ComPtr<IToken> &aToken)
|
---|
2481 | {
|
---|
2482 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2483 |
|
---|
2484 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2485 | if (m->queryInfoRunning)
|
---|
2486 | {
|
---|
2487 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2488 | * lock and thus we would run into a deadlock here. */
|
---|
2489 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2490 | while (m->queryInfoRunning)
|
---|
2491 | {
|
---|
2492 | alock.release();
|
---|
2493 | /* must not hold the object lock now */
|
---|
2494 | Assert(!isWriteLockOnCurrentThread());
|
---|
2495 | {
|
---|
2496 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2497 | }
|
---|
2498 | alock.acquire();
|
---|
2499 | }
|
---|
2500 | }
|
---|
2501 |
|
---|
2502 | HRESULT rc = S_OK;
|
---|
2503 |
|
---|
2504 | switch (m->state)
|
---|
2505 | {
|
---|
2506 | case MediumState_Created:
|
---|
2507 | case MediumState_Inaccessible:
|
---|
2508 | {
|
---|
2509 | m->preLockState = m->state;
|
---|
2510 |
|
---|
2511 | LogFlowThisFunc(("Okay - prev state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2512 | m->state = MediumState_LockedWrite;
|
---|
2513 |
|
---|
2514 | ComObjPtr<MediumLockToken> pToken;
|
---|
2515 | rc = pToken.createObject();
|
---|
2516 | if (SUCCEEDED(rc))
|
---|
2517 | rc = pToken->init(this, true /* fWrite */);
|
---|
2518 | if (FAILED(rc))
|
---|
2519 | {
|
---|
2520 | m->state = m->preLockState;
|
---|
2521 | return rc;
|
---|
2522 | }
|
---|
2523 |
|
---|
2524 | pToken.queryInterfaceTo(aToken.asOutParam());
|
---|
2525 | break;
|
---|
2526 | }
|
---|
2527 | default:
|
---|
2528 | {
|
---|
2529 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2530 | rc = i_setStateError();
|
---|
2531 | break;
|
---|
2532 | }
|
---|
2533 | }
|
---|
2534 |
|
---|
2535 | return rc;
|
---|
2536 | }
|
---|
2537 |
|
---|
2538 | /**
|
---|
2539 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2540 | * in-process calls).
|
---|
2541 | */
|
---|
2542 | HRESULT Medium::i_unlockWrite(MediumState_T *aState)
|
---|
2543 | {
|
---|
2544 | AutoCaller autoCaller(this);
|
---|
2545 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2546 |
|
---|
2547 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2548 |
|
---|
2549 | HRESULT rc = S_OK;
|
---|
2550 |
|
---|
2551 | switch (m->state)
|
---|
2552 | {
|
---|
2553 | case MediumState_LockedWrite:
|
---|
2554 | {
|
---|
2555 | m->state = m->preLockState;
|
---|
2556 | /* There are cases where we inject the deleting state into
|
---|
2557 | * a medium locked for writing. Make sure #unmarkForDeletion()
|
---|
2558 | * gets the right state afterwards. */
|
---|
2559 | if (m->preLockState == MediumState_Deleting)
|
---|
2560 | m->preLockState = MediumState_Created;
|
---|
2561 | LogFlowThisFunc(("new state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2562 | break;
|
---|
2563 | }
|
---|
2564 | default:
|
---|
2565 | {
|
---|
2566 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2567 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2568 | tr("Medium '%s' is not locked for writing"),
|
---|
2569 | m->strLocationFull.c_str());
|
---|
2570 | break;
|
---|
2571 | }
|
---|
2572 | }
|
---|
2573 |
|
---|
2574 | /* return the current state after */
|
---|
2575 | if (aState)
|
---|
2576 | *aState = m->state;
|
---|
2577 |
|
---|
2578 | return rc;
|
---|
2579 | }
|
---|
2580 |
|
---|
2581 | HRESULT Medium::close(AutoCaller &aAutoCaller)
|
---|
2582 | {
|
---|
2583 | // make a copy of VirtualBox pointer which gets nulled by uninit()
|
---|
2584 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2585 |
|
---|
2586 | Guid uId = i_getId();
|
---|
2587 | DeviceType_T devType = i_getDeviceType();
|
---|
2588 | MultiResult mrc = i_close(aAutoCaller);
|
---|
2589 |
|
---|
2590 | pVirtualBox->i_saveModifiedRegistries();
|
---|
2591 |
|
---|
2592 | if (SUCCEEDED(mrc) && uId.isValid() && !uId.isZero())
|
---|
2593 | pVirtualBox->i_onMediumRegistered(uId, devType, FALSE);
|
---|
2594 |
|
---|
2595 | return mrc;
|
---|
2596 | }
|
---|
2597 |
|
---|
2598 | HRESULT Medium::getProperty(const com::Utf8Str &aName,
|
---|
2599 | com::Utf8Str &aValue)
|
---|
2600 | {
|
---|
2601 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2602 |
|
---|
2603 | settings::StringsMap::const_iterator it = m->mapProperties.find(aName);
|
---|
2604 | if (it == m->mapProperties.end())
|
---|
2605 | {
|
---|
2606 | if (!aName.startsWith("Special/"))
|
---|
2607 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2608 | tr("Property '%s' does not exist"), aName.c_str());
|
---|
2609 | else
|
---|
2610 | /* be more silent here */
|
---|
2611 | return VBOX_E_OBJECT_NOT_FOUND;
|
---|
2612 | }
|
---|
2613 |
|
---|
2614 | aValue = it->second;
|
---|
2615 |
|
---|
2616 | return S_OK;
|
---|
2617 | }
|
---|
2618 |
|
---|
2619 | HRESULT Medium::setProperty(const com::Utf8Str &aName,
|
---|
2620 | const com::Utf8Str &aValue)
|
---|
2621 | {
|
---|
2622 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2623 |
|
---|
2624 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2625 | if (m->queryInfoRunning)
|
---|
2626 | {
|
---|
2627 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2628 | * lock and thus we would run into a deadlock here. */
|
---|
2629 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2630 | while (m->queryInfoRunning)
|
---|
2631 | {
|
---|
2632 | mlock.release();
|
---|
2633 | /* must not hold the object lock now */
|
---|
2634 | Assert(!isWriteLockOnCurrentThread());
|
---|
2635 | {
|
---|
2636 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2637 | }
|
---|
2638 | mlock.acquire();
|
---|
2639 | }
|
---|
2640 | }
|
---|
2641 |
|
---|
2642 | switch (m->state)
|
---|
2643 | {
|
---|
2644 | case MediumState_NotCreated:
|
---|
2645 | case MediumState_Created:
|
---|
2646 | case MediumState_Inaccessible:
|
---|
2647 | break;
|
---|
2648 | default:
|
---|
2649 | return i_setStateError();
|
---|
2650 | }
|
---|
2651 |
|
---|
2652 | settings::StringsMap::iterator it = m->mapProperties.find(aName);
|
---|
2653 | if ( !aName.startsWith("Special/")
|
---|
2654 | && !i_isPropertyForFilter(aName))
|
---|
2655 | {
|
---|
2656 | if (it == m->mapProperties.end())
|
---|
2657 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2658 | tr("Property '%s' does not exist"),
|
---|
2659 | aName.c_str());
|
---|
2660 | it->second = aValue;
|
---|
2661 | }
|
---|
2662 | else
|
---|
2663 | {
|
---|
2664 | if (it == m->mapProperties.end())
|
---|
2665 | {
|
---|
2666 | if (!aValue.isEmpty())
|
---|
2667 | m->mapProperties[aName] = aValue;
|
---|
2668 | }
|
---|
2669 | else
|
---|
2670 | {
|
---|
2671 | if (!aValue.isEmpty())
|
---|
2672 | it->second = aValue;
|
---|
2673 | else
|
---|
2674 | m->mapProperties.erase(it);
|
---|
2675 | }
|
---|
2676 | }
|
---|
2677 |
|
---|
2678 | // save the settings
|
---|
2679 | mlock.release();
|
---|
2680 | i_markRegistriesModified();
|
---|
2681 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2682 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
2683 |
|
---|
2684 | return S_OK;
|
---|
2685 | }
|
---|
2686 |
|
---|
2687 | HRESULT Medium::getProperties(const com::Utf8Str &aNames,
|
---|
2688 | std::vector<com::Utf8Str> &aReturnNames,
|
---|
2689 | std::vector<com::Utf8Str> &aReturnValues)
|
---|
2690 | {
|
---|
2691 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2692 |
|
---|
2693 | /// @todo make use of aNames according to the documentation
|
---|
2694 | NOREF(aNames);
|
---|
2695 |
|
---|
2696 | aReturnNames.resize(m->mapProperties.size());
|
---|
2697 | aReturnValues.resize(m->mapProperties.size());
|
---|
2698 | size_t i = 0;
|
---|
2699 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
2700 | it != m->mapProperties.end();
|
---|
2701 | ++it, ++i)
|
---|
2702 | {
|
---|
2703 | aReturnNames[i] = it->first;
|
---|
2704 | aReturnValues[i] = it->second;
|
---|
2705 | }
|
---|
2706 | return S_OK;
|
---|
2707 | }
|
---|
2708 |
|
---|
2709 | HRESULT Medium::setProperties(const std::vector<com::Utf8Str> &aNames,
|
---|
2710 | const std::vector<com::Utf8Str> &aValues)
|
---|
2711 | {
|
---|
2712 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2713 |
|
---|
2714 | /* first pass: validate names */
|
---|
2715 | for (size_t i = 0;
|
---|
2716 | i < aNames.size();
|
---|
2717 | ++i)
|
---|
2718 | {
|
---|
2719 | Utf8Str strName(aNames[i]);
|
---|
2720 | if ( !strName.startsWith("Special/")
|
---|
2721 | && !i_isPropertyForFilter(strName)
|
---|
2722 | && m->mapProperties.find(strName) == m->mapProperties.end())
|
---|
2723 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2724 | tr("Property '%s' does not exist"), strName.c_str());
|
---|
2725 | }
|
---|
2726 |
|
---|
2727 | /* second pass: assign */
|
---|
2728 | for (size_t i = 0;
|
---|
2729 | i < aNames.size();
|
---|
2730 | ++i)
|
---|
2731 | {
|
---|
2732 | Utf8Str strName(aNames[i]);
|
---|
2733 | Utf8Str strValue(aValues[i]);
|
---|
2734 | settings::StringsMap::iterator it = m->mapProperties.find(strName);
|
---|
2735 | if ( !strName.startsWith("Special/")
|
---|
2736 | && !i_isPropertyForFilter(strName))
|
---|
2737 | {
|
---|
2738 | AssertReturn(it != m->mapProperties.end(), E_FAIL);
|
---|
2739 | it->second = strValue;
|
---|
2740 | }
|
---|
2741 | else
|
---|
2742 | {
|
---|
2743 | if (it == m->mapProperties.end())
|
---|
2744 | {
|
---|
2745 | if (!strValue.isEmpty())
|
---|
2746 | m->mapProperties[strName] = strValue;
|
---|
2747 | }
|
---|
2748 | else
|
---|
2749 | {
|
---|
2750 | if (!strValue.isEmpty())
|
---|
2751 | it->second = strValue;
|
---|
2752 | else
|
---|
2753 | m->mapProperties.erase(it);
|
---|
2754 | }
|
---|
2755 | }
|
---|
2756 | }
|
---|
2757 |
|
---|
2758 | // save the settings
|
---|
2759 | mlock.release();
|
---|
2760 | i_markRegistriesModified();
|
---|
2761 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2762 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
2763 |
|
---|
2764 | return S_OK;
|
---|
2765 | }
|
---|
2766 |
|
---|
2767 | HRESULT Medium::createBaseStorage(LONG64 aLogicalSize,
|
---|
2768 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2769 | ComPtr<IProgress> &aProgress)
|
---|
2770 | {
|
---|
2771 | if (aLogicalSize < 0)
|
---|
2772 | return setError(E_INVALIDARG, tr("The medium size argument (%lld) is negative"), aLogicalSize);
|
---|
2773 |
|
---|
2774 | HRESULT rc = S_OK;
|
---|
2775 | ComObjPtr<Progress> pProgress;
|
---|
2776 | Medium::Task *pTask = NULL;
|
---|
2777 |
|
---|
2778 | try
|
---|
2779 | {
|
---|
2780 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2781 |
|
---|
2782 | ULONG mediumVariantFlags = 0;
|
---|
2783 |
|
---|
2784 | if (aVariant.size())
|
---|
2785 | {
|
---|
2786 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2787 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2788 | }
|
---|
2789 |
|
---|
2790 | mediumVariantFlags &= ((unsigned)~MediumVariant_Diff);
|
---|
2791 |
|
---|
2792 | if ( !(mediumVariantFlags & MediumVariant_Fixed)
|
---|
2793 | && !(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_CreateDynamic))
|
---|
2794 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2795 | tr("Medium format '%s' does not support dynamic storage creation"),
|
---|
2796 | m->strFormat.c_str());
|
---|
2797 |
|
---|
2798 | if ( (mediumVariantFlags & MediumVariant_Fixed)
|
---|
2799 | && !(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_CreateFixed))
|
---|
2800 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2801 | tr("Medium format '%s' does not support fixed storage creation"),
|
---|
2802 | m->strFormat.c_str());
|
---|
2803 |
|
---|
2804 | if ( (mediumVariantFlags & MediumVariant_Formatted)
|
---|
2805 | && i_getDeviceType() != DeviceType_Floppy)
|
---|
2806 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2807 | tr("Medium variant 'formatted' applies to floppy images only"));
|
---|
2808 |
|
---|
2809 | if (m->state != MediumState_NotCreated)
|
---|
2810 | throw i_setStateError();
|
---|
2811 |
|
---|
2812 | pProgress.createObject();
|
---|
2813 | rc = pProgress->init(m->pVirtualBox,
|
---|
2814 | static_cast<IMedium*>(this),
|
---|
2815 | (mediumVariantFlags & MediumVariant_Fixed)
|
---|
2816 | ? BstrFmt(tr("Creating fixed medium storage unit '%s'"), m->strLocationFull.c_str()).raw()
|
---|
2817 | : BstrFmt(tr("Creating dynamic medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
2818 | TRUE /* aCancelable */);
|
---|
2819 | if (FAILED(rc))
|
---|
2820 | throw rc;
|
---|
2821 |
|
---|
2822 | /* setup task object to carry out the operation asynchronously */
|
---|
2823 | pTask = new Medium::CreateBaseTask(this, pProgress, (uint64_t)aLogicalSize,
|
---|
2824 | (MediumVariant_T)mediumVariantFlags);
|
---|
2825 | rc = pTask->rc();
|
---|
2826 | AssertComRC(rc);
|
---|
2827 | if (FAILED(rc))
|
---|
2828 | throw rc;
|
---|
2829 |
|
---|
2830 | m->state = MediumState_Creating;
|
---|
2831 | }
|
---|
2832 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2833 |
|
---|
2834 | if (SUCCEEDED(rc))
|
---|
2835 | {
|
---|
2836 | rc = pTask->createThread();
|
---|
2837 | pTask = NULL;
|
---|
2838 |
|
---|
2839 | if (SUCCEEDED(rc))
|
---|
2840 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2841 | }
|
---|
2842 | else if (pTask != NULL)
|
---|
2843 | delete pTask;
|
---|
2844 |
|
---|
2845 | return rc;
|
---|
2846 | }
|
---|
2847 |
|
---|
2848 | HRESULT Medium::deleteStorage(ComPtr<IProgress> &aProgress)
|
---|
2849 | {
|
---|
2850 | ComObjPtr<Progress> pProgress;
|
---|
2851 |
|
---|
2852 | MultiResult mrc = i_deleteStorage(&pProgress,
|
---|
2853 | false /* aWait */,
|
---|
2854 | true /* aNotify */);
|
---|
2855 | /* Must save the registries in any case, since an entry was removed. */
|
---|
2856 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2857 |
|
---|
2858 | if (SUCCEEDED(mrc))
|
---|
2859 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2860 |
|
---|
2861 | return mrc;
|
---|
2862 | }
|
---|
2863 |
|
---|
2864 | HRESULT Medium::createDiffStorage(AutoCaller &autoCaller,
|
---|
2865 | const ComPtr<IMedium> &aTarget,
|
---|
2866 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2867 | ComPtr<IProgress> &aProgress)
|
---|
2868 | {
|
---|
2869 | IMedium *aT = aTarget;
|
---|
2870 | ComObjPtr<Medium> diff = static_cast<Medium*>(aT);
|
---|
2871 |
|
---|
2872 | autoCaller.release();
|
---|
2873 |
|
---|
2874 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
2875 | * the pVirtualBox reference, see #uninit(). */
|
---|
2876 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2877 |
|
---|
2878 | // we access m->pParent
|
---|
2879 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
2880 |
|
---|
2881 | autoCaller.add();
|
---|
2882 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2883 |
|
---|
2884 | AutoMultiWriteLock2 alock(this, diff COMMA_LOCKVAL_SRC_POS);
|
---|
2885 |
|
---|
2886 | if (m->type == MediumType_Writethrough)
|
---|
2887 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2888 | tr("Medium type of '%s' is Writethrough"),
|
---|
2889 | m->strLocationFull.c_str());
|
---|
2890 | else if (m->type == MediumType_Shareable)
|
---|
2891 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2892 | tr("Medium type of '%s' is Shareable"),
|
---|
2893 | m->strLocationFull.c_str());
|
---|
2894 | else if (m->type == MediumType_Readonly)
|
---|
2895 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2896 | tr("Medium type of '%s' is Readonly"),
|
---|
2897 | m->strLocationFull.c_str());
|
---|
2898 |
|
---|
2899 | /* Apply the normal locking logic to the entire chain. */
|
---|
2900 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
2901 | alock.release();
|
---|
2902 | autoCaller.release();
|
---|
2903 | treeLock.release();
|
---|
2904 | HRESULT rc = diff->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2905 | diff /* pToLockWrite */,
|
---|
2906 | false /* fMediumLockWriteAll */,
|
---|
2907 | this,
|
---|
2908 | *pMediumLockList);
|
---|
2909 | treeLock.acquire();
|
---|
2910 | autoCaller.add();
|
---|
2911 | if (FAILED(autoCaller.rc()))
|
---|
2912 | rc = autoCaller.rc();
|
---|
2913 | alock.acquire();
|
---|
2914 | if (FAILED(rc))
|
---|
2915 | {
|
---|
2916 | delete pMediumLockList;
|
---|
2917 | return rc;
|
---|
2918 | }
|
---|
2919 |
|
---|
2920 | alock.release();
|
---|
2921 | autoCaller.release();
|
---|
2922 | treeLock.release();
|
---|
2923 | rc = pMediumLockList->Lock();
|
---|
2924 | treeLock.acquire();
|
---|
2925 | autoCaller.add();
|
---|
2926 | if (FAILED(autoCaller.rc()))
|
---|
2927 | rc = autoCaller.rc();
|
---|
2928 | alock.acquire();
|
---|
2929 | if (FAILED(rc))
|
---|
2930 | {
|
---|
2931 | delete pMediumLockList;
|
---|
2932 |
|
---|
2933 | return setError(rc, tr("Could not lock medium when creating diff '%s'"),
|
---|
2934 | diff->i_getLocationFull().c_str());
|
---|
2935 | }
|
---|
2936 |
|
---|
2937 | Guid parentMachineRegistry;
|
---|
2938 | if (i_getFirstRegistryMachineId(parentMachineRegistry))
|
---|
2939 | {
|
---|
2940 | /* since this medium has been just created it isn't associated yet */
|
---|
2941 | diff->m->llRegistryIDs.push_back(parentMachineRegistry);
|
---|
2942 | alock.release();
|
---|
2943 | autoCaller.release();
|
---|
2944 | treeLock.release();
|
---|
2945 | diff->i_markRegistriesModified();
|
---|
2946 | treeLock.acquire();
|
---|
2947 | autoCaller.add();
|
---|
2948 | alock.acquire();
|
---|
2949 | }
|
---|
2950 |
|
---|
2951 | alock.release();
|
---|
2952 | autoCaller.release();
|
---|
2953 | treeLock.release();
|
---|
2954 |
|
---|
2955 | ComObjPtr<Progress> pProgress;
|
---|
2956 |
|
---|
2957 | ULONG mediumVariantFlags = 0;
|
---|
2958 |
|
---|
2959 | if (aVariant.size())
|
---|
2960 | {
|
---|
2961 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2962 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2963 | }
|
---|
2964 |
|
---|
2965 | if (mediumVariantFlags & MediumVariant_Formatted)
|
---|
2966 | {
|
---|
2967 | delete pMediumLockList;
|
---|
2968 | return setError(VBOX_E_NOT_SUPPORTED,
|
---|
2969 | tr("Medium variant 'formatted' applies to floppy images only"));
|
---|
2970 | }
|
---|
2971 |
|
---|
2972 | rc = i_createDiffStorage(diff, (MediumVariant_T)mediumVariantFlags, pMediumLockList,
|
---|
2973 | &pProgress, false /* aWait */, true /* aNotify */);
|
---|
2974 | if (FAILED(rc))
|
---|
2975 | delete pMediumLockList;
|
---|
2976 | else
|
---|
2977 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2978 |
|
---|
2979 | return rc;
|
---|
2980 | }
|
---|
2981 |
|
---|
2982 | HRESULT Medium::mergeTo(const ComPtr<IMedium> &aTarget,
|
---|
2983 | ComPtr<IProgress> &aProgress)
|
---|
2984 | {
|
---|
2985 | IMedium *aT = aTarget;
|
---|
2986 |
|
---|
2987 | ComAssertRet(aT != this, E_INVALIDARG);
|
---|
2988 |
|
---|
2989 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aT);
|
---|
2990 |
|
---|
2991 | bool fMergeForward = false;
|
---|
2992 | ComObjPtr<Medium> pParentForTarget;
|
---|
2993 | MediumLockList *pChildrenToReparent = NULL;
|
---|
2994 | MediumLockList *pMediumLockList = NULL;
|
---|
2995 |
|
---|
2996 | HRESULT rc = S_OK;
|
---|
2997 |
|
---|
2998 | rc = i_prepareMergeTo(pTarget, NULL, NULL, true, fMergeForward,
|
---|
2999 | pParentForTarget, pChildrenToReparent, pMediumLockList);
|
---|
3000 | if (FAILED(rc)) return rc;
|
---|
3001 |
|
---|
3002 | ComObjPtr<Progress> pProgress;
|
---|
3003 |
|
---|
3004 | rc = i_mergeTo(pTarget, fMergeForward, pParentForTarget, pChildrenToReparent,
|
---|
3005 | pMediumLockList, &pProgress, false /* aWait */, true /* aNotify */);
|
---|
3006 | if (FAILED(rc))
|
---|
3007 | i_cancelMergeTo(pChildrenToReparent, pMediumLockList);
|
---|
3008 | else
|
---|
3009 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3010 |
|
---|
3011 | return rc;
|
---|
3012 | }
|
---|
3013 |
|
---|
3014 | HRESULT Medium::cloneToBase(const ComPtr<IMedium> &aTarget,
|
---|
3015 | const std::vector<MediumVariant_T> &aVariant,
|
---|
3016 | ComPtr<IProgress> &aProgress)
|
---|
3017 | {
|
---|
3018 | return cloneTo(aTarget, aVariant, NULL, aProgress);
|
---|
3019 | }
|
---|
3020 |
|
---|
3021 | HRESULT Medium::cloneTo(const ComPtr<IMedium> &aTarget,
|
---|
3022 | const std::vector<MediumVariant_T> &aVariant,
|
---|
3023 | const ComPtr<IMedium> &aParent,
|
---|
3024 | ComPtr<IProgress> &aProgress)
|
---|
3025 | {
|
---|
3026 | return resizeAndCloneTo(aTarget, 0, aVariant, aParent, aProgress);
|
---|
3027 | }
|
---|
3028 |
|
---|
3029 | /**
|
---|
3030 | * This is a helper function that combines the functionality of
|
---|
3031 | * Medium::cloneTo() and Medium::resize(). The target medium will take the
|
---|
3032 | * contents of the calling medium.
|
---|
3033 | *
|
---|
3034 | * @param aTarget Medium to resize and clone to
|
---|
3035 | * @param aLogicalSize Desired size for targer medium
|
---|
3036 | * @param aVariant
|
---|
3037 | * @param aParent
|
---|
3038 | * @param aProgress
|
---|
3039 | * @return HRESULT
|
---|
3040 | */
|
---|
3041 | HRESULT Medium::resizeAndCloneTo(const ComPtr<IMedium> &aTarget,
|
---|
3042 | const LONG64 aLogicalSize,
|
---|
3043 | const std::vector<MediumVariant_T> &aVariant,
|
---|
3044 | const ComPtr<IMedium> &aParent,
|
---|
3045 | ComPtr<IProgress> &aProgress)
|
---|
3046 | {
|
---|
3047 | /* Check for valid args */
|
---|
3048 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
3049 | CheckComArgExpr(aLogicalSize, aLogicalSize >= 0);
|
---|
3050 |
|
---|
3051 | /* Convert args to usable/needed types */
|
---|
3052 | IMedium *aT = aTarget;
|
---|
3053 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aT);
|
---|
3054 | ComObjPtr<Medium> pParent;
|
---|
3055 | if (aParent)
|
---|
3056 | {
|
---|
3057 | IMedium *aP = aParent;
|
---|
3058 | pParent = static_cast<Medium*>(aP);
|
---|
3059 | }
|
---|
3060 |
|
---|
3061 | /* Set up variables. Fetch needed data in lockable blocks */
|
---|
3062 | HRESULT rc = S_OK;
|
---|
3063 | ComObjPtr<Progress> pTmpProgress;
|
---|
3064 | Medium::Task *pTask = NULL;
|
---|
3065 |
|
---|
3066 | Utf8Str strSourceName;
|
---|
3067 | {
|
---|
3068 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3069 | strSourceName = i_getName();
|
---|
3070 | }
|
---|
3071 |
|
---|
3072 | uint64_t uTargetExistingSize = 0;
|
---|
3073 | Utf8Str strTargetName;
|
---|
3074 | {
|
---|
3075 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
3076 | uTargetExistingSize = pTarget->i_getLogicalSize();
|
---|
3077 | strTargetName = pTarget->i_getName();
|
---|
3078 | }
|
---|
3079 |
|
---|
3080 | /* Set up internal multi-subprocess progress object */
|
---|
3081 | ComObjPtr<Progress> pProgress;
|
---|
3082 | pProgress.createObject();
|
---|
3083 | rc = pProgress->init(m->pVirtualBox,
|
---|
3084 | static_cast<IMedium*>(this),
|
---|
3085 | BstrFmt(tr("Resizing medium and cloning into it")).raw(),
|
---|
3086 | TRUE, /* aCancelable */
|
---|
3087 | 2, /* Number of opearations */
|
---|
3088 | BstrFmt(tr("Resizing medium before clone")).raw()
|
---|
3089 | );
|
---|
3090 |
|
---|
3091 | if (FAILED(rc))
|
---|
3092 | throw rc;
|
---|
3093 |
|
---|
3094 | /* If target does not exist, handle resize. */
|
---|
3095 | if (pTarget->m->state != MediumState_NotCreated && aLogicalSize > 0)
|
---|
3096 | {
|
---|
3097 | if ((LONG64)uTargetExistingSize != aLogicalSize) {
|
---|
3098 | if (!i_isMediumFormatFile())
|
---|
3099 | {
|
---|
3100 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3101 | rc = setError(VBOX_E_NOT_SUPPORTED,
|
---|
3102 | tr("Sizes of '%s' and '%s' are different and \
|
---|
3103 | medium format does not support resing"),
|
---|
3104 | strSourceName.c_str(), strTargetName.c_str());
|
---|
3105 | throw rc;
|
---|
3106 | }
|
---|
3107 |
|
---|
3108 | /**
|
---|
3109 | * Need to lock the target medium as i_resize does do so
|
---|
3110 | * automatically.
|
---|
3111 | */
|
---|
3112 |
|
---|
3113 | ComPtr<IToken> pToken;
|
---|
3114 | rc = pTarget->LockWrite(pToken.asOutParam());
|
---|
3115 |
|
---|
3116 | if (FAILED(rc)) throw rc;
|
---|
3117 |
|
---|
3118 | /**
|
---|
3119 | * Have to make own lock list, because "resize" method resizes only
|
---|
3120 | * last image in the lock chain.
|
---|
3121 | */
|
---|
3122 |
|
---|
3123 | MediumLockList* pMediumLockListForResize = new MediumLockList();
|
---|
3124 | pMediumLockListForResize->Append(pTarget, pTarget->m->state == MediumState_LockedWrite);
|
---|
3125 |
|
---|
3126 | rc = pMediumLockListForResize->Lock(true /* fSkipOverLockedMedia */);
|
---|
3127 |
|
---|
3128 | if (FAILED(rc))
|
---|
3129 | {
|
---|
3130 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3131 | rc = setError(rc,
|
---|
3132 | tr("Failed to lock the medium '%s' to resize before merge"),
|
---|
3133 | strTargetName.c_str());
|
---|
3134 | delete pMediumLockListForResize;
|
---|
3135 | throw rc;
|
---|
3136 | }
|
---|
3137 |
|
---|
3138 |
|
---|
3139 | rc = pTarget->i_resize((uint64_t)aLogicalSize, pMediumLockListForResize, &pProgress, true, false);
|
---|
3140 |
|
---|
3141 | if (FAILED(rc))
|
---|
3142 | {
|
---|
3143 | /* No need to setError becasue i_resize and i_taskResizeHandler handle this automatically. */
|
---|
3144 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3145 | delete pMediumLockListForResize;
|
---|
3146 | throw rc;
|
---|
3147 | }
|
---|
3148 |
|
---|
3149 | delete pMediumLockListForResize;
|
---|
3150 |
|
---|
3151 | pTarget->m->logicalSize = aLogicalSize;
|
---|
3152 |
|
---|
3153 | pToken->Abandon();
|
---|
3154 | pToken.setNull();
|
---|
3155 | }
|
---|
3156 | }
|
---|
3157 |
|
---|
3158 | /* Report progress to supplied progress argument */
|
---|
3159 | if (SUCCEEDED(rc))
|
---|
3160 | {
|
---|
3161 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3162 | }
|
---|
3163 |
|
---|
3164 | try
|
---|
3165 | {
|
---|
3166 | // locking: we need the tree lock first because we access parent pointers
|
---|
3167 | // and we need to write-lock the media involved
|
---|
3168 | uint32_t cHandles = 3;
|
---|
3169 | LockHandle* pHandles[4] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
3170 | this->lockHandle(),
|
---|
3171 | pTarget->lockHandle() };
|
---|
3172 | /* Only add parent to the lock if it is not null */
|
---|
3173 | if (!pParent.isNull())
|
---|
3174 | pHandles[cHandles++] = pParent->lockHandle();
|
---|
3175 | AutoWriteLock alock(cHandles,
|
---|
3176 | pHandles
|
---|
3177 | COMMA_LOCKVAL_SRC_POS);
|
---|
3178 |
|
---|
3179 | if ( pTarget->m->state != MediumState_NotCreated
|
---|
3180 | && pTarget->m->state != MediumState_Created)
|
---|
3181 | throw pTarget->i_setStateError();
|
---|
3182 |
|
---|
3183 | /* Build the source lock list. */
|
---|
3184 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
3185 | alock.release();
|
---|
3186 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3187 | NULL /* pToLockWrite */,
|
---|
3188 | false /* fMediumLockWriteAll */,
|
---|
3189 | NULL,
|
---|
3190 | *pSourceMediumLockList);
|
---|
3191 | alock.acquire();
|
---|
3192 | if (FAILED(rc))
|
---|
3193 | {
|
---|
3194 | delete pSourceMediumLockList;
|
---|
3195 | throw rc;
|
---|
3196 | }
|
---|
3197 |
|
---|
3198 | /* Build the target lock list (including the to-be parent chain). */
|
---|
3199 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
3200 | alock.release();
|
---|
3201 | rc = pTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3202 | pTarget /* pToLockWrite */,
|
---|
3203 | false /* fMediumLockWriteAll */,
|
---|
3204 | pParent,
|
---|
3205 | *pTargetMediumLockList);
|
---|
3206 | alock.acquire();
|
---|
3207 | if (FAILED(rc))
|
---|
3208 | {
|
---|
3209 | delete pSourceMediumLockList;
|
---|
3210 | delete pTargetMediumLockList;
|
---|
3211 | throw rc;
|
---|
3212 | }
|
---|
3213 |
|
---|
3214 | alock.release();
|
---|
3215 | rc = pSourceMediumLockList->Lock();
|
---|
3216 | alock.acquire();
|
---|
3217 | if (FAILED(rc))
|
---|
3218 | {
|
---|
3219 | delete pSourceMediumLockList;
|
---|
3220 | delete pTargetMediumLockList;
|
---|
3221 | throw setError(rc,
|
---|
3222 | tr("Failed to lock source media '%s'"),
|
---|
3223 | i_getLocationFull().c_str());
|
---|
3224 | }
|
---|
3225 | alock.release();
|
---|
3226 | rc = pTargetMediumLockList->Lock();
|
---|
3227 | alock.acquire();
|
---|
3228 | if (FAILED(rc))
|
---|
3229 | {
|
---|
3230 | delete pSourceMediumLockList;
|
---|
3231 | delete pTargetMediumLockList;
|
---|
3232 | throw setError(rc,
|
---|
3233 | tr("Failed to lock target media '%s'"),
|
---|
3234 | pTarget->i_getLocationFull().c_str());
|
---|
3235 | }
|
---|
3236 |
|
---|
3237 | ULONG mediumVariantFlags = 0;
|
---|
3238 |
|
---|
3239 | if (aVariant.size())
|
---|
3240 | {
|
---|
3241 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
3242 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
3243 | }
|
---|
3244 |
|
---|
3245 | if (mediumVariantFlags & MediumVariant_Formatted)
|
---|
3246 | {
|
---|
3247 | delete pSourceMediumLockList;
|
---|
3248 | delete pTargetMediumLockList;
|
---|
3249 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3250 | tr("Medium variant 'formatted' applies to floppy images only"));
|
---|
3251 | }
|
---|
3252 |
|
---|
3253 | if (pTarget->m->state != MediumState_NotCreated || aLogicalSize == 0)
|
---|
3254 | {
|
---|
3255 | /* setup task object to carry out the operation asynchronously */
|
---|
3256 | pTask = new Medium::CloneTask(this, pProgress, pTarget,
|
---|
3257 | (MediumVariant_T)mediumVariantFlags,
|
---|
3258 | pParent, UINT32_MAX, UINT32_MAX,
|
---|
3259 | pSourceMediumLockList, pTargetMediumLockList,
|
---|
3260 | false, false, true, 0);
|
---|
3261 | }
|
---|
3262 | else
|
---|
3263 | {
|
---|
3264 | /* setup task object to carry out the operation asynchronously */
|
---|
3265 | pTask = new Medium::CloneTask(this, pProgress, pTarget,
|
---|
3266 | (MediumVariant_T)mediumVariantFlags,
|
---|
3267 | pParent, UINT32_MAX, UINT32_MAX,
|
---|
3268 | pSourceMediumLockList, pTargetMediumLockList,
|
---|
3269 | false, false, true, aLogicalSize);
|
---|
3270 | }
|
---|
3271 |
|
---|
3272 | rc = pTask->rc();
|
---|
3273 | AssertComRC(rc);
|
---|
3274 | if (FAILED(rc))
|
---|
3275 | throw rc;
|
---|
3276 |
|
---|
3277 | if (pTarget->m->state == MediumState_NotCreated)
|
---|
3278 | pTarget->m->state = MediumState_Creating;
|
---|
3279 | }
|
---|
3280 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3281 |
|
---|
3282 | if (SUCCEEDED(rc))
|
---|
3283 | {
|
---|
3284 | rc = pTask->createThread();
|
---|
3285 | pTask = NULL;
|
---|
3286 | if (SUCCEEDED(rc))
|
---|
3287 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3288 | }
|
---|
3289 | else if (pTask != NULL) {
|
---|
3290 | delete pTask;
|
---|
3291 | throw rc;
|
---|
3292 | }
|
---|
3293 |
|
---|
3294 | return rc;
|
---|
3295 | }
|
---|
3296 |
|
---|
3297 | HRESULT Medium::moveTo(AutoCaller &autoCaller, const com::Utf8Str &aLocation, ComPtr<IProgress> &aProgress)
|
---|
3298 | {
|
---|
3299 | ComObjPtr<Medium> pParent;
|
---|
3300 | ComObjPtr<Progress> pProgress;
|
---|
3301 | HRESULT rc = S_OK;
|
---|
3302 | Medium::Task *pTask = NULL;
|
---|
3303 |
|
---|
3304 | try
|
---|
3305 | {
|
---|
3306 | /// @todo NEWMEDIA for file names, add the default extension if no extension
|
---|
3307 | /// is present (using the information from the VD backend which also implies
|
---|
3308 | /// that one more parameter should be passed to moveTo() requesting
|
---|
3309 | /// that functionality since it is only allowed when called from this method
|
---|
3310 |
|
---|
3311 | /// @todo NEWMEDIA rename the file and set m->location on success, then save
|
---|
3312 | /// the global registry (and local registries of portable VMs referring to
|
---|
3313 | /// this medium), this will also require to add the mRegistered flag to data
|
---|
3314 |
|
---|
3315 | autoCaller.release();
|
---|
3316 |
|
---|
3317 | // locking: we need the tree lock first because we access parent pointers
|
---|
3318 | // and we need to write-lock the media involved
|
---|
3319 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3320 |
|
---|
3321 | autoCaller.add();
|
---|
3322 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3323 |
|
---|
3324 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3325 |
|
---|
3326 | /* play with locations */
|
---|
3327 | {
|
---|
3328 | /* get source path and filename */
|
---|
3329 | Utf8Str sourcePath = i_getLocationFull();
|
---|
3330 | Utf8Str sourceFName = i_getName();
|
---|
3331 |
|
---|
3332 | if (aLocation.isEmpty())
|
---|
3333 | {
|
---|
3334 | rc = setErrorVrc(VERR_PATH_ZERO_LENGTH,
|
---|
3335 | tr("Medium '%s' can't be moved. Destination path is empty."),
|
---|
3336 | i_getLocationFull().c_str());
|
---|
3337 | throw rc;
|
---|
3338 | }
|
---|
3339 |
|
---|
3340 | /* extract destination path and filename */
|
---|
3341 | Utf8Str destPath(aLocation);
|
---|
3342 | Utf8Str destFName(destPath);
|
---|
3343 | destFName.stripPath();
|
---|
3344 |
|
---|
3345 | if (destFName.isNotEmpty() && !RTPathHasSuffix(destFName.c_str()))
|
---|
3346 | {
|
---|
3347 | /*
|
---|
3348 | * The target path has no filename: Either "/path/to/new/location" or
|
---|
3349 | * just "newname" (no trailing backslash or there is no filename extension).
|
---|
3350 | */
|
---|
3351 | if (destPath.equals(destFName))
|
---|
3352 | {
|
---|
3353 | /* new path contains only "newname", no path, no extension */
|
---|
3354 | destFName.append(RTPathSuffix(sourceFName.c_str()));
|
---|
3355 | destPath = destFName;
|
---|
3356 | }
|
---|
3357 | else
|
---|
3358 | {
|
---|
3359 | /* new path looks like "/path/to/new/location" */
|
---|
3360 | destFName.setNull();
|
---|
3361 | destPath.append(RTPATH_SLASH);
|
---|
3362 | }
|
---|
3363 | }
|
---|
3364 |
|
---|
3365 | if (destFName.isEmpty())
|
---|
3366 | {
|
---|
3367 | /* No target name */
|
---|
3368 | destPath.append(sourceFName);
|
---|
3369 | }
|
---|
3370 | else
|
---|
3371 | {
|
---|
3372 | if (destPath.equals(destFName))
|
---|
3373 | {
|
---|
3374 | /*
|
---|
3375 | * The target path contains of only a filename without a directory.
|
---|
3376 | * Move the medium within the source directory to the new name
|
---|
3377 | * (actually rename operation).
|
---|
3378 | * Scratches sourcePath!
|
---|
3379 | */
|
---|
3380 | destPath = sourcePath.stripFilename().append(RTPATH_SLASH).append(destFName);
|
---|
3381 | }
|
---|
3382 |
|
---|
3383 | const char *pszSuffix = RTPathSuffix(sourceFName.c_str());
|
---|
3384 |
|
---|
3385 | /* Suffix is empty and one is deduced from the medium format */
|
---|
3386 | if (pszSuffix == NULL)
|
---|
3387 | {
|
---|
3388 | Utf8Str strExt = i_getFormat();
|
---|
3389 | if (strExt.compare("RAW", Utf8Str::CaseInsensitive) == 0)
|
---|
3390 | {
|
---|
3391 | DeviceType_T devType = i_getDeviceType();
|
---|
3392 | switch (devType)
|
---|
3393 | {
|
---|
3394 | case DeviceType_DVD:
|
---|
3395 | strExt = "iso";
|
---|
3396 | break;
|
---|
3397 | case DeviceType_Floppy:
|
---|
3398 | strExt = "img";
|
---|
3399 | break;
|
---|
3400 | default:
|
---|
3401 | rc = setErrorVrc(VERR_NOT_A_FILE, /** @todo r=bird: Mixing status codes again. */
|
---|
3402 | tr("Medium '%s' has RAW type. \"Move\" operation isn't supported for this type."),
|
---|
3403 | i_getLocationFull().c_str());
|
---|
3404 | throw rc;
|
---|
3405 | }
|
---|
3406 | }
|
---|
3407 | else if (strExt.compare("Parallels", Utf8Str::CaseInsensitive) == 0)
|
---|
3408 | {
|
---|
3409 | strExt = "hdd";
|
---|
3410 | }
|
---|
3411 |
|
---|
3412 | /* Set the target extension like on the source. Any conversions are prohibited */
|
---|
3413 | strExt.toLower();
|
---|
3414 | destPath.stripSuffix().append('.').append(strExt);
|
---|
3415 | }
|
---|
3416 | else
|
---|
3417 | destPath.stripSuffix().append(pszSuffix);
|
---|
3418 | }
|
---|
3419 |
|
---|
3420 | /* Simple check for existence */
|
---|
3421 | if (RTFileExists(destPath.c_str()))
|
---|
3422 | {
|
---|
3423 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3424 | tr("The given path '%s' is an existing file. Delete or rename this file."),
|
---|
3425 | destPath.c_str());
|
---|
3426 | throw rc;
|
---|
3427 | }
|
---|
3428 |
|
---|
3429 | if (!i_isMediumFormatFile())
|
---|
3430 | {
|
---|
3431 | rc = setErrorVrc(VERR_NOT_A_FILE,
|
---|
3432 | tr("Medium '%s' isn't a file object. \"Move\" operation isn't supported."),
|
---|
3433 | i_getLocationFull().c_str());
|
---|
3434 | throw rc;
|
---|
3435 | }
|
---|
3436 | /* Path must be absolute */
|
---|
3437 | if (!RTPathStartsWithRoot(destPath.c_str()))
|
---|
3438 | {
|
---|
3439 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3440 | tr("The given path '%s' is not fully qualified"),
|
---|
3441 | destPath.c_str());
|
---|
3442 | throw rc;
|
---|
3443 | }
|
---|
3444 | /* Check path for a new file object */
|
---|
3445 | rc = VirtualBox::i_ensureFilePathExists(destPath, true);
|
---|
3446 | if (FAILED(rc))
|
---|
3447 | throw rc;
|
---|
3448 |
|
---|
3449 | /* Set needed variables for "moving" procedure. It'll be used later in separate thread task */
|
---|
3450 | rc = i_preparationForMoving(destPath);
|
---|
3451 | if (FAILED(rc))
|
---|
3452 | {
|
---|
3453 | rc = setErrorVrc(VERR_NO_CHANGE,
|
---|
3454 | tr("Medium '%s' is already in the correct location"),
|
---|
3455 | i_getLocationFull().c_str());
|
---|
3456 | throw rc;
|
---|
3457 | }
|
---|
3458 | }
|
---|
3459 |
|
---|
3460 | /* Check VMs which have this medium attached to*/
|
---|
3461 | std::vector<com::Guid> aMachineIds;
|
---|
3462 | rc = getMachineIds(aMachineIds);
|
---|
3463 | std::vector<com::Guid>::const_iterator currMachineID = aMachineIds.begin();
|
---|
3464 | std::vector<com::Guid>::const_iterator lastMachineID = aMachineIds.end();
|
---|
3465 |
|
---|
3466 | while (currMachineID != lastMachineID)
|
---|
3467 | {
|
---|
3468 | Guid id(*currMachineID);
|
---|
3469 | ComObjPtr<Machine> aMachine;
|
---|
3470 |
|
---|
3471 | alock.release();
|
---|
3472 | autoCaller.release();
|
---|
3473 | treeLock.release();
|
---|
3474 | rc = m->pVirtualBox->i_findMachine(id, false, true, &aMachine);
|
---|
3475 | treeLock.acquire();
|
---|
3476 | autoCaller.add();
|
---|
3477 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3478 | alock.acquire();
|
---|
3479 |
|
---|
3480 | if (SUCCEEDED(rc))
|
---|
3481 | {
|
---|
3482 | ComObjPtr<SessionMachine> sm;
|
---|
3483 | ComPtr<IInternalSessionControl> ctl;
|
---|
3484 |
|
---|
3485 | alock.release();
|
---|
3486 | autoCaller.release();
|
---|
3487 | treeLock.release();
|
---|
3488 | bool ses = aMachine->i_isSessionOpenVM(sm, &ctl);
|
---|
3489 | treeLock.acquire();
|
---|
3490 | autoCaller.add();
|
---|
3491 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3492 | alock.acquire();
|
---|
3493 |
|
---|
3494 | if (ses)
|
---|
3495 | {
|
---|
3496 | rc = setError(VBOX_E_INVALID_VM_STATE,
|
---|
3497 | tr("At least the VM '%s' to whom this medium '%s' attached has currently an opened session. Stop all VMs before relocating this medium"),
|
---|
3498 | id.toString().c_str(),
|
---|
3499 | i_getLocationFull().c_str());
|
---|
3500 | throw rc;
|
---|
3501 | }
|
---|
3502 | }
|
---|
3503 | ++currMachineID;
|
---|
3504 | }
|
---|
3505 |
|
---|
3506 | /* Build the source lock list. */
|
---|
3507 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3508 | alock.release();
|
---|
3509 | autoCaller.release();
|
---|
3510 | treeLock.release();
|
---|
3511 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3512 | this /* pToLockWrite */,
|
---|
3513 | true /* fMediumLockWriteAll */,
|
---|
3514 | NULL,
|
---|
3515 | *pMediumLockList);
|
---|
3516 | treeLock.acquire();
|
---|
3517 | autoCaller.add();
|
---|
3518 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3519 | alock.acquire();
|
---|
3520 | if (FAILED(rc))
|
---|
3521 | {
|
---|
3522 | delete pMediumLockList;
|
---|
3523 | throw setError(rc,
|
---|
3524 | tr("Failed to create medium lock list for '%s'"),
|
---|
3525 | i_getLocationFull().c_str());
|
---|
3526 | }
|
---|
3527 | alock.release();
|
---|
3528 | autoCaller.release();
|
---|
3529 | treeLock.release();
|
---|
3530 | rc = pMediumLockList->Lock();
|
---|
3531 | treeLock.acquire();
|
---|
3532 | autoCaller.add();
|
---|
3533 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3534 | alock.acquire();
|
---|
3535 | if (FAILED(rc))
|
---|
3536 | {
|
---|
3537 | delete pMediumLockList;
|
---|
3538 | throw setError(rc,
|
---|
3539 | tr("Failed to lock media '%s'"),
|
---|
3540 | i_getLocationFull().c_str());
|
---|
3541 | }
|
---|
3542 |
|
---|
3543 | pProgress.createObject();
|
---|
3544 | rc = pProgress->init(m->pVirtualBox,
|
---|
3545 | static_cast <IMedium *>(this),
|
---|
3546 | BstrFmt(tr("Moving medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3547 | TRUE /* aCancelable */);
|
---|
3548 |
|
---|
3549 | /* Do the disk moving. */
|
---|
3550 | if (SUCCEEDED(rc))
|
---|
3551 | {
|
---|
3552 | ULONG mediumVariantFlags = i_getVariant();
|
---|
3553 |
|
---|
3554 | /* setup task object to carry out the operation asynchronously */
|
---|
3555 | pTask = new Medium::MoveTask(this, pProgress,
|
---|
3556 | (MediumVariant_T)mediumVariantFlags,
|
---|
3557 | pMediumLockList);
|
---|
3558 | rc = pTask->rc();
|
---|
3559 | AssertComRC(rc);
|
---|
3560 | if (FAILED(rc))
|
---|
3561 | throw rc;
|
---|
3562 | }
|
---|
3563 |
|
---|
3564 | }
|
---|
3565 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3566 |
|
---|
3567 | if (SUCCEEDED(rc))
|
---|
3568 | {
|
---|
3569 | rc = pTask->createThread();
|
---|
3570 | pTask = NULL;
|
---|
3571 | if (SUCCEEDED(rc))
|
---|
3572 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3573 | }
|
---|
3574 | else
|
---|
3575 | {
|
---|
3576 | if (pTask)
|
---|
3577 | delete pTask;
|
---|
3578 | }
|
---|
3579 |
|
---|
3580 | return rc;
|
---|
3581 | }
|
---|
3582 |
|
---|
3583 | HRESULT Medium::setLocation(const com::Utf8Str &aLocation)
|
---|
3584 | {
|
---|
3585 | HRESULT rc = S_OK;
|
---|
3586 |
|
---|
3587 | try
|
---|
3588 | {
|
---|
3589 | // locking: we need the tree lock first because we access parent pointers
|
---|
3590 | // and we need to write-lock the media involved
|
---|
3591 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
3592 |
|
---|
3593 | AutoCaller autoCaller(this);
|
---|
3594 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3595 |
|
---|
3596 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3597 |
|
---|
3598 | Utf8Str destPath(aLocation);
|
---|
3599 |
|
---|
3600 | // some check for file based medium
|
---|
3601 | if (i_isMediumFormatFile())
|
---|
3602 | {
|
---|
3603 | /* Path must be absolute */
|
---|
3604 | if (!RTPathStartsWithRoot(destPath.c_str()))
|
---|
3605 | {
|
---|
3606 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3607 | tr("The given path '%s' is not fully qualified"),
|
---|
3608 | destPath.c_str());
|
---|
3609 | throw rc;
|
---|
3610 | }
|
---|
3611 |
|
---|
3612 | /* Simple check for existence */
|
---|
3613 | if (!RTFileExists(destPath.c_str()))
|
---|
3614 | {
|
---|
3615 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3616 | tr("The given path '%s' is not an existing file. New location is invalid."),
|
---|
3617 | destPath.c_str());
|
---|
3618 | throw rc;
|
---|
3619 | }
|
---|
3620 | }
|
---|
3621 |
|
---|
3622 | /* Check VMs which have this medium attached to*/
|
---|
3623 | std::vector<com::Guid> aMachineIds;
|
---|
3624 | rc = getMachineIds(aMachineIds);
|
---|
3625 |
|
---|
3626 | // switch locks only if there are machines with this medium attached
|
---|
3627 | if (!aMachineIds.empty())
|
---|
3628 | {
|
---|
3629 | std::vector<com::Guid>::const_iterator currMachineID = aMachineIds.begin();
|
---|
3630 | std::vector<com::Guid>::const_iterator lastMachineID = aMachineIds.end();
|
---|
3631 |
|
---|
3632 | alock.release();
|
---|
3633 | autoCaller.release();
|
---|
3634 | treeLock.release();
|
---|
3635 |
|
---|
3636 | while (currMachineID != lastMachineID)
|
---|
3637 | {
|
---|
3638 | Guid id(*currMachineID);
|
---|
3639 | ComObjPtr<Machine> aMachine;
|
---|
3640 | rc = m->pVirtualBox->i_findMachine(id, false, true, &aMachine);
|
---|
3641 | if (SUCCEEDED(rc))
|
---|
3642 | {
|
---|
3643 | ComObjPtr<SessionMachine> sm;
|
---|
3644 | ComPtr<IInternalSessionControl> ctl;
|
---|
3645 |
|
---|
3646 | bool ses = aMachine->i_isSessionOpenVM(sm, &ctl);
|
---|
3647 | if (ses)
|
---|
3648 | {
|
---|
3649 | treeLock.acquire();
|
---|
3650 | autoCaller.add();
|
---|
3651 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3652 | alock.acquire();
|
---|
3653 |
|
---|
3654 | rc = setError(VBOX_E_INVALID_VM_STATE,
|
---|
3655 | tr("At least the VM '%s' to whom this medium '%s' attached has currently an opened session. Stop all VMs before set location for this medium"),
|
---|
3656 | id.toString().c_str(),
|
---|
3657 | i_getLocationFull().c_str());
|
---|
3658 | throw rc;
|
---|
3659 | }
|
---|
3660 | }
|
---|
3661 | ++currMachineID;
|
---|
3662 | }
|
---|
3663 |
|
---|
3664 | treeLock.acquire();
|
---|
3665 | autoCaller.add();
|
---|
3666 | AssertComRCThrowRC(autoCaller.rc());
|
---|
3667 | alock.acquire();
|
---|
3668 | }
|
---|
3669 |
|
---|
3670 | m->strLocationFull = destPath;
|
---|
3671 |
|
---|
3672 | // save the settings
|
---|
3673 | alock.release();
|
---|
3674 | autoCaller.release();
|
---|
3675 | treeLock.release();
|
---|
3676 |
|
---|
3677 | i_markRegistriesModified();
|
---|
3678 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
3679 |
|
---|
3680 | MediumState_T mediumState;
|
---|
3681 | refreshState(autoCaller, &mediumState);
|
---|
3682 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
3683 | }
|
---|
3684 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3685 |
|
---|
3686 | return rc;
|
---|
3687 | }
|
---|
3688 |
|
---|
3689 | HRESULT Medium::compact(ComPtr<IProgress> &aProgress)
|
---|
3690 | {
|
---|
3691 | HRESULT rc = S_OK;
|
---|
3692 | ComObjPtr<Progress> pProgress;
|
---|
3693 | Medium::Task *pTask = NULL;
|
---|
3694 |
|
---|
3695 | try
|
---|
3696 | {
|
---|
3697 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3698 |
|
---|
3699 | /* Build the medium lock list. */
|
---|
3700 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3701 | alock.release();
|
---|
3702 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3703 | this /* pToLockWrite */,
|
---|
3704 | false /* fMediumLockWriteAll */,
|
---|
3705 | NULL,
|
---|
3706 | *pMediumLockList);
|
---|
3707 | alock.acquire();
|
---|
3708 | if (FAILED(rc))
|
---|
3709 | {
|
---|
3710 | delete pMediumLockList;
|
---|
3711 | throw rc;
|
---|
3712 | }
|
---|
3713 |
|
---|
3714 | alock.release();
|
---|
3715 | rc = pMediumLockList->Lock();
|
---|
3716 | alock.acquire();
|
---|
3717 | if (FAILED(rc))
|
---|
3718 | {
|
---|
3719 | delete pMediumLockList;
|
---|
3720 | throw setError(rc,
|
---|
3721 | tr("Failed to lock media when compacting '%s'"),
|
---|
3722 | i_getLocationFull().c_str());
|
---|
3723 | }
|
---|
3724 |
|
---|
3725 | pProgress.createObject();
|
---|
3726 | rc = pProgress->init(m->pVirtualBox,
|
---|
3727 | static_cast <IMedium *>(this),
|
---|
3728 | BstrFmt(tr("Compacting medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3729 | TRUE /* aCancelable */);
|
---|
3730 | if (FAILED(rc))
|
---|
3731 | {
|
---|
3732 | delete pMediumLockList;
|
---|
3733 | throw rc;
|
---|
3734 | }
|
---|
3735 |
|
---|
3736 | /* setup task object to carry out the operation asynchronously */
|
---|
3737 | pTask = new Medium::CompactTask(this, pProgress, pMediumLockList);
|
---|
3738 | rc = pTask->rc();
|
---|
3739 | AssertComRC(rc);
|
---|
3740 | if (FAILED(rc))
|
---|
3741 | throw rc;
|
---|
3742 | }
|
---|
3743 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3744 |
|
---|
3745 | if (SUCCEEDED(rc))
|
---|
3746 | {
|
---|
3747 | rc = pTask->createThread();
|
---|
3748 | pTask = NULL;
|
---|
3749 | if (SUCCEEDED(rc))
|
---|
3750 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3751 | }
|
---|
3752 | else if (pTask != NULL)
|
---|
3753 | delete pTask;
|
---|
3754 |
|
---|
3755 | return rc;
|
---|
3756 | }
|
---|
3757 |
|
---|
3758 | HRESULT Medium::resize(LONG64 aLogicalSize,
|
---|
3759 | ComPtr<IProgress> &aProgress)
|
---|
3760 | {
|
---|
3761 | CheckComArgExpr(aLogicalSize, aLogicalSize > 0);
|
---|
3762 | HRESULT rc = S_OK;
|
---|
3763 | ComObjPtr<Progress> pProgress;
|
---|
3764 |
|
---|
3765 | /* Build the medium lock list. */
|
---|
3766 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3767 |
|
---|
3768 | try
|
---|
3769 | {
|
---|
3770 | const char *pszError = NULL;
|
---|
3771 |
|
---|
3772 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3773 | this /* pToLockWrite */,
|
---|
3774 | false /* fMediumLockWriteAll */,
|
---|
3775 | NULL,
|
---|
3776 | *pMediumLockList);
|
---|
3777 | if (FAILED(rc))
|
---|
3778 | {
|
---|
3779 | pszError = tr("Failed to create medium lock list when resizing '%s'");
|
---|
3780 | }
|
---|
3781 | else
|
---|
3782 | {
|
---|
3783 | rc = pMediumLockList->Lock();
|
---|
3784 | if (FAILED(rc))
|
---|
3785 | pszError = tr("Failed to lock media when resizing '%s'");
|
---|
3786 | }
|
---|
3787 |
|
---|
3788 |
|
---|
3789 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3790 |
|
---|
3791 | if (FAILED(rc))
|
---|
3792 | {
|
---|
3793 | throw setError(rc, pszError, i_getLocationFull().c_str());
|
---|
3794 | }
|
---|
3795 |
|
---|
3796 | pProgress.createObject();
|
---|
3797 | rc = pProgress->init(m->pVirtualBox,
|
---|
3798 | static_cast <IMedium *>(this),
|
---|
3799 | BstrFmt(tr("Resizing medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3800 | TRUE /* aCancelable */);
|
---|
3801 | if (FAILED(rc))
|
---|
3802 | {
|
---|
3803 | throw rc;
|
---|
3804 | }
|
---|
3805 | }
|
---|
3806 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3807 |
|
---|
3808 | if (SUCCEEDED(rc))
|
---|
3809 | rc = i_resize((uint64_t)aLogicalSize, pMediumLockList, &pProgress, false /* aWait */, true /* aNotify */);
|
---|
3810 |
|
---|
3811 | if (SUCCEEDED(rc))
|
---|
3812 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3813 | else
|
---|
3814 | delete pMediumLockList;
|
---|
3815 |
|
---|
3816 | return rc;
|
---|
3817 | }
|
---|
3818 |
|
---|
3819 | HRESULT Medium::reset(AutoCaller &autoCaller, ComPtr<IProgress> &aProgress)
|
---|
3820 | {
|
---|
3821 | HRESULT rc = S_OK;
|
---|
3822 | ComObjPtr<Progress> pProgress;
|
---|
3823 | Medium::Task *pTask = NULL;
|
---|
3824 |
|
---|
3825 | try
|
---|
3826 | {
|
---|
3827 | autoCaller.release();
|
---|
3828 |
|
---|
3829 | /* It is possible that some previous/concurrent uninit has already
|
---|
3830 | * cleared the pVirtualBox reference, see #uninit(). */
|
---|
3831 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
3832 |
|
---|
3833 | /* i_canClose() needs the tree lock */
|
---|
3834 | AutoMultiWriteLock2 multilock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL,
|
---|
3835 | this->lockHandle()
|
---|
3836 | COMMA_LOCKVAL_SRC_POS);
|
---|
3837 |
|
---|
3838 | autoCaller.add();
|
---|
3839 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
3840 |
|
---|
3841 | LogFlowThisFunc(("ENTER for medium %s\n", m->strLocationFull.c_str()));
|
---|
3842 |
|
---|
3843 | if (m->pParent.isNull())
|
---|
3844 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3845 | tr("Medium type of '%s' is not differencing"),
|
---|
3846 | m->strLocationFull.c_str());
|
---|
3847 |
|
---|
3848 | rc = i_canClose();
|
---|
3849 | if (FAILED(rc))
|
---|
3850 | throw rc;
|
---|
3851 |
|
---|
3852 | /* Build the medium lock list. */
|
---|
3853 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3854 | multilock.release();
|
---|
3855 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3856 | this /* pToLockWrite */,
|
---|
3857 | false /* fMediumLockWriteAll */,
|
---|
3858 | NULL,
|
---|
3859 | *pMediumLockList);
|
---|
3860 | multilock.acquire();
|
---|
3861 | if (FAILED(rc))
|
---|
3862 | {
|
---|
3863 | delete pMediumLockList;
|
---|
3864 | throw rc;
|
---|
3865 | }
|
---|
3866 |
|
---|
3867 | multilock.release();
|
---|
3868 | rc = pMediumLockList->Lock();
|
---|
3869 | multilock.acquire();
|
---|
3870 | if (FAILED(rc))
|
---|
3871 | {
|
---|
3872 | delete pMediumLockList;
|
---|
3873 | throw setError(rc,
|
---|
3874 | tr("Failed to lock media when resetting '%s'"),
|
---|
3875 | i_getLocationFull().c_str());
|
---|
3876 | }
|
---|
3877 |
|
---|
3878 | pProgress.createObject();
|
---|
3879 | rc = pProgress->init(m->pVirtualBox,
|
---|
3880 | static_cast<IMedium*>(this),
|
---|
3881 | BstrFmt(tr("Resetting differencing medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3882 | FALSE /* aCancelable */);
|
---|
3883 | if (FAILED(rc))
|
---|
3884 | throw rc;
|
---|
3885 |
|
---|
3886 | /* setup task object to carry out the operation asynchronously */
|
---|
3887 | pTask = new Medium::ResetTask(this, pProgress, pMediumLockList);
|
---|
3888 | rc = pTask->rc();
|
---|
3889 | AssertComRC(rc);
|
---|
3890 | if (FAILED(rc))
|
---|
3891 | throw rc;
|
---|
3892 | }
|
---|
3893 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3894 |
|
---|
3895 | if (SUCCEEDED(rc))
|
---|
3896 | {
|
---|
3897 | rc = pTask->createThread();
|
---|
3898 | pTask = NULL;
|
---|
3899 | if (SUCCEEDED(rc))
|
---|
3900 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3901 | }
|
---|
3902 | else if (pTask != NULL)
|
---|
3903 | delete pTask;
|
---|
3904 |
|
---|
3905 | LogFlowThisFunc(("LEAVE, rc=%Rhrc\n", rc));
|
---|
3906 |
|
---|
3907 | return rc;
|
---|
3908 | }
|
---|
3909 |
|
---|
3910 | HRESULT Medium::changeEncryption(const com::Utf8Str &aCurrentPassword, const com::Utf8Str &aCipher,
|
---|
3911 | const com::Utf8Str &aNewPassword, const com::Utf8Str &aNewPasswordId,
|
---|
3912 | ComPtr<IProgress> &aProgress)
|
---|
3913 | {
|
---|
3914 | HRESULT rc = S_OK;
|
---|
3915 | ComObjPtr<Progress> pProgress;
|
---|
3916 | Medium::Task *pTask = NULL;
|
---|
3917 |
|
---|
3918 | try
|
---|
3919 | {
|
---|
3920 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3921 |
|
---|
3922 | DeviceType_T devType = i_getDeviceType();
|
---|
3923 | /* Cannot encrypt DVD or floppy images so far. */
|
---|
3924 | if ( devType == DeviceType_DVD
|
---|
3925 | || devType == DeviceType_Floppy)
|
---|
3926 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3927 | tr("Cannot encrypt DVD or Floppy medium '%s'"),
|
---|
3928 | m->strLocationFull.c_str());
|
---|
3929 |
|
---|
3930 | /* Cannot encrypt media which are attached to more than one virtual machine. */
|
---|
3931 | if (m->backRefs.size() > 1)
|
---|
3932 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3933 | tr("Cannot encrypt medium '%s' because it is attached to %d virtual machines", "", m->backRefs.size()),
|
---|
3934 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
3935 |
|
---|
3936 | if (i_getChildren().size() != 0)
|
---|
3937 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3938 | tr("Cannot encrypt medium '%s' because it has %d children", "", i_getChildren().size()),
|
---|
3939 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
3940 |
|
---|
3941 | /* Build the medium lock list. */
|
---|
3942 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3943 | alock.release();
|
---|
3944 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3945 | this /* pToLockWrite */,
|
---|
3946 | true /* fMediumLockAllWrite */,
|
---|
3947 | NULL,
|
---|
3948 | *pMediumLockList);
|
---|
3949 | alock.acquire();
|
---|
3950 | if (FAILED(rc))
|
---|
3951 | {
|
---|
3952 | delete pMediumLockList;
|
---|
3953 | throw rc;
|
---|
3954 | }
|
---|
3955 |
|
---|
3956 | alock.release();
|
---|
3957 | rc = pMediumLockList->Lock();
|
---|
3958 | alock.acquire();
|
---|
3959 | if (FAILED(rc))
|
---|
3960 | {
|
---|
3961 | delete pMediumLockList;
|
---|
3962 | throw setError(rc,
|
---|
3963 | tr("Failed to lock media for encryption '%s'"),
|
---|
3964 | i_getLocationFull().c_str());
|
---|
3965 | }
|
---|
3966 |
|
---|
3967 | /*
|
---|
3968 | * Check all media in the chain to not contain any branches or references to
|
---|
3969 | * other virtual machines, we support encrypting only a list of differencing media at the moment.
|
---|
3970 | */
|
---|
3971 | MediumLockList::Base::const_iterator mediumListBegin = pMediumLockList->GetBegin();
|
---|
3972 | MediumLockList::Base::const_iterator mediumListEnd = pMediumLockList->GetEnd();
|
---|
3973 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
3974 | it != mediumListEnd;
|
---|
3975 | ++it)
|
---|
3976 | {
|
---|
3977 | const MediumLock &mediumLock = *it;
|
---|
3978 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
3979 | AutoReadLock mediumReadLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
3980 |
|
---|
3981 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
3982 |
|
---|
3983 | if (pMedium->m->backRefs.size() > 1)
|
---|
3984 | {
|
---|
3985 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3986 | tr("Cannot encrypt medium '%s' because it is attached to %d virtual machines", "",
|
---|
3987 | pMedium->m->backRefs.size()),
|
---|
3988 | pMedium->m->strLocationFull.c_str(), pMedium->m->backRefs.size());
|
---|
3989 | break;
|
---|
3990 | }
|
---|
3991 | else if (pMedium->i_getChildren().size() > 1)
|
---|
3992 | {
|
---|
3993 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3994 | tr("Cannot encrypt medium '%s' because it has %d children", "", pMedium->i_getChildren().size()),
|
---|
3995 | pMedium->m->strLocationFull.c_str(), pMedium->i_getChildren().size());
|
---|
3996 | break;
|
---|
3997 | }
|
---|
3998 | }
|
---|
3999 |
|
---|
4000 | if (FAILED(rc))
|
---|
4001 | {
|
---|
4002 | delete pMediumLockList;
|
---|
4003 | throw rc;
|
---|
4004 | }
|
---|
4005 |
|
---|
4006 | const char *pszAction = tr("Encrypting medium");
|
---|
4007 | if ( aCurrentPassword.isNotEmpty()
|
---|
4008 | && aCipher.isEmpty())
|
---|
4009 | pszAction = tr("Decrypting medium");
|
---|
4010 |
|
---|
4011 | pProgress.createObject();
|
---|
4012 | rc = pProgress->init(m->pVirtualBox,
|
---|
4013 | static_cast <IMedium *>(this),
|
---|
4014 | BstrFmt("%s '%s'", pszAction, m->strLocationFull.c_str()).raw(),
|
---|
4015 | TRUE /* aCancelable */);
|
---|
4016 | if (FAILED(rc))
|
---|
4017 | {
|
---|
4018 | delete pMediumLockList;
|
---|
4019 | throw rc;
|
---|
4020 | }
|
---|
4021 |
|
---|
4022 | /* setup task object to carry out the operation asynchronously */
|
---|
4023 | pTask = new Medium::EncryptTask(this, aNewPassword, aCurrentPassword,
|
---|
4024 | aCipher, aNewPasswordId, pProgress, pMediumLockList);
|
---|
4025 | rc = pTask->rc();
|
---|
4026 | AssertComRC(rc);
|
---|
4027 | if (FAILED(rc))
|
---|
4028 | throw rc;
|
---|
4029 | }
|
---|
4030 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4031 |
|
---|
4032 | if (SUCCEEDED(rc))
|
---|
4033 | {
|
---|
4034 | rc = pTask->createThread();
|
---|
4035 | pTask = NULL;
|
---|
4036 | if (SUCCEEDED(rc))
|
---|
4037 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
4038 | }
|
---|
4039 | else if (pTask != NULL)
|
---|
4040 | delete pTask;
|
---|
4041 |
|
---|
4042 | return rc;
|
---|
4043 | }
|
---|
4044 |
|
---|
4045 | HRESULT Medium::getEncryptionSettings(AutoCaller &autoCaller, com::Utf8Str &aCipher, com::Utf8Str &aPasswordId)
|
---|
4046 | {
|
---|
4047 | #ifndef VBOX_WITH_EXTPACK
|
---|
4048 | RT_NOREF(aCipher, aPasswordId);
|
---|
4049 | #endif
|
---|
4050 | HRESULT rc = S_OK;
|
---|
4051 |
|
---|
4052 | try
|
---|
4053 | {
|
---|
4054 | autoCaller.release();
|
---|
4055 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
4056 | autoCaller.add();
|
---|
4057 | if (FAILED(autoCaller.rc()))
|
---|
4058 | throw rc;
|
---|
4059 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4060 |
|
---|
4061 | /* Check whether encryption is configured for this medium. */
|
---|
4062 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
4063 | if (it == pBase->m->mapProperties.end())
|
---|
4064 | throw VBOX_E_NOT_SUPPORTED;
|
---|
4065 |
|
---|
4066 | # ifdef VBOX_WITH_EXTPACK
|
---|
4067 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
4068 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
4069 | {
|
---|
4070 | /* Load the plugin */
|
---|
4071 | Utf8Str strPlugin;
|
---|
4072 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
4073 | if (SUCCEEDED(rc))
|
---|
4074 | {
|
---|
4075 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
4076 | if (RT_FAILURE(vrc))
|
---|
4077 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
4078 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
4079 | i_vdError(vrc).c_str());
|
---|
4080 | }
|
---|
4081 | else
|
---|
4082 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
4083 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
4084 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
4085 | }
|
---|
4086 | else
|
---|
4087 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
4088 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
4089 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
4090 |
|
---|
4091 | PVDISK pDisk = NULL;
|
---|
4092 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
4093 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
4094 |
|
---|
4095 | MediumCryptoFilterSettings CryptoSettings;
|
---|
4096 |
|
---|
4097 | i_taskEncryptSettingsSetup(&CryptoSettings, NULL, it->second.c_str(), NULL, false /* fCreateKeyStore */);
|
---|
4098 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ | VD_FILTER_FLAGS_INFO, CryptoSettings.vdFilterIfaces);
|
---|
4099 | if (RT_FAILURE(vrc))
|
---|
4100 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc,
|
---|
4101 | tr("Failed to load the encryption filter: %s"),
|
---|
4102 | i_vdError(vrc).c_str());
|
---|
4103 |
|
---|
4104 | it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
4105 | if (it == pBase->m->mapProperties.end())
|
---|
4106 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4107 | tr("Image is configured for encryption but doesn't has a KeyId set"));
|
---|
4108 |
|
---|
4109 | aPasswordId = it->second.c_str();
|
---|
4110 | aCipher = CryptoSettings.pszCipherReturned;
|
---|
4111 | RTStrFree(CryptoSettings.pszCipherReturned);
|
---|
4112 |
|
---|
4113 | VDDestroy(pDisk);
|
---|
4114 | # else
|
---|
4115 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
4116 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
4117 | # endif
|
---|
4118 | }
|
---|
4119 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4120 |
|
---|
4121 | return rc;
|
---|
4122 | }
|
---|
4123 |
|
---|
4124 | HRESULT Medium::checkEncryptionPassword(const com::Utf8Str &aPassword)
|
---|
4125 | {
|
---|
4126 | HRESULT rc = S_OK;
|
---|
4127 |
|
---|
4128 | try
|
---|
4129 | {
|
---|
4130 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
4131 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4132 |
|
---|
4133 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
4134 | if (it == pBase->m->mapProperties.end())
|
---|
4135 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
4136 | tr("The image is not configured for encryption"));
|
---|
4137 |
|
---|
4138 | if (aPassword.isEmpty())
|
---|
4139 | throw setError(E_INVALIDARG,
|
---|
4140 | tr("The given password must not be empty"));
|
---|
4141 |
|
---|
4142 | # ifdef VBOX_WITH_EXTPACK
|
---|
4143 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
4144 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
4145 | {
|
---|
4146 | /* Load the plugin */
|
---|
4147 | Utf8Str strPlugin;
|
---|
4148 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
4149 | if (SUCCEEDED(rc))
|
---|
4150 | {
|
---|
4151 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
4152 | if (RT_FAILURE(vrc))
|
---|
4153 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
4154 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
4155 | i_vdError(vrc).c_str());
|
---|
4156 | }
|
---|
4157 | else
|
---|
4158 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
4159 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
4160 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
4161 | }
|
---|
4162 | else
|
---|
4163 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
4164 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
4165 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
4166 |
|
---|
4167 | PVDISK pDisk = NULL;
|
---|
4168 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
4169 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
4170 |
|
---|
4171 | MediumCryptoFilterSettings CryptoSettings;
|
---|
4172 |
|
---|
4173 | i_taskEncryptSettingsSetup(&CryptoSettings, NULL, it->second.c_str(), aPassword.c_str(),
|
---|
4174 | false /* fCreateKeyStore */);
|
---|
4175 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ, CryptoSettings.vdFilterIfaces);
|
---|
4176 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
4177 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
4178 | tr("The given password is incorrect"));
|
---|
4179 | else if (RT_FAILURE(vrc))
|
---|
4180 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc,
|
---|
4181 | tr("Failed to load the encryption filter: %s"),
|
---|
4182 | i_vdError(vrc).c_str());
|
---|
4183 |
|
---|
4184 | VDDestroy(pDisk);
|
---|
4185 | # else
|
---|
4186 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
4187 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
4188 | # endif
|
---|
4189 | }
|
---|
4190 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4191 |
|
---|
4192 | return rc;
|
---|
4193 | }
|
---|
4194 |
|
---|
4195 | HRESULT Medium::openForIO(BOOL aWritable, com::Utf8Str const &aPassword, ComPtr<IMediumIO> &aMediumIO)
|
---|
4196 | {
|
---|
4197 | /*
|
---|
4198 | * Input validation.
|
---|
4199 | */
|
---|
4200 | if (aWritable && i_isReadOnly())
|
---|
4201 | return setError(E_ACCESSDENIED, tr("Write access denied: read-only"));
|
---|
4202 |
|
---|
4203 | com::Utf8Str const strKeyId = i_getKeyId();
|
---|
4204 | if (strKeyId.isEmpty() && aPassword.isNotEmpty())
|
---|
4205 | return setError(E_INVALIDARG, tr("Password given for unencrypted medium"));
|
---|
4206 | if (strKeyId.isNotEmpty() && aPassword.isEmpty())
|
---|
4207 | return setError(E_INVALIDARG, tr("Password needed for encrypted medium"));
|
---|
4208 |
|
---|
4209 | /*
|
---|
4210 | * Create IO object and return it.
|
---|
4211 | */
|
---|
4212 | ComObjPtr<MediumIO> ptrIO;
|
---|
4213 | HRESULT hrc = ptrIO.createObject();
|
---|
4214 | if (SUCCEEDED(hrc))
|
---|
4215 | {
|
---|
4216 | hrc = ptrIO->initForMedium(this, m->pVirtualBox, aWritable != FALSE, strKeyId, aPassword);
|
---|
4217 | if (SUCCEEDED(hrc))
|
---|
4218 | ptrIO.queryInterfaceTo(aMediumIO.asOutParam());
|
---|
4219 | }
|
---|
4220 | return hrc;
|
---|
4221 | }
|
---|
4222 |
|
---|
4223 |
|
---|
4224 | ////////////////////////////////////////////////////////////////////////////////
|
---|
4225 | //
|
---|
4226 | // Medium public internal methods
|
---|
4227 | //
|
---|
4228 | ////////////////////////////////////////////////////////////////////////////////
|
---|
4229 |
|
---|
4230 | /**
|
---|
4231 | * Internal method to return the medium's parent medium. Must have caller + locking!
|
---|
4232 | * @return
|
---|
4233 | */
|
---|
4234 | const ComObjPtr<Medium>& Medium::i_getParent() const
|
---|
4235 | {
|
---|
4236 | return m->pParent;
|
---|
4237 | }
|
---|
4238 |
|
---|
4239 | /**
|
---|
4240 | * Internal method to return the medium's list of child media. Must have caller + locking!
|
---|
4241 | * @return
|
---|
4242 | */
|
---|
4243 | const MediaList& Medium::i_getChildren() const
|
---|
4244 | {
|
---|
4245 | return m->llChildren;
|
---|
4246 | }
|
---|
4247 |
|
---|
4248 | /**
|
---|
4249 | * Internal method to return the medium's GUID. Must have caller + locking!
|
---|
4250 | * @return
|
---|
4251 | */
|
---|
4252 | const Guid& Medium::i_getId() const
|
---|
4253 | {
|
---|
4254 | return m->id;
|
---|
4255 | }
|
---|
4256 |
|
---|
4257 | /**
|
---|
4258 | * Internal method to return the medium's state. Must have caller + locking!
|
---|
4259 | * @return
|
---|
4260 | */
|
---|
4261 | MediumState_T Medium::i_getState() const
|
---|
4262 | {
|
---|
4263 | return m->state;
|
---|
4264 | }
|
---|
4265 |
|
---|
4266 | /**
|
---|
4267 | * Internal method to return the medium's variant. Must have caller + locking!
|
---|
4268 | * @return
|
---|
4269 | */
|
---|
4270 | MediumVariant_T Medium::i_getVariant() const
|
---|
4271 | {
|
---|
4272 | return m->variant;
|
---|
4273 | }
|
---|
4274 |
|
---|
4275 | /**
|
---|
4276 | * Internal method which returns true if this medium represents a host drive.
|
---|
4277 | * @return
|
---|
4278 | */
|
---|
4279 | bool Medium::i_isHostDrive() const
|
---|
4280 | {
|
---|
4281 | return m->hostDrive;
|
---|
4282 | }
|
---|
4283 |
|
---|
4284 | /**
|
---|
4285 | * Internal method to return the medium's full location. Must have caller + locking!
|
---|
4286 | * @return
|
---|
4287 | */
|
---|
4288 | const Utf8Str& Medium::i_getLocationFull() const
|
---|
4289 | {
|
---|
4290 | return m->strLocationFull;
|
---|
4291 | }
|
---|
4292 |
|
---|
4293 | /**
|
---|
4294 | * Internal method to return the medium's format string. Must have caller + locking!
|
---|
4295 | * @return
|
---|
4296 | */
|
---|
4297 | const Utf8Str& Medium::i_getFormat() const
|
---|
4298 | {
|
---|
4299 | return m->strFormat;
|
---|
4300 | }
|
---|
4301 |
|
---|
4302 | /**
|
---|
4303 | * Internal method to return the medium's format object. Must have caller + locking!
|
---|
4304 | * @return
|
---|
4305 | */
|
---|
4306 | const ComObjPtr<MediumFormat>& Medium::i_getMediumFormat() const
|
---|
4307 | {
|
---|
4308 | return m->formatObj;
|
---|
4309 | }
|
---|
4310 |
|
---|
4311 | /**
|
---|
4312 | * Internal method that returns true if the medium is represented by a file on the host disk
|
---|
4313 | * (and not iSCSI or something).
|
---|
4314 | * @return
|
---|
4315 | */
|
---|
4316 | bool Medium::i_isMediumFormatFile() const
|
---|
4317 | {
|
---|
4318 | if ( m->formatObj
|
---|
4319 | && (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
4320 | )
|
---|
4321 | return true;
|
---|
4322 | return false;
|
---|
4323 | }
|
---|
4324 |
|
---|
4325 | /**
|
---|
4326 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
4327 | * @return
|
---|
4328 | */
|
---|
4329 | uint64_t Medium::i_getSize() const
|
---|
4330 | {
|
---|
4331 | return m->size;
|
---|
4332 | }
|
---|
4333 |
|
---|
4334 | /**
|
---|
4335 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
4336 | * @return
|
---|
4337 | */
|
---|
4338 | uint64_t Medium::i_getLogicalSize() const
|
---|
4339 | {
|
---|
4340 | return m->logicalSize;
|
---|
4341 | }
|
---|
4342 |
|
---|
4343 | /**
|
---|
4344 | * Returns the medium device type. Must have caller + locking!
|
---|
4345 | * @return
|
---|
4346 | */
|
---|
4347 | DeviceType_T Medium::i_getDeviceType() const
|
---|
4348 | {
|
---|
4349 | return m->devType;
|
---|
4350 | }
|
---|
4351 |
|
---|
4352 | /**
|
---|
4353 | * Returns the medium type. Must have caller + locking!
|
---|
4354 | * @return
|
---|
4355 | */
|
---|
4356 | MediumType_T Medium::i_getType() const
|
---|
4357 | {
|
---|
4358 | return m->type;
|
---|
4359 | }
|
---|
4360 |
|
---|
4361 | /**
|
---|
4362 | * Returns a short version of the location attribute.
|
---|
4363 | *
|
---|
4364 | * @note Must be called from under this object's read or write lock.
|
---|
4365 | */
|
---|
4366 | Utf8Str Medium::i_getName()
|
---|
4367 | {
|
---|
4368 | Utf8Str name = RTPathFilename(m->strLocationFull.c_str());
|
---|
4369 | return name;
|
---|
4370 | }
|
---|
4371 |
|
---|
4372 | /**
|
---|
4373 | * Same as i_addRegistry() except that we don't check the object state, making
|
---|
4374 | * it safe to call with initFromSettings() on the call stack.
|
---|
4375 | */
|
---|
4376 | bool Medium::i_addRegistryNoCallerCheck(const Guid &id)
|
---|
4377 | {
|
---|
4378 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4379 |
|
---|
4380 | bool fAdd = true;
|
---|
4381 |
|
---|
4382 | // hard disks cannot be in more than one registry
|
---|
4383 | if ( m->devType == DeviceType_HardDisk
|
---|
4384 | && m->llRegistryIDs.size() > 0)
|
---|
4385 | fAdd = false;
|
---|
4386 |
|
---|
4387 | // no need to add the UUID twice
|
---|
4388 | if (fAdd)
|
---|
4389 | {
|
---|
4390 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
4391 | it != m->llRegistryIDs.end();
|
---|
4392 | ++it)
|
---|
4393 | {
|
---|
4394 | if ((*it) == id)
|
---|
4395 | {
|
---|
4396 | fAdd = false;
|
---|
4397 | break;
|
---|
4398 | }
|
---|
4399 | }
|
---|
4400 | }
|
---|
4401 |
|
---|
4402 | if (fAdd)
|
---|
4403 | m->llRegistryIDs.push_back(id);
|
---|
4404 |
|
---|
4405 | return fAdd;
|
---|
4406 | }
|
---|
4407 |
|
---|
4408 | /**
|
---|
4409 | * This adds the given UUID to the list of media registries in which this
|
---|
4410 | * medium should be registered. The UUID can either be a machine UUID,
|
---|
4411 | * to add a machine registry, or the global registry UUID as returned by
|
---|
4412 | * VirtualBox::getGlobalRegistryId().
|
---|
4413 | *
|
---|
4414 | * Note that for hard disks, this method does nothing if the medium is
|
---|
4415 | * already in another registry to avoid having hard disks in more than
|
---|
4416 | * one registry, which causes trouble with keeping diff images in sync.
|
---|
4417 | * See getFirstRegistryMachineId() for details.
|
---|
4418 | *
|
---|
4419 | * @param id
|
---|
4420 | * @return true if the registry was added; false if the given id was already on the list.
|
---|
4421 | */
|
---|
4422 | bool Medium::i_addRegistry(const Guid &id)
|
---|
4423 | {
|
---|
4424 | AutoCaller autoCaller(this);
|
---|
4425 | if (FAILED(autoCaller.rc()))
|
---|
4426 | return false;
|
---|
4427 | return i_addRegistryNoCallerCheck(id);
|
---|
4428 | }
|
---|
4429 |
|
---|
4430 | /**
|
---|
4431 | * This adds the given UUID to the list of media registries in which this
|
---|
4432 | * medium should be registered. The UUID can either be a machine UUID,
|
---|
4433 | * to add a machine registry, or the global registry UUID as returned by
|
---|
4434 | * VirtualBox::getGlobalRegistryId(). Thisis applied to all children.
|
---|
4435 | *
|
---|
4436 | * Note that for hard disks, this method does nothing if the medium is
|
---|
4437 | * already in another registry to avoid having hard disks in more than
|
---|
4438 | * one registry, which causes trouble with keeping diff images in sync.
|
---|
4439 | * See getFirstRegistryMachineId() for details.
|
---|
4440 | *
|
---|
4441 | * @note the caller must hold the media tree lock for reading.
|
---|
4442 | *
|
---|
4443 | * @param id
|
---|
4444 | * @return true if the registry was added; false if the given id was already on the list.
|
---|
4445 | */
|
---|
4446 | bool Medium::i_addRegistryAll(const Guid &id)
|
---|
4447 | {
|
---|
4448 | MediaList llMediaTodo;
|
---|
4449 | llMediaTodo.push_back(this);
|
---|
4450 |
|
---|
4451 | bool fAdd = false;
|
---|
4452 |
|
---|
4453 | while (!llMediaTodo.empty())
|
---|
4454 | {
|
---|
4455 | ComObjPtr<Medium> pMedium = llMediaTodo.front();
|
---|
4456 | llMediaTodo.pop_front();
|
---|
4457 |
|
---|
4458 | AutoCaller mediumCaller(pMedium);
|
---|
4459 | if (FAILED(mediumCaller.rc())) continue;
|
---|
4460 |
|
---|
4461 | fAdd |= pMedium->i_addRegistryNoCallerCheck(id);
|
---|
4462 |
|
---|
4463 | // protected by the medium tree lock held by our original caller
|
---|
4464 | MediaList::const_iterator itBegin = pMedium->i_getChildren().begin();
|
---|
4465 | MediaList::const_iterator itEnd = pMedium->i_getChildren().end();
|
---|
4466 | for (MediaList::const_iterator it = itBegin; it != itEnd; ++it)
|
---|
4467 | llMediaTodo.push_back(*it);
|
---|
4468 | }
|
---|
4469 |
|
---|
4470 | return fAdd;
|
---|
4471 | }
|
---|
4472 |
|
---|
4473 | /**
|
---|
4474 | * Removes the given UUID from the list of media registry UUIDs of this medium.
|
---|
4475 | *
|
---|
4476 | * @param id
|
---|
4477 | * @return true if the UUID was found or false if not.
|
---|
4478 | */
|
---|
4479 | bool Medium::i_removeRegistry(const Guid &id)
|
---|
4480 | {
|
---|
4481 | AutoCaller autoCaller(this);
|
---|
4482 | if (FAILED(autoCaller.rc()))
|
---|
4483 | return false;
|
---|
4484 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4485 |
|
---|
4486 | bool fRemove = false;
|
---|
4487 |
|
---|
4488 | /// @todo r=klaus eliminate this code, replace it by using find.
|
---|
4489 | for (GuidList::iterator it = m->llRegistryIDs.begin();
|
---|
4490 | it != m->llRegistryIDs.end();
|
---|
4491 | ++it)
|
---|
4492 | {
|
---|
4493 | if ((*it) == id)
|
---|
4494 | {
|
---|
4495 | // getting away with this as the iterator isn't used after
|
---|
4496 | m->llRegistryIDs.erase(it);
|
---|
4497 | fRemove = true;
|
---|
4498 | break;
|
---|
4499 | }
|
---|
4500 | }
|
---|
4501 |
|
---|
4502 | return fRemove;
|
---|
4503 | }
|
---|
4504 |
|
---|
4505 | /**
|
---|
4506 | * Removes the given UUID from the list of media registry UUIDs, for this
|
---|
4507 | * medium and all its children.
|
---|
4508 | *
|
---|
4509 | * @note the caller must hold the media tree lock for reading.
|
---|
4510 | *
|
---|
4511 | * @param id
|
---|
4512 | * @return true if the UUID was found or false if not.
|
---|
4513 | */
|
---|
4514 | bool Medium::i_removeRegistryAll(const Guid &id)
|
---|
4515 | {
|
---|
4516 | MediaList llMediaTodo;
|
---|
4517 | llMediaTodo.push_back(this);
|
---|
4518 |
|
---|
4519 | bool fRemove = false;
|
---|
4520 |
|
---|
4521 | while (!llMediaTodo.empty())
|
---|
4522 | {
|
---|
4523 | ComObjPtr<Medium> pMedium = llMediaTodo.front();
|
---|
4524 | llMediaTodo.pop_front();
|
---|
4525 |
|
---|
4526 | AutoCaller mediumCaller(pMedium);
|
---|
4527 | if (FAILED(mediumCaller.rc())) continue;
|
---|
4528 |
|
---|
4529 | fRemove |= pMedium->i_removeRegistry(id);
|
---|
4530 |
|
---|
4531 | // protected by the medium tree lock held by our original caller
|
---|
4532 | MediaList::const_iterator itBegin = pMedium->i_getChildren().begin();
|
---|
4533 | MediaList::const_iterator itEnd = pMedium->i_getChildren().end();
|
---|
4534 | for (MediaList::const_iterator it = itBegin; it != itEnd; ++it)
|
---|
4535 | llMediaTodo.push_back(*it);
|
---|
4536 | }
|
---|
4537 |
|
---|
4538 | return fRemove;
|
---|
4539 | }
|
---|
4540 |
|
---|
4541 | /**
|
---|
4542 | * Returns true if id is in the list of media registries for this medium.
|
---|
4543 | *
|
---|
4544 | * Must have caller + read locking!
|
---|
4545 | *
|
---|
4546 | * @param id
|
---|
4547 | * @return
|
---|
4548 | */
|
---|
4549 | bool Medium::i_isInRegistry(const Guid &id)
|
---|
4550 | {
|
---|
4551 | /// @todo r=klaus eliminate this code, replace it by using find.
|
---|
4552 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
4553 | it != m->llRegistryIDs.end();
|
---|
4554 | ++it)
|
---|
4555 | {
|
---|
4556 | if (*it == id)
|
---|
4557 | return true;
|
---|
4558 | }
|
---|
4559 |
|
---|
4560 | return false;
|
---|
4561 | }
|
---|
4562 |
|
---|
4563 | /**
|
---|
4564 | * Internal method to return the medium's first registry machine (i.e. the machine in whose
|
---|
4565 | * machine XML this medium is listed).
|
---|
4566 | *
|
---|
4567 | * Every attached medium must now (4.0) reside in at least one media registry, which is identified
|
---|
4568 | * by a UUID. This is either a machine UUID if the machine is from 4.0 or newer, in which case
|
---|
4569 | * machines have their own media registries, or it is the pseudo-UUID of the VirtualBox
|
---|
4570 | * object if the machine is old and still needs the global registry in VirtualBox.xml.
|
---|
4571 | *
|
---|
4572 | * By definition, hard disks may only be in one media registry, in which all its children
|
---|
4573 | * will be stored as well. Otherwise we run into problems with having keep multiple registries
|
---|
4574 | * in sync. (This is the "cloned VM" case in which VM1 may link to the disks of VM2; in this
|
---|
4575 | * case, only VM2's registry is used for the disk in question.)
|
---|
4576 | *
|
---|
4577 | * If there is no medium registry, particularly if the medium has not been attached yet, this
|
---|
4578 | * does not modify uuid and returns false.
|
---|
4579 | *
|
---|
4580 | * ISOs and RAWs, by contrast, can be in more than one repository to make things easier for
|
---|
4581 | * the user.
|
---|
4582 | *
|
---|
4583 | * Must have caller + locking!
|
---|
4584 | *
|
---|
4585 | * @param uuid Receives first registry machine UUID, if available.
|
---|
4586 | * @return true if uuid was set.
|
---|
4587 | */
|
---|
4588 | bool Medium::i_getFirstRegistryMachineId(Guid &uuid) const
|
---|
4589 | {
|
---|
4590 | if (m->llRegistryIDs.size())
|
---|
4591 | {
|
---|
4592 | uuid = m->llRegistryIDs.front();
|
---|
4593 | return true;
|
---|
4594 | }
|
---|
4595 | return false;
|
---|
4596 | }
|
---|
4597 |
|
---|
4598 | /**
|
---|
4599 | * Marks all the registries in which this medium is registered as modified.
|
---|
4600 | */
|
---|
4601 | void Medium::i_markRegistriesModified()
|
---|
4602 | {
|
---|
4603 | AutoCaller autoCaller(this);
|
---|
4604 | if (FAILED(autoCaller.rc())) return;
|
---|
4605 |
|
---|
4606 | // Get local copy, as keeping the lock over VirtualBox::markRegistryModified
|
---|
4607 | // causes trouble with the lock order
|
---|
4608 | GuidList llRegistryIDs;
|
---|
4609 | {
|
---|
4610 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4611 | llRegistryIDs = m->llRegistryIDs;
|
---|
4612 | }
|
---|
4613 |
|
---|
4614 | autoCaller.release();
|
---|
4615 |
|
---|
4616 | /* Save the error information now, the implicit restore when this goes
|
---|
4617 | * out of scope will throw away spurious additional errors created below. */
|
---|
4618 | ErrorInfoKeeper eik;
|
---|
4619 | for (GuidList::const_iterator it = llRegistryIDs.begin();
|
---|
4620 | it != llRegistryIDs.end();
|
---|
4621 | ++it)
|
---|
4622 | {
|
---|
4623 | m->pVirtualBox->i_markRegistryModified(*it);
|
---|
4624 | }
|
---|
4625 | }
|
---|
4626 |
|
---|
4627 | /**
|
---|
4628 | * Adds the given machine and optionally the snapshot to the list of the objects
|
---|
4629 | * this medium is attached to.
|
---|
4630 | *
|
---|
4631 | * @param aMachineId Machine ID.
|
---|
4632 | * @param aSnapshotId Snapshot ID; when non-empty, adds a snapshot attachment.
|
---|
4633 | */
|
---|
4634 | HRESULT Medium::i_addBackReference(const Guid &aMachineId,
|
---|
4635 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
4636 | {
|
---|
4637 | AssertReturn(aMachineId.isValid(), E_FAIL);
|
---|
4638 |
|
---|
4639 | LogFlowThisFunc(("ENTER, aMachineId: {%RTuuid}, aSnapshotId: {%RTuuid}\n", aMachineId.raw(), aSnapshotId.raw()));
|
---|
4640 |
|
---|
4641 | AutoCaller autoCaller(this);
|
---|
4642 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4643 |
|
---|
4644 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4645 |
|
---|
4646 | switch (m->state)
|
---|
4647 | {
|
---|
4648 | case MediumState_Created:
|
---|
4649 | case MediumState_Inaccessible:
|
---|
4650 | case MediumState_LockedRead:
|
---|
4651 | case MediumState_LockedWrite:
|
---|
4652 | break;
|
---|
4653 |
|
---|
4654 | default:
|
---|
4655 | return i_setStateError();
|
---|
4656 | }
|
---|
4657 |
|
---|
4658 | if (m->numCreateDiffTasks > 0)
|
---|
4659 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4660 | tr("Cannot attach medium '%s' {%RTuuid}: %u differencing child media are being created", "",
|
---|
4661 | m->numCreateDiffTasks),
|
---|
4662 | m->strLocationFull.c_str(),
|
---|
4663 | m->id.raw(),
|
---|
4664 | m->numCreateDiffTasks);
|
---|
4665 |
|
---|
4666 | BackRefList::iterator it = std::find_if(m->backRefs.begin(),
|
---|
4667 | m->backRefs.end(),
|
---|
4668 | BackRef::EqualsTo(aMachineId));
|
---|
4669 | if (it == m->backRefs.end())
|
---|
4670 | {
|
---|
4671 | BackRef ref(aMachineId, aSnapshotId);
|
---|
4672 | m->backRefs.push_back(ref);
|
---|
4673 |
|
---|
4674 | return S_OK;
|
---|
4675 | }
|
---|
4676 | bool fDvd = false;
|
---|
4677 | {
|
---|
4678 | AutoReadLock arlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4679 | /*
|
---|
4680 | * Check the medium is DVD and readonly. It's for the case if DVD
|
---|
4681 | * will be able to be writable sometime in the future.
|
---|
4682 | */
|
---|
4683 | fDvd = m->type == MediumType_Readonly && m->devType == DeviceType_DVD;
|
---|
4684 | }
|
---|
4685 |
|
---|
4686 | // if the caller has not supplied a snapshot ID, then we're attaching
|
---|
4687 | // to a machine a medium which represents the machine's current state,
|
---|
4688 | // so set the flag
|
---|
4689 |
|
---|
4690 | if (aSnapshotId.isZero())
|
---|
4691 | {
|
---|
4692 | // Allow DVD having MediumType_Readonly to be attached twice.
|
---|
4693 | // (the medium already had been added to back reference)
|
---|
4694 | if (fDvd)
|
---|
4695 | {
|
---|
4696 | it->iRefCnt++;
|
---|
4697 | return S_OK;
|
---|
4698 | }
|
---|
4699 |
|
---|
4700 | /* sanity: no duplicate attachments */
|
---|
4701 | if (it->fInCurState)
|
---|
4702 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4703 | tr("Cannot attach medium '%s' {%RTuuid}: medium is already associated with the current state of machine uuid {%RTuuid}!"),
|
---|
4704 | m->strLocationFull.c_str(),
|
---|
4705 | m->id.raw(),
|
---|
4706 | aMachineId.raw());
|
---|
4707 | it->fInCurState = true;
|
---|
4708 |
|
---|
4709 | return S_OK;
|
---|
4710 | }
|
---|
4711 |
|
---|
4712 | // otherwise: a snapshot medium is being attached
|
---|
4713 |
|
---|
4714 | /* sanity: no duplicate attachments */
|
---|
4715 | for (std::list<SnapshotRef>::iterator jt = it->llSnapshotIds.begin();
|
---|
4716 | jt != it->llSnapshotIds.end();
|
---|
4717 | ++jt)
|
---|
4718 | {
|
---|
4719 | const Guid &idOldSnapshot = jt->snapshotId;
|
---|
4720 |
|
---|
4721 | if (idOldSnapshot == aSnapshotId)
|
---|
4722 | {
|
---|
4723 | if (fDvd)
|
---|
4724 | {
|
---|
4725 | jt->iRefCnt++;
|
---|
4726 | return S_OK;
|
---|
4727 | }
|
---|
4728 | #ifdef DEBUG
|
---|
4729 | i_dumpBackRefs();
|
---|
4730 | #endif
|
---|
4731 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4732 | tr("Cannot attach medium '%s' {%RTuuid} from snapshot '%RTuuid': medium is already in use by this snapshot!"),
|
---|
4733 | m->strLocationFull.c_str(),
|
---|
4734 | m->id.raw(),
|
---|
4735 | aSnapshotId.raw());
|
---|
4736 | }
|
---|
4737 | }
|
---|
4738 |
|
---|
4739 | it->llSnapshotIds.push_back(SnapshotRef(aSnapshotId));
|
---|
4740 | // Do not touch fInCurState, as the image may be attached to the current
|
---|
4741 | // state *and* a snapshot, otherwise we lose the current state association!
|
---|
4742 |
|
---|
4743 | LogFlowThisFuncLeave();
|
---|
4744 |
|
---|
4745 | return S_OK;
|
---|
4746 | }
|
---|
4747 |
|
---|
4748 | /**
|
---|
4749 | * Removes the given machine and optionally the snapshot from the list of the
|
---|
4750 | * objects this medium is attached to.
|
---|
4751 | *
|
---|
4752 | * @param aMachineId Machine ID.
|
---|
4753 | * @param aSnapshotId Snapshot ID; when non-empty, removes the snapshot
|
---|
4754 | * attachment.
|
---|
4755 | */
|
---|
4756 | HRESULT Medium::i_removeBackReference(const Guid &aMachineId,
|
---|
4757 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
4758 | {
|
---|
4759 | AssertReturn(aMachineId.isValid(), E_FAIL);
|
---|
4760 |
|
---|
4761 | AutoCaller autoCaller(this);
|
---|
4762 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4763 |
|
---|
4764 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4765 |
|
---|
4766 | BackRefList::iterator it =
|
---|
4767 | std::find_if(m->backRefs.begin(), m->backRefs.end(),
|
---|
4768 | BackRef::EqualsTo(aMachineId));
|
---|
4769 | AssertReturn(it != m->backRefs.end(), E_FAIL);
|
---|
4770 |
|
---|
4771 | if (aSnapshotId.isZero())
|
---|
4772 | {
|
---|
4773 | it->iRefCnt--;
|
---|
4774 | if (it->iRefCnt > 0)
|
---|
4775 | return S_OK;
|
---|
4776 |
|
---|
4777 | /* remove the current state attachment */
|
---|
4778 | it->fInCurState = false;
|
---|
4779 | }
|
---|
4780 | else
|
---|
4781 | {
|
---|
4782 | /* remove the snapshot attachment */
|
---|
4783 | std::list<SnapshotRef>::iterator jt =
|
---|
4784 | std::find_if(it->llSnapshotIds.begin(),
|
---|
4785 | it->llSnapshotIds.end(),
|
---|
4786 | SnapshotRef::EqualsTo(aSnapshotId));
|
---|
4787 |
|
---|
4788 | AssertReturn(jt != it->llSnapshotIds.end(), E_FAIL);
|
---|
4789 |
|
---|
4790 | jt->iRefCnt--;
|
---|
4791 | if (jt->iRefCnt > 0)
|
---|
4792 | return S_OK;
|
---|
4793 |
|
---|
4794 | it->llSnapshotIds.erase(jt);
|
---|
4795 | }
|
---|
4796 |
|
---|
4797 | /* if the backref becomes empty, remove it */
|
---|
4798 | if (it->fInCurState == false && it->llSnapshotIds.size() == 0)
|
---|
4799 | m->backRefs.erase(it);
|
---|
4800 |
|
---|
4801 | return S_OK;
|
---|
4802 | }
|
---|
4803 |
|
---|
4804 | /**
|
---|
4805 | * Internal method to return the medium's list of backrefs. Must have caller + locking!
|
---|
4806 | * @return
|
---|
4807 | */
|
---|
4808 | const Guid* Medium::i_getFirstMachineBackrefId() const
|
---|
4809 | {
|
---|
4810 | if (!m->backRefs.size())
|
---|
4811 | return NULL;
|
---|
4812 |
|
---|
4813 | return &m->backRefs.front().machineId;
|
---|
4814 | }
|
---|
4815 |
|
---|
4816 | /**
|
---|
4817 | * Internal method which returns a machine that either this medium or one of its children
|
---|
4818 | * is attached to. This is used for finding a replacement media registry when an existing
|
---|
4819 | * media registry is about to be deleted in VirtualBox::unregisterMachine().
|
---|
4820 | *
|
---|
4821 | * Must have caller + locking, *and* caller must hold the media tree lock!
|
---|
4822 | * @param aId Id to ignore when looking for backrefs.
|
---|
4823 | * @return
|
---|
4824 | */
|
---|
4825 | const Guid* Medium::i_getAnyMachineBackref(const Guid &aId) const
|
---|
4826 | {
|
---|
4827 | std::list<const Medium *> llMediaTodo;
|
---|
4828 | llMediaTodo.push_back(this);
|
---|
4829 |
|
---|
4830 | while (!llMediaTodo.empty())
|
---|
4831 | {
|
---|
4832 | const Medium *pMedium = llMediaTodo.front();
|
---|
4833 | llMediaTodo.pop_front();
|
---|
4834 |
|
---|
4835 | if (pMedium->m->backRefs.size())
|
---|
4836 | {
|
---|
4837 | if (pMedium->m->backRefs.front().machineId != aId)
|
---|
4838 | return &pMedium->m->backRefs.front().machineId;
|
---|
4839 | if (pMedium->m->backRefs.size() > 1)
|
---|
4840 | {
|
---|
4841 | BackRefList::const_iterator it = pMedium->m->backRefs.begin();
|
---|
4842 | ++it;
|
---|
4843 | return &it->machineId;
|
---|
4844 | }
|
---|
4845 | }
|
---|
4846 |
|
---|
4847 | MediaList::const_iterator itBegin = pMedium->i_getChildren().begin();
|
---|
4848 | MediaList::const_iterator itEnd = pMedium->i_getChildren().end();
|
---|
4849 | for (MediaList::const_iterator it = itBegin; it != itEnd; ++it)
|
---|
4850 | llMediaTodo.push_back(*it);
|
---|
4851 | }
|
---|
4852 |
|
---|
4853 | return NULL;
|
---|
4854 | }
|
---|
4855 |
|
---|
4856 | const Guid* Medium::i_getFirstMachineBackrefSnapshotId() const
|
---|
4857 | {
|
---|
4858 | if (!m->backRefs.size())
|
---|
4859 | return NULL;
|
---|
4860 |
|
---|
4861 | const BackRef &ref = m->backRefs.front();
|
---|
4862 | if (ref.llSnapshotIds.empty())
|
---|
4863 | return NULL;
|
---|
4864 |
|
---|
4865 | return &ref.llSnapshotIds.front().snapshotId;
|
---|
4866 | }
|
---|
4867 |
|
---|
4868 | size_t Medium::i_getMachineBackRefCount() const
|
---|
4869 | {
|
---|
4870 | return m->backRefs.size();
|
---|
4871 | }
|
---|
4872 |
|
---|
4873 | #ifdef DEBUG
|
---|
4874 | /**
|
---|
4875 | * Debugging helper that gets called after VirtualBox initialization that writes all
|
---|
4876 | * machine backreferences to the debug log.
|
---|
4877 | */
|
---|
4878 | void Medium::i_dumpBackRefs()
|
---|
4879 | {
|
---|
4880 | AutoCaller autoCaller(this);
|
---|
4881 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4882 |
|
---|
4883 | LogFlowThisFunc(("Dumping backrefs for medium '%s':\n", m->strLocationFull.c_str()));
|
---|
4884 |
|
---|
4885 | for (BackRefList::iterator it2 = m->backRefs.begin();
|
---|
4886 | it2 != m->backRefs.end();
|
---|
4887 | ++it2)
|
---|
4888 | {
|
---|
4889 | const BackRef &ref = *it2;
|
---|
4890 | LogFlowThisFunc((" Backref from machine {%RTuuid} (fInCurState: %d, iRefCnt: %d)\n", ref.machineId.raw(), ref.fInCurState, ref.iRefCnt));
|
---|
4891 |
|
---|
4892 | for (std::list<SnapshotRef>::const_iterator jt2 = it2->llSnapshotIds.begin();
|
---|
4893 | jt2 != it2->llSnapshotIds.end();
|
---|
4894 | ++jt2)
|
---|
4895 | {
|
---|
4896 | const Guid &id = jt2->snapshotId;
|
---|
4897 | LogFlowThisFunc((" Backref from snapshot {%RTuuid} (iRefCnt = %d)\n", id.raw(), jt2->iRefCnt));
|
---|
4898 | }
|
---|
4899 | }
|
---|
4900 | }
|
---|
4901 | #endif
|
---|
4902 |
|
---|
4903 | /**
|
---|
4904 | * Checks if the given change of \a aOldPath to \a aNewPath affects the location
|
---|
4905 | * of this media and updates it if necessary to reflect the new location.
|
---|
4906 | *
|
---|
4907 | * @param strOldPath Old path (full).
|
---|
4908 | * @param strNewPath New path (full).
|
---|
4909 | *
|
---|
4910 | * @note Locks this object for writing.
|
---|
4911 | */
|
---|
4912 | HRESULT Medium::i_updatePath(const Utf8Str &strOldPath, const Utf8Str &strNewPath)
|
---|
4913 | {
|
---|
4914 | AssertReturn(!strOldPath.isEmpty(), E_FAIL);
|
---|
4915 | AssertReturn(!strNewPath.isEmpty(), E_FAIL);
|
---|
4916 |
|
---|
4917 | AutoCaller autoCaller(this);
|
---|
4918 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4919 |
|
---|
4920 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4921 |
|
---|
4922 | LogFlowThisFunc(("locationFull.before='%s'\n", m->strLocationFull.c_str()));
|
---|
4923 |
|
---|
4924 | const char *pcszMediumPath = m->strLocationFull.c_str();
|
---|
4925 |
|
---|
4926 | if (RTPathStartsWith(pcszMediumPath, strOldPath.c_str()))
|
---|
4927 | {
|
---|
4928 | Utf8Str newPath(strNewPath);
|
---|
4929 | newPath.append(pcszMediumPath + strOldPath.length());
|
---|
4930 | unconst(m->strLocationFull) = newPath;
|
---|
4931 |
|
---|
4932 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
4933 |
|
---|
4934 | LogFlowThisFunc(("locationFull.after='%s'\n", m->strLocationFull.c_str()));
|
---|
4935 | // we changed something
|
---|
4936 | return S_OK;
|
---|
4937 | }
|
---|
4938 |
|
---|
4939 | // no change was necessary, signal error which the caller needs to interpret
|
---|
4940 | return VBOX_E_FILE_ERROR;
|
---|
4941 | }
|
---|
4942 |
|
---|
4943 | /**
|
---|
4944 | * Returns the base medium of the media chain this medium is part of.
|
---|
4945 | *
|
---|
4946 | * The base medium is found by walking up the parent-child relationship axis.
|
---|
4947 | * If the medium doesn't have a parent (i.e. it's a base medium), it
|
---|
4948 | * returns itself in response to this method.
|
---|
4949 | *
|
---|
4950 | * @param aLevel Where to store the number of ancestors of this medium
|
---|
4951 | * (zero for the base), may be @c NULL.
|
---|
4952 | *
|
---|
4953 | * @note Locks medium tree for reading.
|
---|
4954 | */
|
---|
4955 | ComObjPtr<Medium> Medium::i_getBase(uint32_t *aLevel /*= NULL*/)
|
---|
4956 | {
|
---|
4957 | ComObjPtr<Medium> pBase;
|
---|
4958 |
|
---|
4959 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4960 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4961 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4962 | if (!pVirtualBox)
|
---|
4963 | return pBase;
|
---|
4964 |
|
---|
4965 | /* we access m->pParent */
|
---|
4966 | AutoReadLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4967 |
|
---|
4968 | AutoCaller autoCaller(this);
|
---|
4969 | AssertReturn(autoCaller.isOk(), pBase);
|
---|
4970 |
|
---|
4971 | pBase = this;
|
---|
4972 | uint32_t level = 0;
|
---|
4973 |
|
---|
4974 | if (m->pParent)
|
---|
4975 | {
|
---|
4976 | for (;;)
|
---|
4977 | {
|
---|
4978 | AutoCaller baseCaller(pBase);
|
---|
4979 | AssertReturn(baseCaller.isOk(), pBase);
|
---|
4980 |
|
---|
4981 | if (pBase->m->pParent.isNull())
|
---|
4982 | break;
|
---|
4983 |
|
---|
4984 | pBase = pBase->m->pParent;
|
---|
4985 | ++level;
|
---|
4986 | }
|
---|
4987 | }
|
---|
4988 |
|
---|
4989 | if (aLevel != NULL)
|
---|
4990 | *aLevel = level;
|
---|
4991 |
|
---|
4992 | return pBase;
|
---|
4993 | }
|
---|
4994 |
|
---|
4995 | /**
|
---|
4996 | * Returns the depth of this medium in the media chain.
|
---|
4997 | *
|
---|
4998 | * @note Locks medium tree for reading.
|
---|
4999 | */
|
---|
5000 | uint32_t Medium::i_getDepth()
|
---|
5001 | {
|
---|
5002 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
5003 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
5004 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
5005 | if (!pVirtualBox)
|
---|
5006 | return 1;
|
---|
5007 |
|
---|
5008 | /* we access m->pParent */
|
---|
5009 | AutoReadLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5010 |
|
---|
5011 | uint32_t cDepth = 0;
|
---|
5012 | ComObjPtr<Medium> pMedium(this);
|
---|
5013 | while (!pMedium.isNull())
|
---|
5014 | {
|
---|
5015 | AutoCaller autoCaller(this);
|
---|
5016 | AssertReturn(autoCaller.isOk(), cDepth + 1);
|
---|
5017 |
|
---|
5018 | pMedium = pMedium->m->pParent;
|
---|
5019 | cDepth++;
|
---|
5020 | }
|
---|
5021 |
|
---|
5022 | return cDepth;
|
---|
5023 | }
|
---|
5024 |
|
---|
5025 | /**
|
---|
5026 | * Returns @c true if this medium cannot be modified because it has
|
---|
5027 | * dependents (children) or is part of the snapshot. Related to the medium
|
---|
5028 | * type and posterity, not to the current media state.
|
---|
5029 | *
|
---|
5030 | * @note Locks this object and medium tree for reading.
|
---|
5031 | */
|
---|
5032 | bool Medium::i_isReadOnly()
|
---|
5033 | {
|
---|
5034 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
5035 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
5036 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
5037 | if (!pVirtualBox)
|
---|
5038 | return false;
|
---|
5039 |
|
---|
5040 | /* we access children */
|
---|
5041 | AutoReadLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5042 |
|
---|
5043 | AutoCaller autoCaller(this);
|
---|
5044 | AssertComRCReturn(autoCaller.rc(), false);
|
---|
5045 |
|
---|
5046 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5047 |
|
---|
5048 | switch (m->type)
|
---|
5049 | {
|
---|
5050 | case MediumType_Normal:
|
---|
5051 | {
|
---|
5052 | if (i_getChildren().size() != 0)
|
---|
5053 | return true;
|
---|
5054 |
|
---|
5055 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
5056 | it != m->backRefs.end(); ++it)
|
---|
5057 | if (it->llSnapshotIds.size() != 0)
|
---|
5058 | return true;
|
---|
5059 |
|
---|
5060 | if (m->variant & MediumVariant_VmdkStreamOptimized)
|
---|
5061 | return true;
|
---|
5062 |
|
---|
5063 | return false;
|
---|
5064 | }
|
---|
5065 | case MediumType_Immutable:
|
---|
5066 | case MediumType_MultiAttach:
|
---|
5067 | return true;
|
---|
5068 | case MediumType_Writethrough:
|
---|
5069 | case MediumType_Shareable:
|
---|
5070 | case MediumType_Readonly: /* explicit readonly media has no diffs */
|
---|
5071 | return false;
|
---|
5072 | default:
|
---|
5073 | break;
|
---|
5074 | }
|
---|
5075 |
|
---|
5076 | AssertFailedReturn(false);
|
---|
5077 | }
|
---|
5078 |
|
---|
5079 | /**
|
---|
5080 | * Internal method to update the medium's id. Must have caller + locking!
|
---|
5081 | * @return
|
---|
5082 | */
|
---|
5083 | void Medium::i_updateId(const Guid &id)
|
---|
5084 | {
|
---|
5085 | unconst(m->id) = id;
|
---|
5086 | }
|
---|
5087 |
|
---|
5088 | /**
|
---|
5089 | * Saves the settings of one medium.
|
---|
5090 | *
|
---|
5091 | * @note Caller MUST take care of the medium tree lock and caller.
|
---|
5092 | *
|
---|
5093 | * @param data Settings struct to be updated.
|
---|
5094 | * @param strHardDiskFolder Folder for which paths should be relative.
|
---|
5095 | */
|
---|
5096 | void Medium::i_saveSettingsOne(settings::Medium &data, const Utf8Str &strHardDiskFolder)
|
---|
5097 | {
|
---|
5098 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5099 |
|
---|
5100 | data.uuid = m->id;
|
---|
5101 |
|
---|
5102 | // make path relative if needed
|
---|
5103 | if ( !strHardDiskFolder.isEmpty()
|
---|
5104 | && RTPathStartsWith(m->strLocationFull.c_str(), strHardDiskFolder.c_str())
|
---|
5105 | )
|
---|
5106 | data.strLocation = m->strLocationFull.substr(strHardDiskFolder.length() + 1);
|
---|
5107 | else
|
---|
5108 | data.strLocation = m->strLocationFull;
|
---|
5109 | data.strFormat = m->strFormat;
|
---|
5110 |
|
---|
5111 | /* optional, only for diffs, default is false */
|
---|
5112 | if (m->pParent)
|
---|
5113 | data.fAutoReset = m->autoReset;
|
---|
5114 | else
|
---|
5115 | data.fAutoReset = false;
|
---|
5116 |
|
---|
5117 | /* optional */
|
---|
5118 | data.strDescription = m->strDescription;
|
---|
5119 |
|
---|
5120 | /* optional properties */
|
---|
5121 | data.properties.clear();
|
---|
5122 |
|
---|
5123 | /* handle iSCSI initiator secrets transparently */
|
---|
5124 | bool fHaveInitiatorSecretEncrypted = false;
|
---|
5125 | Utf8Str strCiphertext;
|
---|
5126 | settings::StringsMap::const_iterator itPln = m->mapProperties.find("InitiatorSecret");
|
---|
5127 | if ( itPln != m->mapProperties.end()
|
---|
5128 | && !itPln->second.isEmpty())
|
---|
5129 | {
|
---|
5130 | /* Encrypt the plain secret. If that does not work (i.e. no or wrong settings key
|
---|
5131 | * specified), just use the encrypted secret (if there is any). */
|
---|
5132 | int rc = m->pVirtualBox->i_encryptSetting(itPln->second, &strCiphertext);
|
---|
5133 | if (RT_SUCCESS(rc))
|
---|
5134 | fHaveInitiatorSecretEncrypted = true;
|
---|
5135 | }
|
---|
5136 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
5137 | it != m->mapProperties.end();
|
---|
5138 | ++it)
|
---|
5139 | {
|
---|
5140 | /* only save properties that have non-default values */
|
---|
5141 | if (!it->second.isEmpty())
|
---|
5142 | {
|
---|
5143 | const Utf8Str &name = it->first;
|
---|
5144 | const Utf8Str &value = it->second;
|
---|
5145 | bool fCreateOnly = false;
|
---|
5146 | for (MediumFormat::PropertyArray::const_iterator itf = m->formatObj->i_getProperties().begin();
|
---|
5147 | itf != m->formatObj->i_getProperties().end();
|
---|
5148 | ++itf)
|
---|
5149 | {
|
---|
5150 | if ( itf->strName.equals(name)
|
---|
5151 | && (itf->flags & VD_CFGKEY_CREATEONLY))
|
---|
5152 | {
|
---|
5153 | fCreateOnly = true;
|
---|
5154 | break;
|
---|
5155 | }
|
---|
5156 | }
|
---|
5157 | if (!fCreateOnly)
|
---|
5158 | /* do NOT store the plain InitiatorSecret */
|
---|
5159 | if ( !fHaveInitiatorSecretEncrypted
|
---|
5160 | || !name.equals("InitiatorSecret"))
|
---|
5161 | data.properties[name] = value;
|
---|
5162 | }
|
---|
5163 | }
|
---|
5164 | if (fHaveInitiatorSecretEncrypted)
|
---|
5165 | data.properties["InitiatorSecretEncrypted"] = strCiphertext;
|
---|
5166 |
|
---|
5167 | /* only for base media */
|
---|
5168 | if (m->pParent.isNull())
|
---|
5169 | data.hdType = m->type;
|
---|
5170 | }
|
---|
5171 |
|
---|
5172 | /**
|
---|
5173 | * Saves medium data by putting it into the provided data structure.
|
---|
5174 | * The settings of all children is saved, too.
|
---|
5175 | *
|
---|
5176 | * @param data Settings struct to be updated.
|
---|
5177 | * @param strHardDiskFolder Folder for which paths should be relative.
|
---|
5178 | *
|
---|
5179 | * @note Locks this object, medium tree and children for reading.
|
---|
5180 | */
|
---|
5181 | HRESULT Medium::i_saveSettings(settings::Medium &data,
|
---|
5182 | const Utf8Str &strHardDiskFolder)
|
---|
5183 | {
|
---|
5184 | /* we access m->pParent */
|
---|
5185 | AutoReadLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5186 |
|
---|
5187 | AutoCaller autoCaller(this);
|
---|
5188 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5189 |
|
---|
5190 | MediaList llMediaTodo;
|
---|
5191 | llMediaTodo.push_back(this);
|
---|
5192 | std::list<settings::Medium *> llSettingsTodo;
|
---|
5193 | llSettingsTodo.push_back(&data);
|
---|
5194 |
|
---|
5195 | while (!llMediaTodo.empty())
|
---|
5196 | {
|
---|
5197 | ComObjPtr<Medium> pMedium = llMediaTodo.front();
|
---|
5198 | llMediaTodo.pop_front();
|
---|
5199 | settings::Medium *current = llSettingsTodo.front();
|
---|
5200 | llSettingsTodo.pop_front();
|
---|
5201 |
|
---|
5202 | AutoCaller mediumCaller(pMedium);
|
---|
5203 | if (FAILED(mediumCaller.rc())) return mediumCaller.rc();
|
---|
5204 |
|
---|
5205 | pMedium->i_saveSettingsOne(*current, strHardDiskFolder);
|
---|
5206 |
|
---|
5207 | /* save all children */
|
---|
5208 | MediaList::const_iterator itBegin = pMedium->i_getChildren().begin();
|
---|
5209 | MediaList::const_iterator itEnd = pMedium->i_getChildren().end();
|
---|
5210 | for (MediaList::const_iterator it = itBegin; it != itEnd; ++it)
|
---|
5211 | {
|
---|
5212 | llMediaTodo.push_back(*it);
|
---|
5213 | current->llChildren.push_back(settings::Medium::Empty);
|
---|
5214 | llSettingsTodo.push_back(¤t->llChildren.back());
|
---|
5215 | }
|
---|
5216 | }
|
---|
5217 |
|
---|
5218 | return S_OK;
|
---|
5219 | }
|
---|
5220 |
|
---|
5221 | /**
|
---|
5222 | * Constructs a medium lock list for this medium. The lock is not taken.
|
---|
5223 | *
|
---|
5224 | * @note Caller MUST NOT hold the media tree or medium lock.
|
---|
5225 | *
|
---|
5226 | * @param fFailIfInaccessible If true, this fails with an error if a medium is inaccessible. If false,
|
---|
5227 | * inaccessible media are silently skipped and not locked (i.e. their state remains "Inaccessible");
|
---|
5228 | * this is necessary for a VM's removable media VM startup for which we do not want to fail.
|
---|
5229 | * @param pToLockWrite If not NULL, associate a write lock with this medium object.
|
---|
5230 | * @param fMediumLockWriteAll Whether to associate a write lock to all other media too.
|
---|
5231 | * @param pToBeParent Medium which will become the parent of this medium.
|
---|
5232 | * @param mediumLockList Where to store the resulting list.
|
---|
5233 | */
|
---|
5234 | HRESULT Medium::i_createMediumLockList(bool fFailIfInaccessible,
|
---|
5235 | Medium *pToLockWrite,
|
---|
5236 | bool fMediumLockWriteAll,
|
---|
5237 | Medium *pToBeParent,
|
---|
5238 | MediumLockList &mediumLockList)
|
---|
5239 | {
|
---|
5240 | /** @todo r=klaus this needs to be reworked, as the code below uses
|
---|
5241 | * i_getParent without holding the tree lock, and changing this is
|
---|
5242 | * a significant amount of effort. */
|
---|
5243 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
5244 | Assert(!isWriteLockOnCurrentThread());
|
---|
5245 |
|
---|
5246 | AutoCaller autoCaller(this);
|
---|
5247 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5248 |
|
---|
5249 | HRESULT rc = S_OK;
|
---|
5250 |
|
---|
5251 | /* paranoid sanity checking if the medium has a to-be parent medium */
|
---|
5252 | if (pToBeParent)
|
---|
5253 | {
|
---|
5254 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5255 | ComAssertRet(i_getParent().isNull(), E_FAIL);
|
---|
5256 | ComAssertRet(i_getChildren().size() == 0, E_FAIL);
|
---|
5257 | }
|
---|
5258 |
|
---|
5259 | ErrorInfoKeeper eik;
|
---|
5260 | MultiResult mrc(S_OK);
|
---|
5261 |
|
---|
5262 | ComObjPtr<Medium> pMedium = this;
|
---|
5263 | while (!pMedium.isNull())
|
---|
5264 | {
|
---|
5265 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
5266 |
|
---|
5267 | /* Accessibility check must be first, otherwise locking interferes
|
---|
5268 | * with getting the medium state. Lock lists are not created for
|
---|
5269 | * fun, and thus getting the medium status is no luxury. */
|
---|
5270 | MediumState_T mediumState = pMedium->i_getState();
|
---|
5271 | if (mediumState == MediumState_Inaccessible)
|
---|
5272 | {
|
---|
5273 | alock.release();
|
---|
5274 | rc = pMedium->i_queryInfo(false /* fSetImageId */, false /* fSetParentId */,
|
---|
5275 | autoCaller);
|
---|
5276 | alock.acquire();
|
---|
5277 | if (FAILED(rc)) return rc;
|
---|
5278 |
|
---|
5279 | mediumState = pMedium->i_getState();
|
---|
5280 | if (mediumState == MediumState_Inaccessible)
|
---|
5281 | {
|
---|
5282 | // ignore inaccessible ISO media and silently return S_OK,
|
---|
5283 | // otherwise VM startup (esp. restore) may fail without good reason
|
---|
5284 | if (!fFailIfInaccessible)
|
---|
5285 | return S_OK;
|
---|
5286 |
|
---|
5287 | // otherwise report an error
|
---|
5288 | Bstr error;
|
---|
5289 | rc = pMedium->COMGETTER(LastAccessError)(error.asOutParam());
|
---|
5290 | if (FAILED(rc)) return rc;
|
---|
5291 |
|
---|
5292 | /* collect multiple errors */
|
---|
5293 | eik.restore();
|
---|
5294 | Assert(!error.isEmpty());
|
---|
5295 | mrc = setError(E_FAIL,
|
---|
5296 | "%ls",
|
---|
5297 | error.raw());
|
---|
5298 | // error message will be something like
|
---|
5299 | // "Could not open the medium ... VD: error VERR_FILE_NOT_FOUND opening image file ... (VERR_FILE_NOT_FOUND).
|
---|
5300 | eik.fetch();
|
---|
5301 | }
|
---|
5302 | }
|
---|
5303 |
|
---|
5304 | if (pMedium == pToLockWrite)
|
---|
5305 | mediumLockList.Prepend(pMedium, true);
|
---|
5306 | else
|
---|
5307 | mediumLockList.Prepend(pMedium, fMediumLockWriteAll);
|
---|
5308 |
|
---|
5309 | pMedium = pMedium->i_getParent();
|
---|
5310 | if (pMedium.isNull() && pToBeParent)
|
---|
5311 | {
|
---|
5312 | pMedium = pToBeParent;
|
---|
5313 | pToBeParent = NULL;
|
---|
5314 | }
|
---|
5315 | }
|
---|
5316 |
|
---|
5317 | return mrc;
|
---|
5318 | }
|
---|
5319 |
|
---|
5320 | /**
|
---|
5321 | * Creates a new differencing storage unit using the format of the given target
|
---|
5322 | * medium and the location. Note that @c aTarget must be NotCreated.
|
---|
5323 | *
|
---|
5324 | * The @a aMediumLockList parameter contains the associated medium lock list,
|
---|
5325 | * which must be in locked state. If @a aWait is @c true then the caller is
|
---|
5326 | * responsible for unlocking.
|
---|
5327 | *
|
---|
5328 | * If @a aProgress is not NULL but the object it points to is @c null then a
|
---|
5329 | * new progress object will be created and assigned to @a *aProgress on
|
---|
5330 | * success, otherwise the existing progress object is used. If @a aProgress is
|
---|
5331 | * NULL, then no progress object is created/used at all.
|
---|
5332 | *
|
---|
5333 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
5334 | * create operation asynchronously and will return immediately. Otherwise, it
|
---|
5335 | * will perform the operation on the calling thread and will not return to the
|
---|
5336 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
5337 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
5338 | *
|
---|
5339 | * @param aTarget Target medium.
|
---|
5340 | * @param aVariant Precise medium variant to create.
|
---|
5341 | * @param aMediumLockList List of media which should be locked.
|
---|
5342 | * @param aProgress Where to find/store a Progress object to track
|
---|
5343 | * operation completion.
|
---|
5344 | * @param aWait @c true if this method should block instead of
|
---|
5345 | * creating an asynchronous thread.
|
---|
5346 | * @param aNotify Notify about mediums which metadatа are changed
|
---|
5347 | * during execution of the function.
|
---|
5348 | *
|
---|
5349 | * @note Locks this object and @a aTarget for writing.
|
---|
5350 | */
|
---|
5351 | HRESULT Medium::i_createDiffStorage(ComObjPtr<Medium> &aTarget,
|
---|
5352 | MediumVariant_T aVariant,
|
---|
5353 | MediumLockList *aMediumLockList,
|
---|
5354 | ComObjPtr<Progress> *aProgress,
|
---|
5355 | bool aWait,
|
---|
5356 | bool aNotify)
|
---|
5357 | {
|
---|
5358 | AssertReturn(!aTarget.isNull(), E_FAIL);
|
---|
5359 | AssertReturn(aMediumLockList, E_FAIL);
|
---|
5360 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
5361 |
|
---|
5362 | AutoCaller autoCaller(this);
|
---|
5363 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5364 |
|
---|
5365 | AutoCaller targetCaller(aTarget);
|
---|
5366 | if (FAILED(targetCaller.rc())) return targetCaller.rc();
|
---|
5367 |
|
---|
5368 | HRESULT rc = S_OK;
|
---|
5369 | ComObjPtr<Progress> pProgress;
|
---|
5370 | Medium::Task *pTask = NULL;
|
---|
5371 |
|
---|
5372 | try
|
---|
5373 | {
|
---|
5374 | AutoMultiWriteLock2 alock(this, aTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5375 |
|
---|
5376 | ComAssertThrow( m->type != MediumType_Writethrough
|
---|
5377 | && m->type != MediumType_Shareable
|
---|
5378 | && m->type != MediumType_Readonly, E_FAIL);
|
---|
5379 | ComAssertThrow(m->state == MediumState_LockedRead, E_FAIL);
|
---|
5380 |
|
---|
5381 | if (aTarget->m->state != MediumState_NotCreated)
|
---|
5382 | throw aTarget->i_setStateError();
|
---|
5383 |
|
---|
5384 | /* Check that the medium is not attached to the current state of
|
---|
5385 | * any VM referring to it. */
|
---|
5386 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
5387 | it != m->backRefs.end();
|
---|
5388 | ++it)
|
---|
5389 | {
|
---|
5390 | if (it->fInCurState)
|
---|
5391 | {
|
---|
5392 | /* Note: when a VM snapshot is being taken, all normal media
|
---|
5393 | * attached to the VM in the current state will be, as an
|
---|
5394 | * exception, also associated with the snapshot which is about
|
---|
5395 | * to create (see SnapshotMachine::init()) before deassociating
|
---|
5396 | * them from the current state (which takes place only on
|
---|
5397 | * success in Machine::fixupHardDisks()), so that the size of
|
---|
5398 | * snapshotIds will be 1 in this case. The extra condition is
|
---|
5399 | * used to filter out this legal situation. */
|
---|
5400 | if (it->llSnapshotIds.size() == 0)
|
---|
5401 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5402 | tr("Medium '%s' is attached to a virtual machine with UUID {%RTuuid}. No differencing media based on it may be created until it is detached"),
|
---|
5403 | m->strLocationFull.c_str(), it->machineId.raw());
|
---|
5404 |
|
---|
5405 | Assert(it->llSnapshotIds.size() == 1);
|
---|
5406 | }
|
---|
5407 | }
|
---|
5408 |
|
---|
5409 | if (aProgress != NULL)
|
---|
5410 | {
|
---|
5411 | /* use the existing progress object... */
|
---|
5412 | pProgress = *aProgress;
|
---|
5413 |
|
---|
5414 | /* ...but create a new one if it is null */
|
---|
5415 | if (pProgress.isNull())
|
---|
5416 | {
|
---|
5417 | pProgress.createObject();
|
---|
5418 | rc = pProgress->init(m->pVirtualBox,
|
---|
5419 | static_cast<IMedium*>(this),
|
---|
5420 | BstrFmt(tr("Creating differencing medium storage unit '%s'"),
|
---|
5421 | aTarget->m->strLocationFull.c_str()).raw(),
|
---|
5422 | TRUE /* aCancelable */);
|
---|
5423 | if (FAILED(rc))
|
---|
5424 | throw rc;
|
---|
5425 | }
|
---|
5426 | }
|
---|
5427 |
|
---|
5428 | /* setup task object to carry out the operation sync/async */
|
---|
5429 | pTask = new Medium::CreateDiffTask(this, pProgress, aTarget, aVariant,
|
---|
5430 | aMediumLockList,
|
---|
5431 | aWait /* fKeepMediumLockList */,
|
---|
5432 | aNotify);
|
---|
5433 | rc = pTask->rc();
|
---|
5434 | AssertComRC(rc);
|
---|
5435 | if (FAILED(rc))
|
---|
5436 | throw rc;
|
---|
5437 |
|
---|
5438 | /* register a task (it will deregister itself when done) */
|
---|
5439 | ++m->numCreateDiffTasks;
|
---|
5440 | Assert(m->numCreateDiffTasks != 0); /* overflow? */
|
---|
5441 |
|
---|
5442 | aTarget->m->state = MediumState_Creating;
|
---|
5443 | }
|
---|
5444 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5445 |
|
---|
5446 | if (SUCCEEDED(rc))
|
---|
5447 | {
|
---|
5448 | if (aWait)
|
---|
5449 | {
|
---|
5450 | rc = pTask->runNow();
|
---|
5451 | delete pTask;
|
---|
5452 | }
|
---|
5453 | else
|
---|
5454 | rc = pTask->createThread();
|
---|
5455 | pTask = NULL;
|
---|
5456 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
5457 | *aProgress = pProgress;
|
---|
5458 | }
|
---|
5459 | else if (pTask != NULL)
|
---|
5460 | delete pTask;
|
---|
5461 |
|
---|
5462 | return rc;
|
---|
5463 | }
|
---|
5464 |
|
---|
5465 | /**
|
---|
5466 | * Returns a preferred format for differencing media.
|
---|
5467 | */
|
---|
5468 | Utf8Str Medium::i_getPreferredDiffFormat()
|
---|
5469 | {
|
---|
5470 | AutoCaller autoCaller(this);
|
---|
5471 | AssertComRCReturn(autoCaller.rc(), Utf8Str::Empty);
|
---|
5472 |
|
---|
5473 | /* check that our own format supports diffs */
|
---|
5474 | if (!(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_Differencing))
|
---|
5475 | {
|
---|
5476 | /* use the default format if not */
|
---|
5477 | Utf8Str tmp;
|
---|
5478 | m->pVirtualBox->i_getDefaultHardDiskFormat(tmp);
|
---|
5479 | return tmp;
|
---|
5480 | }
|
---|
5481 |
|
---|
5482 | /* m->strFormat is const, no need to lock */
|
---|
5483 | return m->strFormat;
|
---|
5484 | }
|
---|
5485 |
|
---|
5486 | /**
|
---|
5487 | * Returns a preferred variant for differencing media.
|
---|
5488 | */
|
---|
5489 | MediumVariant_T Medium::i_getPreferredDiffVariant()
|
---|
5490 | {
|
---|
5491 | AutoCaller autoCaller(this);
|
---|
5492 | AssertComRCReturn(autoCaller.rc(), MediumVariant_Standard);
|
---|
5493 |
|
---|
5494 | /* check that our own format supports diffs */
|
---|
5495 | if (!(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_Differencing))
|
---|
5496 | return MediumVariant_Standard;
|
---|
5497 |
|
---|
5498 | /* m->variant is const, no need to lock */
|
---|
5499 | ULONG mediumVariantFlags = (ULONG)m->variant;
|
---|
5500 | mediumVariantFlags &= ~(ULONG)(MediumVariant_Fixed | MediumVariant_VmdkStreamOptimized);
|
---|
5501 | mediumVariantFlags |= MediumVariant_Diff;
|
---|
5502 | return (MediumVariant_T)mediumVariantFlags;
|
---|
5503 | }
|
---|
5504 |
|
---|
5505 | /**
|
---|
5506 | * Implementation for the public Medium::Close() with the exception of calling
|
---|
5507 | * VirtualBox::saveRegistries(), in case someone wants to call this for several
|
---|
5508 | * media.
|
---|
5509 | *
|
---|
5510 | * After this returns with success, uninit() has been called on the medium, and
|
---|
5511 | * the object is no longer usable ("not ready" state).
|
---|
5512 | *
|
---|
5513 | * @param autoCaller AutoCaller instance which must have been created on the caller's
|
---|
5514 | * stack for this medium. This gets released hereupon
|
---|
5515 | * which the Medium instance gets uninitialized.
|
---|
5516 | * @return
|
---|
5517 | */
|
---|
5518 | HRESULT Medium::i_close(AutoCaller &autoCaller)
|
---|
5519 | {
|
---|
5520 | // must temporarily drop the caller, need the tree lock first
|
---|
5521 | autoCaller.release();
|
---|
5522 |
|
---|
5523 | // we're accessing parent/child and backrefs, so lock the tree first, then ourselves
|
---|
5524 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
5525 | this->lockHandle()
|
---|
5526 | COMMA_LOCKVAL_SRC_POS);
|
---|
5527 |
|
---|
5528 | autoCaller.add();
|
---|
5529 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5530 |
|
---|
5531 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
5532 | while (m->queryInfoRunning)
|
---|
5533 | {
|
---|
5534 | autoCaller.release();
|
---|
5535 | multilock.release();
|
---|
5536 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs
|
---|
5537 | * this lock and thus we would run into a deadlock here. */
|
---|
5538 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
5539 | /* must not hold the object lock now */
|
---|
5540 | Assert(!isWriteLockOnCurrentThread());
|
---|
5541 | {
|
---|
5542 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
5543 | }
|
---|
5544 | multilock.acquire();
|
---|
5545 | autoCaller.add();
|
---|
5546 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5547 | }
|
---|
5548 |
|
---|
5549 | LogFlowFunc(("ENTER for %s\n", i_getLocationFull().c_str()));
|
---|
5550 |
|
---|
5551 | bool wasCreated = true;
|
---|
5552 |
|
---|
5553 | switch (m->state)
|
---|
5554 | {
|
---|
5555 | case MediumState_NotCreated:
|
---|
5556 | wasCreated = false;
|
---|
5557 | break;
|
---|
5558 | case MediumState_Created:
|
---|
5559 | case MediumState_Inaccessible:
|
---|
5560 | break;
|
---|
5561 | default:
|
---|
5562 | return i_setStateError();
|
---|
5563 | }
|
---|
5564 |
|
---|
5565 | if (m->backRefs.size() != 0)
|
---|
5566 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
5567 | tr("Medium '%s' cannot be closed because it is still attached to %d virtual machines", "",
|
---|
5568 | m->backRefs.size()),
|
---|
5569 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
5570 |
|
---|
5571 | // perform extra media-dependent close checks
|
---|
5572 | HRESULT rc = i_canClose();
|
---|
5573 | if (FAILED(rc)) return rc;
|
---|
5574 |
|
---|
5575 | m->fClosing = true;
|
---|
5576 |
|
---|
5577 | if (wasCreated)
|
---|
5578 | {
|
---|
5579 | // remove from the list of known media before performing actual
|
---|
5580 | // uninitialization (to keep the media registry consistent on
|
---|
5581 | // failure to do so)
|
---|
5582 | rc = i_unregisterWithVirtualBox();
|
---|
5583 | if (FAILED(rc)) return rc;
|
---|
5584 |
|
---|
5585 | multilock.release();
|
---|
5586 | // Release the AutoCaller now, as otherwise uninit() will simply hang.
|
---|
5587 | // Needs to be done before mark the registries as modified and saving
|
---|
5588 | // the registry, as otherwise there may be a deadlock with someone else
|
---|
5589 | // closing this object while we're in i_saveModifiedRegistries(), which
|
---|
5590 | // needs the media tree lock, which the other thread holds until after
|
---|
5591 | // uninit() below.
|
---|
5592 | autoCaller.release();
|
---|
5593 | i_markRegistriesModified();
|
---|
5594 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
5595 | }
|
---|
5596 | else
|
---|
5597 | {
|
---|
5598 | multilock.release();
|
---|
5599 | // release the AutoCaller, as otherwise uninit() will simply hang
|
---|
5600 | autoCaller.release();
|
---|
5601 | }
|
---|
5602 |
|
---|
5603 | // Keep the locks held until after uninit, as otherwise the consistency
|
---|
5604 | // of the medium tree cannot be guaranteed.
|
---|
5605 | uninit();
|
---|
5606 |
|
---|
5607 | LogFlowFuncLeave();
|
---|
5608 |
|
---|
5609 | return rc;
|
---|
5610 | }
|
---|
5611 |
|
---|
5612 | /**
|
---|
5613 | * Deletes the medium storage unit.
|
---|
5614 | *
|
---|
5615 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
5616 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
5617 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
5618 | * progress object is created/used at all.
|
---|
5619 | *
|
---|
5620 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
5621 | * delete operation asynchronously and will return immediately. Otherwise, it
|
---|
5622 | * will perform the operation on the calling thread and will not return to the
|
---|
5623 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
5624 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
5625 | *
|
---|
5626 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
5627 | * completion.
|
---|
5628 | * @param aWait @c true if this method should block instead of creating
|
---|
5629 | * an asynchronous thread.
|
---|
5630 | * @param aNotify Notify about mediums which metadatа are changed
|
---|
5631 | * during execution of the function.
|
---|
5632 | *
|
---|
5633 | * @note Locks mVirtualBox and this object for writing. Locks medium tree for
|
---|
5634 | * writing.
|
---|
5635 | */
|
---|
5636 | HRESULT Medium::i_deleteStorage(ComObjPtr<Progress> *aProgress,
|
---|
5637 | bool aWait, bool aNotify)
|
---|
5638 | {
|
---|
5639 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
5640 |
|
---|
5641 | HRESULT rc = S_OK;
|
---|
5642 | ComObjPtr<Progress> pProgress;
|
---|
5643 | Medium::Task *pTask = NULL;
|
---|
5644 |
|
---|
5645 | try
|
---|
5646 | {
|
---|
5647 | /* we're accessing the media tree, and i_canClose() needs it too */
|
---|
5648 | AutoWriteLock treelock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5649 |
|
---|
5650 | AutoCaller autoCaller(this);
|
---|
5651 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5652 |
|
---|
5653 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5654 |
|
---|
5655 | LogFlowThisFunc(("aWait=%RTbool locationFull=%s\n", aWait, i_getLocationFull().c_str() ));
|
---|
5656 |
|
---|
5657 | if ( !(m->formatObj->i_getCapabilities() & ( MediumFormatCapabilities_CreateDynamic
|
---|
5658 | | MediumFormatCapabilities_CreateFixed)))
|
---|
5659 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
5660 | tr("Medium format '%s' does not support storage deletion"),
|
---|
5661 | m->strFormat.c_str());
|
---|
5662 |
|
---|
5663 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
5664 | /** @todo r=klaus would be great if this could be moved to the async
|
---|
5665 | * part of the operation as it can take quite a while */
|
---|
5666 | while (m->queryInfoRunning)
|
---|
5667 | {
|
---|
5668 | alock.release();
|
---|
5669 | autoCaller.release();
|
---|
5670 | treelock.release();
|
---|
5671 | /* Must not hold the media tree lock or the object lock, as
|
---|
5672 | * Medium::i_queryInfo needs this lock and thus we would run
|
---|
5673 | * into a deadlock here. */
|
---|
5674 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
5675 | Assert(!isWriteLockOnCurrentThread());
|
---|
5676 | {
|
---|
5677 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
5678 | }
|
---|
5679 | treelock.acquire();
|
---|
5680 | autoCaller.add();
|
---|
5681 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5682 | alock.acquire();
|
---|
5683 | }
|
---|
5684 |
|
---|
5685 | /* Note that we are fine with Inaccessible state too: a) for symmetry
|
---|
5686 | * with create calls and b) because it doesn't really harm to try, if
|
---|
5687 | * it is really inaccessible, the delete operation will fail anyway.
|
---|
5688 | * Accepting Inaccessible state is especially important because all
|
---|
5689 | * registered media are initially Inaccessible upon VBoxSVC startup
|
---|
5690 | * until COMGETTER(RefreshState) is called. Accept Deleting state
|
---|
5691 | * because some callers need to put the medium in this state early
|
---|
5692 | * to prevent races. */
|
---|
5693 | switch (m->state)
|
---|
5694 | {
|
---|
5695 | case MediumState_Created:
|
---|
5696 | case MediumState_Deleting:
|
---|
5697 | case MediumState_Inaccessible:
|
---|
5698 | break;
|
---|
5699 | default:
|
---|
5700 | throw i_setStateError();
|
---|
5701 | }
|
---|
5702 |
|
---|
5703 | if (m->backRefs.size() != 0)
|
---|
5704 | {
|
---|
5705 | Utf8Str strMachines;
|
---|
5706 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
5707 | it != m->backRefs.end();
|
---|
5708 | ++it)
|
---|
5709 | {
|
---|
5710 | const BackRef &b = *it;
|
---|
5711 | if (strMachines.length())
|
---|
5712 | strMachines.append(", ");
|
---|
5713 | strMachines.append(b.machineId.toString().c_str());
|
---|
5714 | }
|
---|
5715 | #ifdef DEBUG
|
---|
5716 | i_dumpBackRefs();
|
---|
5717 | #endif
|
---|
5718 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5719 | tr("Cannot delete storage: medium '%s' is still attached to the following %d virtual machine(s): %s",
|
---|
5720 | "", m->backRefs.size()),
|
---|
5721 | m->strLocationFull.c_str(),
|
---|
5722 | m->backRefs.size(),
|
---|
5723 | strMachines.c_str());
|
---|
5724 | }
|
---|
5725 |
|
---|
5726 | rc = i_canClose();
|
---|
5727 | if (FAILED(rc))
|
---|
5728 | throw rc;
|
---|
5729 |
|
---|
5730 | /* go to Deleting state, so that the medium is not actually locked */
|
---|
5731 | if (m->state != MediumState_Deleting)
|
---|
5732 | {
|
---|
5733 | rc = i_markForDeletion();
|
---|
5734 | if (FAILED(rc))
|
---|
5735 | throw rc;
|
---|
5736 | }
|
---|
5737 |
|
---|
5738 | /* Build the medium lock list. */
|
---|
5739 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
5740 | alock.release();
|
---|
5741 | autoCaller.release();
|
---|
5742 | treelock.release();
|
---|
5743 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5744 | this /* pToLockWrite */,
|
---|
5745 | false /* fMediumLockWriteAll */,
|
---|
5746 | NULL,
|
---|
5747 | *pMediumLockList);
|
---|
5748 | treelock.acquire();
|
---|
5749 | autoCaller.add();
|
---|
5750 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5751 | alock.acquire();
|
---|
5752 | if (FAILED(rc))
|
---|
5753 | {
|
---|
5754 | delete pMediumLockList;
|
---|
5755 | throw rc;
|
---|
5756 | }
|
---|
5757 |
|
---|
5758 | alock.release();
|
---|
5759 | autoCaller.release();
|
---|
5760 | treelock.release();
|
---|
5761 | rc = pMediumLockList->Lock();
|
---|
5762 | treelock.acquire();
|
---|
5763 | autoCaller.add();
|
---|
5764 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5765 | alock.acquire();
|
---|
5766 | if (FAILED(rc))
|
---|
5767 | {
|
---|
5768 | delete pMediumLockList;
|
---|
5769 | throw setError(rc,
|
---|
5770 | tr("Failed to lock media when deleting '%s'"),
|
---|
5771 | i_getLocationFull().c_str());
|
---|
5772 | }
|
---|
5773 |
|
---|
5774 | /* try to remove from the list of known media before performing
|
---|
5775 | * actual deletion (we favor the consistency of the media registry
|
---|
5776 | * which would have been broken if unregisterWithVirtualBox() failed
|
---|
5777 | * after we successfully deleted the storage) */
|
---|
5778 | rc = i_unregisterWithVirtualBox();
|
---|
5779 | if (FAILED(rc))
|
---|
5780 | throw rc;
|
---|
5781 | // no longer need lock
|
---|
5782 | alock.release();
|
---|
5783 | autoCaller.release();
|
---|
5784 | treelock.release();
|
---|
5785 | i_markRegistriesModified();
|
---|
5786 |
|
---|
5787 | if (aProgress != NULL)
|
---|
5788 | {
|
---|
5789 | /* use the existing progress object... */
|
---|
5790 | pProgress = *aProgress;
|
---|
5791 |
|
---|
5792 | /* ...but create a new one if it is null */
|
---|
5793 | if (pProgress.isNull())
|
---|
5794 | {
|
---|
5795 | pProgress.createObject();
|
---|
5796 | rc = pProgress->init(m->pVirtualBox,
|
---|
5797 | static_cast<IMedium*>(this),
|
---|
5798 | BstrFmt(tr("Deleting medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
5799 | FALSE /* aCancelable */);
|
---|
5800 | if (FAILED(rc))
|
---|
5801 | throw rc;
|
---|
5802 | }
|
---|
5803 | }
|
---|
5804 |
|
---|
5805 | /* setup task object to carry out the operation sync/async */
|
---|
5806 | pTask = new Medium::DeleteTask(this, pProgress, pMediumLockList, false, aNotify);
|
---|
5807 | rc = pTask->rc();
|
---|
5808 | AssertComRC(rc);
|
---|
5809 | if (FAILED(rc))
|
---|
5810 | throw rc;
|
---|
5811 | }
|
---|
5812 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5813 |
|
---|
5814 | if (SUCCEEDED(rc))
|
---|
5815 | {
|
---|
5816 | if (aWait)
|
---|
5817 | {
|
---|
5818 | rc = pTask->runNow();
|
---|
5819 | delete pTask;
|
---|
5820 | }
|
---|
5821 | else
|
---|
5822 | rc = pTask->createThread();
|
---|
5823 | pTask = NULL;
|
---|
5824 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
5825 | *aProgress = pProgress;
|
---|
5826 | }
|
---|
5827 | else
|
---|
5828 | {
|
---|
5829 | if (pTask)
|
---|
5830 | delete pTask;
|
---|
5831 |
|
---|
5832 | /* Undo deleting state if necessary. */
|
---|
5833 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5834 | /* Make sure that any error signalled by unmarkForDeletion() is not
|
---|
5835 | * ending up in the error list (if the caller uses MultiResult). It
|
---|
5836 | * usually is spurious, as in most cases the medium hasn't been marked
|
---|
5837 | * for deletion when the error was thrown above. */
|
---|
5838 | ErrorInfoKeeper eik;
|
---|
5839 | i_unmarkForDeletion();
|
---|
5840 | }
|
---|
5841 |
|
---|
5842 | return rc;
|
---|
5843 | }
|
---|
5844 |
|
---|
5845 | /**
|
---|
5846 | * Mark a medium for deletion.
|
---|
5847 | *
|
---|
5848 | * @note Caller must hold the write lock on this medium!
|
---|
5849 | */
|
---|
5850 | HRESULT Medium::i_markForDeletion()
|
---|
5851 | {
|
---|
5852 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5853 | switch (m->state)
|
---|
5854 | {
|
---|
5855 | case MediumState_Created:
|
---|
5856 | case MediumState_Inaccessible:
|
---|
5857 | m->preLockState = m->state;
|
---|
5858 | m->state = MediumState_Deleting;
|
---|
5859 | return S_OK;
|
---|
5860 | default:
|
---|
5861 | return i_setStateError();
|
---|
5862 | }
|
---|
5863 | }
|
---|
5864 |
|
---|
5865 | /**
|
---|
5866 | * Removes the "mark for deletion".
|
---|
5867 | *
|
---|
5868 | * @note Caller must hold the write lock on this medium!
|
---|
5869 | */
|
---|
5870 | HRESULT Medium::i_unmarkForDeletion()
|
---|
5871 | {
|
---|
5872 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5873 | switch (m->state)
|
---|
5874 | {
|
---|
5875 | case MediumState_Deleting:
|
---|
5876 | m->state = m->preLockState;
|
---|
5877 | return S_OK;
|
---|
5878 | default:
|
---|
5879 | return i_setStateError();
|
---|
5880 | }
|
---|
5881 | }
|
---|
5882 |
|
---|
5883 | /**
|
---|
5884 | * Mark a medium for deletion which is in locked state.
|
---|
5885 | *
|
---|
5886 | * @note Caller must hold the write lock on this medium!
|
---|
5887 | */
|
---|
5888 | HRESULT Medium::i_markLockedForDeletion()
|
---|
5889 | {
|
---|
5890 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5891 | if ( ( m->state == MediumState_LockedRead
|
---|
5892 | || m->state == MediumState_LockedWrite)
|
---|
5893 | && m->preLockState == MediumState_Created)
|
---|
5894 | {
|
---|
5895 | m->preLockState = MediumState_Deleting;
|
---|
5896 | return S_OK;
|
---|
5897 | }
|
---|
5898 | else
|
---|
5899 | return i_setStateError();
|
---|
5900 | }
|
---|
5901 |
|
---|
5902 | /**
|
---|
5903 | * Removes the "mark for deletion" for a medium in locked state.
|
---|
5904 | *
|
---|
5905 | * @note Caller must hold the write lock on this medium!
|
---|
5906 | */
|
---|
5907 | HRESULT Medium::i_unmarkLockedForDeletion()
|
---|
5908 | {
|
---|
5909 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5910 | if ( ( m->state == MediumState_LockedRead
|
---|
5911 | || m->state == MediumState_LockedWrite)
|
---|
5912 | && m->preLockState == MediumState_Deleting)
|
---|
5913 | {
|
---|
5914 | m->preLockState = MediumState_Created;
|
---|
5915 | return S_OK;
|
---|
5916 | }
|
---|
5917 | else
|
---|
5918 | return i_setStateError();
|
---|
5919 | }
|
---|
5920 |
|
---|
5921 | /**
|
---|
5922 | * Queries the preferred merge direction from this to the other medium, i.e.
|
---|
5923 | * the one which requires the least amount of I/O and therefore time and
|
---|
5924 | * disk consumption.
|
---|
5925 | *
|
---|
5926 | * @returns Status code.
|
---|
5927 | * @retval E_FAIL in case determining the merge direction fails for some reason,
|
---|
5928 | * for example if getting the size of the media fails. There is no
|
---|
5929 | * error set though and the caller is free to continue to find out
|
---|
5930 | * what was going wrong later. Leaves fMergeForward unset.
|
---|
5931 | * @retval VBOX_E_INVALID_OBJECT_STATE if both media are not related to each other
|
---|
5932 | * An error is set.
|
---|
5933 | * @param pOther The other medium to merge with.
|
---|
5934 | * @param fMergeForward Resulting preferred merge direction (out).
|
---|
5935 | */
|
---|
5936 | HRESULT Medium::i_queryPreferredMergeDirection(const ComObjPtr<Medium> &pOther,
|
---|
5937 | bool &fMergeForward)
|
---|
5938 | {
|
---|
5939 | AssertReturn(pOther != NULL, E_FAIL);
|
---|
5940 | AssertReturn(pOther != this, E_FAIL);
|
---|
5941 |
|
---|
5942 | HRESULT rc = S_OK;
|
---|
5943 | bool fThisParent = false; /**<< Flag whether this medium is the parent of pOther. */
|
---|
5944 |
|
---|
5945 | try
|
---|
5946 | {
|
---|
5947 | // locking: we need the tree lock first because we access parent pointers
|
---|
5948 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5949 |
|
---|
5950 | AutoCaller autoCaller(this);
|
---|
5951 | AssertComRCThrowRC(autoCaller.rc());
|
---|
5952 |
|
---|
5953 | AutoCaller otherCaller(pOther);
|
---|
5954 | AssertComRCThrowRC(otherCaller.rc());
|
---|
5955 |
|
---|
5956 | /* more sanity checking and figuring out the current merge direction */
|
---|
5957 | ComObjPtr<Medium> pMedium = i_getParent();
|
---|
5958 | while (!pMedium.isNull() && pMedium != pOther)
|
---|
5959 | pMedium = pMedium->i_getParent();
|
---|
5960 | if (pMedium == pOther)
|
---|
5961 | fThisParent = false;
|
---|
5962 | else
|
---|
5963 | {
|
---|
5964 | pMedium = pOther->i_getParent();
|
---|
5965 | while (!pMedium.isNull() && pMedium != this)
|
---|
5966 | pMedium = pMedium->i_getParent();
|
---|
5967 | if (pMedium == this)
|
---|
5968 | fThisParent = true;
|
---|
5969 | else
|
---|
5970 | {
|
---|
5971 | Utf8Str tgtLoc;
|
---|
5972 | {
|
---|
5973 | AutoReadLock alock(pOther COMMA_LOCKVAL_SRC_POS);
|
---|
5974 | tgtLoc = pOther->i_getLocationFull();
|
---|
5975 | }
|
---|
5976 |
|
---|
5977 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5978 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5979 | tr("Media '%s' and '%s' are unrelated"),
|
---|
5980 | m->strLocationFull.c_str(), tgtLoc.c_str());
|
---|
5981 | }
|
---|
5982 | }
|
---|
5983 |
|
---|
5984 | /*
|
---|
5985 | * Figure out the preferred merge direction. The current way is to
|
---|
5986 | * get the current sizes of file based images and select the merge
|
---|
5987 | * direction depending on the size.
|
---|
5988 | *
|
---|
5989 | * Can't use the VD API to get current size here as the media might
|
---|
5990 | * be write locked by a running VM. Resort to RTFileQuerySize().
|
---|
5991 | */
|
---|
5992 | int vrc = VINF_SUCCESS;
|
---|
5993 | uint64_t cbMediumThis = 0;
|
---|
5994 | uint64_t cbMediumOther = 0;
|
---|
5995 |
|
---|
5996 | if (i_isMediumFormatFile() && pOther->i_isMediumFormatFile())
|
---|
5997 | {
|
---|
5998 | vrc = RTFileQuerySizeByPath(this->i_getLocationFull().c_str(), &cbMediumThis);
|
---|
5999 | if (RT_SUCCESS(vrc))
|
---|
6000 | {
|
---|
6001 | vrc = RTFileQuerySizeByPath(pOther->i_getLocationFull().c_str(),
|
---|
6002 | &cbMediumOther);
|
---|
6003 | }
|
---|
6004 |
|
---|
6005 | if (RT_FAILURE(vrc))
|
---|
6006 | rc = E_FAIL;
|
---|
6007 | else
|
---|
6008 | {
|
---|
6009 | /*
|
---|
6010 | * Check which merge direction might be more optimal.
|
---|
6011 | * This method is not bullet proof of course as there might
|
---|
6012 | * be overlapping blocks in the images so the file size is
|
---|
6013 | * not the best indicator but it is good enough for our purpose
|
---|
6014 | * and everything else is too complicated, especially when the
|
---|
6015 | * media are used by a running VM.
|
---|
6016 | */
|
---|
6017 |
|
---|
6018 | uint32_t mediumVariants = MediumVariant_Fixed | MediumVariant_VmdkStreamOptimized;
|
---|
6019 | uint32_t mediumCaps = MediumFormatCapabilities_CreateDynamic | MediumFormatCapabilities_File;
|
---|
6020 |
|
---|
6021 | bool fDynamicOther = pOther->i_getMediumFormat()->i_getCapabilities() & mediumCaps
|
---|
6022 | && pOther->i_getVariant() & ~mediumVariants;
|
---|
6023 | bool fDynamicThis = i_getMediumFormat()->i_getCapabilities() & mediumCaps
|
---|
6024 | && i_getVariant() & ~mediumVariants;
|
---|
6025 | bool fMergeIntoThis = (fDynamicThis && !fDynamicOther)
|
---|
6026 | || (fDynamicThis == fDynamicOther && cbMediumThis > cbMediumOther);
|
---|
6027 | fMergeForward = fMergeIntoThis != fThisParent;
|
---|
6028 | }
|
---|
6029 | }
|
---|
6030 | }
|
---|
6031 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6032 |
|
---|
6033 | return rc;
|
---|
6034 | }
|
---|
6035 |
|
---|
6036 | /**
|
---|
6037 | * Prepares this (source) medium, target medium and all intermediate media
|
---|
6038 | * for the merge operation.
|
---|
6039 | *
|
---|
6040 | * This method is to be called prior to calling the #mergeTo() to perform
|
---|
6041 | * necessary consistency checks and place involved media to appropriate
|
---|
6042 | * states. If #mergeTo() is not called or fails, the state modifications
|
---|
6043 | * performed by this method must be undone by #i_cancelMergeTo().
|
---|
6044 | *
|
---|
6045 | * See #mergeTo() for more information about merging.
|
---|
6046 | *
|
---|
6047 | * @param pTarget Target medium.
|
---|
6048 | * @param aMachineId Allowed machine attachment. NULL means do not check.
|
---|
6049 | * @param aSnapshotId Allowed snapshot attachment. NULL or empty UUID means
|
---|
6050 | * do not check.
|
---|
6051 | * @param fLockMedia Flag whether to lock the medium lock list or not.
|
---|
6052 | * If set to false and the medium lock list locking fails
|
---|
6053 | * later you must call #i_cancelMergeTo().
|
---|
6054 | * @param fMergeForward Resulting merge direction (out).
|
---|
6055 | * @param pParentForTarget New parent for target medium after merge (out).
|
---|
6056 | * @param aChildrenToReparent Medium lock list containing all children of the
|
---|
6057 | * source which will have to be reparented to the target
|
---|
6058 | * after merge (out).
|
---|
6059 | * @param aMediumLockList Medium locking information (out).
|
---|
6060 | *
|
---|
6061 | * @note Locks medium tree for reading. Locks this object, aTarget and all
|
---|
6062 | * intermediate media for writing.
|
---|
6063 | */
|
---|
6064 | HRESULT Medium::i_prepareMergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
6065 | const Guid *aMachineId,
|
---|
6066 | const Guid *aSnapshotId,
|
---|
6067 | bool fLockMedia,
|
---|
6068 | bool &fMergeForward,
|
---|
6069 | ComObjPtr<Medium> &pParentForTarget,
|
---|
6070 | MediumLockList * &aChildrenToReparent,
|
---|
6071 | MediumLockList * &aMediumLockList)
|
---|
6072 | {
|
---|
6073 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
6074 | AssertReturn(pTarget != this, E_FAIL);
|
---|
6075 |
|
---|
6076 | HRESULT rc = S_OK;
|
---|
6077 | fMergeForward = false;
|
---|
6078 | pParentForTarget.setNull();
|
---|
6079 | Assert(aChildrenToReparent == NULL);
|
---|
6080 | aChildrenToReparent = NULL;
|
---|
6081 | Assert(aMediumLockList == NULL);
|
---|
6082 | aMediumLockList = NULL;
|
---|
6083 |
|
---|
6084 | try
|
---|
6085 | {
|
---|
6086 | // locking: we need the tree lock first because we access parent pointers
|
---|
6087 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
6088 |
|
---|
6089 | AutoCaller autoCaller(this);
|
---|
6090 | AssertComRCThrowRC(autoCaller.rc());
|
---|
6091 |
|
---|
6092 | AutoCaller targetCaller(pTarget);
|
---|
6093 | AssertComRCThrowRC(targetCaller.rc());
|
---|
6094 |
|
---|
6095 | /* more sanity checking and figuring out the merge direction */
|
---|
6096 | ComObjPtr<Medium> pMedium = i_getParent();
|
---|
6097 | while (!pMedium.isNull() && pMedium != pTarget)
|
---|
6098 | pMedium = pMedium->i_getParent();
|
---|
6099 | if (pMedium == pTarget)
|
---|
6100 | fMergeForward = false;
|
---|
6101 | else
|
---|
6102 | {
|
---|
6103 | pMedium = pTarget->i_getParent();
|
---|
6104 | while (!pMedium.isNull() && pMedium != this)
|
---|
6105 | pMedium = pMedium->i_getParent();
|
---|
6106 | if (pMedium == this)
|
---|
6107 | fMergeForward = true;
|
---|
6108 | else
|
---|
6109 | {
|
---|
6110 | Utf8Str tgtLoc;
|
---|
6111 | {
|
---|
6112 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
6113 | tgtLoc = pTarget->i_getLocationFull();
|
---|
6114 | }
|
---|
6115 |
|
---|
6116 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6117 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6118 | tr("Media '%s' and '%s' are unrelated"),
|
---|
6119 | m->strLocationFull.c_str(), tgtLoc.c_str());
|
---|
6120 | }
|
---|
6121 | }
|
---|
6122 |
|
---|
6123 | /* Build the lock list. */
|
---|
6124 | aMediumLockList = new MediumLockList();
|
---|
6125 | targetCaller.release();
|
---|
6126 | autoCaller.release();
|
---|
6127 | treeLock.release();
|
---|
6128 | if (fMergeForward)
|
---|
6129 | rc = pTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6130 | pTarget /* pToLockWrite */,
|
---|
6131 | false /* fMediumLockWriteAll */,
|
---|
6132 | NULL,
|
---|
6133 | *aMediumLockList);
|
---|
6134 | else
|
---|
6135 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6136 | pTarget /* pToLockWrite */,
|
---|
6137 | false /* fMediumLockWriteAll */,
|
---|
6138 | NULL,
|
---|
6139 | *aMediumLockList);
|
---|
6140 | treeLock.acquire();
|
---|
6141 | autoCaller.add();
|
---|
6142 | AssertComRCThrowRC(autoCaller.rc());
|
---|
6143 | targetCaller.add();
|
---|
6144 | AssertComRCThrowRC(targetCaller.rc());
|
---|
6145 | if (FAILED(rc))
|
---|
6146 | throw rc;
|
---|
6147 |
|
---|
6148 | /* Sanity checking, must be after lock list creation as it depends on
|
---|
6149 | * valid medium states. The medium objects must be accessible. Only
|
---|
6150 | * do this if immediate locking is requested, otherwise it fails when
|
---|
6151 | * we construct a medium lock list for an already running VM. Snapshot
|
---|
6152 | * deletion uses this to simplify its life. */
|
---|
6153 | if (fLockMedia)
|
---|
6154 | {
|
---|
6155 | {
|
---|
6156 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6157 | if (m->state != MediumState_Created)
|
---|
6158 | throw i_setStateError();
|
---|
6159 | }
|
---|
6160 | {
|
---|
6161 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
6162 | if (pTarget->m->state != MediumState_Created)
|
---|
6163 | throw pTarget->i_setStateError();
|
---|
6164 | }
|
---|
6165 | }
|
---|
6166 |
|
---|
6167 | /* check medium attachment and other sanity conditions */
|
---|
6168 | if (fMergeForward)
|
---|
6169 | {
|
---|
6170 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6171 | if (i_getChildren().size() > 1)
|
---|
6172 | {
|
---|
6173 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6174 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
6175 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
6176 | }
|
---|
6177 | /* One backreference is only allowed if the machine ID is not empty
|
---|
6178 | * and it matches the machine the medium is attached to (including
|
---|
6179 | * the snapshot ID if not empty). */
|
---|
6180 | if ( m->backRefs.size() != 0
|
---|
6181 | && ( !aMachineId
|
---|
6182 | || m->backRefs.size() != 1
|
---|
6183 | || aMachineId->isZero()
|
---|
6184 | || *i_getFirstMachineBackrefId() != *aMachineId
|
---|
6185 | || ( (!aSnapshotId || !aSnapshotId->isZero())
|
---|
6186 | && *i_getFirstMachineBackrefSnapshotId() != *aSnapshotId)))
|
---|
6187 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
6188 | tr("Medium '%s' is attached to %d virtual machines", "", m->backRefs.size()),
|
---|
6189 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
6190 | if (m->type == MediumType_Immutable)
|
---|
6191 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6192 | tr("Medium '%s' is immutable"),
|
---|
6193 | m->strLocationFull.c_str());
|
---|
6194 | if (m->type == MediumType_MultiAttach)
|
---|
6195 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6196 | tr("Medium '%s' is multi-attach"),
|
---|
6197 | m->strLocationFull.c_str());
|
---|
6198 | }
|
---|
6199 | else
|
---|
6200 | {
|
---|
6201 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
6202 | if (pTarget->i_getChildren().size() > 1)
|
---|
6203 | {
|
---|
6204 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
6205 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
6206 | pTarget->m->strLocationFull.c_str(),
|
---|
6207 | pTarget->i_getChildren().size());
|
---|
6208 | }
|
---|
6209 | if (pTarget->m->type == MediumType_Immutable)
|
---|
6210 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6211 | tr("Medium '%s' is immutable"),
|
---|
6212 | pTarget->m->strLocationFull.c_str());
|
---|
6213 | if (pTarget->m->type == MediumType_MultiAttach)
|
---|
6214 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
6215 | tr("Medium '%s' is multi-attach"),
|
---|
6216 | pTarget->m->strLocationFull.c_str());
|
---|
6217 | }
|
---|
6218 | ComObjPtr<Medium> pLast(fMergeForward ? (Medium *)pTarget : this);
|
---|
6219 | ComObjPtr<Medium> pLastIntermediate = pLast->i_getParent();
|
---|
6220 | for (pLast = pLastIntermediate;
|
---|
6221 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
6222 | pLast = pLast->i_getParent())
|
---|
6223 | {
|
---|
6224 | AutoReadLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
6225 | if (pLast->i_getChildren().size() > 1)
|
---|
6226 | {
|
---|
6227 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
6228 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
6229 | pLast->m->strLocationFull.c_str(),
|
---|
6230 | pLast->i_getChildren().size());
|
---|
6231 | }
|
---|
6232 | if (pLast->m->backRefs.size() != 0)
|
---|
6233 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
6234 | tr("Medium '%s' is attached to %d virtual machines", "", pLast->m->backRefs.size()),
|
---|
6235 | pLast->m->strLocationFull.c_str(),
|
---|
6236 | pLast->m->backRefs.size());
|
---|
6237 |
|
---|
6238 | }
|
---|
6239 |
|
---|
6240 | /* Update medium states appropriately */
|
---|
6241 | {
|
---|
6242 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6243 |
|
---|
6244 | if (m->state == MediumState_Created)
|
---|
6245 | {
|
---|
6246 | rc = i_markForDeletion();
|
---|
6247 | if (FAILED(rc))
|
---|
6248 | throw rc;
|
---|
6249 | }
|
---|
6250 | else
|
---|
6251 | {
|
---|
6252 | if (fLockMedia)
|
---|
6253 | throw i_setStateError();
|
---|
6254 | else if ( m->state == MediumState_LockedWrite
|
---|
6255 | || m->state == MediumState_LockedRead)
|
---|
6256 | {
|
---|
6257 | /* Either mark it for deletion in locked state or allow
|
---|
6258 | * others to have done so. */
|
---|
6259 | if (m->preLockState == MediumState_Created)
|
---|
6260 | i_markLockedForDeletion();
|
---|
6261 | else if (m->preLockState != MediumState_Deleting)
|
---|
6262 | throw i_setStateError();
|
---|
6263 | }
|
---|
6264 | else
|
---|
6265 | throw i_setStateError();
|
---|
6266 | }
|
---|
6267 | }
|
---|
6268 |
|
---|
6269 | if (fMergeForward)
|
---|
6270 | {
|
---|
6271 | /* we will need parent to reparent target */
|
---|
6272 | pParentForTarget = i_getParent();
|
---|
6273 | }
|
---|
6274 | else
|
---|
6275 | {
|
---|
6276 | /* we will need to reparent children of the source */
|
---|
6277 | aChildrenToReparent = new MediumLockList();
|
---|
6278 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
6279 | it != i_getChildren().end();
|
---|
6280 | ++it)
|
---|
6281 | {
|
---|
6282 | pMedium = *it;
|
---|
6283 | aChildrenToReparent->Append(pMedium, true /* fLockWrite */);
|
---|
6284 | }
|
---|
6285 | if (fLockMedia && aChildrenToReparent)
|
---|
6286 | {
|
---|
6287 | targetCaller.release();
|
---|
6288 | autoCaller.release();
|
---|
6289 | treeLock.release();
|
---|
6290 | rc = aChildrenToReparent->Lock();
|
---|
6291 | treeLock.acquire();
|
---|
6292 | autoCaller.add();
|
---|
6293 | AssertComRCThrowRC(autoCaller.rc());
|
---|
6294 | targetCaller.add();
|
---|
6295 | AssertComRCThrowRC(targetCaller.rc());
|
---|
6296 | if (FAILED(rc))
|
---|
6297 | throw rc;
|
---|
6298 | }
|
---|
6299 | }
|
---|
6300 | for (pLast = pLastIntermediate;
|
---|
6301 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
6302 | pLast = pLast->i_getParent())
|
---|
6303 | {
|
---|
6304 | AutoWriteLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
6305 | if (pLast->m->state == MediumState_Created)
|
---|
6306 | {
|
---|
6307 | rc = pLast->i_markForDeletion();
|
---|
6308 | if (FAILED(rc))
|
---|
6309 | throw rc;
|
---|
6310 | }
|
---|
6311 | else
|
---|
6312 | throw pLast->i_setStateError();
|
---|
6313 | }
|
---|
6314 |
|
---|
6315 | /* Tweak the lock list in the backward merge case, as the target
|
---|
6316 | * isn't marked to be locked for writing yet. */
|
---|
6317 | if (!fMergeForward)
|
---|
6318 | {
|
---|
6319 | MediumLockList::Base::iterator lockListBegin =
|
---|
6320 | aMediumLockList->GetBegin();
|
---|
6321 | MediumLockList::Base::iterator lockListEnd =
|
---|
6322 | aMediumLockList->GetEnd();
|
---|
6323 | ++lockListEnd;
|
---|
6324 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
6325 | it != lockListEnd;
|
---|
6326 | ++it)
|
---|
6327 | {
|
---|
6328 | MediumLock &mediumLock = *it;
|
---|
6329 | if (mediumLock.GetMedium() == pTarget)
|
---|
6330 | {
|
---|
6331 | HRESULT rc2 = mediumLock.UpdateLock(true);
|
---|
6332 | AssertComRC(rc2);
|
---|
6333 | break;
|
---|
6334 | }
|
---|
6335 | }
|
---|
6336 | }
|
---|
6337 |
|
---|
6338 | if (fLockMedia)
|
---|
6339 | {
|
---|
6340 | targetCaller.release();
|
---|
6341 | autoCaller.release();
|
---|
6342 | treeLock.release();
|
---|
6343 | rc = aMediumLockList->Lock();
|
---|
6344 | treeLock.acquire();
|
---|
6345 | autoCaller.add();
|
---|
6346 | AssertComRCThrowRC(autoCaller.rc());
|
---|
6347 | targetCaller.add();
|
---|
6348 | AssertComRCThrowRC(targetCaller.rc());
|
---|
6349 | if (FAILED(rc))
|
---|
6350 | {
|
---|
6351 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
6352 | throw setError(rc,
|
---|
6353 | tr("Failed to lock media when merging to '%s'"),
|
---|
6354 | pTarget->i_getLocationFull().c_str());
|
---|
6355 | }
|
---|
6356 | }
|
---|
6357 | }
|
---|
6358 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6359 |
|
---|
6360 | if (FAILED(rc))
|
---|
6361 | {
|
---|
6362 | if (aMediumLockList)
|
---|
6363 | {
|
---|
6364 | delete aMediumLockList;
|
---|
6365 | aMediumLockList = NULL;
|
---|
6366 | }
|
---|
6367 | if (aChildrenToReparent)
|
---|
6368 | {
|
---|
6369 | delete aChildrenToReparent;
|
---|
6370 | aChildrenToReparent = NULL;
|
---|
6371 | }
|
---|
6372 | }
|
---|
6373 |
|
---|
6374 | return rc;
|
---|
6375 | }
|
---|
6376 |
|
---|
6377 | /**
|
---|
6378 | * Merges this medium to the specified medium which must be either its
|
---|
6379 | * direct ancestor or descendant.
|
---|
6380 | *
|
---|
6381 | * Given this medium is SOURCE and the specified medium is TARGET, we will
|
---|
6382 | * get two variants of the merge operation:
|
---|
6383 | *
|
---|
6384 | * forward merge
|
---|
6385 | * ------------------------->
|
---|
6386 | * [Extra] <- SOURCE <- Intermediate <- TARGET
|
---|
6387 | * Any Del Del LockWr
|
---|
6388 | *
|
---|
6389 | *
|
---|
6390 | * backward merge
|
---|
6391 | * <-------------------------
|
---|
6392 | * TARGET <- Intermediate <- SOURCE <- [Extra]
|
---|
6393 | * LockWr Del Del LockWr
|
---|
6394 | *
|
---|
6395 | * Each diagram shows the involved media on the media chain where
|
---|
6396 | * SOURCE and TARGET belong. Under each medium there is a state value which
|
---|
6397 | * the medium must have at a time of the mergeTo() call.
|
---|
6398 | *
|
---|
6399 | * The media in the square braces may be absent (e.g. when the forward
|
---|
6400 | * operation takes place and SOURCE is the base medium, or when the backward
|
---|
6401 | * merge operation takes place and TARGET is the last child in the chain) but if
|
---|
6402 | * they present they are involved too as shown.
|
---|
6403 | *
|
---|
6404 | * Neither the source medium nor intermediate media may be attached to
|
---|
6405 | * any VM directly or in the snapshot, otherwise this method will assert.
|
---|
6406 | *
|
---|
6407 | * The #i_prepareMergeTo() method must be called prior to this method to place
|
---|
6408 | * all involved to necessary states and perform other consistency checks.
|
---|
6409 | *
|
---|
6410 | * If @a aWait is @c true then this method will perform the operation on the
|
---|
6411 | * calling thread and will not return to the caller until the operation is
|
---|
6412 | * completed. When this method succeeds, all intermediate medium objects in
|
---|
6413 | * the chain will be uninitialized, the state of the target medium (and all
|
---|
6414 | * involved extra media) will be restored. @a aMediumLockList will not be
|
---|
6415 | * deleted, whether the operation is successful or not. The caller has to do
|
---|
6416 | * this if appropriate. Note that this (source) medium is not uninitialized
|
---|
6417 | * because of possible AutoCaller instances held by the caller of this method
|
---|
6418 | * on the current thread. It's therefore the responsibility of the caller to
|
---|
6419 | * call Medium::uninit() after releasing all callers.
|
---|
6420 | *
|
---|
6421 | * If @a aWait is @c false then this method will create a thread to perform the
|
---|
6422 | * operation asynchronously and will return immediately. If the operation
|
---|
6423 | * succeeds, the thread will uninitialize the source medium object and all
|
---|
6424 | * intermediate medium objects in the chain, reset the state of the target
|
---|
6425 | * medium (and all involved extra media) and delete @a aMediumLockList.
|
---|
6426 | * If the operation fails, the thread will only reset the states of all
|
---|
6427 | * involved media and delete @a aMediumLockList.
|
---|
6428 | *
|
---|
6429 | * When this method fails (regardless of the @a aWait mode), it is a caller's
|
---|
6430 | * responsibility to undo state changes and delete @a aMediumLockList using
|
---|
6431 | * #i_cancelMergeTo().
|
---|
6432 | *
|
---|
6433 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
6434 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
6435 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
6436 | * progress object is created/used at all. Note that @a aProgress cannot be
|
---|
6437 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
6438 | *
|
---|
6439 | * @param pTarget Target medium.
|
---|
6440 | * @param fMergeForward Merge direction.
|
---|
6441 | * @param pParentForTarget New parent for target medium after merge.
|
---|
6442 | * @param aChildrenToReparent List of children of the source which will have
|
---|
6443 | * to be reparented to the target after merge.
|
---|
6444 | * @param aMediumLockList Medium locking information.
|
---|
6445 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
6446 | * completion.
|
---|
6447 | * @param aWait @c true if this method should block instead of creating
|
---|
6448 | * an asynchronous thread.
|
---|
6449 | * @param aNotify Notify about mediums which metadatа are changed
|
---|
6450 | * during execution of the function.
|
---|
6451 | *
|
---|
6452 | * @note Locks the tree lock for writing. Locks the media from the chain
|
---|
6453 | * for writing.
|
---|
6454 | */
|
---|
6455 | HRESULT Medium::i_mergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
6456 | bool fMergeForward,
|
---|
6457 | const ComObjPtr<Medium> &pParentForTarget,
|
---|
6458 | MediumLockList *aChildrenToReparent,
|
---|
6459 | MediumLockList *aMediumLockList,
|
---|
6460 | ComObjPtr<Progress> *aProgress,
|
---|
6461 | bool aWait, bool aNotify)
|
---|
6462 | {
|
---|
6463 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
6464 | AssertReturn(pTarget != this, E_FAIL);
|
---|
6465 | AssertReturn(aMediumLockList != NULL, E_FAIL);
|
---|
6466 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
6467 |
|
---|
6468 | AutoCaller autoCaller(this);
|
---|
6469 | AssertComRCReturnRC(autoCaller.rc());
|
---|
6470 |
|
---|
6471 | AutoCaller targetCaller(pTarget);
|
---|
6472 | AssertComRCReturnRC(targetCaller.rc());
|
---|
6473 |
|
---|
6474 | HRESULT rc = S_OK;
|
---|
6475 | ComObjPtr<Progress> pProgress;
|
---|
6476 | Medium::Task *pTask = NULL;
|
---|
6477 |
|
---|
6478 | try
|
---|
6479 | {
|
---|
6480 | if (aProgress != NULL)
|
---|
6481 | {
|
---|
6482 | /* use the existing progress object... */
|
---|
6483 | pProgress = *aProgress;
|
---|
6484 |
|
---|
6485 | /* ...but create a new one if it is null */
|
---|
6486 | if (pProgress.isNull())
|
---|
6487 | {
|
---|
6488 | Utf8Str tgtName;
|
---|
6489 | {
|
---|
6490 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
6491 | tgtName = pTarget->i_getName();
|
---|
6492 | }
|
---|
6493 |
|
---|
6494 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6495 |
|
---|
6496 | pProgress.createObject();
|
---|
6497 | rc = pProgress->init(m->pVirtualBox,
|
---|
6498 | static_cast<IMedium*>(this),
|
---|
6499 | BstrFmt(tr("Merging medium '%s' to '%s'"),
|
---|
6500 | i_getName().c_str(),
|
---|
6501 | tgtName.c_str()).raw(),
|
---|
6502 | TRUE, /* aCancelable */
|
---|
6503 | 2, /* Number of opearations */
|
---|
6504 | BstrFmt(tr("Resizing medium '%s' before merge"),
|
---|
6505 | tgtName.c_str()).raw()
|
---|
6506 | );
|
---|
6507 | if (FAILED(rc))
|
---|
6508 | throw rc;
|
---|
6509 | }
|
---|
6510 | }
|
---|
6511 |
|
---|
6512 | /* setup task object to carry out the operation sync/async */
|
---|
6513 | pTask = new Medium::MergeTask(this, pTarget, fMergeForward,
|
---|
6514 | pParentForTarget, aChildrenToReparent,
|
---|
6515 | pProgress, aMediumLockList,
|
---|
6516 | aWait /* fKeepMediumLockList */,
|
---|
6517 | aNotify);
|
---|
6518 | rc = pTask->rc();
|
---|
6519 | AssertComRC(rc);
|
---|
6520 | if (FAILED(rc))
|
---|
6521 | throw rc;
|
---|
6522 | }
|
---|
6523 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6524 |
|
---|
6525 | if (SUCCEEDED(rc))
|
---|
6526 | {
|
---|
6527 | if (aWait)
|
---|
6528 | {
|
---|
6529 | rc = pTask->runNow();
|
---|
6530 | delete pTask;
|
---|
6531 | }
|
---|
6532 | else
|
---|
6533 | rc = pTask->createThread();
|
---|
6534 | pTask = NULL;
|
---|
6535 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
6536 | *aProgress = pProgress;
|
---|
6537 | }
|
---|
6538 | else if (pTask != NULL)
|
---|
6539 | delete pTask;
|
---|
6540 |
|
---|
6541 | return rc;
|
---|
6542 | }
|
---|
6543 |
|
---|
6544 | /**
|
---|
6545 | * Undoes what #i_prepareMergeTo() did. Must be called if #mergeTo() is not
|
---|
6546 | * called or fails. Frees memory occupied by @a aMediumLockList and unlocks
|
---|
6547 | * the medium objects in @a aChildrenToReparent.
|
---|
6548 | *
|
---|
6549 | * @param aChildrenToReparent List of children of the source which will have
|
---|
6550 | * to be reparented to the target after merge.
|
---|
6551 | * @param aMediumLockList Medium locking information.
|
---|
6552 | *
|
---|
6553 | * @note Locks the tree lock for writing. Locks the media from the chain
|
---|
6554 | * for writing.
|
---|
6555 | */
|
---|
6556 | void Medium::i_cancelMergeTo(MediumLockList *aChildrenToReparent,
|
---|
6557 | MediumLockList *aMediumLockList)
|
---|
6558 | {
|
---|
6559 | AutoCaller autoCaller(this);
|
---|
6560 | AssertComRCReturnVoid(autoCaller.rc());
|
---|
6561 |
|
---|
6562 | AssertReturnVoid(aMediumLockList != NULL);
|
---|
6563 |
|
---|
6564 | /* Revert media marked for deletion to previous state. */
|
---|
6565 | HRESULT rc;
|
---|
6566 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
6567 | aMediumLockList->GetBegin();
|
---|
6568 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
6569 | aMediumLockList->GetEnd();
|
---|
6570 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
6571 | it != mediumListEnd;
|
---|
6572 | ++it)
|
---|
6573 | {
|
---|
6574 | const MediumLock &mediumLock = *it;
|
---|
6575 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
6576 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
6577 |
|
---|
6578 | if (pMedium->m->state == MediumState_Deleting)
|
---|
6579 | {
|
---|
6580 | rc = pMedium->i_unmarkForDeletion();
|
---|
6581 | AssertComRC(rc);
|
---|
6582 | }
|
---|
6583 | else if ( ( pMedium->m->state == MediumState_LockedWrite
|
---|
6584 | || pMedium->m->state == MediumState_LockedRead)
|
---|
6585 | && pMedium->m->preLockState == MediumState_Deleting)
|
---|
6586 | {
|
---|
6587 | rc = pMedium->i_unmarkLockedForDeletion();
|
---|
6588 | AssertComRC(rc);
|
---|
6589 | }
|
---|
6590 | }
|
---|
6591 |
|
---|
6592 | /* the destructor will do the work */
|
---|
6593 | delete aMediumLockList;
|
---|
6594 |
|
---|
6595 | /* unlock the children which had to be reparented, the destructor will do
|
---|
6596 | * the work */
|
---|
6597 | if (aChildrenToReparent)
|
---|
6598 | delete aChildrenToReparent;
|
---|
6599 | }
|
---|
6600 |
|
---|
6601 | /**
|
---|
6602 | * Resizes the media.
|
---|
6603 | *
|
---|
6604 | * If @a aWait is @c true then this method will perform the operation on the
|
---|
6605 | * calling thread and will not return to the caller until the operation is
|
---|
6606 | * completed. When this method succeeds, the state of the target medium (and all
|
---|
6607 | * involved extra media) will be restored. @a aMediumLockList will not be
|
---|
6608 | * deleted, whether the operation is successful or not. The caller has to do
|
---|
6609 | * this if appropriate.
|
---|
6610 | *
|
---|
6611 | * If @a aWait is @c false then this method will create a thread to perform the
|
---|
6612 | * operation asynchronously and will return immediately. The thread will reset
|
---|
6613 | * the state of the target medium (and all involved extra media) and delete
|
---|
6614 | * @a aMediumLockList.
|
---|
6615 | *
|
---|
6616 | * When this method fails (regardless of the @a aWait mode), it is a caller's
|
---|
6617 | * responsibility to undo state changes and delete @a aMediumLockList.
|
---|
6618 | *
|
---|
6619 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
6620 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
6621 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
6622 | * progress object is created/used at all. Note that @a aProgress cannot be
|
---|
6623 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
6624 | *
|
---|
6625 | * @param aLogicalSize New nominal capacity of the medium in bytes.
|
---|
6626 | * @param aMediumLockList Medium locking information.
|
---|
6627 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
6628 | * completion.
|
---|
6629 | * @param aWait @c true if this method should block instead of creating
|
---|
6630 | * an asynchronous thread.
|
---|
6631 | * @param aNotify Notify about mediums which metadatа are changed
|
---|
6632 | * during execution of the function.
|
---|
6633 | *
|
---|
6634 | * @note Locks the media from the chain for writing.
|
---|
6635 | */
|
---|
6636 |
|
---|
6637 | HRESULT Medium::i_resize(uint64_t aLogicalSize,
|
---|
6638 | MediumLockList *aMediumLockList,
|
---|
6639 | ComObjPtr<Progress> *aProgress,
|
---|
6640 | bool aWait,
|
---|
6641 | bool aNotify)
|
---|
6642 | {
|
---|
6643 | AssertReturn(aMediumLockList != NULL, E_FAIL);
|
---|
6644 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
6645 |
|
---|
6646 | AutoCaller autoCaller(this);
|
---|
6647 | AssertComRCReturnRC(autoCaller.rc());
|
---|
6648 |
|
---|
6649 | HRESULT rc = S_OK;
|
---|
6650 | ComObjPtr<Progress> pProgress;
|
---|
6651 | Medium::Task *pTask = NULL;
|
---|
6652 |
|
---|
6653 | try
|
---|
6654 | {
|
---|
6655 | if (aProgress != NULL)
|
---|
6656 | {
|
---|
6657 | /* use the existing progress object... */
|
---|
6658 | pProgress = *aProgress;
|
---|
6659 |
|
---|
6660 | /* ...but create a new one if it is null */
|
---|
6661 | if (pProgress.isNull())
|
---|
6662 | {
|
---|
6663 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
6664 |
|
---|
6665 | pProgress.createObject();
|
---|
6666 | rc = pProgress->init(m->pVirtualBox,
|
---|
6667 | static_cast <IMedium *>(this),
|
---|
6668 | BstrFmt(tr("Resizing medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
6669 | TRUE /* aCancelable */);
|
---|
6670 | if (FAILED(rc))
|
---|
6671 | throw rc;
|
---|
6672 | }
|
---|
6673 | }
|
---|
6674 |
|
---|
6675 | /* setup task object to carry out the operation asynchronously */
|
---|
6676 | pTask = new Medium::ResizeTask(this,
|
---|
6677 | aLogicalSize,
|
---|
6678 | pProgress,
|
---|
6679 | aMediumLockList,
|
---|
6680 | aWait /* fKeepMediumLockList */,
|
---|
6681 | aNotify);
|
---|
6682 | rc = pTask->rc();
|
---|
6683 | AssertComRC(rc);
|
---|
6684 | if (FAILED(rc))
|
---|
6685 | throw rc;
|
---|
6686 | }
|
---|
6687 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6688 |
|
---|
6689 | if (SUCCEEDED(rc))
|
---|
6690 | {
|
---|
6691 | if (aWait)
|
---|
6692 | {
|
---|
6693 | rc = pTask->runNow();
|
---|
6694 | delete pTask;
|
---|
6695 | }
|
---|
6696 | else
|
---|
6697 | rc = pTask->createThread();
|
---|
6698 | pTask = NULL;
|
---|
6699 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
6700 | *aProgress = pProgress;
|
---|
6701 | }
|
---|
6702 | else if (pTask != NULL)
|
---|
6703 | delete pTask;
|
---|
6704 |
|
---|
6705 | return rc;
|
---|
6706 | }
|
---|
6707 |
|
---|
6708 | /**
|
---|
6709 | * Fix the parent UUID of all children to point to this medium as their
|
---|
6710 | * parent.
|
---|
6711 | */
|
---|
6712 | HRESULT Medium::i_fixParentUuidOfChildren(MediumLockList *pChildrenToReparent)
|
---|
6713 | {
|
---|
6714 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
6715 | * to lock order violations, it probably causes lock order issues related
|
---|
6716 | * to the AutoCaller usage. Likewise the code using this method seems
|
---|
6717 | * problematic. */
|
---|
6718 | Assert(!isWriteLockOnCurrentThread());
|
---|
6719 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
6720 | MediumLockList mediumLockList;
|
---|
6721 | HRESULT rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
6722 | NULL /* pToLockWrite */,
|
---|
6723 | false /* fMediumLockWriteAll */,
|
---|
6724 | this,
|
---|
6725 | mediumLockList);
|
---|
6726 | AssertComRCReturnRC(rc);
|
---|
6727 |
|
---|
6728 | try
|
---|
6729 | {
|
---|
6730 | PVDISK hdd;
|
---|
6731 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
6732 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
6733 |
|
---|
6734 | try
|
---|
6735 | {
|
---|
6736 | MediumLockList::Base::iterator lockListBegin =
|
---|
6737 | mediumLockList.GetBegin();
|
---|
6738 | MediumLockList::Base::iterator lockListEnd =
|
---|
6739 | mediumLockList.GetEnd();
|
---|
6740 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
6741 | it != lockListEnd;
|
---|
6742 | ++it)
|
---|
6743 | {
|
---|
6744 | MediumLock &mediumLock = *it;
|
---|
6745 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
6746 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
6747 |
|
---|
6748 | // open the medium
|
---|
6749 | vrc = VDOpen(hdd,
|
---|
6750 | pMedium->m->strFormat.c_str(),
|
---|
6751 | pMedium->m->strLocationFull.c_str(),
|
---|
6752 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
6753 | pMedium->m->vdImageIfaces);
|
---|
6754 | if (RT_FAILURE(vrc))
|
---|
6755 | throw vrc;
|
---|
6756 | }
|
---|
6757 |
|
---|
6758 | MediumLockList::Base::iterator childrenBegin = pChildrenToReparent->GetBegin();
|
---|
6759 | MediumLockList::Base::iterator childrenEnd = pChildrenToReparent->GetEnd();
|
---|
6760 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
6761 | it != childrenEnd;
|
---|
6762 | ++it)
|
---|
6763 | {
|
---|
6764 | Medium *pMedium = it->GetMedium();
|
---|
6765 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
6766 | vrc = VDOpen(hdd,
|
---|
6767 | pMedium->m->strFormat.c_str(),
|
---|
6768 | pMedium->m->strLocationFull.c_str(),
|
---|
6769 | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
6770 | pMedium->m->vdImageIfaces);
|
---|
6771 | if (RT_FAILURE(vrc))
|
---|
6772 | throw vrc;
|
---|
6773 |
|
---|
6774 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE, m->id.raw());
|
---|
6775 | if (RT_FAILURE(vrc))
|
---|
6776 | throw vrc;
|
---|
6777 |
|
---|
6778 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
6779 | if (RT_FAILURE(vrc))
|
---|
6780 | throw vrc;
|
---|
6781 | }
|
---|
6782 | }
|
---|
6783 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6784 | catch (int aVRC)
|
---|
6785 | {
|
---|
6786 | rc = setErrorBoth(E_FAIL, aVRC,
|
---|
6787 | tr("Could not update medium UUID references to parent '%s' (%s)"),
|
---|
6788 | m->strLocationFull.c_str(),
|
---|
6789 | i_vdError(aVRC).c_str());
|
---|
6790 | }
|
---|
6791 |
|
---|
6792 | VDDestroy(hdd);
|
---|
6793 | }
|
---|
6794 | catch (HRESULT aRC) { rc = aRC; }
|
---|
6795 |
|
---|
6796 | return rc;
|
---|
6797 | }
|
---|
6798 |
|
---|
6799 | /**
|
---|
6800 | *
|
---|
6801 | * @note Similar code exists in i_taskExportHandler.
|
---|
6802 | */
|
---|
6803 | HRESULT Medium::i_addRawToFss(const char *aFilename, SecretKeyStore *pKeyStore, RTVFSFSSTREAM hVfsFssDst,
|
---|
6804 | const ComObjPtr<Progress> &aProgress, bool fSparse)
|
---|
6805 | {
|
---|
6806 | AutoCaller autoCaller(this);
|
---|
6807 | HRESULT hrc = autoCaller.rc();
|
---|
6808 | if (SUCCEEDED(hrc))
|
---|
6809 | {
|
---|
6810 | /*
|
---|
6811 | * Get a readonly hdd for this medium.
|
---|
6812 | */
|
---|
6813 | MediumCryptoFilterSettings CryptoSettingsRead;
|
---|
6814 | MediumLockList SourceMediumLockList;
|
---|
6815 | PVDISK pHdd;
|
---|
6816 | hrc = i_openForIO(false /*fWritable*/, pKeyStore, &pHdd, &SourceMediumLockList, &CryptoSettingsRead);
|
---|
6817 | if (SUCCEEDED(hrc))
|
---|
6818 | {
|
---|
6819 | /*
|
---|
6820 | * Create a VFS file interface to the HDD and attach a progress wrapper
|
---|
6821 | * that monitors the progress reading of the raw image. The image will
|
---|
6822 | * be read twice if hVfsFssDst does sparse processing.
|
---|
6823 | */
|
---|
6824 | RTVFSFILE hVfsFileDisk = NIL_RTVFSFILE;
|
---|
6825 | int vrc = VDCreateVfsFileFromDisk(pHdd, 0 /*fFlags*/, &hVfsFileDisk);
|
---|
6826 | if (RT_SUCCESS(vrc))
|
---|
6827 | {
|
---|
6828 | RTVFSFILE hVfsFileProgress = NIL_RTVFSFILE;
|
---|
6829 | vrc = RTVfsCreateProgressForFile(hVfsFileDisk, aProgress->i_iprtProgressCallback, &*aProgress,
|
---|
6830 | RTVFSPROGRESS_F_CANCELABLE | RTVFSPROGRESS_F_FORWARD_SEEK_AS_READ,
|
---|
6831 | VDGetSize(pHdd, VD_LAST_IMAGE) * (fSparse ? 2 : 1) /*cbExpectedRead*/,
|
---|
6832 | 0 /*cbExpectedWritten*/, &hVfsFileProgress);
|
---|
6833 | RTVfsFileRelease(hVfsFileDisk);
|
---|
6834 | if (RT_SUCCESS(vrc))
|
---|
6835 | {
|
---|
6836 | RTVFSOBJ hVfsObj = RTVfsObjFromFile(hVfsFileProgress);
|
---|
6837 | RTVfsFileRelease(hVfsFileProgress);
|
---|
6838 |
|
---|
6839 | vrc = RTVfsFsStrmAdd(hVfsFssDst, aFilename, hVfsObj, 0 /*fFlags*/);
|
---|
6840 | RTVfsObjRelease(hVfsObj);
|
---|
6841 | if (RT_FAILURE(vrc))
|
---|
6842 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Failed to add '%s' to output (%Rrc)"), aFilename, vrc);
|
---|
6843 | }
|
---|
6844 | else
|
---|
6845 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
6846 | tr("RTVfsCreateProgressForFile failed when processing '%s' (%Rrc)"), aFilename, vrc);
|
---|
6847 | }
|
---|
6848 | else
|
---|
6849 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("VDCreateVfsFileFromDisk failed for '%s' (%Rrc)"), aFilename, vrc);
|
---|
6850 | VDDestroy(pHdd);
|
---|
6851 | }
|
---|
6852 | }
|
---|
6853 | return hrc;
|
---|
6854 | }
|
---|
6855 |
|
---|
6856 | /**
|
---|
6857 | * Used by IAppliance to export disk images.
|
---|
6858 | *
|
---|
6859 | * @param aFilename Filename to create (UTF8).
|
---|
6860 | * @param aFormat Medium format for creating @a aFilename.
|
---|
6861 | * @param aVariant Which exact image format variant to use for the
|
---|
6862 | * destination image.
|
---|
6863 | * @param pKeyStore The optional key store for decrypting the data for
|
---|
6864 | * encrypted media during the export.
|
---|
6865 | * @param hVfsIosDst The destination I/O stream object.
|
---|
6866 | * @param aProgress Progress object to use.
|
---|
6867 | * @return
|
---|
6868 | *
|
---|
6869 | * @note The source format is defined by the Medium instance.
|
---|
6870 | */
|
---|
6871 | HRESULT Medium::i_exportFile(const char *aFilename,
|
---|
6872 | const ComObjPtr<MediumFormat> &aFormat,
|
---|
6873 | MediumVariant_T aVariant,
|
---|
6874 | SecretKeyStore *pKeyStore,
|
---|
6875 | RTVFSIOSTREAM hVfsIosDst,
|
---|
6876 | const ComObjPtr<Progress> &aProgress)
|
---|
6877 | {
|
---|
6878 | AssertPtrReturn(aFilename, E_INVALIDARG);
|
---|
6879 | AssertReturn(aFormat.isNotNull(), E_INVALIDARG);
|
---|
6880 | AssertReturn(aProgress.isNotNull(), E_INVALIDARG);
|
---|
6881 |
|
---|
6882 | AutoCaller autoCaller(this);
|
---|
6883 | HRESULT hrc = autoCaller.rc();
|
---|
6884 | if (SUCCEEDED(hrc))
|
---|
6885 | {
|
---|
6886 | /*
|
---|
6887 | * Setup VD interfaces.
|
---|
6888 | */
|
---|
6889 | PVDINTERFACE pVDImageIfaces = m->vdImageIfaces;
|
---|
6890 | PVDINTERFACEIO pVfsIoIf;
|
---|
6891 | int vrc = VDIfCreateFromVfsStream(hVfsIosDst, RTFILE_O_WRITE, &pVfsIoIf);
|
---|
6892 | if (RT_SUCCESS(vrc))
|
---|
6893 | {
|
---|
6894 | vrc = VDInterfaceAdd(&pVfsIoIf->Core, "Medium::ExportTaskVfsIos", VDINTERFACETYPE_IO,
|
---|
6895 | pVfsIoIf, sizeof(VDINTERFACEIO), &pVDImageIfaces);
|
---|
6896 | if (RT_SUCCESS(vrc))
|
---|
6897 | {
|
---|
6898 | /*
|
---|
6899 | * Get a readonly hdd for this medium (source).
|
---|
6900 | */
|
---|
6901 | MediumCryptoFilterSettings CryptoSettingsRead;
|
---|
6902 | MediumLockList SourceMediumLockList;
|
---|
6903 | PVDISK pSrcHdd;
|
---|
6904 | hrc = i_openForIO(false /*fWritable*/, pKeyStore, &pSrcHdd, &SourceMediumLockList, &CryptoSettingsRead);
|
---|
6905 | if (SUCCEEDED(hrc))
|
---|
6906 | {
|
---|
6907 | /*
|
---|
6908 | * Create the target medium.
|
---|
6909 | */
|
---|
6910 | Utf8Str strDstFormat(aFormat->i_getId());
|
---|
6911 |
|
---|
6912 | /* ensure the target directory exists */
|
---|
6913 | uint64_t fDstCapabilities = aFormat->i_getCapabilities();
|
---|
6914 | if (fDstCapabilities & MediumFormatCapabilities_File)
|
---|
6915 | {
|
---|
6916 | Utf8Str strDstLocation(aFilename);
|
---|
6917 | hrc = VirtualBox::i_ensureFilePathExists(strDstLocation.c_str(),
|
---|
6918 | !(aVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
6919 | }
|
---|
6920 | if (SUCCEEDED(hrc))
|
---|
6921 | {
|
---|
6922 | PVDISK pDstHdd;
|
---|
6923 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDstHdd);
|
---|
6924 | if (RT_SUCCESS(vrc))
|
---|
6925 | {
|
---|
6926 | /*
|
---|
6927 | * Create an interface for getting progress callbacks.
|
---|
6928 | */
|
---|
6929 | VDINTERFACEPROGRESS ProgressIf = VDINTERFACEPROGRESS_INITALIZER(aProgress->i_vdProgressCallback);
|
---|
6930 | PVDINTERFACE pProgress = NULL;
|
---|
6931 | vrc = VDInterfaceAdd(&ProgressIf.Core, "export-progress", VDINTERFACETYPE_PROGRESS,
|
---|
6932 | &*aProgress, sizeof(ProgressIf), &pProgress);
|
---|
6933 | AssertRC(vrc);
|
---|
6934 |
|
---|
6935 | /*
|
---|
6936 | * Do the exporting.
|
---|
6937 | */
|
---|
6938 | vrc = VDCopy(pSrcHdd,
|
---|
6939 | VD_LAST_IMAGE,
|
---|
6940 | pDstHdd,
|
---|
6941 | strDstFormat.c_str(),
|
---|
6942 | aFilename,
|
---|
6943 | false /* fMoveByRename */,
|
---|
6944 | 0 /* cbSize */,
|
---|
6945 | aVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted),
|
---|
6946 | NULL /* pDstUuid */,
|
---|
6947 | VD_OPEN_FLAGS_NORMAL | VD_OPEN_FLAGS_SEQUENTIAL,
|
---|
6948 | pProgress,
|
---|
6949 | pVDImageIfaces,
|
---|
6950 | NULL);
|
---|
6951 | if (RT_SUCCESS(vrc))
|
---|
6952 | hrc = S_OK;
|
---|
6953 | else
|
---|
6954 | hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Could not create the exported medium '%s'%s"),
|
---|
6955 | aFilename, i_vdError(vrc).c_str());
|
---|
6956 | VDDestroy(pDstHdd);
|
---|
6957 | }
|
---|
6958 | else
|
---|
6959 | hrc = setErrorVrc(vrc);
|
---|
6960 | }
|
---|
6961 | }
|
---|
6962 | VDDestroy(pSrcHdd);
|
---|
6963 | }
|
---|
6964 | else
|
---|
6965 | hrc = setErrorVrc(vrc, "VDInterfaceAdd -> %Rrc", vrc);
|
---|
6966 | VDIfDestroyFromVfsStream(pVfsIoIf);
|
---|
6967 | }
|
---|
6968 | else
|
---|
6969 | hrc = setErrorVrc(vrc, "VDIfCreateFromVfsStream -> %Rrc", vrc);
|
---|
6970 | }
|
---|
6971 | return hrc;
|
---|
6972 | }
|
---|
6973 |
|
---|
6974 | /**
|
---|
6975 | * Used by IAppliance to import disk images.
|
---|
6976 | *
|
---|
6977 | * @param aFilename Filename to read (UTF8).
|
---|
6978 | * @param aFormat Medium format for reading @a aFilename.
|
---|
6979 | * @param aVariant Which exact image format variant to use
|
---|
6980 | * for the destination image.
|
---|
6981 | * @param aVfsIosSrc Handle to the source I/O stream.
|
---|
6982 | * @param aParent Parent medium. May be NULL.
|
---|
6983 | * @param aProgress Progress object to use.
|
---|
6984 | * @param aNotify Notify about mediums which metadatа are changed
|
---|
6985 | * during execution of the function.
|
---|
6986 | * @return
|
---|
6987 | * @note The destination format is defined by the Medium instance.
|
---|
6988 | *
|
---|
6989 | * @todo The only consumer of this method (Appliance::i_importOneDiskImage) is
|
---|
6990 | * already on a worker thread, so perhaps consider bypassing the thread
|
---|
6991 | * here and run in the task synchronously? VBoxSVC has enough threads as
|
---|
6992 | * it is...
|
---|
6993 | */
|
---|
6994 | HRESULT Medium::i_importFile(const char *aFilename,
|
---|
6995 | const ComObjPtr<MediumFormat> &aFormat,
|
---|
6996 | MediumVariant_T aVariant,
|
---|
6997 | RTVFSIOSTREAM aVfsIosSrc,
|
---|
6998 | const ComObjPtr<Medium> &aParent,
|
---|
6999 | const ComObjPtr<Progress> &aProgress,
|
---|
7000 | bool aNotify)
|
---|
7001 | {
|
---|
7002 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
7003 | * to lock order violations, it probably causes lock order issues related
|
---|
7004 | * to the AutoCaller usage. */
|
---|
7005 | AssertPtrReturn(aFilename, E_INVALIDARG);
|
---|
7006 | AssertReturn(!aFormat.isNull(), E_INVALIDARG);
|
---|
7007 | AssertReturn(!aProgress.isNull(), E_INVALIDARG);
|
---|
7008 |
|
---|
7009 | AutoCaller autoCaller(this);
|
---|
7010 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
7011 |
|
---|
7012 | HRESULT rc = S_OK;
|
---|
7013 | Medium::Task *pTask = NULL;
|
---|
7014 |
|
---|
7015 | try
|
---|
7016 | {
|
---|
7017 | // locking: we need the tree lock first because we access parent pointers
|
---|
7018 | // and we need to write-lock the media involved
|
---|
7019 | uint32_t cHandles = 2;
|
---|
7020 | LockHandle* pHandles[3] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
7021 | this->lockHandle() };
|
---|
7022 | /* Only add parent to the lock if it is not null */
|
---|
7023 | if (!aParent.isNull())
|
---|
7024 | pHandles[cHandles++] = aParent->lockHandle();
|
---|
7025 | AutoWriteLock alock(cHandles,
|
---|
7026 | pHandles
|
---|
7027 | COMMA_LOCKVAL_SRC_POS);
|
---|
7028 |
|
---|
7029 | if ( m->state != MediumState_NotCreated
|
---|
7030 | && m->state != MediumState_Created)
|
---|
7031 | throw i_setStateError();
|
---|
7032 |
|
---|
7033 | /* Build the target lock list. */
|
---|
7034 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
7035 | alock.release();
|
---|
7036 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
7037 | this /* pToLockWrite */,
|
---|
7038 | false /* fMediumLockWriteAll */,
|
---|
7039 | aParent,
|
---|
7040 | *pTargetMediumLockList);
|
---|
7041 | alock.acquire();
|
---|
7042 | if (FAILED(rc))
|
---|
7043 | {
|
---|
7044 | delete pTargetMediumLockList;
|
---|
7045 | throw rc;
|
---|
7046 | }
|
---|
7047 |
|
---|
7048 | alock.release();
|
---|
7049 | rc = pTargetMediumLockList->Lock();
|
---|
7050 | alock.acquire();
|
---|
7051 | if (FAILED(rc))
|
---|
7052 | {
|
---|
7053 | delete pTargetMediumLockList;
|
---|
7054 | throw setError(rc,
|
---|
7055 | tr("Failed to lock target media '%s'"),
|
---|
7056 | i_getLocationFull().c_str());
|
---|
7057 | }
|
---|
7058 |
|
---|
7059 | /* setup task object to carry out the operation asynchronously */
|
---|
7060 | pTask = new Medium::ImportTask(this, aProgress, aFilename, aFormat, aVariant,
|
---|
7061 | aVfsIosSrc, aParent, pTargetMediumLockList, false, aNotify);
|
---|
7062 | rc = pTask->rc();
|
---|
7063 | AssertComRC(rc);
|
---|
7064 | if (FAILED(rc))
|
---|
7065 | throw rc;
|
---|
7066 |
|
---|
7067 | if (m->state == MediumState_NotCreated)
|
---|
7068 | m->state = MediumState_Creating;
|
---|
7069 | }
|
---|
7070 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7071 |
|
---|
7072 | if (SUCCEEDED(rc))
|
---|
7073 | {
|
---|
7074 | rc = pTask->createThread();
|
---|
7075 | pTask = NULL;
|
---|
7076 | }
|
---|
7077 | else if (pTask != NULL)
|
---|
7078 | delete pTask;
|
---|
7079 |
|
---|
7080 | return rc;
|
---|
7081 | }
|
---|
7082 |
|
---|
7083 | /**
|
---|
7084 | * Internal version of the public CloneTo API which allows to enable certain
|
---|
7085 | * optimizations to improve speed during VM cloning.
|
---|
7086 | *
|
---|
7087 | * @param aTarget Target medium
|
---|
7088 | * @param aVariant Which exact image format variant to use
|
---|
7089 | * for the destination image.
|
---|
7090 | * @param aParent Parent medium. May be NULL.
|
---|
7091 | * @param aProgress Progress object to use.
|
---|
7092 | * @param idxSrcImageSame The last image in the source chain which has the
|
---|
7093 | * same content as the given image in the destination
|
---|
7094 | * chain. Use UINT32_MAX to disable this optimization.
|
---|
7095 | * @param idxDstImageSame The last image in the destination chain which has the
|
---|
7096 | * same content as the given image in the source chain.
|
---|
7097 | * Use UINT32_MAX to disable this optimization.
|
---|
7098 | * @param aNotify Notify about mediums which metadatа are changed
|
---|
7099 | * during execution of the function.
|
---|
7100 | * @return
|
---|
7101 | */
|
---|
7102 | HRESULT Medium::i_cloneToEx(const ComObjPtr<Medium> &aTarget, MediumVariant_T aVariant,
|
---|
7103 | const ComObjPtr<Medium> &aParent, IProgress **aProgress,
|
---|
7104 | uint32_t idxSrcImageSame, uint32_t idxDstImageSame, bool aNotify)
|
---|
7105 | {
|
---|
7106 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
7107 | * to lock order violations, it probably causes lock order issues related
|
---|
7108 | * to the AutoCaller usage. */
|
---|
7109 | CheckComArgNotNull(aTarget);
|
---|
7110 | CheckComArgOutPointerValid(aProgress);
|
---|
7111 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
7112 |
|
---|
7113 | AutoCaller autoCaller(this);
|
---|
7114 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
7115 |
|
---|
7116 | HRESULT rc = S_OK;
|
---|
7117 | ComObjPtr<Progress> pProgress;
|
---|
7118 | Medium::Task *pTask = NULL;
|
---|
7119 |
|
---|
7120 | try
|
---|
7121 | {
|
---|
7122 | // locking: we need the tree lock first because we access parent pointers
|
---|
7123 | // and we need to write-lock the media involved
|
---|
7124 | uint32_t cHandles = 3;
|
---|
7125 | LockHandle* pHandles[4] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
7126 | this->lockHandle(),
|
---|
7127 | aTarget->lockHandle() };
|
---|
7128 | /* Only add parent to the lock if it is not null */
|
---|
7129 | if (!aParent.isNull())
|
---|
7130 | pHandles[cHandles++] = aParent->lockHandle();
|
---|
7131 | AutoWriteLock alock(cHandles,
|
---|
7132 | pHandles
|
---|
7133 | COMMA_LOCKVAL_SRC_POS);
|
---|
7134 |
|
---|
7135 | if ( aTarget->m->state != MediumState_NotCreated
|
---|
7136 | && aTarget->m->state != MediumState_Created)
|
---|
7137 | throw aTarget->i_setStateError();
|
---|
7138 |
|
---|
7139 | /* Build the source lock list. */
|
---|
7140 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
7141 | alock.release();
|
---|
7142 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
7143 | NULL /* pToLockWrite */,
|
---|
7144 | false /* fMediumLockWriteAll */,
|
---|
7145 | NULL,
|
---|
7146 | *pSourceMediumLockList);
|
---|
7147 | alock.acquire();
|
---|
7148 | if (FAILED(rc))
|
---|
7149 | {
|
---|
7150 | delete pSourceMediumLockList;
|
---|
7151 | throw rc;
|
---|
7152 | }
|
---|
7153 |
|
---|
7154 | /* Build the target lock list (including the to-be parent chain). */
|
---|
7155 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
7156 | alock.release();
|
---|
7157 | rc = aTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
7158 | aTarget /* pToLockWrite */,
|
---|
7159 | false /* fMediumLockWriteAll */,
|
---|
7160 | aParent,
|
---|
7161 | *pTargetMediumLockList);
|
---|
7162 | alock.acquire();
|
---|
7163 | if (FAILED(rc))
|
---|
7164 | {
|
---|
7165 | delete pSourceMediumLockList;
|
---|
7166 | delete pTargetMediumLockList;
|
---|
7167 | throw rc;
|
---|
7168 | }
|
---|
7169 |
|
---|
7170 | alock.release();
|
---|
7171 | rc = pSourceMediumLockList->Lock();
|
---|
7172 | alock.acquire();
|
---|
7173 | if (FAILED(rc))
|
---|
7174 | {
|
---|
7175 | delete pSourceMediumLockList;
|
---|
7176 | delete pTargetMediumLockList;
|
---|
7177 | throw setError(rc,
|
---|
7178 | tr("Failed to lock source media '%s'"),
|
---|
7179 | i_getLocationFull().c_str());
|
---|
7180 | }
|
---|
7181 | alock.release();
|
---|
7182 | rc = pTargetMediumLockList->Lock();
|
---|
7183 | alock.acquire();
|
---|
7184 | if (FAILED(rc))
|
---|
7185 | {
|
---|
7186 | delete pSourceMediumLockList;
|
---|
7187 | delete pTargetMediumLockList;
|
---|
7188 | throw setError(rc,
|
---|
7189 | tr("Failed to lock target media '%s'"),
|
---|
7190 | aTarget->i_getLocationFull().c_str());
|
---|
7191 | }
|
---|
7192 |
|
---|
7193 | pProgress.createObject();
|
---|
7194 | rc = pProgress->init(m->pVirtualBox,
|
---|
7195 | static_cast <IMedium *>(this),
|
---|
7196 | BstrFmt(tr("Creating clone medium '%s'"), aTarget->m->strLocationFull.c_str()).raw(),
|
---|
7197 | TRUE /* aCancelable */);
|
---|
7198 | if (FAILED(rc))
|
---|
7199 | {
|
---|
7200 | delete pSourceMediumLockList;
|
---|
7201 | delete pTargetMediumLockList;
|
---|
7202 | throw rc;
|
---|
7203 | }
|
---|
7204 |
|
---|
7205 | /* setup task object to carry out the operation asynchronously */
|
---|
7206 | pTask = new Medium::CloneTask(this, pProgress, aTarget, aVariant,
|
---|
7207 | aParent, idxSrcImageSame,
|
---|
7208 | idxDstImageSame, pSourceMediumLockList,
|
---|
7209 | pTargetMediumLockList, false, false, aNotify);
|
---|
7210 | rc = pTask->rc();
|
---|
7211 | AssertComRC(rc);
|
---|
7212 | if (FAILED(rc))
|
---|
7213 | throw rc;
|
---|
7214 |
|
---|
7215 | if (aTarget->m->state == MediumState_NotCreated)
|
---|
7216 | aTarget->m->state = MediumState_Creating;
|
---|
7217 | }
|
---|
7218 | catch (HRESULT aRC) { rc = aRC; }
|
---|
7219 |
|
---|
7220 | if (SUCCEEDED(rc))
|
---|
7221 | {
|
---|
7222 | rc = pTask->createThread();
|
---|
7223 | pTask = NULL;
|
---|
7224 | if (SUCCEEDED(rc))
|
---|
7225 | pProgress.queryInterfaceTo(aProgress);
|
---|
7226 | }
|
---|
7227 | else if (pTask != NULL)
|
---|
7228 | delete pTask;
|
---|
7229 |
|
---|
7230 | return rc;
|
---|
7231 | }
|
---|
7232 |
|
---|
7233 | /**
|
---|
7234 | * Returns the key identifier for this medium if encryption is configured.
|
---|
7235 | *
|
---|
7236 | * @returns Key identifier or empty string if no encryption is configured.
|
---|
7237 | */
|
---|
7238 | const Utf8Str& Medium::i_getKeyId()
|
---|
7239 | {
|
---|
7240 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
7241 |
|
---|
7242 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7243 |
|
---|
7244 | settings::StringsMap::const_iterator it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
7245 | if (it == pBase->m->mapProperties.end())
|
---|
7246 | return Utf8Str::Empty;
|
---|
7247 |
|
---|
7248 | return it->second;
|
---|
7249 | }
|
---|
7250 |
|
---|
7251 |
|
---|
7252 | /**
|
---|
7253 | * Returns all filter related properties.
|
---|
7254 | *
|
---|
7255 | * @returns COM status code.
|
---|
7256 | * @param aReturnNames Where to store the properties names on success.
|
---|
7257 | * @param aReturnValues Where to store the properties values on success.
|
---|
7258 | */
|
---|
7259 | HRESULT Medium::i_getFilterProperties(std::vector<com::Utf8Str> &aReturnNames,
|
---|
7260 | std::vector<com::Utf8Str> &aReturnValues)
|
---|
7261 | {
|
---|
7262 | std::vector<com::Utf8Str> aPropNames;
|
---|
7263 | std::vector<com::Utf8Str> aPropValues;
|
---|
7264 | HRESULT hrc = getProperties(Utf8Str(""), aPropNames, aPropValues);
|
---|
7265 |
|
---|
7266 | if (SUCCEEDED(hrc))
|
---|
7267 | {
|
---|
7268 | unsigned cReturnSize = 0;
|
---|
7269 | aReturnNames.resize(0);
|
---|
7270 | aReturnValues.resize(0);
|
---|
7271 | for (unsigned idx = 0; idx < aPropNames.size(); idx++)
|
---|
7272 | {
|
---|
7273 | if (i_isPropertyForFilter(aPropNames[idx]))
|
---|
7274 | {
|
---|
7275 | aReturnNames.resize(cReturnSize + 1);
|
---|
7276 | aReturnValues.resize(cReturnSize + 1);
|
---|
7277 | aReturnNames[cReturnSize] = aPropNames[idx];
|
---|
7278 | aReturnValues[cReturnSize] = aPropValues[idx];
|
---|
7279 | cReturnSize++;
|
---|
7280 | }
|
---|
7281 | }
|
---|
7282 | }
|
---|
7283 |
|
---|
7284 | return hrc;
|
---|
7285 | }
|
---|
7286 |
|
---|
7287 | /**
|
---|
7288 | * Preparation to move this medium to a new location
|
---|
7289 | *
|
---|
7290 | * @param aLocation Location of the storage unit. If the location is a FS-path,
|
---|
7291 | * then it can be relative to the VirtualBox home directory.
|
---|
7292 | *
|
---|
7293 | * @note Must be called from under this object's write lock.
|
---|
7294 | */
|
---|
7295 | HRESULT Medium::i_preparationForMoving(const Utf8Str &aLocation)
|
---|
7296 | {
|
---|
7297 | HRESULT rc = E_FAIL;
|
---|
7298 |
|
---|
7299 | if (i_getLocationFull() != aLocation)
|
---|
7300 | {
|
---|
7301 | m->strNewLocationFull = aLocation;
|
---|
7302 | m->fMoveThisMedium = true;
|
---|
7303 | rc = S_OK;
|
---|
7304 | }
|
---|
7305 |
|
---|
7306 | return rc;
|
---|
7307 | }
|
---|
7308 |
|
---|
7309 | /**
|
---|
7310 | * Checking whether current operation "moving" or not
|
---|
7311 | */
|
---|
7312 | bool Medium::i_isMoveOperation(const ComObjPtr<Medium> &aTarget) const
|
---|
7313 | {
|
---|
7314 | RT_NOREF(aTarget);
|
---|
7315 | return m->fMoveThisMedium;
|
---|
7316 | }
|
---|
7317 |
|
---|
7318 | bool Medium::i_resetMoveOperationData()
|
---|
7319 | {
|
---|
7320 | m->strNewLocationFull.setNull();
|
---|
7321 | m->fMoveThisMedium = false;
|
---|
7322 | return true;
|
---|
7323 | }
|
---|
7324 |
|
---|
7325 | Utf8Str Medium::i_getNewLocationForMoving() const
|
---|
7326 | {
|
---|
7327 | if (m->fMoveThisMedium == true)
|
---|
7328 | return m->strNewLocationFull;
|
---|
7329 | else
|
---|
7330 | return Utf8Str();
|
---|
7331 | }
|
---|
7332 | ////////////////////////////////////////////////////////////////////////////////
|
---|
7333 | //
|
---|
7334 | // Private methods
|
---|
7335 | //
|
---|
7336 | ////////////////////////////////////////////////////////////////////////////////
|
---|
7337 |
|
---|
7338 | /**
|
---|
7339 | * Queries information from the medium.
|
---|
7340 | *
|
---|
7341 | * As a result of this call, the accessibility state and data members such as
|
---|
7342 | * size and description will be updated with the current information.
|
---|
7343 | *
|
---|
7344 | * @note This method may block during a system I/O call that checks storage
|
---|
7345 | * accessibility.
|
---|
7346 | *
|
---|
7347 | * @note Caller MUST NOT hold the media tree or medium lock.
|
---|
7348 | *
|
---|
7349 | * @note Locks m->pParent for reading. Locks this object for writing.
|
---|
7350 | *
|
---|
7351 | * @param fSetImageId Whether to reset the UUID contained in the image file
|
---|
7352 | * to the UUID in the medium instance data (see SetIDs())
|
---|
7353 | * @param fSetParentId Whether to reset the parent UUID contained in the image
|
---|
7354 | * file to the parent UUID in the medium instance data (see
|
---|
7355 | * SetIDs())
|
---|
7356 | * @param autoCaller
|
---|
7357 | * @return
|
---|
7358 | */
|
---|
7359 | HRESULT Medium::i_queryInfo(bool fSetImageId, bool fSetParentId, AutoCaller &autoCaller)
|
---|
7360 | {
|
---|
7361 | Assert(!isWriteLockOnCurrentThread());
|
---|
7362 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7363 |
|
---|
7364 | if ( ( m->state != MediumState_Created
|
---|
7365 | && m->state != MediumState_Inaccessible
|
---|
7366 | && m->state != MediumState_LockedRead)
|
---|
7367 | || m->fClosing)
|
---|
7368 | return E_FAIL;
|
---|
7369 |
|
---|
7370 | HRESULT rc = S_OK;
|
---|
7371 |
|
---|
7372 | int vrc = VINF_SUCCESS;
|
---|
7373 |
|
---|
7374 | /* check if a blocking i_queryInfo() call is in progress on some other thread,
|
---|
7375 | * and wait for it to finish if so instead of querying data ourselves */
|
---|
7376 | if (m->queryInfoRunning)
|
---|
7377 | {
|
---|
7378 | Assert( m->state == MediumState_LockedRead
|
---|
7379 | || m->state == MediumState_LockedWrite);
|
---|
7380 |
|
---|
7381 | while (m->queryInfoRunning)
|
---|
7382 | {
|
---|
7383 | alock.release();
|
---|
7384 | /* must not hold the object lock now */
|
---|
7385 | Assert(!isWriteLockOnCurrentThread());
|
---|
7386 | {
|
---|
7387 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
7388 | }
|
---|
7389 | alock.acquire();
|
---|
7390 | }
|
---|
7391 |
|
---|
7392 | return S_OK;
|
---|
7393 | }
|
---|
7394 |
|
---|
7395 | bool success = false;
|
---|
7396 | Utf8Str lastAccessError;
|
---|
7397 |
|
---|
7398 | /* are we dealing with a new medium constructed using the existing
|
---|
7399 | * location? */
|
---|
7400 | bool isImport = m->id.isZero();
|
---|
7401 | unsigned uOpenFlags = VD_OPEN_FLAGS_INFO;
|
---|
7402 |
|
---|
7403 | /* Note that we don't use VD_OPEN_FLAGS_READONLY when opening new
|
---|
7404 | * media because that would prevent necessary modifications
|
---|
7405 | * when opening media of some third-party formats for the first
|
---|
7406 | * time in VirtualBox (such as VMDK for which VDOpen() needs to
|
---|
7407 | * generate an UUID if it is missing) */
|
---|
7408 | if ( m->hddOpenMode == OpenReadOnly
|
---|
7409 | || m->type == MediumType_Readonly
|
---|
7410 | || (!isImport && !fSetImageId && !fSetParentId)
|
---|
7411 | )
|
---|
7412 | uOpenFlags |= VD_OPEN_FLAGS_READONLY;
|
---|
7413 |
|
---|
7414 | /* Open shareable medium with the appropriate flags */
|
---|
7415 | if (m->type == MediumType_Shareable)
|
---|
7416 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
7417 |
|
---|
7418 | /* Lock the medium, which makes the behavior much more consistent, must be
|
---|
7419 | * done before dropping the object lock and setting queryInfoRunning. */
|
---|
7420 | ComPtr<IToken> pToken;
|
---|
7421 | if (uOpenFlags & (VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SHAREABLE))
|
---|
7422 | rc = LockRead(pToken.asOutParam());
|
---|
7423 | else
|
---|
7424 | rc = LockWrite(pToken.asOutParam());
|
---|
7425 | if (FAILED(rc)) return rc;
|
---|
7426 |
|
---|
7427 | /* Copies of the input state fields which are not read-only,
|
---|
7428 | * as we're dropping the lock. CAUTION: be extremely careful what
|
---|
7429 | * you do with the contents of this medium object, as you will
|
---|
7430 | * create races if there are concurrent changes. */
|
---|
7431 | Utf8Str format(m->strFormat);
|
---|
7432 | Utf8Str location(m->strLocationFull);
|
---|
7433 | ComObjPtr<MediumFormat> formatObj = m->formatObj;
|
---|
7434 |
|
---|
7435 | /* "Output" values which can't be set because the lock isn't held
|
---|
7436 | * at the time the values are determined. */
|
---|
7437 | Guid mediumId = m->id;
|
---|
7438 | uint64_t mediumSize = 0;
|
---|
7439 | uint64_t mediumLogicalSize = 0;
|
---|
7440 |
|
---|
7441 | /* Flag whether a base image has a non-zero parent UUID and thus
|
---|
7442 | * need repairing after it was closed again. */
|
---|
7443 | bool fRepairImageZeroParentUuid = false;
|
---|
7444 |
|
---|
7445 | ComObjPtr<VirtualBox> pVirtualBox = m->pVirtualBox;
|
---|
7446 |
|
---|
7447 | /* must be set before leaving the object lock the first time */
|
---|
7448 | m->queryInfoRunning = true;
|
---|
7449 |
|
---|
7450 | /* must leave object lock now, because a lock from a higher lock class
|
---|
7451 | * is needed and also a lengthy operation is coming */
|
---|
7452 | alock.release();
|
---|
7453 | autoCaller.release();
|
---|
7454 |
|
---|
7455 | /* Note that taking the queryInfoSem after leaving the object lock above
|
---|
7456 | * can lead to short spinning of the loops waiting for i_queryInfo() to
|
---|
7457 | * complete. This is unavoidable since the other order causes a lock order
|
---|
7458 | * violation: here it would be requesting the object lock (at the beginning
|
---|
7459 | * of the method), then queryInfoSem, and below the other way round. */
|
---|
7460 | AutoWriteLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
7461 |
|
---|
7462 | /* take the opportunity to have a media tree lock, released initially */
|
---|
7463 | Assert(!isWriteLockOnCurrentThread());
|
---|
7464 | Assert(!pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
7465 | AutoWriteLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
7466 | treeLock.release();
|
---|
7467 |
|
---|
7468 | /* re-take the caller, but not the object lock, to keep uninit away */
|
---|
7469 | autoCaller.add();
|
---|
7470 | if (FAILED(autoCaller.rc()))
|
---|
7471 | {
|
---|
7472 | m->queryInfoRunning = false;
|
---|
7473 | return autoCaller.rc();
|
---|
7474 | }
|
---|
7475 |
|
---|
7476 | try
|
---|
7477 | {
|
---|
7478 | /* skip accessibility checks for host drives */
|
---|
7479 | if (m->hostDrive)
|
---|
7480 | {
|
---|
7481 | success = true;
|
---|
7482 | throw S_OK;
|
---|
7483 | }
|
---|
7484 |
|
---|
7485 | PVDISK hdd;
|
---|
7486 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
7487 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7488 |
|
---|
7489 | try
|
---|
7490 | {
|
---|
7491 | /** @todo This kind of opening of media is assuming that diff
|
---|
7492 | * media can be opened as base media. Should be documented that
|
---|
7493 | * it must work for all medium format backends. */
|
---|
7494 | vrc = VDOpen(hdd,
|
---|
7495 | format.c_str(),
|
---|
7496 | location.c_str(),
|
---|
7497 | uOpenFlags | m->uOpenFlagsDef,
|
---|
7498 | m->vdImageIfaces);
|
---|
7499 | if (RT_FAILURE(vrc))
|
---|
7500 | {
|
---|
7501 | lastAccessError = Utf8StrFmt(tr("Could not open the medium '%s'%s"),
|
---|
7502 | location.c_str(), i_vdError(vrc).c_str());
|
---|
7503 | throw S_OK;
|
---|
7504 | }
|
---|
7505 |
|
---|
7506 | if (formatObj->i_getCapabilities() & MediumFormatCapabilities_Uuid)
|
---|
7507 | {
|
---|
7508 | /* Modify the UUIDs if necessary. The associated fields are
|
---|
7509 | * not modified by other code, so no need to copy. */
|
---|
7510 | if (fSetImageId)
|
---|
7511 | {
|
---|
7512 | alock.acquire();
|
---|
7513 | vrc = VDSetUuid(hdd, 0, m->uuidImage.raw());
|
---|
7514 | alock.release();
|
---|
7515 | if (RT_FAILURE(vrc))
|
---|
7516 | {
|
---|
7517 | lastAccessError = Utf8StrFmt(tr("Could not update the UUID of medium '%s'%s"),
|
---|
7518 | location.c_str(), i_vdError(vrc).c_str());
|
---|
7519 | throw S_OK;
|
---|
7520 | }
|
---|
7521 | mediumId = m->uuidImage;
|
---|
7522 | }
|
---|
7523 | if (fSetParentId)
|
---|
7524 | {
|
---|
7525 | alock.acquire();
|
---|
7526 | vrc = VDSetParentUuid(hdd, 0, m->uuidParentImage.raw());
|
---|
7527 | alock.release();
|
---|
7528 | if (RT_FAILURE(vrc))
|
---|
7529 | {
|
---|
7530 | lastAccessError = Utf8StrFmt(tr("Could not update the parent UUID of medium '%s'%s"),
|
---|
7531 | location.c_str(), i_vdError(vrc).c_str());
|
---|
7532 | throw S_OK;
|
---|
7533 | }
|
---|
7534 | }
|
---|
7535 | /* zap the information, these are no long-term members */
|
---|
7536 | alock.acquire();
|
---|
7537 | unconst(m->uuidImage).clear();
|
---|
7538 | unconst(m->uuidParentImage).clear();
|
---|
7539 | alock.release();
|
---|
7540 |
|
---|
7541 | /* check the UUID */
|
---|
7542 | RTUUID uuid;
|
---|
7543 | vrc = VDGetUuid(hdd, 0, &uuid);
|
---|
7544 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7545 |
|
---|
7546 | if (isImport)
|
---|
7547 | {
|
---|
7548 | mediumId = uuid;
|
---|
7549 |
|
---|
7550 | if (mediumId.isZero() && (m->hddOpenMode == OpenReadOnly))
|
---|
7551 | // only when importing a VDMK that has no UUID, create one in memory
|
---|
7552 | mediumId.create();
|
---|
7553 | }
|
---|
7554 | else
|
---|
7555 | {
|
---|
7556 | Assert(!mediumId.isZero());
|
---|
7557 |
|
---|
7558 | if (mediumId != uuid)
|
---|
7559 | {
|
---|
7560 | /** @todo r=klaus this always refers to VirtualBox.xml as the medium registry, even for new VMs */
|
---|
7561 | lastAccessError = Utf8StrFmt(
|
---|
7562 | tr("UUID {%RTuuid} of the medium '%s' does not match the value {%RTuuid} stored in the media registry ('%s')"),
|
---|
7563 | &uuid,
|
---|
7564 | location.c_str(),
|
---|
7565 | mediumId.raw(),
|
---|
7566 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
7567 | throw S_OK;
|
---|
7568 | }
|
---|
7569 | }
|
---|
7570 | }
|
---|
7571 | else
|
---|
7572 | {
|
---|
7573 | /* the backend does not support storing UUIDs within the
|
---|
7574 | * underlying storage so use what we store in XML */
|
---|
7575 |
|
---|
7576 | if (fSetImageId)
|
---|
7577 | {
|
---|
7578 | /* set the UUID if an API client wants to change it */
|
---|
7579 | alock.acquire();
|
---|
7580 | mediumId = m->uuidImage;
|
---|
7581 | alock.release();
|
---|
7582 | }
|
---|
7583 | else if (isImport)
|
---|
7584 | {
|
---|
7585 | /* generate an UUID for an imported UUID-less medium */
|
---|
7586 | mediumId.create();
|
---|
7587 | }
|
---|
7588 | }
|
---|
7589 |
|
---|
7590 | /* set the image uuid before the below parent uuid handling code
|
---|
7591 | * might place it somewhere in the media tree, so that the medium
|
---|
7592 | * UUID is valid at this point */
|
---|
7593 | alock.acquire();
|
---|
7594 | if (isImport || fSetImageId)
|
---|
7595 | unconst(m->id) = mediumId;
|
---|
7596 | alock.release();
|
---|
7597 |
|
---|
7598 | /* get the medium variant */
|
---|
7599 | unsigned uImageFlags;
|
---|
7600 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
7601 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7602 | alock.acquire();
|
---|
7603 | m->variant = (MediumVariant_T)uImageFlags;
|
---|
7604 | alock.release();
|
---|
7605 |
|
---|
7606 | /* check/get the parent uuid and update corresponding state */
|
---|
7607 | if (uImageFlags & VD_IMAGE_FLAGS_DIFF)
|
---|
7608 | {
|
---|
7609 | RTUUID parentId;
|
---|
7610 | vrc = VDGetParentUuid(hdd, 0, &parentId);
|
---|
7611 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7612 |
|
---|
7613 | /* streamOptimized VMDK images are only accepted as base
|
---|
7614 | * images, as this allows automatic repair of OVF appliances.
|
---|
7615 | * Since such images don't support random writes they will not
|
---|
7616 | * be created for diff images. Only an overly smart user might
|
---|
7617 | * manually create this case. Too bad for him. */
|
---|
7618 | if ( (isImport || fSetParentId)
|
---|
7619 | && !(uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
|
---|
7620 | {
|
---|
7621 | /* the parent must be known to us. Note that we freely
|
---|
7622 | * call locking methods of mVirtualBox and parent, as all
|
---|
7623 | * relevant locks must be already held. There may be no
|
---|
7624 | * concurrent access to the just opened medium on other
|
---|
7625 | * threads yet (and init() will fail if this method reports
|
---|
7626 | * MediumState_Inaccessible) */
|
---|
7627 |
|
---|
7628 | ComObjPtr<Medium> pParent;
|
---|
7629 | if (RTUuidIsNull(&parentId))
|
---|
7630 | rc = VBOX_E_OBJECT_NOT_FOUND;
|
---|
7631 | else
|
---|
7632 | rc = pVirtualBox->i_findHardDiskById(Guid(parentId), false /* aSetError */, &pParent);
|
---|
7633 | if (FAILED(rc))
|
---|
7634 | {
|
---|
7635 | if (fSetImageId && !fSetParentId)
|
---|
7636 | {
|
---|
7637 | /* If the image UUID gets changed for an existing
|
---|
7638 | * image then the parent UUID can be stale. In such
|
---|
7639 | * cases clear the parent information. The parent
|
---|
7640 | * information may/will be re-set later if the
|
---|
7641 | * API client wants to adjust a complete medium
|
---|
7642 | * hierarchy one by one. */
|
---|
7643 | rc = S_OK;
|
---|
7644 | alock.acquire();
|
---|
7645 | RTUuidClear(&parentId);
|
---|
7646 | vrc = VDSetParentUuid(hdd, 0, &parentId);
|
---|
7647 | alock.release();
|
---|
7648 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7649 | }
|
---|
7650 | else
|
---|
7651 | {
|
---|
7652 | lastAccessError = Utf8StrFmt(tr("Parent medium with UUID {%RTuuid} of the medium '%s' is not found in the media registry ('%s')"),
|
---|
7653 | &parentId, location.c_str(),
|
---|
7654 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
7655 | throw S_OK;
|
---|
7656 | }
|
---|
7657 | }
|
---|
7658 |
|
---|
7659 | /* must drop the caller before taking the tree lock */
|
---|
7660 | autoCaller.release();
|
---|
7661 | /* we set m->pParent & children() */
|
---|
7662 | treeLock.acquire();
|
---|
7663 | autoCaller.add();
|
---|
7664 | if (FAILED(autoCaller.rc()))
|
---|
7665 | throw autoCaller.rc();
|
---|
7666 |
|
---|
7667 | if (m->pParent)
|
---|
7668 | i_deparent();
|
---|
7669 |
|
---|
7670 | if (!pParent.isNull())
|
---|
7671 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
7672 | {
|
---|
7673 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
7674 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7675 | tr("Cannot open differencing image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
7676 | pParent->m->strLocationFull.c_str());
|
---|
7677 | }
|
---|
7678 | i_setParent(pParent);
|
---|
7679 |
|
---|
7680 | treeLock.release();
|
---|
7681 | }
|
---|
7682 | else
|
---|
7683 | {
|
---|
7684 | /* must drop the caller before taking the tree lock */
|
---|
7685 | autoCaller.release();
|
---|
7686 | /* we access m->pParent */
|
---|
7687 | treeLock.acquire();
|
---|
7688 | autoCaller.add();
|
---|
7689 | if (FAILED(autoCaller.rc()))
|
---|
7690 | throw autoCaller.rc();
|
---|
7691 |
|
---|
7692 | /* check that parent UUIDs match. Note that there's no need
|
---|
7693 | * for the parent's AutoCaller (our lifetime is bound to
|
---|
7694 | * it) */
|
---|
7695 |
|
---|
7696 | if (m->pParent.isNull())
|
---|
7697 | {
|
---|
7698 | /* Due to a bug in VDCopy() in VirtualBox 3.0.0-3.0.14
|
---|
7699 | * and 3.1.0-3.1.8 there are base images out there
|
---|
7700 | * which have a non-zero parent UUID. No point in
|
---|
7701 | * complaining about them, instead automatically
|
---|
7702 | * repair the problem. Later we can bring back the
|
---|
7703 | * error message, but we should wait until really
|
---|
7704 | * most users have repaired their images, either with
|
---|
7705 | * VBoxFixHdd or this way. */
|
---|
7706 | #if 1
|
---|
7707 | fRepairImageZeroParentUuid = true;
|
---|
7708 | #else /* 0 */
|
---|
7709 | lastAccessError = Utf8StrFmt(
|
---|
7710 | tr("Medium type of '%s' is differencing but it is not associated with any parent medium in the media registry ('%s')"),
|
---|
7711 | location.c_str(),
|
---|
7712 | pVirtualBox->settingsFilePath().c_str());
|
---|
7713 | treeLock.release();
|
---|
7714 | throw S_OK;
|
---|
7715 | #endif /* 0 */
|
---|
7716 | }
|
---|
7717 |
|
---|
7718 | {
|
---|
7719 | autoCaller.release();
|
---|
7720 | AutoReadLock parentLock(m->pParent COMMA_LOCKVAL_SRC_POS);
|
---|
7721 | autoCaller.add();
|
---|
7722 | if (FAILED(autoCaller.rc()))
|
---|
7723 | throw autoCaller.rc();
|
---|
7724 |
|
---|
7725 | if ( !fRepairImageZeroParentUuid
|
---|
7726 | && m->pParent->i_getState() != MediumState_Inaccessible
|
---|
7727 | && m->pParent->i_getId() != parentId)
|
---|
7728 | {
|
---|
7729 | /** @todo r=klaus this always refers to VirtualBox.xml as the medium registry, even for new VMs */
|
---|
7730 | lastAccessError = Utf8StrFmt(
|
---|
7731 | tr("Parent UUID {%RTuuid} of the medium '%s' does not match UUID {%RTuuid} of its parent medium stored in the media registry ('%s')"),
|
---|
7732 | &parentId, location.c_str(),
|
---|
7733 | m->pParent->i_getId().raw(),
|
---|
7734 | pVirtualBox->i_settingsFilePath().c_str());
|
---|
7735 | parentLock.release();
|
---|
7736 | treeLock.release();
|
---|
7737 | throw S_OK;
|
---|
7738 | }
|
---|
7739 | }
|
---|
7740 |
|
---|
7741 | /// @todo NEWMEDIA what to do if the parent is not
|
---|
7742 | /// accessible while the diff is? Probably nothing. The
|
---|
7743 | /// real code will detect the mismatch anyway.
|
---|
7744 |
|
---|
7745 | treeLock.release();
|
---|
7746 | }
|
---|
7747 | }
|
---|
7748 |
|
---|
7749 | mediumSize = VDGetFileSize(hdd, 0);
|
---|
7750 | mediumLogicalSize = VDGetSize(hdd, 0);
|
---|
7751 |
|
---|
7752 | success = true;
|
---|
7753 | }
|
---|
7754 | catch (HRESULT aRC)
|
---|
7755 | {
|
---|
7756 | rc = aRC;
|
---|
7757 | }
|
---|
7758 |
|
---|
7759 | vrc = VDDestroy(hdd);
|
---|
7760 | if (RT_FAILURE(vrc))
|
---|
7761 | {
|
---|
7762 | lastAccessError = Utf8StrFmt(tr("Could not update and close the medium '%s'%s"),
|
---|
7763 | location.c_str(), i_vdError(vrc).c_str());
|
---|
7764 | success = false;
|
---|
7765 | throw S_OK;
|
---|
7766 | }
|
---|
7767 | }
|
---|
7768 | catch (HRESULT aRC)
|
---|
7769 | {
|
---|
7770 | rc = aRC;
|
---|
7771 | }
|
---|
7772 |
|
---|
7773 | autoCaller.release();
|
---|
7774 | treeLock.acquire();
|
---|
7775 | autoCaller.add();
|
---|
7776 | if (FAILED(autoCaller.rc()))
|
---|
7777 | {
|
---|
7778 | m->queryInfoRunning = false;
|
---|
7779 | return autoCaller.rc();
|
---|
7780 | }
|
---|
7781 | alock.acquire();
|
---|
7782 |
|
---|
7783 | if (success)
|
---|
7784 | {
|
---|
7785 | m->size = mediumSize;
|
---|
7786 | m->logicalSize = mediumLogicalSize;
|
---|
7787 | m->strLastAccessError.setNull();
|
---|
7788 | }
|
---|
7789 | else
|
---|
7790 | {
|
---|
7791 | m->strLastAccessError = lastAccessError;
|
---|
7792 | Log1WarningFunc(("'%s' is not accessible (error='%s', rc=%Rhrc, vrc=%Rrc)\n",
|
---|
7793 | location.c_str(), m->strLastAccessError.c_str(), rc, vrc));
|
---|
7794 | }
|
---|
7795 |
|
---|
7796 | /* Set the proper state according to the result of the check */
|
---|
7797 | if (success)
|
---|
7798 | m->preLockState = MediumState_Created;
|
---|
7799 | else
|
---|
7800 | m->preLockState = MediumState_Inaccessible;
|
---|
7801 |
|
---|
7802 | /* unblock anyone waiting for the i_queryInfo results */
|
---|
7803 | qlock.release();
|
---|
7804 | m->queryInfoRunning = false;
|
---|
7805 |
|
---|
7806 | pToken->Abandon();
|
---|
7807 | pToken.setNull();
|
---|
7808 |
|
---|
7809 | if (FAILED(rc))
|
---|
7810 | return rc;
|
---|
7811 |
|
---|
7812 | /* If this is a base image which incorrectly has a parent UUID set,
|
---|
7813 | * repair the image now by zeroing the parent UUID. This is only done
|
---|
7814 | * when we have structural information from a config file, on import
|
---|
7815 | * this is not possible. If someone would accidentally call openMedium
|
---|
7816 | * with a diff image before the base is registered this would destroy
|
---|
7817 | * the diff. Not acceptable. */
|
---|
7818 | do
|
---|
7819 | {
|
---|
7820 | if (fRepairImageZeroParentUuid)
|
---|
7821 | {
|
---|
7822 | rc = LockWrite(pToken.asOutParam());
|
---|
7823 | if (FAILED(rc))
|
---|
7824 | break;
|
---|
7825 |
|
---|
7826 | alock.release();
|
---|
7827 |
|
---|
7828 | try
|
---|
7829 | {
|
---|
7830 | PVDISK hdd;
|
---|
7831 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
7832 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7833 |
|
---|
7834 | try
|
---|
7835 | {
|
---|
7836 | vrc = VDOpen(hdd,
|
---|
7837 | format.c_str(),
|
---|
7838 | location.c_str(),
|
---|
7839 | (uOpenFlags & ~VD_OPEN_FLAGS_READONLY) | m->uOpenFlagsDef,
|
---|
7840 | m->vdImageIfaces);
|
---|
7841 | if (RT_FAILURE(vrc))
|
---|
7842 | throw S_OK;
|
---|
7843 |
|
---|
7844 | RTUUID zeroParentUuid;
|
---|
7845 | RTUuidClear(&zeroParentUuid);
|
---|
7846 | vrc = VDSetParentUuid(hdd, 0, &zeroParentUuid);
|
---|
7847 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
7848 | }
|
---|
7849 | catch (HRESULT aRC)
|
---|
7850 | {
|
---|
7851 | rc = aRC;
|
---|
7852 | }
|
---|
7853 |
|
---|
7854 | VDDestroy(hdd);
|
---|
7855 | }
|
---|
7856 | catch (HRESULT aRC)
|
---|
7857 | {
|
---|
7858 | rc = aRC;
|
---|
7859 | }
|
---|
7860 |
|
---|
7861 | pToken->Abandon();
|
---|
7862 | pToken.setNull();
|
---|
7863 | if (FAILED(rc))
|
---|
7864 | break;
|
---|
7865 | }
|
---|
7866 | } while(0);
|
---|
7867 |
|
---|
7868 | return rc;
|
---|
7869 | }
|
---|
7870 |
|
---|
7871 | /**
|
---|
7872 | * Performs extra checks if the medium can be closed and returns S_OK in
|
---|
7873 | * this case. Otherwise, returns a respective error message. Called by
|
---|
7874 | * Close() under the medium tree lock and the medium lock.
|
---|
7875 | *
|
---|
7876 | * @note Also reused by Medium::Reset().
|
---|
7877 | *
|
---|
7878 | * @note Caller must hold the media tree write lock!
|
---|
7879 | */
|
---|
7880 | HRESULT Medium::i_canClose()
|
---|
7881 | {
|
---|
7882 | Assert(m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
7883 |
|
---|
7884 | if (i_getChildren().size() != 0)
|
---|
7885 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
7886 | tr("Cannot close medium '%s' because it has %d child media", "", i_getChildren().size()),
|
---|
7887 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
7888 |
|
---|
7889 | return S_OK;
|
---|
7890 | }
|
---|
7891 |
|
---|
7892 | /**
|
---|
7893 | * Unregisters this medium with mVirtualBox. Called by close() under the medium tree lock.
|
---|
7894 | *
|
---|
7895 | * @note Caller must have locked the media tree lock for writing!
|
---|
7896 | */
|
---|
7897 | HRESULT Medium::i_unregisterWithVirtualBox()
|
---|
7898 | {
|
---|
7899 | /* Note that we need to de-associate ourselves from the parent to let
|
---|
7900 | * VirtualBox::i_unregisterMedium() properly save the registry */
|
---|
7901 |
|
---|
7902 | /* we modify m->pParent and access children */
|
---|
7903 | Assert(m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
7904 |
|
---|
7905 | Medium *pParentBackup = m->pParent;
|
---|
7906 | AssertReturn(i_getChildren().size() == 0, E_FAIL);
|
---|
7907 | if (m->pParent)
|
---|
7908 | i_deparent();
|
---|
7909 |
|
---|
7910 | HRESULT rc = m->pVirtualBox->i_unregisterMedium(this);
|
---|
7911 | if (FAILED(rc))
|
---|
7912 | {
|
---|
7913 | if (pParentBackup)
|
---|
7914 | {
|
---|
7915 | // re-associate with the parent as we are still relatives in the registry
|
---|
7916 | i_setParent(pParentBackup);
|
---|
7917 | }
|
---|
7918 | }
|
---|
7919 |
|
---|
7920 | return rc;
|
---|
7921 | }
|
---|
7922 |
|
---|
7923 | /**
|
---|
7924 | * Like SetProperty but do not trigger a settings store. Only for internal use!
|
---|
7925 | */
|
---|
7926 | HRESULT Medium::i_setPropertyDirect(const Utf8Str &aName, const Utf8Str &aValue)
|
---|
7927 | {
|
---|
7928 | AutoCaller autoCaller(this);
|
---|
7929 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
7930 |
|
---|
7931 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
7932 |
|
---|
7933 | switch (m->state)
|
---|
7934 | {
|
---|
7935 | case MediumState_Created:
|
---|
7936 | case MediumState_Inaccessible:
|
---|
7937 | break;
|
---|
7938 | default:
|
---|
7939 | return i_setStateError();
|
---|
7940 | }
|
---|
7941 |
|
---|
7942 | m->mapProperties[aName] = aValue;
|
---|
7943 |
|
---|
7944 | return S_OK;
|
---|
7945 | }
|
---|
7946 |
|
---|
7947 | /**
|
---|
7948 | * Sets the extended error info according to the current media state.
|
---|
7949 | *
|
---|
7950 | * @note Must be called from under this object's write or read lock.
|
---|
7951 | */
|
---|
7952 | HRESULT Medium::i_setStateError()
|
---|
7953 | {
|
---|
7954 | HRESULT rc = E_FAIL;
|
---|
7955 |
|
---|
7956 | switch (m->state)
|
---|
7957 | {
|
---|
7958 | case MediumState_NotCreated:
|
---|
7959 | {
|
---|
7960 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7961 | tr("Storage for the medium '%s' is not created"),
|
---|
7962 | m->strLocationFull.c_str());
|
---|
7963 | break;
|
---|
7964 | }
|
---|
7965 | case MediumState_Created:
|
---|
7966 | {
|
---|
7967 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7968 | tr("Storage for the medium '%s' is already created"),
|
---|
7969 | m->strLocationFull.c_str());
|
---|
7970 | break;
|
---|
7971 | }
|
---|
7972 | case MediumState_LockedRead:
|
---|
7973 | {
|
---|
7974 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7975 | tr("Medium '%s' is locked for reading by another task"),
|
---|
7976 | m->strLocationFull.c_str());
|
---|
7977 | break;
|
---|
7978 | }
|
---|
7979 | case MediumState_LockedWrite:
|
---|
7980 | {
|
---|
7981 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7982 | tr("Medium '%s' is locked for writing by another task"),
|
---|
7983 | m->strLocationFull.c_str());
|
---|
7984 | break;
|
---|
7985 | }
|
---|
7986 | case MediumState_Inaccessible:
|
---|
7987 | {
|
---|
7988 | /* be in sync with Console::powerUpThread() */
|
---|
7989 | if (!m->strLastAccessError.isEmpty())
|
---|
7990 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7991 | tr("Medium '%s' is not accessible. %s"),
|
---|
7992 | m->strLocationFull.c_str(), m->strLastAccessError.c_str());
|
---|
7993 | else
|
---|
7994 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
7995 | tr("Medium '%s' is not accessible"),
|
---|
7996 | m->strLocationFull.c_str());
|
---|
7997 | break;
|
---|
7998 | }
|
---|
7999 | case MediumState_Creating:
|
---|
8000 | {
|
---|
8001 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8002 | tr("Storage for the medium '%s' is being created"),
|
---|
8003 | m->strLocationFull.c_str());
|
---|
8004 | break;
|
---|
8005 | }
|
---|
8006 | case MediumState_Deleting:
|
---|
8007 | {
|
---|
8008 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8009 | tr("Storage for the medium '%s' is being deleted"),
|
---|
8010 | m->strLocationFull.c_str());
|
---|
8011 | break;
|
---|
8012 | }
|
---|
8013 | default:
|
---|
8014 | {
|
---|
8015 | AssertFailed();
|
---|
8016 | break;
|
---|
8017 | }
|
---|
8018 | }
|
---|
8019 |
|
---|
8020 | return rc;
|
---|
8021 | }
|
---|
8022 |
|
---|
8023 | /**
|
---|
8024 | * Sets the value of m->strLocationFull. The given location must be a fully
|
---|
8025 | * qualified path; relative paths are not supported here.
|
---|
8026 | *
|
---|
8027 | * As a special exception, if the specified location is a file path that ends with '/'
|
---|
8028 | * then the file name part will be generated by this method automatically in the format
|
---|
8029 | * '{\<uuid\>}.\<ext\>' where \<uuid\> is a fresh UUID that this method will generate
|
---|
8030 | * and assign to this medium, and \<ext\> is the default extension for this
|
---|
8031 | * medium's storage format. Note that this procedure requires the media state to
|
---|
8032 | * be NotCreated and will return a failure otherwise.
|
---|
8033 | *
|
---|
8034 | * @param aLocation Location of the storage unit. If the location is a FS-path,
|
---|
8035 | * then it can be relative to the VirtualBox home directory.
|
---|
8036 | * @param aFormat Optional fallback format if it is an import and the format
|
---|
8037 | * cannot be determined.
|
---|
8038 | *
|
---|
8039 | * @note Must be called from under this object's write lock.
|
---|
8040 | */
|
---|
8041 | HRESULT Medium::i_setLocation(const Utf8Str &aLocation,
|
---|
8042 | const Utf8Str &aFormat /* = Utf8Str::Empty */)
|
---|
8043 | {
|
---|
8044 | AssertReturn(!aLocation.isEmpty(), E_FAIL);
|
---|
8045 |
|
---|
8046 | AutoCaller autoCaller(this);
|
---|
8047 | AssertComRCReturnRC(autoCaller.rc());
|
---|
8048 |
|
---|
8049 | /* formatObj may be null only when initializing from an existing path and
|
---|
8050 | * no format is known yet */
|
---|
8051 | AssertReturn( (!m->strFormat.isEmpty() && !m->formatObj.isNull())
|
---|
8052 | || ( getObjectState().getState() == ObjectState::InInit
|
---|
8053 | && m->state != MediumState_NotCreated
|
---|
8054 | && m->id.isZero()
|
---|
8055 | && m->strFormat.isEmpty()
|
---|
8056 | && m->formatObj.isNull()),
|
---|
8057 | E_FAIL);
|
---|
8058 |
|
---|
8059 | /* are we dealing with a new medium constructed using the existing
|
---|
8060 | * location? */
|
---|
8061 | bool isImport = m->strFormat.isEmpty();
|
---|
8062 |
|
---|
8063 | if ( isImport
|
---|
8064 | || ( (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
8065 | && !m->hostDrive))
|
---|
8066 | {
|
---|
8067 | Guid id;
|
---|
8068 |
|
---|
8069 | Utf8Str locationFull(aLocation);
|
---|
8070 |
|
---|
8071 | if (m->state == MediumState_NotCreated)
|
---|
8072 | {
|
---|
8073 | /* must be a file (formatObj must be already known) */
|
---|
8074 | Assert(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File);
|
---|
8075 |
|
---|
8076 | if (RTPathFilename(aLocation.c_str()) == NULL)
|
---|
8077 | {
|
---|
8078 | /* no file name is given (either an empty string or ends with a
|
---|
8079 | * slash), generate a new UUID + file name if the state allows
|
---|
8080 | * this */
|
---|
8081 |
|
---|
8082 | ComAssertMsgRet(!m->formatObj->i_getFileExtensions().empty(),
|
---|
8083 | (tr("Must be at least one extension if it is MediumFormatCapabilities_File\n")),
|
---|
8084 | E_FAIL);
|
---|
8085 |
|
---|
8086 | Utf8Str strExt = m->formatObj->i_getFileExtensions().front();
|
---|
8087 | ComAssertMsgRet(!strExt.isEmpty(),
|
---|
8088 | (tr("Default extension must not be empty\n")),
|
---|
8089 | E_FAIL);
|
---|
8090 |
|
---|
8091 | id.create();
|
---|
8092 |
|
---|
8093 | locationFull = Utf8StrFmt("%s{%RTuuid}.%s",
|
---|
8094 | aLocation.c_str(), id.raw(), strExt.c_str());
|
---|
8095 | }
|
---|
8096 | }
|
---|
8097 |
|
---|
8098 | // we must always have full paths now (if it refers to a file)
|
---|
8099 | if ( ( m->formatObj.isNull()
|
---|
8100 | || m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
8101 | && !RTPathStartsWithRoot(locationFull.c_str()))
|
---|
8102 | return setError(VBOX_E_FILE_ERROR,
|
---|
8103 | tr("The given path '%s' is not fully qualified"),
|
---|
8104 | locationFull.c_str());
|
---|
8105 |
|
---|
8106 | /* detect the backend from the storage unit if importing */
|
---|
8107 | if (isImport)
|
---|
8108 | {
|
---|
8109 | VDTYPE const enmDesiredType = i_convertDeviceType();
|
---|
8110 | VDTYPE enmType = VDTYPE_INVALID;
|
---|
8111 | char *backendName = NULL;
|
---|
8112 |
|
---|
8113 | /* is it a file? */
|
---|
8114 | RTFILE hFile;
|
---|
8115 | int vrc = RTFileOpen(&hFile, locationFull.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
8116 | if (RT_SUCCESS(vrc))
|
---|
8117 | {
|
---|
8118 | RTFileClose(hFile);
|
---|
8119 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
8120 | locationFull.c_str(), enmDesiredType, &backendName, &enmType);
|
---|
8121 | }
|
---|
8122 | else if ( vrc != VERR_FILE_NOT_FOUND
|
---|
8123 | && vrc != VERR_PATH_NOT_FOUND
|
---|
8124 | && vrc != VERR_ACCESS_DENIED
|
---|
8125 | && locationFull != aLocation)
|
---|
8126 | {
|
---|
8127 | /* assume it's not a file, restore the original location */
|
---|
8128 | locationFull = aLocation;
|
---|
8129 | vrc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
|
---|
8130 | locationFull.c_str(), enmDesiredType, &backendName, &enmType);
|
---|
8131 | }
|
---|
8132 |
|
---|
8133 | if (RT_FAILURE(vrc))
|
---|
8134 | {
|
---|
8135 | if (vrc == VERR_ACCESS_DENIED)
|
---|
8136 | return setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8137 | tr("Permission problem accessing the file for the medium '%s' (%Rrc)"),
|
---|
8138 | locationFull.c_str(), vrc);
|
---|
8139 | if (vrc == VERR_FILE_NOT_FOUND || vrc == VERR_PATH_NOT_FOUND)
|
---|
8140 | return setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8141 | tr("Could not find file for the medium '%s' (%Rrc)"),
|
---|
8142 | locationFull.c_str(), vrc);
|
---|
8143 | if (aFormat.isEmpty())
|
---|
8144 | return setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
|
---|
8145 | tr("Could not get the storage format of the medium '%s' (%Rrc)"),
|
---|
8146 | locationFull.c_str(), vrc);
|
---|
8147 | HRESULT rc = i_setFormat(aFormat);
|
---|
8148 | /* setFormat() must not fail since we've just used the backend so
|
---|
8149 | * the format object must be there */
|
---|
8150 | AssertComRCReturnRC(rc);
|
---|
8151 | }
|
---|
8152 | else if ( enmType == VDTYPE_INVALID
|
---|
8153 | || m->devType != i_convertToDeviceType(enmType))
|
---|
8154 | {
|
---|
8155 | /*
|
---|
8156 | * The user tried to use a image as a device which is not supported
|
---|
8157 | * by the backend.
|
---|
8158 | */
|
---|
8159 | RTStrFree(backendName);
|
---|
8160 | return setError(E_FAIL,
|
---|
8161 | tr("The medium '%s' can't be used as the requested device type (%s, detected %s)"),
|
---|
8162 | locationFull.c_str(), getDeviceTypeName(m->devType), getVDTypeName(enmType));
|
---|
8163 | }
|
---|
8164 | else
|
---|
8165 | {
|
---|
8166 | ComAssertRet(backendName != NULL && *backendName != '\0', E_FAIL);
|
---|
8167 |
|
---|
8168 | HRESULT rc = i_setFormat(backendName);
|
---|
8169 | RTStrFree(backendName);
|
---|
8170 |
|
---|
8171 | /* setFormat() must not fail since we've just used the backend so
|
---|
8172 | * the format object must be there */
|
---|
8173 | AssertComRCReturnRC(rc);
|
---|
8174 | }
|
---|
8175 | }
|
---|
8176 |
|
---|
8177 | m->strLocationFull = locationFull;
|
---|
8178 |
|
---|
8179 | /* is it still a file? */
|
---|
8180 | if ( (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
8181 | && (m->state == MediumState_NotCreated)
|
---|
8182 | )
|
---|
8183 | /* assign a new UUID (this UUID will be used when calling
|
---|
8184 | * VDCreateBase/VDCreateDiff as a wanted UUID). Note that we
|
---|
8185 | * also do that if we didn't generate it to make sure it is
|
---|
8186 | * either generated by us or reset to null */
|
---|
8187 | unconst(m->id) = id;
|
---|
8188 | }
|
---|
8189 | else
|
---|
8190 | m->strLocationFull = aLocation;
|
---|
8191 |
|
---|
8192 | return S_OK;
|
---|
8193 | }
|
---|
8194 |
|
---|
8195 | /**
|
---|
8196 | * Checks that the format ID is valid and sets it on success.
|
---|
8197 | *
|
---|
8198 | * Note that this method will caller-reference the format object on success!
|
---|
8199 | * This reference must be released somewhere to let the MediumFormat object be
|
---|
8200 | * uninitialized.
|
---|
8201 | *
|
---|
8202 | * @note Must be called from under this object's write lock.
|
---|
8203 | */
|
---|
8204 | HRESULT Medium::i_setFormat(const Utf8Str &aFormat)
|
---|
8205 | {
|
---|
8206 | /* get the format object first */
|
---|
8207 | {
|
---|
8208 | SystemProperties *pSysProps = m->pVirtualBox->i_getSystemProperties();
|
---|
8209 | AutoReadLock propsLock(pSysProps COMMA_LOCKVAL_SRC_POS);
|
---|
8210 |
|
---|
8211 | unconst(m->formatObj) = pSysProps->i_mediumFormat(aFormat);
|
---|
8212 | if (m->formatObj.isNull())
|
---|
8213 | return setError(E_INVALIDARG,
|
---|
8214 | tr("Invalid medium storage format '%s'"),
|
---|
8215 | aFormat.c_str());
|
---|
8216 |
|
---|
8217 | /* get properties (preinsert them as keys in the map). Note that the
|
---|
8218 | * map doesn't grow over the object life time since the set of
|
---|
8219 | * properties is meant to be constant. */
|
---|
8220 |
|
---|
8221 | Assert(m->mapProperties.empty());
|
---|
8222 |
|
---|
8223 | for (MediumFormat::PropertyArray::const_iterator it = m->formatObj->i_getProperties().begin();
|
---|
8224 | it != m->formatObj->i_getProperties().end();
|
---|
8225 | ++it)
|
---|
8226 | {
|
---|
8227 | m->mapProperties.insert(std::make_pair(it->strName, Utf8Str::Empty));
|
---|
8228 | }
|
---|
8229 | }
|
---|
8230 |
|
---|
8231 | unconst(m->strFormat) = aFormat;
|
---|
8232 |
|
---|
8233 | return S_OK;
|
---|
8234 | }
|
---|
8235 |
|
---|
8236 | /**
|
---|
8237 | * Converts the Medium device type to the VD type.
|
---|
8238 | */
|
---|
8239 | VDTYPE Medium::i_convertDeviceType()
|
---|
8240 | {
|
---|
8241 | VDTYPE enmType;
|
---|
8242 |
|
---|
8243 | switch (m->devType)
|
---|
8244 | {
|
---|
8245 | case DeviceType_HardDisk:
|
---|
8246 | enmType = VDTYPE_HDD;
|
---|
8247 | break;
|
---|
8248 | case DeviceType_DVD:
|
---|
8249 | enmType = VDTYPE_OPTICAL_DISC;
|
---|
8250 | break;
|
---|
8251 | case DeviceType_Floppy:
|
---|
8252 | enmType = VDTYPE_FLOPPY;
|
---|
8253 | break;
|
---|
8254 | default:
|
---|
8255 | ComAssertFailedRet(VDTYPE_INVALID);
|
---|
8256 | }
|
---|
8257 |
|
---|
8258 | return enmType;
|
---|
8259 | }
|
---|
8260 |
|
---|
8261 | /**
|
---|
8262 | * Converts from the VD type to the medium type.
|
---|
8263 | */
|
---|
8264 | DeviceType_T Medium::i_convertToDeviceType(VDTYPE enmType)
|
---|
8265 | {
|
---|
8266 | DeviceType_T devType;
|
---|
8267 |
|
---|
8268 | switch (enmType)
|
---|
8269 | {
|
---|
8270 | case VDTYPE_HDD:
|
---|
8271 | devType = DeviceType_HardDisk;
|
---|
8272 | break;
|
---|
8273 | case VDTYPE_OPTICAL_DISC:
|
---|
8274 | devType = DeviceType_DVD;
|
---|
8275 | break;
|
---|
8276 | case VDTYPE_FLOPPY:
|
---|
8277 | devType = DeviceType_Floppy;
|
---|
8278 | break;
|
---|
8279 | default:
|
---|
8280 | ComAssertFailedRet(DeviceType_Null);
|
---|
8281 | }
|
---|
8282 |
|
---|
8283 | return devType;
|
---|
8284 | }
|
---|
8285 |
|
---|
8286 | /**
|
---|
8287 | * Internal method which checks whether a property name is for a filter plugin.
|
---|
8288 | */
|
---|
8289 | bool Medium::i_isPropertyForFilter(const com::Utf8Str &aName)
|
---|
8290 | {
|
---|
8291 | /* If the name contains "/" use the part before as a filter name and lookup the filter. */
|
---|
8292 | size_t offSlash;
|
---|
8293 | if ((offSlash = aName.find("/", 0)) != aName.npos)
|
---|
8294 | {
|
---|
8295 | com::Utf8Str strFilter;
|
---|
8296 | com::Utf8Str strKey;
|
---|
8297 |
|
---|
8298 | HRESULT rc = strFilter.assignEx(aName, 0, offSlash);
|
---|
8299 | if (FAILED(rc))
|
---|
8300 | return false;
|
---|
8301 |
|
---|
8302 | rc = strKey.assignEx(aName, offSlash + 1, aName.length() - offSlash - 1); /* Skip slash */
|
---|
8303 | if (FAILED(rc))
|
---|
8304 | return false;
|
---|
8305 |
|
---|
8306 | VDFILTERINFO FilterInfo;
|
---|
8307 | int vrc = VDFilterInfoOne(strFilter.c_str(), &FilterInfo);
|
---|
8308 | if (RT_SUCCESS(vrc))
|
---|
8309 | {
|
---|
8310 | /* Check that the property exists. */
|
---|
8311 | PCVDCONFIGINFO paConfig = FilterInfo.paConfigInfo;
|
---|
8312 | while (paConfig->pszKey)
|
---|
8313 | {
|
---|
8314 | if (strKey.equals(paConfig->pszKey))
|
---|
8315 | return true;
|
---|
8316 | paConfig++;
|
---|
8317 | }
|
---|
8318 | }
|
---|
8319 | }
|
---|
8320 |
|
---|
8321 | return false;
|
---|
8322 | }
|
---|
8323 |
|
---|
8324 | /**
|
---|
8325 | * Returns the last error message collected by the i_vdErrorCall callback and
|
---|
8326 | * resets it.
|
---|
8327 | *
|
---|
8328 | * The error message is returned prepended with a dot and a space, like this:
|
---|
8329 | * <code>
|
---|
8330 | * ". <error_text> (%Rrc)"
|
---|
8331 | * </code>
|
---|
8332 | * to make it easily appendable to a more general error message. The @c %Rrc
|
---|
8333 | * format string is given @a aVRC as an argument.
|
---|
8334 | *
|
---|
8335 | * If there is no last error message collected by i_vdErrorCall or if it is a
|
---|
8336 | * null or empty string, then this function returns the following text:
|
---|
8337 | * <code>
|
---|
8338 | * " (%Rrc)"
|
---|
8339 | * </code>
|
---|
8340 | *
|
---|
8341 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
8342 | * the callback isn't called by more than one thread at a time.
|
---|
8343 | *
|
---|
8344 | * @param aVRC VBox error code to use when no error message is provided.
|
---|
8345 | */
|
---|
8346 | Utf8Str Medium::i_vdError(int aVRC)
|
---|
8347 | {
|
---|
8348 | Utf8Str error;
|
---|
8349 |
|
---|
8350 | if (m->vdError.isEmpty())
|
---|
8351 | error = Utf8StrFmt(" (%Rrc)", aVRC);
|
---|
8352 | else
|
---|
8353 | error = Utf8StrFmt(".\n%s", m->vdError.c_str());
|
---|
8354 |
|
---|
8355 | m->vdError.setNull();
|
---|
8356 |
|
---|
8357 | return error;
|
---|
8358 | }
|
---|
8359 |
|
---|
8360 | /**
|
---|
8361 | * Error message callback.
|
---|
8362 | *
|
---|
8363 | * Puts the reported error message to the m->vdError field.
|
---|
8364 | *
|
---|
8365 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
8366 | * the callback isn't called by more than one thread at a time.
|
---|
8367 | *
|
---|
8368 | * @param pvUser The opaque data passed on container creation.
|
---|
8369 | * @param rc The VBox error code.
|
---|
8370 | * @param SRC_POS Use RT_SRC_POS.
|
---|
8371 | * @param pszFormat Error message format string.
|
---|
8372 | * @param va Error message arguments.
|
---|
8373 | */
|
---|
8374 | /*static*/
|
---|
8375 | DECLCALLBACK(void) Medium::i_vdErrorCall(void *pvUser, int rc, RT_SRC_POS_DECL,
|
---|
8376 | const char *pszFormat, va_list va)
|
---|
8377 | {
|
---|
8378 | NOREF(pszFile); NOREF(iLine); NOREF(pszFunction); /* RT_SRC_POS_DECL */
|
---|
8379 |
|
---|
8380 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
8381 | AssertReturnVoid(that != NULL);
|
---|
8382 |
|
---|
8383 | if (that->m->vdError.isEmpty())
|
---|
8384 | that->m->vdError =
|
---|
8385 | Utf8StrFmt("%s (%Rrc)", Utf8Str(pszFormat, va).c_str(), rc);
|
---|
8386 | else
|
---|
8387 | that->m->vdError =
|
---|
8388 | Utf8StrFmt("%s.\n%s (%Rrc)", that->m->vdError.c_str(),
|
---|
8389 | Utf8Str(pszFormat, va).c_str(), rc);
|
---|
8390 | }
|
---|
8391 |
|
---|
8392 | /* static */
|
---|
8393 | DECLCALLBACK(bool) Medium::i_vdConfigAreKeysValid(void *pvUser,
|
---|
8394 | const char * /* pszzValid */)
|
---|
8395 | {
|
---|
8396 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
8397 | AssertReturn(that != NULL, false);
|
---|
8398 |
|
---|
8399 | /* we always return true since the only keys we have are those found in
|
---|
8400 | * VDBACKENDINFO */
|
---|
8401 | return true;
|
---|
8402 | }
|
---|
8403 |
|
---|
8404 | /* static */
|
---|
8405 | DECLCALLBACK(int) Medium::i_vdConfigQuerySize(void *pvUser,
|
---|
8406 | const char *pszName,
|
---|
8407 | size_t *pcbValue)
|
---|
8408 | {
|
---|
8409 | AssertPtrReturn(pcbValue, VERR_INVALID_POINTER);
|
---|
8410 |
|
---|
8411 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
8412 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
8413 |
|
---|
8414 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
8415 | if (it == that->m->mapProperties.end())
|
---|
8416 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
8417 |
|
---|
8418 | /* we interpret null values as "no value" in Medium */
|
---|
8419 | if (it->second.isEmpty())
|
---|
8420 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
8421 |
|
---|
8422 | *pcbValue = it->second.length() + 1 /* include terminator */;
|
---|
8423 |
|
---|
8424 | return VINF_SUCCESS;
|
---|
8425 | }
|
---|
8426 |
|
---|
8427 | /* static */
|
---|
8428 | DECLCALLBACK(int) Medium::i_vdConfigQuery(void *pvUser,
|
---|
8429 | const char *pszName,
|
---|
8430 | char *pszValue,
|
---|
8431 | size_t cchValue)
|
---|
8432 | {
|
---|
8433 | AssertPtrReturn(pszValue, VERR_INVALID_POINTER);
|
---|
8434 |
|
---|
8435 | Medium *that = static_cast<Medium*>(pvUser);
|
---|
8436 | AssertReturn(that != NULL, VERR_GENERAL_FAILURE);
|
---|
8437 |
|
---|
8438 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(Utf8Str(pszName));
|
---|
8439 | if (it == that->m->mapProperties.end())
|
---|
8440 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
8441 |
|
---|
8442 | /* we interpret null values as "no value" in Medium */
|
---|
8443 | if (it->second.isEmpty())
|
---|
8444 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
8445 |
|
---|
8446 | const Utf8Str &value = it->second;
|
---|
8447 | if (value.length() >= cchValue)
|
---|
8448 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
8449 |
|
---|
8450 | memcpy(pszValue, value.c_str(), value.length() + 1);
|
---|
8451 |
|
---|
8452 | return VINF_SUCCESS;
|
---|
8453 | }
|
---|
8454 |
|
---|
8455 | DECLCALLBACK(bool) Medium::i_vdCryptoConfigAreKeysValid(void *pvUser, const char *pszzValid)
|
---|
8456 | {
|
---|
8457 | /* Just return always true here. */
|
---|
8458 | NOREF(pvUser);
|
---|
8459 | NOREF(pszzValid);
|
---|
8460 | return true;
|
---|
8461 | }
|
---|
8462 |
|
---|
8463 | DECLCALLBACK(int) Medium::i_vdCryptoConfigQuerySize(void *pvUser, const char *pszName, size_t *pcbValue)
|
---|
8464 | {
|
---|
8465 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8466 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8467 | AssertPtrReturn(pcbValue, VERR_INVALID_POINTER);
|
---|
8468 |
|
---|
8469 | size_t cbValue = 0;
|
---|
8470 | if (!strcmp(pszName, "Algorithm"))
|
---|
8471 | cbValue = strlen(pSettings->pszCipher) + 1;
|
---|
8472 | else if (!strcmp(pszName, "KeyId"))
|
---|
8473 | cbValue = sizeof("irrelevant");
|
---|
8474 | else if (!strcmp(pszName, "KeyStore"))
|
---|
8475 | {
|
---|
8476 | if (!pSettings->pszKeyStoreLoad)
|
---|
8477 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
8478 | cbValue = strlen(pSettings->pszKeyStoreLoad) + 1;
|
---|
8479 | }
|
---|
8480 | else if (!strcmp(pszName, "CreateKeyStore"))
|
---|
8481 | cbValue = 2; /* Single digit + terminator. */
|
---|
8482 | else
|
---|
8483 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
8484 |
|
---|
8485 | *pcbValue = cbValue + 1 /* include terminator */;
|
---|
8486 |
|
---|
8487 | return VINF_SUCCESS;
|
---|
8488 | }
|
---|
8489 |
|
---|
8490 | DECLCALLBACK(int) Medium::i_vdCryptoConfigQuery(void *pvUser, const char *pszName,
|
---|
8491 | char *pszValue, size_t cchValue)
|
---|
8492 | {
|
---|
8493 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8494 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8495 | AssertPtrReturn(pszValue, VERR_INVALID_POINTER);
|
---|
8496 |
|
---|
8497 | const char *psz = NULL;
|
---|
8498 | if (!strcmp(pszName, "Algorithm"))
|
---|
8499 | psz = pSettings->pszCipher;
|
---|
8500 | else if (!strcmp(pszName, "KeyId"))
|
---|
8501 | psz = "irrelevant";
|
---|
8502 | else if (!strcmp(pszName, "KeyStore"))
|
---|
8503 | psz = pSettings->pszKeyStoreLoad;
|
---|
8504 | else if (!strcmp(pszName, "CreateKeyStore"))
|
---|
8505 | {
|
---|
8506 | if (pSettings->fCreateKeyStore)
|
---|
8507 | psz = "1";
|
---|
8508 | else
|
---|
8509 | psz = "0";
|
---|
8510 | }
|
---|
8511 | else
|
---|
8512 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
8513 |
|
---|
8514 | size_t cch = strlen(psz);
|
---|
8515 | if (cch >= cchValue)
|
---|
8516 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
8517 |
|
---|
8518 | memcpy(pszValue, psz, cch + 1);
|
---|
8519 | return VINF_SUCCESS;
|
---|
8520 | }
|
---|
8521 |
|
---|
8522 | DECLCALLBACK(int) Medium::i_vdConfigUpdate(void *pvUser,
|
---|
8523 | bool fCreate,
|
---|
8524 | const char *pszName,
|
---|
8525 | const char *pszValue)
|
---|
8526 | {
|
---|
8527 | Medium *that = (Medium *)pvUser;
|
---|
8528 |
|
---|
8529 | // Detect if this runs inside i_queryInfo() on the current thread.
|
---|
8530 | // Skip if not. Check does not need synchronization.
|
---|
8531 | if (!that->m || !that->m->queryInfoRunning || !that->m->queryInfoSem.isWriteLockOnCurrentThread())
|
---|
8532 | return VINF_SUCCESS;
|
---|
8533 |
|
---|
8534 | // It's guaranteed that this code is executing inside Medium::i_queryInfo,
|
---|
8535 | // can assume it took care of synchronization.
|
---|
8536 | int rv = VINF_SUCCESS;
|
---|
8537 | Utf8Str strName(pszName);
|
---|
8538 | settings::StringsMap::const_iterator it = that->m->mapProperties.find(strName);
|
---|
8539 | if (it == that->m->mapProperties.end() && !fCreate)
|
---|
8540 | rv = VERR_CFGM_VALUE_NOT_FOUND;
|
---|
8541 | else
|
---|
8542 | that->m->mapProperties[strName] = Utf8Str(pszValue);
|
---|
8543 | return rv;
|
---|
8544 | }
|
---|
8545 |
|
---|
8546 | DECLCALLBACK(int) Medium::i_vdCryptoKeyRetain(void *pvUser, const char *pszId,
|
---|
8547 | const uint8_t **ppbKey, size_t *pcbKey)
|
---|
8548 | {
|
---|
8549 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8550 | NOREF(pszId);
|
---|
8551 | NOREF(ppbKey);
|
---|
8552 | NOREF(pcbKey);
|
---|
8553 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8554 | AssertMsgFailedReturn(("This method should not be called here!\n"), VERR_INVALID_STATE);
|
---|
8555 | }
|
---|
8556 |
|
---|
8557 | DECLCALLBACK(int) Medium::i_vdCryptoKeyRelease(void *pvUser, const char *pszId)
|
---|
8558 | {
|
---|
8559 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8560 | NOREF(pszId);
|
---|
8561 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8562 | AssertMsgFailedReturn(("This method should not be called here!\n"), VERR_INVALID_STATE);
|
---|
8563 | }
|
---|
8564 |
|
---|
8565 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStorePasswordRetain(void *pvUser, const char *pszId, const char **ppszPassword)
|
---|
8566 | {
|
---|
8567 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8568 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8569 |
|
---|
8570 | NOREF(pszId);
|
---|
8571 | *ppszPassword = pSettings->pszPassword;
|
---|
8572 | return VINF_SUCCESS;
|
---|
8573 | }
|
---|
8574 |
|
---|
8575 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStorePasswordRelease(void *pvUser, const char *pszId)
|
---|
8576 | {
|
---|
8577 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8578 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8579 | NOREF(pszId);
|
---|
8580 | return VINF_SUCCESS;
|
---|
8581 | }
|
---|
8582 |
|
---|
8583 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStoreSave(void *pvUser, const void *pvKeyStore, size_t cbKeyStore)
|
---|
8584 | {
|
---|
8585 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8586 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8587 |
|
---|
8588 | pSettings->pszKeyStore = (char *)RTMemAllocZ(cbKeyStore);
|
---|
8589 | if (!pSettings->pszKeyStore)
|
---|
8590 | return VERR_NO_MEMORY;
|
---|
8591 |
|
---|
8592 | memcpy(pSettings->pszKeyStore, pvKeyStore, cbKeyStore);
|
---|
8593 | return VINF_SUCCESS;
|
---|
8594 | }
|
---|
8595 |
|
---|
8596 | DECLCALLBACK(int) Medium::i_vdCryptoKeyStoreReturnParameters(void *pvUser, const char *pszCipher,
|
---|
8597 | const uint8_t *pbDek, size_t cbDek)
|
---|
8598 | {
|
---|
8599 | MediumCryptoFilterSettings *pSettings = (MediumCryptoFilterSettings *)pvUser;
|
---|
8600 | AssertPtrReturn(pSettings, VERR_GENERAL_FAILURE);
|
---|
8601 |
|
---|
8602 | pSettings->pszCipherReturned = RTStrDup(pszCipher);
|
---|
8603 | pSettings->pbDek = pbDek;
|
---|
8604 | pSettings->cbDek = cbDek;
|
---|
8605 |
|
---|
8606 | return pSettings->pszCipherReturned ? VINF_SUCCESS : VERR_NO_MEMORY;
|
---|
8607 | }
|
---|
8608 |
|
---|
8609 | /**
|
---|
8610 | * Creates a VDISK instance for this medium.
|
---|
8611 | *
|
---|
8612 | * @note Caller should not hold any medium related locks as this method will
|
---|
8613 | * acquire the medium lock for writing and others (VirtualBox).
|
---|
8614 | *
|
---|
8615 | * @returns COM status code.
|
---|
8616 | * @param fWritable Whether to return a writable VDISK instance
|
---|
8617 | * (true) or a read-only one (false).
|
---|
8618 | * @param pKeyStore The key store.
|
---|
8619 | * @param ppHdd Where to return the pointer to the VDISK on
|
---|
8620 | * success.
|
---|
8621 | * @param pMediumLockList The lock list to populate and lock. Caller
|
---|
8622 | * is responsible for calling the destructor or
|
---|
8623 | * MediumLockList::Clear() after destroying
|
---|
8624 | * @a *ppHdd
|
---|
8625 | * @param pCryptoSettings The crypto settings to use for setting up
|
---|
8626 | * decryption/encryption of the VDISK. This object
|
---|
8627 | * must be alive until the VDISK is destroyed!
|
---|
8628 | */
|
---|
8629 | HRESULT Medium::i_openForIO(bool fWritable, SecretKeyStore *pKeyStore, PVDISK *ppHdd, MediumLockList *pMediumLockList,
|
---|
8630 | MediumCryptoFilterSettings *pCryptoSettings)
|
---|
8631 | {
|
---|
8632 | /*
|
---|
8633 | * Create the media lock list and lock the media.
|
---|
8634 | */
|
---|
8635 | HRESULT hrc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
8636 | fWritable ? this : NULL /* pToLockWrite */,
|
---|
8637 | false /* fMediumLockWriteAll */,
|
---|
8638 | NULL,
|
---|
8639 | *pMediumLockList);
|
---|
8640 | if (SUCCEEDED(hrc))
|
---|
8641 | hrc = pMediumLockList->Lock();
|
---|
8642 | if (FAILED(hrc))
|
---|
8643 | return hrc;
|
---|
8644 |
|
---|
8645 | /*
|
---|
8646 | * Get the base medium before write locking this medium.
|
---|
8647 | */
|
---|
8648 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
8649 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8650 |
|
---|
8651 | /*
|
---|
8652 | * Create the VDISK instance.
|
---|
8653 | */
|
---|
8654 | PVDISK pHdd;
|
---|
8655 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pHdd);
|
---|
8656 | AssertRCReturn(vrc, E_FAIL);
|
---|
8657 |
|
---|
8658 | /*
|
---|
8659 | * Goto avoidance using try/catch/throw(HRESULT).
|
---|
8660 | */
|
---|
8661 | try
|
---|
8662 | {
|
---|
8663 | settings::StringsMap::iterator itKeyStore = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
8664 | if (itKeyStore != pBase->m->mapProperties.end())
|
---|
8665 | {
|
---|
8666 | #ifdef VBOX_WITH_EXTPACK
|
---|
8667 | settings::StringsMap::iterator itKeyId = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
8668 |
|
---|
8669 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
8670 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
8671 | {
|
---|
8672 | /* Load the plugin */
|
---|
8673 | Utf8Str strPlugin;
|
---|
8674 | hrc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
8675 | if (SUCCEEDED(hrc))
|
---|
8676 | {
|
---|
8677 | vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
8678 | if (RT_FAILURE(vrc))
|
---|
8679 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
8680 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
8681 | i_vdError(vrc).c_str());
|
---|
8682 | }
|
---|
8683 | else
|
---|
8684 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
8685 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
8686 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
8687 | }
|
---|
8688 | else
|
---|
8689 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
8690 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
8691 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
8692 |
|
---|
8693 | if (itKeyId == pBase->m->mapProperties.end())
|
---|
8694 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8695 | tr("Image '%s' is configured for encryption but doesn't has a key identifier set"),
|
---|
8696 | pBase->m->strLocationFull.c_str());
|
---|
8697 |
|
---|
8698 | /* Find the proper secret key in the key store. */
|
---|
8699 | if (!pKeyStore)
|
---|
8700 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8701 | tr("Image '%s' is configured for encryption but there is no key store to retrieve the password from"),
|
---|
8702 | pBase->m->strLocationFull.c_str());
|
---|
8703 |
|
---|
8704 | SecretKey *pKey = NULL;
|
---|
8705 | vrc = pKeyStore->retainSecretKey(itKeyId->second, &pKey);
|
---|
8706 | if (RT_FAILURE(vrc))
|
---|
8707 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc,
|
---|
8708 | tr("Failed to retrieve the secret key with ID \"%s\" from the store (%Rrc)"),
|
---|
8709 | itKeyId->second.c_str(), vrc);
|
---|
8710 |
|
---|
8711 | i_taskEncryptSettingsSetup(pCryptoSettings, NULL, itKeyStore->second.c_str(), (const char *)pKey->getKeyBuffer(),
|
---|
8712 | false /* fCreateKeyStore */);
|
---|
8713 | vrc = VDFilterAdd(pHdd, "CRYPT", VD_FILTER_FLAGS_DEFAULT, pCryptoSettings->vdFilterIfaces);
|
---|
8714 | pKeyStore->releaseSecretKey(itKeyId->second);
|
---|
8715 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
8716 | throw setErrorBoth(VBOX_E_PASSWORD_INCORRECT, vrc, tr("The password to decrypt the image is incorrect"));
|
---|
8717 | if (RT_FAILURE(vrc))
|
---|
8718 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc, tr("Failed to load the decryption filter: %s"),
|
---|
8719 | i_vdError(vrc).c_str());
|
---|
8720 | #else
|
---|
8721 | RT_NOREF(pKeyStore, pCryptoSettings);
|
---|
8722 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
8723 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
8724 | #endif /* VBOX_WITH_EXTPACK */
|
---|
8725 | }
|
---|
8726 |
|
---|
8727 | /*
|
---|
8728 | * Open all media in the source chain.
|
---|
8729 | */
|
---|
8730 | MediumLockList::Base::const_iterator sourceListBegin = pMediumLockList->GetBegin();
|
---|
8731 | MediumLockList::Base::const_iterator sourceListEnd = pMediumLockList->GetEnd();
|
---|
8732 | MediumLockList::Base::const_iterator mediumListLast = sourceListEnd;
|
---|
8733 | --mediumListLast;
|
---|
8734 |
|
---|
8735 | for (MediumLockList::Base::const_iterator it = sourceListBegin; it != sourceListEnd; ++it)
|
---|
8736 | {
|
---|
8737 | const MediumLock &mediumLock = *it;
|
---|
8738 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
8739 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
8740 |
|
---|
8741 | /* sanity check */
|
---|
8742 | Assert(pMedium->m->state == (fWritable && it == mediumListLast ? MediumState_LockedWrite : MediumState_LockedRead));
|
---|
8743 |
|
---|
8744 | /* Open all media in read-only mode. */
|
---|
8745 | vrc = VDOpen(pHdd,
|
---|
8746 | pMedium->m->strFormat.c_str(),
|
---|
8747 | pMedium->m->strLocationFull.c_str(),
|
---|
8748 | m->uOpenFlagsDef | (fWritable && it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
8749 | pMedium->m->vdImageIfaces);
|
---|
8750 | if (RT_FAILURE(vrc))
|
---|
8751 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8752 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
8753 | pMedium->m->strLocationFull.c_str(),
|
---|
8754 | i_vdError(vrc).c_str());
|
---|
8755 | }
|
---|
8756 |
|
---|
8757 | Assert(m->state == (fWritable ? MediumState_LockedWrite : MediumState_LockedRead));
|
---|
8758 |
|
---|
8759 | /*
|
---|
8760 | * Done!
|
---|
8761 | */
|
---|
8762 | *ppHdd = pHdd;
|
---|
8763 | return S_OK;
|
---|
8764 | }
|
---|
8765 | catch (HRESULT hrc2)
|
---|
8766 | {
|
---|
8767 | hrc = hrc2;
|
---|
8768 | }
|
---|
8769 |
|
---|
8770 | VDDestroy(pHdd);
|
---|
8771 | return hrc;
|
---|
8772 |
|
---|
8773 | }
|
---|
8774 |
|
---|
8775 | /**
|
---|
8776 | * Implementation code for the "create base" task.
|
---|
8777 | *
|
---|
8778 | * This only gets started from Medium::CreateBaseStorage() and always runs
|
---|
8779 | * asynchronously. As a result, we always save the VirtualBox.xml file when
|
---|
8780 | * we're done here.
|
---|
8781 | *
|
---|
8782 | * @param task
|
---|
8783 | * @return
|
---|
8784 | */
|
---|
8785 | HRESULT Medium::i_taskCreateBaseHandler(Medium::CreateBaseTask &task)
|
---|
8786 | {
|
---|
8787 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8788 | * to lock order violations, it probably causes lock order issues related
|
---|
8789 | * to the AutoCaller usage. */
|
---|
8790 | HRESULT rc = S_OK;
|
---|
8791 |
|
---|
8792 | /* these parameters we need after creation */
|
---|
8793 | uint64_t size = 0, logicalSize = 0;
|
---|
8794 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
8795 | bool fGenerateUuid = false;
|
---|
8796 |
|
---|
8797 | try
|
---|
8798 | {
|
---|
8799 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8800 |
|
---|
8801 | /* The object may request a specific UUID (through a special form of
|
---|
8802 | * the moveTo() argument). Otherwise we have to generate it */
|
---|
8803 | Guid id = m->id;
|
---|
8804 |
|
---|
8805 | fGenerateUuid = id.isZero();
|
---|
8806 | if (fGenerateUuid)
|
---|
8807 | {
|
---|
8808 | id.create();
|
---|
8809 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
8810 | unconst(m->id) = id;
|
---|
8811 | }
|
---|
8812 |
|
---|
8813 | Utf8Str format(m->strFormat);
|
---|
8814 | Utf8Str location(m->strLocationFull);
|
---|
8815 | uint64_t capabilities = m->formatObj->i_getCapabilities();
|
---|
8816 | ComAssertThrow(capabilities & ( MediumFormatCapabilities_CreateFixed
|
---|
8817 | | MediumFormatCapabilities_CreateDynamic), E_FAIL);
|
---|
8818 | Assert(m->state == MediumState_Creating);
|
---|
8819 |
|
---|
8820 | PVDISK hdd;
|
---|
8821 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
8822 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
8823 |
|
---|
8824 | /* unlock before the potentially lengthy operation */
|
---|
8825 | thisLock.release();
|
---|
8826 |
|
---|
8827 | try
|
---|
8828 | {
|
---|
8829 | /* ensure the directory exists */
|
---|
8830 | if (capabilities & MediumFormatCapabilities_File)
|
---|
8831 | {
|
---|
8832 | rc = VirtualBox::i_ensureFilePathExists(location, !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
8833 | if (FAILED(rc))
|
---|
8834 | throw rc;
|
---|
8835 | }
|
---|
8836 |
|
---|
8837 | VDGEOMETRY geo = { 0, 0, 0 }; /* auto-detect */
|
---|
8838 |
|
---|
8839 | vrc = VDCreateBase(hdd,
|
---|
8840 | format.c_str(),
|
---|
8841 | location.c_str(),
|
---|
8842 | task.mSize,
|
---|
8843 | task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted),
|
---|
8844 | NULL,
|
---|
8845 | &geo,
|
---|
8846 | &geo,
|
---|
8847 | id.raw(),
|
---|
8848 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
8849 | m->vdImageIfaces,
|
---|
8850 | task.mVDOperationIfaces);
|
---|
8851 | if (RT_FAILURE(vrc))
|
---|
8852 | {
|
---|
8853 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
8854 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8855 | tr("Parameters for creating the medium storage unit '%s' are invalid%s"),
|
---|
8856 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8857 | else
|
---|
8858 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
8859 | tr("Could not create the medium storage unit '%s'%s"),
|
---|
8860 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8861 | }
|
---|
8862 |
|
---|
8863 | if (task.mVariant & MediumVariant_Formatted)
|
---|
8864 | {
|
---|
8865 | RTVFSFILE hVfsFile;
|
---|
8866 | vrc = VDCreateVfsFileFromDisk(hdd, 0 /*fFlags*/, &hVfsFile);
|
---|
8867 | if (RT_FAILURE(vrc))
|
---|
8868 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Opening medium storage unit '%s' failed%s"),
|
---|
8869 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8870 | RTERRINFOSTATIC ErrInfo;
|
---|
8871 | vrc = RTFsFatVolFormat(hVfsFile, 0 /* offVol */, 0 /* cbVol */, RTFSFATVOL_FMT_F_FULL,
|
---|
8872 | 0 /* cbSector */, 0 /* cbSectorPerCluster */, RTFSFATTYPE_INVALID,
|
---|
8873 | 0 /* cHeads */, 0 /* cSectorsPerTrack*/, 0 /* bMedia */,
|
---|
8874 | 0 /* cRootDirEntries */, 0 /* cHiddenSectors */,
|
---|
8875 | RTErrInfoInitStatic(&ErrInfo));
|
---|
8876 | RTVfsFileRelease(hVfsFile);
|
---|
8877 | if (RT_FAILURE(vrc) && RTErrInfoIsSet(&ErrInfo.Core))
|
---|
8878 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Formatting medium storage unit '%s' failed: %s"),
|
---|
8879 | location.c_str(), ErrInfo.Core.pszMsg);
|
---|
8880 | if (RT_FAILURE(vrc))
|
---|
8881 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Formatting medium storage unit '%s' failed%s"),
|
---|
8882 | location.c_str(), i_vdError(vrc).c_str());
|
---|
8883 | }
|
---|
8884 |
|
---|
8885 | size = VDGetFileSize(hdd, 0);
|
---|
8886 | logicalSize = VDGetSize(hdd, 0);
|
---|
8887 | unsigned uImageFlags;
|
---|
8888 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
8889 | if (RT_SUCCESS(vrc))
|
---|
8890 | variant = (MediumVariant_T)uImageFlags;
|
---|
8891 | }
|
---|
8892 | catch (HRESULT aRC) { rc = aRC; }
|
---|
8893 |
|
---|
8894 | VDDestroy(hdd);
|
---|
8895 | }
|
---|
8896 | catch (HRESULT aRC) { rc = aRC; }
|
---|
8897 |
|
---|
8898 | if (SUCCEEDED(rc))
|
---|
8899 | {
|
---|
8900 | /* register with mVirtualBox as the last step and move to
|
---|
8901 | * Created state only on success (leaving an orphan file is
|
---|
8902 | * better than breaking media registry consistency) */
|
---|
8903 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
8904 | ComObjPtr<Medium> pMedium;
|
---|
8905 | rc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
8906 | Assert(pMedium == NULL || this == pMedium);
|
---|
8907 | }
|
---|
8908 |
|
---|
8909 | // re-acquire the lock before changing state
|
---|
8910 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8911 |
|
---|
8912 | if (SUCCEEDED(rc))
|
---|
8913 | {
|
---|
8914 | m->state = MediumState_Created;
|
---|
8915 |
|
---|
8916 | m->size = size;
|
---|
8917 | m->logicalSize = logicalSize;
|
---|
8918 | m->variant = variant;
|
---|
8919 |
|
---|
8920 | thisLock.release();
|
---|
8921 | i_markRegistriesModified();
|
---|
8922 | if (task.isAsync())
|
---|
8923 | {
|
---|
8924 | // in asynchronous mode, save settings now
|
---|
8925 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
8926 | }
|
---|
8927 | }
|
---|
8928 | else
|
---|
8929 | {
|
---|
8930 | /* back to NotCreated on failure */
|
---|
8931 | m->state = MediumState_NotCreated;
|
---|
8932 |
|
---|
8933 | /* reset UUID to prevent it from being reused next time */
|
---|
8934 | if (fGenerateUuid)
|
---|
8935 | unconst(m->id).clear();
|
---|
8936 | }
|
---|
8937 |
|
---|
8938 | if (task.NotifyAboutChanges() && SUCCEEDED(rc))
|
---|
8939 | {
|
---|
8940 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
8941 | m->pVirtualBox->i_onMediumRegistered(m->id, m->devType, TRUE);
|
---|
8942 | }
|
---|
8943 |
|
---|
8944 | return rc;
|
---|
8945 | }
|
---|
8946 |
|
---|
8947 | /**
|
---|
8948 | * Implementation code for the "create diff" task.
|
---|
8949 | *
|
---|
8950 | * This task always gets started from Medium::createDiffStorage() and can run
|
---|
8951 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
8952 | * that function. If we run synchronously, the caller expects the medium
|
---|
8953 | * registry modification to be set before returning; otherwise (in asynchronous
|
---|
8954 | * mode), we save the settings ourselves.
|
---|
8955 | *
|
---|
8956 | * @param task
|
---|
8957 | * @return
|
---|
8958 | */
|
---|
8959 | HRESULT Medium::i_taskCreateDiffHandler(Medium::CreateDiffTask &task)
|
---|
8960 | {
|
---|
8961 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
8962 | * to lock order violations, it probably causes lock order issues related
|
---|
8963 | * to the AutoCaller usage. */
|
---|
8964 | HRESULT rcTmp = S_OK;
|
---|
8965 |
|
---|
8966 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
8967 |
|
---|
8968 | uint64_t size = 0, logicalSize = 0;
|
---|
8969 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
8970 | bool fGenerateUuid = false;
|
---|
8971 |
|
---|
8972 | try
|
---|
8973 | {
|
---|
8974 | if (i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
8975 | {
|
---|
8976 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
8977 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
8978 | tr("Cannot create differencing image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
8979 | m->strLocationFull.c_str());
|
---|
8980 | }
|
---|
8981 |
|
---|
8982 | /* Lock both in {parent,child} order. */
|
---|
8983 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
8984 |
|
---|
8985 | /* The object may request a specific UUID (through a special form of
|
---|
8986 | * the moveTo() argument). Otherwise we have to generate it */
|
---|
8987 | Guid targetId = pTarget->m->id;
|
---|
8988 |
|
---|
8989 | fGenerateUuid = targetId.isZero();
|
---|
8990 | if (fGenerateUuid)
|
---|
8991 | {
|
---|
8992 | targetId.create();
|
---|
8993 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
8994 | unconst(pTarget->m->id) = targetId;
|
---|
8995 | }
|
---|
8996 |
|
---|
8997 | Guid id = m->id;
|
---|
8998 |
|
---|
8999 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
9000 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
9001 | uint64_t capabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
9002 | ComAssertThrow(capabilities & MediumFormatCapabilities_CreateDynamic, E_FAIL);
|
---|
9003 |
|
---|
9004 | Assert(pTarget->m->state == MediumState_Creating);
|
---|
9005 | Assert(m->state == MediumState_LockedRead);
|
---|
9006 |
|
---|
9007 | PVDISK hdd;
|
---|
9008 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9009 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9010 |
|
---|
9011 | /* the two media are now protected by their non-default states;
|
---|
9012 | * unlock the media before the potentially lengthy operation */
|
---|
9013 | mediaLock.release();
|
---|
9014 |
|
---|
9015 | try
|
---|
9016 | {
|
---|
9017 | /* Open all media in the target chain but the last. */
|
---|
9018 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
9019 | task.mpMediumLockList->GetBegin();
|
---|
9020 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
9021 | task.mpMediumLockList->GetEnd();
|
---|
9022 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
9023 | it != targetListEnd;
|
---|
9024 | ++it)
|
---|
9025 | {
|
---|
9026 | const MediumLock &mediumLock = *it;
|
---|
9027 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9028 |
|
---|
9029 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9030 |
|
---|
9031 | /* Skip over the target diff medium */
|
---|
9032 | if (pMedium->m->state == MediumState_Creating)
|
---|
9033 | continue;
|
---|
9034 |
|
---|
9035 | /* sanity check */
|
---|
9036 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
9037 |
|
---|
9038 | /* Open all media in appropriate mode. */
|
---|
9039 | vrc = VDOpen(hdd,
|
---|
9040 | pMedium->m->strFormat.c_str(),
|
---|
9041 | pMedium->m->strLocationFull.c_str(),
|
---|
9042 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
9043 | pMedium->m->vdImageIfaces);
|
---|
9044 | if (RT_FAILURE(vrc))
|
---|
9045 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9046 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9047 | pMedium->m->strLocationFull.c_str(),
|
---|
9048 | i_vdError(vrc).c_str());
|
---|
9049 | }
|
---|
9050 |
|
---|
9051 | /* ensure the target directory exists */
|
---|
9052 | if (capabilities & MediumFormatCapabilities_File)
|
---|
9053 | {
|
---|
9054 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
9055 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
9056 | if (FAILED(rc))
|
---|
9057 | throw rc;
|
---|
9058 | }
|
---|
9059 |
|
---|
9060 | vrc = VDCreateDiff(hdd,
|
---|
9061 | targetFormat.c_str(),
|
---|
9062 | targetLocation.c_str(),
|
---|
9063 | (task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted | MediumVariant_VmdkESX))
|
---|
9064 | | VD_IMAGE_FLAGS_DIFF,
|
---|
9065 | NULL,
|
---|
9066 | targetId.raw(),
|
---|
9067 | id.raw(),
|
---|
9068 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
9069 | pTarget->m->vdImageIfaces,
|
---|
9070 | task.mVDOperationIfaces);
|
---|
9071 | if (RT_FAILURE(vrc))
|
---|
9072 | {
|
---|
9073 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
9074 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9075 | tr("Parameters for creating the differencing medium storage unit '%s' are invalid%s"),
|
---|
9076 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9077 | else
|
---|
9078 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9079 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
9080 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9081 | }
|
---|
9082 |
|
---|
9083 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
9084 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
9085 | unsigned uImageFlags;
|
---|
9086 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
9087 | if (RT_SUCCESS(vrc))
|
---|
9088 | variant = (MediumVariant_T)uImageFlags;
|
---|
9089 | }
|
---|
9090 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9091 |
|
---|
9092 | VDDestroy(hdd);
|
---|
9093 | }
|
---|
9094 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9095 |
|
---|
9096 | MultiResult mrc(rcTmp);
|
---|
9097 |
|
---|
9098 | if (SUCCEEDED(mrc))
|
---|
9099 | {
|
---|
9100 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
9101 |
|
---|
9102 | Assert(pTarget->m->pParent.isNull());
|
---|
9103 |
|
---|
9104 | /* associate child with the parent, maximum depth was checked above */
|
---|
9105 | pTarget->i_setParent(this);
|
---|
9106 |
|
---|
9107 | /* diffs for immutable media are auto-reset by default */
|
---|
9108 | bool fAutoReset;
|
---|
9109 | {
|
---|
9110 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
9111 | AutoReadLock block(pBase COMMA_LOCKVAL_SRC_POS);
|
---|
9112 | fAutoReset = (pBase->m->type == MediumType_Immutable);
|
---|
9113 | }
|
---|
9114 | {
|
---|
9115 | AutoWriteLock tlock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
9116 | pTarget->m->autoReset = fAutoReset;
|
---|
9117 | }
|
---|
9118 |
|
---|
9119 | /* register with mVirtualBox as the last step and move to
|
---|
9120 | * Created state only on success (leaving an orphan file is
|
---|
9121 | * better than breaking media registry consistency) */
|
---|
9122 | ComObjPtr<Medium> pMedium;
|
---|
9123 | mrc = m->pVirtualBox->i_registerMedium(pTarget, &pMedium, treeLock);
|
---|
9124 | Assert(pTarget == pMedium);
|
---|
9125 |
|
---|
9126 | if (FAILED(mrc))
|
---|
9127 | /* break the parent association on failure to register */
|
---|
9128 | i_deparent();
|
---|
9129 | }
|
---|
9130 |
|
---|
9131 | AutoMultiWriteLock2 mediaLock(this, pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
9132 |
|
---|
9133 | if (SUCCEEDED(mrc))
|
---|
9134 | {
|
---|
9135 | pTarget->m->state = MediumState_Created;
|
---|
9136 |
|
---|
9137 | pTarget->m->size = size;
|
---|
9138 | pTarget->m->logicalSize = logicalSize;
|
---|
9139 | pTarget->m->variant = variant;
|
---|
9140 | }
|
---|
9141 | else
|
---|
9142 | {
|
---|
9143 | /* back to NotCreated on failure */
|
---|
9144 | pTarget->m->state = MediumState_NotCreated;
|
---|
9145 |
|
---|
9146 | pTarget->m->autoReset = false;
|
---|
9147 |
|
---|
9148 | /* reset UUID to prevent it from being reused next time */
|
---|
9149 | if (fGenerateUuid)
|
---|
9150 | unconst(pTarget->m->id).clear();
|
---|
9151 | }
|
---|
9152 |
|
---|
9153 | // deregister the task registered in createDiffStorage()
|
---|
9154 | Assert(m->numCreateDiffTasks != 0);
|
---|
9155 | --m->numCreateDiffTasks;
|
---|
9156 |
|
---|
9157 | mediaLock.release();
|
---|
9158 | i_markRegistriesModified();
|
---|
9159 | if (task.isAsync())
|
---|
9160 | {
|
---|
9161 | // in asynchronous mode, save settings now
|
---|
9162 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9163 | }
|
---|
9164 |
|
---|
9165 | /* Note that in sync mode, it's the caller's responsibility to
|
---|
9166 | * unlock the medium. */
|
---|
9167 |
|
---|
9168 | if (task.NotifyAboutChanges() && SUCCEEDED(mrc))
|
---|
9169 | {
|
---|
9170 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
9171 | m->pVirtualBox->i_onMediumRegistered(m->id, m->devType, TRUE);
|
---|
9172 | }
|
---|
9173 |
|
---|
9174 | return mrc;
|
---|
9175 | }
|
---|
9176 |
|
---|
9177 | /**
|
---|
9178 | * Implementation code for the "merge" task.
|
---|
9179 | *
|
---|
9180 | * This task always gets started from Medium::mergeTo() and can run
|
---|
9181 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
9182 | * that function. If we run synchronously, the caller expects the medium
|
---|
9183 | * registry modification to be set before returning; otherwise (in asynchronous
|
---|
9184 | * mode), we save the settings ourselves.
|
---|
9185 | *
|
---|
9186 | * @param task
|
---|
9187 | * @return
|
---|
9188 | */
|
---|
9189 | HRESULT Medium::i_taskMergeHandler(Medium::MergeTask &task)
|
---|
9190 | {
|
---|
9191 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
9192 | * to lock order violations, it probably causes lock order issues related
|
---|
9193 | * to the AutoCaller usage. */
|
---|
9194 | HRESULT rcTmp = S_OK;
|
---|
9195 |
|
---|
9196 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
9197 |
|
---|
9198 | try
|
---|
9199 | {
|
---|
9200 | if (!task.mParentForTarget.isNull())
|
---|
9201 | if (task.mParentForTarget->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
9202 | {
|
---|
9203 | AutoReadLock plock(task.mParentForTarget COMMA_LOCKVAL_SRC_POS);
|
---|
9204 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
9205 | tr("Cannot merge image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
9206 | task.mParentForTarget->m->strLocationFull.c_str());
|
---|
9207 | }
|
---|
9208 |
|
---|
9209 | // Resize target to source size, if possible. Otherwise throw an error.
|
---|
9210 | // It's offline resizing. Online resizing will be called in the
|
---|
9211 | // SessionMachine::onlineMergeMedium.
|
---|
9212 |
|
---|
9213 | uint64_t sourceSize = 0;
|
---|
9214 | Utf8Str sourceName;
|
---|
9215 | {
|
---|
9216 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9217 | sourceSize = i_getLogicalSize();
|
---|
9218 | sourceName = i_getName();
|
---|
9219 | }
|
---|
9220 | uint64_t targetSize = 0;
|
---|
9221 | Utf8Str targetName;
|
---|
9222 | {
|
---|
9223 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
9224 | targetSize = pTarget->i_getLogicalSize();
|
---|
9225 | targetName = pTarget->i_getName();
|
---|
9226 | }
|
---|
9227 |
|
---|
9228 | //reducing vm disks are not implemented yet
|
---|
9229 | if (sourceSize > targetSize)
|
---|
9230 | {
|
---|
9231 | if (i_isMediumFormatFile())
|
---|
9232 | {
|
---|
9233 | // Have to make own lock list, because "resize" method resizes only last image
|
---|
9234 | // in the lock chain. The lock chain already in the task.mpMediumLockList, so
|
---|
9235 | // just make new lock list based on it. In fact the own lock list neither makes
|
---|
9236 | // double locking of mediums nor unlocks them during delete, because medium
|
---|
9237 | // already locked by task.mpMediumLockList and own list is used just to specify
|
---|
9238 | // what "resize" method should resize.
|
---|
9239 |
|
---|
9240 | MediumLockList* pMediumLockListForResize = new MediumLockList();
|
---|
9241 |
|
---|
9242 | for (MediumLockList::Base::iterator it = task.mpMediumLockList->GetBegin();
|
---|
9243 | it != task.mpMediumLockList->GetEnd();
|
---|
9244 | ++it)
|
---|
9245 | {
|
---|
9246 | ComObjPtr<Medium> pMedium = it->GetMedium();
|
---|
9247 | pMediumLockListForResize->Append(pMedium, pMedium->m->state == MediumState_LockedWrite);
|
---|
9248 | if (pMedium == pTarget)
|
---|
9249 | break;
|
---|
9250 | }
|
---|
9251 |
|
---|
9252 | // just to switch internal state of the lock list to avoid errors during list deletion,
|
---|
9253 | // because all meduims in the list already locked by task.mpMediumLockList
|
---|
9254 | HRESULT rc = pMediumLockListForResize->Lock(true /* fSkipOverLockedMedia */);
|
---|
9255 | if (FAILED(rc))
|
---|
9256 | {
|
---|
9257 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9258 | rc = setError(rc,
|
---|
9259 | tr("Failed to lock the medium '%s' to resize before merge"),
|
---|
9260 | targetName.c_str());
|
---|
9261 | delete pMediumLockListForResize;
|
---|
9262 | throw rc;
|
---|
9263 | }
|
---|
9264 |
|
---|
9265 | ComObjPtr<Progress> pProgress(task.GetProgressObject());
|
---|
9266 | rc = pTarget->i_resize(sourceSize, pMediumLockListForResize, &pProgress, true, false);
|
---|
9267 | if (FAILED(rc))
|
---|
9268 | {
|
---|
9269 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9270 | rc = setError(rc,
|
---|
9271 | tr("Failed to set size of '%s' to size of '%s'"),
|
---|
9272 | targetName.c_str(), sourceName.c_str());
|
---|
9273 | delete pMediumLockListForResize;
|
---|
9274 | throw rc;
|
---|
9275 | }
|
---|
9276 | delete pMediumLockListForResize;
|
---|
9277 | }
|
---|
9278 | else
|
---|
9279 | {
|
---|
9280 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9281 | HRESULT rc = setError(VBOX_E_NOT_SUPPORTED,
|
---|
9282 | tr("Sizes of '%s' and '%s' are different and medium format does not support resing"),
|
---|
9283 | sourceName.c_str(), targetName.c_str());
|
---|
9284 | throw rc;
|
---|
9285 | }
|
---|
9286 | }
|
---|
9287 |
|
---|
9288 | task.GetProgressObject()->SetNextOperation(BstrFmt(tr("Merging medium '%s' to '%s'"),
|
---|
9289 | i_getName().c_str(),
|
---|
9290 | targetName.c_str()).raw(),
|
---|
9291 | 1);
|
---|
9292 |
|
---|
9293 | PVDISK hdd;
|
---|
9294 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9295 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9296 |
|
---|
9297 | try
|
---|
9298 | {
|
---|
9299 | // Similar code appears in SessionMachine::onlineMergeMedium, so
|
---|
9300 | // if you make any changes below check whether they are applicable
|
---|
9301 | // in that context as well.
|
---|
9302 |
|
---|
9303 | unsigned uTargetIdx = VD_LAST_IMAGE;
|
---|
9304 | unsigned uSourceIdx = VD_LAST_IMAGE;
|
---|
9305 | /* Open all media in the chain. */
|
---|
9306 | MediumLockList::Base::iterator lockListBegin =
|
---|
9307 | task.mpMediumLockList->GetBegin();
|
---|
9308 | MediumLockList::Base::iterator lockListEnd =
|
---|
9309 | task.mpMediumLockList->GetEnd();
|
---|
9310 | unsigned i = 0;
|
---|
9311 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
9312 | it != lockListEnd;
|
---|
9313 | ++it)
|
---|
9314 | {
|
---|
9315 | MediumLock &mediumLock = *it;
|
---|
9316 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9317 |
|
---|
9318 | if (pMedium == this)
|
---|
9319 | uSourceIdx = i;
|
---|
9320 | else if (pMedium == pTarget)
|
---|
9321 | uTargetIdx = i;
|
---|
9322 |
|
---|
9323 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9324 |
|
---|
9325 | /*
|
---|
9326 | * complex sanity (sane complexity)
|
---|
9327 | *
|
---|
9328 | * The current medium must be in the Deleting (medium is merged)
|
---|
9329 | * or LockedRead (parent medium) state if it is not the target.
|
---|
9330 | * If it is the target it must be in the LockedWrite state.
|
---|
9331 | */
|
---|
9332 | Assert( ( pMedium != pTarget
|
---|
9333 | && ( pMedium->m->state == MediumState_Deleting
|
---|
9334 | || pMedium->m->state == MediumState_LockedRead))
|
---|
9335 | || ( pMedium == pTarget
|
---|
9336 | && pMedium->m->state == MediumState_LockedWrite));
|
---|
9337 | /*
|
---|
9338 | * Medium must be the target, in the LockedRead state
|
---|
9339 | * or Deleting state where it is not allowed to be attached
|
---|
9340 | * to a virtual machine.
|
---|
9341 | */
|
---|
9342 | Assert( pMedium == pTarget
|
---|
9343 | || pMedium->m->state == MediumState_LockedRead
|
---|
9344 | || ( pMedium->m->backRefs.size() == 0
|
---|
9345 | && pMedium->m->state == MediumState_Deleting));
|
---|
9346 | /* The source medium must be in Deleting state. */
|
---|
9347 | Assert( pMedium != this
|
---|
9348 | || pMedium->m->state == MediumState_Deleting);
|
---|
9349 |
|
---|
9350 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
9351 |
|
---|
9352 | if ( pMedium->m->state == MediumState_LockedRead
|
---|
9353 | || pMedium->m->state == MediumState_Deleting)
|
---|
9354 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
9355 | if (pMedium->m->type == MediumType_Shareable)
|
---|
9356 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
9357 |
|
---|
9358 | /* Open the medium */
|
---|
9359 | vrc = VDOpen(hdd,
|
---|
9360 | pMedium->m->strFormat.c_str(),
|
---|
9361 | pMedium->m->strLocationFull.c_str(),
|
---|
9362 | uOpenFlags | m->uOpenFlagsDef,
|
---|
9363 | pMedium->m->vdImageIfaces);
|
---|
9364 | if (RT_FAILURE(vrc))
|
---|
9365 | throw vrc;
|
---|
9366 |
|
---|
9367 | i++;
|
---|
9368 | }
|
---|
9369 |
|
---|
9370 | ComAssertThrow( uSourceIdx != VD_LAST_IMAGE
|
---|
9371 | && uTargetIdx != VD_LAST_IMAGE, E_FAIL);
|
---|
9372 |
|
---|
9373 | vrc = VDMerge(hdd, uSourceIdx, uTargetIdx,
|
---|
9374 | task.mVDOperationIfaces);
|
---|
9375 | if (RT_FAILURE(vrc))
|
---|
9376 | throw vrc;
|
---|
9377 |
|
---|
9378 | /* update parent UUIDs */
|
---|
9379 | if (!task.mfMergeForward)
|
---|
9380 | {
|
---|
9381 | /* we need to update UUIDs of all source's children
|
---|
9382 | * which cannot be part of the container at once so
|
---|
9383 | * add each one in there individually */
|
---|
9384 | if (task.mpChildrenToReparent)
|
---|
9385 | {
|
---|
9386 | MediumLockList::Base::iterator childrenBegin = task.mpChildrenToReparent->GetBegin();
|
---|
9387 | MediumLockList::Base::iterator childrenEnd = task.mpChildrenToReparent->GetEnd();
|
---|
9388 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
9389 | it != childrenEnd;
|
---|
9390 | ++it)
|
---|
9391 | {
|
---|
9392 | Medium *pMedium = it->GetMedium();
|
---|
9393 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
9394 | vrc = VDOpen(hdd,
|
---|
9395 | pMedium->m->strFormat.c_str(),
|
---|
9396 | pMedium->m->strLocationFull.c_str(),
|
---|
9397 | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
9398 | pMedium->m->vdImageIfaces);
|
---|
9399 | if (RT_FAILURE(vrc))
|
---|
9400 | throw vrc;
|
---|
9401 |
|
---|
9402 | vrc = VDSetParentUuid(hdd, VD_LAST_IMAGE,
|
---|
9403 | pTarget->m->id.raw());
|
---|
9404 | if (RT_FAILURE(vrc))
|
---|
9405 | throw vrc;
|
---|
9406 |
|
---|
9407 | vrc = VDClose(hdd, false /* fDelete */);
|
---|
9408 | if (RT_FAILURE(vrc))
|
---|
9409 | throw vrc;
|
---|
9410 | }
|
---|
9411 | }
|
---|
9412 | }
|
---|
9413 | }
|
---|
9414 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9415 | catch (int aVRC)
|
---|
9416 | {
|
---|
9417 | rcTmp = setErrorBoth(VBOX_E_FILE_ERROR, aVRC,
|
---|
9418 | tr("Could not merge the medium '%s' to '%s'%s"),
|
---|
9419 | m->strLocationFull.c_str(),
|
---|
9420 | pTarget->m->strLocationFull.c_str(),
|
---|
9421 | i_vdError(aVRC).c_str());
|
---|
9422 | }
|
---|
9423 |
|
---|
9424 | VDDestroy(hdd);
|
---|
9425 | }
|
---|
9426 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9427 |
|
---|
9428 | ErrorInfoKeeper eik;
|
---|
9429 | MultiResult mrc(rcTmp);
|
---|
9430 | HRESULT rc2;
|
---|
9431 |
|
---|
9432 | std::set<ComObjPtr<Medium> > pMediumsForNotify;
|
---|
9433 | std::map<Guid, DeviceType_T> uIdsForNotify;
|
---|
9434 |
|
---|
9435 | if (SUCCEEDED(mrc))
|
---|
9436 | {
|
---|
9437 | /* all media but the target were successfully deleted by
|
---|
9438 | * VDMerge; reparent the last one and uninitialize deleted media. */
|
---|
9439 |
|
---|
9440 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
9441 |
|
---|
9442 | if (task.mfMergeForward)
|
---|
9443 | {
|
---|
9444 | /* first, unregister the target since it may become a base
|
---|
9445 | * medium which needs re-registration */
|
---|
9446 | rc2 = m->pVirtualBox->i_unregisterMedium(pTarget);
|
---|
9447 | AssertComRC(rc2);
|
---|
9448 |
|
---|
9449 | /* then, reparent it and disconnect the deleted branch at both ends
|
---|
9450 | * (chain->parent() is source's parent). Depth check above. */
|
---|
9451 | pTarget->i_deparent();
|
---|
9452 | pTarget->i_setParent(task.mParentForTarget);
|
---|
9453 | if (task.mParentForTarget)
|
---|
9454 | {
|
---|
9455 | i_deparent();
|
---|
9456 | if (task.NotifyAboutChanges())
|
---|
9457 | pMediumsForNotify.insert(task.mParentForTarget);
|
---|
9458 | }
|
---|
9459 |
|
---|
9460 | /* then, register again */
|
---|
9461 | ComObjPtr<Medium> pMedium;
|
---|
9462 | rc2 = m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
9463 | treeLock);
|
---|
9464 | AssertComRC(rc2);
|
---|
9465 | }
|
---|
9466 | else
|
---|
9467 | {
|
---|
9468 | Assert(pTarget->i_getChildren().size() == 1);
|
---|
9469 | Medium *targetChild = pTarget->i_getChildren().front();
|
---|
9470 |
|
---|
9471 | /* disconnect the deleted branch at the elder end */
|
---|
9472 | targetChild->i_deparent();
|
---|
9473 |
|
---|
9474 | /* reparent source's children and disconnect the deleted
|
---|
9475 | * branch at the younger end */
|
---|
9476 | if (task.mpChildrenToReparent)
|
---|
9477 | {
|
---|
9478 | /* obey {parent,child} lock order */
|
---|
9479 | AutoWriteLock sourceLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9480 |
|
---|
9481 | MediumLockList::Base::iterator childrenBegin = task.mpChildrenToReparent->GetBegin();
|
---|
9482 | MediumLockList::Base::iterator childrenEnd = task.mpChildrenToReparent->GetEnd();
|
---|
9483 | for (MediumLockList::Base::iterator it = childrenBegin;
|
---|
9484 | it != childrenEnd;
|
---|
9485 | ++it)
|
---|
9486 | {
|
---|
9487 | Medium *pMedium = it->GetMedium();
|
---|
9488 | AutoWriteLock childLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9489 |
|
---|
9490 | pMedium->i_deparent(); // removes pMedium from source
|
---|
9491 | // no depth check, reduces depth
|
---|
9492 | pMedium->i_setParent(pTarget);
|
---|
9493 |
|
---|
9494 | if (task.NotifyAboutChanges())
|
---|
9495 | pMediumsForNotify.insert(pMedium);
|
---|
9496 | }
|
---|
9497 | }
|
---|
9498 | pMediumsForNotify.insert(pTarget);
|
---|
9499 | }
|
---|
9500 |
|
---|
9501 | /* unregister and uninitialize all media removed by the merge */
|
---|
9502 | MediumLockList::Base::iterator lockListBegin =
|
---|
9503 | task.mpMediumLockList->GetBegin();
|
---|
9504 | MediumLockList::Base::iterator lockListEnd =
|
---|
9505 | task.mpMediumLockList->GetEnd();
|
---|
9506 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
9507 | it != lockListEnd;
|
---|
9508 | )
|
---|
9509 | {
|
---|
9510 | MediumLock &mediumLock = *it;
|
---|
9511 | /* Create a real copy of the medium pointer, as the medium
|
---|
9512 | * lock deletion below would invalidate the referenced object. */
|
---|
9513 | const ComObjPtr<Medium> pMedium = mediumLock.GetMedium();
|
---|
9514 |
|
---|
9515 | /* The target and all media not merged (readonly) are skipped */
|
---|
9516 | if ( pMedium == pTarget
|
---|
9517 | || pMedium->m->state == MediumState_LockedRead)
|
---|
9518 | {
|
---|
9519 | ++it;
|
---|
9520 | continue;
|
---|
9521 | }
|
---|
9522 |
|
---|
9523 | uIdsForNotify[pMedium->i_getId()] = pMedium->i_getDeviceType();
|
---|
9524 | rc2 = pMedium->m->pVirtualBox->i_unregisterMedium(pMedium);
|
---|
9525 | AssertComRC(rc2);
|
---|
9526 |
|
---|
9527 | /* now, uninitialize the deleted medium (note that
|
---|
9528 | * due to the Deleting state, uninit() will not touch
|
---|
9529 | * the parent-child relationship so we need to
|
---|
9530 | * uninitialize each disk individually) */
|
---|
9531 |
|
---|
9532 | /* note that the operation initiator medium (which is
|
---|
9533 | * normally also the source medium) is a special case
|
---|
9534 | * -- there is one more caller added by Task to it which
|
---|
9535 | * we must release. Also, if we are in sync mode, the
|
---|
9536 | * caller may still hold an AutoCaller instance for it
|
---|
9537 | * and therefore we cannot uninit() it (it's therefore
|
---|
9538 | * the caller's responsibility) */
|
---|
9539 | if (pMedium == this)
|
---|
9540 | {
|
---|
9541 | Assert(i_getChildren().size() == 0);
|
---|
9542 | Assert(m->backRefs.size() == 0);
|
---|
9543 | task.mMediumCaller.release();
|
---|
9544 | }
|
---|
9545 |
|
---|
9546 | /* Delete the medium lock list entry, which also releases the
|
---|
9547 | * caller added by MergeChain before uninit() and updates the
|
---|
9548 | * iterator to point to the right place. */
|
---|
9549 | rc2 = task.mpMediumLockList->RemoveByIterator(it);
|
---|
9550 | AssertComRC(rc2);
|
---|
9551 |
|
---|
9552 | if (task.isAsync() || pMedium != this)
|
---|
9553 | {
|
---|
9554 | treeLock.release();
|
---|
9555 | pMedium->uninit();
|
---|
9556 | treeLock.acquire();
|
---|
9557 | }
|
---|
9558 | }
|
---|
9559 | }
|
---|
9560 |
|
---|
9561 | i_markRegistriesModified();
|
---|
9562 | if (task.isAsync())
|
---|
9563 | {
|
---|
9564 | // in asynchronous mode, save settings now
|
---|
9565 | eik.restore();
|
---|
9566 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9567 | eik.fetch();
|
---|
9568 | }
|
---|
9569 |
|
---|
9570 | if (FAILED(mrc))
|
---|
9571 | {
|
---|
9572 | /* Here we come if either VDMerge() failed (in which case we
|
---|
9573 | * assume that it tried to do everything to make a further
|
---|
9574 | * retry possible -- e.g. not deleted intermediate media
|
---|
9575 | * and so on) or VirtualBox::saveRegistries() failed (where we
|
---|
9576 | * should have the original tree but with intermediate storage
|
---|
9577 | * units deleted by VDMerge()). We have to only restore states
|
---|
9578 | * (through the MergeChain dtor) unless we are run synchronously
|
---|
9579 | * in which case it's the responsibility of the caller as stated
|
---|
9580 | * in the mergeTo() docs. The latter also implies that we
|
---|
9581 | * don't own the merge chain, so release it in this case. */
|
---|
9582 | if (task.isAsync())
|
---|
9583 | i_cancelMergeTo(task.mpChildrenToReparent, task.mpMediumLockList);
|
---|
9584 | }
|
---|
9585 | else if (task.NotifyAboutChanges())
|
---|
9586 | {
|
---|
9587 | for (std::set<ComObjPtr<Medium> >::const_iterator it = pMediumsForNotify.begin();
|
---|
9588 | it != pMediumsForNotify.end();
|
---|
9589 | ++it)
|
---|
9590 | {
|
---|
9591 | if (it->isNotNull())
|
---|
9592 | m->pVirtualBox->i_onMediumConfigChanged(*it);
|
---|
9593 | }
|
---|
9594 | for (std::map<Guid, DeviceType_T>::const_iterator it = uIdsForNotify.begin();
|
---|
9595 | it != uIdsForNotify.end();
|
---|
9596 | ++it)
|
---|
9597 | {
|
---|
9598 | m->pVirtualBox->i_onMediumRegistered(it->first, it->second, FALSE);
|
---|
9599 | }
|
---|
9600 | }
|
---|
9601 |
|
---|
9602 | return mrc;
|
---|
9603 | }
|
---|
9604 |
|
---|
9605 | /**
|
---|
9606 | * Implementation code for the "clone" task.
|
---|
9607 | *
|
---|
9608 | * This only gets started from Medium::CloneTo() and always runs asynchronously.
|
---|
9609 | * As a result, we always save the VirtualBox.xml file when we're done here.
|
---|
9610 | *
|
---|
9611 | * @param task
|
---|
9612 | * @return
|
---|
9613 | */
|
---|
9614 | HRESULT Medium::i_taskCloneHandler(Medium::CloneTask &task)
|
---|
9615 | {
|
---|
9616 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
9617 | * to lock order violations, it probably causes lock order issues related
|
---|
9618 | * to the AutoCaller usage. */
|
---|
9619 | HRESULT rcTmp = S_OK;
|
---|
9620 |
|
---|
9621 | const ComObjPtr<Medium> &pTarget = task.mTarget;
|
---|
9622 | const ComObjPtr<Medium> &pParent = task.mParent;
|
---|
9623 |
|
---|
9624 | bool fCreatingTarget = false;
|
---|
9625 |
|
---|
9626 | uint64_t size = 0, logicalSize = 0;
|
---|
9627 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
9628 | bool fGenerateUuid = false;
|
---|
9629 |
|
---|
9630 | try
|
---|
9631 | {
|
---|
9632 | if (!pParent.isNull())
|
---|
9633 | {
|
---|
9634 |
|
---|
9635 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
9636 | {
|
---|
9637 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
9638 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
9639 | tr("Cannot clone image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
9640 | pParent->m->strLocationFull.c_str());
|
---|
9641 | }
|
---|
9642 | }
|
---|
9643 |
|
---|
9644 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9645 | * signal from the task initiator (which releases it only after
|
---|
9646 | * RTThreadCreate()) that we can start the job. */
|
---|
9647 | AutoMultiWriteLock3 thisLock(this, pTarget, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
9648 |
|
---|
9649 | fCreatingTarget = pTarget->m->state == MediumState_Creating;
|
---|
9650 |
|
---|
9651 | /* The object may request a specific UUID (through a special form of
|
---|
9652 | * the moveTo() argument). Otherwise we have to generate it */
|
---|
9653 | Guid targetId = pTarget->m->id;
|
---|
9654 |
|
---|
9655 | fGenerateUuid = targetId.isZero();
|
---|
9656 | if (fGenerateUuid)
|
---|
9657 | {
|
---|
9658 | targetId.create();
|
---|
9659 | /* VirtualBox::registerMedium() will need UUID */
|
---|
9660 | unconst(pTarget->m->id) = targetId;
|
---|
9661 | }
|
---|
9662 |
|
---|
9663 | PVDISK hdd;
|
---|
9664 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9665 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9666 |
|
---|
9667 | try
|
---|
9668 | {
|
---|
9669 | /* Open all media in the source chain. */
|
---|
9670 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
9671 | task.mpSourceMediumLockList->GetBegin();
|
---|
9672 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
9673 | task.mpSourceMediumLockList->GetEnd();
|
---|
9674 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
9675 | it != sourceListEnd;
|
---|
9676 | ++it)
|
---|
9677 | {
|
---|
9678 | const MediumLock &mediumLock = *it;
|
---|
9679 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9680 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9681 |
|
---|
9682 | /* sanity check */
|
---|
9683 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
9684 |
|
---|
9685 | /** Open all media in read-only mode. */
|
---|
9686 | vrc = VDOpen(hdd,
|
---|
9687 | pMedium->m->strFormat.c_str(),
|
---|
9688 | pMedium->m->strLocationFull.c_str(),
|
---|
9689 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
9690 | pMedium->m->vdImageIfaces);
|
---|
9691 | if (RT_FAILURE(vrc))
|
---|
9692 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9693 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9694 | pMedium->m->strLocationFull.c_str(),
|
---|
9695 | i_vdError(vrc).c_str());
|
---|
9696 | }
|
---|
9697 |
|
---|
9698 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
9699 | Utf8Str targetLocation(pTarget->m->strLocationFull);
|
---|
9700 | uint64_t capabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
9701 |
|
---|
9702 | Assert( pTarget->m->state == MediumState_Creating
|
---|
9703 | || pTarget->m->state == MediumState_LockedWrite);
|
---|
9704 | Assert(m->state == MediumState_LockedRead);
|
---|
9705 | Assert( pParent.isNull()
|
---|
9706 | || pParent->m->state == MediumState_LockedRead);
|
---|
9707 |
|
---|
9708 | /* unlock before the potentially lengthy operation */
|
---|
9709 | thisLock.release();
|
---|
9710 |
|
---|
9711 | /* ensure the target directory exists */
|
---|
9712 | if (capabilities & MediumFormatCapabilities_File)
|
---|
9713 | {
|
---|
9714 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
9715 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
9716 | if (FAILED(rc))
|
---|
9717 | throw rc;
|
---|
9718 | }
|
---|
9719 |
|
---|
9720 | PVDISK targetHdd;
|
---|
9721 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &targetHdd);
|
---|
9722 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9723 |
|
---|
9724 | try
|
---|
9725 | {
|
---|
9726 | /* Open all media in the target chain. */
|
---|
9727 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
9728 | task.mpTargetMediumLockList->GetBegin();
|
---|
9729 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
9730 | task.mpTargetMediumLockList->GetEnd();
|
---|
9731 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
9732 | it != targetListEnd;
|
---|
9733 | ++it)
|
---|
9734 | {
|
---|
9735 | const MediumLock &mediumLock = *it;
|
---|
9736 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
9737 |
|
---|
9738 | /* If the target medium is not created yet there's no
|
---|
9739 | * reason to open it. */
|
---|
9740 | if (pMedium == pTarget && fCreatingTarget)
|
---|
9741 | continue;
|
---|
9742 |
|
---|
9743 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
9744 |
|
---|
9745 | /* sanity check */
|
---|
9746 | Assert( pMedium->m->state == MediumState_LockedRead
|
---|
9747 | || pMedium->m->state == MediumState_LockedWrite);
|
---|
9748 |
|
---|
9749 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
9750 | if (pMedium->m->state != MediumState_LockedWrite)
|
---|
9751 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
9752 | if (pMedium->m->type == MediumType_Shareable)
|
---|
9753 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
9754 |
|
---|
9755 | /* Open all media in appropriate mode. */
|
---|
9756 | vrc = VDOpen(targetHdd,
|
---|
9757 | pMedium->m->strFormat.c_str(),
|
---|
9758 | pMedium->m->strLocationFull.c_str(),
|
---|
9759 | uOpenFlags | m->uOpenFlagsDef,
|
---|
9760 | pMedium->m->vdImageIfaces);
|
---|
9761 | if (RT_FAILURE(vrc))
|
---|
9762 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9763 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
9764 | pMedium->m->strLocationFull.c_str(),
|
---|
9765 | i_vdError(vrc).c_str());
|
---|
9766 | }
|
---|
9767 |
|
---|
9768 | /* target isn't locked, but no changing data is accessed */
|
---|
9769 | if (task.midxSrcImageSame == UINT32_MAX)
|
---|
9770 | {
|
---|
9771 | vrc = VDCopy(hdd,
|
---|
9772 | VD_LAST_IMAGE,
|
---|
9773 | targetHdd,
|
---|
9774 | targetFormat.c_str(),
|
---|
9775 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
9776 | false /* fMoveByRename */,
|
---|
9777 | (uint64_t) task.mTargetLogicalSize /* cbSize */,
|
---|
9778 | task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted),
|
---|
9779 | targetId.raw(),
|
---|
9780 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
9781 | NULL /* pVDIfsOperation */,
|
---|
9782 | pTarget->m->vdImageIfaces,
|
---|
9783 | task.mVDOperationIfaces);
|
---|
9784 | }
|
---|
9785 | else
|
---|
9786 | {
|
---|
9787 | vrc = VDCopyEx(hdd,
|
---|
9788 | VD_LAST_IMAGE,
|
---|
9789 | targetHdd,
|
---|
9790 | targetFormat.c_str(),
|
---|
9791 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
9792 | false /* fMoveByRename */,
|
---|
9793 | (uint64_t) task.mTargetLogicalSize /* cbSize */,
|
---|
9794 | task.midxSrcImageSame,
|
---|
9795 | task.midxDstImageSame,
|
---|
9796 | task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted),
|
---|
9797 | targetId.raw(),
|
---|
9798 | VD_OPEN_FLAGS_NORMAL | m->uOpenFlagsDef,
|
---|
9799 | NULL /* pVDIfsOperation */,
|
---|
9800 | pTarget->m->vdImageIfaces,
|
---|
9801 | task.mVDOperationIfaces);
|
---|
9802 | }
|
---|
9803 | if (RT_FAILURE(vrc))
|
---|
9804 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
9805 | tr("Could not create the clone medium '%s'%s"),
|
---|
9806 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
9807 |
|
---|
9808 | size = VDGetFileSize(targetHdd, VD_LAST_IMAGE);
|
---|
9809 | logicalSize = VDGetSize(targetHdd, VD_LAST_IMAGE);
|
---|
9810 | unsigned uImageFlags;
|
---|
9811 | vrc = VDGetImageFlags(targetHdd, 0, &uImageFlags);
|
---|
9812 | if (RT_SUCCESS(vrc))
|
---|
9813 | variant = (MediumVariant_T)uImageFlags;
|
---|
9814 | }
|
---|
9815 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9816 |
|
---|
9817 | VDDestroy(targetHdd);
|
---|
9818 | }
|
---|
9819 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9820 |
|
---|
9821 | VDDestroy(hdd);
|
---|
9822 | }
|
---|
9823 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
9824 |
|
---|
9825 | ErrorInfoKeeper eik;
|
---|
9826 | MultiResult mrc(rcTmp);
|
---|
9827 |
|
---|
9828 | /* Only do the parent changes for newly created media. */
|
---|
9829 | if (SUCCEEDED(mrc) && fCreatingTarget)
|
---|
9830 | {
|
---|
9831 | /* we set m->pParent & children() */
|
---|
9832 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
9833 |
|
---|
9834 | Assert(pTarget->m->pParent.isNull());
|
---|
9835 |
|
---|
9836 | if (pParent)
|
---|
9837 | {
|
---|
9838 | /* Associate the clone with the parent and deassociate
|
---|
9839 | * from VirtualBox. Depth check above. */
|
---|
9840 | pTarget->i_setParent(pParent);
|
---|
9841 |
|
---|
9842 | /* register with mVirtualBox as the last step and move to
|
---|
9843 | * Created state only on success (leaving an orphan file is
|
---|
9844 | * better than breaking media registry consistency) */
|
---|
9845 | eik.restore();
|
---|
9846 | ComObjPtr<Medium> pMedium;
|
---|
9847 | mrc = pParent->m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
9848 | treeLock);
|
---|
9849 | Assert( FAILED(mrc)
|
---|
9850 | || pTarget == pMedium);
|
---|
9851 | eik.fetch();
|
---|
9852 |
|
---|
9853 | if (FAILED(mrc))
|
---|
9854 | /* break parent association on failure to register */
|
---|
9855 | pTarget->i_deparent(); // removes target from parent
|
---|
9856 | }
|
---|
9857 | else
|
---|
9858 | {
|
---|
9859 | /* just register */
|
---|
9860 | eik.restore();
|
---|
9861 | ComObjPtr<Medium> pMedium;
|
---|
9862 | mrc = m->pVirtualBox->i_registerMedium(pTarget, &pMedium,
|
---|
9863 | treeLock);
|
---|
9864 | Assert( FAILED(mrc)
|
---|
9865 | || pTarget == pMedium);
|
---|
9866 | eik.fetch();
|
---|
9867 | }
|
---|
9868 | }
|
---|
9869 |
|
---|
9870 | if (fCreatingTarget)
|
---|
9871 | {
|
---|
9872 | AutoWriteLock mLock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
9873 |
|
---|
9874 | if (SUCCEEDED(mrc))
|
---|
9875 | {
|
---|
9876 | pTarget->m->state = MediumState_Created;
|
---|
9877 |
|
---|
9878 | pTarget->m->size = size;
|
---|
9879 | pTarget->m->logicalSize = logicalSize;
|
---|
9880 | pTarget->m->variant = variant;
|
---|
9881 | }
|
---|
9882 | else
|
---|
9883 | {
|
---|
9884 | /* back to NotCreated on failure */
|
---|
9885 | pTarget->m->state = MediumState_NotCreated;
|
---|
9886 |
|
---|
9887 | /* reset UUID to prevent it from being reused next time */
|
---|
9888 | if (fGenerateUuid)
|
---|
9889 | unconst(pTarget->m->id).clear();
|
---|
9890 | }
|
---|
9891 | }
|
---|
9892 |
|
---|
9893 | /* Copy any filter related settings over to the target. */
|
---|
9894 | if (SUCCEEDED(mrc))
|
---|
9895 | {
|
---|
9896 | /* Copy any filter related settings over. */
|
---|
9897 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
9898 | ComObjPtr<Medium> pTargetBase = pTarget->i_getBase();
|
---|
9899 | std::vector<com::Utf8Str> aFilterPropNames;
|
---|
9900 | std::vector<com::Utf8Str> aFilterPropValues;
|
---|
9901 | mrc = pBase->i_getFilterProperties(aFilterPropNames, aFilterPropValues);
|
---|
9902 | if (SUCCEEDED(mrc))
|
---|
9903 | {
|
---|
9904 | /* Go through the properties and add them to the target medium. */
|
---|
9905 | for (unsigned idx = 0; idx < aFilterPropNames.size(); idx++)
|
---|
9906 | {
|
---|
9907 | mrc = pTargetBase->i_setPropertyDirect(aFilterPropNames[idx], aFilterPropValues[idx]);
|
---|
9908 | if (FAILED(mrc)) break;
|
---|
9909 | }
|
---|
9910 |
|
---|
9911 | // now, at the end of this task (always asynchronous), save the settings
|
---|
9912 | if (SUCCEEDED(mrc))
|
---|
9913 | {
|
---|
9914 | // save the settings
|
---|
9915 | i_markRegistriesModified();
|
---|
9916 | /* collect multiple errors */
|
---|
9917 | eik.restore();
|
---|
9918 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
9919 | eik.fetch();
|
---|
9920 |
|
---|
9921 | if (task.NotifyAboutChanges())
|
---|
9922 | {
|
---|
9923 | if (!fCreatingTarget)
|
---|
9924 | {
|
---|
9925 | if (!aFilterPropNames.empty())
|
---|
9926 | m->pVirtualBox->i_onMediumConfigChanged(pTargetBase);
|
---|
9927 | if (pParent)
|
---|
9928 | m->pVirtualBox->i_onMediumConfigChanged(pParent);
|
---|
9929 | }
|
---|
9930 | else
|
---|
9931 | {
|
---|
9932 | m->pVirtualBox->i_onMediumRegistered(pTarget->i_getId(), pTarget->i_getDeviceType(), TRUE);
|
---|
9933 | }
|
---|
9934 | }
|
---|
9935 | }
|
---|
9936 | }
|
---|
9937 | }
|
---|
9938 |
|
---|
9939 | /* Everything is explicitly unlocked when the task exits,
|
---|
9940 | * as the task destruction also destroys the source chain. */
|
---|
9941 |
|
---|
9942 | /* Make sure the source chain is released early. It could happen
|
---|
9943 | * that we get a deadlock in Appliance::Import when Medium::Close
|
---|
9944 | * is called & the source chain is released at the same time. */
|
---|
9945 | task.mpSourceMediumLockList->Clear();
|
---|
9946 |
|
---|
9947 | return mrc;
|
---|
9948 | }
|
---|
9949 |
|
---|
9950 | /**
|
---|
9951 | * Implementation code for the "move" task.
|
---|
9952 | *
|
---|
9953 | * This only gets started from Medium::MoveTo() and always
|
---|
9954 | * runs asynchronously.
|
---|
9955 | *
|
---|
9956 | * @param task
|
---|
9957 | * @return
|
---|
9958 | */
|
---|
9959 | HRESULT Medium::i_taskMoveHandler(Medium::MoveTask &task)
|
---|
9960 | {
|
---|
9961 | LogFlowFuncEnter();
|
---|
9962 | HRESULT rcOut = S_OK;
|
---|
9963 |
|
---|
9964 | /* pTarget is equal "this" in our case */
|
---|
9965 | const ComObjPtr<Medium> &pTarget = task.mMedium;
|
---|
9966 |
|
---|
9967 | uint64_t size = 0; NOREF(size);
|
---|
9968 | uint64_t logicalSize = 0; NOREF(logicalSize);
|
---|
9969 | MediumVariant_T variant = MediumVariant_Standard; NOREF(variant);
|
---|
9970 |
|
---|
9971 | /*
|
---|
9972 | * it's exactly moving, not cloning
|
---|
9973 | */
|
---|
9974 | if (!i_isMoveOperation(pTarget))
|
---|
9975 | {
|
---|
9976 | HRESULT rc = setError(VBOX_E_FILE_ERROR,
|
---|
9977 | tr("Wrong preconditions for moving the medium %s"),
|
---|
9978 | pTarget->m->strLocationFull.c_str());
|
---|
9979 | LogFlowFunc(("LEAVE: rc=%Rhrc (early)\n", rc));
|
---|
9980 | return rc;
|
---|
9981 | }
|
---|
9982 |
|
---|
9983 | try
|
---|
9984 | {
|
---|
9985 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
9986 | * signal from the task initiator (which releases it only after
|
---|
9987 | * RTThreadCreate()) that we can start the job. */
|
---|
9988 |
|
---|
9989 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
9990 |
|
---|
9991 | PVDISK hdd;
|
---|
9992 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
9993 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
9994 |
|
---|
9995 | try
|
---|
9996 | {
|
---|
9997 | /* Open all media in the source chain. */
|
---|
9998 | MediumLockList::Base::const_iterator sourceListBegin =
|
---|
9999 | task.mpMediumLockList->GetBegin();
|
---|
10000 | MediumLockList::Base::const_iterator sourceListEnd =
|
---|
10001 | task.mpMediumLockList->GetEnd();
|
---|
10002 | for (MediumLockList::Base::const_iterator it = sourceListBegin;
|
---|
10003 | it != sourceListEnd;
|
---|
10004 | ++it)
|
---|
10005 | {
|
---|
10006 | const MediumLock &mediumLock = *it;
|
---|
10007 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
10008 | AutoWriteLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
10009 |
|
---|
10010 | /* sanity check */
|
---|
10011 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
10012 |
|
---|
10013 | vrc = VDOpen(hdd,
|
---|
10014 | pMedium->m->strFormat.c_str(),
|
---|
10015 | pMedium->m->strLocationFull.c_str(),
|
---|
10016 | VD_OPEN_FLAGS_NORMAL,
|
---|
10017 | pMedium->m->vdImageIfaces);
|
---|
10018 | if (RT_FAILURE(vrc))
|
---|
10019 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10020 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10021 | pMedium->m->strLocationFull.c_str(),
|
---|
10022 | i_vdError(vrc).c_str());
|
---|
10023 | }
|
---|
10024 |
|
---|
10025 | /* we can directly use pTarget->m->"variables" but for better reading we use local copies */
|
---|
10026 | Guid targetId = pTarget->m->id;
|
---|
10027 | Utf8Str targetFormat(pTarget->m->strFormat);
|
---|
10028 | uint64_t targetCapabilities = pTarget->m->formatObj->i_getCapabilities();
|
---|
10029 |
|
---|
10030 | /*
|
---|
10031 | * change target location
|
---|
10032 | * m->strNewLocationFull has been set already together with m->fMoveThisMedium in
|
---|
10033 | * i_preparationForMoving()
|
---|
10034 | */
|
---|
10035 | Utf8Str targetLocation = i_getNewLocationForMoving();
|
---|
10036 |
|
---|
10037 | /* unlock before the potentially lengthy operation */
|
---|
10038 | thisLock.release();
|
---|
10039 |
|
---|
10040 | /* ensure the target directory exists */
|
---|
10041 | if (targetCapabilities & MediumFormatCapabilities_File)
|
---|
10042 | {
|
---|
10043 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
10044 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
10045 | if (FAILED(rc))
|
---|
10046 | throw rc;
|
---|
10047 | }
|
---|
10048 |
|
---|
10049 | try
|
---|
10050 | {
|
---|
10051 | vrc = VDCopy(hdd,
|
---|
10052 | VD_LAST_IMAGE,
|
---|
10053 | hdd,
|
---|
10054 | targetFormat.c_str(),
|
---|
10055 | targetLocation.c_str(),
|
---|
10056 | true /* fMoveByRename */,
|
---|
10057 | 0 /* cbSize */,
|
---|
10058 | VD_IMAGE_FLAGS_NONE,
|
---|
10059 | targetId.raw(),
|
---|
10060 | VD_OPEN_FLAGS_NORMAL,
|
---|
10061 | NULL /* pVDIfsOperation */,
|
---|
10062 | pTarget->m->vdImageIfaces,
|
---|
10063 | task.mVDOperationIfaces);
|
---|
10064 | if (RT_FAILURE(vrc))
|
---|
10065 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10066 | tr("Could not move medium '%s'%s"),
|
---|
10067 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
10068 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
10069 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
10070 | unsigned uImageFlags;
|
---|
10071 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
10072 | if (RT_SUCCESS(vrc))
|
---|
10073 | variant = (MediumVariant_T)uImageFlags;
|
---|
10074 |
|
---|
10075 | /*
|
---|
10076 | * set current location, because VDCopy\VDCopyEx doesn't do it.
|
---|
10077 | * also reset moving flag
|
---|
10078 | */
|
---|
10079 | i_resetMoveOperationData();
|
---|
10080 | m->strLocationFull = targetLocation;
|
---|
10081 |
|
---|
10082 | }
|
---|
10083 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
10084 |
|
---|
10085 | }
|
---|
10086 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
10087 |
|
---|
10088 | VDDestroy(hdd);
|
---|
10089 | }
|
---|
10090 | catch (HRESULT aRC) { rcOut = aRC; }
|
---|
10091 |
|
---|
10092 | ErrorInfoKeeper eik;
|
---|
10093 | MultiResult mrc(rcOut);
|
---|
10094 |
|
---|
10095 | // now, at the end of this task (always asynchronous), save the settings
|
---|
10096 | if (SUCCEEDED(mrc))
|
---|
10097 | {
|
---|
10098 | // save the settings
|
---|
10099 | i_markRegistriesModified();
|
---|
10100 | /* collect multiple errors */
|
---|
10101 | eik.restore();
|
---|
10102 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
10103 | eik.fetch();
|
---|
10104 | }
|
---|
10105 |
|
---|
10106 | /* Everything is explicitly unlocked when the task exits,
|
---|
10107 | * as the task destruction also destroys the source chain. */
|
---|
10108 |
|
---|
10109 | task.mpMediumLockList->Clear();
|
---|
10110 |
|
---|
10111 | if (task.NotifyAboutChanges() && SUCCEEDED(mrc))
|
---|
10112 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
10113 |
|
---|
10114 | LogFlowFunc(("LEAVE: mrc=%Rhrc\n", (HRESULT)mrc));
|
---|
10115 | return mrc;
|
---|
10116 | }
|
---|
10117 |
|
---|
10118 | /**
|
---|
10119 | * Implementation code for the "delete" task.
|
---|
10120 | *
|
---|
10121 | * This task always gets started from Medium::deleteStorage() and can run
|
---|
10122 | * synchronously or asynchronously depending on the "wait" parameter passed to
|
---|
10123 | * that function.
|
---|
10124 | *
|
---|
10125 | * @param task
|
---|
10126 | * @return
|
---|
10127 | */
|
---|
10128 | HRESULT Medium::i_taskDeleteHandler(Medium::DeleteTask &task)
|
---|
10129 | {
|
---|
10130 | NOREF(task);
|
---|
10131 | HRESULT rc = S_OK;
|
---|
10132 |
|
---|
10133 | try
|
---|
10134 | {
|
---|
10135 | /* The lock is also used as a signal from the task initiator (which
|
---|
10136 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
10137 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10138 |
|
---|
10139 | PVDISK hdd;
|
---|
10140 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
10141 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
10142 |
|
---|
10143 | Utf8Str format(m->strFormat);
|
---|
10144 | Utf8Str location(m->strLocationFull);
|
---|
10145 |
|
---|
10146 | /* unlock before the potentially lengthy operation */
|
---|
10147 | Assert(m->state == MediumState_Deleting);
|
---|
10148 | thisLock.release();
|
---|
10149 |
|
---|
10150 | try
|
---|
10151 | {
|
---|
10152 | vrc = VDOpen(hdd,
|
---|
10153 | format.c_str(),
|
---|
10154 | location.c_str(),
|
---|
10155 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
10156 | m->vdImageIfaces);
|
---|
10157 | if (RT_SUCCESS(vrc))
|
---|
10158 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
10159 |
|
---|
10160 | if (RT_FAILURE(vrc) && vrc != VERR_FILE_NOT_FOUND)
|
---|
10161 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10162 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
10163 | location.c_str(), i_vdError(vrc).c_str());
|
---|
10164 |
|
---|
10165 | }
|
---|
10166 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10167 |
|
---|
10168 | VDDestroy(hdd);
|
---|
10169 | }
|
---|
10170 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10171 |
|
---|
10172 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10173 |
|
---|
10174 | /* go to the NotCreated state even on failure since the storage
|
---|
10175 | * may have been already partially deleted and cannot be used any
|
---|
10176 | * more. One will be able to manually re-open the storage if really
|
---|
10177 | * needed to re-register it. */
|
---|
10178 | m->state = MediumState_NotCreated;
|
---|
10179 |
|
---|
10180 | /* Reset UUID to prevent Create* from reusing it again */
|
---|
10181 | com::Guid uOldId = m->id;
|
---|
10182 | unconst(m->id).clear();
|
---|
10183 |
|
---|
10184 | if (task.NotifyAboutChanges() && SUCCEEDED(rc))
|
---|
10185 | {
|
---|
10186 | if (m->pParent.isNotNull())
|
---|
10187 | m->pVirtualBox->i_onMediumConfigChanged(m->pParent);
|
---|
10188 | m->pVirtualBox->i_onMediumRegistered(uOldId, m->devType, FALSE);
|
---|
10189 | }
|
---|
10190 |
|
---|
10191 | return rc;
|
---|
10192 | }
|
---|
10193 |
|
---|
10194 | /**
|
---|
10195 | * Implementation code for the "reset" task.
|
---|
10196 | *
|
---|
10197 | * This always gets started asynchronously from Medium::Reset().
|
---|
10198 | *
|
---|
10199 | * @param task
|
---|
10200 | * @return
|
---|
10201 | */
|
---|
10202 | HRESULT Medium::i_taskResetHandler(Medium::ResetTask &task)
|
---|
10203 | {
|
---|
10204 | HRESULT rc = S_OK;
|
---|
10205 |
|
---|
10206 | uint64_t size = 0, logicalSize = 0;
|
---|
10207 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
10208 |
|
---|
10209 | try
|
---|
10210 | {
|
---|
10211 | /* The lock is also used as a signal from the task initiator (which
|
---|
10212 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
10213 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10214 |
|
---|
10215 | /// @todo Below we use a pair of delete/create operations to reset
|
---|
10216 | /// the diff contents but the most efficient way will of course be
|
---|
10217 | /// to add a VDResetDiff() API call
|
---|
10218 |
|
---|
10219 | PVDISK hdd;
|
---|
10220 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
10221 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
10222 |
|
---|
10223 | Guid id = m->id;
|
---|
10224 | Utf8Str format(m->strFormat);
|
---|
10225 | Utf8Str location(m->strLocationFull);
|
---|
10226 |
|
---|
10227 | Medium *pParent = m->pParent;
|
---|
10228 | Guid parentId = pParent->m->id;
|
---|
10229 | Utf8Str parentFormat(pParent->m->strFormat);
|
---|
10230 | Utf8Str parentLocation(pParent->m->strLocationFull);
|
---|
10231 |
|
---|
10232 | Assert(m->state == MediumState_LockedWrite);
|
---|
10233 |
|
---|
10234 | /* unlock before the potentially lengthy operation */
|
---|
10235 | thisLock.release();
|
---|
10236 |
|
---|
10237 | try
|
---|
10238 | {
|
---|
10239 | /* Open all media in the target chain but the last. */
|
---|
10240 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
10241 | task.mpMediumLockList->GetBegin();
|
---|
10242 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
10243 | task.mpMediumLockList->GetEnd();
|
---|
10244 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
10245 | it != targetListEnd;
|
---|
10246 | ++it)
|
---|
10247 | {
|
---|
10248 | const MediumLock &mediumLock = *it;
|
---|
10249 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
10250 |
|
---|
10251 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
10252 |
|
---|
10253 | /* sanity check, "this" is checked above */
|
---|
10254 | Assert( pMedium == this
|
---|
10255 | || pMedium->m->state == MediumState_LockedRead);
|
---|
10256 |
|
---|
10257 | /* Open all media in appropriate mode. */
|
---|
10258 | vrc = VDOpen(hdd,
|
---|
10259 | pMedium->m->strFormat.c_str(),
|
---|
10260 | pMedium->m->strLocationFull.c_str(),
|
---|
10261 | VD_OPEN_FLAGS_READONLY | m->uOpenFlagsDef,
|
---|
10262 | pMedium->m->vdImageIfaces);
|
---|
10263 | if (RT_FAILURE(vrc))
|
---|
10264 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10265 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10266 | pMedium->m->strLocationFull.c_str(),
|
---|
10267 | i_vdError(vrc).c_str());
|
---|
10268 |
|
---|
10269 | /* Done when we hit the media which should be reset */
|
---|
10270 | if (pMedium == this)
|
---|
10271 | break;
|
---|
10272 | }
|
---|
10273 |
|
---|
10274 | /* first, delete the storage unit */
|
---|
10275 | vrc = VDClose(hdd, true /* fDelete */);
|
---|
10276 | if (RT_FAILURE(vrc))
|
---|
10277 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10278 | tr("Could not delete the medium storage unit '%s'%s"),
|
---|
10279 | location.c_str(), i_vdError(vrc).c_str());
|
---|
10280 |
|
---|
10281 | /* next, create it again */
|
---|
10282 | vrc = VDOpen(hdd,
|
---|
10283 | parentFormat.c_str(),
|
---|
10284 | parentLocation.c_str(),
|
---|
10285 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | m->uOpenFlagsDef,
|
---|
10286 | m->vdImageIfaces);
|
---|
10287 | if (RT_FAILURE(vrc))
|
---|
10288 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10289 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10290 | parentLocation.c_str(), i_vdError(vrc).c_str());
|
---|
10291 |
|
---|
10292 | vrc = VDCreateDiff(hdd,
|
---|
10293 | format.c_str(),
|
---|
10294 | location.c_str(),
|
---|
10295 | /// @todo use the same medium variant as before
|
---|
10296 | VD_IMAGE_FLAGS_NONE,
|
---|
10297 | NULL,
|
---|
10298 | id.raw(),
|
---|
10299 | parentId.raw(),
|
---|
10300 | VD_OPEN_FLAGS_NORMAL,
|
---|
10301 | m->vdImageIfaces,
|
---|
10302 | task.mVDOperationIfaces);
|
---|
10303 | if (RT_FAILURE(vrc))
|
---|
10304 | {
|
---|
10305 | if (vrc == VERR_VD_INVALID_TYPE)
|
---|
10306 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10307 | tr("Parameters for creating the differencing medium storage unit '%s' are invalid%s"),
|
---|
10308 | location.c_str(), i_vdError(vrc).c_str());
|
---|
10309 | else
|
---|
10310 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10311 | tr("Could not create the differencing medium storage unit '%s'%s"),
|
---|
10312 | location.c_str(), i_vdError(vrc).c_str());
|
---|
10313 | }
|
---|
10314 |
|
---|
10315 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
10316 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
10317 | unsigned uImageFlags;
|
---|
10318 | vrc = VDGetImageFlags(hdd, 0, &uImageFlags);
|
---|
10319 | if (RT_SUCCESS(vrc))
|
---|
10320 | variant = (MediumVariant_T)uImageFlags;
|
---|
10321 | }
|
---|
10322 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10323 |
|
---|
10324 | VDDestroy(hdd);
|
---|
10325 | }
|
---|
10326 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10327 |
|
---|
10328 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10329 |
|
---|
10330 | m->size = size;
|
---|
10331 | m->logicalSize = logicalSize;
|
---|
10332 | m->variant = variant;
|
---|
10333 |
|
---|
10334 | if (task.NotifyAboutChanges() && SUCCEEDED(rc))
|
---|
10335 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
10336 |
|
---|
10337 | /* Everything is explicitly unlocked when the task exits,
|
---|
10338 | * as the task destruction also destroys the media chain. */
|
---|
10339 |
|
---|
10340 | return rc;
|
---|
10341 | }
|
---|
10342 |
|
---|
10343 | /**
|
---|
10344 | * Implementation code for the "compact" task.
|
---|
10345 | *
|
---|
10346 | * @param task
|
---|
10347 | * @return
|
---|
10348 | */
|
---|
10349 | HRESULT Medium::i_taskCompactHandler(Medium::CompactTask &task)
|
---|
10350 | {
|
---|
10351 | HRESULT rc = S_OK;
|
---|
10352 |
|
---|
10353 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
10354 | * signal from the task initiator (which releases it only after
|
---|
10355 | * RTThreadCreate()) that we can start the job. */
|
---|
10356 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10357 |
|
---|
10358 | try
|
---|
10359 | {
|
---|
10360 | PVDISK hdd;
|
---|
10361 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
10362 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
10363 |
|
---|
10364 | try
|
---|
10365 | {
|
---|
10366 | /* Open all media in the chain. */
|
---|
10367 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
10368 | task.mpMediumLockList->GetBegin();
|
---|
10369 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
10370 | task.mpMediumLockList->GetEnd();
|
---|
10371 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
10372 | mediumListEnd;
|
---|
10373 | --mediumListLast;
|
---|
10374 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
10375 | it != mediumListEnd;
|
---|
10376 | ++it)
|
---|
10377 | {
|
---|
10378 | const MediumLock &mediumLock = *it;
|
---|
10379 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
10380 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
10381 |
|
---|
10382 | /* sanity check */
|
---|
10383 | if (it == mediumListLast)
|
---|
10384 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
10385 | else
|
---|
10386 | Assert(pMedium->m->state == MediumState_LockedRead);
|
---|
10387 |
|
---|
10388 | /* Open all media but last in read-only mode. Do not handle
|
---|
10389 | * shareable media, as compaction and sharing are mutually
|
---|
10390 | * exclusive. */
|
---|
10391 | vrc = VDOpen(hdd,
|
---|
10392 | pMedium->m->strFormat.c_str(),
|
---|
10393 | pMedium->m->strLocationFull.c_str(),
|
---|
10394 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
10395 | pMedium->m->vdImageIfaces);
|
---|
10396 | if (RT_FAILURE(vrc))
|
---|
10397 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10398 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10399 | pMedium->m->strLocationFull.c_str(),
|
---|
10400 | i_vdError(vrc).c_str());
|
---|
10401 | }
|
---|
10402 |
|
---|
10403 | Assert(m->state == MediumState_LockedWrite);
|
---|
10404 |
|
---|
10405 | Utf8Str location(m->strLocationFull);
|
---|
10406 |
|
---|
10407 | /* unlock before the potentially lengthy operation */
|
---|
10408 | thisLock.release();
|
---|
10409 |
|
---|
10410 | vrc = VDCompact(hdd, VD_LAST_IMAGE, task.mVDOperationIfaces);
|
---|
10411 | if (RT_FAILURE(vrc))
|
---|
10412 | {
|
---|
10413 | if (vrc == VERR_NOT_SUPPORTED)
|
---|
10414 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
10415 | tr("Compacting is not yet supported for medium '%s'"),
|
---|
10416 | location.c_str());
|
---|
10417 | else if (vrc == VERR_NOT_IMPLEMENTED)
|
---|
10418 | throw setErrorBoth(E_NOTIMPL, vrc,
|
---|
10419 | tr("Compacting is not implemented, medium '%s'"),
|
---|
10420 | location.c_str());
|
---|
10421 | else
|
---|
10422 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10423 | tr("Could not compact medium '%s'%s"),
|
---|
10424 | location.c_str(),
|
---|
10425 | i_vdError(vrc).c_str());
|
---|
10426 | }
|
---|
10427 | }
|
---|
10428 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10429 |
|
---|
10430 | VDDestroy(hdd);
|
---|
10431 | }
|
---|
10432 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10433 |
|
---|
10434 | if (task.NotifyAboutChanges() && SUCCEEDED(rc))
|
---|
10435 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
10436 |
|
---|
10437 | /* Everything is explicitly unlocked when the task exits,
|
---|
10438 | * as the task destruction also destroys the media chain. */
|
---|
10439 |
|
---|
10440 | return rc;
|
---|
10441 | }
|
---|
10442 |
|
---|
10443 | /**
|
---|
10444 | * Implementation code for the "resize" task.
|
---|
10445 | *
|
---|
10446 | * @param task
|
---|
10447 | * @return
|
---|
10448 | */
|
---|
10449 | HRESULT Medium::i_taskResizeHandler(Medium::ResizeTask &task)
|
---|
10450 | {
|
---|
10451 | HRESULT rc = S_OK;
|
---|
10452 |
|
---|
10453 | uint64_t size = 0, logicalSize = 0;
|
---|
10454 |
|
---|
10455 | try
|
---|
10456 | {
|
---|
10457 | /* The lock is also used as a signal from the task initiator (which
|
---|
10458 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
10459 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10460 |
|
---|
10461 | PVDISK hdd;
|
---|
10462 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
10463 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
10464 |
|
---|
10465 | try
|
---|
10466 | {
|
---|
10467 | /* Open all media in the chain. */
|
---|
10468 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
10469 | task.mpMediumLockList->GetBegin();
|
---|
10470 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
10471 | task.mpMediumLockList->GetEnd();
|
---|
10472 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
10473 | mediumListEnd;
|
---|
10474 | --mediumListLast;
|
---|
10475 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
10476 | it != mediumListEnd;
|
---|
10477 | ++it)
|
---|
10478 | {
|
---|
10479 | const MediumLock &mediumLock = *it;
|
---|
10480 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
10481 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
10482 |
|
---|
10483 | /* sanity check */
|
---|
10484 | if (it == mediumListLast)
|
---|
10485 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
10486 | else
|
---|
10487 | Assert(pMedium->m->state == MediumState_LockedRead ||
|
---|
10488 | // Allow resize the target image during mergeTo in case
|
---|
10489 | // of direction from parent to child because all intermediate
|
---|
10490 | // images are marked to MediumState_Deleting and will be
|
---|
10491 | // destroyed after successful merge
|
---|
10492 | pMedium->m->state == MediumState_Deleting);
|
---|
10493 |
|
---|
10494 | /* Open all media but last in read-only mode. Do not handle
|
---|
10495 | * shareable media, as compaction and sharing are mutually
|
---|
10496 | * exclusive. */
|
---|
10497 | vrc = VDOpen(hdd,
|
---|
10498 | pMedium->m->strFormat.c_str(),
|
---|
10499 | pMedium->m->strLocationFull.c_str(),
|
---|
10500 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
10501 | pMedium->m->vdImageIfaces);
|
---|
10502 | if (RT_FAILURE(vrc))
|
---|
10503 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10504 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10505 | pMedium->m->strLocationFull.c_str(),
|
---|
10506 | i_vdError(vrc).c_str());
|
---|
10507 | }
|
---|
10508 |
|
---|
10509 | Assert(m->state == MediumState_LockedWrite);
|
---|
10510 |
|
---|
10511 | Utf8Str location(m->strLocationFull);
|
---|
10512 |
|
---|
10513 | /* unlock before the potentially lengthy operation */
|
---|
10514 | thisLock.release();
|
---|
10515 |
|
---|
10516 | VDGEOMETRY geo = {0, 0, 0}; /* auto */
|
---|
10517 | vrc = VDResize(hdd, task.mSize, &geo, &geo, task.mVDOperationIfaces);
|
---|
10518 | if (RT_FAILURE(vrc))
|
---|
10519 | {
|
---|
10520 | if (vrc == VERR_VD_SHRINK_NOT_SUPPORTED)
|
---|
10521 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
10522 | tr("Shrinking is not yet supported for medium '%s'"),
|
---|
10523 | location.c_str());
|
---|
10524 | if (vrc == VERR_NOT_SUPPORTED)
|
---|
10525 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
10526 | tr("Resizing to new size %llu is not yet supported for medium '%s'"),
|
---|
10527 | task.mSize, location.c_str());
|
---|
10528 | else if (vrc == VERR_NOT_IMPLEMENTED)
|
---|
10529 | throw setErrorBoth(E_NOTIMPL, vrc,
|
---|
10530 | tr("Resizing is not implemented, medium '%s'"),
|
---|
10531 | location.c_str());
|
---|
10532 | else
|
---|
10533 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10534 | tr("Could not resize medium '%s'%s"),
|
---|
10535 | location.c_str(),
|
---|
10536 | i_vdError(vrc).c_str());
|
---|
10537 | }
|
---|
10538 | size = VDGetFileSize(hdd, VD_LAST_IMAGE);
|
---|
10539 | logicalSize = VDGetSize(hdd, VD_LAST_IMAGE);
|
---|
10540 | }
|
---|
10541 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10542 |
|
---|
10543 | VDDestroy(hdd);
|
---|
10544 | }
|
---|
10545 | catch (HRESULT aRC) { rc = aRC; }
|
---|
10546 |
|
---|
10547 | if (SUCCEEDED(rc))
|
---|
10548 | {
|
---|
10549 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10550 | m->size = size;
|
---|
10551 | m->logicalSize = logicalSize;
|
---|
10552 |
|
---|
10553 | if (task.NotifyAboutChanges())
|
---|
10554 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
10555 | }
|
---|
10556 |
|
---|
10557 | /* Everything is explicitly unlocked when the task exits,
|
---|
10558 | * as the task destruction also destroys the media chain. */
|
---|
10559 |
|
---|
10560 | return rc;
|
---|
10561 | }
|
---|
10562 |
|
---|
10563 | /**
|
---|
10564 | * Implementation code for the "import" task.
|
---|
10565 | *
|
---|
10566 | * This only gets started from Medium::importFile() and always runs
|
---|
10567 | * asynchronously. It potentially touches the media registry, so we
|
---|
10568 | * always save the VirtualBox.xml file when we're done here.
|
---|
10569 | *
|
---|
10570 | * @param task
|
---|
10571 | * @return
|
---|
10572 | */
|
---|
10573 | HRESULT Medium::i_taskImportHandler(Medium::ImportTask &task)
|
---|
10574 | {
|
---|
10575 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
10576 | * to lock order violations, it probably causes lock order issues related
|
---|
10577 | * to the AutoCaller usage. */
|
---|
10578 | HRESULT rcTmp = S_OK;
|
---|
10579 |
|
---|
10580 | const ComObjPtr<Medium> &pParent = task.mParent;
|
---|
10581 |
|
---|
10582 | bool fCreatingTarget = false;
|
---|
10583 |
|
---|
10584 | uint64_t size = 0, logicalSize = 0;
|
---|
10585 | MediumVariant_T variant = MediumVariant_Standard;
|
---|
10586 | bool fGenerateUuid = false;
|
---|
10587 |
|
---|
10588 | try
|
---|
10589 | {
|
---|
10590 | if (!pParent.isNull())
|
---|
10591 | if (pParent->i_getDepth() >= SETTINGS_MEDIUM_DEPTH_MAX)
|
---|
10592 | {
|
---|
10593 | AutoReadLock plock(pParent COMMA_LOCKVAL_SRC_POS);
|
---|
10594 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10595 | tr("Cannot import image for medium '%s', because it exceeds the medium tree depth limit. Please merge some images which you no longer need"),
|
---|
10596 | pParent->m->strLocationFull.c_str());
|
---|
10597 | }
|
---|
10598 |
|
---|
10599 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
10600 | * signal from the task initiator (which releases it only after
|
---|
10601 | * RTThreadCreate()) that we can start the job. */
|
---|
10602 | AutoMultiWriteLock2 thisLock(this, pParent COMMA_LOCKVAL_SRC_POS);
|
---|
10603 |
|
---|
10604 | fCreatingTarget = m->state == MediumState_Creating;
|
---|
10605 |
|
---|
10606 | /* The object may request a specific UUID (through a special form of
|
---|
10607 | * the moveTo() argument). Otherwise we have to generate it */
|
---|
10608 | Guid targetId = m->id;
|
---|
10609 |
|
---|
10610 | fGenerateUuid = targetId.isZero();
|
---|
10611 | if (fGenerateUuid)
|
---|
10612 | {
|
---|
10613 | targetId.create();
|
---|
10614 | /* VirtualBox::i_registerMedium() will need UUID */
|
---|
10615 | unconst(m->id) = targetId;
|
---|
10616 | }
|
---|
10617 |
|
---|
10618 |
|
---|
10619 | PVDISK hdd;
|
---|
10620 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &hdd);
|
---|
10621 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
10622 |
|
---|
10623 | try
|
---|
10624 | {
|
---|
10625 | /* Open source medium. */
|
---|
10626 | vrc = VDOpen(hdd,
|
---|
10627 | task.mFormat->i_getId().c_str(),
|
---|
10628 | task.mFilename.c_str(),
|
---|
10629 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_SEQUENTIAL | m->uOpenFlagsDef,
|
---|
10630 | task.mVDImageIfaces);
|
---|
10631 | if (RT_FAILURE(vrc))
|
---|
10632 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10633 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10634 | task.mFilename.c_str(),
|
---|
10635 | i_vdError(vrc).c_str());
|
---|
10636 |
|
---|
10637 | Utf8Str targetFormat(m->strFormat);
|
---|
10638 | Utf8Str targetLocation(m->strLocationFull);
|
---|
10639 | uint64_t capabilities = task.mFormat->i_getCapabilities();
|
---|
10640 |
|
---|
10641 | Assert( m->state == MediumState_Creating
|
---|
10642 | || m->state == MediumState_LockedWrite);
|
---|
10643 | Assert( pParent.isNull()
|
---|
10644 | || pParent->m->state == MediumState_LockedRead);
|
---|
10645 |
|
---|
10646 | /* unlock before the potentially lengthy operation */
|
---|
10647 | thisLock.release();
|
---|
10648 |
|
---|
10649 | /* ensure the target directory exists */
|
---|
10650 | if (capabilities & MediumFormatCapabilities_File)
|
---|
10651 | {
|
---|
10652 | HRESULT rc = VirtualBox::i_ensureFilePathExists(targetLocation,
|
---|
10653 | !(task.mVariant & MediumVariant_NoCreateDir) /* fCreate */);
|
---|
10654 | if (FAILED(rc))
|
---|
10655 | throw rc;
|
---|
10656 | }
|
---|
10657 |
|
---|
10658 | PVDISK targetHdd;
|
---|
10659 | vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &targetHdd);
|
---|
10660 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
10661 |
|
---|
10662 | try
|
---|
10663 | {
|
---|
10664 | /* Open all media in the target chain. */
|
---|
10665 | MediumLockList::Base::const_iterator targetListBegin =
|
---|
10666 | task.mpTargetMediumLockList->GetBegin();
|
---|
10667 | MediumLockList::Base::const_iterator targetListEnd =
|
---|
10668 | task.mpTargetMediumLockList->GetEnd();
|
---|
10669 | for (MediumLockList::Base::const_iterator it = targetListBegin;
|
---|
10670 | it != targetListEnd;
|
---|
10671 | ++it)
|
---|
10672 | {
|
---|
10673 | const MediumLock &mediumLock = *it;
|
---|
10674 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
10675 |
|
---|
10676 | /* If the target medium is not created yet there's no
|
---|
10677 | * reason to open it. */
|
---|
10678 | if (pMedium == this && fCreatingTarget)
|
---|
10679 | continue;
|
---|
10680 |
|
---|
10681 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
10682 |
|
---|
10683 | /* sanity check */
|
---|
10684 | Assert( pMedium->m->state == MediumState_LockedRead
|
---|
10685 | || pMedium->m->state == MediumState_LockedWrite);
|
---|
10686 |
|
---|
10687 | unsigned uOpenFlags = VD_OPEN_FLAGS_NORMAL;
|
---|
10688 | if (pMedium->m->state != MediumState_LockedWrite)
|
---|
10689 | uOpenFlags = VD_OPEN_FLAGS_READONLY;
|
---|
10690 | if (pMedium->m->type == MediumType_Shareable)
|
---|
10691 | uOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
|
---|
10692 |
|
---|
10693 | /* Open all media in appropriate mode. */
|
---|
10694 | vrc = VDOpen(targetHdd,
|
---|
10695 | pMedium->m->strFormat.c_str(),
|
---|
10696 | pMedium->m->strLocationFull.c_str(),
|
---|
10697 | uOpenFlags | m->uOpenFlagsDef,
|
---|
10698 | pMedium->m->vdImageIfaces);
|
---|
10699 | if (RT_FAILURE(vrc))
|
---|
10700 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10701 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
10702 | pMedium->m->strLocationFull.c_str(),
|
---|
10703 | i_vdError(vrc).c_str());
|
---|
10704 | }
|
---|
10705 |
|
---|
10706 | vrc = VDCopy(hdd,
|
---|
10707 | VD_LAST_IMAGE,
|
---|
10708 | targetHdd,
|
---|
10709 | targetFormat.c_str(),
|
---|
10710 | (fCreatingTarget) ? targetLocation.c_str() : (char *)NULL,
|
---|
10711 | false /* fMoveByRename */,
|
---|
10712 | 0 /* cbSize */,
|
---|
10713 | task.mVariant & ~(MediumVariant_NoCreateDir | MediumVariant_Formatted),
|
---|
10714 | targetId.raw(),
|
---|
10715 | VD_OPEN_FLAGS_NORMAL,
|
---|
10716 | NULL /* pVDIfsOperation */,
|
---|
10717 | m->vdImageIfaces,
|
---|
10718 | task.mVDOperationIfaces);
|
---|
10719 | if (RT_FAILURE(vrc))
|
---|
10720 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
10721 | tr("Could not create the imported medium '%s'%s"),
|
---|
10722 | targetLocation.c_str(), i_vdError(vrc).c_str());
|
---|
10723 |
|
---|
10724 | size = VDGetFileSize(targetHdd, VD_LAST_IMAGE);
|
---|
10725 | logicalSize = VDGetSize(targetHdd, VD_LAST_IMAGE);
|
---|
10726 | unsigned uImageFlags;
|
---|
10727 | vrc = VDGetImageFlags(targetHdd, 0, &uImageFlags);
|
---|
10728 | if (RT_SUCCESS(vrc))
|
---|
10729 | variant = (MediumVariant_T)uImageFlags;
|
---|
10730 | }
|
---|
10731 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
10732 |
|
---|
10733 | VDDestroy(targetHdd);
|
---|
10734 | }
|
---|
10735 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
10736 |
|
---|
10737 | VDDestroy(hdd);
|
---|
10738 | }
|
---|
10739 | catch (HRESULT aRC) { rcTmp = aRC; }
|
---|
10740 |
|
---|
10741 | ErrorInfoKeeper eik;
|
---|
10742 | MultiResult mrc(rcTmp);
|
---|
10743 |
|
---|
10744 | /* Only do the parent changes for newly created media. */
|
---|
10745 | if (SUCCEEDED(mrc) && fCreatingTarget)
|
---|
10746 | {
|
---|
10747 | /* we set m->pParent & children() */
|
---|
10748 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
10749 |
|
---|
10750 | Assert(m->pParent.isNull());
|
---|
10751 |
|
---|
10752 | if (pParent)
|
---|
10753 | {
|
---|
10754 | /* Associate the imported medium with the parent and deassociate
|
---|
10755 | * from VirtualBox. Depth check above. */
|
---|
10756 | i_setParent(pParent);
|
---|
10757 |
|
---|
10758 | /* register with mVirtualBox as the last step and move to
|
---|
10759 | * Created state only on success (leaving an orphan file is
|
---|
10760 | * better than breaking media registry consistency) */
|
---|
10761 | eik.restore();
|
---|
10762 | ComObjPtr<Medium> pMedium;
|
---|
10763 | mrc = pParent->m->pVirtualBox->i_registerMedium(this, &pMedium,
|
---|
10764 | treeLock);
|
---|
10765 | Assert(this == pMedium);
|
---|
10766 | eik.fetch();
|
---|
10767 |
|
---|
10768 | if (FAILED(mrc))
|
---|
10769 | /* break parent association on failure to register */
|
---|
10770 | this->i_deparent(); // removes target from parent
|
---|
10771 | }
|
---|
10772 | else
|
---|
10773 | {
|
---|
10774 | /* just register */
|
---|
10775 | eik.restore();
|
---|
10776 | ComObjPtr<Medium> pMedium;
|
---|
10777 | mrc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
10778 | Assert(this == pMedium);
|
---|
10779 | eik.fetch();
|
---|
10780 | }
|
---|
10781 | }
|
---|
10782 |
|
---|
10783 | if (fCreatingTarget)
|
---|
10784 | {
|
---|
10785 | AutoWriteLock mLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10786 |
|
---|
10787 | if (SUCCEEDED(mrc))
|
---|
10788 | {
|
---|
10789 | m->state = MediumState_Created;
|
---|
10790 |
|
---|
10791 | m->size = size;
|
---|
10792 | m->logicalSize = logicalSize;
|
---|
10793 | m->variant = variant;
|
---|
10794 | }
|
---|
10795 | else
|
---|
10796 | {
|
---|
10797 | /* back to NotCreated on failure */
|
---|
10798 | m->state = MediumState_NotCreated;
|
---|
10799 |
|
---|
10800 | /* reset UUID to prevent it from being reused next time */
|
---|
10801 | if (fGenerateUuid)
|
---|
10802 | unconst(m->id).clear();
|
---|
10803 | }
|
---|
10804 | }
|
---|
10805 |
|
---|
10806 | // now, at the end of this task (always asynchronous), save the settings
|
---|
10807 | {
|
---|
10808 | // save the settings
|
---|
10809 | i_markRegistriesModified();
|
---|
10810 | /* collect multiple errors */
|
---|
10811 | eik.restore();
|
---|
10812 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
10813 | eik.fetch();
|
---|
10814 | }
|
---|
10815 |
|
---|
10816 | /* Everything is explicitly unlocked when the task exits,
|
---|
10817 | * as the task destruction also destroys the target chain. */
|
---|
10818 |
|
---|
10819 | /* Make sure the target chain is released early, otherwise it can
|
---|
10820 | * lead to deadlocks with concurrent IAppliance activities. */
|
---|
10821 | task.mpTargetMediumLockList->Clear();
|
---|
10822 |
|
---|
10823 | if (task.NotifyAboutChanges() && SUCCEEDED(mrc))
|
---|
10824 | {
|
---|
10825 | if (pParent)
|
---|
10826 | m->pVirtualBox->i_onMediumConfigChanged(pParent);
|
---|
10827 | if (fCreatingTarget)
|
---|
10828 | m->pVirtualBox->i_onMediumConfigChanged(this);
|
---|
10829 | else
|
---|
10830 | m->pVirtualBox->i_onMediumRegistered(m->id, m->devType, TRUE);
|
---|
10831 | }
|
---|
10832 |
|
---|
10833 | return mrc;
|
---|
10834 | }
|
---|
10835 |
|
---|
10836 | /**
|
---|
10837 | * Sets up the encryption settings for a filter.
|
---|
10838 | */
|
---|
10839 | void Medium::i_taskEncryptSettingsSetup(MediumCryptoFilterSettings *pSettings, const char *pszCipher,
|
---|
10840 | const char *pszKeyStore, const char *pszPassword,
|
---|
10841 | bool fCreateKeyStore)
|
---|
10842 | {
|
---|
10843 | pSettings->pszCipher = pszCipher;
|
---|
10844 | pSettings->pszPassword = pszPassword;
|
---|
10845 | pSettings->pszKeyStoreLoad = pszKeyStore;
|
---|
10846 | pSettings->fCreateKeyStore = fCreateKeyStore;
|
---|
10847 | pSettings->pbDek = NULL;
|
---|
10848 | pSettings->cbDek = 0;
|
---|
10849 | pSettings->vdFilterIfaces = NULL;
|
---|
10850 |
|
---|
10851 | pSettings->vdIfCfg.pfnAreKeysValid = i_vdCryptoConfigAreKeysValid;
|
---|
10852 | pSettings->vdIfCfg.pfnQuerySize = i_vdCryptoConfigQuerySize;
|
---|
10853 | pSettings->vdIfCfg.pfnQuery = i_vdCryptoConfigQuery;
|
---|
10854 | pSettings->vdIfCfg.pfnQueryBytes = NULL;
|
---|
10855 |
|
---|
10856 | pSettings->vdIfCrypto.pfnKeyRetain = i_vdCryptoKeyRetain;
|
---|
10857 | pSettings->vdIfCrypto.pfnKeyRelease = i_vdCryptoKeyRelease;
|
---|
10858 | pSettings->vdIfCrypto.pfnKeyStorePasswordRetain = i_vdCryptoKeyStorePasswordRetain;
|
---|
10859 | pSettings->vdIfCrypto.pfnKeyStorePasswordRelease = i_vdCryptoKeyStorePasswordRelease;
|
---|
10860 | pSettings->vdIfCrypto.pfnKeyStoreSave = i_vdCryptoKeyStoreSave;
|
---|
10861 | pSettings->vdIfCrypto.pfnKeyStoreReturnParameters = i_vdCryptoKeyStoreReturnParameters;
|
---|
10862 |
|
---|
10863 | int vrc = VDInterfaceAdd(&pSettings->vdIfCfg.Core,
|
---|
10864 | "Medium::vdInterfaceCfgCrypto",
|
---|
10865 | VDINTERFACETYPE_CONFIG, pSettings,
|
---|
10866 | sizeof(VDINTERFACECONFIG), &pSettings->vdFilterIfaces);
|
---|
10867 | AssertRC(vrc);
|
---|
10868 |
|
---|
10869 | vrc = VDInterfaceAdd(&pSettings->vdIfCrypto.Core,
|
---|
10870 | "Medium::vdInterfaceCrypto",
|
---|
10871 | VDINTERFACETYPE_CRYPTO, pSettings,
|
---|
10872 | sizeof(VDINTERFACECRYPTO), &pSettings->vdFilterIfaces);
|
---|
10873 | AssertRC(vrc);
|
---|
10874 | }
|
---|
10875 |
|
---|
10876 | /**
|
---|
10877 | * Implementation code for the "encrypt" task.
|
---|
10878 | *
|
---|
10879 | * @param task
|
---|
10880 | * @return
|
---|
10881 | */
|
---|
10882 | HRESULT Medium::i_taskEncryptHandler(Medium::EncryptTask &task)
|
---|
10883 | {
|
---|
10884 | # ifndef VBOX_WITH_EXTPACK
|
---|
10885 | RT_NOREF(task);
|
---|
10886 | # endif
|
---|
10887 | HRESULT rc = S_OK;
|
---|
10888 |
|
---|
10889 | /* Lock all in {parent,child} order. The lock is also used as a
|
---|
10890 | * signal from the task initiator (which releases it only after
|
---|
10891 | * RTThreadCreate()) that we can start the job. */
|
---|
10892 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
10893 | AutoWriteLock thisLock(this COMMA_LOCKVAL_SRC_POS);
|
---|
10894 |
|
---|
10895 | try
|
---|
10896 | {
|
---|
10897 | # ifdef VBOX_WITH_EXTPACK
|
---|
10898 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
10899 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
10900 | {
|
---|
10901 | /* Load the plugin */
|
---|
10902 | Utf8Str strPlugin;
|
---|
10903 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
10904 | if (SUCCEEDED(rc))
|
---|
10905 | {
|
---|
10906 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
10907 | if (RT_FAILURE(vrc))
|
---|
10908 | throw setErrorBoth(VBOX_E_NOT_SUPPORTED, vrc,
|
---|
10909 | tr("Encrypting the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
10910 | i_vdError(vrc).c_str());
|
---|
10911 | }
|
---|
10912 | else
|
---|
10913 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
10914 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
10915 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
10916 | }
|
---|
10917 | else
|
---|
10918 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
10919 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
10920 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
10921 |
|
---|
10922 | PVDISK pDisk = NULL;
|
---|
10923 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
10924 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
10925 |
|
---|
10926 | MediumCryptoFilterSettings CryptoSettingsRead;
|
---|
10927 | MediumCryptoFilterSettings CryptoSettingsWrite;
|
---|
10928 |
|
---|
10929 | void *pvBuf = NULL;
|
---|
10930 | const char *pszPasswordNew = NULL;
|
---|
10931 | try
|
---|
10932 | {
|
---|
10933 | /* Set up disk encryption filters. */
|
---|
10934 | if (task.mstrCurrentPassword.isEmpty())
|
---|
10935 | {
|
---|
10936 | /*
|
---|
10937 | * Query whether the medium property indicating that encryption is
|
---|
10938 | * configured is existing.
|
---|
10939 | */
|
---|
10940 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
10941 | if (it != pBase->m->mapProperties.end())
|
---|
10942 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
10943 | tr("The password given for the encrypted image is incorrect"));
|
---|
10944 | }
|
---|
10945 | else
|
---|
10946 | {
|
---|
10947 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
10948 | if (it == pBase->m->mapProperties.end())
|
---|
10949 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10950 | tr("The image is not configured for encryption"));
|
---|
10951 |
|
---|
10952 | i_taskEncryptSettingsSetup(&CryptoSettingsRead, NULL, it->second.c_str(), task.mstrCurrentPassword.c_str(),
|
---|
10953 | false /* fCreateKeyStore */);
|
---|
10954 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ, CryptoSettingsRead.vdFilterIfaces);
|
---|
10955 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
10956 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
10957 | tr("The password to decrypt the image is incorrect"));
|
---|
10958 | else if (RT_FAILURE(vrc))
|
---|
10959 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10960 | tr("Failed to load the decryption filter: %s"),
|
---|
10961 | i_vdError(vrc).c_str());
|
---|
10962 | }
|
---|
10963 |
|
---|
10964 | if (task.mstrCipher.isNotEmpty())
|
---|
10965 | {
|
---|
10966 | if ( task.mstrNewPassword.isEmpty()
|
---|
10967 | && task.mstrNewPasswordId.isEmpty()
|
---|
10968 | && task.mstrCurrentPassword.isNotEmpty())
|
---|
10969 | {
|
---|
10970 | /* An empty password and password ID will default to the current password. */
|
---|
10971 | pszPasswordNew = task.mstrCurrentPassword.c_str();
|
---|
10972 | }
|
---|
10973 | else if (task.mstrNewPassword.isEmpty())
|
---|
10974 | throw setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
10975 | tr("A password must be given for the image encryption"));
|
---|
10976 | else if (task.mstrNewPasswordId.isEmpty())
|
---|
10977 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10978 | tr("A valid identifier for the password must be given"));
|
---|
10979 | else
|
---|
10980 | pszPasswordNew = task.mstrNewPassword.c_str();
|
---|
10981 |
|
---|
10982 | i_taskEncryptSettingsSetup(&CryptoSettingsWrite, task.mstrCipher.c_str(), NULL,
|
---|
10983 | pszPasswordNew, true /* fCreateKeyStore */);
|
---|
10984 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_WRITE, CryptoSettingsWrite.vdFilterIfaces);
|
---|
10985 | if (RT_FAILURE(vrc))
|
---|
10986 | throw setErrorBoth(VBOX_E_INVALID_OBJECT_STATE, vrc,
|
---|
10987 | tr("Failed to load the encryption filter: %s"),
|
---|
10988 | i_vdError(vrc).c_str());
|
---|
10989 | }
|
---|
10990 | else if (task.mstrNewPasswordId.isNotEmpty() || task.mstrNewPassword.isNotEmpty())
|
---|
10991 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
10992 | tr("The password and password identifier must be empty if the output should be unencrypted"));
|
---|
10993 |
|
---|
10994 | /* Open all media in the chain. */
|
---|
10995 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
10996 | task.mpMediumLockList->GetBegin();
|
---|
10997 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
10998 | task.mpMediumLockList->GetEnd();
|
---|
10999 | MediumLockList::Base::const_iterator mediumListLast =
|
---|
11000 | mediumListEnd;
|
---|
11001 | --mediumListLast;
|
---|
11002 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
11003 | it != mediumListEnd;
|
---|
11004 | ++it)
|
---|
11005 | {
|
---|
11006 | const MediumLock &mediumLock = *it;
|
---|
11007 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
11008 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
11009 |
|
---|
11010 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
11011 |
|
---|
11012 | /* Open all media but last in read-only mode. Do not handle
|
---|
11013 | * shareable media, as compaction and sharing are mutually
|
---|
11014 | * exclusive. */
|
---|
11015 | vrc = VDOpen(pDisk,
|
---|
11016 | pMedium->m->strFormat.c_str(),
|
---|
11017 | pMedium->m->strLocationFull.c_str(),
|
---|
11018 | m->uOpenFlagsDef | (it == mediumListLast ? VD_OPEN_FLAGS_NORMAL : VD_OPEN_FLAGS_READONLY),
|
---|
11019 | pMedium->m->vdImageIfaces);
|
---|
11020 | if (RT_FAILURE(vrc))
|
---|
11021 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
11022 | tr("Could not open the medium storage unit '%s'%s"),
|
---|
11023 | pMedium->m->strLocationFull.c_str(),
|
---|
11024 | i_vdError(vrc).c_str());
|
---|
11025 | }
|
---|
11026 |
|
---|
11027 | Assert(m->state == MediumState_LockedWrite);
|
---|
11028 |
|
---|
11029 | Utf8Str location(m->strLocationFull);
|
---|
11030 |
|
---|
11031 | /* unlock before the potentially lengthy operation */
|
---|
11032 | thisLock.release();
|
---|
11033 |
|
---|
11034 | vrc = VDPrepareWithFilters(pDisk, task.mVDOperationIfaces);
|
---|
11035 | if (RT_FAILURE(vrc))
|
---|
11036 | throw setErrorBoth(VBOX_E_FILE_ERROR, vrc,
|
---|
11037 | tr("Could not prepare disk images for encryption (%Rrc): %s"),
|
---|
11038 | vrc, i_vdError(vrc).c_str());
|
---|
11039 |
|
---|
11040 | thisLock.acquire();
|
---|
11041 | /* If everything went well set the new key store. */
|
---|
11042 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
11043 | if (it != pBase->m->mapProperties.end())
|
---|
11044 | pBase->m->mapProperties.erase(it);
|
---|
11045 |
|
---|
11046 | /* Delete KeyId if encryption is removed or the password did change. */
|
---|
11047 | if ( task.mstrNewPasswordId.isNotEmpty()
|
---|
11048 | || task.mstrCipher.isEmpty())
|
---|
11049 | {
|
---|
11050 | it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
11051 | if (it != pBase->m->mapProperties.end())
|
---|
11052 | pBase->m->mapProperties.erase(it);
|
---|
11053 | }
|
---|
11054 |
|
---|
11055 | if (CryptoSettingsWrite.pszKeyStore)
|
---|
11056 | {
|
---|
11057 | pBase->m->mapProperties["CRYPT/KeyStore"] = Utf8Str(CryptoSettingsWrite.pszKeyStore);
|
---|
11058 | if (task.mstrNewPasswordId.isNotEmpty())
|
---|
11059 | pBase->m->mapProperties["CRYPT/KeyId"] = task.mstrNewPasswordId;
|
---|
11060 | }
|
---|
11061 |
|
---|
11062 | if (CryptoSettingsRead.pszCipherReturned)
|
---|
11063 | RTStrFree(CryptoSettingsRead.pszCipherReturned);
|
---|
11064 |
|
---|
11065 | if (CryptoSettingsWrite.pszCipherReturned)
|
---|
11066 | RTStrFree(CryptoSettingsWrite.pszCipherReturned);
|
---|
11067 |
|
---|
11068 | thisLock.release();
|
---|
11069 | pBase->i_markRegistriesModified();
|
---|
11070 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
11071 | }
|
---|
11072 | catch (HRESULT aRC) { rc = aRC; }
|
---|
11073 |
|
---|
11074 | if (pvBuf)
|
---|
11075 | RTMemFree(pvBuf);
|
---|
11076 |
|
---|
11077 | VDDestroy(pDisk);
|
---|
11078 | # else
|
---|
11079 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
11080 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
11081 | # endif
|
---|
11082 | }
|
---|
11083 | catch (HRESULT aRC) { rc = aRC; }
|
---|
11084 |
|
---|
11085 | /* Everything is explicitly unlocked when the task exits,
|
---|
11086 | * as the task destruction also destroys the media chain. */
|
---|
11087 |
|
---|
11088 | return rc;
|
---|
11089 | }
|
---|
11090 |
|
---|
11091 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|