1 | /* $Id: MediumImpl.cpp 67885 2017-07-10 16:45:06Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox COM class implementation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2016 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 | #include "MediumImpl.h"
|
---|
18 | #include "TokenImpl.h"
|
---|
19 | #include "ProgressImpl.h"
|
---|
20 | #include "SystemPropertiesImpl.h"
|
---|
21 | #include "VirtualBoxImpl.h"
|
---|
22 | #include "ExtPackManagerImpl.h"
|
---|
23 |
|
---|
24 | #include "AutoCaller.h"
|
---|
25 | #include "Logging.h"
|
---|
26 | #include "ThreadTask.h"
|
---|
27 | #include "VBox/com/MultiResult.h"
|
---|
28 | #include "VBox/com/ErrorInfo.h"
|
---|
29 |
|
---|
30 | #include <VBox/err.h>
|
---|
31 | #include <VBox/settings.h>
|
---|
32 |
|
---|
33 | #include <iprt/param.h>
|
---|
34 | #include <iprt/path.h>
|
---|
35 | #include <iprt/file.h>
|
---|
36 | #include <iprt/tcp.h>
|
---|
37 | #include <iprt/cpp/utils.h>
|
---|
38 | #include <iprt/memsafer.h>
|
---|
39 | #include <iprt/base64.h>
|
---|
40 |
|
---|
41 | #include <VBox/vd.h>
|
---|
42 |
|
---|
43 | #include <algorithm>
|
---|
44 | #include <list>
|
---|
45 |
|
---|
46 |
|
---|
47 | typedef std::list<Guid> GuidList;
|
---|
48 |
|
---|
49 |
|
---|
50 | #ifdef VBOX_WITH_EXTPACK
|
---|
51 | static const char g_szVDPlugin[] = "VDPluginCrypt";
|
---|
52 | #endif
|
---|
53 |
|
---|
54 |
|
---|
55 | ////////////////////////////////////////////////////////////////////////////////
|
---|
56 | //
|
---|
57 | // Medium data definition
|
---|
58 | //
|
---|
59 | ////////////////////////////////////////////////////////////////////////////////
|
---|
60 |
|
---|
61 | /** Describes how a machine refers to this medium. */
|
---|
62 | struct BackRef
|
---|
63 | {
|
---|
64 | /** Equality predicate for stdc++. */
|
---|
65 | struct EqualsTo : public std::unary_function <BackRef, bool>
|
---|
66 | {
|
---|
67 | explicit EqualsTo(const Guid &aMachineId) : machineId(aMachineId) {}
|
---|
68 |
|
---|
69 | bool operator()(const argument_type &aThat) const
|
---|
70 | {
|
---|
71 | return aThat.machineId == machineId;
|
---|
72 | }
|
---|
73 |
|
---|
74 | const Guid machineId;
|
---|
75 | };
|
---|
76 |
|
---|
77 | BackRef(const Guid &aMachineId,
|
---|
78 | const Guid &aSnapshotId = Guid::Empty)
|
---|
79 | : machineId(aMachineId),
|
---|
80 | fInCurState(aSnapshotId.isZero())
|
---|
81 | {
|
---|
82 | if (aSnapshotId.isValid() && !aSnapshotId.isZero())
|
---|
83 | llSnapshotIds.push_back(aSnapshotId);
|
---|
84 | }
|
---|
85 |
|
---|
86 | Guid machineId;
|
---|
87 | bool fInCurState : 1;
|
---|
88 | GuidList llSnapshotIds;
|
---|
89 | };
|
---|
90 |
|
---|
91 | typedef std::list<BackRef> BackRefList;
|
---|
92 |
|
---|
93 | struct Medium::Data
|
---|
94 | {
|
---|
95 | Data()
|
---|
96 | : pVirtualBox(NULL),
|
---|
97 | state(MediumState_NotCreated),
|
---|
98 | variant(MediumVariant_Standard),
|
---|
99 | size(0),
|
---|
100 | readers(0),
|
---|
101 | preLockState(MediumState_NotCreated),
|
---|
102 | queryInfoSem(LOCKCLASS_MEDIUMQUERY),
|
---|
103 | queryInfoRunning(false),
|
---|
104 | type(MediumType_Normal),
|
---|
105 | devType(DeviceType_HardDisk),
|
---|
106 | logicalSize(0),
|
---|
107 | hddOpenMode(OpenReadWrite),
|
---|
108 | autoReset(false),
|
---|
109 | hostDrive(false),
|
---|
110 | implicit(false),
|
---|
111 | fClosing(false),
|
---|
112 | uOpenFlagsDef(VD_OPEN_FLAGS_IGNORE_FLUSH),
|
---|
113 | numCreateDiffTasks(0),
|
---|
114 | vdDiskIfaces(NULL),
|
---|
115 | vdImageIfaces(NULL),
|
---|
116 | fMoveThisMedium(false)
|
---|
117 | { }
|
---|
118 |
|
---|
119 | /** weak VirtualBox parent */
|
---|
120 | VirtualBox * const pVirtualBox;
|
---|
121 |
|
---|
122 | // pParent and llChildren are protected by VirtualBox::i_getMediaTreeLockHandle()
|
---|
123 | ComObjPtr<Medium> pParent;
|
---|
124 | MediaList llChildren; // to add a child, just call push_back; to remove
|
---|
125 | // a child, call child->deparent() which does a lookup
|
---|
126 |
|
---|
127 | GuidList llRegistryIDs; // media registries in which this medium is listed
|
---|
128 |
|
---|
129 | const Guid id;
|
---|
130 | Utf8Str strDescription;
|
---|
131 | MediumState_T state;
|
---|
132 | MediumVariant_T variant;
|
---|
133 | Utf8Str strLocationFull;
|
---|
134 | uint64_t size;
|
---|
135 | Utf8Str strLastAccessError;
|
---|
136 |
|
---|
137 | BackRefList backRefs;
|
---|
138 |
|
---|
139 | size_t readers;
|
---|
140 | MediumState_T preLockState;
|
---|
141 |
|
---|
142 | /** Special synchronization for operations which must wait for
|
---|
143 | * Medium::i_queryInfo in another thread to complete. Using a SemRW is
|
---|
144 | * not quite ideal, but at least it is subject to the lock validator,
|
---|
145 | * unlike the SemEventMulti which we had here for many years. Catching
|
---|
146 | * possible deadlocks is more important than a tiny bit of efficiency. */
|
---|
147 | RWLockHandle queryInfoSem;
|
---|
148 | bool queryInfoRunning : 1;
|
---|
149 |
|
---|
150 | const Utf8Str strFormat;
|
---|
151 | ComObjPtr<MediumFormat> formatObj;
|
---|
152 |
|
---|
153 | MediumType_T type;
|
---|
154 | DeviceType_T devType;
|
---|
155 | uint64_t logicalSize;
|
---|
156 |
|
---|
157 | HDDOpenMode hddOpenMode;
|
---|
158 |
|
---|
159 | bool autoReset : 1;
|
---|
160 |
|
---|
161 | /** New UUID to be set on the next Medium::i_queryInfo call. */
|
---|
162 | const Guid uuidImage;
|
---|
163 | /** New parent UUID to be set on the next Medium::i_queryInfo call. */
|
---|
164 | const Guid uuidParentImage;
|
---|
165 |
|
---|
166 | bool hostDrive : 1;
|
---|
167 |
|
---|
168 | settings::StringsMap mapProperties;
|
---|
169 |
|
---|
170 | bool implicit : 1;
|
---|
171 | /** Flag whether the medium is in the process of being closed. */
|
---|
172 | bool fClosing: 1;
|
---|
173 |
|
---|
174 | /** Default flags passed to VDOpen(). */
|
---|
175 | unsigned uOpenFlagsDef;
|
---|
176 |
|
---|
177 | uint32_t numCreateDiffTasks;
|
---|
178 |
|
---|
179 | Utf8Str vdError; /*< Error remembered by the VD error callback. */
|
---|
180 |
|
---|
181 | VDINTERFACEERROR vdIfError;
|
---|
182 |
|
---|
183 | VDINTERFACECONFIG vdIfConfig;
|
---|
184 |
|
---|
185 | VDINTERFACETCPNET vdIfTcpNet;
|
---|
186 |
|
---|
187 | PVDINTERFACE vdDiskIfaces;
|
---|
188 | PVDINTERFACE vdImageIfaces;
|
---|
189 |
|
---|
190 | /** Flag if the medium is going to move to a new
|
---|
191 | * location. */
|
---|
192 | bool fMoveThisMedium;
|
---|
193 | /** new location path */
|
---|
194 | Utf8Str strNewLocationFull;
|
---|
195 | };
|
---|
196 |
|
---|
197 | typedef struct VDSOCKETINT
|
---|
198 | {
|
---|
199 | /** Socket handle. */
|
---|
200 | RTSOCKET hSocket;
|
---|
201 | } VDSOCKETINT, *PVDSOCKETINT;
|
---|
202 |
|
---|
203 | ////////////////////////////////////////////////////////////////////////////////
|
---|
204 | //
|
---|
205 | // Globals
|
---|
206 | //
|
---|
207 | ////////////////////////////////////////////////////////////////////////////////
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Medium::Task class for asynchronous operations.
|
---|
211 | *
|
---|
212 | * @note Instances of this class must be created using new() because the
|
---|
213 | * task thread function will delete them when the task is complete.
|
---|
214 | *
|
---|
215 | * @note The constructor of this class adds a caller on the managed Medium
|
---|
216 | * object which is automatically released upon destruction.
|
---|
217 | */
|
---|
218 | class Medium::Task : public ThreadTask
|
---|
219 | {
|
---|
220 | public:
|
---|
221 | Task(Medium *aMedium, Progress *aProgress)
|
---|
222 | : ThreadTask("Medium::Task"),
|
---|
223 | mVDOperationIfaces(NULL),
|
---|
224 | mMedium(aMedium),
|
---|
225 | mMediumCaller(aMedium),
|
---|
226 | mProgress(aProgress),
|
---|
227 | mVirtualBoxCaller(NULL)
|
---|
228 | {
|
---|
229 | AssertReturnVoidStmt(aMedium, mRC = E_FAIL);
|
---|
230 | mRC = mMediumCaller.rc();
|
---|
231 | if (FAILED(mRC))
|
---|
232 | return;
|
---|
233 |
|
---|
234 | /* Get strong VirtualBox reference, see below. */
|
---|
235 | VirtualBox *pVirtualBox = aMedium->m->pVirtualBox;
|
---|
236 | mVirtualBox = pVirtualBox;
|
---|
237 | mVirtualBoxCaller.attach(pVirtualBox);
|
---|
238 | mRC = mVirtualBoxCaller.rc();
|
---|
239 | if (FAILED(mRC))
|
---|
240 | return;
|
---|
241 |
|
---|
242 | /* Set up a per-operation progress interface, can be used freely (for
|
---|
243 | * binary operations you can use it either on the source or target). */
|
---|
244 | mVDIfProgress.pfnProgress = vdProgressCall;
|
---|
245 | int vrc = VDInterfaceAdd(&mVDIfProgress.Core,
|
---|
246 | "Medium::Task::vdInterfaceProgress",
|
---|
247 | VDINTERFACETYPE_PROGRESS,
|
---|
248 | mProgress,
|
---|
249 | sizeof(VDINTERFACEPROGRESS),
|
---|
250 | &mVDOperationIfaces);
|
---|
251 | AssertRC(vrc);
|
---|
252 | if (RT_FAILURE(vrc))
|
---|
253 | mRC = E_FAIL;
|
---|
254 | }
|
---|
255 |
|
---|
256 | // Make all destructors virtual. Just in case.
|
---|
257 | virtual ~Task()
|
---|
258 | {
|
---|
259 | /* send the notification of completion.*/
|
---|
260 | if ( isAsync()
|
---|
261 | && !mProgress.isNull())
|
---|
262 | mProgress->i_notifyComplete(mRC);
|
---|
263 | }
|
---|
264 |
|
---|
265 | HRESULT rc() const { return mRC; }
|
---|
266 | bool isOk() const { return SUCCEEDED(rc()); }
|
---|
267 |
|
---|
268 | const ComPtr<Progress>& GetProgressObject() const {return mProgress;}
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * Runs Medium::Task::executeTask() on the current thread
|
---|
272 | * instead of creating a new one.
|
---|
273 | */
|
---|
274 | HRESULT runNow()
|
---|
275 | {
|
---|
276 | LogFlowFuncEnter();
|
---|
277 |
|
---|
278 | mRC = executeTask();
|
---|
279 |
|
---|
280 | LogFlowFunc(("rc=%Rhrc\n", mRC));
|
---|
281 | LogFlowFuncLeave();
|
---|
282 | return mRC;
|
---|
283 | }
|
---|
284 |
|
---|
285 | /**
|
---|
286 | * Implementation code for the "create base" task.
|
---|
287 | * Used as function for execution from a standalone thread.
|
---|
288 | */
|
---|
289 | void handler()
|
---|
290 | {
|
---|
291 | LogFlowFuncEnter();
|
---|
292 | try
|
---|
293 | {
|
---|
294 | mRC = executeTask(); /* (destructor picks up mRC, see above) */
|
---|
295 | LogFlowFunc(("rc=%Rhrc\n", mRC));
|
---|
296 | }
|
---|
297 | catch (...)
|
---|
298 | {
|
---|
299 | LogRel(("Some exception in the function Medium::Task:handler()\n"));
|
---|
300 | }
|
---|
301 |
|
---|
302 | LogFlowFuncLeave();
|
---|
303 | }
|
---|
304 |
|
---|
305 | PVDINTERFACE mVDOperationIfaces;
|
---|
306 |
|
---|
307 | const ComObjPtr<Medium> mMedium;
|
---|
308 | AutoCaller mMediumCaller;
|
---|
309 |
|
---|
310 | protected:
|
---|
311 | HRESULT mRC;
|
---|
312 |
|
---|
313 | private:
|
---|
314 | virtual HRESULT executeTask() = 0;
|
---|
315 |
|
---|
316 | const ComObjPtr<Progress> mProgress;
|
---|
317 |
|
---|
318 | static DECLCALLBACK(int) vdProgressCall(void *pvUser, unsigned uPercent);
|
---|
319 |
|
---|
320 | VDINTERFACEPROGRESS mVDIfProgress;
|
---|
321 |
|
---|
322 | /* Must have a strong VirtualBox reference during a task otherwise the
|
---|
323 | * reference count might drop to 0 while a task is still running. This
|
---|
324 | * would result in weird behavior, including deadlocks due to uninit and
|
---|
325 | * locking order issues. The deadlock often is not detectable because the
|
---|
326 | * uninit uses event semaphores which sabotages deadlock detection. */
|
---|
327 | ComObjPtr<VirtualBox> mVirtualBox;
|
---|
328 | AutoCaller mVirtualBoxCaller;
|
---|
329 | };
|
---|
330 |
|
---|
331 | HRESULT Medium::Task::executeTask()
|
---|
332 | {
|
---|
333 | return E_NOTIMPL;//ReturnComNotImplemented()
|
---|
334 | }
|
---|
335 |
|
---|
336 | class Medium::CreateBaseTask : public Medium::Task
|
---|
337 | {
|
---|
338 | public:
|
---|
339 | CreateBaseTask(Medium *aMedium,
|
---|
340 | Progress *aProgress,
|
---|
341 | uint64_t aSize,
|
---|
342 | MediumVariant_T aVariant)
|
---|
343 | : Medium::Task(aMedium, aProgress),
|
---|
344 | mSize(aSize),
|
---|
345 | mVariant(aVariant)
|
---|
346 | {
|
---|
347 | m_strTaskName = "createBase";
|
---|
348 | }
|
---|
349 |
|
---|
350 | uint64_t mSize;
|
---|
351 | MediumVariant_T mVariant;
|
---|
352 |
|
---|
353 | private:
|
---|
354 | HRESULT executeTask();
|
---|
355 | };
|
---|
356 |
|
---|
357 | class Medium::CreateDiffTask : public Medium::Task
|
---|
358 | {
|
---|
359 | public:
|
---|
360 | CreateDiffTask(Medium *aMedium,
|
---|
361 | Progress *aProgress,
|
---|
362 | Medium *aTarget,
|
---|
363 | MediumVariant_T aVariant,
|
---|
364 | MediumLockList *aMediumLockList,
|
---|
365 | bool fKeepMediumLockList = false)
|
---|
366 | : Medium::Task(aMedium, aProgress),
|
---|
367 | mpMediumLockList(aMediumLockList),
|
---|
368 | mTarget(aTarget),
|
---|
369 | mVariant(aVariant),
|
---|
370 | mTargetCaller(aTarget),
|
---|
371 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
372 | {
|
---|
373 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
374 | mRC = mTargetCaller.rc();
|
---|
375 | if (FAILED(mRC))
|
---|
376 | return;
|
---|
377 | m_strTaskName = "createDiff";
|
---|
378 | }
|
---|
379 |
|
---|
380 | ~CreateDiffTask()
|
---|
381 | {
|
---|
382 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
383 | delete mpMediumLockList;
|
---|
384 | }
|
---|
385 |
|
---|
386 | MediumLockList *mpMediumLockList;
|
---|
387 |
|
---|
388 | const ComObjPtr<Medium> mTarget;
|
---|
389 | MediumVariant_T mVariant;
|
---|
390 |
|
---|
391 | private:
|
---|
392 | HRESULT executeTask();
|
---|
393 | AutoCaller mTargetCaller;
|
---|
394 | bool mfKeepMediumLockList;
|
---|
395 | };
|
---|
396 |
|
---|
397 | class Medium::CloneTask : public Medium::Task
|
---|
398 | {
|
---|
399 | public:
|
---|
400 | CloneTask(Medium *aMedium,
|
---|
401 | Progress *aProgress,
|
---|
402 | Medium *aTarget,
|
---|
403 | MediumVariant_T aVariant,
|
---|
404 | Medium *aParent,
|
---|
405 | uint32_t idxSrcImageSame,
|
---|
406 | uint32_t idxDstImageSame,
|
---|
407 | MediumLockList *aSourceMediumLockList,
|
---|
408 | MediumLockList *aTargetMediumLockList,
|
---|
409 | bool fKeepSourceMediumLockList = false,
|
---|
410 | bool fKeepTargetMediumLockList = false)
|
---|
411 | : Medium::Task(aMedium, aProgress),
|
---|
412 | mTarget(aTarget),
|
---|
413 | mParent(aParent),
|
---|
414 | mpSourceMediumLockList(aSourceMediumLockList),
|
---|
415 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
416 | mVariant(aVariant),
|
---|
417 | midxSrcImageSame(idxSrcImageSame),
|
---|
418 | midxDstImageSame(idxDstImageSame),
|
---|
419 | mTargetCaller(aTarget),
|
---|
420 | mParentCaller(aParent),
|
---|
421 | mfKeepSourceMediumLockList(fKeepSourceMediumLockList),
|
---|
422 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
423 | {
|
---|
424 | AssertReturnVoidStmt(aTarget != NULL, mRC = E_FAIL);
|
---|
425 | mRC = mTargetCaller.rc();
|
---|
426 | if (FAILED(mRC))
|
---|
427 | return;
|
---|
428 | /* aParent may be NULL */
|
---|
429 | mRC = mParentCaller.rc();
|
---|
430 | if (FAILED(mRC))
|
---|
431 | return;
|
---|
432 | AssertReturnVoidStmt(aSourceMediumLockList != NULL, mRC = E_FAIL);
|
---|
433 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
434 | m_strTaskName = "createClone";
|
---|
435 | }
|
---|
436 |
|
---|
437 | ~CloneTask()
|
---|
438 | {
|
---|
439 | if (!mfKeepSourceMediumLockList && mpSourceMediumLockList)
|
---|
440 | delete mpSourceMediumLockList;
|
---|
441 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
442 | delete mpTargetMediumLockList;
|
---|
443 | }
|
---|
444 |
|
---|
445 | const ComObjPtr<Medium> mTarget;
|
---|
446 | const ComObjPtr<Medium> mParent;
|
---|
447 | MediumLockList *mpSourceMediumLockList;
|
---|
448 | MediumLockList *mpTargetMediumLockList;
|
---|
449 | MediumVariant_T mVariant;
|
---|
450 | uint32_t midxSrcImageSame;
|
---|
451 | uint32_t midxDstImageSame;
|
---|
452 |
|
---|
453 | private:
|
---|
454 | HRESULT executeTask();
|
---|
455 | AutoCaller mTargetCaller;
|
---|
456 | AutoCaller mParentCaller;
|
---|
457 | bool mfKeepSourceMediumLockList;
|
---|
458 | bool mfKeepTargetMediumLockList;
|
---|
459 | };
|
---|
460 |
|
---|
461 | class Medium::MoveTask : public Medium::Task
|
---|
462 | {
|
---|
463 | public:
|
---|
464 | MoveTask(Medium *aMedium,
|
---|
465 | Progress *aProgress,
|
---|
466 | MediumVariant_T aVariant,
|
---|
467 | MediumLockList *aMediumLockList,
|
---|
468 | bool fKeepMediumLockList = false)
|
---|
469 | : Medium::Task(aMedium, aProgress),
|
---|
470 | mpMediumLockList(aMediumLockList),
|
---|
471 | mVariant(aVariant),
|
---|
472 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
473 | {
|
---|
474 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
475 | m_strTaskName = "createMove";
|
---|
476 | }
|
---|
477 |
|
---|
478 | ~MoveTask()
|
---|
479 | {
|
---|
480 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
481 | delete mpMediumLockList;
|
---|
482 | }
|
---|
483 |
|
---|
484 | MediumLockList *mpMediumLockList;
|
---|
485 | MediumVariant_T mVariant;
|
---|
486 |
|
---|
487 | private:
|
---|
488 | HRESULT executeTask();
|
---|
489 | bool mfKeepMediumLockList;
|
---|
490 | };
|
---|
491 |
|
---|
492 | class Medium::CompactTask : public Medium::Task
|
---|
493 | {
|
---|
494 | public:
|
---|
495 | CompactTask(Medium *aMedium,
|
---|
496 | Progress *aProgress,
|
---|
497 | MediumLockList *aMediumLockList,
|
---|
498 | bool fKeepMediumLockList = false)
|
---|
499 | : Medium::Task(aMedium, aProgress),
|
---|
500 | mpMediumLockList(aMediumLockList),
|
---|
501 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
502 | {
|
---|
503 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
504 | m_strTaskName = "createCompact";
|
---|
505 | }
|
---|
506 |
|
---|
507 | ~CompactTask()
|
---|
508 | {
|
---|
509 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
510 | delete mpMediumLockList;
|
---|
511 | }
|
---|
512 |
|
---|
513 | MediumLockList *mpMediumLockList;
|
---|
514 |
|
---|
515 | private:
|
---|
516 | HRESULT executeTask();
|
---|
517 | bool mfKeepMediumLockList;
|
---|
518 | };
|
---|
519 |
|
---|
520 | class Medium::ResizeTask : public Medium::Task
|
---|
521 | {
|
---|
522 | public:
|
---|
523 | ResizeTask(Medium *aMedium,
|
---|
524 | uint64_t aSize,
|
---|
525 | Progress *aProgress,
|
---|
526 | MediumLockList *aMediumLockList,
|
---|
527 | bool fKeepMediumLockList = false)
|
---|
528 | : Medium::Task(aMedium, aProgress),
|
---|
529 | mSize(aSize),
|
---|
530 | mpMediumLockList(aMediumLockList),
|
---|
531 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
532 | {
|
---|
533 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
534 | m_strTaskName = "createResize";
|
---|
535 | }
|
---|
536 |
|
---|
537 | ~ResizeTask()
|
---|
538 | {
|
---|
539 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
540 | delete mpMediumLockList;
|
---|
541 | }
|
---|
542 |
|
---|
543 | uint64_t mSize;
|
---|
544 | MediumLockList *mpMediumLockList;
|
---|
545 |
|
---|
546 | private:
|
---|
547 | HRESULT executeTask();
|
---|
548 | bool mfKeepMediumLockList;
|
---|
549 | };
|
---|
550 |
|
---|
551 | class Medium::ResetTask : public Medium::Task
|
---|
552 | {
|
---|
553 | public:
|
---|
554 | ResetTask(Medium *aMedium,
|
---|
555 | Progress *aProgress,
|
---|
556 | MediumLockList *aMediumLockList,
|
---|
557 | bool fKeepMediumLockList = false)
|
---|
558 | : Medium::Task(aMedium, aProgress),
|
---|
559 | mpMediumLockList(aMediumLockList),
|
---|
560 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
561 | {
|
---|
562 | m_strTaskName = "createReset";
|
---|
563 | }
|
---|
564 |
|
---|
565 | ~ResetTask()
|
---|
566 | {
|
---|
567 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
568 | delete mpMediumLockList;
|
---|
569 | }
|
---|
570 |
|
---|
571 | MediumLockList *mpMediumLockList;
|
---|
572 |
|
---|
573 | private:
|
---|
574 | HRESULT executeTask();
|
---|
575 | bool mfKeepMediumLockList;
|
---|
576 | };
|
---|
577 |
|
---|
578 | class Medium::DeleteTask : public Medium::Task
|
---|
579 | {
|
---|
580 | public:
|
---|
581 | DeleteTask(Medium *aMedium,
|
---|
582 | Progress *aProgress,
|
---|
583 | MediumLockList *aMediumLockList,
|
---|
584 | bool fKeepMediumLockList = false)
|
---|
585 | : Medium::Task(aMedium, aProgress),
|
---|
586 | mpMediumLockList(aMediumLockList),
|
---|
587 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
588 | {
|
---|
589 | m_strTaskName = "createDelete";
|
---|
590 | }
|
---|
591 |
|
---|
592 | ~DeleteTask()
|
---|
593 | {
|
---|
594 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
595 | delete mpMediumLockList;
|
---|
596 | }
|
---|
597 |
|
---|
598 | MediumLockList *mpMediumLockList;
|
---|
599 |
|
---|
600 | private:
|
---|
601 | HRESULT executeTask();
|
---|
602 | bool mfKeepMediumLockList;
|
---|
603 | };
|
---|
604 |
|
---|
605 | class Medium::MergeTask : public Medium::Task
|
---|
606 | {
|
---|
607 | public:
|
---|
608 | MergeTask(Medium *aMedium,
|
---|
609 | Medium *aTarget,
|
---|
610 | bool fMergeForward,
|
---|
611 | Medium *aParentForTarget,
|
---|
612 | MediumLockList *aChildrenToReparent,
|
---|
613 | Progress *aProgress,
|
---|
614 | MediumLockList *aMediumLockList,
|
---|
615 | bool fKeepMediumLockList = false)
|
---|
616 | : Medium::Task(aMedium, aProgress),
|
---|
617 | mTarget(aTarget),
|
---|
618 | mfMergeForward(fMergeForward),
|
---|
619 | mParentForTarget(aParentForTarget),
|
---|
620 | mpChildrenToReparent(aChildrenToReparent),
|
---|
621 | mpMediumLockList(aMediumLockList),
|
---|
622 | mTargetCaller(aTarget),
|
---|
623 | mParentForTargetCaller(aParentForTarget),
|
---|
624 | mfKeepMediumLockList(fKeepMediumLockList)
|
---|
625 | {
|
---|
626 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
627 | m_strTaskName = "createMerge";
|
---|
628 | }
|
---|
629 |
|
---|
630 | ~MergeTask()
|
---|
631 | {
|
---|
632 | if (!mfKeepMediumLockList && mpMediumLockList)
|
---|
633 | delete mpMediumLockList;
|
---|
634 | if (mpChildrenToReparent)
|
---|
635 | delete mpChildrenToReparent;
|
---|
636 | }
|
---|
637 |
|
---|
638 | const ComObjPtr<Medium> mTarget;
|
---|
639 | bool mfMergeForward;
|
---|
640 | /* When mpChildrenToReparent is null then mParentForTarget is non-null and
|
---|
641 | * vice versa. In other words: they are used in different cases. */
|
---|
642 | const ComObjPtr<Medium> mParentForTarget;
|
---|
643 | MediumLockList *mpChildrenToReparent;
|
---|
644 | MediumLockList *mpMediumLockList;
|
---|
645 |
|
---|
646 | private:
|
---|
647 | HRESULT executeTask();
|
---|
648 | AutoCaller mTargetCaller;
|
---|
649 | AutoCaller mParentForTargetCaller;
|
---|
650 | bool mfKeepMediumLockList;
|
---|
651 | };
|
---|
652 |
|
---|
653 | class Medium::ImportTask : public Medium::Task
|
---|
654 | {
|
---|
655 | public:
|
---|
656 | ImportTask(Medium *aMedium,
|
---|
657 | Progress *aProgress,
|
---|
658 | const char *aFilename,
|
---|
659 | MediumFormat *aFormat,
|
---|
660 | MediumVariant_T aVariant,
|
---|
661 | RTVFSIOSTREAM aVfsIosSrc,
|
---|
662 | Medium *aParent,
|
---|
663 | MediumLockList *aTargetMediumLockList,
|
---|
664 | bool fKeepTargetMediumLockList = false)
|
---|
665 | : Medium::Task(aMedium, aProgress),
|
---|
666 | mFilename(aFilename),
|
---|
667 | mFormat(aFormat),
|
---|
668 | mVariant(aVariant),
|
---|
669 | mParent(aParent),
|
---|
670 | mpTargetMediumLockList(aTargetMediumLockList),
|
---|
671 | mpVfsIoIf(NULL),
|
---|
672 | mParentCaller(aParent),
|
---|
673 | mfKeepTargetMediumLockList(fKeepTargetMediumLockList)
|
---|
674 | {
|
---|
675 | AssertReturnVoidStmt(aTargetMediumLockList != NULL, mRC = E_FAIL);
|
---|
676 | /* aParent may be NULL */
|
---|
677 | mRC = mParentCaller.rc();
|
---|
678 | if (FAILED(mRC))
|
---|
679 | return;
|
---|
680 |
|
---|
681 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
682 |
|
---|
683 | int vrc = VDIfCreateFromVfsStream(aVfsIosSrc, RTFILE_O_READ, &mpVfsIoIf);
|
---|
684 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
685 |
|
---|
686 | vrc = VDInterfaceAdd(&mpVfsIoIf->Core, "Medium::ImportTaskVfsIos",
|
---|
687 | VDINTERFACETYPE_IO, mpVfsIoIf,
|
---|
688 | sizeof(VDINTERFACEIO), &mVDImageIfaces);
|
---|
689 | AssertRCReturnVoidStmt(vrc, mRC = E_FAIL);
|
---|
690 | m_strTaskName = "createImport";
|
---|
691 | }
|
---|
692 |
|
---|
693 | ~ImportTask()
|
---|
694 | {
|
---|
695 | if (!mfKeepTargetMediumLockList && mpTargetMediumLockList)
|
---|
696 | delete mpTargetMediumLockList;
|
---|
697 | if (mpVfsIoIf)
|
---|
698 | {
|
---|
699 | VDIfDestroyFromVfsStream(mpVfsIoIf);
|
---|
700 | mpVfsIoIf = NULL;
|
---|
701 | }
|
---|
702 | }
|
---|
703 |
|
---|
704 | Utf8Str mFilename;
|
---|
705 | ComObjPtr<MediumFormat> mFormat;
|
---|
706 | MediumVariant_T mVariant;
|
---|
707 | const ComObjPtr<Medium> mParent;
|
---|
708 | MediumLockList *mpTargetMediumLockList;
|
---|
709 | PVDINTERFACE mVDImageIfaces;
|
---|
710 | PVDINTERFACEIO mpVfsIoIf; /**< Pointer to the VFS I/O stream to VD I/O interface wrapper. */
|
---|
711 |
|
---|
712 | private:
|
---|
713 | HRESULT executeTask();
|
---|
714 | AutoCaller mParentCaller;
|
---|
715 | bool mfKeepTargetMediumLockList;
|
---|
716 | };
|
---|
717 |
|
---|
718 | class Medium::EncryptTask : public Medium::Task
|
---|
719 | {
|
---|
720 | public:
|
---|
721 | EncryptTask(Medium *aMedium,
|
---|
722 | const com::Utf8Str &strNewPassword,
|
---|
723 | const com::Utf8Str &strCurrentPassword,
|
---|
724 | const com::Utf8Str &strCipher,
|
---|
725 | const com::Utf8Str &strNewPasswordId,
|
---|
726 | Progress *aProgress,
|
---|
727 | MediumLockList *aMediumLockList)
|
---|
728 | : Medium::Task(aMedium, aProgress),
|
---|
729 | mstrNewPassword(strNewPassword),
|
---|
730 | mstrCurrentPassword(strCurrentPassword),
|
---|
731 | mstrCipher(strCipher),
|
---|
732 | mstrNewPasswordId(strNewPasswordId),
|
---|
733 | mpMediumLockList(aMediumLockList)
|
---|
734 | {
|
---|
735 | AssertReturnVoidStmt(aMediumLockList != NULL, mRC = E_FAIL);
|
---|
736 | /* aParent may be NULL */
|
---|
737 | mRC = mParentCaller.rc();
|
---|
738 | if (FAILED(mRC))
|
---|
739 | return;
|
---|
740 |
|
---|
741 | mVDImageIfaces = aMedium->m->vdImageIfaces;
|
---|
742 | m_strTaskName = "createEncrypt";
|
---|
743 | }
|
---|
744 |
|
---|
745 | ~EncryptTask()
|
---|
746 | {
|
---|
747 | if (mstrNewPassword.length())
|
---|
748 | RTMemWipeThoroughly(mstrNewPassword.mutableRaw(), mstrNewPassword.length(), 10 /* cPasses */);
|
---|
749 | if (mstrCurrentPassword.length())
|
---|
750 | RTMemWipeThoroughly(mstrCurrentPassword.mutableRaw(), mstrCurrentPassword.length(), 10 /* cPasses */);
|
---|
751 |
|
---|
752 | /* Keep any errors which might be set when deleting the lock list. */
|
---|
753 | ErrorInfoKeeper eik;
|
---|
754 | delete mpMediumLockList;
|
---|
755 | }
|
---|
756 |
|
---|
757 | Utf8Str mstrNewPassword;
|
---|
758 | Utf8Str mstrCurrentPassword;
|
---|
759 | Utf8Str mstrCipher;
|
---|
760 | Utf8Str mstrNewPasswordId;
|
---|
761 | MediumLockList *mpMediumLockList;
|
---|
762 | PVDINTERFACE mVDImageIfaces;
|
---|
763 |
|
---|
764 | private:
|
---|
765 | HRESULT executeTask();
|
---|
766 | AutoCaller mParentCaller;
|
---|
767 | };
|
---|
768 |
|
---|
769 | /**
|
---|
770 | * Settings for a crypto filter instance.
|
---|
771 | */
|
---|
772 | struct Medium::CryptoFilterSettings
|
---|
773 | {
|
---|
774 | CryptoFilterSettings()
|
---|
775 | : fCreateKeyStore(false),
|
---|
776 | pszPassword(NULL),
|
---|
777 | pszKeyStore(NULL),
|
---|
778 | pszKeyStoreLoad(NULL),
|
---|
779 | pbDek(NULL),
|
---|
780 | cbDek(0),
|
---|
781 | pszCipher(NULL),
|
---|
782 | pszCipherReturned(NULL)
|
---|
783 | { }
|
---|
784 |
|
---|
785 | bool fCreateKeyStore;
|
---|
786 | const char *pszPassword;
|
---|
787 | char *pszKeyStore;
|
---|
788 | const char *pszKeyStoreLoad;
|
---|
789 |
|
---|
790 | const uint8_t *pbDek;
|
---|
791 | size_t cbDek;
|
---|
792 | const char *pszCipher;
|
---|
793 |
|
---|
794 | /** The cipher returned by the crypto filter. */
|
---|
795 | char *pszCipherReturned;
|
---|
796 |
|
---|
797 | PVDINTERFACE vdFilterIfaces;
|
---|
798 |
|
---|
799 | VDINTERFACECONFIG vdIfCfg;
|
---|
800 | VDINTERFACECRYPTO vdIfCrypto;
|
---|
801 | };
|
---|
802 |
|
---|
803 | /**
|
---|
804 | * PFNVDPROGRESS callback handler for Task operations.
|
---|
805 | *
|
---|
806 | * @param pvUser Pointer to the Progress instance.
|
---|
807 | * @param uPercent Completion percentage (0-100).
|
---|
808 | */
|
---|
809 | /*static*/
|
---|
810 | DECLCALLBACK(int) Medium::Task::vdProgressCall(void *pvUser, unsigned uPercent)
|
---|
811 | {
|
---|
812 | Progress *that = static_cast<Progress *>(pvUser);
|
---|
813 |
|
---|
814 | if (that != NULL)
|
---|
815 | {
|
---|
816 | /* update the progress object, capping it at 99% as the final percent
|
---|
817 | * is used for additional operations like setting the UUIDs and similar. */
|
---|
818 | HRESULT rc = that->SetCurrentOperationProgress(uPercent * 99 / 100);
|
---|
819 | if (FAILED(rc))
|
---|
820 | {
|
---|
821 | if (rc == E_FAIL)
|
---|
822 | return VERR_CANCELLED;
|
---|
823 | else
|
---|
824 | return VERR_INVALID_STATE;
|
---|
825 | }
|
---|
826 | }
|
---|
827 |
|
---|
828 | return VINF_SUCCESS;
|
---|
829 | }
|
---|
830 |
|
---|
831 | /**
|
---|
832 | * Implementation code for the "create base" task.
|
---|
833 | */
|
---|
834 | HRESULT Medium::CreateBaseTask::executeTask()
|
---|
835 | {
|
---|
836 | return mMedium->i_taskCreateBaseHandler(*this);
|
---|
837 | }
|
---|
838 |
|
---|
839 | /**
|
---|
840 | * Implementation code for the "create diff" task.
|
---|
841 | */
|
---|
842 | HRESULT Medium::CreateDiffTask::executeTask()
|
---|
843 | {
|
---|
844 | return mMedium->i_taskCreateDiffHandler(*this);
|
---|
845 | }
|
---|
846 |
|
---|
847 | /**
|
---|
848 | * Implementation code for the "clone" task.
|
---|
849 | */
|
---|
850 | HRESULT Medium::CloneTask::executeTask()
|
---|
851 | {
|
---|
852 | return mMedium->i_taskCloneHandler(*this);
|
---|
853 | }
|
---|
854 |
|
---|
855 | /**
|
---|
856 | * Implementation code for the "move" task.
|
---|
857 | */
|
---|
858 | HRESULT Medium::MoveTask::executeTask()
|
---|
859 | {
|
---|
860 | return mMedium->i_taskMoveHandler(*this);
|
---|
861 | }
|
---|
862 |
|
---|
863 | /**
|
---|
864 | * Implementation code for the "compact" task.
|
---|
865 | */
|
---|
866 | HRESULT Medium::CompactTask::executeTask()
|
---|
867 | {
|
---|
868 | return mMedium->i_taskCompactHandler(*this);
|
---|
869 | }
|
---|
870 |
|
---|
871 | /**
|
---|
872 | * Implementation code for the "resize" task.
|
---|
873 | */
|
---|
874 | HRESULT Medium::ResizeTask::executeTask()
|
---|
875 | {
|
---|
876 | return mMedium->i_taskResizeHandler(*this);
|
---|
877 | }
|
---|
878 |
|
---|
879 |
|
---|
880 | /**
|
---|
881 | * Implementation code for the "reset" task.
|
---|
882 | */
|
---|
883 | HRESULT Medium::ResetTask::executeTask()
|
---|
884 | {
|
---|
885 | return mMedium->i_taskResetHandler(*this);
|
---|
886 | }
|
---|
887 |
|
---|
888 | /**
|
---|
889 | * Implementation code for the "delete" task.
|
---|
890 | */
|
---|
891 | HRESULT Medium::DeleteTask::executeTask()
|
---|
892 | {
|
---|
893 | return mMedium->i_taskDeleteHandler(*this);
|
---|
894 | }
|
---|
895 |
|
---|
896 | /**
|
---|
897 | * Implementation code for the "merge" task.
|
---|
898 | */
|
---|
899 | HRESULT Medium::MergeTask::executeTask()
|
---|
900 | {
|
---|
901 | return mMedium->i_taskMergeHandler(*this);
|
---|
902 | }
|
---|
903 |
|
---|
904 | /**
|
---|
905 | * Implementation code for the "import" task.
|
---|
906 | */
|
---|
907 | HRESULT Medium::ImportTask::executeTask()
|
---|
908 | {
|
---|
909 | return mMedium->i_taskImportHandler(*this);
|
---|
910 | }
|
---|
911 |
|
---|
912 | /**
|
---|
913 | * Implementation code for the "encrypt" task.
|
---|
914 | */
|
---|
915 | HRESULT Medium::EncryptTask::executeTask()
|
---|
916 | {
|
---|
917 | return mMedium->i_taskEncryptHandler(*this);
|
---|
918 | }
|
---|
919 |
|
---|
920 | ////////////////////////////////////////////////////////////////////////////////
|
---|
921 | //
|
---|
922 | // Medium constructor / destructor
|
---|
923 | //
|
---|
924 | ////////////////////////////////////////////////////////////////////////////////
|
---|
925 |
|
---|
926 | DEFINE_EMPTY_CTOR_DTOR(Medium)
|
---|
927 |
|
---|
928 | HRESULT Medium::FinalConstruct()
|
---|
929 | {
|
---|
930 | m = new Data;
|
---|
931 |
|
---|
932 | /* Initialize the callbacks of the VD error interface */
|
---|
933 | m->vdIfError.pfnError = i_vdErrorCall;
|
---|
934 | m->vdIfError.pfnMessage = NULL;
|
---|
935 |
|
---|
936 | /* Initialize the callbacks of the VD config interface */
|
---|
937 | m->vdIfConfig.pfnAreKeysValid = i_vdConfigAreKeysValid;
|
---|
938 | m->vdIfConfig.pfnQuerySize = i_vdConfigQuerySize;
|
---|
939 | m->vdIfConfig.pfnQuery = i_vdConfigQuery;
|
---|
940 | m->vdIfConfig.pfnQueryBytes = NULL;
|
---|
941 |
|
---|
942 | /* Initialize the callbacks of the VD TCP interface (we always use the host
|
---|
943 | * IP stack for now) */
|
---|
944 | m->vdIfTcpNet.pfnSocketCreate = i_vdTcpSocketCreate;
|
---|
945 | m->vdIfTcpNet.pfnSocketDestroy = i_vdTcpSocketDestroy;
|
---|
946 | m->vdIfTcpNet.pfnClientConnect = i_vdTcpClientConnect;
|
---|
947 | m->vdIfTcpNet.pfnClientClose = i_vdTcpClientClose;
|
---|
948 | m->vdIfTcpNet.pfnIsClientConnected = i_vdTcpIsClientConnected;
|
---|
949 | m->vdIfTcpNet.pfnSelectOne = i_vdTcpSelectOne;
|
---|
950 | m->vdIfTcpNet.pfnRead = i_vdTcpRead;
|
---|
951 | m->vdIfTcpNet.pfnWrite = i_vdTcpWrite;
|
---|
952 | m->vdIfTcpNet.pfnSgWrite = i_vdTcpSgWrite;
|
---|
953 | m->vdIfTcpNet.pfnFlush = i_vdTcpFlush;
|
---|
954 | m->vdIfTcpNet.pfnSetSendCoalescing = i_vdTcpSetSendCoalescing;
|
---|
955 | m->vdIfTcpNet.pfnGetLocalAddress = i_vdTcpGetLocalAddress;
|
---|
956 | m->vdIfTcpNet.pfnGetPeerAddress = i_vdTcpGetPeerAddress;
|
---|
957 | m->vdIfTcpNet.pfnSelectOneEx = NULL;
|
---|
958 | m->vdIfTcpNet.pfnPoke = NULL;
|
---|
959 |
|
---|
960 | /* Initialize the per-disk interface chain (could be done more globally,
|
---|
961 | * but it's not wasting much time or space so it's not worth it). */
|
---|
962 | int vrc;
|
---|
963 | vrc = VDInterfaceAdd(&m->vdIfError.Core,
|
---|
964 | "Medium::vdInterfaceError",
|
---|
965 | VDINTERFACETYPE_ERROR, this,
|
---|
966 | sizeof(VDINTERFACEERROR), &m->vdDiskIfaces);
|
---|
967 | AssertRCReturn(vrc, E_FAIL);
|
---|
968 |
|
---|
969 | /* Initialize the per-image interface chain */
|
---|
970 | vrc = VDInterfaceAdd(&m->vdIfConfig.Core,
|
---|
971 | "Medium::vdInterfaceConfig",
|
---|
972 | VDINTERFACETYPE_CONFIG, this,
|
---|
973 | sizeof(VDINTERFACECONFIG), &m->vdImageIfaces);
|
---|
974 | AssertRCReturn(vrc, E_FAIL);
|
---|
975 |
|
---|
976 | vrc = VDInterfaceAdd(&m->vdIfTcpNet.Core,
|
---|
977 | "Medium::vdInterfaceTcpNet",
|
---|
978 | VDINTERFACETYPE_TCPNET, this,
|
---|
979 | sizeof(VDINTERFACETCPNET), &m->vdImageIfaces);
|
---|
980 | AssertRCReturn(vrc, E_FAIL);
|
---|
981 |
|
---|
982 | return BaseFinalConstruct();
|
---|
983 | }
|
---|
984 |
|
---|
985 | void Medium::FinalRelease()
|
---|
986 | {
|
---|
987 | uninit();
|
---|
988 |
|
---|
989 | delete m;
|
---|
990 |
|
---|
991 | BaseFinalRelease();
|
---|
992 | }
|
---|
993 |
|
---|
994 | /**
|
---|
995 | * Initializes an empty hard disk object without creating or opening an associated
|
---|
996 | * storage unit.
|
---|
997 | *
|
---|
998 | * This gets called by VirtualBox::CreateMedium() in which case uuidMachineRegistry
|
---|
999 | * is empty since starting with VirtualBox 4.0, we no longer add opened media to a
|
---|
1000 | * registry automatically (this is deferred until the medium is attached to a machine).
|
---|
1001 | *
|
---|
1002 | * This also gets called when VirtualBox creates diff images; in this case uuidMachineRegistry
|
---|
1003 | * is set to the registry of the parent image to make sure they all end up in the same
|
---|
1004 | * file.
|
---|
1005 | *
|
---|
1006 | * For hard disks that don't have the MediumFormatCapabilities_CreateFixed or
|
---|
1007 | * MediumFormatCapabilities_CreateDynamic capability (and therefore cannot be created or deleted
|
---|
1008 | * with the means of VirtualBox) the associated storage unit is assumed to be
|
---|
1009 | * ready for use so the state of the hard disk object will be set to Created.
|
---|
1010 | *
|
---|
1011 | * @param aVirtualBox VirtualBox object.
|
---|
1012 | * @param aFormat
|
---|
1013 | * @param aLocation Storage unit location.
|
---|
1014 | * @param uuidMachineRegistry The registry to which this medium should be added
|
---|
1015 | * (global registry UUID or machine UUID or empty if none).
|
---|
1016 | * @param aDeviceType Device Type.
|
---|
1017 | */
|
---|
1018 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1019 | const Utf8Str &aFormat,
|
---|
1020 | const Utf8Str &aLocation,
|
---|
1021 | const Guid &uuidMachineRegistry,
|
---|
1022 | const DeviceType_T aDeviceType)
|
---|
1023 | {
|
---|
1024 | AssertReturn(aVirtualBox != NULL, E_FAIL);
|
---|
1025 | AssertReturn(!aFormat.isEmpty(), E_FAIL);
|
---|
1026 |
|
---|
1027 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1028 | AutoInitSpan autoInitSpan(this);
|
---|
1029 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1030 |
|
---|
1031 | HRESULT rc = S_OK;
|
---|
1032 |
|
---|
1033 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1034 |
|
---|
1035 | if (uuidMachineRegistry.isValid() && !uuidMachineRegistry.isZero())
|
---|
1036 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
1037 |
|
---|
1038 | /* no storage yet */
|
---|
1039 | m->state = MediumState_NotCreated;
|
---|
1040 |
|
---|
1041 | /* cannot be a host drive */
|
---|
1042 | m->hostDrive = false;
|
---|
1043 |
|
---|
1044 | m->devType = aDeviceType;
|
---|
1045 |
|
---|
1046 | /* No storage unit is created yet, no need to call Medium::i_queryInfo */
|
---|
1047 |
|
---|
1048 | rc = i_setFormat(aFormat);
|
---|
1049 | if (FAILED(rc)) return rc;
|
---|
1050 |
|
---|
1051 | rc = i_setLocation(aLocation);
|
---|
1052 | if (FAILED(rc)) return rc;
|
---|
1053 |
|
---|
1054 | if (!(m->formatObj->i_getCapabilities() & ( MediumFormatCapabilities_CreateFixed
|
---|
1055 | | MediumFormatCapabilities_CreateDynamic))
|
---|
1056 | )
|
---|
1057 | {
|
---|
1058 | /* Storage for mediums of this format can neither be explicitly
|
---|
1059 | * created by VirtualBox nor deleted, so we place the medium to
|
---|
1060 | * Inaccessible state here and also add it to the registry. The
|
---|
1061 | * state means that one has to use RefreshState() to update the
|
---|
1062 | * medium format specific fields. */
|
---|
1063 | m->state = MediumState_Inaccessible;
|
---|
1064 | // create new UUID
|
---|
1065 | unconst(m->id).create();
|
---|
1066 |
|
---|
1067 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1068 | ComObjPtr<Medium> pMedium;
|
---|
1069 |
|
---|
1070 | /*
|
---|
1071 | * Check whether the UUID is taken already and create a new one
|
---|
1072 | * if required.
|
---|
1073 | * Try this only a limited amount of times in case the PRNG is broken
|
---|
1074 | * in some way to prevent an endless loop.
|
---|
1075 | */
|
---|
1076 | for (unsigned i = 0; i < 5; i++)
|
---|
1077 | {
|
---|
1078 | bool fInUse;
|
---|
1079 |
|
---|
1080 | fInUse = m->pVirtualBox->i_isMediaUuidInUse(m->id, aDeviceType);
|
---|
1081 | if (fInUse)
|
---|
1082 | {
|
---|
1083 | // create new UUID
|
---|
1084 | unconst(m->id).create();
|
---|
1085 | }
|
---|
1086 | else
|
---|
1087 | break;
|
---|
1088 | }
|
---|
1089 |
|
---|
1090 | rc = m->pVirtualBox->i_registerMedium(this, &pMedium, treeLock);
|
---|
1091 | Assert(this == pMedium || FAILED(rc));
|
---|
1092 | }
|
---|
1093 |
|
---|
1094 | /* Confirm a successful initialization when it's the case */
|
---|
1095 | if (SUCCEEDED(rc))
|
---|
1096 | autoInitSpan.setSucceeded();
|
---|
1097 |
|
---|
1098 | return rc;
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | /**
|
---|
1102 | * Initializes the medium object by opening the storage unit at the specified
|
---|
1103 | * location. The enOpenMode parameter defines whether the medium will be opened
|
---|
1104 | * read/write or read-only.
|
---|
1105 | *
|
---|
1106 | * This gets called by VirtualBox::OpenMedium() and also by
|
---|
1107 | * Machine::AttachDevice() and createImplicitDiffs() when new diff
|
---|
1108 | * images are created.
|
---|
1109 | *
|
---|
1110 | * There is no registry for this case since starting with VirtualBox 4.0, we
|
---|
1111 | * no longer add opened media to a registry automatically (this is deferred
|
---|
1112 | * until the medium is attached to a machine).
|
---|
1113 | *
|
---|
1114 | * For hard disks, the UUID, format and the parent of this medium will be
|
---|
1115 | * determined when reading the medium storage unit. For DVD and floppy images,
|
---|
1116 | * which have no UUIDs in their storage units, new UUIDs are created.
|
---|
1117 | * If the detected or set parent is not known to VirtualBox, then this method
|
---|
1118 | * will fail.
|
---|
1119 | *
|
---|
1120 | * @param aVirtualBox VirtualBox object.
|
---|
1121 | * @param aLocation Storage unit location.
|
---|
1122 | * @param enOpenMode Whether to open the medium read/write or read-only.
|
---|
1123 | * @param fForceNewUuid Whether a new UUID should be set to avoid duplicates.
|
---|
1124 | * @param aDeviceType Device type of medium.
|
---|
1125 | */
|
---|
1126 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1127 | const Utf8Str &aLocation,
|
---|
1128 | HDDOpenMode enOpenMode,
|
---|
1129 | bool fForceNewUuid,
|
---|
1130 | DeviceType_T aDeviceType)
|
---|
1131 | {
|
---|
1132 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1133 | AssertReturn(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1134 |
|
---|
1135 | HRESULT rc = S_OK;
|
---|
1136 |
|
---|
1137 | {
|
---|
1138 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1139 | AutoInitSpan autoInitSpan(this);
|
---|
1140 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1141 |
|
---|
1142 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1143 |
|
---|
1144 | /* there must be a storage unit */
|
---|
1145 | m->state = MediumState_Created;
|
---|
1146 |
|
---|
1147 | /* remember device type for correct unregistering later */
|
---|
1148 | m->devType = aDeviceType;
|
---|
1149 |
|
---|
1150 | /* cannot be a host drive */
|
---|
1151 | m->hostDrive = false;
|
---|
1152 |
|
---|
1153 | /* remember the open mode (defaults to ReadWrite) */
|
---|
1154 | m->hddOpenMode = enOpenMode;
|
---|
1155 |
|
---|
1156 | if (aDeviceType == DeviceType_DVD)
|
---|
1157 | m->type = MediumType_Readonly;
|
---|
1158 | else if (aDeviceType == DeviceType_Floppy)
|
---|
1159 | m->type = MediumType_Writethrough;
|
---|
1160 |
|
---|
1161 | rc = i_setLocation(aLocation);
|
---|
1162 | if (FAILED(rc)) return rc;
|
---|
1163 |
|
---|
1164 | /* get all the information about the medium from the storage unit */
|
---|
1165 | if (fForceNewUuid)
|
---|
1166 | unconst(m->uuidImage).create();
|
---|
1167 |
|
---|
1168 | m->state = MediumState_Inaccessible;
|
---|
1169 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
1170 |
|
---|
1171 | /* Confirm a successful initialization before the call to i_queryInfo.
|
---|
1172 | * Otherwise we can end up with a AutoCaller deadlock because the
|
---|
1173 | * medium becomes visible but is not marked as initialized. Causes
|
---|
1174 | * locking trouble (e.g. trying to save media registries) which is
|
---|
1175 | * hard to solve. */
|
---|
1176 | autoInitSpan.setSucceeded();
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | /* we're normal code from now on, no longer init */
|
---|
1180 | AutoCaller autoCaller(this);
|
---|
1181 | if (FAILED(autoCaller.rc()))
|
---|
1182 | return autoCaller.rc();
|
---|
1183 |
|
---|
1184 | /* need to call i_queryInfo immediately to correctly place the medium in
|
---|
1185 | * the respective media tree and update other information such as uuid */
|
---|
1186 | rc = i_queryInfo(fForceNewUuid /* fSetImageId */, false /* fSetParentId */,
|
---|
1187 | autoCaller);
|
---|
1188 | if (SUCCEEDED(rc))
|
---|
1189 | {
|
---|
1190 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1191 |
|
---|
1192 | /* if the storage unit is not accessible, it's not acceptable for the
|
---|
1193 | * newly opened media so convert this into an error */
|
---|
1194 | if (m->state == MediumState_Inaccessible)
|
---|
1195 | {
|
---|
1196 | Assert(!m->strLastAccessError.isEmpty());
|
---|
1197 | rc = setError(E_FAIL, "%s", m->strLastAccessError.c_str());
|
---|
1198 | alock.release();
|
---|
1199 | autoCaller.release();
|
---|
1200 | uninit();
|
---|
1201 | }
|
---|
1202 | else
|
---|
1203 | {
|
---|
1204 | AssertStmt(!m->id.isZero(),
|
---|
1205 | alock.release(); autoCaller.release(); uninit(); return E_FAIL);
|
---|
1206 |
|
---|
1207 | /* storage format must be detected by Medium::i_queryInfo if the
|
---|
1208 | * medium is accessible */
|
---|
1209 | AssertStmt(!m->strFormat.isEmpty(),
|
---|
1210 | alock.release(); autoCaller.release(); uninit(); return E_FAIL);
|
---|
1211 | }
|
---|
1212 | }
|
---|
1213 | else
|
---|
1214 | {
|
---|
1215 | /* opening this image failed, mark the object as dead */
|
---|
1216 | autoCaller.release();
|
---|
1217 | uninit();
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 | return rc;
|
---|
1221 | }
|
---|
1222 |
|
---|
1223 | /**
|
---|
1224 | * Initializes the medium object by loading its data from the given settings
|
---|
1225 | * node. The medium will always be opened read/write.
|
---|
1226 | *
|
---|
1227 | * In this case, since we're loading from a registry, uuidMachineRegistry is
|
---|
1228 | * always set: it's either the global registry UUID or a machine UUID when
|
---|
1229 | * loading from a per-machine registry.
|
---|
1230 | *
|
---|
1231 | * @param aParent Parent medium disk or NULL for a root (base) medium.
|
---|
1232 | * @param aDeviceType Device type of the medium.
|
---|
1233 | * @param uuidMachineRegistry The registry to which this medium should be
|
---|
1234 | * added (global registry UUID or machine UUID).
|
---|
1235 | * @param data Configuration settings.
|
---|
1236 | * @param strMachineFolder The machine folder with which to resolve relative paths;
|
---|
1237 | * if empty, then we use the VirtualBox home directory
|
---|
1238 | *
|
---|
1239 | * @note Locks the medium tree for writing.
|
---|
1240 | */
|
---|
1241 | HRESULT Medium::initOne(Medium *aParent,
|
---|
1242 | DeviceType_T aDeviceType,
|
---|
1243 | const Guid &uuidMachineRegistry,
|
---|
1244 | const settings::Medium &data,
|
---|
1245 | const Utf8Str &strMachineFolder)
|
---|
1246 | {
|
---|
1247 | HRESULT rc;
|
---|
1248 |
|
---|
1249 | if (uuidMachineRegistry.isValid() && !uuidMachineRegistry.isZero())
|
---|
1250 | m->llRegistryIDs.push_back(uuidMachineRegistry);
|
---|
1251 |
|
---|
1252 | /* register with VirtualBox/parent early, since uninit() will
|
---|
1253 | * unconditionally unregister on failure */
|
---|
1254 | if (aParent)
|
---|
1255 | {
|
---|
1256 | // differencing medium: add to parent
|
---|
1257 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1258 | // no need to check maximum depth as settings reading did it
|
---|
1259 | i_setParent(aParent);
|
---|
1260 | }
|
---|
1261 |
|
---|
1262 | /* see below why we don't call Medium::i_queryInfo (and therefore treat
|
---|
1263 | * the medium as inaccessible for now */
|
---|
1264 | m->state = MediumState_Inaccessible;
|
---|
1265 | m->strLastAccessError = tr("Accessibility check was not yet performed");
|
---|
1266 |
|
---|
1267 | /* required */
|
---|
1268 | unconst(m->id) = data.uuid;
|
---|
1269 |
|
---|
1270 | /* assume not a host drive */
|
---|
1271 | m->hostDrive = false;
|
---|
1272 |
|
---|
1273 | /* optional */
|
---|
1274 | m->strDescription = data.strDescription;
|
---|
1275 |
|
---|
1276 | /* required */
|
---|
1277 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1278 | {
|
---|
1279 | AssertReturn(!data.strFormat.isEmpty(), E_FAIL);
|
---|
1280 | rc = i_setFormat(data.strFormat);
|
---|
1281 | if (FAILED(rc)) return rc;
|
---|
1282 | }
|
---|
1283 | else
|
---|
1284 | {
|
---|
1285 | /// @todo handle host drive settings here as well?
|
---|
1286 | if (!data.strFormat.isEmpty())
|
---|
1287 | rc = i_setFormat(data.strFormat);
|
---|
1288 | else
|
---|
1289 | rc = i_setFormat("RAW");
|
---|
1290 | if (FAILED(rc)) return rc;
|
---|
1291 | }
|
---|
1292 |
|
---|
1293 | /* optional, only for diffs, default is false; we can only auto-reset
|
---|
1294 | * diff media so they must have a parent */
|
---|
1295 | if (aParent != NULL)
|
---|
1296 | m->autoReset = data.fAutoReset;
|
---|
1297 | else
|
---|
1298 | m->autoReset = false;
|
---|
1299 |
|
---|
1300 | /* properties (after setting the format as it populates the map). Note that
|
---|
1301 | * if some properties are not supported but present in the settings file,
|
---|
1302 | * they will still be read and accessible (for possible backward
|
---|
1303 | * compatibility; we can also clean them up from the XML upon next
|
---|
1304 | * XML format version change if we wish) */
|
---|
1305 | for (settings::StringsMap::const_iterator it = data.properties.begin();
|
---|
1306 | it != data.properties.end();
|
---|
1307 | ++it)
|
---|
1308 | {
|
---|
1309 | const Utf8Str &name = it->first;
|
---|
1310 | const Utf8Str &value = it->second;
|
---|
1311 | m->mapProperties[name] = value;
|
---|
1312 | }
|
---|
1313 |
|
---|
1314 | /* try to decrypt an optional iSCSI initiator secret */
|
---|
1315 | settings::StringsMap::const_iterator itCph = data.properties.find("InitiatorSecretEncrypted");
|
---|
1316 | if ( itCph != data.properties.end()
|
---|
1317 | && !itCph->second.isEmpty())
|
---|
1318 | {
|
---|
1319 | Utf8Str strPlaintext;
|
---|
1320 | int vrc = m->pVirtualBox->i_decryptSetting(&strPlaintext, itCph->second);
|
---|
1321 | if (RT_SUCCESS(vrc))
|
---|
1322 | m->mapProperties["InitiatorSecret"] = strPlaintext;
|
---|
1323 | }
|
---|
1324 |
|
---|
1325 | Utf8Str strFull;
|
---|
1326 | if (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
1327 | {
|
---|
1328 | // compose full path of the medium, if it's not fully qualified...
|
---|
1329 | // slightly convoluted logic here. If the caller has given us a
|
---|
1330 | // machine folder, then a relative path will be relative to that:
|
---|
1331 | if ( !strMachineFolder.isEmpty()
|
---|
1332 | && !RTPathStartsWithRoot(data.strLocation.c_str())
|
---|
1333 | )
|
---|
1334 | {
|
---|
1335 | strFull = strMachineFolder;
|
---|
1336 | strFull += RTPATH_SLASH;
|
---|
1337 | strFull += data.strLocation;
|
---|
1338 | }
|
---|
1339 | else
|
---|
1340 | {
|
---|
1341 | // Otherwise use the old VirtualBox "make absolute path" logic:
|
---|
1342 | rc = m->pVirtualBox->i_calculateFullPath(data.strLocation, strFull);
|
---|
1343 | if (FAILED(rc)) return rc;
|
---|
1344 | }
|
---|
1345 | }
|
---|
1346 | else
|
---|
1347 | strFull = data.strLocation;
|
---|
1348 |
|
---|
1349 | rc = i_setLocation(strFull);
|
---|
1350 | if (FAILED(rc)) return rc;
|
---|
1351 |
|
---|
1352 | if (aDeviceType == DeviceType_HardDisk)
|
---|
1353 | {
|
---|
1354 | /* type is only for base hard disks */
|
---|
1355 | if (m->pParent.isNull())
|
---|
1356 | m->type = data.hdType;
|
---|
1357 | }
|
---|
1358 | else if (aDeviceType == DeviceType_DVD)
|
---|
1359 | m->type = MediumType_Readonly;
|
---|
1360 | else
|
---|
1361 | m->type = MediumType_Writethrough;
|
---|
1362 |
|
---|
1363 | /* remember device type for correct unregistering later */
|
---|
1364 | m->devType = aDeviceType;
|
---|
1365 |
|
---|
1366 | LogFlowThisFunc(("m->strLocationFull='%s', m->strFormat=%s, m->id={%RTuuid}\n",
|
---|
1367 | m->strLocationFull.c_str(), m->strFormat.c_str(), m->id.raw()));
|
---|
1368 |
|
---|
1369 | return S_OK;
|
---|
1370 | }
|
---|
1371 |
|
---|
1372 | /**
|
---|
1373 | * Initializes the medium object and its children by loading its data from the
|
---|
1374 | * given settings node. The medium will always be opened read/write.
|
---|
1375 | *
|
---|
1376 | * In this case, since we're loading from a registry, uuidMachineRegistry is
|
---|
1377 | * always set: it's either the global registry UUID or a machine UUID when
|
---|
1378 | * loading from a per-machine registry.
|
---|
1379 | *
|
---|
1380 | * @param aVirtualBox VirtualBox object.
|
---|
1381 | * @param aParent Parent medium disk or NULL for a root (base) medium.
|
---|
1382 | * @param aDeviceType Device type of the medium.
|
---|
1383 | * @param uuidMachineRegistry The registry to which this medium should be added
|
---|
1384 | * (global registry UUID or machine UUID).
|
---|
1385 | * @param data Configuration settings.
|
---|
1386 | * @param strMachineFolder The machine folder with which to resolve relative
|
---|
1387 | * paths; if empty, then we use the VirtualBox home directory
|
---|
1388 | * @param mediaTreeLock Autolock.
|
---|
1389 | *
|
---|
1390 | * @note Locks the medium tree for writing.
|
---|
1391 | */
|
---|
1392 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1393 | Medium *aParent,
|
---|
1394 | DeviceType_T aDeviceType,
|
---|
1395 | const Guid &uuidMachineRegistry,
|
---|
1396 | const settings::Medium &data,
|
---|
1397 | const Utf8Str &strMachineFolder,
|
---|
1398 | AutoWriteLock &mediaTreeLock)
|
---|
1399 | {
|
---|
1400 | using namespace settings;
|
---|
1401 |
|
---|
1402 | Assert(aVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
1403 | AssertReturn(aVirtualBox, E_INVALIDARG);
|
---|
1404 |
|
---|
1405 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1406 | AutoInitSpan autoInitSpan(this);
|
---|
1407 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1408 |
|
---|
1409 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1410 |
|
---|
1411 | // Do not inline this method call, as the purpose of having this separate
|
---|
1412 | // is to save on stack size. Less local variables are the key for reaching
|
---|
1413 | // deep recursion levels with small stack (XPCOM/g++ without optimization).
|
---|
1414 | HRESULT rc = initOne(aParent, aDeviceType, uuidMachineRegistry, data, strMachineFolder);
|
---|
1415 |
|
---|
1416 |
|
---|
1417 | /* Don't call Medium::i_queryInfo for registered media to prevent the calling
|
---|
1418 | * thread (i.e. the VirtualBox server startup thread) from an unexpected
|
---|
1419 | * freeze but mark it as initially inaccessible instead. The vital UUID,
|
---|
1420 | * location and format properties are read from the registry file above; to
|
---|
1421 | * get the actual state and the rest of the data, the user will have to call
|
---|
1422 | * COMGETTER(State). */
|
---|
1423 |
|
---|
1424 | /* load all children */
|
---|
1425 | for (settings::MediaList::const_iterator it = data.llChildren.begin();
|
---|
1426 | it != data.llChildren.end();
|
---|
1427 | ++it)
|
---|
1428 | {
|
---|
1429 | const settings::Medium &med = *it;
|
---|
1430 |
|
---|
1431 | ComObjPtr<Medium> pMedium;
|
---|
1432 | pMedium.createObject();
|
---|
1433 | rc = pMedium->init(aVirtualBox,
|
---|
1434 | this, // parent
|
---|
1435 | aDeviceType,
|
---|
1436 | uuidMachineRegistry,
|
---|
1437 | med, // child data
|
---|
1438 | strMachineFolder,
|
---|
1439 | mediaTreeLock);
|
---|
1440 | if (FAILED(rc)) break;
|
---|
1441 |
|
---|
1442 | rc = m->pVirtualBox->i_registerMedium(pMedium, &pMedium, mediaTreeLock);
|
---|
1443 | if (FAILED(rc)) break;
|
---|
1444 | }
|
---|
1445 |
|
---|
1446 | /* Confirm a successful initialization when it's the case */
|
---|
1447 | if (SUCCEEDED(rc))
|
---|
1448 | autoInitSpan.setSucceeded();
|
---|
1449 |
|
---|
1450 | return rc;
|
---|
1451 | }
|
---|
1452 |
|
---|
1453 | /**
|
---|
1454 | * Initializes the medium object by providing the host drive information.
|
---|
1455 | * Not used for anything but the host floppy/host DVD case.
|
---|
1456 | *
|
---|
1457 | * There is no registry for this case.
|
---|
1458 | *
|
---|
1459 | * @param aVirtualBox VirtualBox object.
|
---|
1460 | * @param aDeviceType Device type of the medium.
|
---|
1461 | * @param aLocation Location of the host drive.
|
---|
1462 | * @param aDescription Comment for this host drive.
|
---|
1463 | *
|
---|
1464 | * @note Locks VirtualBox lock for writing.
|
---|
1465 | */
|
---|
1466 | HRESULT Medium::init(VirtualBox *aVirtualBox,
|
---|
1467 | DeviceType_T aDeviceType,
|
---|
1468 | const Utf8Str &aLocation,
|
---|
1469 | const Utf8Str &aDescription /* = Utf8Str::Empty */)
|
---|
1470 | {
|
---|
1471 | ComAssertRet(aDeviceType == DeviceType_DVD || aDeviceType == DeviceType_Floppy, E_INVALIDARG);
|
---|
1472 | ComAssertRet(!aLocation.isEmpty(), E_INVALIDARG);
|
---|
1473 |
|
---|
1474 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
1475 | AutoInitSpan autoInitSpan(this);
|
---|
1476 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
1477 |
|
---|
1478 | unconst(m->pVirtualBox) = aVirtualBox;
|
---|
1479 |
|
---|
1480 | // We do not store host drives in VirtualBox.xml or anywhere else, so if we want
|
---|
1481 | // host drives to be identifiable by UUID and not give the drive a different UUID
|
---|
1482 | // every time VirtualBox starts, we need to fake a reproducible UUID here:
|
---|
1483 | RTUUID uuid;
|
---|
1484 | RTUuidClear(&uuid);
|
---|
1485 | if (aDeviceType == DeviceType_DVD)
|
---|
1486 | memcpy(&uuid.au8[0], "DVD", 3);
|
---|
1487 | else
|
---|
1488 | memcpy(&uuid.au8[0], "FD", 2);
|
---|
1489 | /* use device name, adjusted to the end of uuid, shortened if necessary */
|
---|
1490 | size_t lenLocation = aLocation.length();
|
---|
1491 | if (lenLocation > 12)
|
---|
1492 | memcpy(&uuid.au8[4], aLocation.c_str() + (lenLocation - 12), 12);
|
---|
1493 | else
|
---|
1494 | memcpy(&uuid.au8[4 + 12 - lenLocation], aLocation.c_str(), lenLocation);
|
---|
1495 | unconst(m->id) = uuid;
|
---|
1496 |
|
---|
1497 | if (aDeviceType == DeviceType_DVD)
|
---|
1498 | m->type = MediumType_Readonly;
|
---|
1499 | else
|
---|
1500 | m->type = MediumType_Writethrough;
|
---|
1501 | m->devType = aDeviceType;
|
---|
1502 | m->state = MediumState_Created;
|
---|
1503 | m->hostDrive = true;
|
---|
1504 | HRESULT rc = i_setFormat("RAW");
|
---|
1505 | if (FAILED(rc)) return rc;
|
---|
1506 | rc = i_setLocation(aLocation);
|
---|
1507 | if (FAILED(rc)) return rc;
|
---|
1508 | m->strDescription = aDescription;
|
---|
1509 |
|
---|
1510 | autoInitSpan.setSucceeded();
|
---|
1511 | return S_OK;
|
---|
1512 | }
|
---|
1513 |
|
---|
1514 | /**
|
---|
1515 | * Uninitializes the instance.
|
---|
1516 | *
|
---|
1517 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
1518 | *
|
---|
1519 | * @note All children of this medium get uninitialized by calling their
|
---|
1520 | * uninit() methods.
|
---|
1521 | */
|
---|
1522 | void Medium::uninit()
|
---|
1523 | {
|
---|
1524 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1525 | * the pVirtualBox reference, and in this case we don't need to continue.
|
---|
1526 | * Normally this would be handled through the AutoUninitSpan magic, however
|
---|
1527 | * this cannot be done at this point as the media tree must be locked
|
---|
1528 | * before reaching the AutoUninitSpan, otherwise deadlocks can happen.
|
---|
1529 | *
|
---|
1530 | * NOTE: The tree lock is higher priority than the medium caller and medium
|
---|
1531 | * object locks, i.e. the medium caller may have to be released and be
|
---|
1532 | * re-acquired in the right place later. See Medium::getParent() for sample
|
---|
1533 | * code how to do this safely. */
|
---|
1534 | VirtualBox *pVirtualBox = m->pVirtualBox;
|
---|
1535 | if (!pVirtualBox)
|
---|
1536 | return;
|
---|
1537 |
|
---|
1538 | /* Caller must not hold the object or media tree lock over uninit(). */
|
---|
1539 | Assert(!isWriteLockOnCurrentThread());
|
---|
1540 | Assert(!pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
1541 |
|
---|
1542 | AutoWriteLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
1543 |
|
---|
1544 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
1545 | AutoUninitSpan autoUninitSpan(this);
|
---|
1546 | if (autoUninitSpan.uninitDone())
|
---|
1547 | return;
|
---|
1548 |
|
---|
1549 | if (!m->formatObj.isNull())
|
---|
1550 | m->formatObj.setNull();
|
---|
1551 |
|
---|
1552 | if (m->state == MediumState_Deleting)
|
---|
1553 | {
|
---|
1554 | /* This medium has been already deleted (directly or as part of a
|
---|
1555 | * merge). Reparenting has already been done. */
|
---|
1556 | Assert(m->pParent.isNull());
|
---|
1557 | }
|
---|
1558 | else
|
---|
1559 | {
|
---|
1560 | MediaList llChildren(m->llChildren);
|
---|
1561 | m->llChildren.clear();
|
---|
1562 | autoUninitSpan.setSucceeded();
|
---|
1563 |
|
---|
1564 | while (!llChildren.empty())
|
---|
1565 | {
|
---|
1566 | ComObjPtr<Medium> pChild = llChildren.front();
|
---|
1567 | llChildren.pop_front();
|
---|
1568 | pChild->m->pParent.setNull();
|
---|
1569 | treeLock.release();
|
---|
1570 | pChild->uninit();
|
---|
1571 | treeLock.acquire();
|
---|
1572 | }
|
---|
1573 |
|
---|
1574 | if (m->pParent)
|
---|
1575 | {
|
---|
1576 | // this is a differencing disk: then remove it from the parent's children list
|
---|
1577 | i_deparent();
|
---|
1578 | }
|
---|
1579 | }
|
---|
1580 |
|
---|
1581 | unconst(m->pVirtualBox) = NULL;
|
---|
1582 | }
|
---|
1583 |
|
---|
1584 | /**
|
---|
1585 | * Internal helper that removes "this" from the list of children of its
|
---|
1586 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1587 | *
|
---|
1588 | * The caller must hold the medium tree lock!
|
---|
1589 | */
|
---|
1590 | void Medium::i_deparent()
|
---|
1591 | {
|
---|
1592 | MediaList &llParent = m->pParent->m->llChildren;
|
---|
1593 | for (MediaList::iterator it = llParent.begin();
|
---|
1594 | it != llParent.end();
|
---|
1595 | ++it)
|
---|
1596 | {
|
---|
1597 | Medium *pParentsChild = *it;
|
---|
1598 | if (this == pParentsChild)
|
---|
1599 | {
|
---|
1600 | llParent.erase(it);
|
---|
1601 | break;
|
---|
1602 | }
|
---|
1603 | }
|
---|
1604 | m->pParent.setNull();
|
---|
1605 | }
|
---|
1606 |
|
---|
1607 | /**
|
---|
1608 | * Internal helper that removes "this" from the list of children of its
|
---|
1609 | * parent. Used in uninit() and other places when reparenting is necessary.
|
---|
1610 | *
|
---|
1611 | * The caller must hold the medium tree lock!
|
---|
1612 | */
|
---|
1613 | void Medium::i_setParent(const ComObjPtr<Medium> &pParent)
|
---|
1614 | {
|
---|
1615 | m->pParent = pParent;
|
---|
1616 | if (pParent)
|
---|
1617 | pParent->m->llChildren.push_back(this);
|
---|
1618 | }
|
---|
1619 |
|
---|
1620 |
|
---|
1621 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1622 | //
|
---|
1623 | // IMedium public methods
|
---|
1624 | //
|
---|
1625 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1626 |
|
---|
1627 | HRESULT Medium::getId(com::Guid &aId)
|
---|
1628 | {
|
---|
1629 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1630 |
|
---|
1631 | aId = m->id;
|
---|
1632 |
|
---|
1633 | return S_OK;
|
---|
1634 | }
|
---|
1635 |
|
---|
1636 | HRESULT Medium::getDescription(com::Utf8Str &aDescription)
|
---|
1637 | {
|
---|
1638 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1639 |
|
---|
1640 | aDescription = m->strDescription;
|
---|
1641 |
|
---|
1642 | return S_OK;
|
---|
1643 | }
|
---|
1644 |
|
---|
1645 | HRESULT Medium::setDescription(const com::Utf8Str &aDescription)
|
---|
1646 | {
|
---|
1647 | /// @todo update m->strDescription and save the global registry (and local
|
---|
1648 | /// registries of portable VMs referring to this medium), this will also
|
---|
1649 | /// require to add the mRegistered flag to data
|
---|
1650 |
|
---|
1651 | HRESULT rc = S_OK;
|
---|
1652 |
|
---|
1653 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
1654 |
|
---|
1655 | try
|
---|
1656 | {
|
---|
1657 | // locking: we need the tree lock first because we access parent pointers
|
---|
1658 | // and we need to write-lock the media involved
|
---|
1659 | uint32_t cHandles = 2;
|
---|
1660 | LockHandle* pHandles[2] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
1661 | this->lockHandle() };
|
---|
1662 |
|
---|
1663 | AutoWriteLock alock(cHandles,
|
---|
1664 | pHandles
|
---|
1665 | COMMA_LOCKVAL_SRC_POS);
|
---|
1666 |
|
---|
1667 | /* Build the lock list. */
|
---|
1668 | alock.release();
|
---|
1669 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
1670 | this /* pToLockWrite */,
|
---|
1671 | true /* fMediumLockWriteAll */,
|
---|
1672 | NULL,
|
---|
1673 | *pMediumLockList);
|
---|
1674 | alock.acquire();
|
---|
1675 |
|
---|
1676 | if (FAILED(rc))
|
---|
1677 | {
|
---|
1678 | throw setError(rc,
|
---|
1679 | tr("Failed to create medium lock list for '%s'"),
|
---|
1680 | i_getLocationFull().c_str());
|
---|
1681 | }
|
---|
1682 |
|
---|
1683 | alock.release();
|
---|
1684 | rc = pMediumLockList->Lock();
|
---|
1685 | alock.acquire();
|
---|
1686 |
|
---|
1687 | if (FAILED(rc))
|
---|
1688 | {
|
---|
1689 | throw setError(rc,
|
---|
1690 | tr("Failed to lock media '%s'"),
|
---|
1691 | i_getLocationFull().c_str());
|
---|
1692 | }
|
---|
1693 |
|
---|
1694 | /* Set a new description */
|
---|
1695 | if (SUCCEEDED(rc))
|
---|
1696 | {
|
---|
1697 | m->strDescription = aDescription;
|
---|
1698 | }
|
---|
1699 |
|
---|
1700 | // save the settings
|
---|
1701 | i_markRegistriesModified();
|
---|
1702 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
1703 | }
|
---|
1704 | catch (HRESULT aRC) { rc = aRC; }
|
---|
1705 |
|
---|
1706 | delete pMediumLockList;
|
---|
1707 |
|
---|
1708 | return rc;
|
---|
1709 | }
|
---|
1710 |
|
---|
1711 | HRESULT Medium::getState(MediumState_T *aState)
|
---|
1712 | {
|
---|
1713 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1714 | *aState = m->state;
|
---|
1715 |
|
---|
1716 | return S_OK;
|
---|
1717 | }
|
---|
1718 |
|
---|
1719 | HRESULT Medium::getVariant(std::vector<MediumVariant_T> &aVariant)
|
---|
1720 | {
|
---|
1721 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1722 |
|
---|
1723 | const size_t cBits = sizeof(MediumVariant_T) * 8;
|
---|
1724 | aVariant.resize(cBits);
|
---|
1725 | for (size_t i = 0; i < cBits; ++i)
|
---|
1726 | aVariant[i] = (MediumVariant_T)(m->variant & RT_BIT(i));
|
---|
1727 |
|
---|
1728 | return S_OK;
|
---|
1729 | }
|
---|
1730 |
|
---|
1731 | HRESULT Medium::getLocation(com::Utf8Str &aLocation)
|
---|
1732 | {
|
---|
1733 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1734 |
|
---|
1735 | aLocation = m->strLocationFull;
|
---|
1736 |
|
---|
1737 | return S_OK;
|
---|
1738 | }
|
---|
1739 |
|
---|
1740 | HRESULT Medium::getName(com::Utf8Str &aName)
|
---|
1741 | {
|
---|
1742 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1743 |
|
---|
1744 | aName = i_getName();
|
---|
1745 |
|
---|
1746 | return S_OK;
|
---|
1747 | }
|
---|
1748 |
|
---|
1749 | HRESULT Medium::getDeviceType(DeviceType_T *aDeviceType)
|
---|
1750 | {
|
---|
1751 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1752 |
|
---|
1753 | *aDeviceType = m->devType;
|
---|
1754 |
|
---|
1755 | return S_OK;
|
---|
1756 | }
|
---|
1757 |
|
---|
1758 | HRESULT Medium::getHostDrive(BOOL *aHostDrive)
|
---|
1759 | {
|
---|
1760 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1761 |
|
---|
1762 | *aHostDrive = m->hostDrive;
|
---|
1763 |
|
---|
1764 | return S_OK;
|
---|
1765 | }
|
---|
1766 |
|
---|
1767 | HRESULT Medium::getSize(LONG64 *aSize)
|
---|
1768 | {
|
---|
1769 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1770 |
|
---|
1771 | *aSize = m->size;
|
---|
1772 |
|
---|
1773 | return S_OK;
|
---|
1774 | }
|
---|
1775 |
|
---|
1776 | HRESULT Medium::getFormat(com::Utf8Str &aFormat)
|
---|
1777 | {
|
---|
1778 | /* no need to lock, m->strFormat is const */
|
---|
1779 |
|
---|
1780 | aFormat = m->strFormat;
|
---|
1781 | return S_OK;
|
---|
1782 | }
|
---|
1783 |
|
---|
1784 | HRESULT Medium::getMediumFormat(ComPtr<IMediumFormat> &aMediumFormat)
|
---|
1785 | {
|
---|
1786 | /* no need to lock, m->formatObj is const */
|
---|
1787 | m->formatObj.queryInterfaceTo(aMediumFormat.asOutParam());
|
---|
1788 |
|
---|
1789 | return S_OK;
|
---|
1790 | }
|
---|
1791 |
|
---|
1792 | HRESULT Medium::getType(AutoCaller &autoCaller, MediumType_T *aType)
|
---|
1793 | {
|
---|
1794 | NOREF(autoCaller);
|
---|
1795 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1796 |
|
---|
1797 | *aType = m->type;
|
---|
1798 |
|
---|
1799 | return S_OK;
|
---|
1800 | }
|
---|
1801 |
|
---|
1802 | HRESULT Medium::setType(AutoCaller &autoCaller, MediumType_T aType)
|
---|
1803 | {
|
---|
1804 | autoCaller.release();
|
---|
1805 |
|
---|
1806 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1807 | * the pVirtualBox reference, see #uninit(). */
|
---|
1808 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1809 |
|
---|
1810 | // we access m->pParent
|
---|
1811 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
1812 |
|
---|
1813 | autoCaller.add();
|
---|
1814 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1815 |
|
---|
1816 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1817 |
|
---|
1818 | switch (m->state)
|
---|
1819 | {
|
---|
1820 | case MediumState_Created:
|
---|
1821 | case MediumState_Inaccessible:
|
---|
1822 | break;
|
---|
1823 | default:
|
---|
1824 | return i_setStateError();
|
---|
1825 | }
|
---|
1826 |
|
---|
1827 | if (m->type == aType)
|
---|
1828 | {
|
---|
1829 | /* Nothing to do */
|
---|
1830 | return S_OK;
|
---|
1831 | }
|
---|
1832 |
|
---|
1833 | DeviceType_T devType = i_getDeviceType();
|
---|
1834 | // DVD media can only be readonly.
|
---|
1835 | if (devType == DeviceType_DVD && aType != MediumType_Readonly)
|
---|
1836 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1837 | tr("Cannot change the type of DVD medium '%s'"),
|
---|
1838 | m->strLocationFull.c_str());
|
---|
1839 | // Floppy media can only be writethrough or readonly.
|
---|
1840 | if ( devType == DeviceType_Floppy
|
---|
1841 | && aType != MediumType_Writethrough
|
---|
1842 | && aType != MediumType_Readonly)
|
---|
1843 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1844 | tr("Cannot change the type of floppy medium '%s'"),
|
---|
1845 | m->strLocationFull.c_str());
|
---|
1846 |
|
---|
1847 | /* cannot change the type of a differencing medium */
|
---|
1848 | if (m->pParent)
|
---|
1849 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1850 | tr("Cannot change the type of medium '%s' because it is a differencing medium"),
|
---|
1851 | m->strLocationFull.c_str());
|
---|
1852 |
|
---|
1853 | /* Cannot change the type of a medium being in use by more than one VM.
|
---|
1854 | * If the change is to Immutable or MultiAttach then it must not be
|
---|
1855 | * directly attached to any VM, otherwise the assumptions about indirect
|
---|
1856 | * attachment elsewhere are violated and the VM becomes inaccessible.
|
---|
1857 | * Attaching an immutable medium triggers the diff creation, and this is
|
---|
1858 | * vital for the correct operation. */
|
---|
1859 | if ( m->backRefs.size() > 1
|
---|
1860 | || ( ( aType == MediumType_Immutable
|
---|
1861 | || aType == MediumType_MultiAttach)
|
---|
1862 | && m->backRefs.size() > 0))
|
---|
1863 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1864 | tr("Cannot change the type of medium '%s' because it is attached to %d virtual machines"),
|
---|
1865 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
1866 |
|
---|
1867 | switch (aType)
|
---|
1868 | {
|
---|
1869 | case MediumType_Normal:
|
---|
1870 | case MediumType_Immutable:
|
---|
1871 | case MediumType_MultiAttach:
|
---|
1872 | {
|
---|
1873 | /* normal can be easily converted to immutable and vice versa even
|
---|
1874 | * if they have children as long as they are not attached to any
|
---|
1875 | * machine themselves */
|
---|
1876 | break;
|
---|
1877 | }
|
---|
1878 | case MediumType_Writethrough:
|
---|
1879 | case MediumType_Shareable:
|
---|
1880 | case MediumType_Readonly:
|
---|
1881 | {
|
---|
1882 | /* cannot change to writethrough, shareable or readonly
|
---|
1883 | * if there are children */
|
---|
1884 | if (i_getChildren().size() != 0)
|
---|
1885 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
1886 | tr("Cannot change type for medium '%s' since it has %d child media"),
|
---|
1887 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
1888 | if (aType == MediumType_Shareable)
|
---|
1889 | {
|
---|
1890 | MediumVariant_T variant = i_getVariant();
|
---|
1891 | if (!(variant & MediumVariant_Fixed))
|
---|
1892 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1893 | tr("Cannot change type for medium '%s' to 'Shareable' since it is a dynamic medium storage unit"),
|
---|
1894 | m->strLocationFull.c_str());
|
---|
1895 | }
|
---|
1896 | else if (aType == MediumType_Readonly && devType == DeviceType_HardDisk)
|
---|
1897 | {
|
---|
1898 | // Readonly hard disks are not allowed, this medium type is reserved for
|
---|
1899 | // DVDs and floppy images at the moment. Later we might allow readonly hard
|
---|
1900 | // disks, but that's extremely unusual and many guest OSes will have trouble.
|
---|
1901 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1902 | tr("Cannot change type for medium '%s' to 'Readonly' since it is a hard disk"),
|
---|
1903 | m->strLocationFull.c_str());
|
---|
1904 | }
|
---|
1905 | break;
|
---|
1906 | }
|
---|
1907 | default:
|
---|
1908 | AssertFailedReturn(E_FAIL);
|
---|
1909 | }
|
---|
1910 |
|
---|
1911 | if (aType == MediumType_MultiAttach)
|
---|
1912 | {
|
---|
1913 | // This type is new with VirtualBox 4.0 and therefore requires settings
|
---|
1914 | // version 1.11 in the settings backend. Unfortunately it is not enough to do
|
---|
1915 | // the usual routine in MachineConfigFile::bumpSettingsVersionIfNeeded() for
|
---|
1916 | // two reasons: The medium type is a property of the media registry tree, which
|
---|
1917 | // can reside in the global config file (for pre-4.0 media); we would therefore
|
---|
1918 | // possibly need to bump the global config version. We don't want to do that though
|
---|
1919 | // because that might make downgrading to pre-4.0 impossible.
|
---|
1920 | // As a result, we can only use these two new types if the medium is NOT in the
|
---|
1921 | // global registry:
|
---|
1922 | const Guid &uuidGlobalRegistry = m->pVirtualBox->i_getGlobalRegistryId();
|
---|
1923 | if (i_isInRegistry(uuidGlobalRegistry))
|
---|
1924 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
1925 | tr("Cannot change type for medium '%s': the media type 'MultiAttach' can only be used "
|
---|
1926 | "on media registered with a machine that was created with VirtualBox 4.0 or later"),
|
---|
1927 | m->strLocationFull.c_str());
|
---|
1928 | }
|
---|
1929 |
|
---|
1930 | m->type = aType;
|
---|
1931 |
|
---|
1932 | // save the settings
|
---|
1933 | mlock.release();
|
---|
1934 | treeLock.release();
|
---|
1935 | i_markRegistriesModified();
|
---|
1936 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
1937 |
|
---|
1938 | return S_OK;
|
---|
1939 | }
|
---|
1940 |
|
---|
1941 | HRESULT Medium::getAllowedTypes(std::vector<MediumType_T> &aAllowedTypes)
|
---|
1942 | {
|
---|
1943 | NOREF(aAllowedTypes);
|
---|
1944 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
1945 |
|
---|
1946 | ReturnComNotImplemented();
|
---|
1947 | }
|
---|
1948 |
|
---|
1949 | HRESULT Medium::getParent(AutoCaller &autoCaller, ComPtr<IMedium> &aParent)
|
---|
1950 | {
|
---|
1951 | autoCaller.release();
|
---|
1952 |
|
---|
1953 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1954 | * the pVirtualBox reference, see #uninit(). */
|
---|
1955 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1956 |
|
---|
1957 | /* we access m->pParent */
|
---|
1958 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
1959 |
|
---|
1960 | autoCaller.add();
|
---|
1961 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1962 |
|
---|
1963 | m->pParent.queryInterfaceTo(aParent.asOutParam());
|
---|
1964 |
|
---|
1965 | return S_OK;
|
---|
1966 | }
|
---|
1967 |
|
---|
1968 | HRESULT Medium::getChildren(AutoCaller &autoCaller, std::vector<ComPtr<IMedium> > &aChildren)
|
---|
1969 | {
|
---|
1970 | autoCaller.release();
|
---|
1971 |
|
---|
1972 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
1973 | * the pVirtualBox reference, see #uninit(). */
|
---|
1974 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
1975 |
|
---|
1976 | /* we access children */
|
---|
1977 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
1978 |
|
---|
1979 | autoCaller.add();
|
---|
1980 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
1981 |
|
---|
1982 | MediaList children(this->i_getChildren());
|
---|
1983 | aChildren.resize(children.size());
|
---|
1984 | size_t i = 0;
|
---|
1985 | for (MediaList::const_iterator it = children.begin(); it != children.end(); ++it, ++i)
|
---|
1986 | (*it).queryInterfaceTo(aChildren[i].asOutParam());
|
---|
1987 | return S_OK;
|
---|
1988 | }
|
---|
1989 |
|
---|
1990 | HRESULT Medium::getBase(AutoCaller &autoCaller, ComPtr<IMedium> &aBase)
|
---|
1991 | {
|
---|
1992 | autoCaller.release();
|
---|
1993 |
|
---|
1994 | /* i_getBase() will do callers/locking */
|
---|
1995 | i_getBase().queryInterfaceTo(aBase.asOutParam());
|
---|
1996 |
|
---|
1997 | return S_OK;
|
---|
1998 | }
|
---|
1999 |
|
---|
2000 | HRESULT Medium::getReadOnly(AutoCaller &autoCaller, BOOL *aReadOnly)
|
---|
2001 | {
|
---|
2002 | autoCaller.release();
|
---|
2003 |
|
---|
2004 | /* isReadOnly() will do locking */
|
---|
2005 | *aReadOnly = i_isReadOnly();
|
---|
2006 |
|
---|
2007 | return S_OK;
|
---|
2008 | }
|
---|
2009 |
|
---|
2010 | HRESULT Medium::getLogicalSize(LONG64 *aLogicalSize)
|
---|
2011 | {
|
---|
2012 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2013 |
|
---|
2014 | *aLogicalSize = m->logicalSize;
|
---|
2015 |
|
---|
2016 | return S_OK;
|
---|
2017 | }
|
---|
2018 |
|
---|
2019 | HRESULT Medium::getAutoReset(BOOL *aAutoReset)
|
---|
2020 | {
|
---|
2021 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2022 |
|
---|
2023 | if (m->pParent.isNull())
|
---|
2024 | *aAutoReset = FALSE;
|
---|
2025 | else
|
---|
2026 | *aAutoReset = m->autoReset;
|
---|
2027 |
|
---|
2028 | return S_OK;
|
---|
2029 | }
|
---|
2030 |
|
---|
2031 | HRESULT Medium::setAutoReset(BOOL aAutoReset)
|
---|
2032 | {
|
---|
2033 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2034 |
|
---|
2035 | if (m->pParent.isNull())
|
---|
2036 | return setError(VBOX_E_NOT_SUPPORTED,
|
---|
2037 | tr("Medium '%s' is not differencing"),
|
---|
2038 | m->strLocationFull.c_str());
|
---|
2039 |
|
---|
2040 | if (m->autoReset != !!aAutoReset)
|
---|
2041 | {
|
---|
2042 | m->autoReset = !!aAutoReset;
|
---|
2043 |
|
---|
2044 | // save the settings
|
---|
2045 | mlock.release();
|
---|
2046 | i_markRegistriesModified();
|
---|
2047 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2048 | }
|
---|
2049 |
|
---|
2050 | return S_OK;
|
---|
2051 | }
|
---|
2052 |
|
---|
2053 | HRESULT Medium::getLastAccessError(com::Utf8Str &aLastAccessError)
|
---|
2054 | {
|
---|
2055 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2056 |
|
---|
2057 | aLastAccessError = m->strLastAccessError;
|
---|
2058 |
|
---|
2059 | return S_OK;
|
---|
2060 | }
|
---|
2061 |
|
---|
2062 | HRESULT Medium::getMachineIds(std::vector<com::Guid> &aMachineIds)
|
---|
2063 | {
|
---|
2064 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2065 |
|
---|
2066 | if (m->backRefs.size() != 0)
|
---|
2067 | {
|
---|
2068 | BackRefList brlist(m->backRefs);
|
---|
2069 | aMachineIds.resize(brlist.size());
|
---|
2070 | size_t i = 0;
|
---|
2071 | for (BackRefList::const_iterator it = brlist.begin(); it != brlist.end(); ++it, ++i)
|
---|
2072 | aMachineIds[i] = it->machineId;
|
---|
2073 | }
|
---|
2074 |
|
---|
2075 | return S_OK;
|
---|
2076 | }
|
---|
2077 |
|
---|
2078 | HRESULT Medium::setIds(AutoCaller &autoCaller,
|
---|
2079 | BOOL aSetImageId,
|
---|
2080 | const com::Guid &aImageId,
|
---|
2081 | BOOL aSetParentId,
|
---|
2082 | const com::Guid &aParentId)
|
---|
2083 | {
|
---|
2084 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2085 |
|
---|
2086 | switch (m->state)
|
---|
2087 | {
|
---|
2088 | case MediumState_Created:
|
---|
2089 | break;
|
---|
2090 | default:
|
---|
2091 | return i_setStateError();
|
---|
2092 | }
|
---|
2093 |
|
---|
2094 | Guid imageId, parentId;
|
---|
2095 | if (aSetImageId)
|
---|
2096 | {
|
---|
2097 | if (aImageId.isZero())
|
---|
2098 | imageId.create();
|
---|
2099 | else
|
---|
2100 | {
|
---|
2101 | imageId = aImageId;
|
---|
2102 | if (!imageId.isValid())
|
---|
2103 | return setError(E_INVALIDARG, tr("Argument %s is invalid"), "aImageId");
|
---|
2104 | }
|
---|
2105 | }
|
---|
2106 | if (aSetParentId)
|
---|
2107 | {
|
---|
2108 | if (aParentId.isZero())
|
---|
2109 | parentId.create();
|
---|
2110 | else
|
---|
2111 | parentId = aParentId;
|
---|
2112 | }
|
---|
2113 |
|
---|
2114 | unconst(m->uuidImage) = imageId;
|
---|
2115 | unconst(m->uuidParentImage) = parentId;
|
---|
2116 |
|
---|
2117 | // must not hold any locks before calling Medium::i_queryInfo
|
---|
2118 | alock.release();
|
---|
2119 |
|
---|
2120 | HRESULT rc = i_queryInfo(!!aSetImageId /* fSetImageId */,
|
---|
2121 | !!aSetParentId /* fSetParentId */,
|
---|
2122 | autoCaller);
|
---|
2123 |
|
---|
2124 | return rc;
|
---|
2125 | }
|
---|
2126 |
|
---|
2127 | HRESULT Medium::refreshState(AutoCaller &autoCaller, MediumState_T *aState)
|
---|
2128 | {
|
---|
2129 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2130 |
|
---|
2131 | HRESULT rc = S_OK;
|
---|
2132 |
|
---|
2133 | switch (m->state)
|
---|
2134 | {
|
---|
2135 | case MediumState_Created:
|
---|
2136 | case MediumState_Inaccessible:
|
---|
2137 | case MediumState_LockedRead:
|
---|
2138 | {
|
---|
2139 | // must not hold any locks before calling Medium::i_queryInfo
|
---|
2140 | alock.release();
|
---|
2141 |
|
---|
2142 | rc = i_queryInfo(false /* fSetImageId */, false /* fSetParentId */,
|
---|
2143 | autoCaller);
|
---|
2144 |
|
---|
2145 | alock.acquire();
|
---|
2146 | break;
|
---|
2147 | }
|
---|
2148 | default:
|
---|
2149 | break;
|
---|
2150 | }
|
---|
2151 |
|
---|
2152 | *aState = m->state;
|
---|
2153 |
|
---|
2154 | return rc;
|
---|
2155 | }
|
---|
2156 |
|
---|
2157 | HRESULT Medium::getSnapshotIds(const com::Guid &aMachineId,
|
---|
2158 | std::vector<com::Guid> &aSnapshotIds)
|
---|
2159 | {
|
---|
2160 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2161 |
|
---|
2162 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
2163 | it != m->backRefs.end(); ++it)
|
---|
2164 | {
|
---|
2165 | if (it->machineId == aMachineId)
|
---|
2166 | {
|
---|
2167 | size_t size = it->llSnapshotIds.size();
|
---|
2168 |
|
---|
2169 | /* if the medium is attached to the machine in the current state, we
|
---|
2170 | * return its ID as the first element of the array */
|
---|
2171 | if (it->fInCurState)
|
---|
2172 | ++size;
|
---|
2173 |
|
---|
2174 | if (size > 0)
|
---|
2175 | {
|
---|
2176 | aSnapshotIds.resize(size);
|
---|
2177 |
|
---|
2178 | size_t j = 0;
|
---|
2179 | if (it->fInCurState)
|
---|
2180 | aSnapshotIds[j++] = it->machineId.toUtf16();
|
---|
2181 |
|
---|
2182 | for(GuidList::const_iterator jt = it->llSnapshotIds.begin(); jt != it->llSnapshotIds.end(); ++jt, ++j)
|
---|
2183 | aSnapshotIds[j] = (*jt);
|
---|
2184 | }
|
---|
2185 |
|
---|
2186 | break;
|
---|
2187 | }
|
---|
2188 | }
|
---|
2189 |
|
---|
2190 | return S_OK;
|
---|
2191 | }
|
---|
2192 |
|
---|
2193 | HRESULT Medium::lockRead(ComPtr<IToken> &aToken)
|
---|
2194 | {
|
---|
2195 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2196 |
|
---|
2197 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2198 | if (m->queryInfoRunning)
|
---|
2199 | {
|
---|
2200 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2201 | * lock and thus we would run into a deadlock here. */
|
---|
2202 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2203 | while (m->queryInfoRunning)
|
---|
2204 | {
|
---|
2205 | alock.release();
|
---|
2206 | /* must not hold the object lock now */
|
---|
2207 | Assert(!isWriteLockOnCurrentThread());
|
---|
2208 | {
|
---|
2209 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2210 | }
|
---|
2211 | alock.acquire();
|
---|
2212 | }
|
---|
2213 | }
|
---|
2214 |
|
---|
2215 | HRESULT rc = S_OK;
|
---|
2216 |
|
---|
2217 | switch (m->state)
|
---|
2218 | {
|
---|
2219 | case MediumState_Created:
|
---|
2220 | case MediumState_Inaccessible:
|
---|
2221 | case MediumState_LockedRead:
|
---|
2222 | {
|
---|
2223 | ++m->readers;
|
---|
2224 |
|
---|
2225 | ComAssertMsgBreak(m->readers != 0, ("Counter overflow"), rc = E_FAIL);
|
---|
2226 |
|
---|
2227 | /* Remember pre-lock state */
|
---|
2228 | if (m->state != MediumState_LockedRead)
|
---|
2229 | m->preLockState = m->state;
|
---|
2230 |
|
---|
2231 | LogFlowThisFunc(("Okay - prev state=%d readers=%d\n", m->state, m->readers));
|
---|
2232 | m->state = MediumState_LockedRead;
|
---|
2233 |
|
---|
2234 | ComObjPtr<MediumLockToken> pToken;
|
---|
2235 | rc = pToken.createObject();
|
---|
2236 | if (SUCCEEDED(rc))
|
---|
2237 | rc = pToken->init(this, false /* fWrite */);
|
---|
2238 | if (FAILED(rc))
|
---|
2239 | {
|
---|
2240 | --m->readers;
|
---|
2241 | if (m->readers == 0)
|
---|
2242 | m->state = m->preLockState;
|
---|
2243 | return rc;
|
---|
2244 | }
|
---|
2245 |
|
---|
2246 | pToken.queryInterfaceTo(aToken.asOutParam());
|
---|
2247 | break;
|
---|
2248 | }
|
---|
2249 | default:
|
---|
2250 | {
|
---|
2251 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2252 | rc = i_setStateError();
|
---|
2253 | break;
|
---|
2254 | }
|
---|
2255 | }
|
---|
2256 |
|
---|
2257 | return rc;
|
---|
2258 | }
|
---|
2259 |
|
---|
2260 | /**
|
---|
2261 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2262 | * in-process calls).
|
---|
2263 | */
|
---|
2264 | HRESULT Medium::i_unlockRead(MediumState_T *aState)
|
---|
2265 | {
|
---|
2266 | AutoCaller autoCaller(this);
|
---|
2267 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2268 |
|
---|
2269 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2270 |
|
---|
2271 | HRESULT rc = S_OK;
|
---|
2272 |
|
---|
2273 | switch (m->state)
|
---|
2274 | {
|
---|
2275 | case MediumState_LockedRead:
|
---|
2276 | {
|
---|
2277 | ComAssertMsgBreak(m->readers != 0, ("Counter underflow"), rc = E_FAIL);
|
---|
2278 | --m->readers;
|
---|
2279 |
|
---|
2280 | /* Reset the state after the last reader */
|
---|
2281 | if (m->readers == 0)
|
---|
2282 | {
|
---|
2283 | m->state = m->preLockState;
|
---|
2284 | /* There are cases where we inject the deleting state into
|
---|
2285 | * a medium locked for reading. Make sure #unmarkForDeletion()
|
---|
2286 | * gets the right state afterwards. */
|
---|
2287 | if (m->preLockState == MediumState_Deleting)
|
---|
2288 | m->preLockState = MediumState_Created;
|
---|
2289 | }
|
---|
2290 |
|
---|
2291 | LogFlowThisFunc(("new state=%d\n", m->state));
|
---|
2292 | break;
|
---|
2293 | }
|
---|
2294 | default:
|
---|
2295 | {
|
---|
2296 | LogFlowThisFunc(("Failing - state=%d\n", m->state));
|
---|
2297 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2298 | tr("Medium '%s' is not locked for reading"),
|
---|
2299 | m->strLocationFull.c_str());
|
---|
2300 | break;
|
---|
2301 | }
|
---|
2302 | }
|
---|
2303 |
|
---|
2304 | /* return the current state after */
|
---|
2305 | if (aState)
|
---|
2306 | *aState = m->state;
|
---|
2307 |
|
---|
2308 | return rc;
|
---|
2309 | }
|
---|
2310 | HRESULT Medium::lockWrite(ComPtr<IToken> &aToken)
|
---|
2311 | {
|
---|
2312 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2313 |
|
---|
2314 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
2315 | if (m->queryInfoRunning)
|
---|
2316 | {
|
---|
2317 | /* Must not hold the media tree lock, as Medium::i_queryInfo needs this
|
---|
2318 | * lock and thus we would run into a deadlock here. */
|
---|
2319 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
2320 | while (m->queryInfoRunning)
|
---|
2321 | {
|
---|
2322 | alock.release();
|
---|
2323 | /* must not hold the object lock now */
|
---|
2324 | Assert(!isWriteLockOnCurrentThread());
|
---|
2325 | {
|
---|
2326 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
2327 | }
|
---|
2328 | alock.acquire();
|
---|
2329 | }
|
---|
2330 | }
|
---|
2331 |
|
---|
2332 | HRESULT rc = S_OK;
|
---|
2333 |
|
---|
2334 | switch (m->state)
|
---|
2335 | {
|
---|
2336 | case MediumState_Created:
|
---|
2337 | case MediumState_Inaccessible:
|
---|
2338 | {
|
---|
2339 | m->preLockState = m->state;
|
---|
2340 |
|
---|
2341 | LogFlowThisFunc(("Okay - prev state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2342 | m->state = MediumState_LockedWrite;
|
---|
2343 |
|
---|
2344 | ComObjPtr<MediumLockToken> pToken;
|
---|
2345 | rc = pToken.createObject();
|
---|
2346 | if (SUCCEEDED(rc))
|
---|
2347 | rc = pToken->init(this, true /* fWrite */);
|
---|
2348 | if (FAILED(rc))
|
---|
2349 | {
|
---|
2350 | m->state = m->preLockState;
|
---|
2351 | return rc;
|
---|
2352 | }
|
---|
2353 |
|
---|
2354 | pToken.queryInterfaceTo(aToken.asOutParam());
|
---|
2355 | break;
|
---|
2356 | }
|
---|
2357 | default:
|
---|
2358 | {
|
---|
2359 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2360 | rc = i_setStateError();
|
---|
2361 | break;
|
---|
2362 | }
|
---|
2363 | }
|
---|
2364 |
|
---|
2365 | return rc;
|
---|
2366 | }
|
---|
2367 |
|
---|
2368 | /**
|
---|
2369 | * @note @a aState may be NULL if the state value is not needed (only for
|
---|
2370 | * in-process calls).
|
---|
2371 | */
|
---|
2372 | HRESULT Medium::i_unlockWrite(MediumState_T *aState)
|
---|
2373 | {
|
---|
2374 | AutoCaller autoCaller(this);
|
---|
2375 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2376 |
|
---|
2377 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2378 |
|
---|
2379 | HRESULT rc = S_OK;
|
---|
2380 |
|
---|
2381 | switch (m->state)
|
---|
2382 | {
|
---|
2383 | case MediumState_LockedWrite:
|
---|
2384 | {
|
---|
2385 | m->state = m->preLockState;
|
---|
2386 | /* There are cases where we inject the deleting state into
|
---|
2387 | * a medium locked for writing. Make sure #unmarkForDeletion()
|
---|
2388 | * gets the right state afterwards. */
|
---|
2389 | if (m->preLockState == MediumState_Deleting)
|
---|
2390 | m->preLockState = MediumState_Created;
|
---|
2391 | LogFlowThisFunc(("new state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2392 | break;
|
---|
2393 | }
|
---|
2394 | default:
|
---|
2395 | {
|
---|
2396 | LogFlowThisFunc(("Failing - state=%d locationFull=%s\n", m->state, i_getLocationFull().c_str()));
|
---|
2397 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2398 | tr("Medium '%s' is not locked for writing"),
|
---|
2399 | m->strLocationFull.c_str());
|
---|
2400 | break;
|
---|
2401 | }
|
---|
2402 | }
|
---|
2403 |
|
---|
2404 | /* return the current state after */
|
---|
2405 | if (aState)
|
---|
2406 | *aState = m->state;
|
---|
2407 |
|
---|
2408 | return rc;
|
---|
2409 | }
|
---|
2410 |
|
---|
2411 | HRESULT Medium::close(AutoCaller &aAutoCaller)
|
---|
2412 | {
|
---|
2413 | // make a copy of VirtualBox pointer which gets nulled by uninit()
|
---|
2414 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2415 |
|
---|
2416 | MultiResult mrc = i_close(aAutoCaller);
|
---|
2417 |
|
---|
2418 | pVirtualBox->i_saveModifiedRegistries();
|
---|
2419 |
|
---|
2420 | return mrc;
|
---|
2421 | }
|
---|
2422 |
|
---|
2423 | HRESULT Medium::getProperty(const com::Utf8Str &aName,
|
---|
2424 | com::Utf8Str &aValue)
|
---|
2425 | {
|
---|
2426 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2427 |
|
---|
2428 | settings::StringsMap::const_iterator it = m->mapProperties.find(aName);
|
---|
2429 | if (it == m->mapProperties.end())
|
---|
2430 | {
|
---|
2431 | if (!aName.startsWith("Special/"))
|
---|
2432 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2433 | tr("Property '%s' does not exist"), aName.c_str());
|
---|
2434 | else
|
---|
2435 | /* be more silent here */
|
---|
2436 | return VBOX_E_OBJECT_NOT_FOUND;
|
---|
2437 | }
|
---|
2438 |
|
---|
2439 | aValue = it->second;
|
---|
2440 |
|
---|
2441 | return S_OK;
|
---|
2442 | }
|
---|
2443 |
|
---|
2444 | HRESULT Medium::setProperty(const com::Utf8Str &aName,
|
---|
2445 | const com::Utf8Str &aValue)
|
---|
2446 | {
|
---|
2447 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2448 |
|
---|
2449 | switch (m->state)
|
---|
2450 | {
|
---|
2451 | case MediumState_Created:
|
---|
2452 | case MediumState_Inaccessible:
|
---|
2453 | break;
|
---|
2454 | default:
|
---|
2455 | return i_setStateError();
|
---|
2456 | }
|
---|
2457 |
|
---|
2458 | settings::StringsMap::iterator it = m->mapProperties.find(aName);
|
---|
2459 | if ( !aName.startsWith("Special/")
|
---|
2460 | && !i_isPropertyForFilter(aName))
|
---|
2461 | {
|
---|
2462 | if (it == m->mapProperties.end())
|
---|
2463 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2464 | tr("Property '%s' does not exist"),
|
---|
2465 | aName.c_str());
|
---|
2466 | it->second = aValue;
|
---|
2467 | }
|
---|
2468 | else
|
---|
2469 | {
|
---|
2470 | if (it == m->mapProperties.end())
|
---|
2471 | {
|
---|
2472 | if (!aValue.isEmpty())
|
---|
2473 | m->mapProperties[aName] = aValue;
|
---|
2474 | }
|
---|
2475 | else
|
---|
2476 | {
|
---|
2477 | if (!aValue.isEmpty())
|
---|
2478 | it->second = aValue;
|
---|
2479 | else
|
---|
2480 | m->mapProperties.erase(it);
|
---|
2481 | }
|
---|
2482 | }
|
---|
2483 |
|
---|
2484 | // save the settings
|
---|
2485 | mlock.release();
|
---|
2486 | i_markRegistriesModified();
|
---|
2487 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2488 |
|
---|
2489 | return S_OK;
|
---|
2490 | }
|
---|
2491 |
|
---|
2492 | HRESULT Medium::getProperties(const com::Utf8Str &aNames,
|
---|
2493 | std::vector<com::Utf8Str> &aReturnNames,
|
---|
2494 | std::vector<com::Utf8Str> &aReturnValues)
|
---|
2495 | {
|
---|
2496 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2497 |
|
---|
2498 | /// @todo make use of aNames according to the documentation
|
---|
2499 | NOREF(aNames);
|
---|
2500 |
|
---|
2501 | aReturnNames.resize(m->mapProperties.size());
|
---|
2502 | aReturnValues.resize(m->mapProperties.size());
|
---|
2503 | size_t i = 0;
|
---|
2504 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
2505 | it != m->mapProperties.end();
|
---|
2506 | ++it, ++i)
|
---|
2507 | {
|
---|
2508 | aReturnNames[i] = it->first;
|
---|
2509 | aReturnValues[i] = it->second;
|
---|
2510 | }
|
---|
2511 | return S_OK;
|
---|
2512 | }
|
---|
2513 |
|
---|
2514 | HRESULT Medium::setProperties(const std::vector<com::Utf8Str> &aNames,
|
---|
2515 | const std::vector<com::Utf8Str> &aValues)
|
---|
2516 | {
|
---|
2517 | AutoWriteLock mlock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2518 |
|
---|
2519 | /* first pass: validate names */
|
---|
2520 | for (size_t i = 0;
|
---|
2521 | i < aNames.size();
|
---|
2522 | ++i)
|
---|
2523 | {
|
---|
2524 | Utf8Str strName(aNames[i]);
|
---|
2525 | if ( !strName.startsWith("Special/")
|
---|
2526 | && !i_isPropertyForFilter(strName)
|
---|
2527 | && m->mapProperties.find(strName) == m->mapProperties.end())
|
---|
2528 | return setError(VBOX_E_OBJECT_NOT_FOUND,
|
---|
2529 | tr("Property '%s' does not exist"), strName.c_str());
|
---|
2530 | }
|
---|
2531 |
|
---|
2532 | /* second pass: assign */
|
---|
2533 | for (size_t i = 0;
|
---|
2534 | i < aNames.size();
|
---|
2535 | ++i)
|
---|
2536 | {
|
---|
2537 | Utf8Str strName(aNames[i]);
|
---|
2538 | Utf8Str strValue(aValues[i]);
|
---|
2539 | settings::StringsMap::iterator it = m->mapProperties.find(strName);
|
---|
2540 | if ( !strName.startsWith("Special/")
|
---|
2541 | && !i_isPropertyForFilter(strName))
|
---|
2542 | {
|
---|
2543 | AssertReturn(it != m->mapProperties.end(), E_FAIL);
|
---|
2544 | it->second = strValue;
|
---|
2545 | }
|
---|
2546 | else
|
---|
2547 | {
|
---|
2548 | if (it == m->mapProperties.end())
|
---|
2549 | {
|
---|
2550 | if (!strValue.isEmpty())
|
---|
2551 | m->mapProperties[strName] = strValue;
|
---|
2552 | }
|
---|
2553 | else
|
---|
2554 | {
|
---|
2555 | if (!strValue.isEmpty())
|
---|
2556 | it->second = strValue;
|
---|
2557 | else
|
---|
2558 | m->mapProperties.erase(it);
|
---|
2559 | }
|
---|
2560 | }
|
---|
2561 | }
|
---|
2562 |
|
---|
2563 | // save the settings
|
---|
2564 | mlock.release();
|
---|
2565 | i_markRegistriesModified();
|
---|
2566 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2567 |
|
---|
2568 | return S_OK;
|
---|
2569 | }
|
---|
2570 | HRESULT Medium::createBaseStorage(LONG64 aLogicalSize,
|
---|
2571 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2572 | ComPtr<IProgress> &aProgress)
|
---|
2573 | {
|
---|
2574 | if (aLogicalSize < 0)
|
---|
2575 | return setError(E_INVALIDARG, tr("The medium size argument (%lld) is negative"), aLogicalSize);
|
---|
2576 |
|
---|
2577 | HRESULT rc = S_OK;
|
---|
2578 | ComObjPtr<Progress> pProgress;
|
---|
2579 | Medium::Task *pTask = NULL;
|
---|
2580 |
|
---|
2581 | try
|
---|
2582 | {
|
---|
2583 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
2584 |
|
---|
2585 | ULONG mediumVariantFlags = 0;
|
---|
2586 |
|
---|
2587 | if (aVariant.size())
|
---|
2588 | {
|
---|
2589 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2590 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2591 | }
|
---|
2592 |
|
---|
2593 | mediumVariantFlags &= ((unsigned)~MediumVariant_Diff);
|
---|
2594 |
|
---|
2595 | if ( !(mediumVariantFlags & MediumVariant_Fixed)
|
---|
2596 | && !(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_CreateDynamic))
|
---|
2597 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2598 | tr("Medium format '%s' does not support dynamic storage creation"),
|
---|
2599 | m->strFormat.c_str());
|
---|
2600 |
|
---|
2601 | if ( (mediumVariantFlags & MediumVariant_Fixed)
|
---|
2602 | && !(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_CreateFixed))
|
---|
2603 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
2604 | tr("Medium format '%s' does not support fixed storage creation"),
|
---|
2605 | m->strFormat.c_str());
|
---|
2606 |
|
---|
2607 | if (m->state != MediumState_NotCreated)
|
---|
2608 | throw i_setStateError();
|
---|
2609 |
|
---|
2610 | pProgress.createObject();
|
---|
2611 | rc = pProgress->init(m->pVirtualBox,
|
---|
2612 | static_cast<IMedium*>(this),
|
---|
2613 | (mediumVariantFlags & MediumVariant_Fixed)
|
---|
2614 | ? BstrFmt(tr("Creating fixed medium storage unit '%s'"), m->strLocationFull.c_str()).raw()
|
---|
2615 | : BstrFmt(tr("Creating dynamic medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
2616 | TRUE /* aCancelable */);
|
---|
2617 | if (FAILED(rc))
|
---|
2618 | throw rc;
|
---|
2619 |
|
---|
2620 | /* setup task object to carry out the operation asynchronously */
|
---|
2621 | pTask = new Medium::CreateBaseTask(this, pProgress, aLogicalSize,
|
---|
2622 | (MediumVariant_T)mediumVariantFlags);
|
---|
2623 | //(MediumVariant_T)aVariant);
|
---|
2624 | rc = pTask->rc();
|
---|
2625 | AssertComRC(rc);
|
---|
2626 | if (FAILED(rc))
|
---|
2627 | throw rc;
|
---|
2628 |
|
---|
2629 | m->state = MediumState_Creating;
|
---|
2630 | }
|
---|
2631 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2632 |
|
---|
2633 | if (SUCCEEDED(rc))
|
---|
2634 | {
|
---|
2635 | rc = pTask->createThread();
|
---|
2636 |
|
---|
2637 | if (SUCCEEDED(rc))
|
---|
2638 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2639 | }
|
---|
2640 | else if (pTask != NULL)
|
---|
2641 | delete pTask;
|
---|
2642 |
|
---|
2643 | return rc;
|
---|
2644 | }
|
---|
2645 |
|
---|
2646 | HRESULT Medium::deleteStorage(ComPtr<IProgress> &aProgress)
|
---|
2647 | {
|
---|
2648 | ComObjPtr<Progress> pProgress;
|
---|
2649 |
|
---|
2650 | MultiResult mrc = i_deleteStorage(&pProgress,
|
---|
2651 | false /* aWait */);
|
---|
2652 | /* Must save the registries in any case, since an entry was removed. */
|
---|
2653 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
2654 |
|
---|
2655 | if (SUCCEEDED(mrc))
|
---|
2656 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2657 |
|
---|
2658 | return mrc;
|
---|
2659 | }
|
---|
2660 |
|
---|
2661 | HRESULT Medium::createDiffStorage(AutoCaller &autoCaller,
|
---|
2662 | const ComPtr<IMedium> &aTarget,
|
---|
2663 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2664 | ComPtr<IProgress> &aProgress)
|
---|
2665 | {
|
---|
2666 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
2667 | * to lock order violations, it probably causes lock order issues related
|
---|
2668 | * to the AutoCaller usage. */
|
---|
2669 | IMedium *aT = aTarget;
|
---|
2670 | ComObjPtr<Medium> diff = static_cast<Medium*>(aT);
|
---|
2671 |
|
---|
2672 | autoCaller.release();
|
---|
2673 |
|
---|
2674 | /* It is possible that some previous/concurrent uninit has already cleared
|
---|
2675 | * the pVirtualBox reference, see #uninit(). */
|
---|
2676 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
2677 |
|
---|
2678 | // we access m->pParent
|
---|
2679 | AutoReadLock treeLock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL COMMA_LOCKVAL_SRC_POS);
|
---|
2680 |
|
---|
2681 | autoCaller.add();
|
---|
2682 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
2683 |
|
---|
2684 | AutoMultiWriteLock2 alock(this->lockHandle(), diff->lockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
2685 |
|
---|
2686 | if (m->type == MediumType_Writethrough)
|
---|
2687 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2688 | tr("Medium type of '%s' is Writethrough"),
|
---|
2689 | m->strLocationFull.c_str());
|
---|
2690 | else if (m->type == MediumType_Shareable)
|
---|
2691 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2692 | tr("Medium type of '%s' is Shareable"),
|
---|
2693 | m->strLocationFull.c_str());
|
---|
2694 | else if (m->type == MediumType_Readonly)
|
---|
2695 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
2696 | tr("Medium type of '%s' is Readonly"),
|
---|
2697 | m->strLocationFull.c_str());
|
---|
2698 |
|
---|
2699 | /* Apply the normal locking logic to the entire chain. */
|
---|
2700 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
2701 | alock.release();
|
---|
2702 | treeLock.release();
|
---|
2703 | HRESULT rc = diff->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2704 | diff /* pToLockWrite */,
|
---|
2705 | false /* fMediumLockWriteAll */,
|
---|
2706 | this,
|
---|
2707 | *pMediumLockList);
|
---|
2708 | treeLock.acquire();
|
---|
2709 | alock.acquire();
|
---|
2710 | if (FAILED(rc))
|
---|
2711 | {
|
---|
2712 | delete pMediumLockList;
|
---|
2713 | return rc;
|
---|
2714 | }
|
---|
2715 |
|
---|
2716 | alock.release();
|
---|
2717 | treeLock.release();
|
---|
2718 | rc = pMediumLockList->Lock();
|
---|
2719 | treeLock.acquire();
|
---|
2720 | alock.acquire();
|
---|
2721 | if (FAILED(rc))
|
---|
2722 | {
|
---|
2723 | delete pMediumLockList;
|
---|
2724 |
|
---|
2725 | return setError(rc, tr("Could not lock medium when creating diff '%s'"),
|
---|
2726 | diff->i_getLocationFull().c_str());
|
---|
2727 | }
|
---|
2728 |
|
---|
2729 | Guid parentMachineRegistry;
|
---|
2730 | if (i_getFirstRegistryMachineId(parentMachineRegistry))
|
---|
2731 | {
|
---|
2732 | /* since this medium has been just created it isn't associated yet */
|
---|
2733 | diff->m->llRegistryIDs.push_back(parentMachineRegistry);
|
---|
2734 | alock.release();
|
---|
2735 | treeLock.release();
|
---|
2736 | diff->i_markRegistriesModified();
|
---|
2737 | treeLock.acquire();
|
---|
2738 | alock.acquire();
|
---|
2739 | }
|
---|
2740 |
|
---|
2741 | alock.release();
|
---|
2742 | treeLock.release();
|
---|
2743 |
|
---|
2744 | ComObjPtr<Progress> pProgress;
|
---|
2745 |
|
---|
2746 | ULONG mediumVariantFlags = 0;
|
---|
2747 |
|
---|
2748 | if (aVariant.size())
|
---|
2749 | {
|
---|
2750 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2751 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2752 | }
|
---|
2753 |
|
---|
2754 | rc = i_createDiffStorage(diff, (MediumVariant_T)mediumVariantFlags, pMediumLockList,
|
---|
2755 | &pProgress, false /* aWait */);
|
---|
2756 | if (FAILED(rc))
|
---|
2757 | delete pMediumLockList;
|
---|
2758 | else
|
---|
2759 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2760 |
|
---|
2761 | return rc;
|
---|
2762 | }
|
---|
2763 |
|
---|
2764 | HRESULT Medium::mergeTo(const ComPtr<IMedium> &aTarget,
|
---|
2765 | ComPtr<IProgress> &aProgress)
|
---|
2766 | {
|
---|
2767 |
|
---|
2768 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
2769 | * to lock order violations, it probably causes lock order issues related
|
---|
2770 | * to the AutoCaller usage. */
|
---|
2771 | IMedium *aT = aTarget;
|
---|
2772 |
|
---|
2773 | ComAssertRet(aT != this, E_INVALIDARG);
|
---|
2774 |
|
---|
2775 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aT);
|
---|
2776 |
|
---|
2777 | bool fMergeForward = false;
|
---|
2778 | ComObjPtr<Medium> pParentForTarget;
|
---|
2779 | MediumLockList *pChildrenToReparent = NULL;
|
---|
2780 | MediumLockList *pMediumLockList = NULL;
|
---|
2781 |
|
---|
2782 | HRESULT rc = S_OK;
|
---|
2783 |
|
---|
2784 | rc = i_prepareMergeTo(pTarget, NULL, NULL, true, fMergeForward,
|
---|
2785 | pParentForTarget, pChildrenToReparent, pMediumLockList);
|
---|
2786 | if (FAILED(rc)) return rc;
|
---|
2787 |
|
---|
2788 | ComObjPtr<Progress> pProgress;
|
---|
2789 |
|
---|
2790 | rc = i_mergeTo(pTarget, fMergeForward, pParentForTarget, pChildrenToReparent,
|
---|
2791 | pMediumLockList, &pProgress, false /* aWait */);
|
---|
2792 | if (FAILED(rc))
|
---|
2793 | i_cancelMergeTo(pChildrenToReparent, pMediumLockList);
|
---|
2794 | else
|
---|
2795 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2796 |
|
---|
2797 | return rc;
|
---|
2798 | }
|
---|
2799 |
|
---|
2800 | HRESULT Medium::cloneToBase(const ComPtr<IMedium> &aTarget,
|
---|
2801 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2802 | ComPtr<IProgress> &aProgress)
|
---|
2803 | {
|
---|
2804 | int rc = S_OK;
|
---|
2805 |
|
---|
2806 | rc = cloneTo(aTarget, aVariant, NULL, aProgress);
|
---|
2807 | return rc;
|
---|
2808 | }
|
---|
2809 |
|
---|
2810 | HRESULT Medium::cloneTo(const ComPtr<IMedium> &aTarget,
|
---|
2811 | const std::vector<MediumVariant_T> &aVariant,
|
---|
2812 | const ComPtr<IMedium> &aParent,
|
---|
2813 | ComPtr<IProgress> &aProgress)
|
---|
2814 | {
|
---|
2815 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
2816 | * to lock order violations, it probably causes lock order issues related
|
---|
2817 | * to the AutoCaller usage. */
|
---|
2818 | ComAssertRet(aTarget != this, E_INVALIDARG);
|
---|
2819 |
|
---|
2820 | IMedium *aT = aTarget;
|
---|
2821 | ComObjPtr<Medium> pTarget = static_cast<Medium*>(aT);
|
---|
2822 | ComObjPtr<Medium> pParent;
|
---|
2823 | if (aParent)
|
---|
2824 | {
|
---|
2825 | IMedium *aP = aParent;
|
---|
2826 | pParent = static_cast<Medium*>(aP);
|
---|
2827 | }
|
---|
2828 |
|
---|
2829 | HRESULT rc = S_OK;
|
---|
2830 | ComObjPtr<Progress> pProgress;
|
---|
2831 | Medium::Task *pTask = NULL;
|
---|
2832 |
|
---|
2833 | try
|
---|
2834 | {
|
---|
2835 | // locking: we need the tree lock first because we access parent pointers
|
---|
2836 | // and we need to write-lock the media involved
|
---|
2837 | uint32_t cHandles = 3;
|
---|
2838 | LockHandle* pHandles[4] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
2839 | this->lockHandle(),
|
---|
2840 | pTarget->lockHandle() };
|
---|
2841 | /* Only add parent to the lock if it is not null */
|
---|
2842 | if (!pParent.isNull())
|
---|
2843 | pHandles[cHandles++] = pParent->lockHandle();
|
---|
2844 | AutoWriteLock alock(cHandles,
|
---|
2845 | pHandles
|
---|
2846 | COMMA_LOCKVAL_SRC_POS);
|
---|
2847 |
|
---|
2848 | if ( pTarget->m->state != MediumState_NotCreated
|
---|
2849 | && pTarget->m->state != MediumState_Created)
|
---|
2850 | throw pTarget->i_setStateError();
|
---|
2851 |
|
---|
2852 | /* Build the source lock list. */
|
---|
2853 | MediumLockList *pSourceMediumLockList(new MediumLockList());
|
---|
2854 | alock.release();
|
---|
2855 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2856 | NULL /* pToLockWrite */,
|
---|
2857 | false /* fMediumLockWriteAll */,
|
---|
2858 | NULL,
|
---|
2859 | *pSourceMediumLockList);
|
---|
2860 | alock.acquire();
|
---|
2861 | if (FAILED(rc))
|
---|
2862 | {
|
---|
2863 | delete pSourceMediumLockList;
|
---|
2864 | throw rc;
|
---|
2865 | }
|
---|
2866 |
|
---|
2867 | /* Build the target lock list (including the to-be parent chain). */
|
---|
2868 | MediumLockList *pTargetMediumLockList(new MediumLockList());
|
---|
2869 | alock.release();
|
---|
2870 | rc = pTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
2871 | pTarget /* pToLockWrite */,
|
---|
2872 | false /* fMediumLockWriteAll */,
|
---|
2873 | pParent,
|
---|
2874 | *pTargetMediumLockList);
|
---|
2875 | alock.acquire();
|
---|
2876 | if (FAILED(rc))
|
---|
2877 | {
|
---|
2878 | delete pSourceMediumLockList;
|
---|
2879 | delete pTargetMediumLockList;
|
---|
2880 | throw rc;
|
---|
2881 | }
|
---|
2882 |
|
---|
2883 | alock.release();
|
---|
2884 | rc = pSourceMediumLockList->Lock();
|
---|
2885 | alock.acquire();
|
---|
2886 | if (FAILED(rc))
|
---|
2887 | {
|
---|
2888 | delete pSourceMediumLockList;
|
---|
2889 | delete pTargetMediumLockList;
|
---|
2890 | throw setError(rc,
|
---|
2891 | tr("Failed to lock source media '%s'"),
|
---|
2892 | i_getLocationFull().c_str());
|
---|
2893 | }
|
---|
2894 | alock.release();
|
---|
2895 | rc = pTargetMediumLockList->Lock();
|
---|
2896 | alock.acquire();
|
---|
2897 | if (FAILED(rc))
|
---|
2898 | {
|
---|
2899 | delete pSourceMediumLockList;
|
---|
2900 | delete pTargetMediumLockList;
|
---|
2901 | throw setError(rc,
|
---|
2902 | tr("Failed to lock target media '%s'"),
|
---|
2903 | pTarget->i_getLocationFull().c_str());
|
---|
2904 | }
|
---|
2905 |
|
---|
2906 | pProgress.createObject();
|
---|
2907 | rc = pProgress->init(m->pVirtualBox,
|
---|
2908 | static_cast <IMedium *>(this),
|
---|
2909 | BstrFmt(tr("Creating clone medium '%s'"), pTarget->m->strLocationFull.c_str()).raw(),
|
---|
2910 | TRUE /* aCancelable */);
|
---|
2911 | if (FAILED(rc))
|
---|
2912 | {
|
---|
2913 | delete pSourceMediumLockList;
|
---|
2914 | delete pTargetMediumLockList;
|
---|
2915 | throw rc;
|
---|
2916 | }
|
---|
2917 |
|
---|
2918 | ULONG mediumVariantFlags = 0;
|
---|
2919 |
|
---|
2920 | if (aVariant.size())
|
---|
2921 | {
|
---|
2922 | for (size_t i = 0; i < aVariant.size(); i++)
|
---|
2923 | mediumVariantFlags |= (ULONG)aVariant[i];
|
---|
2924 | }
|
---|
2925 |
|
---|
2926 | /* setup task object to carry out the operation asynchronously */
|
---|
2927 | pTask = new Medium::CloneTask(this, pProgress, pTarget,
|
---|
2928 | (MediumVariant_T)mediumVariantFlags,
|
---|
2929 | pParent, UINT32_MAX, UINT32_MAX,
|
---|
2930 | pSourceMediumLockList, pTargetMediumLockList);
|
---|
2931 | rc = pTask->rc();
|
---|
2932 | AssertComRC(rc);
|
---|
2933 | if (FAILED(rc))
|
---|
2934 | throw rc;
|
---|
2935 |
|
---|
2936 | if (pTarget->m->state == MediumState_NotCreated)
|
---|
2937 | pTarget->m->state = MediumState_Creating;
|
---|
2938 | }
|
---|
2939 | catch (HRESULT aRC) { rc = aRC; }
|
---|
2940 |
|
---|
2941 | if (SUCCEEDED(rc))
|
---|
2942 | {
|
---|
2943 | rc = pTask->createThread();
|
---|
2944 |
|
---|
2945 | if (SUCCEEDED(rc))
|
---|
2946 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
2947 | }
|
---|
2948 | else if (pTask != NULL)
|
---|
2949 | delete pTask;
|
---|
2950 |
|
---|
2951 | return rc;
|
---|
2952 | }
|
---|
2953 |
|
---|
2954 | HRESULT Medium::setLocation(const com::Utf8Str &aLocation, ComPtr<IProgress> &aProgress)
|
---|
2955 | {
|
---|
2956 |
|
---|
2957 | ComObjPtr<Medium> pParent;
|
---|
2958 | ComObjPtr<Progress> pProgress;
|
---|
2959 | HRESULT rc = S_OK;
|
---|
2960 | Medium::Task *pTask = NULL;
|
---|
2961 |
|
---|
2962 | try
|
---|
2963 | {
|
---|
2964 | /// @todo NEWMEDIA for file names, add the default extension if no extension
|
---|
2965 | /// is present (using the information from the VD backend which also implies
|
---|
2966 | /// that one more parameter should be passed to setLocation() requesting
|
---|
2967 | /// that functionality since it is only allowed when called from this method
|
---|
2968 |
|
---|
2969 | /// @todo NEWMEDIA rename the file and set m->location on success, then save
|
---|
2970 | /// the global registry (and local registries of portable VMs referring to
|
---|
2971 | /// this medium), this will also require to add the mRegistered flag to data
|
---|
2972 |
|
---|
2973 | // locking: we need the tree lock first because we access parent pointers
|
---|
2974 | // and we need to write-lock the media involved
|
---|
2975 | uint32_t cHandles = 2;
|
---|
2976 | LockHandle* pHandles[2] = { &m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
2977 | this->lockHandle() };
|
---|
2978 |
|
---|
2979 | AutoWriteLock alock(cHandles,
|
---|
2980 | pHandles
|
---|
2981 | COMMA_LOCKVAL_SRC_POS);
|
---|
2982 |
|
---|
2983 | /* play with locations */
|
---|
2984 | {
|
---|
2985 | /* get source path and filename */
|
---|
2986 | Utf8Str sourcePath = i_getLocationFull();
|
---|
2987 | Utf8Str sourceFName = i_getName();
|
---|
2988 |
|
---|
2989 | if (aLocation.isEmpty())
|
---|
2990 | {
|
---|
2991 | rc = setError(VERR_PATH_ZERO_LENGTH,
|
---|
2992 | tr("Medium '%s' can't be moved. Destination path is empty."),
|
---|
2993 | i_getLocationFull().c_str());
|
---|
2994 | throw rc;
|
---|
2995 | }
|
---|
2996 |
|
---|
2997 | /* extract destination path and filename */
|
---|
2998 | Utf8Str destPath(aLocation);
|
---|
2999 | Utf8Str destFName(destPath);
|
---|
3000 | destFName.stripPath();
|
---|
3001 |
|
---|
3002 | Utf8Str suffix(destFName);
|
---|
3003 | suffix.stripSuffix();
|
---|
3004 |
|
---|
3005 | if (suffix.equals(destFName) && !destFName.isEmpty())
|
---|
3006 | {
|
---|
3007 | /*
|
---|
3008 | * The target path has no filename: Either "/path/to/new/location" or
|
---|
3009 | * just "newname" (no trailing backslash or there is no filename with
|
---|
3010 | * extension(suffix)).
|
---|
3011 | */
|
---|
3012 | if (destPath.equals(destFName))
|
---|
3013 | {
|
---|
3014 | /* new path contains only "newname", no path, no extension */
|
---|
3015 | destFName.append(RTPathSuffix(sourceFName.c_str()));
|
---|
3016 | destPath = destFName;
|
---|
3017 | }
|
---|
3018 | else
|
---|
3019 | {
|
---|
3020 | /* new path looks like "/path/to/new/location" */
|
---|
3021 | destFName.setNull();
|
---|
3022 | destPath.append(RTPATH_SLASH);
|
---|
3023 | }
|
---|
3024 | }
|
---|
3025 |
|
---|
3026 | if (destFName.isEmpty())
|
---|
3027 | {
|
---|
3028 | /* No target name */
|
---|
3029 | destPath.append(sourceFName);
|
---|
3030 | }
|
---|
3031 | else
|
---|
3032 | {
|
---|
3033 | if (destPath.equals(destFName))
|
---|
3034 | {
|
---|
3035 | /*
|
---|
3036 | * The target path contains of only a filename without a directory.
|
---|
3037 | * Move the medium within the source directory to the new name
|
---|
3038 | * (actually rename operation).
|
---|
3039 | * Scratches sourcePath!
|
---|
3040 | */
|
---|
3041 | destPath = sourcePath.stripFilename().append(RTPATH_SLASH).append(destFName);
|
---|
3042 | }
|
---|
3043 | suffix = i_getFormat();
|
---|
3044 | if (suffix.compare("RAW", Utf8Str::CaseInsensitive) == 0)
|
---|
3045 | {
|
---|
3046 | if (i_getDeviceType() == DeviceType_DVD)
|
---|
3047 | suffix = "iso";
|
---|
3048 | else
|
---|
3049 | {
|
---|
3050 | rc = setError(VERR_NOT_A_FILE,
|
---|
3051 | tr("Medium '%s' has RAW type. \"Move\" operation isn't supported for this type."),
|
---|
3052 | i_getLocationFull().c_str());
|
---|
3053 | throw rc;
|
---|
3054 | }
|
---|
3055 | }
|
---|
3056 | /* Set the target extension like on the source. Any conversions are prohibited */
|
---|
3057 | suffix.toLower();
|
---|
3058 | destPath.stripSuffix().append('.').append(suffix);
|
---|
3059 | }
|
---|
3060 |
|
---|
3061 | if (!i_isMediumFormatFile())
|
---|
3062 | {
|
---|
3063 | rc = setError(VERR_NOT_A_FILE,
|
---|
3064 | tr("Medium '%s' isn't a file object. \"Move\" operation isn't supported."),
|
---|
3065 | i_getLocationFull().c_str());
|
---|
3066 | throw rc;
|
---|
3067 | }
|
---|
3068 | /* Path must be absolute */
|
---|
3069 | if (!RTPathStartsWithRoot(destPath.c_str()))
|
---|
3070 | {
|
---|
3071 | rc = setError(VBOX_E_FILE_ERROR,
|
---|
3072 | tr("The given path '%s' is not fully qualified"),
|
---|
3073 | destPath.c_str());
|
---|
3074 | throw rc;
|
---|
3075 | }
|
---|
3076 | /* Check path for a new file object */
|
---|
3077 | rc = VirtualBox::i_ensureFilePathExists(destPath, true);
|
---|
3078 | if (FAILED(rc))
|
---|
3079 | throw rc;
|
---|
3080 |
|
---|
3081 | /* Set needed variables for "moving" procedure. It'll be used later in separate thread task */
|
---|
3082 | rc = i_preparationForMoving(destPath);
|
---|
3083 | if (FAILED(rc))
|
---|
3084 | {
|
---|
3085 | rc = setError(VERR_NO_CHANGE,
|
---|
3086 | tr("Medium '%s' is already in the correct location"),
|
---|
3087 | i_getLocationFull().c_str());
|
---|
3088 | throw rc;
|
---|
3089 | }
|
---|
3090 | }
|
---|
3091 |
|
---|
3092 | /* Check VMs which have this medium attached to*/
|
---|
3093 | std::vector<com::Guid> aMachineIds;
|
---|
3094 | rc = getMachineIds(aMachineIds);
|
---|
3095 | std::vector<com::Guid>::const_iterator currMachineID = aMachineIds.begin();
|
---|
3096 | std::vector<com::Guid>::const_iterator lastMachineID = aMachineIds.end();
|
---|
3097 |
|
---|
3098 | while (currMachineID != lastMachineID)
|
---|
3099 | {
|
---|
3100 | Guid id(*currMachineID);
|
---|
3101 | ComObjPtr<Machine> aMachine;
|
---|
3102 |
|
---|
3103 | alock.release();
|
---|
3104 | rc = m->pVirtualBox->i_findMachine(id, false, true, &aMachine);
|
---|
3105 | alock.acquire();
|
---|
3106 |
|
---|
3107 | if (SUCCEEDED(rc))
|
---|
3108 | {
|
---|
3109 | ComObjPtr<SessionMachine> sm;
|
---|
3110 | ComPtr<IInternalSessionControl> ctl;
|
---|
3111 |
|
---|
3112 | alock.release();
|
---|
3113 | bool ses = aMachine->i_isSessionOpenVM(sm, &ctl);
|
---|
3114 | alock.acquire();
|
---|
3115 |
|
---|
3116 | if (ses)
|
---|
3117 | {
|
---|
3118 | rc = setError(VERR_VM_UNEXPECTED_VM_STATE,
|
---|
3119 | tr("At least the VM '%s' to whom this medium '%s' attached has currently an opened session. Stop all VMs before relocating this medium"),
|
---|
3120 | id.toString().c_str(),
|
---|
3121 | i_getLocationFull().c_str());
|
---|
3122 | throw rc;
|
---|
3123 | }
|
---|
3124 | }
|
---|
3125 | ++currMachineID;
|
---|
3126 | }
|
---|
3127 |
|
---|
3128 | /* Build the source lock list. */
|
---|
3129 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3130 | alock.release();
|
---|
3131 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3132 | this /* pToLockWrite */,
|
---|
3133 | true /* fMediumLockWriteAll */,
|
---|
3134 | NULL,
|
---|
3135 | *pMediumLockList);
|
---|
3136 | alock.acquire();
|
---|
3137 | if (FAILED(rc))
|
---|
3138 | {
|
---|
3139 | delete pMediumLockList;
|
---|
3140 | throw setError(rc,
|
---|
3141 | tr("Failed to create medium lock list for '%s'"),
|
---|
3142 | i_getLocationFull().c_str());
|
---|
3143 | }
|
---|
3144 | alock.release();
|
---|
3145 | rc = pMediumLockList->Lock();
|
---|
3146 | alock.acquire();
|
---|
3147 | if (FAILED(rc))
|
---|
3148 | {
|
---|
3149 | delete pMediumLockList;
|
---|
3150 | throw setError(rc,
|
---|
3151 | tr("Failed to lock media '%s'"),
|
---|
3152 | i_getLocationFull().c_str());
|
---|
3153 | }
|
---|
3154 |
|
---|
3155 | pProgress.createObject();
|
---|
3156 | rc = pProgress->init(m->pVirtualBox,
|
---|
3157 | static_cast <IMedium *>(this),
|
---|
3158 | BstrFmt(tr("Moving medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3159 | TRUE /* aCancelable */);
|
---|
3160 |
|
---|
3161 | /* Do the disk moving. */
|
---|
3162 | if (SUCCEEDED(rc))
|
---|
3163 | {
|
---|
3164 | ULONG mediumVariantFlags = i_getVariant();
|
---|
3165 |
|
---|
3166 | /* setup task object to carry out the operation asynchronously */
|
---|
3167 | pTask = new Medium::MoveTask(this, pProgress,
|
---|
3168 | (MediumVariant_T)mediumVariantFlags,
|
---|
3169 | pMediumLockList);
|
---|
3170 | rc = pTask->rc();
|
---|
3171 | AssertComRC(rc);
|
---|
3172 | if (FAILED(rc))
|
---|
3173 | throw rc;
|
---|
3174 | }
|
---|
3175 |
|
---|
3176 | }
|
---|
3177 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3178 |
|
---|
3179 | if (SUCCEEDED(rc))
|
---|
3180 | {
|
---|
3181 | rc = pTask->createThread();
|
---|
3182 |
|
---|
3183 | if (SUCCEEDED(rc))
|
---|
3184 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3185 | }
|
---|
3186 | else
|
---|
3187 | {
|
---|
3188 | if (pTask)
|
---|
3189 | delete pTask;
|
---|
3190 | }
|
---|
3191 |
|
---|
3192 | return rc;
|
---|
3193 | }
|
---|
3194 |
|
---|
3195 | HRESULT Medium::compact(ComPtr<IProgress> &aProgress)
|
---|
3196 | {
|
---|
3197 | HRESULT rc = S_OK;
|
---|
3198 | ComObjPtr<Progress> pProgress;
|
---|
3199 | Medium::Task *pTask = NULL;
|
---|
3200 |
|
---|
3201 | try
|
---|
3202 | {
|
---|
3203 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3204 |
|
---|
3205 | /* Build the medium lock list. */
|
---|
3206 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3207 | alock.release();
|
---|
3208 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3209 | this /* pToLockWrite */,
|
---|
3210 | false /* fMediumLockWriteAll */,
|
---|
3211 | NULL,
|
---|
3212 | *pMediumLockList);
|
---|
3213 | alock.acquire();
|
---|
3214 | if (FAILED(rc))
|
---|
3215 | {
|
---|
3216 | delete pMediumLockList;
|
---|
3217 | throw rc;
|
---|
3218 | }
|
---|
3219 |
|
---|
3220 | alock.release();
|
---|
3221 | rc = pMediumLockList->Lock();
|
---|
3222 | alock.acquire();
|
---|
3223 | if (FAILED(rc))
|
---|
3224 | {
|
---|
3225 | delete pMediumLockList;
|
---|
3226 | throw setError(rc,
|
---|
3227 | tr("Failed to lock media when compacting '%s'"),
|
---|
3228 | i_getLocationFull().c_str());
|
---|
3229 | }
|
---|
3230 |
|
---|
3231 | pProgress.createObject();
|
---|
3232 | rc = pProgress->init(m->pVirtualBox,
|
---|
3233 | static_cast <IMedium *>(this),
|
---|
3234 | BstrFmt(tr("Compacting medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3235 | TRUE /* aCancelable */);
|
---|
3236 | if (FAILED(rc))
|
---|
3237 | {
|
---|
3238 | delete pMediumLockList;
|
---|
3239 | throw rc;
|
---|
3240 | }
|
---|
3241 |
|
---|
3242 | /* setup task object to carry out the operation asynchronously */
|
---|
3243 | pTask = new Medium::CompactTask(this, pProgress, pMediumLockList);
|
---|
3244 | rc = pTask->rc();
|
---|
3245 | AssertComRC(rc);
|
---|
3246 | if (FAILED(rc))
|
---|
3247 | throw rc;
|
---|
3248 | }
|
---|
3249 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3250 |
|
---|
3251 | if (SUCCEEDED(rc))
|
---|
3252 | {
|
---|
3253 | rc = pTask->createThread();
|
---|
3254 |
|
---|
3255 | if (SUCCEEDED(rc))
|
---|
3256 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3257 | }
|
---|
3258 | else if (pTask != NULL)
|
---|
3259 | delete pTask;
|
---|
3260 |
|
---|
3261 | return rc;
|
---|
3262 | }
|
---|
3263 |
|
---|
3264 | HRESULT Medium::resize(LONG64 aLogicalSize,
|
---|
3265 | ComPtr<IProgress> &aProgress)
|
---|
3266 | {
|
---|
3267 | HRESULT rc = S_OK;
|
---|
3268 | ComObjPtr<Progress> pProgress;
|
---|
3269 | Medium::Task *pTask = NULL;
|
---|
3270 |
|
---|
3271 | try
|
---|
3272 | {
|
---|
3273 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3274 |
|
---|
3275 | /* Build the medium lock list. */
|
---|
3276 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3277 | alock.release();
|
---|
3278 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3279 | this /* pToLockWrite */,
|
---|
3280 | false /* fMediumLockWriteAll */,
|
---|
3281 | NULL,
|
---|
3282 | *pMediumLockList);
|
---|
3283 | alock.acquire();
|
---|
3284 | if (FAILED(rc))
|
---|
3285 | {
|
---|
3286 | delete pMediumLockList;
|
---|
3287 | throw rc;
|
---|
3288 | }
|
---|
3289 |
|
---|
3290 | alock.release();
|
---|
3291 | rc = pMediumLockList->Lock();
|
---|
3292 | alock.acquire();
|
---|
3293 | if (FAILED(rc))
|
---|
3294 | {
|
---|
3295 | delete pMediumLockList;
|
---|
3296 | throw setError(rc,
|
---|
3297 | tr("Failed to lock media when compacting '%s'"),
|
---|
3298 | i_getLocationFull().c_str());
|
---|
3299 | }
|
---|
3300 |
|
---|
3301 | pProgress.createObject();
|
---|
3302 | rc = pProgress->init(m->pVirtualBox,
|
---|
3303 | static_cast <IMedium *>(this),
|
---|
3304 | BstrFmt(tr("Compacting medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3305 | TRUE /* aCancelable */);
|
---|
3306 | if (FAILED(rc))
|
---|
3307 | {
|
---|
3308 | delete pMediumLockList;
|
---|
3309 | throw rc;
|
---|
3310 | }
|
---|
3311 |
|
---|
3312 | /* setup task object to carry out the operation asynchronously */
|
---|
3313 | pTask = new Medium::ResizeTask(this, aLogicalSize, pProgress, pMediumLockList);
|
---|
3314 | rc = pTask->rc();
|
---|
3315 | AssertComRC(rc);
|
---|
3316 | if (FAILED(rc))
|
---|
3317 | throw rc;
|
---|
3318 | }
|
---|
3319 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3320 |
|
---|
3321 | if (SUCCEEDED(rc))
|
---|
3322 | {
|
---|
3323 | rc = pTask->createThread();
|
---|
3324 |
|
---|
3325 | if (SUCCEEDED(rc))
|
---|
3326 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3327 | }
|
---|
3328 | else if (pTask != NULL)
|
---|
3329 | delete pTask;
|
---|
3330 |
|
---|
3331 | return rc;
|
---|
3332 | }
|
---|
3333 |
|
---|
3334 | HRESULT Medium::reset(AutoCaller &autoCaller, ComPtr<IProgress> &aProgress)
|
---|
3335 | {
|
---|
3336 | HRESULT rc = S_OK;
|
---|
3337 | ComObjPtr<Progress> pProgress;
|
---|
3338 | Medium::Task *pTask = NULL;
|
---|
3339 |
|
---|
3340 | try
|
---|
3341 | {
|
---|
3342 | autoCaller.release();
|
---|
3343 |
|
---|
3344 | /* It is possible that some previous/concurrent uninit has already
|
---|
3345 | * cleared the pVirtualBox reference, see #uninit(). */
|
---|
3346 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
3347 |
|
---|
3348 | /* canClose() needs the tree lock */
|
---|
3349 | AutoMultiWriteLock2 multilock(!pVirtualBox.isNull() ? &pVirtualBox->i_getMediaTreeLockHandle() : NULL,
|
---|
3350 | this->lockHandle()
|
---|
3351 | COMMA_LOCKVAL_SRC_POS);
|
---|
3352 |
|
---|
3353 | autoCaller.add();
|
---|
3354 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
3355 |
|
---|
3356 | LogFlowThisFunc(("ENTER for medium %s\n", m->strLocationFull.c_str()));
|
---|
3357 |
|
---|
3358 | if (m->pParent.isNull())
|
---|
3359 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3360 | tr("Medium type of '%s' is not differencing"),
|
---|
3361 | m->strLocationFull.c_str());
|
---|
3362 |
|
---|
3363 | rc = i_canClose();
|
---|
3364 | if (FAILED(rc))
|
---|
3365 | throw rc;
|
---|
3366 |
|
---|
3367 | /* Build the medium lock list. */
|
---|
3368 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3369 | multilock.release();
|
---|
3370 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
3371 | this /* pToLockWrite */,
|
---|
3372 | false /* fMediumLockWriteAll */,
|
---|
3373 | NULL,
|
---|
3374 | *pMediumLockList);
|
---|
3375 | multilock.acquire();
|
---|
3376 | if (FAILED(rc))
|
---|
3377 | {
|
---|
3378 | delete pMediumLockList;
|
---|
3379 | throw rc;
|
---|
3380 | }
|
---|
3381 |
|
---|
3382 | multilock.release();
|
---|
3383 | rc = pMediumLockList->Lock();
|
---|
3384 | multilock.acquire();
|
---|
3385 | if (FAILED(rc))
|
---|
3386 | {
|
---|
3387 | delete pMediumLockList;
|
---|
3388 | throw setError(rc,
|
---|
3389 | tr("Failed to lock media when resetting '%s'"),
|
---|
3390 | i_getLocationFull().c_str());
|
---|
3391 | }
|
---|
3392 |
|
---|
3393 | pProgress.createObject();
|
---|
3394 | rc = pProgress->init(m->pVirtualBox,
|
---|
3395 | static_cast<IMedium*>(this),
|
---|
3396 | BstrFmt(tr("Resetting differencing medium '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
3397 | FALSE /* aCancelable */);
|
---|
3398 | if (FAILED(rc))
|
---|
3399 | throw rc;
|
---|
3400 |
|
---|
3401 | /* setup task object to carry out the operation asynchronously */
|
---|
3402 | pTask = new Medium::ResetTask(this, pProgress, pMediumLockList);
|
---|
3403 | rc = pTask->rc();
|
---|
3404 | AssertComRC(rc);
|
---|
3405 | if (FAILED(rc))
|
---|
3406 | throw rc;
|
---|
3407 | }
|
---|
3408 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3409 |
|
---|
3410 | if (SUCCEEDED(rc))
|
---|
3411 | {
|
---|
3412 | rc = pTask->createThread();
|
---|
3413 |
|
---|
3414 | if (SUCCEEDED(rc))
|
---|
3415 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3416 | }
|
---|
3417 | else if (pTask != NULL)
|
---|
3418 | delete pTask;
|
---|
3419 |
|
---|
3420 | LogFlowThisFunc(("LEAVE, rc=%Rhrc\n", rc));
|
---|
3421 |
|
---|
3422 | return rc;
|
---|
3423 | }
|
---|
3424 |
|
---|
3425 | HRESULT Medium::changeEncryption(const com::Utf8Str &aCurrentPassword, const com::Utf8Str &aCipher,
|
---|
3426 | const com::Utf8Str &aNewPassword, const com::Utf8Str &aNewPasswordId,
|
---|
3427 | ComPtr<IProgress> &aProgress)
|
---|
3428 | {
|
---|
3429 | HRESULT rc = S_OK;
|
---|
3430 | ComObjPtr<Progress> pProgress;
|
---|
3431 | Medium::Task *pTask = NULL;
|
---|
3432 |
|
---|
3433 | try
|
---|
3434 | {
|
---|
3435 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3436 |
|
---|
3437 | DeviceType_T devType = i_getDeviceType();
|
---|
3438 | /* Cannot encrypt DVD or floppy images so far. */
|
---|
3439 | if ( devType == DeviceType_DVD
|
---|
3440 | || devType == DeviceType_Floppy)
|
---|
3441 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3442 | tr("Cannot encrypt DVD or Floppy medium '%s'"),
|
---|
3443 | m->strLocationFull.c_str());
|
---|
3444 |
|
---|
3445 | /* Cannot encrypt media which are attached to more than one virtual machine. */
|
---|
3446 | if (m->backRefs.size() > 1)
|
---|
3447 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3448 | tr("Cannot encrypt medium '%s' because it is attached to %d virtual machines"),
|
---|
3449 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
3450 |
|
---|
3451 | if (i_getChildren().size() != 0)
|
---|
3452 | return setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3453 | tr("Cannot encrypt medium '%s' because it has %d children"),
|
---|
3454 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
3455 |
|
---|
3456 | /* Build the medium lock list. */
|
---|
3457 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
3458 | alock.release();
|
---|
3459 | rc = i_createMediumLockList(true /* fFailIfInaccessible */ ,
|
---|
3460 | this /* pToLockWrite */,
|
---|
3461 | true /* fMediumLockAllWrite */,
|
---|
3462 | NULL,
|
---|
3463 | *pMediumLockList);
|
---|
3464 | alock.acquire();
|
---|
3465 | if (FAILED(rc))
|
---|
3466 | {
|
---|
3467 | delete pMediumLockList;
|
---|
3468 | throw rc;
|
---|
3469 | }
|
---|
3470 |
|
---|
3471 | alock.release();
|
---|
3472 | rc = pMediumLockList->Lock();
|
---|
3473 | alock.acquire();
|
---|
3474 | if (FAILED(rc))
|
---|
3475 | {
|
---|
3476 | delete pMediumLockList;
|
---|
3477 | throw setError(rc,
|
---|
3478 | tr("Failed to lock media for encryption '%s'"),
|
---|
3479 | i_getLocationFull().c_str());
|
---|
3480 | }
|
---|
3481 |
|
---|
3482 | /*
|
---|
3483 | * Check all media in the chain to not contain any branches or references to
|
---|
3484 | * other virtual machines, we support encrypting only a list of differencing media at the moment.
|
---|
3485 | */
|
---|
3486 | MediumLockList::Base::const_iterator mediumListBegin = pMediumLockList->GetBegin();
|
---|
3487 | MediumLockList::Base::const_iterator mediumListEnd = pMediumLockList->GetEnd();
|
---|
3488 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
3489 | it != mediumListEnd;
|
---|
3490 | ++it)
|
---|
3491 | {
|
---|
3492 | const MediumLock &mediumLock = *it;
|
---|
3493 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
3494 | AutoReadLock mediumReadLock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
3495 |
|
---|
3496 | Assert(pMedium->m->state == MediumState_LockedWrite);
|
---|
3497 |
|
---|
3498 | if (pMedium->m->backRefs.size() > 1)
|
---|
3499 | {
|
---|
3500 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3501 | tr("Cannot encrypt medium '%s' because it is attached to %d virtual machines"),
|
---|
3502 | pMedium->m->strLocationFull.c_str(), pMedium->m->backRefs.size());
|
---|
3503 | break;
|
---|
3504 | }
|
---|
3505 | else if (pMedium->i_getChildren().size() > 1)
|
---|
3506 | {
|
---|
3507 | rc = setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3508 | tr("Cannot encrypt medium '%s' because it has %d children"),
|
---|
3509 | pMedium->m->strLocationFull.c_str(), pMedium->i_getChildren().size());
|
---|
3510 | break;
|
---|
3511 | }
|
---|
3512 | }
|
---|
3513 |
|
---|
3514 | if (FAILED(rc))
|
---|
3515 | {
|
---|
3516 | delete pMediumLockList;
|
---|
3517 | throw rc;
|
---|
3518 | }
|
---|
3519 |
|
---|
3520 | const char *pszAction = "Encrypting";
|
---|
3521 | if ( aCurrentPassword.isNotEmpty()
|
---|
3522 | && aCipher.isEmpty())
|
---|
3523 | pszAction = "Decrypting";
|
---|
3524 |
|
---|
3525 | pProgress.createObject();
|
---|
3526 | rc = pProgress->init(m->pVirtualBox,
|
---|
3527 | static_cast <IMedium *>(this),
|
---|
3528 | BstrFmt(tr("%s medium '%s'"), pszAction, m->strLocationFull.c_str()).raw(),
|
---|
3529 | TRUE /* aCancelable */);
|
---|
3530 | if (FAILED(rc))
|
---|
3531 | {
|
---|
3532 | delete pMediumLockList;
|
---|
3533 | throw rc;
|
---|
3534 | }
|
---|
3535 |
|
---|
3536 | /* setup task object to carry out the operation asynchronously */
|
---|
3537 | pTask = new Medium::EncryptTask(this, aNewPassword, aCurrentPassword,
|
---|
3538 | aCipher, aNewPasswordId, pProgress, pMediumLockList);
|
---|
3539 | rc = pTask->rc();
|
---|
3540 | AssertComRC(rc);
|
---|
3541 | if (FAILED(rc))
|
---|
3542 | throw rc;
|
---|
3543 | }
|
---|
3544 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3545 |
|
---|
3546 | if (SUCCEEDED(rc))
|
---|
3547 | {
|
---|
3548 | rc = pTask->createThread();
|
---|
3549 |
|
---|
3550 | if (SUCCEEDED(rc))
|
---|
3551 | pProgress.queryInterfaceTo(aProgress.asOutParam());
|
---|
3552 | }
|
---|
3553 | else if (pTask != NULL)
|
---|
3554 | delete pTask;
|
---|
3555 |
|
---|
3556 | return rc;
|
---|
3557 | }
|
---|
3558 |
|
---|
3559 | HRESULT Medium::getEncryptionSettings(com::Utf8Str &aCipher, com::Utf8Str &aPasswordId)
|
---|
3560 | {
|
---|
3561 | #ifndef VBOX_WITH_EXTPACK
|
---|
3562 | RT_NOREF(aCipher, aPasswordId);
|
---|
3563 | #endif
|
---|
3564 | HRESULT rc = S_OK;
|
---|
3565 |
|
---|
3566 | try
|
---|
3567 | {
|
---|
3568 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
3569 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3570 |
|
---|
3571 | /* Check whether encryption is configured for this medium. */
|
---|
3572 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
3573 | if (it == pBase->m->mapProperties.end())
|
---|
3574 | throw VBOX_E_NOT_SUPPORTED;
|
---|
3575 |
|
---|
3576 | # ifdef VBOX_WITH_EXTPACK
|
---|
3577 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
3578 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
3579 | {
|
---|
3580 | /* Load the plugin */
|
---|
3581 | Utf8Str strPlugin;
|
---|
3582 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
3583 | if (SUCCEEDED(rc))
|
---|
3584 | {
|
---|
3585 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
3586 | if (RT_FAILURE(vrc))
|
---|
3587 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3588 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
3589 | i_vdError(vrc).c_str());
|
---|
3590 | }
|
---|
3591 | else
|
---|
3592 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3593 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
3594 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3595 | }
|
---|
3596 | else
|
---|
3597 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3598 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
3599 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3600 |
|
---|
3601 | PVDISK pDisk = NULL;
|
---|
3602 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
3603 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
3604 |
|
---|
3605 | Medium::CryptoFilterSettings CryptoSettings;
|
---|
3606 |
|
---|
3607 | i_taskEncryptSettingsSetup(&CryptoSettings, NULL, it->second.c_str(), NULL, false /* fCreateKeyStore */);
|
---|
3608 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ | VD_FILTER_FLAGS_INFO, CryptoSettings.vdFilterIfaces);
|
---|
3609 | if (RT_FAILURE(vrc))
|
---|
3610 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3611 | tr("Failed to load the encryption filter: %s"),
|
---|
3612 | i_vdError(vrc).c_str());
|
---|
3613 |
|
---|
3614 | it = pBase->m->mapProperties.find("CRYPT/KeyId");
|
---|
3615 | if (it == pBase->m->mapProperties.end())
|
---|
3616 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3617 | tr("Image is configured for encryption but doesn't has a KeyId set"));
|
---|
3618 |
|
---|
3619 | aPasswordId = it->second.c_str();
|
---|
3620 | aCipher = CryptoSettings.pszCipherReturned;
|
---|
3621 | RTStrFree(CryptoSettings.pszCipherReturned);
|
---|
3622 |
|
---|
3623 | VDDestroy(pDisk);
|
---|
3624 | # else
|
---|
3625 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3626 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
3627 | # endif
|
---|
3628 | }
|
---|
3629 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3630 |
|
---|
3631 | return rc;
|
---|
3632 | }
|
---|
3633 |
|
---|
3634 | HRESULT Medium::checkEncryptionPassword(const com::Utf8Str &aPassword)
|
---|
3635 | {
|
---|
3636 | HRESULT rc = S_OK;
|
---|
3637 |
|
---|
3638 | try
|
---|
3639 | {
|
---|
3640 | ComObjPtr<Medium> pBase = i_getBase();
|
---|
3641 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3642 |
|
---|
3643 | settings::StringsMap::iterator it = pBase->m->mapProperties.find("CRYPT/KeyStore");
|
---|
3644 | if (it == pBase->m->mapProperties.end())
|
---|
3645 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3646 | tr("The image is not configured for encryption"));
|
---|
3647 |
|
---|
3648 | if (aPassword.isEmpty())
|
---|
3649 | throw setError(E_INVALIDARG,
|
---|
3650 | tr("The given password must not be empty"));
|
---|
3651 |
|
---|
3652 | # ifdef VBOX_WITH_EXTPACK
|
---|
3653 | ExtPackManager *pExtPackManager = m->pVirtualBox->i_getExtPackManager();
|
---|
3654 | if (pExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
|
---|
3655 | {
|
---|
3656 | /* Load the plugin */
|
---|
3657 | Utf8Str strPlugin;
|
---|
3658 | rc = pExtPackManager->i_getLibraryPathForExtPack(g_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
|
---|
3659 | if (SUCCEEDED(rc))
|
---|
3660 | {
|
---|
3661 | int vrc = VDPluginLoadFromFilename(strPlugin.c_str());
|
---|
3662 | if (RT_FAILURE(vrc))
|
---|
3663 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3664 | tr("Retrieving encryption settings of the image failed because the encryption plugin could not be loaded (%s)"),
|
---|
3665 | i_vdError(vrc).c_str());
|
---|
3666 | }
|
---|
3667 | else
|
---|
3668 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3669 | tr("Encryption is not supported because the extension pack '%s' is missing the encryption plugin (old extension pack installed?)"),
|
---|
3670 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3671 | }
|
---|
3672 | else
|
---|
3673 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3674 | tr("Encryption is not supported because the extension pack '%s' is missing"),
|
---|
3675 | ORACLE_PUEL_EXTPACK_NAME);
|
---|
3676 |
|
---|
3677 | PVDISK pDisk = NULL;
|
---|
3678 | int vrc = VDCreate(m->vdDiskIfaces, i_convertDeviceType(), &pDisk);
|
---|
3679 | ComAssertRCThrow(vrc, E_FAIL);
|
---|
3680 |
|
---|
3681 | Medium::CryptoFilterSettings CryptoSettings;
|
---|
3682 |
|
---|
3683 | i_taskEncryptSettingsSetup(&CryptoSettings, NULL, it->second.c_str(), aPassword.c_str(),
|
---|
3684 | false /* fCreateKeyStore */);
|
---|
3685 | vrc = VDFilterAdd(pDisk, "CRYPT", VD_FILTER_FLAGS_READ, CryptoSettings.vdFilterIfaces);
|
---|
3686 | if (vrc == VERR_VD_PASSWORD_INCORRECT)
|
---|
3687 | throw setError(VBOX_E_PASSWORD_INCORRECT,
|
---|
3688 | tr("The given password is incorrect"));
|
---|
3689 | else if (RT_FAILURE(vrc))
|
---|
3690 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
3691 | tr("Failed to load the encryption filter: %s"),
|
---|
3692 | i_vdError(vrc).c_str());
|
---|
3693 |
|
---|
3694 | VDDestroy(pDisk);
|
---|
3695 | # else
|
---|
3696 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
3697 | tr("Encryption is not supported because extension pack support is not built in"));
|
---|
3698 | # endif
|
---|
3699 | }
|
---|
3700 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3701 |
|
---|
3702 | return rc;
|
---|
3703 | }
|
---|
3704 |
|
---|
3705 | ////////////////////////////////////////////////////////////////////////////////
|
---|
3706 | //
|
---|
3707 | // Medium public internal methods
|
---|
3708 | //
|
---|
3709 | ////////////////////////////////////////////////////////////////////////////////
|
---|
3710 |
|
---|
3711 | /**
|
---|
3712 | * Internal method to return the medium's parent medium. Must have caller + locking!
|
---|
3713 | * @return
|
---|
3714 | */
|
---|
3715 | const ComObjPtr<Medium>& Medium::i_getParent() const
|
---|
3716 | {
|
---|
3717 | return m->pParent;
|
---|
3718 | }
|
---|
3719 |
|
---|
3720 | /**
|
---|
3721 | * Internal method to return the medium's list of child media. Must have caller + locking!
|
---|
3722 | * @return
|
---|
3723 | */
|
---|
3724 | const MediaList& Medium::i_getChildren() const
|
---|
3725 | {
|
---|
3726 | return m->llChildren;
|
---|
3727 | }
|
---|
3728 |
|
---|
3729 | /**
|
---|
3730 | * Internal method to return the medium's GUID. Must have caller + locking!
|
---|
3731 | * @return
|
---|
3732 | */
|
---|
3733 | const Guid& Medium::i_getId() const
|
---|
3734 | {
|
---|
3735 | return m->id;
|
---|
3736 | }
|
---|
3737 |
|
---|
3738 | /**
|
---|
3739 | * Internal method to return the medium's state. Must have caller + locking!
|
---|
3740 | * @return
|
---|
3741 | */
|
---|
3742 | MediumState_T Medium::i_getState() const
|
---|
3743 | {
|
---|
3744 | return m->state;
|
---|
3745 | }
|
---|
3746 |
|
---|
3747 | /**
|
---|
3748 | * Internal method to return the medium's variant. Must have caller + locking!
|
---|
3749 | * @return
|
---|
3750 | */
|
---|
3751 | MediumVariant_T Medium::i_getVariant() const
|
---|
3752 | {
|
---|
3753 | return m->variant;
|
---|
3754 | }
|
---|
3755 |
|
---|
3756 | /**
|
---|
3757 | * Internal method which returns true if this medium represents a host drive.
|
---|
3758 | * @return
|
---|
3759 | */
|
---|
3760 | bool Medium::i_isHostDrive() const
|
---|
3761 | {
|
---|
3762 | return m->hostDrive;
|
---|
3763 | }
|
---|
3764 |
|
---|
3765 | /**
|
---|
3766 | * Internal method to return the medium's full location. Must have caller + locking!
|
---|
3767 | * @return
|
---|
3768 | */
|
---|
3769 | const Utf8Str& Medium::i_getLocationFull() const
|
---|
3770 | {
|
---|
3771 | return m->strLocationFull;
|
---|
3772 | }
|
---|
3773 |
|
---|
3774 | /**
|
---|
3775 | * Internal method to return the medium's format string. Must have caller + locking!
|
---|
3776 | * @return
|
---|
3777 | */
|
---|
3778 | const Utf8Str& Medium::i_getFormat() const
|
---|
3779 | {
|
---|
3780 | return m->strFormat;
|
---|
3781 | }
|
---|
3782 |
|
---|
3783 | /**
|
---|
3784 | * Internal method to return the medium's format object. Must have caller + locking!
|
---|
3785 | * @return
|
---|
3786 | */
|
---|
3787 | const ComObjPtr<MediumFormat>& Medium::i_getMediumFormat() const
|
---|
3788 | {
|
---|
3789 | return m->formatObj;
|
---|
3790 | }
|
---|
3791 |
|
---|
3792 | /**
|
---|
3793 | * Internal method that returns true if the medium is represented by a file on the host disk
|
---|
3794 | * (and not iSCSI or something).
|
---|
3795 | * @return
|
---|
3796 | */
|
---|
3797 | bool Medium::i_isMediumFormatFile() const
|
---|
3798 | {
|
---|
3799 | if ( m->formatObj
|
---|
3800 | && (m->formatObj->i_getCapabilities() & MediumFormatCapabilities_File)
|
---|
3801 | )
|
---|
3802 | return true;
|
---|
3803 | return false;
|
---|
3804 | }
|
---|
3805 |
|
---|
3806 | /**
|
---|
3807 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
3808 | * @return
|
---|
3809 | */
|
---|
3810 | uint64_t Medium::i_getSize() const
|
---|
3811 | {
|
---|
3812 | return m->size;
|
---|
3813 | }
|
---|
3814 |
|
---|
3815 | /**
|
---|
3816 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
3817 | * @return
|
---|
3818 | */
|
---|
3819 | uint64_t Medium::i_getLogicalSize() const
|
---|
3820 | {
|
---|
3821 | return m->logicalSize;
|
---|
3822 | }
|
---|
3823 |
|
---|
3824 | /**
|
---|
3825 | * Returns the medium device type. Must have caller + locking!
|
---|
3826 | * @return
|
---|
3827 | */
|
---|
3828 | DeviceType_T Medium::i_getDeviceType() const
|
---|
3829 | {
|
---|
3830 | return m->devType;
|
---|
3831 | }
|
---|
3832 |
|
---|
3833 | /**
|
---|
3834 | * Returns the medium type. Must have caller + locking!
|
---|
3835 | * @return
|
---|
3836 | */
|
---|
3837 | MediumType_T Medium::i_getType() const
|
---|
3838 | {
|
---|
3839 | return m->type;
|
---|
3840 | }
|
---|
3841 |
|
---|
3842 | /**
|
---|
3843 | * Returns a short version of the location attribute.
|
---|
3844 | *
|
---|
3845 | * @note Must be called from under this object's read or write lock.
|
---|
3846 | */
|
---|
3847 | Utf8Str Medium::i_getName()
|
---|
3848 | {
|
---|
3849 | Utf8Str name = RTPathFilename(m->strLocationFull.c_str());
|
---|
3850 | return name;
|
---|
3851 | }
|
---|
3852 |
|
---|
3853 | /**
|
---|
3854 | * This adds the given UUID to the list of media registries in which this
|
---|
3855 | * medium should be registered. The UUID can either be a machine UUID,
|
---|
3856 | * to add a machine registry, or the global registry UUID as returned by
|
---|
3857 | * VirtualBox::getGlobalRegistryId().
|
---|
3858 | *
|
---|
3859 | * Note that for hard disks, this method does nothing if the medium is
|
---|
3860 | * already in another registry to avoid having hard disks in more than
|
---|
3861 | * one registry, which causes trouble with keeping diff images in sync.
|
---|
3862 | * See getFirstRegistryMachineId() for details.
|
---|
3863 | *
|
---|
3864 | * @param id
|
---|
3865 | * @return true if the registry was added; false if the given id was already on the list.
|
---|
3866 | */
|
---|
3867 | bool Medium::i_addRegistry(const Guid& id)
|
---|
3868 | {
|
---|
3869 | AutoCaller autoCaller(this);
|
---|
3870 | if (FAILED(autoCaller.rc()))
|
---|
3871 | return false;
|
---|
3872 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3873 |
|
---|
3874 | bool fAdd = true;
|
---|
3875 |
|
---|
3876 | // hard disks cannot be in more than one registry
|
---|
3877 | if ( m->devType == DeviceType_HardDisk
|
---|
3878 | && m->llRegistryIDs.size() > 0)
|
---|
3879 | fAdd = false;
|
---|
3880 |
|
---|
3881 | // no need to add the UUID twice
|
---|
3882 | if (fAdd)
|
---|
3883 | {
|
---|
3884 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
3885 | it != m->llRegistryIDs.end();
|
---|
3886 | ++it)
|
---|
3887 | {
|
---|
3888 | if ((*it) == id)
|
---|
3889 | {
|
---|
3890 | fAdd = false;
|
---|
3891 | break;
|
---|
3892 | }
|
---|
3893 | }
|
---|
3894 | }
|
---|
3895 |
|
---|
3896 | if (fAdd)
|
---|
3897 | m->llRegistryIDs.push_back(id);
|
---|
3898 |
|
---|
3899 | return fAdd;
|
---|
3900 | }
|
---|
3901 |
|
---|
3902 | /**
|
---|
3903 | * This adds the given UUID to the list of media registries in which this
|
---|
3904 | * medium should be registered. The UUID can either be a machine UUID,
|
---|
3905 | * to add a machine registry, or the global registry UUID as returned by
|
---|
3906 | * VirtualBox::getGlobalRegistryId(). This recurses over all children.
|
---|
3907 | *
|
---|
3908 | * Note that for hard disks, this method does nothing if the medium is
|
---|
3909 | * already in another registry to avoid having hard disks in more than
|
---|
3910 | * one registry, which causes trouble with keeping diff images in sync.
|
---|
3911 | * See getFirstRegistryMachineId() for details.
|
---|
3912 | *
|
---|
3913 | * @note the caller must hold the media tree lock for reading.
|
---|
3914 | *
|
---|
3915 | * @param id
|
---|
3916 | * @return true if the registry was added; false if the given id was already on the list.
|
---|
3917 | */
|
---|
3918 | bool Medium::i_addRegistryRecursive(const Guid &id)
|
---|
3919 | {
|
---|
3920 | AutoCaller autoCaller(this);
|
---|
3921 | if (FAILED(autoCaller.rc()))
|
---|
3922 | return false;
|
---|
3923 |
|
---|
3924 | bool fAdd = i_addRegistry(id);
|
---|
3925 |
|
---|
3926 | // protected by the medium tree lock held by our original caller
|
---|
3927 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
3928 | it != i_getChildren().end();
|
---|
3929 | ++it)
|
---|
3930 | {
|
---|
3931 | Medium *pChild = *it;
|
---|
3932 | fAdd |= pChild->i_addRegistryRecursive(id);
|
---|
3933 | }
|
---|
3934 |
|
---|
3935 | return fAdd;
|
---|
3936 | }
|
---|
3937 |
|
---|
3938 | /**
|
---|
3939 | * Removes the given UUID from the list of media registry UUIDs of this medium.
|
---|
3940 | *
|
---|
3941 | * @param id
|
---|
3942 | * @return true if the UUID was found or false if not.
|
---|
3943 | */
|
---|
3944 | bool Medium::i_removeRegistry(const Guid &id)
|
---|
3945 | {
|
---|
3946 | AutoCaller autoCaller(this);
|
---|
3947 | if (FAILED(autoCaller.rc()))
|
---|
3948 | return false;
|
---|
3949 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
3950 |
|
---|
3951 | bool fRemove = false;
|
---|
3952 |
|
---|
3953 | /// @todo r=klaus eliminate this code, replace it by using find.
|
---|
3954 | for (GuidList::iterator it = m->llRegistryIDs.begin();
|
---|
3955 | it != m->llRegistryIDs.end();
|
---|
3956 | ++it)
|
---|
3957 | {
|
---|
3958 | if ((*it) == id)
|
---|
3959 | {
|
---|
3960 | // getting away with this as the iterator isn't used after
|
---|
3961 | m->llRegistryIDs.erase(it);
|
---|
3962 | fRemove = true;
|
---|
3963 | break;
|
---|
3964 | }
|
---|
3965 | }
|
---|
3966 |
|
---|
3967 | return fRemove;
|
---|
3968 | }
|
---|
3969 |
|
---|
3970 | /**
|
---|
3971 | * Removes the given UUID from the list of media registry UUIDs, for this
|
---|
3972 | * medium and all its children recursively.
|
---|
3973 | *
|
---|
3974 | * @note the caller must hold the media tree lock for reading.
|
---|
3975 | *
|
---|
3976 | * @param id
|
---|
3977 | * @return true if the UUID was found or false if not.
|
---|
3978 | */
|
---|
3979 | bool Medium::i_removeRegistryRecursive(const Guid &id)
|
---|
3980 | {
|
---|
3981 | AutoCaller autoCaller(this);
|
---|
3982 | if (FAILED(autoCaller.rc()))
|
---|
3983 | return false;
|
---|
3984 |
|
---|
3985 | bool fRemove = i_removeRegistry(id);
|
---|
3986 |
|
---|
3987 | // protected by the medium tree lock held by our original caller
|
---|
3988 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
3989 | it != i_getChildren().end();
|
---|
3990 | ++it)
|
---|
3991 | {
|
---|
3992 | Medium *pChild = *it;
|
---|
3993 | fRemove |= pChild->i_removeRegistryRecursive(id);
|
---|
3994 | }
|
---|
3995 |
|
---|
3996 | return fRemove;
|
---|
3997 | }
|
---|
3998 |
|
---|
3999 | /**
|
---|
4000 | * Returns true if id is in the list of media registries for this medium.
|
---|
4001 | *
|
---|
4002 | * Must have caller + read locking!
|
---|
4003 | *
|
---|
4004 | * @param id
|
---|
4005 | * @return
|
---|
4006 | */
|
---|
4007 | bool Medium::i_isInRegistry(const Guid &id)
|
---|
4008 | {
|
---|
4009 | /// @todo r=klaus eliminate this code, replace it by using find.
|
---|
4010 | for (GuidList::const_iterator it = m->llRegistryIDs.begin();
|
---|
4011 | it != m->llRegistryIDs.end();
|
---|
4012 | ++it)
|
---|
4013 | {
|
---|
4014 | if (*it == id)
|
---|
4015 | return true;
|
---|
4016 | }
|
---|
4017 |
|
---|
4018 | return false;
|
---|
4019 | }
|
---|
4020 |
|
---|
4021 | /**
|
---|
4022 | * Internal method to return the medium's first registry machine (i.e. the machine in whose
|
---|
4023 | * machine XML this medium is listed).
|
---|
4024 | *
|
---|
4025 | * Every attached medium must now (4.0) reside in at least one media registry, which is identified
|
---|
4026 | * by a UUID. This is either a machine UUID if the machine is from 4.0 or newer, in which case
|
---|
4027 | * machines have their own media registries, or it is the pseudo-UUID of the VirtualBox
|
---|
4028 | * object if the machine is old and still needs the global registry in VirtualBox.xml.
|
---|
4029 | *
|
---|
4030 | * By definition, hard disks may only be in one media registry, in which all its children
|
---|
4031 | * will be stored as well. Otherwise we run into problems with having keep multiple registries
|
---|
4032 | * in sync. (This is the "cloned VM" case in which VM1 may link to the disks of VM2; in this
|
---|
4033 | * case, only VM2's registry is used for the disk in question.)
|
---|
4034 | *
|
---|
4035 | * If there is no medium registry, particularly if the medium has not been attached yet, this
|
---|
4036 | * does not modify uuid and returns false.
|
---|
4037 | *
|
---|
4038 | * ISOs and RAWs, by contrast, can be in more than one repository to make things easier for
|
---|
4039 | * the user.
|
---|
4040 | *
|
---|
4041 | * Must have caller + locking!
|
---|
4042 | *
|
---|
4043 | * @param uuid Receives first registry machine UUID, if available.
|
---|
4044 | * @return true if uuid was set.
|
---|
4045 | */
|
---|
4046 | bool Medium::i_getFirstRegistryMachineId(Guid &uuid) const
|
---|
4047 | {
|
---|
4048 | if (m->llRegistryIDs.size())
|
---|
4049 | {
|
---|
4050 | uuid = m->llRegistryIDs.front();
|
---|
4051 | return true;
|
---|
4052 | }
|
---|
4053 | return false;
|
---|
4054 | }
|
---|
4055 |
|
---|
4056 | /**
|
---|
4057 | * Marks all the registries in which this medium is registered as modified.
|
---|
4058 | */
|
---|
4059 | void Medium::i_markRegistriesModified()
|
---|
4060 | {
|
---|
4061 | AutoCaller autoCaller(this);
|
---|
4062 | if (FAILED(autoCaller.rc())) return;
|
---|
4063 |
|
---|
4064 | // Get local copy, as keeping the lock over VirtualBox::markRegistryModified
|
---|
4065 | // causes trouble with the lock order
|
---|
4066 | GuidList llRegistryIDs;
|
---|
4067 | {
|
---|
4068 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4069 | llRegistryIDs = m->llRegistryIDs;
|
---|
4070 | }
|
---|
4071 |
|
---|
4072 | autoCaller.release();
|
---|
4073 |
|
---|
4074 | /* Save the error information now, the implicit restore when this goes
|
---|
4075 | * out of scope will throw away spurious additional errors created below. */
|
---|
4076 | ErrorInfoKeeper eik;
|
---|
4077 | for (GuidList::const_iterator it = llRegistryIDs.begin();
|
---|
4078 | it != llRegistryIDs.end();
|
---|
4079 | ++it)
|
---|
4080 | {
|
---|
4081 | m->pVirtualBox->i_markRegistryModified(*it);
|
---|
4082 | }
|
---|
4083 | }
|
---|
4084 |
|
---|
4085 | /**
|
---|
4086 | * Adds the given machine and optionally the snapshot to the list of the objects
|
---|
4087 | * this medium is attached to.
|
---|
4088 | *
|
---|
4089 | * @param aMachineId Machine ID.
|
---|
4090 | * @param aSnapshotId Snapshot ID; when non-empty, adds a snapshot attachment.
|
---|
4091 | */
|
---|
4092 | HRESULT Medium::i_addBackReference(const Guid &aMachineId,
|
---|
4093 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
4094 | {
|
---|
4095 | AssertReturn(aMachineId.isValid(), E_FAIL);
|
---|
4096 |
|
---|
4097 | LogFlowThisFunc(("ENTER, aMachineId: {%RTuuid}, aSnapshotId: {%RTuuid}\n", aMachineId.raw(), aSnapshotId.raw()));
|
---|
4098 |
|
---|
4099 | AutoCaller autoCaller(this);
|
---|
4100 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4101 |
|
---|
4102 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4103 |
|
---|
4104 | switch (m->state)
|
---|
4105 | {
|
---|
4106 | case MediumState_Created:
|
---|
4107 | case MediumState_Inaccessible:
|
---|
4108 | case MediumState_LockedRead:
|
---|
4109 | case MediumState_LockedWrite:
|
---|
4110 | break;
|
---|
4111 |
|
---|
4112 | default:
|
---|
4113 | return i_setStateError();
|
---|
4114 | }
|
---|
4115 |
|
---|
4116 | if (m->numCreateDiffTasks > 0)
|
---|
4117 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4118 | tr("Cannot attach medium '%s' {%RTuuid}: %u differencing child media are being created"),
|
---|
4119 | m->strLocationFull.c_str(),
|
---|
4120 | m->id.raw(),
|
---|
4121 | m->numCreateDiffTasks);
|
---|
4122 |
|
---|
4123 | BackRefList::iterator it = std::find_if(m->backRefs.begin(),
|
---|
4124 | m->backRefs.end(),
|
---|
4125 | BackRef::EqualsTo(aMachineId));
|
---|
4126 | if (it == m->backRefs.end())
|
---|
4127 | {
|
---|
4128 | BackRef ref(aMachineId, aSnapshotId);
|
---|
4129 | m->backRefs.push_back(ref);
|
---|
4130 |
|
---|
4131 | return S_OK;
|
---|
4132 | }
|
---|
4133 |
|
---|
4134 | // if the caller has not supplied a snapshot ID, then we're attaching
|
---|
4135 | // to a machine a medium which represents the machine's current state,
|
---|
4136 | // so set the flag
|
---|
4137 |
|
---|
4138 | if (aSnapshotId.isZero())
|
---|
4139 | {
|
---|
4140 | /* sanity: no duplicate attachments */
|
---|
4141 | if (it->fInCurState)
|
---|
4142 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4143 | tr("Cannot attach medium '%s' {%RTuuid}: medium is already associated with the current state of machine uuid {%RTuuid}!"),
|
---|
4144 | m->strLocationFull.c_str(),
|
---|
4145 | m->id.raw(),
|
---|
4146 | aMachineId.raw());
|
---|
4147 | it->fInCurState = true;
|
---|
4148 |
|
---|
4149 | return S_OK;
|
---|
4150 | }
|
---|
4151 |
|
---|
4152 | // otherwise: a snapshot medium is being attached
|
---|
4153 |
|
---|
4154 | /* sanity: no duplicate attachments */
|
---|
4155 | for (GuidList::const_iterator jt = it->llSnapshotIds.begin();
|
---|
4156 | jt != it->llSnapshotIds.end();
|
---|
4157 | ++jt)
|
---|
4158 | {
|
---|
4159 | const Guid &idOldSnapshot = *jt;
|
---|
4160 |
|
---|
4161 | if (idOldSnapshot == aSnapshotId)
|
---|
4162 | {
|
---|
4163 | #ifdef DEBUG
|
---|
4164 | i_dumpBackRefs();
|
---|
4165 | #endif
|
---|
4166 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4167 | tr("Cannot attach medium '%s' {%RTuuid} from snapshot '%RTuuid': medium is already in use by this snapshot!"),
|
---|
4168 | m->strLocationFull.c_str(),
|
---|
4169 | m->id.raw(),
|
---|
4170 | aSnapshotId.raw());
|
---|
4171 | }
|
---|
4172 | }
|
---|
4173 |
|
---|
4174 | it->llSnapshotIds.push_back(aSnapshotId);
|
---|
4175 | // Do not touch fInCurState, as the image may be attached to the current
|
---|
4176 | // state *and* a snapshot, otherwise we lose the current state association!
|
---|
4177 |
|
---|
4178 | LogFlowThisFuncLeave();
|
---|
4179 |
|
---|
4180 | return S_OK;
|
---|
4181 | }
|
---|
4182 |
|
---|
4183 | /**
|
---|
4184 | * Removes the given machine and optionally the snapshot from the list of the
|
---|
4185 | * objects this medium is attached to.
|
---|
4186 | *
|
---|
4187 | * @param aMachineId Machine ID.
|
---|
4188 | * @param aSnapshotId Snapshot ID; when non-empty, removes the snapshot
|
---|
4189 | * attachment.
|
---|
4190 | */
|
---|
4191 | HRESULT Medium::i_removeBackReference(const Guid &aMachineId,
|
---|
4192 | const Guid &aSnapshotId /*= Guid::Empty*/)
|
---|
4193 | {
|
---|
4194 | AssertReturn(aMachineId.isValid(), E_FAIL);
|
---|
4195 |
|
---|
4196 | AutoCaller autoCaller(this);
|
---|
4197 | AssertComRCReturnRC(autoCaller.rc());
|
---|
4198 |
|
---|
4199 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4200 |
|
---|
4201 | BackRefList::iterator it =
|
---|
4202 | std::find_if(m->backRefs.begin(), m->backRefs.end(),
|
---|
4203 | BackRef::EqualsTo(aMachineId));
|
---|
4204 | AssertReturn(it != m->backRefs.end(), E_FAIL);
|
---|
4205 |
|
---|
4206 | if (aSnapshotId.isZero())
|
---|
4207 | {
|
---|
4208 | /* remove the current state attachment */
|
---|
4209 | it->fInCurState = false;
|
---|
4210 | }
|
---|
4211 | else
|
---|
4212 | {
|
---|
4213 | /* remove the snapshot attachment */
|
---|
4214 | GuidList::iterator jt = std::find(it->llSnapshotIds.begin(),
|
---|
4215 | it->llSnapshotIds.end(),
|
---|
4216 | aSnapshotId);
|
---|
4217 |
|
---|
4218 | AssertReturn(jt != it->llSnapshotIds.end(), E_FAIL);
|
---|
4219 | it->llSnapshotIds.erase(jt);
|
---|
4220 | }
|
---|
4221 |
|
---|
4222 | /* if the backref becomes empty, remove it */
|
---|
4223 | if (it->fInCurState == false && it->llSnapshotIds.size() == 0)
|
---|
4224 | m->backRefs.erase(it);
|
---|
4225 |
|
---|
4226 | return S_OK;
|
---|
4227 | }
|
---|
4228 |
|
---|
4229 | /**
|
---|
4230 | * Internal method to return the medium's list of backrefs. Must have caller + locking!
|
---|
4231 | * @return
|
---|
4232 | */
|
---|
4233 | const Guid* Medium::i_getFirstMachineBackrefId() const
|
---|
4234 | {
|
---|
4235 | if (!m->backRefs.size())
|
---|
4236 | return NULL;
|
---|
4237 |
|
---|
4238 | return &m->backRefs.front().machineId;
|
---|
4239 | }
|
---|
4240 |
|
---|
4241 | /**
|
---|
4242 | * Internal method which returns a machine that either this medium or one of its children
|
---|
4243 | * is attached to. This is used for finding a replacement media registry when an existing
|
---|
4244 | * media registry is about to be deleted in VirtualBox::unregisterMachine().
|
---|
4245 | *
|
---|
4246 | * Must have caller + locking, *and* caller must hold the media tree lock!
|
---|
4247 | * @return
|
---|
4248 | */
|
---|
4249 | const Guid* Medium::i_getAnyMachineBackref() const
|
---|
4250 | {
|
---|
4251 | if (m->backRefs.size())
|
---|
4252 | return &m->backRefs.front().machineId;
|
---|
4253 |
|
---|
4254 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
4255 | it != i_getChildren().end();
|
---|
4256 | ++it)
|
---|
4257 | {
|
---|
4258 | Medium *pChild = *it;
|
---|
4259 | // recurse for this child
|
---|
4260 | const Guid* puuid;
|
---|
4261 | if ((puuid = pChild->i_getAnyMachineBackref()))
|
---|
4262 | return puuid;
|
---|
4263 | }
|
---|
4264 |
|
---|
4265 | return NULL;
|
---|
4266 | }
|
---|
4267 |
|
---|
4268 | const Guid* Medium::i_getFirstMachineBackrefSnapshotId() const
|
---|
4269 | {
|
---|
4270 | if (!m->backRefs.size())
|
---|
4271 | return NULL;
|
---|
4272 |
|
---|
4273 | const BackRef &ref = m->backRefs.front();
|
---|
4274 | if (ref.llSnapshotIds.empty())
|
---|
4275 | return NULL;
|
---|
4276 |
|
---|
4277 | return &ref.llSnapshotIds.front();
|
---|
4278 | }
|
---|
4279 |
|
---|
4280 | size_t Medium::i_getMachineBackRefCount() const
|
---|
4281 | {
|
---|
4282 | return m->backRefs.size();
|
---|
4283 | }
|
---|
4284 |
|
---|
4285 | #ifdef DEBUG
|
---|
4286 | /**
|
---|
4287 | * Debugging helper that gets called after VirtualBox initialization that writes all
|
---|
4288 | * machine backreferences to the debug log.
|
---|
4289 | */
|
---|
4290 | void Medium::i_dumpBackRefs()
|
---|
4291 | {
|
---|
4292 | AutoCaller autoCaller(this);
|
---|
4293 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4294 |
|
---|
4295 | LogFlowThisFunc(("Dumping backrefs for medium '%s':\n", m->strLocationFull.c_str()));
|
---|
4296 |
|
---|
4297 | for (BackRefList::iterator it2 = m->backRefs.begin();
|
---|
4298 | it2 != m->backRefs.end();
|
---|
4299 | ++it2)
|
---|
4300 | {
|
---|
4301 | const BackRef &ref = *it2;
|
---|
4302 | LogFlowThisFunc((" Backref from machine {%RTuuid} (fInCurState: %d)\n", ref.machineId.raw(), ref.fInCurState));
|
---|
4303 |
|
---|
4304 | for (GuidList::const_iterator jt2 = it2->llSnapshotIds.begin();
|
---|
4305 | jt2 != it2->llSnapshotIds.end();
|
---|
4306 | ++jt2)
|
---|
4307 | {
|
---|
4308 | const Guid &id = *jt2;
|
---|
4309 | LogFlowThisFunc((" Backref from snapshot {%RTuuid}\n", id.raw()));
|
---|
4310 | }
|
---|
4311 | }
|
---|
4312 | }
|
---|
4313 | #endif
|
---|
4314 |
|
---|
4315 | /**
|
---|
4316 | * Checks if the given change of \a aOldPath to \a aNewPath affects the location
|
---|
4317 | * of this media and updates it if necessary to reflect the new location.
|
---|
4318 | *
|
---|
4319 | * @param strOldPath Old path (full).
|
---|
4320 | * @param strNewPath New path (full).
|
---|
4321 | *
|
---|
4322 | * @note Locks this object for writing.
|
---|
4323 | */
|
---|
4324 | HRESULT Medium::i_updatePath(const Utf8Str &strOldPath, const Utf8Str &strNewPath)
|
---|
4325 | {
|
---|
4326 | AssertReturn(!strOldPath.isEmpty(), E_FAIL);
|
---|
4327 | AssertReturn(!strNewPath.isEmpty(), E_FAIL);
|
---|
4328 |
|
---|
4329 | AutoCaller autoCaller(this);
|
---|
4330 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4331 |
|
---|
4332 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4333 |
|
---|
4334 | LogFlowThisFunc(("locationFull.before='%s'\n", m->strLocationFull.c_str()));
|
---|
4335 |
|
---|
4336 | const char *pcszMediumPath = m->strLocationFull.c_str();
|
---|
4337 |
|
---|
4338 | if (RTPathStartsWith(pcszMediumPath, strOldPath.c_str()))
|
---|
4339 | {
|
---|
4340 | Utf8Str newPath(strNewPath);
|
---|
4341 | newPath.append(pcszMediumPath + strOldPath.length());
|
---|
4342 | unconst(m->strLocationFull) = newPath;
|
---|
4343 |
|
---|
4344 | LogFlowThisFunc(("locationFull.after='%s'\n", m->strLocationFull.c_str()));
|
---|
4345 | // we changed something
|
---|
4346 | return S_OK;
|
---|
4347 | }
|
---|
4348 |
|
---|
4349 | // no change was necessary, signal error which the caller needs to interpret
|
---|
4350 | return VBOX_E_FILE_ERROR;
|
---|
4351 | }
|
---|
4352 |
|
---|
4353 | /**
|
---|
4354 | * Returns the base medium of the media chain this medium is part of.
|
---|
4355 | *
|
---|
4356 | * The base medium is found by walking up the parent-child relationship axis.
|
---|
4357 | * If the medium doesn't have a parent (i.e. it's a base medium), it
|
---|
4358 | * returns itself in response to this method.
|
---|
4359 | *
|
---|
4360 | * @param aLevel Where to store the number of ancestors of this medium
|
---|
4361 | * (zero for the base), may be @c NULL.
|
---|
4362 | *
|
---|
4363 | * @note Locks medium tree for reading.
|
---|
4364 | */
|
---|
4365 | ComObjPtr<Medium> Medium::i_getBase(uint32_t *aLevel /*= NULL*/)
|
---|
4366 | {
|
---|
4367 | ComObjPtr<Medium> pBase;
|
---|
4368 |
|
---|
4369 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4370 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4371 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4372 | if (!pVirtualBox)
|
---|
4373 | return pBase;
|
---|
4374 |
|
---|
4375 | /* we access m->pParent */
|
---|
4376 | AutoReadLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4377 |
|
---|
4378 | AutoCaller autoCaller(this);
|
---|
4379 | AssertReturn(autoCaller.isOk(), pBase);
|
---|
4380 |
|
---|
4381 | pBase = this;
|
---|
4382 | uint32_t level = 0;
|
---|
4383 |
|
---|
4384 | if (m->pParent)
|
---|
4385 | {
|
---|
4386 | for (;;)
|
---|
4387 | {
|
---|
4388 | AutoCaller baseCaller(pBase);
|
---|
4389 | AssertReturn(baseCaller.isOk(), pBase);
|
---|
4390 |
|
---|
4391 | if (pBase->m->pParent.isNull())
|
---|
4392 | break;
|
---|
4393 |
|
---|
4394 | pBase = pBase->m->pParent;
|
---|
4395 | ++level;
|
---|
4396 | }
|
---|
4397 | }
|
---|
4398 |
|
---|
4399 | if (aLevel != NULL)
|
---|
4400 | *aLevel = level;
|
---|
4401 |
|
---|
4402 | return pBase;
|
---|
4403 | }
|
---|
4404 |
|
---|
4405 | /**
|
---|
4406 | * Returns the depth of this medium in the media chain.
|
---|
4407 | *
|
---|
4408 | * @note Locks medium tree for reading.
|
---|
4409 | */
|
---|
4410 | uint32_t Medium::i_getDepth()
|
---|
4411 | {
|
---|
4412 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4413 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4414 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4415 | if (!pVirtualBox)
|
---|
4416 | return 1;
|
---|
4417 |
|
---|
4418 | /* we access m->pParent */
|
---|
4419 | AutoReadLock treeLock(pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4420 |
|
---|
4421 | uint32_t cDepth = 0;
|
---|
4422 | ComObjPtr<Medium> pMedium(this);
|
---|
4423 | while (!pMedium.isNull())
|
---|
4424 | {
|
---|
4425 | AutoCaller autoCaller(this);
|
---|
4426 | AssertReturn(autoCaller.isOk(), cDepth + 1);
|
---|
4427 |
|
---|
4428 | pMedium = pMedium->m->pParent;
|
---|
4429 | cDepth++;
|
---|
4430 | }
|
---|
4431 |
|
---|
4432 | return cDepth;
|
---|
4433 | }
|
---|
4434 |
|
---|
4435 | /**
|
---|
4436 | * Returns @c true if this medium cannot be modified because it has
|
---|
4437 | * dependents (children) or is part of the snapshot. Related to the medium
|
---|
4438 | * type and posterity, not to the current media state.
|
---|
4439 | *
|
---|
4440 | * @note Locks this object and medium tree for reading.
|
---|
4441 | */
|
---|
4442 | bool Medium::i_isReadOnly()
|
---|
4443 | {
|
---|
4444 | /* it is possible that some previous/concurrent uninit has already cleared
|
---|
4445 | * the pVirtualBox reference, and in this case we don't need to continue */
|
---|
4446 | ComObjPtr<VirtualBox> pVirtualBox(m->pVirtualBox);
|
---|
4447 | if (!pVirtualBox)
|
---|
4448 | return false;
|
---|
4449 |
|
---|
4450 | /* we access children */
|
---|
4451 | AutoReadLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4452 |
|
---|
4453 | AutoCaller autoCaller(this);
|
---|
4454 | AssertComRCReturn(autoCaller.rc(), false);
|
---|
4455 |
|
---|
4456 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4457 |
|
---|
4458 | switch (m->type)
|
---|
4459 | {
|
---|
4460 | case MediumType_Normal:
|
---|
4461 | {
|
---|
4462 | if (i_getChildren().size() != 0)
|
---|
4463 | return true;
|
---|
4464 |
|
---|
4465 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
4466 | it != m->backRefs.end(); ++it)
|
---|
4467 | if (it->llSnapshotIds.size() != 0)
|
---|
4468 | return true;
|
---|
4469 |
|
---|
4470 | if (m->variant & MediumVariant_VmdkStreamOptimized)
|
---|
4471 | return true;
|
---|
4472 |
|
---|
4473 | return false;
|
---|
4474 | }
|
---|
4475 | case MediumType_Immutable:
|
---|
4476 | case MediumType_MultiAttach:
|
---|
4477 | return true;
|
---|
4478 | case MediumType_Writethrough:
|
---|
4479 | case MediumType_Shareable:
|
---|
4480 | case MediumType_Readonly: /* explicit readonly media has no diffs */
|
---|
4481 | return false;
|
---|
4482 | default:
|
---|
4483 | break;
|
---|
4484 | }
|
---|
4485 |
|
---|
4486 | AssertFailedReturn(false);
|
---|
4487 | }
|
---|
4488 |
|
---|
4489 | /**
|
---|
4490 | * Internal method to return the medium's size. Must have caller + locking!
|
---|
4491 | * @return
|
---|
4492 | */
|
---|
4493 | void Medium::i_updateId(const Guid &id)
|
---|
4494 | {
|
---|
4495 | unconst(m->id) = id;
|
---|
4496 | }
|
---|
4497 |
|
---|
4498 | /**
|
---|
4499 | * Saves the settings of one medium.
|
---|
4500 | *
|
---|
4501 | * @note Caller MUST take care of the medium tree lock and caller.
|
---|
4502 | *
|
---|
4503 | * @param data Settings struct to be updated.
|
---|
4504 | * @param strHardDiskFolder Folder for which paths should be relative.
|
---|
4505 | */
|
---|
4506 | void Medium::i_saveSettingsOne(settings::Medium &data, const Utf8Str &strHardDiskFolder)
|
---|
4507 | {
|
---|
4508 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4509 |
|
---|
4510 | data.uuid = m->id;
|
---|
4511 |
|
---|
4512 | // make path relative if needed
|
---|
4513 | if ( !strHardDiskFolder.isEmpty()
|
---|
4514 | && RTPathStartsWith(m->strLocationFull.c_str(), strHardDiskFolder.c_str())
|
---|
4515 | )
|
---|
4516 | data.strLocation = m->strLocationFull.substr(strHardDiskFolder.length() + 1);
|
---|
4517 | else
|
---|
4518 | data.strLocation = m->strLocationFull;
|
---|
4519 | data.strFormat = m->strFormat;
|
---|
4520 |
|
---|
4521 | /* optional, only for diffs, default is false */
|
---|
4522 | if (m->pParent)
|
---|
4523 | data.fAutoReset = m->autoReset;
|
---|
4524 | else
|
---|
4525 | data.fAutoReset = false;
|
---|
4526 |
|
---|
4527 | /* optional */
|
---|
4528 | data.strDescription = m->strDescription;
|
---|
4529 |
|
---|
4530 | /* optional properties */
|
---|
4531 | data.properties.clear();
|
---|
4532 |
|
---|
4533 | /* handle iSCSI initiator secrets transparently */
|
---|
4534 | bool fHaveInitiatorSecretEncrypted = false;
|
---|
4535 | Utf8Str strCiphertext;
|
---|
4536 | settings::StringsMap::const_iterator itPln = m->mapProperties.find("InitiatorSecret");
|
---|
4537 | if ( itPln != m->mapProperties.end()
|
---|
4538 | && !itPln->second.isEmpty())
|
---|
4539 | {
|
---|
4540 | /* Encrypt the plain secret. If that does not work (i.e. no or wrong settings key
|
---|
4541 | * specified), just use the encrypted secret (if there is any). */
|
---|
4542 | int rc = m->pVirtualBox->i_encryptSetting(itPln->second, &strCiphertext);
|
---|
4543 | if (RT_SUCCESS(rc))
|
---|
4544 | fHaveInitiatorSecretEncrypted = true;
|
---|
4545 | }
|
---|
4546 | for (settings::StringsMap::const_iterator it = m->mapProperties.begin();
|
---|
4547 | it != m->mapProperties.end();
|
---|
4548 | ++it)
|
---|
4549 | {
|
---|
4550 | /* only save properties that have non-default values */
|
---|
4551 | if (!it->second.isEmpty())
|
---|
4552 | {
|
---|
4553 | const Utf8Str &name = it->first;
|
---|
4554 | const Utf8Str &value = it->second;
|
---|
4555 | /* do NOT store the plain InitiatorSecret */
|
---|
4556 | if ( !fHaveInitiatorSecretEncrypted
|
---|
4557 | || !name.equals("InitiatorSecret"))
|
---|
4558 | data.properties[name] = value;
|
---|
4559 | }
|
---|
4560 | }
|
---|
4561 | if (fHaveInitiatorSecretEncrypted)
|
---|
4562 | data.properties["InitiatorSecretEncrypted"] = strCiphertext;
|
---|
4563 |
|
---|
4564 | /* only for base media */
|
---|
4565 | if (m->pParent.isNull())
|
---|
4566 | data.hdType = m->type;
|
---|
4567 | }
|
---|
4568 |
|
---|
4569 | /**
|
---|
4570 | * Saves medium data by putting it into the provided data structure.
|
---|
4571 | * Recurses over all children to save their settings, too.
|
---|
4572 | *
|
---|
4573 | * @param data Settings struct to be updated.
|
---|
4574 | * @param strHardDiskFolder Folder for which paths should be relative.
|
---|
4575 | *
|
---|
4576 | * @note Locks this object, medium tree and children for reading.
|
---|
4577 | */
|
---|
4578 | HRESULT Medium::i_saveSettings(settings::Medium &data,
|
---|
4579 | const Utf8Str &strHardDiskFolder)
|
---|
4580 | {
|
---|
4581 | /* we access m->pParent */
|
---|
4582 | AutoReadLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
4583 |
|
---|
4584 | AutoCaller autoCaller(this);
|
---|
4585 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4586 |
|
---|
4587 | i_saveSettingsOne(data, strHardDiskFolder);
|
---|
4588 |
|
---|
4589 | /* save all children */
|
---|
4590 | settings::MediaList &llSettingsChildren = data.llChildren;
|
---|
4591 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
4592 | it != i_getChildren().end();
|
---|
4593 | ++it)
|
---|
4594 | {
|
---|
4595 | // Use the element straight in the list to reduce both unnecessary
|
---|
4596 | // deep copying (when unwinding the recursion the entire medium
|
---|
4597 | // settings sub-tree is copied) and the stack footprint (the settings
|
---|
4598 | // need almost 1K, and there can be VMs with long image chains.
|
---|
4599 | llSettingsChildren.push_back(settings::Medium::Empty);
|
---|
4600 | HRESULT rc = (*it)->i_saveSettings(llSettingsChildren.back(), strHardDiskFolder);
|
---|
4601 | if (FAILED(rc))
|
---|
4602 | {
|
---|
4603 | llSettingsChildren.pop_back();
|
---|
4604 | return rc;
|
---|
4605 | }
|
---|
4606 | }
|
---|
4607 |
|
---|
4608 | return S_OK;
|
---|
4609 | }
|
---|
4610 |
|
---|
4611 | /**
|
---|
4612 | * Constructs a medium lock list for this medium. The lock is not taken.
|
---|
4613 | *
|
---|
4614 | * @note Caller MUST NOT hold the media tree or medium lock.
|
---|
4615 | *
|
---|
4616 | * @param fFailIfInaccessible If true, this fails with an error if a medium is inaccessible. If false,
|
---|
4617 | * inaccessible media are silently skipped and not locked (i.e. their state remains "Inaccessible");
|
---|
4618 | * this is necessary for a VM's removable media VM startup for which we do not want to fail.
|
---|
4619 | * @param pToLockWrite If not NULL, associate a write lock with this medium object.
|
---|
4620 | * @param fMediumLockWriteAll Whether to associate a write lock to all other media too.
|
---|
4621 | * @param pToBeParent Medium which will become the parent of this medium.
|
---|
4622 | * @param mediumLockList Where to store the resulting list.
|
---|
4623 | */
|
---|
4624 | HRESULT Medium::i_createMediumLockList(bool fFailIfInaccessible,
|
---|
4625 | Medium *pToLockWrite,
|
---|
4626 | bool fMediumLockWriteAll,
|
---|
4627 | Medium *pToBeParent,
|
---|
4628 | MediumLockList &mediumLockList)
|
---|
4629 | {
|
---|
4630 | /** @todo r=klaus this needs to be reworked, as the code below uses
|
---|
4631 | * i_getParent without holding the tree lock, and changing this is
|
---|
4632 | * a significant amount of effort. */
|
---|
4633 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
4634 | Assert(!isWriteLockOnCurrentThread());
|
---|
4635 |
|
---|
4636 | AutoCaller autoCaller(this);
|
---|
4637 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4638 |
|
---|
4639 | HRESULT rc = S_OK;
|
---|
4640 |
|
---|
4641 | /* paranoid sanity checking if the medium has a to-be parent medium */
|
---|
4642 | if (pToBeParent)
|
---|
4643 | {
|
---|
4644 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
4645 | ComAssertRet(i_getParent().isNull(), E_FAIL);
|
---|
4646 | ComAssertRet(i_getChildren().size() == 0, E_FAIL);
|
---|
4647 | }
|
---|
4648 |
|
---|
4649 | ErrorInfoKeeper eik;
|
---|
4650 | MultiResult mrc(S_OK);
|
---|
4651 |
|
---|
4652 | ComObjPtr<Medium> pMedium = this;
|
---|
4653 | while (!pMedium.isNull())
|
---|
4654 | {
|
---|
4655 | AutoReadLock alock(pMedium COMMA_LOCKVAL_SRC_POS);
|
---|
4656 |
|
---|
4657 | /* Accessibility check must be first, otherwise locking interferes
|
---|
4658 | * with getting the medium state. Lock lists are not created for
|
---|
4659 | * fun, and thus getting the medium status is no luxury. */
|
---|
4660 | MediumState_T mediumState = pMedium->i_getState();
|
---|
4661 | if (mediumState == MediumState_Inaccessible)
|
---|
4662 | {
|
---|
4663 | alock.release();
|
---|
4664 | rc = pMedium->i_queryInfo(false /* fSetImageId */, false /* fSetParentId */,
|
---|
4665 | autoCaller);
|
---|
4666 | alock.acquire();
|
---|
4667 | if (FAILED(rc)) return rc;
|
---|
4668 |
|
---|
4669 | mediumState = pMedium->i_getState();
|
---|
4670 | if (mediumState == MediumState_Inaccessible)
|
---|
4671 | {
|
---|
4672 | // ignore inaccessible ISO media and silently return S_OK,
|
---|
4673 | // otherwise VM startup (esp. restore) may fail without good reason
|
---|
4674 | if (!fFailIfInaccessible)
|
---|
4675 | return S_OK;
|
---|
4676 |
|
---|
4677 | // otherwise report an error
|
---|
4678 | Bstr error;
|
---|
4679 | rc = pMedium->COMGETTER(LastAccessError)(error.asOutParam());
|
---|
4680 | if (FAILED(rc)) return rc;
|
---|
4681 |
|
---|
4682 | /* collect multiple errors */
|
---|
4683 | eik.restore();
|
---|
4684 | Assert(!error.isEmpty());
|
---|
4685 | mrc = setError(E_FAIL,
|
---|
4686 | "%ls",
|
---|
4687 | error.raw());
|
---|
4688 | // error message will be something like
|
---|
4689 | // "Could not open the medium ... VD: error VERR_FILE_NOT_FOUND opening image file ... (VERR_FILE_NOT_FOUND).
|
---|
4690 | eik.fetch();
|
---|
4691 | }
|
---|
4692 | }
|
---|
4693 |
|
---|
4694 | if (pMedium == pToLockWrite)
|
---|
4695 | mediumLockList.Prepend(pMedium, true);
|
---|
4696 | else
|
---|
4697 | mediumLockList.Prepend(pMedium, fMediumLockWriteAll);
|
---|
4698 |
|
---|
4699 | pMedium = pMedium->i_getParent();
|
---|
4700 | if (pMedium.isNull() && pToBeParent)
|
---|
4701 | {
|
---|
4702 | pMedium = pToBeParent;
|
---|
4703 | pToBeParent = NULL;
|
---|
4704 | }
|
---|
4705 | }
|
---|
4706 |
|
---|
4707 | return mrc;
|
---|
4708 | }
|
---|
4709 |
|
---|
4710 | /**
|
---|
4711 | * Creates a new differencing storage unit using the format of the given target
|
---|
4712 | * medium and the location. Note that @c aTarget must be NotCreated.
|
---|
4713 | *
|
---|
4714 | * The @a aMediumLockList parameter contains the associated medium lock list,
|
---|
4715 | * which must be in locked state. If @a aWait is @c true then the caller is
|
---|
4716 | * responsible for unlocking.
|
---|
4717 | *
|
---|
4718 | * If @a aProgress is not NULL but the object it points to is @c null then a
|
---|
4719 | * new progress object will be created and assigned to @a *aProgress on
|
---|
4720 | * success, otherwise the existing progress object is used. If @a aProgress is
|
---|
4721 | * NULL, then no progress object is created/used at all.
|
---|
4722 | *
|
---|
4723 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
4724 | * create operation asynchronously and will return immediately. Otherwise, it
|
---|
4725 | * will perform the operation on the calling thread and will not return to the
|
---|
4726 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
4727 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
4728 | *
|
---|
4729 | * @param aTarget Target medium.
|
---|
4730 | * @param aVariant Precise medium variant to create.
|
---|
4731 | * @param aMediumLockList List of media which should be locked.
|
---|
4732 | * @param aProgress Where to find/store a Progress object to track
|
---|
4733 | * operation completion.
|
---|
4734 | * @param aWait @c true if this method should block instead of
|
---|
4735 | * creating an asynchronous thread.
|
---|
4736 | *
|
---|
4737 | * @note Locks this object and @a aTarget for writing.
|
---|
4738 | */
|
---|
4739 | HRESULT Medium::i_createDiffStorage(ComObjPtr<Medium> &aTarget,
|
---|
4740 | MediumVariant_T aVariant,
|
---|
4741 | MediumLockList *aMediumLockList,
|
---|
4742 | ComObjPtr<Progress> *aProgress,
|
---|
4743 | bool aWait)
|
---|
4744 | {
|
---|
4745 | AssertReturn(!aTarget.isNull(), E_FAIL);
|
---|
4746 | AssertReturn(aMediumLockList, E_FAIL);
|
---|
4747 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
4748 |
|
---|
4749 | AutoCaller autoCaller(this);
|
---|
4750 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4751 |
|
---|
4752 | AutoCaller targetCaller(aTarget);
|
---|
4753 | if (FAILED(targetCaller.rc())) return targetCaller.rc();
|
---|
4754 |
|
---|
4755 | HRESULT rc = S_OK;
|
---|
4756 | ComObjPtr<Progress> pProgress;
|
---|
4757 | Medium::Task *pTask = NULL;
|
---|
4758 |
|
---|
4759 | try
|
---|
4760 | {
|
---|
4761 | AutoMultiWriteLock2 alock(this, aTarget COMMA_LOCKVAL_SRC_POS);
|
---|
4762 |
|
---|
4763 | ComAssertThrow( m->type != MediumType_Writethrough
|
---|
4764 | && m->type != MediumType_Shareable
|
---|
4765 | && m->type != MediumType_Readonly, E_FAIL);
|
---|
4766 | ComAssertThrow(m->state == MediumState_LockedRead, E_FAIL);
|
---|
4767 |
|
---|
4768 | if (aTarget->m->state != MediumState_NotCreated)
|
---|
4769 | throw aTarget->i_setStateError();
|
---|
4770 |
|
---|
4771 | /* Check that the medium is not attached to the current state of
|
---|
4772 | * any VM referring to it. */
|
---|
4773 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
4774 | it != m->backRefs.end();
|
---|
4775 | ++it)
|
---|
4776 | {
|
---|
4777 | if (it->fInCurState)
|
---|
4778 | {
|
---|
4779 | /* Note: when a VM snapshot is being taken, all normal media
|
---|
4780 | * attached to the VM in the current state will be, as an
|
---|
4781 | * exception, also associated with the snapshot which is about
|
---|
4782 | * to create (see SnapshotMachine::init()) before deassociating
|
---|
4783 | * them from the current state (which takes place only on
|
---|
4784 | * success in Machine::fixupHardDisks()), so that the size of
|
---|
4785 | * snapshotIds will be 1 in this case. The extra condition is
|
---|
4786 | * used to filter out this legal situation. */
|
---|
4787 | if (it->llSnapshotIds.size() == 0)
|
---|
4788 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
4789 | 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"),
|
---|
4790 | m->strLocationFull.c_str(), it->machineId.raw());
|
---|
4791 |
|
---|
4792 | Assert(it->llSnapshotIds.size() == 1);
|
---|
4793 | }
|
---|
4794 | }
|
---|
4795 |
|
---|
4796 | if (aProgress != NULL)
|
---|
4797 | {
|
---|
4798 | /* use the existing progress object... */
|
---|
4799 | pProgress = *aProgress;
|
---|
4800 |
|
---|
4801 | /* ...but create a new one if it is null */
|
---|
4802 | if (pProgress.isNull())
|
---|
4803 | {
|
---|
4804 | pProgress.createObject();
|
---|
4805 | rc = pProgress->init(m->pVirtualBox,
|
---|
4806 | static_cast<IMedium*>(this),
|
---|
4807 | BstrFmt(tr("Creating differencing medium storage unit '%s'"),
|
---|
4808 | aTarget->m->strLocationFull.c_str()).raw(),
|
---|
4809 | TRUE /* aCancelable */);
|
---|
4810 | if (FAILED(rc))
|
---|
4811 | throw rc;
|
---|
4812 | }
|
---|
4813 | }
|
---|
4814 |
|
---|
4815 | /* setup task object to carry out the operation sync/async */
|
---|
4816 | pTask = new Medium::CreateDiffTask(this, pProgress, aTarget, aVariant,
|
---|
4817 | aMediumLockList,
|
---|
4818 | aWait /* fKeepMediumLockList */);
|
---|
4819 | rc = pTask->rc();
|
---|
4820 | AssertComRC(rc);
|
---|
4821 | if (FAILED(rc))
|
---|
4822 | throw rc;
|
---|
4823 |
|
---|
4824 | /* register a task (it will deregister itself when done) */
|
---|
4825 | ++m->numCreateDiffTasks;
|
---|
4826 | Assert(m->numCreateDiffTasks != 0); /* overflow? */
|
---|
4827 |
|
---|
4828 | aTarget->m->state = MediumState_Creating;
|
---|
4829 | }
|
---|
4830 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4831 |
|
---|
4832 | if (SUCCEEDED(rc))
|
---|
4833 | {
|
---|
4834 | if (aWait)
|
---|
4835 | {
|
---|
4836 | rc = pTask->runNow();
|
---|
4837 |
|
---|
4838 | delete pTask;
|
---|
4839 | }
|
---|
4840 | else
|
---|
4841 | rc = pTask->createThread();
|
---|
4842 |
|
---|
4843 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
4844 | *aProgress = pProgress;
|
---|
4845 | }
|
---|
4846 | else if (pTask != NULL)
|
---|
4847 | delete pTask;
|
---|
4848 |
|
---|
4849 | return rc;
|
---|
4850 | }
|
---|
4851 |
|
---|
4852 | /**
|
---|
4853 | * Returns a preferred format for differencing media.
|
---|
4854 | */
|
---|
4855 | Utf8Str Medium::i_getPreferredDiffFormat()
|
---|
4856 | {
|
---|
4857 | AutoCaller autoCaller(this);
|
---|
4858 | AssertComRCReturn(autoCaller.rc(), Utf8Str::Empty);
|
---|
4859 |
|
---|
4860 | /* check that our own format supports diffs */
|
---|
4861 | if (!(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_Differencing))
|
---|
4862 | {
|
---|
4863 | /* use the default format if not */
|
---|
4864 | Utf8Str tmp;
|
---|
4865 | m->pVirtualBox->i_getDefaultHardDiskFormat(tmp);
|
---|
4866 | return tmp;
|
---|
4867 | }
|
---|
4868 |
|
---|
4869 | /* m->strFormat is const, no need to lock */
|
---|
4870 | return m->strFormat;
|
---|
4871 | }
|
---|
4872 |
|
---|
4873 | /**
|
---|
4874 | * Returns a preferred variant for differencing media.
|
---|
4875 | */
|
---|
4876 | MediumVariant_T Medium::i_getPreferredDiffVariant()
|
---|
4877 | {
|
---|
4878 | AutoCaller autoCaller(this);
|
---|
4879 | AssertComRCReturn(autoCaller.rc(), MediumVariant_Standard);
|
---|
4880 |
|
---|
4881 | /* check that our own format supports diffs */
|
---|
4882 | if (!(m->formatObj->i_getCapabilities() & MediumFormatCapabilities_Differencing))
|
---|
4883 | return MediumVariant_Standard;
|
---|
4884 |
|
---|
4885 | /* m->variant is const, no need to lock */
|
---|
4886 | ULONG mediumVariantFlags = (ULONG)m->variant;
|
---|
4887 | mediumVariantFlags &= ~(MediumVariant_Fixed | MediumVariant_VmdkStreamOptimized);
|
---|
4888 | mediumVariantFlags |= MediumVariant_Diff;
|
---|
4889 | return (MediumVariant_T)mediumVariantFlags;
|
---|
4890 | }
|
---|
4891 |
|
---|
4892 | /**
|
---|
4893 | * Implementation for the public Medium::Close() with the exception of calling
|
---|
4894 | * VirtualBox::saveRegistries(), in case someone wants to call this for several
|
---|
4895 | * media.
|
---|
4896 | *
|
---|
4897 | * After this returns with success, uninit() has been called on the medium, and
|
---|
4898 | * the object is no longer usable ("not ready" state).
|
---|
4899 | *
|
---|
4900 | * @param autoCaller AutoCaller instance which must have been created on the caller's
|
---|
4901 | * stack for this medium. This gets released hereupon
|
---|
4902 | * which the Medium instance gets uninitialized.
|
---|
4903 | * @return
|
---|
4904 | */
|
---|
4905 | HRESULT Medium::i_close(AutoCaller &autoCaller)
|
---|
4906 | {
|
---|
4907 | // must temporarily drop the caller, need the tree lock first
|
---|
4908 | autoCaller.release();
|
---|
4909 |
|
---|
4910 | // we're accessing parent/child and backrefs, so lock the tree first, then ourselves
|
---|
4911 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
4912 | this->lockHandle()
|
---|
4913 | COMMA_LOCKVAL_SRC_POS);
|
---|
4914 |
|
---|
4915 | autoCaller.add();
|
---|
4916 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
4917 |
|
---|
4918 | LogFlowFunc(("ENTER for %s\n", i_getLocationFull().c_str()));
|
---|
4919 |
|
---|
4920 | bool wasCreated = true;
|
---|
4921 |
|
---|
4922 | switch (m->state)
|
---|
4923 | {
|
---|
4924 | case MediumState_NotCreated:
|
---|
4925 | wasCreated = false;
|
---|
4926 | break;
|
---|
4927 | case MediumState_Created:
|
---|
4928 | case MediumState_Inaccessible:
|
---|
4929 | break;
|
---|
4930 | default:
|
---|
4931 | return i_setStateError();
|
---|
4932 | }
|
---|
4933 |
|
---|
4934 | if (m->backRefs.size() != 0)
|
---|
4935 | return setError(VBOX_E_OBJECT_IN_USE,
|
---|
4936 | tr("Medium '%s' cannot be closed because it is still attached to %d virtual machines"),
|
---|
4937 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
4938 |
|
---|
4939 | // perform extra media-dependent close checks
|
---|
4940 | HRESULT rc = i_canClose();
|
---|
4941 | if (FAILED(rc)) return rc;
|
---|
4942 |
|
---|
4943 | m->fClosing = true;
|
---|
4944 |
|
---|
4945 | if (wasCreated)
|
---|
4946 | {
|
---|
4947 | // remove from the list of known media before performing actual
|
---|
4948 | // uninitialization (to keep the media registry consistent on
|
---|
4949 | // failure to do so)
|
---|
4950 | rc = i_unregisterWithVirtualBox();
|
---|
4951 | if (FAILED(rc)) return rc;
|
---|
4952 |
|
---|
4953 | multilock.release();
|
---|
4954 | // Release the AutoCaller now, as otherwise uninit() will simply hang.
|
---|
4955 | // Needs to be done before mark the registries as modified and saving
|
---|
4956 | // the registry, as otherwise there may be a deadlock with someone else
|
---|
4957 | // closing this object while we're in i_saveModifiedRegistries(), which
|
---|
4958 | // needs the media tree lock, which the other thread holds until after
|
---|
4959 | // uninit() below.
|
---|
4960 | autoCaller.release();
|
---|
4961 | i_markRegistriesModified();
|
---|
4962 | m->pVirtualBox->i_saveModifiedRegistries();
|
---|
4963 | }
|
---|
4964 | else
|
---|
4965 | {
|
---|
4966 | multilock.release();
|
---|
4967 | // release the AutoCaller, as otherwise uninit() will simply hang
|
---|
4968 | autoCaller.release();
|
---|
4969 | }
|
---|
4970 |
|
---|
4971 | // Keep the locks held until after uninit, as otherwise the consistency
|
---|
4972 | // of the medium tree cannot be guaranteed.
|
---|
4973 | uninit();
|
---|
4974 |
|
---|
4975 | LogFlowFuncLeave();
|
---|
4976 |
|
---|
4977 | return rc;
|
---|
4978 | }
|
---|
4979 |
|
---|
4980 | /**
|
---|
4981 | * Deletes the medium storage unit.
|
---|
4982 | *
|
---|
4983 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
4984 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
4985 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
4986 | * progress object is created/used at all.
|
---|
4987 | *
|
---|
4988 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
4989 | * delete operation asynchronously and will return immediately. Otherwise, it
|
---|
4990 | * will perform the operation on the calling thread and will not return to the
|
---|
4991 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
4992 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
4993 | *
|
---|
4994 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
4995 | * completion.
|
---|
4996 | * @param aWait @c true if this method should block instead of creating
|
---|
4997 | * an asynchronous thread.
|
---|
4998 | *
|
---|
4999 | * @note Locks mVirtualBox and this object for writing. Locks medium tree for
|
---|
5000 | * writing.
|
---|
5001 | */
|
---|
5002 | HRESULT Medium::i_deleteStorage(ComObjPtr<Progress> *aProgress,
|
---|
5003 | bool aWait)
|
---|
5004 | {
|
---|
5005 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
5006 | * to lock order violations, it probably causes lock order issues related
|
---|
5007 | * to the AutoCaller usage. */
|
---|
5008 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
5009 |
|
---|
5010 | AutoCaller autoCaller(this);
|
---|
5011 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5012 |
|
---|
5013 | HRESULT rc = S_OK;
|
---|
5014 | ComObjPtr<Progress> pProgress;
|
---|
5015 | Medium::Task *pTask = NULL;
|
---|
5016 |
|
---|
5017 | try
|
---|
5018 | {
|
---|
5019 | /* we're accessing the media tree, and canClose() needs it too */
|
---|
5020 | AutoMultiWriteLock2 multilock(&m->pVirtualBox->i_getMediaTreeLockHandle(),
|
---|
5021 | this->lockHandle()
|
---|
5022 | COMMA_LOCKVAL_SRC_POS);
|
---|
5023 | LogFlowThisFunc(("aWait=%RTbool locationFull=%s\n", aWait, i_getLocationFull().c_str() ));
|
---|
5024 |
|
---|
5025 | if ( !(m->formatObj->i_getCapabilities() & ( MediumFormatCapabilities_CreateDynamic
|
---|
5026 | | MediumFormatCapabilities_CreateFixed)))
|
---|
5027 | throw setError(VBOX_E_NOT_SUPPORTED,
|
---|
5028 | tr("Medium format '%s' does not support storage deletion"),
|
---|
5029 | m->strFormat.c_str());
|
---|
5030 |
|
---|
5031 | /* Wait for a concurrently running Medium::i_queryInfo to complete. */
|
---|
5032 | /** @todo r=klaus would be great if this could be moved to the async
|
---|
5033 | * part of the operation as it can take quite a while */
|
---|
5034 | if (m->queryInfoRunning)
|
---|
5035 | {
|
---|
5036 | while (m->queryInfoRunning)
|
---|
5037 | {
|
---|
5038 | multilock.release();
|
---|
5039 | /* Must not hold the media tree lock or the object lock, as
|
---|
5040 | * Medium::i_queryInfo needs this lock and thus we would run
|
---|
5041 | * into a deadlock here. */
|
---|
5042 | Assert(!m->pVirtualBox->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
|
---|
5043 | Assert(!isWriteLockOnCurrentThread());
|
---|
5044 | {
|
---|
5045 | AutoReadLock qlock(m->queryInfoSem COMMA_LOCKVAL_SRC_POS);
|
---|
5046 | }
|
---|
5047 | multilock.acquire();
|
---|
5048 | }
|
---|
5049 | }
|
---|
5050 |
|
---|
5051 | /* Note that we are fine with Inaccessible state too: a) for symmetry
|
---|
5052 | * with create calls and b) because it doesn't really harm to try, if
|
---|
5053 | * it is really inaccessible, the delete operation will fail anyway.
|
---|
5054 | * Accepting Inaccessible state is especially important because all
|
---|
5055 | * registered media are initially Inaccessible upon VBoxSVC startup
|
---|
5056 | * until COMGETTER(RefreshState) is called. Accept Deleting state
|
---|
5057 | * because some callers need to put the medium in this state early
|
---|
5058 | * to prevent races. */
|
---|
5059 | switch (m->state)
|
---|
5060 | {
|
---|
5061 | case MediumState_Created:
|
---|
5062 | case MediumState_Deleting:
|
---|
5063 | case MediumState_Inaccessible:
|
---|
5064 | break;
|
---|
5065 | default:
|
---|
5066 | throw i_setStateError();
|
---|
5067 | }
|
---|
5068 |
|
---|
5069 | if (m->backRefs.size() != 0)
|
---|
5070 | {
|
---|
5071 | Utf8Str strMachines;
|
---|
5072 | for (BackRefList::const_iterator it = m->backRefs.begin();
|
---|
5073 | it != m->backRefs.end();
|
---|
5074 | ++it)
|
---|
5075 | {
|
---|
5076 | const BackRef &b = *it;
|
---|
5077 | if (strMachines.length())
|
---|
5078 | strMachines.append(", ");
|
---|
5079 | strMachines.append(b.machineId.toString().c_str());
|
---|
5080 | }
|
---|
5081 | #ifdef DEBUG
|
---|
5082 | i_dumpBackRefs();
|
---|
5083 | #endif
|
---|
5084 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5085 | tr("Cannot delete storage: medium '%s' is still attached to the following %d virtual machine(s): %s"),
|
---|
5086 | m->strLocationFull.c_str(),
|
---|
5087 | m->backRefs.size(),
|
---|
5088 | strMachines.c_str());
|
---|
5089 | }
|
---|
5090 |
|
---|
5091 | rc = i_canClose();
|
---|
5092 | if (FAILED(rc))
|
---|
5093 | throw rc;
|
---|
5094 |
|
---|
5095 | /* go to Deleting state, so that the medium is not actually locked */
|
---|
5096 | if (m->state != MediumState_Deleting)
|
---|
5097 | {
|
---|
5098 | rc = i_markForDeletion();
|
---|
5099 | if (FAILED(rc))
|
---|
5100 | throw rc;
|
---|
5101 | }
|
---|
5102 |
|
---|
5103 | /* Build the medium lock list. */
|
---|
5104 | MediumLockList *pMediumLockList(new MediumLockList());
|
---|
5105 | multilock.release();
|
---|
5106 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5107 | this /* pToLockWrite */,
|
---|
5108 | false /* fMediumLockWriteAll */,
|
---|
5109 | NULL,
|
---|
5110 | *pMediumLockList);
|
---|
5111 | multilock.acquire();
|
---|
5112 | if (FAILED(rc))
|
---|
5113 | {
|
---|
5114 | delete pMediumLockList;
|
---|
5115 | throw rc;
|
---|
5116 | }
|
---|
5117 |
|
---|
5118 | multilock.release();
|
---|
5119 | rc = pMediumLockList->Lock();
|
---|
5120 | multilock.acquire();
|
---|
5121 | if (FAILED(rc))
|
---|
5122 | {
|
---|
5123 | delete pMediumLockList;
|
---|
5124 | throw setError(rc,
|
---|
5125 | tr("Failed to lock media when deleting '%s'"),
|
---|
5126 | i_getLocationFull().c_str());
|
---|
5127 | }
|
---|
5128 |
|
---|
5129 | /* try to remove from the list of known media before performing
|
---|
5130 | * actual deletion (we favor the consistency of the media registry
|
---|
5131 | * which would have been broken if unregisterWithVirtualBox() failed
|
---|
5132 | * after we successfully deleted the storage) */
|
---|
5133 | rc = i_unregisterWithVirtualBox();
|
---|
5134 | if (FAILED(rc))
|
---|
5135 | throw rc;
|
---|
5136 | // no longer need lock
|
---|
5137 | multilock.release();
|
---|
5138 | i_markRegistriesModified();
|
---|
5139 |
|
---|
5140 | if (aProgress != NULL)
|
---|
5141 | {
|
---|
5142 | /* use the existing progress object... */
|
---|
5143 | pProgress = *aProgress;
|
---|
5144 |
|
---|
5145 | /* ...but create a new one if it is null */
|
---|
5146 | if (pProgress.isNull())
|
---|
5147 | {
|
---|
5148 | pProgress.createObject();
|
---|
5149 | rc = pProgress->init(m->pVirtualBox,
|
---|
5150 | static_cast<IMedium*>(this),
|
---|
5151 | BstrFmt(tr("Deleting medium storage unit '%s'"), m->strLocationFull.c_str()).raw(),
|
---|
5152 | FALSE /* aCancelable */);
|
---|
5153 | if (FAILED(rc))
|
---|
5154 | throw rc;
|
---|
5155 | }
|
---|
5156 | }
|
---|
5157 |
|
---|
5158 | /* setup task object to carry out the operation sync/async */
|
---|
5159 | pTask = new Medium::DeleteTask(this, pProgress, pMediumLockList);
|
---|
5160 | rc = pTask->rc();
|
---|
5161 | AssertComRC(rc);
|
---|
5162 | if (FAILED(rc))
|
---|
5163 | throw rc;
|
---|
5164 | }
|
---|
5165 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5166 |
|
---|
5167 | if (SUCCEEDED(rc))
|
---|
5168 | {
|
---|
5169 | if (aWait)
|
---|
5170 | {
|
---|
5171 | rc = pTask->runNow();
|
---|
5172 |
|
---|
5173 | delete pTask;
|
---|
5174 | }
|
---|
5175 | else
|
---|
5176 | rc = pTask->createThread();
|
---|
5177 |
|
---|
5178 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
5179 | *aProgress = pProgress;
|
---|
5180 |
|
---|
5181 | }
|
---|
5182 | else
|
---|
5183 | {
|
---|
5184 | if (pTask)
|
---|
5185 | delete pTask;
|
---|
5186 |
|
---|
5187 | /* Undo deleting state if necessary. */
|
---|
5188 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5189 | /* Make sure that any error signalled by unmarkForDeletion() is not
|
---|
5190 | * ending up in the error list (if the caller uses MultiResult). It
|
---|
5191 | * usually is spurious, as in most cases the medium hasn't been marked
|
---|
5192 | * for deletion when the error was thrown above. */
|
---|
5193 | ErrorInfoKeeper eik;
|
---|
5194 | i_unmarkForDeletion();
|
---|
5195 | }
|
---|
5196 |
|
---|
5197 | return rc;
|
---|
5198 | }
|
---|
5199 |
|
---|
5200 | /**
|
---|
5201 | * Mark a medium for deletion.
|
---|
5202 | *
|
---|
5203 | * @note Caller must hold the write lock on this medium!
|
---|
5204 | */
|
---|
5205 | HRESULT Medium::i_markForDeletion()
|
---|
5206 | {
|
---|
5207 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5208 | switch (m->state)
|
---|
5209 | {
|
---|
5210 | case MediumState_Created:
|
---|
5211 | case MediumState_Inaccessible:
|
---|
5212 | m->preLockState = m->state;
|
---|
5213 | m->state = MediumState_Deleting;
|
---|
5214 | return S_OK;
|
---|
5215 | default:
|
---|
5216 | return i_setStateError();
|
---|
5217 | }
|
---|
5218 | }
|
---|
5219 |
|
---|
5220 | /**
|
---|
5221 | * Removes the "mark for deletion".
|
---|
5222 | *
|
---|
5223 | * @note Caller must hold the write lock on this medium!
|
---|
5224 | */
|
---|
5225 | HRESULT Medium::i_unmarkForDeletion()
|
---|
5226 | {
|
---|
5227 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5228 | switch (m->state)
|
---|
5229 | {
|
---|
5230 | case MediumState_Deleting:
|
---|
5231 | m->state = m->preLockState;
|
---|
5232 | return S_OK;
|
---|
5233 | default:
|
---|
5234 | return i_setStateError();
|
---|
5235 | }
|
---|
5236 | }
|
---|
5237 |
|
---|
5238 | /**
|
---|
5239 | * Mark a medium for deletion which is in locked state.
|
---|
5240 | *
|
---|
5241 | * @note Caller must hold the write lock on this medium!
|
---|
5242 | */
|
---|
5243 | HRESULT Medium::i_markLockedForDeletion()
|
---|
5244 | {
|
---|
5245 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5246 | if ( ( m->state == MediumState_LockedRead
|
---|
5247 | || m->state == MediumState_LockedWrite)
|
---|
5248 | && m->preLockState == MediumState_Created)
|
---|
5249 | {
|
---|
5250 | m->preLockState = MediumState_Deleting;
|
---|
5251 | return S_OK;
|
---|
5252 | }
|
---|
5253 | else
|
---|
5254 | return i_setStateError();
|
---|
5255 | }
|
---|
5256 |
|
---|
5257 | /**
|
---|
5258 | * Removes the "mark for deletion" for a medium in locked state.
|
---|
5259 | *
|
---|
5260 | * @note Caller must hold the write lock on this medium!
|
---|
5261 | */
|
---|
5262 | HRESULT Medium::i_unmarkLockedForDeletion()
|
---|
5263 | {
|
---|
5264 | ComAssertRet(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
5265 | if ( ( m->state == MediumState_LockedRead
|
---|
5266 | || m->state == MediumState_LockedWrite)
|
---|
5267 | && m->preLockState == MediumState_Deleting)
|
---|
5268 | {
|
---|
5269 | m->preLockState = MediumState_Created;
|
---|
5270 | return S_OK;
|
---|
5271 | }
|
---|
5272 | else
|
---|
5273 | return i_setStateError();
|
---|
5274 | }
|
---|
5275 |
|
---|
5276 | /**
|
---|
5277 | * Queries the preferred merge direction from this to the other medium, i.e.
|
---|
5278 | * the one which requires the least amount of I/O and therefore time and
|
---|
5279 | * disk consumption.
|
---|
5280 | *
|
---|
5281 | * @returns Status code.
|
---|
5282 | * @retval E_FAIL in case determining the merge direction fails for some reason,
|
---|
5283 | * for example if getting the size of the media fails. There is no
|
---|
5284 | * error set though and the caller is free to continue to find out
|
---|
5285 | * what was going wrong later. Leaves fMergeForward unset.
|
---|
5286 | * @retval VBOX_E_INVALID_OBJECT_STATE if both media are not related to each other
|
---|
5287 | * An error is set.
|
---|
5288 | * @param pOther The other medium to merge with.
|
---|
5289 | * @param fMergeForward Resulting preferred merge direction (out).
|
---|
5290 | */
|
---|
5291 | HRESULT Medium::i_queryPreferredMergeDirection(const ComObjPtr<Medium> &pOther,
|
---|
5292 | bool &fMergeForward)
|
---|
5293 | {
|
---|
5294 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
5295 | * to lock order violations, it probably causes lock order issues related
|
---|
5296 | * to the AutoCaller usage. Likewise the code using this method seems
|
---|
5297 | * problematic. */
|
---|
5298 | AssertReturn(pOther != NULL, E_FAIL);
|
---|
5299 | AssertReturn(pOther != this, E_FAIL);
|
---|
5300 |
|
---|
5301 | AutoCaller autoCaller(this);
|
---|
5302 | AssertComRCReturnRC(autoCaller.rc());
|
---|
5303 |
|
---|
5304 | AutoCaller otherCaller(pOther);
|
---|
5305 | AssertComRCReturnRC(otherCaller.rc());
|
---|
5306 |
|
---|
5307 | HRESULT rc = S_OK;
|
---|
5308 | bool fThisParent = false; /**<< Flag whether this medium is the parent of pOther. */
|
---|
5309 |
|
---|
5310 | try
|
---|
5311 | {
|
---|
5312 | // locking: we need the tree lock first because we access parent pointers
|
---|
5313 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5314 |
|
---|
5315 | /* more sanity checking and figuring out the current merge direction */
|
---|
5316 | ComObjPtr<Medium> pMedium = i_getParent();
|
---|
5317 | while (!pMedium.isNull() && pMedium != pOther)
|
---|
5318 | pMedium = pMedium->i_getParent();
|
---|
5319 | if (pMedium == pOther)
|
---|
5320 | fThisParent = false;
|
---|
5321 | else
|
---|
5322 | {
|
---|
5323 | pMedium = pOther->i_getParent();
|
---|
5324 | while (!pMedium.isNull() && pMedium != this)
|
---|
5325 | pMedium = pMedium->i_getParent();
|
---|
5326 | if (pMedium == this)
|
---|
5327 | fThisParent = true;
|
---|
5328 | else
|
---|
5329 | {
|
---|
5330 | Utf8Str tgtLoc;
|
---|
5331 | {
|
---|
5332 | AutoReadLock alock(pOther COMMA_LOCKVAL_SRC_POS);
|
---|
5333 | tgtLoc = pOther->i_getLocationFull();
|
---|
5334 | }
|
---|
5335 |
|
---|
5336 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5337 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5338 | tr("Media '%s' and '%s' are unrelated"),
|
---|
5339 | m->strLocationFull.c_str(), tgtLoc.c_str());
|
---|
5340 | }
|
---|
5341 | }
|
---|
5342 |
|
---|
5343 | /*
|
---|
5344 | * Figure out the preferred merge direction. The current way is to
|
---|
5345 | * get the current sizes of file based images and select the merge
|
---|
5346 | * direction depending on the size.
|
---|
5347 | *
|
---|
5348 | * Can't use the VD API to get current size here as the media might
|
---|
5349 | * be write locked by a running VM. Resort to RTFileQuerySize().
|
---|
5350 | */
|
---|
5351 | int vrc = VINF_SUCCESS;
|
---|
5352 | uint64_t cbMediumThis = 0;
|
---|
5353 | uint64_t cbMediumOther = 0;
|
---|
5354 |
|
---|
5355 | if (i_isMediumFormatFile() && pOther->i_isMediumFormatFile())
|
---|
5356 | {
|
---|
5357 | vrc = RTFileQuerySize(this->i_getLocationFull().c_str(), &cbMediumThis);
|
---|
5358 | if (RT_SUCCESS(vrc))
|
---|
5359 | {
|
---|
5360 | vrc = RTFileQuerySize(pOther->i_getLocationFull().c_str(),
|
---|
5361 | &cbMediumOther);
|
---|
5362 | }
|
---|
5363 |
|
---|
5364 | if (RT_FAILURE(vrc))
|
---|
5365 | rc = E_FAIL;
|
---|
5366 | else
|
---|
5367 | {
|
---|
5368 | /*
|
---|
5369 | * Check which merge direction might be more optimal.
|
---|
5370 | * This method is not bullet proof of course as there might
|
---|
5371 | * be overlapping blocks in the images so the file size is
|
---|
5372 | * not the best indicator but it is good enough for our purpose
|
---|
5373 | * and everything else is too complicated, especially when the
|
---|
5374 | * media are used by a running VM.
|
---|
5375 | */
|
---|
5376 | bool fMergeIntoThis = cbMediumThis > cbMediumOther;
|
---|
5377 | fMergeForward = fMergeIntoThis != fThisParent;
|
---|
5378 | }
|
---|
5379 | }
|
---|
5380 | }
|
---|
5381 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5382 |
|
---|
5383 | return rc;
|
---|
5384 | }
|
---|
5385 |
|
---|
5386 | /**
|
---|
5387 | * Prepares this (source) medium, target medium and all intermediate media
|
---|
5388 | * for the merge operation.
|
---|
5389 | *
|
---|
5390 | * This method is to be called prior to calling the #mergeTo() to perform
|
---|
5391 | * necessary consistency checks and place involved media to appropriate
|
---|
5392 | * states. If #mergeTo() is not called or fails, the state modifications
|
---|
5393 | * performed by this method must be undone by #i_cancelMergeTo().
|
---|
5394 | *
|
---|
5395 | * See #mergeTo() for more information about merging.
|
---|
5396 | *
|
---|
5397 | * @param pTarget Target medium.
|
---|
5398 | * @param aMachineId Allowed machine attachment. NULL means do not check.
|
---|
5399 | * @param aSnapshotId Allowed snapshot attachment. NULL or empty UUID means
|
---|
5400 | * do not check.
|
---|
5401 | * @param fLockMedia Flag whether to lock the medium lock list or not.
|
---|
5402 | * If set to false and the medium lock list locking fails
|
---|
5403 | * later you must call #i_cancelMergeTo().
|
---|
5404 | * @param fMergeForward Resulting merge direction (out).
|
---|
5405 | * @param pParentForTarget New parent for target medium after merge (out).
|
---|
5406 | * @param aChildrenToReparent Medium lock list containing all children of the
|
---|
5407 | * source which will have to be reparented to the target
|
---|
5408 | * after merge (out).
|
---|
5409 | * @param aMediumLockList Medium locking information (out).
|
---|
5410 | *
|
---|
5411 | * @note Locks medium tree for reading. Locks this object, aTarget and all
|
---|
5412 | * intermediate media for writing.
|
---|
5413 | */
|
---|
5414 | HRESULT Medium::i_prepareMergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
5415 | const Guid *aMachineId,
|
---|
5416 | const Guid *aSnapshotId,
|
---|
5417 | bool fLockMedia,
|
---|
5418 | bool &fMergeForward,
|
---|
5419 | ComObjPtr<Medium> &pParentForTarget,
|
---|
5420 | MediumLockList * &aChildrenToReparent,
|
---|
5421 | MediumLockList * &aMediumLockList)
|
---|
5422 | {
|
---|
5423 | /** @todo r=klaus The code below needs to be double checked with regard
|
---|
5424 | * to lock order violations, it probably causes lock order issues related
|
---|
5425 | * to the AutoCaller usage. Likewise the code using this method seems
|
---|
5426 | * problematic. */
|
---|
5427 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
5428 | AssertReturn(pTarget != this, E_FAIL);
|
---|
5429 |
|
---|
5430 | AutoCaller autoCaller(this);
|
---|
5431 | AssertComRCReturnRC(autoCaller.rc());
|
---|
5432 |
|
---|
5433 | AutoCaller targetCaller(pTarget);
|
---|
5434 | AssertComRCReturnRC(targetCaller.rc());
|
---|
5435 |
|
---|
5436 | HRESULT rc = S_OK;
|
---|
5437 | fMergeForward = false;
|
---|
5438 | pParentForTarget.setNull();
|
---|
5439 | Assert(aChildrenToReparent == NULL);
|
---|
5440 | aChildrenToReparent = NULL;
|
---|
5441 | Assert(aMediumLockList == NULL);
|
---|
5442 | aMediumLockList = NULL;
|
---|
5443 |
|
---|
5444 | try
|
---|
5445 | {
|
---|
5446 | // locking: we need the tree lock first because we access parent pointers
|
---|
5447 | AutoWriteLock treeLock(m->pVirtualBox->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
|
---|
5448 |
|
---|
5449 | /* more sanity checking and figuring out the merge direction */
|
---|
5450 | ComObjPtr<Medium> pMedium = i_getParent();
|
---|
5451 | while (!pMedium.isNull() && pMedium != pTarget)
|
---|
5452 | pMedium = pMedium->i_getParent();
|
---|
5453 | if (pMedium == pTarget)
|
---|
5454 | fMergeForward = false;
|
---|
5455 | else
|
---|
5456 | {
|
---|
5457 | pMedium = pTarget->i_getParent();
|
---|
5458 | while (!pMedium.isNull() && pMedium != this)
|
---|
5459 | pMedium = pMedium->i_getParent();
|
---|
5460 | if (pMedium == this)
|
---|
5461 | fMergeForward = true;
|
---|
5462 | else
|
---|
5463 | {
|
---|
5464 | Utf8Str tgtLoc;
|
---|
5465 | {
|
---|
5466 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5467 | tgtLoc = pTarget->i_getLocationFull();
|
---|
5468 | }
|
---|
5469 |
|
---|
5470 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5471 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5472 | tr("Media '%s' and '%s' are unrelated"),
|
---|
5473 | m->strLocationFull.c_str(), tgtLoc.c_str());
|
---|
5474 | }
|
---|
5475 | }
|
---|
5476 |
|
---|
5477 | /* Build the lock list. */
|
---|
5478 | aMediumLockList = new MediumLockList();
|
---|
5479 | treeLock.release();
|
---|
5480 | if (fMergeForward)
|
---|
5481 | rc = pTarget->i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5482 | pTarget /* pToLockWrite */,
|
---|
5483 | false /* fMediumLockWriteAll */,
|
---|
5484 | NULL,
|
---|
5485 | *aMediumLockList);
|
---|
5486 | else
|
---|
5487 | rc = i_createMediumLockList(true /* fFailIfInaccessible */,
|
---|
5488 | pTarget /* pToLockWrite */,
|
---|
5489 | false /* fMediumLockWriteAll */,
|
---|
5490 | NULL,
|
---|
5491 | *aMediumLockList);
|
---|
5492 | treeLock.acquire();
|
---|
5493 | if (FAILED(rc))
|
---|
5494 | throw rc;
|
---|
5495 |
|
---|
5496 | /* Sanity checking, must be after lock list creation as it depends on
|
---|
5497 | * valid medium states. The medium objects must be accessible. Only
|
---|
5498 | * do this if immediate locking is requested, otherwise it fails when
|
---|
5499 | * we construct a medium lock list for an already running VM. Snapshot
|
---|
5500 | * deletion uses this to simplify its life. */
|
---|
5501 | if (fLockMedia)
|
---|
5502 | {
|
---|
5503 | {
|
---|
5504 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5505 | if (m->state != MediumState_Created)
|
---|
5506 | throw i_setStateError();
|
---|
5507 | }
|
---|
5508 | {
|
---|
5509 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5510 | if (pTarget->m->state != MediumState_Created)
|
---|
5511 | throw pTarget->i_setStateError();
|
---|
5512 | }
|
---|
5513 | }
|
---|
5514 |
|
---|
5515 | /* check medium attachment and other sanity conditions */
|
---|
5516 | if (fMergeForward)
|
---|
5517 | {
|
---|
5518 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5519 | if (i_getChildren().size() > 1)
|
---|
5520 | {
|
---|
5521 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5522 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5523 | m->strLocationFull.c_str(), i_getChildren().size());
|
---|
5524 | }
|
---|
5525 | /* One backreference is only allowed if the machine ID is not empty
|
---|
5526 | * and it matches the machine the medium is attached to (including
|
---|
5527 | * the snapshot ID if not empty). */
|
---|
5528 | if ( m->backRefs.size() != 0
|
---|
5529 | && ( !aMachineId
|
---|
5530 | || m->backRefs.size() != 1
|
---|
5531 | || aMachineId->isZero()
|
---|
5532 | || *i_getFirstMachineBackrefId() != *aMachineId
|
---|
5533 | || ( (!aSnapshotId || !aSnapshotId->isZero())
|
---|
5534 | && *i_getFirstMachineBackrefSnapshotId() != *aSnapshotId)))
|
---|
5535 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5536 | tr("Medium '%s' is attached to %d virtual machines"),
|
---|
5537 | m->strLocationFull.c_str(), m->backRefs.size());
|
---|
5538 | if (m->type == MediumType_Immutable)
|
---|
5539 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5540 | tr("Medium '%s' is immutable"),
|
---|
5541 | m->strLocationFull.c_str());
|
---|
5542 | if (m->type == MediumType_MultiAttach)
|
---|
5543 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5544 | tr("Medium '%s' is multi-attach"),
|
---|
5545 | m->strLocationFull.c_str());
|
---|
5546 | }
|
---|
5547 | else
|
---|
5548 | {
|
---|
5549 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5550 | if (pTarget->i_getChildren().size() > 1)
|
---|
5551 | {
|
---|
5552 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5553 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5554 | pTarget->m->strLocationFull.c_str(),
|
---|
5555 | pTarget->i_getChildren().size());
|
---|
5556 | }
|
---|
5557 | if (pTarget->m->type == MediumType_Immutable)
|
---|
5558 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5559 | tr("Medium '%s' is immutable"),
|
---|
5560 | pTarget->m->strLocationFull.c_str());
|
---|
5561 | if (pTarget->m->type == MediumType_MultiAttach)
|
---|
5562 | throw setError(VBOX_E_INVALID_OBJECT_STATE,
|
---|
5563 | tr("Medium '%s' is multi-attach"),
|
---|
5564 | pTarget->m->strLocationFull.c_str());
|
---|
5565 | }
|
---|
5566 | ComObjPtr<Medium> pLast(fMergeForward ? (Medium *)pTarget : this);
|
---|
5567 | ComObjPtr<Medium> pLastIntermediate = pLast->i_getParent();
|
---|
5568 | for (pLast = pLastIntermediate;
|
---|
5569 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
5570 | pLast = pLast->i_getParent())
|
---|
5571 | {
|
---|
5572 | AutoReadLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
5573 | if (pLast->i_getChildren().size() > 1)
|
---|
5574 | {
|
---|
5575 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5576 | tr("Medium '%s' involved in the merge operation has more than one child medium (%d)"),
|
---|
5577 | pLast->m->strLocationFull.c_str(),
|
---|
5578 | pLast->i_getChildren().size());
|
---|
5579 | }
|
---|
5580 | if (pLast->m->backRefs.size() != 0)
|
---|
5581 | throw setError(VBOX_E_OBJECT_IN_USE,
|
---|
5582 | tr("Medium '%s' is attached to %d virtual machines"),
|
---|
5583 | pLast->m->strLocationFull.c_str(),
|
---|
5584 | pLast->m->backRefs.size());
|
---|
5585 |
|
---|
5586 | }
|
---|
5587 |
|
---|
5588 | /* Update medium states appropriately */
|
---|
5589 | {
|
---|
5590 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5591 |
|
---|
5592 | if (m->state == MediumState_Created)
|
---|
5593 | {
|
---|
5594 | rc = i_markForDeletion();
|
---|
5595 | if (FAILED(rc))
|
---|
5596 | throw rc;
|
---|
5597 | }
|
---|
5598 | else
|
---|
5599 | {
|
---|
5600 | if (fLockMedia)
|
---|
5601 | throw i_setStateError();
|
---|
5602 | else if ( m->state == MediumState_LockedWrite
|
---|
5603 | || m->state == MediumState_LockedRead)
|
---|
5604 | {
|
---|
5605 | /* Either mark it for deletion in locked state or allow
|
---|
5606 | * others to have done so. */
|
---|
5607 | if (m->preLockState == MediumState_Created)
|
---|
5608 | i_markLockedForDeletion();
|
---|
5609 | else if (m->preLockState != MediumState_Deleting)
|
---|
5610 | throw i_setStateError();
|
---|
5611 | }
|
---|
5612 | else
|
---|
5613 | throw i_setStateError();
|
---|
5614 | }
|
---|
5615 | }
|
---|
5616 |
|
---|
5617 | if (fMergeForward)
|
---|
5618 | {
|
---|
5619 | /* we will need parent to reparent target */
|
---|
5620 | pParentForTarget = i_getParent();
|
---|
5621 | }
|
---|
5622 | else
|
---|
5623 | {
|
---|
5624 | /* we will need to reparent children of the source */
|
---|
5625 | aChildrenToReparent = new MediumLockList();
|
---|
5626 | for (MediaList::const_iterator it = i_getChildren().begin();
|
---|
5627 | it != i_getChildren().end();
|
---|
5628 | ++it)
|
---|
5629 | {
|
---|
5630 | pMedium = *it;
|
---|
5631 | aChildrenToReparent->Append(pMedium, true /* fLockWrite */);
|
---|
5632 | }
|
---|
5633 | if (fLockMedia && aChildrenToReparent)
|
---|
5634 | {
|
---|
5635 | treeLock.release();
|
---|
5636 | rc = aChildrenToReparent->Lock();
|
---|
5637 | treeLock.acquire();
|
---|
5638 | if (FAILED(rc))
|
---|
5639 | throw rc;
|
---|
5640 | }
|
---|
5641 | }
|
---|
5642 | for (pLast = pLastIntermediate;
|
---|
5643 | !pLast.isNull() && pLast != pTarget && pLast != this;
|
---|
5644 | pLast = pLast->i_getParent())
|
---|
5645 | {
|
---|
5646 | AutoWriteLock alock(pLast COMMA_LOCKVAL_SRC_POS);
|
---|
5647 | if (pLast->m->state == MediumState_Created)
|
---|
5648 | {
|
---|
5649 | rc = pLast->i_markForDeletion();
|
---|
5650 | if (FAILED(rc))
|
---|
5651 | throw rc;
|
---|
5652 | }
|
---|
5653 | else
|
---|
5654 | throw pLast->i_setStateError();
|
---|
5655 | }
|
---|
5656 |
|
---|
5657 | /* Tweak the lock list in the backward merge case, as the target
|
---|
5658 | * isn't marked to be locked for writing yet. */
|
---|
5659 | if (!fMergeForward)
|
---|
5660 | {
|
---|
5661 | MediumLockList::Base::iterator lockListBegin =
|
---|
5662 | aMediumLockList->GetBegin();
|
---|
5663 | MediumLockList::Base::iterator lockListEnd =
|
---|
5664 | aMediumLockList->GetEnd();
|
---|
5665 | ++lockListEnd;
|
---|
5666 | for (MediumLockList::Base::iterator it = lockListBegin;
|
---|
5667 | it != lockListEnd;
|
---|
5668 | ++it)
|
---|
5669 | {
|
---|
5670 | MediumLock &mediumLock = *it;
|
---|
5671 | if (mediumLock.GetMedium() == pTarget)
|
---|
5672 | {
|
---|
5673 | HRESULT rc2 = mediumLock.UpdateLock(true);
|
---|
5674 | AssertComRC(rc2);
|
---|
5675 | break;
|
---|
5676 | }
|
---|
5677 | }
|
---|
5678 | }
|
---|
5679 |
|
---|
5680 | if (fLockMedia)
|
---|
5681 | {
|
---|
5682 | treeLock.release();
|
---|
5683 | rc = aMediumLockList->Lock();
|
---|
5684 | treeLock.acquire();
|
---|
5685 | if (FAILED(rc))
|
---|
5686 | {
|
---|
5687 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5688 | throw setError(rc,
|
---|
5689 | tr("Failed to lock media when merging to '%s'"),
|
---|
5690 | pTarget->i_getLocationFull().c_str());
|
---|
5691 | }
|
---|
5692 | }
|
---|
5693 | }
|
---|
5694 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5695 |
|
---|
5696 | if (FAILED(rc))
|
---|
5697 | {
|
---|
5698 | if (aMediumLockList)
|
---|
5699 | {
|
---|
5700 | delete aMediumLockList;
|
---|
5701 | aMediumLockList = NULL;
|
---|
5702 | }
|
---|
5703 | if (aChildrenToReparent)
|
---|
5704 | {
|
---|
5705 | delete aChildrenToReparent;
|
---|
5706 | aChildrenToReparent = NULL;
|
---|
5707 | }
|
---|
5708 | }
|
---|
5709 |
|
---|
5710 | return rc;
|
---|
5711 | }
|
---|
5712 |
|
---|
5713 | /**
|
---|
5714 | * Merges this medium to the specified medium which must be either its
|
---|
5715 | * direct ancestor or descendant.
|
---|
5716 | *
|
---|
5717 | * Given this medium is SOURCE and the specified medium is TARGET, we will
|
---|
5718 | * get two variants of the merge operation:
|
---|
5719 | *
|
---|
5720 | * forward merge
|
---|
5721 | * ------------------------->
|
---|
5722 | * [Extra] <- SOURCE <- Intermediate <- TARGET
|
---|
5723 | * Any Del Del LockWr
|
---|
5724 | *
|
---|
5725 | *
|
---|
5726 | * backward merge
|
---|
5727 | * <-------------------------
|
---|
5728 | * TARGET <- Intermediate <- SOURCE <- [Extra]
|
---|
5729 | * LockWr Del Del LockWr
|
---|
5730 | *
|
---|
5731 | * Each diagram shows the involved media on the media chain where
|
---|
5732 | * SOURCE and TARGET belong. Under each medium there is a state value which
|
---|
5733 | * the medium must have at a time of the mergeTo() call.
|
---|
5734 | *
|
---|
5735 | * The media in the square braces may be absent (e.g. when the forward
|
---|
5736 | * operation takes place and SOURCE is the base medium, or when the backward
|
---|
5737 | * merge operation takes place and TARGET is the last child in the chain) but if
|
---|
5738 | * they present they are involved too as shown.
|
---|
5739 | *
|
---|
5740 | * Neither the source medium nor intermediate media may be attached to
|
---|
5741 | * any VM directly or in the snapshot, otherwise this method will assert.
|
---|
5742 | *
|
---|
5743 | * The #i_prepareMergeTo() method must be called prior to this method to place
|
---|
5744 | * all involved to necessary states and perform other consistency checks.
|
---|
5745 | *
|
---|
5746 | * If @a aWait is @c true then this method will perform the operation on the
|
---|
5747 | * calling thread and will not return to the caller until the operation is
|
---|
5748 | * completed. When this method succeeds, all intermediate medium objects in
|
---|
5749 | * the chain will be uninitialized, the state of the target medium (and all
|
---|
5750 | * involved extra media) will be restored. @a aMediumLockList will not be
|
---|
5751 | * deleted, whether the operation is successful or not. The caller has to do
|
---|
5752 | * this if appropriate. Note that this (source) medium is not uninitialized
|
---|
5753 | * because of possible AutoCaller instances held by the caller of this method
|
---|
5754 | * on the current thread. It's therefore the responsibility of the caller to
|
---|
5755 | * call Medium::uninit() after releasing all callers.
|
---|
5756 | *
|
---|
5757 | * If @a aWait is @c false then this method will create a thread to perform the
|
---|
5758 | * operation asynchronously and will return immediately. If the operation
|
---|
5759 | * succeeds, the thread will uninitialize the source medium object and all
|
---|
5760 | * intermediate medium objects in the chain, reset the state of the target
|
---|
5761 | * medium (and all involved extra media) and delete @a aMediumLockList.
|
---|
5762 | * If the operation fails, the thread will only reset the states of all
|
---|
5763 | * involved media and delete @a aMediumLockList.
|
---|
5764 | *
|
---|
5765 | * When this method fails (regardless of the @a aWait mode), it is a caller's
|
---|
5766 | * responsibility to undo state changes and delete @a aMediumLockList using
|
---|
5767 | * #i_cancelMergeTo().
|
---|
5768 | *
|
---|
5769 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
5770 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
5771 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
5772 | * progress object is created/used at all. Note that @a aProgress cannot be
|
---|
5773 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
5774 | *
|
---|
5775 | * @param pTarget Target medium.
|
---|
5776 | * @param fMergeForward Merge direction.
|
---|
5777 | * @param pParentForTarget New parent for target medium after merge.
|
---|
5778 | * @param aChildrenToReparent List of children of the source which will have
|
---|
5779 | * to be reparented to the target after merge.
|
---|
5780 | * @param aMediumLockList Medium locking information.
|
---|
5781 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
5782 | * completion.
|
---|
5783 | * @param aWait @c true if this method should block instead of creating
|
---|
5784 | * an asynchronous thread.
|
---|
5785 | *
|
---|
5786 | * @note Locks the tree lock for writing. Locks the media from the chain
|
---|
5787 | * for writing.
|
---|
5788 | */
|
---|
5789 | HRESULT Medium::i_mergeTo(const ComObjPtr<Medium> &pTarget,
|
---|
5790 | bool fMergeForward,
|
---|
5791 | const ComObjPtr<Medium> &pParentForTarget,
|
---|
5792 | MediumLockList *aChildrenToReparent,
|
---|
5793 | MediumLockList *aMediumLockList,
|
---|
5794 | ComObjPtr<Progress> *aProgress,
|
---|
5795 | bool aWait)
|
---|
5796 | {
|
---|
5797 | AssertReturn(pTarget != NULL, E_FAIL);
|
---|
5798 | AssertReturn(pTarget != this, E_FAIL);
|
---|
5799 | AssertReturn(aMediumLockList != NULL, E_FAIL);
|
---|
5800 | AssertReturn(aProgress != NULL || aWait == true, E_FAIL);
|
---|
5801 |
|
---|
5802 | AutoCaller autoCaller(this);
|
---|
5803 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
5804 |
|
---|
5805 | AutoCaller targetCaller(pTarget);
|
---|
5806 | AssertComRCReturnRC(targetCaller.rc());
|
---|
5807 |
|
---|
5808 | HRESULT rc = S_OK;
|
---|
5809 | ComObjPtr<Progress> pProgress;
|
---|
5810 | Medium::Task *pTask = NULL;
|
---|
5811 |
|
---|
5812 | try
|
---|
5813 | {
|
---|
5814 | if (aProgress != NULL)
|
---|
5815 | {
|
---|
5816 | /* use the existing progress object... */
|
---|
5817 | pProgress = *aProgress;
|
---|
5818 |
|
---|
5819 | /* ...but create a new one if it is null */
|
---|
5820 | if (pProgress.isNull())
|
---|
5821 | {
|
---|
5822 | Utf8Str tgtName;
|
---|
5823 | {
|
---|
5824 | AutoReadLock alock(pTarget COMMA_LOCKVAL_SRC_POS);
|
---|
5825 | tgtName = pTarget->i_getName();
|
---|
5826 | }
|
---|
5827 |
|
---|
5828 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
5829 |
|
---|
5830 | pProgress.createObject();
|
---|
5831 | rc = pProgress->init(m->pVirtualBox,
|
---|
5832 | static_cast<IMedium*>(this),
|
---|
5833 | BstrFmt(tr("Merging medium '%s' to '%s'"),
|
---|
5834 | i_getName().c_str(),
|
---|
5835 | tgtName.c_str()).raw(),
|
---|
5836 | TRUE /* aCancelable */);
|
---|
5837 | if (FAILED(rc))
|
---|
5838 | throw rc;
|
---|
5839 | }
|
---|
5840 | }
|
---|
5841 |
|
---|
5842 | /* setup task object to carry out the operation sync/async */
|
---|
5843 | pTask = new Medium::MergeTask(this, pTarget, fMergeForward,
|
---|
5844 | pParentForTarget, aChildrenToReparent,
|
---|
5845 | pProgress, aMediumLockList,
|
---|
5846 | aWait /* fKeepMediumLockList */);
|
---|
5847 | rc = pTask->rc();
|
---|
5848 | AssertComRC(rc);
|
---|
5849 | if (FAILED(rc))
|
---|
5850 | throw rc;
|
---|
5851 | }
|
---|
5852 | catch (HRESULT aRC) { rc = aRC; }
|
---|
5853 |
|
---|
5854 | if (SUCCEEDED(rc))
|
---|
5855 | {
|
---|
5856 | if (aWait)
|
---|
5857 | {
|
---|
5858 | rc = pTask->runNow();
|
---|
5859 |
|
---|
5860 | delete pTask;
|
---|
5861 | }
|
---|
5862 | else
|
---|
5863 | rc = pTask->createThread();
|
---|
5864 |
|
---|
5865 | if (SUCCEEDED(rc) && aProgress != NULL)
|
---|
5866 | *aProgress = pProgress;
|
---|
5867 | }
|
---|
5868 | else if (pTask != NULL)
|
---|
5869 | delete pTask;
|
---|
5870 |
|
---|
5871 | return rc;
|
---|
5872 | }
|
---|
5873 |
|
---|
5874 | /**
|
---|
5875 | * Undoes what #i_prepareMergeTo() did. Must be called if #mergeTo() is not
|
---|
5876 | * called or fails. Frees memory occupied by @a aMediumLockList and unlocks
|
---|
5877 | * the medium objects in @a aChildrenToReparent.
|
---|
5878 | *
|
---|
5879 | * @param aChildrenToReparent List of children of the source which will have
|
---|
5880 | * to be reparented to the target after merge.
|
---|
5881 | * @param aMediumLockList Medium locking information.
|
---|
5882 | *
|
---|
5883 | * @note Locks the media from the chain for writing.
|
---|
5884 | */
|
---|
5885 | void Medium::i_cancelMergeTo(MediumLockList *aChildrenToReparent,
|
---|
5886 | MediumLockList *aMediumLockList)
|
---|
5887 | {
|
---|
5888 | AutoCaller autoCaller(this);
|
---|
5889 | AssertComRCReturnVoid(autoCaller.rc());
|
---|
5890 |
|
---|
5891 | AssertReturnVoid(aMediumLockList != NULL);
|
---|
5892 |
|
---|
5893 | /* Revert media marked for deletion to previous state. */
|
---|
5894 | HRESULT rc;
|
---|
5895 | MediumLockList::Base::const_iterator mediumListBegin =
|
---|
5896 | aMediumLockList->GetBegin();
|
---|
5897 | MediumLockList::Base::const_iterator mediumListEnd =
|
---|
5898 | aMediumLockList->GetEnd();
|
---|
5899 | for (MediumLockList::Base::const_iterator it = mediumListBegin;
|
---|
5900 | it != mediumListEnd;
|
---|
5901 | ++it)
|
---|
5902 | {
|
---|
5903 | const MediumLock &mediumLock = *it;
|
---|
5904 | const ComObjPtr<Medium> &pMedium = mediumLock.GetMedium();
|
---|
5905 | AutoWriteLock |
---|